|
| 1 | +# Sanitizers Support |
| 2 | + |
| 3 | +The rustc compiler contains basic support for following sanitizers: |
| 4 | + |
| 5 | +* [AddressSanitizer][clang-asan] a faster memory error detector. Can |
| 6 | + detect out-of-bounds access to heap, stack, and globals, use after free, use |
| 7 | + after return, double free, invalid free, memory leaks. |
| 8 | +* [LeakSanitizer][clang-lsan] a run-time memory leak detector. |
| 9 | +* [MemorySanitizer][clang-msan] a detector of uninitialized reads. |
| 10 | +* [ThreadSanitizer][clang-tsan] a fast data race detector. |
| 11 | + |
| 12 | +## How to use the sanitizers? |
| 13 | + |
| 14 | +To enable a sanitizer compile with `-Zsanitizer=...` option, where value is one |
| 15 | +of `address`, `leak`, `memory` or `thread`. For more details how to use |
| 16 | +sanitizers please refer to [the unstable book](https://doc.rust-lang.org/unstable-book/). |
| 17 | + |
| 18 | +## How are sanitizers implemented in rustc? |
| 19 | + |
| 20 | +The implementation of sanitizers relies entirely on LLVM. It consists of |
| 21 | +compile time instrumentation passes and runtime libraries. The role rustc plays |
| 22 | +in the implementation is limited to the execution of the following steps: |
| 23 | + |
| 24 | +1. The sanitizer runtime libraries are part of the [compiler-rt] project, and |
| 25 | + [will be built as an LLVM subproject][sanitizer-build] when enabled in |
| 26 | + `config.toml`: |
| 27 | + |
| 28 | + ```toml |
| 29 | + [build] |
| 30 | + sanitizers = true |
| 31 | + ``` |
| 32 | + |
| 33 | + The runtimes are [placed into target libdir][sanitizer-copy]. |
| 34 | + |
| 35 | +2. During LLVM code generation, the functions intended for instrumentation are |
| 36 | + [marked][sanitizer-attribute] with `SanitizeAddress`, `SanitizeMemory`, or |
| 37 | + `SanitizeThread` attribute. Currently those attributes are applied in |
| 38 | + indiscriminate manner. but in principle they could be used to perform |
| 39 | + instrumentation selectively. |
| 40 | + |
| 41 | +3. The LLVM IR generated by rustc is instrumented by [dedicated LLVM |
| 42 | + passes][sanitizer-pass], different for each sanitizer. Instrumentation |
| 43 | + passes are invoked after optimization passes. |
| 44 | + |
| 45 | +4. When producing an executable, the sanitizer specific runtime library is |
| 46 | + [linked in][sanitizer-link]. The libraries are searched for in target libdir |
| 47 | + relative to default system root, so that this process is not affected |
| 48 | + by sysroot overrides used for example by cargo `-Zbuild-std` functionality. |
| 49 | + |
| 50 | +[compiler-rt]: https://github.com/llvm/llvm-project/tree/master/compiler-rt |
| 51 | +[sanitizer-build]: https://github.com/rust-lang/rust/blob/87c3eedffba64830b67e54e75dd479f9fd83cc7d/src/bootstrap/native.rs#L220-L225 |
| 52 | +[sanitizer-copy]: https://github.com/rust-lang/rust/blob/87c3eedffba64830b67e54e75dd479f9fd83cc7d/src/bootstrap/compile.rs#L269-L321 |
| 53 | +[sanitizer-attribute]: https://github.com/rust-lang/rust/blob/1.38.0/src/librustc_codegen_llvm/declare.rs#L53-L66 |
| 54 | +[sanitizer-pass]: https://github.com/rust-lang/rust/blob/1.38.0/src/librustc_codegen_ssa/back/write.rs#L406-L420 |
| 55 | +[sanitizer-link]: https://github.com/rust-lang/rust/blob/87c3eedffba64830b67e54e75dd479f9fd83cc7d/src/librustc_codegen_ssa/back/link.rs#L729-L770 |
| 56 | + |
| 57 | +## Additional Information |
| 58 | + |
| 59 | +* [Sanitizers project page](https://github.com/google/sanitizers/wiki/) |
| 60 | +* [AddressSanitizer in Clang][clang-asan] |
| 61 | +* [LeakSanitizer in Clang][clang-lsan] |
| 62 | +* [MemorySanitizer in Clang][clang-msan] |
| 63 | +* [ThreadSanitizer in Clang][clang-tsan] |
| 64 | + |
| 65 | +[clang-asan]: https://clang.llvm.org/docs/AddressSanitizer.html |
| 66 | +[clang-lsan]: https://clang.llvm.org/docs/LeakSanitizer.html |
| 67 | +[clang-msan]: https://clang.llvm.org/docs/MemorySanitizer.html |
| 68 | +[clang-tsan]: https://clang.llvm.org/docs/ThreadSanitizer.html |
0 commit comments