blob: 632e28552032c1e7002a2034ed87b727b2f7b176 [file] [log] [blame]
Elliott Hughesbfeab1b2012-09-05 17:47:37 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <errno.h>
Elliott Hughes5b9310e2013-10-02 16:59:05 -070020#include <inttypes.h>
Elliott Hughesb95cf0d2013-07-15 14:51:07 -070021#include <limits.h>
Elliott Hughes04620a32014-03-07 17:59:05 -080022#include <malloc.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070023#include <pthread.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080024#include <signal.h>
Yabin Cui140f3672015-02-03 10:32:00 -080025#include <stdio.h>
Elliott Hughes70b24b12013-11-15 11:51:07 -080026#include <sys/mman.h>
Elliott Hughes57b7a612014-08-25 17:26:50 -070027#include <sys/syscall.h>
Narayan Kamath51e6cb32014-03-03 15:38:51 +000028#include <time.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070029#include <unistd.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070030
Yabin Cui08ee8d22015-02-11 17:04:36 -080031#include <atomic>
Yabin Cuib5845722015-03-16 22:46:42 -070032#include <vector>
Yabin Cui08ee8d22015-02-11 17:04:36 -080033
Yabin Cui17393b02015-03-21 15:08:25 -070034#include "private/bionic_macros.h"
35#include "private/ScopeGuard.h"
36#include "BionicDeathTest.h"
37#include "ScopedSignalHandler.h"
38
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070039TEST(pthread, pthread_key_create) {
40 pthread_key_t key;
41 ASSERT_EQ(0, pthread_key_create(&key, NULL));
42 ASSERT_EQ(0, pthread_key_delete(key));
43 // Can't delete a key that's already been deleted.
44 ASSERT_EQ(EINVAL, pthread_key_delete(key));
45}
Elliott Hughes4d014e12012-09-07 16:47:54 -070046
Dan Albertc4bcc752014-09-30 11:48:24 -070047TEST(pthread, pthread_keys_max) {
Yabin Cui6c238f22014-12-11 20:50:41 -080048 // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX.
49 ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070050}
Elliott Hughes718a5b52014-01-28 17:02:03 -080051
Yabin Cui6c238f22014-12-11 20:50:41 -080052TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) {
Dan Albertc4bcc752014-09-30 11:48:24 -070053 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
Yabin Cui6c238f22014-12-11 20:50:41 -080054 ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070055}
56
57TEST(pthread, pthread_key_many_distinct) {
Yabin Cui6c238f22014-12-11 20:50:41 -080058 // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX
59 // pthread keys, but We should be able to allocate at least this many keys.
60 int nkeys = PTHREAD_KEYS_MAX / 2;
Dan Albertc4bcc752014-09-30 11:48:24 -070061 std::vector<pthread_key_t> keys;
62
63 auto scope_guard = make_scope_guard([&keys]{
64 for (auto key : keys) {
65 EXPECT_EQ(0, pthread_key_delete(key));
66 }
67 });
68
69 for (int i = 0; i < nkeys; ++i) {
70 pthread_key_t key;
71 // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is
72 // wrong.
73 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys;
74 keys.push_back(key);
75 ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i)));
76 }
77
78 for (int i = keys.size() - 1; i >= 0; --i) {
79 ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back()));
80 pthread_key_t key = keys.back();
81 keys.pop_back();
82 ASSERT_EQ(0, pthread_key_delete(key));
83 }
84}
85
Yabin Cui6c238f22014-12-11 20:50:41 -080086TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000087 std::vector<pthread_key_t> keys;
Dan Albertc4bcc752014-09-30 11:48:24 -070088 int rv = 0;
Yabin Cui6c238f22014-12-11 20:50:41 -080089
90 // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should
91 // be more than we are allowed to allocate now.
92 for (int i = 0; i < PTHREAD_KEYS_MAX; i++) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000093 pthread_key_t key;
Dan Albertc4bcc752014-09-30 11:48:24 -070094 rv = pthread_key_create(&key, NULL);
95 if (rv == EAGAIN) {
96 break;
97 }
98 EXPECT_EQ(0, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +000099 keys.push_back(key);
100 }
101
Dan Albertc4bcc752014-09-30 11:48:24 -0700102 // Don't leak keys.
103 for (auto key : keys) {
104 EXPECT_EQ(0, pthread_key_delete(key));
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000105 }
Dan Albertc4bcc752014-09-30 11:48:24 -0700106 keys.clear();
107
108 // We should have eventually reached the maximum number of keys and received
109 // EAGAIN.
110 ASSERT_EQ(EAGAIN, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000111}
112
Elliott Hughesebb770f2014-06-25 13:46:46 -0700113TEST(pthread, pthread_key_delete) {
114 void* expected = reinterpret_cast<void*>(1234);
115 pthread_key_t key;
116 ASSERT_EQ(0, pthread_key_create(&key, NULL));
117 ASSERT_EQ(0, pthread_setspecific(key, expected));
118 ASSERT_EQ(expected, pthread_getspecific(key));
119 ASSERT_EQ(0, pthread_key_delete(key));
120 // After deletion, pthread_getspecific returns NULL.
121 ASSERT_EQ(NULL, pthread_getspecific(key));
122 // And you can't use pthread_setspecific with the deleted key.
123 ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
124}
125
Elliott Hughes40a52172014-07-30 14:48:10 -0700126TEST(pthread, pthread_key_fork) {
127 void* expected = reinterpret_cast<void*>(1234);
128 pthread_key_t key;
129 ASSERT_EQ(0, pthread_key_create(&key, NULL));
130 ASSERT_EQ(0, pthread_setspecific(key, expected));
131 ASSERT_EQ(expected, pthread_getspecific(key));
132
133 pid_t pid = fork();
134 ASSERT_NE(-1, pid) << strerror(errno);
135
136 if (pid == 0) {
137 // The surviving thread inherits all the forking thread's TLS values...
138 ASSERT_EQ(expected, pthread_getspecific(key));
139 _exit(99);
140 }
141
142 int status;
143 ASSERT_EQ(pid, waitpid(pid, &status, 0));
144 ASSERT_TRUE(WIFEXITED(status));
145 ASSERT_EQ(99, WEXITSTATUS(status));
146
147 ASSERT_EQ(expected, pthread_getspecific(key));
Dan Albert1d53ae22014-09-02 15:24:26 -0700148 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700149}
150
151static void* DirtyKeyFn(void* key) {
152 return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
153}
154
155TEST(pthread, pthread_key_dirty) {
156 pthread_key_t key;
157 ASSERT_EQ(0, pthread_key_create(&key, NULL));
158
159 size_t stack_size = 128 * 1024;
160 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
161 ASSERT_NE(MAP_FAILED, stack);
162 memset(stack, 0xff, stack_size);
163
164 pthread_attr_t attr;
165 ASSERT_EQ(0, pthread_attr_init(&attr));
166 ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
167
168 pthread_t t;
169 ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
170
171 void* result;
172 ASSERT_EQ(0, pthread_join(t, &result));
173 ASSERT_EQ(nullptr, result); // Not ~0!
174
175 ASSERT_EQ(0, munmap(stack, stack_size));
Dan Albert1d53ae22014-09-02 15:24:26 -0700176 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700177}
178
Elliott Hughes4d014e12012-09-07 16:47:54 -0700179static void* IdFn(void* arg) {
180 return arg;
181}
182
Yabin Cui63481602014-12-01 17:41:04 -0800183class SpinFunctionHelper {
184 public:
185 SpinFunctionHelper() {
186 SpinFunctionHelper::spin_flag_ = true;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400187 }
Yabin Cui63481602014-12-01 17:41:04 -0800188 ~SpinFunctionHelper() {
189 UnSpin();
190 }
191 auto GetFunction() -> void* (*)(void*) {
192 return SpinFunctionHelper::SpinFn;
193 }
194
195 void UnSpin() {
196 SpinFunctionHelper::spin_flag_ = false;
197 }
198
199 private:
200 static void* SpinFn(void*) {
201 while (spin_flag_) {}
202 return NULL;
203 }
204 static volatile bool spin_flag_;
205};
206
207// It doesn't matter if spin_flag_ is used in several tests,
208// because it is always set to false after each test. Each thread
209// loops on spin_flag_ can find it becomes false at some time.
210volatile bool SpinFunctionHelper::spin_flag_ = false;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400211
Elliott Hughes4d014e12012-09-07 16:47:54 -0700212static void* JoinFn(void* arg) {
213 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
214}
215
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400216static void AssertDetached(pthread_t t, bool is_detached) {
217 pthread_attr_t attr;
218 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
219 int detach_state;
220 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
221 pthread_attr_destroy(&attr);
222 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
223}
224
Elliott Hughes9d23e042013-02-15 19:21:51 -0800225static void MakeDeadThread(pthread_t& t) {
226 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
Elliott Hughes34c987a2014-09-22 16:01:26 -0700227 ASSERT_EQ(0, pthread_join(t, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800228}
229
Elliott Hughes4d014e12012-09-07 16:47:54 -0700230TEST(pthread, pthread_create) {
231 void* expected_result = reinterpret_cast<void*>(123);
232 // Can we create a thread?
233 pthread_t t;
234 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
235 // If we join, do we get the expected value back?
236 void* result;
237 ASSERT_EQ(0, pthread_join(t, &result));
238 ASSERT_EQ(expected_result, result);
239}
240
Elliott Hughes3e898472013-02-12 16:40:24 +0000241TEST(pthread, pthread_create_EAGAIN) {
242 pthread_attr_t attributes;
243 ASSERT_EQ(0, pthread_attr_init(&attributes));
244 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
245
246 pthread_t t;
247 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
248}
249
Elliott Hughes4d014e12012-09-07 16:47:54 -0700250TEST(pthread, pthread_no_join_after_detach) {
Yabin Cui63481602014-12-01 17:41:04 -0800251 SpinFunctionHelper spinhelper;
252
Elliott Hughes4d014e12012-09-07 16:47:54 -0700253 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800254 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700255
256 // After a pthread_detach...
257 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400258 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700259
260 // ...pthread_join should fail.
Elliott Hughes34c987a2014-09-22 16:01:26 -0700261 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700262}
263
264TEST(pthread, pthread_no_op_detach_after_join) {
Yabin Cui63481602014-12-01 17:41:04 -0800265 SpinFunctionHelper spinhelper;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400266
Elliott Hughes4d014e12012-09-07 16:47:54 -0700267 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800268 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700269
270 // If thread 2 is already waiting to join thread 1...
271 pthread_t t2;
272 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
273
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400274 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700275
Yabin Cuibbb04322015-03-19 15:19:25 -0700276#if defined(__BIONIC__)
277 ASSERT_EQ(EINVAL, pthread_detach(t1));
278#else
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400279 ASSERT_EQ(0, pthread_detach(t1));
Yabin Cuibbb04322015-03-19 15:19:25 -0700280#endif
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400281 AssertDetached(t1, false);
282
Yabin Cui63481602014-12-01 17:41:04 -0800283 spinhelper.UnSpin();
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400284
285 // ...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 -0700286 void* join_result;
287 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700288 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700289}
Elliott Hughes14f19592012-10-29 10:19:44 -0700290
291TEST(pthread, pthread_join_self) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700292 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL));
Elliott Hughes14f19592012-10-29 10:19:44 -0700293}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700294
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800295struct TestBug37410 {
296 pthread_t main_thread;
297 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700298
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800299 static void main() {
300 TestBug37410 data;
301 data.main_thread = pthread_self();
302 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
303 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
304
305 pthread_t t;
306 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
307
308 // Wait for the thread to be running...
309 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
310 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
311
312 // ...and exit.
313 pthread_exit(NULL);
314 }
315
316 private:
317 static void* thread_fn(void* arg) {
318 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
319
320 // Let the main thread know we're running.
321 pthread_mutex_unlock(&data->mutex);
322
323 // And wait for the main thread to exit.
324 pthread_join(data->main_thread, NULL);
325
326 return NULL;
327 }
328};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700329
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800330// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
331// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800332
333class pthread_DeathTest : public BionicDeathTest {};
334
335TEST_F(pthread_DeathTest, pthread_bug_37410) {
Elliott Hughes4f251be2012-11-01 16:33:29 -0700336 // http://code.google.com/p/android/issues/detail?id=37410
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800337 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700338}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800339
340static void* SignalHandlerFn(void* arg) {
341 sigset_t wait_set;
342 sigfillset(&wait_set);
343 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
344}
345
346TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700347 // Check that SIGUSR1 isn't blocked.
348 sigset_t original_set;
349 sigemptyset(&original_set);
350 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
351 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
352
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800353 // Block SIGUSR1.
354 sigset_t set;
355 sigemptyset(&set);
356 sigaddset(&set, SIGUSR1);
357 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
358
Elliott Hughes19e62322013-10-15 11:23:57 -0700359 // Check that SIGUSR1 is blocked.
360 sigset_t final_set;
361 sigemptyset(&final_set);
362 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
363 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
364 // ...and that sigprocmask agrees with pthread_sigmask.
365 sigemptyset(&final_set);
366 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
367 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
368
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800369 // Spawn a thread that calls sigwait and tells us what it received.
370 pthread_t signal_thread;
371 int received_signal = -1;
372 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
373
374 // Send that thread SIGUSR1.
375 pthread_kill(signal_thread, SIGUSR1);
376
377 // See what it got.
378 void* join_result;
379 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
380 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700381 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700382
383 // Restore the original signal mask.
384 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800385}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800386
Elliott Hughes3e898472013-02-12 16:40:24 +0000387TEST(pthread, pthread_setname_np__too_long) {
388 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
389}
390
391TEST(pthread, pthread_setname_np__self) {
392 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
393}
394
395TEST(pthread, pthread_setname_np__other) {
Yabin Cui63481602014-12-01 17:41:04 -0800396 SpinFunctionHelper spinhelper;
397
Elliott Hughesed29e852014-10-27 12:01:51 -0700398 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800399 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughesed29e852014-10-27 12:01:51 -0700400 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000401}
402
Yabin Cui03324782015-03-24 17:43:14 -0700403TEST_F(pthread_DeathTest, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800404 pthread_t dead_thread;
405 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000406
407 // Call pthread_setname_np after thread has already exited.
Yabin Cui03324782015-03-24 17:43:14 -0700408#if defined(__BIONIC__)
409 ASSERT_EXIT(pthread_setname_np(dead_thread, "short 3"), testing::KilledBySignal(SIGABRT),
410 "No such thread");
411#else
Elliott Hughes68d98d82014-11-12 21:03:26 -0800412 ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
Yabin Cui03324782015-03-24 17:43:14 -0700413#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000414}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800415
416TEST(pthread, pthread_kill__0) {
417 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
418 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
419}
420
421TEST(pthread, pthread_kill__invalid_signal) {
422 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
423}
424
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800425static void pthread_kill__in_signal_handler_helper(int signal_number) {
426 static int count = 0;
427 ASSERT_EQ(SIGALRM, signal_number);
428 if (++count == 1) {
429 // Can we call pthread_kill from a signal handler?
430 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
431 }
432}
433
434TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800435 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800436 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
437}
438
Yabin Cui03324782015-03-24 17:43:14 -0700439TEST_F(pthread_DeathTest, pthread_detach__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800440 pthread_t dead_thread;
441 MakeDeadThread(dead_thread);
442
Yabin Cui03324782015-03-24 17:43:14 -0700443#if defined(__BIONIC__)
444 ASSERT_EXIT(pthread_detach(dead_thread), testing::KilledBySignal(SIGABRT), "No such thread");
445#else
Elliott Hughes9d23e042013-02-15 19:21:51 -0800446 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
Yabin Cui03324782015-03-24 17:43:14 -0700447#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800448}
449
Yabin Cui19e246d2014-12-18 14:22:09 -0800450TEST(pthread, pthread_detach_no_leak) {
Christopher Ferrise3809602014-08-06 14:15:01 -0700451 size_t initial_bytes = 0;
452 // Run this loop more than once since the first loop causes some memory
453 // to be allocated permenantly. Run an extra loop to help catch any subtle
454 // memory leaks.
455 for (size_t loop = 0; loop < 3; loop++) {
456 // Set the initial bytes on the second loop since the memory in use
457 // should have stabilized.
458 if (loop == 1) {
459 initial_bytes = mallinfo().uordblks;
460 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800461
Christopher Ferrise3809602014-08-06 14:15:01 -0700462 pthread_attr_t attr;
463 ASSERT_EQ(0, pthread_attr_init(&attr));
464 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
Elliott Hughes04620a32014-03-07 17:59:05 -0800465
Christopher Ferrise3809602014-08-06 14:15:01 -0700466 std::vector<pthread_t> threads;
467 for (size_t i = 0; i < 32; ++i) {
468 pthread_t t;
469 ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL));
470 threads.push_back(t);
471 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800472
Christopher Ferrise3809602014-08-06 14:15:01 -0700473 sleep(1);
Elliott Hughes04620a32014-03-07 17:59:05 -0800474
Christopher Ferrise3809602014-08-06 14:15:01 -0700475 for (size_t i = 0; i < 32; ++i) {
476 ASSERT_EQ(0, pthread_detach(threads[i])) << i;
477 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800478 }
479
480 size_t final_bytes = mallinfo().uordblks;
Elliott Hughes04620a32014-03-07 17:59:05 -0800481 int leaked_bytes = (final_bytes - initial_bytes);
482
Yabin Cui19e246d2014-12-18 14:22:09 -0800483 ASSERT_EQ(0, leaked_bytes);
Elliott Hughes04620a32014-03-07 17:59:05 -0800484}
485
Jeff Hao9b06cc32013-08-15 14:51:16 -0700486TEST(pthread, pthread_getcpuclockid__clock_gettime) {
Yabin Cui63481602014-12-01 17:41:04 -0800487 SpinFunctionHelper spinhelper;
488
Jeff Hao9b06cc32013-08-15 14:51:16 -0700489 pthread_t t;
Yabin Cui63481602014-12-01 17:41:04 -0800490 ASSERT_EQ(0, pthread_create(&t, NULL, spinhelper.GetFunction(), NULL));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700491
492 clockid_t c;
493 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
494 timespec ts;
495 ASSERT_EQ(0, clock_gettime(c, &ts));
496}
497
Yabin Cui03324782015-03-24 17:43:14 -0700498TEST_F(pthread_DeathTest, pthread_getcpuclockid__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800499 pthread_t dead_thread;
500 MakeDeadThread(dead_thread);
501
502 clockid_t c;
Yabin Cui03324782015-03-24 17:43:14 -0700503#if defined(__BIONIC__)
504 ASSERT_EXIT(pthread_getcpuclockid(dead_thread, &c), testing::KilledBySignal(SIGABRT),
505 "No such thread");
506#else
Elliott Hughes9d23e042013-02-15 19:21:51 -0800507 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
Yabin Cui03324782015-03-24 17:43:14 -0700508#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800509}
510
Yabin Cui03324782015-03-24 17:43:14 -0700511TEST_F(pthread_DeathTest, pthread_getschedparam__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800512 pthread_t dead_thread;
513 MakeDeadThread(dead_thread);
514
515 int policy;
516 sched_param param;
Yabin Cui03324782015-03-24 17:43:14 -0700517#if defined(__BIONIC__)
518 ASSERT_EXIT(pthread_getschedparam(dead_thread, &policy, &param), testing::KilledBySignal(SIGABRT),
519 "No such thread");
520#else
Elliott Hughes9d23e042013-02-15 19:21:51 -0800521 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
Yabin Cui03324782015-03-24 17:43:14 -0700522#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800523}
524
Yabin Cui03324782015-03-24 17:43:14 -0700525TEST_F(pthread_DeathTest, pthread_setschedparam__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800526 pthread_t dead_thread;
527 MakeDeadThread(dead_thread);
528
529 int policy = 0;
530 sched_param param;
Yabin Cui03324782015-03-24 17:43:14 -0700531#if defined(__BIONIC__)
532 ASSERT_EXIT(pthread_setschedparam(dead_thread, policy, &param), testing::KilledBySignal(SIGABRT),
533 "No such thread");
534#else
Elliott Hughes9d23e042013-02-15 19:21:51 -0800535 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
Yabin Cui03324782015-03-24 17:43:14 -0700536#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800537}
538
Yabin Cui03324782015-03-24 17:43:14 -0700539TEST_F(pthread_DeathTest, pthread_join__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800540 pthread_t dead_thread;
541 MakeDeadThread(dead_thread);
542
Yabin Cui03324782015-03-24 17:43:14 -0700543#if defined(__BIONIC__)
544 ASSERT_EXIT(pthread_join(dead_thread, NULL), testing::KilledBySignal(SIGABRT), "No such thread");
545#else
Elliott Hughes34c987a2014-09-22 16:01:26 -0700546 ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL));
Yabin Cui03324782015-03-24 17:43:14 -0700547#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800548}
549
Yabin Cui03324782015-03-24 17:43:14 -0700550TEST_F(pthread_DeathTest, pthread_kill__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800551 pthread_t dead_thread;
552 MakeDeadThread(dead_thread);
553
Yabin Cui03324782015-03-24 17:43:14 -0700554#if defined(__BIONIC__)
555 ASSERT_EXIT(pthread_kill(dead_thread, 0), testing::KilledBySignal(SIGABRT), "No such thread");
556#else
Elliott Hughes9d23e042013-02-15 19:21:51 -0800557 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
Yabin Cui03324782015-03-24 17:43:14 -0700558#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800559}
msg5550f020d12013-06-06 14:59:28 -0400560
561TEST(pthread, pthread_join__multijoin) {
Yabin Cui63481602014-12-01 17:41:04 -0800562 SpinFunctionHelper spinhelper;
msg5550f020d12013-06-06 14:59:28 -0400563
564 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800565 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
msg5550f020d12013-06-06 14:59:28 -0400566
567 pthread_t t2;
568 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
569
570 sleep(1); // (Give t2 a chance to call pthread_join.)
571
572 // Multiple joins to the same thread should fail.
573 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
574
Yabin Cui63481602014-12-01 17:41:04 -0800575 spinhelper.UnSpin();
msg5550f020d12013-06-06 14:59:28 -0400576
577 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
578 void* join_result;
579 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700580 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400581}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700582
Elliott Hughes70b24b12013-11-15 11:51:07 -0800583TEST(pthread, pthread_join__race) {
584 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
585 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
586 for (size_t i = 0; i < 1024; ++i) {
587 size_t stack_size = 64*1024;
588 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
589
590 pthread_attr_t a;
591 pthread_attr_init(&a);
592 pthread_attr_setstack(&a, stack, stack_size);
593
594 pthread_t t;
595 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
596 ASSERT_EQ(0, pthread_join(t, NULL));
597 ASSERT_EQ(0, munmap(stack, stack_size));
598 }
599}
600
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700601static void* GetActualGuardSizeFn(void* arg) {
602 pthread_attr_t attributes;
603 pthread_getattr_np(pthread_self(), &attributes);
604 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
605 return NULL;
606}
607
608static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
609 size_t result;
610 pthread_t t;
611 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700612 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700613 return result;
614}
615
616static void* GetActualStackSizeFn(void* arg) {
617 pthread_attr_t attributes;
618 pthread_getattr_np(pthread_self(), &attributes);
619 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
620 return NULL;
621}
622
623static size_t GetActualStackSize(const pthread_attr_t& attributes) {
624 size_t result;
625 pthread_t t;
626 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700627 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700628 return result;
629}
630
631TEST(pthread, pthread_attr_setguardsize) {
632 pthread_attr_t attributes;
633 ASSERT_EQ(0, pthread_attr_init(&attributes));
634
635 // Get the default guard size.
636 size_t default_guard_size;
637 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
638
639 // No such thing as too small: will be rounded up to one page by pthread_create.
640 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
641 size_t guard_size;
642 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
643 ASSERT_EQ(128U, guard_size);
644 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
645
646 // Large enough and a multiple of the page size.
647 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
648 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
649 ASSERT_EQ(32*1024U, guard_size);
650
651 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
652 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
653 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
654 ASSERT_EQ(32*1024U + 1, guard_size);
655}
656
657TEST(pthread, pthread_attr_setstacksize) {
658 pthread_attr_t attributes;
659 ASSERT_EQ(0, pthread_attr_init(&attributes));
660
661 // Get the default stack size.
662 size_t default_stack_size;
663 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
664
665 // Too small.
666 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
667 size_t stack_size;
668 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
669 ASSERT_EQ(default_stack_size, stack_size);
670 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
671
Yabin Cui917d3902015-01-08 12:32:42 -0800672 // Large enough and a multiple of the page size; may be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700673 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
674 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
675 ASSERT_EQ(32*1024U, stack_size);
Yabin Cui917d3902015-01-08 12:32:42 -0800676 ASSERT_GE(GetActualStackSize(attributes), 32*1024U);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700677
Yabin Cui917d3902015-01-08 12:32:42 -0800678 // Large enough but not aligned; will be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700679 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
680 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
681 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800682#if defined(__BIONIC__)
Yabin Cui917d3902015-01-08 12:32:42 -0800683 ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800684#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700685 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
686 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800687#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700688}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700689
690TEST(pthread, pthread_rwlock_smoke) {
691 pthread_rwlock_t l;
692 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
693
Calin Juravle76f352e2014-05-19 13:41:10 +0100694 // Single read lock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700695 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
696 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
697
Calin Juravle76f352e2014-05-19 13:41:10 +0100698 // Multiple read lock
699 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
700 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
701 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
702 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
703
704 // Write lock
Calin Juravle92687e42014-05-22 19:21:22 +0100705 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
706 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100707
708 // Try writer lock
709 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
710 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
711 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
712 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
713
714 // Try reader lock
715 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
716 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
717 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
718 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
719 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
720
721 // Try writer lock after unlock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700722 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
723 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
724
Calin Juravle76f352e2014-05-19 13:41:10 +0100725#ifdef __BIONIC__
726 // EDEADLK in "read after write"
727 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
728 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
729 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
730
731 // EDEADLK in "write after write"
732 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
733 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
734 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
735#endif
736
Elliott Hughesc3f11402013-10-30 14:40:09 -0700737 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
738}
739
Yabin Cui08ee8d22015-02-11 17:04:36 -0800740struct RwlockWakeupHelperArg {
741 pthread_rwlock_t lock;
742 enum Progress {
743 LOCK_INITIALIZED,
744 LOCK_WAITING,
745 LOCK_RELEASED,
746 LOCK_ACCESSED
747 };
748 std::atomic<Progress> progress;
749};
750
751static void pthread_rwlock_reader_wakeup_writer_helper(RwlockWakeupHelperArg* arg) {
752 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
753 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
754
755 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&arg->lock));
756 ASSERT_EQ(0, pthread_rwlock_wrlock(&arg->lock));
757 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
758 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
759
760 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
761}
762
763TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
764 RwlockWakeupHelperArg wakeup_arg;
765 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
766 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
767 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
768
769 pthread_t thread;
770 ASSERT_EQ(0, pthread_create(&thread, NULL,
771 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_reader_wakeup_writer_helper), &wakeup_arg));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800772 while (wakeup_arg.progress != RwlockWakeupHelperArg::LOCK_WAITING) {
773 usleep(5000);
774 }
775 usleep(5000);
Yabin Cui08ee8d22015-02-11 17:04:36 -0800776 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
777 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
778
779 ASSERT_EQ(0, pthread_join(thread, NULL));
780 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
781 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
782}
783
784static void pthread_rwlock_writer_wakeup_reader_helper(RwlockWakeupHelperArg* arg) {
785 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
786 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
787
788 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&arg->lock));
789 ASSERT_EQ(0, pthread_rwlock_rdlock(&arg->lock));
790 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
791 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
792
793 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
794}
795
796TEST(pthread, pthread_rwlock_writer_wakeup_reader) {
797 RwlockWakeupHelperArg wakeup_arg;
798 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
799 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
800 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
801
802 pthread_t thread;
803 ASSERT_EQ(0, pthread_create(&thread, NULL,
804 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_writer_wakeup_reader_helper), &wakeup_arg));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800805 while (wakeup_arg.progress != RwlockWakeupHelperArg::LOCK_WAITING) {
806 usleep(5000);
807 }
808 usleep(5000);
Yabin Cui08ee8d22015-02-11 17:04:36 -0800809 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
810 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
811
812 ASSERT_EQ(0, pthread_join(thread, NULL));
813 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
814 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
815}
816
Elliott Hughes1728b232014-05-14 10:02:03 -0700817static int g_once_fn_call_count = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700818static void OnceFn() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700819 ++g_once_fn_call_count;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700820}
821
822TEST(pthread, pthread_once_smoke) {
823 pthread_once_t once_control = PTHREAD_ONCE_INIT;
824 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
825 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
Elliott Hughes1728b232014-05-14 10:02:03 -0700826 ASSERT_EQ(1, g_once_fn_call_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700827}
828
Elliott Hughes3694ec62014-05-14 11:46:08 -0700829static std::string pthread_once_1934122_result = "";
830
831static void Routine2() {
832 pthread_once_1934122_result += "2";
833}
834
835static void Routine1() {
836 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
837 pthread_once_1934122_result += "1";
838 pthread_once(&once_control_2, &Routine2);
839}
840
841TEST(pthread, pthread_once_1934122) {
842 // Very old versions of Android couldn't call pthread_once from a
843 // pthread_once init routine. http://b/1934122.
844 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
845 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
846 ASSERT_EQ("12", pthread_once_1934122_result);
847}
848
Elliott Hughes1728b232014-05-14 10:02:03 -0700849static int g_atfork_prepare_calls = 0;
850static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; }
851static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; }
852static int g_atfork_parent_calls = 0;
853static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; }
854static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; }
855static int g_atfork_child_calls = 0;
856static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; }
857static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700858
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800859TEST(pthread, pthread_atfork_smoke) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700860 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
861 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700862
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700863 int pid = fork();
864 ASSERT_NE(-1, pid) << strerror(errno);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700865
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700866 // Child and parent calls are made in the order they were registered.
867 if (pid == 0) {
868 ASSERT_EQ(0x12, g_atfork_child_calls);
869 _exit(0);
870 }
871 ASSERT_EQ(0x12, g_atfork_parent_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700872
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700873 // Prepare calls are made in the reverse order.
874 ASSERT_EQ(0x21, g_atfork_prepare_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700875}
876
877TEST(pthread, pthread_attr_getscope) {
878 pthread_attr_t attr;
879 ASSERT_EQ(0, pthread_attr_init(&attr));
880
881 int scope;
882 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
883 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
884}
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000885
886TEST(pthread, pthread_condattr_init) {
887 pthread_condattr_t attr;
888 pthread_condattr_init(&attr);
889
890 clockid_t clock;
891 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
892 ASSERT_EQ(CLOCK_REALTIME, clock);
893
894 int pshared;
895 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
896 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
897}
898
899TEST(pthread, pthread_condattr_setclock) {
900 pthread_condattr_t attr;
901 pthread_condattr_init(&attr);
902
903 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
904 clockid_t clock;
905 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
906 ASSERT_EQ(CLOCK_REALTIME, clock);
907
908 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
909 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
910 ASSERT_EQ(CLOCK_MONOTONIC, clock);
911
912 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
913}
914
915TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
Yabin Cui32651b82015-03-13 20:30:00 -0700916#if defined(__BIONIC__)
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000917 pthread_condattr_t attr;
918 pthread_condattr_init(&attr);
919
920 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
921 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
922
923 pthread_cond_t cond_var;
924 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
925
926 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
927 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
928
Yabin Cui32651b82015-03-13 20:30:00 -0700929 attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000930 clockid_t clock;
931 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
932 ASSERT_EQ(CLOCK_MONOTONIC, clock);
933 int pshared;
934 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
935 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
Yabin Cui32651b82015-03-13 20:30:00 -0700936#else // !defined(__BIONIC__)
937 GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n";
938#endif // !defined(__BIONIC__)
939}
940
941class pthread_CondWakeupTest : public ::testing::Test {
942 protected:
943 pthread_mutex_t mutex;
944 pthread_cond_t cond;
945
946 enum Progress {
947 INITIALIZED,
948 WAITING,
949 SIGNALED,
950 FINISHED,
951 };
952 std::atomic<Progress> progress;
953 pthread_t thread;
954
955 protected:
956 virtual void SetUp() {
957 ASSERT_EQ(0, pthread_mutex_init(&mutex, NULL));
958 ASSERT_EQ(0, pthread_cond_init(&cond, NULL));
959 progress = INITIALIZED;
960 ASSERT_EQ(0,
961 pthread_create(&thread, NULL, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this));
962 }
963
964 virtual void TearDown() {
965 ASSERT_EQ(0, pthread_join(thread, NULL));
966 ASSERT_EQ(FINISHED, progress);
967 ASSERT_EQ(0, pthread_cond_destroy(&cond));
968 ASSERT_EQ(0, pthread_mutex_destroy(&mutex));
969 }
970
971 void SleepUntilProgress(Progress expected_progress) {
972 while (progress != expected_progress) {
973 usleep(5000);
974 }
975 usleep(5000);
976 }
977
978 private:
979 static void WaitThreadFn(pthread_CondWakeupTest* test) {
980 ASSERT_EQ(0, pthread_mutex_lock(&test->mutex));
981 test->progress = WAITING;
982 while (test->progress == WAITING) {
983 ASSERT_EQ(0, pthread_cond_wait(&test->cond, &test->mutex));
984 }
985 ASSERT_EQ(SIGNALED, test->progress);
986 test->progress = FINISHED;
987 ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex));
988 }
989};
990
991TEST_F(pthread_CondWakeupTest, signal) {
992 SleepUntilProgress(WAITING);
993 progress = SIGNALED;
994 pthread_cond_signal(&cond);
995}
996
997TEST_F(pthread_CondWakeupTest, broadcast) {
998 SleepUntilProgress(WAITING);
999 progress = SIGNALED;
1000 pthread_cond_broadcast(&cond);
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001001}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001002
1003TEST(pthread, pthread_mutex_timedlock) {
1004 pthread_mutex_t m;
1005 ASSERT_EQ(0, pthread_mutex_init(&m, NULL));
1006
1007 // If the mutex is already locked, pthread_mutex_timedlock should time out.
1008 ASSERT_EQ(0, pthread_mutex_lock(&m));
1009
1010 timespec ts;
1011 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1012 ts.tv_nsec += 1;
1013 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
1014
1015 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
1016 ASSERT_EQ(0, pthread_mutex_unlock(&m));
1017
1018 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1019 ts.tv_nsec += 1;
1020 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
1021
1022 ASSERT_EQ(0, pthread_mutex_unlock(&m));
1023 ASSERT_EQ(0, pthread_mutex_destroy(&m));
1024}
Elliott Hughes57b7a612014-08-25 17:26:50 -07001025
1026TEST(pthread, pthread_attr_getstack__main_thread) {
1027 // This test is only meaningful for the main thread, so make sure we're running on it!
1028 ASSERT_EQ(getpid(), syscall(__NR_gettid));
1029
1030 // Get the main thread's attributes.
1031 pthread_attr_t attributes;
1032 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1033
1034 // Check that we correctly report that the main thread has no guard page.
1035 size_t guard_size;
1036 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
1037 ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
1038
1039 // Get the stack base and the stack size (both ways).
1040 void* stack_base;
1041 size_t stack_size;
1042 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1043 size_t stack_size2;
1044 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1045
1046 // The two methods of asking for the stack size should agree.
1047 EXPECT_EQ(stack_size, stack_size2);
1048
1049 // What does /proc/self/maps' [stack] line say?
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001050 void* maps_stack_hi = NULL;
Elliott Hughes57b7a612014-08-25 17:26:50 -07001051 FILE* fp = fopen("/proc/self/maps", "r");
1052 ASSERT_TRUE(fp != NULL);
1053 char line[BUFSIZ];
1054 while (fgets(line, sizeof(line), fp) != NULL) {
1055 uintptr_t lo, hi;
1056 char name[10];
1057 sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name);
1058 if (strcmp(name, "[stack]") == 0) {
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001059 maps_stack_hi = reinterpret_cast<void*>(hi);
Elliott Hughes57b7a612014-08-25 17:26:50 -07001060 break;
1061 }
1062 }
1063 fclose(fp);
1064
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001065 // The stack size should correspond to RLIMIT_STACK.
Elliott Hughes57b7a612014-08-25 17:26:50 -07001066 rlimit rl;
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001067 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001068 uint64_t original_rlim_cur = rl.rlim_cur;
1069#if defined(__BIONIC__)
1070 if (rl.rlim_cur == RLIM_INFINITY) {
1071 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
1072 }
1073#endif
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001074 EXPECT_EQ(rl.rlim_cur, stack_size);
1075
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001076 auto guard = make_scope_guard([&rl, original_rlim_cur]() {
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001077 rl.rlim_cur = original_rlim_cur;
1078 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1079 });
1080
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001081 // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
1082 // Remember that the stack grows down (and is mapped in on demand), so the low address of the
1083 // region isn't very interesting.
1084 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
1085
1086 //
1087 // What if RLIMIT_STACK is smaller than the stack's current extent?
1088 //
Elliott Hughes57b7a612014-08-25 17:26:50 -07001089 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
1090 rl.rlim_max = RLIM_INFINITY;
1091 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1092
1093 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1094 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1095 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1096
1097 EXPECT_EQ(stack_size, stack_size2);
1098 ASSERT_EQ(1024U, stack_size);
1099
1100 //
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001101 // What if RLIMIT_STACK isn't a whole number of pages?
Elliott Hughes57b7a612014-08-25 17:26:50 -07001102 //
1103 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
1104 rl.rlim_max = RLIM_INFINITY;
1105 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1106
1107 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1108 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1109 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1110
1111 EXPECT_EQ(stack_size, stack_size2);
1112 ASSERT_EQ(6666U, stack_size);
1113}
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001114
Yabin Cui917d3902015-01-08 12:32:42 -08001115static void pthread_attr_getstack_18908062_helper(void*) {
1116 char local_variable;
1117 pthread_attr_t attributes;
1118 pthread_getattr_np(pthread_self(), &attributes);
1119 void* stack_base;
1120 size_t stack_size;
1121 pthread_attr_getstack(&attributes, &stack_base, &stack_size);
1122
1123 // Test whether &local_variable is in [stack_base, stack_base + stack_size).
1124 ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable);
1125 ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size);
1126}
1127
1128// Check whether something on stack is in the range of
1129// [stack_base, stack_base + stack_size). see b/18908062.
1130TEST(pthread, pthread_attr_getstack_18908062) {
1131 pthread_t t;
1132 ASSERT_EQ(0, pthread_create(&t, NULL,
1133 reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper),
1134 NULL));
1135 pthread_join(t, NULL);
1136}
1137
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001138#if defined(__BIONIC__)
1139static void* pthread_gettid_np_helper(void* arg) {
1140 *reinterpret_cast<pid_t*>(arg) = gettid();
1141 return NULL;
1142}
1143#endif
1144
1145TEST(pthread, pthread_gettid_np) {
1146#if defined(__BIONIC__)
1147 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
1148
1149 pid_t t_gettid_result;
1150 pthread_t t;
1151 pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
1152
1153 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
1154
Elliott Hughes34c987a2014-09-22 16:01:26 -07001155 pthread_join(t, NULL);
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001156
1157 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
1158#else
1159 GTEST_LOG_(INFO) << "This test does nothing.\n";
1160#endif
1161}
Elliott Hughes34c987a2014-09-22 16:01:26 -07001162
1163static size_t cleanup_counter = 0;
1164
Derek Xue41996952014-09-25 11:05:32 +01001165static void AbortCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001166 abort();
1167}
1168
Derek Xue41996952014-09-25 11:05:32 +01001169static void CountCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001170 ++cleanup_counter;
1171}
1172
Derek Xue41996952014-09-25 11:05:32 +01001173static void PthreadCleanupTester() {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001174 pthread_cleanup_push(CountCleanupRoutine, NULL);
1175 pthread_cleanup_push(CountCleanupRoutine, NULL);
1176 pthread_cleanup_push(AbortCleanupRoutine, NULL);
1177
1178 pthread_cleanup_pop(0); // Pop the abort without executing it.
1179 pthread_cleanup_pop(1); // Pop one count while executing it.
1180 ASSERT_EQ(1U, cleanup_counter);
1181 // Exit while the other count is still on the cleanup stack.
1182 pthread_exit(NULL);
1183
1184 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
1185 pthread_cleanup_pop(0);
1186}
1187
Derek Xue41996952014-09-25 11:05:32 +01001188static void* PthreadCleanupStartRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001189 PthreadCleanupTester();
1190 return NULL;
1191}
1192
1193TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
1194 pthread_t t;
1195 ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL));
1196 pthread_join(t, NULL);
1197 ASSERT_EQ(2U, cleanup_counter);
1198}
Derek Xue41996952014-09-25 11:05:32 +01001199
1200TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
1201 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
1202}
1203
1204TEST(pthread, pthread_mutexattr_gettype) {
1205 pthread_mutexattr_t attr;
1206 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1207
1208 int attr_type;
1209
1210 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1211 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1212 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
1213
1214 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1215 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1216 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
1217
1218 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1219 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1220 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001221
1222 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1223}
1224
Yabin Cui17393b02015-03-21 15:08:25 -07001225struct PthreadMutex {
1226 pthread_mutex_t lock;
1227
1228 PthreadMutex(int mutex_type) {
1229 init(mutex_type);
1230 }
1231
1232 ~PthreadMutex() {
1233 destroy();
1234 }
1235
1236 private:
1237 void init(int mutex_type) {
1238 pthread_mutexattr_t attr;
1239 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1240 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, mutex_type));
1241 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1242 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1243 }
1244
1245 void destroy() {
1246 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1247 }
1248
1249 DISALLOW_COPY_AND_ASSIGN(PthreadMutex);
1250};
Derek Xue41996952014-09-25 11:05:32 +01001251
1252TEST(pthread, pthread_mutex_lock_NORMAL) {
Yabin Cui17393b02015-03-21 15:08:25 -07001253 PthreadMutex m(PTHREAD_MUTEX_NORMAL);
Derek Xue41996952014-09-25 11:05:32 +01001254
Yabin Cui17393b02015-03-21 15:08:25 -07001255 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1256 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Derek Xue41996952014-09-25 11:05:32 +01001257}
1258
1259TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
Yabin Cui17393b02015-03-21 15:08:25 -07001260 PthreadMutex m(PTHREAD_MUTEX_ERRORCHECK);
Derek Xue41996952014-09-25 11:05:32 +01001261
Yabin Cui17393b02015-03-21 15:08:25 -07001262 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1263 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&m.lock));
1264 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1265 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1266 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock));
1267 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1268 ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
Derek Xue41996952014-09-25 11:05:32 +01001269}
1270
1271TEST(pthread, pthread_mutex_lock_RECURSIVE) {
Yabin Cui17393b02015-03-21 15:08:25 -07001272 PthreadMutex m(PTHREAD_MUTEX_RECURSIVE);
Derek Xue41996952014-09-25 11:05:32 +01001273
Yabin Cui17393b02015-03-21 15:08:25 -07001274 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1275 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1276 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1277 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1278 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1279 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1280 ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
1281}
1282
1283TEST(pthread, pthread_mutex_init_same_as_static_initializers) {
1284 pthread_mutex_t lock_normal = PTHREAD_MUTEX_INITIALIZER;
1285 PthreadMutex m1(PTHREAD_MUTEX_NORMAL);
1286 ASSERT_EQ(0, memcmp(&lock_normal, &m1.lock, sizeof(pthread_mutex_t)));
1287 pthread_mutex_destroy(&lock_normal);
1288
1289 pthread_mutex_t lock_errorcheck = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
1290 PthreadMutex m2(PTHREAD_MUTEX_ERRORCHECK);
1291 ASSERT_EQ(0, memcmp(&lock_errorcheck, &m2.lock, sizeof(pthread_mutex_t)));
1292 pthread_mutex_destroy(&lock_errorcheck);
1293
1294 pthread_mutex_t lock_recursive = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
1295 PthreadMutex m3(PTHREAD_MUTEX_RECURSIVE);
1296 ASSERT_EQ(0, memcmp(&lock_recursive, &m3.lock, sizeof(pthread_mutex_t)));
1297 ASSERT_EQ(0, pthread_mutex_destroy(&lock_recursive));
Derek Xue41996952014-09-25 11:05:32 +01001298}
Yabin Cui140f3672015-02-03 10:32:00 -08001299
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001300class MutexWakeupHelper {
1301 private:
Yabin Cui17393b02015-03-21 15:08:25 -07001302 PthreadMutex m;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001303 enum Progress {
1304 LOCK_INITIALIZED,
1305 LOCK_WAITING,
1306 LOCK_RELEASED,
1307 LOCK_ACCESSED
1308 };
1309 std::atomic<Progress> progress;
1310
1311 static void thread_fn(MutexWakeupHelper* helper) {
1312 ASSERT_EQ(LOCK_INITIALIZED, helper->progress);
1313 helper->progress = LOCK_WAITING;
1314
Yabin Cui17393b02015-03-21 15:08:25 -07001315 ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001316 ASSERT_EQ(LOCK_RELEASED, helper->progress);
Yabin Cui17393b02015-03-21 15:08:25 -07001317 ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001318
1319 helper->progress = LOCK_ACCESSED;
1320 }
1321
1322 public:
Yabin Cui17393b02015-03-21 15:08:25 -07001323 MutexWakeupHelper(int mutex_type) : m(mutex_type) {
1324 }
1325
1326 void test() {
1327 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001328 progress = LOCK_INITIALIZED;
1329
1330 pthread_t thread;
1331 ASSERT_EQ(0, pthread_create(&thread, NULL,
1332 reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this));
1333
1334 while (progress != LOCK_WAITING) {
1335 usleep(5000);
1336 }
1337 usleep(5000);
1338 progress = LOCK_RELEASED;
Yabin Cui17393b02015-03-21 15:08:25 -07001339 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001340
1341 ASSERT_EQ(0, pthread_join(thread, NULL));
1342 ASSERT_EQ(LOCK_ACCESSED, progress);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001343 }
1344};
1345
1346TEST(pthread, pthread_mutex_NORMAL_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001347 MutexWakeupHelper helper(PTHREAD_MUTEX_NORMAL);
1348 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001349}
1350
1351TEST(pthread, pthread_mutex_ERRORCHECK_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001352 MutexWakeupHelper helper(PTHREAD_MUTEX_ERRORCHECK);
1353 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001354}
1355
1356TEST(pthread, pthread_mutex_RECURSIVE_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001357 MutexWakeupHelper helper(PTHREAD_MUTEX_RECURSIVE);
1358 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001359}
1360
Yabin Cui140f3672015-02-03 10:32:00 -08001361TEST(pthread, pthread_mutex_owner_tid_limit) {
1362 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
1363 ASSERT_TRUE(fp != NULL);
1364 long pid_max;
1365 ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max));
1366 fclose(fp);
1367 // Current pthread_mutex uses 16 bits to represent owner tid.
1368 // Change the implementation if we need to support higher value than 65535.
1369 ASSERT_LE(pid_max, 65536);
1370}
Yabin Cuib5845722015-03-16 22:46:42 -07001371
1372class StrictAlignmentAllocator {
1373 public:
1374 void* allocate(size_t size, size_t alignment) {
1375 char* p = new char[size + alignment * 2];
1376 allocated_array.push_back(p);
1377 while (!is_strict_aligned(p, alignment)) {
1378 ++p;
1379 }
1380 return p;
1381 }
1382
1383 ~StrictAlignmentAllocator() {
1384 for (auto& p : allocated_array) {
1385 delete [] p;
1386 }
1387 }
1388
1389 private:
1390 bool is_strict_aligned(char* p, size_t alignment) {
1391 return (reinterpret_cast<uintptr_t>(p) % (alignment * 2)) == alignment;
1392 }
1393
1394 std::vector<char*> allocated_array;
1395};
1396
1397TEST(pthread, pthread_types_allow_four_bytes_alignment) {
1398#if defined(__BIONIC__)
1399 // For binary compatibility with old version, we need to allow 4-byte aligned data for pthread types.
1400 StrictAlignmentAllocator allocator;
1401 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(
1402 allocator.allocate(sizeof(pthread_mutex_t), 4));
1403 ASSERT_EQ(0, pthread_mutex_init(mutex, NULL));
1404 ASSERT_EQ(0, pthread_mutex_lock(mutex));
1405 ASSERT_EQ(0, pthread_mutex_unlock(mutex));
1406 ASSERT_EQ(0, pthread_mutex_destroy(mutex));
1407
1408 pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>(
1409 allocator.allocate(sizeof(pthread_cond_t), 4));
1410 ASSERT_EQ(0, pthread_cond_init(cond, NULL));
1411 ASSERT_EQ(0, pthread_cond_signal(cond));
1412 ASSERT_EQ(0, pthread_cond_broadcast(cond));
1413 ASSERT_EQ(0, pthread_cond_destroy(cond));
1414
1415 pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>(
1416 allocator.allocate(sizeof(pthread_rwlock_t), 4));
1417 ASSERT_EQ(0, pthread_rwlock_init(rwlock, NULL));
1418 ASSERT_EQ(0, pthread_rwlock_rdlock(rwlock));
1419 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
1420 ASSERT_EQ(0, pthread_rwlock_wrlock(rwlock));
1421 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
1422 ASSERT_EQ(0, pthread_rwlock_destroy(rwlock));
1423
1424#else
1425 GTEST_LOG_(INFO) << "This test tests bionic implementation details.";
1426#endif
1427}