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