about summary refs log tree commit diff
path: root/malloc
diff options
context:
space:
mode:
authorAdhemerval Zanella Netto <adhemerval.zanella@linaro.org>2022-12-27 18:11:42 -0300
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2023-03-27 13:57:55 -0300
commit88677348b4de73874ca7d5a47451f42880f65f07 (patch)
tree9ebe22b88c2487331521737dfdf089f9e3e2439f /malloc
parente4d336f1ace7c7ca535f7f85485373752bc76ed5 (diff)
downloadglibc-88677348b4de73874ca7d5a47451f42880f65f07.tar.gz
glibc-88677348b4de73874ca7d5a47451f42880f65f07.tar.xz
glibc-88677348b4de73874ca7d5a47451f42880f65f07.zip
Move libc_freeres_ptrs and libc_subfreeres to hidden/weak functions
They are both used by __libc_freeres to free all library malloc
allocated resources to help tooling like mtrace or valgrind with
memory leak tracking.

The current scheme uses assembly markers and linker script entries
to consolidate the free routine function pointers in the RELRO segment
and to be freed buffers in BSS.

This patch changes it to use specific free functions for
libc_freeres_ptrs buffers and call the function pointer array directly
with call_function_static_weak.

It allows the removal of both the internal macros and the linker
script sections.

Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Diffstat (limited to 'malloc')
-rw-r--r--malloc/set-freeres.c156
-rw-r--r--malloc/thread-freeres.c9
2 files changed, 136 insertions, 29 deletions
diff --git a/malloc/set-freeres.c b/malloc/set-freeres.c
index 4a5cec5d4a..f452ff838b 100644
--- a/malloc/set-freeres.c
+++ b/malloc/set-freeres.c
@@ -15,32 +15,62 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <array_length.h>
 #include <atomic.h>
 #include <stdlib.h>
-#include <set-hooks.h>
 #include <libc-internal.h>
 #include <unwind-link.h>
-#include <dlfcn/dlerror.h>
 #include <ldsodefs.h>
+#include <set-freeres.h>
+#include <set-freeres-system.h>
+
+#ifndef SHARED
+# pragma weak __nss_module_freeres
+# pragma weak __nss_action_freeres
+# pragma weak __nss_database_freeres
+# pragma weak __dl_libc_freemem
+# pragma weak __hdestroy
+# pragma weak __gconv_cache_freemem
+# pragma weak __gconv_conf_freemem
+# pragma weak __gconv_db_freemem
+# pragma weak __gconv_dl_freemem
+# pragma weak __intl_freemem
+# pragma weak __libio_freemem
+# pragma weak __libc_fstab_freemem
+# pragma weak __nscd_gr_map_freemem
+# pragma weak __nscd_hst_map_freemem
+# pragma weak __nscd_pw_map_freemem
+# pragma weak __nscd_serv_map_freemem
+# pragma weak __nscd_group_map_freemem
+# pragma weak __libc_regcomp_freemem
+# pragma weak __libc_atfork_freemem
+# pragma weak __res_thread_freeres
+# pragma weak __libc_resolv_conf_freemem
+# pragma weak __libc_printf_freemem
+# pragma weak __libc_fmtmsg_freemem
+# pragma weak __libc_setenv_freemem
+# pragma weak __rpc_freemem
+# pragma weak __rpc_thread_destroy
+# pragma weak __libc_getaddrinfo_freemem
+# pragma weak __libc_tzset_freemem
+# pragma weak __libc_localealias_freemem
+# pragma weak __gai_freemem
+# pragma weak __aio_freemem
+# pragma weak __libpthread_freeres
+# pragma weak __libc_dlerror_result_free
+# pragma weak __check_pf_freemem
+#endif
 
-#include "../nss/nsswitch.h"
-#include "../libio/libioP.h"
-
-DEFINE_HOOK (__libc_subfreeres, (void));
-
-symbol_set_define (__libc_freeres_ptrs);
-
-extern void __libpthread_freeres (void)
-#if PTHREAD_IN_LIBC && defined SHARED
-/* It is possible to call __libpthread_freeres directly in shared
-   builds with an integrated libpthread.  */
-  attribute_hidden
+#ifdef SHARED
+# define call_free_static_weak(__ptr)				\
+   free (__ptr)
 #else
-  __attribute__ ((weak))
+# define call_free_static_weak(__ptr)				\
+  if (&__ptr != NULL)						\
+    free (__ptr);
 #endif
-  ;
 
-void __libc_freeres_fn_section
+void
 __libc_freeres (void)
 {
   /* This function might be called from different places.  So better
@@ -49,8 +79,6 @@ __libc_freeres (void)
 
   if (!atomic_compare_and_exchange_bool_acq (&already_called, 1, 0))
     {
-      void *const *p;
-
       call_function_static_weak (__nss_module_freeres);
       call_function_static_weak (__nss_action_freeres);
       call_function_static_weak (__nss_database_freeres);
@@ -58,7 +86,43 @@ __libc_freeres (void)
       _IO_cleanup ();
 
       /* We run the resource freeing after IO cleanup.  */
-      RUN_HOOK (__libc_subfreeres, ());
+      call_function_static_weak (__dl_libc_freemem);
+      call_function_static_weak (__hdestroy);
+      call_function_static_weak (__gconv_cache_freemem);
+      call_function_static_weak (__gconv_conf_freemem);
+      call_function_static_weak (__gconv_db_freemem);
+      call_function_static_weak (__gconv_dl_freemem);
+      call_function_static_weak (__intl_freemem);
+      call_function_static_weak (__libio_freemem);
+      call_function_static_weak (__libc_fstab_freemem);
+      call_function_static_weak (__nscd_gr_map_freemem);
+      call_function_static_weak (__nscd_hst_map_freemem);
+      call_function_static_weak (__nscd_pw_map_freemem);
+      call_function_static_weak (__nscd_serv_map_freemem);
+      call_function_static_weak (__nscd_group_map_freemem);
+      call_function_static_weak (__libc_regcomp_freemem);
+      call_function_static_weak (__libc_atfork_freemem);
+      /* __res_thread_freeres deallocates the per-thread resolv_context);
+	 which in turn drop the reference count of the current global object.
+	 So it need to be before __libc_resolv_conf_freemem.  */
+      call_function_static_weak (__res_thread_freeres);
+      call_function_static_weak (__libc_resolv_conf_freemem);
+      call_function_static_weak (__libc_printf_freemem);
+      call_function_static_weak (__libc_fmtmsg_freemem);
+      call_function_static_weak (__libc_setenv_freemem);
+#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_31)
+      call_function_static_weak (__rpc_freemem);
+      call_function_static_weak (__rpc_thread_destroy);
+#endif
+      call_function_static_weak (__libc_getaddrinfo_freemem);
+      call_function_static_weak (__libc_tzset_freemem);
+      call_function_static_weak (__libc_localealias_freemem);
+
+#if PTHREAD_IN_LIBC
+      call_function_static_weak (__gai_freemem);
+
+      call_function_static_weak (__aio_freemem);
+#endif
 
       call_function_static_weak (__libpthread_freeres);
 
@@ -72,9 +136,55 @@ __libc_freeres (void)
       GLRO (dl_libc_freeres) ();
 #endif
 
-      for (p = symbol_set_first_element (__libc_freeres_ptrs);
-           !symbol_set_end_p (__libc_freeres_ptrs, p); ++p)
-        free (*p);
+      call_free_static_weak (__libc_fgetgrent_freemem_ptr);
+      call_free_static_weak (__libc_fgetsgent_freeres_ptr);
+      call_free_static_weak (__libc_getnetgrent_freemem_ptr);
+      call_free_static_weak (__libc_rcmd_freemem_ptr);
+      call_free_static_weak (__libc_rexec_freemem_ptr);
+      call_free_static_weak (__libc_mntent_freemem_ptr);
+      call_free_static_weak (__libc_fgetpwent_freemem_ptr);
+      call_free_static_weak (__libc_resolv_res_hconf_freemem_ptr);
+      call_free_static_weak (__libc_fgetspent_freemem_ptr);
+      call_free_static_weak (__libc_tzfile_freemem_ptr);
+      call_free_static_weak (__libc_getnameinfo_freemem_ptr);
+      call_free_static_weak (__libc_getutent_freemem_ptr);
+      call_free_static_weak (__libc_getutid_freemem_ptr);
+      call_free_static_weak (__libc_getutline_freemem_ptr);
+      call_free_static_weak (__libc_reg_printf_freemem_ptr);
+      call_free_static_weak (__libc_reg_type_freemem_ptr);
+
+      call_free_static_weak (__libc_getgrgid_freemem_ptr);
+      call_free_static_weak (__libc_getgrnam_freemem_ptr);
+      call_free_static_weak (__libc_getpwnam_freemem_ptr);
+      call_free_static_weak (__libc_getpwuid_freemem_ptr);
+      call_free_static_weak (__libc_getspnam_freemem_ptr);
+      call_free_static_weak (__libc_getaliasbyname_freemem_ptr);
+      call_free_static_weak (__libc_gethostbyaddr_freemem_ptr);
+      call_free_static_weak (__libc_gethostbyname_freemem_ptr);
+      call_free_static_weak (__libc_gethostbyname2_freemem_ptr);
+      call_free_static_weak (__libc_getnetbyaddr_freemem_ptr);
+      call_free_static_weak (__libc_getnetbyname_freemem_ptr);
+      call_free_static_weak (__libc_getprotobynumber_freemem_ptr);
+      call_free_static_weak (__libc_getprotobyname_freemem_ptr);
+      call_free_static_weak (__libc_getrpcbyname_freemem_ptr);
+      call_free_static_weak (__libc_getrpcbynumber_freemem_ptr);
+      call_free_static_weak (__libc_getservbyname_freemem_ptr);
+      call_free_static_weak (__libc_getservbyport_freemem_ptr);
+
+      call_free_static_weak (__libc_getgrent_freemem_ptr);
+      call_free_static_weak (__libc_getpwent_freemem_ptr);
+      call_free_static_weak (__libc_getspent_freemem_ptr);
+      call_free_static_weak (__libc_getaliasent_freemem_ptr);
+      call_free_static_weak (__libc_gethostent_freemem_ptr);
+      call_free_static_weak (__libc_getnetent_freemem_ptr);
+      call_free_static_weak (__libc_getprotoent_freemem_ptr);
+      call_free_static_weak (__libc_getrpcent_freemem_ptr);
+      call_free_static_weak (__libc_getservent_freemem_ptr);
+
+      call_free_static_weak (__libc_efgcvt_freemem_ptr);
+      call_free_static_weak (__libc_qefgcvt_freemem_ptr);
+
+      call_freeres_system_funcs;
     }
 }
 libc_hidden_def (__libc_freeres)
diff --git a/malloc/thread-freeres.c b/malloc/thread-freeres.c
index cbb99f8bb4..416d199afd 100644
--- a/malloc/thread-freeres.c
+++ b/malloc/thread-freeres.c
@@ -16,18 +16,15 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <dlfcn/dlerror.h>
 #include <libc-internal.h>
 #include <malloc-internal.h>
-#include <resolv/resolv-internal.h>
-#include <rpc/rpc.h>
-#include <string.h>
-#include <tls-internal.h>
+#include <set-freeres.h>
 #include <shlib-compat.h>
+#include <tls-internal.h>
 
 /* 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_freeres, 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)