public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: Florian Weimer <fweimer@redhat.com>, libc-alpha@sourceware.org
Subject: Re: [PATCH v2 3/3] elf: Always call destructors in reverse constructor order
Date: Tue, 15 Feb 2022 08:59:23 -0300	[thread overview]
Message-ID: <2a10bdb3-88ff-0313-a17e-0a0a8e8aa5fb@linaro.org> (raw)
In-Reply-To: <1584a2f86db6fe8b46460c53f499d65ba65b55bb.1643901334.git.fweimer@redhat.com>



On 03/02/2022 12:18, Florian Weimer via Libc-alpha wrote:
> diff --git a/elf/dl-fini.c b/elf/dl-fini.c
> index f841868cdb..ba53c2a4cb 100644
> --- a/elf/dl-fini.c
> +++ b/elf/dl-fini.c
> @@ -29,147 +29,87 @@ typedef void (*fini_t) (void);
>  void
>  _dl_fini (void)
>  {
> -  /* Lots of fun ahead.  We have to call the destructors for all still
> -     loaded objects, in all namespaces.  The problem is that the ELF
> -     specification now demands that dependencies between the modules
> -     are taken into account.  I.e., the destructor for a module is
> -     called before the ones for any of its dependencies.
> -
> -     To make things more complicated, we cannot simply use the reverse
> -     order of the constructors.  Since the user might have loaded objects
> -     using `dlopen' there are possibly several other modules with its
> -     dependencies to be taken into account.  Therefore we have to start
> -     determining the order of the modules once again from the beginning.  */
> -
> -  /* We run the destructors of the main namespaces last.  As for the
> -     other namespaces, we pick run the destructors in them in reverse
> -     order of the namespace ID.  */
> +  /* Call destructors strictly in the reverse order of constructors.
> +     This causes fewer surprises than some arbitrary reordering based
> +     on new (relocation) dependencies.  None of the objects are
> +     unmapped, so applications can deal with this if their DSOs remain
> +     in a consistent state after destructors have run.  */
> +
> +  /* Protect against concurrent loads and unloads.  */
> +  __rtld_lock_lock_recursive (GL(dl_load_lock));
> +
> +  /* Ignore objects which are opened during shutdown.  */
> +  struct link_map *local_init_called_list = _dl_init_called_list;
> +
> +  for (struct link_map *l = local_init_called_list; l != NULL;
> +       l = l->l_init_called_next)
> +      /* Bump l_direct_opencount of all objects so that they
> +	 are not dlclose()ed from underneath us.  */
> +      ++l->l_direct_opencount;
> +
> +  /* After this point, Everything linked from local_init_called_list
> +     cannot be unloaded because of the reference counter update.  */
> +  __rtld_lock_unlock_recursive (GL(dl_load_lock));
> +
> +  /* Perform two passes: One for non-audit modules, one for audit
> +     modules.  This way, audit modules receive unload notifications
> +     for non-audit objects, and the destructors for audit modules
> +     still run.  */
>  #ifdef SHARED
> -  int do_audit = 0;
> - again:
> +  int last_pass = GLRO(dl_naudit) > 0;
> +  Lmid_t last_ns = -1;	     /* Avoid some LA_ACT_DELETE callouts.  */

This has caused as lot of audit regressions, since it will make
_dl_audit_activity_nsid access

  struct link_map *head = GL(dl_ns)[-1]._ns_loaded;

> +  for (int do_audit = 0; do_audit <= last_pass; ++do_audit)
>  #endif
> -  for (Lmid_t ns = GL(dl_nns) - 1; ns >= 0; --ns)
> -    {
> -      /* Protect against concurrent loads and unloads.  */
> -      __rtld_lock_lock_recursive (GL(dl_load_lock));
> -
> -      unsigned int nloaded = GL(dl_ns)[ns]._ns_nloaded;
> -      /* No need to do anything for empty namespaces or those used for
> -	 auditing DSOs.  */
> -      if (nloaded == 0
> +    for (struct link_map *l = local_init_called_list; l != NULL;
> +	 l = l->l_init_called_next)
> +      {
>  #ifdef SHARED
> -	  || GL(dl_ns)[ns]._ns_loaded->l_auditing != do_audit
> -#endif
> -	  )
> -	__rtld_lock_unlock_recursive (GL(dl_load_lock));
> -      else
> -	{
> -#ifdef SHARED
> -	  _dl_audit_activity_nsid (ns, LA_ACT_DELETE);
> +	if (GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing != do_audit)
> +	  continue;
> +
> +	if (last_ns != l->l_ns)
> +	  {

I think you will need a

   if (last_ns != -1)
     _dl_audit_activity_nsid (last_ns, LA_ACT_CONSISTENT);

> +	    _dl_audit_activity_nsid (last_ns, LA_ACT_CONSISTENT);
> +	    _dl_audit_activity_nsid (l->l_ns, LA_ACT_DELETE);
> +	    last_ns = l->l_ns;
> +	  }
>  #endif

  reply	other threads:[~2022-02-15 11:59 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-03 15:17 [PATCH v2 0/3] Predictable ELF destructor ordering Florian Weimer
2022-02-03 15:17 ` [PATCH v2 1/3] elf: Do not rely on relocation dependencies for destructor sorting Florian Weimer
2022-02-14 20:11   ` Adhemerval Zanella
2022-02-03 15:18 ` [PATCH v2 2/3] elf: Do not run constructors for proxy objects Florian Weimer
2022-02-03 15:18 ` [PATCH v2 3/3] elf: Always call destructors in reverse constructor order Florian Weimer
2022-02-15 11:59   ` Adhemerval Zanella [this message]
2023-08-22 10:46     ` Florian Weimer

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=2a10bdb3-88ff-0313-a17e-0a0a8e8aa5fb@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=fweimer@redhat.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).