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