Repository: bitcoin/bitcoin (language: cpp)

Issue:
 Document how to run bitcoin unittests + functional tests with sanitizers
A small guide from ground up on how to run sanitizers(asan/ubsan/msan) on core would be awesome :)

@practicalswift ;)



Discussion / hints:
argh forgot it matters what kind of issue I click on (for labeling)
MSan setup is documented by the config in #18288 ("build: Add MemorySanitizer (MSan) in Travis to detect use of uninitialized memory").

@elichai @jonasnick Would you mind reviewing and/or Concept ACK:ing #18288? :)
A friendly reminder to anyone interested in this issue to review #18288 (a concept ACK would help!) which at least partly solves the issue at hand: it shows which commands to run to setup MSAN for the project :)

MSAN is by far the trickiest of the sanitizers to set-up, so that is a good start :)
Yeah. It will also help beginners like me to understand the approach to writing new unit tests.

This is pretty simple:

* MSan: Compiling with MSan is quite involved. I'd suggest to just compile normally and use `valgrind ./src/test/test_bitcoin` or `./test/functional/test_runner.py --valgrind` instead. Alternatively you can use the `./ci/` system to run any sanitizer task.
* Other sanitizer: Just append `--with-sanitizers=undefined,integer CC=clang CXX=clang++` to your `./configure` call. (Or `--with-sanitizers=address`, ...). Then, run the tests normally, using the env vars as needed:

```
export LSAN_OPTIONS="suppressions=$(pwd)/test/sanitizer_suppressions/lsan"
export TSAN_OPTIONS="suppressions=$(pwd)/test/sanitizer_suppressions/tsan:halt_on_error=1:second_deadlock_stack=1"
export UBSAN_OPTIONS="suppressions=$(pwd)/test/sanitizer_suppressions/ubsan:print_stacktrace=1:halt_on_error=1:report_error_type=1"

Thanks @MarcoFalke! Updated https://jonatack.github.io/articles/how-to-compile-bitcoin-core-and-run-the-tests with that information, along with a link to https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#sanitizers.
Anything left to do here?

Seems mostly a matter of `./configure with-sanitizers=…` and then the test suite runs as normal, albeit slower and more memory hungry.

Fix patch:
diff --git a/doc/developer-notes.md b/doc/developer-notes.md
index e2e54e13d397b..f15df1bf735d4 100644
--- a/doc/developer-notes.md
+++ b/doc/developer-notes.md
@@ -560,8 +560,19 @@ address sanitizer, libtsan for the thread sanitizer, and libubsan for the
 undefined sanitizer. If you are missing required libraries, the configure script
 will fail with a linker error when testing the sanitizer flags.
 
-The test suite should pass cleanly with the `thread` and `undefined` sanitizers,
-but there are a number of known problems when using the `address` sanitizer. The
+The test suite should pass cleanly with the `thread` and `undefined` sanitizers. You
+may need to use a suppressions file, see `test/sanitizer_suppressions`. They may be
+used as follows:
+```bash
+export LSAN_OPTIONS="suppressions=$(pwd)/test/sanitizer_suppressions/lsan"
+export TSAN_OPTIONS="suppressions=$(pwd)/test/sanitizer_suppressions/tsan:halt_on_error=1:second_deadlock_stack=1"
+export UBSAN_OPTIONS="suppressions=$(pwd)/test/sanitizer_suppressions/ubsan:print_stacktrace=1:halt_on_error=1:report_error_type=1"
+```
+
+See the CI config for more examples, and upstream documentation for more information
+about any additional options.
+
+There are a number of known problems when using the `address` sanitizer. The
 address sanitizer is known to fail in
 [sha256_sse4::Transform](/src/crypto/sha256_sse4.cpp) which makes it unusable
 unless you also use `--disable-asm` when running configure. We would like to fix


Test patch: