diff mbox series

[uclibc-ng-devel] TLS memory-leak with dlopen

Message ID 43a99bde957d42369c331f1bbff80c52@h3c.com
State New
Headers show
Series [uclibc-ng-devel] TLS memory-leak with dlopen | expand

Commit Message

Lumingxiang Sept. 26, 2024, 7:54 a.m. UTC
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.











Ref:

https://sourceware.org/bugzilla/show_bug.cgi?id=12650





How to reproduce:



-------------------------------------------------------------------------------------------------------------------------------------
本邮件及其附件含有新华三集团的保密信息,仅限于发送给上面地址中列出
的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、
或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本
邮件!
This e-mail and its attachments contain confidential information from New H3C, which is
intended only for the person or entity whose address is listed above. Any use of the
information contained herein in any way (including, but not limited to, total or partial
disclosure, reproduction, or dissemination) by persons other than the intended
recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender
by phone or email immediately and delete it!

Comments

Waldemar Brodkorb Sept. 27, 2024, 7:47 a.m. UTC | #1
Hi,
Lumingxiang wrote,

> 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.
 
Is it possible to sent a git format-patch -s maybe as attachment?
Your mail client mangled the patch.

Furthermore can you subscribe to the mailinglist, I had to manually
pass your mail.
  
> 
> 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:

Do you have example code to reproduce the issue?

best regards
 Waldemar
Lumingxiang Sept. 29, 2024, 12:52 p.m. UTC | #2
Hi,
Waldemar Brodkorb,

> Is it possible to sent a git format-patch -s maybe as attachment?
 Certainly, please find the attachment.

>Do you have example code to reproduce the issue?
Yes, please see the attached example code. The two source files are compiled into a shared object and an application respectively.
Instructions for compiling them can be found in the first lines of the files.

>Furthermore can you subscribe to the mailinglist, I had to manually pass your mail.
Of course, I have already subscribed to the mailing list.

best regards && have a nice day!
Mingxiang Lu

-----邮件原件-----
发件人: Waldemar Brodkorb <wbx@openadk.org>
发送时间: Friday, September 27, 2024 3:47 PM
收件人: lumingxiang (操作系统开发部/NingOS, RD) <lu.mingxiang@h3c.com>
抄送: devel@uclibc-ng.org; zhangchun (操作系统开发部/NingOS, RD) <zhang.chunA@h3c.com>
主题: Re: [uclibc-ng-devel] TLS memory-leak with dlopen

Hi,
Lumingxiang wrote,

> 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.

Is it possible to sent a git format-patch -s maybe as attachment?
Your mail client mangled the patch.

Furthermore can you subscribe to the mailinglist, I had to manually pass your mail.

>
> 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:

Do you have example code to reproduce the issue?

best regards
 Waldemar
-------------------------------------------------------------------------------------------------------------------------------------
本邮件及其附件含有新华三集团的保密信息,仅限于发送给上面地址中列出
的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、
或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本
邮件!
This e-mail and its attachments contain confidential information from New H3C, which is
intended only for the person or entity whose address is listed above. Any use of the
information contained herein in any way (including, but not limited to, total or partial
disclosure, reproduction, or dissemination) by persons other than the intended
recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender
by phone or email immediately and delete it!
// gcc  -g -O0 -o main ./tls_main.c -L. -lthreadfunc  -ldl -lpthread
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <stdint.h>
#include <unistd.h>


typedef void* (*thread_func_t)(void*);



int main() {
    pthread_t thread;

    void* handle;
    thread_func_t thread_func;

    handle = dlopen("./libthreadfunc.so", RTLD_LAZY);
    if (!handle) {
        fprintf(stderr, "Failed to load libthreadfunc.so: %s\n", dlerror());
        exit(EXIT_FAILURE);
    }

    dlerror();  
    thread_func = (thread_func_t)dlsym(handle, "thread_func");
    const char* dlsym_error = dlerror();
    if (dlsym_error) {
        fprintf(stderr, "Failed to find symbol thread_func: %s\n", dlsym_error);
        dlclose(handle);
        exit(EXIT_FAILURE);
    }

    while (1) {
            if (pthread_create(&thread, NULL, thread_func, NULL)!= 0) {
                perror("Failed to create thread");
                dlclose(handle);
                exit(EXIT_FAILURE);
            }
        sleep(1);
    }
    
    dlclose(handle);

    return 0;
}
// gcc   -g -O0 -fPIC -shared -o libthreadfunc.so tls_func.c -lpthread
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>

#define TLS_SIZE 8192UL

static __thread char g_sArray[TLS_SIZE ] ;


void* thread_func(void* arg) {
    pthread_t tid = pthread_self();

    memset(g_sArray, 0, sizeof(g_sArray));
    
    snprintf(g_sArray, sizeof(g_sArray), "Thread ID: %lu", (unsigned long)tid);

    printf("g_sArray:%s\n", g_sArray);

    pthread_detach(pthread_self());

    return NULL;
}
Waldemar Brodkorb Sept. 30, 2024, 3:14 p.m. UTC | #3
Hi,
Lumingxiang wrote,

> Hi,
> Waldemar Brodkorb,
> 
> > Is it possible to sent a git format-patch -s maybe as attachment?
>  Certainly, please find the attachment.

Okay, thanks.

> >Do you have example code to reproduce the issue?
> Yes, please see the attached example code. The two source files are compiled into a shared object and an application respectively.
> Instructions for compiling them can be found in the first lines of the files.

I tried attached code with valgrind in qemu-system-x86_64 with a
uClibc-ng master system, but valgrind did not found any memleak.
Furthermore I tried the code in the original bugreport, but here
also no memleak is found.
Any idea? Can you post the output of valgrind? Can you see the
memleak?
 
> >Furthermore can you subscribe to the mailinglist, I had to manually pass your mail.
> Of course, I have already subscribed to the mailing list.

Thanks.

best regards
 Waldemar
diff mbox series

Patch

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)