| Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <errno.h> |
| Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 21 | #include <limits.h> |
| Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 22 | #include <malloc.h> |
| Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 23 | #include <pthread.h> |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 24 | #include <signal.h> |
| Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
| Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 26 | #include <sys/cdefs.h> |
| Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 27 | #include <sys/mman.h> |
| Juan Yescas | 65af9a8 | 2023-12-07 11:35:21 -0800 | [diff] [blame] | 28 | #include <sys/param.h> |
| Elliott Hughes | 4d098ca | 2016-04-11 12:43:05 -0700 | [diff] [blame] | 29 | #include <sys/prctl.h> |
| George Burgess IV | 08fd072 | 2019-01-15 19:00:11 -0800 | [diff] [blame] | 30 | #include <sys/resource.h> |
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 31 | #include <sys/syscall.h> |
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 32 | #include <time.h> |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 33 | #include <unistd.h> |
| Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 34 | #include <unwind.h> |
| Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 35 | |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 36 | #include <atomic> |
| Josh Gao | ddf757e | 2018-10-17 15:23:03 -0700 | [diff] [blame] | 37 | #include <future> |
| Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 38 | #include <vector> |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 39 | |
| Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 40 | #include <android-base/macros.h> |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 41 | #include <android-base/parseint.h> |
| Tom Cherry | b8ab618 | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 42 | #include <android-base/scopeguard.h> |
| Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 43 | #include <android-base/silent_death_test.h> |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 44 | #include <android-base/strings.h> |
| Florian Mayer | f966620 | 2023-02-28 14:26:06 -0800 | [diff] [blame] | 45 | #include <android-base/test_utils.h> |
| Tom Cherry | b8ab618 | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 46 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 47 | #include "private/bionic_constants.h" |
| Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 48 | #include "private/bionic_time_conversions.h" |
| Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 49 | #include "SignalUtils.h" |
| Tamas Petz | 8322377 | 2025-05-13 11:49:29 +0200 | [diff] [blame] | 50 | #include "sme_utils.h" |
| Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 51 | #include "utils.h" |
| 52 | |
| Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 53 | using pthread_DeathTest = SilentDeathTest; |
| Elliott Hughes | e657eb4 | 2021-02-18 17:11:56 -0800 | [diff] [blame] | 54 | |
| Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 55 | TEST(pthread, pthread_key_create) { |
| 56 | pthread_key_t key; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 57 | ASSERT_EQ(0, pthread_key_create(&key, nullptr)); |
| Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 58 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 59 | // Can't delete a key that's already been deleted. |
| 60 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 61 | } |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 62 | |
| Elliott Hughes | 1960f1c | 2025-02-26 14:46:52 -0800 | [diff] [blame] | 63 | static std::vector<void*> example_key_destructor_data; |
| 64 | static pthread_key_t example_key; |
| 65 | static void example_key_destructor(void *data) { |
| 66 | // By the time the destructor function is running, |
| 67 | // this thread's value for the key should have been zeroed. |
| 68 | ASSERT_EQ(NULL, pthread_getspecific(example_key)); |
| 69 | |
| 70 | // Store the value so we can check we got the expected result. |
| 71 | example_key_destructor_data.push_back(data); |
| 72 | } |
| 73 | |
| 74 | TEST(pthread, pthread_key_destructors) { |
| 75 | ASSERT_EQ(0, pthread_key_create(&example_key, example_key_destructor)); |
| 76 | |
| 77 | // Check that the destructor isn't called for a default null value. |
| 78 | std::thread([]() {}).join(); |
| 79 | ASSERT_TRUE(example_key_destructor_data.empty()); |
| 80 | |
| 81 | // Check that the destructor isn't called for an explicit null value. |
| 82 | std::thread([]() { |
| 83 | ASSERT_EQ(0, pthread_setspecific(example_key, (void*) 1234)); |
| 84 | ASSERT_EQ(0, pthread_setspecific(example_key, nullptr)); |
| 85 | }).join(); |
| 86 | ASSERT_TRUE(example_key_destructor_data.empty()); |
| 87 | |
| 88 | // Check that the destructor is called for a non-null value. |
| 89 | std::thread([]() { ASSERT_EQ(0, pthread_setspecific(example_key, (void*) 1234)); }).join(); |
| 90 | ASSERT_EQ(1u, example_key_destructor_data.size()); |
| 91 | ASSERT_EQ((void*) 1234, example_key_destructor_data[0]); |
| 92 | |
| 93 | ASSERT_EQ(0, pthread_key_delete(example_key)); |
| 94 | } |
| 95 | |
| Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 96 | TEST(pthread, pthread_keys_max) { |
| Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 97 | // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX. |
| 98 | ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX); |
| Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 99 | } |
| Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 100 | |
| Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 101 | TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) { |
| Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 102 | int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX); |
| Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 103 | ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX); |
| Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | TEST(pthread, pthread_key_many_distinct) { |
| Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 107 | // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX |
| 108 | // pthread keys, but We should be able to allocate at least this many keys. |
| 109 | int nkeys = PTHREAD_KEYS_MAX / 2; |
| Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 110 | std::vector<pthread_key_t> keys; |
| 111 | |
| Tom Cherry | b8ab618 | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 112 | auto scope_guard = android::base::make_scope_guard([&keys] { |
| Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 113 | for (const auto& key : keys) { |
| Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 114 | EXPECT_EQ(0, pthread_key_delete(key)); |
| 115 | } |
| 116 | }); |
| 117 | |
| 118 | for (int i = 0; i < nkeys; ++i) { |
| 119 | pthread_key_t key; |
| Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 120 | // If this fails, it's likely that LIBC_PTHREAD_KEY_RESERVED_COUNT is wrong. |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 121 | ASSERT_EQ(0, pthread_key_create(&key, nullptr)) << i << " of " << nkeys; |
| Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 122 | keys.push_back(key); |
| 123 | ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i))); |
| 124 | } |
| 125 | |
| 126 | for (int i = keys.size() - 1; i >= 0; --i) { |
| 127 | ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back())); |
| 128 | pthread_key_t key = keys.back(); |
| 129 | keys.pop_back(); |
| 130 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 131 | } |
| 132 | } |
| 133 | |
| Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 134 | TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) { |
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 135 | std::vector<pthread_key_t> keys; |
| Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 136 | int rv = 0; |
| Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 137 | |
| 138 | // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should |
| 139 | // be more than we are allowed to allocate now. |
| 140 | for (int i = 0; i < PTHREAD_KEYS_MAX; i++) { |
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 141 | pthread_key_t key; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 142 | rv = pthread_key_create(&key, nullptr); |
| Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 143 | if (rv == EAGAIN) { |
| 144 | break; |
| 145 | } |
| 146 | EXPECT_EQ(0, rv); |
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 147 | keys.push_back(key); |
| 148 | } |
| 149 | |
| Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 150 | // Don't leak keys. |
| Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 151 | for (const auto& key : keys) { |
| Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 152 | EXPECT_EQ(0, pthread_key_delete(key)); |
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 153 | } |
| Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 154 | keys.clear(); |
| 155 | |
| 156 | // We should have eventually reached the maximum number of keys and received |
| 157 | // EAGAIN. |
| 158 | ASSERT_EQ(EAGAIN, rv); |
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| Elliott Hughes | ebb770f | 2014-06-25 13:46:46 -0700 | [diff] [blame] | 161 | TEST(pthread, pthread_key_delete) { |
| 162 | void* expected = reinterpret_cast<void*>(1234); |
| 163 | pthread_key_t key; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 164 | ASSERT_EQ(0, pthread_key_create(&key, nullptr)); |
| Elliott Hughes | ebb770f | 2014-06-25 13:46:46 -0700 | [diff] [blame] | 165 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 166 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 167 | ASSERT_EQ(0, pthread_key_delete(key)); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 168 | // After deletion, pthread_getspecific returns nullptr. |
| 169 | ASSERT_EQ(nullptr, pthread_getspecific(key)); |
| Elliott Hughes | ebb770f | 2014-06-25 13:46:46 -0700 | [diff] [blame] | 170 | // And you can't use pthread_setspecific with the deleted key. |
| 171 | ASSERT_EQ(EINVAL, pthread_setspecific(key, expected)); |
| 172 | } |
| 173 | |
| Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 174 | TEST(pthread, pthread_key_fork) { |
| 175 | void* expected = reinterpret_cast<void*>(1234); |
| 176 | pthread_key_t key; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 177 | ASSERT_EQ(0, pthread_key_create(&key, nullptr)); |
| Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 178 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 179 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 180 | |
| 181 | pid_t pid = fork(); |
| 182 | ASSERT_NE(-1, pid) << strerror(errno); |
| 183 | |
| 184 | if (pid == 0) { |
| 185 | // The surviving thread inherits all the forking thread's TLS values... |
| 186 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 187 | _exit(99); |
| 188 | } |
| 189 | |
| Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 190 | AssertChildExited(pid, 99); |
| Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 191 | |
| 192 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 193 | ASSERT_EQ(0, pthread_key_delete(key)); |
| Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static void* DirtyKeyFn(void* key) { |
| 197 | return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key)); |
| 198 | } |
| 199 | |
| 200 | TEST(pthread, pthread_key_dirty) { |
| 201 | pthread_key_t key; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 202 | ASSERT_EQ(0, pthread_key_create(&key, nullptr)); |
| Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 203 | |
| Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 204 | size_t stack_size = 640 * 1024; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 205 | void* stack = mmap(nullptr, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 206 | ASSERT_NE(MAP_FAILED, stack); |
| 207 | memset(stack, 0xff, stack_size); |
| 208 | |
| 209 | pthread_attr_t attr; |
| 210 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 211 | ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size)); |
| 212 | |
| 213 | pthread_t t; |
| 214 | ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key)); |
| 215 | |
| 216 | void* result; |
| 217 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 218 | ASSERT_EQ(nullptr, result); // Not ~0! |
| 219 | |
| 220 | ASSERT_EQ(0, munmap(stack, stack_size)); |
| Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 221 | ASSERT_EQ(0, pthread_key_delete(key)); |
| Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| Florian Mayer | f966620 | 2023-02-28 14:26:06 -0800 | [diff] [blame] | 224 | static void* FnWithStackFrame(void*) { |
| 225 | int x; |
| 226 | *const_cast<volatile int*>(&x) = 1; |
| 227 | return nullptr; |
| 228 | } |
| 229 | |
| 230 | TEST(pthread, pthread_heap_allocated_stack) { |
| 231 | SKIP_WITH_HWASAN; // TODO(b/148982147): Re-enable when fixed. |
| 232 | |
| 233 | size_t stack_size = 640 * 1024; |
| Elliott Hughes | 18e335b | 2023-04-21 11:18:40 -0700 | [diff] [blame] | 234 | std::unique_ptr<char[]> stack(new (std::align_val_t(getpagesize())) char[stack_size]); |
| 235 | memset(stack.get(), '\xff', stack_size); |
| Florian Mayer | f966620 | 2023-02-28 14:26:06 -0800 | [diff] [blame] | 236 | |
| 237 | pthread_attr_t attr; |
| 238 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| Elliott Hughes | 18e335b | 2023-04-21 11:18:40 -0700 | [diff] [blame] | 239 | ASSERT_EQ(0, pthread_attr_setstack(&attr, stack.get(), stack_size)); |
| Florian Mayer | f966620 | 2023-02-28 14:26:06 -0800 | [diff] [blame] | 240 | |
| 241 | pthread_t t; |
| 242 | ASSERT_EQ(0, pthread_create(&t, &attr, FnWithStackFrame, nullptr)); |
| 243 | |
| 244 | void* result; |
| 245 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 246 | } |
| 247 | |
| Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 248 | TEST(pthread, static_pthread_key_used_before_creation) { |
| 249 | #if defined(__BIONIC__) |
| 250 | // See http://b/19625804. The bug is about a static/global pthread key being used before creation. |
| 251 | // So here tests if the static/global default value 0 can be detected as invalid key. |
| 252 | static pthread_key_t key; |
| 253 | ASSERT_EQ(nullptr, pthread_getspecific(key)); |
| 254 | ASSERT_EQ(EINVAL, pthread_setspecific(key, nullptr)); |
| 255 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 256 | #else |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 257 | GTEST_SKIP() << "bionic-only test"; |
| Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 258 | #endif |
| 259 | } |
| 260 | |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 261 | static void* IdFn(void* arg) { |
| 262 | return arg; |
| 263 | } |
| 264 | |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 265 | class SpinFunctionHelper { |
| 266 | public: |
| 267 | SpinFunctionHelper() { |
| 268 | SpinFunctionHelper::spin_flag_ = true; |
| Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 269 | } |
| Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 270 | |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 271 | ~SpinFunctionHelper() { |
| 272 | UnSpin(); |
| 273 | } |
| Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 274 | |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 275 | auto GetFunction() -> void* (*)(void*) { |
| 276 | return SpinFunctionHelper::SpinFn; |
| 277 | } |
| 278 | |
| 279 | void UnSpin() { |
| 280 | SpinFunctionHelper::spin_flag_ = false; |
| 281 | } |
| 282 | |
| 283 | private: |
| 284 | static void* SpinFn(void*) { |
| 285 | while (spin_flag_) {} |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 286 | return nullptr; |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 287 | } |
| Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 288 | static std::atomic<bool> spin_flag_; |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | // It doesn't matter if spin_flag_ is used in several tests, |
| 292 | // because it is always set to false after each test. Each thread |
| 293 | // loops on spin_flag_ can find it becomes false at some time. |
| Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 294 | std::atomic<bool> SpinFunctionHelper::spin_flag_; |
| Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 295 | |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 296 | static void* JoinFn(void* arg) { |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 297 | return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), nullptr)); |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 300 | static void AssertDetached(pthread_t t, bool is_detached) { |
| 301 | pthread_attr_t attr; |
| 302 | ASSERT_EQ(0, pthread_getattr_np(t, &attr)); |
| 303 | int detach_state; |
| 304 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state)); |
| 305 | pthread_attr_destroy(&attr); |
| 306 | ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED)); |
| 307 | } |
| 308 | |
| Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 309 | static void MakeDeadThread(pthread_t& t) { |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 310 | ASSERT_EQ(0, pthread_create(&t, nullptr, IdFn, nullptr)); |
| 311 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 314 | TEST(pthread, pthread_create) { |
| 315 | void* expected_result = reinterpret_cast<void*>(123); |
| 316 | // Can we create a thread? |
| 317 | pthread_t t; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 318 | ASSERT_EQ(0, pthread_create(&t, nullptr, IdFn, expected_result)); |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 319 | // If we join, do we get the expected value back? |
| 320 | void* result; |
| 321 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 322 | ASSERT_EQ(expected_result, result); |
| 323 | } |
| 324 | |
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 325 | TEST(pthread, pthread_create_EAGAIN) { |
| 326 | pthread_attr_t attributes; |
| 327 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 328 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1))); |
| 329 | |
| 330 | pthread_t t; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 331 | ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, nullptr)); |
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 334 | TEST(pthread, pthread_no_join_after_detach) { |
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 335 | SpinFunctionHelper spin_helper; |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 336 | |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 337 | pthread_t t1; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 338 | ASSERT_EQ(0, pthread_create(&t1, nullptr, spin_helper.GetFunction(), nullptr)); |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 339 | |
| 340 | // After a pthread_detach... |
| 341 | ASSERT_EQ(0, pthread_detach(t1)); |
| Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 342 | AssertDetached(t1, true); |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 343 | |
| 344 | // ...pthread_join should fail. |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 345 | ASSERT_EQ(EINVAL, pthread_join(t1, nullptr)); |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | TEST(pthread, pthread_no_op_detach_after_join) { |
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 349 | SpinFunctionHelper spin_helper; |
| Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 350 | |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 351 | pthread_t t1; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 352 | ASSERT_EQ(0, pthread_create(&t1, nullptr, spin_helper.GetFunction(), nullptr)); |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 353 | |
| 354 | // If thread 2 is already waiting to join thread 1... |
| 355 | pthread_t t2; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 356 | ASSERT_EQ(0, pthread_create(&t2, nullptr, JoinFn, reinterpret_cast<void*>(t1))); |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 357 | |
| Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 358 | sleep(1); // (Give t2 a chance to call pthread_join.) |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 359 | |
| Yabin Cui | bbb0432 | 2015-03-19 15:19:25 -0700 | [diff] [blame] | 360 | #if defined(__BIONIC__) |
| 361 | ASSERT_EQ(EINVAL, pthread_detach(t1)); |
| 362 | #else |
| Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 363 | ASSERT_EQ(0, pthread_detach(t1)); |
| Yabin Cui | bbb0432 | 2015-03-19 15:19:25 -0700 | [diff] [blame] | 364 | #endif |
| Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 365 | AssertDetached(t1, false); |
| 366 | |
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 367 | spin_helper.UnSpin(); |
| Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 368 | |
| 369 | // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 370 | void* join_result; |
| 371 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
| Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 372 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
| Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 373 | } |
| Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 374 | |
| 375 | TEST(pthread, pthread_join_self) { |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 376 | ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), nullptr)); |
| Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 377 | } |
| Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 378 | |
| Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 379 | struct TestBug37410 { |
| 380 | pthread_t main_thread; |
| 381 | pthread_mutex_t mutex; |
| Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 382 | |
| Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 383 | static void main() { |
| 384 | TestBug37410 data; |
| 385 | data.main_thread = pthread_self(); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 386 | ASSERT_EQ(0, pthread_mutex_init(&data.mutex, nullptr)); |
| Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 387 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 388 | |
| 389 | pthread_t t; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 390 | ASSERT_EQ(0, pthread_create(&t, nullptr, TestBug37410::thread_fn, reinterpret_cast<void*>(&data))); |
| Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 391 | |
| 392 | // Wait for the thread to be running... |
| 393 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 394 | ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex)); |
| 395 | |
| 396 | // ...and exit. |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 397 | pthread_exit(nullptr); |
| Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | private: |
| 401 | static void* thread_fn(void* arg) { |
| 402 | TestBug37410* data = reinterpret_cast<TestBug37410*>(arg); |
| 403 | |
| Evgenii Stepanov | 352853a | 2019-02-05 17:37:37 -0800 | [diff] [blame] | 404 | // Unlocking data->mutex will cause the main thread to exit, invalidating *data. Save the handle. |
| 405 | pthread_t main_thread = data->main_thread; |
| 406 | |
| Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 407 | // Let the main thread know we're running. |
| 408 | pthread_mutex_unlock(&data->mutex); |
| 409 | |
| 410 | // And wait for the main thread to exit. |
| Evgenii Stepanov | 352853a | 2019-02-05 17:37:37 -0800 | [diff] [blame] | 411 | pthread_join(main_thread, nullptr); |
| Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 412 | |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 413 | return nullptr; |
| Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 414 | } |
| 415 | }; |
| Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 416 | |
| Elliott Hughes | 7fd803c | 2013-02-14 16:33:52 -0800 | [diff] [blame] | 417 | // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to |
| 418 | // run this test (which exits normally) in its own process. |
| Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 419 | TEST_F(pthread_DeathTest, pthread_bug_37410) { |
| Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 420 | // http://code.google.com/p/android/issues/detail?id=37410 |
| Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 421 | ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), ""); |
| Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 422 | } |
| Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 423 | |
| 424 | static void* SignalHandlerFn(void* arg) { |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 425 | sigset64_t wait_set; |
| 426 | sigfillset64(&wait_set); |
| 427 | return reinterpret_cast<void*>(sigwait64(&wait_set, reinterpret_cast<int*>(arg))); |
| Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | TEST(pthread, pthread_sigmask) { |
| Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 431 | // Check that SIGUSR1 isn't blocked. |
| 432 | sigset_t original_set; |
| 433 | sigemptyset(&original_set); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 434 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, nullptr, &original_set)); |
| Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 435 | ASSERT_FALSE(sigismember(&original_set, SIGUSR1)); |
| 436 | |
| Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 437 | // Block SIGUSR1. |
| 438 | sigset_t set; |
| 439 | sigemptyset(&set); |
| 440 | sigaddset(&set, SIGUSR1); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 441 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, nullptr)); |
| Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 442 | |
| Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 443 | // Check that SIGUSR1 is blocked. |
| 444 | sigset_t final_set; |
| 445 | sigemptyset(&final_set); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 446 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, nullptr, &final_set)); |
| Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 447 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 448 | // ...and that sigprocmask agrees with pthread_sigmask. |
| 449 | sigemptyset(&final_set); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 450 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &final_set)); |
| Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 451 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 452 | |
| Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 453 | // Spawn a thread that calls sigwait and tells us what it received. |
| 454 | pthread_t signal_thread; |
| 455 | int received_signal = -1; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 456 | ASSERT_EQ(0, pthread_create(&signal_thread, nullptr, SignalHandlerFn, &received_signal)); |
| Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 457 | |
| 458 | // Send that thread SIGUSR1. |
| 459 | pthread_kill(signal_thread, SIGUSR1); |
| 460 | |
| 461 | // See what it got. |
| 462 | void* join_result; |
| 463 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 464 | ASSERT_EQ(SIGUSR1, received_signal); |
| Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 465 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
| Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 466 | |
| 467 | // Restore the original signal mask. |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 468 | ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, nullptr)); |
| Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 469 | } |
| Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 470 | |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 471 | TEST(pthread, pthread_sigmask64_SIGTRMIN) { |
| 472 | // Check that SIGRTMIN isn't blocked. |
| 473 | sigset64_t original_set; |
| 474 | sigemptyset64(&original_set); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 475 | ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, nullptr, &original_set)); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 476 | ASSERT_FALSE(sigismember64(&original_set, SIGRTMIN)); |
| 477 | |
| 478 | // Block SIGRTMIN. |
| 479 | sigset64_t set; |
| 480 | sigemptyset64(&set); |
| 481 | sigaddset64(&set, SIGRTMIN); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 482 | ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, &set, nullptr)); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 483 | |
| 484 | // Check that SIGRTMIN is blocked. |
| 485 | sigset64_t final_set; |
| 486 | sigemptyset64(&final_set); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 487 | ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, nullptr, &final_set)); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 488 | ASSERT_TRUE(sigismember64(&final_set, SIGRTMIN)); |
| 489 | // ...and that sigprocmask64 agrees with pthread_sigmask64. |
| 490 | sigemptyset64(&final_set); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 491 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, nullptr, &final_set)); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 492 | ASSERT_TRUE(sigismember64(&final_set, SIGRTMIN)); |
| 493 | |
| 494 | // Spawn a thread that calls sigwait64 and tells us what it received. |
| 495 | pthread_t signal_thread; |
| 496 | int received_signal = -1; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 497 | ASSERT_EQ(0, pthread_create(&signal_thread, nullptr, SignalHandlerFn, &received_signal)); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 498 | |
| 499 | // Send that thread SIGRTMIN. |
| 500 | pthread_kill(signal_thread, SIGRTMIN); |
| 501 | |
| 502 | // See what it got. |
| 503 | void* join_result; |
| 504 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 505 | ASSERT_EQ(SIGRTMIN, received_signal); |
| 506 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
| 507 | |
| 508 | // Restore the original signal mask. |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 509 | ASSERT_EQ(0, pthread_sigmask64(SIG_SETMASK, &original_set, nullptr)); |
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 512 | static void test_pthread_setname_np__pthread_getname_np(pthread_t t) { |
| 513 | ASSERT_EQ(0, pthread_setname_np(t, "short")); |
| 514 | char name[32]; |
| 515 | ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name))); |
| 516 | ASSERT_STREQ("short", name); |
| 517 | |
| Elliott Hughes | d1aea30 | 2015-04-25 10:05:24 -0700 | [diff] [blame] | 518 | // The limit is 15 characters --- the kernel's buffer is 16, but includes a NUL. |
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 519 | ASSERT_EQ(0, pthread_setname_np(t, "123456789012345")); |
| 520 | ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name))); |
| 521 | ASSERT_STREQ("123456789012345", name); |
| 522 | |
| 523 | ASSERT_EQ(ERANGE, pthread_setname_np(t, "1234567890123456")); |
| 524 | |
| 525 | // The passed-in buffer should be at least 16 bytes. |
| 526 | ASSERT_EQ(0, pthread_getname_np(t, name, 16)); |
| 527 | ASSERT_EQ(ERANGE, pthread_getname_np(t, name, 15)); |
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 530 | TEST(pthread, pthread_setname_np__pthread_getname_np__self) { |
| 531 | test_pthread_setname_np__pthread_getname_np(pthread_self()); |
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 534 | TEST(pthread, pthread_setname_np__pthread_getname_np__other) { |
| 535 | SpinFunctionHelper spin_helper; |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 536 | |
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 537 | pthread_t t; |
| Elliott Hughes | 4d098ca | 2016-04-11 12:43:05 -0700 | [diff] [blame] | 538 | ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr)); |
| 539 | test_pthread_setname_np__pthread_getname_np(t); |
| 540 | spin_helper.UnSpin(); |
| 541 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 542 | } |
| 543 | |
| 544 | // http://b/28051133: a kernel misfeature means that you can't change the |
| 545 | // name of another thread if you've set PR_SET_DUMPABLE to 0. |
| 546 | TEST(pthread, pthread_setname_np__pthread_getname_np__other_PR_SET_DUMPABLE) { |
| 547 | ASSERT_EQ(0, prctl(PR_SET_DUMPABLE, 0)) << strerror(errno); |
| 548 | |
| 549 | SpinFunctionHelper spin_helper; |
| 550 | |
| 551 | pthread_t t; |
| 552 | ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr)); |
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 553 | test_pthread_setname_np__pthread_getname_np(t); |
| 554 | spin_helper.UnSpin(); |
| 555 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 558 | TEST_F(pthread_DeathTest, pthread_setname_np__no_such_thread) { |
| Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 559 | pthread_t dead_thread; |
| 560 | MakeDeadThread(dead_thread); |
| 561 | |
| Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 562 | EXPECT_DEATH(pthread_setname_np(dead_thread, "short 3"), |
| 563 | "invalid pthread_t (.*) passed to pthread_setname_np"); |
| Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | TEST_F(pthread_DeathTest, pthread_setname_np__null_thread) { |
| 567 | pthread_t null_thread = 0; |
| 568 | EXPECT_EQ(ENOENT, pthread_setname_np(null_thread, "short 3")); |
| Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | TEST_F(pthread_DeathTest, pthread_getname_np__no_such_thread) { |
| 572 | pthread_t dead_thread; |
| 573 | MakeDeadThread(dead_thread); |
| 574 | |
| Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 575 | char name[64]; |
| Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 576 | EXPECT_DEATH(pthread_getname_np(dead_thread, name, sizeof(name)), |
| 577 | "invalid pthread_t (.*) passed to pthread_getname_np"); |
| Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | TEST_F(pthread_DeathTest, pthread_getname_np__null_thread) { |
| 581 | pthread_t null_thread = 0; |
| 582 | |
| 583 | char name[64]; |
| 584 | EXPECT_EQ(ENOENT, pthread_getname_np(null_thread, name, sizeof(name))); |
| Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 587 | TEST(pthread, pthread_kill__0) { |
| 588 | // Signal 0 just tests that the thread exists, so it's safe to call on ourselves. |
| 589 | ASSERT_EQ(0, pthread_kill(pthread_self(), 0)); |
| 590 | } |
| 591 | |
| 592 | TEST(pthread, pthread_kill__invalid_signal) { |
| 593 | ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1)); |
| 594 | } |
| 595 | |
| Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 596 | static void pthread_kill__in_signal_handler_helper(int signal_number) { |
| 597 | static int count = 0; |
| 598 | ASSERT_EQ(SIGALRM, signal_number); |
| 599 | if (++count == 1) { |
| 600 | // Can we call pthread_kill from a signal handler? |
| 601 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | TEST(pthread, pthread_kill__in_signal_handler) { |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 606 | ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper); |
| Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 607 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 608 | } |
| 609 | |
| Josh Gao | ddf757e | 2018-10-17 15:23:03 -0700 | [diff] [blame] | 610 | TEST(pthread, pthread_kill__exited_thread) { |
| 611 | static std::promise<pid_t> tid_promise; |
| 612 | pthread_t thread; |
| 613 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| 614 | [](void*) -> void* { |
| 615 | tid_promise.set_value(gettid()); |
| 616 | return nullptr; |
| 617 | }, |
| 618 | nullptr)); |
| 619 | |
| 620 | pid_t tid = tid_promise.get_future().get(); |
| 621 | while (TEMP_FAILURE_RETRY(syscall(__NR_tgkill, getpid(), tid, 0)) != -1) { |
| 622 | continue; |
| 623 | } |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 624 | ASSERT_ERRNO(ESRCH); |
| Josh Gao | ddf757e | 2018-10-17 15:23:03 -0700 | [diff] [blame] | 625 | |
| 626 | ASSERT_EQ(ESRCH, pthread_kill(thread, 0)); |
| 627 | } |
| 628 | |
| Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 629 | TEST_F(pthread_DeathTest, pthread_detach__no_such_thread) { |
| Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 630 | pthread_t dead_thread; |
| 631 | MakeDeadThread(dead_thread); |
| 632 | |
| Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 633 | EXPECT_DEATH(pthread_detach(dead_thread), |
| 634 | "invalid pthread_t (.*) passed to pthread_detach"); |
| Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | TEST_F(pthread_DeathTest, pthread_detach__null_thread) { |
| 638 | pthread_t null_thread = 0; |
| 639 | EXPECT_EQ(ESRCH, pthread_detach(null_thread)); |
| Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 642 | TEST(pthread, pthread_getcpuclockid__clock_gettime) { |
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 643 | SpinFunctionHelper spin_helper; |
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 644 | |
| Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 645 | pthread_t t; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 646 | ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr)); |
| Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 647 | |
| 648 | clockid_t c; |
| 649 | ASSERT_EQ(0, pthread_getcpuclockid(t, &c)); |
| 650 | timespec ts; |
| 651 | ASSERT_EQ(0, clock_gettime(c, &ts)); |
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 652 | spin_helper.UnSpin(); |
| Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 653 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 656 | TEST_F(pthread_DeathTest, pthread_getcpuclockid__no_such_thread) { |
| Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 657 | pthread_t dead_thread; |
| 658 | MakeDeadThread(dead_thread); |
| 659 | |
| 660 | clockid_t c; |
| Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 661 | EXPECT_DEATH(pthread_getcpuclockid(dead_thread, &c), |
| 662 | "invalid pthread_t (.*) passed to pthread_getcpuclockid"); |
| Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | TEST_F(pthread_DeathTest, pthread_getcpuclockid__null_thread) { |
| 666 | pthread_t null_thread = 0; |
| 667 | clockid_t c; |
| 668 | EXPECT_EQ(ESRCH, pthread_getcpuclockid(null_thread, &c)); |
| Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 671 | TEST_F(pthread_DeathTest, pthread_getschedparam__no_such_thread) { |
| Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 672 | pthread_t dead_thread; |
| 673 | MakeDeadThread(dead_thread); |
| 674 | |
| 675 | int policy; |
| 676 | sched_param param; |
| Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 677 | EXPECT_DEATH(pthread_getschedparam(dead_thread, &policy, ¶m), |
| 678 | "invalid pthread_t (.*) passed to pthread_getschedparam"); |
| Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | TEST_F(pthread_DeathTest, pthread_getschedparam__null_thread) { |
| 682 | pthread_t null_thread = 0; |
| 683 | int policy; |
| 684 | sched_param param; |
| 685 | EXPECT_EQ(ESRCH, pthread_getschedparam(null_thread, &policy, ¶m)); |
| Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 688 | TEST_F(pthread_DeathTest, pthread_setschedparam__no_such_thread) { |
| Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 689 | pthread_t dead_thread; |
| 690 | MakeDeadThread(dead_thread); |
| 691 | |
| 692 | int policy = 0; |
| 693 | sched_param param; |
| Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 694 | EXPECT_DEATH(pthread_setschedparam(dead_thread, policy, ¶m), |
| 695 | "invalid pthread_t (.*) passed to pthread_setschedparam"); |
| Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | TEST_F(pthread_DeathTest, pthread_setschedparam__null_thread) { |
| 699 | pthread_t null_thread = 0; |
| 700 | int policy = 0; |
| 701 | sched_param param; |
| 702 | EXPECT_EQ(ESRCH, pthread_setschedparam(null_thread, policy, ¶m)); |
| Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| Elliott Hughes | dff08ce | 2017-10-16 09:58:45 -0700 | [diff] [blame] | 705 | TEST_F(pthread_DeathTest, pthread_setschedprio__no_such_thread) { |
| 706 | pthread_t dead_thread; |
| 707 | MakeDeadThread(dead_thread); |
| 708 | |
| Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 709 | EXPECT_DEATH(pthread_setschedprio(dead_thread, 123), |
| 710 | "invalid pthread_t (.*) passed to pthread_setschedprio"); |
| Elliott Hughes | dff08ce | 2017-10-16 09:58:45 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | TEST_F(pthread_DeathTest, pthread_setschedprio__null_thread) { |
| 714 | pthread_t null_thread = 0; |
| 715 | EXPECT_EQ(ESRCH, pthread_setschedprio(null_thread, 123)); |
| 716 | } |
| 717 | |
| Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 718 | TEST_F(pthread_DeathTest, pthread_join__no_such_thread) { |
| Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 719 | pthread_t dead_thread; |
| 720 | MakeDeadThread(dead_thread); |
| 721 | |
| Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 722 | EXPECT_DEATH(pthread_join(dead_thread, nullptr), |
| 723 | "invalid pthread_t (.*) passed to pthread_join"); |
| Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | TEST_F(pthread_DeathTest, pthread_join__null_thread) { |
| 727 | pthread_t null_thread = 0; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 728 | EXPECT_EQ(ESRCH, pthread_join(null_thread, nullptr)); |
| Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 731 | TEST_F(pthread_DeathTest, pthread_kill__no_such_thread) { |
| Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 732 | pthread_t dead_thread; |
| 733 | MakeDeadThread(dead_thread); |
| 734 | |
| Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 735 | EXPECT_DEATH(pthread_kill(dead_thread, 0), |
| 736 | "invalid pthread_t (.*) passed to pthread_kill"); |
| Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | TEST_F(pthread_DeathTest, pthread_kill__null_thread) { |
| 740 | pthread_t null_thread = 0; |
| 741 | EXPECT_EQ(ESRCH, pthread_kill(null_thread, 0)); |
| Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 744 | TEST(pthread, pthread_join__multijoin) { |
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 745 | SpinFunctionHelper spin_helper; |
| msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 746 | |
| 747 | pthread_t t1; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 748 | ASSERT_EQ(0, pthread_create(&t1, nullptr, spin_helper.GetFunction(), nullptr)); |
| msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 749 | |
| 750 | pthread_t t2; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 751 | ASSERT_EQ(0, pthread_create(&t2, nullptr, JoinFn, reinterpret_cast<void*>(t1))); |
| msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 752 | |
| 753 | sleep(1); // (Give t2 a chance to call pthread_join.) |
| 754 | |
| 755 | // Multiple joins to the same thread should fail. |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 756 | ASSERT_EQ(EINVAL, pthread_join(t1, nullptr)); |
| msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 757 | |
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 758 | spin_helper.UnSpin(); |
| msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 759 | |
| 760 | // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). |
| 761 | void* join_result; |
| 762 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
| Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 763 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
| msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 764 | } |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 765 | |
| Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 766 | TEST(pthread, pthread_join__race) { |
| 767 | // http://b/11693195 --- pthread_join could return before the thread had actually exited. |
| 768 | // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread. |
| 769 | for (size_t i = 0; i < 1024; ++i) { |
| Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 770 | size_t stack_size = 640*1024; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 771 | void* stack = mmap(nullptr, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); |
| Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 772 | |
| 773 | pthread_attr_t a; |
| 774 | pthread_attr_init(&a); |
| 775 | pthread_attr_setstack(&a, stack, stack_size); |
| 776 | |
| 777 | pthread_t t; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 778 | ASSERT_EQ(0, pthread_create(&t, &a, IdFn, nullptr)); |
| 779 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 780 | ASSERT_EQ(0, munmap(stack, stack_size)); |
| 781 | } |
| 782 | } |
| 783 | |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 784 | static void* GetActualGuardSizeFn(void* arg) { |
| 785 | pthread_attr_t attributes; |
| 786 | pthread_getattr_np(pthread_self(), &attributes); |
| 787 | pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg)); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 788 | return nullptr; |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | static size_t GetActualGuardSize(const pthread_attr_t& attributes) { |
| 792 | size_t result; |
| 793 | pthread_t t; |
| 794 | pthread_create(&t, &attributes, GetActualGuardSizeFn, &result); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 795 | pthread_join(t, nullptr); |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 796 | return result; |
| 797 | } |
| 798 | |
| 799 | static void* GetActualStackSizeFn(void* arg) { |
| 800 | pthread_attr_t attributes; |
| 801 | pthread_getattr_np(pthread_self(), &attributes); |
| 802 | pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg)); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 803 | return nullptr; |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static size_t GetActualStackSize(const pthread_attr_t& attributes) { |
| 807 | size_t result; |
| 808 | pthread_t t; |
| 809 | pthread_create(&t, &attributes, GetActualStackSizeFn, &result); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 810 | pthread_join(t, nullptr); |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 811 | return result; |
| 812 | } |
| 813 | |
| Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 814 | TEST(pthread, pthread_attr_setguardsize_tiny) { |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 815 | pthread_attr_t attributes; |
| 816 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 817 | |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 818 | // No such thing as too small: will be rounded up to one page by pthread_create. |
| 819 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128)); |
| 820 | size_t guard_size; |
| 821 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 822 | ASSERT_EQ(128U, guard_size); |
| Juan Yescas | 65af9a8 | 2023-12-07 11:35:21 -0800 | [diff] [blame] | 823 | ASSERT_EQ(static_cast<unsigned long>(getpagesize()), GetActualGuardSize(attributes)); |
| Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | TEST(pthread, pthread_attr_setguardsize_reasonable) { |
| 827 | pthread_attr_t attributes; |
| 828 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 829 | |
| 830 | // Large enough and a multiple of the page size. |
| 831 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024)); |
| Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 832 | size_t guard_size; |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 833 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 834 | ASSERT_EQ(32*1024U, guard_size); |
| Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 835 | ASSERT_EQ(32*1024U, GetActualGuardSize(attributes)); |
| 836 | } |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 837 | |
| Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 838 | TEST(pthread, pthread_attr_setguardsize_needs_rounding) { |
| 839 | pthread_attr_t attributes; |
| 840 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 841 | |
| 842 | // Large enough but not a multiple of the page size. |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 843 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1)); |
| Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 844 | size_t guard_size; |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 845 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 846 | ASSERT_EQ(32*1024U + 1, guard_size); |
| Juan Yescas | 65af9a8 | 2023-12-07 11:35:21 -0800 | [diff] [blame] | 847 | ASSERT_EQ(roundup(32 * 1024U + 1, getpagesize()), GetActualGuardSize(attributes)); |
| Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | TEST(pthread, pthread_attr_setguardsize_enormous) { |
| 851 | pthread_attr_t attributes; |
| 852 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 853 | |
| 854 | // Larger than the stack itself. (Historically we mistakenly carved |
| 855 | // the guard out of the stack itself, rather than adding it after the |
| 856 | // end.) |
| 857 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024*1024)); |
| 858 | size_t guard_size; |
| 859 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 860 | ASSERT_EQ(32*1024*1024U, guard_size); |
| 861 | ASSERT_EQ(32*1024*1024U, GetActualGuardSize(attributes)); |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | TEST(pthread, pthread_attr_setstacksize) { |
| 865 | pthread_attr_t attributes; |
| 866 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 867 | |
| 868 | // Get the default stack size. |
| 869 | size_t default_stack_size; |
| 870 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size)); |
| 871 | |
| 872 | // Too small. |
| 873 | ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128)); |
| 874 | size_t stack_size; |
| 875 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 876 | ASSERT_EQ(default_stack_size, stack_size); |
| 877 | ASSERT_GE(GetActualStackSize(attributes), default_stack_size); |
| 878 | |
| Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 879 | // Large enough and a multiple of the page size; may be rounded up by pthread_create. |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 880 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024)); |
| 881 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 882 | ASSERT_EQ(32*1024U, stack_size); |
| Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 883 | ASSERT_GE(GetActualStackSize(attributes), 32*1024U); |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 884 | |
| Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 885 | // Large enough but not aligned; will be rounded up by pthread_create. |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 886 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1)); |
| 887 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 888 | ASSERT_EQ(32*1024U + 1, stack_size); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 889 | #if defined(__BIONIC__) |
| Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 890 | ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 891 | #else // __BIONIC__ |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 892 | // glibc rounds down, in violation of POSIX. They document this in their BUGS section. |
| 893 | ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 894 | #endif // __BIONIC__ |
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 895 | } |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 896 | |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 897 | TEST(pthread, pthread_rwlockattr_smoke) { |
| 898 | pthread_rwlockattr_t attr; |
| 899 | ASSERT_EQ(0, pthread_rwlockattr_init(&attr)); |
| 900 | |
| 901 | int pshared_value_array[] = {PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED}; |
| 902 | for (size_t i = 0; i < sizeof(pshared_value_array) / sizeof(pshared_value_array[0]); ++i) { |
| 903 | ASSERT_EQ(0, pthread_rwlockattr_setpshared(&attr, pshared_value_array[i])); |
| 904 | int pshared; |
| 905 | ASSERT_EQ(0, pthread_rwlockattr_getpshared(&attr, &pshared)); |
| 906 | ASSERT_EQ(pshared_value_array[i], pshared); |
| 907 | } |
| 908 | |
| Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 909 | #if !defined(ANDROID_HOST_MUSL) |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 910 | // musl doesn't have pthread_rwlockattr_setkind_np |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 911 | int kind_array[] = {PTHREAD_RWLOCK_PREFER_READER_NP, |
| 912 | PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP}; |
| 913 | for (size_t i = 0; i < sizeof(kind_array) / sizeof(kind_array[0]); ++i) { |
| 914 | ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_array[i])); |
| 915 | int kind; |
| 916 | ASSERT_EQ(0, pthread_rwlockattr_getkind_np(&attr, &kind)); |
| 917 | ASSERT_EQ(kind_array[i], kind); |
| 918 | } |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 919 | #endif |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 920 | |
| 921 | ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr)); |
| 922 | } |
| 923 | |
| 924 | TEST(pthread, pthread_rwlock_init_same_as_PTHREAD_RWLOCK_INITIALIZER) { |
| 925 | pthread_rwlock_t lock1 = PTHREAD_RWLOCK_INITIALIZER; |
| 926 | pthread_rwlock_t lock2; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 927 | ASSERT_EQ(0, pthread_rwlock_init(&lock2, nullptr)); |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 928 | ASSERT_EQ(0, memcmp(&lock1, &lock2, sizeof(lock1))); |
| 929 | } |
| 930 | |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 931 | TEST(pthread, pthread_rwlock_smoke) { |
| 932 | pthread_rwlock_t l; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 933 | ASSERT_EQ(0, pthread_rwlock_init(&l, nullptr)); |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 934 | |
| Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 935 | // Single read lock |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 936 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 937 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 938 | |
| Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 939 | // Multiple read lock |
| 940 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 941 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 942 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 943 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 944 | |
| 945 | // Write lock |
| Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 946 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 947 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 948 | |
| 949 | // Try writer lock |
| 950 | ASSERT_EQ(0, pthread_rwlock_trywrlock(&l)); |
| 951 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 952 | ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l)); |
| 953 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 954 | |
| 955 | // Try reader lock |
| 956 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 957 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 958 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 959 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 960 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 961 | |
| 962 | // Try writer lock after unlock |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 963 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 964 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 965 | |
| Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 966 | // EDEADLK in "read after write" |
| 967 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 968 | ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l)); |
| 969 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 970 | |
| 971 | // EDEADLK in "write after write" |
| 972 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 973 | ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l)); |
| 974 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 975 | |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 976 | ASSERT_EQ(0, pthread_rwlock_destroy(&l)); |
| 977 | } |
| 978 | |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 979 | struct RwlockWakeupHelperArg { |
| 980 | pthread_rwlock_t lock; |
| 981 | enum Progress { |
| 982 | LOCK_INITIALIZED, |
| 983 | LOCK_WAITING, |
| 984 | LOCK_RELEASED, |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 985 | LOCK_ACCESSED, |
| 986 | LOCK_TIMEDOUT, |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 987 | }; |
| 988 | std::atomic<Progress> progress; |
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 989 | std::atomic<pid_t> tid; |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 990 | std::function<int (pthread_rwlock_t*)> trylock_function; |
| 991 | std::function<int (pthread_rwlock_t*)> lock_function; |
| 992 | std::function<int (pthread_rwlock_t*, const timespec*)> timed_lock_function; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 993 | clockid_t clock; |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 994 | }; |
| 995 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 996 | static void pthread_rwlock_wakeup_helper(RwlockWakeupHelperArg* arg) { |
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 997 | arg->tid = gettid(); |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 998 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress); |
| 999 | arg->progress = RwlockWakeupHelperArg::LOCK_WAITING; |
| 1000 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1001 | ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock)); |
| 1002 | ASSERT_EQ(0, arg->lock_function(&arg->lock)); |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1003 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress); |
| 1004 | ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock)); |
| 1005 | |
| 1006 | arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED; |
| 1007 | } |
| 1008 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1009 | static void test_pthread_rwlock_reader_wakeup_writer(std::function<int (pthread_rwlock_t*)> lock_function) { |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1010 | RwlockWakeupHelperArg wakeup_arg; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1011 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr)); |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1012 | ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock)); |
| 1013 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1014 | wakeup_arg.tid = 0; |
| Tom Cherry | 60ddedf | 2018-02-20 15:40:02 -0800 | [diff] [blame] | 1015 | wakeup_arg.trylock_function = &pthread_rwlock_trywrlock; |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1016 | wakeup_arg.lock_function = lock_function; |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1017 | |
| 1018 | pthread_t thread; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1019 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1020 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg)); |
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1021 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 1022 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 1023 | |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1024 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED; |
| 1025 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 1026 | |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1027 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1028 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress); |
| 1029 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 1030 | } |
| 1031 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1032 | TEST(pthread, pthread_rwlock_reader_wakeup_writer) { |
| 1033 | test_pthread_rwlock_reader_wakeup_writer(pthread_rwlock_wrlock); |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1036 | TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait) { |
| 1037 | timespec ts; |
| 1038 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 1039 | ts.tv_sec += 1; |
| 1040 | test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) { |
| 1041 | return pthread_rwlock_timedwrlock(lock, &ts); |
| 1042 | }); |
| 1043 | } |
| 1044 | |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1045 | TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait_monotonic_np) { |
| 1046 | #if defined(__BIONIC__) |
| 1047 | timespec ts; |
| 1048 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); |
| 1049 | ts.tv_sec += 1; |
| 1050 | test_pthread_rwlock_reader_wakeup_writer( |
| 1051 | [&](pthread_rwlock_t* lock) { return pthread_rwlock_timedwrlock_monotonic_np(lock, &ts); }); |
| 1052 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1053 | GTEST_SKIP() << "pthread_rwlock_timedwrlock_monotonic_np not available"; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1054 | #endif // __BIONIC__ |
| 1055 | } |
| 1056 | |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1057 | TEST(pthread, pthread_rwlock_reader_wakeup_writer_clockwait) { |
| 1058 | #if defined(__BIONIC__) |
| 1059 | timespec ts; |
| 1060 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); |
| 1061 | ts.tv_sec += 1; |
| 1062 | test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) { |
| 1063 | return pthread_rwlock_clockwrlock(lock, CLOCK_MONOTONIC, &ts); |
| 1064 | }); |
| 1065 | |
| 1066 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 1067 | ts.tv_sec += 1; |
| 1068 | test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) { |
| 1069 | return pthread_rwlock_clockwrlock(lock, CLOCK_REALTIME, &ts); |
| 1070 | }); |
| 1071 | #else // __BIONIC__ |
| 1072 | GTEST_SKIP() << "pthread_rwlock_clockwrlock not available"; |
| 1073 | #endif // __BIONIC__ |
| 1074 | } |
| 1075 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1076 | static void test_pthread_rwlock_writer_wakeup_reader(std::function<int (pthread_rwlock_t*)> lock_function) { |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1077 | RwlockWakeupHelperArg wakeup_arg; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1078 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr)); |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1079 | ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock)); |
| 1080 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1081 | wakeup_arg.tid = 0; |
| Tom Cherry | 60ddedf | 2018-02-20 15:40:02 -0800 | [diff] [blame] | 1082 | wakeup_arg.trylock_function = &pthread_rwlock_tryrdlock; |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1083 | wakeup_arg.lock_function = lock_function; |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1084 | |
| 1085 | pthread_t thread; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1086 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1087 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg)); |
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1088 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 1089 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 1090 | |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1091 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED; |
| 1092 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 1093 | |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1094 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1095 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress); |
| 1096 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 1097 | } |
| 1098 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1099 | TEST(pthread, pthread_rwlock_writer_wakeup_reader) { |
| 1100 | test_pthread_rwlock_writer_wakeup_reader(pthread_rwlock_rdlock); |
| 1101 | } |
| 1102 | |
| 1103 | TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait) { |
| 1104 | timespec ts; |
| 1105 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 1106 | ts.tv_sec += 1; |
| 1107 | test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) { |
| 1108 | return pthread_rwlock_timedrdlock(lock, &ts); |
| 1109 | }); |
| 1110 | } |
| 1111 | |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1112 | TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait_monotonic_np) { |
| 1113 | #if defined(__BIONIC__) |
| 1114 | timespec ts; |
| 1115 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); |
| 1116 | ts.tv_sec += 1; |
| 1117 | test_pthread_rwlock_writer_wakeup_reader( |
| 1118 | [&](pthread_rwlock_t* lock) { return pthread_rwlock_timedrdlock_monotonic_np(lock, &ts); }); |
| 1119 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1120 | GTEST_SKIP() << "pthread_rwlock_timedrdlock_monotonic_np not available"; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1121 | #endif // __BIONIC__ |
| 1122 | } |
| 1123 | |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1124 | TEST(pthread, pthread_rwlock_writer_wakeup_reader_clockwait) { |
| 1125 | #if defined(__BIONIC__) |
| 1126 | timespec ts; |
| 1127 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); |
| 1128 | ts.tv_sec += 1; |
| 1129 | test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) { |
| 1130 | return pthread_rwlock_clockrdlock(lock, CLOCK_MONOTONIC, &ts); |
| 1131 | }); |
| 1132 | |
| 1133 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 1134 | ts.tv_sec += 1; |
| 1135 | test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) { |
| 1136 | return pthread_rwlock_clockrdlock(lock, CLOCK_REALTIME, &ts); |
| 1137 | }); |
| 1138 | #else // __BIONIC__ |
| 1139 | GTEST_SKIP() << "pthread_rwlock_clockrdlock not available"; |
| 1140 | #endif // __BIONIC__ |
| 1141 | } |
| 1142 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1143 | static void pthread_rwlock_wakeup_timeout_helper(RwlockWakeupHelperArg* arg) { |
| 1144 | arg->tid = gettid(); |
| 1145 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress); |
| 1146 | arg->progress = RwlockWakeupHelperArg::LOCK_WAITING; |
| 1147 | |
| 1148 | ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock)); |
| 1149 | |
| 1150 | timespec ts; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1151 | ASSERT_EQ(0, clock_gettime(arg->clock, &ts)); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1152 | ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts)); |
| 1153 | ts.tv_nsec = -1; |
| 1154 | ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts)); |
| 1155 | ts.tv_nsec = NS_PER_S; |
| 1156 | ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts)); |
| 1157 | ts.tv_nsec = NS_PER_S - 1; |
| 1158 | ts.tv_sec = -1; |
| 1159 | ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts)); |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1160 | ASSERT_EQ(0, clock_gettime(arg->clock, &ts)); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1161 | ts.tv_sec += 1; |
| 1162 | ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts)); |
| 1163 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, arg->progress); |
| 1164 | arg->progress = RwlockWakeupHelperArg::LOCK_TIMEDOUT; |
| 1165 | } |
| 1166 | |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1167 | static void pthread_rwlock_timedrdlock_timeout_helper( |
| 1168 | clockid_t clock, int (*lock_function)(pthread_rwlock_t* __rwlock, const timespec* __timeout)) { |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1169 | RwlockWakeupHelperArg wakeup_arg; |
| 1170 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr)); |
| 1171 | ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock)); |
| 1172 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
| 1173 | wakeup_arg.tid = 0; |
| Tom Cherry | 60ddedf | 2018-02-20 15:40:02 -0800 | [diff] [blame] | 1174 | wakeup_arg.trylock_function = &pthread_rwlock_tryrdlock; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1175 | wakeup_arg.timed_lock_function = lock_function; |
| 1176 | wakeup_arg.clock = clock; |
| 1177 | |
| 1178 | pthread_t thread; |
| 1179 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| 1180 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg)); |
| 1181 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 1182 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 1183 | |
| 1184 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 1185 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress); |
| 1186 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 1187 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 1188 | } |
| 1189 | |
| 1190 | TEST(pthread, pthread_rwlock_timedrdlock_timeout) { |
| 1191 | pthread_rwlock_timedrdlock_timeout_helper(CLOCK_REALTIME, pthread_rwlock_timedrdlock); |
| 1192 | } |
| 1193 | |
| 1194 | TEST(pthread, pthread_rwlock_timedrdlock_monotonic_np_timeout) { |
| 1195 | #if defined(__BIONIC__) |
| 1196 | pthread_rwlock_timedrdlock_timeout_helper(CLOCK_MONOTONIC, |
| 1197 | pthread_rwlock_timedrdlock_monotonic_np); |
| 1198 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1199 | GTEST_SKIP() << "pthread_rwlock_timedrdlock_monotonic_np not available"; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1200 | #endif // __BIONIC__ |
| 1201 | } |
| 1202 | |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1203 | TEST(pthread, pthread_rwlock_clockrdlock_monotonic_timeout) { |
| 1204 | #if defined(__BIONIC__) |
| 1205 | pthread_rwlock_timedrdlock_timeout_helper( |
| 1206 | CLOCK_MONOTONIC, [](pthread_rwlock_t* __rwlock, const timespec* __timeout) { |
| 1207 | return pthread_rwlock_clockrdlock(__rwlock, CLOCK_MONOTONIC, __timeout); |
| 1208 | }); |
| 1209 | #else // __BIONIC__ |
| 1210 | GTEST_SKIP() << "pthread_rwlock_clockrdlock not available"; |
| 1211 | #endif // __BIONIC__ |
| 1212 | } |
| 1213 | |
| 1214 | TEST(pthread, pthread_rwlock_clockrdlock_realtime_timeout) { |
| 1215 | #if defined(__BIONIC__) |
| 1216 | pthread_rwlock_timedrdlock_timeout_helper( |
| 1217 | CLOCK_REALTIME, [](pthread_rwlock_t* __rwlock, const timespec* __timeout) { |
| 1218 | return pthread_rwlock_clockrdlock(__rwlock, CLOCK_REALTIME, __timeout); |
| 1219 | }); |
| 1220 | #else // __BIONIC__ |
| 1221 | GTEST_SKIP() << "pthread_rwlock_clockrdlock not available"; |
| 1222 | #endif // __BIONIC__ |
| 1223 | } |
| 1224 | |
| 1225 | TEST(pthread, pthread_rwlock_clockrdlock_invalid) { |
| 1226 | #if defined(__BIONIC__) |
| 1227 | pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER; |
| 1228 | timespec ts; |
| 1229 | EXPECT_EQ(EINVAL, pthread_rwlock_clockrdlock(&lock, CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 1230 | #else // __BIONIC__ |
| 1231 | GTEST_SKIP() << "pthread_rwlock_clockrdlock not available"; |
| 1232 | #endif // __BIONIC__ |
| 1233 | } |
| 1234 | |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1235 | static void pthread_rwlock_timedwrlock_timeout_helper( |
| 1236 | clockid_t clock, int (*lock_function)(pthread_rwlock_t* __rwlock, const timespec* __timeout)) { |
| 1237 | RwlockWakeupHelperArg wakeup_arg; |
| 1238 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr)); |
| 1239 | ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock)); |
| 1240 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
| 1241 | wakeup_arg.tid = 0; |
| 1242 | wakeup_arg.trylock_function = &pthread_rwlock_trywrlock; |
| 1243 | wakeup_arg.timed_lock_function = lock_function; |
| 1244 | wakeup_arg.clock = clock; |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1245 | |
| 1246 | pthread_t thread; |
| 1247 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| 1248 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg)); |
| 1249 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 1250 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 1251 | |
| 1252 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 1253 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress); |
| 1254 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 1255 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 1256 | } |
| 1257 | |
| 1258 | TEST(pthread, pthread_rwlock_timedwrlock_timeout) { |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1259 | pthread_rwlock_timedwrlock_timeout_helper(CLOCK_REALTIME, pthread_rwlock_timedwrlock); |
| 1260 | } |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1261 | |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1262 | TEST(pthread, pthread_rwlock_timedwrlock_monotonic_np_timeout) { |
| 1263 | #if defined(__BIONIC__) |
| 1264 | pthread_rwlock_timedwrlock_timeout_helper(CLOCK_MONOTONIC, |
| 1265 | pthread_rwlock_timedwrlock_monotonic_np); |
| 1266 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1267 | GTEST_SKIP() << "pthread_rwlock_timedwrlock_monotonic_np not available"; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1268 | #endif // __BIONIC__ |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1269 | } |
| 1270 | |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1271 | TEST(pthread, pthread_rwlock_clockwrlock_monotonic_timeout) { |
| 1272 | #if defined(__BIONIC__) |
| 1273 | pthread_rwlock_timedwrlock_timeout_helper( |
| 1274 | CLOCK_MONOTONIC, [](pthread_rwlock_t* __rwlock, const timespec* __timeout) { |
| 1275 | return pthread_rwlock_clockwrlock(__rwlock, CLOCK_MONOTONIC, __timeout); |
| 1276 | }); |
| 1277 | #else // __BIONIC__ |
| 1278 | GTEST_SKIP() << "pthread_rwlock_clockwrlock not available"; |
| 1279 | #endif // __BIONIC__ |
| 1280 | } |
| 1281 | |
| 1282 | TEST(pthread, pthread_rwlock_clockwrlock_realtime_timeout) { |
| 1283 | #if defined(__BIONIC__) |
| 1284 | pthread_rwlock_timedwrlock_timeout_helper( |
| 1285 | CLOCK_REALTIME, [](pthread_rwlock_t* __rwlock, const timespec* __timeout) { |
| 1286 | return pthread_rwlock_clockwrlock(__rwlock, CLOCK_REALTIME, __timeout); |
| 1287 | }); |
| 1288 | #else // __BIONIC__ |
| 1289 | GTEST_SKIP() << "pthread_rwlock_clockwrlock not available"; |
| 1290 | #endif // __BIONIC__ |
| 1291 | } |
| 1292 | |
| 1293 | TEST(pthread, pthread_rwlock_clockwrlock_invalid) { |
| 1294 | #if defined(__BIONIC__) |
| 1295 | pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER; |
| 1296 | timespec ts; |
| 1297 | EXPECT_EQ(EINVAL, pthread_rwlock_clockwrlock(&lock, CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 1298 | #else // __BIONIC__ |
| 1299 | GTEST_SKIP() << "pthread_rwlock_clockrwlock not available"; |
| 1300 | #endif // __BIONIC__ |
| 1301 | } |
| 1302 | |
| Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 1303 | #if !defined(ANDROID_HOST_MUSL) |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 1304 | // musl doesn't have pthread_rwlockattr_setkind_np |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1305 | class RwlockKindTestHelper { |
| 1306 | private: |
| 1307 | struct ThreadArg { |
| 1308 | RwlockKindTestHelper* helper; |
| 1309 | std::atomic<pid_t>& tid; |
| 1310 | |
| 1311 | ThreadArg(RwlockKindTestHelper* helper, std::atomic<pid_t>& tid) |
| 1312 | : helper(helper), tid(tid) { } |
| 1313 | }; |
| 1314 | |
| 1315 | public: |
| 1316 | pthread_rwlock_t lock; |
| 1317 | |
| 1318 | public: |
| Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 1319 | explicit RwlockKindTestHelper(int kind_type) { |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1320 | InitRwlock(kind_type); |
| 1321 | } |
| 1322 | |
| 1323 | ~RwlockKindTestHelper() { |
| 1324 | DestroyRwlock(); |
| 1325 | } |
| 1326 | |
| 1327 | void CreateWriterThread(pthread_t& thread, std::atomic<pid_t>& tid) { |
| 1328 | tid = 0; |
| 1329 | ThreadArg* arg = new ThreadArg(this, tid); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1330 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1331 | reinterpret_cast<void* (*)(void*)>(WriterThreadFn), arg)); |
| 1332 | } |
| 1333 | |
| 1334 | void CreateReaderThread(pthread_t& thread, std::atomic<pid_t>& tid) { |
| 1335 | tid = 0; |
| 1336 | ThreadArg* arg = new ThreadArg(this, tid); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1337 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1338 | reinterpret_cast<void* (*)(void*)>(ReaderThreadFn), arg)); |
| 1339 | } |
| 1340 | |
| 1341 | private: |
| 1342 | void InitRwlock(int kind_type) { |
| 1343 | pthread_rwlockattr_t attr; |
| 1344 | ASSERT_EQ(0, pthread_rwlockattr_init(&attr)); |
| 1345 | ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_type)); |
| 1346 | ASSERT_EQ(0, pthread_rwlock_init(&lock, &attr)); |
| 1347 | ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr)); |
| 1348 | } |
| 1349 | |
| 1350 | void DestroyRwlock() { |
| 1351 | ASSERT_EQ(0, pthread_rwlock_destroy(&lock)); |
| 1352 | } |
| 1353 | |
| 1354 | static void WriterThreadFn(ThreadArg* arg) { |
| 1355 | arg->tid = gettid(); |
| 1356 | |
| 1357 | RwlockKindTestHelper* helper = arg->helper; |
| 1358 | ASSERT_EQ(0, pthread_rwlock_wrlock(&helper->lock)); |
| 1359 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock)); |
| 1360 | delete arg; |
| 1361 | } |
| 1362 | |
| 1363 | static void ReaderThreadFn(ThreadArg* arg) { |
| 1364 | arg->tid = gettid(); |
| 1365 | |
| 1366 | RwlockKindTestHelper* helper = arg->helper; |
| 1367 | ASSERT_EQ(0, pthread_rwlock_rdlock(&helper->lock)); |
| 1368 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock)); |
| 1369 | delete arg; |
| 1370 | } |
| 1371 | }; |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 1372 | #endif |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1373 | |
| 1374 | TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_READER_NP) { |
| Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 1375 | #if !defined(ANDROID_HOST_MUSL) |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1376 | RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_READER_NP); |
| 1377 | ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock)); |
| 1378 | |
| 1379 | pthread_t writer_thread; |
| 1380 | std::atomic<pid_t> writer_tid; |
| 1381 | helper.CreateWriterThread(writer_thread, writer_tid); |
| 1382 | WaitUntilThreadSleep(writer_tid); |
| 1383 | |
| 1384 | pthread_t reader_thread; |
| 1385 | std::atomic<pid_t> reader_tid; |
| 1386 | helper.CreateReaderThread(reader_thread, reader_tid); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1387 | ASSERT_EQ(0, pthread_join(reader_thread, nullptr)); |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1388 | |
| 1389 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock)); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1390 | ASSERT_EQ(0, pthread_join(writer_thread, nullptr)); |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 1391 | #else |
| 1392 | GTEST_SKIP() << "musl doesn't have pthread_rwlockattr_setkind_np"; |
| 1393 | #endif |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) { |
| Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 1397 | #if !defined(ANDROID_HOST_MUSL) |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1398 | RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); |
| 1399 | ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock)); |
| 1400 | |
| 1401 | pthread_t writer_thread; |
| 1402 | std::atomic<pid_t> writer_tid; |
| 1403 | helper.CreateWriterThread(writer_thread, writer_tid); |
| 1404 | WaitUntilThreadSleep(writer_tid); |
| 1405 | |
| 1406 | pthread_t reader_thread; |
| 1407 | std::atomic<pid_t> reader_tid; |
| 1408 | helper.CreateReaderThread(reader_thread, reader_tid); |
| 1409 | WaitUntilThreadSleep(reader_tid); |
| 1410 | |
| 1411 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock)); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1412 | ASSERT_EQ(0, pthread_join(writer_thread, nullptr)); |
| 1413 | ASSERT_EQ(0, pthread_join(reader_thread, nullptr)); |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 1414 | #else |
| 1415 | GTEST_SKIP() << "musl doesn't have pthread_rwlockattr_setkind_np"; |
| 1416 | #endif |
| Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1419 | static int g_once_fn_call_count = 0; |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1420 | static void OnceFn() { |
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1421 | ++g_once_fn_call_count; |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | TEST(pthread, pthread_once_smoke) { |
| 1425 | pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 1426 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
| 1427 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1428 | ASSERT_EQ(1, g_once_fn_call_count); |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
| Elliott Hughes | 3694ec6 | 2014-05-14 11:46:08 -0700 | [diff] [blame] | 1431 | static std::string pthread_once_1934122_result = ""; |
| 1432 | |
| 1433 | static void Routine2() { |
| 1434 | pthread_once_1934122_result += "2"; |
| 1435 | } |
| 1436 | |
| 1437 | static void Routine1() { |
| 1438 | pthread_once_t once_control_2 = PTHREAD_ONCE_INIT; |
| 1439 | pthread_once_1934122_result += "1"; |
| 1440 | pthread_once(&once_control_2, &Routine2); |
| 1441 | } |
| 1442 | |
| 1443 | TEST(pthread, pthread_once_1934122) { |
| 1444 | // Very old versions of Android couldn't call pthread_once from a |
| 1445 | // pthread_once init routine. http://b/1934122. |
| 1446 | pthread_once_t once_control_1 = PTHREAD_ONCE_INIT; |
| 1447 | ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1)); |
| 1448 | ASSERT_EQ("12", pthread_once_1934122_result); |
| 1449 | } |
| 1450 | |
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1451 | static int g_atfork_prepare_calls = 0; |
| Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1452 | static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; } |
| 1453 | static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 2; } |
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1454 | static int g_atfork_parent_calls = 0; |
| Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1455 | static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 1; } |
| 1456 | static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 2; } |
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1457 | static int g_atfork_child_calls = 0; |
| Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1458 | static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 1; } |
| 1459 | static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 2; } |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1460 | |
| Elliott Hughes | 2411fff | 2024-02-24 23:55:58 +0000 | [diff] [blame] | 1461 | TEST(pthread, pthread_atfork_smoke_fork) { |
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1462 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); |
| 1463 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1464 | |
| Elliott Hughes | 2411fff | 2024-02-24 23:55:58 +0000 | [diff] [blame] | 1465 | g_atfork_prepare_calls = g_atfork_parent_calls = g_atfork_child_calls = 0; |
| Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 1466 | pid_t pid = fork(); |
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1467 | ASSERT_NE(-1, pid) << strerror(errno); |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1468 | |
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1469 | // Child and parent calls are made in the order they were registered. |
| 1470 | if (pid == 0) { |
| Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1471 | ASSERT_EQ(12, g_atfork_child_calls); |
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1472 | _exit(0); |
| 1473 | } |
| Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1474 | ASSERT_EQ(12, g_atfork_parent_calls); |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1475 | |
| Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1476 | // Prepare calls are made in the reverse order. |
| Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1477 | ASSERT_EQ(21, g_atfork_prepare_calls); |
| Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 1478 | AssertChildExited(pid, 0); |
| Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
| Elliott Hughes | 2411fff | 2024-02-24 23:55:58 +0000 | [diff] [blame] | 1481 | TEST(pthread, pthread_atfork_smoke_vfork) { |
| 1482 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); |
| 1483 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); |
| 1484 | |
| 1485 | g_atfork_prepare_calls = g_atfork_parent_calls = g_atfork_child_calls = 0; |
| 1486 | pid_t pid = vfork(); |
| 1487 | ASSERT_NE(-1, pid) << strerror(errno); |
| 1488 | |
| 1489 | // atfork handlers are not called. |
| 1490 | if (pid == 0) { |
| 1491 | ASSERT_EQ(0, g_atfork_child_calls); |
| 1492 | _exit(0); |
| 1493 | } |
| 1494 | ASSERT_EQ(0, g_atfork_parent_calls); |
| 1495 | ASSERT_EQ(0, g_atfork_prepare_calls); |
| 1496 | AssertChildExited(pid, 0); |
| 1497 | } |
| 1498 | |
| 1499 | TEST(pthread, pthread_atfork_smoke__Fork) { |
| 1500 | #if defined(__BIONIC__) |
| 1501 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); |
| 1502 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); |
| 1503 | |
| 1504 | g_atfork_prepare_calls = g_atfork_parent_calls = g_atfork_child_calls = 0; |
| 1505 | pid_t pid = _Fork(); |
| 1506 | ASSERT_NE(-1, pid) << strerror(errno); |
| 1507 | |
| 1508 | // atfork handlers are not called. |
| 1509 | if (pid == 0) { |
| 1510 | ASSERT_EQ(0, g_atfork_child_calls); |
| 1511 | _exit(0); |
| 1512 | } |
| 1513 | ASSERT_EQ(0, g_atfork_parent_calls); |
| 1514 | ASSERT_EQ(0, g_atfork_prepare_calls); |
| 1515 | AssertChildExited(pid, 0); |
| 1516 | #endif |
| 1517 | } |
| 1518 | |
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1519 | TEST(pthread, pthread_attr_getscope) { |
| 1520 | pthread_attr_t attr; |
| 1521 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 1522 | |
| 1523 | int scope; |
| 1524 | ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope)); |
| 1525 | ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope); |
| 1526 | } |
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1527 | |
| 1528 | TEST(pthread, pthread_condattr_init) { |
| 1529 | pthread_condattr_t attr; |
| 1530 | pthread_condattr_init(&attr); |
| 1531 | |
| 1532 | clockid_t clock; |
| 1533 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1534 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 1535 | |
| 1536 | int pshared; |
| 1537 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 1538 | ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); |
| 1539 | } |
| 1540 | |
| 1541 | TEST(pthread, pthread_condattr_setclock) { |
| 1542 | pthread_condattr_t attr; |
| 1543 | pthread_condattr_init(&attr); |
| 1544 | |
| 1545 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME)); |
| 1546 | clockid_t clock; |
| 1547 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1548 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 1549 | |
| 1550 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 1551 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1552 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 1553 | |
| 1554 | ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID)); |
| 1555 | } |
| 1556 | |
| 1557 | TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) { |
| Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1558 | #if defined(__BIONIC__) |
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1559 | pthread_condattr_t attr; |
| 1560 | pthread_condattr_init(&attr); |
| 1561 | |
| 1562 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 1563 | ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); |
| 1564 | |
| 1565 | pthread_cond_t cond_var; |
| 1566 | ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr)); |
| 1567 | |
| 1568 | ASSERT_EQ(0, pthread_cond_signal(&cond_var)); |
| 1569 | ASSERT_EQ(0, pthread_cond_broadcast(&cond_var)); |
| 1570 | |
| Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1571 | attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private)); |
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1572 | clockid_t clock; |
| 1573 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1574 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 1575 | int pshared; |
| 1576 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 1577 | ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); |
| Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1578 | #else // !defined(__BIONIC__) |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1579 | GTEST_SKIP() << "bionic-only test"; |
| Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1580 | #endif // !defined(__BIONIC__) |
| 1581 | } |
| 1582 | |
| 1583 | class pthread_CondWakeupTest : public ::testing::Test { |
| 1584 | protected: |
| 1585 | pthread_mutex_t mutex; |
| 1586 | pthread_cond_t cond; |
| 1587 | |
| 1588 | enum Progress { |
| 1589 | INITIALIZED, |
| 1590 | WAITING, |
| 1591 | SIGNALED, |
| 1592 | FINISHED, |
| 1593 | }; |
| 1594 | std::atomic<Progress> progress; |
| 1595 | pthread_t thread; |
| Peter Collingbourne | c5b8184 | 2021-12-02 12:38:46 -0800 | [diff] [blame] | 1596 | timespec ts; |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1597 | std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function; |
| Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1598 | |
| 1599 | protected: |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1600 | void SetUp() override { |
| 1601 | ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr)); |
| 1602 | } |
| 1603 | |
| 1604 | void InitCond(clockid_t clock=CLOCK_REALTIME) { |
| 1605 | pthread_condattr_t attr; |
| 1606 | ASSERT_EQ(0, pthread_condattr_init(&attr)); |
| 1607 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, clock)); |
| 1608 | ASSERT_EQ(0, pthread_cond_init(&cond, &attr)); |
| 1609 | ASSERT_EQ(0, pthread_condattr_destroy(&attr)); |
| 1610 | } |
| 1611 | |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1612 | void StartWaitingThread( |
| 1613 | std::function<int(pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function) { |
| Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1614 | progress = INITIALIZED; |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1615 | this->wait_function = wait_function; |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1616 | ASSERT_EQ(0, pthread_create(&thread, nullptr, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), |
| 1617 | this)); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1618 | while (progress != WAITING) { |
| Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1619 | usleep(5000); |
| 1620 | } |
| 1621 | usleep(5000); |
| 1622 | } |
| 1623 | |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1624 | void RunTimedTest( |
| 1625 | clockid_t clock, |
| 1626 | std::function<int(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* timeout)> |
| 1627 | wait_function) { |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1628 | ASSERT_EQ(0, clock_gettime(clock, &ts)); |
| 1629 | ts.tv_sec += 1; |
| 1630 | |
| Peter Collingbourne | c5b8184 | 2021-12-02 12:38:46 -0800 | [diff] [blame] | 1631 | StartWaitingThread([&wait_function, this](pthread_cond_t* cond, pthread_mutex_t* mutex) { |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1632 | return wait_function(cond, mutex, &ts); |
| 1633 | }); |
| 1634 | |
| 1635 | progress = SIGNALED; |
| 1636 | ASSERT_EQ(0, pthread_cond_signal(&cond)); |
| 1637 | } |
| 1638 | |
| 1639 | void RunTimedTest(clockid_t clock, std::function<int(pthread_cond_t* cond, pthread_mutex_t* mutex, |
| 1640 | clockid_t clock, const timespec* timeout)> |
| 1641 | wait_function) { |
| 1642 | RunTimedTest(clock, [clock, &wait_function](pthread_cond_t* cond, pthread_mutex_t* mutex, |
| 1643 | const timespec* timeout) { |
| 1644 | return wait_function(cond, mutex, clock, timeout); |
| 1645 | }); |
| 1646 | } |
| 1647 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1648 | void TearDown() override { |
| 1649 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 1650 | ASSERT_EQ(FINISHED, progress); |
| 1651 | ASSERT_EQ(0, pthread_cond_destroy(&cond)); |
| 1652 | ASSERT_EQ(0, pthread_mutex_destroy(&mutex)); |
| 1653 | } |
| 1654 | |
| Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1655 | private: |
| 1656 | static void WaitThreadFn(pthread_CondWakeupTest* test) { |
| 1657 | ASSERT_EQ(0, pthread_mutex_lock(&test->mutex)); |
| 1658 | test->progress = WAITING; |
| 1659 | while (test->progress == WAITING) { |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1660 | ASSERT_EQ(0, test->wait_function(&test->cond, &test->mutex)); |
| Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1661 | } |
| 1662 | ASSERT_EQ(SIGNALED, test->progress); |
| 1663 | test->progress = FINISHED; |
| 1664 | ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex)); |
| 1665 | } |
| 1666 | }; |
| 1667 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1668 | TEST_F(pthread_CondWakeupTest, signal_wait) { |
| 1669 | InitCond(); |
| 1670 | StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) { |
| 1671 | return pthread_cond_wait(cond, mutex); |
| 1672 | }); |
| Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1673 | progress = SIGNALED; |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1674 | ASSERT_EQ(0, pthread_cond_signal(&cond)); |
| Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1675 | } |
| 1676 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1677 | TEST_F(pthread_CondWakeupTest, broadcast_wait) { |
| 1678 | InitCond(); |
| 1679 | StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) { |
| 1680 | return pthread_cond_wait(cond, mutex); |
| 1681 | }); |
| Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1682 | progress = SIGNALED; |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1683 | ASSERT_EQ(0, pthread_cond_broadcast(&cond)); |
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1684 | } |
| Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1685 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1686 | TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_REALTIME) { |
| 1687 | InitCond(CLOCK_REALTIME); |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1688 | RunTimedTest(CLOCK_REALTIME, pthread_cond_timedwait); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1689 | } |
| Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1690 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1691 | TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC) { |
| 1692 | InitCond(CLOCK_MONOTONIC); |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1693 | RunTimedTest(CLOCK_MONOTONIC, pthread_cond_timedwait); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1694 | } |
| Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1695 | |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1696 | TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC_np) { |
| 1697 | #if defined(__BIONIC__) |
| 1698 | InitCond(CLOCK_REALTIME); |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1699 | RunTimedTest(CLOCK_MONOTONIC, pthread_cond_timedwait_monotonic_np); |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1700 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1701 | GTEST_SKIP() << "pthread_cond_timedwait_monotonic_np not available"; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1702 | #endif // __BIONIC__ |
| 1703 | } |
| 1704 | |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1705 | TEST_F(pthread_CondWakeupTest, signal_clockwait_monotonic_monotonic) { |
| 1706 | #if defined(__BIONIC__) |
| 1707 | InitCond(CLOCK_MONOTONIC); |
| 1708 | RunTimedTest(CLOCK_MONOTONIC, pthread_cond_clockwait); |
| 1709 | #else // __BIONIC__ |
| 1710 | GTEST_SKIP() << "pthread_cond_clockwait not available"; |
| 1711 | #endif // __BIONIC__ |
| 1712 | } |
| 1713 | |
| 1714 | TEST_F(pthread_CondWakeupTest, signal_clockwait_monotonic_realtime) { |
| 1715 | #if defined(__BIONIC__) |
| 1716 | InitCond(CLOCK_MONOTONIC); |
| 1717 | RunTimedTest(CLOCK_REALTIME, pthread_cond_clockwait); |
| 1718 | #else // __BIONIC__ |
| 1719 | GTEST_SKIP() << "pthread_cond_clockwait not available"; |
| 1720 | #endif // __BIONIC__ |
| 1721 | } |
| 1722 | |
| 1723 | TEST_F(pthread_CondWakeupTest, signal_clockwait_realtime_monotonic) { |
| 1724 | #if defined(__BIONIC__) |
| 1725 | InitCond(CLOCK_REALTIME); |
| 1726 | RunTimedTest(CLOCK_MONOTONIC, pthread_cond_clockwait); |
| 1727 | #else // __BIONIC__ |
| 1728 | GTEST_SKIP() << "pthread_cond_clockwait not available"; |
| 1729 | #endif // __BIONIC__ |
| 1730 | } |
| 1731 | |
| 1732 | TEST_F(pthread_CondWakeupTest, signal_clockwait_realtime_realtime) { |
| 1733 | #if defined(__BIONIC__) |
| 1734 | InitCond(CLOCK_REALTIME); |
| 1735 | RunTimedTest(CLOCK_REALTIME, pthread_cond_clockwait); |
| 1736 | #else // __BIONIC__ |
| 1737 | GTEST_SKIP() << "pthread_cond_clockwait not available"; |
| 1738 | #endif // __BIONIC__ |
| 1739 | } |
| 1740 | |
| Tom Cherry | 800c1a9 | 2019-07-17 10:45:18 -0700 | [diff] [blame] | 1741 | static void pthread_cond_timedwait_timeout_helper(bool init_monotonic, clockid_t clock, |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1742 | int (*wait_function)(pthread_cond_t* __cond, |
| 1743 | pthread_mutex_t* __mutex, |
| 1744 | const timespec* __timeout)) { |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1745 | pthread_mutex_t mutex; |
| 1746 | ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr)); |
| 1747 | pthread_cond_t cond; |
| Tom Cherry | 800c1a9 | 2019-07-17 10:45:18 -0700 | [diff] [blame] | 1748 | |
| 1749 | if (init_monotonic) { |
| 1750 | pthread_condattr_t attr; |
| 1751 | pthread_condattr_init(&attr); |
| 1752 | |
| 1753 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 1754 | clockid_t clock; |
| 1755 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1756 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 1757 | |
| 1758 | ASSERT_EQ(0, pthread_cond_init(&cond, &attr)); |
| 1759 | } else { |
| 1760 | ASSERT_EQ(0, pthread_cond_init(&cond, nullptr)); |
| 1761 | } |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1762 | ASSERT_EQ(0, pthread_mutex_lock(&mutex)); |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1763 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1764 | timespec ts; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1765 | ASSERT_EQ(0, clock_gettime(clock, &ts)); |
| 1766 | ASSERT_EQ(ETIMEDOUT, wait_function(&cond, &mutex, &ts)); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1767 | ts.tv_nsec = -1; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1768 | ASSERT_EQ(EINVAL, wait_function(&cond, &mutex, &ts)); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1769 | ts.tv_nsec = NS_PER_S; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1770 | ASSERT_EQ(EINVAL, wait_function(&cond, &mutex, &ts)); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1771 | ts.tv_nsec = NS_PER_S - 1; |
| 1772 | ts.tv_sec = -1; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1773 | ASSERT_EQ(ETIMEDOUT, wait_function(&cond, &mutex, &ts)); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1774 | ASSERT_EQ(0, pthread_mutex_unlock(&mutex)); |
| Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1775 | } |
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1776 | |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1777 | TEST(pthread, pthread_cond_timedwait_timeout) { |
| Tom Cherry | 800c1a9 | 2019-07-17 10:45:18 -0700 | [diff] [blame] | 1778 | pthread_cond_timedwait_timeout_helper(false, CLOCK_REALTIME, pthread_cond_timedwait); |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | TEST(pthread, pthread_cond_timedwait_monotonic_np_timeout) { |
| 1782 | #if defined(__BIONIC__) |
| Tom Cherry | 800c1a9 | 2019-07-17 10:45:18 -0700 | [diff] [blame] | 1783 | pthread_cond_timedwait_timeout_helper(false, CLOCK_MONOTONIC, pthread_cond_timedwait_monotonic_np); |
| 1784 | pthread_cond_timedwait_timeout_helper(true, CLOCK_MONOTONIC, pthread_cond_timedwait_monotonic_np); |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1785 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1786 | GTEST_SKIP() << "pthread_cond_timedwait_monotonic_np not available"; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1787 | #endif // __BIONIC__ |
| 1788 | } |
| 1789 | |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1790 | TEST(pthread, pthread_cond_clockwait_timeout) { |
| 1791 | #if defined(__BIONIC__) |
| 1792 | pthread_cond_timedwait_timeout_helper( |
| Tom Cherry | 800c1a9 | 2019-07-17 10:45:18 -0700 | [diff] [blame] | 1793 | false, CLOCK_MONOTONIC, |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1794 | [](pthread_cond_t* __cond, pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 1795 | return pthread_cond_clockwait(__cond, __mutex, CLOCK_MONOTONIC, __timeout); |
| 1796 | }); |
| 1797 | pthread_cond_timedwait_timeout_helper( |
| Tom Cherry | 800c1a9 | 2019-07-17 10:45:18 -0700 | [diff] [blame] | 1798 | true, CLOCK_MONOTONIC, |
| 1799 | [](pthread_cond_t* __cond, pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 1800 | return pthread_cond_clockwait(__cond, __mutex, CLOCK_MONOTONIC, __timeout); |
| 1801 | }); |
| 1802 | pthread_cond_timedwait_timeout_helper( |
| 1803 | false, CLOCK_REALTIME, |
| 1804 | [](pthread_cond_t* __cond, pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 1805 | return pthread_cond_clockwait(__cond, __mutex, CLOCK_REALTIME, __timeout); |
| 1806 | }); |
| 1807 | pthread_cond_timedwait_timeout_helper( |
| 1808 | true, CLOCK_REALTIME, |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1809 | [](pthread_cond_t* __cond, pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 1810 | return pthread_cond_clockwait(__cond, __mutex, CLOCK_REALTIME, __timeout); |
| 1811 | }); |
| 1812 | #else // __BIONIC__ |
| 1813 | GTEST_SKIP() << "pthread_cond_clockwait not available"; |
| 1814 | #endif // __BIONIC__ |
| 1815 | } |
| 1816 | |
| 1817 | TEST(pthread, pthread_cond_clockwait_invalid) { |
| 1818 | #if defined(__BIONIC__) |
| 1819 | pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
| 1820 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 1821 | timespec ts; |
| 1822 | EXPECT_EQ(EINVAL, pthread_cond_clockwait(&cond, &mutex, CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 1823 | |
| 1824 | #else // __BIONIC__ |
| 1825 | GTEST_SKIP() << "pthread_cond_clockwait not available"; |
| 1826 | #endif // __BIONIC__ |
| 1827 | } |
| 1828 | |
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1829 | TEST(pthread, pthread_attr_getstack__main_thread) { |
| 1830 | // This test is only meaningful for the main thread, so make sure we're running on it! |
| 1831 | ASSERT_EQ(getpid(), syscall(__NR_gettid)); |
| 1832 | |
| 1833 | // Get the main thread's attributes. |
| 1834 | pthread_attr_t attributes; |
| 1835 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1836 | |
| 1837 | // Check that we correctly report that the main thread has no guard page. |
| 1838 | size_t guard_size; |
| 1839 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 1840 | ASSERT_EQ(0U, guard_size); // The main thread has no guard page. |
| 1841 | |
| 1842 | // Get the stack base and the stack size (both ways). |
| 1843 | void* stack_base; |
| 1844 | size_t stack_size; |
| 1845 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1846 | size_t stack_size2; |
| 1847 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1848 | |
| 1849 | // The two methods of asking for the stack size should agree. |
| 1850 | EXPECT_EQ(stack_size, stack_size2); |
| 1851 | |
| Yabin Cui | b0c6f2db | 2015-05-19 15:09:23 -0700 | [diff] [blame] | 1852 | #if defined(__BIONIC__) |
| dimitry | 6dfa5b5 | 2018-01-30 13:24:28 +0100 | [diff] [blame] | 1853 | // Find stack in /proc/self/maps using a pointer to the stack. |
| 1854 | // |
| 1855 | // We do not use "[stack]" label because in native-bridge environment it is not |
| 1856 | // guaranteed to point to the right stack. A native bridge implementation may |
| 1857 | // keep separate stack for the guest code. |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1858 | void* maps_stack_hi = nullptr; |
| Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 1859 | std::vector<map_record> maps; |
| 1860 | ASSERT_TRUE(Maps::parse_maps(&maps)); |
| Evgenii Stepanov | 7cc6706 | 2019-02-05 18:43:34 -0800 | [diff] [blame] | 1861 | uintptr_t stack_address = reinterpret_cast<uintptr_t>(untag_address(&maps_stack_hi)); |
| Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 1862 | for (const auto& map : maps) { |
| dimitry | 6dfa5b5 | 2018-01-30 13:24:28 +0100 | [diff] [blame] | 1863 | if (map.addr_start <= stack_address && map.addr_end > stack_address){ |
| Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 1864 | maps_stack_hi = reinterpret_cast<void*>(map.addr_end); |
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1865 | break; |
| 1866 | } |
| 1867 | } |
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1868 | |
| dimitry | 6dfa5b5 | 2018-01-30 13:24:28 +0100 | [diff] [blame] | 1869 | // The high address of the /proc/self/maps stack region should equal stack_base + stack_size. |
| Yabin Cui | b0c6f2db | 2015-05-19 15:09:23 -0700 | [diff] [blame] | 1870 | // Remember that the stack grows down (and is mapped in on demand), so the low address of the |
| 1871 | // region isn't very interesting. |
| 1872 | EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size); |
| 1873 | |
| Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1874 | // The stack size should correspond to RLIMIT_STACK. |
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1875 | rlimit rl; |
| Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1876 | ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl)); |
| Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1877 | uint64_t original_rlim_cur = rl.rlim_cur; |
| Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1878 | if (rl.rlim_cur == RLIM_INFINITY) { |
| 1879 | rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB. |
| 1880 | } |
| Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1881 | EXPECT_EQ(rl.rlim_cur, stack_size); |
| 1882 | |
| Tom Cherry | b8ab618 | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 1883 | auto guard = android::base::make_scope_guard([&rl, original_rlim_cur]() { |
| Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1884 | rl.rlim_cur = original_rlim_cur; |
| 1885 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1886 | }); |
| 1887 | |
| Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1888 | // |
| 1889 | // What if RLIMIT_STACK is smaller than the stack's current extent? |
| 1890 | // |
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1891 | rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already. |
| 1892 | rl.rlim_max = RLIM_INFINITY; |
| 1893 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1894 | |
| 1895 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1896 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1897 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1898 | |
| 1899 | EXPECT_EQ(stack_size, stack_size2); |
| 1900 | ASSERT_EQ(1024U, stack_size); |
| 1901 | |
| 1902 | // |
| Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1903 | // What if RLIMIT_STACK isn't a whole number of pages? |
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1904 | // |
| 1905 | rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages. |
| 1906 | rl.rlim_max = RLIM_INFINITY; |
| 1907 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1908 | |
| 1909 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1910 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1911 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1912 | |
| 1913 | EXPECT_EQ(stack_size, stack_size2); |
| 1914 | ASSERT_EQ(6666U, stack_size); |
| Yabin Cui | b0c6f2db | 2015-05-19 15:09:23 -0700 | [diff] [blame] | 1915 | #endif |
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1916 | } |
| Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1917 | |
| Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1918 | struct GetStackSignalHandlerArg { |
| 1919 | volatile bool done; |
| Chih-Hung Hsieh | 9af13d2 | 2016-06-02 14:40:09 -0700 | [diff] [blame] | 1920 | void* signal_stack_base; |
| 1921 | size_t signal_stack_size; |
| Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1922 | void* main_stack_base; |
| 1923 | size_t main_stack_size; |
| 1924 | }; |
| 1925 | |
| 1926 | static GetStackSignalHandlerArg getstack_signal_handler_arg; |
| 1927 | |
| 1928 | static void getstack_signal_handler(int sig) { |
| 1929 | ASSERT_EQ(SIGUSR1, sig); |
| 1930 | // Use sleep() to make current thread be switched out by the kernel to provoke the error. |
| 1931 | sleep(1); |
| 1932 | pthread_attr_t attr; |
| 1933 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr)); |
| 1934 | void* stack_base; |
| 1935 | size_t stack_size; |
| 1936 | ASSERT_EQ(0, pthread_attr_getstack(&attr, &stack_base, &stack_size)); |
| Chih-Hung Hsieh | 9af13d2 | 2016-06-02 14:40:09 -0700 | [diff] [blame] | 1937 | |
| 1938 | // Verify if the stack used by the signal handler is the alternate stack just registered. |
| 1939 | ASSERT_LE(getstack_signal_handler_arg.signal_stack_base, &attr); |
| Evgenii Stepanov | 7cc6706 | 2019-02-05 18:43:34 -0800 | [diff] [blame] | 1940 | ASSERT_LT(static_cast<void*>(untag_address(&attr)), |
| Chih-Hung Hsieh | 9af13d2 | 2016-06-02 14:40:09 -0700 | [diff] [blame] | 1941 | static_cast<char*>(getstack_signal_handler_arg.signal_stack_base) + |
| Evgenii Stepanov | 7cc6706 | 2019-02-05 18:43:34 -0800 | [diff] [blame] | 1942 | getstack_signal_handler_arg.signal_stack_size); |
| Chih-Hung Hsieh | 9af13d2 | 2016-06-02 14:40:09 -0700 | [diff] [blame] | 1943 | |
| 1944 | // Verify if the main thread's stack got in the signal handler is correct. |
| 1945 | ASSERT_EQ(getstack_signal_handler_arg.main_stack_base, stack_base); |
| 1946 | ASSERT_LE(getstack_signal_handler_arg.main_stack_size, stack_size); |
| 1947 | |
| Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1948 | getstack_signal_handler_arg.done = true; |
| 1949 | } |
| 1950 | |
| 1951 | // The previous code obtained the main thread's stack by reading the entry in |
| 1952 | // /proc/self/task/<pid>/maps that was labeled [stack]. Unfortunately, on x86/x86_64, the kernel |
| 1953 | // relies on sp0 in task state segment(tss) to label the stack map with [stack]. If the kernel |
| 1954 | // switches a process while the main thread is in an alternate stack, then the kernel will label |
| 1955 | // the wrong map with [stack]. This test verifies that when the above situation happens, the main |
| 1956 | // thread's stack is found correctly. |
| 1957 | TEST(pthread, pthread_attr_getstack_in_signal_handler) { |
| Yabin Cui | 61e4d46 | 2016-03-07 17:44:58 -0800 | [diff] [blame] | 1958 | // This test is only meaningful for the main thread, so make sure we're running on it! |
| 1959 | ASSERT_EQ(getpid(), syscall(__NR_gettid)); |
| 1960 | |
| Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1961 | const size_t sig_stack_size = 16 * 1024; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1962 | void* sig_stack = mmap(nullptr, sig_stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, |
| Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1963 | -1, 0); |
| 1964 | ASSERT_NE(MAP_FAILED, sig_stack); |
| 1965 | stack_t ss; |
| 1966 | ss.ss_sp = sig_stack; |
| 1967 | ss.ss_size = sig_stack_size; |
| 1968 | ss.ss_flags = 0; |
| 1969 | stack_t oss; |
| 1970 | ASSERT_EQ(0, sigaltstack(&ss, &oss)); |
| 1971 | |
| Yabin Cui | 61e4d46 | 2016-03-07 17:44:58 -0800 | [diff] [blame] | 1972 | pthread_attr_t attr; |
| 1973 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr)); |
| 1974 | void* main_stack_base; |
| 1975 | size_t main_stack_size; |
| 1976 | ASSERT_EQ(0, pthread_attr_getstack(&attr, &main_stack_base, &main_stack_size)); |
| 1977 | |
| Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1978 | ScopedSignalHandler handler(SIGUSR1, getstack_signal_handler, SA_ONSTACK); |
| 1979 | getstack_signal_handler_arg.done = false; |
| Chih-Hung Hsieh | 9af13d2 | 2016-06-02 14:40:09 -0700 | [diff] [blame] | 1980 | getstack_signal_handler_arg.signal_stack_base = sig_stack; |
| 1981 | getstack_signal_handler_arg.signal_stack_size = sig_stack_size; |
| 1982 | getstack_signal_handler_arg.main_stack_base = main_stack_base; |
| 1983 | getstack_signal_handler_arg.main_stack_size = main_stack_size; |
| Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1984 | kill(getpid(), SIGUSR1); |
| 1985 | ASSERT_EQ(true, getstack_signal_handler_arg.done); |
| 1986 | |
| Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1987 | ASSERT_EQ(0, sigaltstack(&oss, nullptr)); |
| 1988 | ASSERT_EQ(0, munmap(sig_stack, sig_stack_size)); |
| 1989 | } |
| 1990 | |
| Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 1991 | static void pthread_attr_getstack_18908062_helper(void*) { |
| 1992 | char local_variable; |
| 1993 | pthread_attr_t attributes; |
| 1994 | pthread_getattr_np(pthread_self(), &attributes); |
| 1995 | void* stack_base; |
| 1996 | size_t stack_size; |
| 1997 | pthread_attr_getstack(&attributes, &stack_base, &stack_size); |
| 1998 | |
| 1999 | // Test whether &local_variable is in [stack_base, stack_base + stack_size). |
| 2000 | ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable); |
| Evgenii Stepanov | 7cc6706 | 2019-02-05 18:43:34 -0800 | [diff] [blame] | 2001 | ASSERT_LT(untag_address(&local_variable), reinterpret_cast<char*>(stack_base) + stack_size); |
| Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | // Check whether something on stack is in the range of |
| 2005 | // [stack_base, stack_base + stack_size). see b/18908062. |
| 2006 | TEST(pthread, pthread_attr_getstack_18908062) { |
| 2007 | pthread_t t; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2008 | ASSERT_EQ(0, pthread_create(&t, nullptr, |
| Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 2009 | reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper), |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2010 | nullptr)); |
| 2011 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 2012 | } |
| 2013 | |
| Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2014 | #if defined(__BIONIC__) |
| Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 2015 | static pthread_mutex_t pthread_gettid_np_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 2016 | |
| Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2017 | static void* pthread_gettid_np_helper(void* arg) { |
| 2018 | *reinterpret_cast<pid_t*>(arg) = gettid(); |
| Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 2019 | |
| 2020 | // Wait for our parent to call pthread_gettid_np on us before exiting. |
| 2021 | pthread_mutex_lock(&pthread_gettid_np_mutex); |
| 2022 | pthread_mutex_unlock(&pthread_gettid_np_mutex); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2023 | return nullptr; |
| Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2024 | } |
| 2025 | #endif |
| 2026 | |
| 2027 | TEST(pthread, pthread_gettid_np) { |
| 2028 | #if defined(__BIONIC__) |
| 2029 | ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self())); |
| 2030 | |
| Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 2031 | // Ensure the other thread doesn't exit until after we've called |
| 2032 | // pthread_gettid_np on it. |
| 2033 | pthread_mutex_lock(&pthread_gettid_np_mutex); |
| 2034 | |
| Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2035 | pid_t t_gettid_result; |
| 2036 | pthread_t t; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2037 | pthread_create(&t, nullptr, pthread_gettid_np_helper, &t_gettid_result); |
| Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2038 | |
| 2039 | pid_t t_pthread_gettid_np_result = pthread_gettid_np(t); |
| 2040 | |
| Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 2041 | // Release the other thread and wait for it to exit. |
| 2042 | pthread_mutex_unlock(&pthread_gettid_np_mutex); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2043 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2044 | |
| 2045 | ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result); |
| 2046 | #else |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2047 | GTEST_SKIP() << "pthread_gettid_np not available"; |
| Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2048 | #endif |
| 2049 | } |
| Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2050 | |
| 2051 | static size_t cleanup_counter = 0; |
| 2052 | |
| Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2053 | static void AbortCleanupRoutine(void*) { |
| Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2054 | abort(); |
| 2055 | } |
| 2056 | |
| Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2057 | static void CountCleanupRoutine(void*) { |
| Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2058 | ++cleanup_counter; |
| 2059 | } |
| 2060 | |
| Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2061 | static void PthreadCleanupTester() { |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2062 | pthread_cleanup_push(CountCleanupRoutine, nullptr); |
| 2063 | pthread_cleanup_push(CountCleanupRoutine, nullptr); |
| 2064 | pthread_cleanup_push(AbortCleanupRoutine, nullptr); |
| Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2065 | |
| 2066 | pthread_cleanup_pop(0); // Pop the abort without executing it. |
| 2067 | pthread_cleanup_pop(1); // Pop one count while executing it. |
| 2068 | ASSERT_EQ(1U, cleanup_counter); |
| 2069 | // Exit while the other count is still on the cleanup stack. |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2070 | pthread_exit(nullptr); |
| Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2071 | |
| 2072 | // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced. |
| 2073 | pthread_cleanup_pop(0); |
| 2074 | } |
| 2075 | |
| Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2076 | static void* PthreadCleanupStartRoutine(void*) { |
| Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2077 | PthreadCleanupTester(); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2078 | return nullptr; |
| Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) { |
| 2082 | pthread_t t; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2083 | ASSERT_EQ(0, pthread_create(&t, nullptr, PthreadCleanupStartRoutine, nullptr)); |
| 2084 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2085 | ASSERT_EQ(2U, cleanup_counter); |
| 2086 | } |
| Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2087 | |
| 2088 | TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) { |
| 2089 | ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT); |
| 2090 | } |
| 2091 | |
| 2092 | TEST(pthread, pthread_mutexattr_gettype) { |
| 2093 | pthread_mutexattr_t attr; |
| 2094 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 2095 | |
| 2096 | int attr_type; |
| 2097 | |
| 2098 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)); |
| 2099 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 2100 | ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type); |
| 2101 | |
| 2102 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)); |
| 2103 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 2104 | ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type); |
| 2105 | |
| 2106 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)); |
| 2107 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 2108 | ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type); |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2109 | |
| 2110 | ASSERT_EQ(0, pthread_mutexattr_destroy(&attr)); |
| 2111 | } |
| 2112 | |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2113 | TEST(pthread, pthread_mutexattr_protocol) { |
| 2114 | pthread_mutexattr_t attr; |
| 2115 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 2116 | |
| 2117 | int protocol; |
| 2118 | ASSERT_EQ(0, pthread_mutexattr_getprotocol(&attr, &protocol)); |
| 2119 | ASSERT_EQ(PTHREAD_PRIO_NONE, protocol); |
| 2120 | for (size_t repeat = 0; repeat < 2; ++repeat) { |
| 2121 | for (int set_protocol : {PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT}) { |
| 2122 | ASSERT_EQ(0, pthread_mutexattr_setprotocol(&attr, set_protocol)); |
| 2123 | ASSERT_EQ(0, pthread_mutexattr_getprotocol(&attr, &protocol)); |
| 2124 | ASSERT_EQ(protocol, set_protocol); |
| 2125 | } |
| 2126 | } |
| 2127 | } |
| 2128 | |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2129 | struct PthreadMutex { |
| 2130 | pthread_mutex_t lock; |
| 2131 | |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2132 | explicit PthreadMutex(int mutex_type, int protocol = PTHREAD_PRIO_NONE) { |
| 2133 | init(mutex_type, protocol); |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | ~PthreadMutex() { |
| 2137 | destroy(); |
| 2138 | } |
| 2139 | |
| 2140 | private: |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2141 | void init(int mutex_type, int protocol) { |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2142 | pthread_mutexattr_t attr; |
| 2143 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 2144 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, mutex_type)); |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2145 | ASSERT_EQ(0, pthread_mutexattr_setprotocol(&attr, protocol)); |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2146 | ASSERT_EQ(0, pthread_mutex_init(&lock, &attr)); |
| 2147 | ASSERT_EQ(0, pthread_mutexattr_destroy(&attr)); |
| 2148 | } |
| 2149 | |
| 2150 | void destroy() { |
| 2151 | ASSERT_EQ(0, pthread_mutex_destroy(&lock)); |
| 2152 | } |
| 2153 | |
| 2154 | DISALLOW_COPY_AND_ASSIGN(PthreadMutex); |
| 2155 | }; |
| Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2156 | |
| Ryan Prichard | 4b6c0f5 | 2019-04-18 22:47:04 -0700 | [diff] [blame] | 2157 | static int UnlockFromAnotherThread(pthread_mutex_t* mutex) { |
| 2158 | pthread_t thread; |
| 2159 | pthread_create(&thread, nullptr, [](void* mutex_voidp) -> void* { |
| 2160 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(mutex_voidp); |
| 2161 | intptr_t result = pthread_mutex_unlock(mutex); |
| 2162 | return reinterpret_cast<void*>(result); |
| 2163 | }, mutex); |
| 2164 | void* result; |
| 2165 | EXPECT_EQ(0, pthread_join(thread, &result)); |
| 2166 | return reinterpret_cast<intptr_t>(result); |
| 2167 | }; |
| 2168 | |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2169 | static void TestPthreadMutexLockNormal(int protocol) { |
| 2170 | PthreadMutex m(PTHREAD_MUTEX_NORMAL, protocol); |
| Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2171 | |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2172 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| Ryan Prichard | 4b6c0f5 | 2019-04-18 22:47:04 -0700 | [diff] [blame] | 2173 | if (protocol == PTHREAD_PRIO_INHERIT) { |
| 2174 | ASSERT_EQ(EPERM, UnlockFromAnotherThread(&m.lock)); |
| 2175 | } |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2176 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| Elliott Hughes | d31d4c1 | 2015-12-14 17:35:10 -0800 | [diff] [blame] | 2177 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
| 2178 | ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock)); |
| 2179 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2180 | } |
| 2181 | |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2182 | static void TestPthreadMutexLockErrorCheck(int protocol) { |
| 2183 | PthreadMutex m(PTHREAD_MUTEX_ERRORCHECK, protocol); |
| Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2184 | |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2185 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| Ryan Prichard | 4b6c0f5 | 2019-04-18 22:47:04 -0700 | [diff] [blame] | 2186 | ASSERT_EQ(EPERM, UnlockFromAnotherThread(&m.lock)); |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2187 | ASSERT_EQ(EDEADLK, pthread_mutex_lock(&m.lock)); |
| 2188 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2189 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2190 | if (protocol == PTHREAD_PRIO_NONE) { |
| 2191 | ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock)); |
| 2192 | } else { |
| 2193 | ASSERT_EQ(EDEADLK, pthread_mutex_trylock(&m.lock)); |
| 2194 | } |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2195 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2196 | ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock)); |
| Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2197 | } |
| 2198 | |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2199 | static void TestPthreadMutexLockRecursive(int protocol) { |
| 2200 | PthreadMutex m(PTHREAD_MUTEX_RECURSIVE, protocol); |
| Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2201 | |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2202 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| Ryan Prichard | 4b6c0f5 | 2019-04-18 22:47:04 -0700 | [diff] [blame] | 2203 | ASSERT_EQ(EPERM, UnlockFromAnotherThread(&m.lock)); |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2204 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| Ryan Prichard | 4b6c0f5 | 2019-04-18 22:47:04 -0700 | [diff] [blame] | 2205 | ASSERT_EQ(EPERM, UnlockFromAnotherThread(&m.lock)); |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2206 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2207 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2208 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
| Elliott Hughes | d31d4c1 | 2015-12-14 17:35:10 -0800 | [diff] [blame] | 2209 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
| 2210 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2211 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2212 | ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock)); |
| 2213 | } |
| 2214 | |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2215 | TEST(pthread, pthread_mutex_lock_NORMAL) { |
| 2216 | TestPthreadMutexLockNormal(PTHREAD_PRIO_NONE); |
| 2217 | } |
| 2218 | |
| 2219 | TEST(pthread, pthread_mutex_lock_ERRORCHECK) { |
| 2220 | TestPthreadMutexLockErrorCheck(PTHREAD_PRIO_NONE); |
| 2221 | } |
| 2222 | |
| 2223 | TEST(pthread, pthread_mutex_lock_RECURSIVE) { |
| 2224 | TestPthreadMutexLockRecursive(PTHREAD_PRIO_NONE); |
| 2225 | } |
| 2226 | |
| 2227 | TEST(pthread, pthread_mutex_lock_pi) { |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2228 | TestPthreadMutexLockNormal(PTHREAD_PRIO_INHERIT); |
| 2229 | TestPthreadMutexLockErrorCheck(PTHREAD_PRIO_INHERIT); |
| 2230 | TestPthreadMutexLockRecursive(PTHREAD_PRIO_INHERIT); |
| 2231 | } |
| 2232 | |
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 2233 | TEST(pthread, pthread_mutex_pi_count_limit) { |
| 2234 | #if defined(__BIONIC__) && !defined(__LP64__) |
| 2235 | // Bionic only supports 65536 pi mutexes in 32-bit programs. |
| 2236 | pthread_mutexattr_t attr; |
| 2237 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 2238 | ASSERT_EQ(0, pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT)); |
| 2239 | std::vector<pthread_mutex_t> mutexes(65536); |
| 2240 | // Test if we can use 65536 pi mutexes at the same time. |
| 2241 | // Run 2 times to check if freed pi mutexes can be recycled. |
| 2242 | for (int repeat = 0; repeat < 2; ++repeat) { |
| 2243 | for (auto& m : mutexes) { |
| 2244 | ASSERT_EQ(0, pthread_mutex_init(&m, &attr)); |
| 2245 | } |
| 2246 | pthread_mutex_t m; |
| 2247 | ASSERT_EQ(ENOMEM, pthread_mutex_init(&m, &attr)); |
| 2248 | for (auto& m : mutexes) { |
| 2249 | ASSERT_EQ(0, pthread_mutex_lock(&m)); |
| 2250 | } |
| 2251 | for (auto& m : mutexes) { |
| 2252 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 2253 | } |
| 2254 | for (auto& m : mutexes) { |
| 2255 | ASSERT_EQ(0, pthread_mutex_destroy(&m)); |
| 2256 | } |
| 2257 | } |
| 2258 | ASSERT_EQ(0, pthread_mutexattr_destroy(&attr)); |
| 2259 | #else |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2260 | GTEST_SKIP() << "pi mutex count not limited to 64Ki"; |
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 2261 | #endif |
| 2262 | } |
| 2263 | |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2264 | TEST(pthread, pthread_mutex_init_same_as_static_initializers) { |
| 2265 | pthread_mutex_t lock_normal = PTHREAD_MUTEX_INITIALIZER; |
| 2266 | PthreadMutex m1(PTHREAD_MUTEX_NORMAL); |
| 2267 | ASSERT_EQ(0, memcmp(&lock_normal, &m1.lock, sizeof(pthread_mutex_t))); |
| 2268 | pthread_mutex_destroy(&lock_normal); |
| 2269 | |
| Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 2270 | #if !defined(ANDROID_HOST_MUSL) |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 2271 | // musl doesn't support PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP or |
| 2272 | // PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP. |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2273 | pthread_mutex_t lock_errorcheck = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; |
| 2274 | PthreadMutex m2(PTHREAD_MUTEX_ERRORCHECK); |
| 2275 | ASSERT_EQ(0, memcmp(&lock_errorcheck, &m2.lock, sizeof(pthread_mutex_t))); |
| 2276 | pthread_mutex_destroy(&lock_errorcheck); |
| 2277 | |
| 2278 | pthread_mutex_t lock_recursive = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
| 2279 | PthreadMutex m3(PTHREAD_MUTEX_RECURSIVE); |
| 2280 | ASSERT_EQ(0, memcmp(&lock_recursive, &m3.lock, sizeof(pthread_mutex_t))); |
| 2281 | ASSERT_EQ(0, pthread_mutex_destroy(&lock_recursive)); |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 2282 | #endif |
| Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2283 | } |
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 2284 | |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2285 | class MutexWakeupHelper { |
| 2286 | private: |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2287 | PthreadMutex m; |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2288 | enum Progress { |
| 2289 | LOCK_INITIALIZED, |
| 2290 | LOCK_WAITING, |
| 2291 | LOCK_RELEASED, |
| 2292 | LOCK_ACCESSED |
| 2293 | }; |
| 2294 | std::atomic<Progress> progress; |
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 2295 | std::atomic<pid_t> tid; |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2296 | |
| 2297 | static void thread_fn(MutexWakeupHelper* helper) { |
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 2298 | helper->tid = gettid(); |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2299 | ASSERT_EQ(LOCK_INITIALIZED, helper->progress); |
| 2300 | helper->progress = LOCK_WAITING; |
| 2301 | |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2302 | ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock)); |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2303 | ASSERT_EQ(LOCK_RELEASED, helper->progress); |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2304 | ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock)); |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2305 | |
| 2306 | helper->progress = LOCK_ACCESSED; |
| 2307 | } |
| 2308 | |
| 2309 | public: |
| Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 2310 | explicit MutexWakeupHelper(int mutex_type) : m(mutex_type) { |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | void test() { |
| 2314 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2315 | progress = LOCK_INITIALIZED; |
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 2316 | tid = 0; |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2317 | |
| 2318 | pthread_t thread; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2319 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2320 | reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this)); |
| 2321 | |
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 2322 | WaitUntilThreadSleep(tid); |
| 2323 | ASSERT_EQ(LOCK_WAITING, progress); |
| 2324 | |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2325 | progress = LOCK_RELEASED; |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2326 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2327 | |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2328 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2329 | ASSERT_EQ(LOCK_ACCESSED, progress); |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2330 | } |
| 2331 | }; |
| 2332 | |
| 2333 | TEST(pthread, pthread_mutex_NORMAL_wakeup) { |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2334 | MutexWakeupHelper helper(PTHREAD_MUTEX_NORMAL); |
| 2335 | helper.test(); |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | TEST(pthread, pthread_mutex_ERRORCHECK_wakeup) { |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2339 | MutexWakeupHelper helper(PTHREAD_MUTEX_ERRORCHECK); |
| 2340 | helper.test(); |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | TEST(pthread, pthread_mutex_RECURSIVE_wakeup) { |
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2344 | MutexWakeupHelper helper(PTHREAD_MUTEX_RECURSIVE); |
| 2345 | helper.test(); |
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2346 | } |
| 2347 | |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2348 | static int GetThreadPriority(pid_t tid) { |
| 2349 | // sched_getparam() returns the static priority of a thread, which can't reflect a thread's |
| 2350 | // priority after priority inheritance. So read /proc/<pid>/stat to get the dynamic priority. |
| 2351 | std::string filename = android::base::StringPrintf("/proc/%d/stat", tid); |
| 2352 | std::string content; |
| 2353 | int result = INT_MAX; |
| 2354 | if (!android::base::ReadFileToString(filename, &content)) { |
| 2355 | return result; |
| 2356 | } |
| 2357 | std::vector<std::string> strs = android::base::Split(content, " "); |
| 2358 | if (strs.size() < 18) { |
| 2359 | return result; |
| 2360 | } |
| 2361 | if (!android::base::ParseInt(strs[17], &result)) { |
| 2362 | return INT_MAX; |
| 2363 | } |
| 2364 | return result; |
| 2365 | } |
| 2366 | |
| 2367 | class PIMutexWakeupHelper { |
| 2368 | private: |
| 2369 | PthreadMutex m; |
| 2370 | int protocol; |
| 2371 | enum Progress { |
| 2372 | LOCK_INITIALIZED, |
| 2373 | LOCK_CHILD_READY, |
| 2374 | LOCK_WAITING, |
| 2375 | LOCK_RELEASED, |
| 2376 | }; |
| 2377 | std::atomic<Progress> progress; |
| 2378 | std::atomic<pid_t> main_tid; |
| 2379 | std::atomic<pid_t> child_tid; |
| 2380 | PthreadMutex start_thread_m; |
| 2381 | |
| 2382 | static void thread_fn(PIMutexWakeupHelper* helper) { |
| 2383 | helper->child_tid = gettid(); |
| 2384 | ASSERT_EQ(LOCK_INITIALIZED, helper->progress); |
| 2385 | ASSERT_EQ(0, setpriority(PRIO_PROCESS, gettid(), 1)); |
| 2386 | ASSERT_EQ(21, GetThreadPriority(gettid())); |
| 2387 | ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock)); |
| 2388 | helper->progress = LOCK_CHILD_READY; |
| 2389 | ASSERT_EQ(0, pthread_mutex_lock(&helper->start_thread_m.lock)); |
| 2390 | |
| 2391 | ASSERT_EQ(0, pthread_mutex_unlock(&helper->start_thread_m.lock)); |
| 2392 | WaitUntilThreadSleep(helper->main_tid); |
| 2393 | ASSERT_EQ(LOCK_WAITING, helper->progress); |
| 2394 | |
| 2395 | if (helper->protocol == PTHREAD_PRIO_INHERIT) { |
| 2396 | ASSERT_EQ(20, GetThreadPriority(gettid())); |
| 2397 | } else { |
| 2398 | ASSERT_EQ(21, GetThreadPriority(gettid())); |
| 2399 | } |
| 2400 | helper->progress = LOCK_RELEASED; |
| 2401 | ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock)); |
| 2402 | } |
| 2403 | |
| 2404 | public: |
| 2405 | explicit PIMutexWakeupHelper(int mutex_type, int protocol) |
| 2406 | : m(mutex_type, protocol), protocol(protocol), start_thread_m(PTHREAD_MUTEX_NORMAL) { |
| 2407 | } |
| 2408 | |
| 2409 | void test() { |
| 2410 | ASSERT_EQ(0, pthread_mutex_lock(&start_thread_m.lock)); |
| 2411 | main_tid = gettid(); |
| 2412 | ASSERT_EQ(20, GetThreadPriority(main_tid)); |
| 2413 | progress = LOCK_INITIALIZED; |
| 2414 | child_tid = 0; |
| 2415 | |
| 2416 | pthread_t thread; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2417 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2418 | reinterpret_cast<void* (*)(void*)>(PIMutexWakeupHelper::thread_fn), this)); |
| 2419 | |
| 2420 | WaitUntilThreadSleep(child_tid); |
| 2421 | ASSERT_EQ(LOCK_CHILD_READY, progress); |
| 2422 | ASSERT_EQ(0, pthread_mutex_unlock(&start_thread_m.lock)); |
| 2423 | progress = LOCK_WAITING; |
| 2424 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| 2425 | |
| 2426 | ASSERT_EQ(LOCK_RELEASED, progress); |
| 2427 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2428 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 2429 | } |
| 2430 | }; |
| 2431 | |
| 2432 | TEST(pthread, pthread_mutex_pi_wakeup) { |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2433 | for (int type : {PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK}) { |
| 2434 | for (int protocol : {PTHREAD_PRIO_INHERIT}) { |
| 2435 | PIMutexWakeupHelper helper(type, protocol); |
| 2436 | helper.test(); |
| 2437 | } |
| 2438 | } |
| 2439 | } |
| 2440 | |
| Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 2441 | TEST(pthread, pthread_mutex_owner_tid_limit) { |
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 2442 | #if defined(__BIONIC__) && !defined(__LP64__) |
| Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 2443 | FILE* fp = fopen("/proc/sys/kernel/pid_max", "r"); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2444 | ASSERT_TRUE(fp != nullptr); |
| Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 2445 | long pid_max; |
| 2446 | ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max)); |
| 2447 | fclose(fp); |
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 2448 | // Bionic's pthread_mutex implementation on 32-bit devices uses 16 bits to represent owner tid. |
| Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 2449 | ASSERT_LE(pid_max, 65536); |
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 2450 | #else |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2451 | GTEST_SKIP() << "pthread_mutex supports 32-bit tid"; |
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 2452 | #endif |
| Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 2453 | } |
| Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2454 | |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2455 | static void pthread_mutex_timedlock_helper(clockid_t clock, |
| 2456 | int (*lock_function)(pthread_mutex_t* __mutex, |
| 2457 | const timespec* __timeout)) { |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2458 | pthread_mutex_t m; |
| 2459 | ASSERT_EQ(0, pthread_mutex_init(&m, nullptr)); |
| 2460 | |
| 2461 | // If the mutex is already locked, pthread_mutex_timedlock should time out. |
| 2462 | ASSERT_EQ(0, pthread_mutex_lock(&m)); |
| 2463 | |
| 2464 | timespec ts; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2465 | ASSERT_EQ(0, clock_gettime(clock, &ts)); |
| 2466 | ASSERT_EQ(ETIMEDOUT, lock_function(&m, &ts)); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2467 | ts.tv_nsec = -1; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2468 | ASSERT_EQ(EINVAL, lock_function(&m, &ts)); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2469 | ts.tv_nsec = NS_PER_S; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2470 | ASSERT_EQ(EINVAL, lock_function(&m, &ts)); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2471 | ts.tv_nsec = NS_PER_S - 1; |
| 2472 | ts.tv_sec = -1; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2473 | ASSERT_EQ(ETIMEDOUT, lock_function(&m, &ts)); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2474 | |
| Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 2475 | // Check we wait long enough for the lock before timing out... |
| 2476 | |
| 2477 | // What time is it before we start? |
| Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2478 | ASSERT_EQ(0, clock_gettime(clock, &ts)); |
| Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 2479 | const int64_t start_ns = to_ns(ts); |
| 2480 | // Add a second to get deadline, and wait until we time out. |
| Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2481 | ts.tv_sec += 1; |
| Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2482 | ASSERT_EQ(ETIMEDOUT, lock_function(&m, &ts)); |
| 2483 | |
| Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 2484 | // What time is it now we've timed out? |
| 2485 | timespec ts2; |
| 2486 | clock_gettime(clock, &ts2); |
| 2487 | const int64_t end_ns = to_ns(ts2); |
| 2488 | |
| Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2489 | // The timedlock must have waited at least 1 second before returning. |
| Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 2490 | ASSERT_GE(end_ns - start_ns, NS_PER_S); |
| Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2491 | |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2492 | // If the mutex is unlocked, pthread_mutex_timedlock should succeed. |
| 2493 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2494 | ASSERT_EQ(0, clock_gettime(clock, &ts)); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2495 | ts.tv_sec += 1; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2496 | ASSERT_EQ(0, lock_function(&m, &ts)); |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2497 | |
| 2498 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 2499 | ASSERT_EQ(0, pthread_mutex_destroy(&m)); |
| 2500 | } |
| 2501 | |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2502 | TEST(pthread, pthread_mutex_timedlock) { |
| 2503 | pthread_mutex_timedlock_helper(CLOCK_REALTIME, pthread_mutex_timedlock); |
| 2504 | } |
| 2505 | |
| 2506 | TEST(pthread, pthread_mutex_timedlock_monotonic_np) { |
| 2507 | #if defined(__BIONIC__) |
| 2508 | pthread_mutex_timedlock_helper(CLOCK_MONOTONIC, pthread_mutex_timedlock_monotonic_np); |
| 2509 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2510 | GTEST_SKIP() << "pthread_mutex_timedlock_monotonic_np not available"; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2511 | #endif // __BIONIC__ |
| 2512 | } |
| 2513 | |
| Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 2514 | TEST(pthread, pthread_mutex_clocklock_MONOTONIC) { |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 2515 | #if defined(__BIONIC__) |
| 2516 | pthread_mutex_timedlock_helper( |
| 2517 | CLOCK_MONOTONIC, [](pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 2518 | return pthread_mutex_clocklock(__mutex, CLOCK_MONOTONIC, __timeout); |
| 2519 | }); |
| Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 2520 | #else // __BIONIC__ |
| 2521 | GTEST_SKIP() << "pthread_mutex_clocklock not available"; |
| 2522 | #endif // __BIONIC__ |
| 2523 | } |
| 2524 | |
| 2525 | TEST(pthread, pthread_mutex_clocklock_REALTIME) { |
| 2526 | #if defined(__BIONIC__) |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 2527 | pthread_mutex_timedlock_helper( |
| 2528 | CLOCK_REALTIME, [](pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 2529 | return pthread_mutex_clocklock(__mutex, CLOCK_REALTIME, __timeout); |
| 2530 | }); |
| 2531 | #else // __BIONIC__ |
| 2532 | GTEST_SKIP() << "pthread_mutex_clocklock not available"; |
| 2533 | #endif // __BIONIC__ |
| 2534 | } |
| 2535 | |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2536 | static void pthread_mutex_timedlock_pi_helper(clockid_t clock, |
| 2537 | int (*lock_function)(pthread_mutex_t* __mutex, |
| 2538 | const timespec* __timeout)) { |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2539 | PthreadMutex m(PTHREAD_MUTEX_NORMAL, PTHREAD_PRIO_INHERIT); |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2540 | |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2541 | timespec ts; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2542 | clock_gettime(clock, &ts); |
| Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2543 | const int64_t start_ns = ts.tv_sec * NS_PER_S + ts.tv_nsec; |
| 2544 | |
| 2545 | // add a second to get deadline. |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2546 | ts.tv_sec += 1; |
| Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2547 | |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2548 | ASSERT_EQ(0, lock_function(&m.lock, &ts)); |
| 2549 | |
| 2550 | struct ThreadArgs { |
| 2551 | clockid_t clock; |
| 2552 | int (*lock_function)(pthread_mutex_t* __mutex, const timespec* __timeout); |
| 2553 | PthreadMutex& m; |
| 2554 | }; |
| 2555 | |
| 2556 | ThreadArgs thread_args = { |
| 2557 | .clock = clock, |
| 2558 | .lock_function = lock_function, |
| 2559 | .m = m, |
| 2560 | }; |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2561 | |
| 2562 | auto ThreadFn = [](void* arg) -> void* { |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2563 | auto args = static_cast<ThreadArgs*>(arg); |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2564 | timespec ts; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2565 | clock_gettime(args->clock, &ts); |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2566 | ts.tv_sec += 1; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2567 | intptr_t result = args->lock_function(&args->m.lock, &ts); |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2568 | return reinterpret_cast<void*>(result); |
| 2569 | }; |
| 2570 | |
| 2571 | pthread_t thread; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2572 | ASSERT_EQ(0, pthread_create(&thread, nullptr, ThreadFn, &thread_args)); |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2573 | void* result; |
| 2574 | ASSERT_EQ(0, pthread_join(thread, &result)); |
| 2575 | ASSERT_EQ(ETIMEDOUT, reinterpret_cast<intptr_t>(result)); |
| Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2576 | |
| 2577 | // The timedlock must have waited at least 1 second before returning. |
| 2578 | clock_gettime(clock, &ts); |
| 2579 | const int64_t end_ns = ts.tv_sec * NS_PER_S + ts.tv_nsec; |
| 2580 | ASSERT_GT(end_ns - start_ns, NS_PER_S); |
| 2581 | |
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2582 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2583 | } |
| 2584 | |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2585 | TEST(pthread, pthread_mutex_timedlock_pi) { |
| 2586 | pthread_mutex_timedlock_pi_helper(CLOCK_REALTIME, pthread_mutex_timedlock); |
| 2587 | } |
| 2588 | |
| 2589 | TEST(pthread, pthread_mutex_timedlock_monotonic_np_pi) { |
| 2590 | #if defined(__BIONIC__) |
| 2591 | pthread_mutex_timedlock_pi_helper(CLOCK_MONOTONIC, pthread_mutex_timedlock_monotonic_np); |
| 2592 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2593 | GTEST_SKIP() << "pthread_mutex_timedlock_monotonic_np not available"; |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2594 | #endif // __BIONIC__ |
| 2595 | } |
| 2596 | |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 2597 | TEST(pthread, pthread_mutex_clocklock_pi) { |
| 2598 | #if defined(__BIONIC__) |
| 2599 | pthread_mutex_timedlock_pi_helper( |
| 2600 | CLOCK_MONOTONIC, [](pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 2601 | return pthread_mutex_clocklock(__mutex, CLOCK_MONOTONIC, __timeout); |
| 2602 | }); |
| 2603 | pthread_mutex_timedlock_pi_helper( |
| 2604 | CLOCK_REALTIME, [](pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 2605 | return pthread_mutex_clocklock(__mutex, CLOCK_REALTIME, __timeout); |
| 2606 | }); |
| 2607 | #else // __BIONIC__ |
| 2608 | GTEST_SKIP() << "pthread_mutex_clocklock not available"; |
| 2609 | #endif // __BIONIC__ |
| 2610 | } |
| 2611 | |
| 2612 | TEST(pthread, pthread_mutex_clocklock_invalid) { |
| 2613 | #if defined(__BIONIC__) |
| 2614 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 2615 | timespec ts; |
| 2616 | EXPECT_EQ(EINVAL, pthread_mutex_clocklock(&mutex, CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 2617 | #else // __BIONIC__ |
| 2618 | GTEST_SKIP() << "pthread_mutex_clocklock not available"; |
| 2619 | #endif // __BIONIC__ |
| 2620 | } |
| 2621 | |
| Elliott Hughes | e657eb4 | 2021-02-18 17:11:56 -0800 | [diff] [blame] | 2622 | TEST_F(pthread_DeathTest, pthread_mutex_using_destroyed_mutex) { |
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 2623 | #if defined(__BIONIC__) |
| 2624 | pthread_mutex_t m; |
| 2625 | ASSERT_EQ(0, pthread_mutex_init(&m, nullptr)); |
| 2626 | ASSERT_EQ(0, pthread_mutex_destroy(&m)); |
| 2627 | ASSERT_EXIT(pthread_mutex_lock(&m), ::testing::KilledBySignal(SIGABRT), |
| 2628 | "pthread_mutex_lock called on a destroyed mutex"); |
| 2629 | ASSERT_EXIT(pthread_mutex_unlock(&m), ::testing::KilledBySignal(SIGABRT), |
| 2630 | "pthread_mutex_unlock called on a destroyed mutex"); |
| 2631 | ASSERT_EXIT(pthread_mutex_trylock(&m), ::testing::KilledBySignal(SIGABRT), |
| 2632 | "pthread_mutex_trylock called on a destroyed mutex"); |
| 2633 | timespec ts; |
| 2634 | ASSERT_EXIT(pthread_mutex_timedlock(&m, &ts), ::testing::KilledBySignal(SIGABRT), |
| 2635 | "pthread_mutex_timedlock called on a destroyed mutex"); |
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2636 | ASSERT_EXIT(pthread_mutex_timedlock_monotonic_np(&m, &ts), ::testing::KilledBySignal(SIGABRT), |
| 2637 | "pthread_mutex_timedlock_monotonic_np called on a destroyed mutex"); |
| Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 2638 | ASSERT_EXIT(pthread_mutex_clocklock(&m, CLOCK_MONOTONIC, &ts), ::testing::KilledBySignal(SIGABRT), |
| 2639 | "pthread_mutex_clocklock called on a destroyed mutex"); |
| 2640 | ASSERT_EXIT(pthread_mutex_clocklock(&m, CLOCK_REALTIME, &ts), ::testing::KilledBySignal(SIGABRT), |
| 2641 | "pthread_mutex_clocklock called on a destroyed mutex"); |
| 2642 | ASSERT_EXIT(pthread_mutex_clocklock(&m, CLOCK_PROCESS_CPUTIME_ID, &ts), |
| 2643 | ::testing::KilledBySignal(SIGABRT), |
| 2644 | "pthread_mutex_clocklock called on a destroyed mutex"); |
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 2645 | ASSERT_EXIT(pthread_mutex_destroy(&m), ::testing::KilledBySignal(SIGABRT), |
| 2646 | "pthread_mutex_destroy called on a destroyed mutex"); |
| 2647 | #else |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2648 | GTEST_SKIP() << "bionic-only test"; |
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 2649 | #endif |
| 2650 | } |
| 2651 | |
| Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2652 | class StrictAlignmentAllocator { |
| 2653 | public: |
| 2654 | void* allocate(size_t size, size_t alignment) { |
| 2655 | char* p = new char[size + alignment * 2]; |
| 2656 | allocated_array.push_back(p); |
| 2657 | while (!is_strict_aligned(p, alignment)) { |
| 2658 | ++p; |
| 2659 | } |
| 2660 | return p; |
| 2661 | } |
| 2662 | |
| 2663 | ~StrictAlignmentAllocator() { |
| Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 2664 | for (const auto& p : allocated_array) { |
| 2665 | delete[] p; |
| Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | private: |
| 2670 | bool is_strict_aligned(char* p, size_t alignment) { |
| 2671 | return (reinterpret_cast<uintptr_t>(p) % (alignment * 2)) == alignment; |
| 2672 | } |
| 2673 | |
| 2674 | std::vector<char*> allocated_array; |
| 2675 | }; |
| 2676 | |
| 2677 | TEST(pthread, pthread_types_allow_four_bytes_alignment) { |
| 2678 | #if defined(__BIONIC__) |
| 2679 | // For binary compatibility with old version, we need to allow 4-byte aligned data for pthread types. |
| 2680 | StrictAlignmentAllocator allocator; |
| 2681 | pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>( |
| 2682 | allocator.allocate(sizeof(pthread_mutex_t), 4)); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2683 | ASSERT_EQ(0, pthread_mutex_init(mutex, nullptr)); |
| Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2684 | ASSERT_EQ(0, pthread_mutex_lock(mutex)); |
| 2685 | ASSERT_EQ(0, pthread_mutex_unlock(mutex)); |
| 2686 | ASSERT_EQ(0, pthread_mutex_destroy(mutex)); |
| 2687 | |
| 2688 | pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>( |
| 2689 | allocator.allocate(sizeof(pthread_cond_t), 4)); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2690 | ASSERT_EQ(0, pthread_cond_init(cond, nullptr)); |
| Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2691 | ASSERT_EQ(0, pthread_cond_signal(cond)); |
| 2692 | ASSERT_EQ(0, pthread_cond_broadcast(cond)); |
| 2693 | ASSERT_EQ(0, pthread_cond_destroy(cond)); |
| 2694 | |
| 2695 | pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>( |
| 2696 | allocator.allocate(sizeof(pthread_rwlock_t), 4)); |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2697 | ASSERT_EQ(0, pthread_rwlock_init(rwlock, nullptr)); |
| Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2698 | ASSERT_EQ(0, pthread_rwlock_rdlock(rwlock)); |
| 2699 | ASSERT_EQ(0, pthread_rwlock_unlock(rwlock)); |
| 2700 | ASSERT_EQ(0, pthread_rwlock_wrlock(rwlock)); |
| 2701 | ASSERT_EQ(0, pthread_rwlock_unlock(rwlock)); |
| 2702 | ASSERT_EQ(0, pthread_rwlock_destroy(rwlock)); |
| 2703 | |
| 2704 | #else |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2705 | GTEST_SKIP() << "bionic-only test"; |
| Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2706 | #endif |
| 2707 | } |
| Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2708 | |
| 2709 | TEST(pthread, pthread_mutex_lock_null_32) { |
| 2710 | #if defined(__BIONIC__) && !defined(__LP64__) |
| Dan Albert | baa2a97 | 2015-08-13 16:58:50 -0700 | [diff] [blame] | 2711 | // For LP32, the pthread lock/unlock functions allow a NULL mutex and return |
| 2712 | // EINVAL in that case: http://b/19995172. |
| 2713 | // |
| 2714 | // We decorate the public defintion with _Nonnull so that people recompiling |
| 2715 | // their code with get a warning and might fix their bug, but need to pass |
| 2716 | // NULL here to test that we remain compatible. |
| 2717 | pthread_mutex_t* null_value = nullptr; |
| 2718 | ASSERT_EQ(EINVAL, pthread_mutex_lock(null_value)); |
| Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2719 | #else |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2720 | GTEST_SKIP() << "32-bit bionic-only test"; |
| Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2721 | #endif |
| 2722 | } |
| 2723 | |
| 2724 | TEST(pthread, pthread_mutex_unlock_null_32) { |
| 2725 | #if defined(__BIONIC__) && !defined(__LP64__) |
| Dan Albert | baa2a97 | 2015-08-13 16:58:50 -0700 | [diff] [blame] | 2726 | // For LP32, the pthread lock/unlock functions allow a NULL mutex and return |
| 2727 | // EINVAL in that case: http://b/19995172. |
| 2728 | // |
| 2729 | // We decorate the public defintion with _Nonnull so that people recompiling |
| 2730 | // their code with get a warning and might fix their bug, but need to pass |
| 2731 | // NULL here to test that we remain compatible. |
| 2732 | pthread_mutex_t* null_value = nullptr; |
| 2733 | ASSERT_EQ(EINVAL, pthread_mutex_unlock(null_value)); |
| Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2734 | #else |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2735 | GTEST_SKIP() << "32-bit bionic-only test"; |
| Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2736 | #endif |
| 2737 | } |
| 2738 | |
| 2739 | TEST_F(pthread_DeathTest, pthread_mutex_lock_null_64) { |
| 2740 | #if defined(__BIONIC__) && defined(__LP64__) |
| 2741 | pthread_mutex_t* null_value = nullptr; |
| 2742 | ASSERT_EXIT(pthread_mutex_lock(null_value), testing::KilledBySignal(SIGSEGV), ""); |
| 2743 | #else |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2744 | GTEST_SKIP() << "64-bit bionic-only test"; |
| Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2745 | #endif |
| 2746 | } |
| 2747 | |
| 2748 | TEST_F(pthread_DeathTest, pthread_mutex_unlock_null_64) { |
| 2749 | #if defined(__BIONIC__) && defined(__LP64__) |
| 2750 | pthread_mutex_t* null_value = nullptr; |
| 2751 | ASSERT_EXIT(pthread_mutex_unlock(null_value), testing::KilledBySignal(SIGSEGV), ""); |
| 2752 | #else |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2753 | GTEST_SKIP() << "64-bit bionic-only test"; |
| Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2754 | #endif |
| 2755 | } |
| Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 2756 | |
| 2757 | extern _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx, void* arg); |
| 2758 | |
| 2759 | static volatile bool signal_handler_on_altstack_done; |
| 2760 | |
| Josh Gao | 61db9ac | 2017-03-15 19:42:05 -0700 | [diff] [blame] | 2761 | __attribute__((__noinline__)) |
| 2762 | static void signal_handler_backtrace() { |
| 2763 | // Check if we have enough stack space for unwinding. |
| 2764 | int count = 0; |
| 2765 | _Unwind_Backtrace(FrameCounter, &count); |
| 2766 | ASSERT_GT(count, 0); |
| 2767 | } |
| 2768 | |
| 2769 | __attribute__((__noinline__)) |
| 2770 | static void signal_handler_logging() { |
| 2771 | // Check if we have enough stack space for logging. |
| 2772 | std::string s(2048, '*'); |
| 2773 | GTEST_LOG_(INFO) << s; |
| 2774 | signal_handler_on_altstack_done = true; |
| 2775 | } |
| 2776 | |
| 2777 | __attribute__((__noinline__)) |
| 2778 | static void signal_handler_snprintf() { |
| 2779 | // Check if we have enough stack space for snprintf to a PATH_MAX buffer, plus some extra. |
| 2780 | char buf[PATH_MAX + 2048]; |
| 2781 | ASSERT_GT(snprintf(buf, sizeof(buf), "/proc/%d/status", getpid()), 0); |
| 2782 | } |
| 2783 | |
| Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 2784 | static void SignalHandlerOnAltStack(int signo, siginfo_t*, void*) { |
| 2785 | ASSERT_EQ(SIGUSR1, signo); |
| Josh Gao | 61db9ac | 2017-03-15 19:42:05 -0700 | [diff] [blame] | 2786 | signal_handler_backtrace(); |
| 2787 | signal_handler_logging(); |
| 2788 | signal_handler_snprintf(); |
| Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 2789 | } |
| 2790 | |
| Josh Gao | 415daa8 | 2017-03-06 17:45:33 -0800 | [diff] [blame] | 2791 | TEST(pthread, big_enough_signal_stack) { |
| Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 2792 | signal_handler_on_altstack_done = false; |
| 2793 | ScopedSignalHandler handler(SIGUSR1, SignalHandlerOnAltStack, SA_SIGINFO | SA_ONSTACK); |
| 2794 | kill(getpid(), SIGUSR1); |
| 2795 | ASSERT_TRUE(signal_handler_on_altstack_done); |
| 2796 | } |
| Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2797 | |
| 2798 | TEST(pthread, pthread_barrierattr_smoke) { |
| 2799 | pthread_barrierattr_t attr; |
| 2800 | ASSERT_EQ(0, pthread_barrierattr_init(&attr)); |
| 2801 | int pshared; |
| 2802 | ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared)); |
| 2803 | ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); |
| 2804 | ASSERT_EQ(0, pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); |
| 2805 | ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared)); |
| 2806 | ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); |
| 2807 | ASSERT_EQ(0, pthread_barrierattr_destroy(&attr)); |
| 2808 | } |
| 2809 | |
| Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2810 | struct BarrierTestHelperData { |
| 2811 | size_t thread_count; |
| 2812 | pthread_barrier_t barrier; |
| 2813 | std::atomic<int> finished_mask; |
| 2814 | std::atomic<int> serial_thread_count; |
| Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2815 | size_t iteration_count; |
| Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2816 | std::atomic<size_t> finished_iteration_count; |
| 2817 | |
| 2818 | BarrierTestHelperData(size_t thread_count, size_t iteration_count) |
| 2819 | : thread_count(thread_count), finished_mask(0), serial_thread_count(0), |
| 2820 | iteration_count(iteration_count), finished_iteration_count(0) { |
| 2821 | } |
| 2822 | }; |
| 2823 | |
| 2824 | struct BarrierTestHelperArg { |
| 2825 | int id; |
| 2826 | BarrierTestHelperData* data; |
| Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2827 | }; |
| 2828 | |
| 2829 | static void BarrierTestHelper(BarrierTestHelperArg* arg) { |
| Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2830 | for (size_t i = 0; i < arg->data->iteration_count; ++i) { |
| 2831 | int result = pthread_barrier_wait(&arg->data->barrier); |
| 2832 | if (result == PTHREAD_BARRIER_SERIAL_THREAD) { |
| 2833 | arg->data->serial_thread_count++; |
| 2834 | } else { |
| 2835 | ASSERT_EQ(0, result); |
| 2836 | } |
| Yabin Cui | d5c04c5 | 2017-05-02 12:57:39 -0700 | [diff] [blame] | 2837 | int mask = arg->data->finished_mask.fetch_or(1 << arg->id); |
| Yabin Cui | ab4cddc | 2017-05-02 16:18:13 -0700 | [diff] [blame] | 2838 | mask |= 1 << arg->id; |
| Yabin Cui | d5c04c5 | 2017-05-02 12:57:39 -0700 | [diff] [blame] | 2839 | if (mask == ((1 << arg->data->thread_count) - 1)) { |
| Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2840 | ASSERT_EQ(1, arg->data->serial_thread_count); |
| 2841 | arg->data->finished_iteration_count++; |
| 2842 | arg->data->finished_mask = 0; |
| 2843 | arg->data->serial_thread_count = 0; |
| 2844 | } |
| Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2845 | } |
| 2846 | } |
| 2847 | |
| 2848 | TEST(pthread, pthread_barrier_smoke) { |
| 2849 | const size_t BARRIER_ITERATION_COUNT = 10; |
| 2850 | const size_t BARRIER_THREAD_COUNT = 10; |
| Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2851 | BarrierTestHelperData data(BARRIER_THREAD_COUNT, BARRIER_ITERATION_COUNT); |
| 2852 | ASSERT_EQ(0, pthread_barrier_init(&data.barrier, nullptr, data.thread_count)); |
| 2853 | std::vector<pthread_t> threads(data.thread_count); |
| Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2854 | std::vector<BarrierTestHelperArg> args(threads.size()); |
| 2855 | for (size_t i = 0; i < threads.size(); ++i) { |
| Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2856 | args[i].id = i; |
| 2857 | args[i].data = &data; |
| Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2858 | ASSERT_EQ(0, pthread_create(&threads[i], nullptr, |
| 2859 | reinterpret_cast<void* (*)(void*)>(BarrierTestHelper), &args[i])); |
| 2860 | } |
| Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2861 | for (size_t i = 0; i < threads.size(); ++i) { |
| 2862 | ASSERT_EQ(0, pthread_join(threads[i], nullptr)); |
| 2863 | } |
| Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2864 | ASSERT_EQ(data.iteration_count, data.finished_iteration_count); |
| 2865 | ASSERT_EQ(0, pthread_barrier_destroy(&data.barrier)); |
| 2866 | } |
| 2867 | |
| 2868 | struct BarrierDestroyTestArg { |
| 2869 | std::atomic<int> tid; |
| 2870 | pthread_barrier_t* barrier; |
| 2871 | }; |
| 2872 | |
| 2873 | static void BarrierDestroyTestHelper(BarrierDestroyTestArg* arg) { |
| 2874 | arg->tid = gettid(); |
| 2875 | ASSERT_EQ(0, pthread_barrier_wait(arg->barrier)); |
| Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | TEST(pthread, pthread_barrier_destroy) { |
| 2879 | pthread_barrier_t barrier; |
| 2880 | ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, 2)); |
| 2881 | pthread_t thread; |
| Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2882 | BarrierDestroyTestArg arg; |
| Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2883 | arg.tid = 0; |
| 2884 | arg.barrier = &barrier; |
| Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2885 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2886 | reinterpret_cast<void* (*)(void*)>(BarrierDestroyTestHelper), &arg)); |
| Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2887 | WaitUntilThreadSleep(arg.tid); |
| 2888 | ASSERT_EQ(EBUSY, pthread_barrier_destroy(&barrier)); |
| 2889 | ASSERT_EQ(PTHREAD_BARRIER_SERIAL_THREAD, pthread_barrier_wait(&barrier)); |
| 2890 | // Verify if the barrier can be destroyed directly after pthread_barrier_wait(). |
| 2891 | ASSERT_EQ(0, pthread_barrier_destroy(&barrier)); |
| 2892 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 2893 | #if defined(__BIONIC__) |
| 2894 | ASSERT_EQ(EINVAL, pthread_barrier_destroy(&barrier)); |
| 2895 | #endif |
| 2896 | } |
| 2897 | |
| 2898 | struct BarrierOrderingTestHelperArg { |
| 2899 | pthread_barrier_t* barrier; |
| 2900 | size_t* array; |
| 2901 | size_t array_length; |
| 2902 | size_t id; |
| 2903 | }; |
| 2904 | |
| 2905 | void BarrierOrderingTestHelper(BarrierOrderingTestHelperArg* arg) { |
| 2906 | const size_t ITERATION_COUNT = 10000; |
| 2907 | for (size_t i = 1; i <= ITERATION_COUNT; ++i) { |
| 2908 | arg->array[arg->id] = i; |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2909 | int result = pthread_barrier_wait(arg->barrier); |
| 2910 | ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD); |
| Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2911 | for (size_t j = 0; j < arg->array_length; ++j) { |
| 2912 | ASSERT_EQ(i, arg->array[j]); |
| 2913 | } |
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2914 | result = pthread_barrier_wait(arg->barrier); |
| 2915 | ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD); |
| Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2916 | } |
| 2917 | } |
| 2918 | |
| 2919 | TEST(pthread, pthread_barrier_check_ordering) { |
| 2920 | const size_t THREAD_COUNT = 4; |
| 2921 | pthread_barrier_t barrier; |
| 2922 | ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, THREAD_COUNT)); |
| 2923 | size_t array[THREAD_COUNT]; |
| 2924 | std::vector<pthread_t> threads(THREAD_COUNT); |
| 2925 | std::vector<BarrierOrderingTestHelperArg> args(THREAD_COUNT); |
| 2926 | for (size_t i = 0; i < THREAD_COUNT; ++i) { |
| 2927 | args[i].barrier = &barrier; |
| 2928 | args[i].array = array; |
| 2929 | args[i].array_length = THREAD_COUNT; |
| 2930 | args[i].id = i; |
| 2931 | ASSERT_EQ(0, pthread_create(&threads[i], nullptr, |
| 2932 | reinterpret_cast<void* (*)(void*)>(BarrierOrderingTestHelper), |
| 2933 | &args[i])); |
| 2934 | } |
| 2935 | for (size_t i = 0; i < THREAD_COUNT; ++i) { |
| 2936 | ASSERT_EQ(0, pthread_join(threads[i], nullptr)); |
| 2937 | } |
| 2938 | } |
| Yabin Cui | fe3a83a | 2015-11-17 16:03:18 -0800 | [diff] [blame] | 2939 | |
| Elliott Hughes | 463faad | 2018-07-06 14:34:49 -0700 | [diff] [blame] | 2940 | TEST(pthread, pthread_barrier_init_zero_count) { |
| 2941 | pthread_barrier_t barrier; |
| 2942 | ASSERT_EQ(EINVAL, pthread_barrier_init(&barrier, nullptr, 0)); |
| 2943 | } |
| 2944 | |
| Yabin Cui | fe3a83a | 2015-11-17 16:03:18 -0800 | [diff] [blame] | 2945 | TEST(pthread, pthread_spinlock_smoke) { |
| 2946 | pthread_spinlock_t lock; |
| 2947 | ASSERT_EQ(0, pthread_spin_init(&lock, 0)); |
| 2948 | ASSERT_EQ(0, pthread_spin_trylock(&lock)); |
| 2949 | ASSERT_EQ(0, pthread_spin_unlock(&lock)); |
| 2950 | ASSERT_EQ(0, pthread_spin_lock(&lock)); |
| 2951 | ASSERT_EQ(EBUSY, pthread_spin_trylock(&lock)); |
| 2952 | ASSERT_EQ(0, pthread_spin_unlock(&lock)); |
| 2953 | ASSERT_EQ(0, pthread_spin_destroy(&lock)); |
| 2954 | } |
| Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2955 | |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 2956 | TEST(pthread, pthread_attr_getdetachstate__pthread_attr_setdetachstate) { |
| Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2957 | pthread_attr_t attr; |
| 2958 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 2959 | |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 2960 | int state; |
| Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2961 | ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)); |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 2962 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state)); |
| 2963 | ASSERT_EQ(PTHREAD_CREATE_DETACHED, state); |
| 2964 | |
| Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2965 | ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)); |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 2966 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state)); |
| 2967 | ASSERT_EQ(PTHREAD_CREATE_JOINABLE, state); |
| 2968 | |
| Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2969 | ASSERT_EQ(EINVAL, pthread_attr_setdetachstate(&attr, 123)); |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 2970 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state)); |
| 2971 | ASSERT_EQ(PTHREAD_CREATE_JOINABLE, state); |
| Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | TEST(pthread, pthread_create__mmap_failures) { |
| Evgeny Eltsin | b4f7aaa | 2020-06-09 15:49:20 +0200 | [diff] [blame] | 2975 | // After thread is successfully created, native_bridge might need more memory to run it. |
| 2976 | SKIP_WITH_NATIVE_BRIDGE; |
| 2977 | |
| Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2978 | pthread_attr_t attr; |
| 2979 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 2980 | ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)); |
| 2981 | |
| 2982 | const auto kPageSize = sysconf(_SC_PAGE_SIZE); |
| 2983 | |
| Elliott Hughes | 5751298 | 2017-10-02 22:49:18 -0700 | [diff] [blame] | 2984 | // Use up all the VMAs. By default this is 64Ki (though some will already be in use). |
| Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2985 | std::vector<void*> pages; |
| Elliott Hughes | 5751298 | 2017-10-02 22:49:18 -0700 | [diff] [blame] | 2986 | pages.reserve(64 * 1024); |
| Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2987 | int prot = PROT_NONE; |
| 2988 | while (true) { |
| 2989 | void* page = mmap(nullptr, kPageSize, prot, MAP_ANON|MAP_PRIVATE, -1, 0); |
| 2990 | if (page == MAP_FAILED) break; |
| 2991 | pages.push_back(page); |
| 2992 | prot = (prot == PROT_NONE) ? PROT_READ : PROT_NONE; |
| 2993 | } |
| 2994 | |
| 2995 | // Try creating threads, freeing up a page each time we fail. |
| 2996 | size_t EAGAIN_count = 0; |
| 2997 | size_t i = 0; |
| 2998 | for (; i < pages.size(); ++i) { |
| 2999 | pthread_t t; |
| 3000 | int status = pthread_create(&t, &attr, IdFn, nullptr); |
| 3001 | if (status != EAGAIN) break; |
| 3002 | ++EAGAIN_count; |
| 3003 | ASSERT_EQ(0, munmap(pages[i], kPageSize)); |
| 3004 | } |
| 3005 | |
| Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 3006 | // Creating a thread uses at least three VMAs: the combined stack and TLS, and a guard on each |
| 3007 | // side. So we should have seen at least three failures. |
| 3008 | ASSERT_GE(EAGAIN_count, 3U); |
| Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 3009 | |
| 3010 | for (; i < pages.size(); ++i) { |
| 3011 | ASSERT_EQ(0, munmap(pages[i], kPageSize)); |
| 3012 | } |
| 3013 | } |
| Elliott Hughes | dff08ce | 2017-10-16 09:58:45 -0700 | [diff] [blame] | 3014 | |
| 3015 | TEST(pthread, pthread_setschedparam) { |
| 3016 | sched_param p = { .sched_priority = INT_MIN }; |
| 3017 | ASSERT_EQ(EINVAL, pthread_setschedparam(pthread_self(), INT_MIN, &p)); |
| 3018 | } |
| 3019 | |
| 3020 | TEST(pthread, pthread_setschedprio) { |
| 3021 | ASSERT_EQ(EINVAL, pthread_setschedprio(pthread_self(), INT_MIN)); |
| 3022 | } |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3023 | |
| 3024 | TEST(pthread, pthread_attr_getinheritsched__pthread_attr_setinheritsched) { |
| 3025 | pthread_attr_t attr; |
| 3026 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 3027 | |
| 3028 | int state; |
| 3029 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED)); |
| 3030 | ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state)); |
| 3031 | ASSERT_EQ(PTHREAD_INHERIT_SCHED, state); |
| 3032 | |
| 3033 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)); |
| 3034 | ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state)); |
| 3035 | ASSERT_EQ(PTHREAD_EXPLICIT_SCHED, state); |
| 3036 | |
| 3037 | ASSERT_EQ(EINVAL, pthread_attr_setinheritsched(&attr, 123)); |
| 3038 | ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state)); |
| 3039 | ASSERT_EQ(PTHREAD_EXPLICIT_SCHED, state); |
| 3040 | } |
| 3041 | |
| 3042 | TEST(pthread, pthread_attr_setinheritsched__PTHREAD_INHERIT_SCHED__PTHREAD_EXPLICIT_SCHED) { |
| 3043 | pthread_attr_t attr; |
| 3044 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 3045 | |
| 3046 | // If we set invalid scheduling attributes but choose to inherit, everything's fine... |
| 3047 | sched_param param = { .sched_priority = sched_get_priority_max(SCHED_FIFO) + 1 }; |
| 3048 | ASSERT_EQ(0, pthread_attr_setschedparam(&attr, ¶m)); |
| 3049 | ASSERT_EQ(0, pthread_attr_setschedpolicy(&attr, SCHED_FIFO)); |
| 3050 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED)); |
| 3051 | |
| 3052 | pthread_t t; |
| 3053 | ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, nullptr)); |
| 3054 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 3055 | |
| Elliott Hughes | 7a66066 | 2017-10-30 09:26:06 -0700 | [diff] [blame] | 3056 | #if defined(__LP64__) |
| 3057 | // If we ask to use them, though, we'll see a failure... |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3058 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)); |
| 3059 | ASSERT_EQ(EINVAL, pthread_create(&t, &attr, IdFn, nullptr)); |
| Elliott Hughes | 7a66066 | 2017-10-30 09:26:06 -0700 | [diff] [blame] | 3060 | #else |
| 3061 | // For backwards compatibility with broken apps, we just ignore failures |
| 3062 | // to set scheduler attributes on LP32. |
| 3063 | #endif |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | TEST(pthread, pthread_attr_setinheritsched_PTHREAD_INHERIT_SCHED_takes_effect) { |
| 3067 | sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) }; |
| 3068 | int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m); |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 3069 | if (rc == EPERM) GTEST_SKIP() << "pthread_setschedparam failed with EPERM"; |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3070 | ASSERT_EQ(0, rc); |
| 3071 | |
| 3072 | pthread_attr_t attr; |
| 3073 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 3074 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED)); |
| 3075 | |
| Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3076 | SpinFunctionHelper spin_helper; |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3077 | pthread_t t; |
| Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3078 | ASSERT_EQ(0, pthread_create(&t, &attr, spin_helper.GetFunction(), nullptr)); |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3079 | int actual_policy; |
| 3080 | sched_param actual_param; |
| 3081 | ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param)); |
| 3082 | ASSERT_EQ(SCHED_FIFO, actual_policy); |
| Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3083 | spin_helper.UnSpin(); |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3084 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 3085 | } |
| 3086 | |
| 3087 | TEST(pthread, pthread_attr_setinheritsched_PTHREAD_EXPLICIT_SCHED_takes_effect) { |
| 3088 | sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) }; |
| 3089 | int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m); |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 3090 | if (rc == EPERM) GTEST_SKIP() << "pthread_setschedparam failed with EPERM"; |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3091 | ASSERT_EQ(0, rc); |
| 3092 | |
| 3093 | pthread_attr_t attr; |
| 3094 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 3095 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)); |
| 3096 | ASSERT_EQ(0, pthread_attr_setschedpolicy(&attr, SCHED_OTHER)); |
| 3097 | |
| Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3098 | SpinFunctionHelper spin_helper; |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3099 | pthread_t t; |
| Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3100 | ASSERT_EQ(0, pthread_create(&t, &attr, spin_helper.GetFunction(), nullptr)); |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3101 | int actual_policy; |
| 3102 | sched_param actual_param; |
| 3103 | ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param)); |
| 3104 | ASSERT_EQ(SCHED_OTHER, actual_policy); |
| Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3105 | spin_helper.UnSpin(); |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3106 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 3107 | } |
| 3108 | |
| 3109 | TEST(pthread, pthread_attr_setinheritsched__takes_effect_despite_SCHED_RESET_ON_FORK) { |
| 3110 | sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) }; |
| 3111 | int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO | SCHED_RESET_ON_FORK, ¶m); |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 3112 | if (rc == EPERM) GTEST_SKIP() << "pthread_setschedparam failed with EPERM"; |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3113 | ASSERT_EQ(0, rc); |
| 3114 | |
| 3115 | pthread_attr_t attr; |
| 3116 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 3117 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED)); |
| 3118 | |
| Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3119 | SpinFunctionHelper spin_helper; |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3120 | pthread_t t; |
| Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3121 | ASSERT_EQ(0, pthread_create(&t, &attr, spin_helper.GetFunction(), nullptr)); |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3122 | int actual_policy; |
| 3123 | sched_param actual_param; |
| 3124 | ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param)); |
| 3125 | ASSERT_EQ(SCHED_FIFO | SCHED_RESET_ON_FORK, actual_policy); |
| Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3126 | spin_helper.UnSpin(); |
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3127 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 3128 | } |
| Peter Collingbourne | 5d3aa86 | 2020-09-11 15:05:17 -0700 | [diff] [blame] | 3129 | |
| 3130 | extern "C" bool android_run_on_all_threads(bool (*func)(void*), void* arg); |
| 3131 | |
| 3132 | TEST(pthread, run_on_all_threads) { |
| 3133 | #if defined(__BIONIC__) |
| 3134 | pthread_t t; |
| 3135 | ASSERT_EQ( |
| 3136 | 0, pthread_create( |
| 3137 | &t, nullptr, |
| 3138 | [](void*) -> void* { |
| 3139 | pthread_attr_t detached; |
| 3140 | if (pthread_attr_init(&detached) != 0 || |
| 3141 | pthread_attr_setdetachstate(&detached, PTHREAD_CREATE_DETACHED) != 0) { |
| 3142 | return reinterpret_cast<void*>(errno); |
| 3143 | } |
| 3144 | |
| 3145 | for (int i = 0; i != 1000; ++i) { |
| 3146 | pthread_t t1, t2; |
| 3147 | if (pthread_create( |
| 3148 | &t1, &detached, [](void*) -> void* { return nullptr; }, nullptr) != 0 || |
| 3149 | pthread_create( |
| 3150 | &t2, nullptr, [](void*) -> void* { return nullptr; }, nullptr) != 0 || |
| 3151 | pthread_join(t2, nullptr) != 0) { |
| 3152 | return reinterpret_cast<void*>(errno); |
| 3153 | } |
| 3154 | } |
| 3155 | |
| 3156 | if (pthread_attr_destroy(&detached) != 0) { |
| 3157 | return reinterpret_cast<void*>(errno); |
| 3158 | } |
| 3159 | return nullptr; |
| 3160 | }, |
| 3161 | nullptr)); |
| 3162 | |
| 3163 | for (int i = 0; i != 1000; ++i) { |
| 3164 | ASSERT_TRUE(android_run_on_all_threads([](void* arg) { return arg == nullptr; }, nullptr)); |
| 3165 | } |
| 3166 | |
| 3167 | void *retval; |
| 3168 | ASSERT_EQ(0, pthread_join(t, &retval)); |
| 3169 | ASSERT_EQ(nullptr, retval); |
| 3170 | #else |
| 3171 | GTEST_SKIP() << "bionic-only test"; |
| 3172 | #endif |
| 3173 | } |
| Elliott Hughes | e117d6e | 2024-11-14 17:31:13 +0000 | [diff] [blame] | 3174 | |
| 3175 | TEST(pthread, pthread_getaffinity_np_failure) { |
| 3176 | // Trivial test of the errno-preserving/returning behavior. |
| 3177 | #pragma clang diagnostic push |
| 3178 | #pragma clang diagnostic ignored "-Wnonnull" |
| 3179 | errno = 0; |
| 3180 | ASSERT_EQ(EINVAL, pthread_getaffinity_np(pthread_self(), 0, nullptr)); |
| 3181 | ASSERT_ERRNO(0); |
| 3182 | #pragma clang diagnostic pop |
| 3183 | } |
| 3184 | |
| Elliott Hughes | d354c42 | 2024-11-18 14:49:45 +0000 | [diff] [blame] | 3185 | TEST(pthread, pthread_getaffinity) { |
| 3186 | cpu_set_t set; |
| 3187 | CPU_ZERO(&set); |
| 3188 | ASSERT_EQ(0, pthread_getaffinity_np(pthread_self(), sizeof(set), &set)); |
| 3189 | ASSERT_GT(CPU_COUNT(&set), 0); |
| 3190 | } |
| 3191 | |
| Elliott Hughes | e117d6e | 2024-11-14 17:31:13 +0000 | [diff] [blame] | 3192 | TEST(pthread, pthread_setaffinity_np_failure) { |
| 3193 | // Trivial test of the errno-preserving/returning behavior. |
| 3194 | #pragma clang diagnostic push |
| 3195 | #pragma clang diagnostic ignored "-Wnonnull" |
| 3196 | errno = 0; |
| 3197 | ASSERT_EQ(EINVAL, pthread_setaffinity_np(pthread_self(), 0, nullptr)); |
| 3198 | ASSERT_ERRNO(0); |
| 3199 | #pragma clang diagnostic pop |
| 3200 | } |
| Elliott Hughes | fab5e6f | 2024-11-18 21:37:20 +0000 | [diff] [blame] | 3201 | |
| 3202 | TEST(pthread, pthread_setaffinity) { |
| 3203 | cpu_set_t set; |
| 3204 | CPU_ZERO(&set); |
| 3205 | ASSERT_EQ(0, pthread_getaffinity_np(pthread_self(), sizeof(set), &set)); |
| 3206 | // It's hard to make any more general claim than this, |
| 3207 | // but it ought to be safe to ask for the same affinity you already have. |
| 3208 | ASSERT_EQ(0, pthread_setaffinity_np(pthread_self(), sizeof(set), &set)); |
| 3209 | } |
| Tamas Petz | 8322377 | 2025-05-13 11:49:29 +0200 | [diff] [blame] | 3210 | |
| 3211 | #if defined(__aarch64__) |
| 3212 | |
| 3213 | static void* sme_state_checking_thread(void*) { |
| 3214 | // Expected state in the child thread: |
| 3215 | // - PSTATE.SM is 0 |
| 3216 | // - PSTATE.ZA is 0 |
| 3217 | // - TPIDR2_EL0 is 0 |
| 3218 | EXPECT_FALSE(sme_is_sm_on()); |
| 3219 | EXPECT_FALSE(sme_is_za_on()); |
| 3220 | EXPECT_EQ(0UL, sme_tpidr2_el0()); |
| 3221 | |
| 3222 | return nullptr; |
| 3223 | } |
| 3224 | |
| 3225 | static void create_thread() { |
| 3226 | pthread_t thread; |
| 3227 | // Even if these asserts fail sme_state_cleanup() will still be run. |
| 3228 | ASSERT_EQ(0, pthread_create(&thread, nullptr, &sme_state_checking_thread, nullptr)); |
| 3229 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 3230 | } |
| 3231 | |
| 3232 | // It is expected that the new thread is started with SME off. |
| 3233 | TEST(pthread, pthread_create_with_sme_off) { |
| 3234 | if (!sme_is_enabled()) { |
| 3235 | GTEST_SKIP() << "FEAT_SME is not enabled on the device."; |
| 3236 | } |
| 3237 | |
| 3238 | // It is safe to call __arm_za_disable(). This is required to avoid inter-test dependencies. |
| 3239 | __arm_za_disable(); |
| 3240 | create_thread(); |
| 3241 | sme_state_cleanup(); |
| 3242 | } |
| 3243 | |
| 3244 | // It is expected that the new thread is started with SME off. |
| 3245 | TEST(pthread, pthread_create_with_sme_dormant_state) { |
| 3246 | if (!sme_is_enabled()) { |
| 3247 | GTEST_SKIP() << "FEAT_SME is not enabled on the device."; |
| 3248 | } |
| 3249 | |
| 3250 | __arm_za_disable(); |
| 3251 | sme_dormant_caller(&create_thread); |
| 3252 | sme_state_cleanup(); |
| 3253 | } |
| 3254 | |
| 3255 | #endif // defined(__aarch64__) |