blob: 0181abac9d35fcc7e24da655ca07f5af4e6ac020 [file] [log] [blame]
Elliott Hughesc3f11402013-10-30 14:40:09 -07001/*
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 Hughes61fb3fc2013-11-07 12:28:46 -080031#include <signal.h>
Elliott Hughesc3f11402013-10-30 14:40:09 -070032#include <stdlib.h>
Elliott Hughesf86c4492016-02-25 22:05:28 -080033#include <string.h>
Elliott Hughesc3f11402013-10-30 14:40:09 -070034#include <sys/mman.h>
35
Peter Collingbourne734beec2018-11-14 12:41:41 -080036#include "private/bionic_constants.h"
dimitryfa432522017-10-25 13:07:45 +020037#include "private/bionic_defs.h"
Peter Collingbourne5d3aa862020-09-11 15:05:17 -070038#include "private/ScopedRWLock.h"
Elliott Hughes4b1c6e72018-01-24 18:54:38 -080039#include "private/ScopedSignalBlocker.h"
Elliott Hughesc3f11402013-10-30 14:40:09 -070040#include "pthread_internal.h"
41
Elliott Hughes6203e7b2014-05-30 14:49:00 -070042extern "C" __noreturn void _exit_with_stack_teardown(void*, size_t);
43extern "C" __noreturn void __exit(int);
Christopher Ferris101fb7d2013-12-06 18:54:48 -080044extern "C" int __set_tid_address(int*);
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -070045extern "C" void __cxa_thread_finalize();
Elliott Hughesc3f11402013-10-30 14:40:09 -070046
47/* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions
48 * and thread cancelation
49 */
50
dimitryfa432522017-10-25 13:07:45 +020051__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -070052void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t routine, void* arg) {
53 pthread_internal_t* thread = __get_thread();
54 c->__cleanup_routine = routine;
55 c->__cleanup_arg = arg;
56 c->__cleanup_prev = thread->cleanup_stack;
57 thread->cleanup_stack = c;
58}
59
dimitryfa432522017-10-25 13:07:45 +020060__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -070061void __pthread_cleanup_pop(__pthread_cleanup_t* c, int execute) {
62 pthread_internal_t* thread = __get_thread();
63 thread->cleanup_stack = c->__cleanup_prev;
64 if (execute) {
65 c->__cleanup_routine(c->__cleanup_arg);
66 }
67}
68
dimitryfa432522017-10-25 13:07:45 +020069__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes877ec6d2013-11-15 17:40:18 -080070void pthread_exit(void* return_value) {
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -070071 // Call dtors for thread_local objects first.
72 __cxa_thread_finalize();
73
Elliott Hughesc3f11402013-10-30 14:40:09 -070074 pthread_internal_t* thread = __get_thread();
Elliott Hughes877ec6d2013-11-15 17:40:18 -080075 thread->return_value = return_value;
Elliott Hughesc3f11402013-10-30 14:40:09 -070076
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -070077 // Call the cleanup handlers.
Elliott Hughesc3f11402013-10-30 14:40:09 -070078 while (thread->cleanup_stack) {
79 __pthread_cleanup_t* c = thread->cleanup_stack;
80 thread->cleanup_stack = c->__cleanup_prev;
81 c->__cleanup_routine(c->__cleanup_arg);
82 }
83
84 // Call the TLS destructors. It is important to do that before removing this
85 // thread from the global list. This will ensure that if someone else deletes
86 // a TLS key, the corresponding value will be set to NULL in this thread's TLS
87 // space (see pthread_key_delete).
88 pthread_key_clean_all();
89
Yi Kong32bc0fc2018-08-02 17:31:13 -070090 if (thread->alternate_signal_stack != nullptr) {
Elliott Hughesc3f11402013-10-30 14:40:09 -070091 // Tell the kernel to stop using the alternate signal stack.
Elliott Hughes2c6c9532016-02-25 21:51:50 -080092 stack_t ss;
93 memset(&ss, 0, sizeof(ss));
Elliott Hughesc3f11402013-10-30 14:40:09 -070094 ss.ss_flags = SS_DISABLE;
Yi Kong32bc0fc2018-08-02 17:31:13 -070095 sigaltstack(&ss, nullptr);
Elliott Hughesc3f11402013-10-30 14:40:09 -070096
97 // Free it.
Yabin Cuief115002015-03-30 20:03:57 -070098 munmap(thread->alternate_signal_stack, SIGNAL_STACK_SIZE);
Yi Kong32bc0fc2018-08-02 17:31:13 -070099 thread->alternate_signal_stack = nullptr;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700100 }
101
Ryan Prichard16455b52019-01-18 01:00:59 -0800102 ThreadJoinState old_state = THREAD_NOT_JOINED;
103 while (old_state == THREAD_NOT_JOINED &&
104 !atomic_compare_exchange_weak(&thread->join_state, &old_state, THREAD_EXITED_NOT_JOINED)) {
105 }
106
Peter Collingbourne5d3aa862020-09-11 15:05:17 -0700107 // android_run_on_all_threads() needs to see signals blocked atomically with setting the
108 // terminating flag, so take the creation lock while doing these operations.
109 {
110 ScopedReadLock locker(&g_thread_creation_lock);
111 atomic_store(&thread->terminating, true);
112
113 // We don't want to take a signal after unmapping the stack, the shadow call stack, or dynamic
114 // TLS memory.
115 sigset64_t set;
116 sigfillset64(&set);
117 __rt_sigprocmask(SIG_BLOCK, &set, nullptr, sizeof(sigset64_t));
118 }
Ryan Prichard16455b52019-01-18 01:00:59 -0800119
Elliott Hughes9a7155d2023-02-10 02:00:03 +0000120#if defined(__aarch64__) || defined(__riscv)
Peter Collingbourneda772e22018-09-06 22:20:44 -0700121 // Free the shadow call stack and guard pages.
Peter Collingbourne734beec2018-11-14 12:41:41 -0800122 munmap(thread->shadow_call_stack_guard_region, SCS_GUARD_REGION_SIZE);
Peter Collingbourneda772e22018-09-06 22:20:44 -0700123#endif
124
Ryan Prichard16455b52019-01-18 01:00:59 -0800125 __free_dynamic_tls(__get_bionic_tcb());
Yabin Cui58cf31b2015-03-06 17:23:53 -0800126
127 if (old_state == THREAD_DETACHED) {
Yabin Cuiba8dfc22015-01-06 09:31:00 -0800128 // The thread is detached, no one will use pthread_internal_t after pthread_exit.
129 // So we can free mapped space, which includes pthread_internal_t and thread stack.
Elliott Hughes960ee372013-12-11 12:41:54 -0800130 // First make sure that the kernel does not try to clear the tid field
131 // because we'll have freed the memory before the thread actually exits.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700132 __set_tid_address(nullptr);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800133
Elliott Hughes7484c212017-02-02 02:41:38 +0000134 // pthread_internal_t is freed below with stack, not here.
135 __pthread_internal_remove(thread);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700136 }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700137
Vy Nguyend5007512020-07-14 17:37:04 -0400138 __notify_thread_exit_callbacks();
Evgenii Stepanovbe551f52018-08-13 16:46:15 -0700139 __hwasan_thread_exit();
Vy Nguyend5007512020-07-14 17:37:04 -0400140
Elliott Hughes118d5da2024-05-24 14:47:40 -0400141 if (old_state == THREAD_DETACHED && thread->mmap_size != 0) {
142 // We need to free mapped space for detached threads when they exit.
143 // That's not something we can do in C.
144 _exit_with_stack_teardown(thread->mmap_base, thread->mmap_size);
145 }
146 // No need to free mapped space. Either there was no space mapped,
147 // or it is left for the pthread_join caller to clean up.
Yabin Cui58cf31b2015-03-06 17:23:53 -0800148 __exit(0);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700149}