blob: a4ba4bd33a1c3343354df614e3daf0cb7eb0f389 [file] [log] [blame]
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001// Copyright 2020 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
18 "android/soong/android"
19 "android/soong/cc"
20 "android/soong/rust/config"
21 "fmt"
22 "github.com/google/blueprint"
23)
24
25type SanitizeProperties struct {
Tri Vo0a74c3e2021-04-01 13:59:27 -070026 // enable AddressSanitizer, HWAddressSanitizer, and others.
Ivan Lozano6cd99e62020-02-11 08:24:25 -050027 Sanitize struct {
Tri Vo0a74c3e2021-04-01 13:59:27 -070028 Address *bool `android:"arch_variant"`
29 Hwaddress *bool `android:"arch_variant"`
30 Fuzzer *bool `android:"arch_variant"`
31 Never *bool `android:"arch_variant"`
Ivan Lozano6cd99e62020-02-11 08:24:25 -050032 }
33 SanitizerEnabled bool `blueprint:"mutated"`
34 SanitizeDep bool `blueprint:"mutated"`
35
36 // Used when we need to place libraries in their own directory, such as ASAN.
37 InSanitizerDir bool `blueprint:"mutated"`
38}
39
40var fuzzerFlags = []string{
41 "-C passes='sancov'",
42
43 "--cfg fuzzing",
Ivan Lozanoc044f5b2021-04-02 12:43:28 -040044 "-C llvm-args=-sanitizer-coverage-level=3",
Ivan Lozano6cd99e62020-02-11 08:24:25 -050045 "-C llvm-args=-sanitizer-coverage-trace-compares",
46 "-C llvm-args=-sanitizer-coverage-inline-8bit-counters",
47 "-C llvm-args=-sanitizer-coverage-trace-geps",
48 "-C llvm-args=-sanitizer-coverage-prune-blocks=0",
Ivan Lozano6cd99e62020-02-11 08:24:25 -050049
Ivan Lozanoaa7c9802021-08-02 09:43:46 -040050 // See https://github.com/rust-fuzz/cargo-fuzz/pull/193
51 "-C link-dead-code",
52
Ivan Lozano6cd99e62020-02-11 08:24:25 -050053 // Sancov breaks with lto
54 // TODO: Remove when https://bugs.llvm.org/show_bug.cgi?id=41734 is resolved and sancov works with LTO
55 "-C lto=no",
56}
57
58var asanFlags = []string{
59 "-Z sanitizer=address",
60}
61
Tri Vo0a74c3e2021-04-01 13:59:27 -070062var hwasanFlags = []string{
63 "-Z sanitizer=hwaddress",
64 "-C target-feature=+tagged-globals",
65}
66
Ivan Lozano6cd99e62020-02-11 08:24:25 -050067func boolPtr(v bool) *bool {
68 if v {
69 return &v
70 } else {
71 return nil
72 }
73}
74
75func init() {
76}
77func (sanitize *sanitize) props() []interface{} {
78 return []interface{}{&sanitize.Properties}
79}
80
81func (sanitize *sanitize) begin(ctx BaseModuleContext) {
82 s := sanitize.Properties.Sanitize
83
84 // TODO:(b/178369775)
85 // For now sanitizing is only supported on devices
86 if ctx.Os() == android.Android && Bool(s.Fuzzer) {
87 sanitize.Properties.SanitizerEnabled = true
88 }
89
90 if ctx.Os() == android.Android && Bool(s.Address) {
91 sanitize.Properties.SanitizerEnabled = true
92 }
Tri Vo0a74c3e2021-04-01 13:59:27 -070093
94 // HWASan requires AArch64 hardware feature (top-byte-ignore).
95 if ctx.Arch().ArchType != android.Arm64 {
96 s.Hwaddress = nil
97 }
98
99 if ctx.Os() == android.Android && Bool(s.Hwaddress) {
100 sanitize.Properties.SanitizerEnabled = true
101 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500102}
103
104type sanitize struct {
105 Properties SanitizeProperties
106}
107
108func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
109 if !sanitize.Properties.SanitizerEnabled {
110 return flags, deps
111 }
112 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
113 flags.RustFlags = append(flags.RustFlags, fuzzerFlags...)
Tri Vo505b0e82021-04-08 15:50:48 -0700114 if ctx.Arch().ArchType == android.Arm64 {
115 flags.RustFlags = append(flags.RustFlags, hwasanFlags...)
116 } else {
117 flags.RustFlags = append(flags.RustFlags, asanFlags...)
118 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500119 }
120 if Bool(sanitize.Properties.Sanitize.Address) {
121 flags.RustFlags = append(flags.RustFlags, asanFlags...)
122 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700123 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
124 flags.RustFlags = append(flags.RustFlags, hwasanFlags...)
125 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500126 return flags, deps
127}
128
129func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
130 return deps
131}
132
133func rustSanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
134 if mod, ok := mctx.Module().(*Module); ok && mod.sanitize != nil {
135 if !mod.Enabled() {
136 return
137 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700138
139 variations := mctx.Target().Variations()
140 var depTag blueprint.DependencyTag
141 var deps []string
142
Tri Vo505b0e82021-04-08 15:50:48 -0700143 if mod.IsSanitizerEnabled(cc.Asan) ||
144 (mod.IsSanitizerEnabled(cc.Fuzzer) && mctx.Arch().ArchType != android.Arm64) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700145 variations = append(variations,
146 blueprint.Variation{Mutator: "link", Variation: "shared"})
147 depTag = cc.SharedDepTag()
148 deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan")}
Tri Vo505b0e82021-04-08 15:50:48 -0700149 } else if mod.IsSanitizerEnabled(cc.Hwasan) ||
150 (mod.IsSanitizerEnabled(cc.Fuzzer) && mctx.Arch().ArchType == android.Arm64) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700151 // TODO(b/180495975): HWASan for static Rust binaries isn't supported yet.
152 if binary, ok := mod.compiler.(*binaryDecorator); ok {
153 if Bool(binary.Properties.Static_executable) {
154 mctx.ModuleErrorf("HWASan is not supported for static Rust executables yet.")
155 }
156 }
157
158 if mod.StaticallyLinked() {
159 variations = append(variations,
160 blueprint.Variation{Mutator: "link", Variation: "static"})
161 depTag = cc.StaticDepTag(false)
162 deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "hwasan_static")}
163 } else {
164 variations = append(variations,
165 blueprint.Variation{Mutator: "link", Variation: "shared"})
166 depTag = cc.SharedDepTag()
167 deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "hwasan")}
168 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500169 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700170
171 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500172 }
173}
174
175func (sanitize *sanitize) SetSanitizer(t cc.SanitizerType, b bool) {
176 sanitizerSet := false
177 switch t {
178 case cc.Fuzzer:
179 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
180 sanitizerSet = true
181 case cc.Asan:
182 sanitize.Properties.Sanitize.Address = boolPtr(b)
183 sanitizerSet = true
Tri Vo0a74c3e2021-04-01 13:59:27 -0700184 case cc.Hwasan:
185 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
186 sanitizerSet = true
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500187 default:
188 panic(fmt.Errorf("setting unsupported sanitizerType %d", t))
189 }
190 if b && sanitizerSet {
191 sanitize.Properties.SanitizerEnabled = true
192 }
193}
194
Ivan Lozanod7586b62021-04-01 09:49:36 -0400195func (m *Module) UbsanRuntimeNeeded() bool {
196 return false
197}
198
199func (m *Module) MinimalRuntimeNeeded() bool {
200 return false
201}
202
203func (m *Module) UbsanRuntimeDep() bool {
204 return false
205}
206
207func (m *Module) MinimalRuntimeDep() bool {
208 return false
209}
210
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500211// Check if the sanitizer is explicitly disabled (as opposed to nil by
212// virtue of not being set).
213func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t cc.SanitizerType) bool {
214 if sanitize == nil {
215 return false
216 }
217 if Bool(sanitize.Properties.Sanitize.Never) {
218 return true
219 }
220 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
221 return sanitizerVal != nil && *sanitizerVal == false
222}
223
224// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
225// because enabling a sanitizer either directly (via the blueprint) or
226// indirectly (via a mutator) sets the bool ptr to true, and you can't
227// distinguish between the cases. It isn't needed though - both cases can be
228// treated identically.
229func (sanitize *sanitize) isSanitizerEnabled(t cc.SanitizerType) bool {
230 if sanitize == nil || !sanitize.Properties.SanitizerEnabled {
231 return false
232 }
233
234 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
235 return sanitizerVal != nil && *sanitizerVal == true
236}
237
238func (sanitize *sanitize) getSanitizerBoolPtr(t cc.SanitizerType) *bool {
239 switch t {
240 case cc.Fuzzer:
241 return sanitize.Properties.Sanitize.Fuzzer
242 case cc.Asan:
243 return sanitize.Properties.Sanitize.Address
Tri Vo0a74c3e2021-04-01 13:59:27 -0700244 case cc.Hwasan:
245 return sanitize.Properties.Sanitize.Hwaddress
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500246 default:
247 return nil
248 }
249}
250
Tri Vo0a74c3e2021-04-01 13:59:27 -0700251func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
252 // Add a suffix for hwasan rlib libraries to allow surfacing both the sanitized and
253 // non-sanitized variants to make without a name conflict.
254 if entries.Class == "RLIB_LIBRARIES" || entries.Class == "STATIC_LIBRARIES" {
255 if sanitize.isSanitizerEnabled(cc.Hwasan) {
256 entries.SubName += ".hwasan"
257 }
258 }
259}
260
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500261func (mod *Module) SanitizerSupported(t cc.SanitizerType) bool {
262 if mod.Host() {
263 return false
264 }
265 switch t {
266 case cc.Fuzzer:
267 return true
268 case cc.Asan:
269 return true
Tri Vo0a74c3e2021-04-01 13:59:27 -0700270 case cc.Hwasan:
271 return true
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500272 default:
273 return false
274 }
275}
276
277func (mod *Module) IsSanitizerEnabled(t cc.SanitizerType) bool {
278 return mod.sanitize.isSanitizerEnabled(t)
279}
280
281func (mod *Module) IsSanitizerExplicitlyDisabled(t cc.SanitizerType) bool {
282 if mod.Host() {
283 return true
284 }
285
286 // TODO(b/178365482): Rust/CC interop doesn't work just yet; don't sanitize rust_ffi modules until
287 // linkage issues are resolved.
288 if lib, ok := mod.compiler.(libraryInterface); ok {
289 if lib.shared() || lib.static() {
290 return true
291 }
292 }
293
294 return mod.sanitize.isSanitizerExplicitlyDisabled(t)
295}
296
297func (mod *Module) SanitizeDep() bool {
298 return mod.sanitize.Properties.SanitizeDep
299}
300
301func (mod *Module) SetSanitizer(t cc.SanitizerType, b bool) {
302 if !Bool(mod.sanitize.Properties.Sanitize.Never) {
303 mod.sanitize.SetSanitizer(t, b)
304 }
305}
306
307func (mod *Module) SetSanitizeDep(b bool) {
308 mod.sanitize.Properties.SanitizeDep = b
309}
310
311func (mod *Module) StaticallyLinked() bool {
312 if lib, ok := mod.compiler.(libraryInterface); ok {
Tri Vo505b0e82021-04-08 15:50:48 -0700313 return lib.rlib() || lib.static()
314 } else if binary, ok := mod.compiler.(*binaryDecorator); ok {
315 return Bool(binary.Properties.Static_executable)
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500316 }
317 return false
318}
319
320func (mod *Module) SetInSanitizerDir() {
321 mod.sanitize.Properties.InSanitizerDir = true
322}
323
324func (mod *Module) SanitizeNever() bool {
325 return Bool(mod.sanitize.Properties.Sanitize.Never)
326}
327
328var _ cc.PlatformSanitizeable = (*Module)(nil)
329
330func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
331 switch t := tag.(type) {
332 case dependencyTag:
333 return t.library
334 default:
335 return cc.IsSanitizableDependencyTag(tag)
336 }
337}
338
339func (m *Module) SanitizableDepTagChecker() cc.SantizableDependencyTagChecker {
340 return IsSanitizableDependencyTag
341}