| 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 | |
| 15 | void handler(int signo, siginfo_t *info, void *uctx) { |
| 16 | if (info->si_code == SEGV_ACCERR) { |
| 17 | fprintf(stderr, format: "SCUDO SIGSEGV\n" ); |
| 18 | exit(status: 0); |
| 19 | } |
| 20 | exit(status: 1); |
| 21 | } |
| 22 | |
| 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(s: &a, c: 0, n: sizeof(a)); |
| 31 | a.sa_sigaction = handler; |
| 32 | a.sa_flags = SA_SIGINFO; |
| 33 | |
| 34 | char *p = (char *)malloc(size: size); |
| 35 | assert(p); |
| 36 | memset(s: p, c: 'A', n: size); // This should not trigger anything. |
| 37 | // Set up the SIGSEGV handler now, as the rest should trigger an AV. |
| 38 | sigaction(SIGSEGV, act: &a, NULL); |
| 39 | if (!strcmp(s1: argv[1], s2: "after" )) { |
| 40 | for (int i = 0; i < page_size; i++) |
| 41 | p[size + i] = 'A'; |
| 42 | } |
| 43 | if (!strcmp(s1: argv[1], s2: "before" )) { |
| 44 | for (int i = 1; i < page_size; i++) |
| 45 | p[-i] = 'A'; |
| 46 | } |
| 47 | free(ptr: p); |
| 48 | |
| 49 | return 1; // A successful test means we shouldn't reach this. |
| 50 | } |
| 51 | |
| 52 | // CHECK: SCUDO SIGSEGV |
| 53 | |