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 | "-Z sanitizer=address", |
| 50 | |
| 51 | // Sancov breaks with lto |
| 52 | // TODO: Remove when https://bugs.llvm.org/show_bug.cgi?id=41734 is resolved and sancov works with LTO |
| 53 | "-C lto=no", |
| 54 | } |
| 55 | |
| 56 | var asanFlags = []string{ |
| 57 | "-Z sanitizer=address", |
| 58 | } |
| 59 | |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 60 | var hwasanFlags = []string{ |
| 61 | "-Z sanitizer=hwaddress", |
| 62 | "-C target-feature=+tagged-globals", |
| 63 | } |
| 64 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 65 | func boolPtr(v bool) *bool { |
| 66 | if v { |
| 67 | return &v |
| 68 | } else { |
| 69 | return nil |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func init() { |
| 74 | } |
| 75 | func (sanitize *sanitize) props() []interface{} { |
| 76 | return []interface{}{&sanitize.Properties} |
| 77 | } |
| 78 | |
| 79 | func (sanitize *sanitize) begin(ctx BaseModuleContext) { |
| 80 | s := sanitize.Properties.Sanitize |
| 81 | |
| 82 | // TODO:(b/178369775) |
| 83 | // For now sanitizing is only supported on devices |
| 84 | if ctx.Os() == android.Android && Bool(s.Fuzzer) { |
| 85 | sanitize.Properties.SanitizerEnabled = true |
| 86 | } |
| 87 | |
| 88 | if ctx.Os() == android.Android && Bool(s.Address) { |
| 89 | sanitize.Properties.SanitizerEnabled = true |
| 90 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 91 | |
| 92 | // HWASan requires AArch64 hardware feature (top-byte-ignore). |
| 93 | if ctx.Arch().ArchType != android.Arm64 { |
| 94 | s.Hwaddress = nil |
| 95 | } |
| 96 | |
| 97 | if ctx.Os() == android.Android && Bool(s.Hwaddress) { |
| 98 | sanitize.Properties.SanitizerEnabled = true |
| 99 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | type sanitize struct { |
| 103 | Properties SanitizeProperties |
| 104 | } |
| 105 | |
| 106 | func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) { |
| 107 | if !sanitize.Properties.SanitizerEnabled { |
| 108 | return flags, deps |
| 109 | } |
| 110 | if Bool(sanitize.Properties.Sanitize.Fuzzer) { |
| 111 | flags.RustFlags = append(flags.RustFlags, fuzzerFlags...) |
| 112 | } |
| 113 | if Bool(sanitize.Properties.Sanitize.Address) { |
| 114 | flags.RustFlags = append(flags.RustFlags, asanFlags...) |
| 115 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 116 | if Bool(sanitize.Properties.Sanitize.Hwaddress) { |
| 117 | flags.RustFlags = append(flags.RustFlags, hwasanFlags...) |
| 118 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 119 | return flags, deps |
| 120 | } |
| 121 | |
| 122 | func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 123 | return deps |
| 124 | } |
| 125 | |
| 126 | func rustSanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) { |
| 127 | if mod, ok := mctx.Module().(*Module); ok && mod.sanitize != nil { |
| 128 | if !mod.Enabled() { |
| 129 | return |
| 130 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 131 | |
| 132 | variations := mctx.Target().Variations() |
| 133 | var depTag blueprint.DependencyTag |
| 134 | var deps []string |
| 135 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 136 | if Bool(mod.sanitize.Properties.Sanitize.Fuzzer) || Bool(mod.sanitize.Properties.Sanitize.Address) { |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 137 | variations = append(variations, |
| 138 | blueprint.Variation{Mutator: "link", Variation: "shared"}) |
| 139 | depTag = cc.SharedDepTag() |
| 140 | deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan")} |
| 141 | } else if mod.IsSanitizerEnabled(cc.Hwasan) { |
| 142 | // TODO(b/180495975): HWASan for static Rust binaries isn't supported yet. |
| 143 | if binary, ok := mod.compiler.(*binaryDecorator); ok { |
| 144 | if Bool(binary.Properties.Static_executable) { |
| 145 | mctx.ModuleErrorf("HWASan is not supported for static Rust executables yet.") |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if mod.StaticallyLinked() { |
| 150 | variations = append(variations, |
| 151 | blueprint.Variation{Mutator: "link", Variation: "static"}) |
| 152 | depTag = cc.StaticDepTag(false) |
| 153 | deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "hwasan_static")} |
| 154 | } else { |
| 155 | variations = append(variations, |
| 156 | blueprint.Variation{Mutator: "link", Variation: "shared"}) |
| 157 | depTag = cc.SharedDepTag() |
| 158 | deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "hwasan")} |
| 159 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 160 | } |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 161 | |
| 162 | mctx.AddFarVariationDependencies(variations, depTag, deps...) |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | func (sanitize *sanitize) SetSanitizer(t cc.SanitizerType, b bool) { |
| 167 | sanitizerSet := false |
| 168 | switch t { |
| 169 | case cc.Fuzzer: |
| 170 | sanitize.Properties.Sanitize.Fuzzer = boolPtr(b) |
| 171 | sanitizerSet = true |
| 172 | case cc.Asan: |
| 173 | sanitize.Properties.Sanitize.Address = boolPtr(b) |
| 174 | sanitizerSet = true |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 175 | case cc.Hwasan: |
| 176 | sanitize.Properties.Sanitize.Hwaddress = boolPtr(b) |
| 177 | sanitizerSet = true |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 178 | default: |
| 179 | panic(fmt.Errorf("setting unsupported sanitizerType %d", t)) |
| 180 | } |
| 181 | if b && sanitizerSet { |
| 182 | sanitize.Properties.SanitizerEnabled = true |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // Check if the sanitizer is explicitly disabled (as opposed to nil by |
| 187 | // virtue of not being set). |
| 188 | func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t cc.SanitizerType) bool { |
| 189 | if sanitize == nil { |
| 190 | return false |
| 191 | } |
| 192 | if Bool(sanitize.Properties.Sanitize.Never) { |
| 193 | return true |
| 194 | } |
| 195 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 196 | return sanitizerVal != nil && *sanitizerVal == false |
| 197 | } |
| 198 | |
| 199 | // There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled) |
| 200 | // because enabling a sanitizer either directly (via the blueprint) or |
| 201 | // indirectly (via a mutator) sets the bool ptr to true, and you can't |
| 202 | // distinguish between the cases. It isn't needed though - both cases can be |
| 203 | // treated identically. |
| 204 | func (sanitize *sanitize) isSanitizerEnabled(t cc.SanitizerType) bool { |
| 205 | if sanitize == nil || !sanitize.Properties.SanitizerEnabled { |
| 206 | return false |
| 207 | } |
| 208 | |
| 209 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 210 | return sanitizerVal != nil && *sanitizerVal == true |
| 211 | } |
| 212 | |
| 213 | func (sanitize *sanitize) getSanitizerBoolPtr(t cc.SanitizerType) *bool { |
| 214 | switch t { |
| 215 | case cc.Fuzzer: |
| 216 | return sanitize.Properties.Sanitize.Fuzzer |
| 217 | case cc.Asan: |
| 218 | return sanitize.Properties.Sanitize.Address |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 219 | case cc.Hwasan: |
| 220 | return sanitize.Properties.Sanitize.Hwaddress |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 221 | default: |
| 222 | return nil |
| 223 | } |
| 224 | } |
| 225 | |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 226 | func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, entries *android.AndroidMkEntries) { |
| 227 | // Add a suffix for hwasan rlib libraries to allow surfacing both the sanitized and |
| 228 | // non-sanitized variants to make without a name conflict. |
| 229 | if entries.Class == "RLIB_LIBRARIES" || entries.Class == "STATIC_LIBRARIES" { |
| 230 | if sanitize.isSanitizerEnabled(cc.Hwasan) { |
| 231 | entries.SubName += ".hwasan" |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 236 | func (mod *Module) SanitizerSupported(t cc.SanitizerType) bool { |
| 237 | if mod.Host() { |
| 238 | return false |
| 239 | } |
| 240 | switch t { |
| 241 | case cc.Fuzzer: |
| 242 | return true |
| 243 | case cc.Asan: |
| 244 | return true |
Tri Vo | 0a74c3e | 2021-04-01 13:59:27 -0700 | [diff] [blame] | 245 | case cc.Hwasan: |
| 246 | return true |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 247 | default: |
| 248 | return false |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | func (mod *Module) IsSanitizerEnabled(t cc.SanitizerType) bool { |
| 253 | return mod.sanitize.isSanitizerEnabled(t) |
| 254 | } |
| 255 | |
| 256 | func (mod *Module) IsSanitizerExplicitlyDisabled(t cc.SanitizerType) bool { |
| 257 | if mod.Host() { |
| 258 | return true |
| 259 | } |
| 260 | |
| 261 | // TODO(b/178365482): Rust/CC interop doesn't work just yet; don't sanitize rust_ffi modules until |
| 262 | // linkage issues are resolved. |
| 263 | if lib, ok := mod.compiler.(libraryInterface); ok { |
| 264 | if lib.shared() || lib.static() { |
| 265 | return true |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return mod.sanitize.isSanitizerExplicitlyDisabled(t) |
| 270 | } |
| 271 | |
| 272 | func (mod *Module) SanitizeDep() bool { |
| 273 | return mod.sanitize.Properties.SanitizeDep |
| 274 | } |
| 275 | |
| 276 | func (mod *Module) SetSanitizer(t cc.SanitizerType, b bool) { |
| 277 | if !Bool(mod.sanitize.Properties.Sanitize.Never) { |
| 278 | mod.sanitize.SetSanitizer(t, b) |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | func (mod *Module) SetSanitizeDep(b bool) { |
| 283 | mod.sanitize.Properties.SanitizeDep = b |
| 284 | } |
| 285 | |
| 286 | func (mod *Module) StaticallyLinked() bool { |
| 287 | if lib, ok := mod.compiler.(libraryInterface); ok { |
| 288 | if lib.rlib() || lib.static() { |
| 289 | return true |
| 290 | } |
| 291 | } else if Bool(mod.compiler.(*binaryDecorator).Properties.Static_executable) { |
| 292 | return true |
| 293 | } |
| 294 | return false |
| 295 | } |
| 296 | |
| 297 | func (mod *Module) SetInSanitizerDir() { |
| 298 | mod.sanitize.Properties.InSanitizerDir = true |
| 299 | } |
| 300 | |
| 301 | func (mod *Module) SanitizeNever() bool { |
| 302 | return Bool(mod.sanitize.Properties.Sanitize.Never) |
| 303 | } |
| 304 | |
| 305 | var _ cc.PlatformSanitizeable = (*Module)(nil) |
| 306 | |
| 307 | func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool { |
| 308 | switch t := tag.(type) { |
| 309 | case dependencyTag: |
| 310 | return t.library |
| 311 | default: |
| 312 | return cc.IsSanitizableDependencyTag(tag) |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | func (m *Module) SanitizableDepTagChecker() cc.SantizableDependencyTagChecker { |
| 317 | return IsSanitizableDependencyTag |
| 318 | } |