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 | |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 19 | #include "private/ScopeGuard.h" |
| 20 | #include "BionicDeathTest.h" |
| 21 | #include "ScopedSignalHandler.h" |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 22 | #include "gtest_ex.h" |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 23 | |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 24 | #include <errno.h> |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 25 | #include <inttypes.h> |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 26 | #include <limits.h> |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 27 | #include <malloc.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 28 | #include <pthread.h> |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 29 | #include <signal.h> |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 30 | #include <stdio.h> |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 31 | #include <sys/mman.h> |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 32 | #include <sys/syscall.h> |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 33 | #include <time.h> |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 34 | #include <unistd.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 35 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 36 | #include <atomic> |
| 37 | |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 38 | TEST(pthread, pthread_key_create) { |
| 39 | pthread_key_t key; |
| 40 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 41 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 42 | // Can't delete a key that's already been deleted. |
| 43 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 44 | } |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 45 | |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 46 | TEST(pthread, pthread_keys_max) { |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 47 | // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX. |
| 48 | ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX); |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 49 | } |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 50 | |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 51 | TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) { |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 52 | int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX); |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 53 | ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX); |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | TEST(pthread, pthread_key_many_distinct) { |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 57 | // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX |
| 58 | // pthread keys, but We should be able to allocate at least this many keys. |
| 59 | int nkeys = PTHREAD_KEYS_MAX / 2; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 60 | std::vector<pthread_key_t> keys; |
| 61 | |
| 62 | auto scope_guard = make_scope_guard([&keys]{ |
| 63 | for (auto key : keys) { |
| 64 | EXPECT_EQ(0, pthread_key_delete(key)); |
| 65 | } |
| 66 | }); |
| 67 | |
| 68 | for (int i = 0; i < nkeys; ++i) { |
| 69 | pthread_key_t key; |
| 70 | // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is |
| 71 | // wrong. |
| 72 | ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys; |
| 73 | keys.push_back(key); |
| 74 | ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i))); |
| 75 | } |
| 76 | |
| 77 | for (int i = keys.size() - 1; i >= 0; --i) { |
| 78 | ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back())); |
| 79 | pthread_key_t key = keys.back(); |
| 80 | keys.pop_back(); |
| 81 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 82 | } |
| 83 | } |
| 84 | |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 85 | TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) { |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 86 | std::vector<pthread_key_t> keys; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 87 | int rv = 0; |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 88 | |
| 89 | // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should |
| 90 | // be more than we are allowed to allocate now. |
| 91 | for (int i = 0; i < PTHREAD_KEYS_MAX; i++) { |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 92 | pthread_key_t key; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 93 | rv = pthread_key_create(&key, NULL); |
| 94 | if (rv == EAGAIN) { |
| 95 | break; |
| 96 | } |
| 97 | EXPECT_EQ(0, rv); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 98 | keys.push_back(key); |
| 99 | } |
| 100 | |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 101 | // Don't leak keys. |
| 102 | for (auto key : keys) { |
| 103 | EXPECT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 104 | } |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 105 | keys.clear(); |
| 106 | |
| 107 | // We should have eventually reached the maximum number of keys and received |
| 108 | // EAGAIN. |
| 109 | ASSERT_EQ(EAGAIN, rv); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Elliott Hughes | ebb770f | 2014-06-25 13:46:46 -0700 | [diff] [blame] | 112 | TEST(pthread, pthread_key_delete) { |
| 113 | void* expected = reinterpret_cast<void*>(1234); |
| 114 | pthread_key_t key; |
| 115 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 116 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 117 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 118 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 119 | // After deletion, pthread_getspecific returns NULL. |
| 120 | ASSERT_EQ(NULL, pthread_getspecific(key)); |
| 121 | // And you can't use pthread_setspecific with the deleted key. |
| 122 | ASSERT_EQ(EINVAL, pthread_setspecific(key, expected)); |
| 123 | } |
| 124 | |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 125 | TEST(pthread, pthread_key_fork) { |
| 126 | void* expected = reinterpret_cast<void*>(1234); |
| 127 | pthread_key_t key; |
| 128 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 129 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 130 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 131 | |
| 132 | pid_t pid = fork(); |
| 133 | ASSERT_NE(-1, pid) << strerror(errno); |
| 134 | |
| 135 | if (pid == 0) { |
| 136 | // The surviving thread inherits all the forking thread's TLS values... |
| 137 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 138 | _exit(99); |
| 139 | } |
| 140 | |
| 141 | int status; |
| 142 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
| 143 | ASSERT_TRUE(WIFEXITED(status)); |
| 144 | ASSERT_EQ(99, WEXITSTATUS(status)); |
| 145 | |
| 146 | ASSERT_EQ(expected, pthread_getspecific(key)); |
Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 147 | ASSERT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static void* DirtyKeyFn(void* key) { |
| 151 | return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key)); |
| 152 | } |
| 153 | |
| 154 | TEST(pthread, pthread_key_dirty) { |
| 155 | pthread_key_t key; |
| 156 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 157 | |
| 158 | size_t stack_size = 128 * 1024; |
| 159 | void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 160 | ASSERT_NE(MAP_FAILED, stack); |
| 161 | memset(stack, 0xff, stack_size); |
| 162 | |
| 163 | pthread_attr_t attr; |
| 164 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 165 | ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size)); |
| 166 | |
| 167 | pthread_t t; |
| 168 | ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key)); |
| 169 | |
| 170 | void* result; |
| 171 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 172 | ASSERT_EQ(nullptr, result); // Not ~0! |
| 173 | |
| 174 | ASSERT_EQ(0, munmap(stack, stack_size)); |
Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 175 | ASSERT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 178 | static void* IdFn(void* arg) { |
| 179 | return arg; |
| 180 | } |
| 181 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 182 | class SpinFunctionHelper { |
| 183 | public: |
| 184 | SpinFunctionHelper() { |
| 185 | SpinFunctionHelper::spin_flag_ = true; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 186 | } |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 187 | ~SpinFunctionHelper() { |
| 188 | UnSpin(); |
| 189 | } |
| 190 | auto GetFunction() -> void* (*)(void*) { |
| 191 | return SpinFunctionHelper::SpinFn; |
| 192 | } |
| 193 | |
| 194 | void UnSpin() { |
| 195 | SpinFunctionHelper::spin_flag_ = false; |
| 196 | } |
| 197 | |
| 198 | private: |
| 199 | static void* SpinFn(void*) { |
| 200 | while (spin_flag_) {} |
| 201 | return NULL; |
| 202 | } |
| 203 | static volatile bool spin_flag_; |
| 204 | }; |
| 205 | |
| 206 | // It doesn't matter if spin_flag_ is used in several tests, |
| 207 | // because it is always set to false after each test. Each thread |
| 208 | // loops on spin_flag_ can find it becomes false at some time. |
| 209 | volatile bool SpinFunctionHelper::spin_flag_ = false; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 210 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 211 | static void* JoinFn(void* arg) { |
| 212 | return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL)); |
| 213 | } |
| 214 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 215 | static void AssertDetached(pthread_t t, bool is_detached) { |
| 216 | pthread_attr_t attr; |
| 217 | ASSERT_EQ(0, pthread_getattr_np(t, &attr)); |
| 218 | int detach_state; |
| 219 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state)); |
| 220 | pthread_attr_destroy(&attr); |
| 221 | ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED)); |
| 222 | } |
| 223 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 224 | static void MakeDeadThread(pthread_t& t) { |
| 225 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL)); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 226 | ASSERT_EQ(0, pthread_join(t, NULL)); |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 229 | TEST(pthread, pthread_create) { |
| 230 | void* expected_result = reinterpret_cast<void*>(123); |
| 231 | // Can we create a thread? |
| 232 | pthread_t t; |
| 233 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result)); |
| 234 | // If we join, do we get the expected value back? |
| 235 | void* result; |
| 236 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 237 | ASSERT_EQ(expected_result, result); |
| 238 | } |
| 239 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 240 | TEST(pthread, pthread_create_EAGAIN) { |
| 241 | pthread_attr_t attributes; |
| 242 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 243 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1))); |
| 244 | |
| 245 | pthread_t t; |
| 246 | ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL)); |
| 247 | } |
| 248 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 249 | TEST(pthread, pthread_no_join_after_detach) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 250 | SpinFunctionHelper spinhelper; |
| 251 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 252 | pthread_t t1; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 253 | ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 254 | |
| 255 | // After a pthread_detach... |
| 256 | ASSERT_EQ(0, pthread_detach(t1)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 257 | AssertDetached(t1, true); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 258 | |
| 259 | // ...pthread_join should fail. |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 260 | ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | TEST(pthread, pthread_no_op_detach_after_join) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 264 | SpinFunctionHelper spinhelper; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 265 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 266 | pthread_t t1; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 267 | ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 268 | |
| 269 | // If thread 2 is already waiting to join thread 1... |
| 270 | pthread_t t2; |
| 271 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 272 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 273 | sleep(1); // (Give t2 a chance to call pthread_join.) |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 274 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 275 | // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)... |
| 276 | ASSERT_EQ(0, pthread_detach(t1)); |
| 277 | AssertDetached(t1, false); |
| 278 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 279 | spinhelper.UnSpin(); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 280 | |
| 281 | // ...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] | 282 | void* join_result; |
| 283 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 284 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 285 | } |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 286 | |
| 287 | TEST(pthread, pthread_join_self) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 288 | ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL)); |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 289 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 290 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 291 | struct TestBug37410 { |
| 292 | pthread_t main_thread; |
| 293 | pthread_mutex_t mutex; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 294 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 295 | static void main() { |
| 296 | TestBug37410 data; |
| 297 | data.main_thread = pthread_self(); |
| 298 | ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL)); |
| 299 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 300 | |
| 301 | pthread_t t; |
| 302 | ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data))); |
| 303 | |
| 304 | // Wait for the thread to be running... |
| 305 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 306 | ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex)); |
| 307 | |
| 308 | // ...and exit. |
| 309 | pthread_exit(NULL); |
| 310 | } |
| 311 | |
| 312 | private: |
| 313 | static void* thread_fn(void* arg) { |
| 314 | TestBug37410* data = reinterpret_cast<TestBug37410*>(arg); |
| 315 | |
| 316 | // Let the main thread know we're running. |
| 317 | pthread_mutex_unlock(&data->mutex); |
| 318 | |
| 319 | // And wait for the main thread to exit. |
| 320 | pthread_join(data->main_thread, NULL); |
| 321 | |
| 322 | return NULL; |
| 323 | } |
| 324 | }; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 325 | |
Elliott Hughes | 7fd803c | 2013-02-14 16:33:52 -0800 | [diff] [blame] | 326 | // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to |
| 327 | // run this test (which exits normally) in its own process. |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 328 | |
| 329 | class pthread_DeathTest : public BionicDeathTest {}; |
| 330 | |
| 331 | TEST_F(pthread_DeathTest, pthread_bug_37410) { |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 332 | // http://code.google.com/p/android/issues/detail?id=37410 |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 333 | ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), ""); |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 334 | } |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 335 | |
| 336 | static void* SignalHandlerFn(void* arg) { |
| 337 | sigset_t wait_set; |
| 338 | sigfillset(&wait_set); |
| 339 | return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg))); |
| 340 | } |
| 341 | |
| 342 | TEST(pthread, pthread_sigmask) { |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 343 | // Check that SIGUSR1 isn't blocked. |
| 344 | sigset_t original_set; |
| 345 | sigemptyset(&original_set); |
| 346 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set)); |
| 347 | ASSERT_FALSE(sigismember(&original_set, SIGUSR1)); |
| 348 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 349 | // Block SIGUSR1. |
| 350 | sigset_t set; |
| 351 | sigemptyset(&set); |
| 352 | sigaddset(&set, SIGUSR1); |
| 353 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL)); |
| 354 | |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 355 | // Check that SIGUSR1 is blocked. |
| 356 | sigset_t final_set; |
| 357 | sigemptyset(&final_set); |
| 358 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set)); |
| 359 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 360 | // ...and that sigprocmask agrees with pthread_sigmask. |
| 361 | sigemptyset(&final_set); |
| 362 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set)); |
| 363 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 364 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 365 | // Spawn a thread that calls sigwait and tells us what it received. |
| 366 | pthread_t signal_thread; |
| 367 | int received_signal = -1; |
| 368 | ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal)); |
| 369 | |
| 370 | // Send that thread SIGUSR1. |
| 371 | pthread_kill(signal_thread, SIGUSR1); |
| 372 | |
| 373 | // See what it got. |
| 374 | void* join_result; |
| 375 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 376 | ASSERT_EQ(SIGUSR1, received_signal); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 377 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 378 | |
| 379 | // Restore the original signal mask. |
| 380 | ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL)); |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 381 | } |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 382 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 383 | TEST(pthread, pthread_setname_np__too_long) { |
| 384 | ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux")); |
| 385 | } |
| 386 | |
| 387 | TEST(pthread, pthread_setname_np__self) { |
| 388 | ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1")); |
| 389 | } |
| 390 | |
| 391 | TEST(pthread, pthread_setname_np__other) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 392 | SpinFunctionHelper spinhelper; |
| 393 | |
Elliott Hughes | ed29e85 | 2014-10-27 12:01:51 -0700 | [diff] [blame] | 394 | pthread_t t1; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 395 | ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); |
Elliott Hughes | ed29e85 | 2014-10-27 12:01:51 -0700 | [diff] [blame] | 396 | ASSERT_EQ(0, pthread_setname_np(t1, "short 2")); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | TEST(pthread, pthread_setname_np__no_such_thread) { |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 400 | pthread_t dead_thread; |
| 401 | MakeDeadThread(dead_thread); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 402 | |
| 403 | // Call pthread_setname_np after thread has already exited. |
Elliott Hughes | 68d98d8 | 2014-11-12 21:03:26 -0800 | [diff] [blame] | 404 | ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3")); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 405 | } |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 406 | |
| 407 | TEST(pthread, pthread_kill__0) { |
| 408 | // Signal 0 just tests that the thread exists, so it's safe to call on ourselves. |
| 409 | ASSERT_EQ(0, pthread_kill(pthread_self(), 0)); |
| 410 | } |
| 411 | |
| 412 | TEST(pthread, pthread_kill__invalid_signal) { |
| 413 | ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1)); |
| 414 | } |
| 415 | |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 416 | static void pthread_kill__in_signal_handler_helper(int signal_number) { |
| 417 | static int count = 0; |
| 418 | ASSERT_EQ(SIGALRM, signal_number); |
| 419 | if (++count == 1) { |
| 420 | // Can we call pthread_kill from a signal handler? |
| 421 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | TEST(pthread, pthread_kill__in_signal_handler) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 426 | ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper); |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 427 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 428 | } |
| 429 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 430 | TEST(pthread, pthread_detach__no_such_thread) { |
| 431 | pthread_t dead_thread; |
| 432 | MakeDeadThread(dead_thread); |
| 433 | |
| 434 | ASSERT_EQ(ESRCH, pthread_detach(dead_thread)); |
| 435 | } |
| 436 | |
Yabin Cui | 19e246d | 2014-12-18 14:22:09 -0800 | [diff] [blame] | 437 | TEST(pthread, pthread_detach_no_leak) { |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 438 | size_t initial_bytes = 0; |
| 439 | // Run this loop more than once since the first loop causes some memory |
| 440 | // to be allocated permenantly. Run an extra loop to help catch any subtle |
| 441 | // memory leaks. |
| 442 | for (size_t loop = 0; loop < 3; loop++) { |
| 443 | // Set the initial bytes on the second loop since the memory in use |
| 444 | // should have stabilized. |
| 445 | if (loop == 1) { |
| 446 | initial_bytes = mallinfo().uordblks; |
| 447 | } |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 448 | |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 449 | pthread_attr_t attr; |
| 450 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 451 | ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)); |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 452 | |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 453 | std::vector<pthread_t> threads; |
| 454 | for (size_t i = 0; i < 32; ++i) { |
| 455 | pthread_t t; |
| 456 | ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL)); |
| 457 | threads.push_back(t); |
| 458 | } |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 459 | |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 460 | sleep(1); |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 461 | |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 462 | for (size_t i = 0; i < 32; ++i) { |
| 463 | ASSERT_EQ(0, pthread_detach(threads[i])) << i; |
| 464 | } |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | size_t final_bytes = mallinfo().uordblks; |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 468 | int leaked_bytes = (final_bytes - initial_bytes); |
| 469 | |
Yabin Cui | 19e246d | 2014-12-18 14:22:09 -0800 | [diff] [blame] | 470 | ASSERT_EQ(0, leaked_bytes); |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 473 | TEST(pthread, pthread_getcpuclockid__clock_gettime) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 474 | SpinFunctionHelper spinhelper; |
| 475 | |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 476 | pthread_t t; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 477 | ASSERT_EQ(0, pthread_create(&t, NULL, spinhelper.GetFunction(), NULL)); |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 478 | |
| 479 | clockid_t c; |
| 480 | ASSERT_EQ(0, pthread_getcpuclockid(t, &c)); |
| 481 | timespec ts; |
| 482 | ASSERT_EQ(0, clock_gettime(c, &ts)); |
| 483 | } |
| 484 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 485 | TEST(pthread, pthread_getcpuclockid__no_such_thread) { |
| 486 | pthread_t dead_thread; |
| 487 | MakeDeadThread(dead_thread); |
| 488 | |
| 489 | clockid_t c; |
| 490 | ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c)); |
| 491 | } |
| 492 | |
| 493 | TEST(pthread, pthread_getschedparam__no_such_thread) { |
| 494 | pthread_t dead_thread; |
| 495 | MakeDeadThread(dead_thread); |
| 496 | |
| 497 | int policy; |
| 498 | sched_param param; |
| 499 | ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, ¶m)); |
| 500 | } |
| 501 | |
| 502 | TEST(pthread, pthread_setschedparam__no_such_thread) { |
| 503 | pthread_t dead_thread; |
| 504 | MakeDeadThread(dead_thread); |
| 505 | |
| 506 | int policy = 0; |
| 507 | sched_param param; |
| 508 | ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, ¶m)); |
| 509 | } |
| 510 | |
| 511 | TEST(pthread, pthread_join__no_such_thread) { |
| 512 | pthread_t dead_thread; |
| 513 | MakeDeadThread(dead_thread); |
| 514 | |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 515 | ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL)); |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | TEST(pthread, pthread_kill__no_such_thread) { |
| 519 | pthread_t dead_thread; |
| 520 | MakeDeadThread(dead_thread); |
| 521 | |
| 522 | ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0)); |
| 523 | } |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 524 | |
| 525 | TEST(pthread, pthread_join__multijoin) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 526 | SpinFunctionHelper spinhelper; |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 527 | |
| 528 | pthread_t t1; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 529 | ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 530 | |
| 531 | pthread_t t2; |
| 532 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 533 | |
| 534 | sleep(1); // (Give t2 a chance to call pthread_join.) |
| 535 | |
| 536 | // Multiple joins to the same thread should fail. |
| 537 | ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); |
| 538 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 539 | spinhelper.UnSpin(); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 540 | |
| 541 | // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). |
| 542 | void* join_result; |
| 543 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 544 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 545 | } |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 546 | |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 547 | TEST(pthread, pthread_join__race) { |
| 548 | // http://b/11693195 --- pthread_join could return before the thread had actually exited. |
| 549 | // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread. |
| 550 | for (size_t i = 0; i < 1024; ++i) { |
| 551 | size_t stack_size = 64*1024; |
| 552 | void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); |
| 553 | |
| 554 | pthread_attr_t a; |
| 555 | pthread_attr_init(&a); |
| 556 | pthread_attr_setstack(&a, stack, stack_size); |
| 557 | |
| 558 | pthread_t t; |
| 559 | ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL)); |
| 560 | ASSERT_EQ(0, pthread_join(t, NULL)); |
| 561 | ASSERT_EQ(0, munmap(stack, stack_size)); |
| 562 | } |
| 563 | } |
| 564 | |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 565 | static void* GetActualGuardSizeFn(void* arg) { |
| 566 | pthread_attr_t attributes; |
| 567 | pthread_getattr_np(pthread_self(), &attributes); |
| 568 | pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 569 | return NULL; |
| 570 | } |
| 571 | |
| 572 | static size_t GetActualGuardSize(const pthread_attr_t& attributes) { |
| 573 | size_t result; |
| 574 | pthread_t t; |
| 575 | pthread_create(&t, &attributes, GetActualGuardSizeFn, &result); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 576 | pthread_join(t, NULL); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 577 | return result; |
| 578 | } |
| 579 | |
| 580 | static void* GetActualStackSizeFn(void* arg) { |
| 581 | pthread_attr_t attributes; |
| 582 | pthread_getattr_np(pthread_self(), &attributes); |
| 583 | pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 584 | return NULL; |
| 585 | } |
| 586 | |
| 587 | static size_t GetActualStackSize(const pthread_attr_t& attributes) { |
| 588 | size_t result; |
| 589 | pthread_t t; |
| 590 | pthread_create(&t, &attributes, GetActualStackSizeFn, &result); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 591 | pthread_join(t, NULL); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 592 | return result; |
| 593 | } |
| 594 | |
| 595 | TEST(pthread, pthread_attr_setguardsize) { |
| 596 | pthread_attr_t attributes; |
| 597 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 598 | |
| 599 | // Get the default guard size. |
| 600 | size_t default_guard_size; |
| 601 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size)); |
| 602 | |
| 603 | // No such thing as too small: will be rounded up to one page by pthread_create. |
| 604 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128)); |
| 605 | size_t guard_size; |
| 606 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 607 | ASSERT_EQ(128U, guard_size); |
| 608 | ASSERT_EQ(4096U, GetActualGuardSize(attributes)); |
| 609 | |
| 610 | // Large enough and a multiple of the page size. |
| 611 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024)); |
| 612 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 613 | ASSERT_EQ(32*1024U, guard_size); |
| 614 | |
| 615 | // Large enough but not a multiple of the page size; will be rounded up by pthread_create. |
| 616 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1)); |
| 617 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 618 | ASSERT_EQ(32*1024U + 1, guard_size); |
| 619 | } |
| 620 | |
| 621 | TEST(pthread, pthread_attr_setstacksize) { |
| 622 | pthread_attr_t attributes; |
| 623 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 624 | |
| 625 | // Get the default stack size. |
| 626 | size_t default_stack_size; |
| 627 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size)); |
| 628 | |
| 629 | // Too small. |
| 630 | ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128)); |
| 631 | size_t stack_size; |
| 632 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 633 | ASSERT_EQ(default_stack_size, stack_size); |
| 634 | ASSERT_GE(GetActualStackSize(attributes), default_stack_size); |
| 635 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 636 | // 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] | 637 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024)); |
| 638 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 639 | ASSERT_EQ(32*1024U, stack_size); |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 640 | ASSERT_GE(GetActualStackSize(attributes), 32*1024U); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 641 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 642 | // Large enough but not aligned; will be rounded up by pthread_create. |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 643 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1)); |
| 644 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 645 | ASSERT_EQ(32*1024U + 1, stack_size); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 646 | #if defined(__BIONIC__) |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 647 | ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 648 | #else // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 649 | // glibc rounds down, in violation of POSIX. They document this in their BUGS section. |
| 650 | ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 651 | #endif // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 652 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 653 | |
| 654 | TEST(pthread, pthread_rwlock_smoke) { |
| 655 | pthread_rwlock_t l; |
| 656 | ASSERT_EQ(0, pthread_rwlock_init(&l, NULL)); |
| 657 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 658 | // Single read lock |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 659 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 660 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 661 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 662 | // Multiple read lock |
| 663 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 664 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 665 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 666 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 667 | |
| 668 | // Write lock |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 669 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 670 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 671 | |
| 672 | // Try writer lock |
| 673 | ASSERT_EQ(0, pthread_rwlock_trywrlock(&l)); |
| 674 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 675 | ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l)); |
| 676 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 677 | |
| 678 | // Try reader lock |
| 679 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 680 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 681 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 682 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 683 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 684 | |
| 685 | // Try writer lock after unlock |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 686 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 687 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 688 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 689 | #ifdef __BIONIC__ |
| 690 | // EDEADLK in "read after write" |
| 691 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 692 | ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l)); |
| 693 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 694 | |
| 695 | // EDEADLK in "write after write" |
| 696 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 697 | ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l)); |
| 698 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 699 | #endif |
| 700 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 701 | ASSERT_EQ(0, pthread_rwlock_destroy(&l)); |
| 702 | } |
| 703 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 704 | struct RwlockWakeupHelperArg { |
| 705 | pthread_rwlock_t lock; |
| 706 | enum Progress { |
| 707 | LOCK_INITIALIZED, |
| 708 | LOCK_WAITING, |
| 709 | LOCK_RELEASED, |
| 710 | LOCK_ACCESSED |
| 711 | }; |
| 712 | std::atomic<Progress> progress; |
| 713 | }; |
| 714 | |
| 715 | static void pthread_rwlock_reader_wakeup_writer_helper(RwlockWakeupHelperArg* arg) { |
| 716 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress); |
| 717 | arg->progress = RwlockWakeupHelperArg::LOCK_WAITING; |
| 718 | |
| 719 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&arg->lock)); |
| 720 | ASSERT_EQ(0, pthread_rwlock_wrlock(&arg->lock)); |
| 721 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress); |
| 722 | ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock)); |
| 723 | |
| 724 | arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED; |
| 725 | } |
| 726 | |
| 727 | TEST(pthread, pthread_rwlock_reader_wakeup_writer) { |
| 728 | RwlockWakeupHelperArg wakeup_arg; |
| 729 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL)); |
| 730 | ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock)); |
| 731 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
| 732 | |
| 733 | pthread_t thread; |
| 734 | ASSERT_EQ(0, pthread_create(&thread, NULL, |
| 735 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_reader_wakeup_writer_helper), &wakeup_arg)); |
| 736 | sleep(1); |
| 737 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 738 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED; |
| 739 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 740 | |
| 741 | ASSERT_EQ(0, pthread_join(thread, NULL)); |
| 742 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress); |
| 743 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 744 | } |
| 745 | |
| 746 | static void pthread_rwlock_writer_wakeup_reader_helper(RwlockWakeupHelperArg* arg) { |
| 747 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress); |
| 748 | arg->progress = RwlockWakeupHelperArg::LOCK_WAITING; |
| 749 | |
| 750 | ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&arg->lock)); |
| 751 | ASSERT_EQ(0, pthread_rwlock_rdlock(&arg->lock)); |
| 752 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress); |
| 753 | ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock)); |
| 754 | |
| 755 | arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED; |
| 756 | } |
| 757 | |
| 758 | TEST(pthread, pthread_rwlock_writer_wakeup_reader) { |
| 759 | RwlockWakeupHelperArg wakeup_arg; |
| 760 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL)); |
| 761 | ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock)); |
| 762 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
| 763 | |
| 764 | pthread_t thread; |
| 765 | ASSERT_EQ(0, pthread_create(&thread, NULL, |
| 766 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_writer_wakeup_reader_helper), &wakeup_arg)); |
| 767 | sleep(1); |
| 768 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 769 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED; |
| 770 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 771 | |
| 772 | ASSERT_EQ(0, pthread_join(thread, NULL)); |
| 773 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress); |
| 774 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 775 | } |
| 776 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 777 | static int g_once_fn_call_count = 0; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 778 | static void OnceFn() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 779 | ++g_once_fn_call_count; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | TEST(pthread, pthread_once_smoke) { |
| 783 | pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 784 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
| 785 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 786 | ASSERT_EQ(1, g_once_fn_call_count); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Elliott Hughes | 3694ec6 | 2014-05-14 11:46:08 -0700 | [diff] [blame] | 789 | static std::string pthread_once_1934122_result = ""; |
| 790 | |
| 791 | static void Routine2() { |
| 792 | pthread_once_1934122_result += "2"; |
| 793 | } |
| 794 | |
| 795 | static void Routine1() { |
| 796 | pthread_once_t once_control_2 = PTHREAD_ONCE_INIT; |
| 797 | pthread_once_1934122_result += "1"; |
| 798 | pthread_once(&once_control_2, &Routine2); |
| 799 | } |
| 800 | |
| 801 | TEST(pthread, pthread_once_1934122) { |
| 802 | // Very old versions of Android couldn't call pthread_once from a |
| 803 | // pthread_once init routine. http://b/1934122. |
| 804 | pthread_once_t once_control_1 = PTHREAD_ONCE_INIT; |
| 805 | ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1)); |
| 806 | ASSERT_EQ("12", pthread_once_1934122_result); |
| 807 | } |
| 808 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 809 | static int g_atfork_prepare_calls = 0; |
| 810 | static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; } |
| 811 | static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; } |
| 812 | static int g_atfork_parent_calls = 0; |
| 813 | static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; } |
| 814 | static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; } |
| 815 | static int g_atfork_child_calls = 0; |
| 816 | static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; } |
| 817 | static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 818 | |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 819 | TEST(pthread, pthread_atfork_smoke) { |
Dmitriy Ivanov | 0864d8b | 2014-11-20 21:21:26 -0800 | [diff] [blame] | 820 | test_isolated([] { |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 821 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); |
| 822 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 823 | |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 824 | int pid = fork(); |
| 825 | ASSERT_NE(-1, pid) << strerror(errno); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 826 | |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 827 | // Child and parent calls are made in the order they were registered. |
| 828 | if (pid == 0) { |
| 829 | ASSERT_EQ(0x12, g_atfork_child_calls); |
| 830 | _exit(0); |
| 831 | } |
| 832 | ASSERT_EQ(0x12, g_atfork_parent_calls); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 833 | |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 834 | // Prepare calls are made in the reverse order. |
| 835 | ASSERT_EQ(0x21, g_atfork_prepare_calls); |
| 836 | }); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | TEST(pthread, pthread_attr_getscope) { |
| 840 | pthread_attr_t attr; |
| 841 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 842 | |
| 843 | int scope; |
| 844 | ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope)); |
| 845 | ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope); |
| 846 | } |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 847 | |
| 848 | TEST(pthread, pthread_condattr_init) { |
| 849 | pthread_condattr_t attr; |
| 850 | pthread_condattr_init(&attr); |
| 851 | |
| 852 | clockid_t clock; |
| 853 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 854 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 855 | |
| 856 | int pshared; |
| 857 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 858 | ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); |
| 859 | } |
| 860 | |
| 861 | TEST(pthread, pthread_condattr_setclock) { |
| 862 | pthread_condattr_t attr; |
| 863 | pthread_condattr_init(&attr); |
| 864 | |
| 865 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME)); |
| 866 | clockid_t clock; |
| 867 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 868 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 869 | |
| 870 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 871 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 872 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 873 | |
| 874 | ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID)); |
| 875 | } |
| 876 | |
| 877 | TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) { |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 878 | #if defined(__BIONIC__) |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 879 | pthread_condattr_t attr; |
| 880 | pthread_condattr_init(&attr); |
| 881 | |
| 882 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 883 | ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); |
| 884 | |
| 885 | pthread_cond_t cond_var; |
| 886 | ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr)); |
| 887 | |
| 888 | ASSERT_EQ(0, pthread_cond_signal(&cond_var)); |
| 889 | ASSERT_EQ(0, pthread_cond_broadcast(&cond_var)); |
| 890 | |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 891 | 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] | 892 | clockid_t clock; |
| 893 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 894 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 895 | int pshared; |
| 896 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 897 | ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 898 | #else // !defined(__BIONIC__) |
| 899 | GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n"; |
| 900 | #endif // !defined(__BIONIC__) |
| 901 | } |
| 902 | |
| 903 | class pthread_CondWakeupTest : public ::testing::Test { |
| 904 | protected: |
| 905 | pthread_mutex_t mutex; |
| 906 | pthread_cond_t cond; |
| 907 | |
| 908 | enum Progress { |
| 909 | INITIALIZED, |
| 910 | WAITING, |
| 911 | SIGNALED, |
| 912 | FINISHED, |
| 913 | }; |
| 914 | std::atomic<Progress> progress; |
| 915 | pthread_t thread; |
| 916 | |
| 917 | protected: |
| 918 | virtual void SetUp() { |
| 919 | ASSERT_EQ(0, pthread_mutex_init(&mutex, NULL)); |
| 920 | ASSERT_EQ(0, pthread_cond_init(&cond, NULL)); |
| 921 | progress = INITIALIZED; |
| 922 | ASSERT_EQ(0, |
| 923 | pthread_create(&thread, NULL, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this)); |
| 924 | } |
| 925 | |
| 926 | virtual void TearDown() { |
| 927 | ASSERT_EQ(0, pthread_join(thread, NULL)); |
| 928 | ASSERT_EQ(FINISHED, progress); |
| 929 | ASSERT_EQ(0, pthread_cond_destroy(&cond)); |
| 930 | ASSERT_EQ(0, pthread_mutex_destroy(&mutex)); |
| 931 | } |
| 932 | |
| 933 | void SleepUntilProgress(Progress expected_progress) { |
| 934 | while (progress != expected_progress) { |
| 935 | usleep(5000); |
| 936 | } |
| 937 | usleep(5000); |
| 938 | } |
| 939 | |
| 940 | private: |
| 941 | static void WaitThreadFn(pthread_CondWakeupTest* test) { |
| 942 | ASSERT_EQ(0, pthread_mutex_lock(&test->mutex)); |
| 943 | test->progress = WAITING; |
| 944 | while (test->progress == WAITING) { |
| 945 | ASSERT_EQ(0, pthread_cond_wait(&test->cond, &test->mutex)); |
| 946 | } |
| 947 | ASSERT_EQ(SIGNALED, test->progress); |
| 948 | test->progress = FINISHED; |
| 949 | ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex)); |
| 950 | } |
| 951 | }; |
| 952 | |
| 953 | TEST_F(pthread_CondWakeupTest, signal) { |
| 954 | SleepUntilProgress(WAITING); |
| 955 | progress = SIGNALED; |
| 956 | pthread_cond_signal(&cond); |
| 957 | } |
| 958 | |
| 959 | TEST_F(pthread_CondWakeupTest, broadcast) { |
| 960 | SleepUntilProgress(WAITING); |
| 961 | progress = SIGNALED; |
| 962 | pthread_cond_broadcast(&cond); |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 963 | } |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 964 | |
| 965 | TEST(pthread, pthread_mutex_timedlock) { |
| 966 | pthread_mutex_t m; |
| 967 | ASSERT_EQ(0, pthread_mutex_init(&m, NULL)); |
| 968 | |
| 969 | // If the mutex is already locked, pthread_mutex_timedlock should time out. |
| 970 | ASSERT_EQ(0, pthread_mutex_lock(&m)); |
| 971 | |
| 972 | timespec ts; |
| 973 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 974 | ts.tv_nsec += 1; |
| 975 | ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts)); |
| 976 | |
| 977 | // If the mutex is unlocked, pthread_mutex_timedlock should succeed. |
| 978 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 979 | |
| 980 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 981 | ts.tv_nsec += 1; |
| 982 | ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts)); |
| 983 | |
| 984 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 985 | ASSERT_EQ(0, pthread_mutex_destroy(&m)); |
| 986 | } |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 987 | |
| 988 | TEST(pthread, pthread_attr_getstack__main_thread) { |
| 989 | // This test is only meaningful for the main thread, so make sure we're running on it! |
| 990 | ASSERT_EQ(getpid(), syscall(__NR_gettid)); |
| 991 | |
| 992 | // Get the main thread's attributes. |
| 993 | pthread_attr_t attributes; |
| 994 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 995 | |
| 996 | // Check that we correctly report that the main thread has no guard page. |
| 997 | size_t guard_size; |
| 998 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 999 | ASSERT_EQ(0U, guard_size); // The main thread has no guard page. |
| 1000 | |
| 1001 | // Get the stack base and the stack size (both ways). |
| 1002 | void* stack_base; |
| 1003 | size_t stack_size; |
| 1004 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1005 | size_t stack_size2; |
| 1006 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1007 | |
| 1008 | // The two methods of asking for the stack size should agree. |
| 1009 | EXPECT_EQ(stack_size, stack_size2); |
| 1010 | |
| 1011 | // What does /proc/self/maps' [stack] line say? |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1012 | void* maps_stack_hi = NULL; |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1013 | FILE* fp = fopen("/proc/self/maps", "r"); |
| 1014 | ASSERT_TRUE(fp != NULL); |
| 1015 | char line[BUFSIZ]; |
| 1016 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 1017 | uintptr_t lo, hi; |
| 1018 | char name[10]; |
| 1019 | sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name); |
| 1020 | if (strcmp(name, "[stack]") == 0) { |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1021 | maps_stack_hi = reinterpret_cast<void*>(hi); |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1022 | break; |
| 1023 | } |
| 1024 | } |
| 1025 | fclose(fp); |
| 1026 | |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1027 | // The stack size should correspond to RLIMIT_STACK. |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1028 | rlimit rl; |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1029 | ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl)); |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1030 | uint64_t original_rlim_cur = rl.rlim_cur; |
| 1031 | #if defined(__BIONIC__) |
| 1032 | if (rl.rlim_cur == RLIM_INFINITY) { |
| 1033 | rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB. |
| 1034 | } |
| 1035 | #endif |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1036 | EXPECT_EQ(rl.rlim_cur, stack_size); |
| 1037 | |
Dmitriy Ivanov | d9ff722 | 2014-09-08 16:22:22 -0700 | [diff] [blame] | 1038 | auto guard = make_scope_guard([&rl, original_rlim_cur]() { |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1039 | rl.rlim_cur = original_rlim_cur; |
| 1040 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1041 | }); |
| 1042 | |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1043 | // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size. |
| 1044 | // Remember that the stack grows down (and is mapped in on demand), so the low address of the |
| 1045 | // region isn't very interesting. |
| 1046 | EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size); |
| 1047 | |
| 1048 | // |
| 1049 | // What if RLIMIT_STACK is smaller than the stack's current extent? |
| 1050 | // |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1051 | rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already. |
| 1052 | rl.rlim_max = RLIM_INFINITY; |
| 1053 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1054 | |
| 1055 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1056 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1057 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1058 | |
| 1059 | EXPECT_EQ(stack_size, stack_size2); |
| 1060 | ASSERT_EQ(1024U, stack_size); |
| 1061 | |
| 1062 | // |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1063 | // What if RLIMIT_STACK isn't a whole number of pages? |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1064 | // |
| 1065 | rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages. |
| 1066 | rl.rlim_max = RLIM_INFINITY; |
| 1067 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1068 | |
| 1069 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1070 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1071 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1072 | |
| 1073 | EXPECT_EQ(stack_size, stack_size2); |
| 1074 | ASSERT_EQ(6666U, stack_size); |
| 1075 | } |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1076 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 1077 | static void pthread_attr_getstack_18908062_helper(void*) { |
| 1078 | char local_variable; |
| 1079 | pthread_attr_t attributes; |
| 1080 | pthread_getattr_np(pthread_self(), &attributes); |
| 1081 | void* stack_base; |
| 1082 | size_t stack_size; |
| 1083 | pthread_attr_getstack(&attributes, &stack_base, &stack_size); |
| 1084 | |
| 1085 | // Test whether &local_variable is in [stack_base, stack_base + stack_size). |
| 1086 | ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable); |
| 1087 | ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size); |
| 1088 | } |
| 1089 | |
| 1090 | // Check whether something on stack is in the range of |
| 1091 | // [stack_base, stack_base + stack_size). see b/18908062. |
| 1092 | TEST(pthread, pthread_attr_getstack_18908062) { |
| 1093 | pthread_t t; |
| 1094 | ASSERT_EQ(0, pthread_create(&t, NULL, |
| 1095 | reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper), |
| 1096 | NULL)); |
| 1097 | pthread_join(t, NULL); |
| 1098 | } |
| 1099 | |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1100 | #if defined(__BIONIC__) |
| 1101 | static void* pthread_gettid_np_helper(void* arg) { |
| 1102 | *reinterpret_cast<pid_t*>(arg) = gettid(); |
| 1103 | return NULL; |
| 1104 | } |
| 1105 | #endif |
| 1106 | |
| 1107 | TEST(pthread, pthread_gettid_np) { |
| 1108 | #if defined(__BIONIC__) |
| 1109 | ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self())); |
| 1110 | |
| 1111 | pid_t t_gettid_result; |
| 1112 | pthread_t t; |
| 1113 | pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result); |
| 1114 | |
| 1115 | pid_t t_pthread_gettid_np_result = pthread_gettid_np(t); |
| 1116 | |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1117 | pthread_join(t, NULL); |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1118 | |
| 1119 | ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result); |
| 1120 | #else |
| 1121 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 1122 | #endif |
| 1123 | } |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1124 | |
| 1125 | static size_t cleanup_counter = 0; |
| 1126 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1127 | static void AbortCleanupRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1128 | abort(); |
| 1129 | } |
| 1130 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1131 | static void CountCleanupRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1132 | ++cleanup_counter; |
| 1133 | } |
| 1134 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1135 | static void PthreadCleanupTester() { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1136 | pthread_cleanup_push(CountCleanupRoutine, NULL); |
| 1137 | pthread_cleanup_push(CountCleanupRoutine, NULL); |
| 1138 | pthread_cleanup_push(AbortCleanupRoutine, NULL); |
| 1139 | |
| 1140 | pthread_cleanup_pop(0); // Pop the abort without executing it. |
| 1141 | pthread_cleanup_pop(1); // Pop one count while executing it. |
| 1142 | ASSERT_EQ(1U, cleanup_counter); |
| 1143 | // Exit while the other count is still on the cleanup stack. |
| 1144 | pthread_exit(NULL); |
| 1145 | |
| 1146 | // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced. |
| 1147 | pthread_cleanup_pop(0); |
| 1148 | } |
| 1149 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1150 | static void* PthreadCleanupStartRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1151 | PthreadCleanupTester(); |
| 1152 | return NULL; |
| 1153 | } |
| 1154 | |
| 1155 | TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) { |
| 1156 | pthread_t t; |
| 1157 | ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL)); |
| 1158 | pthread_join(t, NULL); |
| 1159 | ASSERT_EQ(2U, cleanup_counter); |
| 1160 | } |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1161 | |
| 1162 | TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) { |
| 1163 | ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT); |
| 1164 | } |
| 1165 | |
| 1166 | TEST(pthread, pthread_mutexattr_gettype) { |
| 1167 | pthread_mutexattr_t attr; |
| 1168 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 1169 | |
| 1170 | int attr_type; |
| 1171 | |
| 1172 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)); |
| 1173 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 1174 | ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type); |
| 1175 | |
| 1176 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)); |
| 1177 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 1178 | ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type); |
| 1179 | |
| 1180 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)); |
| 1181 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 1182 | ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type); |
| 1183 | } |
| 1184 | |
| 1185 | TEST(pthread, pthread_mutex_lock_NORMAL) { |
| 1186 | pthread_mutexattr_t attr; |
| 1187 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 1188 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)); |
| 1189 | |
| 1190 | pthread_mutex_t lock; |
| 1191 | ASSERT_EQ(0, pthread_mutex_init(&lock, &attr)); |
| 1192 | |
| 1193 | ASSERT_EQ(0, pthread_mutex_lock(&lock)); |
| 1194 | ASSERT_EQ(0, pthread_mutex_unlock(&lock)); |
| 1195 | ASSERT_EQ(0, pthread_mutex_destroy(&lock)); |
| 1196 | } |
| 1197 | |
| 1198 | TEST(pthread, pthread_mutex_lock_ERRORCHECK) { |
| 1199 | pthread_mutexattr_t attr; |
| 1200 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 1201 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)); |
| 1202 | |
| 1203 | pthread_mutex_t lock; |
| 1204 | ASSERT_EQ(0, pthread_mutex_init(&lock, &attr)); |
| 1205 | |
| 1206 | ASSERT_EQ(0, pthread_mutex_lock(&lock)); |
| 1207 | ASSERT_EQ(EDEADLK, pthread_mutex_lock(&lock)); |
| 1208 | ASSERT_EQ(0, pthread_mutex_unlock(&lock)); |
| 1209 | ASSERT_EQ(0, pthread_mutex_trylock(&lock)); |
| 1210 | ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock)); |
| 1211 | ASSERT_EQ(0, pthread_mutex_unlock(&lock)); |
| 1212 | ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock)); |
| 1213 | ASSERT_EQ(0, pthread_mutex_destroy(&lock)); |
| 1214 | } |
| 1215 | |
| 1216 | TEST(pthread, pthread_mutex_lock_RECURSIVE) { |
| 1217 | pthread_mutexattr_t attr; |
| 1218 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 1219 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)); |
| 1220 | |
| 1221 | pthread_mutex_t lock; |
| 1222 | ASSERT_EQ(0, pthread_mutex_init(&lock, &attr)); |
| 1223 | |
| 1224 | ASSERT_EQ(0, pthread_mutex_lock(&lock)); |
| 1225 | ASSERT_EQ(0, pthread_mutex_lock(&lock)); |
| 1226 | ASSERT_EQ(0, pthread_mutex_unlock(&lock)); |
| 1227 | ASSERT_EQ(0, pthread_mutex_unlock(&lock)); |
| 1228 | ASSERT_EQ(0, pthread_mutex_trylock(&lock)); |
| 1229 | ASSERT_EQ(0, pthread_mutex_unlock(&lock)); |
| 1230 | ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock)); |
| 1231 | ASSERT_EQ(0, pthread_mutex_destroy(&lock)); |
| 1232 | } |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 1233 | |
| 1234 | TEST(pthread, pthread_mutex_owner_tid_limit) { |
| 1235 | FILE* fp = fopen("/proc/sys/kernel/pid_max", "r"); |
| 1236 | ASSERT_TRUE(fp != NULL); |
| 1237 | long pid_max; |
| 1238 | ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max)); |
| 1239 | fclose(fp); |
| 1240 | // Current pthread_mutex uses 16 bits to represent owner tid. |
| 1241 | // Change the implementation if we need to support higher value than 65535. |
| 1242 | ASSERT_LE(pid_max, 65536); |
| 1243 | } |