1 // RUN: %clang_scudo %s -o %t
2 // RUN: %run %t after 2>&1 | FileCheck %s
3 // RUN: %run %t before 2>&1 | FileCheck %s
4
5 // Test that we hit a guard page when writing past the end of a chunk
6 // allocated by the Secondary allocator, or writing too far in front of it.
7
8 #include <assert.h>
9 #include <malloc.h>
10 #include <signal.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14
handler(int signo,siginfo_t * info,void * uctx)15 void handler(int signo, siginfo_t *info, void *uctx) {
16 if (info->si_code == SEGV_ACCERR) {
17 fprintf(stderr, "SCUDO SIGSEGV\n");
18 exit(0);
19 }
20 exit(1);
21 }
22
main(int argc,char ** argv)23 int main(int argc, char **argv)
24 {
25 // The size must be large enough to be serviced by the secondary allocator.
26 long page_size = sysconf(_SC_PAGESIZE);
27 size_t size = (1U << 17) + page_size;
28 struct sigaction a;
29
30 assert(argc == 2);
31 memset(&a, 0, sizeof(a));
32 a.sa_sigaction = handler;
33 a.sa_flags = SA_SIGINFO;
34
35 char *p = (char *)malloc(size);
36 assert(p);
37 memset(p, 'A', size); // This should not trigger anything.
38 // Set up the SIGSEGV handler now, as the rest should trigger an AV.
39 sigaction(SIGSEGV, &a, NULL);
40 if (!strcmp(argv[1], "after")) {
41 for (int i = 0; i < page_size; i++)
42 p[size + i] = 'A';
43 }
44 if (!strcmp(argv[1], "before")) {
45 for (int i = 1; i < page_size; i++)
46 p[-i] = 'A';
47 }
48 free(p);
49
50 return 1; // A successful test means we shouldn't reach this.
51 }
52
53 // CHECK: SCUDO SIGSEGV
54