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