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 | |
| 29 | type SanitizeProperties struct { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 30 | // enable AddressSanitizer, HWAddressSanitizer, and others. |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 31 | Sanitize struct { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 32 | Address *bool `android:"arch_variant"` |
| 33 | Hwaddress *bool `android:"arch_variant"` |
| 34 | Fuzzer *bool `android:"arch_variant"` |
| 35 | Never *bool `android:"arch_variant"` |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 36 | } |
| 37 | SanitizerEnabled bool `blueprint:"mutated"` |
| 38 | SanitizeDep bool `blueprint:"mutated"` |
| 39 | |
| 40 | // Used when we need to place libraries in their own directory, such as ASAN. |
| 41 | InSanitizerDir bool `blueprint:"mutated"` |
| 42 | } |
| 43 | |
| 44 | var fuzzerFlags = []string{ |
| 45 | "-C passes='sancov'", |
| 46 | |
| 47 | "--cfg fuzzing", |
Ivan Lozano | c044f5b | 2021-04-02 12:43:28 -0400 | [diff] [blame] | 48 | "-C llvm-args=-sanitizer-coverage-level=3", |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 49 | "-C llvm-args=-sanitizer-coverage-trace-compares", |
| 50 | "-C llvm-args=-sanitizer-coverage-inline-8bit-counters", |
| 51 | "-C llvm-args=-sanitizer-coverage-trace-geps", |
| 52 | "-C llvm-args=-sanitizer-coverage-prune-blocks=0", |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 53 | |
Ivan Lozano | aa7c980 | 2021-08-02 09:43:46 -0400 | [diff] [blame] | 54 | // See https://github.com/rust-fuzz/cargo-fuzz/pull/193 |
| 55 | "-C link-dead-code", |
| 56 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 57 | // Sancov breaks with lto |
| 58 | // TODO: Remove when https://bugs.llvm.org/show_bug.cgi?id=41734 is resolved and sancov works with LTO |
| 59 | "-C lto=no", |
| 60 | } |
| 61 | |
| 62 | var asanFlags = []string{ |
| 63 | "-Z sanitizer=address", |
| 64 | } |
| 65 | |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame^] | 66 | // See cc/sanitize.go's hwasanGlobalOptions for global hwasan options. |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 67 | var hwasanFlags = []string{ |
| 68 | "-Z sanitizer=hwaddress", |
| 69 | "-C target-feature=+tagged-globals", |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame^] | 70 | |
| 71 | // Flags from cc/sanitize.go hwasanFlags |
| 72 | "-C llvm-args=--aarch64-enable-global-isel-at-O=-1", |
| 73 | "-C llvm-args=-fast-isel=false", |
| 74 | "-C llvm-args=-instcombine-lower-dbg-declare=0", |
| 75 | |
| 76 | // Additional flags for HWASAN-ified Rust/C interop |
| 77 | "-C llvm-args=--hwasan-with-ifunc", |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 80 | func boolPtr(v bool) *bool { |
| 81 | if v { |
| 82 | return &v |
| 83 | } else { |
| 84 | return nil |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func init() { |
| 89 | } |
| 90 | func (sanitize *sanitize) props() []interface{} { |
| 91 | return []interface{}{&sanitize.Properties} |
| 92 | } |
| 93 | |
| 94 | func (sanitize *sanitize) begin(ctx BaseModuleContext) { |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame^] | 95 | s := &sanitize.Properties.Sanitize |
| 96 | |
| 97 | // Never always wins. |
| 98 | if Bool(s.Never) { |
| 99 | return |
| 100 | } |
| 101 | |
| 102 | var globalSanitizers []string |
| 103 | |
| 104 | if ctx.Host() { |
| 105 | if !ctx.Windows() { |
| 106 | globalSanitizers = ctx.Config().SanitizeHost() |
| 107 | } |
| 108 | } else { |
| 109 | arches := ctx.Config().SanitizeDeviceArch() |
| 110 | if len(arches) == 0 || android.InList(ctx.Arch().ArchType.Name, arches) { |
| 111 | globalSanitizers = ctx.Config().SanitizeDevice() |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if len(globalSanitizers) > 0 { |
| 116 | var found bool |
| 117 | |
| 118 | // Global Sanitizers |
| 119 | if found, globalSanitizers = android.RemoveFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil { |
| 120 | // TODO(b/180495975): HWASan for static Rust binaries isn't supported yet. |
| 121 | if !ctx.RustModule().StaticExecutable() { |
| 122 | s.Hwaddress = proptools.BoolPtr(true) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if found, globalSanitizers = android.RemoveFromList("address", globalSanitizers); found && s.Address == nil { |
| 127 | s.Address = proptools.BoolPtr(true) |
| 128 | } |
| 129 | |
| 130 | if found, globalSanitizers = android.RemoveFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil { |
| 131 | s.Fuzzer = proptools.BoolPtr(true) |
| 132 | } |
| 133 | |
| 134 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 135 | |
| 136 | // TODO:(b/178369775) |
| 137 | // For now sanitizing is only supported on devices |
| 138 | if ctx.Os() == android.Android && Bool(s.Fuzzer) { |
| 139 | sanitize.Properties.SanitizerEnabled = true |
| 140 | } |
| 141 | |
| 142 | if ctx.Os() == android.Android && Bool(s.Address) { |
| 143 | sanitize.Properties.SanitizerEnabled = true |
| 144 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 145 | |
| 146 | // HWASan requires AArch64 hardware feature (top-byte-ignore). |
| 147 | if ctx.Arch().ArchType != android.Arm64 { |
| 148 | s.Hwaddress = nil |
| 149 | } |
| 150 | |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame^] | 151 | // HWASan ramdisk (which is built from recovery) goes over some bootloader limit. |
| 152 | // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary. |
| 153 | if (ctx.RustModule().InRamdisk() || ctx.RustModule().InVendorRamdisk() || ctx.RustModule().InRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") { |
| 154 | s.Hwaddress = nil |
| 155 | } |
| 156 | |
| 157 | if Bool(s.Hwaddress) { |
| 158 | s.Address = nil |
| 159 | } |
| 160 | |
| 161 | if ctx.Os() == android.Android && (Bool(s.Hwaddress) || Bool(s.Address)) { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 162 | sanitize.Properties.SanitizerEnabled = true |
| 163 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | type sanitize struct { |
| 167 | Properties SanitizeProperties |
| 168 | } |
| 169 | |
| 170 | func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) { |
| 171 | if !sanitize.Properties.SanitizerEnabled { |
| 172 | return flags, deps |
| 173 | } |
| 174 | if Bool(sanitize.Properties.Sanitize.Fuzzer) { |
| 175 | flags.RustFlags = append(flags.RustFlags, fuzzerFlags...) |
Tri Vo | 505b0e8 | 2021-04-08 15:50:48 -0700 | [diff] [blame] | 176 | if ctx.Arch().ArchType == android.Arm64 { |
| 177 | flags.RustFlags = append(flags.RustFlags, hwasanFlags...) |
| 178 | } else { |
| 179 | flags.RustFlags = append(flags.RustFlags, asanFlags...) |
| 180 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 181 | } |
| 182 | if Bool(sanitize.Properties.Sanitize.Address) { |
| 183 | flags.RustFlags = append(flags.RustFlags, asanFlags...) |
| 184 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 185 | if Bool(sanitize.Properties.Sanitize.Hwaddress) { |
| 186 | flags.RustFlags = append(flags.RustFlags, hwasanFlags...) |
| 187 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 188 | return flags, deps |
| 189 | } |
| 190 | |
| 191 | func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 192 | return deps |
| 193 | } |
| 194 | |
| 195 | func rustSanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) { |
| 196 | if mod, ok := mctx.Module().(*Module); ok && mod.sanitize != nil { |
| 197 | if !mod.Enabled() { |
| 198 | return |
| 199 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 200 | |
| 201 | variations := mctx.Target().Variations() |
| 202 | var depTag blueprint.DependencyTag |
| 203 | var deps []string |
| 204 | |
Tri Vo | 505b0e8 | 2021-04-08 15:50:48 -0700 | [diff] [blame] | 205 | if mod.IsSanitizerEnabled(cc.Asan) || |
| 206 | (mod.IsSanitizerEnabled(cc.Fuzzer) && mctx.Arch().ArchType != android.Arm64) { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 207 | variations = append(variations, |
| 208 | blueprint.Variation{Mutator: "link", Variation: "shared"}) |
| 209 | depTag = cc.SharedDepTag() |
| 210 | deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan")} |
Tri Vo | 505b0e8 | 2021-04-08 15:50:48 -0700 | [diff] [blame] | 211 | } else if mod.IsSanitizerEnabled(cc.Hwasan) || |
| 212 | (mod.IsSanitizerEnabled(cc.Fuzzer) && mctx.Arch().ArchType == android.Arm64) { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 213 | // TODO(b/180495975): HWASan for static Rust binaries isn't supported yet. |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame^] | 214 | if binary, ok := mod.compiler.(binaryInterface); ok { |
| 215 | if binary.staticallyLinked() { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 216 | mctx.ModuleErrorf("HWASan is not supported for static Rust executables yet.") |
| 217 | } |
| 218 | } |
| 219 | |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame^] | 220 | // Always link against the shared library -- static binaries will pull in the static |
| 221 | // library during final link if necessary |
| 222 | variations = append(variations, |
| 223 | blueprint.Variation{Mutator: "link", Variation: "shared"}) |
| 224 | depTag = cc.SharedDepTag() |
| 225 | deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "hwasan")} |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 226 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 227 | |
| 228 | mctx.AddFarVariationDependencies(variations, depTag, deps...) |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | func (sanitize *sanitize) SetSanitizer(t cc.SanitizerType, b bool) { |
| 233 | sanitizerSet := false |
| 234 | switch t { |
| 235 | case cc.Fuzzer: |
| 236 | sanitize.Properties.Sanitize.Fuzzer = boolPtr(b) |
| 237 | sanitizerSet = true |
| 238 | case cc.Asan: |
| 239 | sanitize.Properties.Sanitize.Address = boolPtr(b) |
| 240 | sanitizerSet = true |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 241 | case cc.Hwasan: |
| 242 | sanitize.Properties.Sanitize.Hwaddress = boolPtr(b) |
| 243 | sanitizerSet = true |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 244 | default: |
| 245 | panic(fmt.Errorf("setting unsupported sanitizerType %d", t)) |
| 246 | } |
| 247 | if b && sanitizerSet { |
| 248 | sanitize.Properties.SanitizerEnabled = true |
| 249 | } |
| 250 | } |
| 251 | |
Ivan Lozano | d7586b6 | 2021-04-01 09:49:36 -0400 | [diff] [blame] | 252 | func (m *Module) UbsanRuntimeNeeded() bool { |
| 253 | return false |
| 254 | } |
| 255 | |
| 256 | func (m *Module) MinimalRuntimeNeeded() bool { |
| 257 | return false |
| 258 | } |
| 259 | |
| 260 | func (m *Module) UbsanRuntimeDep() bool { |
| 261 | return false |
| 262 | } |
| 263 | |
| 264 | func (m *Module) MinimalRuntimeDep() bool { |
| 265 | return false |
| 266 | } |
| 267 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 268 | // Check if the sanitizer is explicitly disabled (as opposed to nil by |
| 269 | // virtue of not being set). |
| 270 | func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t cc.SanitizerType) bool { |
| 271 | if sanitize == nil { |
| 272 | return false |
| 273 | } |
| 274 | if Bool(sanitize.Properties.Sanitize.Never) { |
| 275 | return true |
| 276 | } |
| 277 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 278 | return sanitizerVal != nil && *sanitizerVal == false |
| 279 | } |
| 280 | |
| 281 | // There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled) |
| 282 | // because enabling a sanitizer either directly (via the blueprint) or |
| 283 | // indirectly (via a mutator) sets the bool ptr to true, and you can't |
| 284 | // distinguish between the cases. It isn't needed though - both cases can be |
| 285 | // treated identically. |
| 286 | func (sanitize *sanitize) isSanitizerEnabled(t cc.SanitizerType) bool { |
| 287 | if sanitize == nil || !sanitize.Properties.SanitizerEnabled { |
| 288 | return false |
| 289 | } |
| 290 | |
| 291 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 292 | return sanitizerVal != nil && *sanitizerVal == true |
| 293 | } |
| 294 | |
| 295 | func (sanitize *sanitize) getSanitizerBoolPtr(t cc.SanitizerType) *bool { |
| 296 | switch t { |
| 297 | case cc.Fuzzer: |
| 298 | return sanitize.Properties.Sanitize.Fuzzer |
| 299 | case cc.Asan: |
| 300 | return sanitize.Properties.Sanitize.Address |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 301 | case cc.Hwasan: |
| 302 | return sanitize.Properties.Sanitize.Hwaddress |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 303 | default: |
| 304 | return nil |
| 305 | } |
| 306 | } |
| 307 | |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 308 | func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, entries *android.AndroidMkEntries) { |
| 309 | // Add a suffix for hwasan rlib libraries to allow surfacing both the sanitized and |
| 310 | // non-sanitized variants to make without a name conflict. |
| 311 | if entries.Class == "RLIB_LIBRARIES" || entries.Class == "STATIC_LIBRARIES" { |
| 312 | if sanitize.isSanitizerEnabled(cc.Hwasan) { |
| 313 | entries.SubName += ".hwasan" |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 318 | func (mod *Module) SanitizerSupported(t cc.SanitizerType) bool { |
| 319 | if mod.Host() { |
| 320 | return false |
| 321 | } |
| 322 | switch t { |
| 323 | case cc.Fuzzer: |
| 324 | return true |
| 325 | case cc.Asan: |
| 326 | return true |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 327 | case cc.Hwasan: |
Ivan Lozano | 5482d6a | 2021-11-01 10:13:25 -0400 | [diff] [blame^] | 328 | // TODO(b/180495975): HWASan for static Rust binaries isn't supported yet. |
| 329 | if mod.StaticExecutable() { |
| 330 | return false |
| 331 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 332 | return true |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 333 | default: |
| 334 | return false |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | func (mod *Module) IsSanitizerEnabled(t cc.SanitizerType) bool { |
| 339 | return mod.sanitize.isSanitizerEnabled(t) |
| 340 | } |
| 341 | |
| 342 | func (mod *Module) IsSanitizerExplicitlyDisabled(t cc.SanitizerType) bool { |
| 343 | if mod.Host() { |
| 344 | return true |
| 345 | } |
| 346 | |
| 347 | // TODO(b/178365482): Rust/CC interop doesn't work just yet; don't sanitize rust_ffi modules until |
| 348 | // linkage issues are resolved. |
| 349 | if lib, ok := mod.compiler.(libraryInterface); ok { |
| 350 | if lib.shared() || lib.static() { |
| 351 | return true |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return mod.sanitize.isSanitizerExplicitlyDisabled(t) |
| 356 | } |
| 357 | |
| 358 | func (mod *Module) SanitizeDep() bool { |
| 359 | return mod.sanitize.Properties.SanitizeDep |
| 360 | } |
| 361 | |
| 362 | func (mod *Module) SetSanitizer(t cc.SanitizerType, b bool) { |
| 363 | if !Bool(mod.sanitize.Properties.Sanitize.Never) { |
| 364 | mod.sanitize.SetSanitizer(t, b) |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | func (mod *Module) SetSanitizeDep(b bool) { |
| 369 | mod.sanitize.Properties.SanitizeDep = b |
| 370 | } |
| 371 | |
| 372 | func (mod *Module) StaticallyLinked() bool { |
| 373 | if lib, ok := mod.compiler.(libraryInterface); ok { |
Tri Vo | 505b0e8 | 2021-04-08 15:50:48 -0700 | [diff] [blame] | 374 | return lib.rlib() || lib.static() |
Ivan Lozano | 21fa0a5 | 2021-11-01 09:19:45 -0400 | [diff] [blame] | 375 | } else if binary, ok := mod.compiler.(binaryInterface); ok { |
| 376 | return binary.staticallyLinked() |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 377 | } |
| 378 | return false |
| 379 | } |
| 380 | |
| 381 | func (mod *Module) SetInSanitizerDir() { |
| 382 | mod.sanitize.Properties.InSanitizerDir = true |
| 383 | } |
| 384 | |
| 385 | func (mod *Module) SanitizeNever() bool { |
| 386 | return Bool(mod.sanitize.Properties.Sanitize.Never) |
| 387 | } |
| 388 | |
| 389 | var _ cc.PlatformSanitizeable = (*Module)(nil) |
| 390 | |
| 391 | func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool { |
| 392 | switch t := tag.(type) { |
| 393 | case dependencyTag: |
| 394 | return t.library |
| 395 | default: |
| 396 | return cc.IsSanitizableDependencyTag(tag) |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | func (m *Module) SanitizableDepTagChecker() cc.SantizableDependencyTagChecker { |
| 401 | return IsSanitizableDependencyTag |
| 402 | } |