]> git.saurik.com Git - apple/libpthread.git/blob - tests/custom_stack.c
libpthread-218.30.1.tar.gz
[apple/libpthread.git] / tests / custom_stack.c
1 #include <pthread.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <os/assumes.h>
6
7 #include <darwintest.h>
8
9 static uintptr_t stackaddr;
10 static const size_t stacksize = 4096 * 8;
11
12 static void *function(void *arg) {
13 // Use the stack...
14 char buffer[BUFSIZ];
15 strlcpy(buffer, arg, sizeof(buffer));
16 strlcat(buffer, arg, sizeof(buffer));
17
18 T_ASSERT_LT((uintptr_t)__builtin_frame_address(0), stackaddr, NULL);
19 T_ASSERT_GT((uintptr_t)__builtin_frame_address(0), stackaddr - stacksize, NULL);
20
21 return (void *)(uintptr_t)strlen(buffer);
22 }
23
24 T_DECL(custom_stack, "creating a pthread with a custom stack",
25 T_META_ALL_VALID_ARCHS(YES)){
26 char *arg = "This is a test and only a test of the pthread stackaddr system.\n";
27 stackaddr = (uintptr_t)valloc(stacksize);
28 stackaddr += stacksize; // address starts at top of stack.
29
30 pthread_t thread;
31 pthread_attr_t attr;
32
33 T_ASSERT_POSIX_ZERO(pthread_attr_init(&attr), NULL);
34 T_ASSERT_POSIX_ZERO(pthread_attr_setstacksize(&attr, stacksize), NULL);
35 T_ASSERT_POSIX_ZERO(pthread_attr_setstackaddr(&attr, (void *)stackaddr), NULL);
36
37 T_ASSERT_POSIX_ZERO(pthread_create(&thread, &attr, function, arg), NULL);
38
39 void *result;
40 T_ASSERT_POSIX_ZERO(pthread_join(thread, &result), NULL);
41 T_ASSERT_EQ((uintptr_t)result, (uintptr_t)strlen(arg)*2, "thread should return correct value");
42
43 free((void*)(stackaddr - stacksize));
44 }