xref: /llvm-project/compiler-rt/test/scudo/secondary.c (revision f7c5c0d87b8ae5e55006fd3a31994cd68d64f102)
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   // The size must be large enough to be serviced by the secondary allocator.
25   long page_size = sysconf(_SC_PAGESIZE);
26   size_t size = (1U << 17) + page_size;
27   struct sigaction a;
28 
29   assert(argc == 2);
30   memset(&a, 0, sizeof(a));
31   a.sa_sigaction = handler;
32   a.sa_flags = SA_SIGINFO;
33 
34   char *p = (char *)malloc(size);
35   assert(p);
36   memset(p, 'A', size); // This should not trigger anything.
37   // Set up the SIGSEGV handler now, as the rest should trigger an AV.
38   sigaction(SIGSEGV, &a, NULL);
39   if (!strcmp(argv[1], "after")) {
40     for (int i = 0; i < page_size; i++)
41       p[size + i] = 'A';
42   }
43   if (!strcmp(argv[1], "before")) {
44     for (int i = 1; i < page_size; i++)
45       p[-i] = 'A';
46   }
47   free(p);
48 
49   return 1; // A successful test means we shouldn't reach this.
50 }
51 
52 // CHECK: SCUDO SIGSEGV
53