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 
31 #include <signal.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/mman.h>
35 
36 #include "private/bionic_constants.h"
37 #include "private/bionic_defs.h"
38 #include "private/ScopedSignalBlocker.h"
39 #include "pthread_internal.h"
40 
41 extern "C" __noreturn void _exit_with_stack_teardown(void*, size_t);
42 extern "C" __noreturn void __exit(int);
43 extern "C" int __set_tid_address(int*);
44 extern "C" void __cxa_thread_finalize();
45 
46 /* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions
47  *         and thread cancelation
48  */
49 
50 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
__pthread_cleanup_push(__pthread_cleanup_t * c,__pthread_cleanup_func_t routine,void * arg)51 void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t routine, void* arg) {
52   pthread_internal_t* thread = __get_thread();
53   c->__cleanup_routine = routine;
54   c->__cleanup_arg = arg;
55   c->__cleanup_prev = thread->cleanup_stack;
56   thread->cleanup_stack = c;
57 }
58 
59 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
__pthread_cleanup_pop(__pthread_cleanup_t * c,int execute)60 void __pthread_cleanup_pop(__pthread_cleanup_t* c, int execute) {
61   pthread_internal_t* thread = __get_thread();
62   thread->cleanup_stack = c->__cleanup_prev;
63   if (execute) {
64     c->__cleanup_routine(c->__cleanup_arg);
65   }
66 }
67 
68 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_exit(void * return_value)69 void pthread_exit(void* return_value) {
70   // Call dtors for thread_local objects first.
71   __cxa_thread_finalize();
72 
73   pthread_internal_t* thread = __get_thread();
74   thread->return_value = return_value;
75 
76   // Call the cleanup handlers.
77   while (thread->cleanup_stack) {
78     __pthread_cleanup_t* c = thread->cleanup_stack;
79     thread->cleanup_stack = c->__cleanup_prev;
80     c->__cleanup_routine(c->__cleanup_arg);
81   }
82 
83   // Call the TLS destructors. It is important to do that before removing this
84   // thread from the global list. This will ensure that if someone else deletes
85   // a TLS key, the corresponding value will be set to NULL in this thread's TLS
86   // space (see pthread_key_delete).
87   pthread_key_clean_all();
88 
89   if (thread->alternate_signal_stack != nullptr) {
90     // Tell the kernel to stop using the alternate signal stack.
91     stack_t ss;
92     memset(&ss, 0, sizeof(ss));
93     ss.ss_flags = SS_DISABLE;
94     sigaltstack(&ss, nullptr);
95 
96     // Free it.
97     munmap(thread->alternate_signal_stack, SIGNAL_STACK_SIZE);
98     thread->alternate_signal_stack = nullptr;
99   }
100 
101   ThreadJoinState old_state = THREAD_NOT_JOINED;
102   while (old_state == THREAD_NOT_JOINED &&
103          !atomic_compare_exchange_weak(&thread->join_state, &old_state, THREAD_EXITED_NOT_JOINED)) {
104   }
105 
106   // We don't want to take a signal after unmapping the stack, the shadow call
107   // stack, or dynamic TLS memory.
108   ScopedSignalBlocker ssb;
109 
110 #ifdef __aarch64__
111   // Free the shadow call stack and guard pages.
112   munmap(thread->shadow_call_stack_guard_region, SCS_GUARD_REGION_SIZE);
113 #endif
114 
115   // Free the ELF TLS DTV and all dynamically-allocated ELF TLS memory.
116   __free_dynamic_tls(__get_bionic_tcb());
117 
118   if (old_state == THREAD_DETACHED) {
119     // The thread is detached, no one will use pthread_internal_t after pthread_exit.
120     // So we can free mapped space, which includes pthread_internal_t and thread stack.
121     // First make sure that the kernel does not try to clear the tid field
122     // because we'll have freed the memory before the thread actually exits.
123     __set_tid_address(nullptr);
124 
125     // pthread_internal_t is freed below with stack, not here.
126     __pthread_internal_remove(thread);
127 
128     if (thread->mmap_size != 0) {
129       // We need to free mapped space for detached threads when they exit.
130       // That's not something we can do in C.
131       __hwasan_thread_exit();
132       _exit_with_stack_teardown(thread->mmap_base, thread->mmap_size);
133     }
134   }
135 
136   // No need to free mapped space. Either there was no space mapped, or it is left for
137   // the pthread_join caller to clean up.
138   __hwasan_thread_exit();
139   __exit(0);
140 }
141