blob: 019016d869477ab9aa24cc183724560fa81c1a6b [file] [log] [blame]
Elliott Hughesbfeab1b2012-09-05 17:47:37 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <errno.h>
Elliott Hughes5b9310e2013-10-02 16:59:05 -070020#include <inttypes.h>
Elliott Hughesb95cf0d2013-07-15 14:51:07 -070021#include <limits.h>
Elliott Hughes04620a32014-03-07 17:59:05 -080022#include <malloc.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070023#include <pthread.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080024#include <signal.h>
Yabin Cui140f3672015-02-03 10:32:00 -080025#include <stdio.h>
Elliott Hughes70b24b12013-11-15 11:51:07 -080026#include <sys/mman.h>
Elliott Hughes57b7a612014-08-25 17:26:50 -070027#include <sys/syscall.h>
Narayan Kamath51e6cb32014-03-03 15:38:51 +000028#include <time.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070029#include <unistd.h>
Yabin Cui33ac04a2015-09-22 11:16:15 -070030#include <unwind.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070031
Yabin Cui08ee8d22015-02-11 17:04:36 -080032#include <atomic>
Yabin Cuib5845722015-03-16 22:46:42 -070033#include <vector>
Yabin Cui08ee8d22015-02-11 17:04:36 -080034
Yabin Cuic9a659c2015-11-05 15:36:08 -080035#include "private/bionic_constants.h"
Yabin Cui17393b02015-03-21 15:08:25 -070036#include "private/bionic_macros.h"
37#include "private/ScopeGuard.h"
38#include "BionicDeathTest.h"
39#include "ScopedSignalHandler.h"
Elliott Hughes15dfd632015-09-22 16:40:14 -070040#include "utils.h"
41
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070042TEST(pthread, pthread_key_create) {
43 pthread_key_t key;
44 ASSERT_EQ(0, pthread_key_create(&key, NULL));
45 ASSERT_EQ(0, pthread_key_delete(key));
46 // Can't delete a key that's already been deleted.
47 ASSERT_EQ(EINVAL, pthread_key_delete(key));
48}
Elliott Hughes4d014e12012-09-07 16:47:54 -070049
Dan Albertc4bcc752014-09-30 11:48:24 -070050TEST(pthread, pthread_keys_max) {
Yabin Cui6c238f22014-12-11 20:50:41 -080051 // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX.
52 ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070053}
Elliott Hughes718a5b52014-01-28 17:02:03 -080054
Yabin Cui6c238f22014-12-11 20:50:41 -080055TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) {
Dan Albertc4bcc752014-09-30 11:48:24 -070056 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
Yabin Cui6c238f22014-12-11 20:50:41 -080057 ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070058}
59
60TEST(pthread, pthread_key_many_distinct) {
Yabin Cui6c238f22014-12-11 20:50:41 -080061 // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX
62 // pthread keys, but We should be able to allocate at least this many keys.
63 int nkeys = PTHREAD_KEYS_MAX / 2;
Dan Albertc4bcc752014-09-30 11:48:24 -070064 std::vector<pthread_key_t> keys;
65
66 auto scope_guard = make_scope_guard([&keys]{
Elliott Hughes0b2acdf2015-10-02 18:25:19 -070067 for (const auto& key : keys) {
Dan Albertc4bcc752014-09-30 11:48:24 -070068 EXPECT_EQ(0, pthread_key_delete(key));
69 }
70 });
71
72 for (int i = 0; i < nkeys; ++i) {
73 pthread_key_t key;
Elliott Hughes61706932015-03-31 10:56:58 -070074 // If this fails, it's likely that LIBC_PTHREAD_KEY_RESERVED_COUNT is wrong.
Dan Albertc4bcc752014-09-30 11:48:24 -070075 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys;
76 keys.push_back(key);
77 ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i)));
78 }
79
80 for (int i = keys.size() - 1; i >= 0; --i) {
81 ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back()));
82 pthread_key_t key = keys.back();
83 keys.pop_back();
84 ASSERT_EQ(0, pthread_key_delete(key));
85 }
86}
87
Yabin Cui6c238f22014-12-11 20:50:41 -080088TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000089 std::vector<pthread_key_t> keys;
Dan Albertc4bcc752014-09-30 11:48:24 -070090 int rv = 0;
Yabin Cui6c238f22014-12-11 20:50:41 -080091
92 // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should
93 // be more than we are allowed to allocate now.
94 for (int i = 0; i < PTHREAD_KEYS_MAX; i++) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000095 pthread_key_t key;
Dan Albertc4bcc752014-09-30 11:48:24 -070096 rv = pthread_key_create(&key, NULL);
97 if (rv == EAGAIN) {
98 break;
99 }
100 EXPECT_EQ(0, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000101 keys.push_back(key);
102 }
103
Dan Albertc4bcc752014-09-30 11:48:24 -0700104 // Don't leak keys.
Elliott Hughes0b2acdf2015-10-02 18:25:19 -0700105 for (const auto& key : keys) {
Dan Albertc4bcc752014-09-30 11:48:24 -0700106 EXPECT_EQ(0, pthread_key_delete(key));
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000107 }
Dan Albertc4bcc752014-09-30 11:48:24 -0700108 keys.clear();
109
110 // We should have eventually reached the maximum number of keys and received
111 // EAGAIN.
112 ASSERT_EQ(EAGAIN, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000113}
114
Elliott Hughesebb770f2014-06-25 13:46:46 -0700115TEST(pthread, pthread_key_delete) {
116 void* expected = reinterpret_cast<void*>(1234);
117 pthread_key_t key;
118 ASSERT_EQ(0, pthread_key_create(&key, NULL));
119 ASSERT_EQ(0, pthread_setspecific(key, expected));
120 ASSERT_EQ(expected, pthread_getspecific(key));
121 ASSERT_EQ(0, pthread_key_delete(key));
122 // After deletion, pthread_getspecific returns NULL.
123 ASSERT_EQ(NULL, pthread_getspecific(key));
124 // And you can't use pthread_setspecific with the deleted key.
125 ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
126}
127
Elliott Hughes40a52172014-07-30 14:48:10 -0700128TEST(pthread, pthread_key_fork) {
129 void* expected = reinterpret_cast<void*>(1234);
130 pthread_key_t key;
131 ASSERT_EQ(0, pthread_key_create(&key, NULL));
132 ASSERT_EQ(0, pthread_setspecific(key, expected));
133 ASSERT_EQ(expected, pthread_getspecific(key));
134
135 pid_t pid = fork();
136 ASSERT_NE(-1, pid) << strerror(errno);
137
138 if (pid == 0) {
139 // The surviving thread inherits all the forking thread's TLS values...
140 ASSERT_EQ(expected, pthread_getspecific(key));
141 _exit(99);
142 }
143
Elliott Hughes33697a02016-01-26 13:04:57 -0800144 AssertChildExited(pid, 99);
Elliott Hughes40a52172014-07-30 14:48:10 -0700145
146 ASSERT_EQ(expected, pthread_getspecific(key));
Dan Albert1d53ae22014-09-02 15:24:26 -0700147 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700148}
149
150static void* DirtyKeyFn(void* key) {
151 return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
152}
153
154TEST(pthread, pthread_key_dirty) {
155 pthread_key_t key;
156 ASSERT_EQ(0, pthread_key_create(&key, NULL));
157
Yabin Cuia36158a2015-11-16 21:06:16 -0800158 size_t stack_size = 640 * 1024;
Elliott Hughes40a52172014-07-30 14:48:10 -0700159 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
160 ASSERT_NE(MAP_FAILED, stack);
161 memset(stack, 0xff, stack_size);
162
163 pthread_attr_t attr;
164 ASSERT_EQ(0, pthread_attr_init(&attr));
165 ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
166
167 pthread_t t;
168 ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
169
170 void* result;
171 ASSERT_EQ(0, pthread_join(t, &result));
172 ASSERT_EQ(nullptr, result); // Not ~0!
173
174 ASSERT_EQ(0, munmap(stack, stack_size));
Dan Albert1d53ae22014-09-02 15:24:26 -0700175 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700176}
177
Yabin Cui5ddbb3f2015-03-05 20:35:32 -0800178TEST(pthread, static_pthread_key_used_before_creation) {
179#if defined(__BIONIC__)
180 // See http://b/19625804. The bug is about a static/global pthread key being used before creation.
181 // So here tests if the static/global default value 0 can be detected as invalid key.
182 static pthread_key_t key;
183 ASSERT_EQ(nullptr, pthread_getspecific(key));
184 ASSERT_EQ(EINVAL, pthread_setspecific(key, nullptr));
185 ASSERT_EQ(EINVAL, pthread_key_delete(key));
186#else
187 GTEST_LOG_(INFO) << "This test tests bionic pthread key implementation detail.\n";
188#endif
189}
190
Elliott Hughes4d014e12012-09-07 16:47:54 -0700191static void* IdFn(void* arg) {
192 return arg;
193}
194
Yabin Cui63481602014-12-01 17:41:04 -0800195class SpinFunctionHelper {
196 public:
197 SpinFunctionHelper() {
198 SpinFunctionHelper::spin_flag_ = true;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400199 }
Yabin Cui63481602014-12-01 17:41:04 -0800200 ~SpinFunctionHelper() {
201 UnSpin();
202 }
203 auto GetFunction() -> void* (*)(void*) {
204 return SpinFunctionHelper::SpinFn;
205 }
206
207 void UnSpin() {
208 SpinFunctionHelper::spin_flag_ = false;
209 }
210
211 private:
212 static void* SpinFn(void*) {
213 while (spin_flag_) {}
214 return NULL;
215 }
Yabin Cuia36158a2015-11-16 21:06:16 -0800216 static std::atomic<bool> spin_flag_;
Yabin Cui63481602014-12-01 17:41:04 -0800217};
218
219// It doesn't matter if spin_flag_ is used in several tests,
220// because it is always set to false after each test. Each thread
221// loops on spin_flag_ can find it becomes false at some time.
Yabin Cuia36158a2015-11-16 21:06:16 -0800222std::atomic<bool> SpinFunctionHelper::spin_flag_;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400223
Elliott Hughes4d014e12012-09-07 16:47:54 -0700224static void* JoinFn(void* arg) {
225 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
226}
227
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400228static void AssertDetached(pthread_t t, bool is_detached) {
229 pthread_attr_t attr;
230 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
231 int detach_state;
232 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
233 pthread_attr_destroy(&attr);
234 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
235}
236
Elliott Hughes9d23e042013-02-15 19:21:51 -0800237static void MakeDeadThread(pthread_t& t) {
238 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
Elliott Hughes34c987a2014-09-22 16:01:26 -0700239 ASSERT_EQ(0, pthread_join(t, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800240}
241
Elliott Hughes4d014e12012-09-07 16:47:54 -0700242TEST(pthread, pthread_create) {
243 void* expected_result = reinterpret_cast<void*>(123);
244 // Can we create a thread?
245 pthread_t t;
246 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
247 // If we join, do we get the expected value back?
248 void* result;
249 ASSERT_EQ(0, pthread_join(t, &result));
250 ASSERT_EQ(expected_result, result);
251}
252
Elliott Hughes3e898472013-02-12 16:40:24 +0000253TEST(pthread, pthread_create_EAGAIN) {
254 pthread_attr_t attributes;
255 ASSERT_EQ(0, pthread_attr_init(&attributes));
256 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
257
258 pthread_t t;
259 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
260}
261
Elliott Hughes4d014e12012-09-07 16:47:54 -0700262TEST(pthread, pthread_no_join_after_detach) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700263 SpinFunctionHelper spin_helper;
Yabin Cui63481602014-12-01 17:41:04 -0800264
Elliott Hughes4d014e12012-09-07 16:47:54 -0700265 pthread_t t1;
Elliott Hughes725b2a92016-03-23 11:20:47 -0700266 ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700267
268 // After a pthread_detach...
269 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400270 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700271
272 // ...pthread_join should fail.
Elliott Hughes34c987a2014-09-22 16:01:26 -0700273 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700274}
275
276TEST(pthread, pthread_no_op_detach_after_join) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700277 SpinFunctionHelper spin_helper;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400278
Elliott Hughes4d014e12012-09-07 16:47:54 -0700279 pthread_t t1;
Elliott Hughes725b2a92016-03-23 11:20:47 -0700280 ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700281
282 // If thread 2 is already waiting to join thread 1...
283 pthread_t t2;
284 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
285
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400286 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700287
Yabin Cuibbb04322015-03-19 15:19:25 -0700288#if defined(__BIONIC__)
289 ASSERT_EQ(EINVAL, pthread_detach(t1));
290#else
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400291 ASSERT_EQ(0, pthread_detach(t1));
Yabin Cuibbb04322015-03-19 15:19:25 -0700292#endif
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400293 AssertDetached(t1, false);
294
Elliott Hughes725b2a92016-03-23 11:20:47 -0700295 spin_helper.UnSpin();
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400296
297 // ...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 -0700298 void* join_result;
299 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700300 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700301}
Elliott Hughes14f19592012-10-29 10:19:44 -0700302
303TEST(pthread, pthread_join_self) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700304 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL));
Elliott Hughes14f19592012-10-29 10:19:44 -0700305}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700306
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800307struct TestBug37410 {
308 pthread_t main_thread;
309 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700310
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800311 static void main() {
312 TestBug37410 data;
313 data.main_thread = pthread_self();
314 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
315 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
316
317 pthread_t t;
318 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
319
320 // Wait for the thread to be running...
321 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
322 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
323
324 // ...and exit.
325 pthread_exit(NULL);
326 }
327
328 private:
329 static void* thread_fn(void* arg) {
330 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
331
332 // Let the main thread know we're running.
333 pthread_mutex_unlock(&data->mutex);
334
335 // And wait for the main thread to exit.
336 pthread_join(data->main_thread, NULL);
337
338 return NULL;
339 }
340};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700341
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800342// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
343// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800344
345class pthread_DeathTest : public BionicDeathTest {};
346
347TEST_F(pthread_DeathTest, pthread_bug_37410) {
Elliott Hughes4f251be2012-11-01 16:33:29 -0700348 // http://code.google.com/p/android/issues/detail?id=37410
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800349 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700350}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800351
352static void* SignalHandlerFn(void* arg) {
353 sigset_t wait_set;
354 sigfillset(&wait_set);
355 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
356}
357
358TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700359 // Check that SIGUSR1 isn't blocked.
360 sigset_t original_set;
361 sigemptyset(&original_set);
362 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
363 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
364
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800365 // Block SIGUSR1.
366 sigset_t set;
367 sigemptyset(&set);
368 sigaddset(&set, SIGUSR1);
369 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
370
Elliott Hughes19e62322013-10-15 11:23:57 -0700371 // Check that SIGUSR1 is blocked.
372 sigset_t final_set;
373 sigemptyset(&final_set);
374 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
375 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
376 // ...and that sigprocmask agrees with pthread_sigmask.
377 sigemptyset(&final_set);
378 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
379 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
380
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800381 // Spawn a thread that calls sigwait and tells us what it received.
382 pthread_t signal_thread;
383 int received_signal = -1;
384 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
385
386 // Send that thread SIGUSR1.
387 pthread_kill(signal_thread, SIGUSR1);
388
389 // See what it got.
390 void* join_result;
391 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
392 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700393 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700394
395 // Restore the original signal mask.
396 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800397}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800398
Elliott Hughes725b2a92016-03-23 11:20:47 -0700399static void test_pthread_setname_np__pthread_getname_np(pthread_t t) {
400 ASSERT_EQ(0, pthread_setname_np(t, "short"));
401 char name[32];
402 ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name)));
403 ASSERT_STREQ("short", name);
404
Elliott Hughesd1aea302015-04-25 10:05:24 -0700405 // The limit is 15 characters --- the kernel's buffer is 16, but includes a NUL.
Elliott Hughes725b2a92016-03-23 11:20:47 -0700406 ASSERT_EQ(0, pthread_setname_np(t, "123456789012345"));
407 ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name)));
408 ASSERT_STREQ("123456789012345", name);
409
410 ASSERT_EQ(ERANGE, pthread_setname_np(t, "1234567890123456"));
411
412 // The passed-in buffer should be at least 16 bytes.
413 ASSERT_EQ(0, pthread_getname_np(t, name, 16));
414 ASSERT_EQ(ERANGE, pthread_getname_np(t, name, 15));
Elliott Hughes3e898472013-02-12 16:40:24 +0000415}
416
Elliott Hughes725b2a92016-03-23 11:20:47 -0700417TEST(pthread, pthread_setname_np__pthread_getname_np__self) {
418 test_pthread_setname_np__pthread_getname_np(pthread_self());
Elliott Hughes3e898472013-02-12 16:40:24 +0000419}
420
Elliott Hughes725b2a92016-03-23 11:20:47 -0700421TEST(pthread, pthread_setname_np__pthread_getname_np__other) {
422 SpinFunctionHelper spin_helper;
Yabin Cui63481602014-12-01 17:41:04 -0800423
Elliott Hughes725b2a92016-03-23 11:20:47 -0700424 pthread_t t;
425 ASSERT_EQ(0, pthread_create(&t, NULL, spin_helper.GetFunction(), NULL));
426 test_pthread_setname_np__pthread_getname_np(t);
427 spin_helper.UnSpin();
428 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes3e898472013-02-12 16:40:24 +0000429}
430
Elliott Hughes725b2a92016-03-23 11:20:47 -0700431TEST(pthread, pthread_setname_np__pthread_getname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800432 pthread_t dead_thread;
433 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000434
Elliott Hughes725b2a92016-03-23 11:20:47 -0700435 // Call pthread_getname_np and pthread_setname_np after the thread has already exited.
Elliott Hughes68d98d82014-11-12 21:03:26 -0800436 ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes725b2a92016-03-23 11:20:47 -0700437 char name[64];
438 ASSERT_EQ(ENOENT, pthread_getname_np(dead_thread, name, sizeof(name)));
Elliott Hughes3e898472013-02-12 16:40:24 +0000439}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800440
441TEST(pthread, pthread_kill__0) {
442 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
443 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
444}
445
446TEST(pthread, pthread_kill__invalid_signal) {
447 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
448}
449
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800450static void pthread_kill__in_signal_handler_helper(int signal_number) {
451 static int count = 0;
452 ASSERT_EQ(SIGALRM, signal_number);
453 if (++count == 1) {
454 // Can we call pthread_kill from a signal handler?
455 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
456 }
457}
458
459TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800460 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800461 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
462}
463
Yabin Cui220b99b2015-03-26 18:13:07 +0000464TEST(pthread, pthread_detach__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800465 pthread_t dead_thread;
466 MakeDeadThread(dead_thread);
467
468 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
469}
470
Jeff Hao9b06cc32013-08-15 14:51:16 -0700471TEST(pthread, pthread_getcpuclockid__clock_gettime) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700472 SpinFunctionHelper spin_helper;
Yabin Cui63481602014-12-01 17:41:04 -0800473
Jeff Hao9b06cc32013-08-15 14:51:16 -0700474 pthread_t t;
Elliott Hughes725b2a92016-03-23 11:20:47 -0700475 ASSERT_EQ(0, pthread_create(&t, NULL, spin_helper.GetFunction(), NULL));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700476
477 clockid_t c;
478 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
479 timespec ts;
480 ASSERT_EQ(0, clock_gettime(c, &ts));
Elliott Hughes725b2a92016-03-23 11:20:47 -0700481 spin_helper.UnSpin();
Yabin Cuia36158a2015-11-16 21:06:16 -0800482 ASSERT_EQ(0, pthread_join(t, nullptr));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700483}
484
Yabin Cui220b99b2015-03-26 18:13:07 +0000485TEST(pthread, pthread_getcpuclockid__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800486 pthread_t dead_thread;
487 MakeDeadThread(dead_thread);
488
489 clockid_t c;
490 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
491}
492
Yabin Cui220b99b2015-03-26 18:13:07 +0000493TEST(pthread, pthread_getschedparam__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800494 pthread_t dead_thread;
495 MakeDeadThread(dead_thread);
496
497 int policy;
498 sched_param param;
499 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
500}
501
Yabin Cui220b99b2015-03-26 18:13:07 +0000502TEST(pthread, pthread_setschedparam__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800503 pthread_t dead_thread;
504 MakeDeadThread(dead_thread);
505
506 int policy = 0;
507 sched_param param;
508 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
509}
510
Yabin Cui220b99b2015-03-26 18:13:07 +0000511TEST(pthread, pthread_join__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800512 pthread_t dead_thread;
513 MakeDeadThread(dead_thread);
514
Elliott Hughes34c987a2014-09-22 16:01:26 -0700515 ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800516}
517
Yabin Cui220b99b2015-03-26 18:13:07 +0000518TEST(pthread, pthread_kill__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800519 pthread_t dead_thread;
520 MakeDeadThread(dead_thread);
521
522 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
523}
msg5550f020d12013-06-06 14:59:28 -0400524
525TEST(pthread, pthread_join__multijoin) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700526 SpinFunctionHelper spin_helper;
msg5550f020d12013-06-06 14:59:28 -0400527
528 pthread_t t1;
Elliott Hughes725b2a92016-03-23 11:20:47 -0700529 ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL));
msg5550f020d12013-06-06 14:59:28 -0400530
531 pthread_t t2;
532 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
533
534 sleep(1); // (Give t2 a chance to call pthread_join.)
535
536 // Multiple joins to the same thread should fail.
537 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
538
Elliott Hughes725b2a92016-03-23 11:20:47 -0700539 spin_helper.UnSpin();
msg5550f020d12013-06-06 14:59:28 -0400540
541 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
542 void* join_result;
543 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700544 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400545}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700546
Elliott Hughes70b24b12013-11-15 11:51:07 -0800547TEST(pthread, pthread_join__race) {
548 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
549 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
550 for (size_t i = 0; i < 1024; ++i) {
Yabin Cuia36158a2015-11-16 21:06:16 -0800551 size_t stack_size = 640*1024;
Elliott Hughes70b24b12013-11-15 11:51:07 -0800552 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
553
554 pthread_attr_t a;
555 pthread_attr_init(&a);
556 pthread_attr_setstack(&a, stack, stack_size);
557
558 pthread_t t;
559 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
560 ASSERT_EQ(0, pthread_join(t, NULL));
561 ASSERT_EQ(0, munmap(stack, stack_size));
562 }
563}
564
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700565static void* GetActualGuardSizeFn(void* arg) {
566 pthread_attr_t attributes;
567 pthread_getattr_np(pthread_self(), &attributes);
568 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
569 return NULL;
570}
571
572static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
573 size_t result;
574 pthread_t t;
575 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700576 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700577 return result;
578}
579
580static void* GetActualStackSizeFn(void* arg) {
581 pthread_attr_t attributes;
582 pthread_getattr_np(pthread_self(), &attributes);
583 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
584 return NULL;
585}
586
587static size_t GetActualStackSize(const pthread_attr_t& attributes) {
588 size_t result;
589 pthread_t t;
590 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700591 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700592 return result;
593}
594
595TEST(pthread, pthread_attr_setguardsize) {
596 pthread_attr_t attributes;
597 ASSERT_EQ(0, pthread_attr_init(&attributes));
598
599 // Get the default guard size.
600 size_t default_guard_size;
601 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
602
603 // No such thing as too small: will be rounded up to one page by pthread_create.
604 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
605 size_t guard_size;
606 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
607 ASSERT_EQ(128U, guard_size);
608 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
609
610 // Large enough and a multiple of the page size.
611 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
612 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
613 ASSERT_EQ(32*1024U, guard_size);
614
615 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
616 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
617 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
618 ASSERT_EQ(32*1024U + 1, guard_size);
619}
620
621TEST(pthread, pthread_attr_setstacksize) {
622 pthread_attr_t attributes;
623 ASSERT_EQ(0, pthread_attr_init(&attributes));
624
625 // Get the default stack size.
626 size_t default_stack_size;
627 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
628
629 // Too small.
630 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
631 size_t stack_size;
632 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
633 ASSERT_EQ(default_stack_size, stack_size);
634 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
635
Yabin Cui917d3902015-01-08 12:32:42 -0800636 // Large enough and a multiple of the page size; may be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700637 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
638 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
639 ASSERT_EQ(32*1024U, stack_size);
Yabin Cui917d3902015-01-08 12:32:42 -0800640 ASSERT_GE(GetActualStackSize(attributes), 32*1024U);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700641
Yabin Cui917d3902015-01-08 12:32:42 -0800642 // Large enough but not aligned; will be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700643 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
644 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
645 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800646#if defined(__BIONIC__)
Yabin Cui917d3902015-01-08 12:32:42 -0800647 ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800648#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700649 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
650 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800651#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700652}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700653
Yabin Cui76615da2015-03-17 14:22:09 -0700654TEST(pthread, pthread_rwlockattr_smoke) {
655 pthread_rwlockattr_t attr;
656 ASSERT_EQ(0, pthread_rwlockattr_init(&attr));
657
658 int pshared_value_array[] = {PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED};
659 for (size_t i = 0; i < sizeof(pshared_value_array) / sizeof(pshared_value_array[0]); ++i) {
660 ASSERT_EQ(0, pthread_rwlockattr_setpshared(&attr, pshared_value_array[i]));
661 int pshared;
662 ASSERT_EQ(0, pthread_rwlockattr_getpshared(&attr, &pshared));
663 ASSERT_EQ(pshared_value_array[i], pshared);
664 }
665
666 int kind_array[] = {PTHREAD_RWLOCK_PREFER_READER_NP,
667 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP};
668 for (size_t i = 0; i < sizeof(kind_array) / sizeof(kind_array[0]); ++i) {
669 ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_array[i]));
670 int kind;
671 ASSERT_EQ(0, pthread_rwlockattr_getkind_np(&attr, &kind));
672 ASSERT_EQ(kind_array[i], kind);
673 }
674
675 ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr));
676}
677
678TEST(pthread, pthread_rwlock_init_same_as_PTHREAD_RWLOCK_INITIALIZER) {
679 pthread_rwlock_t lock1 = PTHREAD_RWLOCK_INITIALIZER;
680 pthread_rwlock_t lock2;
681 ASSERT_EQ(0, pthread_rwlock_init(&lock2, NULL));
682 ASSERT_EQ(0, memcmp(&lock1, &lock2, sizeof(lock1)));
683}
684
Elliott Hughesc3f11402013-10-30 14:40:09 -0700685TEST(pthread, pthread_rwlock_smoke) {
686 pthread_rwlock_t l;
687 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
688
Calin Juravle76f352e2014-05-19 13:41:10 +0100689 // Single read lock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700690 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
691 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
692
Calin Juravle76f352e2014-05-19 13:41:10 +0100693 // Multiple read lock
694 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
695 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
696 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
697 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
698
699 // Write lock
Calin Juravle92687e42014-05-22 19:21:22 +0100700 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
701 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100702
703 // Try writer lock
704 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
705 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
706 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
707 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
708
709 // Try reader lock
710 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
711 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
712 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
713 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
714 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
715
716 // Try writer lock after unlock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700717 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
718 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
719
Calin Juravle76f352e2014-05-19 13:41:10 +0100720 // EDEADLK in "read after write"
721 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
722 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
723 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
724
725 // EDEADLK in "write after write"
726 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
727 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
728 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100729
Elliott Hughesc3f11402013-10-30 14:40:09 -0700730 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
731}
732
Yabin Cui08ee8d22015-02-11 17:04:36 -0800733struct RwlockWakeupHelperArg {
734 pthread_rwlock_t lock;
735 enum Progress {
736 LOCK_INITIALIZED,
737 LOCK_WAITING,
738 LOCK_RELEASED,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800739 LOCK_ACCESSED,
740 LOCK_TIMEDOUT,
Yabin Cui08ee8d22015-02-11 17:04:36 -0800741 };
742 std::atomic<Progress> progress;
Yabin Cuif7969852015-04-02 17:47:48 -0700743 std::atomic<pid_t> tid;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800744 std::function<int (pthread_rwlock_t*)> trylock_function;
745 std::function<int (pthread_rwlock_t*)> lock_function;
746 std::function<int (pthread_rwlock_t*, const timespec*)> timed_lock_function;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800747};
748
Yabin Cuic9a659c2015-11-05 15:36:08 -0800749static void pthread_rwlock_wakeup_helper(RwlockWakeupHelperArg* arg) {
Yabin Cuif7969852015-04-02 17:47:48 -0700750 arg->tid = gettid();
Yabin Cui08ee8d22015-02-11 17:04:36 -0800751 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
752 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
753
Yabin Cuic9a659c2015-11-05 15:36:08 -0800754 ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock));
755 ASSERT_EQ(0, arg->lock_function(&arg->lock));
Yabin Cui08ee8d22015-02-11 17:04:36 -0800756 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
757 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
758
759 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
760}
761
Yabin Cuic9a659c2015-11-05 15:36:08 -0800762static void test_pthread_rwlock_reader_wakeup_writer(std::function<int (pthread_rwlock_t*)> lock_function) {
Yabin Cui08ee8d22015-02-11 17:04:36 -0800763 RwlockWakeupHelperArg wakeup_arg;
764 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
765 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
766 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -0700767 wakeup_arg.tid = 0;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800768 wakeup_arg.trylock_function = pthread_rwlock_trywrlock;
769 wakeup_arg.lock_function = lock_function;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800770
771 pthread_t thread;
772 ASSERT_EQ(0, pthread_create(&thread, NULL,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800773 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg));
Yabin Cuif7969852015-04-02 17:47:48 -0700774 WaitUntilThreadSleep(wakeup_arg.tid);
775 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
776
Yabin Cui08ee8d22015-02-11 17:04:36 -0800777 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
778 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
779
780 ASSERT_EQ(0, pthread_join(thread, NULL));
781 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
782 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
783}
784
Yabin Cuic9a659c2015-11-05 15:36:08 -0800785TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
786 test_pthread_rwlock_reader_wakeup_writer(pthread_rwlock_wrlock);
Yabin Cui08ee8d22015-02-11 17:04:36 -0800787}
788
Yabin Cuic9a659c2015-11-05 15:36:08 -0800789TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait) {
790 timespec ts;
791 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
792 ts.tv_sec += 1;
793 test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) {
794 return pthread_rwlock_timedwrlock(lock, &ts);
795 });
796}
797
798static void test_pthread_rwlock_writer_wakeup_reader(std::function<int (pthread_rwlock_t*)> lock_function) {
Yabin Cui08ee8d22015-02-11 17:04:36 -0800799 RwlockWakeupHelperArg wakeup_arg;
800 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
801 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
802 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -0700803 wakeup_arg.tid = 0;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800804 wakeup_arg.trylock_function = pthread_rwlock_tryrdlock;
805 wakeup_arg.lock_function = lock_function;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800806
807 pthread_t thread;
808 ASSERT_EQ(0, pthread_create(&thread, NULL,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800809 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg));
Yabin Cuif7969852015-04-02 17:47:48 -0700810 WaitUntilThreadSleep(wakeup_arg.tid);
811 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
812
Yabin Cui08ee8d22015-02-11 17:04:36 -0800813 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
814 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
815
816 ASSERT_EQ(0, pthread_join(thread, NULL));
817 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
818 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
819}
820
Yabin Cuic9a659c2015-11-05 15:36:08 -0800821TEST(pthread, pthread_rwlock_writer_wakeup_reader) {
822 test_pthread_rwlock_writer_wakeup_reader(pthread_rwlock_rdlock);
823}
824
825TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait) {
826 timespec ts;
827 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
828 ts.tv_sec += 1;
829 test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) {
830 return pthread_rwlock_timedrdlock(lock, &ts);
831 });
832}
833
834static void pthread_rwlock_wakeup_timeout_helper(RwlockWakeupHelperArg* arg) {
835 arg->tid = gettid();
836 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
837 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
838
839 ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock));
840
841 timespec ts;
842 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
843 ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
844 ts.tv_nsec = -1;
845 ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts));
846 ts.tv_nsec = NS_PER_S;
847 ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts));
848 ts.tv_nsec = NS_PER_S - 1;
849 ts.tv_sec = -1;
850 ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
851 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
852 ts.tv_sec += 1;
853 ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
854 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, arg->progress);
855 arg->progress = RwlockWakeupHelperArg::LOCK_TIMEDOUT;
856}
857
858TEST(pthread, pthread_rwlock_timedrdlock_timeout) {
859 RwlockWakeupHelperArg wakeup_arg;
860 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
861 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
862 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
863 wakeup_arg.tid = 0;
864 wakeup_arg.trylock_function = pthread_rwlock_tryrdlock;
865 wakeup_arg.timed_lock_function = pthread_rwlock_timedrdlock;
866
867 pthread_t thread;
868 ASSERT_EQ(0, pthread_create(&thread, nullptr,
869 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg));
870 WaitUntilThreadSleep(wakeup_arg.tid);
871 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
872
873 ASSERT_EQ(0, pthread_join(thread, nullptr));
874 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress);
875 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
876 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
877}
878
879TEST(pthread, pthread_rwlock_timedwrlock_timeout) {
880 RwlockWakeupHelperArg wakeup_arg;
881 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
882 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
883 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
884 wakeup_arg.tid = 0;
885 wakeup_arg.trylock_function = pthread_rwlock_trywrlock;
886 wakeup_arg.timed_lock_function = pthread_rwlock_timedwrlock;
887
888 pthread_t thread;
889 ASSERT_EQ(0, pthread_create(&thread, nullptr,
890 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg));
891 WaitUntilThreadSleep(wakeup_arg.tid);
892 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
893
894 ASSERT_EQ(0, pthread_join(thread, nullptr));
895 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress);
896 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
897 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
898}
899
Yabin Cui76615da2015-03-17 14:22:09 -0700900class RwlockKindTestHelper {
901 private:
902 struct ThreadArg {
903 RwlockKindTestHelper* helper;
904 std::atomic<pid_t>& tid;
905
906 ThreadArg(RwlockKindTestHelper* helper, std::atomic<pid_t>& tid)
907 : helper(helper), tid(tid) { }
908 };
909
910 public:
911 pthread_rwlock_t lock;
912
913 public:
914 RwlockKindTestHelper(int kind_type) {
915 InitRwlock(kind_type);
916 }
917
918 ~RwlockKindTestHelper() {
919 DestroyRwlock();
920 }
921
922 void CreateWriterThread(pthread_t& thread, std::atomic<pid_t>& tid) {
923 tid = 0;
924 ThreadArg* arg = new ThreadArg(this, tid);
925 ASSERT_EQ(0, pthread_create(&thread, NULL,
926 reinterpret_cast<void* (*)(void*)>(WriterThreadFn), arg));
927 }
928
929 void CreateReaderThread(pthread_t& thread, std::atomic<pid_t>& tid) {
930 tid = 0;
931 ThreadArg* arg = new ThreadArg(this, tid);
932 ASSERT_EQ(0, pthread_create(&thread, NULL,
933 reinterpret_cast<void* (*)(void*)>(ReaderThreadFn), arg));
934 }
935
936 private:
937 void InitRwlock(int kind_type) {
938 pthread_rwlockattr_t attr;
939 ASSERT_EQ(0, pthread_rwlockattr_init(&attr));
940 ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_type));
941 ASSERT_EQ(0, pthread_rwlock_init(&lock, &attr));
942 ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr));
943 }
944
945 void DestroyRwlock() {
946 ASSERT_EQ(0, pthread_rwlock_destroy(&lock));
947 }
948
949 static void WriterThreadFn(ThreadArg* arg) {
950 arg->tid = gettid();
951
952 RwlockKindTestHelper* helper = arg->helper;
953 ASSERT_EQ(0, pthread_rwlock_wrlock(&helper->lock));
954 ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock));
955 delete arg;
956 }
957
958 static void ReaderThreadFn(ThreadArg* arg) {
959 arg->tid = gettid();
960
961 RwlockKindTestHelper* helper = arg->helper;
962 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper->lock));
963 ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock));
964 delete arg;
965 }
966};
967
968TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_READER_NP) {
969 RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_READER_NP);
970 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock));
971
972 pthread_t writer_thread;
973 std::atomic<pid_t> writer_tid;
974 helper.CreateWriterThread(writer_thread, writer_tid);
975 WaitUntilThreadSleep(writer_tid);
976
977 pthread_t reader_thread;
978 std::atomic<pid_t> reader_tid;
979 helper.CreateReaderThread(reader_thread, reader_tid);
980 ASSERT_EQ(0, pthread_join(reader_thread, NULL));
981
982 ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock));
983 ASSERT_EQ(0, pthread_join(writer_thread, NULL));
984}
985
986TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) {
987 RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
988 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock));
989
990 pthread_t writer_thread;
991 std::atomic<pid_t> writer_tid;
992 helper.CreateWriterThread(writer_thread, writer_tid);
993 WaitUntilThreadSleep(writer_tid);
994
995 pthread_t reader_thread;
996 std::atomic<pid_t> reader_tid;
997 helper.CreateReaderThread(reader_thread, reader_tid);
998 WaitUntilThreadSleep(reader_tid);
999
1000 ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock));
1001 ASSERT_EQ(0, pthread_join(writer_thread, NULL));
1002 ASSERT_EQ(0, pthread_join(reader_thread, NULL));
1003}
1004
Elliott Hughes1728b232014-05-14 10:02:03 -07001005static int g_once_fn_call_count = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -07001006static void OnceFn() {
Elliott Hughes1728b232014-05-14 10:02:03 -07001007 ++g_once_fn_call_count;
Elliott Hughesc3f11402013-10-30 14:40:09 -07001008}
1009
1010TEST(pthread, pthread_once_smoke) {
1011 pthread_once_t once_control = PTHREAD_ONCE_INIT;
1012 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
1013 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
Elliott Hughes1728b232014-05-14 10:02:03 -07001014 ASSERT_EQ(1, g_once_fn_call_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001015}
1016
Elliott Hughes3694ec62014-05-14 11:46:08 -07001017static std::string pthread_once_1934122_result = "";
1018
1019static void Routine2() {
1020 pthread_once_1934122_result += "2";
1021}
1022
1023static void Routine1() {
1024 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
1025 pthread_once_1934122_result += "1";
1026 pthread_once(&once_control_2, &Routine2);
1027}
1028
1029TEST(pthread, pthread_once_1934122) {
1030 // Very old versions of Android couldn't call pthread_once from a
1031 // pthread_once init routine. http://b/1934122.
1032 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
1033 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
1034 ASSERT_EQ("12", pthread_once_1934122_result);
1035}
1036
Elliott Hughes1728b232014-05-14 10:02:03 -07001037static int g_atfork_prepare_calls = 0;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001038static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; }
1039static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 2; }
Elliott Hughes1728b232014-05-14 10:02:03 -07001040static int g_atfork_parent_calls = 0;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001041static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 1; }
1042static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 2; }
Elliott Hughes1728b232014-05-14 10:02:03 -07001043static int g_atfork_child_calls = 0;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001044static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 1; }
1045static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 2; }
Elliott Hughesc3f11402013-10-30 14:40:09 -07001046
Dmitriy Ivanov00e37812014-11-20 16:53:47 -08001047TEST(pthread, pthread_atfork_smoke) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001048 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
1049 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
Elliott Hughesc3f11402013-10-30 14:40:09 -07001050
Elliott Hughes33697a02016-01-26 13:04:57 -08001051 pid_t pid = fork();
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001052 ASSERT_NE(-1, pid) << strerror(errno);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001053
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001054 // Child and parent calls are made in the order they were registered.
1055 if (pid == 0) {
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001056 ASSERT_EQ(12, g_atfork_child_calls);
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001057 _exit(0);
1058 }
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001059 ASSERT_EQ(12, g_atfork_parent_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001060
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001061 // Prepare calls are made in the reverse order.
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001062 ASSERT_EQ(21, g_atfork_prepare_calls);
Elliott Hughes33697a02016-01-26 13:04:57 -08001063 AssertChildExited(pid, 0);
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001064}
1065
Elliott Hughesc3f11402013-10-30 14:40:09 -07001066TEST(pthread, pthread_attr_getscope) {
1067 pthread_attr_t attr;
1068 ASSERT_EQ(0, pthread_attr_init(&attr));
1069
1070 int scope;
1071 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
1072 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
1073}
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001074
1075TEST(pthread, pthread_condattr_init) {
1076 pthread_condattr_t attr;
1077 pthread_condattr_init(&attr);
1078
1079 clockid_t clock;
1080 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1081 ASSERT_EQ(CLOCK_REALTIME, clock);
1082
1083 int pshared;
1084 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
1085 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
1086}
1087
1088TEST(pthread, pthread_condattr_setclock) {
1089 pthread_condattr_t attr;
1090 pthread_condattr_init(&attr);
1091
1092 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
1093 clockid_t clock;
1094 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1095 ASSERT_EQ(CLOCK_REALTIME, clock);
1096
1097 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
1098 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1099 ASSERT_EQ(CLOCK_MONOTONIC, clock);
1100
1101 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
1102}
1103
1104TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
Yabin Cui32651b82015-03-13 20:30:00 -07001105#if defined(__BIONIC__)
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001106 pthread_condattr_t attr;
1107 pthread_condattr_init(&attr);
1108
1109 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
1110 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
1111
1112 pthread_cond_t cond_var;
1113 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
1114
1115 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
1116 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
1117
Yabin Cui32651b82015-03-13 20:30:00 -07001118 attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private));
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001119 clockid_t clock;
1120 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1121 ASSERT_EQ(CLOCK_MONOTONIC, clock);
1122 int pshared;
1123 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
1124 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
Yabin Cui32651b82015-03-13 20:30:00 -07001125#else // !defined(__BIONIC__)
1126 GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n";
1127#endif // !defined(__BIONIC__)
1128}
1129
1130class pthread_CondWakeupTest : public ::testing::Test {
1131 protected:
1132 pthread_mutex_t mutex;
1133 pthread_cond_t cond;
1134
1135 enum Progress {
1136 INITIALIZED,
1137 WAITING,
1138 SIGNALED,
1139 FINISHED,
1140 };
1141 std::atomic<Progress> progress;
1142 pthread_t thread;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001143 std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function;
Yabin Cui32651b82015-03-13 20:30:00 -07001144
1145 protected:
Yabin Cuic9a659c2015-11-05 15:36:08 -08001146 void SetUp() override {
1147 ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr));
1148 }
1149
1150 void InitCond(clockid_t clock=CLOCK_REALTIME) {
1151 pthread_condattr_t attr;
1152 ASSERT_EQ(0, pthread_condattr_init(&attr));
1153 ASSERT_EQ(0, pthread_condattr_setclock(&attr, clock));
1154 ASSERT_EQ(0, pthread_cond_init(&cond, &attr));
1155 ASSERT_EQ(0, pthread_condattr_destroy(&attr));
1156 }
1157
1158 void StartWaitingThread(std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function) {
Yabin Cui32651b82015-03-13 20:30:00 -07001159 progress = INITIALIZED;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001160 this->wait_function = wait_function;
1161 ASSERT_EQ(0, pthread_create(&thread, NULL, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this));
1162 while (progress != WAITING) {
Yabin Cui32651b82015-03-13 20:30:00 -07001163 usleep(5000);
1164 }
1165 usleep(5000);
1166 }
1167
Yabin Cuic9a659c2015-11-05 15:36:08 -08001168 void TearDown() override {
1169 ASSERT_EQ(0, pthread_join(thread, nullptr));
1170 ASSERT_EQ(FINISHED, progress);
1171 ASSERT_EQ(0, pthread_cond_destroy(&cond));
1172 ASSERT_EQ(0, pthread_mutex_destroy(&mutex));
1173 }
1174
Yabin Cui32651b82015-03-13 20:30:00 -07001175 private:
1176 static void WaitThreadFn(pthread_CondWakeupTest* test) {
1177 ASSERT_EQ(0, pthread_mutex_lock(&test->mutex));
1178 test->progress = WAITING;
1179 while (test->progress == WAITING) {
Yabin Cuic9a659c2015-11-05 15:36:08 -08001180 ASSERT_EQ(0, test->wait_function(&test->cond, &test->mutex));
Yabin Cui32651b82015-03-13 20:30:00 -07001181 }
1182 ASSERT_EQ(SIGNALED, test->progress);
1183 test->progress = FINISHED;
1184 ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex));
1185 }
1186};
1187
Yabin Cuic9a659c2015-11-05 15:36:08 -08001188TEST_F(pthread_CondWakeupTest, signal_wait) {
1189 InitCond();
1190 StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1191 return pthread_cond_wait(cond, mutex);
1192 });
Yabin Cui32651b82015-03-13 20:30:00 -07001193 progress = SIGNALED;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001194 ASSERT_EQ(0, pthread_cond_signal(&cond));
Yabin Cui32651b82015-03-13 20:30:00 -07001195}
1196
Yabin Cuic9a659c2015-11-05 15:36:08 -08001197TEST_F(pthread_CondWakeupTest, broadcast_wait) {
1198 InitCond();
1199 StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1200 return pthread_cond_wait(cond, mutex);
1201 });
Yabin Cui32651b82015-03-13 20:30:00 -07001202 progress = SIGNALED;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001203 ASSERT_EQ(0, pthread_cond_broadcast(&cond));
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001204}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001205
Yabin Cuic9a659c2015-11-05 15:36:08 -08001206TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_REALTIME) {
1207 InitCond(CLOCK_REALTIME);
Elliott Hughes0e714a52014-03-03 16:42:47 -08001208 timespec ts;
1209 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001210 ts.tv_sec += 1;
1211 StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1212 return pthread_cond_timedwait(cond, mutex, &ts);
1213 });
1214 progress = SIGNALED;
1215 ASSERT_EQ(0, pthread_cond_signal(&cond));
1216}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001217
Yabin Cuic9a659c2015-11-05 15:36:08 -08001218TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC) {
1219 InitCond(CLOCK_MONOTONIC);
1220 timespec ts;
1221 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
1222 ts.tv_sec += 1;
1223 StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1224 return pthread_cond_timedwait(cond, mutex, &ts);
1225 });
1226 progress = SIGNALED;
1227 ASSERT_EQ(0, pthread_cond_signal(&cond));
1228}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001229
Yabin Cuic9a659c2015-11-05 15:36:08 -08001230TEST(pthread, pthread_cond_timedwait_timeout) {
1231 pthread_mutex_t mutex;
1232 ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr));
1233 pthread_cond_t cond;
1234 ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
1235 ASSERT_EQ(0, pthread_mutex_lock(&mutex));
1236 timespec ts;
Elliott Hughes0e714a52014-03-03 16:42:47 -08001237 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001238 ASSERT_EQ(ETIMEDOUT, pthread_cond_timedwait(&cond, &mutex, &ts));
1239 ts.tv_nsec = -1;
1240 ASSERT_EQ(EINVAL, pthread_cond_timedwait(&cond, &mutex, &ts));
1241 ts.tv_nsec = NS_PER_S;
1242 ASSERT_EQ(EINVAL, pthread_cond_timedwait(&cond, &mutex, &ts));
1243 ts.tv_nsec = NS_PER_S - 1;
1244 ts.tv_sec = -1;
1245 ASSERT_EQ(ETIMEDOUT, pthread_cond_timedwait(&cond, &mutex, &ts));
1246 ASSERT_EQ(0, pthread_mutex_unlock(&mutex));
Elliott Hughes0e714a52014-03-03 16:42:47 -08001247}
Elliott Hughes57b7a612014-08-25 17:26:50 -07001248
1249TEST(pthread, pthread_attr_getstack__main_thread) {
1250 // This test is only meaningful for the main thread, so make sure we're running on it!
1251 ASSERT_EQ(getpid(), syscall(__NR_gettid));
1252
1253 // Get the main thread's attributes.
1254 pthread_attr_t attributes;
1255 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1256
1257 // Check that we correctly report that the main thread has no guard page.
1258 size_t guard_size;
1259 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
1260 ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
1261
1262 // Get the stack base and the stack size (both ways).
1263 void* stack_base;
1264 size_t stack_size;
1265 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1266 size_t stack_size2;
1267 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1268
1269 // The two methods of asking for the stack size should agree.
1270 EXPECT_EQ(stack_size, stack_size2);
1271
Yabin Cuib0c6f2db2015-05-19 15:09:23 -07001272#if defined(__BIONIC__)
Elliott Hughes57b7a612014-08-25 17:26:50 -07001273 // What does /proc/self/maps' [stack] line say?
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001274 void* maps_stack_hi = NULL;
Elliott Hughes15dfd632015-09-22 16:40:14 -07001275 std::vector<map_record> maps;
1276 ASSERT_TRUE(Maps::parse_maps(&maps));
Elliott Hughes0b2acdf2015-10-02 18:25:19 -07001277 for (const auto& map : maps) {
Elliott Hughes15dfd632015-09-22 16:40:14 -07001278 if (map.pathname == "[stack]") {
1279 maps_stack_hi = reinterpret_cast<void*>(map.addr_end);
Elliott Hughes57b7a612014-08-25 17:26:50 -07001280 break;
1281 }
1282 }
Elliott Hughes57b7a612014-08-25 17:26:50 -07001283
Yabin Cuib0c6f2db2015-05-19 15:09:23 -07001284 // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
1285 // Remember that the stack grows down (and is mapped in on demand), so the low address of the
1286 // region isn't very interesting.
1287 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
1288
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001289 // The stack size should correspond to RLIMIT_STACK.
Elliott Hughes57b7a612014-08-25 17:26:50 -07001290 rlimit rl;
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001291 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001292 uint64_t original_rlim_cur = rl.rlim_cur;
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001293 if (rl.rlim_cur == RLIM_INFINITY) {
1294 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
1295 }
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001296 EXPECT_EQ(rl.rlim_cur, stack_size);
1297
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001298 auto guard = make_scope_guard([&rl, original_rlim_cur]() {
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001299 rl.rlim_cur = original_rlim_cur;
1300 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1301 });
1302
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001303 //
1304 // What if RLIMIT_STACK is smaller than the stack's current extent?
1305 //
Elliott Hughes57b7a612014-08-25 17:26:50 -07001306 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
1307 rl.rlim_max = RLIM_INFINITY;
1308 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1309
1310 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1311 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1312 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1313
1314 EXPECT_EQ(stack_size, stack_size2);
1315 ASSERT_EQ(1024U, stack_size);
1316
1317 //
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001318 // What if RLIMIT_STACK isn't a whole number of pages?
Elliott Hughes57b7a612014-08-25 17:26:50 -07001319 //
1320 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
1321 rl.rlim_max = RLIM_INFINITY;
1322 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1323
1324 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1325 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1326 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1327
1328 EXPECT_EQ(stack_size, stack_size2);
1329 ASSERT_EQ(6666U, stack_size);
Yabin Cuib0c6f2db2015-05-19 15:09:23 -07001330#endif
Elliott Hughes57b7a612014-08-25 17:26:50 -07001331}
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001332
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001333struct GetStackSignalHandlerArg {
1334 volatile bool done;
1335 void* signal_handler_sp;
1336 void* main_stack_base;
1337 size_t main_stack_size;
1338};
1339
1340static GetStackSignalHandlerArg getstack_signal_handler_arg;
1341
1342static void getstack_signal_handler(int sig) {
1343 ASSERT_EQ(SIGUSR1, sig);
1344 // Use sleep() to make current thread be switched out by the kernel to provoke the error.
1345 sleep(1);
1346 pthread_attr_t attr;
1347 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr));
1348 void* stack_base;
1349 size_t stack_size;
1350 ASSERT_EQ(0, pthread_attr_getstack(&attr, &stack_base, &stack_size));
1351 getstack_signal_handler_arg.signal_handler_sp = &attr;
1352 getstack_signal_handler_arg.main_stack_base = stack_base;
1353 getstack_signal_handler_arg.main_stack_size = stack_size;
1354 getstack_signal_handler_arg.done = true;
1355}
1356
1357// The previous code obtained the main thread's stack by reading the entry in
1358// /proc/self/task/<pid>/maps that was labeled [stack]. Unfortunately, on x86/x86_64, the kernel
1359// relies on sp0 in task state segment(tss) to label the stack map with [stack]. If the kernel
1360// switches a process while the main thread is in an alternate stack, then the kernel will label
1361// the wrong map with [stack]. This test verifies that when the above situation happens, the main
1362// thread's stack is found correctly.
1363TEST(pthread, pthread_attr_getstack_in_signal_handler) {
Yabin Cui61e4d462016-03-07 17:44:58 -08001364 // This test is only meaningful for the main thread, so make sure we're running on it!
1365 ASSERT_EQ(getpid(), syscall(__NR_gettid));
1366
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001367 const size_t sig_stack_size = 16 * 1024;
1368 void* sig_stack = mmap(NULL, sig_stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
1369 -1, 0);
1370 ASSERT_NE(MAP_FAILED, sig_stack);
1371 stack_t ss;
1372 ss.ss_sp = sig_stack;
1373 ss.ss_size = sig_stack_size;
1374 ss.ss_flags = 0;
1375 stack_t oss;
1376 ASSERT_EQ(0, sigaltstack(&ss, &oss));
1377
Yabin Cui61e4d462016-03-07 17:44:58 -08001378 pthread_attr_t attr;
1379 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr));
1380 void* main_stack_base;
1381 size_t main_stack_size;
1382 ASSERT_EQ(0, pthread_attr_getstack(&attr, &main_stack_base, &main_stack_size));
1383
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001384 ScopedSignalHandler handler(SIGUSR1, getstack_signal_handler, SA_ONSTACK);
1385 getstack_signal_handler_arg.done = false;
1386 kill(getpid(), SIGUSR1);
1387 ASSERT_EQ(true, getstack_signal_handler_arg.done);
1388
1389 // Verify if the stack used by the signal handler is the alternate stack just registered.
1390 ASSERT_LE(sig_stack, getstack_signal_handler_arg.signal_handler_sp);
1391 ASSERT_GE(reinterpret_cast<char*>(sig_stack) + sig_stack_size,
1392 getstack_signal_handler_arg.signal_handler_sp);
1393
1394 // Verify if the main thread's stack got in the signal handler is correct.
Yabin Cui61e4d462016-03-07 17:44:58 -08001395 ASSERT_EQ(main_stack_base, getstack_signal_handler_arg.main_stack_base);
1396 ASSERT_LE(main_stack_size, getstack_signal_handler_arg.main_stack_size);
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001397
1398 ASSERT_EQ(0, sigaltstack(&oss, nullptr));
1399 ASSERT_EQ(0, munmap(sig_stack, sig_stack_size));
1400}
1401
Yabin Cui917d3902015-01-08 12:32:42 -08001402static void pthread_attr_getstack_18908062_helper(void*) {
1403 char local_variable;
1404 pthread_attr_t attributes;
1405 pthread_getattr_np(pthread_self(), &attributes);
1406 void* stack_base;
1407 size_t stack_size;
1408 pthread_attr_getstack(&attributes, &stack_base, &stack_size);
1409
1410 // Test whether &local_variable is in [stack_base, stack_base + stack_size).
1411 ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable);
1412 ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size);
1413}
1414
1415// Check whether something on stack is in the range of
1416// [stack_base, stack_base + stack_size). see b/18908062.
1417TEST(pthread, pthread_attr_getstack_18908062) {
1418 pthread_t t;
1419 ASSERT_EQ(0, pthread_create(&t, NULL,
1420 reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper),
1421 NULL));
1422 pthread_join(t, NULL);
1423}
1424
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001425#if defined(__BIONIC__)
Elliott Hughesf2083612015-11-11 13:32:28 -08001426static pthread_mutex_t pthread_gettid_np_mutex = PTHREAD_MUTEX_INITIALIZER;
1427
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001428static void* pthread_gettid_np_helper(void* arg) {
1429 *reinterpret_cast<pid_t*>(arg) = gettid();
Elliott Hughesf2083612015-11-11 13:32:28 -08001430
1431 // Wait for our parent to call pthread_gettid_np on us before exiting.
1432 pthread_mutex_lock(&pthread_gettid_np_mutex);
1433 pthread_mutex_unlock(&pthread_gettid_np_mutex);
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001434 return NULL;
1435}
1436#endif
1437
1438TEST(pthread, pthread_gettid_np) {
1439#if defined(__BIONIC__)
1440 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
1441
Elliott Hughesf2083612015-11-11 13:32:28 -08001442 // Ensure the other thread doesn't exit until after we've called
1443 // pthread_gettid_np on it.
1444 pthread_mutex_lock(&pthread_gettid_np_mutex);
1445
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001446 pid_t t_gettid_result;
1447 pthread_t t;
1448 pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
1449
1450 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
1451
Elliott Hughesf2083612015-11-11 13:32:28 -08001452 // Release the other thread and wait for it to exit.
1453 pthread_mutex_unlock(&pthread_gettid_np_mutex);
Elliott Hughes34c987a2014-09-22 16:01:26 -07001454 pthread_join(t, NULL);
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001455
1456 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
1457#else
1458 GTEST_LOG_(INFO) << "This test does nothing.\n";
1459#endif
1460}
Elliott Hughes34c987a2014-09-22 16:01:26 -07001461
1462static size_t cleanup_counter = 0;
1463
Derek Xue41996952014-09-25 11:05:32 +01001464static void AbortCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001465 abort();
1466}
1467
Derek Xue41996952014-09-25 11:05:32 +01001468static void CountCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001469 ++cleanup_counter;
1470}
1471
Derek Xue41996952014-09-25 11:05:32 +01001472static void PthreadCleanupTester() {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001473 pthread_cleanup_push(CountCleanupRoutine, NULL);
1474 pthread_cleanup_push(CountCleanupRoutine, NULL);
1475 pthread_cleanup_push(AbortCleanupRoutine, NULL);
1476
1477 pthread_cleanup_pop(0); // Pop the abort without executing it.
1478 pthread_cleanup_pop(1); // Pop one count while executing it.
1479 ASSERT_EQ(1U, cleanup_counter);
1480 // Exit while the other count is still on the cleanup stack.
1481 pthread_exit(NULL);
1482
1483 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
1484 pthread_cleanup_pop(0);
1485}
1486
Derek Xue41996952014-09-25 11:05:32 +01001487static void* PthreadCleanupStartRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001488 PthreadCleanupTester();
1489 return NULL;
1490}
1491
1492TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
1493 pthread_t t;
1494 ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL));
1495 pthread_join(t, NULL);
1496 ASSERT_EQ(2U, cleanup_counter);
1497}
Derek Xue41996952014-09-25 11:05:32 +01001498
1499TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
1500 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
1501}
1502
1503TEST(pthread, pthread_mutexattr_gettype) {
1504 pthread_mutexattr_t attr;
1505 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1506
1507 int attr_type;
1508
1509 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1510 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1511 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
1512
1513 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1514 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1515 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
1516
1517 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1518 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1519 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001520
1521 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1522}
1523
Yabin Cui17393b02015-03-21 15:08:25 -07001524struct PthreadMutex {
1525 pthread_mutex_t lock;
1526
1527 PthreadMutex(int mutex_type) {
1528 init(mutex_type);
1529 }
1530
1531 ~PthreadMutex() {
1532 destroy();
1533 }
1534
1535 private:
1536 void init(int mutex_type) {
1537 pthread_mutexattr_t attr;
1538 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1539 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, mutex_type));
1540 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1541 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1542 }
1543
1544 void destroy() {
1545 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1546 }
1547
1548 DISALLOW_COPY_AND_ASSIGN(PthreadMutex);
1549};
Derek Xue41996952014-09-25 11:05:32 +01001550
1551TEST(pthread, pthread_mutex_lock_NORMAL) {
Yabin Cui17393b02015-03-21 15:08:25 -07001552 PthreadMutex m(PTHREAD_MUTEX_NORMAL);
Derek Xue41996952014-09-25 11:05:32 +01001553
Yabin Cui17393b02015-03-21 15:08:25 -07001554 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1555 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Elliott Hughesd31d4c12015-12-14 17:35:10 -08001556 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1557 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock));
1558 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Derek Xue41996952014-09-25 11:05:32 +01001559}
1560
1561TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
Yabin Cui17393b02015-03-21 15:08:25 -07001562 PthreadMutex m(PTHREAD_MUTEX_ERRORCHECK);
Derek Xue41996952014-09-25 11:05:32 +01001563
Yabin Cui17393b02015-03-21 15:08:25 -07001564 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1565 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&m.lock));
1566 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1567 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1568 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock));
1569 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1570 ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
Derek Xue41996952014-09-25 11:05:32 +01001571}
1572
1573TEST(pthread, pthread_mutex_lock_RECURSIVE) {
Yabin Cui17393b02015-03-21 15:08:25 -07001574 PthreadMutex m(PTHREAD_MUTEX_RECURSIVE);
Derek Xue41996952014-09-25 11:05:32 +01001575
Yabin Cui17393b02015-03-21 15:08:25 -07001576 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1577 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1578 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1579 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1580 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
Elliott Hughesd31d4c12015-12-14 17:35:10 -08001581 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1582 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Yabin Cui17393b02015-03-21 15:08:25 -07001583 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1584 ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
1585}
1586
1587TEST(pthread, pthread_mutex_init_same_as_static_initializers) {
1588 pthread_mutex_t lock_normal = PTHREAD_MUTEX_INITIALIZER;
1589 PthreadMutex m1(PTHREAD_MUTEX_NORMAL);
1590 ASSERT_EQ(0, memcmp(&lock_normal, &m1.lock, sizeof(pthread_mutex_t)));
1591 pthread_mutex_destroy(&lock_normal);
1592
1593 pthread_mutex_t lock_errorcheck = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
1594 PthreadMutex m2(PTHREAD_MUTEX_ERRORCHECK);
1595 ASSERT_EQ(0, memcmp(&lock_errorcheck, &m2.lock, sizeof(pthread_mutex_t)));
1596 pthread_mutex_destroy(&lock_errorcheck);
1597
1598 pthread_mutex_t lock_recursive = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
1599 PthreadMutex m3(PTHREAD_MUTEX_RECURSIVE);
1600 ASSERT_EQ(0, memcmp(&lock_recursive, &m3.lock, sizeof(pthread_mutex_t)));
1601 ASSERT_EQ(0, pthread_mutex_destroy(&lock_recursive));
Derek Xue41996952014-09-25 11:05:32 +01001602}
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001603class MutexWakeupHelper {
1604 private:
Yabin Cui17393b02015-03-21 15:08:25 -07001605 PthreadMutex m;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001606 enum Progress {
1607 LOCK_INITIALIZED,
1608 LOCK_WAITING,
1609 LOCK_RELEASED,
1610 LOCK_ACCESSED
1611 };
1612 std::atomic<Progress> progress;
Yabin Cuif7969852015-04-02 17:47:48 -07001613 std::atomic<pid_t> tid;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001614
1615 static void thread_fn(MutexWakeupHelper* helper) {
Yabin Cuif7969852015-04-02 17:47:48 -07001616 helper->tid = gettid();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001617 ASSERT_EQ(LOCK_INITIALIZED, helper->progress);
1618 helper->progress = LOCK_WAITING;
1619
Yabin Cui17393b02015-03-21 15:08:25 -07001620 ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001621 ASSERT_EQ(LOCK_RELEASED, helper->progress);
Yabin Cui17393b02015-03-21 15:08:25 -07001622 ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001623
1624 helper->progress = LOCK_ACCESSED;
1625 }
1626
1627 public:
Yabin Cui17393b02015-03-21 15:08:25 -07001628 MutexWakeupHelper(int mutex_type) : m(mutex_type) {
1629 }
1630
1631 void test() {
1632 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001633 progress = LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -07001634 tid = 0;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001635
1636 pthread_t thread;
1637 ASSERT_EQ(0, pthread_create(&thread, NULL,
1638 reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this));
1639
Yabin Cuif7969852015-04-02 17:47:48 -07001640 WaitUntilThreadSleep(tid);
1641 ASSERT_EQ(LOCK_WAITING, progress);
1642
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001643 progress = LOCK_RELEASED;
Yabin Cui17393b02015-03-21 15:08:25 -07001644 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001645
1646 ASSERT_EQ(0, pthread_join(thread, NULL));
1647 ASSERT_EQ(LOCK_ACCESSED, progress);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001648 }
1649};
1650
1651TEST(pthread, pthread_mutex_NORMAL_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001652 MutexWakeupHelper helper(PTHREAD_MUTEX_NORMAL);
1653 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001654}
1655
1656TEST(pthread, pthread_mutex_ERRORCHECK_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001657 MutexWakeupHelper helper(PTHREAD_MUTEX_ERRORCHECK);
1658 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001659}
1660
1661TEST(pthread, pthread_mutex_RECURSIVE_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001662 MutexWakeupHelper helper(PTHREAD_MUTEX_RECURSIVE);
1663 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001664}
1665
Yabin Cui140f3672015-02-03 10:32:00 -08001666TEST(pthread, pthread_mutex_owner_tid_limit) {
Yabin Cuie69c2452015-02-13 16:21:25 -08001667#if defined(__BIONIC__) && !defined(__LP64__)
Yabin Cui140f3672015-02-03 10:32:00 -08001668 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
1669 ASSERT_TRUE(fp != NULL);
1670 long pid_max;
1671 ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max));
1672 fclose(fp);
Yabin Cuie69c2452015-02-13 16:21:25 -08001673 // Bionic's pthread_mutex implementation on 32-bit devices uses 16 bits to represent owner tid.
Yabin Cui140f3672015-02-03 10:32:00 -08001674 ASSERT_LE(pid_max, 65536);
Yabin Cuie69c2452015-02-13 16:21:25 -08001675#else
1676 GTEST_LOG_(INFO) << "This test does nothing as 32-bit tid is supported by pthread_mutex.\n";
1677#endif
Yabin Cui140f3672015-02-03 10:32:00 -08001678}
Yabin Cuib5845722015-03-16 22:46:42 -07001679
Yabin Cuic9a659c2015-11-05 15:36:08 -08001680TEST(pthread, pthread_mutex_timedlock) {
1681 pthread_mutex_t m;
1682 ASSERT_EQ(0, pthread_mutex_init(&m, nullptr));
1683
1684 // If the mutex is already locked, pthread_mutex_timedlock should time out.
1685 ASSERT_EQ(0, pthread_mutex_lock(&m));
1686
1687 timespec ts;
1688 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1689 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
1690 ts.tv_nsec = -1;
1691 ASSERT_EQ(EINVAL, pthread_mutex_timedlock(&m, &ts));
1692 ts.tv_nsec = NS_PER_S;
1693 ASSERT_EQ(EINVAL, pthread_mutex_timedlock(&m, &ts));
1694 ts.tv_nsec = NS_PER_S - 1;
1695 ts.tv_sec = -1;
1696 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
1697
1698 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
1699 ASSERT_EQ(0, pthread_mutex_unlock(&m));
1700
1701 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1702 ts.tv_sec += 1;
1703 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
1704
1705 ASSERT_EQ(0, pthread_mutex_unlock(&m));
1706 ASSERT_EQ(0, pthread_mutex_destroy(&m));
1707}
1708
Yabin Cuib5845722015-03-16 22:46:42 -07001709class StrictAlignmentAllocator {
1710 public:
1711 void* allocate(size_t size, size_t alignment) {
1712 char* p = new char[size + alignment * 2];
1713 allocated_array.push_back(p);
1714 while (!is_strict_aligned(p, alignment)) {
1715 ++p;
1716 }
1717 return p;
1718 }
1719
1720 ~StrictAlignmentAllocator() {
Elliott Hughes0b2acdf2015-10-02 18:25:19 -07001721 for (const auto& p : allocated_array) {
1722 delete[] p;
Yabin Cuib5845722015-03-16 22:46:42 -07001723 }
1724 }
1725
1726 private:
1727 bool is_strict_aligned(char* p, size_t alignment) {
1728 return (reinterpret_cast<uintptr_t>(p) % (alignment * 2)) == alignment;
1729 }
1730
1731 std::vector<char*> allocated_array;
1732};
1733
1734TEST(pthread, pthread_types_allow_four_bytes_alignment) {
1735#if defined(__BIONIC__)
1736 // For binary compatibility with old version, we need to allow 4-byte aligned data for pthread types.
1737 StrictAlignmentAllocator allocator;
1738 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(
1739 allocator.allocate(sizeof(pthread_mutex_t), 4));
1740 ASSERT_EQ(0, pthread_mutex_init(mutex, NULL));
1741 ASSERT_EQ(0, pthread_mutex_lock(mutex));
1742 ASSERT_EQ(0, pthread_mutex_unlock(mutex));
1743 ASSERT_EQ(0, pthread_mutex_destroy(mutex));
1744
1745 pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>(
1746 allocator.allocate(sizeof(pthread_cond_t), 4));
1747 ASSERT_EQ(0, pthread_cond_init(cond, NULL));
1748 ASSERT_EQ(0, pthread_cond_signal(cond));
1749 ASSERT_EQ(0, pthread_cond_broadcast(cond));
1750 ASSERT_EQ(0, pthread_cond_destroy(cond));
1751
1752 pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>(
1753 allocator.allocate(sizeof(pthread_rwlock_t), 4));
1754 ASSERT_EQ(0, pthread_rwlock_init(rwlock, NULL));
1755 ASSERT_EQ(0, pthread_rwlock_rdlock(rwlock));
1756 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
1757 ASSERT_EQ(0, pthread_rwlock_wrlock(rwlock));
1758 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
1759 ASSERT_EQ(0, pthread_rwlock_destroy(rwlock));
1760
1761#else
1762 GTEST_LOG_(INFO) << "This test tests bionic implementation details.";
1763#endif
1764}
Christopher Ferris60907c72015-06-09 18:46:15 -07001765
1766TEST(pthread, pthread_mutex_lock_null_32) {
1767#if defined(__BIONIC__) && !defined(__LP64__)
1768 ASSERT_EQ(EINVAL, pthread_mutex_lock(NULL));
1769#else
1770 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices.";
1771#endif
1772}
1773
1774TEST(pthread, pthread_mutex_unlock_null_32) {
1775#if defined(__BIONIC__) && !defined(__LP64__)
1776 ASSERT_EQ(EINVAL, pthread_mutex_unlock(NULL));
1777#else
1778 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices.";
1779#endif
1780}
1781
1782TEST_F(pthread_DeathTest, pthread_mutex_lock_null_64) {
1783#if defined(__BIONIC__) && defined(__LP64__)
1784 pthread_mutex_t* null_value = nullptr;
1785 ASSERT_EXIT(pthread_mutex_lock(null_value), testing::KilledBySignal(SIGSEGV), "");
1786#else
1787 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices.";
1788#endif
1789}
1790
1791TEST_F(pthread_DeathTest, pthread_mutex_unlock_null_64) {
1792#if defined(__BIONIC__) && defined(__LP64__)
1793 pthread_mutex_t* null_value = nullptr;
1794 ASSERT_EXIT(pthread_mutex_unlock(null_value), testing::KilledBySignal(SIGSEGV), "");
1795#else
1796 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices.";
1797#endif
1798}
Yabin Cui33ac04a2015-09-22 11:16:15 -07001799
1800extern _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx, void* arg);
1801
1802static volatile bool signal_handler_on_altstack_done;
1803
1804static void SignalHandlerOnAltStack(int signo, siginfo_t*, void*) {
1805 ASSERT_EQ(SIGUSR1, signo);
1806 // Check if we have enough stack space for unwinding.
1807 int count = 0;
1808 _Unwind_Backtrace(FrameCounter, &count);
1809 ASSERT_GT(count, 0);
1810 // Check if we have enough stack space for logging.
1811 std::string s(2048, '*');
1812 GTEST_LOG_(INFO) << s;
1813 signal_handler_on_altstack_done = true;
1814}
1815
1816TEST(pthread, big_enough_signal_stack_for_64bit_arch) {
1817 signal_handler_on_altstack_done = false;
1818 ScopedSignalHandler handler(SIGUSR1, SignalHandlerOnAltStack, SA_SIGINFO | SA_ONSTACK);
1819 kill(getpid(), SIGUSR1);
1820 ASSERT_TRUE(signal_handler_on_altstack_done);
1821}
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001822
1823TEST(pthread, pthread_barrierattr_smoke) {
1824 pthread_barrierattr_t attr;
1825 ASSERT_EQ(0, pthread_barrierattr_init(&attr));
1826 int pshared;
1827 ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared));
1828 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
1829 ASSERT_EQ(0, pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
1830 ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared));
1831 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
1832 ASSERT_EQ(0, pthread_barrierattr_destroy(&attr));
1833}
1834
Yabin Cui81d27972016-03-22 13:45:55 -07001835struct BarrierTestHelperData {
1836 size_t thread_count;
1837 pthread_barrier_t barrier;
1838 std::atomic<int> finished_mask;
1839 std::atomic<int> serial_thread_count;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001840 size_t iteration_count;
Yabin Cui81d27972016-03-22 13:45:55 -07001841 std::atomic<size_t> finished_iteration_count;
1842
1843 BarrierTestHelperData(size_t thread_count, size_t iteration_count)
1844 : thread_count(thread_count), finished_mask(0), serial_thread_count(0),
1845 iteration_count(iteration_count), finished_iteration_count(0) {
1846 }
1847};
1848
1849struct BarrierTestHelperArg {
1850 int id;
1851 BarrierTestHelperData* data;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001852};
1853
1854static void BarrierTestHelper(BarrierTestHelperArg* arg) {
Yabin Cui81d27972016-03-22 13:45:55 -07001855 for (size_t i = 0; i < arg->data->iteration_count; ++i) {
1856 int result = pthread_barrier_wait(&arg->data->barrier);
1857 if (result == PTHREAD_BARRIER_SERIAL_THREAD) {
1858 arg->data->serial_thread_count++;
1859 } else {
1860 ASSERT_EQ(0, result);
1861 }
1862 arg->data->finished_mask |= (1 << arg->id);
1863 if (arg->data->finished_mask == ((1 << arg->data->thread_count) - 1)) {
1864 ASSERT_EQ(1, arg->data->serial_thread_count);
1865 arg->data->finished_iteration_count++;
1866 arg->data->finished_mask = 0;
1867 arg->data->serial_thread_count = 0;
1868 }
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001869 }
1870}
1871
1872TEST(pthread, pthread_barrier_smoke) {
1873 const size_t BARRIER_ITERATION_COUNT = 10;
1874 const size_t BARRIER_THREAD_COUNT = 10;
Yabin Cui81d27972016-03-22 13:45:55 -07001875 BarrierTestHelperData data(BARRIER_THREAD_COUNT, BARRIER_ITERATION_COUNT);
1876 ASSERT_EQ(0, pthread_barrier_init(&data.barrier, nullptr, data.thread_count));
1877 std::vector<pthread_t> threads(data.thread_count);
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001878 std::vector<BarrierTestHelperArg> args(threads.size());
1879 for (size_t i = 0; i < threads.size(); ++i) {
Yabin Cui81d27972016-03-22 13:45:55 -07001880 args[i].id = i;
1881 args[i].data = &data;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001882 ASSERT_EQ(0, pthread_create(&threads[i], nullptr,
1883 reinterpret_cast<void* (*)(void*)>(BarrierTestHelper), &args[i]));
1884 }
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001885 for (size_t i = 0; i < threads.size(); ++i) {
1886 ASSERT_EQ(0, pthread_join(threads[i], nullptr));
1887 }
Yabin Cui81d27972016-03-22 13:45:55 -07001888 ASSERT_EQ(data.iteration_count, data.finished_iteration_count);
1889 ASSERT_EQ(0, pthread_barrier_destroy(&data.barrier));
1890}
1891
1892struct BarrierDestroyTestArg {
1893 std::atomic<int> tid;
1894 pthread_barrier_t* barrier;
1895};
1896
1897static void BarrierDestroyTestHelper(BarrierDestroyTestArg* arg) {
1898 arg->tid = gettid();
1899 ASSERT_EQ(0, pthread_barrier_wait(arg->barrier));
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001900}
1901
1902TEST(pthread, pthread_barrier_destroy) {
1903 pthread_barrier_t barrier;
1904 ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, 2));
1905 pthread_t thread;
Yabin Cui81d27972016-03-22 13:45:55 -07001906 BarrierDestroyTestArg arg;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001907 arg.tid = 0;
1908 arg.barrier = &barrier;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001909 ASSERT_EQ(0, pthread_create(&thread, nullptr,
Yabin Cui81d27972016-03-22 13:45:55 -07001910 reinterpret_cast<void* (*)(void*)>(BarrierDestroyTestHelper), &arg));
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001911 WaitUntilThreadSleep(arg.tid);
1912 ASSERT_EQ(EBUSY, pthread_barrier_destroy(&barrier));
1913 ASSERT_EQ(PTHREAD_BARRIER_SERIAL_THREAD, pthread_barrier_wait(&barrier));
1914 // Verify if the barrier can be destroyed directly after pthread_barrier_wait().
1915 ASSERT_EQ(0, pthread_barrier_destroy(&barrier));
1916 ASSERT_EQ(0, pthread_join(thread, nullptr));
1917#if defined(__BIONIC__)
1918 ASSERT_EQ(EINVAL, pthread_barrier_destroy(&barrier));
1919#endif
1920}
1921
1922struct BarrierOrderingTestHelperArg {
1923 pthread_barrier_t* barrier;
1924 size_t* array;
1925 size_t array_length;
1926 size_t id;
1927};
1928
1929void BarrierOrderingTestHelper(BarrierOrderingTestHelperArg* arg) {
1930 const size_t ITERATION_COUNT = 10000;
1931 for (size_t i = 1; i <= ITERATION_COUNT; ++i) {
1932 arg->array[arg->id] = i;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001933 int result = pthread_barrier_wait(arg->barrier);
1934 ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD);
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001935 for (size_t j = 0; j < arg->array_length; ++j) {
1936 ASSERT_EQ(i, arg->array[j]);
1937 }
Yabin Cuic9a659c2015-11-05 15:36:08 -08001938 result = pthread_barrier_wait(arg->barrier);
1939 ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD);
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001940 }
1941}
1942
1943TEST(pthread, pthread_barrier_check_ordering) {
1944 const size_t THREAD_COUNT = 4;
1945 pthread_barrier_t barrier;
1946 ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, THREAD_COUNT));
1947 size_t array[THREAD_COUNT];
1948 std::vector<pthread_t> threads(THREAD_COUNT);
1949 std::vector<BarrierOrderingTestHelperArg> args(THREAD_COUNT);
1950 for (size_t i = 0; i < THREAD_COUNT; ++i) {
1951 args[i].barrier = &barrier;
1952 args[i].array = array;
1953 args[i].array_length = THREAD_COUNT;
1954 args[i].id = i;
1955 ASSERT_EQ(0, pthread_create(&threads[i], nullptr,
1956 reinterpret_cast<void* (*)(void*)>(BarrierOrderingTestHelper),
1957 &args[i]));
1958 }
1959 for (size_t i = 0; i < THREAD_COUNT; ++i) {
1960 ASSERT_EQ(0, pthread_join(threads[i], nullptr));
1961 }
1962}
Yabin Cuife3a83a2015-11-17 16:03:18 -08001963
1964TEST(pthread, pthread_spinlock_smoke) {
1965 pthread_spinlock_t lock;
1966 ASSERT_EQ(0, pthread_spin_init(&lock, 0));
1967 ASSERT_EQ(0, pthread_spin_trylock(&lock));
1968 ASSERT_EQ(0, pthread_spin_unlock(&lock));
1969 ASSERT_EQ(0, pthread_spin_lock(&lock));
1970 ASSERT_EQ(EBUSY, pthread_spin_trylock(&lock));
1971 ASSERT_EQ(0, pthread_spin_unlock(&lock));
1972 ASSERT_EQ(0, pthread_spin_destroy(&lock));
1973}