hyuk myeong
hyuk myeong

Reputation: 325

Why LSan (with gcc) doesn't find memory leaks which are allocated by mmap?

I tested it with below code

// 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);
}

I build like below and run it
And as the result shows LSan only found a leak with malloc
Is there any option that force it to find leaks with mmap or LSan is just designed to do so?

$ 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).

Upvotes: 0

Views: 1154

Answers (1)

hyuk myeong
hyuk myeong

Reputation: 325

I've got an answer from LSan github
And so please refer below link if someone want to know why LSan has this policy

https://github.com/google/sanitizers/issues/1224

Upvotes: 1

Related Questions