blob: e54ece66a43e2e6e5dcbae9f2d8bb978852f0768 [file] [log] [blame]
Colin Cross16b23492016-01-06 14:41:07 -08001// Copyright 2016 Google Inc. All rights reserved.
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 cc
16
17import (
18 "fmt"
Colin Cross8ff9ef42017-05-08 13:44:11 -070019 "io"
Colin Cross16b23492016-01-06 14:41:07 -080020 "strings"
21
22 "github.com/google/blueprint"
23
Colin Cross635c3b02016-05-18 15:37:25 -070024 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070025 "android/soong/cc/config"
Colin Cross16b23492016-01-06 14:41:07 -080026)
27
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070028var (
29 // Any C flags added by sanitizer which libTooling tools may not
30 // understand also need to be added to ClangLibToolingUnknownCflags in
31 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080032
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070033 asanCflags = []string{"-fno-omit-frame-pointer"}
34 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
35 asanLibs = []string{"libasan"}
36
37 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fvisibility=default",
38 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Vishwath Mohanf3918d32017-02-14 07:59:33 -080039 // FIXME: revert the __cfi_check flag when clang is updated to r280031.
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070040 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
41 "-Wl,-plugin-opt,O1 -Wl,-export-dynamic-symbol=__cfi_check"}
42 cfiArflags = []string{"--plugin ${config.ClangBin}/../lib64/LLVMgold.so"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070043)
44
Colin Cross16b23492016-01-06 14:41:07 -080045type sanitizerType int
46
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070047func boolPtr(v bool) *bool {
48 if v {
49 return &v
50 } else {
51 return nil
52 }
53}
54
Colin Cross16b23492016-01-06 14:41:07 -080055const (
56 asan sanitizerType = iota + 1
57 tsan
58)
59
60func (t sanitizerType) String() string {
61 switch t {
62 case asan:
63 return "asan"
64 case tsan:
65 return "tsan"
66 default:
67 panic(fmt.Errorf("unknown sanitizerType %d", t))
68 }
69}
70
71type SanitizeProperties struct {
72 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
73 Sanitize struct {
74 Never bool `android:"arch_variant"`
75
76 // main sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070077 Address *bool `android:"arch_variant"`
78 Thread *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080079
80 // local sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070081 Undefined *bool `android:"arch_variant"`
82 All_undefined *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080083 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070084 Coverage *bool `android:"arch_variant"`
85 Safestack *bool `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070086 Cfi *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080087
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070088 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
89 // Replaces abort() on error with a human-readable error message.
90 // Address and Thread sanitizers always run in diagnostic mode.
91 Diag struct {
Ivan Lozano651275b2017-06-13 10:24:34 -070092 Undefined *bool `android:"arch_variant"`
93 Cfi *bool `android:"arch_variant"`
94 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070095 }
96
97 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -080098 Recover []string
99
100 // value to pass to -fsanitize-blacklist
101 Blacklist *string
102 } `android:"arch_variant"`
103
104 SanitizerEnabled bool `blueprint:"mutated"`
105 SanitizeDep bool `blueprint:"mutated"`
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700106 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800107}
108
109type sanitize struct {
110 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700111
112 runtimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800113}
114
115func (sanitize *sanitize) props() []interface{} {
116 return []interface{}{&sanitize.Properties}
117}
118
119func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700120 s := &sanitize.Properties.Sanitize
121
Colin Cross16b23492016-01-06 14:41:07 -0800122 // Don't apply sanitizers to NDK code.
123 if ctx.sdk() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700124 s.Never = true
Colin Cross16b23492016-01-06 14:41:07 -0800125 }
126
127 // Never always wins.
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700128 if s.Never {
Colin Cross16b23492016-01-06 14:41:07 -0800129 return
130 }
131
Colin Cross16b23492016-01-06 14:41:07 -0800132 var globalSanitizers []string
133 if ctx.clang() {
134 if ctx.Host() {
135 globalSanitizers = ctx.AConfig().SanitizeHost()
136 } else {
Colin Cross23ae82a2016-11-02 14:34:39 -0700137 arches := ctx.AConfig().SanitizeDeviceArch()
138 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
139 globalSanitizers = ctx.AConfig().SanitizeDevice()
140 }
Colin Cross16b23492016-01-06 14:41:07 -0800141 }
142 }
143
Colin Cross16b23492016-01-06 14:41:07 -0800144 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000145 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700146 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
147 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000148 }
Colin Cross16b23492016-01-06 14:41:07 -0800149
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700150 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
151 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000152 }
153
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800154 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
155 if s.Address == nil {
156 s.Address = boolPtr(true)
157 } else if *s.Address == false {
158 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
159 // disables address, then disable coverage as well.
160 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
161 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000162 }
163
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700164 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
165 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000166 }
167
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700168 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
169 s.Coverage = boolPtr(true)
170 }
171
172 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
173 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000174 }
175
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700176 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
177 s.Cfi = boolPtr(true)
178 }
179
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000180 if len(globalSanitizers) > 0 {
181 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
182 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700183 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700184
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800185 // CFI needs gold linker, and mips toolchain does not have one.
186 if !ctx.AConfig().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800187 s.Cfi = nil
188 s.Diag.Cfi = nil
189 }
190
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800191 // Also disable CFI for arm32 until b/35157333 is fixed.
192 if ctx.Arch().ArchType == android.Arm {
193 s.Cfi = nil
194 s.Diag.Cfi = nil
195 }
196
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700197 // Also disable CFI if ASAN is enabled.
198 if Bool(s.Address) {
199 s.Cfi = nil
200 s.Diag.Cfi = nil
201 }
202
Colin Cross3c344ef2016-07-18 15:44:56 -0700203 if ctx.staticBinary() {
204 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700205 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700206 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800207 }
208
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700209 if Bool(s.All_undefined) {
210 s.Undefined = nil
211 }
212
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700213 if !ctx.toolchain().Is64Bit() {
214 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700215 s.Thread = nil
216 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800217 // TODO(ccross): error for compile_multilib = "32"?
218 }
219
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800220 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
221 Bool(s.Coverage) || Bool(s.Safestack) || Bool(s.Cfi) || len(s.Misc_undefined) > 0) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700222 sanitize.Properties.SanitizerEnabled = true
223 }
224
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700225 if Bool(s.Coverage) {
226 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800227 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
228 }
229 }
230}
231
232func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
233 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
234 return deps
235 }
236
237 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700238 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700239 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800240 }
Colin Cross263abbd2016-07-15 13:10:48 -0700241 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
242 deps.SharedLibs = append(deps.SharedLibs, "libdl")
243 }
Colin Cross16b23492016-01-06 14:41:07 -0800244 }
245
246 return deps
247}
248
249func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
250 if !sanitize.Properties.SanitizerEnabled {
251 return flags
252 }
253
254 if !ctx.clang() {
255 ctx.ModuleErrorf("Use of sanitizers requires clang")
256 }
257
258 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700259 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800260
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700261 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800262 sanitizers = append(sanitizers, "undefined")
263 if ctx.Device() {
264 ctx.ModuleErrorf("ubsan is not yet supported on the device")
265 }
266 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700267 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800268 sanitizers = append(sanitizers,
269 "bool",
270 "integer-divide-by-zero",
271 "return",
272 "returns-nonnull-attribute",
273 "shift-exponent",
274 "unreachable",
275 "vla-bound",
276 // TODO(danalbert): The following checks currently have compiler performance issues.
277 //"alignment",
278 //"bounds",
279 //"enum",
280 //"float-cast-overflow",
281 //"float-divide-by-zero",
282 //"nonnull-attribute",
283 //"null",
284 //"shift-base",
285 //"signed-integer-overflow",
286 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
287 // https://llvm.org/PR19302
288 // http://reviews.llvm.org/D6974
289 // "object-size",
290 )
291 }
292 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
293 }
294
Ivan Lozano651275b2017-06-13 10:24:34 -0700295 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700296 diagSanitizers = append(diagSanitizers, "undefined")
297 }
298
Ivan Lozano651275b2017-06-13 10:24:34 -0700299 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
300
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700301 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700302 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800303 // Frame pointer based unwinder in ASan requires ARM frame setup.
304 // TODO: put in flags?
305 flags.RequiredInstructionSet = "arm"
306 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700307 flags.CFlags = append(flags.CFlags, asanCflags...)
308 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800309
Colin Cross16b23492016-01-06 14:41:07 -0800310 if ctx.Host() {
311 // -nodefaultlibs (provided with libc++) prevents the driver from linking
312 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
313 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
314 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
315 } else {
316 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
317 flags.DynamicLinker = "/system/bin/linker_asan"
318 if flags.Toolchain.Is64Bit() {
319 flags.DynamicLinker += "64"
320 }
321 }
322 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700323 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800324 }
325
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700326 if Bool(sanitize.Properties.Sanitize.Coverage) {
Dan Austin8241abb2017-06-28 15:29:09 -0700327 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard")
Colin Cross16b23492016-01-06 14:41:07 -0800328 }
329
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700330 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700331 sanitizers = append(sanitizers, "safe-stack")
332 }
333
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700334 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800335 if ctx.Arch().ArchType == android.Arm {
336 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
337 // to do this on a function basis, so force Thumb on the entire module.
338 flags.RequiredInstructionSet = "thumb"
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800339 // Workaround for b/33678192. CFI jumptables need Thumb2 codegen. Revert when
340 // Clang is updated past r290384.
341 flags.LdFlags = append(flags.LdFlags, "-march=armv7-a")
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800342 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700343 sanitizers = append(sanitizers, "cfi")
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700344 flags.CFlags = append(flags.CFlags, cfiCflags...)
345 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
346 flags.ArFlags = append(flags.ArFlags, cfiArflags...)
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700347 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
348 diagSanitizers = append(diagSanitizers, "cfi")
349 }
350 }
351
Colin Cross16b23492016-01-06 14:41:07 -0800352 if len(sanitizers) > 0 {
353 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
354 flags.CFlags = append(flags.CFlags, sanitizeArg)
355 if ctx.Host() {
356 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
357 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanovc6482d62017-06-07 15:46:40 -0700358 if ctx.Os() == android.Linux {
359 flags.LdFlags = append(flags.LdFlags, "-lrt")
360 }
361 flags.LdFlags = append(flags.LdFlags, "-ldl")
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800362 // Host sanitizers only link symbols in the final executable, so
363 // there will always be undefined symbols in intermediate libraries.
364 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800365 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700366 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Colin Cross16b23492016-01-06 14:41:07 -0800367 }
368 }
369
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700370 if len(diagSanitizers) > 0 {
371 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
372 }
373 // FIXME: enable RTTI if diag + (cfi or vptr)
374
Andreas Gampe97071162017-05-08 13:15:23 -0700375 if sanitize.Properties.Sanitize.Recover != nil {
376 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
377 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
378 }
379
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700380 // Link a runtime library if needed.
381 runtimeLibrary := ""
382 if Bool(sanitize.Properties.Sanitize.Address) {
383 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
384 } else if len(diagSanitizers) > 0 {
385 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
386 }
387
388 // ASan runtime library must be the first in the link order.
389 if runtimeLibrary != "" {
Colin Cross8ff9ef42017-05-08 13:44:11 -0700390 flags.libFlags = append([]string{
391 "${config.ClangAsanLibDir}/" + runtimeLibrary + ctx.toolchain().ShlibSuffix(),
392 }, flags.libFlags...)
393 sanitize.runtimeLibrary = runtimeLibrary
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700394 }
395
Colin Cross635c3b02016-05-18 15:37:25 -0700396 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800397 if blacklist.Valid() {
398 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
399 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
400 }
401
402 return flags
403}
404
Colin Cross8ff9ef42017-05-08 13:44:11 -0700405func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
406 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
407 if sanitize.runtimeLibrary != "" {
408 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.runtimeLibrary)
409 }
410
411 return nil
412 })
413}
414
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700415func (sanitize *sanitize) inSanitizerDir() bool {
416 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700417}
418
Colin Cross16b23492016-01-06 14:41:07 -0800419func (sanitize *sanitize) Sanitizer(t sanitizerType) bool {
420 if sanitize == nil {
421 return false
422 }
423
424 switch t {
425 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700426 return Bool(sanitize.Properties.Sanitize.Address)
Colin Cross16b23492016-01-06 14:41:07 -0800427 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700428 return Bool(sanitize.Properties.Sanitize.Thread)
Colin Cross16b23492016-01-06 14:41:07 -0800429 default:
430 panic(fmt.Errorf("unknown sanitizerType %d", t))
431 }
432}
433
434func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
435 switch t {
436 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700437 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700438 if !b {
439 sanitize.Properties.Sanitize.Coverage = nil
440 }
Colin Cross16b23492016-01-06 14:41:07 -0800441 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700442 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800443 default:
444 panic(fmt.Errorf("unknown sanitizerType %d", t))
445 }
446 if b {
447 sanitize.Properties.SanitizerEnabled = true
448 }
449}
450
451// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700452func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
453 return func(mctx android.TopDownMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800454 if c, ok := mctx.Module().(*Module); ok && c.sanitize.Sanitizer(t) {
455 mctx.VisitDepsDepthFirst(func(module blueprint.Module) {
456 if d, ok := mctx.Module().(*Module); ok && c.sanitize != nil &&
457 !c.sanitize.Properties.Sanitize.Never {
458 d.sanitize.Properties.SanitizeDep = true
459 }
460 })
461 }
462 }
463}
464
465// Create asan variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700466func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
467 return func(mctx android.BottomUpMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800468 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Colin Crossb916a382016-07-29 17:28:03 -0700469 if c.isDependencyRoot() && c.sanitize.Sanitizer(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700470 modules := mctx.CreateVariations(t.String())
471 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Colin Cross16b23492016-01-06 14:41:07 -0800472 } else if c.sanitize.Properties.SanitizeDep {
Colin Crossb0f28952016-09-19 16:46:53 -0700473 modules := mctx.CreateVariations("", t.String())
474 modules[0].(*Module).sanitize.SetSanitizer(t, false)
475 modules[1].(*Module).sanitize.SetSanitizer(t, true)
476 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
477 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
478 if mctx.Device() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700479 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
Colin Crossb0f28952016-09-19 16:46:53 -0700480 } else {
481 modules[0].(*Module).Properties.PreventInstall = true
482 }
483 if mctx.AConfig().EmbeddedInMake() {
484 modules[0].(*Module).Properties.HideFromMake = true
Colin Cross30d5f512016-05-03 18:02:42 -0700485 }
Colin Cross16b23492016-01-06 14:41:07 -0800486 }
487 c.sanitize.Properties.SanitizeDep = false
488 }
489 }
490}