blob: a299f02f72720b7d15271470e9c15e4f05e8b130 [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
19#include <errno.h>
Elliott Hughes5b9310e2013-10-02 16:59:05 -070020#include <inttypes.h>
Elliott Hughesb95cf0d2013-07-15 14:51:07 -070021#include <limits.h>
Elliott Hughes04620a32014-03-07 17:59:05 -080022#include <malloc.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070023#include <pthread.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080024#include <signal.h>
Yabin Cui140f3672015-02-03 10:32:00 -080025#include <stdio.h>
Elliott Hughes70b24b12013-11-15 11:51:07 -080026#include <sys/mman.h>
Elliott Hughes57b7a612014-08-25 17:26:50 -070027#include <sys/syscall.h>
Narayan Kamath51e6cb32014-03-03 15:38:51 +000028#include <time.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070029#include <unistd.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070030
Yabin Cui08ee8d22015-02-11 17:04:36 -080031#include <atomic>
Yabin Cuif7969852015-04-02 17:47:48 -070032#include <regex>
Yabin Cuib5845722015-03-16 22:46:42 -070033#include <vector>
Yabin Cui08ee8d22015-02-11 17:04:36 -080034
Yabin Cuif7969852015-04-02 17:47:48 -070035#include <base/file.h>
36#include <base/stringprintf.h>
37
Yabin Cui17393b02015-03-21 15:08:25 -070038#include "private/bionic_macros.h"
39#include "private/ScopeGuard.h"
40#include "BionicDeathTest.h"
41#include "ScopedSignalHandler.h"
42
Yabin Cuif7969852015-04-02 17:47:48 -070043extern "C" pid_t gettid();
44
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070045TEST(pthread, pthread_key_create) {
46 pthread_key_t key;
47 ASSERT_EQ(0, pthread_key_create(&key, NULL));
48 ASSERT_EQ(0, pthread_key_delete(key));
49 // Can't delete a key that's already been deleted.
50 ASSERT_EQ(EINVAL, pthread_key_delete(key));
51}
Elliott Hughes4d014e12012-09-07 16:47:54 -070052
Dan Albertc4bcc752014-09-30 11:48:24 -070053TEST(pthread, pthread_keys_max) {
Yabin Cui6c238f22014-12-11 20:50:41 -080054 // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX.
55 ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070056}
Elliott Hughes718a5b52014-01-28 17:02:03 -080057
Yabin Cui6c238f22014-12-11 20:50:41 -080058TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) {
Dan Albertc4bcc752014-09-30 11:48:24 -070059 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
Yabin Cui6c238f22014-12-11 20:50:41 -080060 ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070061}
62
63TEST(pthread, pthread_key_many_distinct) {
Yabin Cui6c238f22014-12-11 20:50:41 -080064 // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX
65 // pthread keys, but We should be able to allocate at least this many keys.
66 int nkeys = PTHREAD_KEYS_MAX / 2;
Dan Albertc4bcc752014-09-30 11:48:24 -070067 std::vector<pthread_key_t> keys;
68
69 auto scope_guard = make_scope_guard([&keys]{
70 for (auto key : keys) {
71 EXPECT_EQ(0, pthread_key_delete(key));
72 }
73 });
74
75 for (int i = 0; i < nkeys; ++i) {
76 pthread_key_t key;
Elliott Hughes61706932015-03-31 10:56:58 -070077 // If this fails, it's likely that LIBC_PTHREAD_KEY_RESERVED_COUNT is wrong.
Dan Albertc4bcc752014-09-30 11:48:24 -070078 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys;
79 keys.push_back(key);
80 ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i)));
81 }
82
83 for (int i = keys.size() - 1; i >= 0; --i) {
84 ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back()));
85 pthread_key_t key = keys.back();
86 keys.pop_back();
87 ASSERT_EQ(0, pthread_key_delete(key));
88 }
89}
90
Yabin Cui6c238f22014-12-11 20:50:41 -080091TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000092 std::vector<pthread_key_t> keys;
Dan Albertc4bcc752014-09-30 11:48:24 -070093 int rv = 0;
Yabin Cui6c238f22014-12-11 20:50:41 -080094
95 // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should
96 // be more than we are allowed to allocate now.
97 for (int i = 0; i < PTHREAD_KEYS_MAX; i++) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000098 pthread_key_t key;
Dan Albertc4bcc752014-09-30 11:48:24 -070099 rv = pthread_key_create(&key, NULL);
100 if (rv == EAGAIN) {
101 break;
102 }
103 EXPECT_EQ(0, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000104 keys.push_back(key);
105 }
106
Dan Albertc4bcc752014-09-30 11:48:24 -0700107 // Don't leak keys.
108 for (auto key : keys) {
109 EXPECT_EQ(0, pthread_key_delete(key));
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000110 }
Dan Albertc4bcc752014-09-30 11:48:24 -0700111 keys.clear();
112
113 // We should have eventually reached the maximum number of keys and received
114 // EAGAIN.
115 ASSERT_EQ(EAGAIN, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000116}
117
Elliott Hughesebb770f2014-06-25 13:46:46 -0700118TEST(pthread, pthread_key_delete) {
119 void* expected = reinterpret_cast<void*>(1234);
120 pthread_key_t key;
121 ASSERT_EQ(0, pthread_key_create(&key, NULL));
122 ASSERT_EQ(0, pthread_setspecific(key, expected));
123 ASSERT_EQ(expected, pthread_getspecific(key));
124 ASSERT_EQ(0, pthread_key_delete(key));
125 // After deletion, pthread_getspecific returns NULL.
126 ASSERT_EQ(NULL, pthread_getspecific(key));
127 // And you can't use pthread_setspecific with the deleted key.
128 ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
129}
130
Elliott Hughes40a52172014-07-30 14:48:10 -0700131TEST(pthread, pthread_key_fork) {
132 void* expected = reinterpret_cast<void*>(1234);
133 pthread_key_t key;
134 ASSERT_EQ(0, pthread_key_create(&key, NULL));
135 ASSERT_EQ(0, pthread_setspecific(key, expected));
136 ASSERT_EQ(expected, pthread_getspecific(key));
137
138 pid_t pid = fork();
139 ASSERT_NE(-1, pid) << strerror(errno);
140
141 if (pid == 0) {
142 // The surviving thread inherits all the forking thread's TLS values...
143 ASSERT_EQ(expected, pthread_getspecific(key));
144 _exit(99);
145 }
146
147 int status;
148 ASSERT_EQ(pid, waitpid(pid, &status, 0));
149 ASSERT_TRUE(WIFEXITED(status));
150 ASSERT_EQ(99, WEXITSTATUS(status));
151
152 ASSERT_EQ(expected, pthread_getspecific(key));
Dan Albert1d53ae22014-09-02 15:24:26 -0700153 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700154}
155
156static void* DirtyKeyFn(void* key) {
157 return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
158}
159
160TEST(pthread, pthread_key_dirty) {
161 pthread_key_t key;
162 ASSERT_EQ(0, pthread_key_create(&key, NULL));
163
164 size_t stack_size = 128 * 1024;
165 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
166 ASSERT_NE(MAP_FAILED, stack);
167 memset(stack, 0xff, stack_size);
168
169 pthread_attr_t attr;
170 ASSERT_EQ(0, pthread_attr_init(&attr));
171 ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
172
173 pthread_t t;
174 ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
175
176 void* result;
177 ASSERT_EQ(0, pthread_join(t, &result));
178 ASSERT_EQ(nullptr, result); // Not ~0!
179
180 ASSERT_EQ(0, munmap(stack, stack_size));
Dan Albert1d53ae22014-09-02 15:24:26 -0700181 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700182}
183
Yabin Cui5ddbb3f2015-03-05 20:35:32 -0800184TEST(pthread, static_pthread_key_used_before_creation) {
185#if defined(__BIONIC__)
186 // See http://b/19625804. The bug is about a static/global pthread key being used before creation.
187 // So here tests if the static/global default value 0 can be detected as invalid key.
188 static pthread_key_t key;
189 ASSERT_EQ(nullptr, pthread_getspecific(key));
190 ASSERT_EQ(EINVAL, pthread_setspecific(key, nullptr));
191 ASSERT_EQ(EINVAL, pthread_key_delete(key));
192#else
193 GTEST_LOG_(INFO) << "This test tests bionic pthread key implementation detail.\n";
194#endif
195}
196
Elliott Hughes4d014e12012-09-07 16:47:54 -0700197static void* IdFn(void* arg) {
198 return arg;
199}
200
Yabin Cui63481602014-12-01 17:41:04 -0800201class SpinFunctionHelper {
202 public:
203 SpinFunctionHelper() {
204 SpinFunctionHelper::spin_flag_ = true;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400205 }
Yabin Cui63481602014-12-01 17:41:04 -0800206 ~SpinFunctionHelper() {
207 UnSpin();
208 }
209 auto GetFunction() -> void* (*)(void*) {
210 return SpinFunctionHelper::SpinFn;
211 }
212
213 void UnSpin() {
214 SpinFunctionHelper::spin_flag_ = false;
215 }
216
217 private:
218 static void* SpinFn(void*) {
219 while (spin_flag_) {}
220 return NULL;
221 }
222 static volatile bool spin_flag_;
223};
224
225// It doesn't matter if spin_flag_ is used in several tests,
226// because it is always set to false after each test. Each thread
227// loops on spin_flag_ can find it becomes false at some time.
228volatile bool SpinFunctionHelper::spin_flag_ = false;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400229
Elliott Hughes4d014e12012-09-07 16:47:54 -0700230static void* JoinFn(void* arg) {
231 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
232}
233
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400234static void AssertDetached(pthread_t t, bool is_detached) {
235 pthread_attr_t attr;
236 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
237 int detach_state;
238 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
239 pthread_attr_destroy(&attr);
240 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
241}
242
Elliott Hughes9d23e042013-02-15 19:21:51 -0800243static void MakeDeadThread(pthread_t& t) {
244 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
Elliott Hughes34c987a2014-09-22 16:01:26 -0700245 ASSERT_EQ(0, pthread_join(t, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800246}
247
Elliott Hughes4d014e12012-09-07 16:47:54 -0700248TEST(pthread, pthread_create) {
249 void* expected_result = reinterpret_cast<void*>(123);
250 // Can we create a thread?
251 pthread_t t;
252 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
253 // If we join, do we get the expected value back?
254 void* result;
255 ASSERT_EQ(0, pthread_join(t, &result));
256 ASSERT_EQ(expected_result, result);
257}
258
Elliott Hughes3e898472013-02-12 16:40:24 +0000259TEST(pthread, pthread_create_EAGAIN) {
260 pthread_attr_t attributes;
261 ASSERT_EQ(0, pthread_attr_init(&attributes));
262 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
263
264 pthread_t t;
265 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
266}
267
Elliott Hughes4d014e12012-09-07 16:47:54 -0700268TEST(pthread, pthread_no_join_after_detach) {
Yabin Cui63481602014-12-01 17:41:04 -0800269 SpinFunctionHelper spinhelper;
270
Elliott Hughes4d014e12012-09-07 16:47:54 -0700271 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800272 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700273
274 // After a pthread_detach...
275 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400276 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700277
278 // ...pthread_join should fail.
Elliott Hughes34c987a2014-09-22 16:01:26 -0700279 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700280}
281
282TEST(pthread, pthread_no_op_detach_after_join) {
Yabin Cui63481602014-12-01 17:41:04 -0800283 SpinFunctionHelper spinhelper;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400284
Elliott Hughes4d014e12012-09-07 16:47:54 -0700285 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800286 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700287
288 // If thread 2 is already waiting to join thread 1...
289 pthread_t t2;
290 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
291
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400292 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700293
Yabin Cuibbb04322015-03-19 15:19:25 -0700294#if defined(__BIONIC__)
295 ASSERT_EQ(EINVAL, pthread_detach(t1));
296#else
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400297 ASSERT_EQ(0, pthread_detach(t1));
Yabin Cuibbb04322015-03-19 15:19:25 -0700298#endif
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400299 AssertDetached(t1, false);
300
Yabin Cui63481602014-12-01 17:41:04 -0800301 spinhelper.UnSpin();
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400302
303 // ...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 -0700304 void* join_result;
305 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700306 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700307}
Elliott Hughes14f19592012-10-29 10:19:44 -0700308
309TEST(pthread, pthread_join_self) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700310 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL));
Elliott Hughes14f19592012-10-29 10:19:44 -0700311}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700312
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800313struct TestBug37410 {
314 pthread_t main_thread;
315 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700316
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800317 static void main() {
318 TestBug37410 data;
319 data.main_thread = pthread_self();
320 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
321 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
322
323 pthread_t t;
324 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
325
326 // Wait for the thread to be running...
327 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
328 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
329
330 // ...and exit.
331 pthread_exit(NULL);
332 }
333
334 private:
335 static void* thread_fn(void* arg) {
336 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
337
338 // Let the main thread know we're running.
339 pthread_mutex_unlock(&data->mutex);
340
341 // And wait for the main thread to exit.
342 pthread_join(data->main_thread, NULL);
343
344 return NULL;
345 }
346};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700347
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800348// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
349// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800350
351class pthread_DeathTest : public BionicDeathTest {};
352
353TEST_F(pthread_DeathTest, pthread_bug_37410) {
Elliott Hughes4f251be2012-11-01 16:33:29 -0700354 // http://code.google.com/p/android/issues/detail?id=37410
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800355 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700356}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800357
358static void* SignalHandlerFn(void* arg) {
359 sigset_t wait_set;
360 sigfillset(&wait_set);
361 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
362}
363
364TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700365 // Check that SIGUSR1 isn't blocked.
366 sigset_t original_set;
367 sigemptyset(&original_set);
368 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
369 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
370
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800371 // Block SIGUSR1.
372 sigset_t set;
373 sigemptyset(&set);
374 sigaddset(&set, SIGUSR1);
375 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
376
Elliott Hughes19e62322013-10-15 11:23:57 -0700377 // Check that SIGUSR1 is blocked.
378 sigset_t final_set;
379 sigemptyset(&final_set);
380 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
381 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
382 // ...and that sigprocmask agrees with pthread_sigmask.
383 sigemptyset(&final_set);
384 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
385 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
386
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800387 // Spawn a thread that calls sigwait and tells us what it received.
388 pthread_t signal_thread;
389 int received_signal = -1;
390 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
391
392 // Send that thread SIGUSR1.
393 pthread_kill(signal_thread, SIGUSR1);
394
395 // See what it got.
396 void* join_result;
397 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
398 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700399 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700400
401 // Restore the original signal mask.
402 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800403}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800404
Elliott Hughes3e898472013-02-12 16:40:24 +0000405TEST(pthread, pthread_setname_np__too_long) {
406 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
407}
408
409TEST(pthread, pthread_setname_np__self) {
410 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
411}
412
413TEST(pthread, pthread_setname_np__other) {
Yabin Cui63481602014-12-01 17:41:04 -0800414 SpinFunctionHelper spinhelper;
415
Elliott Hughesed29e852014-10-27 12:01:51 -0700416 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800417 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughesed29e852014-10-27 12:01:51 -0700418 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000419}
420
Yabin Cui220b99b2015-03-26 18:13:07 +0000421TEST(pthread, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800422 pthread_t dead_thread;
423 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000424
425 // Call pthread_setname_np after thread has already exited.
Elliott Hughes68d98d82014-11-12 21:03:26 -0800426 ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000427}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800428
429TEST(pthread, pthread_kill__0) {
430 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
431 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
432}
433
434TEST(pthread, pthread_kill__invalid_signal) {
435 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
436}
437
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800438static void pthread_kill__in_signal_handler_helper(int signal_number) {
439 static int count = 0;
440 ASSERT_EQ(SIGALRM, signal_number);
441 if (++count == 1) {
442 // Can we call pthread_kill from a signal handler?
443 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
444 }
445}
446
447TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800448 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800449 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
450}
451
Yabin Cui220b99b2015-03-26 18:13:07 +0000452TEST(pthread, pthread_detach__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800453 pthread_t dead_thread;
454 MakeDeadThread(dead_thread);
455
456 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
457}
458
Yabin Cui19e246d2014-12-18 14:22:09 -0800459TEST(pthread, pthread_detach_no_leak) {
Christopher Ferrise3809602014-08-06 14:15:01 -0700460 size_t initial_bytes = 0;
461 // Run this loop more than once since the first loop causes some memory
462 // to be allocated permenantly. Run an extra loop to help catch any subtle
463 // memory leaks.
464 for (size_t loop = 0; loop < 3; loop++) {
465 // Set the initial bytes on the second loop since the memory in use
466 // should have stabilized.
467 if (loop == 1) {
468 initial_bytes = mallinfo().uordblks;
469 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800470
Christopher Ferrise3809602014-08-06 14:15:01 -0700471 pthread_attr_t attr;
472 ASSERT_EQ(0, pthread_attr_init(&attr));
473 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
Elliott Hughes04620a32014-03-07 17:59:05 -0800474
Christopher Ferrise3809602014-08-06 14:15:01 -0700475 std::vector<pthread_t> threads;
476 for (size_t i = 0; i < 32; ++i) {
477 pthread_t t;
478 ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL));
479 threads.push_back(t);
480 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800481
Christopher Ferrise3809602014-08-06 14:15:01 -0700482 sleep(1);
Elliott Hughes04620a32014-03-07 17:59:05 -0800483
Christopher Ferrise3809602014-08-06 14:15:01 -0700484 for (size_t i = 0; i < 32; ++i) {
485 ASSERT_EQ(0, pthread_detach(threads[i])) << i;
486 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800487 }
488
489 size_t final_bytes = mallinfo().uordblks;
Elliott Hughes04620a32014-03-07 17:59:05 -0800490 int leaked_bytes = (final_bytes - initial_bytes);
491
Yabin Cui19e246d2014-12-18 14:22:09 -0800492 ASSERT_EQ(0, leaked_bytes);
Elliott Hughes04620a32014-03-07 17:59:05 -0800493}
494
Jeff Hao9b06cc32013-08-15 14:51:16 -0700495TEST(pthread, pthread_getcpuclockid__clock_gettime) {
Yabin Cui63481602014-12-01 17:41:04 -0800496 SpinFunctionHelper spinhelper;
497
Jeff Hao9b06cc32013-08-15 14:51:16 -0700498 pthread_t t;
Yabin Cui63481602014-12-01 17:41:04 -0800499 ASSERT_EQ(0, pthread_create(&t, NULL, spinhelper.GetFunction(), NULL));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700500
501 clockid_t c;
502 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
503 timespec ts;
504 ASSERT_EQ(0, clock_gettime(c, &ts));
505}
506
Yabin Cui220b99b2015-03-26 18:13:07 +0000507TEST(pthread, pthread_getcpuclockid__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800508 pthread_t dead_thread;
509 MakeDeadThread(dead_thread);
510
511 clockid_t c;
512 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
513}
514
Yabin Cui220b99b2015-03-26 18:13:07 +0000515TEST(pthread, pthread_getschedparam__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800516 pthread_t dead_thread;
517 MakeDeadThread(dead_thread);
518
519 int policy;
520 sched_param param;
521 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
522}
523
Yabin Cui220b99b2015-03-26 18:13:07 +0000524TEST(pthread, pthread_setschedparam__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800525 pthread_t dead_thread;
526 MakeDeadThread(dead_thread);
527
528 int policy = 0;
529 sched_param param;
530 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
531}
532
Yabin Cui220b99b2015-03-26 18:13:07 +0000533TEST(pthread, pthread_join__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800534 pthread_t dead_thread;
535 MakeDeadThread(dead_thread);
536
Elliott Hughes34c987a2014-09-22 16:01:26 -0700537 ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800538}
539
Yabin Cui220b99b2015-03-26 18:13:07 +0000540TEST(pthread, pthread_kill__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800541 pthread_t dead_thread;
542 MakeDeadThread(dead_thread);
543
544 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
545}
msg5550f020d12013-06-06 14:59:28 -0400546
547TEST(pthread, pthread_join__multijoin) {
Yabin Cui63481602014-12-01 17:41:04 -0800548 SpinFunctionHelper spinhelper;
msg5550f020d12013-06-06 14:59:28 -0400549
550 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800551 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
msg5550f020d12013-06-06 14:59:28 -0400552
553 pthread_t t2;
554 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
555
556 sleep(1); // (Give t2 a chance to call pthread_join.)
557
558 // Multiple joins to the same thread should fail.
559 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
560
Yabin Cui63481602014-12-01 17:41:04 -0800561 spinhelper.UnSpin();
msg5550f020d12013-06-06 14:59:28 -0400562
563 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
564 void* join_result;
565 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700566 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400567}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700568
Elliott Hughes70b24b12013-11-15 11:51:07 -0800569TEST(pthread, pthread_join__race) {
570 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
571 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
572 for (size_t i = 0; i < 1024; ++i) {
573 size_t stack_size = 64*1024;
574 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
575
576 pthread_attr_t a;
577 pthread_attr_init(&a);
578 pthread_attr_setstack(&a, stack, stack_size);
579
580 pthread_t t;
581 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
582 ASSERT_EQ(0, pthread_join(t, NULL));
583 ASSERT_EQ(0, munmap(stack, stack_size));
584 }
585}
586
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700587static void* GetActualGuardSizeFn(void* arg) {
588 pthread_attr_t attributes;
589 pthread_getattr_np(pthread_self(), &attributes);
590 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
591 return NULL;
592}
593
594static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
595 size_t result;
596 pthread_t t;
597 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700598 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700599 return result;
600}
601
602static void* GetActualStackSizeFn(void* arg) {
603 pthread_attr_t attributes;
604 pthread_getattr_np(pthread_self(), &attributes);
605 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
606 return NULL;
607}
608
609static size_t GetActualStackSize(const pthread_attr_t& attributes) {
610 size_t result;
611 pthread_t t;
612 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700613 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700614 return result;
615}
616
617TEST(pthread, pthread_attr_setguardsize) {
618 pthread_attr_t attributes;
619 ASSERT_EQ(0, pthread_attr_init(&attributes));
620
621 // Get the default guard size.
622 size_t default_guard_size;
623 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
624
625 // No such thing as too small: will be rounded up to one page by pthread_create.
626 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
627 size_t guard_size;
628 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
629 ASSERT_EQ(128U, guard_size);
630 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
631
632 // Large enough and a multiple of the page size.
633 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
634 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
635 ASSERT_EQ(32*1024U, guard_size);
636
637 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
638 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
639 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
640 ASSERT_EQ(32*1024U + 1, guard_size);
641}
642
643TEST(pthread, pthread_attr_setstacksize) {
644 pthread_attr_t attributes;
645 ASSERT_EQ(0, pthread_attr_init(&attributes));
646
647 // Get the default stack size.
648 size_t default_stack_size;
649 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
650
651 // Too small.
652 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
653 size_t stack_size;
654 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
655 ASSERT_EQ(default_stack_size, stack_size);
656 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
657
Yabin Cui917d3902015-01-08 12:32:42 -0800658 // Large enough and a multiple of the page size; may be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700659 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
660 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
661 ASSERT_EQ(32*1024U, stack_size);
Yabin Cui917d3902015-01-08 12:32:42 -0800662 ASSERT_GE(GetActualStackSize(attributes), 32*1024U);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700663
Yabin Cui917d3902015-01-08 12:32:42 -0800664 // Large enough but not aligned; will be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700665 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
666 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
667 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800668#if defined(__BIONIC__)
Yabin Cui917d3902015-01-08 12:32:42 -0800669 ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800670#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700671 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
672 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800673#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700674}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700675
Yabin Cui76615da2015-03-17 14:22:09 -0700676TEST(pthread, pthread_rwlockattr_smoke) {
677 pthread_rwlockattr_t attr;
678 ASSERT_EQ(0, pthread_rwlockattr_init(&attr));
679
680 int pshared_value_array[] = {PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED};
681 for (size_t i = 0; i < sizeof(pshared_value_array) / sizeof(pshared_value_array[0]); ++i) {
682 ASSERT_EQ(0, pthread_rwlockattr_setpshared(&attr, pshared_value_array[i]));
683 int pshared;
684 ASSERT_EQ(0, pthread_rwlockattr_getpshared(&attr, &pshared));
685 ASSERT_EQ(pshared_value_array[i], pshared);
686 }
687
688 int kind_array[] = {PTHREAD_RWLOCK_PREFER_READER_NP,
689 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP};
690 for (size_t i = 0; i < sizeof(kind_array) / sizeof(kind_array[0]); ++i) {
691 ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_array[i]));
692 int kind;
693 ASSERT_EQ(0, pthread_rwlockattr_getkind_np(&attr, &kind));
694 ASSERT_EQ(kind_array[i], kind);
695 }
696
697 ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr));
698}
699
700TEST(pthread, pthread_rwlock_init_same_as_PTHREAD_RWLOCK_INITIALIZER) {
701 pthread_rwlock_t lock1 = PTHREAD_RWLOCK_INITIALIZER;
702 pthread_rwlock_t lock2;
703 ASSERT_EQ(0, pthread_rwlock_init(&lock2, NULL));
704 ASSERT_EQ(0, memcmp(&lock1, &lock2, sizeof(lock1)));
705}
706
Elliott Hughesc3f11402013-10-30 14:40:09 -0700707TEST(pthread, pthread_rwlock_smoke) {
708 pthread_rwlock_t l;
709 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
710
Calin Juravle76f352e2014-05-19 13:41:10 +0100711 // Single read lock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700712 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
713 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
714
Calin Juravle76f352e2014-05-19 13:41:10 +0100715 // Multiple read lock
716 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
717 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
718 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
719 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
720
721 // Write lock
Calin Juravle92687e42014-05-22 19:21:22 +0100722 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
723 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100724
725 // Try writer lock
726 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
727 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
728 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
729 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
730
731 // Try reader lock
732 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
733 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
734 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
735 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
736 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
737
738 // Try writer lock after unlock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700739 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
740 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
741
Calin Juravle76f352e2014-05-19 13:41:10 +0100742 // EDEADLK in "read after write"
743 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
744 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
745 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
746
747 // EDEADLK in "write after write"
748 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
749 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
750 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100751
Elliott Hughesc3f11402013-10-30 14:40:09 -0700752 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
753}
754
Yabin Cuif7969852015-04-02 17:47:48 -0700755static void WaitUntilThreadSleep(std::atomic<pid_t>& pid) {
756 while (pid == 0) {
757 usleep(1000);
758 }
759 std::string filename = android::base::StringPrintf("/proc/%d/stat", pid.load());
760 std::regex regex {R"(\s+S\s+)"};
761
762 while (true) {
763 std::string content;
764 ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
765 if (std::regex_search(content, regex)) {
766 break;
767 }
768 usleep(1000);
769 }
770}
771
Yabin Cui08ee8d22015-02-11 17:04:36 -0800772struct RwlockWakeupHelperArg {
773 pthread_rwlock_t lock;
774 enum Progress {
775 LOCK_INITIALIZED,
776 LOCK_WAITING,
777 LOCK_RELEASED,
778 LOCK_ACCESSED
779 };
780 std::atomic<Progress> progress;
Yabin Cuif7969852015-04-02 17:47:48 -0700781 std::atomic<pid_t> tid;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800782};
783
784static void pthread_rwlock_reader_wakeup_writer_helper(RwlockWakeupHelperArg* arg) {
Yabin Cuif7969852015-04-02 17:47:48 -0700785 arg->tid = gettid();
Yabin Cui08ee8d22015-02-11 17:04:36 -0800786 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
787 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
788
789 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&arg->lock));
790 ASSERT_EQ(0, pthread_rwlock_wrlock(&arg->lock));
791 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
792 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
793
794 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
795}
796
797TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
798 RwlockWakeupHelperArg wakeup_arg;
799 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
800 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
801 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -0700802 wakeup_arg.tid = 0;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800803
804 pthread_t thread;
805 ASSERT_EQ(0, pthread_create(&thread, NULL,
806 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_reader_wakeup_writer_helper), &wakeup_arg));
Yabin Cuif7969852015-04-02 17:47:48 -0700807 WaitUntilThreadSleep(wakeup_arg.tid);
808 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
809
Yabin Cui08ee8d22015-02-11 17:04:36 -0800810 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
811 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
812
813 ASSERT_EQ(0, pthread_join(thread, NULL));
814 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
815 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
816}
817
818static void pthread_rwlock_writer_wakeup_reader_helper(RwlockWakeupHelperArg* arg) {
Yabin Cuif7969852015-04-02 17:47:48 -0700819 arg->tid = gettid();
Yabin Cui08ee8d22015-02-11 17:04:36 -0800820 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
821 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
822
823 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&arg->lock));
824 ASSERT_EQ(0, pthread_rwlock_rdlock(&arg->lock));
825 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
826 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
827
828 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
829}
830
831TEST(pthread, pthread_rwlock_writer_wakeup_reader) {
832 RwlockWakeupHelperArg wakeup_arg;
833 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
834 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
835 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -0700836 wakeup_arg.tid = 0;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800837
838 pthread_t thread;
839 ASSERT_EQ(0, pthread_create(&thread, NULL,
840 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_writer_wakeup_reader_helper), &wakeup_arg));
Yabin Cuif7969852015-04-02 17:47:48 -0700841 WaitUntilThreadSleep(wakeup_arg.tid);
842 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
843
Yabin Cui08ee8d22015-02-11 17:04:36 -0800844 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
845 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
846
847 ASSERT_EQ(0, pthread_join(thread, NULL));
848 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
849 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
850}
851
Yabin Cui76615da2015-03-17 14:22:09 -0700852class RwlockKindTestHelper {
853 private:
854 struct ThreadArg {
855 RwlockKindTestHelper* helper;
856 std::atomic<pid_t>& tid;
857
858 ThreadArg(RwlockKindTestHelper* helper, std::atomic<pid_t>& tid)
859 : helper(helper), tid(tid) { }
860 };
861
862 public:
863 pthread_rwlock_t lock;
864
865 public:
866 RwlockKindTestHelper(int kind_type) {
867 InitRwlock(kind_type);
868 }
869
870 ~RwlockKindTestHelper() {
871 DestroyRwlock();
872 }
873
874 void CreateWriterThread(pthread_t& thread, std::atomic<pid_t>& tid) {
875 tid = 0;
876 ThreadArg* arg = new ThreadArg(this, tid);
877 ASSERT_EQ(0, pthread_create(&thread, NULL,
878 reinterpret_cast<void* (*)(void*)>(WriterThreadFn), arg));
879 }
880
881 void CreateReaderThread(pthread_t& thread, std::atomic<pid_t>& tid) {
882 tid = 0;
883 ThreadArg* arg = new ThreadArg(this, tid);
884 ASSERT_EQ(0, pthread_create(&thread, NULL,
885 reinterpret_cast<void* (*)(void*)>(ReaderThreadFn), arg));
886 }
887
888 private:
889 void InitRwlock(int kind_type) {
890 pthread_rwlockattr_t attr;
891 ASSERT_EQ(0, pthread_rwlockattr_init(&attr));
892 ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_type));
893 ASSERT_EQ(0, pthread_rwlock_init(&lock, &attr));
894 ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr));
895 }
896
897 void DestroyRwlock() {
898 ASSERT_EQ(0, pthread_rwlock_destroy(&lock));
899 }
900
901 static void WriterThreadFn(ThreadArg* arg) {
902 arg->tid = gettid();
903
904 RwlockKindTestHelper* helper = arg->helper;
905 ASSERT_EQ(0, pthread_rwlock_wrlock(&helper->lock));
906 ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock));
907 delete arg;
908 }
909
910 static void ReaderThreadFn(ThreadArg* arg) {
911 arg->tid = gettid();
912
913 RwlockKindTestHelper* helper = arg->helper;
914 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper->lock));
915 ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock));
916 delete arg;
917 }
918};
919
920TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_READER_NP) {
921 RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_READER_NP);
922 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock));
923
924 pthread_t writer_thread;
925 std::atomic<pid_t> writer_tid;
926 helper.CreateWriterThread(writer_thread, writer_tid);
927 WaitUntilThreadSleep(writer_tid);
928
929 pthread_t reader_thread;
930 std::atomic<pid_t> reader_tid;
931 helper.CreateReaderThread(reader_thread, reader_tid);
932 ASSERT_EQ(0, pthread_join(reader_thread, NULL));
933
934 ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock));
935 ASSERT_EQ(0, pthread_join(writer_thread, NULL));
936}
937
938TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) {
939 RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
940 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock));
941
942 pthread_t writer_thread;
943 std::atomic<pid_t> writer_tid;
944 helper.CreateWriterThread(writer_thread, writer_tid);
945 WaitUntilThreadSleep(writer_tid);
946
947 pthread_t reader_thread;
948 std::atomic<pid_t> reader_tid;
949 helper.CreateReaderThread(reader_thread, reader_tid);
950 WaitUntilThreadSleep(reader_tid);
951
952 ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock));
953 ASSERT_EQ(0, pthread_join(writer_thread, NULL));
954 ASSERT_EQ(0, pthread_join(reader_thread, NULL));
955}
956
Elliott Hughes1728b232014-05-14 10:02:03 -0700957static int g_once_fn_call_count = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700958static void OnceFn() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700959 ++g_once_fn_call_count;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700960}
961
962TEST(pthread, pthread_once_smoke) {
963 pthread_once_t once_control = PTHREAD_ONCE_INIT;
964 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
965 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
Elliott Hughes1728b232014-05-14 10:02:03 -0700966 ASSERT_EQ(1, g_once_fn_call_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700967}
968
Elliott Hughes3694ec62014-05-14 11:46:08 -0700969static std::string pthread_once_1934122_result = "";
970
971static void Routine2() {
972 pthread_once_1934122_result += "2";
973}
974
975static void Routine1() {
976 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
977 pthread_once_1934122_result += "1";
978 pthread_once(&once_control_2, &Routine2);
979}
980
981TEST(pthread, pthread_once_1934122) {
982 // Very old versions of Android couldn't call pthread_once from a
983 // pthread_once init routine. http://b/1934122.
984 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
985 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
986 ASSERT_EQ("12", pthread_once_1934122_result);
987}
988
Elliott Hughes1728b232014-05-14 10:02:03 -0700989static int g_atfork_prepare_calls = 0;
Dimitry Ivanov094f58f2015-04-24 03:45:59 +0000990static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; }
991static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; }
Elliott Hughes1728b232014-05-14 10:02:03 -0700992static int g_atfork_parent_calls = 0;
Dimitry Ivanov094f58f2015-04-24 03:45:59 +0000993static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; }
994static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; }
Elliott Hughes1728b232014-05-14 10:02:03 -0700995static int g_atfork_child_calls = 0;
Dimitry Ivanov094f58f2015-04-24 03:45:59 +0000996static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; }
997static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700998
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800999TEST(pthread, pthread_atfork_smoke) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001000 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
1001 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
Elliott Hughesc3f11402013-10-30 14:40:09 -07001002
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001003 int pid = fork();
1004 ASSERT_NE(-1, pid) << strerror(errno);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001005
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001006 // Child and parent calls are made in the order they were registered.
1007 if (pid == 0) {
Dimitry Ivanov094f58f2015-04-24 03:45:59 +00001008 ASSERT_EQ(0x12, g_atfork_child_calls);
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001009 _exit(0);
1010 }
Dimitry Ivanov094f58f2015-04-24 03:45:59 +00001011 ASSERT_EQ(0x12, g_atfork_parent_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001012
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001013 // Prepare calls are made in the reverse order.
Dimitry Ivanov094f58f2015-04-24 03:45:59 +00001014 ASSERT_EQ(0x21, g_atfork_prepare_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001015}
1016
1017TEST(pthread, pthread_attr_getscope) {
1018 pthread_attr_t attr;
1019 ASSERT_EQ(0, pthread_attr_init(&attr));
1020
1021 int scope;
1022 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
1023 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
1024}
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001025
1026TEST(pthread, pthread_condattr_init) {
1027 pthread_condattr_t attr;
1028 pthread_condattr_init(&attr);
1029
1030 clockid_t clock;
1031 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1032 ASSERT_EQ(CLOCK_REALTIME, clock);
1033
1034 int pshared;
1035 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
1036 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
1037}
1038
1039TEST(pthread, pthread_condattr_setclock) {
1040 pthread_condattr_t attr;
1041 pthread_condattr_init(&attr);
1042
1043 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
1044 clockid_t clock;
1045 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1046 ASSERT_EQ(CLOCK_REALTIME, clock);
1047
1048 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
1049 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1050 ASSERT_EQ(CLOCK_MONOTONIC, clock);
1051
1052 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
1053}
1054
1055TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
Yabin Cui32651b82015-03-13 20:30:00 -07001056#if defined(__BIONIC__)
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001057 pthread_condattr_t attr;
1058 pthread_condattr_init(&attr);
1059
1060 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
1061 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
1062
1063 pthread_cond_t cond_var;
1064 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
1065
1066 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
1067 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
1068
Yabin Cui32651b82015-03-13 20:30:00 -07001069 attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private));
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001070 clockid_t clock;
1071 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1072 ASSERT_EQ(CLOCK_MONOTONIC, clock);
1073 int pshared;
1074 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
1075 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
Yabin Cui32651b82015-03-13 20:30:00 -07001076#else // !defined(__BIONIC__)
1077 GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n";
1078#endif // !defined(__BIONIC__)
1079}
1080
1081class pthread_CondWakeupTest : public ::testing::Test {
1082 protected:
1083 pthread_mutex_t mutex;
1084 pthread_cond_t cond;
1085
1086 enum Progress {
1087 INITIALIZED,
1088 WAITING,
1089 SIGNALED,
1090 FINISHED,
1091 };
1092 std::atomic<Progress> progress;
1093 pthread_t thread;
1094
1095 protected:
1096 virtual void SetUp() {
1097 ASSERT_EQ(0, pthread_mutex_init(&mutex, NULL));
1098 ASSERT_EQ(0, pthread_cond_init(&cond, NULL));
1099 progress = INITIALIZED;
1100 ASSERT_EQ(0,
1101 pthread_create(&thread, NULL, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this));
1102 }
1103
1104 virtual void TearDown() {
1105 ASSERT_EQ(0, pthread_join(thread, NULL));
1106 ASSERT_EQ(FINISHED, progress);
1107 ASSERT_EQ(0, pthread_cond_destroy(&cond));
1108 ASSERT_EQ(0, pthread_mutex_destroy(&mutex));
1109 }
1110
1111 void SleepUntilProgress(Progress expected_progress) {
1112 while (progress != expected_progress) {
1113 usleep(5000);
1114 }
1115 usleep(5000);
1116 }
1117
1118 private:
1119 static void WaitThreadFn(pthread_CondWakeupTest* test) {
1120 ASSERT_EQ(0, pthread_mutex_lock(&test->mutex));
1121 test->progress = WAITING;
1122 while (test->progress == WAITING) {
1123 ASSERT_EQ(0, pthread_cond_wait(&test->cond, &test->mutex));
1124 }
1125 ASSERT_EQ(SIGNALED, test->progress);
1126 test->progress = FINISHED;
1127 ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex));
1128 }
1129};
1130
1131TEST_F(pthread_CondWakeupTest, signal) {
1132 SleepUntilProgress(WAITING);
1133 progress = SIGNALED;
1134 pthread_cond_signal(&cond);
1135}
1136
1137TEST_F(pthread_CondWakeupTest, broadcast) {
1138 SleepUntilProgress(WAITING);
1139 progress = SIGNALED;
1140 pthread_cond_broadcast(&cond);
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001141}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001142
1143TEST(pthread, pthread_mutex_timedlock) {
1144 pthread_mutex_t m;
1145 ASSERT_EQ(0, pthread_mutex_init(&m, NULL));
1146
1147 // If the mutex is already locked, pthread_mutex_timedlock should time out.
1148 ASSERT_EQ(0, pthread_mutex_lock(&m));
1149
1150 timespec ts;
1151 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1152 ts.tv_nsec += 1;
1153 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
1154
1155 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
1156 ASSERT_EQ(0, pthread_mutex_unlock(&m));
1157
1158 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1159 ts.tv_nsec += 1;
1160 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
1161
1162 ASSERT_EQ(0, pthread_mutex_unlock(&m));
1163 ASSERT_EQ(0, pthread_mutex_destroy(&m));
1164}
Elliott Hughes57b7a612014-08-25 17:26:50 -07001165
1166TEST(pthread, pthread_attr_getstack__main_thread) {
1167 // This test is only meaningful for the main thread, so make sure we're running on it!
1168 ASSERT_EQ(getpid(), syscall(__NR_gettid));
1169
1170 // Get the main thread's attributes.
1171 pthread_attr_t attributes;
1172 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1173
1174 // Check that we correctly report that the main thread has no guard page.
1175 size_t guard_size;
1176 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
1177 ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
1178
1179 // Get the stack base and the stack size (both ways).
1180 void* stack_base;
1181 size_t stack_size;
1182 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1183 size_t stack_size2;
1184 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1185
1186 // The two methods of asking for the stack size should agree.
1187 EXPECT_EQ(stack_size, stack_size2);
1188
1189 // What does /proc/self/maps' [stack] line say?
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001190 void* maps_stack_hi = NULL;
Elliott Hughes57b7a612014-08-25 17:26:50 -07001191 FILE* fp = fopen("/proc/self/maps", "r");
1192 ASSERT_TRUE(fp != NULL);
1193 char line[BUFSIZ];
1194 while (fgets(line, sizeof(line), fp) != NULL) {
1195 uintptr_t lo, hi;
1196 char name[10];
1197 sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name);
1198 if (strcmp(name, "[stack]") == 0) {
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001199 maps_stack_hi = reinterpret_cast<void*>(hi);
Elliott Hughes57b7a612014-08-25 17:26:50 -07001200 break;
1201 }
1202 }
1203 fclose(fp);
1204
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001205 // The stack size should correspond to RLIMIT_STACK.
Elliott Hughes57b7a612014-08-25 17:26:50 -07001206 rlimit rl;
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001207 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001208 uint64_t original_rlim_cur = rl.rlim_cur;
1209#if defined(__BIONIC__)
1210 if (rl.rlim_cur == RLIM_INFINITY) {
1211 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
1212 }
1213#endif
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001214 EXPECT_EQ(rl.rlim_cur, stack_size);
1215
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001216 auto guard = make_scope_guard([&rl, original_rlim_cur]() {
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001217 rl.rlim_cur = original_rlim_cur;
1218 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1219 });
1220
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001221 // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
1222 // Remember that the stack grows down (and is mapped in on demand), so the low address of the
1223 // region isn't very interesting.
1224 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
1225
1226 //
1227 // What if RLIMIT_STACK is smaller than the stack's current extent?
1228 //
Elliott Hughes57b7a612014-08-25 17:26:50 -07001229 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
1230 rl.rlim_max = RLIM_INFINITY;
1231 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1232
1233 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1234 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1235 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1236
1237 EXPECT_EQ(stack_size, stack_size2);
1238 ASSERT_EQ(1024U, stack_size);
1239
1240 //
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001241 // What if RLIMIT_STACK isn't a whole number of pages?
Elliott Hughes57b7a612014-08-25 17:26:50 -07001242 //
1243 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
1244 rl.rlim_max = RLIM_INFINITY;
1245 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1246
1247 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1248 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1249 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1250
1251 EXPECT_EQ(stack_size, stack_size2);
1252 ASSERT_EQ(6666U, stack_size);
1253}
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001254
Yabin Cui917d3902015-01-08 12:32:42 -08001255static void pthread_attr_getstack_18908062_helper(void*) {
1256 char local_variable;
1257 pthread_attr_t attributes;
1258 pthread_getattr_np(pthread_self(), &attributes);
1259 void* stack_base;
1260 size_t stack_size;
1261 pthread_attr_getstack(&attributes, &stack_base, &stack_size);
1262
1263 // Test whether &local_variable is in [stack_base, stack_base + stack_size).
1264 ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable);
1265 ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size);
1266}
1267
1268// Check whether something on stack is in the range of
1269// [stack_base, stack_base + stack_size). see b/18908062.
1270TEST(pthread, pthread_attr_getstack_18908062) {
1271 pthread_t t;
1272 ASSERT_EQ(0, pthread_create(&t, NULL,
1273 reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper),
1274 NULL));
1275 pthread_join(t, NULL);
1276}
1277
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001278#if defined(__BIONIC__)
1279static void* pthread_gettid_np_helper(void* arg) {
1280 *reinterpret_cast<pid_t*>(arg) = gettid();
1281 return NULL;
1282}
1283#endif
1284
1285TEST(pthread, pthread_gettid_np) {
1286#if defined(__BIONIC__)
1287 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
1288
1289 pid_t t_gettid_result;
1290 pthread_t t;
1291 pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
1292
1293 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
1294
Elliott Hughes34c987a2014-09-22 16:01:26 -07001295 pthread_join(t, NULL);
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001296
1297 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
1298#else
1299 GTEST_LOG_(INFO) << "This test does nothing.\n";
1300#endif
1301}
Elliott Hughes34c987a2014-09-22 16:01:26 -07001302
1303static size_t cleanup_counter = 0;
1304
Derek Xue41996952014-09-25 11:05:32 +01001305static void AbortCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001306 abort();
1307}
1308
Derek Xue41996952014-09-25 11:05:32 +01001309static void CountCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001310 ++cleanup_counter;
1311}
1312
Derek Xue41996952014-09-25 11:05:32 +01001313static void PthreadCleanupTester() {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001314 pthread_cleanup_push(CountCleanupRoutine, NULL);
1315 pthread_cleanup_push(CountCleanupRoutine, NULL);
1316 pthread_cleanup_push(AbortCleanupRoutine, NULL);
1317
1318 pthread_cleanup_pop(0); // Pop the abort without executing it.
1319 pthread_cleanup_pop(1); // Pop one count while executing it.
1320 ASSERT_EQ(1U, cleanup_counter);
1321 // Exit while the other count is still on the cleanup stack.
1322 pthread_exit(NULL);
1323
1324 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
1325 pthread_cleanup_pop(0);
1326}
1327
Derek Xue41996952014-09-25 11:05:32 +01001328static void* PthreadCleanupStartRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001329 PthreadCleanupTester();
1330 return NULL;
1331}
1332
1333TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
1334 pthread_t t;
1335 ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL));
1336 pthread_join(t, NULL);
1337 ASSERT_EQ(2U, cleanup_counter);
1338}
Derek Xue41996952014-09-25 11:05:32 +01001339
1340TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
1341 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
1342}
1343
1344TEST(pthread, pthread_mutexattr_gettype) {
1345 pthread_mutexattr_t attr;
1346 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1347
1348 int attr_type;
1349
1350 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1351 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1352 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
1353
1354 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1355 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1356 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
1357
1358 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1359 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1360 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001361
1362 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1363}
1364
Yabin Cui17393b02015-03-21 15:08:25 -07001365struct PthreadMutex {
1366 pthread_mutex_t lock;
1367
1368 PthreadMutex(int mutex_type) {
1369 init(mutex_type);
1370 }
1371
1372 ~PthreadMutex() {
1373 destroy();
1374 }
1375
1376 private:
1377 void init(int mutex_type) {
1378 pthread_mutexattr_t attr;
1379 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1380 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, mutex_type));
1381 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1382 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1383 }
1384
1385 void destroy() {
1386 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1387 }
1388
1389 DISALLOW_COPY_AND_ASSIGN(PthreadMutex);
1390};
Derek Xue41996952014-09-25 11:05:32 +01001391
1392TEST(pthread, pthread_mutex_lock_NORMAL) {
Yabin Cui17393b02015-03-21 15:08:25 -07001393 PthreadMutex m(PTHREAD_MUTEX_NORMAL);
Derek Xue41996952014-09-25 11:05:32 +01001394
Yabin Cui17393b02015-03-21 15:08:25 -07001395 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1396 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Derek Xue41996952014-09-25 11:05:32 +01001397}
1398
1399TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
Yabin Cui17393b02015-03-21 15:08:25 -07001400 PthreadMutex m(PTHREAD_MUTEX_ERRORCHECK);
Derek Xue41996952014-09-25 11:05:32 +01001401
Yabin Cui17393b02015-03-21 15:08:25 -07001402 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1403 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&m.lock));
1404 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1405 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1406 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock));
1407 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1408 ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
Derek Xue41996952014-09-25 11:05:32 +01001409}
1410
1411TEST(pthread, pthread_mutex_lock_RECURSIVE) {
Yabin Cui17393b02015-03-21 15:08:25 -07001412 PthreadMutex m(PTHREAD_MUTEX_RECURSIVE);
Derek Xue41996952014-09-25 11:05:32 +01001413
Yabin Cui17393b02015-03-21 15:08:25 -07001414 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1415 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1416 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1417 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1418 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1419 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1420 ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
1421}
1422
1423TEST(pthread, pthread_mutex_init_same_as_static_initializers) {
1424 pthread_mutex_t lock_normal = PTHREAD_MUTEX_INITIALIZER;
1425 PthreadMutex m1(PTHREAD_MUTEX_NORMAL);
1426 ASSERT_EQ(0, memcmp(&lock_normal, &m1.lock, sizeof(pthread_mutex_t)));
1427 pthread_mutex_destroy(&lock_normal);
1428
1429 pthread_mutex_t lock_errorcheck = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
1430 PthreadMutex m2(PTHREAD_MUTEX_ERRORCHECK);
1431 ASSERT_EQ(0, memcmp(&lock_errorcheck, &m2.lock, sizeof(pthread_mutex_t)));
1432 pthread_mutex_destroy(&lock_errorcheck);
1433
1434 pthread_mutex_t lock_recursive = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
1435 PthreadMutex m3(PTHREAD_MUTEX_RECURSIVE);
1436 ASSERT_EQ(0, memcmp(&lock_recursive, &m3.lock, sizeof(pthread_mutex_t)));
1437 ASSERT_EQ(0, pthread_mutex_destroy(&lock_recursive));
Derek Xue41996952014-09-25 11:05:32 +01001438}
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001439class MutexWakeupHelper {
1440 private:
Yabin Cui17393b02015-03-21 15:08:25 -07001441 PthreadMutex m;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001442 enum Progress {
1443 LOCK_INITIALIZED,
1444 LOCK_WAITING,
1445 LOCK_RELEASED,
1446 LOCK_ACCESSED
1447 };
1448 std::atomic<Progress> progress;
Yabin Cuif7969852015-04-02 17:47:48 -07001449 std::atomic<pid_t> tid;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001450
1451 static void thread_fn(MutexWakeupHelper* helper) {
Yabin Cuif7969852015-04-02 17:47:48 -07001452 helper->tid = gettid();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001453 ASSERT_EQ(LOCK_INITIALIZED, helper->progress);
1454 helper->progress = LOCK_WAITING;
1455
Yabin Cui17393b02015-03-21 15:08:25 -07001456 ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001457 ASSERT_EQ(LOCK_RELEASED, helper->progress);
Yabin Cui17393b02015-03-21 15:08:25 -07001458 ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001459
1460 helper->progress = LOCK_ACCESSED;
1461 }
1462
1463 public:
Yabin Cui17393b02015-03-21 15:08:25 -07001464 MutexWakeupHelper(int mutex_type) : m(mutex_type) {
1465 }
1466
1467 void test() {
1468 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001469 progress = LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -07001470 tid = 0;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001471
1472 pthread_t thread;
1473 ASSERT_EQ(0, pthread_create(&thread, NULL,
1474 reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this));
1475
Yabin Cuif7969852015-04-02 17:47:48 -07001476 WaitUntilThreadSleep(tid);
1477 ASSERT_EQ(LOCK_WAITING, progress);
1478
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001479 progress = LOCK_RELEASED;
Yabin Cui17393b02015-03-21 15:08:25 -07001480 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001481
1482 ASSERT_EQ(0, pthread_join(thread, NULL));
1483 ASSERT_EQ(LOCK_ACCESSED, progress);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001484 }
1485};
1486
1487TEST(pthread, pthread_mutex_NORMAL_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001488 MutexWakeupHelper helper(PTHREAD_MUTEX_NORMAL);
1489 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001490}
1491
1492TEST(pthread, pthread_mutex_ERRORCHECK_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001493 MutexWakeupHelper helper(PTHREAD_MUTEX_ERRORCHECK);
1494 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001495}
1496
1497TEST(pthread, pthread_mutex_RECURSIVE_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001498 MutexWakeupHelper helper(PTHREAD_MUTEX_RECURSIVE);
1499 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001500}
1501
Yabin Cui140f3672015-02-03 10:32:00 -08001502TEST(pthread, pthread_mutex_owner_tid_limit) {
Yabin Cuie69c2452015-02-13 16:21:25 -08001503#if defined(__BIONIC__) && !defined(__LP64__)
Yabin Cui140f3672015-02-03 10:32:00 -08001504 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
1505 ASSERT_TRUE(fp != NULL);
1506 long pid_max;
1507 ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max));
1508 fclose(fp);
Yabin Cuie69c2452015-02-13 16:21:25 -08001509 // Bionic's pthread_mutex implementation on 32-bit devices uses 16 bits to represent owner tid.
Yabin Cui140f3672015-02-03 10:32:00 -08001510 ASSERT_LE(pid_max, 65536);
Yabin Cuie69c2452015-02-13 16:21:25 -08001511#else
1512 GTEST_LOG_(INFO) << "This test does nothing as 32-bit tid is supported by pthread_mutex.\n";
1513#endif
Yabin Cui140f3672015-02-03 10:32:00 -08001514}
Yabin Cuib5845722015-03-16 22:46:42 -07001515
1516class StrictAlignmentAllocator {
1517 public:
1518 void* allocate(size_t size, size_t alignment) {
1519 char* p = new char[size + alignment * 2];
1520 allocated_array.push_back(p);
1521 while (!is_strict_aligned(p, alignment)) {
1522 ++p;
1523 }
1524 return p;
1525 }
1526
1527 ~StrictAlignmentAllocator() {
1528 for (auto& p : allocated_array) {
1529 delete [] p;
1530 }
1531 }
1532
1533 private:
1534 bool is_strict_aligned(char* p, size_t alignment) {
1535 return (reinterpret_cast<uintptr_t>(p) % (alignment * 2)) == alignment;
1536 }
1537
1538 std::vector<char*> allocated_array;
1539};
1540
1541TEST(pthread, pthread_types_allow_four_bytes_alignment) {
1542#if defined(__BIONIC__)
1543 // For binary compatibility with old version, we need to allow 4-byte aligned data for pthread types.
1544 StrictAlignmentAllocator allocator;
1545 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(
1546 allocator.allocate(sizeof(pthread_mutex_t), 4));
1547 ASSERT_EQ(0, pthread_mutex_init(mutex, NULL));
1548 ASSERT_EQ(0, pthread_mutex_lock(mutex));
1549 ASSERT_EQ(0, pthread_mutex_unlock(mutex));
1550 ASSERT_EQ(0, pthread_mutex_destroy(mutex));
1551
1552 pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>(
1553 allocator.allocate(sizeof(pthread_cond_t), 4));
1554 ASSERT_EQ(0, pthread_cond_init(cond, NULL));
1555 ASSERT_EQ(0, pthread_cond_signal(cond));
1556 ASSERT_EQ(0, pthread_cond_broadcast(cond));
1557 ASSERT_EQ(0, pthread_cond_destroy(cond));
1558
1559 pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>(
1560 allocator.allocate(sizeof(pthread_rwlock_t), 4));
1561 ASSERT_EQ(0, pthread_rwlock_init(rwlock, NULL));
1562 ASSERT_EQ(0, pthread_rwlock_rdlock(rwlock));
1563 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
1564 ASSERT_EQ(0, pthread_rwlock_wrlock(rwlock));
1565 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
1566 ASSERT_EQ(0, pthread_rwlock_destroy(rwlock));
1567
1568#else
1569 GTEST_LOG_(INFO) << "This test tests bionic implementation details.";
1570#endif
1571}