]> git.saurik.com Git - apple/libc.git/blob - tests/nxheap.c
eb521e8ee7459e77724aec4a4ebc58dd29e59ac0
[apple/libc.git] / tests / nxheap.c
1 #include <bsdtests.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 char *heap;
6 volatile int pass;
7 sigjmp_buf jbuf;
8
9 void
10 action(int signo, struct __siginfo *info, void *uap __attribute__((unused)))
11 {
12 if (info) {
13 pass = (signo == SIGBUS && info->si_addr == heap);
14 }
15 return siglongjmp(jbuf, 0);
16 }
17
18 int
19 main(void)
20 {
21 int ret;
22
23 struct sigaction sa = {
24 .__sigaction_u.__sa_sigaction = action,
25 .sa_flags = SA_SIGINFO,
26 };
27
28 test_start("Non-executable heap");
29
30 ret = sigaction(SIGBUS, &sa, NULL);
31 assert(ret == 0);
32 test_long("sigaction", ret, 0);
33
34 if (sigsetjmp(jbuf, 0)) {
35 // PASS
36 test_long("SIGBUS", 1, 1);
37 test_stop();
38 return EXIT_FAILURE;
39 }
40
41 heap = malloc(1);
42 test_ptr_notnull("malloc", heap);
43
44 *heap = 0xc3; // retq
45 ((void (*)(void))heap)(); // call *%eax
46
47 // FAIL
48 test_long("SIGBUS", 0, 1);
49
50 test_stop();
51
52 return EXIT_SUCCESS;
53 }