Incorrect call frame info on X86_64 target
| Bugzilla Link | 50327 |
| Resolution | INVALID |
| Resolved on | May 18, 2021 01:23 |
| Version | trunk |
| OS | Linux |
| CC | @adrian-prantl,@dwblaikie,@JDevlieghere,@walkerkd,@pogo59 |
Extended Description
$ cat test.c #include <stdlib.h>
int f2(char *input) { return crasher(input, 1); } int crasher(char *input, int crash) { if (crash) input = NULL; input[0] = '1'; input[1] = '2'; input[2] = '3'; input[3] = '\0'; f2(input); if (crash) return 0; else return 1; }
int f1() { char *input = (char *)malloc(sizeof(char) * 10); return crasher(input, 0); } int main(int argc, char **argv) { f1(); return 0; }
This small example when compiled with -O1 produces call frame info that causes incorrect backtrace in GDB.
$ clang -O1 test.c -o test $ ./test Segmentation fault (core dumped) $ lldb test -c core.test ... (lldb) bt
- thread #1, name = 'test', stop reason = signal SIGSEGV
- frame #0: 0x0000000000401156 test
f2 + 6 frame #​1: 0x000000000040116c testcrasher + 12 frame #2: 0x000000000040115b testf2 + 11 frame #​3: 0x000000000040116c testcrasher + 12 frame #4: 0x000000000040115b testf2 + 11 frame #​5: 0x000000000040116c testcrasher + 12 frame #6: 0x000000000040115b testf2 + 11 frame #​7: 0x000000000040116c testcrasher + 12 frame #8: 0x000000000040115b testf2 + 11 frame #​9: 0x000000000040116c testcrasher + 12 frame #10: 0x000000000040115b testf2 + 11 frame #​11: 0x000000000040116c testcrasher + 12 frame #12: 0x000000000040115b testf2 + 11 frame #​13: 0x000000000040116c testcrasher + 12 ...
- frame #0: 0x0000000000401156 test
With gcc, it looks ok: $ gcc -O1 test.c -o test-gcc $ ./test-gcc Segmentation fault (core dumped) $ lldb ./test-gcc -c core.test-gcc ... (lldb) bt
- thread #1, name = 'test-gcc', stop reason = signal SIGSEGV
- frame #0: 0x000000000040115b test-gcc
crasher + 14 frame #​1: 0x000000000040118c test-gccf2 + 19 frame #2: 0x000000000040116f test-gcccrasher + 34 frame #​3: 0x00000000004011ac test-gccf1 + 27 frame #4: 0x00000000004011bf test-gccmain + 14 frame #​5: 0x00007fea1df1c555 libc.so.6__libc_start_main + 245 frame #6: 0x0000000000401089 test-gcc`_start + 41
- frame #0: 0x000000000040115b test-gcc
This has been reproduced with x86_64 GNU/Linux platform.
Thanks! I haven't looked into the generated code, but if this is the scenario, I am closing the bug.
Yep, Anantha's description seems correct to me. The behavior of the program with UB is unpredictable - LLVM's resulting behavior seems fine to me.
This is not call frame related issue. There is an infinite recursion in the code due to compiler optimization in function crasher. This program can terminate when there is access to NULL pointer. Compiler has eliminated the NULL access. Hence crasher calls f2() and f2 calls crasher in an infinite loop.