Skip to content

Commit f244d05

Browse files
committed
Sanitizers implementation in rustc
1 parent fa3a3e4 commit f244d05

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
- [Updating LLVM](./codegen/updating-llvm.md)
9393
- [Debugging LLVM](./codegen/debugging.md)
9494
- [Profile-guided Optimization](./profile-guided-optimization.md)
95+
- [Sanitizers Support](./sanitizers.md)
9596
- [Debugging Support in Rust Compiler](./debugging-support-in-rustc.md)
9697

9798
---

src/sanitizers.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 rustc 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 following steps:
23+
24+
1. The sanitizer runtime libraries are part of [compiler-rt] project, and [will
25+
be built as an LLVM subproject][sanitizer-build] when enabled in `config.toml`:
26+
27+
```toml
28+
[build]
29+
sanitizers = true
30+
```
31+
32+
The runtimes are [placed into target libdir][sanitizer-copy].
33+
34+
2. During LLVM code generation, the functions intended for instrumentation are
35+
[marked][sanitizer-attribute] with `SanitizeAddress`, `SanitizeMemory`, or
36+
`SanitizeThread` attribute. Currently those attributes are applied in
37+
indiscriminate manner. but in principle they could be used to perform
38+
instrumentation selectively.
39+
40+
3. The LLVM IR generated by rustc is instrumented by [dedicated LLVM
41+
passes][sanitizer-pass], different for each sanitizer. Instrumentation
42+
passes are invoked after optimization passes.
43+
44+
4. When producing an executable, the sanitizer specific runtime library is
45+
[linked in][sanitizer-link]. The libraries are searched for in target libdir
46+
relative to default system root, so that this process is not affected
47+
by sysroot overrides used for example by cargo `-Zbuild-std` functionality.
48+
49+
[compiler-rt]: https://github.com/llvm/llvm-project/tree/master/compiler-rt
50+
[sanitizer-build]: https://github.com/rust-lang/rust/blob/87c3eedffba64830b67e54e75dd479f9fd83cc7d/src/bootstrap/native.rs#L220-L225
51+
[sanitizer-copy]: https://github.com/rust-lang/rust/blob/87c3eedffba64830b67e54e75dd479f9fd83cc7d/src/bootstrap/compile.rs#L269-L321
52+
[sanitizer-attribute]: https://github.com/rust-lang/rust/blob/1.38.0/src/librustc_codegen_llvm/declare.rs#L53-L66
53+
[sanitizer-pass]: https://github.com/rust-lang/rust/blob/1.38.0/src/librustc_codegen_ssa/back/write.rs#L406-L420
54+
[sanitizer-link]: https://github.com/rust-lang/rust/blob/87c3eedffba64830b67e54e75dd479f9fd83cc7d/src/librustc_codegen_ssa/back/link.rs#L729-L770
55+
56+
## Additional Information
57+
58+
* [Sanitizers project page](https://github.com/google/sanitizers/wiki/)
59+
* [AddressSanitizer in Clang][clang-asan]
60+
* [LeakSanitizer in Clang][clang-lsan]
61+
* [MemorySanitizer in Clang][clang-msan]
62+
* [ThreadSanitizer in Clang][clang-tsan]
63+
64+
[clang-asan]: https://clang.llvm.org/docs/AddressSanitizer.html
65+
[clang-lsan]: https://clang.llvm.org/docs/LeakSanitizer.html
66+
[clang-msan]: https://clang.llvm.org/docs/MemorySanitizer.html
67+
[clang-tsan]: https://clang.llvm.org/docs/ThreadSanitizer.html

0 commit comments

Comments
 (0)