blob: 797468efe430b050c6083b03e8366f3572714bda [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
181static void* SleepFn(void* arg) {
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700182 sleep(reinterpret_cast<uintptr_t>(arg));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700183 return NULL;
184}
185
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400186static void* SpinFn(void* arg) {
187 volatile bool* b = reinterpret_cast<volatile bool*>(arg);
188 while (!*b) {
189 }
190 return NULL;
191}
192
Elliott Hughes4d014e12012-09-07 16:47:54 -0700193static void* JoinFn(void* arg) {
194 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
195}
196
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400197static void AssertDetached(pthread_t t, bool is_detached) {
198 pthread_attr_t attr;
199 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
200 int detach_state;
201 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
202 pthread_attr_destroy(&attr);
203 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
204}
205
Elliott Hughes9d23e042013-02-15 19:21:51 -0800206static void MakeDeadThread(pthread_t& t) {
207 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
Elliott Hughes34c987a2014-09-22 16:01:26 -0700208 ASSERT_EQ(0, pthread_join(t, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800209}
210
Elliott Hughes4d014e12012-09-07 16:47:54 -0700211TEST(pthread, pthread_create) {
212 void* expected_result = reinterpret_cast<void*>(123);
213 // Can we create a thread?
214 pthread_t t;
215 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
216 // If we join, do we get the expected value back?
217 void* result;
218 ASSERT_EQ(0, pthread_join(t, &result));
219 ASSERT_EQ(expected_result, result);
220}
221
Elliott Hughes3e898472013-02-12 16:40:24 +0000222TEST(pthread, pthread_create_EAGAIN) {
223 pthread_attr_t attributes;
224 ASSERT_EQ(0, pthread_attr_init(&attributes));
225 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
226
227 pthread_t t;
228 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
229}
230
Elliott Hughes4d014e12012-09-07 16:47:54 -0700231TEST(pthread, pthread_no_join_after_detach) {
232 pthread_t t1;
233 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
234
235 // After a pthread_detach...
236 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400237 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700238
239 // ...pthread_join should fail.
Elliott Hughes34c987a2014-09-22 16:01:26 -0700240 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700241}
242
243TEST(pthread, pthread_no_op_detach_after_join) {
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400244 bool done = false;
245
Elliott Hughes4d014e12012-09-07 16:47:54 -0700246 pthread_t t1;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400247 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700248
249 // If thread 2 is already waiting to join thread 1...
250 pthread_t t2;
251 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
252
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400253 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700254
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400255 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
256 ASSERT_EQ(0, pthread_detach(t1));
257 AssertDetached(t1, false);
258
259 done = true;
260
261 // ...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 -0700262 void* join_result;
263 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700264 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700265}
Elliott Hughes14f19592012-10-29 10:19:44 -0700266
267TEST(pthread, pthread_join_self) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700268 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL));
Elliott Hughes14f19592012-10-29 10:19:44 -0700269}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700270
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800271struct TestBug37410 {
272 pthread_t main_thread;
273 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700274
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800275 static void main() {
276 TestBug37410 data;
277 data.main_thread = pthread_self();
278 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
279 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
280
281 pthread_t t;
282 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
283
284 // Wait for the thread to be running...
285 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
286 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
287
288 // ...and exit.
289 pthread_exit(NULL);
290 }
291
292 private:
293 static void* thread_fn(void* arg) {
294 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
295
296 // Let the main thread know we're running.
297 pthread_mutex_unlock(&data->mutex);
298
299 // And wait for the main thread to exit.
300 pthread_join(data->main_thread, NULL);
301
302 return NULL;
303 }
304};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700305
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800306// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
307// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800308
309class pthread_DeathTest : public BionicDeathTest {};
310
311TEST_F(pthread_DeathTest, pthread_bug_37410) {
Elliott Hughes4f251be2012-11-01 16:33:29 -0700312 // http://code.google.com/p/android/issues/detail?id=37410
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800313 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700314}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800315
316static void* SignalHandlerFn(void* arg) {
317 sigset_t wait_set;
318 sigfillset(&wait_set);
319 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
320}
321
322TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700323 // Check that SIGUSR1 isn't blocked.
324 sigset_t original_set;
325 sigemptyset(&original_set);
326 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
327 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
328
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800329 // Block SIGUSR1.
330 sigset_t set;
331 sigemptyset(&set);
332 sigaddset(&set, SIGUSR1);
333 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
334
Elliott Hughes19e62322013-10-15 11:23:57 -0700335 // Check that SIGUSR1 is blocked.
336 sigset_t final_set;
337 sigemptyset(&final_set);
338 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
339 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
340 // ...and that sigprocmask agrees with pthread_sigmask.
341 sigemptyset(&final_set);
342 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
343 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
344
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800345 // Spawn a thread that calls sigwait and tells us what it received.
346 pthread_t signal_thread;
347 int received_signal = -1;
348 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
349
350 // Send that thread SIGUSR1.
351 pthread_kill(signal_thread, SIGUSR1);
352
353 // See what it got.
354 void* join_result;
355 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
356 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700357 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700358
359 // Restore the original signal mask.
360 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800361}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800362
Elliott Hughes3e898472013-02-12 16:40:24 +0000363TEST(pthread, pthread_setname_np__too_long) {
364 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
365}
366
367TEST(pthread, pthread_setname_np__self) {
368 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
369}
370
371TEST(pthread, pthread_setname_np__other) {
Elliott Hughesed29e852014-10-27 12:01:51 -0700372 pthread_t t1;
373 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
374 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000375}
376
377TEST(pthread, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800378 pthread_t dead_thread;
379 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000380
381 // Call pthread_setname_np after thread has already exited.
Elliott Hughes68d98d82014-11-12 21:03:26 -0800382 ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000383}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800384
385TEST(pthread, pthread_kill__0) {
386 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
387 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
388}
389
390TEST(pthread, pthread_kill__invalid_signal) {
391 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
392}
393
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800394static void pthread_kill__in_signal_handler_helper(int signal_number) {
395 static int count = 0;
396 ASSERT_EQ(SIGALRM, signal_number);
397 if (++count == 1) {
398 // Can we call pthread_kill from a signal handler?
399 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
400 }
401}
402
403TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800404 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800405 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
406}
407
Elliott Hughes9d23e042013-02-15 19:21:51 -0800408TEST(pthread, pthread_detach__no_such_thread) {
409 pthread_t dead_thread;
410 MakeDeadThread(dead_thread);
411
412 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
413}
414
Elliott Hughes04620a32014-03-07 17:59:05 -0800415TEST(pthread, pthread_detach__leak) {
Christopher Ferrise3809602014-08-06 14:15:01 -0700416 size_t initial_bytes = 0;
417 // Run this loop more than once since the first loop causes some memory
418 // to be allocated permenantly. Run an extra loop to help catch any subtle
419 // memory leaks.
420 for (size_t loop = 0; loop < 3; loop++) {
421 // Set the initial bytes on the second loop since the memory in use
422 // should have stabilized.
423 if (loop == 1) {
424 initial_bytes = mallinfo().uordblks;
425 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800426
Christopher Ferrise3809602014-08-06 14:15:01 -0700427 pthread_attr_t attr;
428 ASSERT_EQ(0, pthread_attr_init(&attr));
429 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
Elliott Hughes04620a32014-03-07 17:59:05 -0800430
Christopher Ferrise3809602014-08-06 14:15:01 -0700431 std::vector<pthread_t> threads;
432 for (size_t i = 0; i < 32; ++i) {
433 pthread_t t;
434 ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL));
435 threads.push_back(t);
436 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800437
Christopher Ferrise3809602014-08-06 14:15:01 -0700438 sleep(1);
Elliott Hughes04620a32014-03-07 17:59:05 -0800439
Christopher Ferrise3809602014-08-06 14:15:01 -0700440 for (size_t i = 0; i < 32; ++i) {
441 ASSERT_EQ(0, pthread_detach(threads[i])) << i;
442 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800443 }
444
445 size_t final_bytes = mallinfo().uordblks;
Elliott Hughes04620a32014-03-07 17:59:05 -0800446 int leaked_bytes = (final_bytes - initial_bytes);
447
448 // User code (like this test) doesn't know how large pthread_internal_t is.
449 // We can be pretty sure it's more than 128 bytes.
450 ASSERT_LT(leaked_bytes, 32 /*threads*/ * 128 /*bytes*/);
451}
452
Jeff Hao9b06cc32013-08-15 14:51:16 -0700453TEST(pthread, pthread_getcpuclockid__clock_gettime) {
454 pthread_t t;
455 ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5)));
456
457 clockid_t c;
458 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
459 timespec ts;
460 ASSERT_EQ(0, clock_gettime(c, &ts));
461}
462
Elliott Hughes9d23e042013-02-15 19:21:51 -0800463TEST(pthread, pthread_getcpuclockid__no_such_thread) {
464 pthread_t dead_thread;
465 MakeDeadThread(dead_thread);
466
467 clockid_t c;
468 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
469}
470
471TEST(pthread, pthread_getschedparam__no_such_thread) {
472 pthread_t dead_thread;
473 MakeDeadThread(dead_thread);
474
475 int policy;
476 sched_param param;
477 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
478}
479
480TEST(pthread, pthread_setschedparam__no_such_thread) {
481 pthread_t dead_thread;
482 MakeDeadThread(dead_thread);
483
484 int policy = 0;
485 sched_param param;
486 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
487}
488
489TEST(pthread, pthread_join__no_such_thread) {
490 pthread_t dead_thread;
491 MakeDeadThread(dead_thread);
492
Elliott Hughes34c987a2014-09-22 16:01:26 -0700493 ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800494}
495
496TEST(pthread, pthread_kill__no_such_thread) {
497 pthread_t dead_thread;
498 MakeDeadThread(dead_thread);
499
500 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
501}
msg5550f020d12013-06-06 14:59:28 -0400502
503TEST(pthread, pthread_join__multijoin) {
504 bool done = false;
505
506 pthread_t t1;
507 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
508
509 pthread_t t2;
510 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
511
512 sleep(1); // (Give t2 a chance to call pthread_join.)
513
514 // Multiple joins to the same thread should fail.
515 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
516
517 done = true;
518
519 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
520 void* join_result;
521 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700522 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400523}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700524
Elliott Hughes70b24b12013-11-15 11:51:07 -0800525TEST(pthread, pthread_join__race) {
526 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
527 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
528 for (size_t i = 0; i < 1024; ++i) {
529 size_t stack_size = 64*1024;
530 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
531
532 pthread_attr_t a;
533 pthread_attr_init(&a);
534 pthread_attr_setstack(&a, stack, stack_size);
535
536 pthread_t t;
537 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
538 ASSERT_EQ(0, pthread_join(t, NULL));
539 ASSERT_EQ(0, munmap(stack, stack_size));
540 }
541}
542
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700543static void* GetActualGuardSizeFn(void* arg) {
544 pthread_attr_t attributes;
545 pthread_getattr_np(pthread_self(), &attributes);
546 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
547 return NULL;
548}
549
550static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
551 size_t result;
552 pthread_t t;
553 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700554 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700555 return result;
556}
557
558static void* GetActualStackSizeFn(void* arg) {
559 pthread_attr_t attributes;
560 pthread_getattr_np(pthread_self(), &attributes);
561 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
562 return NULL;
563}
564
565static size_t GetActualStackSize(const pthread_attr_t& attributes) {
566 size_t result;
567 pthread_t t;
568 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700569 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700570 return result;
571}
572
573TEST(pthread, pthread_attr_setguardsize) {
574 pthread_attr_t attributes;
575 ASSERT_EQ(0, pthread_attr_init(&attributes));
576
577 // Get the default guard size.
578 size_t default_guard_size;
579 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
580
581 // No such thing as too small: will be rounded up to one page by pthread_create.
582 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
583 size_t guard_size;
584 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
585 ASSERT_EQ(128U, guard_size);
586 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
587
588 // Large enough and a multiple of the page size.
589 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
590 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
591 ASSERT_EQ(32*1024U, guard_size);
592
593 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
594 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
595 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
596 ASSERT_EQ(32*1024U + 1, guard_size);
597}
598
599TEST(pthread, pthread_attr_setstacksize) {
600 pthread_attr_t attributes;
601 ASSERT_EQ(0, pthread_attr_init(&attributes));
602
603 // Get the default stack size.
604 size_t default_stack_size;
605 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
606
607 // Too small.
608 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
609 size_t stack_size;
610 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
611 ASSERT_EQ(default_stack_size, stack_size);
612 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
613
614 // Large enough and a multiple of the page size.
615 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
616 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
617 ASSERT_EQ(32*1024U, stack_size);
618 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
619
620 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
621 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
622 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
623 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800624#if defined(__BIONIC__)
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700625 // Bionic rounds up, which is what POSIX allows.
626 ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800627#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700628 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
629 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800630#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700631}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700632
633TEST(pthread, pthread_rwlock_smoke) {
634 pthread_rwlock_t l;
635 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
636
Calin Juravle76f352e2014-05-19 13:41:10 +0100637 // Single read lock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700638 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
639 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
640
Calin Juravle76f352e2014-05-19 13:41:10 +0100641 // Multiple read lock
642 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
643 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
644 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
645 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
646
647 // Write lock
Calin Juravle92687e42014-05-22 19:21:22 +0100648 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
649 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100650
651 // Try writer lock
652 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
653 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
654 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
655 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
656
657 // Try reader lock
658 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
659 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
660 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
661 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
662 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
663
664 // Try writer lock after unlock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700665 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
666 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
667
Calin Juravle76f352e2014-05-19 13:41:10 +0100668#ifdef __BIONIC__
669 // EDEADLK in "read after write"
670 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
671 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
672 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
673
674 // EDEADLK in "write after write"
675 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
676 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
677 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
678#endif
679
Elliott Hughesc3f11402013-10-30 14:40:09 -0700680 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
681}
682
Elliott Hughes1728b232014-05-14 10:02:03 -0700683static int g_once_fn_call_count = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700684static void OnceFn() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700685 ++g_once_fn_call_count;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700686}
687
688TEST(pthread, pthread_once_smoke) {
689 pthread_once_t once_control = PTHREAD_ONCE_INIT;
690 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
691 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
Elliott Hughes1728b232014-05-14 10:02:03 -0700692 ASSERT_EQ(1, g_once_fn_call_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700693}
694
Elliott Hughes3694ec62014-05-14 11:46:08 -0700695static std::string pthread_once_1934122_result = "";
696
697static void Routine2() {
698 pthread_once_1934122_result += "2";
699}
700
701static void Routine1() {
702 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
703 pthread_once_1934122_result += "1";
704 pthread_once(&once_control_2, &Routine2);
705}
706
707TEST(pthread, pthread_once_1934122) {
708 // Very old versions of Android couldn't call pthread_once from a
709 // pthread_once init routine. http://b/1934122.
710 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
711 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
712 ASSERT_EQ("12", pthread_once_1934122_result);
713}
714
Elliott Hughes1728b232014-05-14 10:02:03 -0700715static int g_atfork_prepare_calls = 0;
716static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; }
717static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; }
718static int g_atfork_parent_calls = 0;
719static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; }
720static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; }
721static int g_atfork_child_calls = 0;
722static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; }
723static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700724
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800725TEST(pthread, pthread_atfork_smoke) {
Dmitriy Ivanov0864d8b2014-11-20 21:21:26 -0800726 test_isolated([] {
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800727 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
728 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700729
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800730 int pid = fork();
731 ASSERT_NE(-1, pid) << strerror(errno);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700732
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800733 // Child and parent calls are made in the order they were registered.
734 if (pid == 0) {
735 ASSERT_EQ(0x12, g_atfork_child_calls);
736 _exit(0);
737 }
738 ASSERT_EQ(0x12, g_atfork_parent_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700739
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800740 // Prepare calls are made in the reverse order.
741 ASSERT_EQ(0x21, g_atfork_prepare_calls);
742 });
Elliott Hughesc3f11402013-10-30 14:40:09 -0700743}
744
745TEST(pthread, pthread_attr_getscope) {
746 pthread_attr_t attr;
747 ASSERT_EQ(0, pthread_attr_init(&attr));
748
749 int scope;
750 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
751 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
752}
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000753
754TEST(pthread, pthread_condattr_init) {
755 pthread_condattr_t attr;
756 pthread_condattr_init(&attr);
757
758 clockid_t clock;
759 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
760 ASSERT_EQ(CLOCK_REALTIME, clock);
761
762 int pshared;
763 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
764 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
765}
766
767TEST(pthread, pthread_condattr_setclock) {
768 pthread_condattr_t attr;
769 pthread_condattr_init(&attr);
770
771 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
772 clockid_t clock;
773 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
774 ASSERT_EQ(CLOCK_REALTIME, clock);
775
776 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
777 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
778 ASSERT_EQ(CLOCK_MONOTONIC, clock);
779
780 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
781}
782
783TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
784#if defined(__BIONIC__) // This tests a bionic implementation detail.
785 pthread_condattr_t attr;
786 pthread_condattr_init(&attr);
787
788 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
789 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
790
791 pthread_cond_t cond_var;
792 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
793
794 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
795 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
796
797 attr = static_cast<pthread_condattr_t>(cond_var.value);
798 clockid_t clock;
799 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
800 ASSERT_EQ(CLOCK_MONOTONIC, clock);
801 int pshared;
802 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
803 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
804#else // __BIONIC__
805 GTEST_LOG_(INFO) << "This test does nothing.\n";
806#endif // __BIONIC__
807}
Elliott Hughes0e714a52014-03-03 16:42:47 -0800808
809TEST(pthread, pthread_mutex_timedlock) {
810 pthread_mutex_t m;
811 ASSERT_EQ(0, pthread_mutex_init(&m, NULL));
812
813 // If the mutex is already locked, pthread_mutex_timedlock should time out.
814 ASSERT_EQ(0, pthread_mutex_lock(&m));
815
816 timespec ts;
817 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
818 ts.tv_nsec += 1;
819 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
820
821 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
822 ASSERT_EQ(0, pthread_mutex_unlock(&m));
823
824 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
825 ts.tv_nsec += 1;
826 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
827
828 ASSERT_EQ(0, pthread_mutex_unlock(&m));
829 ASSERT_EQ(0, pthread_mutex_destroy(&m));
830}
Elliott Hughes57b7a612014-08-25 17:26:50 -0700831
832TEST(pthread, pthread_attr_getstack__main_thread) {
833 // This test is only meaningful for the main thread, so make sure we're running on it!
834 ASSERT_EQ(getpid(), syscall(__NR_gettid));
835
836 // Get the main thread's attributes.
837 pthread_attr_t attributes;
838 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
839
840 // Check that we correctly report that the main thread has no guard page.
841 size_t guard_size;
842 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
843 ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
844
845 // Get the stack base and the stack size (both ways).
846 void* stack_base;
847 size_t stack_size;
848 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
849 size_t stack_size2;
850 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
851
852 // The two methods of asking for the stack size should agree.
853 EXPECT_EQ(stack_size, stack_size2);
854
855 // What does /proc/self/maps' [stack] line say?
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700856 void* maps_stack_hi = NULL;
Elliott Hughes57b7a612014-08-25 17:26:50 -0700857 FILE* fp = fopen("/proc/self/maps", "r");
858 ASSERT_TRUE(fp != NULL);
859 char line[BUFSIZ];
860 while (fgets(line, sizeof(line), fp) != NULL) {
861 uintptr_t lo, hi;
862 char name[10];
863 sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name);
864 if (strcmp(name, "[stack]") == 0) {
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700865 maps_stack_hi = reinterpret_cast<void*>(hi);
Elliott Hughes57b7a612014-08-25 17:26:50 -0700866 break;
867 }
868 }
869 fclose(fp);
870
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700871 // The stack size should correspond to RLIMIT_STACK.
Elliott Hughes57b7a612014-08-25 17:26:50 -0700872 rlimit rl;
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700873 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
Elliott Hughes27a9aed2014-09-04 16:09:25 -0700874 uint64_t original_rlim_cur = rl.rlim_cur;
875#if defined(__BIONIC__)
876 if (rl.rlim_cur == RLIM_INFINITY) {
877 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
878 }
879#endif
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700880 EXPECT_EQ(rl.rlim_cur, stack_size);
881
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700882 auto guard = make_scope_guard([&rl, original_rlim_cur]() {
Elliott Hughes27a9aed2014-09-04 16:09:25 -0700883 rl.rlim_cur = original_rlim_cur;
884 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
885 });
886
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700887 // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
888 // Remember that the stack grows down (and is mapped in on demand), so the low address of the
889 // region isn't very interesting.
890 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
891
892 //
893 // What if RLIMIT_STACK is smaller than the stack's current extent?
894 //
Elliott Hughes57b7a612014-08-25 17:26:50 -0700895 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
896 rl.rlim_max = RLIM_INFINITY;
897 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
898
899 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
900 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
901 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
902
903 EXPECT_EQ(stack_size, stack_size2);
904 ASSERT_EQ(1024U, stack_size);
905
906 //
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700907 // What if RLIMIT_STACK isn't a whole number of pages?
Elliott Hughes57b7a612014-08-25 17:26:50 -0700908 //
909 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
910 rl.rlim_max = RLIM_INFINITY;
911 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
912
913 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
914 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
915 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
916
917 EXPECT_EQ(stack_size, stack_size2);
918 ASSERT_EQ(6666U, stack_size);
919}
Elliott Hughes8fb639c2014-09-12 14:43:07 -0700920
921#if defined(__BIONIC__)
922static void* pthread_gettid_np_helper(void* arg) {
923 *reinterpret_cast<pid_t*>(arg) = gettid();
924 return NULL;
925}
926#endif
927
928TEST(pthread, pthread_gettid_np) {
929#if defined(__BIONIC__)
930 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
931
932 pid_t t_gettid_result;
933 pthread_t t;
934 pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
935
936 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
937
Elliott Hughes34c987a2014-09-22 16:01:26 -0700938 pthread_join(t, NULL);
Elliott Hughes8fb639c2014-09-12 14:43:07 -0700939
940 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
941#else
942 GTEST_LOG_(INFO) << "This test does nothing.\n";
943#endif
944}
Elliott Hughes34c987a2014-09-22 16:01:26 -0700945
946static size_t cleanup_counter = 0;
947
Derek Xue41996952014-09-25 11:05:32 +0100948static void AbortCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700949 abort();
950}
951
Derek Xue41996952014-09-25 11:05:32 +0100952static void CountCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700953 ++cleanup_counter;
954}
955
Derek Xue41996952014-09-25 11:05:32 +0100956static void PthreadCleanupTester() {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700957 pthread_cleanup_push(CountCleanupRoutine, NULL);
958 pthread_cleanup_push(CountCleanupRoutine, NULL);
959 pthread_cleanup_push(AbortCleanupRoutine, NULL);
960
961 pthread_cleanup_pop(0); // Pop the abort without executing it.
962 pthread_cleanup_pop(1); // Pop one count while executing it.
963 ASSERT_EQ(1U, cleanup_counter);
964 // Exit while the other count is still on the cleanup stack.
965 pthread_exit(NULL);
966
967 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
968 pthread_cleanup_pop(0);
969}
970
Derek Xue41996952014-09-25 11:05:32 +0100971static void* PthreadCleanupStartRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700972 PthreadCleanupTester();
973 return NULL;
974}
975
976TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
977 pthread_t t;
978 ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL));
979 pthread_join(t, NULL);
980 ASSERT_EQ(2U, cleanup_counter);
981}
Derek Xue41996952014-09-25 11:05:32 +0100982
983TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
984 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
985}
986
987TEST(pthread, pthread_mutexattr_gettype) {
988 pthread_mutexattr_t attr;
989 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
990
991 int attr_type;
992
993 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
994 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
995 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
996
997 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
998 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
999 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
1000
1001 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1002 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1003 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
1004}
1005
1006TEST(pthread, pthread_mutex_lock_NORMAL) {
1007 pthread_mutexattr_t attr;
1008 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1009 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1010
1011 pthread_mutex_t lock;
1012 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1013
1014 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1015 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1016 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1017}
1018
1019TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
1020 pthread_mutexattr_t attr;
1021 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1022 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1023
1024 pthread_mutex_t lock;
1025 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1026
1027 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1028 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&lock));
1029 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1030 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1031 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock));
1032 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1033 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1034 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1035}
1036
1037TEST(pthread, pthread_mutex_lock_RECURSIVE) {
1038 pthread_mutexattr_t attr;
1039 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1040 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1041
1042 pthread_mutex_t lock;
1043 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1044
1045 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1046 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1047 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1048 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1049 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1050 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1051 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1052 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1053}