Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <pthread.h> |
| 30 | |
Elliott Hughes | 61fb3fc | 2013-11-07 12:28:46 -0800 | [diff] [blame] | 31 | #include <signal.h> |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 32 | #include <stdlib.h> |
Elliott Hughes | f86c449 | 2016-02-25 22:05:28 -0800 | [diff] [blame] | 33 | #include <string.h> |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 34 | #include <sys/mman.h> |
| 35 | |
Florian Mayer | a9e144d | 2024-11-11 13:39:05 -0800 | [diff] [blame] | 36 | #include "platform/bionic/mte.h" |
Peter Collingbourne | 5d3aa86 | 2020-09-11 15:05:17 -0700 | [diff] [blame] | 37 | #include "private/ScopedRWLock.h" |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 38 | #include "private/ScopedSignalBlocker.h" |
Florian Mayer | a9e144d | 2024-11-11 13:39:05 -0800 | [diff] [blame] | 39 | #include "private/bionic_constants.h" |
| 40 | #include "private/bionic_defs.h" |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 41 | #include "pthread_internal.h" |
| 42 | |
Elliott Hughes | 6203e7b | 2014-05-30 14:49:00 -0700 | [diff] [blame] | 43 | extern "C" __noreturn void _exit_with_stack_teardown(void*, size_t); |
| 44 | extern "C" __noreturn void __exit(int); |
Christopher Ferris | 101fb7d | 2013-12-06 18:54:48 -0800 | [diff] [blame] | 45 | extern "C" int __set_tid_address(int*); |
Dmitriy Ivanov | df79c33 | 2015-03-25 17:38:10 -0700 | [diff] [blame] | 46 | extern "C" void __cxa_thread_finalize(); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 47 | |
| 48 | /* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions |
| 49 | * and thread cancelation |
| 50 | */ |
| 51 | |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 52 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 53 | void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t routine, void* arg) { |
| 54 | pthread_internal_t* thread = __get_thread(); |
| 55 | c->__cleanup_routine = routine; |
| 56 | c->__cleanup_arg = arg; |
| 57 | c->__cleanup_prev = thread->cleanup_stack; |
| 58 | thread->cleanup_stack = c; |
| 59 | } |
| 60 | |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 61 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 62 | void __pthread_cleanup_pop(__pthread_cleanup_t* c, int execute) { |
| 63 | pthread_internal_t* thread = __get_thread(); |
| 64 | thread->cleanup_stack = c->__cleanup_prev; |
| 65 | if (execute) { |
| 66 | c->__cleanup_routine(c->__cleanup_arg); |
| 67 | } |
| 68 | } |
| 69 | |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 70 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
Florian Mayer | a9e144d | 2024-11-11 13:39:05 -0800 | [diff] [blame] | 71 | __attribute__((no_sanitize("memtag"))) void pthread_exit(void* return_value) { |
Dmitriy Ivanov | df79c33 | 2015-03-25 17:38:10 -0700 | [diff] [blame] | 72 | // Call dtors for thread_local objects first. |
| 73 | __cxa_thread_finalize(); |
| 74 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 75 | pthread_internal_t* thread = __get_thread(); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 76 | thread->return_value = return_value; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 77 | |
Dmitriy Ivanov | df79c33 | 2015-03-25 17:38:10 -0700 | [diff] [blame] | 78 | // Call the cleanup handlers. |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 79 | while (thread->cleanup_stack) { |
| 80 | __pthread_cleanup_t* c = thread->cleanup_stack; |
| 81 | thread->cleanup_stack = c->__cleanup_prev; |
| 82 | c->__cleanup_routine(c->__cleanup_arg); |
| 83 | } |
| 84 | |
| 85 | // Call the TLS destructors. It is important to do that before removing this |
| 86 | // thread from the global list. This will ensure that if someone else deletes |
| 87 | // a TLS key, the corresponding value will be set to NULL in this thread's TLS |
| 88 | // space (see pthread_key_delete). |
| 89 | pthread_key_clean_all(); |
| 90 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 91 | if (thread->alternate_signal_stack != nullptr) { |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 92 | // Tell the kernel to stop using the alternate signal stack. |
Elliott Hughes | 2c6c953 | 2016-02-25 21:51:50 -0800 | [diff] [blame] | 93 | stack_t ss; |
| 94 | memset(&ss, 0, sizeof(ss)); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 95 | ss.ss_flags = SS_DISABLE; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 96 | sigaltstack(&ss, nullptr); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 97 | |
| 98 | // Free it. |
Yabin Cui | ef11500 | 2015-03-30 20:03:57 -0700 | [diff] [blame] | 99 | munmap(thread->alternate_signal_stack, SIGNAL_STACK_SIZE); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 100 | thread->alternate_signal_stack = nullptr; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 103 | ThreadJoinState old_state = THREAD_NOT_JOINED; |
| 104 | while (old_state == THREAD_NOT_JOINED && |
| 105 | !atomic_compare_exchange_weak(&thread->join_state, &old_state, THREAD_EXITED_NOT_JOINED)) { |
| 106 | } |
| 107 | |
Peter Collingbourne | 5d3aa86 | 2020-09-11 15:05:17 -0700 | [diff] [blame] | 108 | // android_run_on_all_threads() needs to see signals blocked atomically with setting the |
| 109 | // terminating flag, so take the creation lock while doing these operations. |
| 110 | { |
| 111 | ScopedReadLock locker(&g_thread_creation_lock); |
| 112 | atomic_store(&thread->terminating, true); |
| 113 | |
| 114 | // We don't want to take a signal after unmapping the stack, the shadow call stack, or dynamic |
| 115 | // TLS memory. |
| 116 | sigset64_t set; |
| 117 | sigfillset64(&set); |
| 118 | __rt_sigprocmask(SIG_BLOCK, &set, nullptr, sizeof(sigset64_t)); |
| 119 | } |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 120 | |
Elliott Hughes | 9a7155d | 2023-02-10 02:00:03 +0000 | [diff] [blame] | 121 | #if defined(__aarch64__) || defined(__riscv) |
Peter Collingbourne | da772e2 | 2018-09-06 22:20:44 -0700 | [diff] [blame] | 122 | // Free the shadow call stack and guard pages. |
Peter Collingbourne | 734beec | 2018-11-14 12:41:41 -0800 | [diff] [blame] | 123 | munmap(thread->shadow_call_stack_guard_region, SCS_GUARD_REGION_SIZE); |
Peter Collingbourne | da772e2 | 2018-09-06 22:20:44 -0700 | [diff] [blame] | 124 | #endif |
| 125 | |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 126 | __free_dynamic_tls(__get_bionic_tcb()); |
Yabin Cui | 58cf31b | 2015-03-06 17:23:53 -0800 | [diff] [blame] | 127 | |
| 128 | if (old_state == THREAD_DETACHED) { |
Yabin Cui | ba8dfc2 | 2015-01-06 09:31:00 -0800 | [diff] [blame] | 129 | // The thread is detached, no one will use pthread_internal_t after pthread_exit. |
| 130 | // So we can free mapped space, which includes pthread_internal_t and thread stack. |
Elliott Hughes | 960ee37 | 2013-12-11 12:41:54 -0800 | [diff] [blame] | 131 | // First make sure that the kernel does not try to clear the tid field |
| 132 | // because we'll have freed the memory before the thread actually exits. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 133 | __set_tid_address(nullptr); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 134 | |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 135 | // pthread_internal_t is freed below with stack, not here. |
| 136 | __pthread_internal_remove(thread); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 137 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 138 | |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 139 | __notify_thread_exit_callbacks(); |
Evgenii Stepanov | be551f5 | 2018-08-13 16:46:15 -0700 | [diff] [blame] | 140 | __hwasan_thread_exit(); |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 141 | |
Florian Mayer | a9e144d | 2024-11-11 13:39:05 -0800 | [diff] [blame] | 142 | #if defined(__aarch64__) |
| 143 | if (void* stack_mte_tls = thread->bionic_tcb->tls_slot(TLS_SLOT_STACK_MTE)) { |
| 144 | stack_mte_free_ringbuffer(reinterpret_cast<uintptr_t>(stack_mte_tls)); |
| 145 | } |
| 146 | #endif |
| 147 | // Everything below this line needs to be no_sanitize("memtag"). |
| 148 | |
Elliott Hughes | 118d5da | 2024-05-24 14:47:40 -0400 | [diff] [blame] | 149 | if (old_state == THREAD_DETACHED && thread->mmap_size != 0) { |
| 150 | // We need to free mapped space for detached threads when they exit. |
| 151 | // That's not something we can do in C. |
| 152 | _exit_with_stack_teardown(thread->mmap_base, thread->mmap_size); |
| 153 | } |
| 154 | // No need to free mapped space. Either there was no space mapped, |
| 155 | // or it is left for the pthread_join caller to clean up. |
Yabin Cui | 58cf31b | 2015-03-06 17:23:53 -0800 | [diff] [blame] | 156 | __exit(0); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 157 | } |