blob: b4667f93940e0275c92c0f0d3a91abaa0465906d [file] [log] [blame]
Elliott Hughesbfeab1b2012-09-05 17:47:37 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
Yabin Cui9df70402014-11-05 18:01:01 -080019#include "private/ScopeGuard.h"
20#include "BionicDeathTest.h"
21#include "ScopedSignalHandler.h"
22
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070023#include <errno.h>
Elliott Hughes5b9310e2013-10-02 16:59:05 -070024#include <inttypes.h>
Elliott Hughesb95cf0d2013-07-15 14:51:07 -070025#include <limits.h>
Elliott Hughes04620a32014-03-07 17:59:05 -080026#include <malloc.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070027#include <pthread.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080028#include <signal.h>
Yabin Cui140f3672015-02-03 10:32:00 -080029#include <stdio.h>
Elliott Hughes70b24b12013-11-15 11:51:07 -080030#include <sys/mman.h>
Elliott Hughes57b7a612014-08-25 17:26:50 -070031#include <sys/syscall.h>
Narayan Kamath51e6cb32014-03-03 15:38:51 +000032#include <time.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070033#include <unistd.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070034
Yabin Cui08ee8d22015-02-11 17:04:36 -080035#include <atomic>
Yabin Cuib5845722015-03-16 22:46:42 -070036#include <vector>
Yabin Cui08ee8d22015-02-11 17:04:36 -080037
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070038TEST(pthread, pthread_key_create) {
39 pthread_key_t key;
40 ASSERT_EQ(0, pthread_key_create(&key, NULL));
41 ASSERT_EQ(0, pthread_key_delete(key));
42 // Can't delete a key that's already been deleted.
43 ASSERT_EQ(EINVAL, pthread_key_delete(key));
44}
Elliott Hughes4d014e12012-09-07 16:47:54 -070045
Dan Albertc4bcc752014-09-30 11:48:24 -070046TEST(pthread, pthread_keys_max) {
Yabin Cui6c238f22014-12-11 20:50:41 -080047 // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX.
48 ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070049}
Elliott Hughes718a5b52014-01-28 17:02:03 -080050
Yabin Cui6c238f22014-12-11 20:50:41 -080051TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) {
Dan Albertc4bcc752014-09-30 11:48:24 -070052 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
Yabin Cui6c238f22014-12-11 20:50:41 -080053 ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070054}
55
56TEST(pthread, pthread_key_many_distinct) {
Yabin Cui6c238f22014-12-11 20:50:41 -080057 // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX
58 // pthread keys, but We should be able to allocate at least this many keys.
59 int nkeys = PTHREAD_KEYS_MAX / 2;
Dan Albertc4bcc752014-09-30 11:48:24 -070060 std::vector<pthread_key_t> keys;
61
62 auto scope_guard = make_scope_guard([&keys]{
63 for (auto key : keys) {
64 EXPECT_EQ(0, pthread_key_delete(key));
65 }
66 });
67
68 for (int i = 0; i < nkeys; ++i) {
69 pthread_key_t key;
70 // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is
71 // wrong.
72 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys;
73 keys.push_back(key);
74 ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i)));
75 }
76
77 for (int i = keys.size() - 1; i >= 0; --i) {
78 ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back()));
79 pthread_key_t key = keys.back();
80 keys.pop_back();
81 ASSERT_EQ(0, pthread_key_delete(key));
82 }
83}
84
Yabin Cui6c238f22014-12-11 20:50:41 -080085TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000086 std::vector<pthread_key_t> keys;
Dan Albertc4bcc752014-09-30 11:48:24 -070087 int rv = 0;
Yabin Cui6c238f22014-12-11 20:50:41 -080088
89 // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should
90 // be more than we are allowed to allocate now.
91 for (int i = 0; i < PTHREAD_KEYS_MAX; i++) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000092 pthread_key_t key;
Dan Albertc4bcc752014-09-30 11:48:24 -070093 rv = pthread_key_create(&key, NULL);
94 if (rv == EAGAIN) {
95 break;
96 }
97 EXPECT_EQ(0, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +000098 keys.push_back(key);
99 }
100
Dan Albertc4bcc752014-09-30 11:48:24 -0700101 // Don't leak keys.
102 for (auto key : keys) {
103 EXPECT_EQ(0, pthread_key_delete(key));
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000104 }
Dan Albertc4bcc752014-09-30 11:48:24 -0700105 keys.clear();
106
107 // We should have eventually reached the maximum number of keys and received
108 // EAGAIN.
109 ASSERT_EQ(EAGAIN, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000110}
111
Elliott Hughesebb770f2014-06-25 13:46:46 -0700112TEST(pthread, pthread_key_delete) {
113 void* expected = reinterpret_cast<void*>(1234);
114 pthread_key_t key;
115 ASSERT_EQ(0, pthread_key_create(&key, NULL));
116 ASSERT_EQ(0, pthread_setspecific(key, expected));
117 ASSERT_EQ(expected, pthread_getspecific(key));
118 ASSERT_EQ(0, pthread_key_delete(key));
119 // After deletion, pthread_getspecific returns NULL.
120 ASSERT_EQ(NULL, pthread_getspecific(key));
121 // And you can't use pthread_setspecific with the deleted key.
122 ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
123}
124
Elliott Hughes40a52172014-07-30 14:48:10 -0700125TEST(pthread, pthread_key_fork) {
126 void* expected = reinterpret_cast<void*>(1234);
127 pthread_key_t key;
128 ASSERT_EQ(0, pthread_key_create(&key, NULL));
129 ASSERT_EQ(0, pthread_setspecific(key, expected));
130 ASSERT_EQ(expected, pthread_getspecific(key));
131
132 pid_t pid = fork();
133 ASSERT_NE(-1, pid) << strerror(errno);
134
135 if (pid == 0) {
136 // The surviving thread inherits all the forking thread's TLS values...
137 ASSERT_EQ(expected, pthread_getspecific(key));
138 _exit(99);
139 }
140
141 int status;
142 ASSERT_EQ(pid, waitpid(pid, &status, 0));
143 ASSERT_TRUE(WIFEXITED(status));
144 ASSERT_EQ(99, WEXITSTATUS(status));
145
146 ASSERT_EQ(expected, pthread_getspecific(key));
Dan Albert1d53ae22014-09-02 15:24:26 -0700147 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700148}
149
150static void* DirtyKeyFn(void* key) {
151 return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
152}
153
154TEST(pthread, pthread_key_dirty) {
155 pthread_key_t key;
156 ASSERT_EQ(0, pthread_key_create(&key, NULL));
157
158 size_t stack_size = 128 * 1024;
159 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
160 ASSERT_NE(MAP_FAILED, stack);
161 memset(stack, 0xff, stack_size);
162
163 pthread_attr_t attr;
164 ASSERT_EQ(0, pthread_attr_init(&attr));
165 ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
166
167 pthread_t t;
168 ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
169
170 void* result;
171 ASSERT_EQ(0, pthread_join(t, &result));
172 ASSERT_EQ(nullptr, result); // Not ~0!
173
174 ASSERT_EQ(0, munmap(stack, stack_size));
Dan Albert1d53ae22014-09-02 15:24:26 -0700175 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700176}
177
Elliott Hughes4d014e12012-09-07 16:47:54 -0700178static void* IdFn(void* arg) {
179 return arg;
180}
181
Yabin Cui63481602014-12-01 17:41:04 -0800182class SpinFunctionHelper {
183 public:
184 SpinFunctionHelper() {
185 SpinFunctionHelper::spin_flag_ = true;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400186 }
Yabin Cui63481602014-12-01 17:41:04 -0800187 ~SpinFunctionHelper() {
188 UnSpin();
189 }
190 auto GetFunction() -> void* (*)(void*) {
191 return SpinFunctionHelper::SpinFn;
192 }
193
194 void UnSpin() {
195 SpinFunctionHelper::spin_flag_ = false;
196 }
197
198 private:
199 static void* SpinFn(void*) {
200 while (spin_flag_) {}
201 return NULL;
202 }
203 static volatile bool spin_flag_;
204};
205
206// It doesn't matter if spin_flag_ is used in several tests,
207// because it is always set to false after each test. Each thread
208// loops on spin_flag_ can find it becomes false at some time.
209volatile bool SpinFunctionHelper::spin_flag_ = false;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400210
Elliott Hughes4d014e12012-09-07 16:47:54 -0700211static void* JoinFn(void* arg) {
212 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
213}
214
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400215static void AssertDetached(pthread_t t, bool is_detached) {
216 pthread_attr_t attr;
217 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
218 int detach_state;
219 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
220 pthread_attr_destroy(&attr);
221 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
222}
223
Elliott Hughes9d23e042013-02-15 19:21:51 -0800224static void MakeDeadThread(pthread_t& t) {
225 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
Elliott Hughes34c987a2014-09-22 16:01:26 -0700226 ASSERT_EQ(0, pthread_join(t, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800227}
228
Elliott Hughes4d014e12012-09-07 16:47:54 -0700229TEST(pthread, pthread_create) {
230 void* expected_result = reinterpret_cast<void*>(123);
231 // Can we create a thread?
232 pthread_t t;
233 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
234 // If we join, do we get the expected value back?
235 void* result;
236 ASSERT_EQ(0, pthread_join(t, &result));
237 ASSERT_EQ(expected_result, result);
238}
239
Elliott Hughes3e898472013-02-12 16:40:24 +0000240TEST(pthread, pthread_create_EAGAIN) {
241 pthread_attr_t attributes;
242 ASSERT_EQ(0, pthread_attr_init(&attributes));
243 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
244
245 pthread_t t;
246 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
247}
248
Elliott Hughes4d014e12012-09-07 16:47:54 -0700249TEST(pthread, pthread_no_join_after_detach) {
Yabin Cui63481602014-12-01 17:41:04 -0800250 SpinFunctionHelper spinhelper;
251
Elliott Hughes4d014e12012-09-07 16:47:54 -0700252 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800253 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700254
255 // After a pthread_detach...
256 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400257 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700258
259 // ...pthread_join should fail.
Elliott Hughes34c987a2014-09-22 16:01:26 -0700260 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700261}
262
263TEST(pthread, pthread_no_op_detach_after_join) {
Yabin Cui63481602014-12-01 17:41:04 -0800264 SpinFunctionHelper spinhelper;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400265
Elliott Hughes4d014e12012-09-07 16:47:54 -0700266 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800267 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700268
269 // If thread 2 is already waiting to join thread 1...
270 pthread_t t2;
271 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
272
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400273 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700274
Yabin Cuibbb04322015-03-19 15:19:25 -0700275#if defined(__BIONIC__)
276 ASSERT_EQ(EINVAL, pthread_detach(t1));
277#else
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400278 ASSERT_EQ(0, pthread_detach(t1));
Yabin Cuibbb04322015-03-19 15:19:25 -0700279#endif
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400280 AssertDetached(t1, false);
281
Yabin Cui63481602014-12-01 17:41:04 -0800282 spinhelper.UnSpin();
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400283
284 // ...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 -0700285 void* join_result;
286 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700287 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700288}
Elliott Hughes14f19592012-10-29 10:19:44 -0700289
290TEST(pthread, pthread_join_self) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700291 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL));
Elliott Hughes14f19592012-10-29 10:19:44 -0700292}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700293
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800294struct TestBug37410 {
295 pthread_t main_thread;
296 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700297
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800298 static void main() {
299 TestBug37410 data;
300 data.main_thread = pthread_self();
301 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
302 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
303
304 pthread_t t;
305 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
306
307 // Wait for the thread to be running...
308 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
309 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
310
311 // ...and exit.
312 pthread_exit(NULL);
313 }
314
315 private:
316 static void* thread_fn(void* arg) {
317 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
318
319 // Let the main thread know we're running.
320 pthread_mutex_unlock(&data->mutex);
321
322 // And wait for the main thread to exit.
323 pthread_join(data->main_thread, NULL);
324
325 return NULL;
326 }
327};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700328
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800329// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
330// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800331
332class pthread_DeathTest : public BionicDeathTest {};
333
334TEST_F(pthread_DeathTest, pthread_bug_37410) {
Elliott Hughes4f251be2012-11-01 16:33:29 -0700335 // http://code.google.com/p/android/issues/detail?id=37410
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800336 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700337}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800338
339static void* SignalHandlerFn(void* arg) {
340 sigset_t wait_set;
341 sigfillset(&wait_set);
342 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
343}
344
345TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700346 // Check that SIGUSR1 isn't blocked.
347 sigset_t original_set;
348 sigemptyset(&original_set);
349 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
350 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
351
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800352 // Block SIGUSR1.
353 sigset_t set;
354 sigemptyset(&set);
355 sigaddset(&set, SIGUSR1);
356 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
357
Elliott Hughes19e62322013-10-15 11:23:57 -0700358 // Check that SIGUSR1 is blocked.
359 sigset_t final_set;
360 sigemptyset(&final_set);
361 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
362 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
363 // ...and that sigprocmask agrees with pthread_sigmask.
364 sigemptyset(&final_set);
365 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
366 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
367
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800368 // Spawn a thread that calls sigwait and tells us what it received.
369 pthread_t signal_thread;
370 int received_signal = -1;
371 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
372
373 // Send that thread SIGUSR1.
374 pthread_kill(signal_thread, SIGUSR1);
375
376 // See what it got.
377 void* join_result;
378 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
379 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700380 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700381
382 // Restore the original signal mask.
383 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800384}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800385
Elliott Hughes3e898472013-02-12 16:40:24 +0000386TEST(pthread, pthread_setname_np__too_long) {
387 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
388}
389
390TEST(pthread, pthread_setname_np__self) {
391 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
392}
393
394TEST(pthread, pthread_setname_np__other) {
Yabin Cui63481602014-12-01 17:41:04 -0800395 SpinFunctionHelper spinhelper;
396
Elliott Hughesed29e852014-10-27 12:01:51 -0700397 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800398 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughesed29e852014-10-27 12:01:51 -0700399 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000400}
401
Yabin Cui03324782015-03-24 17:43:14 -0700402TEST_F(pthread_DeathTest, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800403 pthread_t dead_thread;
404 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000405
406 // Call pthread_setname_np after thread has already exited.
Yabin Cui03324782015-03-24 17:43:14 -0700407#if defined(__BIONIC__)
408 ASSERT_EXIT(pthread_setname_np(dead_thread, "short 3"), testing::KilledBySignal(SIGABRT),
409 "No such thread");
410#else
Elliott Hughes68d98d82014-11-12 21:03:26 -0800411 ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
Yabin Cui03324782015-03-24 17:43:14 -0700412#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000413}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800414
415TEST(pthread, pthread_kill__0) {
416 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
417 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
418}
419
420TEST(pthread, pthread_kill__invalid_signal) {
421 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
422}
423
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800424static void pthread_kill__in_signal_handler_helper(int signal_number) {
425 static int count = 0;
426 ASSERT_EQ(SIGALRM, signal_number);
427 if (++count == 1) {
428 // Can we call pthread_kill from a signal handler?
429 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
430 }
431}
432
433TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800434 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800435 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
436}
437
Yabin Cui03324782015-03-24 17:43:14 -0700438TEST_F(pthread_DeathTest, pthread_detach__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800439 pthread_t dead_thread;
440 MakeDeadThread(dead_thread);
441
Yabin Cui03324782015-03-24 17:43:14 -0700442#if defined(__BIONIC__)
443 ASSERT_EXIT(pthread_detach(dead_thread), testing::KilledBySignal(SIGABRT), "No such thread");
444#else
Elliott Hughes9d23e042013-02-15 19:21:51 -0800445 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
Yabin Cui03324782015-03-24 17:43:14 -0700446#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800447}
448
Yabin Cui19e246d2014-12-18 14:22:09 -0800449TEST(pthread, pthread_detach_no_leak) {
Christopher Ferrise3809602014-08-06 14:15:01 -0700450 size_t initial_bytes = 0;
451 // Run this loop more than once since the first loop causes some memory
452 // to be allocated permenantly. Run an extra loop to help catch any subtle
453 // memory leaks.
454 for (size_t loop = 0; loop < 3; loop++) {
455 // Set the initial bytes on the second loop since the memory in use
456 // should have stabilized.
457 if (loop == 1) {
458 initial_bytes = mallinfo().uordblks;
459 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800460
Christopher Ferrise3809602014-08-06 14:15:01 -0700461 pthread_attr_t attr;
462 ASSERT_EQ(0, pthread_attr_init(&attr));
463 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
Elliott Hughes04620a32014-03-07 17:59:05 -0800464
Christopher Ferrise3809602014-08-06 14:15:01 -0700465 std::vector<pthread_t> threads;
466 for (size_t i = 0; i < 32; ++i) {
467 pthread_t t;
468 ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL));
469 threads.push_back(t);
470 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800471
Christopher Ferrise3809602014-08-06 14:15:01 -0700472 sleep(1);
Elliott Hughes04620a32014-03-07 17:59:05 -0800473
Christopher Ferrise3809602014-08-06 14:15:01 -0700474 for (size_t i = 0; i < 32; ++i) {
475 ASSERT_EQ(0, pthread_detach(threads[i])) << i;
476 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800477 }
478
479 size_t final_bytes = mallinfo().uordblks;
Elliott Hughes04620a32014-03-07 17:59:05 -0800480 int leaked_bytes = (final_bytes - initial_bytes);
481
Yabin Cui19e246d2014-12-18 14:22:09 -0800482 ASSERT_EQ(0, leaked_bytes);
Elliott Hughes04620a32014-03-07 17:59:05 -0800483}
484
Jeff Hao9b06cc32013-08-15 14:51:16 -0700485TEST(pthread, pthread_getcpuclockid__clock_gettime) {
Yabin Cui63481602014-12-01 17:41:04 -0800486 SpinFunctionHelper spinhelper;
487
Jeff Hao9b06cc32013-08-15 14:51:16 -0700488 pthread_t t;
Yabin Cui63481602014-12-01 17:41:04 -0800489 ASSERT_EQ(0, pthread_create(&t, NULL, spinhelper.GetFunction(), NULL));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700490
491 clockid_t c;
492 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
493 timespec ts;
494 ASSERT_EQ(0, clock_gettime(c, &ts));
495}
496
Yabin Cui03324782015-03-24 17:43:14 -0700497TEST_F(pthread_DeathTest, pthread_getcpuclockid__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800498 pthread_t dead_thread;
499 MakeDeadThread(dead_thread);
500
501 clockid_t c;
Yabin Cui03324782015-03-24 17:43:14 -0700502#if defined(__BIONIC__)
503 ASSERT_EXIT(pthread_getcpuclockid(dead_thread, &c), testing::KilledBySignal(SIGABRT),
504 "No such thread");
505#else
Elliott Hughes9d23e042013-02-15 19:21:51 -0800506 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
Yabin Cui03324782015-03-24 17:43:14 -0700507#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800508}
509
Yabin Cui03324782015-03-24 17:43:14 -0700510TEST_F(pthread_DeathTest, pthread_getschedparam__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800511 pthread_t dead_thread;
512 MakeDeadThread(dead_thread);
513
514 int policy;
515 sched_param param;
Yabin Cui03324782015-03-24 17:43:14 -0700516#if defined(__BIONIC__)
517 ASSERT_EXIT(pthread_getschedparam(dead_thread, &policy, &param), testing::KilledBySignal(SIGABRT),
518 "No such thread");
519#else
Elliott Hughes9d23e042013-02-15 19:21:51 -0800520 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
Yabin Cui03324782015-03-24 17:43:14 -0700521#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800522}
523
Yabin Cui03324782015-03-24 17:43:14 -0700524TEST_F(pthread_DeathTest, pthread_setschedparam__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800525 pthread_t dead_thread;
526 MakeDeadThread(dead_thread);
527
528 int policy = 0;
529 sched_param param;
Yabin Cui03324782015-03-24 17:43:14 -0700530#if defined(__BIONIC__)
531 ASSERT_EXIT(pthread_setschedparam(dead_thread, policy, &param), testing::KilledBySignal(SIGABRT),
532 "No such thread");
533#else
Elliott Hughes9d23e042013-02-15 19:21:51 -0800534 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
Yabin Cui03324782015-03-24 17:43:14 -0700535#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800536}
537
Yabin Cui03324782015-03-24 17:43:14 -0700538TEST_F(pthread_DeathTest, pthread_join__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800539 pthread_t dead_thread;
540 MakeDeadThread(dead_thread);
541
Yabin Cui03324782015-03-24 17:43:14 -0700542#if defined(__BIONIC__)
543 ASSERT_EXIT(pthread_join(dead_thread, NULL), testing::KilledBySignal(SIGABRT), "No such thread");
544#else
Elliott Hughes34c987a2014-09-22 16:01:26 -0700545 ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL));
Yabin Cui03324782015-03-24 17:43:14 -0700546#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800547}
548
Yabin Cui03324782015-03-24 17:43:14 -0700549TEST_F(pthread_DeathTest, pthread_kill__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800550 pthread_t dead_thread;
551 MakeDeadThread(dead_thread);
552
Yabin Cui03324782015-03-24 17:43:14 -0700553#if defined(__BIONIC__)
554 ASSERT_EXIT(pthread_kill(dead_thread, 0), testing::KilledBySignal(SIGABRT), "No such thread");
555#else
Elliott Hughes9d23e042013-02-15 19:21:51 -0800556 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
Yabin Cui03324782015-03-24 17:43:14 -0700557#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800558}
msg5550f020d12013-06-06 14:59:28 -0400559
560TEST(pthread, pthread_join__multijoin) {
Yabin Cui63481602014-12-01 17:41:04 -0800561 SpinFunctionHelper spinhelper;
msg5550f020d12013-06-06 14:59:28 -0400562
563 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800564 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
msg5550f020d12013-06-06 14:59:28 -0400565
566 pthread_t t2;
567 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
568
569 sleep(1); // (Give t2 a chance to call pthread_join.)
570
571 // Multiple joins to the same thread should fail.
572 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
573
Yabin Cui63481602014-12-01 17:41:04 -0800574 spinhelper.UnSpin();
msg5550f020d12013-06-06 14:59:28 -0400575
576 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
577 void* join_result;
578 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700579 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400580}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700581
Elliott Hughes70b24b12013-11-15 11:51:07 -0800582TEST(pthread, pthread_join__race) {
583 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
584 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
585 for (size_t i = 0; i < 1024; ++i) {
586 size_t stack_size = 64*1024;
587 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
588
589 pthread_attr_t a;
590 pthread_attr_init(&a);
591 pthread_attr_setstack(&a, stack, stack_size);
592
593 pthread_t t;
594 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
595 ASSERT_EQ(0, pthread_join(t, NULL));
596 ASSERT_EQ(0, munmap(stack, stack_size));
597 }
598}
599
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700600static void* GetActualGuardSizeFn(void* arg) {
601 pthread_attr_t attributes;
602 pthread_getattr_np(pthread_self(), &attributes);
603 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
604 return NULL;
605}
606
607static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
608 size_t result;
609 pthread_t t;
610 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700611 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700612 return result;
613}
614
615static void* GetActualStackSizeFn(void* arg) {
616 pthread_attr_t attributes;
617 pthread_getattr_np(pthread_self(), &attributes);
618 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
619 return NULL;
620}
621
622static size_t GetActualStackSize(const pthread_attr_t& attributes) {
623 size_t result;
624 pthread_t t;
625 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700626 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700627 return result;
628}
629
630TEST(pthread, pthread_attr_setguardsize) {
631 pthread_attr_t attributes;
632 ASSERT_EQ(0, pthread_attr_init(&attributes));
633
634 // Get the default guard size.
635 size_t default_guard_size;
636 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
637
638 // No such thing as too small: will be rounded up to one page by pthread_create.
639 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
640 size_t guard_size;
641 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
642 ASSERT_EQ(128U, guard_size);
643 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
644
645 // Large enough and a multiple of the page size.
646 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
647 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
648 ASSERT_EQ(32*1024U, guard_size);
649
650 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
651 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
652 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
653 ASSERT_EQ(32*1024U + 1, guard_size);
654}
655
656TEST(pthread, pthread_attr_setstacksize) {
657 pthread_attr_t attributes;
658 ASSERT_EQ(0, pthread_attr_init(&attributes));
659
660 // Get the default stack size.
661 size_t default_stack_size;
662 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
663
664 // Too small.
665 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
666 size_t stack_size;
667 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
668 ASSERT_EQ(default_stack_size, stack_size);
669 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
670
Yabin Cui917d3902015-01-08 12:32:42 -0800671 // Large enough and a multiple of the page size; may be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700672 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
673 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
674 ASSERT_EQ(32*1024U, stack_size);
Yabin Cui917d3902015-01-08 12:32:42 -0800675 ASSERT_GE(GetActualStackSize(attributes), 32*1024U);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700676
Yabin Cui917d3902015-01-08 12:32:42 -0800677 // Large enough but not aligned; will be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700678 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
679 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
680 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800681#if defined(__BIONIC__)
Yabin Cui917d3902015-01-08 12:32:42 -0800682 ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800683#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700684 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
685 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800686#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700687}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700688
689TEST(pthread, pthread_rwlock_smoke) {
690 pthread_rwlock_t l;
691 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
692
Calin Juravle76f352e2014-05-19 13:41:10 +0100693 // Single read lock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700694 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
695 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
696
Calin Juravle76f352e2014-05-19 13:41:10 +0100697 // Multiple read lock
698 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
699 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
700 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
701 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
702
703 // Write lock
Calin Juravle92687e42014-05-22 19:21:22 +0100704 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
705 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100706
707 // Try writer lock
708 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
709 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
710 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
711 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
712
713 // Try reader lock
714 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
715 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
716 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
717 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
718 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
719
720 // Try writer lock after unlock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700721 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
722 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
723
Calin Juravle76f352e2014-05-19 13:41:10 +0100724#ifdef __BIONIC__
725 // EDEADLK in "read after write"
726 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
727 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
728 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
729
730 // EDEADLK in "write after write"
731 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
732 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
733 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
734#endif
735
Elliott Hughesc3f11402013-10-30 14:40:09 -0700736 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
737}
738
Yabin Cui08ee8d22015-02-11 17:04:36 -0800739struct RwlockWakeupHelperArg {
740 pthread_rwlock_t lock;
741 enum Progress {
742 LOCK_INITIALIZED,
743 LOCK_WAITING,
744 LOCK_RELEASED,
745 LOCK_ACCESSED
746 };
747 std::atomic<Progress> progress;
748};
749
750static void pthread_rwlock_reader_wakeup_writer_helper(RwlockWakeupHelperArg* arg) {
751 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
752 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
753
754 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&arg->lock));
755 ASSERT_EQ(0, pthread_rwlock_wrlock(&arg->lock));
756 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
762TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
763 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;
767
768 pthread_t thread;
769 ASSERT_EQ(0, pthread_create(&thread, NULL,
770 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_reader_wakeup_writer_helper), &wakeup_arg));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800771 while (wakeup_arg.progress != RwlockWakeupHelperArg::LOCK_WAITING) {
772 usleep(5000);
773 }
774 usleep(5000);
Yabin Cui08ee8d22015-02-11 17:04:36 -0800775 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
776 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
777
778 ASSERT_EQ(0, pthread_join(thread, NULL));
779 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
780 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
781}
782
783static void pthread_rwlock_writer_wakeup_reader_helper(RwlockWakeupHelperArg* arg) {
784 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
785 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
786
787 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&arg->lock));
788 ASSERT_EQ(0, pthread_rwlock_rdlock(&arg->lock));
789 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
790 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
791
792 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
793}
794
795TEST(pthread, pthread_rwlock_writer_wakeup_reader) {
796 RwlockWakeupHelperArg wakeup_arg;
797 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
798 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
799 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
800
801 pthread_t thread;
802 ASSERT_EQ(0, pthread_create(&thread, NULL,
803 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_writer_wakeup_reader_helper), &wakeup_arg));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800804 while (wakeup_arg.progress != RwlockWakeupHelperArg::LOCK_WAITING) {
805 usleep(5000);
806 }
807 usleep(5000);
Yabin Cui08ee8d22015-02-11 17:04:36 -0800808 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
809 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
810
811 ASSERT_EQ(0, pthread_join(thread, NULL));
812 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
813 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
814}
815
Elliott Hughes1728b232014-05-14 10:02:03 -0700816static int g_once_fn_call_count = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700817static void OnceFn() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700818 ++g_once_fn_call_count;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700819}
820
821TEST(pthread, pthread_once_smoke) {
822 pthread_once_t once_control = PTHREAD_ONCE_INIT;
823 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
824 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
Elliott Hughes1728b232014-05-14 10:02:03 -0700825 ASSERT_EQ(1, g_once_fn_call_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700826}
827
Elliott Hughes3694ec62014-05-14 11:46:08 -0700828static std::string pthread_once_1934122_result = "";
829
830static void Routine2() {
831 pthread_once_1934122_result += "2";
832}
833
834static void Routine1() {
835 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
836 pthread_once_1934122_result += "1";
837 pthread_once(&once_control_2, &Routine2);
838}
839
840TEST(pthread, pthread_once_1934122) {
841 // Very old versions of Android couldn't call pthread_once from a
842 // pthread_once init routine. http://b/1934122.
843 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
844 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
845 ASSERT_EQ("12", pthread_once_1934122_result);
846}
847
Elliott Hughes1728b232014-05-14 10:02:03 -0700848static int g_atfork_prepare_calls = 0;
849static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; }
850static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; }
851static int g_atfork_parent_calls = 0;
852static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; }
853static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; }
854static int g_atfork_child_calls = 0;
855static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; }
856static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700857
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800858TEST(pthread, pthread_atfork_smoke) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700859 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
860 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700861
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700862 int pid = fork();
863 ASSERT_NE(-1, pid) << strerror(errno);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700864
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700865 // Child and parent calls are made in the order they were registered.
866 if (pid == 0) {
867 ASSERT_EQ(0x12, g_atfork_child_calls);
868 _exit(0);
869 }
870 ASSERT_EQ(0x12, g_atfork_parent_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700871
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700872 // Prepare calls are made in the reverse order.
873 ASSERT_EQ(0x21, g_atfork_prepare_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700874}
875
876TEST(pthread, pthread_attr_getscope) {
877 pthread_attr_t attr;
878 ASSERT_EQ(0, pthread_attr_init(&attr));
879
880 int scope;
881 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
882 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
883}
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000884
885TEST(pthread, pthread_condattr_init) {
886 pthread_condattr_t attr;
887 pthread_condattr_init(&attr);
888
889 clockid_t clock;
890 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
891 ASSERT_EQ(CLOCK_REALTIME, clock);
892
893 int pshared;
894 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
895 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
896}
897
898TEST(pthread, pthread_condattr_setclock) {
899 pthread_condattr_t attr;
900 pthread_condattr_init(&attr);
901
902 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
903 clockid_t clock;
904 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
905 ASSERT_EQ(CLOCK_REALTIME, clock);
906
907 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
908 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
909 ASSERT_EQ(CLOCK_MONOTONIC, clock);
910
911 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
912}
913
914TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
Yabin Cui32651b82015-03-13 20:30:00 -0700915#if defined(__BIONIC__)
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000916 pthread_condattr_t attr;
917 pthread_condattr_init(&attr);
918
919 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
920 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
921
922 pthread_cond_t cond_var;
923 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
924
925 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
926 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
927
Yabin Cui32651b82015-03-13 20:30:00 -0700928 attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000929 clockid_t clock;
930 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
931 ASSERT_EQ(CLOCK_MONOTONIC, clock);
932 int pshared;
933 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
934 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
Yabin Cui32651b82015-03-13 20:30:00 -0700935#else // !defined(__BIONIC__)
936 GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n";
937#endif // !defined(__BIONIC__)
938}
939
940class pthread_CondWakeupTest : public ::testing::Test {
941 protected:
942 pthread_mutex_t mutex;
943 pthread_cond_t cond;
944
945 enum Progress {
946 INITIALIZED,
947 WAITING,
948 SIGNALED,
949 FINISHED,
950 };
951 std::atomic<Progress> progress;
952 pthread_t thread;
953
954 protected:
955 virtual void SetUp() {
956 ASSERT_EQ(0, pthread_mutex_init(&mutex, NULL));
957 ASSERT_EQ(0, pthread_cond_init(&cond, NULL));
958 progress = INITIALIZED;
959 ASSERT_EQ(0,
960 pthread_create(&thread, NULL, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this));
961 }
962
963 virtual void TearDown() {
964 ASSERT_EQ(0, pthread_join(thread, NULL));
965 ASSERT_EQ(FINISHED, progress);
966 ASSERT_EQ(0, pthread_cond_destroy(&cond));
967 ASSERT_EQ(0, pthread_mutex_destroy(&mutex));
968 }
969
970 void SleepUntilProgress(Progress expected_progress) {
971 while (progress != expected_progress) {
972 usleep(5000);
973 }
974 usleep(5000);
975 }
976
977 private:
978 static void WaitThreadFn(pthread_CondWakeupTest* test) {
979 ASSERT_EQ(0, pthread_mutex_lock(&test->mutex));
980 test->progress = WAITING;
981 while (test->progress == WAITING) {
982 ASSERT_EQ(0, pthread_cond_wait(&test->cond, &test->mutex));
983 }
984 ASSERT_EQ(SIGNALED, test->progress);
985 test->progress = FINISHED;
986 ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex));
987 }
988};
989
990TEST_F(pthread_CondWakeupTest, signal) {
991 SleepUntilProgress(WAITING);
992 progress = SIGNALED;
993 pthread_cond_signal(&cond);
994}
995
996TEST_F(pthread_CondWakeupTest, broadcast) {
997 SleepUntilProgress(WAITING);
998 progress = SIGNALED;
999 pthread_cond_broadcast(&cond);
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001000}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001001
1002TEST(pthread, pthread_mutex_timedlock) {
1003 pthread_mutex_t m;
1004 ASSERT_EQ(0, pthread_mutex_init(&m, NULL));
1005
1006 // If the mutex is already locked, pthread_mutex_timedlock should time out.
1007 ASSERT_EQ(0, pthread_mutex_lock(&m));
1008
1009 timespec ts;
1010 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1011 ts.tv_nsec += 1;
1012 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
1013
1014 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
1015 ASSERT_EQ(0, pthread_mutex_unlock(&m));
1016
1017 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1018 ts.tv_nsec += 1;
1019 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
1020
1021 ASSERT_EQ(0, pthread_mutex_unlock(&m));
1022 ASSERT_EQ(0, pthread_mutex_destroy(&m));
1023}
Elliott Hughes57b7a612014-08-25 17:26:50 -07001024
1025TEST(pthread, pthread_attr_getstack__main_thread) {
1026 // This test is only meaningful for the main thread, so make sure we're running on it!
1027 ASSERT_EQ(getpid(), syscall(__NR_gettid));
1028
1029 // Get the main thread's attributes.
1030 pthread_attr_t attributes;
1031 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1032
1033 // Check that we correctly report that the main thread has no guard page.
1034 size_t guard_size;
1035 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
1036 ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
1037
1038 // Get the stack base and the stack size (both ways).
1039 void* stack_base;
1040 size_t stack_size;
1041 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1042 size_t stack_size2;
1043 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1044
1045 // The two methods of asking for the stack size should agree.
1046 EXPECT_EQ(stack_size, stack_size2);
1047
1048 // What does /proc/self/maps' [stack] line say?
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001049 void* maps_stack_hi = NULL;
Elliott Hughes57b7a612014-08-25 17:26:50 -07001050 FILE* fp = fopen("/proc/self/maps", "r");
1051 ASSERT_TRUE(fp != NULL);
1052 char line[BUFSIZ];
1053 while (fgets(line, sizeof(line), fp) != NULL) {
1054 uintptr_t lo, hi;
1055 char name[10];
1056 sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name);
1057 if (strcmp(name, "[stack]") == 0) {
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001058 maps_stack_hi = reinterpret_cast<void*>(hi);
Elliott Hughes57b7a612014-08-25 17:26:50 -07001059 break;
1060 }
1061 }
1062 fclose(fp);
1063
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001064 // The stack size should correspond to RLIMIT_STACK.
Elliott Hughes57b7a612014-08-25 17:26:50 -07001065 rlimit rl;
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001066 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001067 uint64_t original_rlim_cur = rl.rlim_cur;
1068#if defined(__BIONIC__)
1069 if (rl.rlim_cur == RLIM_INFINITY) {
1070 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
1071 }
1072#endif
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001073 EXPECT_EQ(rl.rlim_cur, stack_size);
1074
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001075 auto guard = make_scope_guard([&rl, original_rlim_cur]() {
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001076 rl.rlim_cur = original_rlim_cur;
1077 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1078 });
1079
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001080 // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
1081 // Remember that the stack grows down (and is mapped in on demand), so the low address of the
1082 // region isn't very interesting.
1083 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
1084
1085 //
1086 // What if RLIMIT_STACK is smaller than the stack's current extent?
1087 //
Elliott Hughes57b7a612014-08-25 17:26:50 -07001088 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
1089 rl.rlim_max = RLIM_INFINITY;
1090 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1091
1092 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1093 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1094 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1095
1096 EXPECT_EQ(stack_size, stack_size2);
1097 ASSERT_EQ(1024U, stack_size);
1098
1099 //
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001100 // What if RLIMIT_STACK isn't a whole number of pages?
Elliott Hughes57b7a612014-08-25 17:26:50 -07001101 //
1102 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
1103 rl.rlim_max = RLIM_INFINITY;
1104 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1105
1106 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1107 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1108 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1109
1110 EXPECT_EQ(stack_size, stack_size2);
1111 ASSERT_EQ(6666U, stack_size);
1112}
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001113
Yabin Cui917d3902015-01-08 12:32:42 -08001114static void pthread_attr_getstack_18908062_helper(void*) {
1115 char local_variable;
1116 pthread_attr_t attributes;
1117 pthread_getattr_np(pthread_self(), &attributes);
1118 void* stack_base;
1119 size_t stack_size;
1120 pthread_attr_getstack(&attributes, &stack_base, &stack_size);
1121
1122 // Test whether &local_variable is in [stack_base, stack_base + stack_size).
1123 ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable);
1124 ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size);
1125}
1126
1127// Check whether something on stack is in the range of
1128// [stack_base, stack_base + stack_size). see b/18908062.
1129TEST(pthread, pthread_attr_getstack_18908062) {
1130 pthread_t t;
1131 ASSERT_EQ(0, pthread_create(&t, NULL,
1132 reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper),
1133 NULL));
1134 pthread_join(t, NULL);
1135}
1136
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001137#if defined(__BIONIC__)
1138static void* pthread_gettid_np_helper(void* arg) {
1139 *reinterpret_cast<pid_t*>(arg) = gettid();
1140 return NULL;
1141}
1142#endif
1143
1144TEST(pthread, pthread_gettid_np) {
1145#if defined(__BIONIC__)
1146 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
1147
1148 pid_t t_gettid_result;
1149 pthread_t t;
1150 pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
1151
1152 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
1153
Elliott Hughes34c987a2014-09-22 16:01:26 -07001154 pthread_join(t, NULL);
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001155
1156 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
1157#else
1158 GTEST_LOG_(INFO) << "This test does nothing.\n";
1159#endif
1160}
Elliott Hughes34c987a2014-09-22 16:01:26 -07001161
1162static size_t cleanup_counter = 0;
1163
Derek Xue41996952014-09-25 11:05:32 +01001164static void AbortCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001165 abort();
1166}
1167
Derek Xue41996952014-09-25 11:05:32 +01001168static void CountCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001169 ++cleanup_counter;
1170}
1171
Derek Xue41996952014-09-25 11:05:32 +01001172static void PthreadCleanupTester() {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001173 pthread_cleanup_push(CountCleanupRoutine, NULL);
1174 pthread_cleanup_push(CountCleanupRoutine, NULL);
1175 pthread_cleanup_push(AbortCleanupRoutine, NULL);
1176
1177 pthread_cleanup_pop(0); // Pop the abort without executing it.
1178 pthread_cleanup_pop(1); // Pop one count while executing it.
1179 ASSERT_EQ(1U, cleanup_counter);
1180 // Exit while the other count is still on the cleanup stack.
1181 pthread_exit(NULL);
1182
1183 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
1184 pthread_cleanup_pop(0);
1185}
1186
Derek Xue41996952014-09-25 11:05:32 +01001187static void* PthreadCleanupStartRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001188 PthreadCleanupTester();
1189 return NULL;
1190}
1191
1192TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
1193 pthread_t t;
1194 ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL));
1195 pthread_join(t, NULL);
1196 ASSERT_EQ(2U, cleanup_counter);
1197}
Derek Xue41996952014-09-25 11:05:32 +01001198
1199TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
1200 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
1201}
1202
1203TEST(pthread, pthread_mutexattr_gettype) {
1204 pthread_mutexattr_t attr;
1205 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1206
1207 int attr_type;
1208
1209 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1210 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1211 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
1212
1213 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1214 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1215 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
1216
1217 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1218 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1219 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001220
1221 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1222}
1223
1224static void CreateMutex(pthread_mutex_t& mutex, int mutex_type) {
1225 pthread_mutexattr_t attr;
1226 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1227 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, mutex_type));
1228 ASSERT_EQ(0, pthread_mutex_init(&mutex, &attr));
1229 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
Derek Xue41996952014-09-25 11:05:32 +01001230}
1231
1232TEST(pthread, pthread_mutex_lock_NORMAL) {
Derek Xue41996952014-09-25 11:05:32 +01001233 pthread_mutex_t lock;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001234 CreateMutex(lock, PTHREAD_MUTEX_NORMAL);
Derek Xue41996952014-09-25 11:05:32 +01001235
1236 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1237 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1238 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1239}
1240
1241TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
Derek Xue41996952014-09-25 11:05:32 +01001242 pthread_mutex_t lock;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001243 CreateMutex(lock, PTHREAD_MUTEX_ERRORCHECK);
Derek Xue41996952014-09-25 11:05:32 +01001244
1245 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1246 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&lock));
1247 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1248 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1249 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock));
1250 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1251 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1252 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1253}
1254
1255TEST(pthread, pthread_mutex_lock_RECURSIVE) {
Derek Xue41996952014-09-25 11:05:32 +01001256 pthread_mutex_t lock;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001257 CreateMutex(lock, PTHREAD_MUTEX_RECURSIVE);
Derek Xue41996952014-09-25 11:05:32 +01001258
1259 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1260 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1261 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1262 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1263 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1264 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1265 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1266 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1267}
Yabin Cui140f3672015-02-03 10:32:00 -08001268
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001269class MutexWakeupHelper {
1270 private:
1271 pthread_mutex_t mutex;
1272 enum Progress {
1273 LOCK_INITIALIZED,
1274 LOCK_WAITING,
1275 LOCK_RELEASED,
1276 LOCK_ACCESSED
1277 };
1278 std::atomic<Progress> progress;
1279
1280 static void thread_fn(MutexWakeupHelper* helper) {
1281 ASSERT_EQ(LOCK_INITIALIZED, helper->progress);
1282 helper->progress = LOCK_WAITING;
1283
1284 ASSERT_EQ(0, pthread_mutex_lock(&helper->mutex));
1285 ASSERT_EQ(LOCK_RELEASED, helper->progress);
1286 ASSERT_EQ(0, pthread_mutex_unlock(&helper->mutex));
1287
1288 helper->progress = LOCK_ACCESSED;
1289 }
1290
1291 public:
1292 void test(int mutex_type) {
1293 CreateMutex(mutex, mutex_type);
1294 ASSERT_EQ(0, pthread_mutex_lock(&mutex));
1295 progress = LOCK_INITIALIZED;
1296
1297 pthread_t thread;
1298 ASSERT_EQ(0, pthread_create(&thread, NULL,
1299 reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this));
1300
1301 while (progress != LOCK_WAITING) {
1302 usleep(5000);
1303 }
1304 usleep(5000);
1305 progress = LOCK_RELEASED;
1306 ASSERT_EQ(0, pthread_mutex_unlock(&mutex));
1307
1308 ASSERT_EQ(0, pthread_join(thread, NULL));
1309 ASSERT_EQ(LOCK_ACCESSED, progress);
1310 ASSERT_EQ(0, pthread_mutex_destroy(&mutex));
1311 }
1312};
1313
1314TEST(pthread, pthread_mutex_NORMAL_wakeup) {
1315 MutexWakeupHelper helper;
1316 helper.test(PTHREAD_MUTEX_NORMAL);
1317}
1318
1319TEST(pthread, pthread_mutex_ERRORCHECK_wakeup) {
1320 MutexWakeupHelper helper;
1321 helper.test(PTHREAD_MUTEX_ERRORCHECK);
1322}
1323
1324TEST(pthread, pthread_mutex_RECURSIVE_wakeup) {
1325 MutexWakeupHelper helper;
1326 helper.test(PTHREAD_MUTEX_RECURSIVE);
1327}
1328
Yabin Cui140f3672015-02-03 10:32:00 -08001329TEST(pthread, pthread_mutex_owner_tid_limit) {
1330 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
1331 ASSERT_TRUE(fp != NULL);
1332 long pid_max;
1333 ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max));
1334 fclose(fp);
1335 // Current pthread_mutex uses 16 bits to represent owner tid.
1336 // Change the implementation if we need to support higher value than 65535.
1337 ASSERT_LE(pid_max, 65536);
1338}
Yabin Cuib5845722015-03-16 22:46:42 -07001339
1340class StrictAlignmentAllocator {
1341 public:
1342 void* allocate(size_t size, size_t alignment) {
1343 char* p = new char[size + alignment * 2];
1344 allocated_array.push_back(p);
1345 while (!is_strict_aligned(p, alignment)) {
1346 ++p;
1347 }
1348 return p;
1349 }
1350
1351 ~StrictAlignmentAllocator() {
1352 for (auto& p : allocated_array) {
1353 delete [] p;
1354 }
1355 }
1356
1357 private:
1358 bool is_strict_aligned(char* p, size_t alignment) {
1359 return (reinterpret_cast<uintptr_t>(p) % (alignment * 2)) == alignment;
1360 }
1361
1362 std::vector<char*> allocated_array;
1363};
1364
1365TEST(pthread, pthread_types_allow_four_bytes_alignment) {
1366#if defined(__BIONIC__)
1367 // For binary compatibility with old version, we need to allow 4-byte aligned data for pthread types.
1368 StrictAlignmentAllocator allocator;
1369 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(
1370 allocator.allocate(sizeof(pthread_mutex_t), 4));
1371 ASSERT_EQ(0, pthread_mutex_init(mutex, NULL));
1372 ASSERT_EQ(0, pthread_mutex_lock(mutex));
1373 ASSERT_EQ(0, pthread_mutex_unlock(mutex));
1374 ASSERT_EQ(0, pthread_mutex_destroy(mutex));
1375
1376 pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>(
1377 allocator.allocate(sizeof(pthread_cond_t), 4));
1378 ASSERT_EQ(0, pthread_cond_init(cond, NULL));
1379 ASSERT_EQ(0, pthread_cond_signal(cond));
1380 ASSERT_EQ(0, pthread_cond_broadcast(cond));
1381 ASSERT_EQ(0, pthread_cond_destroy(cond));
1382
1383 pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>(
1384 allocator.allocate(sizeof(pthread_rwlock_t), 4));
1385 ASSERT_EQ(0, pthread_rwlock_init(rwlock, NULL));
1386 ASSERT_EQ(0, pthread_rwlock_rdlock(rwlock));
1387 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
1388 ASSERT_EQ(0, pthread_rwlock_wrlock(rwlock));
1389 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
1390 ASSERT_EQ(0, pthread_rwlock_destroy(rwlock));
1391
1392#else
1393 GTEST_LOG_(INFO) << "This test tests bionic implementation details.";
1394#endif
1395}