我试图使用地址消毒液使用(-fsanitize=address),我不确定它是属于CFLAGS还是LDFLAGS。实际上,添加到LDFLAGS中似乎很好,但我不知道这是巧合还是应该是这样。
编译本身是否需要-fsanitize=address,还是为链接步骤提供标志就足够了?
发布于 2020-03-28 15:20:42
是编译本身所需的-fsanitize=address,还是为链接步骤提供标志就足够了?
地址消毒仪器的源代码插入额外的检查,所以必须在编译时出现。
只在链接行上提供参数将导致asan运行时被链接到流程中,但除了一个小子集(即通过插入new delete、malloc、free和其他标准函数可以实现的检查)之外,没有实际执行检查。
示例:
1 #include <malloc.h>
2 #include <stdio.h>
3
4 void fn(int *ip)
5 {
6 ip[0] = 1; // BUG: heap buffer overflow
7 }
8
9 int main()
10 {
11 int *ip = malloc(1); // Allocation too small.
12 printf("%d\n", ip[0]); // BUG: heap buffer overflow
13 free(ip);
14 free(ip); // BUG: double free
15 }如果没有检测工具,则只检测到“双空闲”:
gcc -g -c t.c && gcc -fsanitize=address t.o && ./a.out
190
=================================================================
==55787==ERROR: AddressSanitizer: attempting double-free on 0x602000000010 in thread T0:使用工具:printf中的bug和fn中的bug都会被检测到。
gcc -g -c -fsanitize=address t.c && gcc -fsanitize=address t.o && ./a.out
=================================================================
==58202==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000010 at pc 0x564565639252 bp 0x7ffe36b0a560 sp 0x7ffe36b0a558
READ of size 4 at 0x602000000010 thread T0
#0 0x564565639251 in main /tmp/t.c:12https://stackoverflow.com/questions/60830562
复制相似问题