• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Test __sanitizer_reset_coverage().
2 
3 // RUN: %clangxx_asan -fsanitize-coverage=func %s -o %t
4 // RUN: %env_asan_opts=coverage=1 %run %t
5 
6 // https://github.com/google/sanitizers/issues/618
7 // UNSUPPORTED: android
8 
9 #include <sanitizer/coverage_interface.h>
10 #include <stdio.h>
11 #include <assert.h>
12 static volatile int sink;
bar()13 __attribute__((noinline)) void bar() { sink = 2; }
foo()14 __attribute__((noinline)) void foo() { sink = 1; }
15 
16 #define GET_AND_PRINT_COVERAGE()                                       \
17   bitset = 0;                                                  \
18   for (size_t i = 0; i < n_guards; i++)                        \
19     if (guards[i]) bitset |= 1U << i;                          \
20   printf("line %d: bitset %zd total: %zd\n", __LINE__, bitset, \
21          __sanitizer_get_total_unique_coverage());
22 
23 #define IS_POWER_OF_TWO(a) ((a & ((a) - 1)) == 0)
24 
main()25 int main() {
26   size_t *guards = 0;
27   size_t bitset;
28   size_t n_guards = __sanitizer_get_coverage_guards(&guards);
29 
30   GET_AND_PRINT_COVERAGE();
31   size_t main_bit = bitset;
32   assert(IS_POWER_OF_TWO(main_bit));
33 
34   foo();
35   GET_AND_PRINT_COVERAGE();
36   size_t foo_bit = bitset & ~main_bit;
37   assert(IS_POWER_OF_TWO(foo_bit));
38 
39   bar();
40   GET_AND_PRINT_COVERAGE();
41   size_t bar_bit = bitset & ~(main_bit | foo_bit);
42   assert(IS_POWER_OF_TWO(bar_bit));
43 
44   __sanitizer_reset_coverage();
45   assert(__sanitizer_get_total_unique_coverage() == 0);
46   GET_AND_PRINT_COVERAGE();
47   assert(bitset == 0);
48 
49   foo();
50   GET_AND_PRINT_COVERAGE();
51   assert(bitset == foo_bit);
52 
53   bar();
54   GET_AND_PRINT_COVERAGE();
55   assert(bitset == (foo_bit | bar_bit));
56 }
57