From 2827ab990aefbb0e53374199b875d98f116d6390 Mon Sep 17 00:00:00 2001 From: Carlos O'Donell Date: Fri, 22 Jun 2018 09:28:47 -0400 Subject: libc: Extend __libc_freeres framework (Bug 23329). The __libc_freeres framework does not extend to non-libc.so objects. This causes problems in general for valgrind and mtrace detecting unfreed objects in both libdl.so and libpthread.so. This change is a pre-requisite to properly moving the malloc hooks out of malloc since such a move now requires precise accounting of all allocated data before destructors are run. This commit adds a proper hook in libc.so.6 for both libdl.so and for libpthread.so, this ensures that shm-directory.c which uses freeit () to free memory is called properly. We also remove the nptl_freeres hook and fall back to using weak-ref-and-check idiom for a loaded libpthread.so, thus making this process similar for all DSOs. Lastly we follow best practice and use explicit free calls for both libdl.so and libpthread.so instead of the generic hook process which has undefined order. Tested on x86_64 with no regressions. Signed-off-by: DJ Delorie Signed-off-by: Carlos O'Donell --- malloc/set-freeres.c | 15 +++++++++++++++ malloc/thread-freeres.c | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) (limited to 'malloc') diff --git a/malloc/set-freeres.c b/malloc/set-freeres.c index f4a0e7bda4..cda368479f 100644 --- a/malloc/set-freeres.c +++ b/malloc/set-freeres.c @@ -26,6 +26,10 @@ DEFINE_HOOK (__libc_subfreeres, (void)); symbol_set_define (__libc_freeres_ptrs); +extern __attribute__ ((weak)) void __libdl_freeres (void); + +extern __attribute__ ((weak)) void __libpthread_freeres (void); + void __libc_freeres_fn_section __libc_freeres (void) { @@ -39,8 +43,19 @@ __libc_freeres (void) _IO_cleanup (); + /* We run the resource freeing after IO cleanup. */ RUN_HOOK (__libc_subfreeres, ()); + /* Call the libdl list of cleanup functions + (weak-ref-and-check). */ + if (&__libdl_freeres != NULL) + __libdl_freeres (); + + /* Call the libpthread list of cleanup functions + (weak-ref-and-check). */ + if (&__libpthread_freeres != NULL) + __libpthread_freeres (); + for (p = symbol_set_first_element (__libc_freeres_ptrs); !symbol_set_end_p (__libc_freeres_ptrs, p); ++p) free (*p); diff --git a/malloc/thread-freeres.c b/malloc/thread-freeres.c index 8902c845bc..a63b6c93f3 100644 --- a/malloc/thread-freeres.c +++ b/malloc/thread-freeres.c @@ -24,8 +24,8 @@ /* Thread shutdown function. Note that this function must be called for threads during shutdown for correctness reasons. Unlike - __libc_subfreeres, skipping calls to it is not a valid - optimization. */ + __libc_subfreeres, skipping calls to it is not a valid optimization. + This is called directly from pthread_create as the thread exits. */ void __libc_thread_freeres (void) { -- cgit 1.4.1