]> git.saurik.com Git - apple/libc.git/blob - tests/nxheap.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / tests / nxheap.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <signal.h>
4 #include <setjmp.h>
5 #if __has_feature(ptrauth_calls) && !defined(__OPEN_SOURCE)
6 #include <ptrauth.h>
7 #endif
8
9 #include <darwintest.h>
10
11 static char *heap;
12 static volatile int pass;
13 static sigjmp_buf jbuf;
14
15 static void __dead2
16 action(int signo, struct __siginfo *info, void *uap __attribute__((unused)))
17 {
18 if (info) {
19 pass = (signo == SIGBUS && info->si_addr == heap);
20 }
21 siglongjmp(jbuf, 0);
22 }
23
24 T_DECL(nxheap, "Non-executable heap", T_META_CHECK_LEAKS(false), T_META_ASROOT(true))
25 {
26 struct sigaction sa = {
27 .__sigaction_u.__sa_sigaction = action,
28 .sa_flags = SA_SIGINFO,
29 };
30
31 T_ASSERT_POSIX_ZERO(sigaction(SIGBUS, &sa, NULL), NULL);
32
33 if (sigsetjmp(jbuf, 0)) {
34 T_PASS("SIGBUS");
35 T_END;
36 }
37
38 T_QUIET; T_ASSERT_NOTNULL((heap = malloc(1)), NULL);
39
40 *heap = (char)0xc3; // retq
41 #if __has_feature(ptrauth_calls) && !defined(__OPEN_SOURCE)
42 heap = ptrauth_sign_unauthenticated(heap, ptrauth_key_function_pointer, 0);
43 #endif
44 ((void (*)(void))heap)(); // call *%eax
45
46 T_FAIL("SIGBUS");
47 }