blob: 870a5260a8f788e0a6a5d1e731e8189e1a0948fa [file] [log] [blame]
Elliott Hughes7484c212017-02-02 02:41:38 +00001/*
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_internal.h"
30
31#include <errno.h>
32#include <stdlib.h>
33#include <string.h>
34#include <sys/mman.h>
35
Christopher Ferris7a3681e2017-04-24 17:48:32 -070036#include <async_safe/log.h>
37
Elliott Hughes7484c212017-02-02 02:41:38 +000038#include "private/bionic_futex.h"
39#include "private/bionic_tls.h"
Elliott Hughes7484c212017-02-02 02:41:38 +000040
Elliott Hughes11859d42017-02-13 17:59:29 -080041static pthread_internal_t* g_thread_list = nullptr;
42static pthread_rwlock_t g_thread_list_lock = PTHREAD_RWLOCK_INITIALIZER;
43
44template <bool write> class ScopedRWLock {
45 public:
Chih-Hung Hsieh770032d2019-01-02 10:59:48 -080046 explicit ScopedRWLock(pthread_rwlock_t* rwlock) : rwlock_(rwlock) {
Elliott Hughes11859d42017-02-13 17:59:29 -080047 (write ? pthread_rwlock_wrlock : pthread_rwlock_rdlock)(rwlock_);
48 }
49
50 ~ScopedRWLock() {
51 pthread_rwlock_unlock(rwlock_);
52 }
53
54 private:
55 pthread_rwlock_t* rwlock_;
Elliott Hughes5e62b342018-10-25 11:00:00 -070056 BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedRWLock);
Elliott Hughes11859d42017-02-13 17:59:29 -080057};
58
59typedef ScopedRWLock<true> ScopedWriteLock;
60typedef ScopedRWLock<false> ScopedReadLock;
Elliott Hughes7484c212017-02-02 02:41:38 +000061
62pthread_t __pthread_internal_add(pthread_internal_t* thread) {
Elliott Hughes11859d42017-02-13 17:59:29 -080063 ScopedWriteLock locker(&g_thread_list_lock);
Elliott Hughes7484c212017-02-02 02:41:38 +000064
65 // We insert at the head.
66 thread->next = g_thread_list;
Elliott Hughes11859d42017-02-13 17:59:29 -080067 thread->prev = nullptr;
68 if (thread->next != nullptr) {
Elliott Hughes7484c212017-02-02 02:41:38 +000069 thread->next->prev = thread;
70 }
71 g_thread_list = thread;
72 return reinterpret_cast<pthread_t>(thread);
73}
74
75void __pthread_internal_remove(pthread_internal_t* thread) {
Elliott Hughes11859d42017-02-13 17:59:29 -080076 ScopedWriteLock locker(&g_thread_list_lock);
Elliott Hughes7484c212017-02-02 02:41:38 +000077
Elliott Hughes11859d42017-02-13 17:59:29 -080078 if (thread->next != nullptr) {
Elliott Hughes7484c212017-02-02 02:41:38 +000079 thread->next->prev = thread->prev;
80 }
Elliott Hughes11859d42017-02-13 17:59:29 -080081 if (thread->prev != nullptr) {
Elliott Hughes7484c212017-02-02 02:41:38 +000082 thread->prev->next = thread->next;
83 } else {
84 g_thread_list = thread->next;
85 }
86}
87
88static void __pthread_internal_free(pthread_internal_t* thread) {
89 if (thread->mmap_size != 0) {
90 // Free mapped space, including thread stack and pthread_internal_t.
Ryan Prichard45d13492019-01-03 02:51:30 -080091 munmap(thread->mmap_base, thread->mmap_size);
Elliott Hughes7484c212017-02-02 02:41:38 +000092 }
93}
94
95void __pthread_internal_remove_and_free(pthread_internal_t* thread) {
96 __pthread_internal_remove(thread);
97 __pthread_internal_free(thread);
98}
99
100pthread_internal_t* __pthread_internal_find(pthread_t thread_id) {
101 pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(thread_id);
102
Elliott Hughes11859d42017-02-13 17:59:29 -0800103 // Check if we're looking for ourselves before acquiring the lock.
104 if (thread == __get_thread()) return thread;
105
Christopher Ferris79829142017-09-18 14:39:33 -0700106 {
107 // Make sure to release the lock before the abort below. Otherwise,
108 // some apps might deadlock in their own crash handlers (see b/6565627).
109 ScopedReadLock locker(&g_thread_list_lock);
110 for (pthread_internal_t* t = g_thread_list; t != nullptr; t = t->next) {
111 if (t == thread) return thread;
112 }
Elliott Hughes7484c212017-02-02 02:41:38 +0000113 }
114
Elliott Hughes11859d42017-02-13 17:59:29 -0800115 // Historically we'd return null, but
Elliott Hughesc0f46562018-11-09 15:38:52 -0800116 if (android_get_application_target_sdk_version() >= __ANDROID_API_O__) {
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800117 if (thread == nullptr) {
118 // This seems to be a common mistake, and it's relatively harmless because
119 // there will never be a valid thread at address 0, whereas other invalid
120 // addresses might sometimes contain threads or things that look enough like
121 // threads for us to do some real damage by continuing.
122 // TODO: try getting rid of this when Treble lets us keep vendor blobs on an old API level.
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700123 async_safe_format_log(ANDROID_LOG_WARN, "libc", "invalid pthread_t (0) passed to libc");
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800124 } else {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700125 async_safe_fatal("invalid pthread_t %p passed to libc", thread);
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800126 }
Elliott Hughes7484c212017-02-02 02:41:38 +0000127 }
Elliott Hughes11859d42017-02-13 17:59:29 -0800128 return nullptr;
Elliott Hughes7484c212017-02-02 02:41:38 +0000129}