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

when asan doesn't report anything when link with 3rd party .o and library?? #1420

Open
incity opened this issue Jun 28, 2021 · 6 comments
Open

Comments

@incity
Copy link

incity commented Jun 28, 2021

  1. i write a test.cpp as follow, and compile it with -fsanitize=address -fno-omit-frame-pointer -O0
#include <stdio.h>
#include <stdlib.h>
int main(int, char**) {
    char* buf = (char*)malloc(10);
    buf[0] = 40;
    buf[1] = 0;
    printf("%s\n", buf);
    return 0;
}

and get the follow report :


=================================================================
==24784==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 10 byte(s) in 1 object(s) allocated from:
    #0 0x7f9baf94c602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x40079e in main /home/suyc/wething/test.cpp:36
    #2 0x7f9baf50a83f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2083f)

SUMMARY: AddressSanitizer: 10 byte(s) leaked in 1 allocation(s).

for now everything is right. but when i link it with a 3rd-party .o and a 3rd-party library, the asan doesn't report the error anymore.

first, i compile the test.cpp to test.o :

/usr/bin/g++ -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--gc-sections -Bdynamic -Wl,-z,nocopyreloc -pie -Lout/target/product/x86/obj/lib -Wl,-rpath-link=out/target/product/x86/obj/lib -Wl,-rpath,\$ORIGIN/../lib test.o -Wl,--whole-archive -Wl,--no-whole-archive -lsqlite -o a.out -fsanitize=address -fno-omit-frame-pointer -ldl -lpthread -lm

and then link it with a 3rd-party .o and a 3rd-party .so

/usr/bin/g++ -Lout/target/product/x86/obj/lib -Wl,-rpath,\$ORIGIN/../lib out/target/product/x86/obj/EXECUTABLES/thirdparty_demo_intermediates/thirdparty_demo.o test.o -lthirdpartylib -lsqlite -o test -fsanitize=address -fno-omit-frame-pointer -ldl -lpthread -lm

and i run the test, the asan doesn't have the memory leak report.

the attachment is my 2 executables, one without link to 3rdpart library, another is linked with 3rd -party library named test-3rdparty.
output.zip

i have tried with g++ 5.4 and g++ 7.5

@vitalybuka
Copy link
Contributor

This SEGV has nothing to do with main() from example?

@incity
Copy link
Author

incity commented Jun 29, 2021

i pasted wrong output. i has modified it. @vitalybuka

@incity
Copy link
Author

incity commented Jun 30, 2021

@vitalybuka did you find any problem? how can i solve it

@vitalybuka
Copy link
Contributor

lsan can't guaranty finding all leaks. Probably pointer is still alive somewhere on the stack.
It's very likely if program leaks in main() just before leak checker invoked.
In general, the farther from main(), the higher probability to detect.

@incity
Copy link
Author

incity commented Jun 30, 2021

but if i don't link with 3rd-party .o and 3rd-party .so, the leak can be detected. why the 3rd-party library can affect this?
i still not invoke any function in the library. i tested for the same simple code

#include <stdio.h>
#include <stdlib.h>
int main(int, char**) {
    char* buf = (char*)malloc(10);
    buf[0] = 40;
    buf[1] = 0;
    printf("%s\n", buf);
    return 0;
}

and so far as i know, the only difference is if i link with 3rd-party library, there is some static objects in the library will be initialized before the main function.

@vitalybuka
Copy link
Contributor

you can try to add more leaks into the program assigning into the same var.
If it still does not detect then maybe 3rd party lib disables asan.
E.g.

int main(int, char**) {
char* buf = (char*)malloc(10);
buf[0] = 40;
buf[1] = 0;
printf("%s\n", buf);
buf = (char*)malloc(20);
buf[0] = 41;
buf[1] = 0;
printf("%s\n", buf);
buf = (char*)malloc(30);
buf[0] = 42;
buf[1] = 0;
printf("%s\n", buf);

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

2 participants