blob: 2f44b204b6e4a057f4d54ba141d970e4dc161271 [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 {
26 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
27 Sanitize struct {
28 Address *bool `android:"arch_variant"`
29 Fuzzer *bool `android:"arch_variant"`
30 Never *bool `android:"arch_variant"`
31 }
32 SanitizerEnabled bool `blueprint:"mutated"`
33 SanitizeDep bool `blueprint:"mutated"`
34
35 // Used when we need to place libraries in their own directory, such as ASAN.
36 InSanitizerDir bool `blueprint:"mutated"`
37}
38
39var fuzzerFlags = []string{
40 "-C passes='sancov'",
41
42 "--cfg fuzzing",
43 "-C llvm-args=-sanitizer-coverage-level=4",
44 "-C llvm-args=-sanitizer-coverage-trace-compares",
45 "-C llvm-args=-sanitizer-coverage-inline-8bit-counters",
46 "-C llvm-args=-sanitizer-coverage-trace-geps",
47 "-C llvm-args=-sanitizer-coverage-prune-blocks=0",
48 "-C llvm-args=-sanitizer-coverage-pc-table",
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
60func boolPtr(v bool) *bool {
61 if v {
62 return &v
63 } else {
64 return nil
65 }
66}
67
68func init() {
69}
70func (sanitize *sanitize) props() []interface{} {
71 return []interface{}{&sanitize.Properties}
72}
73
74func (sanitize *sanitize) begin(ctx BaseModuleContext) {
75 s := sanitize.Properties.Sanitize
76
77 // TODO:(b/178369775)
78 // For now sanitizing is only supported on devices
79 if ctx.Os() == android.Android && Bool(s.Fuzzer) {
80 sanitize.Properties.SanitizerEnabled = true
81 }
82
83 if ctx.Os() == android.Android && Bool(s.Address) {
84 sanitize.Properties.SanitizerEnabled = true
85 }
86}
87
88type sanitize struct {
89 Properties SanitizeProperties
90}
91
92func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
93 if !sanitize.Properties.SanitizerEnabled {
94 return flags, deps
95 }
96 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
97 flags.RustFlags = append(flags.RustFlags, fuzzerFlags...)
98 }
99 if Bool(sanitize.Properties.Sanitize.Address) {
100 flags.RustFlags = append(flags.RustFlags, asanFlags...)
101 }
102 return flags, deps
103}
104
105func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
106 return deps
107}
108
109func rustSanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
110 if mod, ok := mctx.Module().(*Module); ok && mod.sanitize != nil {
111 if !mod.Enabled() {
112 return
113 }
114 if Bool(mod.sanitize.Properties.Sanitize.Fuzzer) || Bool(mod.sanitize.Properties.Sanitize.Address) {
115 mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{
116 {Mutator: "link", Variation: "shared"},
117 }...), cc.SharedDepTag(), config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan"))
118 }
119 }
120}
121
122func (sanitize *sanitize) SetSanitizer(t cc.SanitizerType, b bool) {
123 sanitizerSet := false
124 switch t {
125 case cc.Fuzzer:
126 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
127 sanitizerSet = true
128 case cc.Asan:
129 sanitize.Properties.Sanitize.Address = boolPtr(b)
130 sanitizerSet = true
131 default:
132 panic(fmt.Errorf("setting unsupported sanitizerType %d", t))
133 }
134 if b && sanitizerSet {
135 sanitize.Properties.SanitizerEnabled = true
136 }
137}
138
139// Check if the sanitizer is explicitly disabled (as opposed to nil by
140// virtue of not being set).
141func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t cc.SanitizerType) bool {
142 if sanitize == nil {
143 return false
144 }
145 if Bool(sanitize.Properties.Sanitize.Never) {
146 return true
147 }
148 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
149 return sanitizerVal != nil && *sanitizerVal == false
150}
151
152// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
153// because enabling a sanitizer either directly (via the blueprint) or
154// indirectly (via a mutator) sets the bool ptr to true, and you can't
155// distinguish between the cases. It isn't needed though - both cases can be
156// treated identically.
157func (sanitize *sanitize) isSanitizerEnabled(t cc.SanitizerType) bool {
158 if sanitize == nil || !sanitize.Properties.SanitizerEnabled {
159 return false
160 }
161
162 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
163 return sanitizerVal != nil && *sanitizerVal == true
164}
165
166func (sanitize *sanitize) getSanitizerBoolPtr(t cc.SanitizerType) *bool {
167 switch t {
168 case cc.Fuzzer:
169 return sanitize.Properties.Sanitize.Fuzzer
170 case cc.Asan:
171 return sanitize.Properties.Sanitize.Address
172 default:
173 return nil
174 }
175}
176
177func (mod *Module) SanitizerSupported(t cc.SanitizerType) bool {
178 if mod.Host() {
179 return false
180 }
181 switch t {
182 case cc.Fuzzer:
183 return true
184 case cc.Asan:
185 return true
186 default:
187 return false
188 }
189}
190
191func (mod *Module) IsSanitizerEnabled(t cc.SanitizerType) bool {
192 return mod.sanitize.isSanitizerEnabled(t)
193}
194
195func (mod *Module) IsSanitizerExplicitlyDisabled(t cc.SanitizerType) bool {
196 if mod.Host() {
197 return true
198 }
199
200 // TODO(b/178365482): Rust/CC interop doesn't work just yet; don't sanitize rust_ffi modules until
201 // linkage issues are resolved.
202 if lib, ok := mod.compiler.(libraryInterface); ok {
203 if lib.shared() || lib.static() {
204 return true
205 }
206 }
207
208 return mod.sanitize.isSanitizerExplicitlyDisabled(t)
209}
210
211func (mod *Module) SanitizeDep() bool {
212 return mod.sanitize.Properties.SanitizeDep
213}
214
215func (mod *Module) SetSanitizer(t cc.SanitizerType, b bool) {
216 if !Bool(mod.sanitize.Properties.Sanitize.Never) {
217 mod.sanitize.SetSanitizer(t, b)
218 }
219}
220
221func (mod *Module) SetSanitizeDep(b bool) {
222 mod.sanitize.Properties.SanitizeDep = b
223}
224
225func (mod *Module) StaticallyLinked() bool {
226 if lib, ok := mod.compiler.(libraryInterface); ok {
227 if lib.rlib() || lib.static() {
228 return true
229 }
230 } else if Bool(mod.compiler.(*binaryDecorator).Properties.Static_executable) {
231 return true
232 }
233 return false
234}
235
236func (mod *Module) SetInSanitizerDir() {
237 mod.sanitize.Properties.InSanitizerDir = true
238}
239
240func (mod *Module) SanitizeNever() bool {
241 return Bool(mod.sanitize.Properties.Sanitize.Never)
242}
243
244var _ cc.PlatformSanitizeable = (*Module)(nil)
245
246func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
247 switch t := tag.(type) {
248 case dependencyTag:
249 return t.library
250 default:
251 return cc.IsSanitizableDependencyTag(tag)
252 }
253}
254
255func (m *Module) SanitizableDepTagChecker() cc.SantizableDependencyTagChecker {
256 return IsSanitizableDependencyTag
257}