11# Sanitizers Support
22
3- The rustc compiler contains basic support for following sanitizers:
3+ The rustc compiler contains support for following sanitizers:
44
55* [ AddressSanitizer] [ clang-asan ] a faster memory error detector. Can
66 detect out-of-bounds access to heap, stack, and globals, use after free, use
@@ -17,13 +17,12 @@ sanitizers please refer to [the unstable book](https://doc.rust-lang.org/unstabl
1717
1818## How are sanitizers implemented in rustc?
1919
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 :
20+ The implementation of sanitizers relies almost entirely on LLVM. The rustc is
21+ an integration point for LLVM compile time instrumentation passes and runtime
22+ libraries. Highlight of the most important aspects of the implementation :
2323
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 ` :
24+ * The sanitizer runtime libraries are part of the [ compiler-rt] project, and
25+ [ will be built on supported targets] [ sanitizer-build ] when enabled in ` config.toml ` :
2726
2827 ``` toml
2928 [build ]
@@ -32,27 +31,34 @@ in the implementation is limited to the execution of the following steps:
3231
3332 The runtimes are [ placed into target libdir] [ sanitizer-copy ] .
3433
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 .
34+ * During LLVM code generation, the functions intended for instrumentation are
35+ [ marked] [ sanitizer-attribute ] with appropriate LLVM attribute:
36+ ` SanitizeAddress ` , ` SanitizeMemory ` , or ` SanitizeThread ` . By default all
37+ functions are instrumented, but this behaviour can be changed with
38+ ` #[no_sanitize(...)] ` .
4039
41- 3 . The LLVM IR generated by rustc is instrumented by [ dedicated LLVM
40+ * The decision whether to perform instrumentation or not is possible only at a
41+ function granularity. In the cases were those decision differ between
42+ functions it might be necessary to inhibit inlining, both at [ MIR
43+ level] [ inline-mir ] and [ LLVM level] [ inline-llvm ] .
44+
45+ * The LLVM IR generated by rustc is instrumented by [ dedicated LLVM
4246 passes] [ sanitizer-pass ] , different for each sanitizer. Instrumentation
4347 passes are invoked after optimization passes.
4448
45- 4 . When producing an executable, the sanitizer specific runtime library is
49+ * When producing an executable, the sanitizer specific runtime library is
4650 [ linked in] [ sanitizer-link ] . The libraries are searched for in target libdir
4751 relative to default system root, so that this process is not affected
4852 by sysroot overrides used for example by cargo ` -Zbuild-std ` functionality.
4953
5054[ 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
55+ [ sanitizer-build ] : https://github.com/rust-lang/rust/blob/a29424a2265411dda7d7446516ac5fd7499e2b55/src/bootstrap/native.rs#L566-L624
56+ [ sanitizer-copy ] : https://github.com/rust-lang/rust/blob/a29424a2265411dda7d7446516ac5fd7499e2b55/src/bootstrap/compile.rs#L270-L304
57+ [ sanitizer-attribute ] : https://github.com/rust-lang/rust/blob/a29424a2265411dda7d7446516ac5fd7499e2b55/src/librustc_codegen_llvm/attributes.rs#L49-L72
58+ [ inline-mir ] : https://github.com/rust-lang/rust/blob/a29424a2265411dda7d7446516ac5fd7499e2b55/src/librustc_mir/transform/inline.rs#L232-L252
59+ [ inline-llvm ] : https://github.com/rust-lang/llvm-project/blob/9330ec5a4c1df5fc1fa62f993ed6a04da68cb040/llvm/include/llvm/IR/Attributes.td#L225-L241
60+ [ sanitizer-pass ] : https://github.com/rust-lang/rust/blob/a29424a2265411dda7d7446516ac5fd7499e2b55/src/librustc_codegen_llvm/back/write.rs#L454-L475
61+ [ sanitizer-link ] : https://github.com/rust-lang/rust/blob/a29424a2265411dda7d7446516ac5fd7499e2b55/src/librustc_codegen_ssa/back/link.rs#L748-L787
5662
5763## Additional Information
5864
0 commit comments