gpt4 book ai didi

c - 如何中断 gdb 中的多个 clang/ubsan 警告?

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:36 24 4
gpt4 key购买 nike

拿下面的测试程序(用clang 3.4编译,在gdb 7.6.1下运行):

#include <limits.h>
#include <stdio.h>

int main(void)
{
int a = INT_MAX + 1;
int b = INT_MAX + 2;
printf("Result: a = %d, b = %d\n", a, b);
}

我希望能够使用 gdb 在此处出现未定义行为的第二次时自动停止 (int b = ...)。

如果我编译:

clang -fsanitize=undefined -O0 -ggdb3 -o test test.c

...然后在 gdb 下运行程序只会导致它运行完成:

test.c:6:21: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
test.c:7:21: runtime error: signed integer overflow: 2147483647 + 2 cannot be represented in type 'int'
Result: a = -2147483648, b = -2147483647
[Inferior 1 (process 24185) exited normally]

但是如果我使用:

clang -fsanitize=undefined-trap -fsanitize-undefined-trap-on-error -O0 -ggdb3 -o test test.c

...然后我无法继续超过第一次出现:

Program received signal SIGILL, Illegal instruction.
0x0000000000400556 in main () at test.c:6
6 int a = INT_MAX + 1;
(gdb) c
Continuing.

Program terminated with signal SIGILL, Illegal instruction.
The program no longer exists.

是否有可能在标记未定义行为时(且仅在标记时)让 gdb 中断,但让程序继续运行?我正在寻找一种技术,它不仅适用于这个示例,而且通常情况下,违规行可能在循环内,值可能在运行时确定,等等。

最佳答案

在 x86-64 上,导致 SIGILL 并停止程序的指令是 ud2 ( http://asm.inightmare.org/opcodelst/index.php?op=UD2 )。为了存档您的目标,您可以更改 gdb 对 SIGILL 的处理并使用跳转(您需要在 x86_64 上向 $pc 添加 2):

这是指令 ud2 在 x86_64 上的测试程序代码中的放置方式:

   0x00000000004004f0 <+32>:    0f 85 02 00 00 00       jne    0x4004f8 <main+40>
=> 0x00000000004004f6 <+38>: 0f 0b ud2
0x00000000004004f8 <+40>: b8 ff ff ff 7f mov $0x7fffffff,%eax

这些是必须使用的 gdb 命令:

handle SIGILL stop print nopass
set $pc = $pc + 2

这是您的测试程序的示例:

$ gdb -q ./test
Reading symbols from /home/test...done.
(gdb) handle SIGILL stop print nopass
Signal Stop Print Pass to program Description
SIGILL Yes Yes No Illegal instruction
(gdb) r
Starting program: /home/test

Program received signal SIGILL, Illegal instruction.
0x00000000004004f6 in main () at test.c:6
6 int a = INT_MAX + 1;
(gdb) set $pc = $pc + 2
(gdb) c
Continuing.

Program received signal SIGILL, Illegal instruction.
0x000000000040051f in main () at test.c:7
7 int b = INT_MAX + 2;
(gdb) set $pc = $pc + 2
(gdb) c
Continuing.
Result: a = -2147483648, b = -2147483647
[Inferior 1 (process 7898) exited normally]
(gdb)

有用的链接:

关于c - 如何中断 gdb 中的多个 clang/ubsan 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23798178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com