blob: 4fc5bed51fcaf1d7773d08ef8f95aaaa1f569814 [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) {
Yabin Cui6c238f22014-12-11 20:50:41 -080045 // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX.
46 ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070047}
Elliott Hughes718a5b52014-01-28 17:02:03 -080048
Yabin Cui6c238f22014-12-11 20:50:41 -080049TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) {
Dan Albertc4bcc752014-09-30 11:48:24 -070050 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
Yabin Cui6c238f22014-12-11 20:50:41 -080051 ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070052}
53
54TEST(pthread, pthread_key_many_distinct) {
Yabin Cui6c238f22014-12-11 20:50:41 -080055 // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX
56 // pthread keys, but We should be able to allocate at least this many keys.
57 int nkeys = PTHREAD_KEYS_MAX / 2;
Dan Albertc4bcc752014-09-30 11:48:24 -070058 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
Yabin Cui6c238f22014-12-11 20:50:41 -080083TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000084 std::vector<pthread_key_t> keys;
Dan Albertc4bcc752014-09-30 11:48:24 -070085 int rv = 0;
Yabin Cui6c238f22014-12-11 20:50:41 -080086
87 // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should
88 // be more than we are allowed to allocate now.
89 for (int i = 0; i < PTHREAD_KEYS_MAX; i++) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000090 pthread_key_t key;
Dan Albertc4bcc752014-09-30 11:48:24 -070091 rv = pthread_key_create(&key, NULL);
92 if (rv == EAGAIN) {
93 break;
94 }
95 EXPECT_EQ(0, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +000096 keys.push_back(key);
97 }
98
Dan Albertc4bcc752014-09-30 11:48:24 -070099 // Don't leak keys.
100 for (auto key : keys) {
101 EXPECT_EQ(0, pthread_key_delete(key));
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000102 }
Dan Albertc4bcc752014-09-30 11:48:24 -0700103 keys.clear();
104
105 // We should have eventually reached the maximum number of keys and received
106 // EAGAIN.
107 ASSERT_EQ(EAGAIN, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000108}
109
Elliott Hughesebb770f2014-06-25 13:46:46 -0700110TEST(pthread, pthread_key_delete) {
111 void* expected = reinterpret_cast<void*>(1234);
112 pthread_key_t key;
113 ASSERT_EQ(0, pthread_key_create(&key, NULL));
114 ASSERT_EQ(0, pthread_setspecific(key, expected));
115 ASSERT_EQ(expected, pthread_getspecific(key));
116 ASSERT_EQ(0, pthread_key_delete(key));
117 // After deletion, pthread_getspecific returns NULL.
118 ASSERT_EQ(NULL, pthread_getspecific(key));
119 // And you can't use pthread_setspecific with the deleted key.
120 ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
121}
122
Elliott Hughes40a52172014-07-30 14:48:10 -0700123TEST(pthread, pthread_key_fork) {
124 void* expected = reinterpret_cast<void*>(1234);
125 pthread_key_t key;
126 ASSERT_EQ(0, pthread_key_create(&key, NULL));
127 ASSERT_EQ(0, pthread_setspecific(key, expected));
128 ASSERT_EQ(expected, pthread_getspecific(key));
129
130 pid_t pid = fork();
131 ASSERT_NE(-1, pid) << strerror(errno);
132
133 if (pid == 0) {
134 // The surviving thread inherits all the forking thread's TLS values...
135 ASSERT_EQ(expected, pthread_getspecific(key));
136 _exit(99);
137 }
138
139 int status;
140 ASSERT_EQ(pid, waitpid(pid, &status, 0));
141 ASSERT_TRUE(WIFEXITED(status));
142 ASSERT_EQ(99, WEXITSTATUS(status));
143
144 ASSERT_EQ(expected, pthread_getspecific(key));
Dan Albert1d53ae22014-09-02 15:24:26 -0700145 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700146}
147
148static void* DirtyKeyFn(void* key) {
149 return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
150}
151
152TEST(pthread, pthread_key_dirty) {
153 pthread_key_t key;
154 ASSERT_EQ(0, pthread_key_create(&key, NULL));
155
156 size_t stack_size = 128 * 1024;
157 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
158 ASSERT_NE(MAP_FAILED, stack);
159 memset(stack, 0xff, stack_size);
160
161 pthread_attr_t attr;
162 ASSERT_EQ(0, pthread_attr_init(&attr));
163 ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
164
165 pthread_t t;
166 ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
167
168 void* result;
169 ASSERT_EQ(0, pthread_join(t, &result));
170 ASSERT_EQ(nullptr, result); // Not ~0!
171
172 ASSERT_EQ(0, munmap(stack, stack_size));
Dan Albert1d53ae22014-09-02 15:24:26 -0700173 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700174}
175
Elliott Hughes4d014e12012-09-07 16:47:54 -0700176static void* IdFn(void* arg) {
177 return arg;
178}
179
Yabin Cui63481602014-12-01 17:41:04 -0800180class SpinFunctionHelper {
181 public:
182 SpinFunctionHelper() {
183 SpinFunctionHelper::spin_flag_ = true;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400184 }
Yabin Cui63481602014-12-01 17:41:04 -0800185 ~SpinFunctionHelper() {
186 UnSpin();
187 }
188 auto GetFunction() -> void* (*)(void*) {
189 return SpinFunctionHelper::SpinFn;
190 }
191
192 void UnSpin() {
193 SpinFunctionHelper::spin_flag_ = false;
194 }
195
196 private:
197 static void* SpinFn(void*) {
198 while (spin_flag_) {}
199 return NULL;
200 }
201 static volatile bool spin_flag_;
202};
203
204// It doesn't matter if spin_flag_ is used in several tests,
205// because it is always set to false after each test. Each thread
206// loops on spin_flag_ can find it becomes false at some time.
207volatile bool SpinFunctionHelper::spin_flag_ = false;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400208
Elliott Hughes4d014e12012-09-07 16:47:54 -0700209static void* JoinFn(void* arg) {
210 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
211}
212
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400213static void AssertDetached(pthread_t t, bool is_detached) {
214 pthread_attr_t attr;
215 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
216 int detach_state;
217 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
218 pthread_attr_destroy(&attr);
219 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
220}
221
Elliott Hughes9d23e042013-02-15 19:21:51 -0800222static void MakeDeadThread(pthread_t& t) {
223 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
Elliott Hughes34c987a2014-09-22 16:01:26 -0700224 ASSERT_EQ(0, pthread_join(t, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800225}
226
Elliott Hughes4d014e12012-09-07 16:47:54 -0700227TEST(pthread, pthread_create) {
228 void* expected_result = reinterpret_cast<void*>(123);
229 // Can we create a thread?
230 pthread_t t;
231 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
232 // If we join, do we get the expected value back?
233 void* result;
234 ASSERT_EQ(0, pthread_join(t, &result));
235 ASSERT_EQ(expected_result, result);
236}
237
Elliott Hughes3e898472013-02-12 16:40:24 +0000238TEST(pthread, pthread_create_EAGAIN) {
239 pthread_attr_t attributes;
240 ASSERT_EQ(0, pthread_attr_init(&attributes));
241 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
242
243 pthread_t t;
244 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
245}
246
Elliott Hughes4d014e12012-09-07 16:47:54 -0700247TEST(pthread, pthread_no_join_after_detach) {
Yabin Cui63481602014-12-01 17:41:04 -0800248 SpinFunctionHelper spinhelper;
249
Elliott Hughes4d014e12012-09-07 16:47:54 -0700250 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800251 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700252
253 // After a pthread_detach...
254 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400255 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700256
257 // ...pthread_join should fail.
Elliott Hughes34c987a2014-09-22 16:01:26 -0700258 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700259}
260
261TEST(pthread, pthread_no_op_detach_after_join) {
Yabin Cui63481602014-12-01 17:41:04 -0800262 SpinFunctionHelper spinhelper;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400263
Elliott Hughes4d014e12012-09-07 16:47:54 -0700264 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800265 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700266
267 // If thread 2 is already waiting to join thread 1...
268 pthread_t t2;
269 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
270
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400271 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700272
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400273 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
274 ASSERT_EQ(0, pthread_detach(t1));
275 AssertDetached(t1, false);
276
Yabin Cui63481602014-12-01 17:41:04 -0800277 spinhelper.UnSpin();
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400278
279 // ...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 -0700280 void* join_result;
281 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700282 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700283}
Elliott Hughes14f19592012-10-29 10:19:44 -0700284
285TEST(pthread, pthread_join_self) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700286 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL));
Elliott Hughes14f19592012-10-29 10:19:44 -0700287}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700288
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800289struct TestBug37410 {
290 pthread_t main_thread;
291 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700292
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800293 static void main() {
294 TestBug37410 data;
295 data.main_thread = pthread_self();
296 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
297 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
298
299 pthread_t t;
300 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
301
302 // Wait for the thread to be running...
303 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
304 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
305
306 // ...and exit.
307 pthread_exit(NULL);
308 }
309
310 private:
311 static void* thread_fn(void* arg) {
312 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
313
314 // Let the main thread know we're running.
315 pthread_mutex_unlock(&data->mutex);
316
317 // And wait for the main thread to exit.
318 pthread_join(data->main_thread, NULL);
319
320 return NULL;
321 }
322};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700323
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800324// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
325// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800326
327class pthread_DeathTest : public BionicDeathTest {};
328
329TEST_F(pthread_DeathTest, pthread_bug_37410) {
Elliott Hughes4f251be2012-11-01 16:33:29 -0700330 // http://code.google.com/p/android/issues/detail?id=37410
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800331 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700332}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800333
334static void* SignalHandlerFn(void* arg) {
335 sigset_t wait_set;
336 sigfillset(&wait_set);
337 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
338}
339
340TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700341 // Check that SIGUSR1 isn't blocked.
342 sigset_t original_set;
343 sigemptyset(&original_set);
344 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
345 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
346
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800347 // Block SIGUSR1.
348 sigset_t set;
349 sigemptyset(&set);
350 sigaddset(&set, SIGUSR1);
351 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
352
Elliott Hughes19e62322013-10-15 11:23:57 -0700353 // Check that SIGUSR1 is blocked.
354 sigset_t final_set;
355 sigemptyset(&final_set);
356 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
357 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
358 // ...and that sigprocmask agrees with pthread_sigmask.
359 sigemptyset(&final_set);
360 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
361 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
362
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800363 // Spawn a thread that calls sigwait and tells us what it received.
364 pthread_t signal_thread;
365 int received_signal = -1;
366 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
367
368 // Send that thread SIGUSR1.
369 pthread_kill(signal_thread, SIGUSR1);
370
371 // See what it got.
372 void* join_result;
373 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
374 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700375 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700376
377 // Restore the original signal mask.
378 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800379}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800380
Elliott Hughes3e898472013-02-12 16:40:24 +0000381TEST(pthread, pthread_setname_np__too_long) {
382 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
383}
384
385TEST(pthread, pthread_setname_np__self) {
386 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
387}
388
389TEST(pthread, pthread_setname_np__other) {
Yabin Cui63481602014-12-01 17:41:04 -0800390 SpinFunctionHelper spinhelper;
391
Elliott Hughesed29e852014-10-27 12:01:51 -0700392 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800393 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughesed29e852014-10-27 12:01:51 -0700394 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000395}
396
397TEST(pthread, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800398 pthread_t dead_thread;
399 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000400
401 // Call pthread_setname_np after thread has already exited.
Elliott Hughes68d98d82014-11-12 21:03:26 -0800402 ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000403}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800404
405TEST(pthread, pthread_kill__0) {
406 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
407 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
408}
409
410TEST(pthread, pthread_kill__invalid_signal) {
411 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
412}
413
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800414static void pthread_kill__in_signal_handler_helper(int signal_number) {
415 static int count = 0;
416 ASSERT_EQ(SIGALRM, signal_number);
417 if (++count == 1) {
418 // Can we call pthread_kill from a signal handler?
419 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
420 }
421}
422
423TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800424 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800425 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
426}
427
Elliott Hughes9d23e042013-02-15 19:21:51 -0800428TEST(pthread, pthread_detach__no_such_thread) {
429 pthread_t dead_thread;
430 MakeDeadThread(dead_thread);
431
432 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
433}
434
Elliott Hughes04620a32014-03-07 17:59:05 -0800435TEST(pthread, pthread_detach__leak) {
Christopher Ferrise3809602014-08-06 14:15:01 -0700436 size_t initial_bytes = 0;
437 // Run this loop more than once since the first loop causes some memory
438 // to be allocated permenantly. Run an extra loop to help catch any subtle
439 // memory leaks.
440 for (size_t loop = 0; loop < 3; loop++) {
441 // Set the initial bytes on the second loop since the memory in use
442 // should have stabilized.
443 if (loop == 1) {
444 initial_bytes = mallinfo().uordblks;
445 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800446
Christopher Ferrise3809602014-08-06 14:15:01 -0700447 pthread_attr_t attr;
448 ASSERT_EQ(0, pthread_attr_init(&attr));
449 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
Elliott Hughes04620a32014-03-07 17:59:05 -0800450
Christopher Ferrise3809602014-08-06 14:15:01 -0700451 std::vector<pthread_t> threads;
452 for (size_t i = 0; i < 32; ++i) {
453 pthread_t t;
454 ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL));
455 threads.push_back(t);
456 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800457
Christopher Ferrise3809602014-08-06 14:15:01 -0700458 sleep(1);
Elliott Hughes04620a32014-03-07 17:59:05 -0800459
Christopher Ferrise3809602014-08-06 14:15:01 -0700460 for (size_t i = 0; i < 32; ++i) {
461 ASSERT_EQ(0, pthread_detach(threads[i])) << i;
462 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800463 }
464
465 size_t final_bytes = mallinfo().uordblks;
Elliott Hughes04620a32014-03-07 17:59:05 -0800466 int leaked_bytes = (final_bytes - initial_bytes);
467
468 // User code (like this test) doesn't know how large pthread_internal_t is.
469 // We can be pretty sure it's more than 128 bytes.
470 ASSERT_LT(leaked_bytes, 32 /*threads*/ * 128 /*bytes*/);
471}
472
Jeff Hao9b06cc32013-08-15 14:51:16 -0700473TEST(pthread, pthread_getcpuclockid__clock_gettime) {
Yabin Cui63481602014-12-01 17:41:04 -0800474 SpinFunctionHelper spinhelper;
475
Jeff Hao9b06cc32013-08-15 14:51:16 -0700476 pthread_t t;
Yabin Cui63481602014-12-01 17:41:04 -0800477 ASSERT_EQ(0, pthread_create(&t, NULL, spinhelper.GetFunction(), NULL));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700478
479 clockid_t c;
480 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
481 timespec ts;
482 ASSERT_EQ(0, clock_gettime(c, &ts));
483}
484
Elliott Hughes9d23e042013-02-15 19:21:51 -0800485TEST(pthread, pthread_getcpuclockid__no_such_thread) {
486 pthread_t dead_thread;
487 MakeDeadThread(dead_thread);
488
489 clockid_t c;
490 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
491}
492
493TEST(pthread, pthread_getschedparam__no_such_thread) {
494 pthread_t dead_thread;
495 MakeDeadThread(dead_thread);
496
497 int policy;
498 sched_param param;
499 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
500}
501
502TEST(pthread, pthread_setschedparam__no_such_thread) {
503 pthread_t dead_thread;
504 MakeDeadThread(dead_thread);
505
506 int policy = 0;
507 sched_param param;
508 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
509}
510
511TEST(pthread, pthread_join__no_such_thread) {
512 pthread_t dead_thread;
513 MakeDeadThread(dead_thread);
514
Elliott Hughes34c987a2014-09-22 16:01:26 -0700515 ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800516}
517
518TEST(pthread, pthread_kill__no_such_thread) {
519 pthread_t dead_thread;
520 MakeDeadThread(dead_thread);
521
522 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
523}
msg5550f020d12013-06-06 14:59:28 -0400524
525TEST(pthread, pthread_join__multijoin) {
Yabin Cui63481602014-12-01 17:41:04 -0800526 SpinFunctionHelper spinhelper;
msg5550f020d12013-06-06 14:59:28 -0400527
528 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800529 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
msg5550f020d12013-06-06 14:59:28 -0400530
531 pthread_t t2;
532 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
533
534 sleep(1); // (Give t2 a chance to call pthread_join.)
535
536 // Multiple joins to the same thread should fail.
537 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
538
Yabin Cui63481602014-12-01 17:41:04 -0800539 spinhelper.UnSpin();
msg5550f020d12013-06-06 14:59:28 -0400540
541 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
542 void* join_result;
543 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700544 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400545}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700546
Elliott Hughes70b24b12013-11-15 11:51:07 -0800547TEST(pthread, pthread_join__race) {
548 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
549 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
550 for (size_t i = 0; i < 1024; ++i) {
551 size_t stack_size = 64*1024;
552 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
553
554 pthread_attr_t a;
555 pthread_attr_init(&a);
556 pthread_attr_setstack(&a, stack, stack_size);
557
558 pthread_t t;
559 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
560 ASSERT_EQ(0, pthread_join(t, NULL));
561 ASSERT_EQ(0, munmap(stack, stack_size));
562 }
563}
564
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700565static void* GetActualGuardSizeFn(void* arg) {
566 pthread_attr_t attributes;
567 pthread_getattr_np(pthread_self(), &attributes);
568 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
569 return NULL;
570}
571
572static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
573 size_t result;
574 pthread_t t;
575 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700576 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700577 return result;
578}
579
580static void* GetActualStackSizeFn(void* arg) {
581 pthread_attr_t attributes;
582 pthread_getattr_np(pthread_self(), &attributes);
583 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
584 return NULL;
585}
586
587static size_t GetActualStackSize(const pthread_attr_t& attributes) {
588 size_t result;
589 pthread_t t;
590 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700591 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700592 return result;
593}
594
595TEST(pthread, pthread_attr_setguardsize) {
596 pthread_attr_t attributes;
597 ASSERT_EQ(0, pthread_attr_init(&attributes));
598
599 // Get the default guard size.
600 size_t default_guard_size;
601 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
602
603 // No such thing as too small: will be rounded up to one page by pthread_create.
604 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
605 size_t guard_size;
606 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
607 ASSERT_EQ(128U, guard_size);
608 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
609
610 // Large enough and a multiple of the page size.
611 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
612 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
613 ASSERT_EQ(32*1024U, guard_size);
614
615 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
616 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
617 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
618 ASSERT_EQ(32*1024U + 1, guard_size);
619}
620
621TEST(pthread, pthread_attr_setstacksize) {
622 pthread_attr_t attributes;
623 ASSERT_EQ(0, pthread_attr_init(&attributes));
624
625 // Get the default stack size.
626 size_t default_stack_size;
627 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
628
629 // Too small.
630 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
631 size_t stack_size;
632 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
633 ASSERT_EQ(default_stack_size, stack_size);
634 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
635
636 // Large enough and a multiple of the page size.
637 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
638 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
639 ASSERT_EQ(32*1024U, stack_size);
640 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
641
642 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
643 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
644 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
645 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800646#if defined(__BIONIC__)
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700647 // Bionic rounds up, which is what POSIX allows.
648 ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800649#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700650 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
651 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800652#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700653}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700654
655TEST(pthread, pthread_rwlock_smoke) {
656 pthread_rwlock_t l;
657 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
658
Calin Juravle76f352e2014-05-19 13:41:10 +0100659 // Single read lock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700660 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
661 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
662
Calin Juravle76f352e2014-05-19 13:41:10 +0100663 // Multiple read lock
664 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
665 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
666 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
667 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
668
669 // Write lock
Calin Juravle92687e42014-05-22 19:21:22 +0100670 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
671 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100672
673 // Try writer lock
674 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
675 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
676 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
677 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
678
679 // Try reader lock
680 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
681 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
682 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
683 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
684 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
685
686 // Try writer lock after unlock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700687 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
688 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
689
Calin Juravle76f352e2014-05-19 13:41:10 +0100690#ifdef __BIONIC__
691 // EDEADLK in "read after write"
692 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
693 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
694 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
695
696 // EDEADLK in "write after write"
697 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
698 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
699 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
700#endif
701
Elliott Hughesc3f11402013-10-30 14:40:09 -0700702 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
703}
704
Elliott Hughes1728b232014-05-14 10:02:03 -0700705static int g_once_fn_call_count = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700706static void OnceFn() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700707 ++g_once_fn_call_count;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700708}
709
710TEST(pthread, pthread_once_smoke) {
711 pthread_once_t once_control = PTHREAD_ONCE_INIT;
712 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
713 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
Elliott Hughes1728b232014-05-14 10:02:03 -0700714 ASSERT_EQ(1, g_once_fn_call_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700715}
716
Elliott Hughes3694ec62014-05-14 11:46:08 -0700717static std::string pthread_once_1934122_result = "";
718
719static void Routine2() {
720 pthread_once_1934122_result += "2";
721}
722
723static void Routine1() {
724 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
725 pthread_once_1934122_result += "1";
726 pthread_once(&once_control_2, &Routine2);
727}
728
729TEST(pthread, pthread_once_1934122) {
730 // Very old versions of Android couldn't call pthread_once from a
731 // pthread_once init routine. http://b/1934122.
732 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
733 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
734 ASSERT_EQ("12", pthread_once_1934122_result);
735}
736
Elliott Hughes1728b232014-05-14 10:02:03 -0700737static int g_atfork_prepare_calls = 0;
738static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; }
739static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; }
740static int g_atfork_parent_calls = 0;
741static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; }
742static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; }
743static int g_atfork_child_calls = 0;
744static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; }
745static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700746
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800747TEST(pthread, pthread_atfork_smoke) {
Dmitriy Ivanov0864d8b2014-11-20 21:21:26 -0800748 test_isolated([] {
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800749 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
750 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700751
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800752 int pid = fork();
753 ASSERT_NE(-1, pid) << strerror(errno);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700754
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800755 // Child and parent calls are made in the order they were registered.
756 if (pid == 0) {
757 ASSERT_EQ(0x12, g_atfork_child_calls);
758 _exit(0);
759 }
760 ASSERT_EQ(0x12, g_atfork_parent_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700761
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800762 // Prepare calls are made in the reverse order.
763 ASSERT_EQ(0x21, g_atfork_prepare_calls);
764 });
Elliott Hughesc3f11402013-10-30 14:40:09 -0700765}
766
767TEST(pthread, pthread_attr_getscope) {
768 pthread_attr_t attr;
769 ASSERT_EQ(0, pthread_attr_init(&attr));
770
771 int scope;
772 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
773 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
774}
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000775
776TEST(pthread, pthread_condattr_init) {
777 pthread_condattr_t attr;
778 pthread_condattr_init(&attr);
779
780 clockid_t clock;
781 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
782 ASSERT_EQ(CLOCK_REALTIME, clock);
783
784 int pshared;
785 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
786 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
787}
788
789TEST(pthread, pthread_condattr_setclock) {
790 pthread_condattr_t attr;
791 pthread_condattr_init(&attr);
792
793 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
794 clockid_t clock;
795 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
796 ASSERT_EQ(CLOCK_REALTIME, clock);
797
798 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
799 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
800 ASSERT_EQ(CLOCK_MONOTONIC, clock);
801
802 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
803}
804
805TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
806#if defined(__BIONIC__) // This tests a bionic implementation detail.
807 pthread_condattr_t attr;
808 pthread_condattr_init(&attr);
809
810 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
811 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
812
813 pthread_cond_t cond_var;
814 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
815
816 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
817 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
818
819 attr = static_cast<pthread_condattr_t>(cond_var.value);
820 clockid_t clock;
821 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
822 ASSERT_EQ(CLOCK_MONOTONIC, clock);
823 int pshared;
824 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
825 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
826#else // __BIONIC__
827 GTEST_LOG_(INFO) << "This test does nothing.\n";
828#endif // __BIONIC__
829}
Elliott Hughes0e714a52014-03-03 16:42:47 -0800830
831TEST(pthread, pthread_mutex_timedlock) {
832 pthread_mutex_t m;
833 ASSERT_EQ(0, pthread_mutex_init(&m, NULL));
834
835 // If the mutex is already locked, pthread_mutex_timedlock should time out.
836 ASSERT_EQ(0, pthread_mutex_lock(&m));
837
838 timespec ts;
839 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
840 ts.tv_nsec += 1;
841 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
842
843 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
844 ASSERT_EQ(0, pthread_mutex_unlock(&m));
845
846 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
847 ts.tv_nsec += 1;
848 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
849
850 ASSERT_EQ(0, pthread_mutex_unlock(&m));
851 ASSERT_EQ(0, pthread_mutex_destroy(&m));
852}
Elliott Hughes57b7a612014-08-25 17:26:50 -0700853
854TEST(pthread, pthread_attr_getstack__main_thread) {
855 // This test is only meaningful for the main thread, so make sure we're running on it!
856 ASSERT_EQ(getpid(), syscall(__NR_gettid));
857
858 // Get the main thread's attributes.
859 pthread_attr_t attributes;
860 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
861
862 // Check that we correctly report that the main thread has no guard page.
863 size_t guard_size;
864 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
865 ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
866
867 // Get the stack base and the stack size (both ways).
868 void* stack_base;
869 size_t stack_size;
870 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
871 size_t stack_size2;
872 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
873
874 // The two methods of asking for the stack size should agree.
875 EXPECT_EQ(stack_size, stack_size2);
876
877 // What does /proc/self/maps' [stack] line say?
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700878 void* maps_stack_hi = NULL;
Elliott Hughes57b7a612014-08-25 17:26:50 -0700879 FILE* fp = fopen("/proc/self/maps", "r");
880 ASSERT_TRUE(fp != NULL);
881 char line[BUFSIZ];
882 while (fgets(line, sizeof(line), fp) != NULL) {
883 uintptr_t lo, hi;
884 char name[10];
885 sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name);
886 if (strcmp(name, "[stack]") == 0) {
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700887 maps_stack_hi = reinterpret_cast<void*>(hi);
Elliott Hughes57b7a612014-08-25 17:26:50 -0700888 break;
889 }
890 }
891 fclose(fp);
892
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700893 // The stack size should correspond to RLIMIT_STACK.
Elliott Hughes57b7a612014-08-25 17:26:50 -0700894 rlimit rl;
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700895 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
Elliott Hughes27a9aed2014-09-04 16:09:25 -0700896 uint64_t original_rlim_cur = rl.rlim_cur;
897#if defined(__BIONIC__)
898 if (rl.rlim_cur == RLIM_INFINITY) {
899 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
900 }
901#endif
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700902 EXPECT_EQ(rl.rlim_cur, stack_size);
903
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700904 auto guard = make_scope_guard([&rl, original_rlim_cur]() {
Elliott Hughes27a9aed2014-09-04 16:09:25 -0700905 rl.rlim_cur = original_rlim_cur;
906 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
907 });
908
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700909 // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
910 // Remember that the stack grows down (and is mapped in on demand), so the low address of the
911 // region isn't very interesting.
912 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
913
914 //
915 // What if RLIMIT_STACK is smaller than the stack's current extent?
916 //
Elliott Hughes57b7a612014-08-25 17:26:50 -0700917 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
918 rl.rlim_max = RLIM_INFINITY;
919 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
920
921 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
922 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
923 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
924
925 EXPECT_EQ(stack_size, stack_size2);
926 ASSERT_EQ(1024U, stack_size);
927
928 //
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700929 // What if RLIMIT_STACK isn't a whole number of pages?
Elliott Hughes57b7a612014-08-25 17:26:50 -0700930 //
931 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
932 rl.rlim_max = RLIM_INFINITY;
933 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
934
935 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
936 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
937 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
938
939 EXPECT_EQ(stack_size, stack_size2);
940 ASSERT_EQ(6666U, stack_size);
941}
Elliott Hughes8fb639c2014-09-12 14:43:07 -0700942
943#if defined(__BIONIC__)
944static void* pthread_gettid_np_helper(void* arg) {
945 *reinterpret_cast<pid_t*>(arg) = gettid();
946 return NULL;
947}
948#endif
949
950TEST(pthread, pthread_gettid_np) {
951#if defined(__BIONIC__)
952 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
953
954 pid_t t_gettid_result;
955 pthread_t t;
956 pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
957
958 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
959
Elliott Hughes34c987a2014-09-22 16:01:26 -0700960 pthread_join(t, NULL);
Elliott Hughes8fb639c2014-09-12 14:43:07 -0700961
962 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
963#else
964 GTEST_LOG_(INFO) << "This test does nothing.\n";
965#endif
966}
Elliott Hughes34c987a2014-09-22 16:01:26 -0700967
968static size_t cleanup_counter = 0;
969
Derek Xue41996952014-09-25 11:05:32 +0100970static void AbortCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700971 abort();
972}
973
Derek Xue41996952014-09-25 11:05:32 +0100974static void CountCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700975 ++cleanup_counter;
976}
977
Derek Xue41996952014-09-25 11:05:32 +0100978static void PthreadCleanupTester() {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700979 pthread_cleanup_push(CountCleanupRoutine, NULL);
980 pthread_cleanup_push(CountCleanupRoutine, NULL);
981 pthread_cleanup_push(AbortCleanupRoutine, NULL);
982
983 pthread_cleanup_pop(0); // Pop the abort without executing it.
984 pthread_cleanup_pop(1); // Pop one count while executing it.
985 ASSERT_EQ(1U, cleanup_counter);
986 // Exit while the other count is still on the cleanup stack.
987 pthread_exit(NULL);
988
989 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
990 pthread_cleanup_pop(0);
991}
992
Derek Xue41996952014-09-25 11:05:32 +0100993static void* PthreadCleanupStartRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700994 PthreadCleanupTester();
995 return NULL;
996}
997
998TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
999 pthread_t t;
1000 ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL));
1001 pthread_join(t, NULL);
1002 ASSERT_EQ(2U, cleanup_counter);
1003}
Derek Xue41996952014-09-25 11:05:32 +01001004
1005TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
1006 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
1007}
1008
1009TEST(pthread, pthread_mutexattr_gettype) {
1010 pthread_mutexattr_t attr;
1011 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1012
1013 int attr_type;
1014
1015 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1016 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1017 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
1018
1019 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1020 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1021 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
1022
1023 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1024 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1025 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
1026}
1027
1028TEST(pthread, pthread_mutex_lock_NORMAL) {
1029 pthread_mutexattr_t attr;
1030 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1031 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1032
1033 pthread_mutex_t lock;
1034 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1035
1036 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1037 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1038 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1039}
1040
1041TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
1042 pthread_mutexattr_t attr;
1043 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1044 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1045
1046 pthread_mutex_t lock;
1047 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1048
1049 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1050 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&lock));
1051 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1052 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1053 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock));
1054 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1055 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1056 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1057}
1058
1059TEST(pthread, pthread_mutex_lock_RECURSIVE) {
1060 pthread_mutexattr_t attr;
1061 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1062 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1063
1064 pthread_mutex_t lock;
1065 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1066
1067 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1068 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1069 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1070 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1071 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1072 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1073 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1074 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1075}