[[LLVM-COV] #if #endif in the source code lead to incorrect coverage report when exit() statement follows "while() ;"
| Bugzilla Link | 41849 |
| Version | trunk |
| OS | Windows NT |
| Reporter | LLVM Bugzilla Contributor |
| CC | @dwblaikie |
Extended Description
$ clang -v clang version 9.0.0-svn358899-1~exp1+0~20190422164136.937~1.gbpac4d8e (trunk) Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8.5 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.5.0 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.5 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.5.0 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.4.0 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0 Candidate multilib: .;@m64 Candidate multilib: 32;@m32 Candidate multilib: x32;@mx32 Selected multilib: .;@m64
$ cat small.c extern void abort (void); extern void exit (int);
int main (void) { #if INT_MAX >= 2147483647 struct { int count: 31; } s = { 0 }; while (s.count--) ; // abort (); #elif INT_MAX >= 32767 abort(1); #else #endif exit (2); }
$ clang -w -O0 -g -fcoverage-mapping -fprofile-instr-generate=small.profraw small.c; ./a.out; echo $?; llvm-profdata merge small.profraw -o small.profdata; llvm-cov show a.out -instr-profile=small.profdata small.c > small.c.lcov; cat small.c.lcov 2 1| |extern void abort (void); 2| |extern void exit (int); 3| | 4| |int 5| |main (void) 6| 1|{ 7| 1|#if INT_MAX >= 2147483647 8| 1| struct { int count: 31; } s = { 0 }; 9| 1| while (s.count--) 10| 0| ; // abort (); 11| |#elif INT_MAX >= 32767 12| | abort(1); 13| |#else 14| |#endif 15| | exit (2); 16| 1|}
Line # 15 is marked as "" which indicates that it is not an instrumentation site. However, we can found that the exit code of this program is 2. Thus we can determine that line #15 is executed during this execution.
When Line #10 is not removed, Line #15 is marked as executed once as follows.
$ clang -w -O0 -g -fcoverage-mapping -fprofile-instr-generate=small.profraw small.c; ./a.out; echo $?; llvm-profdata merge small.profraw -o small.profdata; llvm-cov show a.out -instr-profile=small.profdata small.c > small.c.lcov; cat small.c.lcov 2 1| |extern void abort (void); 2| |extern void exit (int); 3| | 4| |int 5| |main (void) 6| 1|{ 7| 1|#if INT_MAX >= 2147483647 8| 1| struct { int count: 31; } s = { 0 }; 9| 1| while (s.count--) 10| 0| abort (); 11| |#elif INT_MAX >= 32767 12| | abort(1); 13| |#else 14| |#endif 15| 1| exit (2); 16| 1|}
Looks to me like this program might have undefined behavior (calling "x--" on an int until that evaluates to zero, means the program decrements the zero value (underflowing the integer, which is UB)) so I expect the reachability/coverage of things to be unreliable in such a situation.
clang -w -O0 -g -fcoverage-mapping -fprofile-instr-generate=small.profraw small.c; ./a.out; llvm-profdata merge small.profraw -o small.profdata; llvm-cov show a.out -instr-profile=small.profdata small.c > small.c.lcov; cat small.c.lcov
1| |extern void abort (void);
2| |extern void exit (int);
3| |
4| |int
5| |main (void)
6| 1|{
7| 1|#if INT_MAX >= 2147483647
8| 1| struct { int count: 31; } s = { 0 };
9| 1| while (s.count--)
10| 0| ; // abort ();
11| |#elif INT_MAX >= 32767
12| | abort(1);
13| |#else
14| |#endif
15| 1| exit (2);
16| 1|}
$ clang --version
Ubuntu clang version 16.0.0-++20220905063833+2417618d5ca4-1exp120220905183932.719
As can be seen, the reported code coverage for Line #15 is correct in the trunk version.
I was wondering that whether this bug should be also closed as fixed. Thank you very much.