Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / sanitizer_common / TestCases / Posix / mmap_write_exec.cpp
blobeec2161573a77133f146e10f8c5bcf8ad53c81b4
1 // RUN: %clangxx %s -o %t
2 // RUN: %env_tool_opts=detect_write_exec=1 %run %t 2>&1 | FileCheck %s
3 // RUN: %env_tool_opts=detect_write_exec=0 %run %t 2>&1 | FileCheck %s \
4 // RUN: --check-prefix=CHECK-DISABLED
5 // ubsan and lsan do not install mmap interceptors UNSUPPORTED: ubsan, lsan
7 // TODO: Fix option on Android, it hangs there for unknown reasons.
8 // XFAIL: android
10 #if defined(__APPLE__)
11 #include <Availability.h>
12 #endif
13 #include <pthread.h>
14 #include <stdio.h>
15 #include <sys/mman.h>
17 int main(int argc, char **argv) {
18 char *p = (char *)mmap(0, 1024, PROT_READ | PROT_WRITE | PROT_EXEC,
19 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
20 // CHECK: WARNING: {{.*}}Sanitizer: writable-executable page usage
21 // CHECK: #{{[0-9]+.*}}main{{.*}}mmap_write_exec.cpp:[[@LINE-3]]
22 // CHECK: SUMMARY: {{.*}}Sanitizer: w-and-x-usage
24 char *q = (char *)mmap(p, 64, PROT_READ | PROT_WRITE,
25 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
26 (void)mprotect(q, 64, PROT_WRITE | PROT_EXEC);
27 // CHECK: WARNING: {{.*}}Sanitizer: writable-executable page usage
28 // CHECK: #{{[0-9]+.*}}main{{.*}}mmap_write_exec.cpp:[[@LINE-2]]
29 // CHECK: SUMMARY: {{.*}}Sanitizer: w-and-x-usage
31 char *a = (char *)mmap(0, 1024, PROT_READ | PROT_WRITE,
32 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
33 char *b = (char *)mmap(a, 64, PROT_READ | PROT_WRITE,
34 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
35 (void)mprotect(q, 64, PROT_READ | PROT_EXEC);
36 // CHECK-NOT: Sanitizer
37 #if defined(__APPLE__)
38 # if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
39 __MAC_OS_X_VERSION_MIN_REQUIRED >= 110000)
40 pthread_jit_write_protect_np(false);
41 # endif
42 char *c = (char *)mmap(0, 128, PROT_WRITE | PROT_EXEC,
43 MAP_ANONYMOUS | MAP_PRIVATE | MAP_JIT, -1, 0);
44 // CHECK-NOT: Sanitizer
45 #endif
47 printf("done\n");
48 // CHECK-DISABLED-NOT: Sanitizer
49 // CHECK-DISABLED: done