2

AddressSanitizer and MemorySanitizer are very useful tools, but they require that the whole program be appropriately instrumented. (At least, for the Clang version of AddressSanitizer; see here in the MemorySanitizer docs and the "using private aliases for globals" section of AddressSanitizerClangVsGCC.)

If taken at its word, this means that all library dependencies need to be built with the appropriate compiler flags to enable ASan or MSan. For a typical Linux application that requires various third-party dependencies, what's a practical way of doing this? The Sanitizers are apparently a Google project, and I get the impression that Google code mostly just uses their own monorepo and their own build tools, but this may be outside of the reach of the average developer. Is there a simple way of getting libraries built with the Sanitizers without investing in a lot of extra infrastructure or build scripts?

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
  • Write unit tests. Lots. Then the unit tests may work without external dependencies and can be compiled in their entirety with instrumentation. – Kerrek SB Mar 15 '17 at 22:37
  • @KerrekSB - A lot of my unit tests depend on Protocol Buffers, and I'm getting errors within libprotobuf if I try to build with ASan, so unfortunately, that wouldn't be an easy change. And I'd like to have integration tests run under the Sanitizers as well, if possible. – Josh Kelley Mar 15 '17 at 23:00
  • @KerrekSB Unit tests in a large project may still require lots of dependencies. Also in a typical legacy codebase you only have system tests, not units. – yugr Mar 16 '17 at 09:19
  • 1
    @JoshKelley "using private aliases for globals" - in practice bugs like this are extremely rare. Also note that they are working fine in GCC and we've recently submitted a [patch to Clang](http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20160208/330654.html) to fix it there (not enabled by default yet, though). – yugr Mar 16 '17 at 09:21

2 Answers2

2

AddressSanitizer supports separate instrumentation i.e. you can instrument just parts of your program with it (separate DSOs or even separate object files). Note however that if you use static Asan runtime (which is default on Clang, unless you build with -shared-libasan) you must instrument main executable. Shared runtime (default in GCC) does not have this problem but you'll need to LD_PRELOAD it if executable isn't instrumented. See discussion in wiki for details.

As for MemorySanitizer, it indeed requires all of it's dependencies to be rebuilt (see this for starters). This is the major reason why the tool isn't widely used outside Google.

yugr
  • 19,769
  • 3
  • 51
  • 96
1

The sanitizers can easily be used on your own code without rebuilding system dependencies. I do so routinely with a large(ish) 2M line code base on RHEL 6 & 7 using both GCC and Clang - it's not a problem. If it is a problem; you are doing it wrong (or there is a bug somewhere that needs fixing).

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • After further investigation, you're right; I was running into https://github.com/google/protobuf/issues/1450, which was the root of my problem (and was causing different symptoms in Clang vs. GCC, which led me down the wrong trail of blaming using uninstrumented code in Clang). – Josh Kelley Mar 16 '17 at 21:46