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 | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 27 | #include <sys/syscall.h> |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 28 | #include <time.h> |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 29 | #include <unistd.h> |
Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 30 | #include <unwind.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 31 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 32 | #include <atomic> |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 33 | #include <regex> |
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 | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 36 | #include <base/file.h> |
| 37 | #include <base/stringprintf.h> |
| 38 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 39 | #include "private/bionic_macros.h" |
| 40 | #include "private/ScopeGuard.h" |
| 41 | #include "BionicDeathTest.h" |
| 42 | #include "ScopedSignalHandler.h" |
| 43 | |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 44 | #include "utils.h" |
| 45 | |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 46 | extern "C" pid_t gettid(); |
| 47 | |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 48 | TEST(pthread, pthread_key_create) { |
| 49 | pthread_key_t key; |
| 50 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 51 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 52 | // Can't delete a key that's already been deleted. |
| 53 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 54 | } |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 55 | |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 56 | TEST(pthread, pthread_keys_max) { |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 57 | // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX. |
| 58 | ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX); |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 59 | } |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 60 | |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 61 | TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) { |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 62 | int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX); |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 63 | ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX); |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST(pthread, pthread_key_many_distinct) { |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 67 | // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX |
| 68 | // pthread keys, but We should be able to allocate at least this many keys. |
| 69 | int nkeys = PTHREAD_KEYS_MAX / 2; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 70 | std::vector<pthread_key_t> keys; |
| 71 | |
| 72 | auto scope_guard = make_scope_guard([&keys]{ |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 73 | for (const auto& key : keys) { |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 74 | EXPECT_EQ(0, pthread_key_delete(key)); |
| 75 | } |
| 76 | }); |
| 77 | |
| 78 | for (int i = 0; i < nkeys; ++i) { |
| 79 | pthread_key_t key; |
Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 80 | // If this fails, it's likely that LIBC_PTHREAD_KEY_RESERVED_COUNT is wrong. |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 81 | ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys; |
| 82 | keys.push_back(key); |
| 83 | ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i))); |
| 84 | } |
| 85 | |
| 86 | for (int i = keys.size() - 1; i >= 0; --i) { |
| 87 | ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back())); |
| 88 | pthread_key_t key = keys.back(); |
| 89 | keys.pop_back(); |
| 90 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 91 | } |
| 92 | } |
| 93 | |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 94 | TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) { |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 95 | std::vector<pthread_key_t> keys; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 96 | int rv = 0; |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 97 | |
| 98 | // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should |
| 99 | // be more than we are allowed to allocate now. |
| 100 | for (int i = 0; i < PTHREAD_KEYS_MAX; i++) { |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 101 | pthread_key_t key; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 102 | rv = pthread_key_create(&key, NULL); |
| 103 | if (rv == EAGAIN) { |
| 104 | break; |
| 105 | } |
| 106 | EXPECT_EQ(0, rv); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 107 | keys.push_back(key); |
| 108 | } |
| 109 | |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 110 | // Don't leak keys. |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 111 | for (const auto& key : keys) { |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 112 | EXPECT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 113 | } |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 114 | keys.clear(); |
| 115 | |
| 116 | // We should have eventually reached the maximum number of keys and received |
| 117 | // EAGAIN. |
| 118 | ASSERT_EQ(EAGAIN, rv); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Elliott Hughes | ebb770f | 2014-06-25 13:46:46 -0700 | [diff] [blame] | 121 | TEST(pthread, pthread_key_delete) { |
| 122 | void* expected = reinterpret_cast<void*>(1234); |
| 123 | pthread_key_t key; |
| 124 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 125 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 126 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 127 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 128 | // After deletion, pthread_getspecific returns NULL. |
| 129 | ASSERT_EQ(NULL, pthread_getspecific(key)); |
| 130 | // And you can't use pthread_setspecific with the deleted key. |
| 131 | ASSERT_EQ(EINVAL, pthread_setspecific(key, expected)); |
| 132 | } |
| 133 | |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 134 | TEST(pthread, pthread_key_fork) { |
| 135 | void* expected = reinterpret_cast<void*>(1234); |
| 136 | pthread_key_t key; |
| 137 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 138 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 139 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 140 | |
| 141 | pid_t pid = fork(); |
| 142 | ASSERT_NE(-1, pid) << strerror(errno); |
| 143 | |
| 144 | if (pid == 0) { |
| 145 | // The surviving thread inherits all the forking thread's TLS values... |
| 146 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 147 | _exit(99); |
| 148 | } |
| 149 | |
| 150 | int status; |
| 151 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
| 152 | ASSERT_TRUE(WIFEXITED(status)); |
| 153 | ASSERT_EQ(99, WEXITSTATUS(status)); |
| 154 | |
| 155 | ASSERT_EQ(expected, pthread_getspecific(key)); |
Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 156 | ASSERT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void* DirtyKeyFn(void* key) { |
| 160 | return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key)); |
| 161 | } |
| 162 | |
| 163 | TEST(pthread, pthread_key_dirty) { |
| 164 | pthread_key_t key; |
| 165 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 166 | |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame^] | 167 | size_t stack_size = 640 * 1024; |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 168 | void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 169 | ASSERT_NE(MAP_FAILED, stack); |
| 170 | memset(stack, 0xff, stack_size); |
| 171 | |
| 172 | pthread_attr_t attr; |
| 173 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 174 | ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size)); |
| 175 | |
| 176 | pthread_t t; |
| 177 | ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key)); |
| 178 | |
| 179 | void* result; |
| 180 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 181 | ASSERT_EQ(nullptr, result); // Not ~0! |
| 182 | |
| 183 | ASSERT_EQ(0, munmap(stack, stack_size)); |
Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 184 | ASSERT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 187 | TEST(pthread, static_pthread_key_used_before_creation) { |
| 188 | #if defined(__BIONIC__) |
| 189 | // See http://b/19625804. The bug is about a static/global pthread key being used before creation. |
| 190 | // So here tests if the static/global default value 0 can be detected as invalid key. |
| 191 | static pthread_key_t key; |
| 192 | ASSERT_EQ(nullptr, pthread_getspecific(key)); |
| 193 | ASSERT_EQ(EINVAL, pthread_setspecific(key, nullptr)); |
| 194 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 195 | #else |
| 196 | GTEST_LOG_(INFO) << "This test tests bionic pthread key implementation detail.\n"; |
| 197 | #endif |
| 198 | } |
| 199 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 200 | static void* IdFn(void* arg) { |
| 201 | return arg; |
| 202 | } |
| 203 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 204 | class SpinFunctionHelper { |
| 205 | public: |
| 206 | SpinFunctionHelper() { |
| 207 | SpinFunctionHelper::spin_flag_ = true; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 208 | } |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 209 | ~SpinFunctionHelper() { |
| 210 | UnSpin(); |
| 211 | } |
| 212 | auto GetFunction() -> void* (*)(void*) { |
| 213 | return SpinFunctionHelper::SpinFn; |
| 214 | } |
| 215 | |
| 216 | void UnSpin() { |
| 217 | SpinFunctionHelper::spin_flag_ = false; |
| 218 | } |
| 219 | |
| 220 | private: |
| 221 | static void* SpinFn(void*) { |
| 222 | while (spin_flag_) {} |
| 223 | return NULL; |
| 224 | } |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame^] | 225 | static std::atomic<bool> spin_flag_; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | // It doesn't matter if spin_flag_ is used in several tests, |
| 229 | // because it is always set to false after each test. Each thread |
| 230 | // loops on spin_flag_ can find it becomes false at some time. |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame^] | 231 | std::atomic<bool> SpinFunctionHelper::spin_flag_; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 232 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 233 | static void* JoinFn(void* arg) { |
| 234 | return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL)); |
| 235 | } |
| 236 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 237 | static void AssertDetached(pthread_t t, bool is_detached) { |
| 238 | pthread_attr_t attr; |
| 239 | ASSERT_EQ(0, pthread_getattr_np(t, &attr)); |
| 240 | int detach_state; |
| 241 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state)); |
| 242 | pthread_attr_destroy(&attr); |
| 243 | ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED)); |
| 244 | } |
| 245 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 246 | static void MakeDeadThread(pthread_t& t) { |
| 247 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL)); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 248 | ASSERT_EQ(0, pthread_join(t, NULL)); |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 251 | TEST(pthread, pthread_create) { |
| 252 | void* expected_result = reinterpret_cast<void*>(123); |
| 253 | // Can we create a thread? |
| 254 | pthread_t t; |
| 255 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result)); |
| 256 | // If we join, do we get the expected value back? |
| 257 | void* result; |
| 258 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 259 | ASSERT_EQ(expected_result, result); |
| 260 | } |
| 261 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 262 | TEST(pthread, pthread_create_EAGAIN) { |
| 263 | pthread_attr_t attributes; |
| 264 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 265 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1))); |
| 266 | |
| 267 | pthread_t t; |
| 268 | ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL)); |
| 269 | } |
| 270 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 271 | TEST(pthread, pthread_no_join_after_detach) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 272 | SpinFunctionHelper spinhelper; |
| 273 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 274 | pthread_t t1; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 275 | ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 276 | |
| 277 | // After a pthread_detach... |
| 278 | ASSERT_EQ(0, pthread_detach(t1)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 279 | AssertDetached(t1, true); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 280 | |
| 281 | // ...pthread_join should fail. |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 282 | ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | TEST(pthread, pthread_no_op_detach_after_join) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 286 | SpinFunctionHelper spinhelper; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 287 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 288 | pthread_t t1; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 289 | ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 290 | |
| 291 | // If thread 2 is already waiting to join thread 1... |
| 292 | pthread_t t2; |
| 293 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 294 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 295 | sleep(1); // (Give t2 a chance to call pthread_join.) |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 296 | |
Yabin Cui | bbb0432 | 2015-03-19 15:19:25 -0700 | [diff] [blame] | 297 | #if defined(__BIONIC__) |
| 298 | ASSERT_EQ(EINVAL, pthread_detach(t1)); |
| 299 | #else |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 300 | ASSERT_EQ(0, pthread_detach(t1)); |
Yabin Cui | bbb0432 | 2015-03-19 15:19:25 -0700 | [diff] [blame] | 301 | #endif |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 302 | AssertDetached(t1, false); |
| 303 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 304 | spinhelper.UnSpin(); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 305 | |
| 306 | // ...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] | 307 | void* join_result; |
| 308 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 309 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 310 | } |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 311 | |
| 312 | TEST(pthread, pthread_join_self) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 313 | ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL)); |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 314 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 315 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 316 | struct TestBug37410 { |
| 317 | pthread_t main_thread; |
| 318 | pthread_mutex_t mutex; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 319 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 320 | static void main() { |
| 321 | TestBug37410 data; |
| 322 | data.main_thread = pthread_self(); |
| 323 | ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL)); |
| 324 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 325 | |
| 326 | pthread_t t; |
| 327 | ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data))); |
| 328 | |
| 329 | // Wait for the thread to be running... |
| 330 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 331 | ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex)); |
| 332 | |
| 333 | // ...and exit. |
| 334 | pthread_exit(NULL); |
| 335 | } |
| 336 | |
| 337 | private: |
| 338 | static void* thread_fn(void* arg) { |
| 339 | TestBug37410* data = reinterpret_cast<TestBug37410*>(arg); |
| 340 | |
| 341 | // Let the main thread know we're running. |
| 342 | pthread_mutex_unlock(&data->mutex); |
| 343 | |
| 344 | // And wait for the main thread to exit. |
| 345 | pthread_join(data->main_thread, NULL); |
| 346 | |
| 347 | return NULL; |
| 348 | } |
| 349 | }; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 350 | |
Elliott Hughes | 7fd803c | 2013-02-14 16:33:52 -0800 | [diff] [blame] | 351 | // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to |
| 352 | // run this test (which exits normally) in its own process. |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 353 | |
| 354 | class pthread_DeathTest : public BionicDeathTest {}; |
| 355 | |
| 356 | TEST_F(pthread_DeathTest, pthread_bug_37410) { |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 357 | // http://code.google.com/p/android/issues/detail?id=37410 |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 358 | ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), ""); |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 359 | } |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 360 | |
| 361 | static void* SignalHandlerFn(void* arg) { |
| 362 | sigset_t wait_set; |
| 363 | sigfillset(&wait_set); |
| 364 | return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg))); |
| 365 | } |
| 366 | |
| 367 | TEST(pthread, pthread_sigmask) { |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 368 | // Check that SIGUSR1 isn't blocked. |
| 369 | sigset_t original_set; |
| 370 | sigemptyset(&original_set); |
| 371 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set)); |
| 372 | ASSERT_FALSE(sigismember(&original_set, SIGUSR1)); |
| 373 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 374 | // Block SIGUSR1. |
| 375 | sigset_t set; |
| 376 | sigemptyset(&set); |
| 377 | sigaddset(&set, SIGUSR1); |
| 378 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL)); |
| 379 | |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 380 | // Check that SIGUSR1 is blocked. |
| 381 | sigset_t final_set; |
| 382 | sigemptyset(&final_set); |
| 383 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set)); |
| 384 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 385 | // ...and that sigprocmask agrees with pthread_sigmask. |
| 386 | sigemptyset(&final_set); |
| 387 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set)); |
| 388 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 389 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 390 | // Spawn a thread that calls sigwait and tells us what it received. |
| 391 | pthread_t signal_thread; |
| 392 | int received_signal = -1; |
| 393 | ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal)); |
| 394 | |
| 395 | // Send that thread SIGUSR1. |
| 396 | pthread_kill(signal_thread, SIGUSR1); |
| 397 | |
| 398 | // See what it got. |
| 399 | void* join_result; |
| 400 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 401 | ASSERT_EQ(SIGUSR1, received_signal); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 402 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 403 | |
| 404 | // Restore the original signal mask. |
| 405 | ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL)); |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 406 | } |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 407 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 408 | TEST(pthread, pthread_setname_np__too_long) { |
Elliott Hughes | d1aea30 | 2015-04-25 10:05:24 -0700 | [diff] [blame] | 409 | // The limit is 15 characters --- the kernel's buffer is 16, but includes a NUL. |
| 410 | ASSERT_EQ(0, pthread_setname_np(pthread_self(), "123456789012345")); |
| 411 | ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "1234567890123456")); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | TEST(pthread, pthread_setname_np__self) { |
| 415 | ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1")); |
| 416 | } |
| 417 | |
| 418 | TEST(pthread, pthread_setname_np__other) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 419 | SpinFunctionHelper spinhelper; |
| 420 | |
Elliott Hughes | ed29e85 | 2014-10-27 12:01:51 -0700 | [diff] [blame] | 421 | pthread_t t1; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 422 | ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); |
Elliott Hughes | ed29e85 | 2014-10-27 12:01:51 -0700 | [diff] [blame] | 423 | ASSERT_EQ(0, pthread_setname_np(t1, "short 2")); |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame^] | 424 | spinhelper.UnSpin(); |
| 425 | ASSERT_EQ(0, pthread_join(t1, nullptr)); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Yabin Cui | 220b99b | 2015-03-26 18:13:07 +0000 | [diff] [blame] | 428 | TEST(pthread, pthread_setname_np__no_such_thread) { |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 429 | pthread_t dead_thread; |
| 430 | MakeDeadThread(dead_thread); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 431 | |
| 432 | // Call pthread_setname_np after thread has already exited. |
Elliott Hughes | 68d98d8 | 2014-11-12 21:03:26 -0800 | [diff] [blame] | 433 | ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3")); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 434 | } |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 435 | |
| 436 | TEST(pthread, pthread_kill__0) { |
| 437 | // Signal 0 just tests that the thread exists, so it's safe to call on ourselves. |
| 438 | ASSERT_EQ(0, pthread_kill(pthread_self(), 0)); |
| 439 | } |
| 440 | |
| 441 | TEST(pthread, pthread_kill__invalid_signal) { |
| 442 | ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1)); |
| 443 | } |
| 444 | |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 445 | static void pthread_kill__in_signal_handler_helper(int signal_number) { |
| 446 | static int count = 0; |
| 447 | ASSERT_EQ(SIGALRM, signal_number); |
| 448 | if (++count == 1) { |
| 449 | // Can we call pthread_kill from a signal handler? |
| 450 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | TEST(pthread, pthread_kill__in_signal_handler) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 455 | ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper); |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 456 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 457 | } |
| 458 | |
Yabin Cui | 220b99b | 2015-03-26 18:13:07 +0000 | [diff] [blame] | 459 | TEST(pthread, pthread_detach__no_such_thread) { |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 460 | pthread_t dead_thread; |
| 461 | MakeDeadThread(dead_thread); |
| 462 | |
| 463 | ASSERT_EQ(ESRCH, pthread_detach(dead_thread)); |
| 464 | } |
| 465 | |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 466 | TEST(pthread, pthread_getcpuclockid__clock_gettime) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 467 | SpinFunctionHelper spinhelper; |
| 468 | |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 469 | pthread_t t; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 470 | ASSERT_EQ(0, pthread_create(&t, NULL, spinhelper.GetFunction(), NULL)); |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 471 | |
| 472 | clockid_t c; |
| 473 | ASSERT_EQ(0, pthread_getcpuclockid(t, &c)); |
| 474 | timespec ts; |
| 475 | ASSERT_EQ(0, clock_gettime(c, &ts)); |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame^] | 476 | spinhelper.UnSpin(); |
| 477 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Yabin Cui | 220b99b | 2015-03-26 18:13:07 +0000 | [diff] [blame] | 480 | TEST(pthread, pthread_getcpuclockid__no_such_thread) { |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 481 | pthread_t dead_thread; |
| 482 | MakeDeadThread(dead_thread); |
| 483 | |
| 484 | clockid_t c; |
| 485 | ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c)); |
| 486 | } |
| 487 | |
Yabin Cui | 220b99b | 2015-03-26 18:13:07 +0000 | [diff] [blame] | 488 | TEST(pthread, pthread_getschedparam__no_such_thread) { |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 489 | pthread_t dead_thread; |
| 490 | MakeDeadThread(dead_thread); |
| 491 | |
| 492 | int policy; |
| 493 | sched_param param; |
| 494 | ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, ¶m)); |
| 495 | } |
| 496 | |
Yabin Cui | 220b99b | 2015-03-26 18:13:07 +0000 | [diff] [blame] | 497 | TEST(pthread, pthread_setschedparam__no_such_thread) { |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 498 | pthread_t dead_thread; |
| 499 | MakeDeadThread(dead_thread); |
| 500 | |
| 501 | int policy = 0; |
| 502 | sched_param param; |
| 503 | ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, ¶m)); |
| 504 | } |
| 505 | |
Yabin Cui | 220b99b | 2015-03-26 18:13:07 +0000 | [diff] [blame] | 506 | TEST(pthread, pthread_join__no_such_thread) { |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 507 | pthread_t dead_thread; |
| 508 | MakeDeadThread(dead_thread); |
| 509 | |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 510 | ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL)); |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Yabin Cui | 220b99b | 2015-03-26 18:13:07 +0000 | [diff] [blame] | 513 | TEST(pthread, pthread_kill__no_such_thread) { |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 514 | pthread_t dead_thread; |
| 515 | MakeDeadThread(dead_thread); |
| 516 | |
| 517 | ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0)); |
| 518 | } |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 519 | |
| 520 | TEST(pthread, pthread_join__multijoin) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 521 | SpinFunctionHelper spinhelper; |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 522 | |
| 523 | pthread_t t1; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 524 | ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 525 | |
| 526 | pthread_t t2; |
| 527 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 528 | |
| 529 | sleep(1); // (Give t2 a chance to call pthread_join.) |
| 530 | |
| 531 | // Multiple joins to the same thread should fail. |
| 532 | ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); |
| 533 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 534 | spinhelper.UnSpin(); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 535 | |
| 536 | // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). |
| 537 | void* join_result; |
| 538 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 539 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 540 | } |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 541 | |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 542 | TEST(pthread, pthread_join__race) { |
| 543 | // http://b/11693195 --- pthread_join could return before the thread had actually exited. |
| 544 | // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread. |
| 545 | for (size_t i = 0; i < 1024; ++i) { |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame^] | 546 | size_t stack_size = 640*1024; |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 547 | void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); |
| 548 | |
| 549 | pthread_attr_t a; |
| 550 | pthread_attr_init(&a); |
| 551 | pthread_attr_setstack(&a, stack, stack_size); |
| 552 | |
| 553 | pthread_t t; |
| 554 | ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL)); |
| 555 | ASSERT_EQ(0, pthread_join(t, NULL)); |
| 556 | ASSERT_EQ(0, munmap(stack, stack_size)); |
| 557 | } |
| 558 | } |
| 559 | |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 560 | static void* GetActualGuardSizeFn(void* arg) { |
| 561 | pthread_attr_t attributes; |
| 562 | pthread_getattr_np(pthread_self(), &attributes); |
| 563 | pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 564 | return NULL; |
| 565 | } |
| 566 | |
| 567 | static size_t GetActualGuardSize(const pthread_attr_t& attributes) { |
| 568 | size_t result; |
| 569 | pthread_t t; |
| 570 | pthread_create(&t, &attributes, GetActualGuardSizeFn, &result); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 571 | pthread_join(t, NULL); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 572 | return result; |
| 573 | } |
| 574 | |
| 575 | static void* GetActualStackSizeFn(void* arg) { |
| 576 | pthread_attr_t attributes; |
| 577 | pthread_getattr_np(pthread_self(), &attributes); |
| 578 | pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | static size_t GetActualStackSize(const pthread_attr_t& attributes) { |
| 583 | size_t result; |
| 584 | pthread_t t; |
| 585 | pthread_create(&t, &attributes, GetActualStackSizeFn, &result); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 586 | pthread_join(t, NULL); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 587 | return result; |
| 588 | } |
| 589 | |
| 590 | TEST(pthread, pthread_attr_setguardsize) { |
| 591 | pthread_attr_t attributes; |
| 592 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 593 | |
| 594 | // Get the default guard size. |
| 595 | size_t default_guard_size; |
| 596 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size)); |
| 597 | |
| 598 | // No such thing as too small: will be rounded up to one page by pthread_create. |
| 599 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128)); |
| 600 | size_t guard_size; |
| 601 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 602 | ASSERT_EQ(128U, guard_size); |
| 603 | ASSERT_EQ(4096U, GetActualGuardSize(attributes)); |
| 604 | |
| 605 | // Large enough and a multiple of the page size. |
| 606 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024)); |
| 607 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 608 | ASSERT_EQ(32*1024U, guard_size); |
| 609 | |
| 610 | // Large enough but not a multiple of the page size; will be rounded up by pthread_create. |
| 611 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1)); |
| 612 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 613 | ASSERT_EQ(32*1024U + 1, guard_size); |
| 614 | } |
| 615 | |
| 616 | TEST(pthread, pthread_attr_setstacksize) { |
| 617 | pthread_attr_t attributes; |
| 618 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 619 | |
| 620 | // Get the default stack size. |
| 621 | size_t default_stack_size; |
| 622 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size)); |
| 623 | |
| 624 | // Too small. |
| 625 | ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128)); |
| 626 | size_t stack_size; |
| 627 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 628 | ASSERT_EQ(default_stack_size, stack_size); |
| 629 | ASSERT_GE(GetActualStackSize(attributes), default_stack_size); |
| 630 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 631 | // 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] | 632 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024)); |
| 633 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 634 | ASSERT_EQ(32*1024U, stack_size); |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 635 | ASSERT_GE(GetActualStackSize(attributes), 32*1024U); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 636 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 637 | // Large enough but not aligned; will be rounded up by pthread_create. |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 638 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1)); |
| 639 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 640 | ASSERT_EQ(32*1024U + 1, stack_size); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 641 | #if defined(__BIONIC__) |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 642 | ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 643 | #else // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 644 | // glibc rounds down, in violation of POSIX. They document this in their BUGS section. |
| 645 | ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 646 | #endif // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 647 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 648 | |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 649 | TEST(pthread, pthread_rwlockattr_smoke) { |
| 650 | pthread_rwlockattr_t attr; |
| 651 | ASSERT_EQ(0, pthread_rwlockattr_init(&attr)); |
| 652 | |
| 653 | int pshared_value_array[] = {PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED}; |
| 654 | for (size_t i = 0; i < sizeof(pshared_value_array) / sizeof(pshared_value_array[0]); ++i) { |
| 655 | ASSERT_EQ(0, pthread_rwlockattr_setpshared(&attr, pshared_value_array[i])); |
| 656 | int pshared; |
| 657 | ASSERT_EQ(0, pthread_rwlockattr_getpshared(&attr, &pshared)); |
| 658 | ASSERT_EQ(pshared_value_array[i], pshared); |
| 659 | } |
| 660 | |
| 661 | int kind_array[] = {PTHREAD_RWLOCK_PREFER_READER_NP, |
| 662 | PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP}; |
| 663 | for (size_t i = 0; i < sizeof(kind_array) / sizeof(kind_array[0]); ++i) { |
| 664 | ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_array[i])); |
| 665 | int kind; |
| 666 | ASSERT_EQ(0, pthread_rwlockattr_getkind_np(&attr, &kind)); |
| 667 | ASSERT_EQ(kind_array[i], kind); |
| 668 | } |
| 669 | |
| 670 | ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr)); |
| 671 | } |
| 672 | |
| 673 | TEST(pthread, pthread_rwlock_init_same_as_PTHREAD_RWLOCK_INITIALIZER) { |
| 674 | pthread_rwlock_t lock1 = PTHREAD_RWLOCK_INITIALIZER; |
| 675 | pthread_rwlock_t lock2; |
| 676 | ASSERT_EQ(0, pthread_rwlock_init(&lock2, NULL)); |
| 677 | ASSERT_EQ(0, memcmp(&lock1, &lock2, sizeof(lock1))); |
| 678 | } |
| 679 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 680 | TEST(pthread, pthread_rwlock_smoke) { |
| 681 | pthread_rwlock_t l; |
| 682 | ASSERT_EQ(0, pthread_rwlock_init(&l, NULL)); |
| 683 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 684 | // Single read lock |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 685 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 686 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 687 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 688 | // Multiple read lock |
| 689 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 690 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 691 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 692 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 693 | |
| 694 | // Write lock |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 695 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 696 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 697 | |
| 698 | // Try writer lock |
| 699 | ASSERT_EQ(0, pthread_rwlock_trywrlock(&l)); |
| 700 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 701 | ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l)); |
| 702 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 703 | |
| 704 | // Try reader lock |
| 705 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 706 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 707 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 708 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 709 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 710 | |
| 711 | // Try writer lock after unlock |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 712 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 713 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 714 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 715 | // EDEADLK in "read after write" |
| 716 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 717 | ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l)); |
| 718 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 719 | |
| 720 | // EDEADLK in "write after write" |
| 721 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 722 | ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l)); |
| 723 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 724 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 725 | ASSERT_EQ(0, pthread_rwlock_destroy(&l)); |
| 726 | } |
| 727 | |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 728 | static void WaitUntilThreadSleep(std::atomic<pid_t>& tid) { |
| 729 | while (tid == 0) { |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 730 | usleep(1000); |
| 731 | } |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 732 | std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load()); |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 733 | std::regex regex {R"(\s+S\s+)"}; |
| 734 | |
| 735 | while (true) { |
| 736 | std::string content; |
| 737 | ASSERT_TRUE(android::base::ReadFileToString(filename, &content)); |
| 738 | if (std::regex_search(content, regex)) { |
| 739 | break; |
| 740 | } |
| 741 | usleep(1000); |
| 742 | } |
| 743 | } |
| 744 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 745 | struct RwlockWakeupHelperArg { |
| 746 | pthread_rwlock_t lock; |
| 747 | enum Progress { |
| 748 | LOCK_INITIALIZED, |
| 749 | LOCK_WAITING, |
| 750 | LOCK_RELEASED, |
| 751 | LOCK_ACCESSED |
| 752 | }; |
| 753 | std::atomic<Progress> progress; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 754 | std::atomic<pid_t> tid; |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 755 | }; |
| 756 | |
| 757 | static void pthread_rwlock_reader_wakeup_writer_helper(RwlockWakeupHelperArg* arg) { |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 758 | arg->tid = gettid(); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 759 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress); |
| 760 | arg->progress = RwlockWakeupHelperArg::LOCK_WAITING; |
| 761 | |
| 762 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&arg->lock)); |
| 763 | ASSERT_EQ(0, pthread_rwlock_wrlock(&arg->lock)); |
| 764 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress); |
| 765 | ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock)); |
| 766 | |
| 767 | arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED; |
| 768 | } |
| 769 | |
| 770 | TEST(pthread, pthread_rwlock_reader_wakeup_writer) { |
| 771 | RwlockWakeupHelperArg wakeup_arg; |
| 772 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL)); |
| 773 | ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock)); |
| 774 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 775 | wakeup_arg.tid = 0; |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 776 | |
| 777 | pthread_t thread; |
| 778 | ASSERT_EQ(0, pthread_create(&thread, NULL, |
| 779 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_reader_wakeup_writer_helper), &wakeup_arg)); |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 780 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 781 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 782 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 783 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED; |
| 784 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 785 | |
| 786 | ASSERT_EQ(0, pthread_join(thread, NULL)); |
| 787 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress); |
| 788 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 789 | } |
| 790 | |
| 791 | static void pthread_rwlock_writer_wakeup_reader_helper(RwlockWakeupHelperArg* arg) { |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 792 | arg->tid = gettid(); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 793 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress); |
| 794 | arg->progress = RwlockWakeupHelperArg::LOCK_WAITING; |
| 795 | |
| 796 | ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&arg->lock)); |
| 797 | ASSERT_EQ(0, pthread_rwlock_rdlock(&arg->lock)); |
| 798 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress); |
| 799 | ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock)); |
| 800 | |
| 801 | arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED; |
| 802 | } |
| 803 | |
| 804 | TEST(pthread, pthread_rwlock_writer_wakeup_reader) { |
| 805 | RwlockWakeupHelperArg wakeup_arg; |
| 806 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL)); |
| 807 | ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock)); |
| 808 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 809 | wakeup_arg.tid = 0; |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 810 | |
| 811 | pthread_t thread; |
| 812 | ASSERT_EQ(0, pthread_create(&thread, NULL, |
| 813 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_writer_wakeup_reader_helper), &wakeup_arg)); |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 814 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 815 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 816 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 817 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED; |
| 818 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 819 | |
| 820 | ASSERT_EQ(0, pthread_join(thread, NULL)); |
| 821 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress); |
| 822 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 823 | } |
| 824 | |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 825 | class RwlockKindTestHelper { |
| 826 | private: |
| 827 | struct ThreadArg { |
| 828 | RwlockKindTestHelper* helper; |
| 829 | std::atomic<pid_t>& tid; |
| 830 | |
| 831 | ThreadArg(RwlockKindTestHelper* helper, std::atomic<pid_t>& tid) |
| 832 | : helper(helper), tid(tid) { } |
| 833 | }; |
| 834 | |
| 835 | public: |
| 836 | pthread_rwlock_t lock; |
| 837 | |
| 838 | public: |
| 839 | RwlockKindTestHelper(int kind_type) { |
| 840 | InitRwlock(kind_type); |
| 841 | } |
| 842 | |
| 843 | ~RwlockKindTestHelper() { |
| 844 | DestroyRwlock(); |
| 845 | } |
| 846 | |
| 847 | void CreateWriterThread(pthread_t& thread, std::atomic<pid_t>& tid) { |
| 848 | tid = 0; |
| 849 | ThreadArg* arg = new ThreadArg(this, tid); |
| 850 | ASSERT_EQ(0, pthread_create(&thread, NULL, |
| 851 | reinterpret_cast<void* (*)(void*)>(WriterThreadFn), arg)); |
| 852 | } |
| 853 | |
| 854 | void CreateReaderThread(pthread_t& thread, std::atomic<pid_t>& tid) { |
| 855 | tid = 0; |
| 856 | ThreadArg* arg = new ThreadArg(this, tid); |
| 857 | ASSERT_EQ(0, pthread_create(&thread, NULL, |
| 858 | reinterpret_cast<void* (*)(void*)>(ReaderThreadFn), arg)); |
| 859 | } |
| 860 | |
| 861 | private: |
| 862 | void InitRwlock(int kind_type) { |
| 863 | pthread_rwlockattr_t attr; |
| 864 | ASSERT_EQ(0, pthread_rwlockattr_init(&attr)); |
| 865 | ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_type)); |
| 866 | ASSERT_EQ(0, pthread_rwlock_init(&lock, &attr)); |
| 867 | ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr)); |
| 868 | } |
| 869 | |
| 870 | void DestroyRwlock() { |
| 871 | ASSERT_EQ(0, pthread_rwlock_destroy(&lock)); |
| 872 | } |
| 873 | |
| 874 | static void WriterThreadFn(ThreadArg* arg) { |
| 875 | arg->tid = gettid(); |
| 876 | |
| 877 | RwlockKindTestHelper* helper = arg->helper; |
| 878 | ASSERT_EQ(0, pthread_rwlock_wrlock(&helper->lock)); |
| 879 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock)); |
| 880 | delete arg; |
| 881 | } |
| 882 | |
| 883 | static void ReaderThreadFn(ThreadArg* arg) { |
| 884 | arg->tid = gettid(); |
| 885 | |
| 886 | RwlockKindTestHelper* helper = arg->helper; |
| 887 | ASSERT_EQ(0, pthread_rwlock_rdlock(&helper->lock)); |
| 888 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock)); |
| 889 | delete arg; |
| 890 | } |
| 891 | }; |
| 892 | |
| 893 | TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_READER_NP) { |
| 894 | RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_READER_NP); |
| 895 | ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock)); |
| 896 | |
| 897 | pthread_t writer_thread; |
| 898 | std::atomic<pid_t> writer_tid; |
| 899 | helper.CreateWriterThread(writer_thread, writer_tid); |
| 900 | WaitUntilThreadSleep(writer_tid); |
| 901 | |
| 902 | pthread_t reader_thread; |
| 903 | std::atomic<pid_t> reader_tid; |
| 904 | helper.CreateReaderThread(reader_thread, reader_tid); |
| 905 | ASSERT_EQ(0, pthread_join(reader_thread, NULL)); |
| 906 | |
| 907 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock)); |
| 908 | ASSERT_EQ(0, pthread_join(writer_thread, NULL)); |
| 909 | } |
| 910 | |
| 911 | TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) { |
| 912 | RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); |
| 913 | ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock)); |
| 914 | |
| 915 | pthread_t writer_thread; |
| 916 | std::atomic<pid_t> writer_tid; |
| 917 | helper.CreateWriterThread(writer_thread, writer_tid); |
| 918 | WaitUntilThreadSleep(writer_tid); |
| 919 | |
| 920 | pthread_t reader_thread; |
| 921 | std::atomic<pid_t> reader_tid; |
| 922 | helper.CreateReaderThread(reader_thread, reader_tid); |
| 923 | WaitUntilThreadSleep(reader_tid); |
| 924 | |
| 925 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock)); |
| 926 | ASSERT_EQ(0, pthread_join(writer_thread, NULL)); |
| 927 | ASSERT_EQ(0, pthread_join(reader_thread, NULL)); |
| 928 | } |
| 929 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 930 | static int g_once_fn_call_count = 0; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 931 | static void OnceFn() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 932 | ++g_once_fn_call_count; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | TEST(pthread, pthread_once_smoke) { |
| 936 | pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 937 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
| 938 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 939 | ASSERT_EQ(1, g_once_fn_call_count); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Elliott Hughes | 3694ec6 | 2014-05-14 11:46:08 -0700 | [diff] [blame] | 942 | static std::string pthread_once_1934122_result = ""; |
| 943 | |
| 944 | static void Routine2() { |
| 945 | pthread_once_1934122_result += "2"; |
| 946 | } |
| 947 | |
| 948 | static void Routine1() { |
| 949 | pthread_once_t once_control_2 = PTHREAD_ONCE_INIT; |
| 950 | pthread_once_1934122_result += "1"; |
| 951 | pthread_once(&once_control_2, &Routine2); |
| 952 | } |
| 953 | |
| 954 | TEST(pthread, pthread_once_1934122) { |
| 955 | // Very old versions of Android couldn't call pthread_once from a |
| 956 | // pthread_once init routine. http://b/1934122. |
| 957 | pthread_once_t once_control_1 = PTHREAD_ONCE_INIT; |
| 958 | ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1)); |
| 959 | ASSERT_EQ("12", pthread_once_1934122_result); |
| 960 | } |
| 961 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 962 | static int g_atfork_prepare_calls = 0; |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 963 | static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; } |
| 964 | 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] | 965 | static int g_atfork_parent_calls = 0; |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 966 | static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 1; } |
| 967 | 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] | 968 | static int g_atfork_child_calls = 0; |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 969 | static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 1; } |
| 970 | 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] | 971 | |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 972 | TEST(pthread, pthread_atfork_smoke) { |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 973 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); |
| 974 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 975 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 976 | int pid = fork(); |
| 977 | ASSERT_NE(-1, pid) << strerror(errno); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 978 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 979 | // Child and parent calls are made in the order they were registered. |
| 980 | if (pid == 0) { |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 981 | ASSERT_EQ(12, g_atfork_child_calls); |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 982 | _exit(0); |
| 983 | } |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 984 | ASSERT_EQ(12, g_atfork_parent_calls); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 985 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 986 | // Prepare calls are made in the reverse order. |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 987 | ASSERT_EQ(21, g_atfork_prepare_calls); |
| 988 | int status; |
| 989 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
| 990 | } |
| 991 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 992 | TEST(pthread, pthread_attr_getscope) { |
| 993 | pthread_attr_t attr; |
| 994 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 995 | |
| 996 | int scope; |
| 997 | ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope)); |
| 998 | ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope); |
| 999 | } |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1000 | |
| 1001 | TEST(pthread, pthread_condattr_init) { |
| 1002 | pthread_condattr_t attr; |
| 1003 | pthread_condattr_init(&attr); |
| 1004 | |
| 1005 | clockid_t clock; |
| 1006 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1007 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 1008 | |
| 1009 | int pshared; |
| 1010 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 1011 | ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); |
| 1012 | } |
| 1013 | |
| 1014 | TEST(pthread, pthread_condattr_setclock) { |
| 1015 | pthread_condattr_t attr; |
| 1016 | pthread_condattr_init(&attr); |
| 1017 | |
| 1018 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME)); |
| 1019 | clockid_t clock; |
| 1020 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1021 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 1022 | |
| 1023 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 1024 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1025 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 1026 | |
| 1027 | ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID)); |
| 1028 | } |
| 1029 | |
| 1030 | TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) { |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1031 | #if defined(__BIONIC__) |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1032 | pthread_condattr_t attr; |
| 1033 | pthread_condattr_init(&attr); |
| 1034 | |
| 1035 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 1036 | ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); |
| 1037 | |
| 1038 | pthread_cond_t cond_var; |
| 1039 | ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr)); |
| 1040 | |
| 1041 | ASSERT_EQ(0, pthread_cond_signal(&cond_var)); |
| 1042 | ASSERT_EQ(0, pthread_cond_broadcast(&cond_var)); |
| 1043 | |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1044 | 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] | 1045 | clockid_t clock; |
| 1046 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1047 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 1048 | int pshared; |
| 1049 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 1050 | ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1051 | #else // !defined(__BIONIC__) |
| 1052 | GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n"; |
| 1053 | #endif // !defined(__BIONIC__) |
| 1054 | } |
| 1055 | |
| 1056 | class pthread_CondWakeupTest : public ::testing::Test { |
| 1057 | protected: |
| 1058 | pthread_mutex_t mutex; |
| 1059 | pthread_cond_t cond; |
| 1060 | |
| 1061 | enum Progress { |
| 1062 | INITIALIZED, |
| 1063 | WAITING, |
| 1064 | SIGNALED, |
| 1065 | FINISHED, |
| 1066 | }; |
| 1067 | std::atomic<Progress> progress; |
| 1068 | pthread_t thread; |
| 1069 | |
| 1070 | protected: |
| 1071 | virtual void SetUp() { |
| 1072 | ASSERT_EQ(0, pthread_mutex_init(&mutex, NULL)); |
| 1073 | ASSERT_EQ(0, pthread_cond_init(&cond, NULL)); |
| 1074 | progress = INITIALIZED; |
| 1075 | ASSERT_EQ(0, |
| 1076 | pthread_create(&thread, NULL, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this)); |
| 1077 | } |
| 1078 | |
| 1079 | virtual void TearDown() { |
| 1080 | ASSERT_EQ(0, pthread_join(thread, NULL)); |
| 1081 | ASSERT_EQ(FINISHED, progress); |
| 1082 | ASSERT_EQ(0, pthread_cond_destroy(&cond)); |
| 1083 | ASSERT_EQ(0, pthread_mutex_destroy(&mutex)); |
| 1084 | } |
| 1085 | |
| 1086 | void SleepUntilProgress(Progress expected_progress) { |
| 1087 | while (progress != expected_progress) { |
| 1088 | usleep(5000); |
| 1089 | } |
| 1090 | usleep(5000); |
| 1091 | } |
| 1092 | |
| 1093 | private: |
| 1094 | static void WaitThreadFn(pthread_CondWakeupTest* test) { |
| 1095 | ASSERT_EQ(0, pthread_mutex_lock(&test->mutex)); |
| 1096 | test->progress = WAITING; |
| 1097 | while (test->progress == WAITING) { |
| 1098 | ASSERT_EQ(0, pthread_cond_wait(&test->cond, &test->mutex)); |
| 1099 | } |
| 1100 | ASSERT_EQ(SIGNALED, test->progress); |
| 1101 | test->progress = FINISHED; |
| 1102 | ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex)); |
| 1103 | } |
| 1104 | }; |
| 1105 | |
| 1106 | TEST_F(pthread_CondWakeupTest, signal) { |
| 1107 | SleepUntilProgress(WAITING); |
| 1108 | progress = SIGNALED; |
| 1109 | pthread_cond_signal(&cond); |
| 1110 | } |
| 1111 | |
| 1112 | TEST_F(pthread_CondWakeupTest, broadcast) { |
| 1113 | SleepUntilProgress(WAITING); |
| 1114 | progress = SIGNALED; |
| 1115 | pthread_cond_broadcast(&cond); |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1116 | } |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1117 | |
| 1118 | TEST(pthread, pthread_mutex_timedlock) { |
| 1119 | pthread_mutex_t m; |
| 1120 | ASSERT_EQ(0, pthread_mutex_init(&m, NULL)); |
| 1121 | |
| 1122 | // If the mutex is already locked, pthread_mutex_timedlock should time out. |
| 1123 | ASSERT_EQ(0, pthread_mutex_lock(&m)); |
| 1124 | |
| 1125 | timespec ts; |
| 1126 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 1127 | ts.tv_nsec += 1; |
| 1128 | ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts)); |
| 1129 | |
| 1130 | // If the mutex is unlocked, pthread_mutex_timedlock should succeed. |
| 1131 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 1132 | |
| 1133 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 1134 | ts.tv_nsec += 1; |
| 1135 | ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts)); |
| 1136 | |
| 1137 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 1138 | ASSERT_EQ(0, pthread_mutex_destroy(&m)); |
| 1139 | } |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1140 | |
| 1141 | TEST(pthread, pthread_attr_getstack__main_thread) { |
| 1142 | // This test is only meaningful for the main thread, so make sure we're running on it! |
| 1143 | ASSERT_EQ(getpid(), syscall(__NR_gettid)); |
| 1144 | |
| 1145 | // Get the main thread's attributes. |
| 1146 | pthread_attr_t attributes; |
| 1147 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1148 | |
| 1149 | // Check that we correctly report that the main thread has no guard page. |
| 1150 | size_t guard_size; |
| 1151 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 1152 | ASSERT_EQ(0U, guard_size); // The main thread has no guard page. |
| 1153 | |
| 1154 | // Get the stack base and the stack size (both ways). |
| 1155 | void* stack_base; |
| 1156 | size_t stack_size; |
| 1157 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1158 | size_t stack_size2; |
| 1159 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1160 | |
| 1161 | // The two methods of asking for the stack size should agree. |
| 1162 | EXPECT_EQ(stack_size, stack_size2); |
| 1163 | |
Yabin Cui | b0c6f2db | 2015-05-19 15:09:23 -0700 | [diff] [blame] | 1164 | #if defined(__BIONIC__) |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1165 | // What does /proc/self/maps' [stack] line say? |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1166 | void* maps_stack_hi = NULL; |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 1167 | std::vector<map_record> maps; |
| 1168 | ASSERT_TRUE(Maps::parse_maps(&maps)); |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 1169 | for (const auto& map : maps) { |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 1170 | if (map.pathname == "[stack]") { |
| 1171 | maps_stack_hi = reinterpret_cast<void*>(map.addr_end); |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1172 | break; |
| 1173 | } |
| 1174 | } |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1175 | |
Yabin Cui | b0c6f2db | 2015-05-19 15:09:23 -0700 | [diff] [blame] | 1176 | // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size. |
| 1177 | // Remember that the stack grows down (and is mapped in on demand), so the low address of the |
| 1178 | // region isn't very interesting. |
| 1179 | EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size); |
| 1180 | |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1181 | // The stack size should correspond to RLIMIT_STACK. |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1182 | rlimit rl; |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1183 | ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl)); |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1184 | uint64_t original_rlim_cur = rl.rlim_cur; |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1185 | if (rl.rlim_cur == RLIM_INFINITY) { |
| 1186 | rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB. |
| 1187 | } |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1188 | EXPECT_EQ(rl.rlim_cur, stack_size); |
| 1189 | |
Dmitriy Ivanov | d9ff722 | 2014-09-08 16:22:22 -0700 | [diff] [blame] | 1190 | auto guard = make_scope_guard([&rl, original_rlim_cur]() { |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1191 | rl.rlim_cur = original_rlim_cur; |
| 1192 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1193 | }); |
| 1194 | |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1195 | // |
| 1196 | // What if RLIMIT_STACK is smaller than the stack's current extent? |
| 1197 | // |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1198 | rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already. |
| 1199 | rl.rlim_max = RLIM_INFINITY; |
| 1200 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1201 | |
| 1202 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1203 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1204 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1205 | |
| 1206 | EXPECT_EQ(stack_size, stack_size2); |
| 1207 | ASSERT_EQ(1024U, stack_size); |
| 1208 | |
| 1209 | // |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1210 | // What if RLIMIT_STACK isn't a whole number of pages? |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1211 | // |
| 1212 | rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages. |
| 1213 | rl.rlim_max = RLIM_INFINITY; |
| 1214 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1215 | |
| 1216 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1217 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1218 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1219 | |
| 1220 | EXPECT_EQ(stack_size, stack_size2); |
| 1221 | ASSERT_EQ(6666U, stack_size); |
Yabin Cui | b0c6f2db | 2015-05-19 15:09:23 -0700 | [diff] [blame] | 1222 | #endif |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1223 | } |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1224 | |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1225 | struct GetStackSignalHandlerArg { |
| 1226 | volatile bool done; |
| 1227 | void* signal_handler_sp; |
| 1228 | void* main_stack_base; |
| 1229 | size_t main_stack_size; |
| 1230 | }; |
| 1231 | |
| 1232 | static GetStackSignalHandlerArg getstack_signal_handler_arg; |
| 1233 | |
| 1234 | static void getstack_signal_handler(int sig) { |
| 1235 | ASSERT_EQ(SIGUSR1, sig); |
| 1236 | // Use sleep() to make current thread be switched out by the kernel to provoke the error. |
| 1237 | sleep(1); |
| 1238 | pthread_attr_t attr; |
| 1239 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr)); |
| 1240 | void* stack_base; |
| 1241 | size_t stack_size; |
| 1242 | ASSERT_EQ(0, pthread_attr_getstack(&attr, &stack_base, &stack_size)); |
| 1243 | getstack_signal_handler_arg.signal_handler_sp = &attr; |
| 1244 | getstack_signal_handler_arg.main_stack_base = stack_base; |
| 1245 | getstack_signal_handler_arg.main_stack_size = stack_size; |
| 1246 | getstack_signal_handler_arg.done = true; |
| 1247 | } |
| 1248 | |
| 1249 | // The previous code obtained the main thread's stack by reading the entry in |
| 1250 | // /proc/self/task/<pid>/maps that was labeled [stack]. Unfortunately, on x86/x86_64, the kernel |
| 1251 | // relies on sp0 in task state segment(tss) to label the stack map with [stack]. If the kernel |
| 1252 | // switches a process while the main thread is in an alternate stack, then the kernel will label |
| 1253 | // the wrong map with [stack]. This test verifies that when the above situation happens, the main |
| 1254 | // thread's stack is found correctly. |
| 1255 | TEST(pthread, pthread_attr_getstack_in_signal_handler) { |
| 1256 | const size_t sig_stack_size = 16 * 1024; |
| 1257 | void* sig_stack = mmap(NULL, sig_stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, |
| 1258 | -1, 0); |
| 1259 | ASSERT_NE(MAP_FAILED, sig_stack); |
| 1260 | stack_t ss; |
| 1261 | ss.ss_sp = sig_stack; |
| 1262 | ss.ss_size = sig_stack_size; |
| 1263 | ss.ss_flags = 0; |
| 1264 | stack_t oss; |
| 1265 | ASSERT_EQ(0, sigaltstack(&ss, &oss)); |
| 1266 | |
| 1267 | ScopedSignalHandler handler(SIGUSR1, getstack_signal_handler, SA_ONSTACK); |
| 1268 | getstack_signal_handler_arg.done = false; |
| 1269 | kill(getpid(), SIGUSR1); |
| 1270 | ASSERT_EQ(true, getstack_signal_handler_arg.done); |
| 1271 | |
| 1272 | // Verify if the stack used by the signal handler is the alternate stack just registered. |
| 1273 | ASSERT_LE(sig_stack, getstack_signal_handler_arg.signal_handler_sp); |
| 1274 | ASSERT_GE(reinterpret_cast<char*>(sig_stack) + sig_stack_size, |
| 1275 | getstack_signal_handler_arg.signal_handler_sp); |
| 1276 | |
| 1277 | // Verify if the main thread's stack got in the signal handler is correct. |
| 1278 | ASSERT_LE(getstack_signal_handler_arg.main_stack_base, &ss); |
| 1279 | ASSERT_GE(reinterpret_cast<char*>(getstack_signal_handler_arg.main_stack_base) + |
| 1280 | getstack_signal_handler_arg.main_stack_size, reinterpret_cast<void*>(&ss)); |
| 1281 | |
| 1282 | ASSERT_EQ(0, sigaltstack(&oss, nullptr)); |
| 1283 | ASSERT_EQ(0, munmap(sig_stack, sig_stack_size)); |
| 1284 | } |
| 1285 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 1286 | static void pthread_attr_getstack_18908062_helper(void*) { |
| 1287 | char local_variable; |
| 1288 | pthread_attr_t attributes; |
| 1289 | pthread_getattr_np(pthread_self(), &attributes); |
| 1290 | void* stack_base; |
| 1291 | size_t stack_size; |
| 1292 | pthread_attr_getstack(&attributes, &stack_base, &stack_size); |
| 1293 | |
| 1294 | // Test whether &local_variable is in [stack_base, stack_base + stack_size). |
| 1295 | ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable); |
| 1296 | ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size); |
| 1297 | } |
| 1298 | |
| 1299 | // Check whether something on stack is in the range of |
| 1300 | // [stack_base, stack_base + stack_size). see b/18908062. |
| 1301 | TEST(pthread, pthread_attr_getstack_18908062) { |
| 1302 | pthread_t t; |
| 1303 | ASSERT_EQ(0, pthread_create(&t, NULL, |
| 1304 | reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper), |
| 1305 | NULL)); |
| 1306 | pthread_join(t, NULL); |
| 1307 | } |
| 1308 | |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1309 | #if defined(__BIONIC__) |
Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 1310 | static pthread_mutex_t pthread_gettid_np_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 1311 | |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1312 | static void* pthread_gettid_np_helper(void* arg) { |
| 1313 | *reinterpret_cast<pid_t*>(arg) = gettid(); |
Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 1314 | |
| 1315 | // Wait for our parent to call pthread_gettid_np on us before exiting. |
| 1316 | pthread_mutex_lock(&pthread_gettid_np_mutex); |
| 1317 | pthread_mutex_unlock(&pthread_gettid_np_mutex); |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1318 | return NULL; |
| 1319 | } |
| 1320 | #endif |
| 1321 | |
| 1322 | TEST(pthread, pthread_gettid_np) { |
| 1323 | #if defined(__BIONIC__) |
| 1324 | ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self())); |
| 1325 | |
Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 1326 | // Ensure the other thread doesn't exit until after we've called |
| 1327 | // pthread_gettid_np on it. |
| 1328 | pthread_mutex_lock(&pthread_gettid_np_mutex); |
| 1329 | |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1330 | pid_t t_gettid_result; |
| 1331 | pthread_t t; |
| 1332 | pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result); |
| 1333 | |
| 1334 | pid_t t_pthread_gettid_np_result = pthread_gettid_np(t); |
| 1335 | |
Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 1336 | // Release the other thread and wait for it to exit. |
| 1337 | pthread_mutex_unlock(&pthread_gettid_np_mutex); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1338 | pthread_join(t, NULL); |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1339 | |
| 1340 | ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result); |
| 1341 | #else |
| 1342 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 1343 | #endif |
| 1344 | } |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1345 | |
| 1346 | static size_t cleanup_counter = 0; |
| 1347 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1348 | static void AbortCleanupRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1349 | abort(); |
| 1350 | } |
| 1351 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1352 | static void CountCleanupRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1353 | ++cleanup_counter; |
| 1354 | } |
| 1355 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1356 | static void PthreadCleanupTester() { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1357 | pthread_cleanup_push(CountCleanupRoutine, NULL); |
| 1358 | pthread_cleanup_push(CountCleanupRoutine, NULL); |
| 1359 | pthread_cleanup_push(AbortCleanupRoutine, NULL); |
| 1360 | |
| 1361 | pthread_cleanup_pop(0); // Pop the abort without executing it. |
| 1362 | pthread_cleanup_pop(1); // Pop one count while executing it. |
| 1363 | ASSERT_EQ(1U, cleanup_counter); |
| 1364 | // Exit while the other count is still on the cleanup stack. |
| 1365 | pthread_exit(NULL); |
| 1366 | |
| 1367 | // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced. |
| 1368 | pthread_cleanup_pop(0); |
| 1369 | } |
| 1370 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1371 | static void* PthreadCleanupStartRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1372 | PthreadCleanupTester(); |
| 1373 | return NULL; |
| 1374 | } |
| 1375 | |
| 1376 | TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) { |
| 1377 | pthread_t t; |
| 1378 | ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL)); |
| 1379 | pthread_join(t, NULL); |
| 1380 | ASSERT_EQ(2U, cleanup_counter); |
| 1381 | } |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1382 | |
| 1383 | TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) { |
| 1384 | ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT); |
| 1385 | } |
| 1386 | |
| 1387 | TEST(pthread, pthread_mutexattr_gettype) { |
| 1388 | pthread_mutexattr_t attr; |
| 1389 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 1390 | |
| 1391 | int attr_type; |
| 1392 | |
| 1393 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)); |
| 1394 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 1395 | ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type); |
| 1396 | |
| 1397 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)); |
| 1398 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 1399 | ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type); |
| 1400 | |
| 1401 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)); |
| 1402 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 1403 | ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1404 | |
| 1405 | ASSERT_EQ(0, pthread_mutexattr_destroy(&attr)); |
| 1406 | } |
| 1407 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1408 | struct PthreadMutex { |
| 1409 | pthread_mutex_t lock; |
| 1410 | |
| 1411 | PthreadMutex(int mutex_type) { |
| 1412 | init(mutex_type); |
| 1413 | } |
| 1414 | |
| 1415 | ~PthreadMutex() { |
| 1416 | destroy(); |
| 1417 | } |
| 1418 | |
| 1419 | private: |
| 1420 | void init(int mutex_type) { |
| 1421 | pthread_mutexattr_t attr; |
| 1422 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 1423 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, mutex_type)); |
| 1424 | ASSERT_EQ(0, pthread_mutex_init(&lock, &attr)); |
| 1425 | ASSERT_EQ(0, pthread_mutexattr_destroy(&attr)); |
| 1426 | } |
| 1427 | |
| 1428 | void destroy() { |
| 1429 | ASSERT_EQ(0, pthread_mutex_destroy(&lock)); |
| 1430 | } |
| 1431 | |
| 1432 | DISALLOW_COPY_AND_ASSIGN(PthreadMutex); |
| 1433 | }; |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1434 | |
| 1435 | TEST(pthread, pthread_mutex_lock_NORMAL) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1436 | PthreadMutex m(PTHREAD_MUTEX_NORMAL); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1437 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1438 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| 1439 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | TEST(pthread, pthread_mutex_lock_ERRORCHECK) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1443 | PthreadMutex m(PTHREAD_MUTEX_ERRORCHECK); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1444 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1445 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| 1446 | ASSERT_EQ(EDEADLK, pthread_mutex_lock(&m.lock)); |
| 1447 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 1448 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
| 1449 | ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock)); |
| 1450 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 1451 | ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock)); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | TEST(pthread, pthread_mutex_lock_RECURSIVE) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1455 | PthreadMutex m(PTHREAD_MUTEX_RECURSIVE); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1456 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1457 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| 1458 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| 1459 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 1460 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 1461 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
| 1462 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 1463 | ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock)); |
| 1464 | } |
| 1465 | |
| 1466 | TEST(pthread, pthread_mutex_init_same_as_static_initializers) { |
| 1467 | pthread_mutex_t lock_normal = PTHREAD_MUTEX_INITIALIZER; |
| 1468 | PthreadMutex m1(PTHREAD_MUTEX_NORMAL); |
| 1469 | ASSERT_EQ(0, memcmp(&lock_normal, &m1.lock, sizeof(pthread_mutex_t))); |
| 1470 | pthread_mutex_destroy(&lock_normal); |
| 1471 | |
| 1472 | pthread_mutex_t lock_errorcheck = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; |
| 1473 | PthreadMutex m2(PTHREAD_MUTEX_ERRORCHECK); |
| 1474 | ASSERT_EQ(0, memcmp(&lock_errorcheck, &m2.lock, sizeof(pthread_mutex_t))); |
| 1475 | pthread_mutex_destroy(&lock_errorcheck); |
| 1476 | |
| 1477 | pthread_mutex_t lock_recursive = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
| 1478 | PthreadMutex m3(PTHREAD_MUTEX_RECURSIVE); |
| 1479 | ASSERT_EQ(0, memcmp(&lock_recursive, &m3.lock, sizeof(pthread_mutex_t))); |
| 1480 | ASSERT_EQ(0, pthread_mutex_destroy(&lock_recursive)); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1481 | } |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1482 | class MutexWakeupHelper { |
| 1483 | private: |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1484 | PthreadMutex m; |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1485 | enum Progress { |
| 1486 | LOCK_INITIALIZED, |
| 1487 | LOCK_WAITING, |
| 1488 | LOCK_RELEASED, |
| 1489 | LOCK_ACCESSED |
| 1490 | }; |
| 1491 | std::atomic<Progress> progress; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1492 | std::atomic<pid_t> tid; |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1493 | |
| 1494 | static void thread_fn(MutexWakeupHelper* helper) { |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1495 | helper->tid = gettid(); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1496 | ASSERT_EQ(LOCK_INITIALIZED, helper->progress); |
| 1497 | helper->progress = LOCK_WAITING; |
| 1498 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1499 | ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock)); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1500 | ASSERT_EQ(LOCK_RELEASED, helper->progress); |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1501 | ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock)); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1502 | |
| 1503 | helper->progress = LOCK_ACCESSED; |
| 1504 | } |
| 1505 | |
| 1506 | public: |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1507 | MutexWakeupHelper(int mutex_type) : m(mutex_type) { |
| 1508 | } |
| 1509 | |
| 1510 | void test() { |
| 1511 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1512 | progress = LOCK_INITIALIZED; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1513 | tid = 0; |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1514 | |
| 1515 | pthread_t thread; |
| 1516 | ASSERT_EQ(0, pthread_create(&thread, NULL, |
| 1517 | reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this)); |
| 1518 | |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1519 | WaitUntilThreadSleep(tid); |
| 1520 | ASSERT_EQ(LOCK_WAITING, progress); |
| 1521 | |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1522 | progress = LOCK_RELEASED; |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1523 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1524 | |
| 1525 | ASSERT_EQ(0, pthread_join(thread, NULL)); |
| 1526 | ASSERT_EQ(LOCK_ACCESSED, progress); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1527 | } |
| 1528 | }; |
| 1529 | |
| 1530 | TEST(pthread, pthread_mutex_NORMAL_wakeup) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1531 | MutexWakeupHelper helper(PTHREAD_MUTEX_NORMAL); |
| 1532 | helper.test(); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | TEST(pthread, pthread_mutex_ERRORCHECK_wakeup) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1536 | MutexWakeupHelper helper(PTHREAD_MUTEX_ERRORCHECK); |
| 1537 | helper.test(); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | TEST(pthread, pthread_mutex_RECURSIVE_wakeup) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1541 | MutexWakeupHelper helper(PTHREAD_MUTEX_RECURSIVE); |
| 1542 | helper.test(); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1543 | } |
| 1544 | |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 1545 | TEST(pthread, pthread_mutex_owner_tid_limit) { |
Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 1546 | #if defined(__BIONIC__) && !defined(__LP64__) |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 1547 | FILE* fp = fopen("/proc/sys/kernel/pid_max", "r"); |
| 1548 | ASSERT_TRUE(fp != NULL); |
| 1549 | long pid_max; |
| 1550 | ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max)); |
| 1551 | fclose(fp); |
Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 1552 | // 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] | 1553 | ASSERT_LE(pid_max, 65536); |
Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 1554 | #else |
| 1555 | GTEST_LOG_(INFO) << "This test does nothing as 32-bit tid is supported by pthread_mutex.\n"; |
| 1556 | #endif |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 1557 | } |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 1558 | |
| 1559 | class StrictAlignmentAllocator { |
| 1560 | public: |
| 1561 | void* allocate(size_t size, size_t alignment) { |
| 1562 | char* p = new char[size + alignment * 2]; |
| 1563 | allocated_array.push_back(p); |
| 1564 | while (!is_strict_aligned(p, alignment)) { |
| 1565 | ++p; |
| 1566 | } |
| 1567 | return p; |
| 1568 | } |
| 1569 | |
| 1570 | ~StrictAlignmentAllocator() { |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 1571 | for (const auto& p : allocated_array) { |
| 1572 | delete[] p; |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | private: |
| 1577 | bool is_strict_aligned(char* p, size_t alignment) { |
| 1578 | return (reinterpret_cast<uintptr_t>(p) % (alignment * 2)) == alignment; |
| 1579 | } |
| 1580 | |
| 1581 | std::vector<char*> allocated_array; |
| 1582 | }; |
| 1583 | |
| 1584 | TEST(pthread, pthread_types_allow_four_bytes_alignment) { |
| 1585 | #if defined(__BIONIC__) |
| 1586 | // For binary compatibility with old version, we need to allow 4-byte aligned data for pthread types. |
| 1587 | StrictAlignmentAllocator allocator; |
| 1588 | pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>( |
| 1589 | allocator.allocate(sizeof(pthread_mutex_t), 4)); |
| 1590 | ASSERT_EQ(0, pthread_mutex_init(mutex, NULL)); |
| 1591 | ASSERT_EQ(0, pthread_mutex_lock(mutex)); |
| 1592 | ASSERT_EQ(0, pthread_mutex_unlock(mutex)); |
| 1593 | ASSERT_EQ(0, pthread_mutex_destroy(mutex)); |
| 1594 | |
| 1595 | pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>( |
| 1596 | allocator.allocate(sizeof(pthread_cond_t), 4)); |
| 1597 | ASSERT_EQ(0, pthread_cond_init(cond, NULL)); |
| 1598 | ASSERT_EQ(0, pthread_cond_signal(cond)); |
| 1599 | ASSERT_EQ(0, pthread_cond_broadcast(cond)); |
| 1600 | ASSERT_EQ(0, pthread_cond_destroy(cond)); |
| 1601 | |
| 1602 | pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>( |
| 1603 | allocator.allocate(sizeof(pthread_rwlock_t), 4)); |
| 1604 | ASSERT_EQ(0, pthread_rwlock_init(rwlock, NULL)); |
| 1605 | ASSERT_EQ(0, pthread_rwlock_rdlock(rwlock)); |
| 1606 | ASSERT_EQ(0, pthread_rwlock_unlock(rwlock)); |
| 1607 | ASSERT_EQ(0, pthread_rwlock_wrlock(rwlock)); |
| 1608 | ASSERT_EQ(0, pthread_rwlock_unlock(rwlock)); |
| 1609 | ASSERT_EQ(0, pthread_rwlock_destroy(rwlock)); |
| 1610 | |
| 1611 | #else |
| 1612 | GTEST_LOG_(INFO) << "This test tests bionic implementation details."; |
| 1613 | #endif |
| 1614 | } |
Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 1615 | |
| 1616 | TEST(pthread, pthread_mutex_lock_null_32) { |
| 1617 | #if defined(__BIONIC__) && !defined(__LP64__) |
| 1618 | ASSERT_EQ(EINVAL, pthread_mutex_lock(NULL)); |
| 1619 | #else |
| 1620 | GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices."; |
| 1621 | #endif |
| 1622 | } |
| 1623 | |
| 1624 | TEST(pthread, pthread_mutex_unlock_null_32) { |
| 1625 | #if defined(__BIONIC__) && !defined(__LP64__) |
| 1626 | ASSERT_EQ(EINVAL, pthread_mutex_unlock(NULL)); |
| 1627 | #else |
| 1628 | GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices."; |
| 1629 | #endif |
| 1630 | } |
| 1631 | |
| 1632 | TEST_F(pthread_DeathTest, pthread_mutex_lock_null_64) { |
| 1633 | #if defined(__BIONIC__) && defined(__LP64__) |
| 1634 | pthread_mutex_t* null_value = nullptr; |
| 1635 | ASSERT_EXIT(pthread_mutex_lock(null_value), testing::KilledBySignal(SIGSEGV), ""); |
| 1636 | #else |
| 1637 | GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices."; |
| 1638 | #endif |
| 1639 | } |
| 1640 | |
| 1641 | TEST_F(pthread_DeathTest, pthread_mutex_unlock_null_64) { |
| 1642 | #if defined(__BIONIC__) && defined(__LP64__) |
| 1643 | pthread_mutex_t* null_value = nullptr; |
| 1644 | ASSERT_EXIT(pthread_mutex_unlock(null_value), testing::KilledBySignal(SIGSEGV), ""); |
| 1645 | #else |
| 1646 | GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices."; |
| 1647 | #endif |
| 1648 | } |
Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 1649 | |
| 1650 | extern _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx, void* arg); |
| 1651 | |
| 1652 | static volatile bool signal_handler_on_altstack_done; |
| 1653 | |
| 1654 | static void SignalHandlerOnAltStack(int signo, siginfo_t*, void*) { |
| 1655 | ASSERT_EQ(SIGUSR1, signo); |
| 1656 | // Check if we have enough stack space for unwinding. |
| 1657 | int count = 0; |
| 1658 | _Unwind_Backtrace(FrameCounter, &count); |
| 1659 | ASSERT_GT(count, 0); |
| 1660 | // Check if we have enough stack space for logging. |
| 1661 | std::string s(2048, '*'); |
| 1662 | GTEST_LOG_(INFO) << s; |
| 1663 | signal_handler_on_altstack_done = true; |
| 1664 | } |
| 1665 | |
| 1666 | TEST(pthread, big_enough_signal_stack_for_64bit_arch) { |
| 1667 | signal_handler_on_altstack_done = false; |
| 1668 | ScopedSignalHandler handler(SIGUSR1, SignalHandlerOnAltStack, SA_SIGINFO | SA_ONSTACK); |
| 1669 | kill(getpid(), SIGUSR1); |
| 1670 | ASSERT_TRUE(signal_handler_on_altstack_done); |
| 1671 | } |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 1672 | |
| 1673 | TEST(pthread, pthread_barrierattr_smoke) { |
| 1674 | pthread_barrierattr_t attr; |
| 1675 | ASSERT_EQ(0, pthread_barrierattr_init(&attr)); |
| 1676 | int pshared; |
| 1677 | ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared)); |
| 1678 | ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); |
| 1679 | ASSERT_EQ(0, pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); |
| 1680 | ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared)); |
| 1681 | ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); |
| 1682 | ASSERT_EQ(0, pthread_barrierattr_destroy(&attr)); |
| 1683 | } |
| 1684 | |
| 1685 | struct BarrierTestHelperArg { |
| 1686 | std::atomic<pid_t> tid; |
| 1687 | pthread_barrier_t* barrier; |
| 1688 | size_t iteration_count; |
| 1689 | }; |
| 1690 | |
| 1691 | static void BarrierTestHelper(BarrierTestHelperArg* arg) { |
| 1692 | arg->tid = gettid(); |
| 1693 | for (size_t i = 0; i < arg->iteration_count; ++i) { |
| 1694 | ASSERT_EQ(0, pthread_barrier_wait(arg->barrier)); |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | TEST(pthread, pthread_barrier_smoke) { |
| 1699 | const size_t BARRIER_ITERATION_COUNT = 10; |
| 1700 | const size_t BARRIER_THREAD_COUNT = 10; |
| 1701 | pthread_barrier_t barrier; |
| 1702 | ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, BARRIER_THREAD_COUNT + 1)); |
| 1703 | std::vector<pthread_t> threads(BARRIER_THREAD_COUNT); |
| 1704 | std::vector<BarrierTestHelperArg> args(threads.size()); |
| 1705 | for (size_t i = 0; i < threads.size(); ++i) { |
| 1706 | args[i].tid = 0; |
| 1707 | args[i].barrier = &barrier; |
| 1708 | args[i].iteration_count = BARRIER_ITERATION_COUNT; |
| 1709 | ASSERT_EQ(0, pthread_create(&threads[i], nullptr, |
| 1710 | reinterpret_cast<void* (*)(void*)>(BarrierTestHelper), &args[i])); |
| 1711 | } |
| 1712 | for (size_t iteration = 0; iteration < BARRIER_ITERATION_COUNT; ++iteration) { |
| 1713 | for (size_t i = 0; i < threads.size(); ++i) { |
| 1714 | WaitUntilThreadSleep(args[i].tid); |
| 1715 | } |
| 1716 | ASSERT_EQ(PTHREAD_BARRIER_SERIAL_THREAD, pthread_barrier_wait(&barrier)); |
| 1717 | } |
| 1718 | for (size_t i = 0; i < threads.size(); ++i) { |
| 1719 | ASSERT_EQ(0, pthread_join(threads[i], nullptr)); |
| 1720 | } |
| 1721 | ASSERT_EQ(0, pthread_barrier_destroy(&barrier)); |
| 1722 | } |
| 1723 | |
| 1724 | TEST(pthread, pthread_barrier_destroy) { |
| 1725 | pthread_barrier_t barrier; |
| 1726 | ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, 2)); |
| 1727 | pthread_t thread; |
| 1728 | BarrierTestHelperArg arg; |
| 1729 | arg.tid = 0; |
| 1730 | arg.barrier = &barrier; |
| 1731 | arg.iteration_count = 1; |
| 1732 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| 1733 | reinterpret_cast<void* (*)(void*)>(BarrierTestHelper), &arg)); |
| 1734 | WaitUntilThreadSleep(arg.tid); |
| 1735 | ASSERT_EQ(EBUSY, pthread_barrier_destroy(&barrier)); |
| 1736 | ASSERT_EQ(PTHREAD_BARRIER_SERIAL_THREAD, pthread_barrier_wait(&barrier)); |
| 1737 | // Verify if the barrier can be destroyed directly after pthread_barrier_wait(). |
| 1738 | ASSERT_EQ(0, pthread_barrier_destroy(&barrier)); |
| 1739 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 1740 | #if defined(__BIONIC__) |
| 1741 | ASSERT_EQ(EINVAL, pthread_barrier_destroy(&barrier)); |
| 1742 | #endif |
| 1743 | } |
| 1744 | |
| 1745 | struct BarrierOrderingTestHelperArg { |
| 1746 | pthread_barrier_t* barrier; |
| 1747 | size_t* array; |
| 1748 | size_t array_length; |
| 1749 | size_t id; |
| 1750 | }; |
| 1751 | |
| 1752 | void BarrierOrderingTestHelper(BarrierOrderingTestHelperArg* arg) { |
| 1753 | const size_t ITERATION_COUNT = 10000; |
| 1754 | for (size_t i = 1; i <= ITERATION_COUNT; ++i) { |
| 1755 | arg->array[arg->id] = i; |
| 1756 | int ret = pthread_barrier_wait(arg->barrier); |
| 1757 | ASSERT_TRUE(ret == 0 || ret == PTHREAD_BARRIER_SERIAL_THREAD); |
| 1758 | for (size_t j = 0; j < arg->array_length; ++j) { |
| 1759 | ASSERT_EQ(i, arg->array[j]); |
| 1760 | } |
| 1761 | ret = pthread_barrier_wait(arg->barrier); |
| 1762 | ASSERT_TRUE(ret == 0 || ret == PTHREAD_BARRIER_SERIAL_THREAD); |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | TEST(pthread, pthread_barrier_check_ordering) { |
| 1767 | const size_t THREAD_COUNT = 4; |
| 1768 | pthread_barrier_t barrier; |
| 1769 | ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, THREAD_COUNT)); |
| 1770 | size_t array[THREAD_COUNT]; |
| 1771 | std::vector<pthread_t> threads(THREAD_COUNT); |
| 1772 | std::vector<BarrierOrderingTestHelperArg> args(THREAD_COUNT); |
| 1773 | for (size_t i = 0; i < THREAD_COUNT; ++i) { |
| 1774 | args[i].barrier = &barrier; |
| 1775 | args[i].array = array; |
| 1776 | args[i].array_length = THREAD_COUNT; |
| 1777 | args[i].id = i; |
| 1778 | ASSERT_EQ(0, pthread_create(&threads[i], nullptr, |
| 1779 | reinterpret_cast<void* (*)(void*)>(BarrierOrderingTestHelper), |
| 1780 | &args[i])); |
| 1781 | } |
| 1782 | for (size_t i = 0; i < THREAD_COUNT; ++i) { |
| 1783 | ASSERT_EQ(0, pthread_join(threads[i], nullptr)); |
| 1784 | } |
| 1785 | } |