diff --git a/libc/intrin/cxaatexit.internal.h b/libc/intrin/cxaatexit.internal.h index f9ddf76a3..af6bbb5ad 100644 --- a/libc/intrin/cxaatexit.internal.h +++ b/libc/intrin/cxaatexit.internal.h @@ -21,7 +21,10 @@ extern struct CxaAtexitBlocks __cxa_blocks; void __cxa_lock(void); void __cxa_unlock(void); +void __cxa_thread_finalize(void); void __cxa_printexits(FILE *, void *); +int __cxa_thread_atexit(void *, void *, void *); +int __cxa_thread_atexit_impl(void *, void *, void *); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/log/leaks.c b/libc/log/leaks.c index 048f18410..41a72138b 100644 --- a/libc/log/leaks.c +++ b/libc/log/leaks.c @@ -20,6 +20,7 @@ #include "libc/intrin/asan.internal.h" #include "libc/intrin/bits.h" #include "libc/intrin/cmpxchg.h" +#include "libc/intrin/cxaatexit.internal.h" #include "libc/intrin/kprintf.h" #include "libc/intrin/strace.internal.h" #include "libc/mem/mem.h" @@ -94,6 +95,7 @@ dontasan void CheckForMemoryLeaks(void) { exit(0); } _pthread_unwind(_pthread_self()); + __cxa_thread_finalize(); _pthread_unkey(__get_tls()); _pthread_ungarbage(); __cxa_finalize(0); diff --git a/libc/thread/__cxa_thread_atexit.c b/libc/thread/__cxa_thread_atexit.c new file mode 100644 index 000000000..c557341c4 --- /dev/null +++ b/libc/thread/__cxa_thread_atexit.c @@ -0,0 +1,23 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2023 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/intrin/cxaatexit.internal.h" + +int __cxa_thread_atexit(void *fun, void *arg, void *dso_symbol) { + return __cxa_thread_atexit_impl(fun, arg, dso_symbol); +} diff --git a/libc/thread/__cxa_thread_atexit_impl.c b/libc/thread/__cxa_thread_atexit_impl.c new file mode 100644 index 000000000..b08f2b3fd --- /dev/null +++ b/libc/thread/__cxa_thread_atexit_impl.c @@ -0,0 +1,47 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2023 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/intrin/cxaatexit.internal.h" +#include "libc/mem/mem.h" + +struct Dtor { + void *fun; + void *arg; + struct Dtor *next; +}; + +static _Thread_local struct Dtor *__cxa_thread_atexit_list; + +void __cxa_thread_finalize(void) { + struct Dtor *dtor; + while ((dtor = __cxa_thread_atexit_list)) { + __cxa_thread_atexit_list = dtor->next; + ((void (*)(void *))dtor->fun)(dtor->arg); + free(dtor); + } +} + +int __cxa_thread_atexit_impl(void *fun, void *arg, void *dso_symbol) { + struct Dtor *dtor; + if (!(dtor = malloc(sizeof(struct Dtor)))) return -1; + dtor->fun = fun; + dtor->arg = arg; + dtor->next = __cxa_thread_atexit_list; + __cxa_thread_atexit_list = dtor; + return 0; +} diff --git a/libc/thread/pthread_exit.c b/libc/thread/pthread_exit.c index 6e8e9489b..3c95aee9c 100644 --- a/libc/thread/pthread_exit.c +++ b/libc/thread/pthread_exit.c @@ -20,6 +20,7 @@ #include "libc/atomic.h" #include "libc/dce.h" #include "libc/intrin/atomic.h" +#include "libc/intrin/cxaatexit.internal.h" #include "libc/intrin/strace.internal.h" #include "libc/intrin/weaken.h" #include "libc/limits.h" @@ -110,6 +111,9 @@ wontreturn void pthread_exit(void *rc) { // free resources _pthread_unwind(pt); + if (_weaken(__cxa_thread_finalize)) { + _weaken(__cxa_thread_finalize)(); + } _pthread_unkey(tib); _pthread_ungarbage(); _pthread_decimate(); diff --git a/test/libc/thread/pthread_exit_test.c b/test/libc/thread/pthread_exit_test.c index 786a2c588..61bd4f0f3 100644 --- a/test/libc/thread/pthread_exit_test.c +++ b/test/libc/thread/pthread_exit_test.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/intrin/cxaatexit.internal.h" #include "libc/runtime/runtime.h" #include "libc/testlib/subprocess.h" #include "libc/testlib/testlib.h" @@ -66,3 +67,21 @@ TEST(pthread_exit, detachedOrphanedChild_runsAtexitHandlers) { pthread_exit(0); EXITS(CHILD); } + +void OnMainThreadExit(void *arg) { + _Exit((long)arg); +} + +TEST(__cxa_thread_atexit, exit_wontInvokeThreadDestructors) { + SPAWN(fork); + __cxa_thread_atexit(OnMainThreadExit, (void *)123L, 0); + exit(0); + EXITS(0); +} + +TEST(__cxa_thread_atexit, pthread_exit_willInvokeThreadDestructors) { + SPAWN(fork); + __cxa_thread_atexit(OnMainThreadExit, (void *)123L, 0); + pthread_exit(0); + EXITS(123); +}