blob: ae3eff0062d7f30dc4e2c882cf603bee54f69002 [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 "-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
56var asanFlags = []string{
57 "-Z sanitizer=address",
58}
59
Tri Vo0a74c3e2021-04-01 13:59:27 -070060var hwasanFlags = []string{
61 "-Z sanitizer=hwaddress",
62 "-C target-feature=+tagged-globals",
63}
64
Ivan Lozano6cd99e62020-02-11 08:24:25 -050065func boolPtr(v bool) *bool {
66 if v {
67 return &v
68 } else {
69 return nil
70 }
71}
72
73func init() {
74}
75func (sanitize *sanitize) props() []interface{} {
76 return []interface{}{&sanitize.Properties}
77}
78
79func (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 Vo0a74c3e2021-04-01 13:59:27 -070091
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 Lozano6cd99e62020-02-11 08:24:25 -0500100}
101
102type sanitize struct {
103 Properties SanitizeProperties
104}
105
106func (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 Vo0a74c3e2021-04-01 13:59:27 -0700116 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
117 flags.RustFlags = append(flags.RustFlags, hwasanFlags...)
118 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500119 return flags, deps
120}
121
122func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
123 return deps
124}
125
126func rustSanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
127 if mod, ok := mctx.Module().(*Module); ok && mod.sanitize != nil {
128 if !mod.Enabled() {
129 return
130 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700131
132 variations := mctx.Target().Variations()
133 var depTag blueprint.DependencyTag
134 var deps []string
135
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500136 if Bool(mod.sanitize.Properties.Sanitize.Fuzzer) || Bool(mod.sanitize.Properties.Sanitize.Address) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700137 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 Lozano6cd99e62020-02-11 08:24:25 -0500160 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700161
162 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500163 }
164}
165
166func (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 Vo0a74c3e2021-04-01 13:59:27 -0700175 case cc.Hwasan:
176 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
177 sanitizerSet = true
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500178 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).
188func (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.
204func (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
213func (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 Vo0a74c3e2021-04-01 13:59:27 -0700219 case cc.Hwasan:
220 return sanitize.Properties.Sanitize.Hwaddress
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500221 default:
222 return nil
223 }
224}
225
Tri Vo0a74c3e2021-04-01 13:59:27 -0700226func (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 Lozano6cd99e62020-02-11 08:24:25 -0500236func (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 Vo0a74c3e2021-04-01 13:59:27 -0700245 case cc.Hwasan:
246 return true
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500247 default:
248 return false
249 }
250}
251
252func (mod *Module) IsSanitizerEnabled(t cc.SanitizerType) bool {
253 return mod.sanitize.isSanitizerEnabled(t)
254}
255
256func (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
272func (mod *Module) SanitizeDep() bool {
273 return mod.sanitize.Properties.SanitizeDep
274}
275
276func (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
282func (mod *Module) SetSanitizeDep(b bool) {
283 mod.sanitize.Properties.SanitizeDep = b
284}
285
286func (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
297func (mod *Module) SetInSanitizerDir() {
298 mod.sanitize.Properties.InSanitizerDir = true
299}
300
301func (mod *Module) SanitizeNever() bool {
302 return Bool(mod.sanitize.Properties.Sanitize.Never)
303}
304
305var _ cc.PlatformSanitizeable = (*Module)(nil)
306
307func 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
316func (m *Module) SanitizableDepTagChecker() cc.SantizableDependencyTagChecker {
317 return IsSanitizableDependencyTag
318}