blob: d3b53325b2c2a4acc94aa9bfefa1bb1cb8781758 [file] [log] [blame]
Elliott Hughesbfeab1b2012-09-05 17:47:37 -07001/*
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 Cui9df70402014-11-05 18:01:01 -080019#include "private/ScopeGuard.h"
20#include "BionicDeathTest.h"
21#include "ScopedSignalHandler.h"
22
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070023#include <errno.h>
Elliott Hughes5b9310e2013-10-02 16:59:05 -070024#include <inttypes.h>
Elliott Hughesb95cf0d2013-07-15 14:51:07 -070025#include <limits.h>
Elliott Hughes04620a32014-03-07 17:59:05 -080026#include <malloc.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070027#include <pthread.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080028#include <signal.h>
Yabin Cui140f3672015-02-03 10:32:00 -080029#include <stdio.h>
Elliott Hughes70b24b12013-11-15 11:51:07 -080030#include <sys/mman.h>
Elliott Hughes57b7a612014-08-25 17:26:50 -070031#include <sys/syscall.h>
Narayan Kamath51e6cb32014-03-03 15:38:51 +000032#include <time.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070033#include <unistd.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070034
Yabin Cui08ee8d22015-02-11 17:04:36 -080035#include <atomic>
36
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070037TEST(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 Hughes4d014e12012-09-07 16:47:54 -070044
Dan Albertc4bcc752014-09-30 11:48:24 -070045TEST(pthread, pthread_keys_max) {
Yabin Cui6c238f22014-12-11 20:50:41 -080046 // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX.
47 ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070048}
Elliott Hughes718a5b52014-01-28 17:02:03 -080049
Yabin Cui6c238f22014-12-11 20:50:41 -080050TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) {
Dan Albertc4bcc752014-09-30 11:48:24 -070051 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
Yabin Cui6c238f22014-12-11 20:50:41 -080052 ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070053}
54
55TEST(pthread, pthread_key_many_distinct) {
Yabin Cui6c238f22014-12-11 20:50:41 -080056 // 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 Albertc4bcc752014-09-30 11:48:24 -070059 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 Cui6c238f22014-12-11 20:50:41 -080084TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000085 std::vector<pthread_key_t> keys;
Dan Albertc4bcc752014-09-30 11:48:24 -070086 int rv = 0;
Yabin Cui6c238f22014-12-11 20:50:41 -080087
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 Hughes44b53ad2013-02-11 20:18:47 +000091 pthread_key_t key;
Dan Albertc4bcc752014-09-30 11:48:24 -070092 rv = pthread_key_create(&key, NULL);
93 if (rv == EAGAIN) {
94 break;
95 }
96 EXPECT_EQ(0, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +000097 keys.push_back(key);
98 }
99
Dan Albertc4bcc752014-09-30 11:48:24 -0700100 // Don't leak keys.
101 for (auto key : keys) {
102 EXPECT_EQ(0, pthread_key_delete(key));
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000103 }
Dan Albertc4bcc752014-09-30 11:48:24 -0700104 keys.clear();
105
106 // We should have eventually reached the maximum number of keys and received
107 // EAGAIN.
108 ASSERT_EQ(EAGAIN, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000109}
110
Elliott Hughesebb770f2014-06-25 13:46:46 -0700111TEST(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 Hughes40a52172014-07-30 14:48:10 -0700124TEST(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 Albert1d53ae22014-09-02 15:24:26 -0700146 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700147}
148
149static void* DirtyKeyFn(void* key) {
150 return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
151}
152
153TEST(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 Albert1d53ae22014-09-02 15:24:26 -0700174 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700175}
176
Elliott Hughes4d014e12012-09-07 16:47:54 -0700177static void* IdFn(void* arg) {
178 return arg;
179}
180
Yabin Cui63481602014-12-01 17:41:04 -0800181class SpinFunctionHelper {
182 public:
183 SpinFunctionHelper() {
184 SpinFunctionHelper::spin_flag_ = true;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400185 }
Yabin Cui63481602014-12-01 17:41:04 -0800186 ~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.
208volatile bool SpinFunctionHelper::spin_flag_ = false;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400209
Elliott Hughes4d014e12012-09-07 16:47:54 -0700210static void* JoinFn(void* arg) {
211 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
212}
213
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400214static 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 Hughes9d23e042013-02-15 19:21:51 -0800223static void MakeDeadThread(pthread_t& t) {
224 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
Elliott Hughes34c987a2014-09-22 16:01:26 -0700225 ASSERT_EQ(0, pthread_join(t, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800226}
227
Elliott Hughes4d014e12012-09-07 16:47:54 -0700228TEST(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 Hughes3e898472013-02-12 16:40:24 +0000239TEST(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 Hughes4d014e12012-09-07 16:47:54 -0700248TEST(pthread, pthread_no_join_after_detach) {
Yabin Cui63481602014-12-01 17:41:04 -0800249 SpinFunctionHelper spinhelper;
250
Elliott Hughes4d014e12012-09-07 16:47:54 -0700251 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800252 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700253
254 // After a pthread_detach...
255 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400256 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700257
258 // ...pthread_join should fail.
Elliott Hughes34c987a2014-09-22 16:01:26 -0700259 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700260}
261
262TEST(pthread, pthread_no_op_detach_after_join) {
Yabin Cui63481602014-12-01 17:41:04 -0800263 SpinFunctionHelper spinhelper;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400264
Elliott Hughes4d014e12012-09-07 16:47:54 -0700265 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800266 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700267
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 Melnikov10ce9692012-10-26 14:06:43 +0400272 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700273
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400274 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
275 ASSERT_EQ(0, pthread_detach(t1));
276 AssertDetached(t1, false);
277
Yabin Cui63481602014-12-01 17:41:04 -0800278 spinhelper.UnSpin();
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400279
280 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
Elliott Hughes4d014e12012-09-07 16:47:54 -0700281 void* join_result;
282 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700283 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700284}
Elliott Hughes14f19592012-10-29 10:19:44 -0700285
286TEST(pthread, pthread_join_self) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700287 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL));
Elliott Hughes14f19592012-10-29 10:19:44 -0700288}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700289
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800290struct TestBug37410 {
291 pthread_t main_thread;
292 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700293
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800294 static void main() {
295 TestBug37410 data;
296 data.main_thread = pthread_self();
297 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
298 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
299
300 pthread_t t;
301 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
302
303 // Wait for the thread to be running...
304 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
305 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
306
307 // ...and exit.
308 pthread_exit(NULL);
309 }
310
311 private:
312 static void* thread_fn(void* arg) {
313 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
314
315 // Let the main thread know we're running.
316 pthread_mutex_unlock(&data->mutex);
317
318 // And wait for the main thread to exit.
319 pthread_join(data->main_thread, NULL);
320
321 return NULL;
322 }
323};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700324
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800325// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
326// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800327
328class pthread_DeathTest : public BionicDeathTest {};
329
330TEST_F(pthread_DeathTest, pthread_bug_37410) {
Elliott Hughes4f251be2012-11-01 16:33:29 -0700331 // http://code.google.com/p/android/issues/detail?id=37410
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800332 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700333}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800334
335static void* SignalHandlerFn(void* arg) {
336 sigset_t wait_set;
337 sigfillset(&wait_set);
338 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
339}
340
341TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700342 // Check that SIGUSR1 isn't blocked.
343 sigset_t original_set;
344 sigemptyset(&original_set);
345 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
346 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
347
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800348 // Block SIGUSR1.
349 sigset_t set;
350 sigemptyset(&set);
351 sigaddset(&set, SIGUSR1);
352 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
353
Elliott Hughes19e62322013-10-15 11:23:57 -0700354 // Check that SIGUSR1 is blocked.
355 sigset_t final_set;
356 sigemptyset(&final_set);
357 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
358 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
359 // ...and that sigprocmask agrees with pthread_sigmask.
360 sigemptyset(&final_set);
361 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
362 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
363
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800364 // Spawn a thread that calls sigwait and tells us what it received.
365 pthread_t signal_thread;
366 int received_signal = -1;
367 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
368
369 // Send that thread SIGUSR1.
370 pthread_kill(signal_thread, SIGUSR1);
371
372 // See what it got.
373 void* join_result;
374 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
375 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700376 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700377
378 // Restore the original signal mask.
379 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800380}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800381
Elliott Hughes3e898472013-02-12 16:40:24 +0000382TEST(pthread, pthread_setname_np__too_long) {
383 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
384}
385
386TEST(pthread, pthread_setname_np__self) {
387 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
388}
389
390TEST(pthread, pthread_setname_np__other) {
Yabin Cui63481602014-12-01 17:41:04 -0800391 SpinFunctionHelper spinhelper;
392
Elliott Hughesed29e852014-10-27 12:01:51 -0700393 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800394 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughesed29e852014-10-27 12:01:51 -0700395 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000396}
397
398TEST(pthread, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800399 pthread_t dead_thread;
400 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000401
402 // Call pthread_setname_np after thread has already exited.
Elliott Hughes68d98d82014-11-12 21:03:26 -0800403 ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000404}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800405
406TEST(pthread, pthread_kill__0) {
407 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
408 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
409}
410
411TEST(pthread, pthread_kill__invalid_signal) {
412 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
413}
414
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800415static void pthread_kill__in_signal_handler_helper(int signal_number) {
416 static int count = 0;
417 ASSERT_EQ(SIGALRM, signal_number);
418 if (++count == 1) {
419 // Can we call pthread_kill from a signal handler?
420 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
421 }
422}
423
424TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800425 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800426 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
427}
428
Elliott Hughes9d23e042013-02-15 19:21:51 -0800429TEST(pthread, pthread_detach__no_such_thread) {
430 pthread_t dead_thread;
431 MakeDeadThread(dead_thread);
432
433 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
434}
435
Yabin Cui19e246d2014-12-18 14:22:09 -0800436TEST(pthread, pthread_detach_no_leak) {
Christopher Ferrise3809602014-08-06 14:15:01 -0700437 size_t initial_bytes = 0;
438 // Run this loop more than once since the first loop causes some memory
439 // to be allocated permenantly. Run an extra loop to help catch any subtle
440 // memory leaks.
441 for (size_t loop = 0; loop < 3; loop++) {
442 // Set the initial bytes on the second loop since the memory in use
443 // should have stabilized.
444 if (loop == 1) {
445 initial_bytes = mallinfo().uordblks;
446 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800447
Christopher Ferrise3809602014-08-06 14:15:01 -0700448 pthread_attr_t attr;
449 ASSERT_EQ(0, pthread_attr_init(&attr));
450 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
Elliott Hughes04620a32014-03-07 17:59:05 -0800451
Christopher Ferrise3809602014-08-06 14:15:01 -0700452 std::vector<pthread_t> threads;
453 for (size_t i = 0; i < 32; ++i) {
454 pthread_t t;
455 ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL));
456 threads.push_back(t);
457 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800458
Christopher Ferrise3809602014-08-06 14:15:01 -0700459 sleep(1);
Elliott Hughes04620a32014-03-07 17:59:05 -0800460
Christopher Ferrise3809602014-08-06 14:15:01 -0700461 for (size_t i = 0; i < 32; ++i) {
462 ASSERT_EQ(0, pthread_detach(threads[i])) << i;
463 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800464 }
465
466 size_t final_bytes = mallinfo().uordblks;
Elliott Hughes04620a32014-03-07 17:59:05 -0800467 int leaked_bytes = (final_bytes - initial_bytes);
468
Yabin Cui19e246d2014-12-18 14:22:09 -0800469 ASSERT_EQ(0, leaked_bytes);
Elliott Hughes04620a32014-03-07 17:59:05 -0800470}
471
Jeff Hao9b06cc32013-08-15 14:51:16 -0700472TEST(pthread, pthread_getcpuclockid__clock_gettime) {
Yabin Cui63481602014-12-01 17:41:04 -0800473 SpinFunctionHelper spinhelper;
474
Jeff Hao9b06cc32013-08-15 14:51:16 -0700475 pthread_t t;
Yabin Cui63481602014-12-01 17:41:04 -0800476 ASSERT_EQ(0, pthread_create(&t, NULL, spinhelper.GetFunction(), NULL));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700477
478 clockid_t c;
479 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
480 timespec ts;
481 ASSERT_EQ(0, clock_gettime(c, &ts));
482}
483
Elliott Hughes9d23e042013-02-15 19:21:51 -0800484TEST(pthread, pthread_getcpuclockid__no_such_thread) {
485 pthread_t dead_thread;
486 MakeDeadThread(dead_thread);
487
488 clockid_t c;
489 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
490}
491
492TEST(pthread, pthread_getschedparam__no_such_thread) {
493 pthread_t dead_thread;
494 MakeDeadThread(dead_thread);
495
496 int policy;
497 sched_param param;
498 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
499}
500
501TEST(pthread, pthread_setschedparam__no_such_thread) {
502 pthread_t dead_thread;
503 MakeDeadThread(dead_thread);
504
505 int policy = 0;
506 sched_param param;
507 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
508}
509
510TEST(pthread, pthread_join__no_such_thread) {
511 pthread_t dead_thread;
512 MakeDeadThread(dead_thread);
513
Elliott Hughes34c987a2014-09-22 16:01:26 -0700514 ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800515}
516
517TEST(pthread, pthread_kill__no_such_thread) {
518 pthread_t dead_thread;
519 MakeDeadThread(dead_thread);
520
521 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
522}
msg5550f020d12013-06-06 14:59:28 -0400523
524TEST(pthread, pthread_join__multijoin) {
Yabin Cui63481602014-12-01 17:41:04 -0800525 SpinFunctionHelper spinhelper;
msg5550f020d12013-06-06 14:59:28 -0400526
527 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800528 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
msg5550f020d12013-06-06 14:59:28 -0400529
530 pthread_t t2;
531 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
532
533 sleep(1); // (Give t2 a chance to call pthread_join.)
534
535 // Multiple joins to the same thread should fail.
536 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
537
Yabin Cui63481602014-12-01 17:41:04 -0800538 spinhelper.UnSpin();
msg5550f020d12013-06-06 14:59:28 -0400539
540 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
541 void* join_result;
542 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700543 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400544}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700545
Elliott Hughes70b24b12013-11-15 11:51:07 -0800546TEST(pthread, pthread_join__race) {
547 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
548 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
549 for (size_t i = 0; i < 1024; ++i) {
550 size_t stack_size = 64*1024;
551 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
552
553 pthread_attr_t a;
554 pthread_attr_init(&a);
555 pthread_attr_setstack(&a, stack, stack_size);
556
557 pthread_t t;
558 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
559 ASSERT_EQ(0, pthread_join(t, NULL));
560 ASSERT_EQ(0, munmap(stack, stack_size));
561 }
562}
563
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700564static void* GetActualGuardSizeFn(void* arg) {
565 pthread_attr_t attributes;
566 pthread_getattr_np(pthread_self(), &attributes);
567 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
568 return NULL;
569}
570
571static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
572 size_t result;
573 pthread_t t;
574 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700575 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700576 return result;
577}
578
579static void* GetActualStackSizeFn(void* arg) {
580 pthread_attr_t attributes;
581 pthread_getattr_np(pthread_self(), &attributes);
582 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
583 return NULL;
584}
585
586static size_t GetActualStackSize(const pthread_attr_t& attributes) {
587 size_t result;
588 pthread_t t;
589 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700590 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700591 return result;
592}
593
594TEST(pthread, pthread_attr_setguardsize) {
595 pthread_attr_t attributes;
596 ASSERT_EQ(0, pthread_attr_init(&attributes));
597
598 // Get the default guard size.
599 size_t default_guard_size;
600 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
601
602 // No such thing as too small: will be rounded up to one page by pthread_create.
603 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
604 size_t guard_size;
605 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
606 ASSERT_EQ(128U, guard_size);
607 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
608
609 // Large enough and a multiple of the page size.
610 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
611 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
612 ASSERT_EQ(32*1024U, guard_size);
613
614 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
615 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
616 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
617 ASSERT_EQ(32*1024U + 1, guard_size);
618}
619
620TEST(pthread, pthread_attr_setstacksize) {
621 pthread_attr_t attributes;
622 ASSERT_EQ(0, pthread_attr_init(&attributes));
623
624 // Get the default stack size.
625 size_t default_stack_size;
626 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
627
628 // Too small.
629 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
630 size_t stack_size;
631 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
632 ASSERT_EQ(default_stack_size, stack_size);
633 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
634
Yabin Cui917d3902015-01-08 12:32:42 -0800635 // Large enough and a multiple of the page size; may be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700636 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
637 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
638 ASSERT_EQ(32*1024U, stack_size);
Yabin Cui917d3902015-01-08 12:32:42 -0800639 ASSERT_GE(GetActualStackSize(attributes), 32*1024U);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700640
Yabin Cui917d3902015-01-08 12:32:42 -0800641 // Large enough but not aligned; will be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700642 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
643 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
644 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800645#if defined(__BIONIC__)
Yabin Cui917d3902015-01-08 12:32:42 -0800646 ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800647#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700648 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
649 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800650#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700651}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700652
653TEST(pthread, pthread_rwlock_smoke) {
654 pthread_rwlock_t l;
655 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
656
Calin Juravle76f352e2014-05-19 13:41:10 +0100657 // Single read lock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700658 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
659 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
660
Calin Juravle76f352e2014-05-19 13:41:10 +0100661 // Multiple read lock
662 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
663 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
664 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
665 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
666
667 // Write lock
Calin Juravle92687e42014-05-22 19:21:22 +0100668 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
669 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100670
671 // Try writer lock
672 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
673 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
674 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
675 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
676
677 // Try reader lock
678 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
679 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
680 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
681 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
682 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
683
684 // Try writer lock after unlock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700685 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
686 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
687
Calin Juravle76f352e2014-05-19 13:41:10 +0100688#ifdef __BIONIC__
689 // EDEADLK in "read after write"
690 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
691 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
692 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
693
694 // EDEADLK in "write after write"
695 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
696 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
697 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
698#endif
699
Elliott Hughesc3f11402013-10-30 14:40:09 -0700700 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
701}
702
Yabin Cui08ee8d22015-02-11 17:04:36 -0800703struct RwlockWakeupHelperArg {
704 pthread_rwlock_t lock;
705 enum Progress {
706 LOCK_INITIALIZED,
707 LOCK_WAITING,
708 LOCK_RELEASED,
709 LOCK_ACCESSED
710 };
711 std::atomic<Progress> progress;
712};
713
714static void pthread_rwlock_reader_wakeup_writer_helper(RwlockWakeupHelperArg* arg) {
715 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
716 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
717
718 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&arg->lock));
719 ASSERT_EQ(0, pthread_rwlock_wrlock(&arg->lock));
720 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
721 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
722
723 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
724}
725
726TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
727 RwlockWakeupHelperArg wakeup_arg;
728 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
729 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
730 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
731
732 pthread_t thread;
733 ASSERT_EQ(0, pthread_create(&thread, NULL,
734 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_reader_wakeup_writer_helper), &wakeup_arg));
735 sleep(1);
736 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
737 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
738 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
739
740 ASSERT_EQ(0, pthread_join(thread, NULL));
741 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
742 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
743}
744
745static void pthread_rwlock_writer_wakeup_reader_helper(RwlockWakeupHelperArg* arg) {
746 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
747 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
748
749 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&arg->lock));
750 ASSERT_EQ(0, pthread_rwlock_rdlock(&arg->lock));
751 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
752 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
753
754 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
755}
756
757TEST(pthread, pthread_rwlock_writer_wakeup_reader) {
758 RwlockWakeupHelperArg wakeup_arg;
759 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
760 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
761 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
762
763 pthread_t thread;
764 ASSERT_EQ(0, pthread_create(&thread, NULL,
765 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_writer_wakeup_reader_helper), &wakeup_arg));
766 sleep(1);
767 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
768 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
769 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
770
771 ASSERT_EQ(0, pthread_join(thread, NULL));
772 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
773 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
774}
775
Elliott Hughes1728b232014-05-14 10:02:03 -0700776static int g_once_fn_call_count = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700777static void OnceFn() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700778 ++g_once_fn_call_count;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700779}
780
781TEST(pthread, pthread_once_smoke) {
782 pthread_once_t once_control = PTHREAD_ONCE_INIT;
783 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
784 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
Elliott Hughes1728b232014-05-14 10:02:03 -0700785 ASSERT_EQ(1, g_once_fn_call_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700786}
787
Elliott Hughes3694ec62014-05-14 11:46:08 -0700788static std::string pthread_once_1934122_result = "";
789
790static void Routine2() {
791 pthread_once_1934122_result += "2";
792}
793
794static void Routine1() {
795 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
796 pthread_once_1934122_result += "1";
797 pthread_once(&once_control_2, &Routine2);
798}
799
800TEST(pthread, pthread_once_1934122) {
801 // Very old versions of Android couldn't call pthread_once from a
802 // pthread_once init routine. http://b/1934122.
803 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
804 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
805 ASSERT_EQ("12", pthread_once_1934122_result);
806}
807
Elliott Hughes1728b232014-05-14 10:02:03 -0700808static int g_atfork_prepare_calls = 0;
809static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; }
810static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; }
811static int g_atfork_parent_calls = 0;
812static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; }
813static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; }
814static int g_atfork_child_calls = 0;
815static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; }
816static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700817
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800818TEST(pthread, pthread_atfork_smoke) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700819 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
820 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700821
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700822 int pid = fork();
823 ASSERT_NE(-1, pid) << strerror(errno);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700824
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700825 // Child and parent calls are made in the order they were registered.
826 if (pid == 0) {
827 ASSERT_EQ(0x12, g_atfork_child_calls);
828 _exit(0);
829 }
830 ASSERT_EQ(0x12, g_atfork_parent_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700831
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700832 // Prepare calls are made in the reverse order.
833 ASSERT_EQ(0x21, g_atfork_prepare_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700834}
835
836TEST(pthread, pthread_attr_getscope) {
837 pthread_attr_t attr;
838 ASSERT_EQ(0, pthread_attr_init(&attr));
839
840 int scope;
841 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
842 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
843}
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000844
845TEST(pthread, pthread_condattr_init) {
846 pthread_condattr_t attr;
847 pthread_condattr_init(&attr);
848
849 clockid_t clock;
850 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
851 ASSERT_EQ(CLOCK_REALTIME, clock);
852
853 int pshared;
854 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
855 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
856}
857
858TEST(pthread, pthread_condattr_setclock) {
859 pthread_condattr_t attr;
860 pthread_condattr_init(&attr);
861
862 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
863 clockid_t clock;
864 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
865 ASSERT_EQ(CLOCK_REALTIME, clock);
866
867 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
868 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
869 ASSERT_EQ(CLOCK_MONOTONIC, clock);
870
871 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
872}
873
874TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
Yabin Cui32651b82015-03-13 20:30:00 -0700875#if defined(__BIONIC__)
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000876 pthread_condattr_t attr;
877 pthread_condattr_init(&attr);
878
879 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
880 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
881
882 pthread_cond_t cond_var;
883 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
884
885 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
886 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
887
Yabin Cui32651b82015-03-13 20:30:00 -0700888 attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000889 clockid_t clock;
890 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
891 ASSERT_EQ(CLOCK_MONOTONIC, clock);
892 int pshared;
893 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
894 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
Yabin Cui32651b82015-03-13 20:30:00 -0700895#else // !defined(__BIONIC__)
896 GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n";
897#endif // !defined(__BIONIC__)
898}
899
900class pthread_CondWakeupTest : public ::testing::Test {
901 protected:
902 pthread_mutex_t mutex;
903 pthread_cond_t cond;
904
905 enum Progress {
906 INITIALIZED,
907 WAITING,
908 SIGNALED,
909 FINISHED,
910 };
911 std::atomic<Progress> progress;
912 pthread_t thread;
913
914 protected:
915 virtual void SetUp() {
916 ASSERT_EQ(0, pthread_mutex_init(&mutex, NULL));
917 ASSERT_EQ(0, pthread_cond_init(&cond, NULL));
918 progress = INITIALIZED;
919 ASSERT_EQ(0,
920 pthread_create(&thread, NULL, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this));
921 }
922
923 virtual void TearDown() {
924 ASSERT_EQ(0, pthread_join(thread, NULL));
925 ASSERT_EQ(FINISHED, progress);
926 ASSERT_EQ(0, pthread_cond_destroy(&cond));
927 ASSERT_EQ(0, pthread_mutex_destroy(&mutex));
928 }
929
930 void SleepUntilProgress(Progress expected_progress) {
931 while (progress != expected_progress) {
932 usleep(5000);
933 }
934 usleep(5000);
935 }
936
937 private:
938 static void WaitThreadFn(pthread_CondWakeupTest* test) {
939 ASSERT_EQ(0, pthread_mutex_lock(&test->mutex));
940 test->progress = WAITING;
941 while (test->progress == WAITING) {
942 ASSERT_EQ(0, pthread_cond_wait(&test->cond, &test->mutex));
943 }
944 ASSERT_EQ(SIGNALED, test->progress);
945 test->progress = FINISHED;
946 ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex));
947 }
948};
949
950TEST_F(pthread_CondWakeupTest, signal) {
951 SleepUntilProgress(WAITING);
952 progress = SIGNALED;
953 pthread_cond_signal(&cond);
954}
955
956TEST_F(pthread_CondWakeupTest, broadcast) {
957 SleepUntilProgress(WAITING);
958 progress = SIGNALED;
959 pthread_cond_broadcast(&cond);
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000960}
Elliott Hughes0e714a52014-03-03 16:42:47 -0800961
962TEST(pthread, pthread_mutex_timedlock) {
963 pthread_mutex_t m;
964 ASSERT_EQ(0, pthread_mutex_init(&m, NULL));
965
966 // If the mutex is already locked, pthread_mutex_timedlock should time out.
967 ASSERT_EQ(0, pthread_mutex_lock(&m));
968
969 timespec ts;
970 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
971 ts.tv_nsec += 1;
972 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
973
974 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
975 ASSERT_EQ(0, pthread_mutex_unlock(&m));
976
977 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
978 ts.tv_nsec += 1;
979 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
980
981 ASSERT_EQ(0, pthread_mutex_unlock(&m));
982 ASSERT_EQ(0, pthread_mutex_destroy(&m));
983}
Elliott Hughes57b7a612014-08-25 17:26:50 -0700984
985TEST(pthread, pthread_attr_getstack__main_thread) {
986 // This test is only meaningful for the main thread, so make sure we're running on it!
987 ASSERT_EQ(getpid(), syscall(__NR_gettid));
988
989 // Get the main thread's attributes.
990 pthread_attr_t attributes;
991 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
992
993 // Check that we correctly report that the main thread has no guard page.
994 size_t guard_size;
995 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
996 ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
997
998 // Get the stack base and the stack size (both ways).
999 void* stack_base;
1000 size_t stack_size;
1001 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1002 size_t stack_size2;
1003 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1004
1005 // The two methods of asking for the stack size should agree.
1006 EXPECT_EQ(stack_size, stack_size2);
1007
1008 // What does /proc/self/maps' [stack] line say?
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001009 void* maps_stack_hi = NULL;
Elliott Hughes57b7a612014-08-25 17:26:50 -07001010 FILE* fp = fopen("/proc/self/maps", "r");
1011 ASSERT_TRUE(fp != NULL);
1012 char line[BUFSIZ];
1013 while (fgets(line, sizeof(line), fp) != NULL) {
1014 uintptr_t lo, hi;
1015 char name[10];
1016 sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name);
1017 if (strcmp(name, "[stack]") == 0) {
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001018 maps_stack_hi = reinterpret_cast<void*>(hi);
Elliott Hughes57b7a612014-08-25 17:26:50 -07001019 break;
1020 }
1021 }
1022 fclose(fp);
1023
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001024 // The stack size should correspond to RLIMIT_STACK.
Elliott Hughes57b7a612014-08-25 17:26:50 -07001025 rlimit rl;
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001026 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001027 uint64_t original_rlim_cur = rl.rlim_cur;
1028#if defined(__BIONIC__)
1029 if (rl.rlim_cur == RLIM_INFINITY) {
1030 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
1031 }
1032#endif
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001033 EXPECT_EQ(rl.rlim_cur, stack_size);
1034
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001035 auto guard = make_scope_guard([&rl, original_rlim_cur]() {
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001036 rl.rlim_cur = original_rlim_cur;
1037 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1038 });
1039
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001040 // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
1041 // Remember that the stack grows down (and is mapped in on demand), so the low address of the
1042 // region isn't very interesting.
1043 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
1044
1045 //
1046 // What if RLIMIT_STACK is smaller than the stack's current extent?
1047 //
Elliott Hughes57b7a612014-08-25 17:26:50 -07001048 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
1049 rl.rlim_max = RLIM_INFINITY;
1050 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1051
1052 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1053 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1054 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1055
1056 EXPECT_EQ(stack_size, stack_size2);
1057 ASSERT_EQ(1024U, stack_size);
1058
1059 //
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001060 // What if RLIMIT_STACK isn't a whole number of pages?
Elliott Hughes57b7a612014-08-25 17:26:50 -07001061 //
1062 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
1063 rl.rlim_max = RLIM_INFINITY;
1064 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1065
1066 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1067 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1068 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1069
1070 EXPECT_EQ(stack_size, stack_size2);
1071 ASSERT_EQ(6666U, stack_size);
1072}
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001073
Yabin Cui917d3902015-01-08 12:32:42 -08001074static void pthread_attr_getstack_18908062_helper(void*) {
1075 char local_variable;
1076 pthread_attr_t attributes;
1077 pthread_getattr_np(pthread_self(), &attributes);
1078 void* stack_base;
1079 size_t stack_size;
1080 pthread_attr_getstack(&attributes, &stack_base, &stack_size);
1081
1082 // Test whether &local_variable is in [stack_base, stack_base + stack_size).
1083 ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable);
1084 ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size);
1085}
1086
1087// Check whether something on stack is in the range of
1088// [stack_base, stack_base + stack_size). see b/18908062.
1089TEST(pthread, pthread_attr_getstack_18908062) {
1090 pthread_t t;
1091 ASSERT_EQ(0, pthread_create(&t, NULL,
1092 reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper),
1093 NULL));
1094 pthread_join(t, NULL);
1095}
1096
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001097#if defined(__BIONIC__)
1098static void* pthread_gettid_np_helper(void* arg) {
1099 *reinterpret_cast<pid_t*>(arg) = gettid();
1100 return NULL;
1101}
1102#endif
1103
1104TEST(pthread, pthread_gettid_np) {
1105#if defined(__BIONIC__)
1106 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
1107
1108 pid_t t_gettid_result;
1109 pthread_t t;
1110 pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
1111
1112 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
1113
Elliott Hughes34c987a2014-09-22 16:01:26 -07001114 pthread_join(t, NULL);
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001115
1116 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
1117#else
1118 GTEST_LOG_(INFO) << "This test does nothing.\n";
1119#endif
1120}
Elliott Hughes34c987a2014-09-22 16:01:26 -07001121
1122static size_t cleanup_counter = 0;
1123
Derek Xue41996952014-09-25 11:05:32 +01001124static void AbortCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001125 abort();
1126}
1127
Derek Xue41996952014-09-25 11:05:32 +01001128static void CountCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001129 ++cleanup_counter;
1130}
1131
Derek Xue41996952014-09-25 11:05:32 +01001132static void PthreadCleanupTester() {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001133 pthread_cleanup_push(CountCleanupRoutine, NULL);
1134 pthread_cleanup_push(CountCleanupRoutine, NULL);
1135 pthread_cleanup_push(AbortCleanupRoutine, NULL);
1136
1137 pthread_cleanup_pop(0); // Pop the abort without executing it.
1138 pthread_cleanup_pop(1); // Pop one count while executing it.
1139 ASSERT_EQ(1U, cleanup_counter);
1140 // Exit while the other count is still on the cleanup stack.
1141 pthread_exit(NULL);
1142
1143 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
1144 pthread_cleanup_pop(0);
1145}
1146
Derek Xue41996952014-09-25 11:05:32 +01001147static void* PthreadCleanupStartRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001148 PthreadCleanupTester();
1149 return NULL;
1150}
1151
1152TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
1153 pthread_t t;
1154 ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL));
1155 pthread_join(t, NULL);
1156 ASSERT_EQ(2U, cleanup_counter);
1157}
Derek Xue41996952014-09-25 11:05:32 +01001158
1159TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
1160 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
1161}
1162
1163TEST(pthread, pthread_mutexattr_gettype) {
1164 pthread_mutexattr_t attr;
1165 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1166
1167 int attr_type;
1168
1169 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1170 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1171 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
1172
1173 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1174 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1175 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
1176
1177 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1178 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1179 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
1180}
1181
1182TEST(pthread, pthread_mutex_lock_NORMAL) {
1183 pthread_mutexattr_t attr;
1184 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1185 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1186
1187 pthread_mutex_t lock;
1188 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1189
1190 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1191 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1192 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1193}
1194
1195TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
1196 pthread_mutexattr_t attr;
1197 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1198 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1199
1200 pthread_mutex_t lock;
1201 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1202
1203 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1204 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&lock));
1205 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1206 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1207 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock));
1208 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1209 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1210 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1211}
1212
1213TEST(pthread, pthread_mutex_lock_RECURSIVE) {
1214 pthread_mutexattr_t attr;
1215 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1216 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1217
1218 pthread_mutex_t lock;
1219 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1220
1221 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1222 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1223 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1224 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1225 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1226 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1227 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1228 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1229}
Yabin Cui140f3672015-02-03 10:32:00 -08001230
1231TEST(pthread, pthread_mutex_owner_tid_limit) {
1232 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
1233 ASSERT_TRUE(fp != NULL);
1234 long pid_max;
1235 ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max));
1236 fclose(fp);
1237 // Current pthread_mutex uses 16 bits to represent owner tid.
1238 // Change the implementation if we need to support higher value than 65535.
1239 ASSERT_LE(pid_max, 65536);
1240}