google / sanitizers

AddressSanitizer, ThreadSanitizer, MemorySanitizer
Other
11.51k stars 1.03k forks source link

Setting suppressions file at compile time #1628

Open vpshastry opened 1 year ago

vpshastry commented 1 year ago

When I build my binary with ASAN enabled, I finally be able to run my executable with LSAN_OPTIONS=suppressions=<suppression-file> environment variable that always suppresses leaks from a specific, known, third party library. The point is, I know the suppression file at compile time. Is there a way I can set it at compile time?

Ex: Say my binary is compiled with below options

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")

The final binary generated is myBinary. Running myBinary should always take the suppression file set at compile time.

vitalybuka commented 1 year ago

Depends on type of bugs, https://clang.llvm.org/docs/SanitizerSpecialCaseList.html Leaks are only runtime.

vpshastry commented 1 year ago

@vitalybuka thanks for the answer.

Can this issue be kept open to address this and provide an option to set this option at compile time? Preferrably via the same mechanism as -fsanitize-ignorelist. With a section for leak.

vitalybuka commented 1 year ago

It will break important feature of lsan, it's link time feature. The program does not need to be recompiled to use lsan. Compiler time ignore list will require to recompile.

However if you can change your program, you can use __lsan_ignore_object Example in https://android.googlesource.com/platform/external/abseil-cpp/+/HEAD/absl/debugging/leak_check.cc

Probably with asan, when we still need to recompile we can try to apply such ignore list. We can __lsan_disable()/__lsan_enable() for the scope, matching to ignore list entry, and ignore all allocations in the middle.

We can keep it open, but I don't think we will have enough interest to work on this.

Enna1 commented 1 year ago

You can define a function named __lsan_default_suppressions in your source code:

extern "C" const char* __lsan_default_suppressions() {
  return "leak:foo()";
}
vpshastry commented 1 year ago

Wow! Both are great solutions. Thanks a ton. I guess we can close the issue.

cmorty commented 2 months ago

https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer#suppressions is missing documentation about __lsan_default_suppressions. I'd write it if I'd be able to create a PR for the wiki. :-)