triplev
triplev

Reputation: 9

Why does adress sanitizer not work for shared library?

I have 3 sources. 2 of them (a.cpp and b.cpp) I want to use as shared library.

a.cpp:


#include <string.h>

int XXX[2] = {2, 3};
extern int YYY[];

int test() {
  memset(XXX, 0, 2 * sizeof(int));
  int res = YYY[-1];
  return res;
}

b.cpp:

int YYY[3] = {3, 2, 1};

main.cpp:

extern int test();

int main() { return test(); }

I try to compile the code using the options to enable sanitizer, but it doesn't work. There are no errors in the startup process:

g++ -fsanitize=address -fno-omit-frame-pointer -O0 -fPIC a.cpp -c -o a.o
g++ -fsanitize=address -fno-omit-frame-pointer -O0 -fPIC -shared a.o -o a.so -Wl,--emit-relocs

g++ -fsanitize=address -fno-omit-frame-pointer -O0 -fPIC b.cpp -c -o b.o
g++ -fsanitize=address -fno-omit-frame-pointer -O0 -fPIC -shared b.o -o b.so -Wl,--emit-relocs

g++ -fsanitize=address -fno-omit-frame-pointer -O0 main.cpp -c -o main.o

g++ -fsanitize=address -fno-omit-frame-pointer main.o -o test.elf a.so b.so

./test.elf

echo $?
0

If I compile everything differently and refuse shared libraries, then asan works correctly.

g++ -fsanitize=address -fno-omit-frame-pointer main.o -o test.elf a.cpp b.cpp

./test.elf

==5682==ERROR: AddressSanitizer: global-buffer-overflow on address 0x55d80ef8b0fc at pc 0x55d80ef88200 bp 0x7ffc94fc6db0 sp 0x7ffc94fc6da8
...

Why is this so?

I've tried various combinations with shared libraries and nothing worked for me. The only option that works is if I move all the content from b.cpp to a.cpp, then asan will detect an error during execution.

g++ version:

g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/12/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 12.2.0-14' --with-bugurl=file:///usr/share/doc/gcc-12/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-12 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-12-bTRWOB/gcc-12-12.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-12-bTRWOB/gcc-12-12.2.0/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (Debian 12.2.0-14) 

I tried: https://stackoverflow.com/a/47022141/28866435 But it's not helped me, case still not working. The problem is not the options, but the optimizations. Because of that it doesn't work, in my opinion.

Upvotes: 0

Views: 49

Answers (0)

Related Questions