opnsense-src/lib/libc/stdlib
Aurélien Croc de Suray 23427c8e1f libc: allow __cxa_atexit handlers to be added during __cxa_finalize
science/dlib-cpp reveals an interesting scenario that works fine on
other platforms but not on FreeBSD; notably, it ends up creating a new
global object from some destructor which is called during
__cxa_finalize.  This breaks when libdlib is dlopen()ed and then
subsequently dlclose()ed, as we never end up invoking the created
object's dtor until program exit when the shlib is already unmapped.

Fix it by noting when we're in the middle of __cxa_finalize for a dso,
and then restarting the search if __cxa_atexit() was called in the
middle somewhere.

We wait until we've processed the initial set before starting over and
processing the newly added handlers as if it were a complete set of
handlers added during runtime.  The alternative is calling them as
they're added to maintain a LIFO in terms of total ordering, but in
theory a constructor could add another global object that also needs to
be destroyed, and that object needs to be destroyed after the one that
constructed it to avoid creating unexpected lifetime issues.

This manifests in the pdlib PHP extension for dlib crashing, see [0].

[0] https://github.com/goodspb/pdlib/issues/39

PR:		285870
Reviewed by:	kevans (also supplied commit message)
MFC after:	1 week
2025-04-04 19:47:53 -05:00
..
malloc jemalloc: Move generated files into lib/libc tree 2024-12-13 16:04:39 -07:00
_Exit.c Remove $FreeBSD$: two-line .h pattern 2023-08-16 11:54:16 -06:00
a64l.3 Remove $FreeBSD$: two-line nroff pattern 2023-08-16 11:55:10 -06:00
a64l.c Remove $FreeBSD$: one-line .c pattern 2023-08-16 11:54:42 -06:00
abort.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
abort.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
abs.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
abs.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
alloca.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
at_quick_exit.3 Remove $FreeBSD$: two-line nroff pattern 2023-08-16 11:55:10 -06:00
atexit.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
atexit.c libc: allow __cxa_atexit handlers to be added during __cxa_finalize 2025-04-04 19:47:53 -05:00
atexit.h lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
atof.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
atof.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
atoi.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
atoi.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
atol.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
atol.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
atoll.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
bsearch.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
bsearch.c bsearch_b: fix compilation with gcc 2024-03-06 19:46:12 +00:00
bsearch_b.c Remove $FreeBSD$: two-line .h pattern 2023-08-16 11:54:16 -06:00
cxa_thread_atexit.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
cxa_thread_atexit_impl.c __cxa_thread_call_dtors(3): fix dtor pointer validity check 2024-05-04 02:23:15 +03:00
div.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
div.c libc: Remove support for pre-C99 C standards 2024-04-12 12:05:09 -07:00
exit.3 exit(3): clarify how to obtain full exit status of the exited process 2024-07-30 02:57:34 +03:00
exit.c exit(3): make it thread-safe 2024-07-30 02:57:34 +03:00
getenv.3 libc: indicate existing functions that are POSIX 2024 2024-11-14 21:32:20 -05:00
getenv.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
getopt.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
getopt.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
getopt_long.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
getopt_long.c lib: Automated cleanup of cdefs and other formatting 2023-11-26 22:23:59 -07:00
getsubopt.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
getsubopt.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
hcreate.3 Remove $FreeBSD$: two-line nroff pattern 2023-08-16 11:55:10 -06:00
hcreate.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
hcreate_r.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
hdestroy_r.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
heapsort.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
heapsort_b.c Remove $FreeBSD$: two-line .h pattern 2023-08-16 11:54:16 -06:00
hsearch.h Remove $FreeBSD$: two-line .h pattern 2023-08-16 11:54:16 -06:00
hsearch_r.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
imaxabs.3 Remove $FreeBSD$: two-line nroff pattern 2023-08-16 11:55:10 -06:00
imaxabs.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
imaxdiv.3 Remove $FreeBSD$: two-line nroff pattern 2023-08-16 11:55:10 -06:00
imaxdiv.c libc: Remove support for pre-C99 C standards 2024-04-12 12:05:09 -07:00
insque.3 Remove $FreeBSD$: two-line nroff pattern 2023-08-16 11:55:10 -06:00
insque.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
l64a.c Remove $FreeBSD$: one-line .c pattern 2023-08-16 11:54:42 -06:00
labs.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
labs.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
ldiv.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
ldiv.c libc: Remove support for pre-C99 C standards 2024-04-12 12:05:09 -07:00
llabs.3 Remove $FreeBSD$: two-line nroff pattern 2023-08-16 11:55:10 -06:00
llabs.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
lldiv.3 Remove $FreeBSD$: two-line nroff pattern 2023-08-16 11:55:10 -06:00
lldiv.c libc: Remove support for pre-C99 C standards 2024-04-12 12:05:09 -07:00
lsearch.3 Remove $FreeBSD$: two-line nroff pattern 2023-08-16 11:55:10 -06:00
lsearch.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
Makefile.inc Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
memory.3 manuals: Fix "unusual .Xr" warnings with a script 2024-10-15 17:18:14 -03:00
merge.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
mergesort_b.c Remove $FreeBSD$: two-line .h pattern 2023-08-16 11:54:16 -06:00
ptsname.3 ptsname.3: accommodate upcoming POSIX Issue 8 ptsname_r 2023-10-13 22:10:21 -04:00
ptsname.c lib: Automated cleanup of cdefs and other formatting 2023-11-26 22:23:59 -07:00
qsort.3 Revise qsort(3 reflect POSIX.1-2024 update. 2024-10-25 00:31:50 -07:00
qsort.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
qsort_r.c Remove $FreeBSD$: two-line .h pattern 2023-08-16 11:54:16 -06:00
qsort_r_compat.c Remove $FreeBSD$: two-line .h pattern 2023-08-16 11:54:16 -06:00
qsort_s.c Remove $FreeBSD$: two-line .h pattern 2023-08-16 11:54:16 -06:00
quick_exit.3 Remove $FreeBSD$: two-line nroff pattern 2023-08-16 11:55:10 -06:00
quick_exit.c libc: Rewrite quick_exit() and at_quick_exit() using C11 atomics. 2023-09-26 22:07:10 +02:00
radixsort.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
radixsort.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
rand.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
rand.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
random.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
random.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
random.h Remove $FreeBSD$: two-line .h pattern 2023-08-16 11:54:16 -06:00
reallocarray.3 libc: indicate existing functions that are POSIX 2024 2024-11-14 21:32:20 -05:00
reallocarray.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
reallocf.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
reallocf.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
realpath.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
realpath.c libc: use __sys___realpathat directly in realpath 2025-02-19 16:55:04 +00:00
remque.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
set_constraint_handler_s.3 Remove $FreeBSD$: two-line nroff pattern 2023-08-16 11:55:10 -06:00
set_constraint_handler_s.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
strfmon.3 strfmon.3: Cleanup example code 2023-12-09 03:06:13 +02:00
strfmon.c strfmon.c: Use the restrict keyword directly 2023-12-10 23:28:25 +02:00
strtod.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
strtoimax.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
strtol.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
strtol.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
strtold.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
strtoll.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
strtonum.3 Remove $FreeBSD$: one-line nroff pattern 2023-08-16 11:55:15 -06:00
strtonum.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
strtoq.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
strtoul.3 lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
strtoul.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
strtoull.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
strtoumax.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
strtouq.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
Symbol.map libc: Remove empty comments in Symbol.map 2023-11-15 17:51:03 +00:00
system.3 system(3): fix typo santized -> sanitized 2024-01-12 11:29:22 -07:00
system.c lib: Remove ancient SCCS tags. 2023-11-26 22:23:28 -07:00
tdelete.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
tfind.c Remove $FreeBSD$: one-line .c pattern 2023-08-16 11:54:42 -06:00
tsearch.3 Remove $FreeBSD$: one-line nroff pattern 2023-08-16 11:55:15 -06:00
tsearch.c libc: Purge unneeded cdefs.h 2023-11-01 16:44:30 -06:00
tsearch_path.h Remove $FreeBSD$: two-line .h pattern 2023-08-16 11:54:16 -06:00
twalk.c Remove $FreeBSD$: one-line .c pattern 2023-08-16 11:54:42 -06:00