xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/Linux/coverage-missing.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // Test for "sancov.py missing ...".
2*7c3d14c8STreehugger Robot 
3*7c3d14c8STreehugger Robot // First case: coverage from executable. main() is called on every code path.
4*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -fsanitize-coverage=func %s -o %t -DFOOBAR -DMAIN
5*7c3d14c8STreehugger Robot // RUN: rm -rf %T/coverage-missing
6*7c3d14c8STreehugger Robot // RUN: mkdir -p %T/coverage-missing
7*7c3d14c8STreehugger Robot // RUN: cd %T/coverage-missing
8*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=coverage=1:coverage_dir=%T/coverage-missing %run %t
9*7c3d14c8STreehugger Robot // RUN: %sancov print *.sancov > main.txt
10*7c3d14c8STreehugger Robot // RUN: rm *.sancov
11*7c3d14c8STreehugger Robot // RUN: [ $(cat main.txt | wc -l) == 1 ]
12*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=coverage=1:coverage_dir=%T/coverage-missing %run %t x
13*7c3d14c8STreehugger Robot // RUN: %sancov print *.sancov > foo.txt
14*7c3d14c8STreehugger Robot // RUN: rm *.sancov
15*7c3d14c8STreehugger Robot // RUN: [ $(cat foo.txt | wc -l) == 3 ]
16*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=coverage=1:coverage_dir=%T/coverage-missing %run %t x x
17*7c3d14c8STreehugger Robot // RUN: %sancov print *.sancov > bar.txt
18*7c3d14c8STreehugger Robot // RUN: rm *.sancov
19*7c3d14c8STreehugger Robot // RUN: [ $(cat bar.txt | wc -l) == 4 ]
20*7c3d14c8STreehugger Robot // RUN: %sancov missing %t < foo.txt > foo-missing.txt
21*7c3d14c8STreehugger Robot // RUN: sort main.txt foo-missing.txt -o foo-missing-with-main.txt
22*7c3d14c8STreehugger Robot // The "missing from foo" set may contain a few bogus PCs from the sanitizer
23*7c3d14c8STreehugger Robot // runtime, but it must include the entire "bar" code path as a subset. Sorted
24*7c3d14c8STreehugger Robot // lists can be tested for set inclusion with diff + grep.
25*7c3d14c8STreehugger Robot // RUN: ( diff bar.txt foo-missing-with-main.txt || true ) | not grep "^<"
26*7c3d14c8STreehugger Robot 
27*7c3d14c8STreehugger Robot // Second case: coverage from DSO.
28*7c3d14c8STreehugger Robot // cd %T
29*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -fsanitize-coverage=func %s -o %dynamiclib -DFOOBAR -shared -fPIC
30*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -fsanitize-coverage=func %s %dynamiclib -o %t -DMAIN
31*7c3d14c8STreehugger Robot // RUN: export LIBNAME=`basename %dynamiclib`
32*7c3d14c8STreehugger Robot // RUN: rm -rf %T/coverage-missing
33*7c3d14c8STreehugger Robot // RUN: mkdir -p %T/coverage-missing
34*7c3d14c8STreehugger Robot // RUN: cd %T/coverage-missing
35*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=coverage=1:coverage_dir=%T/coverage-missing %run %t x
36*7c3d14c8STreehugger Robot // RUN: %sancov print $LIBNAME.*.sancov > foo.txt
37*7c3d14c8STreehugger Robot // RUN: rm *.sancov
38*7c3d14c8STreehugger Robot // RUN: [ $(cat foo.txt | wc -l) == 2 ]
39*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=coverage=1:coverage_dir=%T/coverage-missing %run %t x x
40*7c3d14c8STreehugger Robot // RUN: %sancov print $LIBNAME.*.sancov > bar.txt
41*7c3d14c8STreehugger Robot // RUN: rm *.sancov
42*7c3d14c8STreehugger Robot // RUN: [ $(cat bar.txt | wc -l) == 3 ]
43*7c3d14c8STreehugger Robot // RUN: %sancov missing %dynamiclib < foo.txt > foo-missing.txt
44*7c3d14c8STreehugger Robot // RUN: ( diff bar.txt foo-missing.txt || true ) | not grep "^<"
45*7c3d14c8STreehugger Robot 
46*7c3d14c8STreehugger Robot // REQUIRES: x86-target-arch
47*7c3d14c8STreehugger Robot // XFAIL: android
48*7c3d14c8STreehugger Robot 
49*7c3d14c8STreehugger Robot #include <stdio.h>
50*7c3d14c8STreehugger Robot 
51*7c3d14c8STreehugger Robot void foo1();
52*7c3d14c8STreehugger Robot void foo2();
53*7c3d14c8STreehugger Robot void bar1();
54*7c3d14c8STreehugger Robot void bar2();
55*7c3d14c8STreehugger Robot void bar3();
56*7c3d14c8STreehugger Robot 
57*7c3d14c8STreehugger Robot #if defined(FOOBAR)
foo1()58*7c3d14c8STreehugger Robot void foo1() { fprintf(stderr, "foo1\n"); }
foo2()59*7c3d14c8STreehugger Robot void foo2() { fprintf(stderr, "foo2\n"); }
60*7c3d14c8STreehugger Robot 
bar1()61*7c3d14c8STreehugger Robot void bar1() { fprintf(stderr, "bar1\n"); }
bar2()62*7c3d14c8STreehugger Robot void bar2() { fprintf(stderr, "bar2\n"); }
bar3()63*7c3d14c8STreehugger Robot void bar3() { fprintf(stderr, "bar3\n"); }
64*7c3d14c8STreehugger Robot #endif
65*7c3d14c8STreehugger Robot 
66*7c3d14c8STreehugger Robot #if defined(MAIN)
main(int argc,char ** argv)67*7c3d14c8STreehugger Robot int main(int argc, char **argv) {
68*7c3d14c8STreehugger Robot   switch (argc) {
69*7c3d14c8STreehugger Robot     case 1:
70*7c3d14c8STreehugger Robot       break;
71*7c3d14c8STreehugger Robot     case 2:
72*7c3d14c8STreehugger Robot       foo1();
73*7c3d14c8STreehugger Robot       foo2();
74*7c3d14c8STreehugger Robot       break;
75*7c3d14c8STreehugger Robot     case 3:
76*7c3d14c8STreehugger Robot       bar1();
77*7c3d14c8STreehugger Robot       bar2();
78*7c3d14c8STreehugger Robot       bar3();
79*7c3d14c8STreehugger Robot       break;
80*7c3d14c8STreehugger Robot   }
81*7c3d14c8STreehugger Robot }
82*7c3d14c8STreehugger Robot #endif
83