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

Asan memory leak detection on x86 platform #1049

Closed
ganeshemb opened this issue Feb 4, 2019 · 6 comments
Closed

Asan memory leak detection on x86 platform #1049

ganeshemb opened this issue Feb 4, 2019 · 6 comments

Comments

@ganeshemb
Copy link

Hi,

Change description mentioned as part of "https://reviews.llvm.org/D28609" says memory leaks detection is disabled by default, may i know how to enable it at run time (or) compile time?

Sample C program with memory leak
cat asantest.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[])
{
   char *name = (char*) malloc(10);
   strcpy(name, argv[1]);
   printf("Hello, %s!\n", name);
   return 0;
}

gcc -g -m32 -fsanitize=address -fno-omit-frame-pointer asantest.c -o asantest

objdump -a asantest

asantest:     file format elf32-i386
 ldd asantest
        linux-gate.so.1 (0xf778a000)
        libasan.so.3 => /usr/lib32/libasan.so.3 (0xf71e0000)
gcc --version
gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516 

when program run as shown below , no leaks were detected.

./asantest world
Hello, world!

when program run with ASAN_OPTIONS , following error is shown.

ASAN_OPTIONS=detect_leaks=1 ./asantest world
==8192==AddressSanitizer: detect_leaks is not supported on this platform.

My system info

 lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
uname -r
4.9.0-8-amd64

Thanks in advance.

@kcc
Copy link
Contributor

kcc commented Feb 11, 2019

works for current clang:

clang++ ~/misc-c/leak.cc -fsanitize=address -m32  && ./a.out 

=================================================================
==185140==ERROR: LeakSanitizer: detected memory leaks

Doesn't work for my gcc (7.3.0).
Most likely, gcc didn't update the sanitizer run-time for a while.
Please check the current gcc head, then ask in the gcc bugtracker.

@kcc
Copy link
Contributor

kcc commented Feb 11, 2019

(closing since there is nothing to fix in the LLVM trunk)

@kcc kcc closed this as completed Feb 11, 2019
@ganeshemb
Copy link
Author

i tried using clang6.0 to compile my test app but it does not seem to work either, may i know which version of clang are you using ?

clang-6.0 -g -fsanitize=address -fno-omit-frame-pointer -m32 -o asantest asantest.c && ./asantest hi
Hello, hi!

@kcc
Copy link
Contributor

kcc commented Feb 13, 2019

I am using the current clang trunk (9.0), pretty sure it will work the same in 8.0 and 7.0, but please verify.

@lygstate
Copy link

On ubuntu 20.04 clang -m32 not works

lygstate@yonggang-ubuntu2004:~/work/iotjs$ clang --version
clang version 10.0.0-4ubuntu1 
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
lygstate@yonggang-ubuntu2004:~/work/iotjs$ clang -m32 -g3 -fsanitize=address -o asantest asantest.c
lygstate@yonggang-ubuntu2004:~/work/iotjs$ ./asantest abc
Hello, abc!
lygstate@yonggang-ubuntu2004:~/work/iotjs$ cat asantest.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[])
{
   char *name = (char*) malloc(10);
   strcpy(name, argv[1]);
   printf("Hello, %s!\n", name);
   return 0;
}lygstate@yonggang-ubuntu2004:~/work/iotjs$ clang -g3 -fsanitize=address -o asantest asantest.c
lygstate@yonggang-ubuntu2004:~/work/iotjs$ ./asantest abc
Hello, abc!

=================================================================
==69746==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 10 byte(s) in 1 object(s) allocated from:
    #0 0x493a1d in malloc (/home/lygstate/work/iotjs/asantest+0x493a1d)
    #1 0x4c312f in main /home/lygstate/work/iotjs/asantest.c:7:25
    #2 0x7ffb671ab0b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16

SUMMARY: AddressSanitizer: 10 byte(s) leaked in 1 allocation(s).
lygstate@yonggang-ubuntu2004:~/work/iotjs$ 

@vitalybuka
Copy link
Contributor

lsan does not detect 100% of leaks. after return from main() pointer still can be on the stack in some uninitialized stack variable and be visible to the lsan long after leak happens from the c++ perspective.
If you try to do the same in some loop, it's likely will be detected.
Also ASAN_OPTIONS=detect_stack_use_after_return=1 can help

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

4 participants