blob: 4a7c155c0734a79e4ab89b6b6f5cc9ce1f87796c [file] [log] [blame]
Elliott Hughesbfeab1b2012-09-05 17:47:37 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <errno.h>
Elliott Hughes5b9310e2013-10-02 16:59:05 -070020#include <inttypes.h>
Elliott Hughesb95cf0d2013-07-15 14:51:07 -070021#include <limits.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070022#include <pthread.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070023#include <unistd.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070024
25TEST(pthread, pthread_key_create) {
26 pthread_key_t key;
27 ASSERT_EQ(0, pthread_key_create(&key, NULL));
28 ASSERT_EQ(0, pthread_key_delete(key));
29 // Can't delete a key that's already been deleted.
30 ASSERT_EQ(EINVAL, pthread_key_delete(key));
31}
Elliott Hughes4d014e12012-09-07 16:47:54 -070032
Elliott Hughes3e898472013-02-12 16:40:24 +000033#if !defined(__GLIBC__) // glibc uses keys internally that its sysconf value doesn't account for.
Elliott Hughes44b53ad2013-02-11 20:18:47 +000034TEST(pthread, pthread_key_create_lots) {
35 // We can allocate _SC_THREAD_KEYS_MAX keys.
36 std::vector<pthread_key_t> keys;
37 for (int i = 0; i < sysconf(_SC_THREAD_KEYS_MAX); ++i) {
38 pthread_key_t key;
Elliott Hughes3e898472013-02-12 16:40:24 +000039 // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong.
40 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf(_SC_THREAD_KEYS_MAX);
Elliott Hughes44b53ad2013-02-11 20:18:47 +000041 keys.push_back(key);
42 }
43
44 // ...and that really is the maximum.
45 pthread_key_t key;
46 ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
47
48 // (Don't leak all those keys!)
49 for (size_t i = 0; i < keys.size(); ++i) {
50 ASSERT_EQ(0, pthread_key_delete(keys[i]));
51 }
52}
Elliott Hughes3e898472013-02-12 16:40:24 +000053#endif
Elliott Hughes44b53ad2013-02-11 20:18:47 +000054
Elliott Hughes4d014e12012-09-07 16:47:54 -070055static void* IdFn(void* arg) {
56 return arg;
57}
58
59static void* SleepFn(void* arg) {
Elliott Hughes5b9310e2013-10-02 16:59:05 -070060 sleep(reinterpret_cast<uintptr_t>(arg));
Elliott Hughes4d014e12012-09-07 16:47:54 -070061 return NULL;
62}
63
Sergey Melnikov10ce9692012-10-26 14:06:43 +040064static void* SpinFn(void* arg) {
65 volatile bool* b = reinterpret_cast<volatile bool*>(arg);
66 while (!*b) {
67 }
68 return NULL;
69}
70
Elliott Hughes4d014e12012-09-07 16:47:54 -070071static void* JoinFn(void* arg) {
72 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
73}
74
Sergey Melnikov10ce9692012-10-26 14:06:43 +040075static void AssertDetached(pthread_t t, bool is_detached) {
76 pthread_attr_t attr;
77 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
78 int detach_state;
79 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
80 pthread_attr_destroy(&attr);
81 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
82}
83
Elliott Hughes9d23e042013-02-15 19:21:51 -080084static void MakeDeadThread(pthread_t& t) {
85 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
86 void* result;
87 ASSERT_EQ(0, pthread_join(t, &result));
88}
89
Elliott Hughes4d014e12012-09-07 16:47:54 -070090TEST(pthread, pthread_create) {
91 void* expected_result = reinterpret_cast<void*>(123);
92 // Can we create a thread?
93 pthread_t t;
94 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
95 // If we join, do we get the expected value back?
96 void* result;
97 ASSERT_EQ(0, pthread_join(t, &result));
98 ASSERT_EQ(expected_result, result);
99}
100
Elliott Hughes3e898472013-02-12 16:40:24 +0000101TEST(pthread, pthread_create_EAGAIN) {
102 pthread_attr_t attributes;
103 ASSERT_EQ(0, pthread_attr_init(&attributes));
104 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
105
106 pthread_t t;
107 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
108}
109
Elliott Hughes4d014e12012-09-07 16:47:54 -0700110TEST(pthread, pthread_no_join_after_detach) {
111 pthread_t t1;
112 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
113
114 // After a pthread_detach...
115 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400116 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700117
118 // ...pthread_join should fail.
119 void* result;
120 ASSERT_EQ(EINVAL, pthread_join(t1, &result));
121}
122
123TEST(pthread, pthread_no_op_detach_after_join) {
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400124 bool done = false;
125
Elliott Hughes4d014e12012-09-07 16:47:54 -0700126 pthread_t t1;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400127 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700128
129 // If thread 2 is already waiting to join thread 1...
130 pthread_t t2;
131 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
132
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400133 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700134
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400135 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
136 ASSERT_EQ(0, pthread_detach(t1));
137 AssertDetached(t1, false);
138
139 done = true;
140
141 // ...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 -0700142 void* join_result;
143 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700144 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700145}
Elliott Hughes14f19592012-10-29 10:19:44 -0700146
147TEST(pthread, pthread_join_self) {
148 void* result;
149 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
150}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700151
152#if __BIONIC__ // For some reason, gtest on bionic can cope with this but gtest on glibc can't.
153
154static void TestBug37410() {
155 pthread_t t1;
156 ASSERT_EQ(0, pthread_create(&t1, NULL, JoinFn, reinterpret_cast<void*>(pthread_self())));
157 pthread_exit(NULL);
158}
159
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800160// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
161// run this test (which exits normally) in its own process.
Elliott Hughes4f251be2012-11-01 16:33:29 -0700162TEST(pthread_DeathTest, pthread_bug_37410) {
163 // http://code.google.com/p/android/issues/detail?id=37410
164 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800165 ASSERT_EXIT(TestBug37410(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700166}
167#endif
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800168
169static void* SignalHandlerFn(void* arg) {
170 sigset_t wait_set;
171 sigfillset(&wait_set);
172 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
173}
174
175TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700176 // Check that SIGUSR1 isn't blocked.
177 sigset_t original_set;
178 sigemptyset(&original_set);
179 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
180 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
181
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800182 // Block SIGUSR1.
183 sigset_t set;
184 sigemptyset(&set);
185 sigaddset(&set, SIGUSR1);
186 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
187
Elliott Hughes19e62322013-10-15 11:23:57 -0700188 // Check that SIGUSR1 is blocked.
189 sigset_t final_set;
190 sigemptyset(&final_set);
191 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
192 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
193 // ...and that sigprocmask agrees with pthread_sigmask.
194 sigemptyset(&final_set);
195 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
196 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
197
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800198 // Spawn a thread that calls sigwait and tells us what it received.
199 pthread_t signal_thread;
200 int received_signal = -1;
201 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
202
203 // Send that thread SIGUSR1.
204 pthread_kill(signal_thread, SIGUSR1);
205
206 // See what it got.
207 void* join_result;
208 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
209 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700210 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700211
212 // Restore the original signal mask.
213 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800214}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800215
Elliott Hughes3e898472013-02-12 16:40:24 +0000216#if __BIONIC__
Elliott Hughes40eabe22013-02-14 18:59:37 -0800217extern "C" int __pthread_clone(void* (*fn)(void*), void* child_stack, int flags, void* arg);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800218TEST(pthread, __pthread_clone) {
219 uintptr_t fake_child_stack[16];
220 errno = 0;
221 ASSERT_EQ(-1, __pthread_clone(NULL, &fake_child_stack[0], CLONE_THREAD, NULL));
222 ASSERT_EQ(EINVAL, errno);
223}
224#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000225
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800226#if __BIONIC__ // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
Elliott Hughes3e898472013-02-12 16:40:24 +0000227TEST(pthread, pthread_setname_np__too_long) {
228 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
229}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800230#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000231
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800232#if __BIONIC__ // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
Elliott Hughes3e898472013-02-12 16:40:24 +0000233TEST(pthread, pthread_setname_np__self) {
234 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
235}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800236#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000237
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800238#if __BIONIC__ // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
Elliott Hughes3e898472013-02-12 16:40:24 +0000239TEST(pthread, pthread_setname_np__other) {
Elliott Hughes40eabe22013-02-14 18:59:37 -0800240 // Emulator kernels don't currently support setting the name of other threads.
241 char* filename = NULL;
242 asprintf(&filename, "/proc/self/task/%d/comm", gettid());
243 struct stat sb;
244 bool has_comm = (stat(filename, &sb) != -1);
245 free(filename);
246
247 if (has_comm) {
248 pthread_t t1;
249 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
250 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
251 } else {
252 fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n");
253 }
Elliott Hughes3e898472013-02-12 16:40:24 +0000254}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800255#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000256
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800257#if __BIONIC__ // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
Elliott Hughes3e898472013-02-12 16:40:24 +0000258TEST(pthread, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800259 pthread_t dead_thread;
260 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000261
262 // Call pthread_setname_np after thread has already exited.
Elliott Hughesa41ba2f2013-03-21 20:02:35 -0700263 ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000264}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800265#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800266
267TEST(pthread, pthread_kill__0) {
268 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
269 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
270}
271
272TEST(pthread, pthread_kill__invalid_signal) {
273 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
274}
275
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800276static void pthread_kill__in_signal_handler_helper(int signal_number) {
277 static int count = 0;
278 ASSERT_EQ(SIGALRM, signal_number);
279 if (++count == 1) {
280 // Can we call pthread_kill from a signal handler?
281 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
282 }
283}
284
285TEST(pthread, pthread_kill__in_signal_handler) {
286 struct sigaction action;
287 sigemptyset(&action.sa_mask);
288 action.sa_flags = 0;
289 action.sa_handler = pthread_kill__in_signal_handler_helper;
290 sigaction(SIGALRM, &action, NULL);
291 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
292}
293
Elliott Hughes9d23e042013-02-15 19:21:51 -0800294TEST(pthread, pthread_detach__no_such_thread) {
295 pthread_t dead_thread;
296 MakeDeadThread(dead_thread);
297
298 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
299}
300
Jeff Hao9b06cc32013-08-15 14:51:16 -0700301TEST(pthread, pthread_getcpuclockid__clock_gettime) {
302 pthread_t t;
303 ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5)));
304
305 clockid_t c;
306 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
307 timespec ts;
308 ASSERT_EQ(0, clock_gettime(c, &ts));
309}
310
Elliott Hughes9d23e042013-02-15 19:21:51 -0800311TEST(pthread, pthread_getcpuclockid__no_such_thread) {
312 pthread_t dead_thread;
313 MakeDeadThread(dead_thread);
314
315 clockid_t c;
316 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
317}
318
319TEST(pthread, pthread_getschedparam__no_such_thread) {
320 pthread_t dead_thread;
321 MakeDeadThread(dead_thread);
322
323 int policy;
324 sched_param param;
325 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
326}
327
328TEST(pthread, pthread_setschedparam__no_such_thread) {
329 pthread_t dead_thread;
330 MakeDeadThread(dead_thread);
331
332 int policy = 0;
333 sched_param param;
334 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
335}
336
337TEST(pthread, pthread_join__no_such_thread) {
338 pthread_t dead_thread;
339 MakeDeadThread(dead_thread);
340
341 void* result;
342 ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result));
343}
344
345TEST(pthread, pthread_kill__no_such_thread) {
346 pthread_t dead_thread;
347 MakeDeadThread(dead_thread);
348
349 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
350}
msg5550f020d12013-06-06 14:59:28 -0400351
352TEST(pthread, pthread_join__multijoin) {
353 bool done = false;
354
355 pthread_t t1;
356 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
357
358 pthread_t t2;
359 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
360
361 sleep(1); // (Give t2 a chance to call pthread_join.)
362
363 // Multiple joins to the same thread should fail.
364 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
365
366 done = true;
367
368 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
369 void* join_result;
370 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700371 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400372}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700373
374static void* GetActualGuardSizeFn(void* arg) {
375 pthread_attr_t attributes;
376 pthread_getattr_np(pthread_self(), &attributes);
377 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
378 return NULL;
379}
380
381static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
382 size_t result;
383 pthread_t t;
384 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
385 void* join_result;
386 pthread_join(t, &join_result);
387 return result;
388}
389
390static void* GetActualStackSizeFn(void* arg) {
391 pthread_attr_t attributes;
392 pthread_getattr_np(pthread_self(), &attributes);
393 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
394 return NULL;
395}
396
397static size_t GetActualStackSize(const pthread_attr_t& attributes) {
398 size_t result;
399 pthread_t t;
400 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
401 void* join_result;
402 pthread_join(t, &join_result);
403 return result;
404}
405
406TEST(pthread, pthread_attr_setguardsize) {
407 pthread_attr_t attributes;
408 ASSERT_EQ(0, pthread_attr_init(&attributes));
409
410 // Get the default guard size.
411 size_t default_guard_size;
412 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
413
414 // No such thing as too small: will be rounded up to one page by pthread_create.
415 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
416 size_t guard_size;
417 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
418 ASSERT_EQ(128U, guard_size);
419 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
420
421 // Large enough and a multiple of the page size.
422 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
423 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
424 ASSERT_EQ(32*1024U, guard_size);
425
426 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
427 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
428 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
429 ASSERT_EQ(32*1024U + 1, guard_size);
430}
431
432TEST(pthread, pthread_attr_setstacksize) {
433 pthread_attr_t attributes;
434 ASSERT_EQ(0, pthread_attr_init(&attributes));
435
436 // Get the default stack size.
437 size_t default_stack_size;
438 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
439
440 // Too small.
441 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
442 size_t stack_size;
443 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
444 ASSERT_EQ(default_stack_size, stack_size);
445 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
446
447 // Large enough and a multiple of the page size.
448 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
449 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
450 ASSERT_EQ(32*1024U, stack_size);
451 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
452
453 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
454 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
455 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
456 ASSERT_EQ(32*1024U + 1, stack_size);
457#if __BIONIC__
458 // Bionic rounds up, which is what POSIX allows.
459 ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
460#else
461 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
462 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
463#endif
464}