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