blob: dfd86f08b6639bcb8cad7d0cbb9af300013d0137 [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
Dan Willemsencbceaab2016-10-13 16:44:07 -070028const (
Dan Willemsen78ffeea2016-10-20 18:46:48 -070029 asanCflags = "-fno-omit-frame-pointer"
Dan Willemsencbceaab2016-10-13 16:44:07 -070030 asanLdflags = "-Wl,-u,__asan_preinit"
Dan Willemsen78ffeea2016-10-20 18:46:48 -070031 asanLibs = "libasan"
Vishwath Mohanf3918d32017-02-14 07:59:33 -080032
33 cfiCflags = "-flto -fsanitize-cfi-cross-dso -fvisibility=default " +
34 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"
35 // FIXME: revert the __cfi_check flag when clang is updated to r280031.
36 cfiLdflags = "-flto -fsanitize-cfi-cross-dso -fsanitize=cfi " +
37 "-Wl,-plugin-opt,O1 -Wl,-export-dynamic-symbol=__cfi_check"
Vishwath Mohan7a5b46d2017-03-16 16:36:16 -070038 cfiArflags = "--plugin ${config.ClangBin}/../lib64/LLVMgold.so"
Dan Willemsencbceaab2016-10-13 16:44:07 -070039)
40
Colin Cross16b23492016-01-06 14:41:07 -080041type sanitizerType int
42
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070043func boolPtr(v bool) *bool {
44 if v {
45 return &v
46 } else {
47 return nil
48 }
49}
50
Colin Cross16b23492016-01-06 14:41:07 -080051const (
52 asan sanitizerType = iota + 1
53 tsan
54)
55
56func (t sanitizerType) String() string {
57 switch t {
58 case asan:
59 return "asan"
60 case tsan:
61 return "tsan"
62 default:
63 panic(fmt.Errorf("unknown sanitizerType %d", t))
64 }
65}
66
67type SanitizeProperties struct {
68 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
69 Sanitize struct {
70 Never bool `android:"arch_variant"`
71
72 // main sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070073 Address *bool `android:"arch_variant"`
74 Thread *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080075
76 // local sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070077 Undefined *bool `android:"arch_variant"`
78 All_undefined *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080079 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070080 Coverage *bool `android:"arch_variant"`
81 Safestack *bool `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070082 Cfi *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080083
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070084 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
85 // Replaces abort() on error with a human-readable error message.
86 // Address and Thread sanitizers always run in diagnostic mode.
87 Diag struct {
Ivan Lozano651275b2017-06-13 10:24:34 -070088 Undefined *bool `android:"arch_variant"`
89 Cfi *bool `android:"arch_variant"`
90 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070091 }
92
93 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -080094 Recover []string
95
96 // value to pass to -fsanitize-blacklist
97 Blacklist *string
98 } `android:"arch_variant"`
99
100 SanitizerEnabled bool `blueprint:"mutated"`
101 SanitizeDep bool `blueprint:"mutated"`
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700102 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800103}
104
105type sanitize struct {
106 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700107
108 runtimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800109}
110
111func (sanitize *sanitize) props() []interface{} {
112 return []interface{}{&sanitize.Properties}
113}
114
115func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700116 s := &sanitize.Properties.Sanitize
117
Colin Cross16b23492016-01-06 14:41:07 -0800118 // Don't apply sanitizers to NDK code.
119 if ctx.sdk() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700120 s.Never = true
Colin Cross16b23492016-01-06 14:41:07 -0800121 }
122
123 // Never always wins.
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700124 if s.Never {
Colin Cross16b23492016-01-06 14:41:07 -0800125 return
126 }
127
Colin Cross16b23492016-01-06 14:41:07 -0800128 var globalSanitizers []string
129 if ctx.clang() {
130 if ctx.Host() {
131 globalSanitizers = ctx.AConfig().SanitizeHost()
132 } else {
Colin Cross23ae82a2016-11-02 14:34:39 -0700133 arches := ctx.AConfig().SanitizeDeviceArch()
134 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
135 globalSanitizers = ctx.AConfig().SanitizeDevice()
136 }
Colin Cross16b23492016-01-06 14:41:07 -0800137 }
138 }
139
Colin Cross16b23492016-01-06 14:41:07 -0800140 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000141 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700142 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
143 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000144 }
Colin Cross16b23492016-01-06 14:41:07 -0800145
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700146 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
147 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000148 }
149
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800150 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
151 if s.Address == nil {
152 s.Address = boolPtr(true)
153 } else if *s.Address == false {
154 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
155 // disables address, then disable coverage as well.
156 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
157 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000158 }
159
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700160 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
161 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000162 }
163
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700164 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
165 s.Coverage = boolPtr(true)
166 }
167
168 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
169 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000170 }
171
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700172 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
173 s.Cfi = boolPtr(true)
174 }
175
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000176 if len(globalSanitizers) > 0 {
177 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
178 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700179 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700180
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800181 // CFI needs gold linker, and mips toolchain does not have one.
182 if !ctx.AConfig().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800183 s.Cfi = nil
184 s.Diag.Cfi = nil
185 }
186
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800187 // Also disable CFI for arm32 until b/35157333 is fixed.
188 if ctx.Arch().ArchType == android.Arm {
189 s.Cfi = nil
190 s.Diag.Cfi = nil
191 }
192
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700193 // Also disable CFI if ASAN is enabled.
194 if Bool(s.Address) {
195 s.Cfi = nil
196 s.Diag.Cfi = nil
197 }
198
Colin Cross3c344ef2016-07-18 15:44:56 -0700199 if ctx.staticBinary() {
200 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700201 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700202 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800203 }
204
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700205 if Bool(s.All_undefined) {
206 s.Undefined = nil
207 }
208
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700209 if !ctx.toolchain().Is64Bit() {
210 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700211 s.Thread = nil
212 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800213 // TODO(ccross): error for compile_multilib = "32"?
214 }
215
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800216 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
217 Bool(s.Coverage) || Bool(s.Safestack) || Bool(s.Cfi) || len(s.Misc_undefined) > 0) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700218 sanitize.Properties.SanitizerEnabled = true
219 }
220
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700221 if Bool(s.Coverage) {
222 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800223 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
224 }
225 }
226}
227
228func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
229 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
230 return deps
231 }
232
233 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700234 if Bool(sanitize.Properties.Sanitize.Address) {
Dan Willemsencbceaab2016-10-13 16:44:07 -0700235 deps.StaticLibs = append(deps.StaticLibs, asanLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800236 }
Colin Cross263abbd2016-07-15 13:10:48 -0700237 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
238 deps.SharedLibs = append(deps.SharedLibs, "libdl")
239 }
Colin Cross16b23492016-01-06 14:41:07 -0800240 }
241
242 return deps
243}
244
245func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
246 if !sanitize.Properties.SanitizerEnabled {
247 return flags
248 }
249
250 if !ctx.clang() {
251 ctx.ModuleErrorf("Use of sanitizers requires clang")
252 }
253
254 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700255 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800256
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700257 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800258 sanitizers = append(sanitizers, "undefined")
259 if ctx.Device() {
260 ctx.ModuleErrorf("ubsan is not yet supported on the device")
261 }
262 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700263 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800264 sanitizers = append(sanitizers,
265 "bool",
266 "integer-divide-by-zero",
267 "return",
268 "returns-nonnull-attribute",
269 "shift-exponent",
270 "unreachable",
271 "vla-bound",
272 // TODO(danalbert): The following checks currently have compiler performance issues.
273 //"alignment",
274 //"bounds",
275 //"enum",
276 //"float-cast-overflow",
277 //"float-divide-by-zero",
278 //"nonnull-attribute",
279 //"null",
280 //"shift-base",
281 //"signed-integer-overflow",
282 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
283 // https://llvm.org/PR19302
284 // http://reviews.llvm.org/D6974
285 // "object-size",
286 )
287 }
288 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
289 }
290
Ivan Lozano651275b2017-06-13 10:24:34 -0700291 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700292 diagSanitizers = append(diagSanitizers, "undefined")
293 }
294
Ivan Lozano651275b2017-06-13 10:24:34 -0700295 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
296
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700297 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700298 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800299 // Frame pointer based unwinder in ASan requires ARM frame setup.
300 // TODO: put in flags?
301 flags.RequiredInstructionSet = "arm"
302 }
Dan Willemsencbceaab2016-10-13 16:44:07 -0700303 flags.CFlags = append(flags.CFlags, asanCflags)
304 flags.LdFlags = append(flags.LdFlags, asanLdflags)
Colin Cross16b23492016-01-06 14:41:07 -0800305
Colin Cross16b23492016-01-06 14:41:07 -0800306 if ctx.Host() {
307 // -nodefaultlibs (provided with libc++) prevents the driver from linking
308 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
309 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
310 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
311 } else {
312 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
313 flags.DynamicLinker = "/system/bin/linker_asan"
314 if flags.Toolchain.Is64Bit() {
315 flags.DynamicLinker += "64"
316 }
317 }
318 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700319 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800320 }
321
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700322 if Bool(sanitize.Properties.Sanitize.Coverage) {
Colin Cross16b23492016-01-06 14:41:07 -0800323 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp")
324 }
325
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700326 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700327 sanitizers = append(sanitizers, "safe-stack")
328 }
329
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700330 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800331 if ctx.Arch().ArchType == android.Arm {
332 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
333 // to do this on a function basis, so force Thumb on the entire module.
334 flags.RequiredInstructionSet = "thumb"
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800335 // Workaround for b/33678192. CFI jumptables need Thumb2 codegen. Revert when
336 // Clang is updated past r290384.
337 flags.LdFlags = append(flags.LdFlags, "-march=armv7-a")
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800338 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700339 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanf3918d32017-02-14 07:59:33 -0800340 flags.CFlags = append(flags.CFlags, cfiCflags)
341 flags.LdFlags = append(flags.LdFlags, cfiLdflags)
Vishwath Mohan7a5b46d2017-03-16 16:36:16 -0700342 flags.ArFlags = append(flags.ArFlags, cfiArflags)
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700343 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
344 diagSanitizers = append(diagSanitizers, "cfi")
345 }
346 }
347
Colin Cross16b23492016-01-06 14:41:07 -0800348 if len(sanitizers) > 0 {
349 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
350 flags.CFlags = append(flags.CFlags, sanitizeArg)
351 if ctx.Host() {
352 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
353 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanovc6482d62017-06-07 15:46:40 -0700354 if ctx.Os() == android.Linux {
355 flags.LdFlags = append(flags.LdFlags, "-lrt")
356 }
357 flags.LdFlags = append(flags.LdFlags, "-ldl")
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800358 // Host sanitizers only link symbols in the final executable, so
359 // there will always be undefined symbols in intermediate libraries.
360 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800361 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700362 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Colin Cross16b23492016-01-06 14:41:07 -0800363 }
364 }
365
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700366 if len(diagSanitizers) > 0 {
367 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
368 }
369 // FIXME: enable RTTI if diag + (cfi or vptr)
370
Andreas Gampe97071162017-05-08 13:15:23 -0700371 if sanitize.Properties.Sanitize.Recover != nil {
372 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
373 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
374 }
375
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700376 // Link a runtime library if needed.
377 runtimeLibrary := ""
378 if Bool(sanitize.Properties.Sanitize.Address) {
379 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
380 } else if len(diagSanitizers) > 0 {
381 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
382 }
383
384 // ASan runtime library must be the first in the link order.
385 if runtimeLibrary != "" {
Colin Cross8ff9ef42017-05-08 13:44:11 -0700386 flags.libFlags = append([]string{
387 "${config.ClangAsanLibDir}/" + runtimeLibrary + ctx.toolchain().ShlibSuffix(),
388 }, flags.libFlags...)
389 sanitize.runtimeLibrary = runtimeLibrary
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700390 }
391
Colin Cross635c3b02016-05-18 15:37:25 -0700392 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800393 if blacklist.Valid() {
394 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
395 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
396 }
397
398 return flags
399}
400
Colin Cross8ff9ef42017-05-08 13:44:11 -0700401func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
402 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
403 if sanitize.runtimeLibrary != "" {
404 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.runtimeLibrary)
405 }
406
407 return nil
408 })
409}
410
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700411func (sanitize *sanitize) inSanitizerDir() bool {
412 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700413}
414
Colin Cross16b23492016-01-06 14:41:07 -0800415func (sanitize *sanitize) Sanitizer(t sanitizerType) bool {
416 if sanitize == nil {
417 return false
418 }
419
420 switch t {
421 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700422 return Bool(sanitize.Properties.Sanitize.Address)
Colin Cross16b23492016-01-06 14:41:07 -0800423 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700424 return Bool(sanitize.Properties.Sanitize.Thread)
Colin Cross16b23492016-01-06 14:41:07 -0800425 default:
426 panic(fmt.Errorf("unknown sanitizerType %d", t))
427 }
428}
429
430func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
431 switch t {
432 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700433 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700434 if !b {
435 sanitize.Properties.Sanitize.Coverage = nil
436 }
Colin Cross16b23492016-01-06 14:41:07 -0800437 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700438 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800439 default:
440 panic(fmt.Errorf("unknown sanitizerType %d", t))
441 }
442 if b {
443 sanitize.Properties.SanitizerEnabled = true
444 }
445}
446
447// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700448func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
449 return func(mctx android.TopDownMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800450 if c, ok := mctx.Module().(*Module); ok && c.sanitize.Sanitizer(t) {
451 mctx.VisitDepsDepthFirst(func(module blueprint.Module) {
452 if d, ok := mctx.Module().(*Module); ok && c.sanitize != nil &&
453 !c.sanitize.Properties.Sanitize.Never {
454 d.sanitize.Properties.SanitizeDep = true
455 }
456 })
457 }
458 }
459}
460
461// Create asan variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700462func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
463 return func(mctx android.BottomUpMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800464 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Colin Crossb916a382016-07-29 17:28:03 -0700465 if c.isDependencyRoot() && c.sanitize.Sanitizer(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700466 modules := mctx.CreateVariations(t.String())
467 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Colin Cross16b23492016-01-06 14:41:07 -0800468 } else if c.sanitize.Properties.SanitizeDep {
Colin Crossb0f28952016-09-19 16:46:53 -0700469 modules := mctx.CreateVariations("", t.String())
470 modules[0].(*Module).sanitize.SetSanitizer(t, false)
471 modules[1].(*Module).sanitize.SetSanitizer(t, true)
472 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
473 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
474 if mctx.Device() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700475 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
Colin Crossb0f28952016-09-19 16:46:53 -0700476 } else {
477 modules[0].(*Module).Properties.PreventInstall = true
478 }
479 if mctx.AConfig().EmbeddedInMake() {
480 modules[0].(*Module).Properties.HideFromMake = true
Colin Cross30d5f512016-05-03 18:02:42 -0700481 }
Colin Cross16b23492016-01-06 14:41:07 -0800482 }
483 c.sanitize.Properties.SanitizeDep = false
484 }
485 }
486}