8

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.

mark
  • 7,381
  • 5
  • 36
  • 61
  • Would it be possible to set different CFLAGS while compiling the test library? This would exclude the entire library (not just the framework::run function), but at least you could run your program. – Sjlver Jan 15 '15 at 14:58
  • nice suggestion but our unit tests currently use boost::test via header files, as opposed to linking against a separate library. – mark Jan 15 '15 at 15:10

2 Answers2

7

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.

Sjlver
  • 1,227
  • 1
  • 12
  • 28
2

A bit late to the party, but for what it's worth: You can also pass the blacklist file as an option via an environment variable to the sanitizer. Which is useful if you do not have access to the source code e.g.

LSAN_OPTIONS=suppressions=blacklist.txt ./unit-tests
divhart
  • 371
  • 2
  • 5