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