blob: 18d6c16e173c4fd716660945d6248d4573c6738e [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"
19 "strings"
20
21 "github.com/google/blueprint"
22
Colin Cross635c3b02016-05-18 15:37:25 -070023 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070024 "android/soong/cc/config"
Colin Cross16b23492016-01-06 14:41:07 -080025)
26
Dan Willemsencbceaab2016-10-13 16:44:07 -070027const (
Dan Willemsen78ffeea2016-10-20 18:46:48 -070028 asanCflags = "-fno-omit-frame-pointer"
Dan Willemsencbceaab2016-10-13 16:44:07 -070029 asanLdflags = "-Wl,-u,__asan_preinit"
Dan Willemsen78ffeea2016-10-20 18:46:48 -070030 asanLibs = "libasan"
Vishwath Mohanf3918d32017-02-14 07:59:33 -080031
32 cfiCflags = "-flto -fsanitize-cfi-cross-dso -fvisibility=default " +
33 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"
34 // FIXME: revert the __cfi_check flag when clang is updated to r280031.
35 cfiLdflags = "-flto -fsanitize-cfi-cross-dso -fsanitize=cfi " +
36 "-Wl,-plugin-opt,O1 -Wl,-export-dynamic-symbol=__cfi_check"
Vishwath Mohan7a5b46d2017-03-16 16:36:16 -070037 cfiArflags = "--plugin ${config.ClangBin}/../lib64/LLVMgold.so"
Dan Willemsencbceaab2016-10-13 16:44:07 -070038)
39
Colin Cross16b23492016-01-06 14:41:07 -080040type sanitizerType int
41
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070042func boolPtr(v bool) *bool {
43 if v {
44 return &v
45 } else {
46 return nil
47 }
48}
49
Colin Cross16b23492016-01-06 14:41:07 -080050const (
51 asan sanitizerType = iota + 1
52 tsan
53)
54
55func (t sanitizerType) String() string {
56 switch t {
57 case asan:
58 return "asan"
59 case tsan:
60 return "tsan"
61 default:
62 panic(fmt.Errorf("unknown sanitizerType %d", t))
63 }
64}
65
66type SanitizeProperties struct {
67 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
68 Sanitize struct {
69 Never bool `android:"arch_variant"`
70
71 // main sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070072 Address *bool `android:"arch_variant"`
73 Thread *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080074
75 // local sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070076 Undefined *bool `android:"arch_variant"`
77 All_undefined *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080078 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070079 Coverage *bool `android:"arch_variant"`
80 Safestack *bool `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070081 Cfi *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080082
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070083 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
84 // Replaces abort() on error with a human-readable error message.
85 // Address and Thread sanitizers always run in diagnostic mode.
86 Diag struct {
87 Undefined *bool `android:"arch_variant"`
88 Cfi *bool `android:"arch_variant"`
89 }
90
91 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -080092 Recover []string
93
94 // value to pass to -fsanitize-blacklist
95 Blacklist *string
96 } `android:"arch_variant"`
97
98 SanitizerEnabled bool `blueprint:"mutated"`
99 SanitizeDep bool `blueprint:"mutated"`
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700100 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800101}
102
103type sanitize struct {
104 Properties SanitizeProperties
105}
106
107func (sanitize *sanitize) props() []interface{} {
108 return []interface{}{&sanitize.Properties}
109}
110
111func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700112 s := &sanitize.Properties.Sanitize
113
Colin Cross16b23492016-01-06 14:41:07 -0800114 // Don't apply sanitizers to NDK code.
115 if ctx.sdk() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700116 s.Never = true
Colin Cross16b23492016-01-06 14:41:07 -0800117 }
118
119 // Never always wins.
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700120 if s.Never {
Colin Cross16b23492016-01-06 14:41:07 -0800121 return
122 }
123
Colin Cross16b23492016-01-06 14:41:07 -0800124 var globalSanitizers []string
125 if ctx.clang() {
126 if ctx.Host() {
127 globalSanitizers = ctx.AConfig().SanitizeHost()
128 } else {
Colin Cross23ae82a2016-11-02 14:34:39 -0700129 arches := ctx.AConfig().SanitizeDeviceArch()
130 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
131 globalSanitizers = ctx.AConfig().SanitizeDevice()
132 }
Colin Cross16b23492016-01-06 14:41:07 -0800133 }
134 }
135
Colin Cross16b23492016-01-06 14:41:07 -0800136 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000137 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700138 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
139 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000140 }
Colin Cross16b23492016-01-06 14:41:07 -0800141
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700142 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
143 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000144 }
145
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800146 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
147 if s.Address == nil {
148 s.Address = boolPtr(true)
149 } else if *s.Address == false {
150 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
151 // disables address, then disable coverage as well.
152 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
153 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000154 }
155
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700156 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
157 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000158 }
159
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700160 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
161 s.Coverage = boolPtr(true)
162 }
163
164 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
165 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000166 }
167
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700168 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
169 s.Cfi = boolPtr(true)
170 }
171
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000172 if len(globalSanitizers) > 0 {
173 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
174 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700175 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700176
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800177 // CFI needs gold linker, and mips toolchain does not have one.
178 if !ctx.AConfig().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800179 s.Cfi = nil
180 s.Diag.Cfi = nil
181 }
182
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800183 // Also disable CFI for arm32 until b/35157333 is fixed.
184 if ctx.Arch().ArchType == android.Arm {
185 s.Cfi = nil
186 s.Diag.Cfi = nil
187 }
188
Colin Cross3c344ef2016-07-18 15:44:56 -0700189 if ctx.staticBinary() {
190 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700191 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700192 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800193 }
194
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700195 if Bool(s.All_undefined) {
196 s.Undefined = nil
197 }
198
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700199 if !ctx.toolchain().Is64Bit() {
200 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700201 s.Thread = nil
202 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800203 // TODO(ccross): error for compile_multilib = "32"?
204 }
205
Colin Cross3c344ef2016-07-18 15:44:56 -0700206 if Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) ||
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700207 Bool(s.Thread) || Bool(s.Coverage) || Bool(s.Safestack) || Bool(s.Cfi) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700208 sanitize.Properties.SanitizerEnabled = true
209 }
210
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700211 if Bool(s.Coverage) {
212 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800213 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
214 }
215 }
216}
217
218func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
219 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
220 return deps
221 }
222
223 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700224 if Bool(sanitize.Properties.Sanitize.Address) {
Dan Willemsencbceaab2016-10-13 16:44:07 -0700225 deps.StaticLibs = append(deps.StaticLibs, asanLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800226 }
Colin Cross263abbd2016-07-15 13:10:48 -0700227 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
228 deps.SharedLibs = append(deps.SharedLibs, "libdl")
229 }
Colin Cross16b23492016-01-06 14:41:07 -0800230 }
231
232 return deps
233}
234
235func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
236 if !sanitize.Properties.SanitizerEnabled {
237 return flags
238 }
239
240 if !ctx.clang() {
241 ctx.ModuleErrorf("Use of sanitizers requires clang")
242 }
243
244 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700245 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800246
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700247 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800248 sanitizers = append(sanitizers, "undefined")
249 if ctx.Device() {
250 ctx.ModuleErrorf("ubsan is not yet supported on the device")
251 }
252 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700253 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800254 sanitizers = append(sanitizers,
255 "bool",
256 "integer-divide-by-zero",
257 "return",
258 "returns-nonnull-attribute",
259 "shift-exponent",
260 "unreachable",
261 "vla-bound",
262 // TODO(danalbert): The following checks currently have compiler performance issues.
263 //"alignment",
264 //"bounds",
265 //"enum",
266 //"float-cast-overflow",
267 //"float-divide-by-zero",
268 //"nonnull-attribute",
269 //"null",
270 //"shift-base",
271 //"signed-integer-overflow",
272 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
273 // https://llvm.org/PR19302
274 // http://reviews.llvm.org/D6974
275 // "object-size",
276 )
277 }
278 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
279 }
280
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700281 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) &&
282 (Bool(sanitize.Properties.Sanitize.All_undefined) ||
283 Bool(sanitize.Properties.Sanitize.Undefined) ||
284 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) {
285 diagSanitizers = append(diagSanitizers, "undefined")
286 }
287
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700288 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700289 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800290 // Frame pointer based unwinder in ASan requires ARM frame setup.
291 // TODO: put in flags?
292 flags.RequiredInstructionSet = "arm"
293 }
Dan Willemsencbceaab2016-10-13 16:44:07 -0700294 flags.CFlags = append(flags.CFlags, asanCflags)
295 flags.LdFlags = append(flags.LdFlags, asanLdflags)
Colin Cross16b23492016-01-06 14:41:07 -0800296
Colin Cross16b23492016-01-06 14:41:07 -0800297 if ctx.Host() {
298 // -nodefaultlibs (provided with libc++) prevents the driver from linking
299 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
300 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
301 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
Colin Cross46974e22016-10-20 12:41:14 -0700302 // Host ASAN only links symbols in the final executable, so
303 // there will always be undefined symbols in intermediate libraries.
304 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800305 } else {
306 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
307 flags.DynamicLinker = "/system/bin/linker_asan"
308 if flags.Toolchain.Is64Bit() {
309 flags.DynamicLinker += "64"
310 }
311 }
312 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700313 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800314 }
315
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700316 if Bool(sanitize.Properties.Sanitize.Coverage) {
Colin Cross16b23492016-01-06 14:41:07 -0800317 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp")
318 }
319
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700320 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700321 sanitizers = append(sanitizers, "safe-stack")
322 }
323
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700324 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800325 if ctx.Arch().ArchType == android.Arm {
326 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
327 // to do this on a function basis, so force Thumb on the entire module.
328 flags.RequiredInstructionSet = "thumb"
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800329 // Workaround for b/33678192. CFI jumptables need Thumb2 codegen. Revert when
330 // Clang is updated past r290384.
331 flags.LdFlags = append(flags.LdFlags, "-march=armv7-a")
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800332 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700333 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanf3918d32017-02-14 07:59:33 -0800334 flags.CFlags = append(flags.CFlags, cfiCflags)
335 flags.LdFlags = append(flags.LdFlags, cfiLdflags)
Vishwath Mohan7a5b46d2017-03-16 16:36:16 -0700336 flags.ArFlags = append(flags.ArFlags, cfiArflags)
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700337 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
338 diagSanitizers = append(diagSanitizers, "cfi")
339 }
340 }
341
Colin Cross16b23492016-01-06 14:41:07 -0800342 if sanitize.Properties.Sanitize.Recover != nil {
343 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
344 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
345 }
346
347 if len(sanitizers) > 0 {
348 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
349 flags.CFlags = append(flags.CFlags, sanitizeArg)
350 if ctx.Host() {
351 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
352 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
353 flags.LdFlags = append(flags.LdFlags, "-lrt", "-ldl")
354 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700355 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Colin Cross16b23492016-01-06 14:41:07 -0800356 }
357 }
358
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700359 if len(diagSanitizers) > 0 {
360 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
361 }
362 // FIXME: enable RTTI if diag + (cfi or vptr)
363
364 // Link a runtime library if needed.
365 runtimeLibrary := ""
366 if Bool(sanitize.Properties.Sanitize.Address) {
367 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
368 } else if len(diagSanitizers) > 0 {
369 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
370 }
371
372 // ASan runtime library must be the first in the link order.
373 if runtimeLibrary != "" {
374 flags.libFlags = append([]string{"${config.ClangAsanLibDir}/" + runtimeLibrary}, flags.libFlags...)
375 }
376
Colin Cross635c3b02016-05-18 15:37:25 -0700377 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800378 if blacklist.Valid() {
379 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
380 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
381 }
382
383 return flags
384}
385
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700386func (sanitize *sanitize) inSanitizerDir() bool {
387 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700388}
389
Colin Cross16b23492016-01-06 14:41:07 -0800390func (sanitize *sanitize) Sanitizer(t sanitizerType) bool {
391 if sanitize == nil {
392 return false
393 }
394
395 switch t {
396 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700397 return Bool(sanitize.Properties.Sanitize.Address)
Colin Cross16b23492016-01-06 14:41:07 -0800398 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700399 return Bool(sanitize.Properties.Sanitize.Thread)
Colin Cross16b23492016-01-06 14:41:07 -0800400 default:
401 panic(fmt.Errorf("unknown sanitizerType %d", t))
402 }
403}
404
405func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
406 switch t {
407 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700408 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700409 if !b {
410 sanitize.Properties.Sanitize.Coverage = nil
411 }
Colin Cross16b23492016-01-06 14:41:07 -0800412 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700413 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800414 default:
415 panic(fmt.Errorf("unknown sanitizerType %d", t))
416 }
417 if b {
418 sanitize.Properties.SanitizerEnabled = true
419 }
420}
421
422// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700423func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
424 return func(mctx android.TopDownMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800425 if c, ok := mctx.Module().(*Module); ok && c.sanitize.Sanitizer(t) {
426 mctx.VisitDepsDepthFirst(func(module blueprint.Module) {
427 if d, ok := mctx.Module().(*Module); ok && c.sanitize != nil &&
428 !c.sanitize.Properties.Sanitize.Never {
429 d.sanitize.Properties.SanitizeDep = true
430 }
431 })
432 }
433 }
434}
435
436// Create asan variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700437func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
438 return func(mctx android.BottomUpMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800439 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Colin Crossb916a382016-07-29 17:28:03 -0700440 if c.isDependencyRoot() && c.sanitize.Sanitizer(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700441 modules := mctx.CreateVariations(t.String())
442 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Colin Cross16b23492016-01-06 14:41:07 -0800443 } else if c.sanitize.Properties.SanitizeDep {
Colin Crossb0f28952016-09-19 16:46:53 -0700444 modules := mctx.CreateVariations("", t.String())
445 modules[0].(*Module).sanitize.SetSanitizer(t, false)
446 modules[1].(*Module).sanitize.SetSanitizer(t, true)
447 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
448 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
449 if mctx.Device() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700450 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
Colin Crossb0f28952016-09-19 16:46:53 -0700451 } else {
452 modules[0].(*Module).Properties.PreventInstall = true
453 }
454 if mctx.AConfig().EmbeddedInMake() {
455 modules[0].(*Module).Properties.HideFromMake = true
Colin Cross30d5f512016-05-03 18:02:42 -0700456 }
Colin Cross16b23492016-01-06 14:41:07 -0800457 }
458 c.sanitize.Properties.SanitizeDep = false
459 }
460 }
461}