c++gccboostboost-test

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.


Solution

  • 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.