blob: 85978bd09d744257d3e717dcbe4e8b9c2faf3e96 [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 Hughes4d098ca2016-04-11 12:43:05 -070027#include <sys/prctl.h>
Elliott Hughes57b7a612014-08-25 17:26:50 -070028#include <sys/syscall.h>
Narayan Kamath51e6cb32014-03-03 15:38:51 +000029#include <time.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070030#include <unistd.h>
Yabin Cui33ac04a2015-09-22 11:16:15 -070031#include <unwind.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070032
Yabin Cui08ee8d22015-02-11 17:04:36 -080033#include <atomic>
Yabin Cuib5845722015-03-16 22:46:42 -070034#include <vector>
Yabin Cui08ee8d22015-02-11 17:04:36 -080035
Tom Cherryb8ab6182017-04-05 16:20:29 -070036#include <android-base/scopeguard.h>
37
Yabin Cuic9a659c2015-11-05 15:36:08 -080038#include "private/bionic_constants.h"
Yabin Cui17393b02015-03-21 15:08:25 -070039#include "private/bionic_macros.h"
Yabin Cui17393b02015-03-21 15:08:25 -070040#include "BionicDeathTest.h"
41#include "ScopedSignalHandler.h"
Elliott Hughes15dfd632015-09-22 16:40:14 -070042#include "utils.h"
43
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070044TEST(pthread, pthread_key_create) {
45 pthread_key_t key;
46 ASSERT_EQ(0, pthread_key_create(&key, NULL));
47 ASSERT_EQ(0, pthread_key_delete(key));
48 // Can't delete a key that's already been deleted.
49 ASSERT_EQ(EINVAL, pthread_key_delete(key));
50}
Elliott Hughes4d014e12012-09-07 16:47:54 -070051
Dan Albertc4bcc752014-09-30 11:48:24 -070052TEST(pthread, pthread_keys_max) {
Yabin Cui6c238f22014-12-11 20:50:41 -080053 // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX.
54 ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070055}
Elliott Hughes718a5b52014-01-28 17:02:03 -080056
Yabin Cui6c238f22014-12-11 20:50:41 -080057TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) {
Dan Albertc4bcc752014-09-30 11:48:24 -070058 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
Yabin Cui6c238f22014-12-11 20:50:41 -080059 ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070060}
61
62TEST(pthread, pthread_key_many_distinct) {
Yabin Cui6c238f22014-12-11 20:50:41 -080063 // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX
64 // pthread keys, but We should be able to allocate at least this many keys.
65 int nkeys = PTHREAD_KEYS_MAX / 2;
Dan Albertc4bcc752014-09-30 11:48:24 -070066 std::vector<pthread_key_t> keys;
67
Tom Cherryb8ab6182017-04-05 16:20:29 -070068 auto scope_guard = android::base::make_scope_guard([&keys] {
Elliott Hughes0b2acdf2015-10-02 18:25:19 -070069 for (const auto& key : keys) {
Dan Albertc4bcc752014-09-30 11:48:24 -070070 EXPECT_EQ(0, pthread_key_delete(key));
71 }
72 });
73
74 for (int i = 0; i < nkeys; ++i) {
75 pthread_key_t key;
Elliott Hughes61706932015-03-31 10:56:58 -070076 // If this fails, it's likely that LIBC_PTHREAD_KEY_RESERVED_COUNT is wrong.
Dan Albertc4bcc752014-09-30 11:48:24 -070077 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys;
78 keys.push_back(key);
79 ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i)));
80 }
81
82 for (int i = keys.size() - 1; i >= 0; --i) {
83 ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back()));
84 pthread_key_t key = keys.back();
85 keys.pop_back();
86 ASSERT_EQ(0, pthread_key_delete(key));
87 }
88}
89
Yabin Cui6c238f22014-12-11 20:50:41 -080090TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000091 std::vector<pthread_key_t> keys;
Dan Albertc4bcc752014-09-30 11:48:24 -070092 int rv = 0;
Yabin Cui6c238f22014-12-11 20:50:41 -080093
94 // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should
95 // be more than we are allowed to allocate now.
96 for (int i = 0; i < PTHREAD_KEYS_MAX; i++) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000097 pthread_key_t key;
Dan Albertc4bcc752014-09-30 11:48:24 -070098 rv = pthread_key_create(&key, NULL);
99 if (rv == EAGAIN) {
100 break;
101 }
102 EXPECT_EQ(0, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000103 keys.push_back(key);
104 }
105
Dan Albertc4bcc752014-09-30 11:48:24 -0700106 // Don't leak keys.
Elliott Hughes0b2acdf2015-10-02 18:25:19 -0700107 for (const auto& key : keys) {
Dan Albertc4bcc752014-09-30 11:48:24 -0700108 EXPECT_EQ(0, pthread_key_delete(key));
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000109 }
Dan Albertc4bcc752014-09-30 11:48:24 -0700110 keys.clear();
111
112 // We should have eventually reached the maximum number of keys and received
113 // EAGAIN.
114 ASSERT_EQ(EAGAIN, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000115}
116
Elliott Hughesebb770f2014-06-25 13:46:46 -0700117TEST(pthread, pthread_key_delete) {
118 void* expected = reinterpret_cast<void*>(1234);
119 pthread_key_t key;
120 ASSERT_EQ(0, pthread_key_create(&key, NULL));
121 ASSERT_EQ(0, pthread_setspecific(key, expected));
122 ASSERT_EQ(expected, pthread_getspecific(key));
123 ASSERT_EQ(0, pthread_key_delete(key));
124 // After deletion, pthread_getspecific returns NULL.
125 ASSERT_EQ(NULL, pthread_getspecific(key));
126 // And you can't use pthread_setspecific with the deleted key.
127 ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
128}
129
Elliott Hughes40a52172014-07-30 14:48:10 -0700130TEST(pthread, pthread_key_fork) {
131 void* expected = reinterpret_cast<void*>(1234);
132 pthread_key_t key;
133 ASSERT_EQ(0, pthread_key_create(&key, NULL));
134 ASSERT_EQ(0, pthread_setspecific(key, expected));
135 ASSERT_EQ(expected, pthread_getspecific(key));
136
137 pid_t pid = fork();
138 ASSERT_NE(-1, pid) << strerror(errno);
139
140 if (pid == 0) {
141 // The surviving thread inherits all the forking thread's TLS values...
142 ASSERT_EQ(expected, pthread_getspecific(key));
143 _exit(99);
144 }
145
Elliott Hughes33697a02016-01-26 13:04:57 -0800146 AssertChildExited(pid, 99);
Elliott Hughes40a52172014-07-30 14:48:10 -0700147
148 ASSERT_EQ(expected, pthread_getspecific(key));
Dan Albert1d53ae22014-09-02 15:24:26 -0700149 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700150}
151
152static void* DirtyKeyFn(void* key) {
153 return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
154}
155
156TEST(pthread, pthread_key_dirty) {
157 pthread_key_t key;
158 ASSERT_EQ(0, pthread_key_create(&key, NULL));
159
Yabin Cuia36158a2015-11-16 21:06:16 -0800160 size_t stack_size = 640 * 1024;
Elliott Hughes40a52172014-07-30 14:48:10 -0700161 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
162 ASSERT_NE(MAP_FAILED, stack);
163 memset(stack, 0xff, stack_size);
164
165 pthread_attr_t attr;
166 ASSERT_EQ(0, pthread_attr_init(&attr));
167 ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
168
169 pthread_t t;
170 ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
171
172 void* result;
173 ASSERT_EQ(0, pthread_join(t, &result));
174 ASSERT_EQ(nullptr, result); // Not ~0!
175
176 ASSERT_EQ(0, munmap(stack, stack_size));
Dan Albert1d53ae22014-09-02 15:24:26 -0700177 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700178}
179
Yabin Cui5ddbb3f2015-03-05 20:35:32 -0800180TEST(pthread, static_pthread_key_used_before_creation) {
181#if defined(__BIONIC__)
182 // See http://b/19625804. The bug is about a static/global pthread key being used before creation.
183 // So here tests if the static/global default value 0 can be detected as invalid key.
184 static pthread_key_t key;
185 ASSERT_EQ(nullptr, pthread_getspecific(key));
186 ASSERT_EQ(EINVAL, pthread_setspecific(key, nullptr));
187 ASSERT_EQ(EINVAL, pthread_key_delete(key));
188#else
189 GTEST_LOG_(INFO) << "This test tests bionic pthread key implementation detail.\n";
190#endif
191}
192
Elliott Hughes4d014e12012-09-07 16:47:54 -0700193static void* IdFn(void* arg) {
194 return arg;
195}
196
Yabin Cui63481602014-12-01 17:41:04 -0800197class SpinFunctionHelper {
198 public:
199 SpinFunctionHelper() {
200 SpinFunctionHelper::spin_flag_ = true;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400201 }
Yabin Cui63481602014-12-01 17:41:04 -0800202 ~SpinFunctionHelper() {
203 UnSpin();
204 }
205 auto GetFunction() -> void* (*)(void*) {
206 return SpinFunctionHelper::SpinFn;
207 }
208
209 void UnSpin() {
210 SpinFunctionHelper::spin_flag_ = false;
211 }
212
213 private:
214 static void* SpinFn(void*) {
215 while (spin_flag_) {}
216 return NULL;
217 }
Yabin Cuia36158a2015-11-16 21:06:16 -0800218 static std::atomic<bool> spin_flag_;
Yabin Cui63481602014-12-01 17:41:04 -0800219};
220
221// It doesn't matter if spin_flag_ is used in several tests,
222// because it is always set to false after each test. Each thread
223// loops on spin_flag_ can find it becomes false at some time.
Yabin Cuia36158a2015-11-16 21:06:16 -0800224std::atomic<bool> SpinFunctionHelper::spin_flag_;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400225
Elliott Hughes4d014e12012-09-07 16:47:54 -0700226static void* JoinFn(void* arg) {
227 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
228}
229
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400230static void AssertDetached(pthread_t t, bool is_detached) {
231 pthread_attr_t attr;
232 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
233 int detach_state;
234 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
235 pthread_attr_destroy(&attr);
236 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
237}
238
Elliott Hughes7484c212017-02-02 02:41:38 +0000239static void MakeDeadThread(pthread_t& t) {
240 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
241 ASSERT_EQ(0, pthread_join(t, NULL));
242}
243
Elliott Hughes4d014e12012-09-07 16:47:54 -0700244TEST(pthread, pthread_create) {
245 void* expected_result = reinterpret_cast<void*>(123);
246 // Can we create a thread?
247 pthread_t t;
248 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
249 // If we join, do we get the expected value back?
250 void* result;
251 ASSERT_EQ(0, pthread_join(t, &result));
252 ASSERT_EQ(expected_result, result);
253}
254
Elliott Hughes3e898472013-02-12 16:40:24 +0000255TEST(pthread, pthread_create_EAGAIN) {
256 pthread_attr_t attributes;
257 ASSERT_EQ(0, pthread_attr_init(&attributes));
258 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
259
260 pthread_t t;
261 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
262}
263
Elliott Hughes4d014e12012-09-07 16:47:54 -0700264TEST(pthread, pthread_no_join_after_detach) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700265 SpinFunctionHelper spin_helper;
Yabin Cui63481602014-12-01 17:41:04 -0800266
Elliott Hughes4d014e12012-09-07 16:47:54 -0700267 pthread_t t1;
Elliott Hughes725b2a92016-03-23 11:20:47 -0700268 ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700269
270 // After a pthread_detach...
271 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400272 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700273
274 // ...pthread_join should fail.
Elliott Hughes34c987a2014-09-22 16:01:26 -0700275 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700276}
277
278TEST(pthread, pthread_no_op_detach_after_join) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700279 SpinFunctionHelper spin_helper;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400280
Elliott Hughes4d014e12012-09-07 16:47:54 -0700281 pthread_t t1;
Elliott Hughes725b2a92016-03-23 11:20:47 -0700282 ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700283
284 // If thread 2 is already waiting to join thread 1...
285 pthread_t t2;
286 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
287
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400288 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700289
Yabin Cuibbb04322015-03-19 15:19:25 -0700290#if defined(__BIONIC__)
291 ASSERT_EQ(EINVAL, pthread_detach(t1));
292#else
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400293 ASSERT_EQ(0, pthread_detach(t1));
Yabin Cuibbb04322015-03-19 15:19:25 -0700294#endif
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400295 AssertDetached(t1, false);
296
Elliott Hughes725b2a92016-03-23 11:20:47 -0700297 spin_helper.UnSpin();
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400298
299 // ...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 -0700300 void* join_result;
301 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700302 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700303}
Elliott Hughes14f19592012-10-29 10:19:44 -0700304
305TEST(pthread, pthread_join_self) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700306 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL));
Elliott Hughes14f19592012-10-29 10:19:44 -0700307}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700308
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800309struct TestBug37410 {
310 pthread_t main_thread;
311 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700312
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800313 static void main() {
314 TestBug37410 data;
315 data.main_thread = pthread_self();
316 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
317 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
318
319 pthread_t t;
320 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
321
322 // Wait for the thread to be running...
323 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
324 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
325
326 // ...and exit.
327 pthread_exit(NULL);
328 }
329
330 private:
331 static void* thread_fn(void* arg) {
332 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
333
334 // Let the main thread know we're running.
335 pthread_mutex_unlock(&data->mutex);
336
337 // And wait for the main thread to exit.
338 pthread_join(data->main_thread, NULL);
339
340 return NULL;
341 }
342};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700343
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800344// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
345// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800346
347class pthread_DeathTest : public BionicDeathTest {};
348
349TEST_F(pthread_DeathTest, pthread_bug_37410) {
Elliott Hughes4f251be2012-11-01 16:33:29 -0700350 // http://code.google.com/p/android/issues/detail?id=37410
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800351 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700352}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800353
354static void* SignalHandlerFn(void* arg) {
355 sigset_t wait_set;
356 sigfillset(&wait_set);
357 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
358}
359
360TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700361 // Check that SIGUSR1 isn't blocked.
362 sigset_t original_set;
363 sigemptyset(&original_set);
364 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
365 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
366
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800367 // Block SIGUSR1.
368 sigset_t set;
369 sigemptyset(&set);
370 sigaddset(&set, SIGUSR1);
371 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
372
Elliott Hughes19e62322013-10-15 11:23:57 -0700373 // Check that SIGUSR1 is blocked.
374 sigset_t final_set;
375 sigemptyset(&final_set);
376 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
377 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
378 // ...and that sigprocmask agrees with pthread_sigmask.
379 sigemptyset(&final_set);
380 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
381 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
382
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800383 // Spawn a thread that calls sigwait and tells us what it received.
384 pthread_t signal_thread;
385 int received_signal = -1;
386 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
387
388 // Send that thread SIGUSR1.
389 pthread_kill(signal_thread, SIGUSR1);
390
391 // See what it got.
392 void* join_result;
393 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
394 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700395 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700396
397 // Restore the original signal mask.
398 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800399}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800400
Elliott Hughes725b2a92016-03-23 11:20:47 -0700401static void test_pthread_setname_np__pthread_getname_np(pthread_t t) {
402 ASSERT_EQ(0, pthread_setname_np(t, "short"));
403 char name[32];
404 ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name)));
405 ASSERT_STREQ("short", name);
406
Elliott Hughesd1aea302015-04-25 10:05:24 -0700407 // The limit is 15 characters --- the kernel's buffer is 16, but includes a NUL.
Elliott Hughes725b2a92016-03-23 11:20:47 -0700408 ASSERT_EQ(0, pthread_setname_np(t, "123456789012345"));
409 ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name)));
410 ASSERT_STREQ("123456789012345", name);
411
412 ASSERT_EQ(ERANGE, pthread_setname_np(t, "1234567890123456"));
413
414 // The passed-in buffer should be at least 16 bytes.
415 ASSERT_EQ(0, pthread_getname_np(t, name, 16));
416 ASSERT_EQ(ERANGE, pthread_getname_np(t, name, 15));
Elliott Hughes3e898472013-02-12 16:40:24 +0000417}
418
Elliott Hughes725b2a92016-03-23 11:20:47 -0700419TEST(pthread, pthread_setname_np__pthread_getname_np__self) {
420 test_pthread_setname_np__pthread_getname_np(pthread_self());
Elliott Hughes3e898472013-02-12 16:40:24 +0000421}
422
Elliott Hughes725b2a92016-03-23 11:20:47 -0700423TEST(pthread, pthread_setname_np__pthread_getname_np__other) {
424 SpinFunctionHelper spin_helper;
Yabin Cui63481602014-12-01 17:41:04 -0800425
Elliott Hughes725b2a92016-03-23 11:20:47 -0700426 pthread_t t;
Elliott Hughes4d098ca2016-04-11 12:43:05 -0700427 ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr));
428 test_pthread_setname_np__pthread_getname_np(t);
429 spin_helper.UnSpin();
430 ASSERT_EQ(0, pthread_join(t, nullptr));
431}
432
433// http://b/28051133: a kernel misfeature means that you can't change the
434// name of another thread if you've set PR_SET_DUMPABLE to 0.
435TEST(pthread, pthread_setname_np__pthread_getname_np__other_PR_SET_DUMPABLE) {
436 ASSERT_EQ(0, prctl(PR_SET_DUMPABLE, 0)) << strerror(errno);
437
438 SpinFunctionHelper spin_helper;
439
440 pthread_t t;
441 ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr));
Elliott Hughes725b2a92016-03-23 11:20:47 -0700442 test_pthread_setname_np__pthread_getname_np(t);
443 spin_helper.UnSpin();
444 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes3e898472013-02-12 16:40:24 +0000445}
446
Elliott Hughes11859d42017-02-13 17:59:29 -0800447TEST_F(pthread_DeathTest, pthread_setname_np__no_such_thread) {
Elliott Hughesbcb15292017-02-07 21:05:30 +0000448 pthread_t dead_thread;
449 MakeDeadThread(dead_thread);
450
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800451 EXPECT_DEATH(pthread_setname_np(dead_thread, "short 3"), "invalid pthread_t");
452}
453
454TEST_F(pthread_DeathTest, pthread_setname_np__null_thread) {
455 pthread_t null_thread = 0;
456 EXPECT_EQ(ENOENT, pthread_setname_np(null_thread, "short 3"));
Elliott Hughes11859d42017-02-13 17:59:29 -0800457}
458
459TEST_F(pthread_DeathTest, pthread_getname_np__no_such_thread) {
460 pthread_t dead_thread;
461 MakeDeadThread(dead_thread);
462
Elliott Hughesbcb15292017-02-07 21:05:30 +0000463 char name[64];
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800464 EXPECT_DEATH(pthread_getname_np(dead_thread, name, sizeof(name)), "invalid pthread_t");
465}
466
467TEST_F(pthread_DeathTest, pthread_getname_np__null_thread) {
468 pthread_t null_thread = 0;
469
470 char name[64];
471 EXPECT_EQ(ENOENT, pthread_getname_np(null_thread, name, sizeof(name)));
Elliott Hughesbcb15292017-02-07 21:05:30 +0000472}
473
Elliott Hughes9d23e042013-02-15 19:21:51 -0800474TEST(pthread, pthread_kill__0) {
475 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
476 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
477}
478
479TEST(pthread, pthread_kill__invalid_signal) {
480 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
481}
482
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800483static void pthread_kill__in_signal_handler_helper(int signal_number) {
484 static int count = 0;
485 ASSERT_EQ(SIGALRM, signal_number);
486 if (++count == 1) {
487 // Can we call pthread_kill from a signal handler?
488 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
489 }
490}
491
492TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800493 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800494 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
495}
496
Elliott Hughes11859d42017-02-13 17:59:29 -0800497TEST_F(pthread_DeathTest, pthread_detach__no_such_thread) {
Elliott Hughes7484c212017-02-02 02:41:38 +0000498 pthread_t dead_thread;
499 MakeDeadThread(dead_thread);
500
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800501 EXPECT_DEATH(pthread_detach(dead_thread), "invalid pthread_t");
502}
503
504TEST_F(pthread_DeathTest, pthread_detach__null_thread) {
505 pthread_t null_thread = 0;
506 EXPECT_EQ(ESRCH, pthread_detach(null_thread));
Elliott Hughes7484c212017-02-02 02:41:38 +0000507}
508
Jeff Hao9b06cc32013-08-15 14:51:16 -0700509TEST(pthread, pthread_getcpuclockid__clock_gettime) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700510 SpinFunctionHelper spin_helper;
Yabin Cui63481602014-12-01 17:41:04 -0800511
Jeff Hao9b06cc32013-08-15 14:51:16 -0700512 pthread_t t;
Elliott Hughes725b2a92016-03-23 11:20:47 -0700513 ASSERT_EQ(0, pthread_create(&t, NULL, spin_helper.GetFunction(), NULL));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700514
515 clockid_t c;
516 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
517 timespec ts;
518 ASSERT_EQ(0, clock_gettime(c, &ts));
Elliott Hughes725b2a92016-03-23 11:20:47 -0700519 spin_helper.UnSpin();
Yabin Cuia36158a2015-11-16 21:06:16 -0800520 ASSERT_EQ(0, pthread_join(t, nullptr));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700521}
522
Elliott Hughes11859d42017-02-13 17:59:29 -0800523TEST_F(pthread_DeathTest, pthread_getcpuclockid__no_such_thread) {
Elliott Hughesbcb15292017-02-07 21:05:30 +0000524 pthread_t dead_thread;
525 MakeDeadThread(dead_thread);
526
527 clockid_t c;
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800528 EXPECT_DEATH(pthread_getcpuclockid(dead_thread, &c), "invalid pthread_t");
529}
530
531TEST_F(pthread_DeathTest, pthread_getcpuclockid__null_thread) {
532 pthread_t null_thread = 0;
533 clockid_t c;
534 EXPECT_EQ(ESRCH, pthread_getcpuclockid(null_thread, &c));
Elliott Hughesbcb15292017-02-07 21:05:30 +0000535}
536
Elliott Hughes11859d42017-02-13 17:59:29 -0800537TEST_F(pthread_DeathTest, pthread_getschedparam__no_such_thread) {
Elliott Hughesbcb15292017-02-07 21:05:30 +0000538 pthread_t dead_thread;
539 MakeDeadThread(dead_thread);
540
541 int policy;
542 sched_param param;
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800543 EXPECT_DEATH(pthread_getschedparam(dead_thread, &policy, &param), "invalid pthread_t");
544}
545
546TEST_F(pthread_DeathTest, pthread_getschedparam__null_thread) {
547 pthread_t null_thread = 0;
548 int policy;
549 sched_param param;
550 EXPECT_EQ(ESRCH, pthread_getschedparam(null_thread, &policy, &param));
Elliott Hughesbcb15292017-02-07 21:05:30 +0000551}
552
Elliott Hughes11859d42017-02-13 17:59:29 -0800553TEST_F(pthread_DeathTest, pthread_setschedparam__no_such_thread) {
Elliott Hughesbcb15292017-02-07 21:05:30 +0000554 pthread_t dead_thread;
555 MakeDeadThread(dead_thread);
556
557 int policy = 0;
558 sched_param param;
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800559 EXPECT_DEATH(pthread_setschedparam(dead_thread, policy, &param), "invalid pthread_t");
560}
561
562TEST_F(pthread_DeathTest, pthread_setschedparam__null_thread) {
563 pthread_t null_thread = 0;
564 int policy = 0;
565 sched_param param;
566 EXPECT_EQ(ESRCH, pthread_setschedparam(null_thread, policy, &param));
Elliott Hughesbcb15292017-02-07 21:05:30 +0000567}
568
Elliott Hughesdff08ce2017-10-16 09:58:45 -0700569TEST_F(pthread_DeathTest, pthread_setschedprio__no_such_thread) {
570 pthread_t dead_thread;
571 MakeDeadThread(dead_thread);
572
573 EXPECT_DEATH(pthread_setschedprio(dead_thread, 123), "invalid pthread_t");
574}
575
576TEST_F(pthread_DeathTest, pthread_setschedprio__null_thread) {
577 pthread_t null_thread = 0;
578 EXPECT_EQ(ESRCH, pthread_setschedprio(null_thread, 123));
579}
580
Elliott Hughes11859d42017-02-13 17:59:29 -0800581TEST_F(pthread_DeathTest, pthread_join__no_such_thread) {
Elliott Hughes7484c212017-02-02 02:41:38 +0000582 pthread_t dead_thread;
583 MakeDeadThread(dead_thread);
584
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800585 EXPECT_DEATH(pthread_join(dead_thread, NULL), "invalid pthread_t");
586}
587
588TEST_F(pthread_DeathTest, pthread_join__null_thread) {
589 pthread_t null_thread = 0;
590 EXPECT_EQ(ESRCH, pthread_join(null_thread, NULL));
Elliott Hughes7484c212017-02-02 02:41:38 +0000591}
592
Elliott Hughes11859d42017-02-13 17:59:29 -0800593TEST_F(pthread_DeathTest, pthread_kill__no_such_thread) {
Elliott Hughes7484c212017-02-02 02:41:38 +0000594 pthread_t dead_thread;
595 MakeDeadThread(dead_thread);
596
Elliott Hughes6ce686c2017-02-21 13:15:20 -0800597 EXPECT_DEATH(pthread_kill(dead_thread, 0), "invalid pthread_t");
598}
599
600TEST_F(pthread_DeathTest, pthread_kill__null_thread) {
601 pthread_t null_thread = 0;
602 EXPECT_EQ(ESRCH, pthread_kill(null_thread, 0));
Elliott Hughes7484c212017-02-02 02:41:38 +0000603}
604
msg5550f020d12013-06-06 14:59:28 -0400605TEST(pthread, pthread_join__multijoin) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700606 SpinFunctionHelper spin_helper;
msg5550f020d12013-06-06 14:59:28 -0400607
608 pthread_t t1;
Elliott Hughes725b2a92016-03-23 11:20:47 -0700609 ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL));
msg5550f020d12013-06-06 14:59:28 -0400610
611 pthread_t t2;
612 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
613
614 sleep(1); // (Give t2 a chance to call pthread_join.)
615
616 // Multiple joins to the same thread should fail.
617 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
618
Elliott Hughes725b2a92016-03-23 11:20:47 -0700619 spin_helper.UnSpin();
msg5550f020d12013-06-06 14:59:28 -0400620
621 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
622 void* join_result;
623 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700624 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400625}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700626
Elliott Hughes70b24b12013-11-15 11:51:07 -0800627TEST(pthread, pthread_join__race) {
628 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
629 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
630 for (size_t i = 0; i < 1024; ++i) {
Yabin Cuia36158a2015-11-16 21:06:16 -0800631 size_t stack_size = 640*1024;
Elliott Hughes70b24b12013-11-15 11:51:07 -0800632 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
633
634 pthread_attr_t a;
635 pthread_attr_init(&a);
636 pthread_attr_setstack(&a, stack, stack_size);
637
638 pthread_t t;
639 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
640 ASSERT_EQ(0, pthread_join(t, NULL));
641 ASSERT_EQ(0, munmap(stack, stack_size));
642 }
643}
644
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700645static void* GetActualGuardSizeFn(void* arg) {
646 pthread_attr_t attributes;
647 pthread_getattr_np(pthread_self(), &attributes);
648 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
649 return NULL;
650}
651
652static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
653 size_t result;
654 pthread_t t;
655 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700656 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700657 return result;
658}
659
660static void* GetActualStackSizeFn(void* arg) {
661 pthread_attr_t attributes;
662 pthread_getattr_np(pthread_self(), &attributes);
663 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
664 return NULL;
665}
666
667static size_t GetActualStackSize(const pthread_attr_t& attributes) {
668 size_t result;
669 pthread_t t;
670 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700671 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700672 return result;
673}
674
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700675TEST(pthread, pthread_attr_setguardsize_tiny) {
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700676 pthread_attr_t attributes;
677 ASSERT_EQ(0, pthread_attr_init(&attributes));
678
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700679 // No such thing as too small: will be rounded up to one page by pthread_create.
680 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
681 size_t guard_size;
682 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
683 ASSERT_EQ(128U, guard_size);
684 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700685}
686
687TEST(pthread, pthread_attr_setguardsize_reasonable) {
688 pthread_attr_t attributes;
689 ASSERT_EQ(0, pthread_attr_init(&attributes));
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700690
691 // Large enough and a multiple of the page size.
692 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700693 size_t guard_size;
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700694 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
695 ASSERT_EQ(32*1024U, guard_size);
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700696 ASSERT_EQ(32*1024U, GetActualGuardSize(attributes));
697}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700698
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700699TEST(pthread, pthread_attr_setguardsize_needs_rounding) {
700 pthread_attr_t attributes;
701 ASSERT_EQ(0, pthread_attr_init(&attributes));
702
703 // Large enough but not a multiple of the page size.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700704 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700705 size_t guard_size;
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700706 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
707 ASSERT_EQ(32*1024U + 1, guard_size);
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700708 ASSERT_EQ(36*1024U, GetActualGuardSize(attributes));
709}
710
711TEST(pthread, pthread_attr_setguardsize_enormous) {
712 pthread_attr_t attributes;
713 ASSERT_EQ(0, pthread_attr_init(&attributes));
714
715 // Larger than the stack itself. (Historically we mistakenly carved
716 // the guard out of the stack itself, rather than adding it after the
717 // end.)
718 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024*1024));
719 size_t guard_size;
720 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
721 ASSERT_EQ(32*1024*1024U, guard_size);
722 ASSERT_EQ(32*1024*1024U, GetActualGuardSize(attributes));
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700723}
724
725TEST(pthread, pthread_attr_setstacksize) {
726 pthread_attr_t attributes;
727 ASSERT_EQ(0, pthread_attr_init(&attributes));
728
729 // Get the default stack size.
730 size_t default_stack_size;
731 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
732
733 // Too small.
734 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
735 size_t stack_size;
736 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
737 ASSERT_EQ(default_stack_size, stack_size);
738 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
739
Yabin Cui917d3902015-01-08 12:32:42 -0800740 // Large enough and a multiple of the page size; may be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700741 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
742 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
743 ASSERT_EQ(32*1024U, stack_size);
Yabin Cui917d3902015-01-08 12:32:42 -0800744 ASSERT_GE(GetActualStackSize(attributes), 32*1024U);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700745
Yabin Cui917d3902015-01-08 12:32:42 -0800746 // Large enough but not aligned; will be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700747 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
748 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
749 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800750#if defined(__BIONIC__)
Yabin Cui917d3902015-01-08 12:32:42 -0800751 ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800752#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700753 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
754 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800755#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700756}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700757
Yabin Cui76615da2015-03-17 14:22:09 -0700758TEST(pthread, pthread_rwlockattr_smoke) {
759 pthread_rwlockattr_t attr;
760 ASSERT_EQ(0, pthread_rwlockattr_init(&attr));
761
762 int pshared_value_array[] = {PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED};
763 for (size_t i = 0; i < sizeof(pshared_value_array) / sizeof(pshared_value_array[0]); ++i) {
764 ASSERT_EQ(0, pthread_rwlockattr_setpshared(&attr, pshared_value_array[i]));
765 int pshared;
766 ASSERT_EQ(0, pthread_rwlockattr_getpshared(&attr, &pshared));
767 ASSERT_EQ(pshared_value_array[i], pshared);
768 }
769
770 int kind_array[] = {PTHREAD_RWLOCK_PREFER_READER_NP,
771 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP};
772 for (size_t i = 0; i < sizeof(kind_array) / sizeof(kind_array[0]); ++i) {
773 ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_array[i]));
774 int kind;
775 ASSERT_EQ(0, pthread_rwlockattr_getkind_np(&attr, &kind));
776 ASSERT_EQ(kind_array[i], kind);
777 }
778
779 ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr));
780}
781
782TEST(pthread, pthread_rwlock_init_same_as_PTHREAD_RWLOCK_INITIALIZER) {
783 pthread_rwlock_t lock1 = PTHREAD_RWLOCK_INITIALIZER;
784 pthread_rwlock_t lock2;
785 ASSERT_EQ(0, pthread_rwlock_init(&lock2, NULL));
786 ASSERT_EQ(0, memcmp(&lock1, &lock2, sizeof(lock1)));
787}
788
Elliott Hughesc3f11402013-10-30 14:40:09 -0700789TEST(pthread, pthread_rwlock_smoke) {
790 pthread_rwlock_t l;
791 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
792
Calin Juravle76f352e2014-05-19 13:41:10 +0100793 // Single read lock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700794 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
795 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
796
Calin Juravle76f352e2014-05-19 13:41:10 +0100797 // Multiple read lock
798 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
799 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
800 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
801 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
802
803 // Write lock
Calin Juravle92687e42014-05-22 19:21:22 +0100804 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
805 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100806
807 // Try writer lock
808 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
809 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
810 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
811 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
812
813 // Try reader lock
814 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
815 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
816 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
817 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
818 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
819
820 // Try writer lock after unlock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700821 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
822 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
823
Calin Juravle76f352e2014-05-19 13:41:10 +0100824 // EDEADLK in "read after write"
825 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
826 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
827 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
828
829 // EDEADLK in "write after write"
830 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
831 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
832 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100833
Elliott Hughesc3f11402013-10-30 14:40:09 -0700834 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
835}
836
Yabin Cui08ee8d22015-02-11 17:04:36 -0800837struct RwlockWakeupHelperArg {
838 pthread_rwlock_t lock;
839 enum Progress {
840 LOCK_INITIALIZED,
841 LOCK_WAITING,
842 LOCK_RELEASED,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800843 LOCK_ACCESSED,
844 LOCK_TIMEDOUT,
Yabin Cui08ee8d22015-02-11 17:04:36 -0800845 };
846 std::atomic<Progress> progress;
Yabin Cuif7969852015-04-02 17:47:48 -0700847 std::atomic<pid_t> tid;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800848 std::function<int (pthread_rwlock_t*)> trylock_function;
849 std::function<int (pthread_rwlock_t*)> lock_function;
850 std::function<int (pthread_rwlock_t*, const timespec*)> timed_lock_function;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800851};
852
Yabin Cuic9a659c2015-11-05 15:36:08 -0800853static void pthread_rwlock_wakeup_helper(RwlockWakeupHelperArg* arg) {
Yabin Cuif7969852015-04-02 17:47:48 -0700854 arg->tid = gettid();
Yabin Cui08ee8d22015-02-11 17:04:36 -0800855 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
856 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
857
Yabin Cuic9a659c2015-11-05 15:36:08 -0800858 ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock));
859 ASSERT_EQ(0, arg->lock_function(&arg->lock));
Yabin Cui08ee8d22015-02-11 17:04:36 -0800860 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
861 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
862
863 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
864}
865
Yabin Cuic9a659c2015-11-05 15:36:08 -0800866static void test_pthread_rwlock_reader_wakeup_writer(std::function<int (pthread_rwlock_t*)> lock_function) {
Yabin Cui08ee8d22015-02-11 17:04:36 -0800867 RwlockWakeupHelperArg wakeup_arg;
868 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
869 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
870 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -0700871 wakeup_arg.tid = 0;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800872 wakeup_arg.trylock_function = pthread_rwlock_trywrlock;
873 wakeup_arg.lock_function = lock_function;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800874
875 pthread_t thread;
876 ASSERT_EQ(0, pthread_create(&thread, NULL,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800877 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg));
Yabin Cuif7969852015-04-02 17:47:48 -0700878 WaitUntilThreadSleep(wakeup_arg.tid);
879 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
880
Yabin Cui08ee8d22015-02-11 17:04:36 -0800881 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
882 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
883
884 ASSERT_EQ(0, pthread_join(thread, NULL));
885 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
886 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
887}
888
Yabin Cuic9a659c2015-11-05 15:36:08 -0800889TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
890 test_pthread_rwlock_reader_wakeup_writer(pthread_rwlock_wrlock);
Yabin Cui08ee8d22015-02-11 17:04:36 -0800891}
892
Yabin Cuic9a659c2015-11-05 15:36:08 -0800893TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait) {
894 timespec ts;
895 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
896 ts.tv_sec += 1;
897 test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) {
898 return pthread_rwlock_timedwrlock(lock, &ts);
899 });
900}
901
902static void test_pthread_rwlock_writer_wakeup_reader(std::function<int (pthread_rwlock_t*)> lock_function) {
Yabin Cui08ee8d22015-02-11 17:04:36 -0800903 RwlockWakeupHelperArg wakeup_arg;
904 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
905 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
906 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -0700907 wakeup_arg.tid = 0;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800908 wakeup_arg.trylock_function = pthread_rwlock_tryrdlock;
909 wakeup_arg.lock_function = lock_function;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800910
911 pthread_t thread;
912 ASSERT_EQ(0, pthread_create(&thread, NULL,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800913 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg));
Yabin Cuif7969852015-04-02 17:47:48 -0700914 WaitUntilThreadSleep(wakeup_arg.tid);
915 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
916
Yabin Cui08ee8d22015-02-11 17:04:36 -0800917 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
918 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
919
920 ASSERT_EQ(0, pthread_join(thread, NULL));
921 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
922 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
923}
924
Yabin Cuic9a659c2015-11-05 15:36:08 -0800925TEST(pthread, pthread_rwlock_writer_wakeup_reader) {
926 test_pthread_rwlock_writer_wakeup_reader(pthread_rwlock_rdlock);
927}
928
929TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait) {
930 timespec ts;
931 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
932 ts.tv_sec += 1;
933 test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) {
934 return pthread_rwlock_timedrdlock(lock, &ts);
935 });
936}
937
938static void pthread_rwlock_wakeup_timeout_helper(RwlockWakeupHelperArg* arg) {
939 arg->tid = gettid();
940 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
941 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
942
943 ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock));
944
945 timespec ts;
946 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
947 ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
948 ts.tv_nsec = -1;
949 ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts));
950 ts.tv_nsec = NS_PER_S;
951 ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts));
952 ts.tv_nsec = NS_PER_S - 1;
953 ts.tv_sec = -1;
954 ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
955 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
956 ts.tv_sec += 1;
957 ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
958 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, arg->progress);
959 arg->progress = RwlockWakeupHelperArg::LOCK_TIMEDOUT;
960}
961
962TEST(pthread, pthread_rwlock_timedrdlock_timeout) {
963 RwlockWakeupHelperArg wakeup_arg;
964 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
965 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
966 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
967 wakeup_arg.tid = 0;
968 wakeup_arg.trylock_function = pthread_rwlock_tryrdlock;
969 wakeup_arg.timed_lock_function = pthread_rwlock_timedrdlock;
970
971 pthread_t thread;
972 ASSERT_EQ(0, pthread_create(&thread, nullptr,
973 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg));
974 WaitUntilThreadSleep(wakeup_arg.tid);
975 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
976
977 ASSERT_EQ(0, pthread_join(thread, nullptr));
978 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress);
979 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
980 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
981}
982
983TEST(pthread, pthread_rwlock_timedwrlock_timeout) {
984 RwlockWakeupHelperArg wakeup_arg;
985 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
986 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
987 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
988 wakeup_arg.tid = 0;
989 wakeup_arg.trylock_function = pthread_rwlock_trywrlock;
990 wakeup_arg.timed_lock_function = pthread_rwlock_timedwrlock;
991
992 pthread_t thread;
993 ASSERT_EQ(0, pthread_create(&thread, nullptr,
994 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg));
995 WaitUntilThreadSleep(wakeup_arg.tid);
996 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
997
998 ASSERT_EQ(0, pthread_join(thread, nullptr));
999 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress);
1000 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
1001 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
1002}
1003
Yabin Cui76615da2015-03-17 14:22:09 -07001004class RwlockKindTestHelper {
1005 private:
1006 struct ThreadArg {
1007 RwlockKindTestHelper* helper;
1008 std::atomic<pid_t>& tid;
1009
1010 ThreadArg(RwlockKindTestHelper* helper, std::atomic<pid_t>& tid)
1011 : helper(helper), tid(tid) { }
1012 };
1013
1014 public:
1015 pthread_rwlock_t lock;
1016
1017 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -07001018 explicit RwlockKindTestHelper(int kind_type) {
Yabin Cui76615da2015-03-17 14:22:09 -07001019 InitRwlock(kind_type);
1020 }
1021
1022 ~RwlockKindTestHelper() {
1023 DestroyRwlock();
1024 }
1025
1026 void CreateWriterThread(pthread_t& thread, std::atomic<pid_t>& tid) {
1027 tid = 0;
1028 ThreadArg* arg = new ThreadArg(this, tid);
1029 ASSERT_EQ(0, pthread_create(&thread, NULL,
1030 reinterpret_cast<void* (*)(void*)>(WriterThreadFn), arg));
1031 }
1032
1033 void CreateReaderThread(pthread_t& thread, std::atomic<pid_t>& tid) {
1034 tid = 0;
1035 ThreadArg* arg = new ThreadArg(this, tid);
1036 ASSERT_EQ(0, pthread_create(&thread, NULL,
1037 reinterpret_cast<void* (*)(void*)>(ReaderThreadFn), arg));
1038 }
1039
1040 private:
1041 void InitRwlock(int kind_type) {
1042 pthread_rwlockattr_t attr;
1043 ASSERT_EQ(0, pthread_rwlockattr_init(&attr));
1044 ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_type));
1045 ASSERT_EQ(0, pthread_rwlock_init(&lock, &attr));
1046 ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr));
1047 }
1048
1049 void DestroyRwlock() {
1050 ASSERT_EQ(0, pthread_rwlock_destroy(&lock));
1051 }
1052
1053 static void WriterThreadFn(ThreadArg* arg) {
1054 arg->tid = gettid();
1055
1056 RwlockKindTestHelper* helper = arg->helper;
1057 ASSERT_EQ(0, pthread_rwlock_wrlock(&helper->lock));
1058 ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock));
1059 delete arg;
1060 }
1061
1062 static void ReaderThreadFn(ThreadArg* arg) {
1063 arg->tid = gettid();
1064
1065 RwlockKindTestHelper* helper = arg->helper;
1066 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper->lock));
1067 ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock));
1068 delete arg;
1069 }
1070};
1071
1072TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_READER_NP) {
1073 RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_READER_NP);
1074 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock));
1075
1076 pthread_t writer_thread;
1077 std::atomic<pid_t> writer_tid;
1078 helper.CreateWriterThread(writer_thread, writer_tid);
1079 WaitUntilThreadSleep(writer_tid);
1080
1081 pthread_t reader_thread;
1082 std::atomic<pid_t> reader_tid;
1083 helper.CreateReaderThread(reader_thread, reader_tid);
1084 ASSERT_EQ(0, pthread_join(reader_thread, NULL));
1085
1086 ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock));
1087 ASSERT_EQ(0, pthread_join(writer_thread, NULL));
1088}
1089
1090TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) {
1091 RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
1092 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock));
1093
1094 pthread_t writer_thread;
1095 std::atomic<pid_t> writer_tid;
1096 helper.CreateWriterThread(writer_thread, writer_tid);
1097 WaitUntilThreadSleep(writer_tid);
1098
1099 pthread_t reader_thread;
1100 std::atomic<pid_t> reader_tid;
1101 helper.CreateReaderThread(reader_thread, reader_tid);
1102 WaitUntilThreadSleep(reader_tid);
1103
1104 ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock));
1105 ASSERT_EQ(0, pthread_join(writer_thread, NULL));
1106 ASSERT_EQ(0, pthread_join(reader_thread, NULL));
1107}
1108
Elliott Hughes1728b232014-05-14 10:02:03 -07001109static int g_once_fn_call_count = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -07001110static void OnceFn() {
Elliott Hughes1728b232014-05-14 10:02:03 -07001111 ++g_once_fn_call_count;
Elliott Hughesc3f11402013-10-30 14:40:09 -07001112}
1113
1114TEST(pthread, pthread_once_smoke) {
1115 pthread_once_t once_control = PTHREAD_ONCE_INIT;
1116 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
1117 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
Elliott Hughes1728b232014-05-14 10:02:03 -07001118 ASSERT_EQ(1, g_once_fn_call_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001119}
1120
Elliott Hughes3694ec62014-05-14 11:46:08 -07001121static std::string pthread_once_1934122_result = "";
1122
1123static void Routine2() {
1124 pthread_once_1934122_result += "2";
1125}
1126
1127static void Routine1() {
1128 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
1129 pthread_once_1934122_result += "1";
1130 pthread_once(&once_control_2, &Routine2);
1131}
1132
1133TEST(pthread, pthread_once_1934122) {
1134 // Very old versions of Android couldn't call pthread_once from a
1135 // pthread_once init routine. http://b/1934122.
1136 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
1137 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
1138 ASSERT_EQ("12", pthread_once_1934122_result);
1139}
1140
Elliott Hughes1728b232014-05-14 10:02:03 -07001141static int g_atfork_prepare_calls = 0;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001142static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; }
1143static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 2; }
Elliott Hughes1728b232014-05-14 10:02:03 -07001144static int g_atfork_parent_calls = 0;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001145static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 1; }
1146static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 2; }
Elliott Hughes1728b232014-05-14 10:02:03 -07001147static int g_atfork_child_calls = 0;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001148static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 1; }
1149static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 2; }
Elliott Hughesc3f11402013-10-30 14:40:09 -07001150
Dmitriy Ivanov00e37812014-11-20 16:53:47 -08001151TEST(pthread, pthread_atfork_smoke) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001152 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
1153 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
Elliott Hughesc3f11402013-10-30 14:40:09 -07001154
Elliott Hughes33697a02016-01-26 13:04:57 -08001155 pid_t pid = fork();
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001156 ASSERT_NE(-1, pid) << strerror(errno);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001157
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001158 // Child and parent calls are made in the order they were registered.
1159 if (pid == 0) {
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001160 ASSERT_EQ(12, g_atfork_child_calls);
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001161 _exit(0);
1162 }
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001163 ASSERT_EQ(12, g_atfork_parent_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001164
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001165 // Prepare calls are made in the reverse order.
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001166 ASSERT_EQ(21, g_atfork_prepare_calls);
Elliott Hughes33697a02016-01-26 13:04:57 -08001167 AssertChildExited(pid, 0);
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001168}
1169
Elliott Hughesc3f11402013-10-30 14:40:09 -07001170TEST(pthread, pthread_attr_getscope) {
1171 pthread_attr_t attr;
1172 ASSERT_EQ(0, pthread_attr_init(&attr));
1173
1174 int scope;
1175 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
1176 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
1177}
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001178
1179TEST(pthread, pthread_condattr_init) {
1180 pthread_condattr_t attr;
1181 pthread_condattr_init(&attr);
1182
1183 clockid_t clock;
1184 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1185 ASSERT_EQ(CLOCK_REALTIME, clock);
1186
1187 int pshared;
1188 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
1189 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
1190}
1191
1192TEST(pthread, pthread_condattr_setclock) {
1193 pthread_condattr_t attr;
1194 pthread_condattr_init(&attr);
1195
1196 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
1197 clockid_t clock;
1198 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1199 ASSERT_EQ(CLOCK_REALTIME, clock);
1200
1201 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
1202 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1203 ASSERT_EQ(CLOCK_MONOTONIC, clock);
1204
1205 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
1206}
1207
1208TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
Yabin Cui32651b82015-03-13 20:30:00 -07001209#if defined(__BIONIC__)
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001210 pthread_condattr_t attr;
1211 pthread_condattr_init(&attr);
1212
1213 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
1214 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
1215
1216 pthread_cond_t cond_var;
1217 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
1218
1219 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
1220 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
1221
Yabin Cui32651b82015-03-13 20:30:00 -07001222 attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private));
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001223 clockid_t clock;
1224 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1225 ASSERT_EQ(CLOCK_MONOTONIC, clock);
1226 int pshared;
1227 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
1228 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
Yabin Cui32651b82015-03-13 20:30:00 -07001229#else // !defined(__BIONIC__)
1230 GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n";
1231#endif // !defined(__BIONIC__)
1232}
1233
1234class pthread_CondWakeupTest : public ::testing::Test {
1235 protected:
1236 pthread_mutex_t mutex;
1237 pthread_cond_t cond;
1238
1239 enum Progress {
1240 INITIALIZED,
1241 WAITING,
1242 SIGNALED,
1243 FINISHED,
1244 };
1245 std::atomic<Progress> progress;
1246 pthread_t thread;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001247 std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function;
Yabin Cui32651b82015-03-13 20:30:00 -07001248
1249 protected:
Yabin Cuic9a659c2015-11-05 15:36:08 -08001250 void SetUp() override {
1251 ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr));
1252 }
1253
1254 void InitCond(clockid_t clock=CLOCK_REALTIME) {
1255 pthread_condattr_t attr;
1256 ASSERT_EQ(0, pthread_condattr_init(&attr));
1257 ASSERT_EQ(0, pthread_condattr_setclock(&attr, clock));
1258 ASSERT_EQ(0, pthread_cond_init(&cond, &attr));
1259 ASSERT_EQ(0, pthread_condattr_destroy(&attr));
1260 }
1261
1262 void StartWaitingThread(std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function) {
Yabin Cui32651b82015-03-13 20:30:00 -07001263 progress = INITIALIZED;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001264 this->wait_function = wait_function;
1265 ASSERT_EQ(0, pthread_create(&thread, NULL, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this));
1266 while (progress != WAITING) {
Yabin Cui32651b82015-03-13 20:30:00 -07001267 usleep(5000);
1268 }
1269 usleep(5000);
1270 }
1271
Yabin Cuic9a659c2015-11-05 15:36:08 -08001272 void TearDown() override {
1273 ASSERT_EQ(0, pthread_join(thread, nullptr));
1274 ASSERT_EQ(FINISHED, progress);
1275 ASSERT_EQ(0, pthread_cond_destroy(&cond));
1276 ASSERT_EQ(0, pthread_mutex_destroy(&mutex));
1277 }
1278
Yabin Cui32651b82015-03-13 20:30:00 -07001279 private:
1280 static void WaitThreadFn(pthread_CondWakeupTest* test) {
1281 ASSERT_EQ(0, pthread_mutex_lock(&test->mutex));
1282 test->progress = WAITING;
1283 while (test->progress == WAITING) {
Yabin Cuic9a659c2015-11-05 15:36:08 -08001284 ASSERT_EQ(0, test->wait_function(&test->cond, &test->mutex));
Yabin Cui32651b82015-03-13 20:30:00 -07001285 }
1286 ASSERT_EQ(SIGNALED, test->progress);
1287 test->progress = FINISHED;
1288 ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex));
1289 }
1290};
1291
Yabin Cuic9a659c2015-11-05 15:36:08 -08001292TEST_F(pthread_CondWakeupTest, signal_wait) {
1293 InitCond();
1294 StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1295 return pthread_cond_wait(cond, mutex);
1296 });
Yabin Cui32651b82015-03-13 20:30:00 -07001297 progress = SIGNALED;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001298 ASSERT_EQ(0, pthread_cond_signal(&cond));
Yabin Cui32651b82015-03-13 20:30:00 -07001299}
1300
Yabin Cuic9a659c2015-11-05 15:36:08 -08001301TEST_F(pthread_CondWakeupTest, broadcast_wait) {
1302 InitCond();
1303 StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1304 return pthread_cond_wait(cond, mutex);
1305 });
Yabin Cui32651b82015-03-13 20:30:00 -07001306 progress = SIGNALED;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001307 ASSERT_EQ(0, pthread_cond_broadcast(&cond));
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001308}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001309
Yabin Cuic9a659c2015-11-05 15:36:08 -08001310TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_REALTIME) {
1311 InitCond(CLOCK_REALTIME);
Elliott Hughes0e714a52014-03-03 16:42:47 -08001312 timespec ts;
1313 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001314 ts.tv_sec += 1;
1315 StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1316 return pthread_cond_timedwait(cond, mutex, &ts);
1317 });
1318 progress = SIGNALED;
1319 ASSERT_EQ(0, pthread_cond_signal(&cond));
1320}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001321
Yabin Cuic9a659c2015-11-05 15:36:08 -08001322TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC) {
1323 InitCond(CLOCK_MONOTONIC);
1324 timespec ts;
1325 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
1326 ts.tv_sec += 1;
1327 StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1328 return pthread_cond_timedwait(cond, mutex, &ts);
1329 });
1330 progress = SIGNALED;
1331 ASSERT_EQ(0, pthread_cond_signal(&cond));
1332}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001333
Yabin Cuic9a659c2015-11-05 15:36:08 -08001334TEST(pthread, pthread_cond_timedwait_timeout) {
1335 pthread_mutex_t mutex;
1336 ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr));
1337 pthread_cond_t cond;
1338 ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
1339 ASSERT_EQ(0, pthread_mutex_lock(&mutex));
1340 timespec ts;
Elliott Hughes0e714a52014-03-03 16:42:47 -08001341 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001342 ASSERT_EQ(ETIMEDOUT, pthread_cond_timedwait(&cond, &mutex, &ts));
1343 ts.tv_nsec = -1;
1344 ASSERT_EQ(EINVAL, pthread_cond_timedwait(&cond, &mutex, &ts));
1345 ts.tv_nsec = NS_PER_S;
1346 ASSERT_EQ(EINVAL, pthread_cond_timedwait(&cond, &mutex, &ts));
1347 ts.tv_nsec = NS_PER_S - 1;
1348 ts.tv_sec = -1;
1349 ASSERT_EQ(ETIMEDOUT, pthread_cond_timedwait(&cond, &mutex, &ts));
1350 ASSERT_EQ(0, pthread_mutex_unlock(&mutex));
Elliott Hughes0e714a52014-03-03 16:42:47 -08001351}
Elliott Hughes57b7a612014-08-25 17:26:50 -07001352
1353TEST(pthread, pthread_attr_getstack__main_thread) {
1354 // This test is only meaningful for the main thread, so make sure we're running on it!
1355 ASSERT_EQ(getpid(), syscall(__NR_gettid));
1356
1357 // Get the main thread's attributes.
1358 pthread_attr_t attributes;
1359 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1360
1361 // Check that we correctly report that the main thread has no guard page.
1362 size_t guard_size;
1363 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
1364 ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
1365
1366 // Get the stack base and the stack size (both ways).
1367 void* stack_base;
1368 size_t stack_size;
1369 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1370 size_t stack_size2;
1371 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1372
1373 // The two methods of asking for the stack size should agree.
1374 EXPECT_EQ(stack_size, stack_size2);
1375
Yabin Cuib0c6f2db2015-05-19 15:09:23 -07001376#if defined(__BIONIC__)
Elliott Hughes57b7a612014-08-25 17:26:50 -07001377 // What does /proc/self/maps' [stack] line say?
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001378 void* maps_stack_hi = NULL;
Elliott Hughes15dfd632015-09-22 16:40:14 -07001379 std::vector<map_record> maps;
1380 ASSERT_TRUE(Maps::parse_maps(&maps));
Elliott Hughes0b2acdf2015-10-02 18:25:19 -07001381 for (const auto& map : maps) {
Elliott Hughes15dfd632015-09-22 16:40:14 -07001382 if (map.pathname == "[stack]") {
1383 maps_stack_hi = reinterpret_cast<void*>(map.addr_end);
Elliott Hughes57b7a612014-08-25 17:26:50 -07001384 break;
1385 }
1386 }
Elliott Hughes57b7a612014-08-25 17:26:50 -07001387
Yabin Cuib0c6f2db2015-05-19 15:09:23 -07001388 // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
1389 // Remember that the stack grows down (and is mapped in on demand), so the low address of the
1390 // region isn't very interesting.
1391 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
1392
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001393 // The stack size should correspond to RLIMIT_STACK.
Elliott Hughes57b7a612014-08-25 17:26:50 -07001394 rlimit rl;
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001395 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001396 uint64_t original_rlim_cur = rl.rlim_cur;
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001397 if (rl.rlim_cur == RLIM_INFINITY) {
1398 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
1399 }
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001400 EXPECT_EQ(rl.rlim_cur, stack_size);
1401
Tom Cherryb8ab6182017-04-05 16:20:29 -07001402 auto guard = android::base::make_scope_guard([&rl, original_rlim_cur]() {
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001403 rl.rlim_cur = original_rlim_cur;
1404 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1405 });
1406
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001407 //
1408 // What if RLIMIT_STACK is smaller than the stack's current extent?
1409 //
Elliott Hughes57b7a612014-08-25 17:26:50 -07001410 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
1411 rl.rlim_max = RLIM_INFINITY;
1412 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1413
1414 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1415 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1416 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1417
1418 EXPECT_EQ(stack_size, stack_size2);
1419 ASSERT_EQ(1024U, stack_size);
1420
1421 //
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001422 // What if RLIMIT_STACK isn't a whole number of pages?
Elliott Hughes57b7a612014-08-25 17:26:50 -07001423 //
1424 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
1425 rl.rlim_max = RLIM_INFINITY;
1426 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1427
1428 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1429 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1430 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1431
1432 EXPECT_EQ(stack_size, stack_size2);
1433 ASSERT_EQ(6666U, stack_size);
Yabin Cuib0c6f2db2015-05-19 15:09:23 -07001434#endif
Elliott Hughes57b7a612014-08-25 17:26:50 -07001435}
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001436
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001437struct GetStackSignalHandlerArg {
1438 volatile bool done;
Chih-Hung Hsieh9af13d22016-06-02 14:40:09 -07001439 void* signal_stack_base;
1440 size_t signal_stack_size;
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001441 void* main_stack_base;
1442 size_t main_stack_size;
1443};
1444
1445static GetStackSignalHandlerArg getstack_signal_handler_arg;
1446
1447static void getstack_signal_handler(int sig) {
1448 ASSERT_EQ(SIGUSR1, sig);
1449 // Use sleep() to make current thread be switched out by the kernel to provoke the error.
1450 sleep(1);
1451 pthread_attr_t attr;
1452 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr));
1453 void* stack_base;
1454 size_t stack_size;
1455 ASSERT_EQ(0, pthread_attr_getstack(&attr, &stack_base, &stack_size));
Chih-Hung Hsieh9af13d22016-06-02 14:40:09 -07001456
1457 // Verify if the stack used by the signal handler is the alternate stack just registered.
1458 ASSERT_LE(getstack_signal_handler_arg.signal_stack_base, &attr);
1459 ASSERT_LT(static_cast<void*>(&attr),
1460 static_cast<char*>(getstack_signal_handler_arg.signal_stack_base) +
1461 getstack_signal_handler_arg.signal_stack_size);
1462
1463 // Verify if the main thread's stack got in the signal handler is correct.
1464 ASSERT_EQ(getstack_signal_handler_arg.main_stack_base, stack_base);
1465 ASSERT_LE(getstack_signal_handler_arg.main_stack_size, stack_size);
1466
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001467 getstack_signal_handler_arg.done = true;
1468}
1469
1470// The previous code obtained the main thread's stack by reading the entry in
1471// /proc/self/task/<pid>/maps that was labeled [stack]. Unfortunately, on x86/x86_64, the kernel
1472// relies on sp0 in task state segment(tss) to label the stack map with [stack]. If the kernel
1473// switches a process while the main thread is in an alternate stack, then the kernel will label
1474// the wrong map with [stack]. This test verifies that when the above situation happens, the main
1475// thread's stack is found correctly.
1476TEST(pthread, pthread_attr_getstack_in_signal_handler) {
Yabin Cui61e4d462016-03-07 17:44:58 -08001477 // This test is only meaningful for the main thread, so make sure we're running on it!
1478 ASSERT_EQ(getpid(), syscall(__NR_gettid));
1479
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001480 const size_t sig_stack_size = 16 * 1024;
1481 void* sig_stack = mmap(NULL, sig_stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
1482 -1, 0);
1483 ASSERT_NE(MAP_FAILED, sig_stack);
1484 stack_t ss;
1485 ss.ss_sp = sig_stack;
1486 ss.ss_size = sig_stack_size;
1487 ss.ss_flags = 0;
1488 stack_t oss;
1489 ASSERT_EQ(0, sigaltstack(&ss, &oss));
1490
Yabin Cui61e4d462016-03-07 17:44:58 -08001491 pthread_attr_t attr;
1492 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr));
1493 void* main_stack_base;
1494 size_t main_stack_size;
1495 ASSERT_EQ(0, pthread_attr_getstack(&attr, &main_stack_base, &main_stack_size));
1496
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001497 ScopedSignalHandler handler(SIGUSR1, getstack_signal_handler, SA_ONSTACK);
1498 getstack_signal_handler_arg.done = false;
Chih-Hung Hsieh9af13d22016-06-02 14:40:09 -07001499 getstack_signal_handler_arg.signal_stack_base = sig_stack;
1500 getstack_signal_handler_arg.signal_stack_size = sig_stack_size;
1501 getstack_signal_handler_arg.main_stack_base = main_stack_base;
1502 getstack_signal_handler_arg.main_stack_size = main_stack_size;
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001503 kill(getpid(), SIGUSR1);
1504 ASSERT_EQ(true, getstack_signal_handler_arg.done);
1505
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001506 ASSERT_EQ(0, sigaltstack(&oss, nullptr));
1507 ASSERT_EQ(0, munmap(sig_stack, sig_stack_size));
1508}
1509
Yabin Cui917d3902015-01-08 12:32:42 -08001510static void pthread_attr_getstack_18908062_helper(void*) {
1511 char local_variable;
1512 pthread_attr_t attributes;
1513 pthread_getattr_np(pthread_self(), &attributes);
1514 void* stack_base;
1515 size_t stack_size;
1516 pthread_attr_getstack(&attributes, &stack_base, &stack_size);
1517
1518 // Test whether &local_variable is in [stack_base, stack_base + stack_size).
1519 ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable);
1520 ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size);
1521}
1522
1523// Check whether something on stack is in the range of
1524// [stack_base, stack_base + stack_size). see b/18908062.
1525TEST(pthread, pthread_attr_getstack_18908062) {
1526 pthread_t t;
1527 ASSERT_EQ(0, pthread_create(&t, NULL,
1528 reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper),
1529 NULL));
1530 pthread_join(t, NULL);
1531}
1532
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001533#if defined(__BIONIC__)
Elliott Hughesf2083612015-11-11 13:32:28 -08001534static pthread_mutex_t pthread_gettid_np_mutex = PTHREAD_MUTEX_INITIALIZER;
1535
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001536static void* pthread_gettid_np_helper(void* arg) {
1537 *reinterpret_cast<pid_t*>(arg) = gettid();
Elliott Hughesf2083612015-11-11 13:32:28 -08001538
1539 // Wait for our parent to call pthread_gettid_np on us before exiting.
1540 pthread_mutex_lock(&pthread_gettid_np_mutex);
1541 pthread_mutex_unlock(&pthread_gettid_np_mutex);
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001542 return NULL;
1543}
1544#endif
1545
1546TEST(pthread, pthread_gettid_np) {
1547#if defined(__BIONIC__)
1548 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
1549
Elliott Hughesf2083612015-11-11 13:32:28 -08001550 // Ensure the other thread doesn't exit until after we've called
1551 // pthread_gettid_np on it.
1552 pthread_mutex_lock(&pthread_gettid_np_mutex);
1553
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001554 pid_t t_gettid_result;
1555 pthread_t t;
1556 pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
1557
1558 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
1559
Elliott Hughesf2083612015-11-11 13:32:28 -08001560 // Release the other thread and wait for it to exit.
1561 pthread_mutex_unlock(&pthread_gettid_np_mutex);
Elliott Hughes34c987a2014-09-22 16:01:26 -07001562 pthread_join(t, NULL);
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001563
1564 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
1565#else
1566 GTEST_LOG_(INFO) << "This test does nothing.\n";
1567#endif
1568}
Elliott Hughes34c987a2014-09-22 16:01:26 -07001569
1570static size_t cleanup_counter = 0;
1571
Derek Xue41996952014-09-25 11:05:32 +01001572static void AbortCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001573 abort();
1574}
1575
Derek Xue41996952014-09-25 11:05:32 +01001576static void CountCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001577 ++cleanup_counter;
1578}
1579
Derek Xue41996952014-09-25 11:05:32 +01001580static void PthreadCleanupTester() {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001581 pthread_cleanup_push(CountCleanupRoutine, NULL);
1582 pthread_cleanup_push(CountCleanupRoutine, NULL);
1583 pthread_cleanup_push(AbortCleanupRoutine, NULL);
1584
1585 pthread_cleanup_pop(0); // Pop the abort without executing it.
1586 pthread_cleanup_pop(1); // Pop one count while executing it.
1587 ASSERT_EQ(1U, cleanup_counter);
1588 // Exit while the other count is still on the cleanup stack.
1589 pthread_exit(NULL);
1590
1591 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
1592 pthread_cleanup_pop(0);
1593}
1594
Derek Xue41996952014-09-25 11:05:32 +01001595static void* PthreadCleanupStartRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001596 PthreadCleanupTester();
1597 return NULL;
1598}
1599
1600TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
1601 pthread_t t;
1602 ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL));
1603 pthread_join(t, NULL);
1604 ASSERT_EQ(2U, cleanup_counter);
1605}
Derek Xue41996952014-09-25 11:05:32 +01001606
1607TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
1608 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
1609}
1610
1611TEST(pthread, pthread_mutexattr_gettype) {
1612 pthread_mutexattr_t attr;
1613 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1614
1615 int attr_type;
1616
1617 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1618 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1619 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
1620
1621 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1622 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1623 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
1624
1625 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1626 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1627 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001628
1629 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1630}
1631
Yabin Cui17393b02015-03-21 15:08:25 -07001632struct PthreadMutex {
1633 pthread_mutex_t lock;
1634
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -07001635 explicit PthreadMutex(int mutex_type) {
Yabin Cui17393b02015-03-21 15:08:25 -07001636 init(mutex_type);
1637 }
1638
1639 ~PthreadMutex() {
1640 destroy();
1641 }
1642
1643 private:
1644 void init(int mutex_type) {
1645 pthread_mutexattr_t attr;
1646 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1647 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, mutex_type));
1648 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1649 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1650 }
1651
1652 void destroy() {
1653 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1654 }
1655
1656 DISALLOW_COPY_AND_ASSIGN(PthreadMutex);
1657};
Derek Xue41996952014-09-25 11:05:32 +01001658
1659TEST(pthread, pthread_mutex_lock_NORMAL) {
Yabin Cui17393b02015-03-21 15:08:25 -07001660 PthreadMutex m(PTHREAD_MUTEX_NORMAL);
Derek Xue41996952014-09-25 11:05:32 +01001661
Yabin Cui17393b02015-03-21 15:08:25 -07001662 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1663 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Elliott Hughesd31d4c12015-12-14 17:35:10 -08001664 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1665 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock));
1666 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Derek Xue41996952014-09-25 11:05:32 +01001667}
1668
1669TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
Yabin Cui17393b02015-03-21 15:08:25 -07001670 PthreadMutex m(PTHREAD_MUTEX_ERRORCHECK);
Derek Xue41996952014-09-25 11:05:32 +01001671
Yabin Cui17393b02015-03-21 15:08:25 -07001672 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1673 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&m.lock));
1674 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1675 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1676 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock));
1677 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1678 ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
Derek Xue41996952014-09-25 11:05:32 +01001679}
1680
1681TEST(pthread, pthread_mutex_lock_RECURSIVE) {
Yabin Cui17393b02015-03-21 15:08:25 -07001682 PthreadMutex m(PTHREAD_MUTEX_RECURSIVE);
Derek Xue41996952014-09-25 11:05:32 +01001683
Yabin Cui17393b02015-03-21 15:08:25 -07001684 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1685 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1686 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1687 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1688 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
Elliott Hughesd31d4c12015-12-14 17:35:10 -08001689 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1690 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Yabin Cui17393b02015-03-21 15:08:25 -07001691 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1692 ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
1693}
1694
1695TEST(pthread, pthread_mutex_init_same_as_static_initializers) {
1696 pthread_mutex_t lock_normal = PTHREAD_MUTEX_INITIALIZER;
1697 PthreadMutex m1(PTHREAD_MUTEX_NORMAL);
1698 ASSERT_EQ(0, memcmp(&lock_normal, &m1.lock, sizeof(pthread_mutex_t)));
1699 pthread_mutex_destroy(&lock_normal);
1700
1701 pthread_mutex_t lock_errorcheck = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
1702 PthreadMutex m2(PTHREAD_MUTEX_ERRORCHECK);
1703 ASSERT_EQ(0, memcmp(&lock_errorcheck, &m2.lock, sizeof(pthread_mutex_t)));
1704 pthread_mutex_destroy(&lock_errorcheck);
1705
1706 pthread_mutex_t lock_recursive = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
1707 PthreadMutex m3(PTHREAD_MUTEX_RECURSIVE);
1708 ASSERT_EQ(0, memcmp(&lock_recursive, &m3.lock, sizeof(pthread_mutex_t)));
1709 ASSERT_EQ(0, pthread_mutex_destroy(&lock_recursive));
Derek Xue41996952014-09-25 11:05:32 +01001710}
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001711class MutexWakeupHelper {
1712 private:
Yabin Cui17393b02015-03-21 15:08:25 -07001713 PthreadMutex m;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001714 enum Progress {
1715 LOCK_INITIALIZED,
1716 LOCK_WAITING,
1717 LOCK_RELEASED,
1718 LOCK_ACCESSED
1719 };
1720 std::atomic<Progress> progress;
Yabin Cuif7969852015-04-02 17:47:48 -07001721 std::atomic<pid_t> tid;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001722
1723 static void thread_fn(MutexWakeupHelper* helper) {
Yabin Cuif7969852015-04-02 17:47:48 -07001724 helper->tid = gettid();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001725 ASSERT_EQ(LOCK_INITIALIZED, helper->progress);
1726 helper->progress = LOCK_WAITING;
1727
Yabin Cui17393b02015-03-21 15:08:25 -07001728 ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001729 ASSERT_EQ(LOCK_RELEASED, helper->progress);
Yabin Cui17393b02015-03-21 15:08:25 -07001730 ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001731
1732 helper->progress = LOCK_ACCESSED;
1733 }
1734
1735 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -07001736 explicit MutexWakeupHelper(int mutex_type) : m(mutex_type) {
Yabin Cui17393b02015-03-21 15:08:25 -07001737 }
1738
1739 void test() {
1740 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001741 progress = LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -07001742 tid = 0;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001743
1744 pthread_t thread;
1745 ASSERT_EQ(0, pthread_create(&thread, NULL,
1746 reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this));
1747
Yabin Cuif7969852015-04-02 17:47:48 -07001748 WaitUntilThreadSleep(tid);
1749 ASSERT_EQ(LOCK_WAITING, progress);
1750
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001751 progress = LOCK_RELEASED;
Yabin Cui17393b02015-03-21 15:08:25 -07001752 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001753
1754 ASSERT_EQ(0, pthread_join(thread, NULL));
1755 ASSERT_EQ(LOCK_ACCESSED, progress);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001756 }
1757};
1758
1759TEST(pthread, pthread_mutex_NORMAL_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001760 MutexWakeupHelper helper(PTHREAD_MUTEX_NORMAL);
1761 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001762}
1763
1764TEST(pthread, pthread_mutex_ERRORCHECK_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001765 MutexWakeupHelper helper(PTHREAD_MUTEX_ERRORCHECK);
1766 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001767}
1768
1769TEST(pthread, pthread_mutex_RECURSIVE_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001770 MutexWakeupHelper helper(PTHREAD_MUTEX_RECURSIVE);
1771 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001772}
1773
Yabin Cui140f3672015-02-03 10:32:00 -08001774TEST(pthread, pthread_mutex_owner_tid_limit) {
Yabin Cuie69c2452015-02-13 16:21:25 -08001775#if defined(__BIONIC__) && !defined(__LP64__)
Yabin Cui140f3672015-02-03 10:32:00 -08001776 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
1777 ASSERT_TRUE(fp != NULL);
1778 long pid_max;
1779 ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max));
1780 fclose(fp);
Yabin Cuie69c2452015-02-13 16:21:25 -08001781 // Bionic's pthread_mutex implementation on 32-bit devices uses 16 bits to represent owner tid.
Yabin Cui140f3672015-02-03 10:32:00 -08001782 ASSERT_LE(pid_max, 65536);
Yabin Cuie69c2452015-02-13 16:21:25 -08001783#else
1784 GTEST_LOG_(INFO) << "This test does nothing as 32-bit tid is supported by pthread_mutex.\n";
1785#endif
Yabin Cui140f3672015-02-03 10:32:00 -08001786}
Yabin Cuib5845722015-03-16 22:46:42 -07001787
Yabin Cuic9a659c2015-11-05 15:36:08 -08001788TEST(pthread, pthread_mutex_timedlock) {
1789 pthread_mutex_t m;
1790 ASSERT_EQ(0, pthread_mutex_init(&m, nullptr));
1791
1792 // If the mutex is already locked, pthread_mutex_timedlock should time out.
1793 ASSERT_EQ(0, pthread_mutex_lock(&m));
1794
1795 timespec ts;
1796 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1797 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
1798 ts.tv_nsec = -1;
1799 ASSERT_EQ(EINVAL, pthread_mutex_timedlock(&m, &ts));
1800 ts.tv_nsec = NS_PER_S;
1801 ASSERT_EQ(EINVAL, pthread_mutex_timedlock(&m, &ts));
1802 ts.tv_nsec = NS_PER_S - 1;
1803 ts.tv_sec = -1;
1804 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
1805
1806 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
1807 ASSERT_EQ(0, pthread_mutex_unlock(&m));
1808
1809 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1810 ts.tv_sec += 1;
1811 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
1812
1813 ASSERT_EQ(0, pthread_mutex_unlock(&m));
1814 ASSERT_EQ(0, pthread_mutex_destroy(&m));
1815}
1816
Yabin Cuib5845722015-03-16 22:46:42 -07001817class StrictAlignmentAllocator {
1818 public:
1819 void* allocate(size_t size, size_t alignment) {
1820 char* p = new char[size + alignment * 2];
1821 allocated_array.push_back(p);
1822 while (!is_strict_aligned(p, alignment)) {
1823 ++p;
1824 }
1825 return p;
1826 }
1827
1828 ~StrictAlignmentAllocator() {
Elliott Hughes0b2acdf2015-10-02 18:25:19 -07001829 for (const auto& p : allocated_array) {
1830 delete[] p;
Yabin Cuib5845722015-03-16 22:46:42 -07001831 }
1832 }
1833
1834 private:
1835 bool is_strict_aligned(char* p, size_t alignment) {
1836 return (reinterpret_cast<uintptr_t>(p) % (alignment * 2)) == alignment;
1837 }
1838
1839 std::vector<char*> allocated_array;
1840};
1841
1842TEST(pthread, pthread_types_allow_four_bytes_alignment) {
1843#if defined(__BIONIC__)
1844 // For binary compatibility with old version, we need to allow 4-byte aligned data for pthread types.
1845 StrictAlignmentAllocator allocator;
1846 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(
1847 allocator.allocate(sizeof(pthread_mutex_t), 4));
1848 ASSERT_EQ(0, pthread_mutex_init(mutex, NULL));
1849 ASSERT_EQ(0, pthread_mutex_lock(mutex));
1850 ASSERT_EQ(0, pthread_mutex_unlock(mutex));
1851 ASSERT_EQ(0, pthread_mutex_destroy(mutex));
1852
1853 pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>(
1854 allocator.allocate(sizeof(pthread_cond_t), 4));
1855 ASSERT_EQ(0, pthread_cond_init(cond, NULL));
1856 ASSERT_EQ(0, pthread_cond_signal(cond));
1857 ASSERT_EQ(0, pthread_cond_broadcast(cond));
1858 ASSERT_EQ(0, pthread_cond_destroy(cond));
1859
1860 pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>(
1861 allocator.allocate(sizeof(pthread_rwlock_t), 4));
1862 ASSERT_EQ(0, pthread_rwlock_init(rwlock, NULL));
1863 ASSERT_EQ(0, pthread_rwlock_rdlock(rwlock));
1864 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
1865 ASSERT_EQ(0, pthread_rwlock_wrlock(rwlock));
1866 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
1867 ASSERT_EQ(0, pthread_rwlock_destroy(rwlock));
1868
1869#else
1870 GTEST_LOG_(INFO) << "This test tests bionic implementation details.";
1871#endif
1872}
Christopher Ferris60907c72015-06-09 18:46:15 -07001873
1874TEST(pthread, pthread_mutex_lock_null_32) {
1875#if defined(__BIONIC__) && !defined(__LP64__)
Dan Albertbaa2a972015-08-13 16:58:50 -07001876 // For LP32, the pthread lock/unlock functions allow a NULL mutex and return
1877 // EINVAL in that case: http://b/19995172.
1878 //
1879 // We decorate the public defintion with _Nonnull so that people recompiling
1880 // their code with get a warning and might fix their bug, but need to pass
1881 // NULL here to test that we remain compatible.
1882 pthread_mutex_t* null_value = nullptr;
1883 ASSERT_EQ(EINVAL, pthread_mutex_lock(null_value));
Christopher Ferris60907c72015-06-09 18:46:15 -07001884#else
1885 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices.";
1886#endif
1887}
1888
1889TEST(pthread, pthread_mutex_unlock_null_32) {
1890#if defined(__BIONIC__) && !defined(__LP64__)
Dan Albertbaa2a972015-08-13 16:58:50 -07001891 // For LP32, the pthread lock/unlock functions allow a NULL mutex and return
1892 // EINVAL in that case: http://b/19995172.
1893 //
1894 // We decorate the public defintion with _Nonnull so that people recompiling
1895 // their code with get a warning and might fix their bug, but need to pass
1896 // NULL here to test that we remain compatible.
1897 pthread_mutex_t* null_value = nullptr;
1898 ASSERT_EQ(EINVAL, pthread_mutex_unlock(null_value));
Christopher Ferris60907c72015-06-09 18:46:15 -07001899#else
1900 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices.";
1901#endif
1902}
1903
1904TEST_F(pthread_DeathTest, pthread_mutex_lock_null_64) {
1905#if defined(__BIONIC__) && defined(__LP64__)
1906 pthread_mutex_t* null_value = nullptr;
1907 ASSERT_EXIT(pthread_mutex_lock(null_value), testing::KilledBySignal(SIGSEGV), "");
1908#else
1909 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices.";
1910#endif
1911}
1912
1913TEST_F(pthread_DeathTest, pthread_mutex_unlock_null_64) {
1914#if defined(__BIONIC__) && defined(__LP64__)
1915 pthread_mutex_t* null_value = nullptr;
1916 ASSERT_EXIT(pthread_mutex_unlock(null_value), testing::KilledBySignal(SIGSEGV), "");
1917#else
1918 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices.";
1919#endif
1920}
Yabin Cui33ac04a2015-09-22 11:16:15 -07001921
1922extern _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx, void* arg);
1923
1924static volatile bool signal_handler_on_altstack_done;
1925
Josh Gao61db9ac2017-03-15 19:42:05 -07001926__attribute__((__noinline__))
1927static void signal_handler_backtrace() {
1928 // Check if we have enough stack space for unwinding.
1929 int count = 0;
1930 _Unwind_Backtrace(FrameCounter, &count);
1931 ASSERT_GT(count, 0);
1932}
1933
1934__attribute__((__noinline__))
1935static void signal_handler_logging() {
1936 // Check if we have enough stack space for logging.
1937 std::string s(2048, '*');
1938 GTEST_LOG_(INFO) << s;
1939 signal_handler_on_altstack_done = true;
1940}
1941
1942__attribute__((__noinline__))
1943static void signal_handler_snprintf() {
1944 // Check if we have enough stack space for snprintf to a PATH_MAX buffer, plus some extra.
1945 char buf[PATH_MAX + 2048];
1946 ASSERT_GT(snprintf(buf, sizeof(buf), "/proc/%d/status", getpid()), 0);
1947}
1948
Yabin Cui33ac04a2015-09-22 11:16:15 -07001949static void SignalHandlerOnAltStack(int signo, siginfo_t*, void*) {
1950 ASSERT_EQ(SIGUSR1, signo);
Josh Gao61db9ac2017-03-15 19:42:05 -07001951 signal_handler_backtrace();
1952 signal_handler_logging();
1953 signal_handler_snprintf();
Yabin Cui33ac04a2015-09-22 11:16:15 -07001954}
1955
Josh Gao415daa82017-03-06 17:45:33 -08001956TEST(pthread, big_enough_signal_stack) {
Yabin Cui33ac04a2015-09-22 11:16:15 -07001957 signal_handler_on_altstack_done = false;
1958 ScopedSignalHandler handler(SIGUSR1, SignalHandlerOnAltStack, SA_SIGINFO | SA_ONSTACK);
1959 kill(getpid(), SIGUSR1);
1960 ASSERT_TRUE(signal_handler_on_altstack_done);
1961}
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001962
1963TEST(pthread, pthread_barrierattr_smoke) {
1964 pthread_barrierattr_t attr;
1965 ASSERT_EQ(0, pthread_barrierattr_init(&attr));
1966 int pshared;
1967 ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared));
1968 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
1969 ASSERT_EQ(0, pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
1970 ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared));
1971 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
1972 ASSERT_EQ(0, pthread_barrierattr_destroy(&attr));
1973}
1974
Yabin Cui81d27972016-03-22 13:45:55 -07001975struct BarrierTestHelperData {
1976 size_t thread_count;
1977 pthread_barrier_t barrier;
1978 std::atomic<int> finished_mask;
1979 std::atomic<int> serial_thread_count;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001980 size_t iteration_count;
Yabin Cui81d27972016-03-22 13:45:55 -07001981 std::atomic<size_t> finished_iteration_count;
1982
1983 BarrierTestHelperData(size_t thread_count, size_t iteration_count)
1984 : thread_count(thread_count), finished_mask(0), serial_thread_count(0),
1985 iteration_count(iteration_count), finished_iteration_count(0) {
1986 }
1987};
1988
1989struct BarrierTestHelperArg {
1990 int id;
1991 BarrierTestHelperData* data;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001992};
1993
1994static void BarrierTestHelper(BarrierTestHelperArg* arg) {
Yabin Cui81d27972016-03-22 13:45:55 -07001995 for (size_t i = 0; i < arg->data->iteration_count; ++i) {
1996 int result = pthread_barrier_wait(&arg->data->barrier);
1997 if (result == PTHREAD_BARRIER_SERIAL_THREAD) {
1998 arg->data->serial_thread_count++;
1999 } else {
2000 ASSERT_EQ(0, result);
2001 }
Yabin Cuid5c04c52017-05-02 12:57:39 -07002002 int mask = arg->data->finished_mask.fetch_or(1 << arg->id);
Yabin Cuiab4cddc2017-05-02 16:18:13 -07002003 mask |= 1 << arg->id;
Yabin Cuid5c04c52017-05-02 12:57:39 -07002004 if (mask == ((1 << arg->data->thread_count) - 1)) {
Yabin Cui81d27972016-03-22 13:45:55 -07002005 ASSERT_EQ(1, arg->data->serial_thread_count);
2006 arg->data->finished_iteration_count++;
2007 arg->data->finished_mask = 0;
2008 arg->data->serial_thread_count = 0;
2009 }
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002010 }
2011}
2012
2013TEST(pthread, pthread_barrier_smoke) {
2014 const size_t BARRIER_ITERATION_COUNT = 10;
2015 const size_t BARRIER_THREAD_COUNT = 10;
Yabin Cui81d27972016-03-22 13:45:55 -07002016 BarrierTestHelperData data(BARRIER_THREAD_COUNT, BARRIER_ITERATION_COUNT);
2017 ASSERT_EQ(0, pthread_barrier_init(&data.barrier, nullptr, data.thread_count));
2018 std::vector<pthread_t> threads(data.thread_count);
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002019 std::vector<BarrierTestHelperArg> args(threads.size());
2020 for (size_t i = 0; i < threads.size(); ++i) {
Yabin Cui81d27972016-03-22 13:45:55 -07002021 args[i].id = i;
2022 args[i].data = &data;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002023 ASSERT_EQ(0, pthread_create(&threads[i], nullptr,
2024 reinterpret_cast<void* (*)(void*)>(BarrierTestHelper), &args[i]));
2025 }
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002026 for (size_t i = 0; i < threads.size(); ++i) {
2027 ASSERT_EQ(0, pthread_join(threads[i], nullptr));
2028 }
Yabin Cui81d27972016-03-22 13:45:55 -07002029 ASSERT_EQ(data.iteration_count, data.finished_iteration_count);
2030 ASSERT_EQ(0, pthread_barrier_destroy(&data.barrier));
2031}
2032
2033struct BarrierDestroyTestArg {
2034 std::atomic<int> tid;
2035 pthread_barrier_t* barrier;
2036};
2037
2038static void BarrierDestroyTestHelper(BarrierDestroyTestArg* arg) {
2039 arg->tid = gettid();
2040 ASSERT_EQ(0, pthread_barrier_wait(arg->barrier));
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002041}
2042
2043TEST(pthread, pthread_barrier_destroy) {
2044 pthread_barrier_t barrier;
2045 ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, 2));
2046 pthread_t thread;
Yabin Cui81d27972016-03-22 13:45:55 -07002047 BarrierDestroyTestArg arg;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002048 arg.tid = 0;
2049 arg.barrier = &barrier;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002050 ASSERT_EQ(0, pthread_create(&thread, nullptr,
Yabin Cui81d27972016-03-22 13:45:55 -07002051 reinterpret_cast<void* (*)(void*)>(BarrierDestroyTestHelper), &arg));
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002052 WaitUntilThreadSleep(arg.tid);
2053 ASSERT_EQ(EBUSY, pthread_barrier_destroy(&barrier));
2054 ASSERT_EQ(PTHREAD_BARRIER_SERIAL_THREAD, pthread_barrier_wait(&barrier));
2055 // Verify if the barrier can be destroyed directly after pthread_barrier_wait().
2056 ASSERT_EQ(0, pthread_barrier_destroy(&barrier));
2057 ASSERT_EQ(0, pthread_join(thread, nullptr));
2058#if defined(__BIONIC__)
2059 ASSERT_EQ(EINVAL, pthread_barrier_destroy(&barrier));
2060#endif
2061}
2062
2063struct BarrierOrderingTestHelperArg {
2064 pthread_barrier_t* barrier;
2065 size_t* array;
2066 size_t array_length;
2067 size_t id;
2068};
2069
2070void BarrierOrderingTestHelper(BarrierOrderingTestHelperArg* arg) {
2071 const size_t ITERATION_COUNT = 10000;
2072 for (size_t i = 1; i <= ITERATION_COUNT; ++i) {
2073 arg->array[arg->id] = i;
Yabin Cuic9a659c2015-11-05 15:36:08 -08002074 int result = pthread_barrier_wait(arg->barrier);
2075 ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD);
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002076 for (size_t j = 0; j < arg->array_length; ++j) {
2077 ASSERT_EQ(i, arg->array[j]);
2078 }
Yabin Cuic9a659c2015-11-05 15:36:08 -08002079 result = pthread_barrier_wait(arg->barrier);
2080 ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD);
Yabin Cuie7c2fff2015-11-05 22:06:09 -08002081 }
2082}
2083
2084TEST(pthread, pthread_barrier_check_ordering) {
2085 const size_t THREAD_COUNT = 4;
2086 pthread_barrier_t barrier;
2087 ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, THREAD_COUNT));
2088 size_t array[THREAD_COUNT];
2089 std::vector<pthread_t> threads(THREAD_COUNT);
2090 std::vector<BarrierOrderingTestHelperArg> args(THREAD_COUNT);
2091 for (size_t i = 0; i < THREAD_COUNT; ++i) {
2092 args[i].barrier = &barrier;
2093 args[i].array = array;
2094 args[i].array_length = THREAD_COUNT;
2095 args[i].id = i;
2096 ASSERT_EQ(0, pthread_create(&threads[i], nullptr,
2097 reinterpret_cast<void* (*)(void*)>(BarrierOrderingTestHelper),
2098 &args[i]));
2099 }
2100 for (size_t i = 0; i < THREAD_COUNT; ++i) {
2101 ASSERT_EQ(0, pthread_join(threads[i], nullptr));
2102 }
2103}
Yabin Cuife3a83a2015-11-17 16:03:18 -08002104
2105TEST(pthread, pthread_spinlock_smoke) {
2106 pthread_spinlock_t lock;
2107 ASSERT_EQ(0, pthread_spin_init(&lock, 0));
2108 ASSERT_EQ(0, pthread_spin_trylock(&lock));
2109 ASSERT_EQ(0, pthread_spin_unlock(&lock));
2110 ASSERT_EQ(0, pthread_spin_lock(&lock));
2111 ASSERT_EQ(EBUSY, pthread_spin_trylock(&lock));
2112 ASSERT_EQ(0, pthread_spin_unlock(&lock));
2113 ASSERT_EQ(0, pthread_spin_destroy(&lock));
2114}
Elliott Hughes53dc9dd2017-09-19 14:02:50 -07002115
2116TEST(pthread, pthread_attr_setdetachstate) {
2117 pthread_attr_t attr;
2118 ASSERT_EQ(0, pthread_attr_init(&attr));
2119
2120 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
2121 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
2122 ASSERT_EQ(EINVAL, pthread_attr_setdetachstate(&attr, 123));
2123}
2124
2125TEST(pthread, pthread_create__mmap_failures) {
2126 pthread_attr_t attr;
2127 ASSERT_EQ(0, pthread_attr_init(&attr));
2128 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
2129
2130 const auto kPageSize = sysconf(_SC_PAGE_SIZE);
2131
Elliott Hughes57512982017-10-02 22:49:18 -07002132 // Use up all the VMAs. By default this is 64Ki (though some will already be in use).
Elliott Hughes53dc9dd2017-09-19 14:02:50 -07002133 std::vector<void*> pages;
Elliott Hughes57512982017-10-02 22:49:18 -07002134 pages.reserve(64 * 1024);
Elliott Hughes53dc9dd2017-09-19 14:02:50 -07002135 int prot = PROT_NONE;
2136 while (true) {
2137 void* page = mmap(nullptr, kPageSize, prot, MAP_ANON|MAP_PRIVATE, -1, 0);
2138 if (page == MAP_FAILED) break;
2139 pages.push_back(page);
2140 prot = (prot == PROT_NONE) ? PROT_READ : PROT_NONE;
2141 }
2142
2143 // Try creating threads, freeing up a page each time we fail.
2144 size_t EAGAIN_count = 0;
2145 size_t i = 0;
2146 for (; i < pages.size(); ++i) {
2147 pthread_t t;
2148 int status = pthread_create(&t, &attr, IdFn, nullptr);
2149 if (status != EAGAIN) break;
2150 ++EAGAIN_count;
2151 ASSERT_EQ(0, munmap(pages[i], kPageSize));
2152 }
2153
2154 // Creating a thread uses at least six VMAs: the stack, the TLS, and a guard each side of both.
2155 // So we should have seen at least six failures.
2156 ASSERT_GE(EAGAIN_count, 6U);
2157
2158 for (; i < pages.size(); ++i) {
2159 ASSERT_EQ(0, munmap(pages[i], kPageSize));
2160 }
2161}
Elliott Hughesdff08ce2017-10-16 09:58:45 -07002162
2163TEST(pthread, pthread_setschedparam) {
2164 sched_param p = { .sched_priority = INT_MIN };
2165 ASSERT_EQ(EINVAL, pthread_setschedparam(pthread_self(), INT_MIN, &p));
2166}
2167
2168TEST(pthread, pthread_setschedprio) {
2169 ASSERT_EQ(EINVAL, pthread_setschedprio(pthread_self(), INT_MIN));
2170}