blob: 5318be393ff85d02a0ab74e975ab673c08113c41 [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 (
Ivan Lozano5482d6a2021-11-01 10:13:25 -040018 "fmt"
19 "strings"
20
21 "github.com/google/blueprint"
22 "github.com/google/blueprint/proptools"
23
Ivan Lozano6cd99e62020-02-11 08:24:25 -050024 "android/soong/android"
25 "android/soong/cc"
26 "android/soong/rust/config"
Ivan Lozano6cd99e62020-02-11 08:24:25 -050027)
28
Ivan Lozano62cd0382021-11-01 10:27:54 -040029// TODO: When Rust has sanitizer-parity with CC, deduplicate this struct
Ivan Lozano6cd99e62020-02-11 08:24:25 -050030type SanitizeProperties struct {
Tri Vo0a74c3e2021-04-01 13:59:27 -070031 // enable AddressSanitizer, HWAddressSanitizer, and others.
Ivan Lozano6cd99e62020-02-11 08:24:25 -050032 Sanitize struct {
Tri Vo0a74c3e2021-04-01 13:59:27 -070033 Address *bool `android:"arch_variant"`
34 Hwaddress *bool `android:"arch_variant"`
Ivan Lozano62cd0382021-11-01 10:27:54 -040035
36 // Memory-tagging, only available on arm64
37 // if diag.memtag unset or false, enables async memory tagging
38 Memtag_heap *bool `android:"arch_variant"`
39 Fuzzer *bool `android:"arch_variant"`
40 Never *bool `android:"arch_variant"`
41
42 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
43 // Replaces abort() on error with a human-readable error message.
44 // Address and Thread sanitizers always run in diagnostic mode.
45 Diag struct {
46 // Memory-tagging, only available on arm64
47 // requires sanitizer.memtag: true
48 // if set, enables sync memory tagging
49 Memtag_heap *bool `android:"arch_variant"`
50 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -050051 }
52 SanitizerEnabled bool `blueprint:"mutated"`
53 SanitizeDep bool `blueprint:"mutated"`
54
55 // Used when we need to place libraries in their own directory, such as ASAN.
56 InSanitizerDir bool `blueprint:"mutated"`
57}
58
59var fuzzerFlags = []string{
60 "-C passes='sancov'",
61
62 "--cfg fuzzing",
Ivan Lozanoc044f5b2021-04-02 12:43:28 -040063 "-C llvm-args=-sanitizer-coverage-level=3",
Ivan Lozano6cd99e62020-02-11 08:24:25 -050064 "-C llvm-args=-sanitizer-coverage-trace-compares",
65 "-C llvm-args=-sanitizer-coverage-inline-8bit-counters",
66 "-C llvm-args=-sanitizer-coverage-trace-geps",
67 "-C llvm-args=-sanitizer-coverage-prune-blocks=0",
Ivan Lozano6cd99e62020-02-11 08:24:25 -050068
Ivan Lozanoaa7c9802021-08-02 09:43:46 -040069 // See https://github.com/rust-fuzz/cargo-fuzz/pull/193
70 "-C link-dead-code",
71
Ivan Lozano6cd99e62020-02-11 08:24:25 -050072 // Sancov breaks with lto
73 // TODO: Remove when https://bugs.llvm.org/show_bug.cgi?id=41734 is resolved and sancov works with LTO
74 "-C lto=no",
75}
76
77var asanFlags = []string{
78 "-Z sanitizer=address",
79}
80
Ivan Lozano5482d6a2021-11-01 10:13:25 -040081// See cc/sanitize.go's hwasanGlobalOptions for global hwasan options.
Tri Vo0a74c3e2021-04-01 13:59:27 -070082var hwasanFlags = []string{
83 "-Z sanitizer=hwaddress",
84 "-C target-feature=+tagged-globals",
Ivan Lozano5482d6a2021-11-01 10:13:25 -040085
86 // Flags from cc/sanitize.go hwasanFlags
87 "-C llvm-args=--aarch64-enable-global-isel-at-O=-1",
88 "-C llvm-args=-fast-isel=false",
89 "-C llvm-args=-instcombine-lower-dbg-declare=0",
90
91 // Additional flags for HWASAN-ified Rust/C interop
92 "-C llvm-args=--hwasan-with-ifunc",
Tri Vo0a74c3e2021-04-01 13:59:27 -070093}
94
Ivan Lozano6cd99e62020-02-11 08:24:25 -050095func boolPtr(v bool) *bool {
96 if v {
97 return &v
98 } else {
99 return nil
100 }
101}
102
103func init() {
104}
105func (sanitize *sanitize) props() []interface{} {
106 return []interface{}{&sanitize.Properties}
107}
108
109func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400110 s := &sanitize.Properties.Sanitize
111
112 // Never always wins.
113 if Bool(s.Never) {
114 return
115 }
116
Ivan Lozano62cd0382021-11-01 10:27:54 -0400117 // rust_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {Memtag_heap}).
118 if binary, ok := ctx.RustModule().compiler.(binaryInterface); ok && binary.testBinary() {
119 if s.Memtag_heap == nil {
120 s.Memtag_heap = proptools.BoolPtr(true)
121 }
122 if s.Diag.Memtag_heap == nil {
123 s.Diag.Memtag_heap = proptools.BoolPtr(true)
124 }
125 }
126
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400127 var globalSanitizers []string
Ivan Lozano62cd0382021-11-01 10:27:54 -0400128 var globalSanitizersDiag []string
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400129
130 if ctx.Host() {
131 if !ctx.Windows() {
132 globalSanitizers = ctx.Config().SanitizeHost()
133 }
134 } else {
135 arches := ctx.Config().SanitizeDeviceArch()
136 if len(arches) == 0 || android.InList(ctx.Arch().ArchType.Name, arches) {
137 globalSanitizers = ctx.Config().SanitizeDevice()
Ivan Lozano62cd0382021-11-01 10:27:54 -0400138 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400139 }
140 }
141
142 if len(globalSanitizers) > 0 {
143 var found bool
144
145 // Global Sanitizers
146 if found, globalSanitizers = android.RemoveFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
147 // TODO(b/180495975): HWASan for static Rust binaries isn't supported yet.
148 if !ctx.RustModule().StaticExecutable() {
149 s.Hwaddress = proptools.BoolPtr(true)
150 }
151 }
152
Ivan Lozano62cd0382021-11-01 10:27:54 -0400153 if found, globalSanitizers = android.RemoveFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
154 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
155 s.Memtag_heap = proptools.BoolPtr(true)
156 }
157 }
158
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400159 if found, globalSanitizers = android.RemoveFromList("address", globalSanitizers); found && s.Address == nil {
160 s.Address = proptools.BoolPtr(true)
161 }
162
163 if found, globalSanitizers = android.RemoveFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
164 s.Fuzzer = proptools.BoolPtr(true)
165 }
166
Ivan Lozano62cd0382021-11-01 10:27:54 -0400167 // Global Diag Sanitizers
168 if found, globalSanitizersDiag = android.RemoveFromList("memtag_heap", globalSanitizersDiag); found &&
169 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
170 s.Diag.Memtag_heap = proptools.BoolPtr(true)
171 }
172 }
173
174 // Enable Memtag for all components in the include paths (for Aarch64 only)
175 if ctx.Arch().ArchType == android.Arm64 {
176 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
177 if s.Memtag_heap == nil {
178 s.Memtag_heap = proptools.BoolPtr(true)
179 }
180 if s.Diag.Memtag_heap == nil {
181 s.Diag.Memtag_heap = proptools.BoolPtr(true)
182 }
183 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
184 if s.Memtag_heap == nil {
185 s.Memtag_heap = proptools.BoolPtr(true)
186 }
187 }
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400188 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500189
190 // TODO:(b/178369775)
191 // For now sanitizing is only supported on devices
192 if ctx.Os() == android.Android && Bool(s.Fuzzer) {
193 sanitize.Properties.SanitizerEnabled = true
194 }
195
196 if ctx.Os() == android.Android && Bool(s.Address) {
197 sanitize.Properties.SanitizerEnabled = true
198 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700199
200 // HWASan requires AArch64 hardware feature (top-byte-ignore).
201 if ctx.Arch().ArchType != android.Arm64 {
202 s.Hwaddress = nil
203 }
204
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400205 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
206 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
207 if (ctx.RustModule().InRamdisk() || ctx.RustModule().InVendorRamdisk() || ctx.RustModule().InRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
208 s.Hwaddress = nil
209 }
210
211 if Bool(s.Hwaddress) {
212 s.Address = nil
213 }
214
Ivan Lozano62cd0382021-11-01 10:27:54 -0400215 // Memtag_heap is only implemented on AArch64.
216 if ctx.Arch().ArchType != android.Arm64 {
217 s.Memtag_heap = nil
218 }
219
220 if ctx.Os() == android.Android && (Bool(s.Hwaddress) || Bool(s.Address) || Bool(s.Memtag_heap)) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700221 sanitize.Properties.SanitizerEnabled = true
222 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500223}
224
225type sanitize struct {
226 Properties SanitizeProperties
227}
228
229func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
230 if !sanitize.Properties.SanitizerEnabled {
231 return flags, deps
232 }
233 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
234 flags.RustFlags = append(flags.RustFlags, fuzzerFlags...)
Tri Vo505b0e82021-04-08 15:50:48 -0700235 if ctx.Arch().ArchType == android.Arm64 {
236 flags.RustFlags = append(flags.RustFlags, hwasanFlags...)
237 } else {
238 flags.RustFlags = append(flags.RustFlags, asanFlags...)
239 }
Ivan Lozanof3e8fc32021-11-13 07:43:54 -0500240 } else if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700241 flags.RustFlags = append(flags.RustFlags, hwasanFlags...)
Ivan Lozanof3e8fc32021-11-13 07:43:54 -0500242 } else if Bool(sanitize.Properties.Sanitize.Address) {
243 flags.RustFlags = append(flags.RustFlags, asanFlags...)
Tri Vo0a74c3e2021-04-01 13:59:27 -0700244 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500245 return flags, deps
246}
247
248func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
249 return deps
250}
251
252func rustSanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
253 if mod, ok := mctx.Module().(*Module); ok && mod.sanitize != nil {
254 if !mod.Enabled() {
255 return
256 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700257
Ivan Lozano62cd0382021-11-01 10:27:54 -0400258 if Bool(mod.sanitize.Properties.Sanitize.Memtag_heap) && mod.Binary() {
259 noteDep := "note_memtag_heap_async"
260 if Bool(mod.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
261 noteDep = "note_memtag_heap_sync"
262 }
263 // If we're using snapshots, redirect to snapshot whenever possible
264 // TODO(b/178470649): clean manual snapshot redirections
265 snapshot := mctx.Provider(cc.SnapshotInfoProvider).(cc.SnapshotInfo)
266 if lib, ok := snapshot.StaticLibs[noteDep]; ok {
267 noteDep = lib
268 }
269 depTag := cc.StaticDepTag(true)
270 variations := append(mctx.Target().Variations(),
271 blueprint.Variation{Mutator: "link", Variation: "static"})
272 if mod.Device() {
273 variations = append(variations, mod.ImageVariation())
274 }
275 mctx.AddFarVariationDependencies(variations, depTag, noteDep)
276 }
277
Tri Vo0a74c3e2021-04-01 13:59:27 -0700278 variations := mctx.Target().Variations()
279 var depTag blueprint.DependencyTag
280 var deps []string
281
Tri Vo505b0e82021-04-08 15:50:48 -0700282 if mod.IsSanitizerEnabled(cc.Asan) ||
283 (mod.IsSanitizerEnabled(cc.Fuzzer) && mctx.Arch().ArchType != android.Arm64) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700284 variations = append(variations,
285 blueprint.Variation{Mutator: "link", Variation: "shared"})
286 depTag = cc.SharedDepTag()
287 deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan")}
Tri Vo505b0e82021-04-08 15:50:48 -0700288 } else if mod.IsSanitizerEnabled(cc.Hwasan) ||
289 (mod.IsSanitizerEnabled(cc.Fuzzer) && mctx.Arch().ArchType == android.Arm64) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700290 // TODO(b/180495975): HWASan for static Rust binaries isn't supported yet.
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400291 if binary, ok := mod.compiler.(binaryInterface); ok {
292 if binary.staticallyLinked() {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700293 mctx.ModuleErrorf("HWASan is not supported for static Rust executables yet.")
294 }
295 }
296
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400297 // Always link against the shared library -- static binaries will pull in the static
298 // library during final link if necessary
299 variations = append(variations,
300 blueprint.Variation{Mutator: "link", Variation: "shared"})
301 depTag = cc.SharedDepTag()
302 deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "hwasan")}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500303 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700304
Ivan Lozano62cd0382021-11-01 10:27:54 -0400305 if len(deps) > 0 {
306 mctx.AddFarVariationDependencies(variations, depTag, deps...)
307 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500308 }
309}
310
311func (sanitize *sanitize) SetSanitizer(t cc.SanitizerType, b bool) {
312 sanitizerSet := false
313 switch t {
314 case cc.Fuzzer:
315 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
316 sanitizerSet = true
317 case cc.Asan:
318 sanitize.Properties.Sanitize.Address = boolPtr(b)
319 sanitizerSet = true
Tri Vo0a74c3e2021-04-01 13:59:27 -0700320 case cc.Hwasan:
321 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
322 sanitizerSet = true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400323 case cc.Memtag_heap:
324 sanitize.Properties.Sanitize.Memtag_heap = boolPtr(b)
325 sanitizerSet = true
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500326 default:
327 panic(fmt.Errorf("setting unsupported sanitizerType %d", t))
328 }
329 if b && sanitizerSet {
330 sanitize.Properties.SanitizerEnabled = true
331 }
332}
333
Ivan Lozanod7586b62021-04-01 09:49:36 -0400334func (m *Module) UbsanRuntimeNeeded() bool {
335 return false
336}
337
338func (m *Module) MinimalRuntimeNeeded() bool {
339 return false
340}
341
342func (m *Module) UbsanRuntimeDep() bool {
343 return false
344}
345
346func (m *Module) MinimalRuntimeDep() bool {
347 return false
348}
349
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500350// Check if the sanitizer is explicitly disabled (as opposed to nil by
351// virtue of not being set).
352func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t cc.SanitizerType) bool {
353 if sanitize == nil {
354 return false
355 }
356 if Bool(sanitize.Properties.Sanitize.Never) {
357 return true
358 }
359 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
360 return sanitizerVal != nil && *sanitizerVal == false
361}
362
363// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
364// because enabling a sanitizer either directly (via the blueprint) or
365// indirectly (via a mutator) sets the bool ptr to true, and you can't
366// distinguish between the cases. It isn't needed though - both cases can be
367// treated identically.
368func (sanitize *sanitize) isSanitizerEnabled(t cc.SanitizerType) bool {
369 if sanitize == nil || !sanitize.Properties.SanitizerEnabled {
370 return false
371 }
372
373 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
374 return sanitizerVal != nil && *sanitizerVal == true
375}
376
377func (sanitize *sanitize) getSanitizerBoolPtr(t cc.SanitizerType) *bool {
378 switch t {
379 case cc.Fuzzer:
380 return sanitize.Properties.Sanitize.Fuzzer
381 case cc.Asan:
382 return sanitize.Properties.Sanitize.Address
Tri Vo0a74c3e2021-04-01 13:59:27 -0700383 case cc.Hwasan:
384 return sanitize.Properties.Sanitize.Hwaddress
Ivan Lozano62cd0382021-11-01 10:27:54 -0400385 case cc.Memtag_heap:
386 return sanitize.Properties.Sanitize.Memtag_heap
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500387 default:
388 return nil
389 }
390}
391
Tri Vo0a74c3e2021-04-01 13:59:27 -0700392func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
393 // Add a suffix for hwasan rlib libraries to allow surfacing both the sanitized and
394 // non-sanitized variants to make without a name conflict.
395 if entries.Class == "RLIB_LIBRARIES" || entries.Class == "STATIC_LIBRARIES" {
396 if sanitize.isSanitizerEnabled(cc.Hwasan) {
397 entries.SubName += ".hwasan"
398 }
399 }
400}
401
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500402func (mod *Module) SanitizerSupported(t cc.SanitizerType) bool {
403 if mod.Host() {
404 return false
405 }
406 switch t {
407 case cc.Fuzzer:
408 return true
409 case cc.Asan:
410 return true
Tri Vo0a74c3e2021-04-01 13:59:27 -0700411 case cc.Hwasan:
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400412 // TODO(b/180495975): HWASan for static Rust binaries isn't supported yet.
413 if mod.StaticExecutable() {
414 return false
415 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700416 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400417 case cc.Memtag_heap:
418 return true
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500419 default:
420 return false
421 }
422}
423
424func (mod *Module) IsSanitizerEnabled(t cc.SanitizerType) bool {
425 return mod.sanitize.isSanitizerEnabled(t)
426}
427
428func (mod *Module) IsSanitizerExplicitlyDisabled(t cc.SanitizerType) bool {
429 if mod.Host() {
430 return true
431 }
432
433 // TODO(b/178365482): Rust/CC interop doesn't work just yet; don't sanitize rust_ffi modules until
434 // linkage issues are resolved.
435 if lib, ok := mod.compiler.(libraryInterface); ok {
436 if lib.shared() || lib.static() {
437 return true
438 }
439 }
440
441 return mod.sanitize.isSanitizerExplicitlyDisabled(t)
442}
443
444func (mod *Module) SanitizeDep() bool {
445 return mod.sanitize.Properties.SanitizeDep
446}
447
448func (mod *Module) SetSanitizer(t cc.SanitizerType, b bool) {
449 if !Bool(mod.sanitize.Properties.Sanitize.Never) {
450 mod.sanitize.SetSanitizer(t, b)
451 }
452}
453
454func (mod *Module) SetSanitizeDep(b bool) {
455 mod.sanitize.Properties.SanitizeDep = b
456}
457
458func (mod *Module) StaticallyLinked() bool {
459 if lib, ok := mod.compiler.(libraryInterface); ok {
Tri Vo505b0e82021-04-08 15:50:48 -0700460 return lib.rlib() || lib.static()
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400461 } else if binary, ok := mod.compiler.(binaryInterface); ok {
462 return binary.staticallyLinked()
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500463 }
464 return false
465}
466
467func (mod *Module) SetInSanitizerDir() {
468 mod.sanitize.Properties.InSanitizerDir = true
469}
470
471func (mod *Module) SanitizeNever() bool {
472 return Bool(mod.sanitize.Properties.Sanitize.Never)
473}
474
475var _ cc.PlatformSanitizeable = (*Module)(nil)
476
477func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
478 switch t := tag.(type) {
479 case dependencyTag:
480 return t.library
481 default:
482 return cc.IsSanitizableDependencyTag(tag)
483 }
484}
485
486func (m *Module) SanitizableDepTagChecker() cc.SantizableDependencyTagChecker {
487 return IsSanitizableDependencyTag
488}