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