blob: 83cf05552249c65052e856f26f907eb8a78a790f [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 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +020052 SanitizerEnabled bool `blueprint:"mutated"`
Ivan Lozano6cd99e62020-02-11 08:24:25 -050053
54 // Used when we need to place libraries in their own directory, such as ASAN.
55 InSanitizerDir bool `blueprint:"mutated"`
56}
57
58var fuzzerFlags = []string{
Charisee5ddec432022-03-01 03:02:51 +000059 "-C passes='sancov-module'",
Ivan Lozano6cd99e62020-02-11 08:24:25 -050060
61 "--cfg fuzzing",
Ivan Lozanoc044f5b2021-04-02 12:43:28 -040062 "-C llvm-args=-sanitizer-coverage-level=3",
Ivan Lozano6cd99e62020-02-11 08:24:25 -050063 "-C llvm-args=-sanitizer-coverage-trace-compares",
64 "-C llvm-args=-sanitizer-coverage-inline-8bit-counters",
65 "-C llvm-args=-sanitizer-coverage-trace-geps",
66 "-C llvm-args=-sanitizer-coverage-prune-blocks=0",
Ivan Lozano6cd99e62020-02-11 08:24:25 -050067
Ivan Lozanoaa7c9802021-08-02 09:43:46 -040068 // See https://github.com/rust-fuzz/cargo-fuzz/pull/193
69 "-C link-dead-code",
70
Ivan Lozano6cd99e62020-02-11 08:24:25 -050071 // Sancov breaks with lto
Charisee5ddec432022-03-01 03:02:51 +000072 // TODO: Remove when https://bugs.llvm.org/show_bug.cgi?id=41734 is resolved and sancov-module works with LTO
Ivan Lozano6cd99e62020-02-11 08:24:25 -050073 "-C lto=no",
74}
75
76var asanFlags = []string{
77 "-Z sanitizer=address",
78}
79
Ivan Lozano5482d6a2021-11-01 10:13:25 -040080// See cc/sanitize.go's hwasanGlobalOptions for global hwasan options.
Tri Vo0a74c3e2021-04-01 13:59:27 -070081var hwasanFlags = []string{
82 "-Z sanitizer=hwaddress",
83 "-C target-feature=+tagged-globals",
Ivan Lozano5482d6a2021-11-01 10:13:25 -040084
85 // Flags from cc/sanitize.go hwasanFlags
86 "-C llvm-args=--aarch64-enable-global-isel-at-O=-1",
87 "-C llvm-args=-fast-isel=false",
88 "-C llvm-args=-instcombine-lower-dbg-declare=0",
89
90 // Additional flags for HWASAN-ified Rust/C interop
91 "-C llvm-args=--hwasan-with-ifunc",
Tri Vo0a74c3e2021-04-01 13:59:27 -070092}
93
Ivan Lozano6cd99e62020-02-11 08:24:25 -050094func boolPtr(v bool) *bool {
95 if v {
96 return &v
97 } else {
98 return nil
99 }
100}
101
102func init() {
103}
104func (sanitize *sanitize) props() []interface{} {
105 return []interface{}{&sanitize.Properties}
106}
107
108func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400109 s := &sanitize.Properties.Sanitize
110
111 // Never always wins.
112 if Bool(s.Never) {
113 return
114 }
115
Ivan Lozano62cd0382021-11-01 10:27:54 -0400116 // rust_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {Memtag_heap}).
117 if binary, ok := ctx.RustModule().compiler.(binaryInterface); ok && binary.testBinary() {
118 if s.Memtag_heap == nil {
119 s.Memtag_heap = proptools.BoolPtr(true)
120 }
121 if s.Diag.Memtag_heap == nil {
122 s.Diag.Memtag_heap = proptools.BoolPtr(true)
123 }
124 }
125
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400126 var globalSanitizers []string
Ivan Lozano62cd0382021-11-01 10:27:54 -0400127 var globalSanitizersDiag []string
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400128
129 if ctx.Host() {
130 if !ctx.Windows() {
131 globalSanitizers = ctx.Config().SanitizeHost()
132 }
133 } else {
134 arches := ctx.Config().SanitizeDeviceArch()
135 if len(arches) == 0 || android.InList(ctx.Arch().ArchType.Name, arches) {
136 globalSanitizers = ctx.Config().SanitizeDevice()
Ivan Lozano62cd0382021-11-01 10:27:54 -0400137 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400138 }
139 }
140
141 if len(globalSanitizers) > 0 {
142 var found bool
143
144 // Global Sanitizers
145 if found, globalSanitizers = android.RemoveFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Ivan Lozano6c5b8f42021-11-15 09:35:12 -0500146 // TODO(b/204776996): HWASan for static Rust binaries isn't supported yet.
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400147 if !ctx.RustModule().StaticExecutable() {
148 s.Hwaddress = proptools.BoolPtr(true)
149 }
150 }
151
Ivan Lozano62cd0382021-11-01 10:27:54 -0400152 if found, globalSanitizers = android.RemoveFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
153 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
154 s.Memtag_heap = proptools.BoolPtr(true)
155 }
156 }
157
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400158 if found, globalSanitizers = android.RemoveFromList("address", globalSanitizers); found && s.Address == nil {
159 s.Address = proptools.BoolPtr(true)
160 }
161
162 if found, globalSanitizers = android.RemoveFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Ivan Lozano6c5b8f42021-11-15 09:35:12 -0500163 // TODO(b/204776996): HWASan for static Rust binaries isn't supported yet, and fuzzer enables HWAsan
164 if !ctx.RustModule().StaticExecutable() {
165 s.Fuzzer = proptools.BoolPtr(true)
166 }
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400167 }
168
Ivan Lozano62cd0382021-11-01 10:27:54 -0400169 // Global Diag Sanitizers
170 if found, globalSanitizersDiag = android.RemoveFromList("memtag_heap", globalSanitizersDiag); found &&
171 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
172 s.Diag.Memtag_heap = proptools.BoolPtr(true)
173 }
174 }
175
176 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross1faf8232022-06-24 18:42:32 -0700177 if ctx.Arch().ArchType == android.Arm64 && ctx.Os().Bionic() {
Ivan Lozano62cd0382021-11-01 10:27:54 -0400178 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
179 if s.Memtag_heap == nil {
180 s.Memtag_heap = proptools.BoolPtr(true)
181 }
182 if s.Diag.Memtag_heap == nil {
183 s.Diag.Memtag_heap = proptools.BoolPtr(true)
184 }
185 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
186 if s.Memtag_heap == nil {
187 s.Memtag_heap = proptools.BoolPtr(true)
188 }
189 }
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400190 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500191
Tri Vo0a74c3e2021-04-01 13:59:27 -0700192 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross1faf8232022-06-24 18:42:32 -0700193 if ctx.Arch().ArchType != android.Arm64 || !ctx.Os().Bionic() {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700194 s.Hwaddress = nil
195 }
196
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400197 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
198 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
199 if (ctx.RustModule().InRamdisk() || ctx.RustModule().InVendorRamdisk() || ctx.RustModule().InRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
200 s.Hwaddress = nil
201 }
202
203 if Bool(s.Hwaddress) {
204 s.Address = nil
205 }
206
Ivan Lozano62cd0382021-11-01 10:27:54 -0400207 // Memtag_heap is only implemented on AArch64.
Colin Cross1faf8232022-06-24 18:42:32 -0700208 if ctx.Arch().ArchType != android.Arm64 || !ctx.Os().Bionic() {
Ivan Lozano62cd0382021-11-01 10:27:54 -0400209 s.Memtag_heap = nil
210 }
211
Florian Mayer296d5952022-10-12 19:30:14 +0000212 // TODO:(b/178369775)
213 // For now sanitizing is only supported on devices
214 if ctx.Os() == android.Android && (Bool(s.Hwaddress) || Bool(s.Address) || Bool(s.Memtag_heap) || Bool(s.Fuzzer)) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700215 sanitize.Properties.SanitizerEnabled = true
216 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500217}
218
219type sanitize struct {
220 Properties SanitizeProperties
221}
222
223func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
224 if !sanitize.Properties.SanitizerEnabled {
225 return flags, deps
226 }
227 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
228 flags.RustFlags = append(flags.RustFlags, fuzzerFlags...)
Ivan Lozanof3e8fc32021-11-13 07:43:54 -0500229 } else if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700230 flags.RustFlags = append(flags.RustFlags, hwasanFlags...)
Ivan Lozanof3e8fc32021-11-13 07:43:54 -0500231 } else if Bool(sanitize.Properties.Sanitize.Address) {
232 flags.RustFlags = append(flags.RustFlags, asanFlags...)
Tri Vo0a74c3e2021-04-01 13:59:27 -0700233 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500234 return flags, deps
235}
236
237func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
238 return deps
239}
240
241func rustSanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
242 if mod, ok := mctx.Module().(*Module); ok && mod.sanitize != nil {
243 if !mod.Enabled() {
244 return
245 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700246
Ivan Lozano62cd0382021-11-01 10:27:54 -0400247 if Bool(mod.sanitize.Properties.Sanitize.Memtag_heap) && mod.Binary() {
248 noteDep := "note_memtag_heap_async"
249 if Bool(mod.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
250 noteDep = "note_memtag_heap_sync"
251 }
252 // If we're using snapshots, redirect to snapshot whenever possible
253 // TODO(b/178470649): clean manual snapshot redirections
254 snapshot := mctx.Provider(cc.SnapshotInfoProvider).(cc.SnapshotInfo)
255 if lib, ok := snapshot.StaticLibs[noteDep]; ok {
256 noteDep = lib
257 }
258 depTag := cc.StaticDepTag(true)
259 variations := append(mctx.Target().Variations(),
260 blueprint.Variation{Mutator: "link", Variation: "static"})
261 if mod.Device() {
262 variations = append(variations, mod.ImageVariation())
263 }
264 mctx.AddFarVariationDependencies(variations, depTag, noteDep)
265 }
266
Tri Vo0a74c3e2021-04-01 13:59:27 -0700267 variations := mctx.Target().Variations()
268 var depTag blueprint.DependencyTag
269 var deps []string
270
Tri Vo505b0e82021-04-08 15:50:48 -0700271 if mod.IsSanitizerEnabled(cc.Asan) ||
Colin Cross1faf8232022-06-24 18:42:32 -0700272 (mod.IsSanitizerEnabled(cc.Fuzzer) && (mctx.Arch().ArchType != android.Arm64 || !mctx.Os().Bionic())) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700273 variations = append(variations,
274 blueprint.Variation{Mutator: "link", Variation: "shared"})
275 depTag = cc.SharedDepTag()
276 deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan")}
Tri Vo505b0e82021-04-08 15:50:48 -0700277 } else if mod.IsSanitizerEnabled(cc.Hwasan) ||
Colin Cross1faf8232022-06-24 18:42:32 -0700278 (mod.IsSanitizerEnabled(cc.Fuzzer) && mctx.Arch().ArchType == android.Arm64 && mctx.Os().Bionic()) {
Ivan Lozano6c5b8f42021-11-15 09:35:12 -0500279 // TODO(b/204776996): HWASan for static Rust binaries isn't supported yet.
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400280 if binary, ok := mod.compiler.(binaryInterface); ok {
281 if binary.staticallyLinked() {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700282 mctx.ModuleErrorf("HWASan is not supported for static Rust executables yet.")
283 }
284 }
285
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400286 // Always link against the shared library -- static binaries will pull in the static
287 // library during final link if necessary
288 variations = append(variations,
289 blueprint.Variation{Mutator: "link", Variation: "shared"})
290 depTag = cc.SharedDepTag()
291 deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "hwasan")}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500292 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700293
Ivan Lozano62cd0382021-11-01 10:27:54 -0400294 if len(deps) > 0 {
295 mctx.AddFarVariationDependencies(variations, depTag, deps...)
296 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500297 }
298}
299
300func (sanitize *sanitize) SetSanitizer(t cc.SanitizerType, b bool) {
301 sanitizerSet := false
302 switch t {
303 case cc.Fuzzer:
304 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
305 sanitizerSet = true
306 case cc.Asan:
307 sanitize.Properties.Sanitize.Address = boolPtr(b)
308 sanitizerSet = true
Tri Vo0a74c3e2021-04-01 13:59:27 -0700309 case cc.Hwasan:
310 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
311 sanitizerSet = true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400312 case cc.Memtag_heap:
313 sanitize.Properties.Sanitize.Memtag_heap = boolPtr(b)
314 sanitizerSet = true
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500315 default:
316 panic(fmt.Errorf("setting unsupported sanitizerType %d", t))
317 }
318 if b && sanitizerSet {
319 sanitize.Properties.SanitizerEnabled = true
320 }
321}
322
Ivan Lozanod7586b62021-04-01 09:49:36 -0400323func (m *Module) UbsanRuntimeNeeded() bool {
324 return false
325}
326
327func (m *Module) MinimalRuntimeNeeded() bool {
328 return false
329}
330
331func (m *Module) UbsanRuntimeDep() bool {
332 return false
333}
334
335func (m *Module) MinimalRuntimeDep() bool {
336 return false
337}
338
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500339// Check if the sanitizer is explicitly disabled (as opposed to nil by
340// virtue of not being set).
341func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t cc.SanitizerType) bool {
342 if sanitize == nil {
343 return false
344 }
345 if Bool(sanitize.Properties.Sanitize.Never) {
346 return true
347 }
348 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
349 return sanitizerVal != nil && *sanitizerVal == false
350}
351
352// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
353// because enabling a sanitizer either directly (via the blueprint) or
354// indirectly (via a mutator) sets the bool ptr to true, and you can't
355// distinguish between the cases. It isn't needed though - both cases can be
356// treated identically.
357func (sanitize *sanitize) isSanitizerEnabled(t cc.SanitizerType) bool {
358 if sanitize == nil || !sanitize.Properties.SanitizerEnabled {
359 return false
360 }
361
362 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
363 return sanitizerVal != nil && *sanitizerVal == true
364}
365
366func (sanitize *sanitize) getSanitizerBoolPtr(t cc.SanitizerType) *bool {
367 switch t {
368 case cc.Fuzzer:
369 return sanitize.Properties.Sanitize.Fuzzer
370 case cc.Asan:
371 return sanitize.Properties.Sanitize.Address
Tri Vo0a74c3e2021-04-01 13:59:27 -0700372 case cc.Hwasan:
373 return sanitize.Properties.Sanitize.Hwaddress
Ivan Lozano62cd0382021-11-01 10:27:54 -0400374 case cc.Memtag_heap:
375 return sanitize.Properties.Sanitize.Memtag_heap
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500376 default:
377 return nil
378 }
379}
380
Tri Vo0a74c3e2021-04-01 13:59:27 -0700381func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
382 // Add a suffix for hwasan rlib libraries to allow surfacing both the sanitized and
383 // non-sanitized variants to make without a name conflict.
384 if entries.Class == "RLIB_LIBRARIES" || entries.Class == "STATIC_LIBRARIES" {
385 if sanitize.isSanitizerEnabled(cc.Hwasan) {
386 entries.SubName += ".hwasan"
387 }
388 }
389}
390
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500391func (mod *Module) SanitizerSupported(t cc.SanitizerType) bool {
392 if mod.Host() {
393 return false
394 }
395 switch t {
396 case cc.Fuzzer:
397 return true
398 case cc.Asan:
399 return true
Tri Vo0a74c3e2021-04-01 13:59:27 -0700400 case cc.Hwasan:
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400401 // TODO(b/180495975): HWASan for static Rust binaries isn't supported yet.
402 if mod.StaticExecutable() {
403 return false
404 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700405 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400406 case cc.Memtag_heap:
407 return true
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500408 default:
409 return false
410 }
411}
412
413func (mod *Module) IsSanitizerEnabled(t cc.SanitizerType) bool {
414 return mod.sanitize.isSanitizerEnabled(t)
415}
416
417func (mod *Module) IsSanitizerExplicitlyDisabled(t cc.SanitizerType) bool {
418 if mod.Host() {
419 return true
420 }
421
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500422 return mod.sanitize.isSanitizerExplicitlyDisabled(t)
423}
424
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500425func (mod *Module) SetSanitizer(t cc.SanitizerType, b bool) {
426 if !Bool(mod.sanitize.Properties.Sanitize.Never) {
427 mod.sanitize.SetSanitizer(t, b)
428 }
429}
430
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500431func (mod *Module) StaticallyLinked() bool {
432 if lib, ok := mod.compiler.(libraryInterface); ok {
Tri Vo505b0e82021-04-08 15:50:48 -0700433 return lib.rlib() || lib.static()
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400434 } else if binary, ok := mod.compiler.(binaryInterface); ok {
435 return binary.staticallyLinked()
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500436 }
437 return false
438}
439
440func (mod *Module) SetInSanitizerDir() {
441 mod.sanitize.Properties.InSanitizerDir = true
442}
443
444func (mod *Module) SanitizeNever() bool {
445 return Bool(mod.sanitize.Properties.Sanitize.Never)
446}
447
448var _ cc.PlatformSanitizeable = (*Module)(nil)
449
450func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
451 switch t := tag.(type) {
452 case dependencyTag:
453 return t.library
454 default:
455 return cc.IsSanitizableDependencyTag(tag)
456 }
457}
458
459func (m *Module) SanitizableDepTagChecker() cc.SantizableDependencyTagChecker {
460 return IsSanitizableDependencyTag
461}