1c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes/*
2c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * Copyright (C) 2008 The Android Open Source Project
3c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * All rights reserved.
4c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *
5c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * Redistribution and use in source and binary forms, with or without
6c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * modification, are permitted provided that the following conditions
7c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * are met:
8c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *  * Redistributions of source code must retain the above copyright
9c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *    notice, this list of conditions and the following disclaimer.
10c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *  * Redistributions in binary form must reproduce the above copyright
11c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *    notice, this list of conditions and the following disclaimer in
12c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *    the documentation and/or other materials provided with the
13c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *    distribution.
14c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *
15c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * SUCH DAMAGE.
27c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes */
28c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
29c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include <pthread.h>
30c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
3161fb3fc770566c7bafe7af8fb93590bcad387fbbElliott Hughes#include <signal.h>
32c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include <stdlib.h>
33f86c4494ff9613dcdf3dd7713d1e2c5d989d8ebaElliott Hughes#include <string.h>
34c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include <sys/mman.h>
35c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
36c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include "pthread_internal.h"
37c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
386203e7b853a587fbd70cea2e58b63ae38a71a13eElliott Hughesextern "C" __noreturn void _exit_with_stack_teardown(void*, size_t);
396203e7b853a587fbd70cea2e58b63ae38a71a13eElliott Hughesextern "C" __noreturn void __exit(int);
40101fb7d963ed362c4e351d95e55cbd70dc59eac3Christopher Ferrisextern "C" int __set_tid_address(int*);
41df79c330d895af31f39ee301dee62731fa586168Dmitriy Ivanovextern "C" void __cxa_thread_finalize();
42c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
43c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes/* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions
44c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *         and thread cancelation
45c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes */
46c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
47c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesvoid __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t routine, void* arg) {
48c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_internal_t* thread = __get_thread();
49c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  c->__cleanup_routine = routine;
50c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  c->__cleanup_arg = arg;
51c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  c->__cleanup_prev = thread->cleanup_stack;
52c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  thread->cleanup_stack = c;
53c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
54c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
55c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesvoid __pthread_cleanup_pop(__pthread_cleanup_t* c, int execute) {
56c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_internal_t* thread = __get_thread();
57c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  thread->cleanup_stack = c->__cleanup_prev;
58c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  if (execute) {
59c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    c->__cleanup_routine(c->__cleanup_arg);
60c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  }
61c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
62c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
63877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughesvoid pthread_exit(void* return_value) {
64df79c330d895af31f39ee301dee62731fa586168Dmitriy Ivanov  // Call dtors for thread_local objects first.
65df79c330d895af31f39ee301dee62731fa586168Dmitriy Ivanov  __cxa_thread_finalize();
66df79c330d895af31f39ee301dee62731fa586168Dmitriy Ivanov
67c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_internal_t* thread = __get_thread();
68877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  thread->return_value = return_value;
69c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
70df79c330d895af31f39ee301dee62731fa586168Dmitriy Ivanov  // Call the cleanup handlers.
71c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  while (thread->cleanup_stack) {
72c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    __pthread_cleanup_t* c = thread->cleanup_stack;
73c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    thread->cleanup_stack = c->__cleanup_prev;
74c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    c->__cleanup_routine(c->__cleanup_arg);
75c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  }
76c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
77c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  // Call the TLS destructors. It is important to do that before removing this
78c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  // thread from the global list. This will ensure that if someone else deletes
79c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  // a TLS key, the corresponding value will be set to NULL in this thread's TLS
80c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  // space (see pthread_key_delete).
81c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_key_clean_all();
82c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
83c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  if (thread->alternate_signal_stack != NULL) {
84c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    // Tell the kernel to stop using the alternate signal stack.
852c6c95348ca9df1727fd7402b3704e0e87cb347eElliott Hughes    stack_t ss;
862c6c95348ca9df1727fd7402b3704e0e87cb347eElliott Hughes    memset(&ss, 0, sizeof(ss));
87c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    ss.ss_flags = SS_DISABLE;
88c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    sigaltstack(&ss, NULL);
89c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
90c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    // Free it.
91ef115003012f61cf5539fdfeb201b98e4a92f610Yabin Cui    munmap(thread->alternate_signal_stack, SIGNAL_STACK_SIZE);
92c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    thread->alternate_signal_stack = NULL;
93c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  }
94c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
955450f86b31287173278513eb8b4c8e73b9fdd3aeJosh Gao  // Unmap the bionic TLS, including guard pages.
965450f86b31287173278513eb8b4c8e73b9fdd3aeJosh Gao  void* allocation = reinterpret_cast<char*>(thread->bionic_tls) - PAGE_SIZE;
975450f86b31287173278513eb8b4c8e73b9fdd3aeJosh Gao  munmap(allocation, BIONIC_TLS_SIZE + 2 * PAGE_SIZE);
985450f86b31287173278513eb8b4c8e73b9fdd3aeJosh Gao
9958cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui  ThreadJoinState old_state = THREAD_NOT_JOINED;
10058cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui  while (old_state == THREAD_NOT_JOINED &&
10158cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui         !atomic_compare_exchange_weak(&thread->join_state, &old_state, THREAD_EXITED_NOT_JOINED)) {
10258cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui  }
10358cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui
10458cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui  if (old_state == THREAD_DETACHED) {
105ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui    // The thread is detached, no one will use pthread_internal_t after pthread_exit.
106ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui    // So we can free mapped space, which includes pthread_internal_t and thread stack.
107960ee37f2aaf52bbec2f6265fd6e30fb2b41fef3Elliott Hughes    // First make sure that the kernel does not try to clear the tid field
108960ee37f2aaf52bbec2f6265fd6e30fb2b41fef3Elliott Hughes    // because we'll have freed the memory before the thread actually exits.
109101fb7d963ed362c4e351d95e55cbd70dc59eac3Christopher Ferris    __set_tid_address(NULL);
1108cf1b305670123aed7638d984ca39bfd22388440Yabin Cui
1117484c21c4c352a2200d94939fabc10d1bd3f0723Elliott Hughes    // pthread_internal_t is freed below with stack, not here.
1127484c21c4c352a2200d94939fabc10d1bd3f0723Elliott Hughes    __pthread_internal_remove(thread);
1137484c21c4c352a2200d94939fabc10d1bd3f0723Elliott Hughes
11458cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui    if (thread->mmap_size != 0) {
11558cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui      // We need to free mapped space for detached threads when they exit.
11658cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui      // That's not something we can do in C.
11758cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui
11858cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui      // We don't want to take a signal after we've unmapped the stack.
11958cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui      // That's one last thing we can handle in C.
12058cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui      sigset_t mask;
12158cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui      sigfillset(&mask);
12258cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui      sigprocmask(SIG_SETMASK, &mask, NULL);
12358cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui
12458cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui      _exit_with_stack_teardown(thread->attr.stack_base, thread->mmap_size);
12558cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui    }
126c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  }
12758cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui
12858cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui  // No need to free mapped space. Either there was no space mapped, or it is left for
12958cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui  // the pthread_join caller to clean up.
13058cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui  __exit(0);
131c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
132