google / sanitizers

AddressSanitizer, ThreadSanitizer, MemorySanitizer
Other
11.01k stars 998 forks source link

LSan API doesn't allow control of child threads #1719

Open mchristoff opened 5 months ago

mchristoff commented 5 months ago

With larger and more complicated codebases, using LSan in a productive way can be difficult. As such, LSan provides an (undocumented) API for interacting with it and modifying its behavior: https://github.com/gcc-mirror/gcc/blob/master/libsanitizer/include/sanitizer/lsan_interface.h

(Apologies, I would link to the LLVM version of the repository rather than the GCC mirror, but tracking down this code is much more difficult given that it doesn't show up easily in Google search results and the README page for this repository doesn't link in an easy way to sanitizer-related code, just llvm.org: https://github.com/google/sanitizers)

This API is extremely useful in working with larger codebases because it can be used in testing infrastructure to turn LSan entirely on or off and do leak-checking when you want to (optionally without killing the process). There is one caveat, however: This only affects the current thread.

The "__lsan_disable" funcion provided by this API eventually reaches one of these calls to DisableOnThisThread, which, as the name implies, only affects the current thread: https://github.com/search?q=repo%3Agcc-mirror%2Fgcc+DisableInThisThread&type=code

As far as I can tell, there isn't any way to control LSan on child threads other than just never turning it on in the first place, which limits LSan's usefulness in systems with intentional or unavoidable memory leaks (such as those that exist for the lifetime of the process) on child threads. This API should provide functions that give better control over this.