Reputation: 33
I'm trying to use -fsanitize=address
with gcc
.
I declare global variable(ex. int*
) and allocate memory with malloc, then I didn't call free function. I expect the sanitizer will show up error message about memory leak, but it exit with no error message.
So, I use local variable for test. Sanitizer works well on that test code. I put my codes below.
this is a global variable code.
#include<stdio.h>
#include<stdlib.h>
int *gv;
int main(){
gv = (int*)malloc(sizeof(int)*4);
printf("yooooolooooooo\n");
return 0;
}
and this is local variable code.
#include<stdio.h>
#include<stdlib.h>
int main(){
int *gv = (int*)malloc(sizeof(int)*4);
printf("yooooolooooooo\n");
return 0;
}
I compile with gcc -fsanitize=address -o test test.c
.
upper code shows me just
yooooolooooooo
but local variable code shows
yooooolooooooo
=================================================================
==15484==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 16 byte(s) in 1 object(s) allocated from:
#0 0x7f6e43395b60 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xedb60)
#1 0x564b5f8bd936 in main (/home/jiho/lab/test+0x936)
#2 0x7f6e42ed8b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
SUMMARY: AddressSanitizer: 16 byte(s) leaked in 1 allocation(s).
I wonder why sanitizer doesn't works with global variable...
thank you and i hope someone knows about it.
ps. my system is ubuntu 18.04 and x86_64.
Upvotes: 3
Views: 1375
Reputation: 53
Based on information in this link: https://github.com/google/sanitizers/issues/705, __sanitizer_print_memory_profile function (#include <sanitizer/common_interface_defs.h>) can be used to print allocation backtrace of memory that is not yet freed but still reachable. I found it useful for tracking memory allocations / leaks from global variable
Upvotes: 0
Reputation: 21954
Similar to Valgrind LeakSanitizer reports only "direct" leaks i.e. addresses that are no longer accessible from any existing user data (called "root-set" in LSan design document). In case of global variable the address is obviously still accessible.
Upvotes: 4