2

当我使用 Address Sanitizer(clang v3.4)检测内存泄漏时,我发现使用 -O(except -O0) 选项总是会导致一个 no-leak-detected 结果。

代码很简单:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int* array = (int *)malloc(sizeof(int) * 100);
    for (int i = 0; i < 100; i++) //Initialize
    array[i] = 0;
    return 0;
} 

使用 -O0 编译时,

clang -fsanitize=address -g -O0 main.cpp

它会正确检测内存,

==2978==WARNING: Trying to symbolize code, but external symbolizer is not initialized!

=================================================================
==2978==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 400 byte(s) in 1 object(s) allocated from:
    #0 0x4652f9 (/home/mrkikokiko/sdk/MemoryCheck/a.out+0x4652f9)
    #1 0x47b612 (/home/mrkikokiko/sdk/MemoryCheck/a.out+0x47b612)
    #2 0x7fce3603af44 (/lib/x86_64-linux-gnu/libc.so.6+0x21f44)

SUMMARY: AddressSanitizer: 400 byte(s) leaked in 1 allocation(s).

但是,当 -O 添加时,

clang -fsanitize=address -g -O main.cpp

什么都没有检测到!我在官方文件中没有找到任何关于它的信息。

4

2 回答 2

7

这是因为您的代码已经完全优化掉了。生成的程序集类似于:

main:                                   # @main
    xorl    %eax, %eax
    retq

没有任何调用malloc,就没有内存分配......因此没有内存泄漏。


为了让AddressSanitizer检测内存泄漏,您可以:

  • 编译时禁用优化,正如Simon Kraemer在评论中提到的那样。

  • 标记arrayvolatile防止优化

     main:                                   # @main
            pushq   %rax
            movl    $400, %edi              # imm = 0x190
            callq   malloc                  # <<<<<< call to malloc
            movl    $9, %ecx
    .LBB0_1:                                # =>This Inner Loop Header: Depth=1
            movl    $0, -36(%rax,%rcx,4)
            movl    $0, -32(%rax,%rcx,4)
            movl    $0, -28(%rax,%rcx,4)
            movl    $0, -24(%rax,%rcx,4)
            movl    $0, -20(%rax,%rcx,4)
            movl    $0, -16(%rax,%rcx,4)
            movl    $0, -12(%rax,%rcx,4)
            movl    $0, -8(%rax,%rcx,4)
            movl    $0, -4(%rax,%rcx,4)
            movl    $0, (%rax,%rcx,4)
            addq    $10, %rcx
            cmpq    $109, %rcx
            jne     .LBB0_1
            xorl    %eax, %eax
            popq    %rcx
            retq
    
于 2017-01-09T12:15:55.173 回答
4

查看生成的代码。

GCC 和 Clang 实际上都知道malloc. 因为在我的 Linux/Debian 系统上<stdlib.h> 包含

extern void *malloc (size_t __size) __THROW __attribute_malloc__ __wur;

__attribute_malloc__& _wur(and __THROW) 是在别处定义的宏。阅读GCC 文档中的Common Function Attributes和 Clang 文档

Clang 旨在支持广泛的 GCC 扩展。

我强烈怀疑通过删除它来优化-O 调用。malloc

在我的 Linux/x86-64 机器上使用clang -O -S psbshdk.c(使用 clang 3.8)我确实得到了:

    .globl  main
    .align  16, 0x90
    .type   main,@function
 main:                                   # @main
    .cfi_startproc
 # BB#0:
    xorl    %eax, %eax
    retq
 .Lfunc_end0:
    .size   main, .Lfunc_end0-main
    .cfi_endproc

地址清理程序正在处理发出的二进制文件(不包含任何malloc调用)。

顺便说一句,你可以编译clang -O -g然后使用valgrind,或者编译clang -O -fsanitize=address -g. clang&都gcc能够优化并提供一些调试信息(在优化很多时可能是“近似的”)。

于 2017-01-09T12:14:24.110 回答