Home | History | Annotate | Download | only in Linux
      1 // RUN: %clangxx_asan -mllvm -asan-coverage=1 -DSHARED %s -shared -o %T/libcoverage_test.so -fPIC
      2 // RUN: %clangxx_asan -mllvm -asan-coverage=1 %s   -o %t -Wl,-R,\$ORIGIN -L%T -lcoverage_test
      3 // RUN: export ASAN_OPTIONS=coverage=1:verbosity=1
      4 // RUN: mkdir -p %T/coverage && cd %T/coverage
      5 // RUN: %run %t 2>&1         | FileCheck %s --check-prefix=CHECK-main
      6 // RUN: %run %t foo 2>&1     | FileCheck %s --check-prefix=CHECK-foo
      7 // RUN: %run %t bar 2>&1     | FileCheck %s --check-prefix=CHECK-bar
      8 // RUN: %run %t foo bar 2>&1 | FileCheck %s --check-prefix=CHECK-foo-bar
      9 // RUN: not %run %t foo bar 4    2>&1 | FileCheck %s --check-prefix=CHECK-report
     10 // RUN: not %run %t foo bar 4 5  2>&1 | FileCheck %s --check-prefix=CHECK-segv
     11 // RUN: cd .. && rm coverage -r
     12 //
     13 // https://code.google.com/p/address-sanitizer/issues/detail?id=263
     14 // XFAIL: android
     15 
     16 #include <stdio.h>
     17 #include <string.h>
     18 #include <unistd.h>
     19 
     20 #ifdef SHARED
     21 void bar() { printf("bar\n"); }
     22 #else
     23 __attribute__((noinline))
     24 void foo() { printf("foo\n"); }
     25 extern void bar();
     26 
     27 int G[4];
     28 
     29 int main(int argc, char **argv) {
     30   fprintf(stderr, "PID: %d\n", getpid());
     31   for (int i = 1; i < argc; i++) {
     32     if (!strcmp(argv[i], "foo"))
     33       foo();
     34     if (!strcmp(argv[i], "bar"))
     35       bar();
     36   }
     37   if (argc == 5) {
     38     static volatile char *zero = 0;
     39     *zero = 0;  // SEGV if argc == 5.
     40   }
     41   return G[argc];  // Buffer overflow if argc >= 4.
     42 }
     43 #endif
     44 
     45 // CHECK-main: PID: [[PID:[0-9]+]]
     46 // CHECK-main: [[PID]].sancov: 1 PCs written
     47 // CHECK-main-NOT: .so.[[PID]]
     48 //
     49 // CHECK-foo: PID: [[PID:[0-9]+]]
     50 // CHECK-foo: [[PID]].sancov: 2 PCs written
     51 // CHECK-foo-NOT: .so.[[PID]]
     52 //
     53 // CHECK-bar: PID: [[PID:[0-9]+]]
     54 // CHECK-bar: [[PID]].sancov: 1 PCs written
     55 // CHECK-bar: .so.[[PID]].sancov: 1 PCs written
     56 //
     57 // CHECK-foo-bar: PID: [[PID:[0-9]+]]
     58 // CHECK-foo-bar: [[PID]].sancov: 2 PCs written
     59 // CHECK-foo-bar: so.[[PID]].sancov: 1 PCs written
     60 //
     61 // CHECK-report: AddressSanitizer: global-buffer-overflow
     62 // CHECK-report: PCs written
     63 //
     64 // CHECK-segv: AddressSanitizer: SEGV
     65 // CHECK-segv: PCs written
     66