about summary refs log tree commit diff
path: root/nptl
diff options
context:
space:
mode:
authorCarlos O'Donell <carlos@redhat.com>2018-06-22 09:28:47 -0400
committerCarlos O'Donell <carlos@redhat.com>2018-06-29 22:39:06 -0400
commit2827ab990aefbb0e53374199b875d98f116d6390 (patch)
tree0063b4c07be87d887d4a4893b9681b9e61479e8b /nptl
parent37d3d244e1a0ca7e7ac89b8e768e665adbb2e2d8 (diff)
downloadglibc-2827ab990aefbb0e53374199b875d98f116d6390.tar.gz
glibc-2827ab990aefbb0e53374199b875d98f116d6390.tar.xz
glibc-2827ab990aefbb0e53374199b875d98f116d6390.zip
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 <dj@redhat.com>
Signed-off-by: Carlos O'Donell <carlos@redhat.com>
Diffstat (limited to 'nptl')
-rw-r--r--nptl/Makefile2
-rw-r--r--nptl/Versions1
-rw-r--r--nptl/allocatestack.c12
-rw-r--r--nptl/libc_pthread_init.c8
-rw-r--r--nptl/nptl-init.c15
-rw-r--r--nptl/nptlfreeres.c31
-rw-r--r--nptl/pthreadP.h5
7 files changed, 45 insertions, 29 deletions
diff --git a/nptl/Makefile b/nptl/Makefile
index 0f9c44afa0..2f2bb0569d 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 0ae5def464..b1c2da06c0 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 9c10b993fd..f9e053f9e5 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 f390480d4b..e254ea2552 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 1d3790f500..907411d5bc 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 0000000000..cfa54e1bc7
--- /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
+   <http://www.gnu.org/licenses/>.  */
+
+#include <set-hooks.h>
+#include <libc-symbols.h>
+#include <pthreadP.h>
+
+/* 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 075530c15c..3cba0b6f9e 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;