aboutsummaryrefslogtreecommitdiff
path: root/test/asan/TestCases/coverage-reset.cc
blob: 11c5ef66ecf6502880d0cc80104853d71bc3566e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Test __sanitizer_reset_coverage().

// RUN: %clangxx_asan -fsanitize-coverage=func %s -o %t
// RUN: %env_asan_opts=coverage=1 %run %t

// https://github.com/google/sanitizers/issues/618
// UNSUPPORTED: android

#include <sanitizer/coverage_interface.h>
#include <stdio.h>
#include <assert.h>
static volatile int sink;
__attribute__((noinline)) void bar() { sink = 2; }
__attribute__((noinline)) void foo() { sink = 1; }

// In MSVC 2015, printf is an inline function, which causes this test to fail as
// it introduces an extra coverage point. Define away printf on that platform to
// avoid the issue.
#if _MSC_VER >= 1900
# define printf(arg, ...)
#endif

#define GET_AND_PRINT_COVERAGE()                                       \
  bitset = 0;                                                  \
  for (size_t i = 0; i < n_guards; i++)                        \
    if (guards[i]) bitset |= 1U << i;                          \
  printf("line %d: bitset %zd total: %zd\n", __LINE__, bitset, \
         __sanitizer_get_total_unique_coverage());

#define IS_POWER_OF_TWO(a) ((a & ((a) - 1)) == 0)

int main() {
  size_t *guards = 0;
  size_t bitset;
  size_t n_guards = __sanitizer_get_coverage_guards(&guards);

  GET_AND_PRINT_COVERAGE();
  size_t main_bit = bitset;
  assert(IS_POWER_OF_TWO(main_bit));

  foo();
  GET_AND_PRINT_COVERAGE();
  size_t foo_bit = bitset & ~main_bit;
  assert(IS_POWER_OF_TWO(foo_bit));

  bar();
  GET_AND_PRINT_COVERAGE();
  size_t bar_bit = bitset & ~(main_bit | foo_bit);
  assert(IS_POWER_OF_TWO(bar_bit));

  __sanitizer_reset_coverage();
  assert(__sanitizer_get_total_unique_coverage() == 0);
  GET_AND_PRINT_COVERAGE();
  assert(bitset == 0);

  foo();
  GET_AND_PRINT_COVERAGE();
  assert(bitset == foo_bit);

  bar();
  GET_AND_PRINT_COVERAGE();
  assert(bitset == (foo_bit | bar_bit));
}