[lldb] Fix TestLoadUnload.py (#117416)
[llvm-project.git] / compiler-rt / test / scudo / secondary.c
blob32bb93b9d51821f9e238715c54ffe71dac18e7e9
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
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.
8 #include <assert.h>
9 #include <malloc.h>
10 #include <signal.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
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);
20 exit(1);
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;
29 assert(argc == 2);
30 memset(&a, 0, sizeof(a));
31 a.sa_sigaction = handler;
32 a.sa_flags = SA_SIGINFO;
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';
43 if (!strcmp(argv[1], "before")) {
44 for (int i = 1; i < page_size; i++)
45 p[-i] = 'A';
47 free(p);
49 return 1; // A successful test means we shouldn't reach this.
52 // CHECK: SCUDO SIGSEGV