1 // RUN: %clangxx_asan -fsanitize-coverage=2 -DSHARED %s -shared -o %dynamiclib -fPIC %ld_flags_rpath_so
2 // RUN: %clangxx_asan -fsanitize-coverage=1 %s -o %t %ld_flags_rpath_exe
3 // RUN: export ASAN_OPTIONS=coverage=1:verbosity=1
4 // RUN: rm -rf %T/coverage_sandboxing_test
5 // RUN: mkdir %T/coverage_sandboxing_test && cd %T/coverage_sandboxing_test
6 // RUN: mkdir vanilla && cd vanilla
7 // RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-vanilla
8 // RUN: mkdir ../sandbox1 && cd ../sandbox1
9 // RUN: %run %t a 2>&1 | FileCheck %s --check-prefix=CHECK-sandbox
10 // RUN: %sancov unpack coverage_sandboxing_test.sancov.packed
11 // RUN: mkdir ../sandbox2 && cd ../sandbox2
12 // RUN: %run %t a b 2>&1 | FileCheck %s --check-prefix=CHECK-sandbox
13 // RUN: %sancov unpack coverage_sandboxing_test.sancov.packed
14 // RUN: cd ..
15 // RUN: %sancov print vanilla/`basename %dynamiclib`*.sancov > vanilla.txt
16 // RUN: %sancov print sandbox1/`basename %dynamiclib`*.sancov > sandbox1.txt
17 // RUN: %sancov print sandbox2/`basename %dynamiclib`*.sancov > sandbox2.txt
18 // RUN: diff vanilla.txt sandbox1.txt
19 // RUN: diff vanilla.txt sandbox2.txt
20 // RUN: rm -r %T/coverage_sandboxing_test
21 // https://code.google.com/p/address-sanitizer/issues/detail?id=263
22 // XFAIL: android
23
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include <sanitizer/coverage_interface.h>
31
32 #define bb0(n) \
33 case n: \
34 fprintf(stderr, "foo: %d\n", n); \
35 break;
36
37 #define bb1(n) bb0(n) bb0(n + 1)
38 #define bb2(n) bb1(n) bb1(n + 2)
39 #define bb3(n) bb2(n) bb2(n + 4)
40 #define bb4(n) bb3(n) bb3(n + 8)
41 #define bb5(n) bb4(n) bb4(n + 16)
42 #define bb6(n) bb5(n) bb5(n + 32)
43 #define bb7(n) bb6(n) bb6(n + 64)
44 #define bb8(n) bb7(n) bb7(n + 128)
45
46 #ifdef SHARED
foo(int i)47 void foo(int i) {
48 switch(i) {
49 // 256 basic blocks
50 bb8(0)
51 }
52 }
53 #else
54 extern void foo(int i);
55
main(int argc,char ** argv)56 int main(int argc, char **argv) {
57 assert(argc <= 3);
58 for (int i = 0; i < 256; i++) foo(i);
59 fprintf(stderr, "PID: %d\n", getpid());
60 if (argc == 1) {
61 // Vanilla mode, dump to individual files.
62 return 0;
63 }
64 // Dump to packed file.
65 int fd = creat("coverage_sandboxing_test.sancov.packed", 0660);
66 __sanitizer_sandbox_arguments args = {0};
67 args.coverage_sandboxed = 1;
68 args.coverage_fd = fd;
69 if (argc == 2)
70 // Write to packed file, do not split into blocks.
71 args.coverage_max_block_size = 0;
72 else if (argc == 3)
73 // Write to packed file, split into blocks (as if writing to a socket).
74 args.coverage_max_block_size = 100;
75 __sanitizer_sandbox_on_notify(&args);
76 return 0;
77 }
78 #endif
79
80 // CHECK-vanilla: PID: [[PID:[0-9]+]]
81 // CHECK-vanilla: .so.[[PID]].sancov: 258 PCs written
82 // CHECK-vanilla: [[PID]].sancov: 1 PCs written
83
84 // CHECK-sandbox: PID: [[PID:[0-9]+]]
85 // CHECK-sandbox: 258 PCs written to packed file
86