@@ -5,15 +5,18 @@ The rustc compiler contains support for following sanitizers:
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
77 after return, double free, invalid free, memory leaks.
8+ * [ Hardware-assisted AddressSanitizer] [ clang-hwasan ] a tool similar to
9+ AddressSanitizer but based on partial hardware assistance.
810* [ LeakSanitizer] [ clang-lsan ] a run-time memory leak detector.
911* [ MemorySanitizer] [ clang-msan ] a detector of uninitialized reads.
1012* [ ThreadSanitizer] [ clang-tsan ] a fast data race detector.
1113
1214## How to use the sanitizers?
1315
1416To enable a sanitizer compile with ` -Z sanitizer=... ` 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+ of ` address ` , ` hwaddress ` , ` leak ` , ` memory ` or ` thread ` . For more details on how
18+ to use sanitizers please refer to the sanitizer flag in [ the unstable
19+ book] ( https://doc.rust-lang.org/unstable-book/ ) .
1720
1821## How are sanitizers implemented in rustc?
1922
@@ -22,7 +25,8 @@ an integration point for LLVM compile time instrumentation passes and runtime
2225libraries. Highlight of the most important aspects of the implementation:
2326
2427* 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 ` :
28+ [ will be built] [ sanitizer-build ] on [ supported targets] [ sanitizer-targets ]
29+ when enabled in ` config.toml ` :
2630
2731 ``` toml
2832 [build ]
@@ -33,9 +37,9 @@ libraries. Highlight of the most important aspects of the implementation:
3337
3438* During LLVM code generation, the functions intended for instrumentation are
3539 [ 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(...)] ` .
40+ ` SanitizeAddress ` , ` SanitizeHWAddress ` , ` SanitizeMemory ` , or
41+ ` SanitizeThread ` . By default all functions are instrumented, but this
42+ behaviour can be changed with ` #[no_sanitize(...)] ` .
3943
4044* The decision whether to perform instrumentation or not is possible only at a
4145 function granularity. In the cases were those decision differ between
@@ -47,28 +51,66 @@ libraries. Highlight of the most important aspects of the implementation:
4751 passes are invoked after optimization passes.
4852
4953* When producing an executable, the sanitizer specific runtime library is
50- [ linked in] [ sanitizer-link ] . The libraries are searched for in target libdir
51- relative to default system root, so that this process is not affected
52- by sysroot overrides used for example by cargo ` -Z build-std ` functionality.
54+ [ linked in] [ sanitizer-link ] . The libraries are searched for in the target
55+ libdir. First relative to the overridden system root and subsequently
56+ relative to the default system root. Fall-back to the default system root
57+ ensures that sanitizer runtimes remain available when using sysroot overrides
58+ constructed by cargo ` -Z build-std ` or xargo.
5359
5460[ compiler-rt ] : https://github.com/llvm/llvm-project/tree/main/compiler-rt
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
61+ [ sanitizer-build ] : https://github.com/rust-lang/rust/blob/1.55.0/src/bootstrap/native.rs#L700-L765
62+ [ sanitizer-targets ] : https://github.com/rust-lang/rust/blob/1.55.0/src/bootstrap/native.rs#L806-L820
63+ [ sanitizer-copy ] : https://github.com/rust-lang/rust/blob/1.55.0/src/bootstrap/compile.rs#L376-L407
64+ [ sanitizer-attribute ] : https://github.com/rust-lang/rust/blob/1.55.0/compiler/rustc_codegen_llvm/src/attributes.rs#L42-L58
65+ [ inline-mir ] : https://github.com/rust-lang/rust/blob/1.55.0/compiler/rustc_mir/src/transform/inline.rs#L314-L316
5966[ 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
67+ [ sanitizer-pass ] : https://github.com/rust-lang/rust/blob/1.55.0/compiler/rustc_codegen_llvm/src/back/write.rs#L660-L678
68+ [ sanitizer-link ] : https://github.com/rust-lang/rust/blob/1.55.0/compiler/rustc_codegen_ssa/src/back/link.rs#L1053-L1089
69+
70+ ## Testing sanitizers
71+
72+ Sanitizers are validated by code generation tests in
73+ [ ` src/test/codegen/sanitize*.rs ` ] [ test-cg ] and end-to-end functional tests in
74+ [ ` src/test/ui/sanitize/ ` ] [ test-ui ] directory.
75+
76+ Testing sanitizer functionality requires the sanitizer runtimes (built when
77+ ` sanitizer = true ` in ` config.toml ` ) and target providing support for particular
78+ sanitizer. When sanitizer is unsupported on given target, sanitizers tests will
79+ be ignored. This behaviour is controlled by compiletest ` needs-sanitizer-* `
80+ directives.
81+
82+ [ test-cg ] : https://github.com/rust-lang/rust/tree/master/src/test/codegen
83+ [ test-ui ] : https://github.com/rust-lang/rust/tree/master/src/test/ui/sanitize
84+
85+ ## Enabling sanitizer on a new target
86+
87+ To enable a sanitizer on a new target which is already supported by LLVM:
88+
89+ 1 . Include the sanitizer in the list of ` supported_sanitizers ` in [ the target
90+ definition] [ target-definition ] . ` rustc --target .. -Zsanitizer=.. ` should now
91+ recognize sanitizer as supported.
92+ 2 . [ Build the runtime for the target and include it in the libdir.] [ sanitizer-targets ]
93+ 3 . [ Teach compiletest that your target now supports the sanitizer.] [ compiletest-definition ]
94+ Tests marked with ` needs-sanitizer-* ` should now run on the target.
95+ 4 . Run tests ` ./x.py test --force-rerun src/test/ui/sanitize/ ` to verify.
96+ 5 . [ --enable-sanitizers in the CI configuration] [ ci-configuration ] to build and
97+ distribute the sanitizer runtime as part of the release process.
98+
99+ [ target-definition ] : https://github.com/rust-lang/rust/blob/1.55.0/compiler/rustc_target/src/spec/x86_64_unknown_linux_gnu.rs#L10-L11
100+ [ compiletest-definition ] : https://github.com/rust-lang/rust/blob/1.55.0/src/tools/compiletest/src/util.rs#L87-L116
101+ [ ci-configuration ] : https://github.com/rust-lang/rust/blob/1.55.0/src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile#L94
62102
63103## Additional Information
64104
65105* [ Sanitizers project page] ( https://github.com/google/sanitizers/wiki/ )
66106* [ AddressSanitizer in Clang] [ clang-asan ]
107+ * [ Hardware-assisted AddressSanitizer] [ clang-hwasan ]
67108* [ LeakSanitizer in Clang] [ clang-lsan ]
68109* [ MemorySanitizer in Clang] [ clang-msan ]
69110* [ ThreadSanitizer in Clang] [ clang-tsan ]
70111
71112[ clang-asan ] : https://clang.llvm.org/docs/AddressSanitizer.html
113+ [ clang-hwasan ] : https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html
72114[ clang-lsan ] : https://clang.llvm.org/docs/LeakSanitizer.html
73115[ clang-msan ] : https://clang.llvm.org/docs/MemorySanitizer.html
74116[ clang-tsan ] : https://clang.llvm.org/docs/ThreadSanitizer.html
0 commit comments