Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 1 | // 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 18 | "fmt" |
| 19 | "strings" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | "github.com/google/blueprint/proptools" |
| 23 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 24 | "android/soong/android" |
| 25 | "android/soong/cc" |
| 26 | "android/soong/rust/config" |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 27 | ) |
| 28 | |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 29 | // TODO: When Rust has sanitizer-parity with CC, deduplicate this struct |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 30 | type SanitizeProperties struct { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 31 | // enable AddressSanitizer, HWAddressSanitizer, and others. |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 32 | Sanitize struct { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 33 | Address *bool `android:"arch_variant"` |
| 34 | Hwaddress *bool `android:"arch_variant"` |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 35 | |
| 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 Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 51 | } |
Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 52 | SanitizerEnabled bool `blueprint:"mutated"` |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 53 | |
| 54 | // Used when we need to place libraries in their own directory, such as ASAN. |
| 55 | InSanitizerDir bool `blueprint:"mutated"` |
| 56 | } |
| 57 | |
| 58 | var fuzzerFlags = []string{ |
Charisee | 5ddec43 | 2022-03-01 03:02:51 +0000 | [diff] [blame] | 59 | "-C passes='sancov-module'", |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 60 | |
| 61 | "--cfg fuzzing", |
Ivan Lozano | c044f5b | 2021-04-02 12:43:28 -0400 | [diff] [blame] | 62 | "-C llvm-args=-sanitizer-coverage-level=3", |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 63 | "-C llvm-args=-sanitizer-coverage-trace-compares", |
| 64 | "-C llvm-args=-sanitizer-coverage-inline-8bit-counters", |
Ivan Lozano | 1247b3c | 2023-06-12 14:57:44 -0400 | [diff] [blame] | 65 | "-C llvm-args=-sanitizer-coverage-pc-table", |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 66 | |
Ivan Lozano | aa7c980 | 2021-08-02 09:43:46 -0400 | [diff] [blame] | 67 | // See https://github.com/rust-fuzz/cargo-fuzz/pull/193 |
| 68 | "-C link-dead-code", |
| 69 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 70 | // Sancov breaks with lto |
Charisee | 5ddec43 | 2022-03-01 03:02:51 +0000 | [diff] [blame] | 71 | // TODO: Remove when https://bugs.llvm.org/show_bug.cgi?id=41734 is resolved and sancov-module works with LTO |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 72 | "-C lto=no", |
| 73 | } |
| 74 | |
| 75 | var asanFlags = []string{ |
| 76 | "-Z sanitizer=address", |
| 77 | } |
| 78 | |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 79 | // See cc/sanitize.go's hwasanGlobalOptions for global hwasan options. |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 80 | var hwasanFlags = []string{ |
| 81 | "-Z sanitizer=hwaddress", |
| 82 | "-C target-feature=+tagged-globals", |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 83 | |
| 84 | // Flags from cc/sanitize.go hwasanFlags |
| 85 | "-C llvm-args=--aarch64-enable-global-isel-at-O=-1", |
| 86 | "-C llvm-args=-fast-isel=false", |
| 87 | "-C llvm-args=-instcombine-lower-dbg-declare=0", |
| 88 | |
| 89 | // Additional flags for HWASAN-ified Rust/C interop |
| 90 | "-C llvm-args=--hwasan-with-ifunc", |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 93 | func boolPtr(v bool) *bool { |
| 94 | if v { |
| 95 | return &v |
| 96 | } else { |
| 97 | return nil |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func init() { |
| 102 | } |
| 103 | func (sanitize *sanitize) props() []interface{} { |
| 104 | return []interface{}{&sanitize.Properties} |
| 105 | } |
| 106 | |
| 107 | func (sanitize *sanitize) begin(ctx BaseModuleContext) { |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 108 | s := &sanitize.Properties.Sanitize |
| 109 | |
| 110 | // Never always wins. |
| 111 | if Bool(s.Never) { |
| 112 | return |
| 113 | } |
| 114 | |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 115 | // rust_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {Memtag_heap}). |
| 116 | if binary, ok := ctx.RustModule().compiler.(binaryInterface); ok && binary.testBinary() { |
| 117 | if s.Memtag_heap == nil { |
| 118 | s.Memtag_heap = proptools.BoolPtr(true) |
| 119 | } |
| 120 | if s.Diag.Memtag_heap == nil { |
| 121 | s.Diag.Memtag_heap = proptools.BoolPtr(true) |
| 122 | } |
| 123 | } |
| 124 | |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 125 | var globalSanitizers []string |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 126 | var globalSanitizersDiag []string |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 127 | |
| 128 | if ctx.Host() { |
| 129 | if !ctx.Windows() { |
| 130 | globalSanitizers = ctx.Config().SanitizeHost() |
| 131 | } |
| 132 | } else { |
| 133 | arches := ctx.Config().SanitizeDeviceArch() |
| 134 | if len(arches) == 0 || android.InList(ctx.Arch().ArchType.Name, arches) { |
| 135 | globalSanitizers = ctx.Config().SanitizeDevice() |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 136 | globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag() |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | if len(globalSanitizers) > 0 { |
| 141 | var found bool |
| 142 | |
| 143 | // Global Sanitizers |
| 144 | if found, globalSanitizers = android.RemoveFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil { |
Ivan Lozano | 6c5b8f4 | 2021-11-15 09:35:12 -0500 | [diff] [blame] | 145 | // TODO(b/204776996): HWASan for static Rust binaries isn't supported yet. |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 146 | if !ctx.RustModule().StaticExecutable() { |
| 147 | s.Hwaddress = proptools.BoolPtr(true) |
| 148 | } |
| 149 | } |
| 150 | |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 151 | if found, globalSanitizers = android.RemoveFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil { |
| 152 | if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) { |
| 153 | s.Memtag_heap = proptools.BoolPtr(true) |
| 154 | } |
| 155 | } |
| 156 | |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 157 | if found, globalSanitizers = android.RemoveFromList("address", globalSanitizers); found && s.Address == nil { |
| 158 | s.Address = proptools.BoolPtr(true) |
| 159 | } |
| 160 | |
| 161 | if found, globalSanitizers = android.RemoveFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil { |
Ivan Lozano | 6c5b8f4 | 2021-11-15 09:35:12 -0500 | [diff] [blame] | 162 | // TODO(b/204776996): HWASan for static Rust binaries isn't supported yet, and fuzzer enables HWAsan |
| 163 | if !ctx.RustModule().StaticExecutable() { |
| 164 | s.Fuzzer = proptools.BoolPtr(true) |
| 165 | } |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 168 | // Global Diag Sanitizers |
| 169 | if found, globalSanitizersDiag = android.RemoveFromList("memtag_heap", globalSanitizersDiag); found && |
| 170 | s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) { |
| 171 | s.Diag.Memtag_heap = proptools.BoolPtr(true) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Enable Memtag for all components in the include paths (for Aarch64 only) |
Colin Cross | 1faf823 | 2022-06-24 18:42:32 -0700 | [diff] [blame] | 176 | if ctx.Arch().ArchType == android.Arm64 && ctx.Os().Bionic() { |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 177 | if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) { |
| 178 | if s.Memtag_heap == nil { |
| 179 | s.Memtag_heap = proptools.BoolPtr(true) |
| 180 | } |
| 181 | if s.Diag.Memtag_heap == nil { |
| 182 | s.Diag.Memtag_heap = proptools.BoolPtr(true) |
| 183 | } |
| 184 | } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) { |
| 185 | if s.Memtag_heap == nil { |
| 186 | s.Memtag_heap = proptools.BoolPtr(true) |
| 187 | } |
| 188 | } |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 189 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 190 | |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 191 | // HWASan requires AArch64 hardware feature (top-byte-ignore). |
Colin Cross | 1faf823 | 2022-06-24 18:42:32 -0700 | [diff] [blame] | 192 | if ctx.Arch().ArchType != android.Arm64 || !ctx.Os().Bionic() { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 193 | s.Hwaddress = nil |
| 194 | } |
| 195 | |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 196 | // HWASan ramdisk (which is built from recovery) goes over some bootloader limit. |
| 197 | // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary. |
| 198 | if (ctx.RustModule().InRamdisk() || ctx.RustModule().InVendorRamdisk() || ctx.RustModule().InRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") { |
| 199 | s.Hwaddress = nil |
| 200 | } |
| 201 | |
| 202 | if Bool(s.Hwaddress) { |
| 203 | s.Address = nil |
| 204 | } |
| 205 | |
Ivan Lozano | 839b5f9 | 2023-10-11 13:18:37 -0400 | [diff] [blame] | 206 | // TODO: Remove once b/304507701 is resolved |
| 207 | if Bool(s.Address) && ctx.Host() { |
| 208 | s.Address = nil |
| 209 | } |
| 210 | |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 211 | // Memtag_heap is only implemented on AArch64. |
Colin Cross | 1faf823 | 2022-06-24 18:42:32 -0700 | [diff] [blame] | 212 | if ctx.Arch().ArchType != android.Arm64 || !ctx.Os().Bionic() { |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 213 | s.Memtag_heap = nil |
| 214 | } |
| 215 | |
Colin Cross | 3ef9285 | 2023-09-05 12:09:30 -0700 | [diff] [blame] | 216 | // Disable sanitizers for musl x86 modules, rustc does not support any sanitizers. |
| 217 | if ctx.Os() == android.LinuxMusl && ctx.Arch().ArchType == android.X86 { |
| 218 | s.Never = boolPtr(true) |
| 219 | } |
| 220 | |
Florian Mayer | 296d595 | 2022-10-12 19:30:14 +0000 | [diff] [blame] | 221 | // TODO:(b/178369775) |
Ivan Lozano | 2fcbffa | 2023-07-27 10:40:52 -0400 | [diff] [blame] | 222 | // For now sanitizing is only supported on non-windows targets |
| 223 | if ctx.Os() != android.Windows && (Bool(s.Hwaddress) || Bool(s.Address) || Bool(s.Memtag_heap) || Bool(s.Fuzzer)) { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 224 | sanitize.Properties.SanitizerEnabled = true |
| 225 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | type sanitize struct { |
| 229 | Properties SanitizeProperties |
| 230 | } |
| 231 | |
| 232 | func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) { |
| 233 | if !sanitize.Properties.SanitizerEnabled { |
| 234 | return flags, deps |
| 235 | } |
Ivan Lozano | 408d373 | 2023-07-27 13:28:08 -0400 | [diff] [blame] | 236 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 237 | if Bool(sanitize.Properties.Sanitize.Fuzzer) { |
| 238 | flags.RustFlags = append(flags.RustFlags, fuzzerFlags...) |
Ivan Lozano | 408d373 | 2023-07-27 13:28:08 -0400 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | if Bool(sanitize.Properties.Sanitize.Hwaddress) { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 242 | flags.RustFlags = append(flags.RustFlags, hwasanFlags...) |
Ivan Lozano | 408d373 | 2023-07-27 13:28:08 -0400 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | if Bool(sanitize.Properties.Sanitize.Address) { |
Ivan Lozano | f3e8fc3 | 2021-11-13 07:43:54 -0500 | [diff] [blame] | 246 | flags.RustFlags = append(flags.RustFlags, asanFlags...) |
Ivan Lozano | 2fcbffa | 2023-07-27 10:40:52 -0400 | [diff] [blame] | 247 | if ctx.Host() { |
| 248 | // -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 249 | // libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
| 250 | flags.LinkFlags = append(flags.LinkFlags, []string{"-Wl,--no-as-needed"}...) |
| 251 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 252 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 253 | return flags, deps |
| 254 | } |
| 255 | |
| 256 | func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 257 | return deps |
| 258 | } |
| 259 | |
| 260 | func rustSanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) { |
| 261 | if mod, ok := mctx.Module().(*Module); ok && mod.sanitize != nil { |
| 262 | if !mod.Enabled() { |
| 263 | return |
| 264 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 265 | |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 266 | if Bool(mod.sanitize.Properties.Sanitize.Memtag_heap) && mod.Binary() { |
| 267 | noteDep := "note_memtag_heap_async" |
| 268 | if Bool(mod.sanitize.Properties.Sanitize.Diag.Memtag_heap) { |
| 269 | noteDep = "note_memtag_heap_sync" |
| 270 | } |
| 271 | // If we're using snapshots, redirect to snapshot whenever possible |
| 272 | // TODO(b/178470649): clean manual snapshot redirections |
Colin Cross | ff694a8 | 2023-12-13 15:54:49 -0800 | [diff] [blame] | 273 | snapshot, _ := android.ModuleProvider(mctx, cc.SnapshotInfoProvider) |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 274 | if lib, ok := snapshot.StaticLibs[noteDep]; ok { |
| 275 | noteDep = lib |
| 276 | } |
| 277 | depTag := cc.StaticDepTag(true) |
| 278 | variations := append(mctx.Target().Variations(), |
| 279 | blueprint.Variation{Mutator: "link", Variation: "static"}) |
| 280 | if mod.Device() { |
| 281 | variations = append(variations, mod.ImageVariation()) |
| 282 | } |
| 283 | mctx.AddFarVariationDependencies(variations, depTag, noteDep) |
| 284 | } |
| 285 | |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 286 | variations := mctx.Target().Variations() |
| 287 | var depTag blueprint.DependencyTag |
| 288 | var deps []string |
| 289 | |
Ivan Lozano | 408d373 | 2023-07-27 13:28:08 -0400 | [diff] [blame] | 290 | if mod.IsSanitizerEnabled(cc.Asan) { |
Ivan Lozano | 2fcbffa | 2023-07-27 10:40:52 -0400 | [diff] [blame] | 291 | if mod.Host() { |
| 292 | variations = append(variations, |
| 293 | blueprint.Variation{Mutator: "link", Variation: "static"}) |
| 294 | depTag = cc.StaticDepTag(false) |
| 295 | deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan.static")} |
| 296 | } else { |
| 297 | variations = append(variations, |
| 298 | blueprint.Variation{Mutator: "link", Variation: "shared"}) |
| 299 | depTag = cc.SharedDepTag() |
| 300 | deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan")} |
| 301 | } |
Ivan Lozano | 408d373 | 2023-07-27 13:28:08 -0400 | [diff] [blame] | 302 | } else if mod.IsSanitizerEnabled(cc.Hwasan) { |
Ivan Lozano | 6c5b8f4 | 2021-11-15 09:35:12 -0500 | [diff] [blame] | 303 | // TODO(b/204776996): HWASan for static Rust binaries isn't supported yet. |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 304 | if binary, ok := mod.compiler.(binaryInterface); ok { |
| 305 | if binary.staticallyLinked() { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 306 | mctx.ModuleErrorf("HWASan is not supported for static Rust executables yet.") |
| 307 | } |
| 308 | } |
| 309 | |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 310 | // Always link against the shared library -- static binaries will pull in the static |
| 311 | // library during final link if necessary |
| 312 | variations = append(variations, |
| 313 | blueprint.Variation{Mutator: "link", Variation: "shared"}) |
| 314 | depTag = cc.SharedDepTag() |
| 315 | deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "hwasan")} |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 316 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 317 | |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 318 | if len(deps) > 0 { |
| 319 | mctx.AddFarVariationDependencies(variations, depTag, deps...) |
| 320 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
| 324 | func (sanitize *sanitize) SetSanitizer(t cc.SanitizerType, b bool) { |
| 325 | sanitizerSet := false |
| 326 | switch t { |
| 327 | case cc.Fuzzer: |
| 328 | sanitize.Properties.Sanitize.Fuzzer = boolPtr(b) |
| 329 | sanitizerSet = true |
| 330 | case cc.Asan: |
| 331 | sanitize.Properties.Sanitize.Address = boolPtr(b) |
| 332 | sanitizerSet = true |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 333 | case cc.Hwasan: |
| 334 | sanitize.Properties.Sanitize.Hwaddress = boolPtr(b) |
| 335 | sanitizerSet = true |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 336 | case cc.Memtag_heap: |
| 337 | sanitize.Properties.Sanitize.Memtag_heap = boolPtr(b) |
| 338 | sanitizerSet = true |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 339 | default: |
| 340 | panic(fmt.Errorf("setting unsupported sanitizerType %d", t)) |
| 341 | } |
| 342 | if b && sanitizerSet { |
| 343 | sanitize.Properties.SanitizerEnabled = true |
| 344 | } |
| 345 | } |
| 346 | |
Ivan Lozano | d7586b6 | 2021-04-01 09:49:36 -0400 | [diff] [blame] | 347 | func (m *Module) UbsanRuntimeNeeded() bool { |
| 348 | return false |
| 349 | } |
| 350 | |
| 351 | func (m *Module) MinimalRuntimeNeeded() bool { |
| 352 | return false |
| 353 | } |
| 354 | |
| 355 | func (m *Module) UbsanRuntimeDep() bool { |
| 356 | return false |
| 357 | } |
| 358 | |
| 359 | func (m *Module) MinimalRuntimeDep() bool { |
| 360 | return false |
| 361 | } |
| 362 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 363 | // Check if the sanitizer is explicitly disabled (as opposed to nil by |
| 364 | // virtue of not being set). |
| 365 | func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t cc.SanitizerType) bool { |
| 366 | if sanitize == nil { |
| 367 | return false |
| 368 | } |
| 369 | if Bool(sanitize.Properties.Sanitize.Never) { |
| 370 | return true |
| 371 | } |
| 372 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 373 | return sanitizerVal != nil && *sanitizerVal == false |
| 374 | } |
| 375 | |
| 376 | // There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled) |
| 377 | // because enabling a sanitizer either directly (via the blueprint) or |
| 378 | // indirectly (via a mutator) sets the bool ptr to true, and you can't |
| 379 | // distinguish between the cases. It isn't needed though - both cases can be |
| 380 | // treated identically. |
| 381 | func (sanitize *sanitize) isSanitizerEnabled(t cc.SanitizerType) bool { |
| 382 | if sanitize == nil || !sanitize.Properties.SanitizerEnabled { |
| 383 | return false |
| 384 | } |
| 385 | |
| 386 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 387 | return sanitizerVal != nil && *sanitizerVal == true |
| 388 | } |
| 389 | |
| 390 | func (sanitize *sanitize) getSanitizerBoolPtr(t cc.SanitizerType) *bool { |
| 391 | switch t { |
| 392 | case cc.Fuzzer: |
| 393 | return sanitize.Properties.Sanitize.Fuzzer |
| 394 | case cc.Asan: |
| 395 | return sanitize.Properties.Sanitize.Address |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 396 | case cc.Hwasan: |
| 397 | return sanitize.Properties.Sanitize.Hwaddress |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 398 | case cc.Memtag_heap: |
| 399 | return sanitize.Properties.Sanitize.Memtag_heap |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 400 | default: |
| 401 | return nil |
| 402 | } |
| 403 | } |
| 404 | |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 405 | func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, entries *android.AndroidMkEntries) { |
| 406 | // Add a suffix for hwasan rlib libraries to allow surfacing both the sanitized and |
| 407 | // non-sanitized variants to make without a name conflict. |
| 408 | if entries.Class == "RLIB_LIBRARIES" || entries.Class == "STATIC_LIBRARIES" { |
| 409 | if sanitize.isSanitizerEnabled(cc.Hwasan) { |
| 410 | entries.SubName += ".hwasan" |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 415 | func (mod *Module) SanitizerSupported(t cc.SanitizerType) bool { |
Ivan Lozano | 2fcbffa | 2023-07-27 10:40:52 -0400 | [diff] [blame] | 416 | // Sanitizers are not supported on Windows targets. |
| 417 | if mod.Os() == android.Windows { |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 418 | return false |
| 419 | } |
| 420 | switch t { |
| 421 | case cc.Fuzzer: |
| 422 | return true |
| 423 | case cc.Asan: |
| 424 | return true |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 425 | case cc.Hwasan: |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame] | 426 | // TODO(b/180495975): HWASan for static Rust binaries isn't supported yet. |
| 427 | if mod.StaticExecutable() { |
| 428 | return false |
| 429 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 430 | return true |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 431 | case cc.Memtag_heap: |
| 432 | return true |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 433 | default: |
| 434 | return false |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | func (mod *Module) IsSanitizerEnabled(t cc.SanitizerType) bool { |
| 439 | return mod.sanitize.isSanitizerEnabled(t) |
| 440 | } |
| 441 | |
| 442 | func (mod *Module) IsSanitizerExplicitlyDisabled(t cc.SanitizerType) bool { |
Ivan Lozano | 2fcbffa | 2023-07-27 10:40:52 -0400 | [diff] [blame] | 443 | // Sanitizers are not supported on Windows targets. |
| 444 | if mod.Os() == android.Windows { |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 445 | return true |
| 446 | } |
| 447 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 448 | return mod.sanitize.isSanitizerExplicitlyDisabled(t) |
| 449 | } |
| 450 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 451 | func (mod *Module) SetSanitizer(t cc.SanitizerType, b bool) { |
| 452 | if !Bool(mod.sanitize.Properties.Sanitize.Never) { |
| 453 | mod.sanitize.SetSanitizer(t, b) |
| 454 | } |
| 455 | } |
| 456 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 457 | func (mod *Module) StaticallyLinked() bool { |
| 458 | if lib, ok := mod.compiler.(libraryInterface); ok { |
Tri Vo | 505b0e8 | 2021-04-08 15:50:48 -0700 | [diff] [blame] | 459 | return lib.rlib() || lib.static() |
Ivan Lozano | 21fa0a5 | 2021-11-01 09:19:45 -0400 | [diff] [blame] | 460 | } else if binary, ok := mod.compiler.(binaryInterface); ok { |
| 461 | return binary.staticallyLinked() |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 462 | } |
| 463 | return false |
| 464 | } |
| 465 | |
| 466 | func (mod *Module) SetInSanitizerDir() { |
| 467 | mod.sanitize.Properties.InSanitizerDir = true |
| 468 | } |
| 469 | |
| 470 | func (mod *Module) SanitizeNever() bool { |
| 471 | return Bool(mod.sanitize.Properties.Sanitize.Never) |
| 472 | } |
| 473 | |
| 474 | var _ cc.PlatformSanitizeable = (*Module)(nil) |
| 475 | |
| 476 | func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool { |
| 477 | switch t := tag.(type) { |
| 478 | case dependencyTag: |
| 479 | return t.library |
| 480 | default: |
| 481 | return cc.IsSanitizableDependencyTag(tag) |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | func (m *Module) SanitizableDepTagChecker() cc.SantizableDependencyTagChecker { |
| 486 | return IsSanitizableDependencyTag |
| 487 | } |