@@ -5,15 +5,18 @@ The rustc compiler contains support for following sanitizers:
5
5
* [ AddressSanitizer] [ clang-asan ] a faster memory error detector. Can
6
6
detect out-of-bounds access to heap, stack, and globals, use after free, use
7
7
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.
8
10
* [ LeakSanitizer] [ clang-lsan ] a run-time memory leak detector.
9
11
* [ MemorySanitizer] [ clang-msan ] a detector of uninitialized reads.
10
12
* [ ThreadSanitizer] [ clang-tsan ] a fast data race detector.
11
13
12
14
## How to use the sanitizers?
13
15
14
16
To 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/ ) .
17
20
18
21
## How are sanitizers implemented in rustc?
19
22
@@ -22,7 +25,8 @@ an integration point for LLVM compile time instrumentation passes and runtime
22
25
libraries. Highlight of the most important aspects of the implementation:
23
26
24
27
* 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 ` :
26
30
27
31
``` toml
28
32
[build ]
@@ -33,9 +37,9 @@ libraries. Highlight of the most important aspects of the implementation:
33
37
34
38
* During LLVM code generation, the functions intended for instrumentation are
35
39
[ 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(...)] ` .
39
43
40
44
* The decision whether to perform instrumentation or not is possible only at a
41
45
function granularity. In the cases were those decision differ between
@@ -47,28 +51,66 @@ libraries. Highlight of the most important aspects of the implementation:
47
51
passes are invoked after optimization passes.
48
52
49
53
* 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.
53
59
54
60
[ 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
59
66
[ 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
62
102
63
103
## Additional Information
64
104
65
105
* [ Sanitizers project page] ( https://github.com/google/sanitizers/wiki/ )
66
106
* [ AddressSanitizer in Clang] [ clang-asan ]
107
+ * [ Hardware-assisted AddressSanitizer] [ clang-hwasan ]
67
108
* [ LeakSanitizer in Clang] [ clang-lsan ]
68
109
* [ MemorySanitizer in Clang] [ clang-msan ]
69
110
* [ ThreadSanitizer in Clang] [ clang-tsan ]
70
111
71
112
[ clang-asan ] : https://clang.llvm.org/docs/AddressSanitizer.html
113
+ [ clang-hwasan ] : https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html
72
114
[ clang-lsan ] : https://clang.llvm.org/docs/LeakSanitizer.html
73
115
[ clang-msan ] : https://clang.llvm.org/docs/MemorySanitizer.html
74
116
[ clang-tsan ] : https://clang.llvm.org/docs/ThreadSanitizer.html
0 commit comments