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