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