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 --- nptl/Makefile | 2 +- nptl/Versions | 1 + nptl/allocatestack.c | 12 +++++++++--- nptl/libc_pthread_init.c | 8 -------- nptl/nptl-init.c | 15 --------------- nptl/nptlfreeres.c | 31 +++++++++++++++++++++++++++++++ nptl/pthreadP.h | 5 +++-- 7 files changed, 45 insertions(+), 29 deletions(-) create mode 100644 nptl/nptlfreeres.c (limited to 'nptl') diff --git a/nptl/Makefile b/nptl/Makefile index 0f9c44a..2f2bb05 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -45,7 +45,7 @@ pthread-compat-wrappers = \ sigwait sigsuspend \ recvmsg sendmsg -libpthread-routines = nptl-init vars events version pt-interp \ +libpthread-routines = nptl-init nptlfreeres vars events version pt-interp \ pthread_create pthread_exit pthread_detach \ pthread_join pthread_tryjoin pthread_timedjoin \ pthread_join_common \ diff --git a/nptl/Versions b/nptl/Versions index 0ae5def..b1c2da0 100644 --- a/nptl/Versions +++ b/nptl/Versions @@ -271,5 +271,6 @@ libpthread { __pthread_unwind; __pthread_get_minstack; __pthread_barrier_init; __pthread_barrier_wait; __shm_directory; + __libpthread_freeres; } } diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c index 9c10b99..f9e053f 100644 --- a/nptl/allocatestack.c +++ b/nptl/allocatestack.c @@ -251,8 +251,8 @@ get_cached_stack (size_t *sizep, void **memp) /* Free stacks until cache size is lower than LIMIT. */ -void -__free_stacks (size_t limit) +static void +free_stacks (size_t limit) { /* We reduce the size of the cache. Remove the last entries until the size is below the limit. */ @@ -288,6 +288,12 @@ __free_stacks (size_t limit) } } +/* Free all the stacks on cleanup. */ +void +__nptl_stacks_freeres (void) +{ + free_stacks (0); +} /* Add a stack frame which is not used anymore to the stack. Must be called with the cache lock held. */ @@ -302,7 +308,7 @@ queue_stack (struct pthread *stack) stack_cache_actsize += stack->stackblock_size; if (__glibc_unlikely (stack_cache_actsize > stack_cache_maxsize)) - __free_stacks (stack_cache_maxsize); + free_stacks (stack_cache_maxsize); } diff --git a/nptl/libc_pthread_init.c b/nptl/libc_pthread_init.c index f390480..e254ea2 100644 --- a/nptl/libc_pthread_init.c +++ b/nptl/libc_pthread_init.c @@ -77,11 +77,3 @@ __libc_pthread_init (unsigned long int *ptr, void (*reclaim) (void), return &__libc_multiple_threads; #endif } - -#ifdef SHARED -libc_freeres_fn (freeres_libptread) -{ - if (__libc_pthread_functions_init) - PTHFCT_CALL (ptr_freeres, ()); -} -#endif diff --git a/nptl/nptl-init.c b/nptl/nptl-init.c index 1d3790f..907411d 100644 --- a/nptl/nptl-init.c +++ b/nptl/nptl-init.c @@ -78,9 +78,6 @@ extern void __nptl_set_robust (struct pthread *); #ifdef SHARED -static void nptl_freeres (void); - - static const struct pthread_functions pthread_functions = { .ptr_pthread_attr_destroy = __pthread_attr_destroy, @@ -140,8 +137,6 @@ static const struct pthread_functions pthread_functions = # ifdef SIGSETXID .ptr__nptl_setxid = __nptl_setxid, # endif - /* For now only the stack cache needs to be freed. */ - .ptr_freeres = nptl_freeres, .ptr_set_robust = __nptl_set_robust }; # define ptr_pthread_functions &pthread_functions @@ -151,16 +146,6 @@ static const struct pthread_functions pthread_functions = #ifdef SHARED -/* This function is called indirectly from the freeres code in libc. */ -static void -__libc_freeres_fn_section -nptl_freeres (void) -{ - __unwind_freeres (); - __free_stacks (0); -} - - static #endif void diff --git a/nptl/nptlfreeres.c b/nptl/nptlfreeres.c new file mode 100644 index 0000000..cfa54e1 --- /dev/null +++ b/nptl/nptlfreeres.c @@ -0,0 +1,31 @@ +/* Clean up allocated libpthread memory on demand. + Copyright (C) 2018 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +/* Free libpthread.so resources. + Note: Caller ensures we are called only once. */ +void +__libpthread_freeres (void) +{ + call_function_static_weak (__nptl_stacks_freeres); + call_function_static_weak (__shm_directory_freeres); + call_function_static_weak (__nptl_unwind_freeres); +} diff --git a/nptl/pthreadP.h b/nptl/pthreadP.h index 075530c..3cba0b6 100644 --- a/nptl/pthreadP.h +++ b/nptl/pthreadP.h @@ -279,8 +279,8 @@ hidden_proto (__pthread_register_cancel) hidden_proto (__pthread_unregister_cancel) # ifdef SHARED extern void attribute_hidden pthread_cancel_init (void); -extern void __unwind_freeres (void); # endif +extern void __nptl_unwind_freeres (void) attribute_hidden; #endif @@ -597,7 +597,8 @@ extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden; extern void __nptl_set_robust (struct pthread *self); #endif -extern void __free_stacks (size_t limit) attribute_hidden; +extern void __nptl_stacks_freeres (void) attribute_hidden; +extern void __shm_directory_freeres (void) attribute_hidden; extern void __wait_lookup_done (void) attribute_hidden; -- cgit v1.1