]> git.saurik.com Git - apple/libpthread.git/blob - tests/stack.c
libpthread-416.40.3.tar.gz
[apple/libpthread.git] / tests / stack.c
1 #include <signal.h>
2 #include <pthread/stack_np.h>
3
4 #include "darwintest_defaults.h"
5 #include <darwintest_utils.h>
6
7 #if defined(__arm64__)
8 #define call_chkstk(value) \
9 __asm__ volatile("orr x9, xzr, %0\t\n" \
10 "bl _thread_chkstk_darwin" : : "i"(value) : "x9")
11 #elif defined(__x86_64__)
12 #define call_chkstk(value) \
13 __asm__ volatile("movq %0, %%rax\t\n" \
14 "callq _thread_chkstk_darwin" : : "i"(value) : "rax")
15 #elif defined(__i386__)
16 #define call_chkstk(value) \
17 __asm__ volatile("movl %0, %%eax\t\n" \
18 "calll _thread_chkstk_darwin" : : "i"(value) : "eax")
19 #endif
20
21 static void
22 got_signal(int signo __unused)
23 {
24 T_PASS("calling with 1 << 24 crashed");
25 T_END;
26 }
27
28 T_DECL(chkstk, "chkstk",
29 T_META_ALL_VALID_ARCHS(YES), T_META_CHECK_LEAKS(NO))
30 {
31 #if defined(__arm__)
32 T_SKIP("not on armv7");
33 #else
34
35 call_chkstk(1 << 8);
36 T_PASS("calling with 1 << 8");
37
38 call_chkstk(1 << 16);
39 T_PASS("calling with 1 << 16");
40
41 stack_t ss = {
42 .ss_sp = malloc(MINSIGSTKSZ),
43 .ss_size = MINSIGSTKSZ,
44 };
45 T_ASSERT_POSIX_SUCCESS(sigaltstack(&ss, NULL), "sigaltstack");
46
47 struct sigaction sa = {
48 .sa_handler = got_signal,
49 .sa_flags = SA_ONSTACK,
50 };
51 T_ASSERT_POSIX_SUCCESS(sigaction(SIGSEGV, &sa, NULL), "sigaction");
52
53 call_chkstk(1 << 24);
54 T_FAIL("should have crashed");
55 #endif
56 }
57
58 struct frame {
59 uintptr_t frame;
60 uintptr_t ret;
61 };
62
63 OS_NOINLINE OS_NOT_TAIL_CALLED
64 static void
65 do_stack_frame_decode_test(struct frame frames[], size_t n, size_t count)
66 {
67 if (n < count) {
68 frames[n].frame = (uintptr_t)__builtin_frame_address(1);
69 frames[n].ret = (uintptr_t)__builtin_return_address(0);
70 do_stack_frame_decode_test(frames, n + 1, count);
71 } else {
72 uintptr_t frame = (uintptr_t)__builtin_frame_address(1);
73 uintptr_t ret;
74 while (count-- > 0) {
75 frame = pthread_stack_frame_decode_np(frame, &ret);
76 T_EXPECT_EQ(frames[count].frame, frame, "Frame %zd", count);
77 T_EXPECT_EQ(frames[count].ret, ret, "Retaddr %zd", count);
78 }
79 }
80 }
81
82 T_DECL(pthread_stack_frame_decode_np, "pthread_stack_frame_decode_np",
83 T_META_ALL_VALID_ARCHS(YES), T_META_CHECK_LEAKS(NO))
84 {
85 struct frame frames[10];
86 frames[0].frame = (uintptr_t)__builtin_frame_address(1);
87 frames[0].ret = (uintptr_t)__builtin_return_address(0);
88 do_stack_frame_decode_test(frames, 1, 10);
89 }