xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/coverage-pc-buffer.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // Test __sanitizer_coverage_pc_buffer().
2*7c3d14c8STreehugger Robot 
3*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -fsanitize-coverage=edge %s -o %t && %run %t
4*7c3d14c8STreehugger Robot 
5*7c3d14c8STreehugger Robot // UNSUPPORTED: android
6*7c3d14c8STreehugger Robot 
7*7c3d14c8STreehugger Robot #include <assert.h>
8*7c3d14c8STreehugger Robot #include <sanitizer/coverage_interface.h>
9*7c3d14c8STreehugger Robot #include <stdio.h>
10*7c3d14c8STreehugger Robot 
11*7c3d14c8STreehugger Robot static volatile int sink;
bar()12*7c3d14c8STreehugger Robot __attribute__((noinline)) void bar() { sink = 2; }
foo()13*7c3d14c8STreehugger Robot __attribute__((noinline)) void foo() { sink = 1; }
14*7c3d14c8STreehugger Robot 
assertNotZeroPcs(uintptr_t * buf,uintptr_t size)15*7c3d14c8STreehugger Robot void assertNotZeroPcs(uintptr_t *buf, uintptr_t size) {
16*7c3d14c8STreehugger Robot   assert(buf);
17*7c3d14c8STreehugger Robot   for (uintptr_t i = 0; i < size; ++i)
18*7c3d14c8STreehugger Robot     assert(buf[i]);
19*7c3d14c8STreehugger Robot }
20*7c3d14c8STreehugger Robot 
main()21*7c3d14c8STreehugger Robot int main() {
22*7c3d14c8STreehugger Robot   {
23*7c3d14c8STreehugger Robot     uintptr_t *buf = NULL;
24*7c3d14c8STreehugger Robot     uintptr_t sz = __sanitizer_get_coverage_pc_buffer(&buf);
25*7c3d14c8STreehugger Robot     assertNotZeroPcs(buf, sz);
26*7c3d14c8STreehugger Robot     assert(sz);
27*7c3d14c8STreehugger Robot   }
28*7c3d14c8STreehugger Robot 
29*7c3d14c8STreehugger Robot   {
30*7c3d14c8STreehugger Robot     uintptr_t *buf = NULL;
31*7c3d14c8STreehugger Robot     uintptr_t sz = __sanitizer_get_coverage_pc_buffer(&buf);
32*7c3d14c8STreehugger Robot     // call functions for the first time.
33*7c3d14c8STreehugger Robot     foo();
34*7c3d14c8STreehugger Robot     bar();
35*7c3d14c8STreehugger Robot     uintptr_t *buf1 = NULL;
36*7c3d14c8STreehugger Robot     uintptr_t sz1 = __sanitizer_get_coverage_pc_buffer(&buf1);
37*7c3d14c8STreehugger Robot     assertNotZeroPcs(buf1, sz1);
38*7c3d14c8STreehugger Robot     assert(buf1 == buf);
39*7c3d14c8STreehugger Robot     assert(sz1 > sz);
40*7c3d14c8STreehugger Robot   }
41*7c3d14c8STreehugger Robot 
42*7c3d14c8STreehugger Robot   {
43*7c3d14c8STreehugger Robot     uintptr_t *buf = NULL;
44*7c3d14c8STreehugger Robot     uintptr_t sz = __sanitizer_get_coverage_pc_buffer(&buf);
45*7c3d14c8STreehugger Robot     // second call shouldn't increase coverage.
46*7c3d14c8STreehugger Robot     bar();
47*7c3d14c8STreehugger Robot     uintptr_t *buf1 = NULL;
48*7c3d14c8STreehugger Robot     uintptr_t sz1 = __sanitizer_get_coverage_pc_buffer(&buf1);
49*7c3d14c8STreehugger Robot     assertNotZeroPcs(buf1, sz1);
50*7c3d14c8STreehugger Robot     assert(buf1 == buf);
51*7c3d14c8STreehugger Robot     assert(sz1 == sz);
52*7c3d14c8STreehugger Robot   }
53*7c3d14c8STreehugger Robot 
54*7c3d14c8STreehugger Robot   {
55*7c3d14c8STreehugger Robot     uintptr_t *buf = NULL;
56*7c3d14c8STreehugger Robot     uintptr_t sz = __sanitizer_get_coverage_pc_buffer(&buf);
57*7c3d14c8STreehugger Robot     // reset coverage to 0.
58*7c3d14c8STreehugger Robot     __sanitizer_reset_coverage();
59*7c3d14c8STreehugger Robot     uintptr_t *buf1 = NULL;
60*7c3d14c8STreehugger Robot     uintptr_t sz1 = __sanitizer_get_coverage_pc_buffer(&buf1);
61*7c3d14c8STreehugger Robot     assertNotZeroPcs(buf1, sz1);
62*7c3d14c8STreehugger Robot     assert(buf1 == buf);
63*7c3d14c8STreehugger Robot     assert(sz1 < sz);
64*7c3d14c8STreehugger Robot   }
65*7c3d14c8STreehugger Robot }
66