Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leak sanitizer doesn't detect leak #1594

Open
hroash opened this issue Nov 24, 2022 · 0 comments
Open

leak sanitizer doesn't detect leak #1594

hroash opened this issue Nov 24, 2022 · 0 comments

Comments

@hroash
Copy link

hroash commented Nov 24, 2022

I'm trying Leak sanitizer with Asan using clang version : 14.0.0-1ubuntu1
test 1:

  __attribute__((noinline))
  void my_leak(){
    int **p=(int **)malloc(7*sizeof(int *));
    p[2] = (int*)malloc (17*sizeof(int ));
  }
  int main() {  
     my_leak();
    return 0;
  }

when I compiled : clang -g -m64 -fsanitize=address memory-leak.c -o memory-leak.clang.m64
when I run it : ./memory-leak.clang.m64 , 2 leaks was detected as expected:

=================================================================
==59354==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 56 byte(s) in 1 object(s) allocated from:
#0 0x55696842a14e in __interceptor_malloc ((mypath)/memory-leak.clang.m64+0xa114e) (BuildId: c3a46fdb8fa5c44dc8fec15e9bcc65f245a4af1f)
#1 0x556968464eb1 in my_leak (mypath)/memory-leak.c:139:19
#2 0x556968464f23 in main (mypath)/memory-leak.c:150:3
#3 0x7f565ca67d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16

Indirect leak of 68 byte(s) in 1 object(s) allocated from:
#0 0x55696842a14e in __interceptor_malloc ((mypath)/memory-leak.clang.m64+0xa114e) (BuildId: c3a46fdb8fa5c44dc8fec15e9bcc65f245a4af1f)
#1 0x556968464ebf in my_leak (mypath)/memory-leak.c:140:16
#2 0x556968464f23 in main (mypath)/memory-leak.c:150:3
#3 0x7f565ca67d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16

SUMMARY: AddressSanitizer: 124 byte(s) leaked in 2 allocation(s).

but when I'm compiling with the same flags the same leak with wrapper function and run it ,no leak was detected:
test 2:

  __attribute__((noinline))
  void my_leak(){
    int **p=(int **)malloc(7*sizeof(int *));
    p[2] = (int*)malloc (17*sizeof(int ));
  }
  
  void wrap_function(){
    my_leak(); 
  }
  
  int main() {  
    wrap_function();
    return 0;
  }

why this is happening?

Moreover when I trying to force the detection with __lsan_do_leak_check() sometimes it help and sometimes it's not
test 3: 2 leaks was detected as expected

  #include <sanitizer/lsan_interface.h>
  __attribute__((noinline))
  void my_leak(){
    int **p=(int **)malloc(7*sizeof(int *));
    p[2] = (int*)malloc (17*sizeof(int ));   
  }
  void wrap_function(){
    my_leak(); 
    __lsan_do_leak_check();
  }
  int main() {  
    wrap_function();
    return 0;
  }

so if leaks exist and Lsan know it, why sometime the leak was reported and sometimes no?

additionally when compiling with m32 flag:

test 4: 1 leak was detected as expected

  __attribute__((noinline))
  void my_leak(){
    int **p=(int **)malloc(7*sizeof(int *));
    p[2] = (int*)malloc (17*sizeof(int ));
    free(p);  
  }

  int main() {  
    my_leak();
    return 0;
  }

test 5 : no leak was detected....

  __attribute__((noinline))
  void my_leak(){
    int **p=(int **)malloc(7*sizeof(int *));
    p[2] = (int*)malloc (17*sizeof(int ));
    free(p);    
  }
  void wrap_function(){
    my_leak(); 
  }
  int main() {  
    wrap_function();
    return 0;
  }

test 5: 1 leak was detected by force

 #include <sanitizer/lsan_interface.h>
  __attribute__((noinline))
  void my_leak(){
    int **p=(int **)malloc(7*sizeof(int *));
    p[2] = (int*)malloc (17*sizeof(int ));
    free(p);    
  }
  void wrap_function(){
    my_leak(); 
    __lsan_do_leak_check();
  }
  int main() {  
    wrap_function();
    return 0;
  }

test 6: no leak was detected....

   #include <sanitizer/lsan_interface.h>
  __attribute__((noinline))
  void my_leak(){
    int **p=(int **)malloc(7*sizeof(int *));
    p[2] = (int*)malloc (17*sizeof(int ));
    free(p);    
  }
  void wrap_function(){
    my_leak(); 
  }
  int main() {  
    wrap_function();
     __lsan_do_leak_check();
    return 0;
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant