Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC Address Sanitizer - blacklisting library functions (specifically boost::test)

All of my unit-test code is based around boost::test. I have just tried the GCC Address sanitizer and it reports some issues with boost::test:

==25309==ERROR: AddressSanitizer: heap-use-after-free on address 0xf5801344 at pc 0x8259412 bp 0xff9966c8 sp 0xff9966bc
READ of size 4 at 0xf5801344 thread T0
#0 0x8259411 in boost::unit_test::framework::run(unsigned long, bool)     ../common/lib/boost/boost/test/impl/framework.ipp:450
#1 0x82732f7 in boost::unit_test::unit_test_main(bool (*)(), int, char**) ../common/lib/boost/boost/test/impl/unit_test_main.ipp:185
#2 0x827b5a3 in main ../common/lib/boost/boost/test/unit_test.hpp:59
#3 0x213ce5 in __libc_start_main (/lib/libc.so.6+0x16ce5)
#4 0x8238680 (/home/marpashl/lte/sw/build/x86/bin/framework_unit_test+0x8238680)

I would like to hide this message (as it is for a known error in a test library) so that I only see issues within my own code.

Is there a way of doing this with GCC?

Note Compiler version GCC: /opt/gcc-x86-4.9.2/bin/c++

I found that with CLANG files can be blacklisted using -fsanitize-blacklist=blacklist.txt but this is not currently available for GCC.

like image 545
mark Avatar asked Jan 15 '15 14:01

mark


People also ask

What is AddressSanitizer in GCC?

Address Sanitizer is a tool developed by Google detect memory access error such as use-after-free and memory leaks. It is built into GCC versions >= 4.8 and can be used on both C and C++ codes.

How do I turn off AddressSanitizer?

AddressSanitizer can optionally detect stack use after return problems. This is available by default, or explicitly ( -fsanitize-address-use-after-return=runtime ). To disable this check at runtime, set the environment variable ASAN_OPTIONS=detect_stack_use_after_return=0 .

What does AddressSanitizer do?

AddressSanitizer (or ASan) is an open source programming tool that detects memory corruption bugs such as buffer overflows or accesses to a dangling pointer (use-after-free). AddressSanitizer is based on compiler instrumentation and directly mapped shadow memory.

How do I enable AddressSanitizer?

AddressSanitizer is integrated with the Visual Studio project system, the CMake build system, and the IDE. Projects can enable AddressSanitizer by setting a project property, or by using one extra compiler option: /fsanitize=address .


1 Answers

If the sanitize-blacklist is not available, but you have access to the source code, you can exclude individual functions from being sanitized using a function attribute:

It is supported by Clang (3.3+) and GCC (4.8+). You can define the following macro:

#if defined(__clang__) || defined (__GNUC__)
# define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
#else
# define ATTRIBUTE_NO_SANITIZE_ADDRESS
#endif
...
ATTRIBUTE_NO_SANITIZE_ADDRESS
void ThisFunctionWillNotBeInstrumented() {...}

See this page for more details.

like image 136
Sjlver Avatar answered Oct 11 '22 23:10

Sjlver