Bug 64234 - Statically sanitized executable does not export ASan symbols
Summary: Statically sanitized executable does not export ASan symbols
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: sanitizer (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-12-09 09:35 UTC by Yury Gribov
Modified: 2022-11-12 02:55 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Yury Gribov 2014-12-09 09:35:09 UTC
If executable is linked with -static-libasan, it won't export libasan public API (__asan_reportXYZ, etc.), causing dlopens of sanitized shlibs to fail. Same applies to other sanitizers (-static-libubsan, etc.).
Comment 1 Jakub Jelinek 2014-12-09 10:02:03 UTC
I think we can only recommend not to do that, or suggest users to consider -Wl,-E.  The linker automatically makes symbols mentioned in dependent libraries exported from binaries, but e.g. implying -Wl,-E from -static-libasan is IMNSHO undesirable, it doesn't make just the selected few symbols dynamic, but all, and that really should be users decision whether it is desirable or not.
Comment 2 Yury Gribov 2014-12-09 10:18:05 UTC
(In reply to Jakub Jelinek from comment #1)
> I think we can only recommend not to do that

For legacy codebase (e.g. when sanitizing full distributions) you often don't have a choice.

> implying -Wl,-E from -static-libasan is IMNSHO undesirable,
> it doesn't make just the selected few symbols dynamic, but all,

Right, -Wl,-E would be an overkill.  AFAIK Clang automatically appends --dynamic-list=something.syms when it compiles sanitized executable.  Perhaps we could do this as well?
Comment 3 Jakub Jelinek 2014-12-09 10:22:44 UTC
But why do you want to use -static-libasan ?  Just link it dynamically...
--dynamic-list is hard to maintain, aren't there hundreds of symbols that are exported from libasan?
Comment 4 Yury Gribov 2014-12-09 10:35:28 UTC
(In reply to Jakub Jelinek from comment #3)
> But why do you want to use -static-libasan ?  Just link it dynamically...

For one thing it can speed up code by avoiding PLT calls.  

> --dynamic-list is hard to maintain, aren't there hundreds of symbols that
> are exported from libasan?

Indeed there are, upstream has a special script to autogenerate file with symbols.
Comment 5 Yury Gribov 2017-10-31 17:16:28 UTC
Few workarounds for this are listed in https://stackoverflow.com/questions/46682210/undefined-symbol-error-with-static-libasan/46684596#46684596
Comment 6 lo1ol 2022-05-31 08:36:10 UTC
I've the same problem when try to link at runtime shared library compiled with static asan. Here is minimal example:

echo '
#include <stdlib.h>
#include <stdio.h>

void f(){
    int* x = new int;
    printf("kek\n");
}' > lib.cpp

echo '
#include <dlfcn.h>
#include <stdio.h>
int main() {
    printf("lol\n");
    void* handler = dlopen("./libkek.so", RTLD_NOW);
    if (!handler)
        return 1;

    void (* func)() = reinterpret_cast<void(*)()>(dlsym(handler, "_Z1fv"));
    printf("kek\n");
    func();
    printf("cheburek\n");
}' > main.cpp


g++ -fsanitize=address -static-libasan -static-libstdc++ -static-libgcc -fPIC -shared lib.cpp -o libkek.so
g++ -fsanitize=address -static-libasan -static-libstdc++ -static-libgcc -ldl main.cpp
LD_DEBUG=libs ./a.out; echo $?

Part of the output:
...
    347802:     initialize program: ./a.out
    347802:
    347802:
    347802:     transferring control: ./a.out
    347802:
lol
    347802:     ./libkek.so: error: symbol lookup error: undefined symbol: __asan_unregister_globals (fatal)
    347802:
    347802:     calling fini: ./a.out [0]
    347802:
    347802:
    347802:     calling fini: /lib/x86_64-linux-gnu/libm.so.6 [0]
    347802:


With clang everything is ok....

GCC version 12.1
System: Ubuntu 21.10
Comment 7 Boris Kolpackov 2022-07-18 13:07:37 UTC
> But why do you want to use -static-libasan ?  Just link it dynamically...

Another reason is shared linking clobbers executable's RUNPATH: https://github.com/google/sanitizers/issues/1219
Comment 8 lo1ol 2022-11-12 02:55:46 UTC Comment hidden (spam)