blob: 5b7597e144e5b51e1b0f438d3892f66f240a047f [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
29type SanitizeProperties struct {
Tri Vo0a74c3e2021-04-01 13:59:27 -070030 // enable AddressSanitizer, HWAddressSanitizer, and others.
Ivan Lozano6cd99e62020-02-11 08:24:25 -050031 Sanitize struct {
Tri Vo0a74c3e2021-04-01 13:59:27 -070032 Address *bool `android:"arch_variant"`
33 Hwaddress *bool `android:"arch_variant"`
34 Fuzzer *bool `android:"arch_variant"`
35 Never *bool `android:"arch_variant"`
Ivan Lozano6cd99e62020-02-11 08:24:25 -050036 }
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
44var fuzzerFlags = []string{
45 "-C passes='sancov'",
46
47 "--cfg fuzzing",
Ivan Lozanoc044f5b2021-04-02 12:43:28 -040048 "-C llvm-args=-sanitizer-coverage-level=3",
Ivan Lozano6cd99e62020-02-11 08:24:25 -050049 "-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 Lozano6cd99e62020-02-11 08:24:25 -050053
Ivan Lozanoaa7c9802021-08-02 09:43:46 -040054 // See https://github.com/rust-fuzz/cargo-fuzz/pull/193
55 "-C link-dead-code",
56
Ivan Lozano6cd99e62020-02-11 08:24:25 -050057 // 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
62var asanFlags = []string{
63 "-Z sanitizer=address",
64}
65
Ivan Lozano5482d6a2021-11-01 10:13:25 -040066// See cc/sanitize.go's hwasanGlobalOptions for global hwasan options.
Tri Vo0a74c3e2021-04-01 13:59:27 -070067var hwasanFlags = []string{
68 "-Z sanitizer=hwaddress",
69 "-C target-feature=+tagged-globals",
Ivan Lozano5482d6a2021-11-01 10:13:25 -040070
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 Vo0a74c3e2021-04-01 13:59:27 -070078}
79
Ivan Lozano6cd99e62020-02-11 08:24:25 -050080func boolPtr(v bool) *bool {
81 if v {
82 return &v
83 } else {
84 return nil
85 }
86}
87
88func init() {
89}
90func (sanitize *sanitize) props() []interface{} {
91 return []interface{}{&sanitize.Properties}
92}
93
94func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Ivan Lozano5482d6a2021-11-01 10:13:25 -040095 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 Lozano6cd99e62020-02-11 08:24:25 -0500135
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 Vo0a74c3e2021-04-01 13:59:27 -0700145
146 // HWASan requires AArch64 hardware feature (top-byte-ignore).
147 if ctx.Arch().ArchType != android.Arm64 {
148 s.Hwaddress = nil
149 }
150
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400151 // 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 Vo0a74c3e2021-04-01 13:59:27 -0700162 sanitize.Properties.SanitizerEnabled = true
163 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500164}
165
166type sanitize struct {
167 Properties SanitizeProperties
168}
169
170func (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 Vo505b0e82021-04-08 15:50:48 -0700176 if ctx.Arch().ArchType == android.Arm64 {
177 flags.RustFlags = append(flags.RustFlags, hwasanFlags...)
178 } else {
179 flags.RustFlags = append(flags.RustFlags, asanFlags...)
180 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500181 }
182 if Bool(sanitize.Properties.Sanitize.Address) {
183 flags.RustFlags = append(flags.RustFlags, asanFlags...)
184 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700185 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
186 flags.RustFlags = append(flags.RustFlags, hwasanFlags...)
187 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500188 return flags, deps
189}
190
191func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
192 return deps
193}
194
195func rustSanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
196 if mod, ok := mctx.Module().(*Module); ok && mod.sanitize != nil {
197 if !mod.Enabled() {
198 return
199 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700200
201 variations := mctx.Target().Variations()
202 var depTag blueprint.DependencyTag
203 var deps []string
204
Tri Vo505b0e82021-04-08 15:50:48 -0700205 if mod.IsSanitizerEnabled(cc.Asan) ||
206 (mod.IsSanitizerEnabled(cc.Fuzzer) && mctx.Arch().ArchType != android.Arm64) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700207 variations = append(variations,
208 blueprint.Variation{Mutator: "link", Variation: "shared"})
209 depTag = cc.SharedDepTag()
210 deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan")}
Tri Vo505b0e82021-04-08 15:50:48 -0700211 } else if mod.IsSanitizerEnabled(cc.Hwasan) ||
212 (mod.IsSanitizerEnabled(cc.Fuzzer) && mctx.Arch().ArchType == android.Arm64) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700213 // TODO(b/180495975): HWASan for static Rust binaries isn't supported yet.
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400214 if binary, ok := mod.compiler.(binaryInterface); ok {
215 if binary.staticallyLinked() {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700216 mctx.ModuleErrorf("HWASan is not supported for static Rust executables yet.")
217 }
218 }
219
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400220 // 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 Lozano6cd99e62020-02-11 08:24:25 -0500226 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700227
228 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500229 }
230}
231
232func (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 Vo0a74c3e2021-04-01 13:59:27 -0700241 case cc.Hwasan:
242 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
243 sanitizerSet = true
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500244 default:
245 panic(fmt.Errorf("setting unsupported sanitizerType %d", t))
246 }
247 if b && sanitizerSet {
248 sanitize.Properties.SanitizerEnabled = true
249 }
250}
251
Ivan Lozanod7586b62021-04-01 09:49:36 -0400252func (m *Module) UbsanRuntimeNeeded() bool {
253 return false
254}
255
256func (m *Module) MinimalRuntimeNeeded() bool {
257 return false
258}
259
260func (m *Module) UbsanRuntimeDep() bool {
261 return false
262}
263
264func (m *Module) MinimalRuntimeDep() bool {
265 return false
266}
267
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500268// Check if the sanitizer is explicitly disabled (as opposed to nil by
269// virtue of not being set).
270func (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.
286func (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
295func (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 Vo0a74c3e2021-04-01 13:59:27 -0700301 case cc.Hwasan:
302 return sanitize.Properties.Sanitize.Hwaddress
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500303 default:
304 return nil
305 }
306}
307
Tri Vo0a74c3e2021-04-01 13:59:27 -0700308func (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 Lozano6cd99e62020-02-11 08:24:25 -0500318func (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 Vo0a74c3e2021-04-01 13:59:27 -0700327 case cc.Hwasan:
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400328 // TODO(b/180495975): HWASan for static Rust binaries isn't supported yet.
329 if mod.StaticExecutable() {
330 return false
331 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700332 return true
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500333 default:
334 return false
335 }
336}
337
338func (mod *Module) IsSanitizerEnabled(t cc.SanitizerType) bool {
339 return mod.sanitize.isSanitizerEnabled(t)
340}
341
342func (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
358func (mod *Module) SanitizeDep() bool {
359 return mod.sanitize.Properties.SanitizeDep
360}
361
362func (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
368func (mod *Module) SetSanitizeDep(b bool) {
369 mod.sanitize.Properties.SanitizeDep = b
370}
371
372func (mod *Module) StaticallyLinked() bool {
373 if lib, ok := mod.compiler.(libraryInterface); ok {
Tri Vo505b0e82021-04-08 15:50:48 -0700374 return lib.rlib() || lib.static()
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400375 } else if binary, ok := mod.compiler.(binaryInterface); ok {
376 return binary.staticallyLinked()
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500377 }
378 return false
379}
380
381func (mod *Module) SetInSanitizerDir() {
382 mod.sanitize.Properties.InSanitizerDir = true
383}
384
385func (mod *Module) SanitizeNever() bool {
386 return Bool(mod.sanitize.Properties.Sanitize.Never)
387}
388
389var _ cc.PlatformSanitizeable = (*Module)(nil)
390
391func 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
400func (m *Module) SanitizableDepTagChecker() cc.SantizableDependencyTagChecker {
401 return IsSanitizableDependencyTag
402}