If a shared library (so) is loaded using dlopen and utilizes thread-local storage (TLS), the memory will not be freed upon thread exit, resulting in
a memory leak.
This issue has been resolved in glibc-2.14. However, it remains unresolved in uClibc.
The following patch is based on the patch made in glibc and has been validated as effective on the x86-64 architecture.
diff --git a/libpthread/nptl/allocatestack.c b/libpthread/nptl/allocatestack.c
index
7ef884543..941ef22f0
100644
--- a/libpthread/nptl/allocatestack.c
+++ b/libpthread/nptl/allocatestack.c
@@ -24,6
+24,7
@@
#include
<unistd.h>
#include
<sys/mman.h>
#include
<sys/param.h>
+#include <dl-tls.h>
#include
<tls.h>
#include
<lowlevellock.h>
#include
<link.h>
@@ -241,6
+242,10
@@ get_cached_stack (size_t
*sizep,
void **memp)
/* Clear the DTV. */
dtv_t *dtv
= GET_DTV (TLS_TPADJ
(result));
+ for (size_t
cnt = 0; cnt <
dtv[-1].counter;
++cnt)
+ if (!
dtv[1
+ cnt].pointer.is_static
+ &&
dtv[1
+ cnt].pointer.val != TLS_DTV_UNALLOCATED)
+ free
(dtv[1
+ cnt].pointer.val);
memset
(dtv, '\0',
(dtv[-1].counter
+ 1) *
sizeof (dtv_t));
/* Re-initialize the TLS. */
diff --git a/libpthread/nptl/sysdeps/generic/dl-tls.c b/libpthread/nptl/sysdeps/generic/dl-tls.c
index
7d25e4706..7b7991be8
100644
--- a/libpthread/nptl/sysdeps/generic/dl-tls.c
+++ b/libpthread/nptl/sysdeps/generic/dl-tls.c
@@ -45,8
+45,6
@@
to allow dynamic loading of modules defining IE-model TLS data. */
# define
TLS_STATIC_SURPLUS 64
+ DL_NNS * 100
-/* Value used for dtv entries for which the allocation is delayed. */
-# define
TLS_DTV_UNALLOCATED ((void
*) -1l)
#ifndef
SHARED
extern
dtv_t static_dtv;
diff --git a/libpthread/nptl/sysdeps/x86_64/dl-tls.h b/libpthread/nptl/sysdeps/x86_64/dl-tls.h
index d6c338cda..5cac55f33
100644
--- a/libpthread/nptl/sysdeps/x86_64/dl-tls.h
+++ b/libpthread/nptl/sysdeps/x86_64/dl-tls.h
@@ -26,3
+26,6
@@ typedef
struct
extern
void *__tls_get_addr
(tls_index *ti);
+
+/* Value used for dtv entries for which the allocation is delayed. */
+#define
TLS_DTV_UNALLOCATED ((void
*) -1l)
Ref:
https://sourceware.org/bugzilla/show_bug.cgi?id=12650
How to reproduce: