blob: e1bbc55cdb39650148b59b1928f2675659e1a66a [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"
Dmitriy Ivanov00e37812014-11-20 16:53:47 -080022#include "gtest_ex.h"
Yabin Cui9df70402014-11-05 18:01:01 -080023
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070024#include <errno.h>
Elliott Hughes5b9310e2013-10-02 16:59:05 -070025#include <inttypes.h>
Elliott Hughesb95cf0d2013-07-15 14:51:07 -070026#include <limits.h>
Elliott Hughes04620a32014-03-07 17:59:05 -080027#include <malloc.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070028#include <pthread.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080029#include <signal.h>
Yabin Cui140f3672015-02-03 10:32:00 -080030#include <stdio.h>
Elliott Hughes70b24b12013-11-15 11:51:07 -080031#include <sys/mman.h>
Elliott Hughes57b7a612014-08-25 17:26:50 -070032#include <sys/syscall.h>
Narayan Kamath51e6cb32014-03-03 15:38:51 +000033#include <time.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070034#include <unistd.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070035
Yabin Cui08ee8d22015-02-11 17:04:36 -080036#include <atomic>
37
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070038TEST(pthread, pthread_key_create) {
39 pthread_key_t key;
40 ASSERT_EQ(0, pthread_key_create(&key, NULL));
41 ASSERT_EQ(0, pthread_key_delete(key));
42 // Can't delete a key that's already been deleted.
43 ASSERT_EQ(EINVAL, pthread_key_delete(key));
44}
Elliott Hughes4d014e12012-09-07 16:47:54 -070045
Dan Albertc4bcc752014-09-30 11:48:24 -070046TEST(pthread, pthread_keys_max) {
Yabin Cui6c238f22014-12-11 20:50:41 -080047 // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX.
48 ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070049}
Elliott Hughes718a5b52014-01-28 17:02:03 -080050
Yabin Cui6c238f22014-12-11 20:50:41 -080051TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) {
Dan Albertc4bcc752014-09-30 11:48:24 -070052 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
Yabin Cui6c238f22014-12-11 20:50:41 -080053 ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070054}
55
56TEST(pthread, pthread_key_many_distinct) {
Yabin Cui6c238f22014-12-11 20:50:41 -080057 // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX
58 // pthread keys, but We should be able to allocate at least this many keys.
59 int nkeys = PTHREAD_KEYS_MAX / 2;
Dan Albertc4bcc752014-09-30 11:48:24 -070060 std::vector<pthread_key_t> keys;
61
62 auto scope_guard = make_scope_guard([&keys]{
63 for (auto key : keys) {
64 EXPECT_EQ(0, pthread_key_delete(key));
65 }
66 });
67
68 for (int i = 0; i < nkeys; ++i) {
69 pthread_key_t key;
70 // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is
71 // wrong.
72 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys;
73 keys.push_back(key);
74 ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i)));
75 }
76
77 for (int i = keys.size() - 1; i >= 0; --i) {
78 ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back()));
79 pthread_key_t key = keys.back();
80 keys.pop_back();
81 ASSERT_EQ(0, pthread_key_delete(key));
82 }
83}
84
Yabin Cui6c238f22014-12-11 20:50:41 -080085TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000086 std::vector<pthread_key_t> keys;
Dan Albertc4bcc752014-09-30 11:48:24 -070087 int rv = 0;
Yabin Cui6c238f22014-12-11 20:50:41 -080088
89 // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should
90 // be more than we are allowed to allocate now.
91 for (int i = 0; i < PTHREAD_KEYS_MAX; i++) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000092 pthread_key_t key;
Dan Albertc4bcc752014-09-30 11:48:24 -070093 rv = pthread_key_create(&key, NULL);
94 if (rv == EAGAIN) {
95 break;
96 }
97 EXPECT_EQ(0, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +000098 keys.push_back(key);
99 }
100
Dan Albertc4bcc752014-09-30 11:48:24 -0700101 // Don't leak keys.
102 for (auto key : keys) {
103 EXPECT_EQ(0, pthread_key_delete(key));
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000104 }
Dan Albertc4bcc752014-09-30 11:48:24 -0700105 keys.clear();
106
107 // We should have eventually reached the maximum number of keys and received
108 // EAGAIN.
109 ASSERT_EQ(EAGAIN, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000110}
111
Elliott Hughesebb770f2014-06-25 13:46:46 -0700112TEST(pthread, pthread_key_delete) {
113 void* expected = reinterpret_cast<void*>(1234);
114 pthread_key_t key;
115 ASSERT_EQ(0, pthread_key_create(&key, NULL));
116 ASSERT_EQ(0, pthread_setspecific(key, expected));
117 ASSERT_EQ(expected, pthread_getspecific(key));
118 ASSERT_EQ(0, pthread_key_delete(key));
119 // After deletion, pthread_getspecific returns NULL.
120 ASSERT_EQ(NULL, pthread_getspecific(key));
121 // And you can't use pthread_setspecific with the deleted key.
122 ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
123}
124
Elliott Hughes40a52172014-07-30 14:48:10 -0700125TEST(pthread, pthread_key_fork) {
126 void* expected = reinterpret_cast<void*>(1234);
127 pthread_key_t key;
128 ASSERT_EQ(0, pthread_key_create(&key, NULL));
129 ASSERT_EQ(0, pthread_setspecific(key, expected));
130 ASSERT_EQ(expected, pthread_getspecific(key));
131
132 pid_t pid = fork();
133 ASSERT_NE(-1, pid) << strerror(errno);
134
135 if (pid == 0) {
136 // The surviving thread inherits all the forking thread's TLS values...
137 ASSERT_EQ(expected, pthread_getspecific(key));
138 _exit(99);
139 }
140
141 int status;
142 ASSERT_EQ(pid, waitpid(pid, &status, 0));
143 ASSERT_TRUE(WIFEXITED(status));
144 ASSERT_EQ(99, WEXITSTATUS(status));
145
146 ASSERT_EQ(expected, pthread_getspecific(key));
Dan Albert1d53ae22014-09-02 15:24:26 -0700147 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700148}
149
150static void* DirtyKeyFn(void* key) {
151 return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
152}
153
154TEST(pthread, pthread_key_dirty) {
155 pthread_key_t key;
156 ASSERT_EQ(0, pthread_key_create(&key, NULL));
157
158 size_t stack_size = 128 * 1024;
159 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
160 ASSERT_NE(MAP_FAILED, stack);
161 memset(stack, 0xff, stack_size);
162
163 pthread_attr_t attr;
164 ASSERT_EQ(0, pthread_attr_init(&attr));
165 ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
166
167 pthread_t t;
168 ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
169
170 void* result;
171 ASSERT_EQ(0, pthread_join(t, &result));
172 ASSERT_EQ(nullptr, result); // Not ~0!
173
174 ASSERT_EQ(0, munmap(stack, stack_size));
Dan Albert1d53ae22014-09-02 15:24:26 -0700175 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700176}
177
Elliott Hughes4d014e12012-09-07 16:47:54 -0700178static void* IdFn(void* arg) {
179 return arg;
180}
181
Yabin Cui63481602014-12-01 17:41:04 -0800182class SpinFunctionHelper {
183 public:
184 SpinFunctionHelper() {
185 SpinFunctionHelper::spin_flag_ = true;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400186 }
Yabin Cui63481602014-12-01 17:41:04 -0800187 ~SpinFunctionHelper() {
188 UnSpin();
189 }
190 auto GetFunction() -> void* (*)(void*) {
191 return SpinFunctionHelper::SpinFn;
192 }
193
194 void UnSpin() {
195 SpinFunctionHelper::spin_flag_ = false;
196 }
197
198 private:
199 static void* SpinFn(void*) {
200 while (spin_flag_) {}
201 return NULL;
202 }
203 static volatile bool spin_flag_;
204};
205
206// It doesn't matter if spin_flag_ is used in several tests,
207// because it is always set to false after each test. Each thread
208// loops on spin_flag_ can find it becomes false at some time.
209volatile bool SpinFunctionHelper::spin_flag_ = false;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400210
Elliott Hughes4d014e12012-09-07 16:47:54 -0700211static void* JoinFn(void* arg) {
212 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
213}
214
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400215static void AssertDetached(pthread_t t, bool is_detached) {
216 pthread_attr_t attr;
217 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
218 int detach_state;
219 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
220 pthread_attr_destroy(&attr);
221 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
222}
223
Elliott Hughes9d23e042013-02-15 19:21:51 -0800224static void MakeDeadThread(pthread_t& t) {
225 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
Elliott Hughes34c987a2014-09-22 16:01:26 -0700226 ASSERT_EQ(0, pthread_join(t, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800227}
228
Elliott Hughes4d014e12012-09-07 16:47:54 -0700229TEST(pthread, pthread_create) {
230 void* expected_result = reinterpret_cast<void*>(123);
231 // Can we create a thread?
232 pthread_t t;
233 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
234 // If we join, do we get the expected value back?
235 void* result;
236 ASSERT_EQ(0, pthread_join(t, &result));
237 ASSERT_EQ(expected_result, result);
238}
239
Elliott Hughes3e898472013-02-12 16:40:24 +0000240TEST(pthread, pthread_create_EAGAIN) {
241 pthread_attr_t attributes;
242 ASSERT_EQ(0, pthread_attr_init(&attributes));
243 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
244
245 pthread_t t;
246 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
247}
248
Elliott Hughes4d014e12012-09-07 16:47:54 -0700249TEST(pthread, pthread_no_join_after_detach) {
Yabin Cui63481602014-12-01 17:41:04 -0800250 SpinFunctionHelper spinhelper;
251
Elliott Hughes4d014e12012-09-07 16:47:54 -0700252 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800253 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700254
255 // After a pthread_detach...
256 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400257 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700258
259 // ...pthread_join should fail.
Elliott Hughes34c987a2014-09-22 16:01:26 -0700260 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700261}
262
263TEST(pthread, pthread_no_op_detach_after_join) {
Yabin Cui63481602014-12-01 17:41:04 -0800264 SpinFunctionHelper spinhelper;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400265
Elliott Hughes4d014e12012-09-07 16:47:54 -0700266 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800267 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700268
269 // If thread 2 is already waiting to join thread 1...
270 pthread_t t2;
271 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
272
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400273 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700274
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400275 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
276 ASSERT_EQ(0, pthread_detach(t1));
277 AssertDetached(t1, false);
278
Yabin Cui63481602014-12-01 17:41:04 -0800279 spinhelper.UnSpin();
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400280
281 // ...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 -0700282 void* join_result;
283 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700284 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700285}
Elliott Hughes14f19592012-10-29 10:19:44 -0700286
287TEST(pthread, pthread_join_self) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700288 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL));
Elliott Hughes14f19592012-10-29 10:19:44 -0700289}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700290
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800291struct TestBug37410 {
292 pthread_t main_thread;
293 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700294
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800295 static void main() {
296 TestBug37410 data;
297 data.main_thread = pthread_self();
298 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
299 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
300
301 pthread_t t;
302 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
303
304 // Wait for the thread to be running...
305 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
306 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
307
308 // ...and exit.
309 pthread_exit(NULL);
310 }
311
312 private:
313 static void* thread_fn(void* arg) {
314 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
315
316 // Let the main thread know we're running.
317 pthread_mutex_unlock(&data->mutex);
318
319 // And wait for the main thread to exit.
320 pthread_join(data->main_thread, NULL);
321
322 return NULL;
323 }
324};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700325
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800326// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
327// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800328
329class pthread_DeathTest : public BionicDeathTest {};
330
331TEST_F(pthread_DeathTest, pthread_bug_37410) {
Elliott Hughes4f251be2012-11-01 16:33:29 -0700332 // http://code.google.com/p/android/issues/detail?id=37410
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800333 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700334}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800335
336static void* SignalHandlerFn(void* arg) {
337 sigset_t wait_set;
338 sigfillset(&wait_set);
339 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
340}
341
342TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700343 // Check that SIGUSR1 isn't blocked.
344 sigset_t original_set;
345 sigemptyset(&original_set);
346 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
347 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
348
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800349 // Block SIGUSR1.
350 sigset_t set;
351 sigemptyset(&set);
352 sigaddset(&set, SIGUSR1);
353 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
354
Elliott Hughes19e62322013-10-15 11:23:57 -0700355 // Check that SIGUSR1 is blocked.
356 sigset_t final_set;
357 sigemptyset(&final_set);
358 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
359 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
360 // ...and that sigprocmask agrees with pthread_sigmask.
361 sigemptyset(&final_set);
362 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
363 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
364
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800365 // Spawn a thread that calls sigwait and tells us what it received.
366 pthread_t signal_thread;
367 int received_signal = -1;
368 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
369
370 // Send that thread SIGUSR1.
371 pthread_kill(signal_thread, SIGUSR1);
372
373 // See what it got.
374 void* join_result;
375 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
376 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700377 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700378
379 // Restore the original signal mask.
380 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800381}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800382
Elliott Hughes3e898472013-02-12 16:40:24 +0000383TEST(pthread, pthread_setname_np__too_long) {
384 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
385}
386
387TEST(pthread, pthread_setname_np__self) {
388 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
389}
390
391TEST(pthread, pthread_setname_np__other) {
Yabin Cui63481602014-12-01 17:41:04 -0800392 SpinFunctionHelper spinhelper;
393
Elliott Hughesed29e852014-10-27 12:01:51 -0700394 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800395 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughesed29e852014-10-27 12:01:51 -0700396 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000397}
398
399TEST(pthread, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800400 pthread_t dead_thread;
401 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000402
403 // Call pthread_setname_np after thread has already exited.
Elliott Hughes68d98d82014-11-12 21:03:26 -0800404 ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000405}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800406
407TEST(pthread, pthread_kill__0) {
408 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
409 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
410}
411
412TEST(pthread, pthread_kill__invalid_signal) {
413 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
414}
415
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800416static void pthread_kill__in_signal_handler_helper(int signal_number) {
417 static int count = 0;
418 ASSERT_EQ(SIGALRM, signal_number);
419 if (++count == 1) {
420 // Can we call pthread_kill from a signal handler?
421 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
422 }
423}
424
425TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800426 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800427 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
428}
429
Elliott Hughes9d23e042013-02-15 19:21:51 -0800430TEST(pthread, pthread_detach__no_such_thread) {
431 pthread_t dead_thread;
432 MakeDeadThread(dead_thread);
433
434 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
435}
436
Yabin Cui19e246d2014-12-18 14:22:09 -0800437TEST(pthread, pthread_detach_no_leak) {
Christopher Ferrise3809602014-08-06 14:15:01 -0700438 size_t initial_bytes = 0;
439 // Run this loop more than once since the first loop causes some memory
440 // to be allocated permenantly. Run an extra loop to help catch any subtle
441 // memory leaks.
442 for (size_t loop = 0; loop < 3; loop++) {
443 // Set the initial bytes on the second loop since the memory in use
444 // should have stabilized.
445 if (loop == 1) {
446 initial_bytes = mallinfo().uordblks;
447 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800448
Christopher Ferrise3809602014-08-06 14:15:01 -0700449 pthread_attr_t attr;
450 ASSERT_EQ(0, pthread_attr_init(&attr));
451 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
Elliott Hughes04620a32014-03-07 17:59:05 -0800452
Christopher Ferrise3809602014-08-06 14:15:01 -0700453 std::vector<pthread_t> threads;
454 for (size_t i = 0; i < 32; ++i) {
455 pthread_t t;
456 ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL));
457 threads.push_back(t);
458 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800459
Christopher Ferrise3809602014-08-06 14:15:01 -0700460 sleep(1);
Elliott Hughes04620a32014-03-07 17:59:05 -0800461
Christopher Ferrise3809602014-08-06 14:15:01 -0700462 for (size_t i = 0; i < 32; ++i) {
463 ASSERT_EQ(0, pthread_detach(threads[i])) << i;
464 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800465 }
466
467 size_t final_bytes = mallinfo().uordblks;
Elliott Hughes04620a32014-03-07 17:59:05 -0800468 int leaked_bytes = (final_bytes - initial_bytes);
469
Yabin Cui19e246d2014-12-18 14:22:09 -0800470 ASSERT_EQ(0, leaked_bytes);
Elliott Hughes04620a32014-03-07 17:59:05 -0800471}
472
Jeff Hao9b06cc32013-08-15 14:51:16 -0700473TEST(pthread, pthread_getcpuclockid__clock_gettime) {
Yabin Cui63481602014-12-01 17:41:04 -0800474 SpinFunctionHelper spinhelper;
475
Jeff Hao9b06cc32013-08-15 14:51:16 -0700476 pthread_t t;
Yabin Cui63481602014-12-01 17:41:04 -0800477 ASSERT_EQ(0, pthread_create(&t, NULL, spinhelper.GetFunction(), NULL));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700478
479 clockid_t c;
480 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
481 timespec ts;
482 ASSERT_EQ(0, clock_gettime(c, &ts));
483}
484
Elliott Hughes9d23e042013-02-15 19:21:51 -0800485TEST(pthread, pthread_getcpuclockid__no_such_thread) {
486 pthread_t dead_thread;
487 MakeDeadThread(dead_thread);
488
489 clockid_t c;
490 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
491}
492
493TEST(pthread, pthread_getschedparam__no_such_thread) {
494 pthread_t dead_thread;
495 MakeDeadThread(dead_thread);
496
497 int policy;
498 sched_param param;
499 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
500}
501
502TEST(pthread, pthread_setschedparam__no_such_thread) {
503 pthread_t dead_thread;
504 MakeDeadThread(dead_thread);
505
506 int policy = 0;
507 sched_param param;
508 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
509}
510
511TEST(pthread, pthread_join__no_such_thread) {
512 pthread_t dead_thread;
513 MakeDeadThread(dead_thread);
514
Elliott Hughes34c987a2014-09-22 16:01:26 -0700515 ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800516}
517
518TEST(pthread, pthread_kill__no_such_thread) {
519 pthread_t dead_thread;
520 MakeDeadThread(dead_thread);
521
522 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
523}
msg5550f020d12013-06-06 14:59:28 -0400524
525TEST(pthread, pthread_join__multijoin) {
Yabin Cui63481602014-12-01 17:41:04 -0800526 SpinFunctionHelper spinhelper;
msg5550f020d12013-06-06 14:59:28 -0400527
528 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800529 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
msg5550f020d12013-06-06 14:59:28 -0400530
531 pthread_t t2;
532 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
533
534 sleep(1); // (Give t2 a chance to call pthread_join.)
535
536 // Multiple joins to the same thread should fail.
537 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
538
Yabin Cui63481602014-12-01 17:41:04 -0800539 spinhelper.UnSpin();
msg5550f020d12013-06-06 14:59:28 -0400540
541 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
542 void* join_result;
543 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700544 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400545}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700546
Elliott Hughes70b24b12013-11-15 11:51:07 -0800547TEST(pthread, pthread_join__race) {
548 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
549 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
550 for (size_t i = 0; i < 1024; ++i) {
551 size_t stack_size = 64*1024;
552 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
553
554 pthread_attr_t a;
555 pthread_attr_init(&a);
556 pthread_attr_setstack(&a, stack, stack_size);
557
558 pthread_t t;
559 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
560 ASSERT_EQ(0, pthread_join(t, NULL));
561 ASSERT_EQ(0, munmap(stack, stack_size));
562 }
563}
564
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700565static void* GetActualGuardSizeFn(void* arg) {
566 pthread_attr_t attributes;
567 pthread_getattr_np(pthread_self(), &attributes);
568 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
569 return NULL;
570}
571
572static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
573 size_t result;
574 pthread_t t;
575 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700576 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700577 return result;
578}
579
580static void* GetActualStackSizeFn(void* arg) {
581 pthread_attr_t attributes;
582 pthread_getattr_np(pthread_self(), &attributes);
583 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
584 return NULL;
585}
586
587static size_t GetActualStackSize(const pthread_attr_t& attributes) {
588 size_t result;
589 pthread_t t;
590 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700591 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700592 return result;
593}
594
595TEST(pthread, pthread_attr_setguardsize) {
596 pthread_attr_t attributes;
597 ASSERT_EQ(0, pthread_attr_init(&attributes));
598
599 // Get the default guard size.
600 size_t default_guard_size;
601 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
602
603 // No such thing as too small: will be rounded up to one page by pthread_create.
604 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
605 size_t guard_size;
606 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
607 ASSERT_EQ(128U, guard_size);
608 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
609
610 // Large enough and a multiple of the page size.
611 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
612 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
613 ASSERT_EQ(32*1024U, guard_size);
614
615 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
616 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
617 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
618 ASSERT_EQ(32*1024U + 1, guard_size);
619}
620
621TEST(pthread, pthread_attr_setstacksize) {
622 pthread_attr_t attributes;
623 ASSERT_EQ(0, pthread_attr_init(&attributes));
624
625 // Get the default stack size.
626 size_t default_stack_size;
627 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
628
629 // Too small.
630 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
631 size_t stack_size;
632 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
633 ASSERT_EQ(default_stack_size, stack_size);
634 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
635
Yabin Cui917d3902015-01-08 12:32:42 -0800636 // Large enough and a multiple of the page size; may be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700637 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
638 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
639 ASSERT_EQ(32*1024U, stack_size);
Yabin Cui917d3902015-01-08 12:32:42 -0800640 ASSERT_GE(GetActualStackSize(attributes), 32*1024U);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700641
Yabin Cui917d3902015-01-08 12:32:42 -0800642 // Large enough but not aligned; will be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700643 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
644 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
645 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800646#if defined(__BIONIC__)
Yabin Cui917d3902015-01-08 12:32:42 -0800647 ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800648#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700649 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
650 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800651#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700652}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700653
654TEST(pthread, pthread_rwlock_smoke) {
655 pthread_rwlock_t l;
656 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
657
Calin Juravle76f352e2014-05-19 13:41:10 +0100658 // Single read lock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700659 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
660 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
661
Calin Juravle76f352e2014-05-19 13:41:10 +0100662 // Multiple read lock
663 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
664 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
665 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
666 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
667
668 // Write lock
Calin Juravle92687e42014-05-22 19:21:22 +0100669 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
670 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100671
672 // Try writer lock
673 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
674 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
675 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
676 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
677
678 // Try reader lock
679 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
680 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
681 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
682 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
683 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
684
685 // Try writer lock after unlock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700686 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
687 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
688
Calin Juravle76f352e2014-05-19 13:41:10 +0100689#ifdef __BIONIC__
690 // EDEADLK in "read after write"
691 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
692 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
693 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
694
695 // EDEADLK in "write after write"
696 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
697 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
698 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
699#endif
700
Elliott Hughesc3f11402013-10-30 14:40:09 -0700701 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
702}
703
Yabin Cui08ee8d22015-02-11 17:04:36 -0800704struct RwlockWakeupHelperArg {
705 pthread_rwlock_t lock;
706 enum Progress {
707 LOCK_INITIALIZED,
708 LOCK_WAITING,
709 LOCK_RELEASED,
710 LOCK_ACCESSED
711 };
712 std::atomic<Progress> progress;
713};
714
715static void pthread_rwlock_reader_wakeup_writer_helper(RwlockWakeupHelperArg* arg) {
716 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
717 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
718
719 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&arg->lock));
720 ASSERT_EQ(0, pthread_rwlock_wrlock(&arg->lock));
721 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
722 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
723
724 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
725}
726
727TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
728 RwlockWakeupHelperArg wakeup_arg;
729 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
730 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
731 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
732
733 pthread_t thread;
734 ASSERT_EQ(0, pthread_create(&thread, NULL,
735 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_reader_wakeup_writer_helper), &wakeup_arg));
736 sleep(1);
737 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
738 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
739 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
740
741 ASSERT_EQ(0, pthread_join(thread, NULL));
742 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
743 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
744}
745
746static void pthread_rwlock_writer_wakeup_reader_helper(RwlockWakeupHelperArg* arg) {
747 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
748 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
749
750 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&arg->lock));
751 ASSERT_EQ(0, pthread_rwlock_rdlock(&arg->lock));
752 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
753 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
754
755 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
756}
757
758TEST(pthread, pthread_rwlock_writer_wakeup_reader) {
759 RwlockWakeupHelperArg wakeup_arg;
760 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
761 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
762 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
763
764 pthread_t thread;
765 ASSERT_EQ(0, pthread_create(&thread, NULL,
766 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_writer_wakeup_reader_helper), &wakeup_arg));
767 sleep(1);
768 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
769 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
770 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
771
772 ASSERT_EQ(0, pthread_join(thread, NULL));
773 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
774 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
775}
776
Elliott Hughes1728b232014-05-14 10:02:03 -0700777static int g_once_fn_call_count = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700778static void OnceFn() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700779 ++g_once_fn_call_count;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700780}
781
782TEST(pthread, pthread_once_smoke) {
783 pthread_once_t once_control = PTHREAD_ONCE_INIT;
784 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
785 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
Elliott Hughes1728b232014-05-14 10:02:03 -0700786 ASSERT_EQ(1, g_once_fn_call_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700787}
788
Elliott Hughes3694ec62014-05-14 11:46:08 -0700789static std::string pthread_once_1934122_result = "";
790
791static void Routine2() {
792 pthread_once_1934122_result += "2";
793}
794
795static void Routine1() {
796 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
797 pthread_once_1934122_result += "1";
798 pthread_once(&once_control_2, &Routine2);
799}
800
801TEST(pthread, pthread_once_1934122) {
802 // Very old versions of Android couldn't call pthread_once from a
803 // pthread_once init routine. http://b/1934122.
804 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
805 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
806 ASSERT_EQ("12", pthread_once_1934122_result);
807}
808
Elliott Hughes1728b232014-05-14 10:02:03 -0700809static int g_atfork_prepare_calls = 0;
810static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; }
811static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; }
812static int g_atfork_parent_calls = 0;
813static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; }
814static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; }
815static int g_atfork_child_calls = 0;
816static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; }
817static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700818
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800819TEST(pthread, pthread_atfork_smoke) {
Dmitriy Ivanov0864d8b2014-11-20 21:21:26 -0800820 test_isolated([] {
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800821 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
822 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700823
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800824 int pid = fork();
825 ASSERT_NE(-1, pid) << strerror(errno);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700826
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800827 // Child and parent calls are made in the order they were registered.
828 if (pid == 0) {
829 ASSERT_EQ(0x12, g_atfork_child_calls);
830 _exit(0);
831 }
832 ASSERT_EQ(0x12, g_atfork_parent_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700833
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800834 // Prepare calls are made in the reverse order.
835 ASSERT_EQ(0x21, g_atfork_prepare_calls);
836 });
Elliott Hughesc3f11402013-10-30 14:40:09 -0700837}
838
839TEST(pthread, pthread_attr_getscope) {
840 pthread_attr_t attr;
841 ASSERT_EQ(0, pthread_attr_init(&attr));
842
843 int scope;
844 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
845 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
846}
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000847
848TEST(pthread, pthread_condattr_init) {
849 pthread_condattr_t attr;
850 pthread_condattr_init(&attr);
851
852 clockid_t clock;
853 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
854 ASSERT_EQ(CLOCK_REALTIME, clock);
855
856 int pshared;
857 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
858 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
859}
860
861TEST(pthread, pthread_condattr_setclock) {
862 pthread_condattr_t attr;
863 pthread_condattr_init(&attr);
864
865 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
866 clockid_t clock;
867 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
868 ASSERT_EQ(CLOCK_REALTIME, clock);
869
870 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
871 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
872 ASSERT_EQ(CLOCK_MONOTONIC, clock);
873
874 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
875}
876
877TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
Yabin Cui32651b82015-03-13 20:30:00 -0700878#if defined(__BIONIC__)
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000879 pthread_condattr_t attr;
880 pthread_condattr_init(&attr);
881
882 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
883 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
884
885 pthread_cond_t cond_var;
886 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
887
888 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
889 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
890
Yabin Cui32651b82015-03-13 20:30:00 -0700891 attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000892 clockid_t clock;
893 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
894 ASSERT_EQ(CLOCK_MONOTONIC, clock);
895 int pshared;
896 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
897 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
Yabin Cui32651b82015-03-13 20:30:00 -0700898#else // !defined(__BIONIC__)
899 GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n";
900#endif // !defined(__BIONIC__)
901}
902
903class pthread_CondWakeupTest : public ::testing::Test {
904 protected:
905 pthread_mutex_t mutex;
906 pthread_cond_t cond;
907
908 enum Progress {
909 INITIALIZED,
910 WAITING,
911 SIGNALED,
912 FINISHED,
913 };
914 std::atomic<Progress> progress;
915 pthread_t thread;
916
917 protected:
918 virtual void SetUp() {
919 ASSERT_EQ(0, pthread_mutex_init(&mutex, NULL));
920 ASSERT_EQ(0, pthread_cond_init(&cond, NULL));
921 progress = INITIALIZED;
922 ASSERT_EQ(0,
923 pthread_create(&thread, NULL, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this));
924 }
925
926 virtual void TearDown() {
927 ASSERT_EQ(0, pthread_join(thread, NULL));
928 ASSERT_EQ(FINISHED, progress);
929 ASSERT_EQ(0, pthread_cond_destroy(&cond));
930 ASSERT_EQ(0, pthread_mutex_destroy(&mutex));
931 }
932
933 void SleepUntilProgress(Progress expected_progress) {
934 while (progress != expected_progress) {
935 usleep(5000);
936 }
937 usleep(5000);
938 }
939
940 private:
941 static void WaitThreadFn(pthread_CondWakeupTest* test) {
942 ASSERT_EQ(0, pthread_mutex_lock(&test->mutex));
943 test->progress = WAITING;
944 while (test->progress == WAITING) {
945 ASSERT_EQ(0, pthread_cond_wait(&test->cond, &test->mutex));
946 }
947 ASSERT_EQ(SIGNALED, test->progress);
948 test->progress = FINISHED;
949 ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex));
950 }
951};
952
953TEST_F(pthread_CondWakeupTest, signal) {
954 SleepUntilProgress(WAITING);
955 progress = SIGNALED;
956 pthread_cond_signal(&cond);
957}
958
959TEST_F(pthread_CondWakeupTest, broadcast) {
960 SleepUntilProgress(WAITING);
961 progress = SIGNALED;
962 pthread_cond_broadcast(&cond);
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000963}
Elliott Hughes0e714a52014-03-03 16:42:47 -0800964
965TEST(pthread, pthread_mutex_timedlock) {
966 pthread_mutex_t m;
967 ASSERT_EQ(0, pthread_mutex_init(&m, NULL));
968
969 // If the mutex is already locked, pthread_mutex_timedlock should time out.
970 ASSERT_EQ(0, pthread_mutex_lock(&m));
971
972 timespec ts;
973 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
974 ts.tv_nsec += 1;
975 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
976
977 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
978 ASSERT_EQ(0, pthread_mutex_unlock(&m));
979
980 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
981 ts.tv_nsec += 1;
982 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
983
984 ASSERT_EQ(0, pthread_mutex_unlock(&m));
985 ASSERT_EQ(0, pthread_mutex_destroy(&m));
986}
Elliott Hughes57b7a612014-08-25 17:26:50 -0700987
988TEST(pthread, pthread_attr_getstack__main_thread) {
989 // This test is only meaningful for the main thread, so make sure we're running on it!
990 ASSERT_EQ(getpid(), syscall(__NR_gettid));
991
992 // Get the main thread's attributes.
993 pthread_attr_t attributes;
994 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
995
996 // Check that we correctly report that the main thread has no guard page.
997 size_t guard_size;
998 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
999 ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
1000
1001 // Get the stack base and the stack size (both ways).
1002 void* stack_base;
1003 size_t stack_size;
1004 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1005 size_t stack_size2;
1006 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1007
1008 // The two methods of asking for the stack size should agree.
1009 EXPECT_EQ(stack_size, stack_size2);
1010
1011 // What does /proc/self/maps' [stack] line say?
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001012 void* maps_stack_hi = NULL;
Elliott Hughes57b7a612014-08-25 17:26:50 -07001013 FILE* fp = fopen("/proc/self/maps", "r");
1014 ASSERT_TRUE(fp != NULL);
1015 char line[BUFSIZ];
1016 while (fgets(line, sizeof(line), fp) != NULL) {
1017 uintptr_t lo, hi;
1018 char name[10];
1019 sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name);
1020 if (strcmp(name, "[stack]") == 0) {
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001021 maps_stack_hi = reinterpret_cast<void*>(hi);
Elliott Hughes57b7a612014-08-25 17:26:50 -07001022 break;
1023 }
1024 }
1025 fclose(fp);
1026
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001027 // The stack size should correspond to RLIMIT_STACK.
Elliott Hughes57b7a612014-08-25 17:26:50 -07001028 rlimit rl;
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001029 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001030 uint64_t original_rlim_cur = rl.rlim_cur;
1031#if defined(__BIONIC__)
1032 if (rl.rlim_cur == RLIM_INFINITY) {
1033 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
1034 }
1035#endif
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001036 EXPECT_EQ(rl.rlim_cur, stack_size);
1037
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001038 auto guard = make_scope_guard([&rl, original_rlim_cur]() {
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001039 rl.rlim_cur = original_rlim_cur;
1040 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1041 });
1042
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001043 // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
1044 // Remember that the stack grows down (and is mapped in on demand), so the low address of the
1045 // region isn't very interesting.
1046 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
1047
1048 //
1049 // What if RLIMIT_STACK is smaller than the stack's current extent?
1050 //
Elliott Hughes57b7a612014-08-25 17:26:50 -07001051 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
1052 rl.rlim_max = RLIM_INFINITY;
1053 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1054
1055 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1056 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1057 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1058
1059 EXPECT_EQ(stack_size, stack_size2);
1060 ASSERT_EQ(1024U, stack_size);
1061
1062 //
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001063 // What if RLIMIT_STACK isn't a whole number of pages?
Elliott Hughes57b7a612014-08-25 17:26:50 -07001064 //
1065 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
1066 rl.rlim_max = RLIM_INFINITY;
1067 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1068
1069 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1070 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1071 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1072
1073 EXPECT_EQ(stack_size, stack_size2);
1074 ASSERT_EQ(6666U, stack_size);
1075}
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001076
Yabin Cui917d3902015-01-08 12:32:42 -08001077static void pthread_attr_getstack_18908062_helper(void*) {
1078 char local_variable;
1079 pthread_attr_t attributes;
1080 pthread_getattr_np(pthread_self(), &attributes);
1081 void* stack_base;
1082 size_t stack_size;
1083 pthread_attr_getstack(&attributes, &stack_base, &stack_size);
1084
1085 // Test whether &local_variable is in [stack_base, stack_base + stack_size).
1086 ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable);
1087 ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size);
1088}
1089
1090// Check whether something on stack is in the range of
1091// [stack_base, stack_base + stack_size). see b/18908062.
1092TEST(pthread, pthread_attr_getstack_18908062) {
1093 pthread_t t;
1094 ASSERT_EQ(0, pthread_create(&t, NULL,
1095 reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper),
1096 NULL));
1097 pthread_join(t, NULL);
1098}
1099
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001100#if defined(__BIONIC__)
1101static void* pthread_gettid_np_helper(void* arg) {
1102 *reinterpret_cast<pid_t*>(arg) = gettid();
1103 return NULL;
1104}
1105#endif
1106
1107TEST(pthread, pthread_gettid_np) {
1108#if defined(__BIONIC__)
1109 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
1110
1111 pid_t t_gettid_result;
1112 pthread_t t;
1113 pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
1114
1115 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
1116
Elliott Hughes34c987a2014-09-22 16:01:26 -07001117 pthread_join(t, NULL);
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001118
1119 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
1120#else
1121 GTEST_LOG_(INFO) << "This test does nothing.\n";
1122#endif
1123}
Elliott Hughes34c987a2014-09-22 16:01:26 -07001124
1125static size_t cleanup_counter = 0;
1126
Derek Xue41996952014-09-25 11:05:32 +01001127static void AbortCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001128 abort();
1129}
1130
Derek Xue41996952014-09-25 11:05:32 +01001131static void CountCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001132 ++cleanup_counter;
1133}
1134
Derek Xue41996952014-09-25 11:05:32 +01001135static void PthreadCleanupTester() {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001136 pthread_cleanup_push(CountCleanupRoutine, NULL);
1137 pthread_cleanup_push(CountCleanupRoutine, NULL);
1138 pthread_cleanup_push(AbortCleanupRoutine, NULL);
1139
1140 pthread_cleanup_pop(0); // Pop the abort without executing it.
1141 pthread_cleanup_pop(1); // Pop one count while executing it.
1142 ASSERT_EQ(1U, cleanup_counter);
1143 // Exit while the other count is still on the cleanup stack.
1144 pthread_exit(NULL);
1145
1146 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
1147 pthread_cleanup_pop(0);
1148}
1149
Derek Xue41996952014-09-25 11:05:32 +01001150static void* PthreadCleanupStartRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001151 PthreadCleanupTester();
1152 return NULL;
1153}
1154
1155TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
1156 pthread_t t;
1157 ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL));
1158 pthread_join(t, NULL);
1159 ASSERT_EQ(2U, cleanup_counter);
1160}
Derek Xue41996952014-09-25 11:05:32 +01001161
1162TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
1163 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
1164}
1165
1166TEST(pthread, pthread_mutexattr_gettype) {
1167 pthread_mutexattr_t attr;
1168 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1169
1170 int attr_type;
1171
1172 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1173 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1174 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
1175
1176 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1177 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1178 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
1179
1180 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1181 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1182 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
1183}
1184
1185TEST(pthread, pthread_mutex_lock_NORMAL) {
1186 pthread_mutexattr_t attr;
1187 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1188 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1189
1190 pthread_mutex_t lock;
1191 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1192
1193 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1194 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1195 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1196}
1197
1198TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
1199 pthread_mutexattr_t attr;
1200 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1201 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1202
1203 pthread_mutex_t lock;
1204 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1205
1206 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1207 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&lock));
1208 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1209 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1210 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock));
1211 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1212 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1213 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1214}
1215
1216TEST(pthread, pthread_mutex_lock_RECURSIVE) {
1217 pthread_mutexattr_t attr;
1218 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1219 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1220
1221 pthread_mutex_t lock;
1222 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1223
1224 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1225 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1226 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1227 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1228 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1229 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1230 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1231 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1232}
Yabin Cui140f3672015-02-03 10:32:00 -08001233
1234TEST(pthread, pthread_mutex_owner_tid_limit) {
1235 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
1236 ASSERT_TRUE(fp != NULL);
1237 long pid_max;
1238 ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max));
1239 fclose(fp);
1240 // Current pthread_mutex uses 16 bits to represent owner tid.
1241 // Change the implementation if we need to support higher value than 65535.
1242 ASSERT_LE(pid_max, 65536);
1243}