Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [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_internal.h" |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/mman.h> |
| 35 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 36 | #include <async_safe/log.h> |
| 37 | |
Ryan Prichard | c86576c | 2019-01-09 03:09:42 -0800 | [diff] [blame] | 38 | #include "private/ScopedRWLock.h" |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 39 | #include "private/bionic_futex.h" |
| 40 | #include "private/bionic_tls.h" |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 41 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 42 | static pthread_internal_t* g_thread_list = nullptr; |
| 43 | static pthread_rwlock_t g_thread_list_lock = PTHREAD_RWLOCK_INITIALIZER; |
| 44 | |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 45 | pthread_t __pthread_internal_add(pthread_internal_t* thread) { |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 46 | ScopedWriteLock locker(&g_thread_list_lock); |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 47 | |
| 48 | // We insert at the head. |
| 49 | thread->next = g_thread_list; |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 50 | thread->prev = nullptr; |
| 51 | if (thread->next != nullptr) { |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 52 | thread->next->prev = thread; |
| 53 | } |
| 54 | g_thread_list = thread; |
| 55 | return reinterpret_cast<pthread_t>(thread); |
| 56 | } |
| 57 | |
| 58 | void __pthread_internal_remove(pthread_internal_t* thread) { |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 59 | ScopedWriteLock locker(&g_thread_list_lock); |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 60 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 61 | if (thread->next != nullptr) { |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 62 | thread->next->prev = thread->prev; |
| 63 | } |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 64 | if (thread->prev != nullptr) { |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 65 | thread->prev->next = thread->next; |
| 66 | } else { |
| 67 | g_thread_list = thread->next; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static void __pthread_internal_free(pthread_internal_t* thread) { |
| 72 | if (thread->mmap_size != 0) { |
| 73 | // Free mapped space, including thread stack and pthread_internal_t. |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 74 | munmap(thread->mmap_base, thread->mmap_size); |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | void __pthread_internal_remove_and_free(pthread_internal_t* thread) { |
| 79 | __pthread_internal_remove(thread); |
| 80 | __pthread_internal_free(thread); |
| 81 | } |
| 82 | |
Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 83 | pid_t __pthread_internal_gettid(pthread_t thread_id, const char* caller) { |
| 84 | pthread_internal_t* thread = __pthread_internal_find(thread_id, caller); |
| 85 | return thread ? thread->tid : -1; |
| 86 | } |
| 87 | |
| 88 | pthread_internal_t* __pthread_internal_find(pthread_t thread_id, const char* caller) { |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 89 | pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(thread_id); |
| 90 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 91 | // Check if we're looking for ourselves before acquiring the lock. |
| 92 | if (thread == __get_thread()) return thread; |
| 93 | |
Christopher Ferris | 7982914 | 2017-09-18 14:39:33 -0700 | [diff] [blame] | 94 | { |
| 95 | // Make sure to release the lock before the abort below. Otherwise, |
| 96 | // some apps might deadlock in their own crash handlers (see b/6565627). |
| 97 | ScopedReadLock locker(&g_thread_list_lock); |
| 98 | for (pthread_internal_t* t = g_thread_list; t != nullptr; t = t->next) { |
| 99 | if (t == thread) return thread; |
| 100 | } |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame^] | 103 | // Historically we'd return null, but from API level 26 we catch this error. |
| 104 | if (android_get_application_target_sdk_version() >= 26) { |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 105 | if (thread == nullptr) { |
| 106 | // This seems to be a common mistake, and it's relatively harmless because |
| 107 | // there will never be a valid thread at address 0, whereas other invalid |
| 108 | // addresses might sometimes contain threads or things that look enough like |
| 109 | // threads for us to do some real damage by continuing. |
| 110 | // TODO: try getting rid of this when Treble lets us keep vendor blobs on an old API level. |
Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 111 | async_safe_format_log(ANDROID_LOG_WARN, "libc", "invalid pthread_t (0) passed to %s", caller); |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 112 | } else { |
Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 113 | async_safe_fatal("invalid pthread_t %p passed to %s", thread, caller); |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 114 | } |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 115 | } |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 116 | return nullptr; |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 117 | } |