llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

llvm-cov: wrong coverage with a function pointer points to the exit() function

Open llvmbot opened this issue 6 years ago • 3 comments

Bugzilla Link 45850
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @dwblaikie

Extended Description

$ clang --version clang version 11.0.0 (/home/yibiao/.cache/yay/llvm-git/llvm-project 871beba234a83a2a02da9dedbd59b91a1bfbd7af) Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin

$ clang -O0 -w -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| |void exit(int); 2| |void (*myexit)(int); 3| | 4| |int main() 5| 1|{ 6| 1| int i = 0, j = 0; 7| 1| myexit = exit; 8| 3| for (i=0;i<5;i++) { 9| 2| if (i) 10| 1| myexit(0); 11| 2| j++; 12| 2| } 13| 1|}


Line 11 is wrongly marked as executed twice. myexit is a function pointer. When setting breakpoint for Line 11 in lldb, only hit one time.

When replace Line 10 with exit(0), the coverage will be correct as follow: 1| |void exit(int); 2| |void (*myexit)(int); 3| | 4| |int main() 5| 1|{ 6| 1| int i = 0, j = 0; 7| 1| myexit = exit; 8| 2| for (i=0;i<5;i++) { 9| 2| if (i) 10| 1| exit(0); // myexit(0); 11| 1| j++; 12| 1| } 13| 1|}

$ cat small.c void exit(int); void (*myexit)(int);

int main() { int i = 0, j = 0; myexit = exit; for (i=0;i<5;i++) { if (i) exit(0); // myexit(0); j++; } }

llvmbot avatar May 08 '20 19:05 llvmbot

Yeah, sounds like a bug to me. I can confirm the counts/behavior change depending on whether the call is direct or indirect.

dwblaikie avatar Dec 17 '20 07:12 dwblaikie

I rechecked this bug and found that the reported code coverage of Line 11 is correct in the latest trunk version.

$ clang --version Ubuntu clang version 16.0.0-++20220905063833+2417618d5ca4-1~exp1~20220905183932.719

$ clang -O0 -w -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| |void exit(int); 2| |void (*myexit)(int); 3| | 4| |int main() 5| 1|{ 6| 1| int i = 0, j = 0; 7| 1| myexit = exit; 8| 2| for (i=0;i<5;i++) { 9| 2| if (i) 10| 1| myexit(0); 11| 1| j++; 12| 1| } 13| 1|}

iamanonymouscs avatar Sep 07 '22 01:09 iamanonymouscs

I was wondering that whether this bug should be closed as fixed.

iamanonymouscs avatar Sep 08 '22 11:09 iamanonymouscs