我用下面的代码测试了它
// main.c
#include <sys/mman.h>
#include <string.h>
#include <stdlib.h>
int main() {
int* ptr1 = (int*)malloc(1);
int* ptr2 = (int*)mmap(0, 4096*10, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
}
我像下面那样构建并运行它
结果显示 LSan 只发现了 malloc 泄漏
是否有任何选项可以强制它使用 mmap 查找泄漏或 LSan 只是为了这样做而设计的?
$ gcc -fsanitize=address -O0 main.cpp
$ ASAN_OPTIONS=detect_leaks=1 ./a.out
=================================================================
==14654==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 1 byte(s) in 1 object(s) allocated from:
#0 0x7fc1054f0b50 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb50)
#1 0x55c984e2283b in main (/home/work/a.out+0x83b)
#2 0x7fc105042b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
SUMMARY: AddressSanitizer: 1 byte(s) leaked in 1 allocation(s).