linux 程序退出后,LeakSanitizer未生成报告

yx2lnoni  于 2022-12-29  发布在  Linux
关注(0)|答案(1)|浏览(304)

我正在编译地址消毒剂,我试图得到泄漏消毒剂报告,但它只产生一个地址消毒剂报告,而不是产生泄漏消毒剂报告后,程序退出的某种原因。
我在Centos 7.9上用G ++9.3.1编译,Cmake版本为3.16.2。
我正在编译
列表(添加SAN标志"-fsanitize =地址""-fno省略帧指针""-g3")
启用泄漏清理程序(根据文档,应在Linux上使用地址清理程序启用)
我设置

Environment="ASAN_OPTIONS=log_path=/var/log/foo_asan.log:detect_leaks=1"

在service文件中,然后用systemd运行我的程序(也在Centos 7. 9上)。用systemctl停止程序会像预期的那样创建一个foo_asan. log文件,但是日志文件只有AddressSanitizer报告,而没有LeakSanitizer报告。我如何获得Leaksanitizer报告?

brgchamk

brgchamk1#

日志文件只有AddressSanitizer报告,而没有LeakSanitizer报告。
默认情况下,AddressSanitizer在出错时会中止。LeakSanitizer只在exit上运行,因为你的程序不是exit,所以你不会得到泄漏报告。
下面是一个同时存在内存泄漏和缓冲区溢出的示例程序:

#include <malloc.h>

void *alloc(size_t sz) { return malloc(sz); }
void fn(char *p, int n) { p[n] = '\0'; }

int main()
{
  const int sz = 10;
  fn(alloc(sz), sz);

  return 0;
}

如果我用clang -g -fsanitize=address leaky.c -o leaky构建它,然后用ASAN_OPTIONS=detect_leaks=1运行它,我只会得到错误报告:

=================================================================
==1130841==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000001a at pc 0x5555a7f31f0f bp 0x7ffc86de9420 sp 0x7ffc86de9418
WRITE of size 1 at 0x60200000001a thread T0
    #0 0x5555a7f31f0e in fn /tmp/leaky.c:4:32
    #1 0x5555a7f31f4c in main /tmp/leaky.c:9:3
...
==1130841==ABORTING     <<<=== Note this.

This answer展示了如何使程序在出错时 * 不 * 中止。当我这样做时,我同时得到AddressSanitizer和LeakSanitizer报告:

clang -g -fsanitize=address -fsanitize-recover=address leaky.c -o leaky
ASAN_OPTIONS=detect_leaks=1:halt_on_error=0 ./leaky

=================================================================
==1131013==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000001a at pc 0x560f9d467f0f bp 0x7ffef258d2b0 sp 0x7ffef258d2a8
WRITE of size 1 at 0x60200000001a thread T0
    #0 0x560f9d467f0e in fn /tmp/leaky.c:4:32
    #1 0x560f9d467f5c in main /tmp/leaky.c:9:3
...
=================================================================
==1131013==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 10 byte(s) in 1 object(s) allocated from:
    #0 0x560f9d42d14e in __interceptor_malloc (/tmp/leaky+0xa314e) (BuildId: d2d081353c37b4b7a8659b1ac39242cfe6259078)
    #1 0x560f9d467eb4 in alloc /tmp/leaky.c:3:33
    #2 0x560f9d467f4f in main /tmp/leaky.c:9:6
    #3 0x7fbf2cc29209 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16

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

相关问题