From 39cf53f61b1457c071c599f354aad0322e2d07a7 Mon Sep 17 00:00:00 2001
From: Mingxiang Lu <lu.mingxiang@h3c.com>
Date: Tue, 29 Oct 2024 19:26:33 +0800
Subject: [PATCH] fix TLS memory leak with dlopen

Signed-off-by: Mingxiang Lu <lu.mingxiang@h3c.com>
---
 libpthread/nptl/allocatestack.c             | 5 +++++
 libpthread/nptl/sysdeps/alpha/dl-tls.h      | 3 +++
 libpthread/nptl/sysdeps/arc/dl-tls.h        | 3 +++
 libpthread/nptl/sysdeps/arm/dl-tls.h        | 3 +++
 libpthread/nptl/sysdeps/csky/dl-tls.h       | 3 +++
 libpthread/nptl/sysdeps/generic/dl-tls.c    | 2 --
 libpthread/nptl/sysdeps/m68k/dl-tls.h       | 3 +++
 libpthread/nptl/sysdeps/metag/dl-tls.h      | 3 +++
 libpthread/nptl/sysdeps/microblaze/dl-tls.h | 3 +++
 libpthread/nptl/sysdeps/mips/dl-tls.h       | 3 +++
 libpthread/nptl/sysdeps/nds32/dl-tls.h      | 3 +++
 libpthread/nptl/sysdeps/or1k/dl-tls.h       | 3 +++
 libpthread/nptl/sysdeps/powerpc/dl-tls.h    | 3 +++
 libpthread/nptl/sysdeps/riscv32/dl-tls.h    | 3 +++
 libpthread/nptl/sysdeps/riscv64/dl-tls.h    | 3 +++
 libpthread/nptl/sysdeps/sh/dl-tls.h         | 3 +++
 libpthread/nptl/sysdeps/sparc/dl-tls.h      | 3 +++
 libpthread/nptl/sysdeps/x86_64/dl-tls.h     | 3 +++
 libpthread/nptl/sysdeps/xtensa/dl-tls.h     | 4 ++++
 19 files changed, 57 insertions(+), 2 deletions(-)

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/alpha/dl-tls.h b/libpthread/nptl/sysdeps/alpha/dl-tls.h
index a87f66deb..7c1ec20aa 100644
--- a/libpthread/nptl/sysdeps/alpha/dl-tls.h
+++ b/libpthread/nptl/sysdeps/alpha/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)
diff --git a/libpthread/nptl/sysdeps/arc/dl-tls.h b/libpthread/nptl/sysdeps/arc/dl-tls.h
index 34e54e4d3..c50c58648 100644
--- a/libpthread/nptl/sysdeps/arc/dl-tls.h
+++ b/libpthread/nptl/sysdeps/arc/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)
diff --git a/libpthread/nptl/sysdeps/arm/dl-tls.h b/libpthread/nptl/sysdeps/arm/dl-tls.h
index a728455e4..ff35eb571 100644
--- a/libpthread/nptl/sysdeps/arm/dl-tls.h
+++ b/libpthread/nptl/sysdeps/arm/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)
diff --git a/libpthread/nptl/sysdeps/csky/dl-tls.h b/libpthread/nptl/sysdeps/csky/dl-tls.h
index ebf1a7dc3..81a790c24 100644
--- a/libpthread/nptl/sysdeps/csky/dl-tls.h
+++ b/libpthread/nptl/sysdeps/csky/dl-tls.h
@@ -14,3 +14,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)
+
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/m68k/dl-tls.h b/libpthread/nptl/sysdeps/m68k/dl-tls.h
index c0af06e1e..4960c9532 100644
--- a/libpthread/nptl/sysdeps/m68k/dl-tls.h
+++ b/libpthread/nptl/sysdeps/m68k/dl-tls.h
@@ -44,3 +44,6 @@ extern void *__tls_get_addr (tls_index *ti);
 
 #define GET_ADDR_OFFSET		(ti->ti_offset + TLS_DTV_OFFSET)
 #define __TLS_GET_ADDR(__ti)	(__tls_get_addr (__ti) - TLS_DTV_OFFSET)
+
+/* Value used for dtv entries for which the allocation is delayed.  */
+#define TLS_DTV_UNALLOCATED	((void *) -1l)
diff --git a/libpthread/nptl/sysdeps/metag/dl-tls.h b/libpthread/nptl/sysdeps/metag/dl-tls.h
index 81632e057..a49213194 100644
--- a/libpthread/nptl/sysdeps/metag/dl-tls.h
+++ b/libpthread/nptl/sysdeps/metag/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)
diff --git a/libpthread/nptl/sysdeps/microblaze/dl-tls.h b/libpthread/nptl/sysdeps/microblaze/dl-tls.h
index 5613e21e2..f0f820518 100644
--- a/libpthread/nptl/sysdeps/microblaze/dl-tls.h
+++ b/libpthread/nptl/sysdeps/microblaze/dl-tls.h
@@ -24,3 +24,6 @@ typedef struct
 } tls_index;
 
 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)
diff --git a/libpthread/nptl/sysdeps/mips/dl-tls.h b/libpthread/nptl/sysdeps/mips/dl-tls.h
index e26aa388b..6c23c0aff 100644
--- a/libpthread/nptl/sysdeps/mips/dl-tls.h
+++ b/libpthread/nptl/sysdeps/mips/dl-tls.h
@@ -43,3 +43,6 @@ extern void *__tls_get_addr (tls_index *ti);
 
 # define GET_ADDR_OFFSET	(ti->ti_offset + TLS_DTV_OFFSET)
 # define __TLS_GET_ADDR(__ti)	(__tls_get_addr (__ti) - TLS_DTV_OFFSET)
+
+/* Value used for dtv entries for which the allocation is delayed.  */
+#define TLS_DTV_UNALLOCATED	((void *) -1l)
diff --git a/libpthread/nptl/sysdeps/nds32/dl-tls.h b/libpthread/nptl/sysdeps/nds32/dl-tls.h
index f0107cacb..953a4d36e 100644
--- a/libpthread/nptl/sysdeps/nds32/dl-tls.h
+++ b/libpthread/nptl/sysdeps/nds32/dl-tls.h
@@ -56,4 +56,7 @@ extern void *_dl_make_tlsdesc_dynamic (struct link_map *map, size_t ti_offset);
 extern ptrdiff_t attribute_hidden
   _dl_tlsdesc_dynamic(struct tlsdesc *);
 
+/* Value used for dtv entries for which the allocation is delayed.  */
+#define TLS_DTV_UNALLOCATED	((void *) -1l)
+
 #endif //_NDS32_DL_TLS_H
diff --git a/libpthread/nptl/sysdeps/or1k/dl-tls.h b/libpthread/nptl/sysdeps/or1k/dl-tls.h
index 5613e21e2..f0f820518 100644
--- a/libpthread/nptl/sysdeps/or1k/dl-tls.h
+++ b/libpthread/nptl/sysdeps/or1k/dl-tls.h
@@ -24,3 +24,6 @@ typedef struct
 } tls_index;
 
 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)
diff --git a/libpthread/nptl/sysdeps/powerpc/dl-tls.h b/libpthread/nptl/sysdeps/powerpc/dl-tls.h
index c322ade60..6a084d38a 100644
--- a/libpthread/nptl/sysdeps/powerpc/dl-tls.h
+++ b/libpthread/nptl/sysdeps/powerpc/dl-tls.h
@@ -46,3 +46,6 @@ extern void *__tls_get_addr (tls_index *ti);
 # define GET_ADDR_OFFSET	(ti->ti_offset + TLS_DTV_OFFSET)
 # define __TLS_GET_ADDR(__ti)	(__tls_get_addr (__ti) - TLS_DTV_OFFSET)
 #endif
+
+/* Value used for dtv entries for which the allocation is delayed.  */
+#define TLS_DTV_UNALLOCATED	((void *) -1l)
diff --git a/libpthread/nptl/sysdeps/riscv32/dl-tls.h b/libpthread/nptl/sysdeps/riscv32/dl-tls.h
index 31991be0d..5554ba608 100644
--- a/libpthread/nptl/sysdeps/riscv32/dl-tls.h
+++ b/libpthread/nptl/sysdeps/riscv32/dl-tls.h
@@ -32,3 +32,6 @@ extern void *__tls_get_addr (tls_index *ti);
 
 #define GET_ADDR_OFFSET        (ti->ti_offset + TLS_DTV_OFFSET)
 #define __TLS_GET_ADDR(__ti)	(__tls_get_addr (__ti) - TLS_DTV_OFFSET)
+
+/* Value used for dtv entries for which the allocation is delayed.  */
+#define TLS_DTV_UNALLOCATED	((void *) -1l)
diff --git a/libpthread/nptl/sysdeps/riscv64/dl-tls.h b/libpthread/nptl/sysdeps/riscv64/dl-tls.h
index 31991be0d..5554ba608 100644
--- a/libpthread/nptl/sysdeps/riscv64/dl-tls.h
+++ b/libpthread/nptl/sysdeps/riscv64/dl-tls.h
@@ -32,3 +32,6 @@ extern void *__tls_get_addr (tls_index *ti);
 
 #define GET_ADDR_OFFSET        (ti->ti_offset + TLS_DTV_OFFSET)
 #define __TLS_GET_ADDR(__ti)	(__tls_get_addr (__ti) - TLS_DTV_OFFSET)
+
+/* Value used for dtv entries for which the allocation is delayed.  */
+#define TLS_DTV_UNALLOCATED	((void *) -1l)
diff --git a/libpthread/nptl/sysdeps/sh/dl-tls.h b/libpthread/nptl/sysdeps/sh/dl-tls.h
index f5f90beaf..6f6d0433a 100644
--- a/libpthread/nptl/sysdeps/sh/dl-tls.h
+++ b/libpthread/nptl/sysdeps/sh/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)
diff --git a/libpthread/nptl/sysdeps/sparc/dl-tls.h b/libpthread/nptl/sysdeps/sparc/dl-tls.h
index 7d9e8f0f7..91fe0516a 100644
--- a/libpthread/nptl/sysdeps/sparc/dl-tls.h
+++ b/libpthread/nptl/sysdeps/sparc/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)
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)
diff --git a/libpthread/nptl/sysdeps/xtensa/dl-tls.h b/libpthread/nptl/sysdeps/xtensa/dl-tls.h
index 5fc4ea2d0..77587b740 100644
--- a/libpthread/nptl/sysdeps/xtensa/dl-tls.h
+++ b/libpthread/nptl/sysdeps/xtensa/dl-tls.h
@@ -68,5 +68,9 @@ extern ptrdiff_t attribute_hidden
 extern void *_dl_make_tlsdesc_dynamic (struct link_map *map, size_t ti_offset);
 extern ptrdiff_t attribute_hidden
   _dl_tlsdesc_dynamic(struct tlsdesc_dynamic_arg *);
+
+/* Value used for dtv entries for which the allocation is delayed.  */
+#define TLS_DTV_UNALLOCATED	((void *) -1l)
+
 #endif
 #endif
-- 
2.34.1

