blob: fcb3289615fd5288d740c468ae4de7a0bdb6eb7d [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 {
88 Undefined *bool `android:"arch_variant"`
89 Cfi *bool `android:"arch_variant"`
90 }
91
92 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -080093 Recover []string
94
95 // value to pass to -fsanitize-blacklist
96 Blacklist *string
97 } `android:"arch_variant"`
98
99 SanitizerEnabled bool `blueprint:"mutated"`
100 SanitizeDep bool `blueprint:"mutated"`
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700101 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800102}
103
104type sanitize struct {
105 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700106
107 runtimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800108}
109
110func (sanitize *sanitize) props() []interface{} {
111 return []interface{}{&sanitize.Properties}
112}
113
114func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700115 s := &sanitize.Properties.Sanitize
116
Colin Cross16b23492016-01-06 14:41:07 -0800117 // Don't apply sanitizers to NDK code.
118 if ctx.sdk() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700119 s.Never = true
Colin Cross16b23492016-01-06 14:41:07 -0800120 }
121
122 // Never always wins.
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700123 if s.Never {
Colin Cross16b23492016-01-06 14:41:07 -0800124 return
125 }
126
Colin Cross16b23492016-01-06 14:41:07 -0800127 var globalSanitizers []string
128 if ctx.clang() {
129 if ctx.Host() {
130 globalSanitizers = ctx.AConfig().SanitizeHost()
131 } else {
Colin Cross23ae82a2016-11-02 14:34:39 -0700132 arches := ctx.AConfig().SanitizeDeviceArch()
133 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
134 globalSanitizers = ctx.AConfig().SanitizeDevice()
135 }
Colin Cross16b23492016-01-06 14:41:07 -0800136 }
137 }
138
Colin Cross16b23492016-01-06 14:41:07 -0800139 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000140 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700141 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
142 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000143 }
Colin Cross16b23492016-01-06 14:41:07 -0800144
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700145 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
146 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000147 }
148
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800149 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
150 if s.Address == nil {
151 s.Address = boolPtr(true)
152 } else if *s.Address == false {
153 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
154 // disables address, then disable coverage as well.
155 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
156 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000157 }
158
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700159 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
160 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000161 }
162
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700163 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
164 s.Coverage = boolPtr(true)
165 }
166
167 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
168 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000169 }
170
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700171 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
172 s.Cfi = boolPtr(true)
173 }
174
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000175 if len(globalSanitizers) > 0 {
176 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
177 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700178 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700179
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800180 // CFI needs gold linker, and mips toolchain does not have one.
181 if !ctx.AConfig().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800182 s.Cfi = nil
183 s.Diag.Cfi = nil
184 }
185
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800186 // Also disable CFI for arm32 until b/35157333 is fixed.
187 if ctx.Arch().ArchType == android.Arm {
188 s.Cfi = nil
189 s.Diag.Cfi = nil
190 }
191
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700192 // Also disable CFI if ASAN is enabled.
193 if Bool(s.Address) {
194 s.Cfi = nil
195 s.Diag.Cfi = nil
196 }
197
Colin Cross3c344ef2016-07-18 15:44:56 -0700198 if ctx.staticBinary() {
199 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700200 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700201 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800202 }
203
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700204 if Bool(s.All_undefined) {
205 s.Undefined = nil
206 }
207
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700208 if !ctx.toolchain().Is64Bit() {
209 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700210 s.Thread = nil
211 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800212 // TODO(ccross): error for compile_multilib = "32"?
213 }
214
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800215 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
216 Bool(s.Coverage) || Bool(s.Safestack) || Bool(s.Cfi) || len(s.Misc_undefined) > 0) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700217 sanitize.Properties.SanitizerEnabled = true
218 }
219
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700220 if Bool(s.Coverage) {
221 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800222 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
223 }
224 }
225}
226
227func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
228 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
229 return deps
230 }
231
232 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700233 if Bool(sanitize.Properties.Sanitize.Address) {
Dan Willemsencbceaab2016-10-13 16:44:07 -0700234 deps.StaticLibs = append(deps.StaticLibs, asanLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800235 }
Colin Cross263abbd2016-07-15 13:10:48 -0700236 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
237 deps.SharedLibs = append(deps.SharedLibs, "libdl")
238 }
Colin Cross16b23492016-01-06 14:41:07 -0800239 }
240
241 return deps
242}
243
244func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
245 if !sanitize.Properties.SanitizerEnabled {
246 return flags
247 }
248
249 if !ctx.clang() {
250 ctx.ModuleErrorf("Use of sanitizers requires clang")
251 }
252
253 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700254 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800255
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700256 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800257 sanitizers = append(sanitizers, "undefined")
258 if ctx.Device() {
259 ctx.ModuleErrorf("ubsan is not yet supported on the device")
260 }
261 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700262 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800263 sanitizers = append(sanitizers,
264 "bool",
265 "integer-divide-by-zero",
266 "return",
267 "returns-nonnull-attribute",
268 "shift-exponent",
269 "unreachable",
270 "vla-bound",
271 // TODO(danalbert): The following checks currently have compiler performance issues.
272 //"alignment",
273 //"bounds",
274 //"enum",
275 //"float-cast-overflow",
276 //"float-divide-by-zero",
277 //"nonnull-attribute",
278 //"null",
279 //"shift-base",
280 //"signed-integer-overflow",
281 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
282 // https://llvm.org/PR19302
283 // http://reviews.llvm.org/D6974
284 // "object-size",
285 )
286 }
287 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
288 }
289
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700290 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) &&
291 (Bool(sanitize.Properties.Sanitize.All_undefined) ||
292 Bool(sanitize.Properties.Sanitize.Undefined) ||
293 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) {
294 diagSanitizers = append(diagSanitizers, "undefined")
295 }
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)
354 flags.LdFlags = append(flags.LdFlags, "-lrt", "-ldl")
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800355 // Host sanitizers only link symbols in the final executable, so
356 // there will always be undefined symbols in intermediate libraries.
357 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800358 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700359 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Colin Cross16b23492016-01-06 14:41:07 -0800360 }
361 }
362
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700363 if len(diagSanitizers) > 0 {
364 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
365 }
366 // FIXME: enable RTTI if diag + (cfi or vptr)
367
Andreas Gampe97071162017-05-08 13:15:23 -0700368 if sanitize.Properties.Sanitize.Recover != nil {
369 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
370 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
371 }
372
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700373 // Link a runtime library if needed.
374 runtimeLibrary := ""
375 if Bool(sanitize.Properties.Sanitize.Address) {
376 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
377 } else if len(diagSanitizers) > 0 {
378 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
379 }
380
381 // ASan runtime library must be the first in the link order.
382 if runtimeLibrary != "" {
Colin Cross8ff9ef42017-05-08 13:44:11 -0700383 flags.libFlags = append([]string{
384 "${config.ClangAsanLibDir}/" + runtimeLibrary + ctx.toolchain().ShlibSuffix(),
385 }, flags.libFlags...)
386 sanitize.runtimeLibrary = runtimeLibrary
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700387 }
388
Colin Cross635c3b02016-05-18 15:37:25 -0700389 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800390 if blacklist.Valid() {
391 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
392 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
393 }
394
395 return flags
396}
397
Colin Cross8ff9ef42017-05-08 13:44:11 -0700398func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
399 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
400 if sanitize.runtimeLibrary != "" {
401 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.runtimeLibrary)
402 }
403
404 return nil
405 })
406}
407
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700408func (sanitize *sanitize) inSanitizerDir() bool {
409 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700410}
411
Colin Cross16b23492016-01-06 14:41:07 -0800412func (sanitize *sanitize) Sanitizer(t sanitizerType) bool {
413 if sanitize == nil {
414 return false
415 }
416
417 switch t {
418 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700419 return Bool(sanitize.Properties.Sanitize.Address)
Colin Cross16b23492016-01-06 14:41:07 -0800420 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700421 return Bool(sanitize.Properties.Sanitize.Thread)
Colin Cross16b23492016-01-06 14:41:07 -0800422 default:
423 panic(fmt.Errorf("unknown sanitizerType %d", t))
424 }
425}
426
427func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
428 switch t {
429 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700430 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700431 if !b {
432 sanitize.Properties.Sanitize.Coverage = nil
433 }
Colin Cross16b23492016-01-06 14:41:07 -0800434 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700435 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800436 default:
437 panic(fmt.Errorf("unknown sanitizerType %d", t))
438 }
439 if b {
440 sanitize.Properties.SanitizerEnabled = true
441 }
442}
443
444// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700445func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
446 return func(mctx android.TopDownMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800447 if c, ok := mctx.Module().(*Module); ok && c.sanitize.Sanitizer(t) {
448 mctx.VisitDepsDepthFirst(func(module blueprint.Module) {
449 if d, ok := mctx.Module().(*Module); ok && c.sanitize != nil &&
450 !c.sanitize.Properties.Sanitize.Never {
451 d.sanitize.Properties.SanitizeDep = true
452 }
453 })
454 }
455 }
456}
457
458// Create asan variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700459func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
460 return func(mctx android.BottomUpMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800461 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Colin Crossb916a382016-07-29 17:28:03 -0700462 if c.isDependencyRoot() && c.sanitize.Sanitizer(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700463 modules := mctx.CreateVariations(t.String())
464 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Colin Cross16b23492016-01-06 14:41:07 -0800465 } else if c.sanitize.Properties.SanitizeDep {
Colin Crossb0f28952016-09-19 16:46:53 -0700466 modules := mctx.CreateVariations("", t.String())
467 modules[0].(*Module).sanitize.SetSanitizer(t, false)
468 modules[1].(*Module).sanitize.SetSanitizer(t, true)
469 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
470 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
471 if mctx.Device() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700472 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
Colin Crossb0f28952016-09-19 16:46:53 -0700473 } else {
474 modules[0].(*Module).Properties.PreventInstall = true
475 }
476 if mctx.AConfig().EmbeddedInMake() {
477 modules[0].(*Module).Properties.HideFromMake = true
Colin Cross30d5f512016-05-03 18:02:42 -0700478 }
Colin Cross16b23492016-01-06 14:41:07 -0800479 }
480 c.sanitize.Properties.SanitizeDep = false
481 }
482 }
483}