Blame stdlib/cxa_thread_atexit_impl.c

Packit 6c4009
/* Register destructors for C++ TLS variables declared with thread_local.
Packit 6c4009
   Copyright (C) 2013-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* CONCURRENCY NOTES:
Packit 6c4009
Packit 6c4009
   This documents concurrency for the non-POD TLS destructor registration,
Packit 6c4009
   calling and destruction.  The functions __cxa_thread_atexit_impl,
Packit 6c4009
   _dl_close_worker and __call_tls_dtors are the three main routines that may
Packit 6c4009
   run concurrently and access shared data.  The shared data in all possible
Packit 6c4009
   combinations of all three functions are the link map list, a link map for a
Packit 6c4009
   DSO and the link map member l_tls_dtor_count.
Packit 6c4009
Packit 6c4009
   __cxa_thread_atexit_impl acquires the dl_load_lock before accessing any
Packit 6c4009
   shared state and hence multiple of its instances can safely execute
Packit 6c4009
   concurrently.
Packit 6c4009
Packit 6c4009
   _dl_close_worker acquires the dl_load_lock before accessing any shared state
Packit 6c4009
   as well and hence can concurrently execute multiple of its own instances as
Packit 6c4009
   well as those of __cxa_thread_atexit_impl safely.  Not all accesses to
Packit 6c4009
   l_tls_dtor_count are protected by the dl_load_lock, so we need to
Packit 6c4009
   synchronize using atomics.
Packit 6c4009
Packit 6c4009
   __call_tls_dtors accesses the l_tls_dtor_count without taking the lock; it
Packit 6c4009
   decrements the value by one.  It does not need the big lock because it does
Packit 6c4009
   not access any other shared state except for the current DSO link map and
Packit 6c4009
   its member l_tls_dtor_count.
Packit 6c4009
Packit 6c4009
   Correspondingly, _dl_close_worker loads l_tls_dtor_count and if it is zero,
Packit 6c4009
   unloads the DSO, thus deallocating the current link map.  This is the goal
Packit 6c4009
   of maintaining l_tls_dtor_count - to unload the DSO and free resources if
Packit 6c4009
   there are no pending destructors to be called.
Packit 6c4009
Packit 6c4009
   We want to eliminate the inconsistent state where the DSO is unloaded in
Packit 6c4009
   _dl_close_worker before it is used in __call_tls_dtors.  This could happen
Packit 6c4009
   if __call_tls_dtors uses the link map after it sets l_tls_dtor_count to 0,
Packit 6c4009
   since _dl_close_worker will conclude from the 0 l_tls_dtor_count value that
Packit 6c4009
   it is safe to unload the DSO.  Hence, to ensure that this does not happen,
Packit 6c4009
   the following conditions must be met:
Packit 6c4009
Packit 6c4009
   1. In _dl_close_worker, the l_tls_dtor_count load happens before the DSO is
Packit 6c4009
      unloaded and its link map is freed
Packit 6c4009
   2. The link map dereference in __call_tls_dtors happens before the
Packit 6c4009
      l_tls_dtor_count dereference.
Packit 6c4009
Packit 6c4009
   To ensure this, the l_tls_dtor_count decrement in __call_tls_dtors should
Packit 6c4009
   have release semantics and the load in _dl_close_worker should have acquire
Packit 6c4009
   semantics.
Packit 6c4009
Packit 6c4009
   Concurrent executions of __call_tls_dtors should only ensure that the value
Packit 6c4009
   is accessed atomically; no reordering constraints need to be considered.
Packit 6c4009
   Likewise for the increment of l_tls_dtor_count in __cxa_thread_atexit_impl.
Packit 6c4009
Packit 6c4009
   There is still a possibility on concurrent execution of _dl_close_worker and
Packit 6c4009
   __call_tls_dtors where _dl_close_worker reads the value of l_tls_dtor_count
Packit 6c4009
   as 1, __call_tls_dtors decrements the value of l_tls_dtor_count but
Packit 6c4009
   _dl_close_worker does not unload the DSO, having read the old value.  This
Packit 6c4009
   is not very different from a case where __call_tls_dtors is called after
Packit 6c4009
   _dl_close_worker on the DSO and hence is an accepted execution.  */
Packit 6c4009
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <ldsodefs.h>
Packit 6c4009
Packit 6c4009
typedef void (*dtor_func) (void *);
Packit 6c4009
Packit 6c4009
struct dtor_list
Packit 6c4009
{
Packit 6c4009
  dtor_func func;
Packit 6c4009
  void *obj;
Packit 6c4009
  struct link_map *map;
Packit 6c4009
  struct dtor_list *next;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static __thread struct dtor_list *tls_dtor_list;
Packit 6c4009
static __thread void *dso_symbol_cache;
Packit 6c4009
static __thread struct link_map *lm_cache;
Packit 6c4009
Packit 6c4009
/* Register a destructor for TLS variables declared with the 'thread_local'
Packit 6c4009
   keyword.  This function is only called from code generated by the C++
Packit 6c4009
   compiler.  FUNC is the destructor function and OBJ is the object to be
Packit 6c4009
   passed to the destructor.  DSO_SYMBOL is the __dso_handle symbol that each
Packit 6c4009
   DSO has at a unique address in its map, added from crtbegin.o during the
Packit 6c4009
   linking phase.  */
Packit 6c4009
int
Packit 6c4009
__cxa_thread_atexit_impl (dtor_func func, void *obj, void *dso_symbol)
Packit 6c4009
{
Packit 6c4009
#ifdef PTR_MANGLE
Packit 6c4009
  PTR_MANGLE (func);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* Prepend.  */
Packit 6c4009
  struct dtor_list *new = calloc (1, sizeof (struct dtor_list));
Packit 6c4009
  new->func = func;
Packit 6c4009
  new->obj = obj;
Packit 6c4009
  new->next = tls_dtor_list;
Packit 6c4009
  tls_dtor_list = new;
Packit 6c4009
Packit 6c4009
  /* We have to acquire the big lock to prevent a racing dlclose from pulling
Packit 6c4009
     our DSO from underneath us while we're setting up our destructor.  */
Packit 6c4009
  __rtld_lock_lock_recursive (GL(dl_load_lock));
Packit 6c4009
Packit 6c4009
  /* See if we already encountered the DSO.  */
Packit 6c4009
  if (__glibc_unlikely (dso_symbol_cache != dso_symbol))
Packit 6c4009
    {
Packit 6c4009
      ElfW(Addr) caller = (ElfW(Addr)) dso_symbol;
Packit 6c4009
Packit 6c4009
      struct link_map *l = _dl_find_dso_for_object (caller);
Packit 6c4009
Packit 6c4009
      /* If the address is not recognized the call comes from the main
Packit 6c4009
	 program (we hope).  */
Packit 6c4009
      lm_cache = l ? l : GL(dl_ns)[LM_ID_BASE]._ns_loaded;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* This increment may only be concurrently observed either by the decrement
Packit 6c4009
     in __call_tls_dtors since the other l_tls_dtor_count access in
Packit 6c4009
     _dl_close_worker is protected by the dl_load_lock.  The execution in
Packit 6c4009
     __call_tls_dtors does not really depend on this value beyond the fact that
Packit 6c4009
     it should be atomic, so Relaxed MO should be sufficient.  */
Packit 6c4009
  atomic_fetch_add_relaxed (&lm_cache->l_tls_dtor_count, 1);
Packit 6c4009
  __rtld_lock_unlock_recursive (GL(dl_load_lock));
Packit 6c4009
Packit 6c4009
  new->map = lm_cache;
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Call the destructors.  This is called either when a thread returns from the
Packit 6c4009
   initial function or when the process exits via the exit function.  */
Packit 6c4009
void
Packit 6c4009
__call_tls_dtors (void)
Packit 6c4009
{
Packit 6c4009
  while (tls_dtor_list)
Packit 6c4009
    {
Packit 6c4009
      struct dtor_list *cur = tls_dtor_list;
Packit 6c4009
      dtor_func func = cur->func;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
      PTR_DEMANGLE (func);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
      tls_dtor_list = tls_dtor_list->next;
Packit 6c4009
      func (cur->obj);
Packit 6c4009
Packit 6c4009
      /* Ensure that the MAP dereference happens before
Packit 6c4009
	 l_tls_dtor_count decrement.  That way, we protect this access from a
Packit 6c4009
	 potential DSO unload in _dl_close_worker, which happens when
Packit 6c4009
	 l_tls_dtor_count is 0.  See CONCURRENCY NOTES for more detail.  */
Packit 6c4009
      atomic_fetch_add_release (&cur->map->l_tls_dtor_count, -1);
Packit 6c4009
      free (cur);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__call_tls_dtors)