]> git.saurik.com Git - apple/libpthread.git/blob - tests/pthread_attr_setstacksize.c
libpthread-301.20.1.tar.gz
[apple/libpthread.git] / tests / pthread_attr_setstacksize.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <limits.h>
5 #include <pthread.h>
6
7 #include "darwintest_defaults.h"
8
9 #define STACK_ALLOWANCE (1024ULL * 6)
10
11 static void *
12 pthread_attr_setstacksize_func(void *arg)
13 {
14 if ((size_t)arg < 1024ULL * 32) {
15 /*
16 * We can't use darwintest because it requires a bigger stack than
17 * this, so cheat and use the return value for the test.
18 */
19 #ifndef __arm64__
20 if ((size_t)arg != pthread_get_stacksize_np(pthread_self())) {
21 return NULL;
22 }
23 #endif
24 return (void*)pthread_attr_setstacksize_func;
25 }
26
27 #if defined(__arm64__)
28 // Because of <rdar://problem/19941744>, the kext adds additional size to the stack on arm64.
29 T_EXPECTFAIL;
30 #endif
31 T_EXPECT_EQ((size_t)arg, pthread_get_stacksize_np(pthread_self()), "[stacksize=%zu] pthread_self stack size matches", (size_t)arg);
32
33 size_t stacksize = (size_t)arg - STACK_ALLOWANCE;
34 char *buf = alloca(stacksize);
35
36 memset_s(buf, sizeof(buf), 0, sizeof(buf) - 1);
37
38 return (void*)pthread_attr_setstacksize_func;
39 }
40
41 T_DECL(pthread_attr_setstacksize, "pthread_attr_setstacksize")
42 {
43 size_t stacksizes[] = {PTHREAD_STACK_MIN, 1024ULL * 16, 1024ULL * 32, 1024ULL * 1024};
44 for (int i = 0; (size_t)i < sizeof(stacksizes)/sizeof(stacksizes[0]); i++){
45 pthread_t t = NULL;
46 pthread_attr_t attr;
47 size_t stacksize = stacksizes[i];
48
49 T_ASSERT_POSIX_ZERO(pthread_attr_init(&attr), "[stacksize=%zu] pthread_attr_init", stacksize);
50 T_ASSERT_POSIX_ZERO(pthread_attr_setstacksize(&attr, stacksize), "[stacksize=%zu] pthread_attr_stacksize", stacksize);
51
52 T_ASSERT_POSIX_ZERO(pthread_create(&t, &attr, pthread_attr_setstacksize_func, (void*)stacksize), "[stacksize=%zu] pthread_create", stacksize);
53 T_ASSERT_NOTNULL(t, "[stacksize=%zu] pthread pointer not null", stacksize);
54
55 T_EXPECT_POSIX_ZERO(pthread_attr_destroy(&attr), "[stacksize=%zu] pthread_attr_destroy", stacksize);
56
57 #if defined(__arm64__)
58 // Because of <rdar://problem/19941744>, the kext adds additional size to the stack on arm64.
59 T_EXPECTFAIL;
60 #endif
61 T_EXPECT_EQ(stacksize, pthread_get_stacksize_np(t), "[stacksize=%zu] pthread stack size matches", stacksize);
62
63 void *out = NULL;
64 T_ASSERT_POSIX_ZERO(pthread_join(t, &out), "[stacksize=%zu] pthread_join", stacksize);
65 T_EXPECT_EQ_PTR(out, (void*)pthread_attr_setstacksize_func, "[stacksize=%zu] pthread_join returns correct value", stacksize);
66 }
67 }