1 // RUN: %clang_esan_wset -O0 %s -o %t 2>&1
2 // RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-ESAN
3
4 // RUN: %clang -O0 %s -o %t 2>&1
5 // RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NO-ESAN
6
7 #include <sanitizer/esan_interface.h>
8 #include <sched.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/mman.h>
13
14 const int size = 0x1 << 25; // 523288 cache lines
15 const int iters = 6;
16
main(int argc,char ** argv)17 int main(int argc, char **argv) {
18 char *buf = (char *)mmap(0, size, PROT_READ | PROT_WRITE,
19 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
20 // Try to increase the probability that the sideline thread is
21 // scheduled. Unfortunately we can't do proper synchronization
22 // without some form of annotation or something.
23 sched_yield();
24 // Do enough work to get at least 4 samples.
25 for (int j = 0; j < iters; ++j) {
26 for (int i = 0; i < size; ++i)
27 buf[i] = i;
28 sched_yield();
29 }
30 // Ensure a non-esan build works without ifdefs:
31 if (__esan_report) {
32 // We should get 2 roughly identical reports:
33 __esan_report();
34 }
35 munmap(buf, size);
36 fprintf(stderr, "all done\n");
37 // CHECK-NO-ESAN: all done
38 // We only check for a few samples here to reduce the chance of flakiness:
39 // CHECK-ESAN: =={{[0-9]+}}== Total number of samples: {{[0-9]+}}
40 // CHECK-ESAN-NEXT: =={{[0-9]+}}== Samples array #0 at period 20 ms
41 // CHECK-ESAN-NEXT: =={{[0-9]+}}==# 0: {{[ 0-9]+}} {{KB|MB|Bytes}} ({{[ 0-9]+}} cache lines)
42 // CHECK-ESAN-NEXT: =={{[0-9]+}}==# 1: {{[ 0-9]+}} {{KB|MB|Bytes}} ({{[ 0-9]+}} cache lines)
43 // CHECK-ESAN-NEXT: =={{[0-9]+}}==# 2: {{[ 0-9]+}} {{KB|MB|Bytes}} ({{[ 0-9]+}} cache lines)
44 // CHECK-ESAN-NEXT: =={{[0-9]+}}==# 3: {{[ 0-9]+}} {{KB|MB|Bytes}} ({{[ 0-9]+}} cache lines)
45 // CHECK-ESAN: =={{[0-9]+}}== Samples array #1 at period 80 ms
46 // CHECK-ESAN-NEXT: =={{[0-9]+}}==# 0: {{[ 0-9]+}} {{KB|MB|Bytes}} ({{[ 0-9]+}} cache lines)
47 // CHECK-ESAN: =={{[0-9]+}}== Samples array #2 at period 320 ms
48 // CHECK-ESAN: =={{[0-9]+}}== Samples array #3 at period 1280 ms
49 // CHECK-ESAN: =={{[0-9]+}}== Samples array #4 at period 5120 ms
50 // CHECK-ESAN: =={{[0-9]+}}== Samples array #5 at period 20 sec
51 // CHECK-ESAN: =={{[0-9]+}}== Samples array #6 at period 81 sec
52 // CHECK-ESAN: =={{[0-9]+}}== Samples array #7 at period 327 sec
53 // CHECK-ESAN: {{.*}} EfficiencySanitizer: the total working set size: 32 MB (5242{{[0-9][0-9]}} cache lines)
54 // CHECK-ESAN-NEXT: all done
55 // CHECK-ESAN-NEXT: =={{[0-9]+}}== Total number of samples: {{[0-9]+}}
56 // CHECK-ESAN-NEXT: =={{[0-9]+}}== Samples array #0 at period 20 ms
57 // CHECK-ESAN-NEXT: =={{[0-9]+}}==# 0: {{[ 0-9]+}} {{KB|MB|Bytes}} ({{[ 0-9]+}} cache lines)
58 // CHECK-ESAN-NEXT: =={{[0-9]+}}==# 1: {{[ 0-9]+}} {{KB|MB|Bytes}} ({{[ 0-9]+}} cache lines)
59 // CHECK-ESAN-NEXT: =={{[0-9]+}}==# 2: {{[ 0-9]+}} {{KB|MB|Bytes}} ({{[ 0-9]+}} cache lines)
60 // CHECK-ESAN-NEXT: =={{[0-9]+}}==# 3: {{[ 0-9]+}} {{KB|MB|Bytes}} ({{[ 0-9]+}} cache lines)
61 // CHECK-ESAN: =={{[0-9]+}}== Samples array #1 at period 80 ms
62 // CHECK-ESAN-NEXT: =={{[0-9]+}}==# 0: {{[ 0-9]+}} {{KB|MB|Bytes}} ({{[ 0-9]+}} cache lines)
63 // CHECK-ESAN: =={{[0-9]+}}== Samples array #2 at period 320 ms
64 // CHECK-ESAN: =={{[0-9]+}}== Samples array #3 at period 1280 ms
65 // CHECK-ESAN: =={{[0-9]+}}== Samples array #4 at period 5120 ms
66 // CHECK-ESAN: =={{[0-9]+}}== Samples array #5 at period 20 sec
67 // CHECK-ESAN: =={{[0-9]+}}== Samples array #6 at period 81 sec
68 // CHECK-ESAN: =={{[0-9]+}}== Samples array #7 at period 327 sec
69 // CHECK-ESAN: {{.*}} EfficiencySanitizer: the total working set size: 32 MB (5242{{[0-9][0-9]}} cache lines)
70 return 0;
71 }
72