【问题标题】:Should -fsanitize=address go into CFLAGS or LDFLAGS?-fsanitize=address 应该进入 CFLAGS 还是 LDFLAGS?
【发布时间】:2020-03-24 11:54:42
【问题描述】:

我正在尝试通过 (-fsanitize=address) 使用地址清理程序,但我不确定它属于 CFLAGS 还是 LDFLAGS。仅将其添加到 LDFLAGS 时,它实际上似乎工作正常,但我不知道这是巧合还是应该是这样的。

编译本身需要-fsanitize=address,还是为链接步骤提供标志就足够了?

【问题讨论】:

    标签: address-sanitizer sanitizer


    【解决方案1】:

    编译本身需要 -fsanitize=address,还是为链接步骤提供标志就足够了?

    Address Sanitizer 工具 源代码 以插入额外的检查,因此在编译时必须存在。

    仅在链接行上提供参数导致 asan runtime 被链接到进程中,但实际上没有进行任何检查,除了一小部分 - 即通过插入 @987654322 可实现的检查@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 中的错误和fn 中的错误也被检测到。

    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:12
    

    【讨论】:

    猜你喜欢
    • 2017-06-24
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 2013-06-25
    • 1970-01-01
    • 2022-01-02
    • 2012-04-08
    相关资源
    最近更新 更多