public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "Vivek Das Mohapatra" <vivek@collabora.com>
To: libc-alpha <libc-alpha@sourceware.org>
Subject: [RFC][PATCH v7 05/20] elf/dl-fini.c: Handle proxy link_map entries in the shutdown path
Date: Wed, 16 Dec 2020 13:26:35 +0000	[thread overview]
Message-ID: <20201216132650.22949-6-vivek@collabora.com> (raw)
In-Reply-To: <20201216132650.22949-1-vivek@collabora.com>

When cleaning up before exit we should not call destructors or
otherwise free [most of] the contents of proxied link_map entries
since they share [most of] their contents with the LM_ID_BASE
objects to which they point.
---
 elf/dl-close.c | 43 ++++++++++++++++++++++++++-----------------
 elf/dl-fini.c  |  6 ++++--
 2 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/elf/dl-close.c b/elf/dl-close.c
index 8e146ecee1..4847b3294f 100644
--- a/elf/dl-close.c
+++ b/elf/dl-close.c
@@ -283,8 +283,9 @@ _dl_close_worker (struct link_map *map, bool force)
 
 	  /* Call its termination function.  Do not do it for
 	     half-cooked objects.  Temporarily disable exception
-	     handling, so that errors are fatal.  */
-	  if (imap->l_init_called)
+	     handling, so that errors are fatal.
+	     Proxies should never have this flag set, but we double check.  */
+	  if (imap->l_init_called && !imap->l_proxy)
 	    {
 	      /* When debugging print a message first.  */
 	      if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS,
@@ -360,7 +361,9 @@ _dl_close_worker (struct link_map *map, bool force)
 	     one for the terminating NULL pointer.  */
 	  size_t remain = (new_list != NULL) + 1;
 	  bool removed_any = false;
-	  for (size_t cnt = 0; imap->l_scope[cnt] != NULL; ++cnt)
+	  for (size_t cnt = 0;
+               imap->l_scope && imap->l_scope[cnt] != NULL;
+               ++cnt)
 	    /* This relies on l_scope[] entries being always set either
 	       to its own l_symbolic_searchlist address, or some map's
 	       l_searchlist address.  */
@@ -686,8 +689,10 @@ _dl_close_worker (struct link_map *map, bool force)
 
 	  /* We can unmap all the maps at once.  We determined the
 	     start address and length when we loaded the object and
-	     the `munmap' call does the rest.  */
-	  DL_UNMAP (imap);
+	     the `munmap' call does the rest. Proxies do not have
+             any segments of their own to unmap.  */
+          if (!imap->l_proxy)
+            DL_UNMAP (imap);
 
 	  /* Finally, unlink the data structure and free it.  */
 #if DL_NNS == 1
@@ -727,19 +732,23 @@ _dl_close_worker (struct link_map *map, bool force)
 	    _dl_debug_printf ("\nfile=%s [%lu];  destroying link map\n",
 			      imap->l_name, imap->l_ns);
 
-	  /* This name always is allocated.  */
-	  free (imap->l_name);
-	  /* Remove the list with all the names of the shared object.  */
+          /* Skip structures borrowed by proxies from the real map.  */
+          if (!imap->l_proxy)
+            {
+              /* This name always is allocated.  */
+              free (imap->l_name);
+              /* Remove the list with all the names of the shared object.  */
 
-	  struct libname_list *lnp = imap->l_libname;
-	  do
-	    {
-	      struct libname_list *this = lnp;
-	      lnp = lnp->next;
-	      if (!this->dont_free)
-		free (this);
-	    }
-	  while (lnp != NULL);
+              struct libname_list *lnp = imap->l_libname;
+              do
+                {
+                  struct libname_list *this = lnp;
+                  lnp = lnp->next;
+                  if (!this->dont_free)
+                    free (this);
+                }
+              while (lnp != NULL);
+            }
 
 	  /* Remove the searchlists.  */
 	  free (imap->l_initfini);
diff --git a/elf/dl-fini.c b/elf/dl-fini.c
index 231db3d66d..25feb5216d 100644
--- a/elf/dl-fini.c
+++ b/elf/dl-fini.c
@@ -73,7 +73,7 @@ _dl_fini (void)
 	  assert (nloaded != 0 || GL(dl_ns)[ns]._ns_loaded == NULL);
 	  for (l = GL(dl_ns)[ns]._ns_loaded, i = 0; l != NULL; l = l->l_next)
 	    /* Do not handle ld.so in secondary namespaces.  */
-	    if (l == l->l_real)
+	    if (l == l->l_real || l->l_proxy)
 	      {
 		assert (i < nloaded);
 
@@ -111,7 +111,9 @@ _dl_fini (void)
 	    {
 	      struct link_map *l = maps[i];
 
-	      if (l->l_init_called)
+              /* Do not call fini functions via proxies, or for
+                 objects which are not marked as initialised.  */
+	      if (l->l_init_called && !l->l_proxy)
 		{
 		  /* Make sure nothing happens if we are called twice.  */
 		  l->l_init_called = 0;
-- 
2.20.1


  parent reply	other threads:[~2020-12-16 13:27 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-16 13:26 [RFC][PATCH v7 00/20] Implementation of RTLD_SHARED for dlmopen Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 01/20] Declare and describe the dlmopen RTLD_SHARED flag Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 02/20] include/link.h: Update the link_map struct to allow proxies Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 03/20] elf/dl-object.c: Implement a helper function to proxy link_map entries Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 04/20] elf/dl-load.c, elf-dl-open.c: Implement RTLD_SHARED dlmopen proxying Vivek Das Mohapatra
2020-12-16 13:26 ` Vivek Das Mohapatra [this message]
2020-12-16 13:26 ` [RFC][PATCH v7 06/20] elf/dl-init.c: Skip proxied link map entries in the dl init path Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 07/20] elf/dl-open.c: Don't try libc linit in namespaces with no libc mapping Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 08/20] elf/dl-open.c: when creating a proxy check the libc_map in NS 0 Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 09/20] Define a new dynamic section tag - DT_GNU_FLAGS_1 Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 10/20] Abstract the loaded-DSO search code into a private helper function Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 11/20] Compare loaded DSOs by file ID and check for DF_GNU_1_UNIQUE Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 12/20] Use the new DSO finder helper function since we have it Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 13/20] Use the DSO search helper to check for preloaded DT_GNU_UNIQUE DSOs Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 14/20] When loading DSOs into alternate namespaces check for DT_GNU_UNIQUE Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 15/20] Suppress audit calls when a (new) namespace is empty Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 16/20] Suppress inter-namespace DSO sharing for audit libraries Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 17/20] dlsym, dlvsym should be able to look up symbols via DSO proxies Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 18/20] Add DT_GNU_FLAGS_1/DF_GNU_1_UNIQUE dynamic section+flag to glibc DSOs Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 19/20] Add dlmopen / RTLD_SHARED tests Vivek Das Mohapatra
2020-12-16 13:26 ` [RFC][PATCH v7 20/20] Restore separate libc loading for the TLS/namespace storage test Vivek Das Mohapatra
2021-01-07 17:00   ` Ping " Vivek Das Mohapatra
2021-01-07 19:55     ` Adhemerval Zanella
2021-01-08  0:33       ` Vivek Das Mohapatra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201216132650.22949-6-vivek@collabora.com \
    --to=vivek@collabora.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).