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

Incorrect call frame info on X86_64 target

Open djtodoro opened this issue 5 years ago • 3 comments

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 testf2 + 6 frame #&#8203;1: 0x000000000040116c testcrasher + 12 frame #​2: 0x000000000040115b testf2 + 11 frame #&#8203;3: 0x000000000040116c testcrasher + 12 frame #​4: 0x000000000040115b testf2 + 11 frame #&#8203;5: 0x000000000040116c testcrasher + 12 frame #​6: 0x000000000040115b testf2 + 11 frame #&#8203;7: 0x000000000040116c testcrasher + 12 frame #​8: 0x000000000040115b testf2 + 11 frame #&#8203;9: 0x000000000040116c testcrasher + 12 frame #​10: 0x000000000040115b testf2 + 11 frame #&#8203;11: 0x000000000040116c testcrasher + 12 frame #​12: 0x000000000040115b testf2 + 11 frame #&#8203;13: 0x000000000040116c testcrasher + 12 ...

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-gcccrasher + 14 frame #&#8203;1: 0x000000000040118c test-gccf2 + 19 frame #​2: 0x000000000040116f test-gcccrasher + 34 frame #&#8203;3: 0x00000000004011ac test-gccf1 + 27 frame #​4: 0x00000000004011bf test-gccmain + 14 frame #&#8203;5: 0x00007fea1df1c555 libc.so.6__libc_start_main + 245 frame #​6: 0x0000000000401089 test-gcc`_start + 41

This has been reproduced with x86_64 GNU/Linux platform.

djtodoro avatar May 13 '21 12:05 djtodoro

Thanks! I haven't looked into the generated code, but if this is the scenario, I am closing the bug.

djtodoro avatar May 18 '21 08:05 djtodoro

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.

dwblaikie avatar May 17 '21 22:05 dwblaikie

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.

llvmbot avatar May 17 '21 20:05 llvmbot