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 ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/cc" |
| 20 | "android/soong/rust/config" |
| 21 | "fmt" |
| 22 | "github.com/google/blueprint" |
| 23 | ) |
| 24 | |
| 25 | type SanitizeProperties struct { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 26 | // enable AddressSanitizer, HWAddressSanitizer, and others. |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 27 | Sanitize struct { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 28 | Address *bool `android:"arch_variant"` |
| 29 | Hwaddress *bool `android:"arch_variant"` |
| 30 | Fuzzer *bool `android:"arch_variant"` |
| 31 | Never *bool `android:"arch_variant"` |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 32 | } |
| 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 | |
| 40 | var fuzzerFlags = []string{ |
| 41 | "-C passes='sancov'", |
| 42 | |
| 43 | "--cfg fuzzing", |
Ivan Lozano | c044f5b | 2021-04-02 12:43:28 -0400 | [diff] [blame] | 44 | "-C llvm-args=-sanitizer-coverage-level=3", |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 45 | "-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 Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 49 | |
Ivan Lozano | aa7c980 | 2021-08-02 09:43:46 -0400 | [diff] [blame] | 50 | // See https://github.com/rust-fuzz/cargo-fuzz/pull/193 |
| 51 | "-C link-dead-code", |
| 52 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 53 | // 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 | |
| 58 | var asanFlags = []string{ |
| 59 | "-Z sanitizer=address", |
| 60 | } |
| 61 | |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 62 | var hwasanFlags = []string{ |
| 63 | "-Z sanitizer=hwaddress", |
| 64 | "-C target-feature=+tagged-globals", |
| 65 | } |
| 66 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 67 | func boolPtr(v bool) *bool { |
| 68 | if v { |
| 69 | return &v |
| 70 | } else { |
| 71 | return nil |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func init() { |
| 76 | } |
| 77 | func (sanitize *sanitize) props() []interface{} { |
| 78 | return []interface{}{&sanitize.Properties} |
| 79 | } |
| 80 | |
| 81 | func (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 Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 93 | |
| 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 Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | type sanitize struct { |
| 105 | Properties SanitizeProperties |
| 106 | } |
| 107 | |
| 108 | func (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 Vo | 505b0e8 | 2021-04-08 15:50:48 -0700 | [diff] [blame] | 114 | if ctx.Arch().ArchType == android.Arm64 { |
| 115 | flags.RustFlags = append(flags.RustFlags, hwasanFlags...) |
| 116 | } else { |
| 117 | flags.RustFlags = append(flags.RustFlags, asanFlags...) |
| 118 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 119 | } |
| 120 | if Bool(sanitize.Properties.Sanitize.Address) { |
| 121 | flags.RustFlags = append(flags.RustFlags, asanFlags...) |
| 122 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 123 | if Bool(sanitize.Properties.Sanitize.Hwaddress) { |
| 124 | flags.RustFlags = append(flags.RustFlags, hwasanFlags...) |
| 125 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 126 | return flags, deps |
| 127 | } |
| 128 | |
| 129 | func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 130 | return deps |
| 131 | } |
| 132 | |
| 133 | func rustSanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) { |
| 134 | if mod, ok := mctx.Module().(*Module); ok && mod.sanitize != nil { |
| 135 | if !mod.Enabled() { |
| 136 | return |
| 137 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 138 | |
| 139 | variations := mctx.Target().Variations() |
| 140 | var depTag blueprint.DependencyTag |
| 141 | var deps []string |
| 142 | |
Tri Vo | 505b0e8 | 2021-04-08 15:50:48 -0700 | [diff] [blame] | 143 | if mod.IsSanitizerEnabled(cc.Asan) || |
| 144 | (mod.IsSanitizerEnabled(cc.Fuzzer) && mctx.Arch().ArchType != android.Arm64) { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 145 | variations = append(variations, |
| 146 | blueprint.Variation{Mutator: "link", Variation: "shared"}) |
| 147 | depTag = cc.SharedDepTag() |
| 148 | deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan")} |
Tri Vo | 505b0e8 | 2021-04-08 15:50:48 -0700 | [diff] [blame] | 149 | } else if mod.IsSanitizerEnabled(cc.Hwasan) || |
| 150 | (mod.IsSanitizerEnabled(cc.Fuzzer) && mctx.Arch().ArchType == android.Arm64) { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 151 | // 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 Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 169 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 170 | |
| 171 | mctx.AddFarVariationDependencies(variations, depTag, deps...) |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | func (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 Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 184 | case cc.Hwasan: |
| 185 | sanitize.Properties.Sanitize.Hwaddress = boolPtr(b) |
| 186 | sanitizerSet = true |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 187 | default: |
| 188 | panic(fmt.Errorf("setting unsupported sanitizerType %d", t)) |
| 189 | } |
| 190 | if b && sanitizerSet { |
| 191 | sanitize.Properties.SanitizerEnabled = true |
| 192 | } |
| 193 | } |
| 194 | |
Ivan Lozano | d7586b6 | 2021-04-01 09:49:36 -0400 | [diff] [blame] | 195 | func (m *Module) UbsanRuntimeNeeded() bool { |
| 196 | return false |
| 197 | } |
| 198 | |
| 199 | func (m *Module) MinimalRuntimeNeeded() bool { |
| 200 | return false |
| 201 | } |
| 202 | |
| 203 | func (m *Module) UbsanRuntimeDep() bool { |
| 204 | return false |
| 205 | } |
| 206 | |
| 207 | func (m *Module) MinimalRuntimeDep() bool { |
| 208 | return false |
| 209 | } |
| 210 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 211 | // Check if the sanitizer is explicitly disabled (as opposed to nil by |
| 212 | // virtue of not being set). |
| 213 | func (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. |
| 229 | func (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 | |
| 238 | func (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 Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 244 | case cc.Hwasan: |
| 245 | return sanitize.Properties.Sanitize.Hwaddress |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 246 | default: |
| 247 | return nil |
| 248 | } |
| 249 | } |
| 250 | |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 251 | func (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 Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 261 | func (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 Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 270 | case cc.Hwasan: |
| 271 | return true |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 272 | default: |
| 273 | return false |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | func (mod *Module) IsSanitizerEnabled(t cc.SanitizerType) bool { |
| 278 | return mod.sanitize.isSanitizerEnabled(t) |
| 279 | } |
| 280 | |
| 281 | func (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 | |
| 297 | func (mod *Module) SanitizeDep() bool { |
| 298 | return mod.sanitize.Properties.SanitizeDep |
| 299 | } |
| 300 | |
| 301 | func (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 | |
| 307 | func (mod *Module) SetSanitizeDep(b bool) { |
| 308 | mod.sanitize.Properties.SanitizeDep = b |
| 309 | } |
| 310 | |
| 311 | func (mod *Module) StaticallyLinked() bool { |
| 312 | if lib, ok := mod.compiler.(libraryInterface); ok { |
Tri Vo | 505b0e8 | 2021-04-08 15:50:48 -0700 | [diff] [blame] | 313 | return lib.rlib() || lib.static() |
Ivan Lozano | 21fa0a5 | 2021-11-01 09:19:45 -0400 | [diff] [blame^] | 314 | } else if binary, ok := mod.compiler.(binaryInterface); ok { |
| 315 | return binary.staticallyLinked() |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 316 | } |
| 317 | return false |
| 318 | } |
| 319 | |
| 320 | func (mod *Module) SetInSanitizerDir() { |
| 321 | mod.sanitize.Properties.InSanitizerDir = true |
| 322 | } |
| 323 | |
| 324 | func (mod *Module) SanitizeNever() bool { |
| 325 | return Bool(mod.sanitize.Properties.Sanitize.Never) |
| 326 | } |
| 327 | |
| 328 | var _ cc.PlatformSanitizeable = (*Module)(nil) |
| 329 | |
| 330 | func 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 | |
| 339 | func (m *Module) SanitizableDepTagChecker() cc.SantizableDependencyTagChecker { |
| 340 | return IsSanitizableDependencyTag |
| 341 | } |