Skip to content

Commit f5fab75

Browse files
Debugging tips: sanitizers, why and how
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
1 parent a657669 commit f5fab75

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

kb/development/debugging_tips.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,92 @@ This document assumes some familiarity with the project, e.g. that you already k
77

88
This document is written primarily with Linux in mind. Similar platforms such as macOS will require few adaptations. Windows (except WSL) is out of scope.
99

10+
## Sanitizers
11+
12+
### Sanitizers used in test scripts
13+
14+
#### ASan: AddressSanitizer
15+
16+
* Documentation: https://github.com/google/sanitizers/wiki/addresssanitizer
17+
* Detects: buffer overflows, use after free, memory leaks
18+
* Compilers: GCC, Clang
19+
* Compiler flags: `-fsanitize=address -fno-sanitize-recover=all` (in both `CFLAGS` and `LDFLAGS`)
20+
* CMake build types: `ASan`, `ASanDbg`
21+
* Used in: most builds in `all.sh`
22+
23+
#### MSan: MemorySanitizer
24+
25+
* Documentation: https://github.com/google/sanitizers/wiki/memorysanitizer
26+
* Detects: uninitialized memory
27+
* Compilers: GCC, Clang
28+
* Compiler flags: `-fsanitize=memory` (in both `CFLAGS` and `LDFLAGS`)
29+
* CMake build types: `MemSan`, `MemSanDbg`
30+
* Used in: `component_test_memsan*`
31+
32+
#### TSan: ThreadSanitizer
33+
34+
* Documentation: https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual
35+
* Detects: race conditions
36+
* Compilers: GCC, Clang
37+
* Compiler flags: `-fsanitize=thread` (in both `CFLAGS` and `LDFLAGS`)
38+
* CMake build types: `TSan`, `TSanDbg`
39+
* Used in: `component_test_tsan*`
40+
41+
#### UBSan: UndefinedBehaviorSanitizer
42+
43+
* Documentation: https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
44+
* Detects: null pointer misuse, bitwise shift amount out of range, signed integer overflow, …
45+
* Compilers: GCC, Clang
46+
* Compiler flags: `-fsanitize=undefined` (in both `CFLAGS` and `LDFLAGS`)
47+
* CMake build types: `ASan`, `ASanDbg`
48+
* Used in: most builds in `all.sh`
49+
50+
### Valgrind
51+
52+
Valgrind mostly duplicates Asan+Msan, but very occasionally finds something that they don't.
53+
54+
* Documentation: https://valgrind.org/docs/manual/manual.html
55+
* Detects: buffer overflows, use after free, memory leaks, uninitialized memory
56+
* We don't currently use it for race conditions.
57+
* Compilers: any
58+
* Compiler flags: N/A — runtime instrumentation only
59+
* CMake target: `make memcheck`
60+
* Run with:
61+
```
62+
valgrind -q --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=50 --log-file=myprogram.MemoryChecker.log myprogram
63+
grep . myprogram.MemoryChecker.log myprogram
64+
```
65+
* Used in: `component_release_test_valgrind*`
66+
67+
### Getting symbolic backtraces from symbolizers
68+
69+
By default, ASan/MSan/TSan/UBSan display traces without symbolic information. For traces with symbol names, you need to set environment variables:
70+
71+
```
72+
export ASAN_OPTIONS=symbolize=1
73+
export MSAN_OPTIONS=symbolize=1
74+
export TSAN_OPTIONS=symbolize=1
75+
export UBSAN_OPTIONS=print_stacktrace=1
76+
```
77+
78+
With Clang, depending on how it's installed, you may need to specify the path to the correct version of `llvm-symbolizer` in `ASAN_SYMBOLIZER_PATH`, `MSAN_SYMBOLIZER_PATH` and `TSAN_SYMBOLIZER_PATH`. For example:
79+
80+
```
81+
if ASAN_SYMBOLIZER_PATH=$(readlink -f "$(command -v clang)") &&
82+
ASAN_SYMBOLIZER_PATH="${ASAN_SYMBOLIZER_PATH%/*}/llvm-symbolizer"
83+
then
84+
export ASAN_SYMBOLIZER_PATH
85+
export MSAN_SYMBOLIZER_PATH="$ASAN_SYMBOLIZER_PATH"
86+
export TSAN_SYMBOLIZER_PATH="$ASAN_SYMBOLIZER_PATH"
87+
fi
88+
```
89+
90+
See [SanitizerCommonFlags](https://github.com/google/sanitizers/wiki/SanitizerCommonFlags) for more flags you can use in `$xxSAN_OPTIONS`.
91+
92+
### Sanitizers for constant-time testing
93+
94+
See “[Mbed TLS test guidelines — Constant-flow testing](../development/test_suites.md#constant-flow-testing)”.
95+
1096
## Reverse debugging
1197
1298
### What is reverse debugging?

0 commit comments

Comments
 (0)