blob: f63d1ee93e79a7eac2e1ed5bee48a190836be465 [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"
Dmitriy Ivanov00e37812014-11-20 16:53:47 -080022#include "gtest_ex.h"
Yabin Cui9df70402014-11-05 18:01:01 -080023
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070024#include <errno.h>
Elliott Hughes5b9310e2013-10-02 16:59:05 -070025#include <inttypes.h>
Elliott Hughesb95cf0d2013-07-15 14:51:07 -070026#include <limits.h>
Elliott Hughes04620a32014-03-07 17:59:05 -080027#include <malloc.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070028#include <pthread.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080029#include <signal.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
Elliott Hughes4b558f52014-03-04 15:58:02 -080035
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070036TEST(pthread, pthread_key_create) {
37 pthread_key_t key;
38 ASSERT_EQ(0, pthread_key_create(&key, NULL));
39 ASSERT_EQ(0, pthread_key_delete(key));
40 // Can't delete a key that's already been deleted.
41 ASSERT_EQ(EINVAL, pthread_key_delete(key));
42}
Elliott Hughes4d014e12012-09-07 16:47:54 -070043
Dan Albertc4bcc752014-09-30 11:48:24 -070044TEST(pthread, pthread_keys_max) {
Elliott Hughes18876212013-12-12 11:02:41 -080045 // POSIX says PTHREAD_KEYS_MAX should be at least 128.
46 ASSERT_GE(PTHREAD_KEYS_MAX, 128);
Dan Albertc4bcc752014-09-30 11:48:24 -070047}
Elliott Hughes718a5b52014-01-28 17:02:03 -080048
Dan Albertc4bcc752014-09-30 11:48:24 -070049TEST(pthread, _SC_THREAD_KEYS_MAX_big_enough_for_POSIX) {
50 // sysconf shouldn't return a smaller value.
51 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
52 ASSERT_GE(sysconf_max, PTHREAD_KEYS_MAX);
53}
54
55TEST(pthread, pthread_key_many_distinct) {
56 // We should be able to allocate at least this many keys.
57 int nkeys = sysconf(_SC_THREAD_KEYS_MAX) / 2;
58 std::vector<pthread_key_t> keys;
59
60 auto scope_guard = make_scope_guard([&keys]{
61 for (auto key : keys) {
62 EXPECT_EQ(0, pthread_key_delete(key));
63 }
64 });
65
66 for (int i = 0; i < nkeys; ++i) {
67 pthread_key_t key;
68 // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is
69 // wrong.
70 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys;
71 keys.push_back(key);
72 ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i)));
73 }
74
75 for (int i = keys.size() - 1; i >= 0; --i) {
76 ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back()));
77 pthread_key_t key = keys.back();
78 keys.pop_back();
79 ASSERT_EQ(0, pthread_key_delete(key));
80 }
81}
82
83TEST(pthread, pthread_key_EAGAIN) {
Elliott Hughes718a5b52014-01-28 17:02:03 -080084 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
85
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;
88 // Two keys are used by gtest, so sysconf_max should be more than we are
89 // allowed to allocate now.
90 for (int i = 0; i < sysconf_max; i++) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000091 pthread_key_t key;
Dan Albertc4bcc752014-09-30 11:48:24 -070092 rv = pthread_key_create(&key, NULL);
93 if (rv == EAGAIN) {
94 break;
95 }
96 EXPECT_EQ(0, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +000097 keys.push_back(key);
98 }
99
Dan Albertc4bcc752014-09-30 11:48:24 -0700100 // Don't leak keys.
101 for (auto key : keys) {
102 EXPECT_EQ(0, pthread_key_delete(key));
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000103 }
Dan Albertc4bcc752014-09-30 11:48:24 -0700104 keys.clear();
105
106 // We should have eventually reached the maximum number of keys and received
107 // EAGAIN.
108 ASSERT_EQ(EAGAIN, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000109}
110
Elliott Hughesebb770f2014-06-25 13:46:46 -0700111TEST(pthread, pthread_key_delete) {
112 void* expected = reinterpret_cast<void*>(1234);
113 pthread_key_t key;
114 ASSERT_EQ(0, pthread_key_create(&key, NULL));
115 ASSERT_EQ(0, pthread_setspecific(key, expected));
116 ASSERT_EQ(expected, pthread_getspecific(key));
117 ASSERT_EQ(0, pthread_key_delete(key));
118 // After deletion, pthread_getspecific returns NULL.
119 ASSERT_EQ(NULL, pthread_getspecific(key));
120 // And you can't use pthread_setspecific with the deleted key.
121 ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
122}
123
Elliott Hughes40a52172014-07-30 14:48:10 -0700124TEST(pthread, pthread_key_fork) {
125 void* expected = reinterpret_cast<void*>(1234);
126 pthread_key_t key;
127 ASSERT_EQ(0, pthread_key_create(&key, NULL));
128 ASSERT_EQ(0, pthread_setspecific(key, expected));
129 ASSERT_EQ(expected, pthread_getspecific(key));
130
131 pid_t pid = fork();
132 ASSERT_NE(-1, pid) << strerror(errno);
133
134 if (pid == 0) {
135 // The surviving thread inherits all the forking thread's TLS values...
136 ASSERT_EQ(expected, pthread_getspecific(key));
137 _exit(99);
138 }
139
140 int status;
141 ASSERT_EQ(pid, waitpid(pid, &status, 0));
142 ASSERT_TRUE(WIFEXITED(status));
143 ASSERT_EQ(99, WEXITSTATUS(status));
144
145 ASSERT_EQ(expected, pthread_getspecific(key));
Dan Albert1d53ae22014-09-02 15:24:26 -0700146 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700147}
148
149static void* DirtyKeyFn(void* key) {
150 return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
151}
152
153TEST(pthread, pthread_key_dirty) {
154 pthread_key_t key;
155 ASSERT_EQ(0, pthread_key_create(&key, NULL));
156
157 size_t stack_size = 128 * 1024;
158 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
159 ASSERT_NE(MAP_FAILED, stack);
160 memset(stack, 0xff, stack_size);
161
162 pthread_attr_t attr;
163 ASSERT_EQ(0, pthread_attr_init(&attr));
164 ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
165
166 pthread_t t;
167 ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
168
169 void* result;
170 ASSERT_EQ(0, pthread_join(t, &result));
171 ASSERT_EQ(nullptr, result); // Not ~0!
172
173 ASSERT_EQ(0, munmap(stack, stack_size));
Dan Albert1d53ae22014-09-02 15:24:26 -0700174 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700175}
176
Elliott Hughes4d014e12012-09-07 16:47:54 -0700177static void* IdFn(void* arg) {
178 return arg;
179}
180
Yabin Cui63481602014-12-01 17:41:04 -0800181class SpinFunctionHelper {
182 public:
183 SpinFunctionHelper() {
184 SpinFunctionHelper::spin_flag_ = true;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400185 }
Yabin Cui63481602014-12-01 17:41:04 -0800186 ~SpinFunctionHelper() {
187 UnSpin();
188 }
189 auto GetFunction() -> void* (*)(void*) {
190 return SpinFunctionHelper::SpinFn;
191 }
192
193 void UnSpin() {
194 SpinFunctionHelper::spin_flag_ = false;
195 }
196
197 private:
198 static void* SpinFn(void*) {
199 while (spin_flag_) {}
200 return NULL;
201 }
202 static volatile bool spin_flag_;
203};
204
205// It doesn't matter if spin_flag_ is used in several tests,
206// because it is always set to false after each test. Each thread
207// loops on spin_flag_ can find it becomes false at some time.
208volatile bool SpinFunctionHelper::spin_flag_ = false;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400209
Elliott Hughes4d014e12012-09-07 16:47:54 -0700210static void* JoinFn(void* arg) {
211 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
212}
213
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400214static void AssertDetached(pthread_t t, bool is_detached) {
215 pthread_attr_t attr;
216 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
217 int detach_state;
218 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
219 pthread_attr_destroy(&attr);
220 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
221}
222
Elliott Hughes9d23e042013-02-15 19:21:51 -0800223static void MakeDeadThread(pthread_t& t) {
224 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
Elliott Hughes34c987a2014-09-22 16:01:26 -0700225 ASSERT_EQ(0, pthread_join(t, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800226}
227
Elliott Hughes4d014e12012-09-07 16:47:54 -0700228TEST(pthread, pthread_create) {
229 void* expected_result = reinterpret_cast<void*>(123);
230 // Can we create a thread?
231 pthread_t t;
232 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
233 // If we join, do we get the expected value back?
234 void* result;
235 ASSERT_EQ(0, pthread_join(t, &result));
236 ASSERT_EQ(expected_result, result);
237}
238
Elliott Hughes3e898472013-02-12 16:40:24 +0000239TEST(pthread, pthread_create_EAGAIN) {
240 pthread_attr_t attributes;
241 ASSERT_EQ(0, pthread_attr_init(&attributes));
242 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
243
244 pthread_t t;
245 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
246}
247
Elliott Hughes4d014e12012-09-07 16:47:54 -0700248TEST(pthread, pthread_no_join_after_detach) {
Yabin Cui63481602014-12-01 17:41:04 -0800249 SpinFunctionHelper spinhelper;
250
Elliott Hughes4d014e12012-09-07 16:47:54 -0700251 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800252 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700253
254 // After a pthread_detach...
255 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400256 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700257
258 // ...pthread_join should fail.
Elliott Hughes34c987a2014-09-22 16:01:26 -0700259 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700260}
261
262TEST(pthread, pthread_no_op_detach_after_join) {
Yabin Cui63481602014-12-01 17:41:04 -0800263 SpinFunctionHelper spinhelper;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400264
Elliott Hughes4d014e12012-09-07 16:47:54 -0700265 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800266 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700267
268 // If thread 2 is already waiting to join thread 1...
269 pthread_t t2;
270 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
271
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400272 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700273
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400274 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
275 ASSERT_EQ(0, pthread_detach(t1));
276 AssertDetached(t1, false);
277
Yabin Cui63481602014-12-01 17:41:04 -0800278 spinhelper.UnSpin();
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400279
280 // ...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 -0700281 void* join_result;
282 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700283 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700284}
Elliott Hughes14f19592012-10-29 10:19:44 -0700285
286TEST(pthread, pthread_join_self) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700287 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL));
Elliott Hughes14f19592012-10-29 10:19:44 -0700288}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700289
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800290struct TestBug37410 {
291 pthread_t main_thread;
292 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700293
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800294 static void main() {
295 TestBug37410 data;
296 data.main_thread = pthread_self();
297 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
298 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
299
300 pthread_t t;
301 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
302
303 // Wait for the thread to be running...
304 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
305 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
306
307 // ...and exit.
308 pthread_exit(NULL);
309 }
310
311 private:
312 static void* thread_fn(void* arg) {
313 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
314
315 // Let the main thread know we're running.
316 pthread_mutex_unlock(&data->mutex);
317
318 // And wait for the main thread to exit.
319 pthread_join(data->main_thread, NULL);
320
321 return NULL;
322 }
323};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700324
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800325// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
326// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800327
328class pthread_DeathTest : public BionicDeathTest {};
329
330TEST_F(pthread_DeathTest, pthread_bug_37410) {
Elliott Hughes4f251be2012-11-01 16:33:29 -0700331 // http://code.google.com/p/android/issues/detail?id=37410
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800332 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700333}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800334
335static void* SignalHandlerFn(void* arg) {
336 sigset_t wait_set;
337 sigfillset(&wait_set);
338 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
339}
340
341TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700342 // Check that SIGUSR1 isn't blocked.
343 sigset_t original_set;
344 sigemptyset(&original_set);
345 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
346 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
347
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800348 // Block SIGUSR1.
349 sigset_t set;
350 sigemptyset(&set);
351 sigaddset(&set, SIGUSR1);
352 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
353
Elliott Hughes19e62322013-10-15 11:23:57 -0700354 // Check that SIGUSR1 is blocked.
355 sigset_t final_set;
356 sigemptyset(&final_set);
357 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
358 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
359 // ...and that sigprocmask agrees with pthread_sigmask.
360 sigemptyset(&final_set);
361 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
362 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
363
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800364 // Spawn a thread that calls sigwait and tells us what it received.
365 pthread_t signal_thread;
366 int received_signal = -1;
367 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
368
369 // Send that thread SIGUSR1.
370 pthread_kill(signal_thread, SIGUSR1);
371
372 // See what it got.
373 void* join_result;
374 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
375 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700376 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700377
378 // Restore the original signal mask.
379 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800380}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800381
Elliott Hughes3e898472013-02-12 16:40:24 +0000382TEST(pthread, pthread_setname_np__too_long) {
383 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
384}
385
386TEST(pthread, pthread_setname_np__self) {
387 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
388}
389
390TEST(pthread, pthread_setname_np__other) {
Yabin Cui63481602014-12-01 17:41:04 -0800391 SpinFunctionHelper spinhelper;
392
Elliott Hughesed29e852014-10-27 12:01:51 -0700393 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800394 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughesed29e852014-10-27 12:01:51 -0700395 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000396}
397
398TEST(pthread, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800399 pthread_t dead_thread;
400 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000401
402 // Call pthread_setname_np after thread has already exited.
Elliott Hughes68d98d82014-11-12 21:03:26 -0800403 ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000404}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800405
406TEST(pthread, pthread_kill__0) {
407 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
408 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
409}
410
411TEST(pthread, pthread_kill__invalid_signal) {
412 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
413}
414
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800415static void pthread_kill__in_signal_handler_helper(int signal_number) {
416 static int count = 0;
417 ASSERT_EQ(SIGALRM, signal_number);
418 if (++count == 1) {
419 // Can we call pthread_kill from a signal handler?
420 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
421 }
422}
423
424TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800425 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800426 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
427}
428
Elliott Hughes9d23e042013-02-15 19:21:51 -0800429TEST(pthread, pthread_detach__no_such_thread) {
430 pthread_t dead_thread;
431 MakeDeadThread(dead_thread);
432
433 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
434}
435
Elliott Hughes04620a32014-03-07 17:59:05 -0800436TEST(pthread, pthread_detach__leak) {
Christopher Ferrise3809602014-08-06 14:15:01 -0700437 size_t initial_bytes = 0;
438 // Run this loop more than once since the first loop causes some memory
439 // to be allocated permenantly. Run an extra loop to help catch any subtle
440 // memory leaks.
441 for (size_t loop = 0; loop < 3; loop++) {
442 // Set the initial bytes on the second loop since the memory in use
443 // should have stabilized.
444 if (loop == 1) {
445 initial_bytes = mallinfo().uordblks;
446 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800447
Christopher Ferrise3809602014-08-06 14:15:01 -0700448 pthread_attr_t attr;
449 ASSERT_EQ(0, pthread_attr_init(&attr));
450 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
Elliott Hughes04620a32014-03-07 17:59:05 -0800451
Christopher Ferrise3809602014-08-06 14:15:01 -0700452 std::vector<pthread_t> threads;
453 for (size_t i = 0; i < 32; ++i) {
454 pthread_t t;
455 ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL));
456 threads.push_back(t);
457 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800458
Christopher Ferrise3809602014-08-06 14:15:01 -0700459 sleep(1);
Elliott Hughes04620a32014-03-07 17:59:05 -0800460
Christopher Ferrise3809602014-08-06 14:15:01 -0700461 for (size_t i = 0; i < 32; ++i) {
462 ASSERT_EQ(0, pthread_detach(threads[i])) << i;
463 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800464 }
465
466 size_t final_bytes = mallinfo().uordblks;
Elliott Hughes04620a32014-03-07 17:59:05 -0800467 int leaked_bytes = (final_bytes - initial_bytes);
468
469 // User code (like this test) doesn't know how large pthread_internal_t is.
470 // We can be pretty sure it's more than 128 bytes.
471 ASSERT_LT(leaked_bytes, 32 /*threads*/ * 128 /*bytes*/);
472}
473
Jeff Hao9b06cc32013-08-15 14:51:16 -0700474TEST(pthread, pthread_getcpuclockid__clock_gettime) {
Yabin Cui63481602014-12-01 17:41:04 -0800475 SpinFunctionHelper spinhelper;
476
Jeff Hao9b06cc32013-08-15 14:51:16 -0700477 pthread_t t;
Yabin Cui63481602014-12-01 17:41:04 -0800478 ASSERT_EQ(0, pthread_create(&t, NULL, spinhelper.GetFunction(), NULL));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700479
480 clockid_t c;
481 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
482 timespec ts;
483 ASSERT_EQ(0, clock_gettime(c, &ts));
484}
485
Elliott Hughes9d23e042013-02-15 19:21:51 -0800486TEST(pthread, pthread_getcpuclockid__no_such_thread) {
487 pthread_t dead_thread;
488 MakeDeadThread(dead_thread);
489
490 clockid_t c;
491 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
492}
493
494TEST(pthread, pthread_getschedparam__no_such_thread) {
495 pthread_t dead_thread;
496 MakeDeadThread(dead_thread);
497
498 int policy;
499 sched_param param;
500 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
501}
502
503TEST(pthread, pthread_setschedparam__no_such_thread) {
504 pthread_t dead_thread;
505 MakeDeadThread(dead_thread);
506
507 int policy = 0;
508 sched_param param;
509 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
510}
511
512TEST(pthread, pthread_join__no_such_thread) {
513 pthread_t dead_thread;
514 MakeDeadThread(dead_thread);
515
Elliott Hughes34c987a2014-09-22 16:01:26 -0700516 ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800517}
518
519TEST(pthread, pthread_kill__no_such_thread) {
520 pthread_t dead_thread;
521 MakeDeadThread(dead_thread);
522
523 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
524}
msg5550f020d12013-06-06 14:59:28 -0400525
526TEST(pthread, pthread_join__multijoin) {
Yabin Cui63481602014-12-01 17:41:04 -0800527 SpinFunctionHelper spinhelper;
msg5550f020d12013-06-06 14:59:28 -0400528
529 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800530 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
msg5550f020d12013-06-06 14:59:28 -0400531
532 pthread_t t2;
533 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
534
535 sleep(1); // (Give t2 a chance to call pthread_join.)
536
537 // Multiple joins to the same thread should fail.
538 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
539
Yabin Cui63481602014-12-01 17:41:04 -0800540 spinhelper.UnSpin();
msg5550f020d12013-06-06 14:59:28 -0400541
542 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
543 void* join_result;
544 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700545 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400546}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700547
Elliott Hughes70b24b12013-11-15 11:51:07 -0800548TEST(pthread, pthread_join__race) {
549 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
550 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
551 for (size_t i = 0; i < 1024; ++i) {
552 size_t stack_size = 64*1024;
553 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
554
555 pthread_attr_t a;
556 pthread_attr_init(&a);
557 pthread_attr_setstack(&a, stack, stack_size);
558
559 pthread_t t;
560 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
561 ASSERT_EQ(0, pthread_join(t, NULL));
562 ASSERT_EQ(0, munmap(stack, stack_size));
563 }
564}
565
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700566static void* GetActualGuardSizeFn(void* arg) {
567 pthread_attr_t attributes;
568 pthread_getattr_np(pthread_self(), &attributes);
569 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
570 return NULL;
571}
572
573static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
574 size_t result;
575 pthread_t t;
576 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700577 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700578 return result;
579}
580
581static void* GetActualStackSizeFn(void* arg) {
582 pthread_attr_t attributes;
583 pthread_getattr_np(pthread_self(), &attributes);
584 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
585 return NULL;
586}
587
588static size_t GetActualStackSize(const pthread_attr_t& attributes) {
589 size_t result;
590 pthread_t t;
591 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700592 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700593 return result;
594}
595
596TEST(pthread, pthread_attr_setguardsize) {
597 pthread_attr_t attributes;
598 ASSERT_EQ(0, pthread_attr_init(&attributes));
599
600 // Get the default guard size.
601 size_t default_guard_size;
602 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
603
604 // No such thing as too small: will be rounded up to one page by pthread_create.
605 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
606 size_t guard_size;
607 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
608 ASSERT_EQ(128U, guard_size);
609 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
610
611 // Large enough and a multiple of the page size.
612 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
613 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
614 ASSERT_EQ(32*1024U, guard_size);
615
616 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
617 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
618 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
619 ASSERT_EQ(32*1024U + 1, guard_size);
620}
621
622TEST(pthread, pthread_attr_setstacksize) {
623 pthread_attr_t attributes;
624 ASSERT_EQ(0, pthread_attr_init(&attributes));
625
626 // Get the default stack size.
627 size_t default_stack_size;
628 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
629
630 // Too small.
631 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
632 size_t stack_size;
633 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
634 ASSERT_EQ(default_stack_size, stack_size);
635 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
636
637 // Large enough and a multiple of the page size.
638 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
639 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
640 ASSERT_EQ(32*1024U, stack_size);
641 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
642
643 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
644 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
645 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
646 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800647#if defined(__BIONIC__)
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700648 // Bionic rounds up, which is what POSIX allows.
649 ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800650#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700651 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
652 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800653#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700654}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700655
656TEST(pthread, pthread_rwlock_smoke) {
657 pthread_rwlock_t l;
658 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
659
Calin Juravle76f352e2014-05-19 13:41:10 +0100660 // Single read lock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700661 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
662 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
663
Calin Juravle76f352e2014-05-19 13:41:10 +0100664 // Multiple read lock
665 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
666 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
667 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
668 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
669
670 // Write lock
Calin Juravle92687e42014-05-22 19:21:22 +0100671 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
672 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100673
674 // Try writer lock
675 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
676 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
677 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
678 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
679
680 // Try reader lock
681 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
682 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
683 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
684 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
685 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
686
687 // Try writer lock after unlock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700688 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
689 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
690
Calin Juravle76f352e2014-05-19 13:41:10 +0100691#ifdef __BIONIC__
692 // EDEADLK in "read after write"
693 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
694 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
695 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
696
697 // EDEADLK in "write after write"
698 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
699 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
700 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
701#endif
702
Elliott Hughesc3f11402013-10-30 14:40:09 -0700703 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
704}
705
Elliott Hughes1728b232014-05-14 10:02:03 -0700706static int g_once_fn_call_count = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700707static void OnceFn() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700708 ++g_once_fn_call_count;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700709}
710
711TEST(pthread, pthread_once_smoke) {
712 pthread_once_t once_control = PTHREAD_ONCE_INIT;
713 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
714 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
Elliott Hughes1728b232014-05-14 10:02:03 -0700715 ASSERT_EQ(1, g_once_fn_call_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700716}
717
Elliott Hughes3694ec62014-05-14 11:46:08 -0700718static std::string pthread_once_1934122_result = "";
719
720static void Routine2() {
721 pthread_once_1934122_result += "2";
722}
723
724static void Routine1() {
725 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
726 pthread_once_1934122_result += "1";
727 pthread_once(&once_control_2, &Routine2);
728}
729
730TEST(pthread, pthread_once_1934122) {
731 // Very old versions of Android couldn't call pthread_once from a
732 // pthread_once init routine. http://b/1934122.
733 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
734 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
735 ASSERT_EQ("12", pthread_once_1934122_result);
736}
737
Elliott Hughes1728b232014-05-14 10:02:03 -0700738static int g_atfork_prepare_calls = 0;
739static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; }
740static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; }
741static int g_atfork_parent_calls = 0;
742static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; }
743static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; }
744static int g_atfork_child_calls = 0;
745static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; }
746static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700747
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800748TEST(pthread, pthread_atfork_smoke) {
Dmitriy Ivanov0864d8b2014-11-20 21:21:26 -0800749 test_isolated([] {
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800750 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
751 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700752
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800753 int pid = fork();
754 ASSERT_NE(-1, pid) << strerror(errno);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700755
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800756 // Child and parent calls are made in the order they were registered.
757 if (pid == 0) {
758 ASSERT_EQ(0x12, g_atfork_child_calls);
759 _exit(0);
760 }
761 ASSERT_EQ(0x12, g_atfork_parent_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700762
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800763 // Prepare calls are made in the reverse order.
764 ASSERT_EQ(0x21, g_atfork_prepare_calls);
765 });
Elliott Hughesc3f11402013-10-30 14:40:09 -0700766}
767
768TEST(pthread, pthread_attr_getscope) {
769 pthread_attr_t attr;
770 ASSERT_EQ(0, pthread_attr_init(&attr));
771
772 int scope;
773 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
774 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
775}
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000776
777TEST(pthread, pthread_condattr_init) {
778 pthread_condattr_t attr;
779 pthread_condattr_init(&attr);
780
781 clockid_t clock;
782 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
783 ASSERT_EQ(CLOCK_REALTIME, clock);
784
785 int pshared;
786 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
787 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
788}
789
790TEST(pthread, pthread_condattr_setclock) {
791 pthread_condattr_t attr;
792 pthread_condattr_init(&attr);
793
794 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
795 clockid_t clock;
796 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
797 ASSERT_EQ(CLOCK_REALTIME, clock);
798
799 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
800 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
801 ASSERT_EQ(CLOCK_MONOTONIC, clock);
802
803 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
804}
805
806TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
807#if defined(__BIONIC__) // This tests a bionic implementation detail.
808 pthread_condattr_t attr;
809 pthread_condattr_init(&attr);
810
811 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
812 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
813
814 pthread_cond_t cond_var;
815 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
816
817 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
818 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
819
820 attr = static_cast<pthread_condattr_t>(cond_var.value);
821 clockid_t clock;
822 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
823 ASSERT_EQ(CLOCK_MONOTONIC, clock);
824 int pshared;
825 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
826 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
827#else // __BIONIC__
828 GTEST_LOG_(INFO) << "This test does nothing.\n";
829#endif // __BIONIC__
830}
Elliott Hughes0e714a52014-03-03 16:42:47 -0800831
832TEST(pthread, pthread_mutex_timedlock) {
833 pthread_mutex_t m;
834 ASSERT_EQ(0, pthread_mutex_init(&m, NULL));
835
836 // If the mutex is already locked, pthread_mutex_timedlock should time out.
837 ASSERT_EQ(0, pthread_mutex_lock(&m));
838
839 timespec ts;
840 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
841 ts.tv_nsec += 1;
842 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
843
844 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
845 ASSERT_EQ(0, pthread_mutex_unlock(&m));
846
847 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
848 ts.tv_nsec += 1;
849 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
850
851 ASSERT_EQ(0, pthread_mutex_unlock(&m));
852 ASSERT_EQ(0, pthread_mutex_destroy(&m));
853}
Elliott Hughes57b7a612014-08-25 17:26:50 -0700854
855TEST(pthread, pthread_attr_getstack__main_thread) {
856 // This test is only meaningful for the main thread, so make sure we're running on it!
857 ASSERT_EQ(getpid(), syscall(__NR_gettid));
858
859 // Get the main thread's attributes.
860 pthread_attr_t attributes;
861 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
862
863 // Check that we correctly report that the main thread has no guard page.
864 size_t guard_size;
865 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
866 ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
867
868 // Get the stack base and the stack size (both ways).
869 void* stack_base;
870 size_t stack_size;
871 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
872 size_t stack_size2;
873 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
874
875 // The two methods of asking for the stack size should agree.
876 EXPECT_EQ(stack_size, stack_size2);
877
878 // What does /proc/self/maps' [stack] line say?
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700879 void* maps_stack_hi = NULL;
Elliott Hughes57b7a612014-08-25 17:26:50 -0700880 FILE* fp = fopen("/proc/self/maps", "r");
881 ASSERT_TRUE(fp != NULL);
882 char line[BUFSIZ];
883 while (fgets(line, sizeof(line), fp) != NULL) {
884 uintptr_t lo, hi;
885 char name[10];
886 sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name);
887 if (strcmp(name, "[stack]") == 0) {
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700888 maps_stack_hi = reinterpret_cast<void*>(hi);
Elliott Hughes57b7a612014-08-25 17:26:50 -0700889 break;
890 }
891 }
892 fclose(fp);
893
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700894 // The stack size should correspond to RLIMIT_STACK.
Elliott Hughes57b7a612014-08-25 17:26:50 -0700895 rlimit rl;
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700896 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
Elliott Hughes27a9aed2014-09-04 16:09:25 -0700897 uint64_t original_rlim_cur = rl.rlim_cur;
898#if defined(__BIONIC__)
899 if (rl.rlim_cur == RLIM_INFINITY) {
900 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
901 }
902#endif
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700903 EXPECT_EQ(rl.rlim_cur, stack_size);
904
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700905 auto guard = make_scope_guard([&rl, original_rlim_cur]() {
Elliott Hughes27a9aed2014-09-04 16:09:25 -0700906 rl.rlim_cur = original_rlim_cur;
907 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
908 });
909
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700910 // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
911 // Remember that the stack grows down (and is mapped in on demand), so the low address of the
912 // region isn't very interesting.
913 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
914
915 //
916 // What if RLIMIT_STACK is smaller than the stack's current extent?
917 //
Elliott Hughes57b7a612014-08-25 17:26:50 -0700918 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
919 rl.rlim_max = RLIM_INFINITY;
920 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
921
922 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
923 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
924 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
925
926 EXPECT_EQ(stack_size, stack_size2);
927 ASSERT_EQ(1024U, stack_size);
928
929 //
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700930 // What if RLIMIT_STACK isn't a whole number of pages?
Elliott Hughes57b7a612014-08-25 17:26:50 -0700931 //
932 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
933 rl.rlim_max = RLIM_INFINITY;
934 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
935
936 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
937 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
938 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
939
940 EXPECT_EQ(stack_size, stack_size2);
941 ASSERT_EQ(6666U, stack_size);
942}
Elliott Hughes8fb639c2014-09-12 14:43:07 -0700943
944#if defined(__BIONIC__)
945static void* pthread_gettid_np_helper(void* arg) {
946 *reinterpret_cast<pid_t*>(arg) = gettid();
947 return NULL;
948}
949#endif
950
951TEST(pthread, pthread_gettid_np) {
952#if defined(__BIONIC__)
953 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
954
955 pid_t t_gettid_result;
956 pthread_t t;
957 pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
958
959 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
960
Elliott Hughes34c987a2014-09-22 16:01:26 -0700961 pthread_join(t, NULL);
Elliott Hughes8fb639c2014-09-12 14:43:07 -0700962
963 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
964#else
965 GTEST_LOG_(INFO) << "This test does nothing.\n";
966#endif
967}
Elliott Hughes34c987a2014-09-22 16:01:26 -0700968
969static size_t cleanup_counter = 0;
970
Derek Xue41996952014-09-25 11:05:32 +0100971static void AbortCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700972 abort();
973}
974
Derek Xue41996952014-09-25 11:05:32 +0100975static void CountCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700976 ++cleanup_counter;
977}
978
Derek Xue41996952014-09-25 11:05:32 +0100979static void PthreadCleanupTester() {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700980 pthread_cleanup_push(CountCleanupRoutine, NULL);
981 pthread_cleanup_push(CountCleanupRoutine, NULL);
982 pthread_cleanup_push(AbortCleanupRoutine, NULL);
983
984 pthread_cleanup_pop(0); // Pop the abort without executing it.
985 pthread_cleanup_pop(1); // Pop one count while executing it.
986 ASSERT_EQ(1U, cleanup_counter);
987 // Exit while the other count is still on the cleanup stack.
988 pthread_exit(NULL);
989
990 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
991 pthread_cleanup_pop(0);
992}
993
Derek Xue41996952014-09-25 11:05:32 +0100994static void* PthreadCleanupStartRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700995 PthreadCleanupTester();
996 return NULL;
997}
998
999TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
1000 pthread_t t;
1001 ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL));
1002 pthread_join(t, NULL);
1003 ASSERT_EQ(2U, cleanup_counter);
1004}
Derek Xue41996952014-09-25 11:05:32 +01001005
1006TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
1007 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
1008}
1009
1010TEST(pthread, pthread_mutexattr_gettype) {
1011 pthread_mutexattr_t attr;
1012 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1013
1014 int attr_type;
1015
1016 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1017 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1018 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
1019
1020 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1021 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1022 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
1023
1024 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1025 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1026 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
1027}
1028
1029TEST(pthread, pthread_mutex_lock_NORMAL) {
1030 pthread_mutexattr_t attr;
1031 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1032 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1033
1034 pthread_mutex_t lock;
1035 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1036
1037 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1038 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1039 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1040}
1041
1042TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
1043 pthread_mutexattr_t attr;
1044 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1045 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1046
1047 pthread_mutex_t lock;
1048 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1049
1050 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1051 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&lock));
1052 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1053 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1054 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock));
1055 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1056 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1057 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1058}
1059
1060TEST(pthread, pthread_mutex_lock_RECURSIVE) {
1061 pthread_mutexattr_t attr;
1062 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1063 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1064
1065 pthread_mutex_t lock;
1066 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1067
1068 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1069 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1070 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1071 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1072 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1073 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1074 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1075 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1076}