From b168057aaacf1d149246d0eb3e84332492d35063 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Fri, 2 Jan 2015 16:28:19 +0000 Subject: Update copyright dates with scripts/update-copyrights. --- include/link.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/link.h') diff --git a/include/link.h b/include/link.h index 670d40157b..7ccd9c3760 100644 --- a/include/link.h +++ b/include/link.h @@ -1,6 +1,6 @@ /* Data structure for communication from the run-time dynamic linker for loaded ELF shared objects. - Copyright (C) 1995-2014 Free Software Foundation, Inc. + Copyright (C) 1995-2015 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 -- cgit v1.2.3 From c01ae97eb8986600488332f4db98f4b6d6ebedbc Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Tue, 14 Jul 2015 13:15:26 -0700 Subject: Factor file identity rules out of generic rtld code. --- ChangeLog | 11 ++++++++++ elf/dl-load.c | 18 ++++++++-------- include/link.h | 4 ++-- sysdeps/generic/dl-fileid.h | 46 +++++++++++++++++++++++++++++++++++++++++ sysdeps/nacl/dl-fileid.h | 8 ++++++++ sysdeps/posix/dl-fileid.h | 50 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 125 insertions(+), 12 deletions(-) create mode 100644 sysdeps/generic/dl-fileid.h create mode 100644 sysdeps/nacl/dl-fileid.h create mode 100644 sysdeps/posix/dl-fileid.h (limited to 'include/link.h') diff --git a/ChangeLog b/ChangeLog index 120588a4d4..3bc1ba973b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2015-07-14 Roland McGrath + + * sysdeps/generic/dl-fileid.h: New file. + * sysdeps/posix/dl-fileid.h: New file. + * sysdeps/nacl/dl-fileid.h: New file. + * include/link.h: Include . + (struct link_map): Replace l_dev and l_ino with l_file_id. + * elf/dl-load.c (_dl_map_object_from_fd): Use _dl_get_file_id rather + than __fxstat64. Use _dl_file_id_match_p rather than comparing l_dev + and l_ino directly. Initialize l_file_id rather than l_dev and l_ino. + 2015-07-14 Siddhesh Poyarekar * stdlib/tst-tls-atexit.c (do_test): Fix typo. diff --git a/elf/dl-load.c b/elf/dl-load.c index 41b91fcc86..0c052e449a 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -872,7 +872,6 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp, const ElfW(Phdr) *ph; size_t maplength; int type; - struct stat64 st; /* Initialize to keep the compiler happy. */ const char *errstring = NULL; int errval = 0; @@ -880,7 +879,8 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp, bool make_consistent = false; /* Get file information. */ - if (__glibc_unlikely (__fxstat64 (_STAT_VER, fd, &st) < 0)) + struct r_file_id id; + if (__glibc_unlikely (!_dl_get_file_id (fd, &id))) { errstring = N_("cannot stat shared object"); call_lose_errno: @@ -891,8 +891,8 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp, } /* Look again to see if the real name matched another already loaded. */ - for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next) - if (l->l_removed == 0 && l->l_ino == st.st_ino && l->l_dev == st.st_dev) + for (l = GL(dl_ns)[nsid]._ns_loaded; l != NULL; l = l->l_next) + if (!l->l_removed && _dl_file_id_match_p (&l->l_file_id, &id)) { /* The object is already loaded. Just bump its reference count and return it. */ @@ -910,8 +910,7 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp, /* When loading into a namespace other than the base one we must avoid loading ld.so since there can only be one copy. Ever. */ if (__glibc_unlikely (nsid != LM_ID_BASE) - && ((st.st_ino == GL(dl_rtld_map).l_ino - && st.st_dev == GL(dl_rtld_map).l_dev) + && (_dl_file_id_match_p (&id, &GL(dl_rtld_map).l_file_id) || _dl_name_match_p (name, &GL(dl_rtld_map)))) { /* This is indeed ld.so. Create a new link_map which refers to @@ -1220,7 +1219,7 @@ cannot allocate TLS data structures for initial thread"); l_map_start, l_map_end, l_addr, l_contiguous, l_text_end, l_phdr */ errstring = _dl_map_segments (l, fd, header, type, loadcmds, nloadcmds, - maplength, has_holes, loader); + maplength, has_holes, loader); if (__glibc_unlikely (errstring != NULL)) goto call_lose; } @@ -1390,8 +1389,7 @@ cannot enable executable stack as shared object requires"); GL(dl_initfirst) = l; /* Finally the file information. */ - l->l_dev = st.st_dev; - l->l_ino = st.st_ino; + l->l_file_id = id; /* When we profile the SONAME might be needed for something else but loading. Add it right away. */ @@ -1890,7 +1888,7 @@ open_path (const char *name, size_t namelen, int mode, free (sps->dirs); /* rtld_search_dirs and env_path_list are attribute_relro, therefore - avoid writing into it. */ + avoid writing into it. */ if (sps != &rtld_search_dirs && sps != &env_path_list) sps->dirs = (void *) -1; } diff --git a/include/link.h b/include/link.h index 7ccd9c3760..423a69585d 100644 --- a/include/link.h +++ b/include/link.h @@ -40,6 +40,7 @@ extern unsigned int la_objopen (struct link_map *__map, Lmid_t __lmid, #include #include #include +#include #include #include #include @@ -235,8 +236,7 @@ struct link_map /* This information is kept to check for sure whether a shared object is the same as one already loaded. */ - dev_t l_dev; - ino64_t l_ino; + struct r_file_id l_file_id; /* Collected information about own RUNPATH directories. */ struct r_search_path_struct l_runpath_dirs; diff --git a/sysdeps/generic/dl-fileid.h b/sysdeps/generic/dl-fileid.h new file mode 100644 index 0000000000..2cbd21dcbe --- /dev/null +++ b/sysdeps/generic/dl-fileid.h @@ -0,0 +1,46 @@ +/* File identity for the dynamic linker. Stub version. + Copyright (C) 2015 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 + . */ + +#include + +/* This type stores whatever information is fetched by _dl_get_file_id + and compared by _dl_file_id_match_p. */ +struct r_file_id + { + /* In the stub version, we don't record anything at all. */ + }; + +/* Sample FD to fill in *ID. Returns true on success. + On error, returns false, with errno set. */ +static inline bool +_dl_get_file_id (int fd __attribute__ ((unused)), + struct r_file_id *id __attribute__ ((unused))) +{ + return true; +} + +/* Compare two results from _dl_get_file_id for equality. + It's crucial that this never return false-positive matches. + It's ideal that it never return false-negative mismatches either, + but lack of matches is survivable. */ +static inline bool +_dl_file_id_match_p (const struct r_file_id *a __attribute__ ((unused)), + const struct r_file_id *b __attribute__ ((unused))) +{ + return false; +} diff --git a/sysdeps/nacl/dl-fileid.h b/sysdeps/nacl/dl-fileid.h new file mode 100644 index 0000000000..4c34581701 --- /dev/null +++ b/sysdeps/nacl/dl-fileid.h @@ -0,0 +1,8 @@ +/* Bypass sysdeps/posix/dl-fileid.h, which relies on st_dev/st_ino being + reliable. Under NaCl, we cannot always expect them to be useful. + Fortunately, in the ways NaCl is used it's far less likely that two + different names for the same file would be used in dlopen or the like, + so failing to notice re-opening the same file is not so likely to be a + problem in practice. */ + +#include diff --git a/sysdeps/posix/dl-fileid.h b/sysdeps/posix/dl-fileid.h new file mode 100644 index 0000000000..d0d5436641 --- /dev/null +++ b/sysdeps/posix/dl-fileid.h @@ -0,0 +1,50 @@ +/* File identity for the dynamic linker. Generic POSIX.1 version. + Copyright (C) 2015 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 + . */ + +#include +#include + +/* For POSIX.1 systems, the pair of st_dev and st_ino constitute + a unique identifier for a file. */ +struct r_file_id + { + dev_t dev; + ino64_t ino; + }; + +/* Sample FD to fill in *ID. Returns true on success. + On error, returns false, with errno set. */ +static inline bool +_dl_get_file_id (int fd, struct r_file_id *id) +{ + struct stat64 st; + + if (__glibc_unlikely (__fxstat64 (_STAT_VER, fd, &st) < 0)) + return false; + + id->dev = st.st_dev; + id->ino = st.st_ino; + return true; +} + +/* Compare two results from _dl_get_file_id for equality. */ +static inline bool +_dl_file_id_match_p (const struct r_file_id *a, const struct r_file_id *b) +{ + return a->dev == b->dev && a->ino == b->ino; +} -- cgit v1.2.3 From 90b37cac8b5a3e1548c29d91e3e0bff1014d2e5c Mon Sep 17 00:00:00 2001 From: Siddhesh Poyarekar Date: Thu, 23 Jul 2015 11:16:18 +0530 Subject: Also use l_tls_dtor_count to decide on object unload (BZ #18657) When an TLS destructor is registered, we set the DF_1_NODELETE flag to signal that the object should not be destroyed. We then clear the DF_1_NODELETE flag when all destructors are called, which is wrong - the flag could have been set by other means too. This patch replaces this use of the flag by using l_tls_dtor_count directly to determine whether it is safe to unload the object. This change has the added advantage of eliminating the lock taking when calling the destructors, which could result in a deadlock. The patch also fixes the test case tst-tls-atexit - it was making an invalid dlclose call, which would just return an error silently. I have also added a detailed note on concurrency which also aims to justify why I chose the semantics I chose for accesses to l_tls_dtor_count. Thanks to Torvald for his help in getting me started on this and (literally) teaching my how to approach the problem. Change verified on x86_64; the test suite does not show any regressions due to the patch. ChangeLog: [BZ #18657] * elf/dl-close.c (_dl_close_worker): Don't unload DSO if there are pending TLS destructor calls. * include/link.h (struct link_map): Add concurrency note for L_TLS_DTOR_COUNT. * stdlib/cxa_thread_atexit_impl.c (__cxa_thread_atexit_impl): Don't touch the link map flag. Atomically increment l_tls_dtor_count. (__call_tls_dtors): Atomically decrement l_tls_dtor_count. Avoid taking the load lock and don't touch the link map flag. * stdlib/tst-tls-atexit-nodelete.c: New test case. * stdlib/Makefile (tests): Use it. * stdlib/tst-tls-atexit.c (do_test): dlopen tst-tls-atexit-lib.so again before dlclose. Add conditionals to allow tst-tls-atexit-nodelete test case to use it. --- ChangeLog | 18 ++++++++ NEWS | 2 +- elf/dl-close.c | 9 +++- include/link.h | 4 +- stdlib/Makefile | 5 ++- stdlib/cxa_thread_atexit_impl.c | 90 ++++++++++++++++++++++++++++++++-------- stdlib/tst-tls-atexit-nodelete.c | 24 +++++++++++ stdlib/tst-tls-atexit.c | 60 ++++++++++++++++++++------- 8 files changed, 176 insertions(+), 36 deletions(-) create mode 100644 stdlib/tst-tls-atexit-nodelete.c (limited to 'include/link.h') diff --git a/ChangeLog b/ChangeLog index 652d968604..25abba6213 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2015-07-23 Siddhesh Poyarekar + + [BZ #18657] + * elf/dl-close.c (_dl_close_worker): Don't unload DSO if there + are pending TLS destructor calls. + * include/link.h (struct link_map): Add concurrency note for + L_TLS_DTOR_COUNT. + * stdlib/cxa_thread_atexit_impl.c (__cxa_thread_atexit_impl): + Don't touch the link map flag. Atomically increment + l_tls_dtor_count. + (__call_tls_dtors): Atomically decrement l_tls_dtor_count. + Avoid taking the load lock and don't touch the link map flag. + * stdlib/tst-tls-atexit-nodelete.c: New test case. + * stdlib/Makefile (tests): Use it. + * stdlib/tst-tls-atexit.c (do_test): dlopen + tst-tls-atexit-lib.so again before dlclose. Add conditionals + to allow tst-tls-atexit-nodelete test case to use it. + 2015-07-22 Mike Frysinger * sysdeps/unix/sysv/linux/ia64/bits/msq.h: Change sys/types.h include diff --git a/NEWS b/NEWS index 007d57d109..fbe8484656 100644 --- a/NEWS +++ b/NEWS @@ -27,7 +27,7 @@ Version 2.22 18522, 18527, 18528, 18529, 18530, 18532, 18533, 18534, 18536, 18539, 18540, 18542, 18544, 18545, 18546, 18547, 18549, 18553, 18557, 18558, 18569, 18583, 18585, 18586, 18592, 18593, 18594, 18602, 18612, 18613, - 18619, 18633, 18641, 18643, 18648, 18676, 18694, 18696. + 18619, 18633, 18641, 18643, 18648, 18657, 18676, 18694, 18696. * Cache information can be queried via sysconf() function on s390 e.g. with _SC_LEVEL1_ICACHE_SIZE as argument. diff --git a/elf/dl-close.c b/elf/dl-close.c index 2104674bd0..910527746e 100644 --- a/elf/dl-close.c +++ b/elf/dl-close.c @@ -153,7 +153,11 @@ _dl_close_worker (struct link_map *map, bool force) maps[idx] = l; ++idx; - /* Clear DF_1_NODELETE to force object deletion. */ + /* Clear DF_1_NODELETE to force object deletion. We don't need to touch + l_tls_dtor_count because forced object deletion only happens when an + error occurs during object load. Destructor registration for TLS + non-POD objects should not have happened till then for this + object. */ if (force) l->l_flags_1 &= ~DF_1_NODELETE; } @@ -177,6 +181,9 @@ _dl_close_worker (struct link_map *map, bool force) if (l->l_type == lt_loaded && l->l_direct_opencount == 0 && (l->l_flags_1 & DF_1_NODELETE) == 0 + /* See CONCURRENCY NOTES in cxa_thread_atexit_impl.c to know why + acquire is sufficient and correct. */ + && atomic_load_acquire (&l->l_tls_dtor_count) == 0 && !used[done_index]) continue; diff --git a/include/link.h b/include/link.h index 423a69585d..7e7eb79f10 100644 --- a/include/link.h +++ b/include/link.h @@ -302,7 +302,9 @@ struct link_map /* Index of the module in the dtv array. */ size_t l_tls_modid; - /* Number of thread_local objects constructed by this DSO. */ + /* Number of thread_local objects constructed by this DSO. This is + atomically accessed and modified and is not always protected by the load + lock. See also: CONCURRENCY NOTES in cxa_thread_atexit_impl.c. */ size_t l_tls_dtor_count; /* Information used to change permission after the relocations are diff --git a/stdlib/Makefile b/stdlib/Makefile index 7fc5a807fa..402466a623 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -74,7 +74,7 @@ tests := tst-strtol tst-strtod testmb testrand testsort testdiv \ tst-makecontext3 bug-getcontext bug-fmtmsg1 \ tst-secure-getenv tst-strtod-overflow tst-strtod-round \ tst-tininess tst-strtod-underflow tst-tls-atexit \ - tst-setcontext3 + tst-setcontext3 tst-tls-atexit-nodelete tests-static := tst-secure-getenv modules-names = tst-tls-atexit-lib @@ -159,6 +159,9 @@ tst-tls-atexit-lib.so-no-z-defs = yes $(objpfx)tst-tls-atexit: $(shared-thread-library) $(libdl) $(objpfx)tst-tls-atexit.out: $(objpfx)tst-tls-atexit-lib.so +$(objpfx)tst-tls-atexit-nodelete: $(shared-thread-library) $(libdl) +$(objpfx)tst-tls-atexit-nodelete.out: $(objpfx)tst-tls-atexit-lib.so + $(objpfx)tst-setcontext3.out: tst-setcontext3.sh $(objpfx)tst-setcontext3 $(SHELL) $< $(common-objpfx) '$(test-program-prefix-before-env)' \ '$(run-program-env)' '$(test-program-prefix-after-env)' \ diff --git a/stdlib/cxa_thread_atexit_impl.c b/stdlib/cxa_thread_atexit_impl.c index 70c97dabc2..8e26380799 100644 --- a/stdlib/cxa_thread_atexit_impl.c +++ b/stdlib/cxa_thread_atexit_impl.c @@ -16,6 +16,61 @@ License along with the GNU C Library; if not, see . */ +/* CONCURRENCY NOTES: + + This documents concurrency for the non-POD TLS destructor registration, + calling and destruction. The functions __cxa_thread_atexit_impl, + _dl_close_worker and __call_tls_dtors are the three main routines that may + run concurrently and access shared data. The shared data in all possible + combinations of all three functions are the link map list, a link map for a + DSO and the link map member l_tls_dtor_count. + + __cxa_thread_atexit_impl acquires the load_lock before accessing any shared + state and hence multiple of its instances can safely execute concurrently. + + _dl_close_worker acquires the load_lock before accessing any shared state as + well and hence can concurrently execute multiple of its own instances as + well as those of __cxa_thread_atexit_impl safely. Not all accesses to + l_tls_dtor_count are protected by the load lock, so we need to synchronize + using atomics. + + __call_tls_dtors accesses the l_tls_dtor_count without taking the lock; it + decrements the value by one. It does not need the big lock because it does + not access any other shared state except for the current DSO link map and + its member l_tls_dtor_count. + + Correspondingly, _dl_close_worker loads l_tls_dtor_count and if it is zero, + unloads the DSO, thus deallocating the current link map. This is the goal + of maintaining l_tls_dtor_count - to unload the DSO and free resources if + there are no pending destructors to be called. + + We want to eliminate the inconsistent state where the DSO is unloaded in + _dl_close_worker before it is used in __call_tls_dtors. This could happen + if __call_tls_dtors uses the link map after it sets l_tls_dtor_count to 0, + since _dl_close_worker will conclude from the 0 l_tls_dtor_count value that + it is safe to unload the DSO. Hence, to ensure that this does not happen, + the following conditions must be met: + + 1. In _dl_close_worker, the l_tls_dtor_count load happens before the DSO + is unload and its link map is freed + 2. The link map dereference in __call_tls_dtors happens before the + l_tls_dtor_count dereference. + + To ensure this, the l_tls_dtor_count decrement in __call_tls_dtors should + have release semantics and the load in _dl_close_worker should have acquire + semantics. + + Concurrent executions of __call_tls_dtors should only ensure that the value + is accessed atomically; no reordering constraints need to be considered. + Likewise for the increment of l_tls_dtor_count in __cxa_thread_atexit_impl. + + There is still a possibility on concurrent execution of _dl_close_worker and + __call_tls_dtors where _dl_close_worker reads the value of l_tls_dtor_count + as 1, __call_tls_dtors decrements the value of l_tls_dtor_count but + _dl_close_worker does not unload the DSO, having read the old value. This + is not very different from a case where __call_tls_dtors is called after + _dl_close_worker on the DSO and hence is an accepted execution. */ + #include #include @@ -49,9 +104,11 @@ __cxa_thread_atexit_impl (dtor_func func, void *obj, void *dso_symbol) new->next = tls_dtor_list; tls_dtor_list = new; - /* See if we already encountered the DSO. */ + /* We have to acquire the big lock to prevent a racing dlclose from pulling + our DSO from underneath us while we're setting up our destructor. */ __rtld_lock_lock_recursive (GL(dl_load_lock)); + /* See if we already encountered the DSO. */ if (__glibc_unlikely (dso_symbol_cache != dso_symbol)) { ElfW(Addr) caller = (ElfW(Addr)) dso_symbol; @@ -62,16 +119,17 @@ __cxa_thread_atexit_impl (dtor_func func, void *obj, void *dso_symbol) program (we hope). */ lm_cache = l ? l : GL(dl_ns)[LM_ID_BASE]._ns_loaded; } - /* A destructor could result in a thread_local construction and the former - could have cleared the flag. */ - if (lm_cache->l_type == lt_loaded && lm_cache->l_tls_dtor_count == 0) - lm_cache->l_flags_1 |= DF_1_NODELETE; - - new->map = lm_cache; - new->map->l_tls_dtor_count++; + /* This increment may only be concurrently observed either by the decrement + in __call_tls_dtors since the other l_tls_dtor_count access in + _dl_close_worker is protected by the load lock. The execution in + __call_tls_dtors does not really depend on this value beyond the fact that + it should be atomic, so Relaxed MO should be sufficient. */ + atomic_fetch_add_relaxed (&lm_cache->l_tls_dtor_count, 1); __rtld_lock_unlock_recursive (GL(dl_load_lock)); + new->map = lm_cache; + return 0; } @@ -83,19 +141,15 @@ __call_tls_dtors (void) while (tls_dtor_list) { struct dtor_list *cur = tls_dtor_list; - tls_dtor_list = tls_dtor_list->next; + tls_dtor_list = tls_dtor_list->next; cur->func (cur->obj); - __rtld_lock_lock_recursive (GL(dl_load_lock)); - - /* Allow DSO unload if count drops to zero. */ - cur->map->l_tls_dtor_count--; - if (cur->map->l_tls_dtor_count == 0 && cur->map->l_type == lt_loaded) - cur->map->l_flags_1 &= ~DF_1_NODELETE; - - __rtld_lock_unlock_recursive (GL(dl_load_lock)); - + /* Ensure that the MAP dereference happens before + l_tls_dtor_count decrement. That way, we protect this access from a + potential DSO unload in _dl_close_worker, which happens when + l_tls_dtor_count is 0. See CONCURRENCY NOTES for more detail. */ + atomic_fetch_add_release (&cur->map->l_tls_dtor_count, -1); free (cur); } } diff --git a/stdlib/tst-tls-atexit-nodelete.c b/stdlib/tst-tls-atexit-nodelete.c new file mode 100644 index 0000000000..ff290fa987 --- /dev/null +++ b/stdlib/tst-tls-atexit-nodelete.c @@ -0,0 +1,24 @@ +/* Verify that a RTLD_NODELETE DSO is not unloaded even if its TLS objects are + destroyed. + + Copyright (C) 2015 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 + . */ + +#define NO_DELETE 1 +#define H2_RTLD_FLAGS (RTLD_LAZY | RTLD_NODELETE) +#define LOADED_IS_GOOD true +#include "tst-tls-atexit.c" diff --git a/stdlib/tst-tls-atexit.c b/stdlib/tst-tls-atexit.c index cea655decc..e9839d8b15 100644 --- a/stdlib/tst-tls-atexit.c +++ b/stdlib/tst-tls-atexit.c @@ -16,12 +16,20 @@ License along with the GNU C Library; if not, see . */ -/* This test dynamically loads a DSO and spawns a thread that subsequently - calls into the DSO to register a destructor for an object in the DSO and - then calls dlclose on the handle for the DSO. When the thread exits, the - DSO should not be unloaded or else the destructor called during thread exit - will crash. Further in the main thread, the DSO is opened and closed again, - at which point the DSO should be unloaded. */ +/* For the default case, i.e. NO_DELETE not defined, the test dynamically loads + a DSO and spawns a thread that subsequently calls into the DSO to register a + destructor for an object in the DSO and then calls dlclose on the handle for + the DSO. When the thread exits, the DSO should not be unloaded or else the + destructor called during thread exit will crash. Further in the main + thread, the DSO is opened and closed again, at which point the DSO should be + unloaded. + + When NO_DELETE is defined, the DSO is loaded twice, once with just RTLD_LAZY + flag and the second time with the RTLD_NODELETE flag set. The thread is + spawned, destructor registered and then thread exits without closing the + DSO. In the main thread, the first handle is then closed, followed by the + second handle. In the end, the DSO should remain loaded due to the + RTLD_NODELETE flag being set in the second dlopen call. */ #include #include @@ -31,6 +39,14 @@ #include #include +#ifndef NO_DELETE +# define LOADED_IS_GOOD false +#endif + +#ifndef H2_RTLD_FLAGS +# define H2_RTLD_FLAGS (RTLD_LAZY) +#endif + #define DSO_NAME "$ORIGIN/tst-tls-atexit-lib.so" /* Walk through the map in the _r_debug structure to see if our lib is still @@ -43,7 +59,10 @@ is_loaded (void) for (; lm; lm = lm->l_next) if (lm->l_type == lt_loaded && lm->l_name && strcmp (basename (DSO_NAME), basename (lm->l_name)) == 0) - return true; + { + printf ("%s is still loaded\n", lm->l_name); + return true; + } return false; } @@ -63,7 +82,9 @@ reg_dtor_and_close (void *h) reg_dtor (); +#ifndef NO_DELETE dlclose (h); +#endif return NULL; } @@ -104,19 +125,30 @@ do_test (void) return 1; } +#ifndef NO_DELETE if (spawn_thread (h1) != 0) return 1; +#endif - /* Now this should unload the DSO. FIXME: This is a bug, calling dlclose - like this is actually wrong, but it works because cxa_thread_atexit_impl - has a bug which results in dlclose allowing this to work. */ - dlclose (h1); + void *h2 = dlopen (DSO_NAME, H2_RTLD_FLAGS); + if (h2 == NULL) + { + printf ("h2: Unable to load DSO: %s\n", dlerror ()); + return 1; + } - /* Check link maps to ensure that the DSO has unloaded. */ - if (is_loaded ()) +#ifdef NO_DELETE + if (spawn_thread (h1) != 0) return 1; - return 0; + dlclose (h1); +#endif + dlclose (h2); + + /* Check link maps to ensure that the DSO has unloaded. In the normal case, + the DSO should be unloaded if there are no uses. However, if one of the + dlopen calls were with RTLD_NODELETE, the DSO should remain loaded. */ + return is_loaded () == LOADED_IS_GOOD ? 0 : 1; } #define TEST_FUNCTION do_test () -- cgit v1.2.3