blob: 7d7bd0903aaa34a12f098876fd20462a4b63b6ab [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
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700189 // Also disable CFI if ASAN is enabled.
190 if Bool(s.Address) {
191 s.Cfi = nil
192 s.Diag.Cfi = nil
193 }
194
Colin Cross3c344ef2016-07-18 15:44:56 -0700195 if ctx.staticBinary() {
196 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700197 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700198 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800199 }
200
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700201 if Bool(s.All_undefined) {
202 s.Undefined = nil
203 }
204
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700205 if !ctx.toolchain().Is64Bit() {
206 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700207 s.Thread = nil
208 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800209 // TODO(ccross): error for compile_multilib = "32"?
210 }
211
Colin Cross3c344ef2016-07-18 15:44:56 -0700212 if Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) ||
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700213 Bool(s.Thread) || Bool(s.Coverage) || Bool(s.Safestack) || Bool(s.Cfi) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700214 sanitize.Properties.SanitizerEnabled = true
215 }
216
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700217 if Bool(s.Coverage) {
218 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800219 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
220 }
221 }
222}
223
224func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
225 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
226 return deps
227 }
228
229 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700230 if Bool(sanitize.Properties.Sanitize.Address) {
Dan Willemsencbceaab2016-10-13 16:44:07 -0700231 deps.StaticLibs = append(deps.StaticLibs, asanLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800232 }
Colin Cross263abbd2016-07-15 13:10:48 -0700233 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
234 deps.SharedLibs = append(deps.SharedLibs, "libdl")
235 }
Colin Cross16b23492016-01-06 14:41:07 -0800236 }
237
238 return deps
239}
240
241func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
242 if !sanitize.Properties.SanitizerEnabled {
243 return flags
244 }
245
246 if !ctx.clang() {
247 ctx.ModuleErrorf("Use of sanitizers requires clang")
248 }
249
250 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700251 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800252
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700253 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800254 sanitizers = append(sanitizers, "undefined")
255 if ctx.Device() {
256 ctx.ModuleErrorf("ubsan is not yet supported on the device")
257 }
258 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700259 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800260 sanitizers = append(sanitizers,
261 "bool",
262 "integer-divide-by-zero",
263 "return",
264 "returns-nonnull-attribute",
265 "shift-exponent",
266 "unreachable",
267 "vla-bound",
268 // TODO(danalbert): The following checks currently have compiler performance issues.
269 //"alignment",
270 //"bounds",
271 //"enum",
272 //"float-cast-overflow",
273 //"float-divide-by-zero",
274 //"nonnull-attribute",
275 //"null",
276 //"shift-base",
277 //"signed-integer-overflow",
278 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
279 // https://llvm.org/PR19302
280 // http://reviews.llvm.org/D6974
281 // "object-size",
282 )
283 }
284 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
285 }
286
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700287 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) &&
288 (Bool(sanitize.Properties.Sanitize.All_undefined) ||
289 Bool(sanitize.Properties.Sanitize.Undefined) ||
290 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) {
291 diagSanitizers = append(diagSanitizers, "undefined")
292 }
293
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700294 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700295 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800296 // Frame pointer based unwinder in ASan requires ARM frame setup.
297 // TODO: put in flags?
298 flags.RequiredInstructionSet = "arm"
299 }
Dan Willemsencbceaab2016-10-13 16:44:07 -0700300 flags.CFlags = append(flags.CFlags, asanCflags)
301 flags.LdFlags = append(flags.LdFlags, asanLdflags)
Colin Cross16b23492016-01-06 14:41:07 -0800302
Colin Cross16b23492016-01-06 14:41:07 -0800303 if ctx.Host() {
304 // -nodefaultlibs (provided with libc++) prevents the driver from linking
305 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
306 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
307 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
Colin Cross46974e22016-10-20 12:41:14 -0700308 // Host ASAN only links symbols in the final executable, so
309 // there will always be undefined symbols in intermediate libraries.
310 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800311 } 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 sanitize.Properties.Sanitize.Recover != nil {
349 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
350 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
351 }
352
353 if len(sanitizers) > 0 {
354 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
355 flags.CFlags = append(flags.CFlags, sanitizeArg)
356 if ctx.Host() {
357 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
358 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
359 flags.LdFlags = append(flags.LdFlags, "-lrt", "-ldl")
360 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700361 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Colin Cross16b23492016-01-06 14:41:07 -0800362 }
363 }
364
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700365 if len(diagSanitizers) > 0 {
366 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
367 }
368 // FIXME: enable RTTI if diag + (cfi or vptr)
369
370 // Link a runtime library if needed.
371 runtimeLibrary := ""
372 if Bool(sanitize.Properties.Sanitize.Address) {
373 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
374 } else if len(diagSanitizers) > 0 {
375 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
376 }
377
378 // ASan runtime library must be the first in the link order.
379 if runtimeLibrary != "" {
380 flags.libFlags = append([]string{"${config.ClangAsanLibDir}/" + runtimeLibrary}, flags.libFlags...)
381 }
382
Colin Cross635c3b02016-05-18 15:37:25 -0700383 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800384 if blacklist.Valid() {
385 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
386 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
387 }
388
389 return flags
390}
391
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700392func (sanitize *sanitize) inSanitizerDir() bool {
393 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700394}
395
Colin Cross16b23492016-01-06 14:41:07 -0800396func (sanitize *sanitize) Sanitizer(t sanitizerType) bool {
397 if sanitize == nil {
398 return false
399 }
400
401 switch t {
402 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700403 return Bool(sanitize.Properties.Sanitize.Address)
Colin Cross16b23492016-01-06 14:41:07 -0800404 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700405 return Bool(sanitize.Properties.Sanitize.Thread)
Colin Cross16b23492016-01-06 14:41:07 -0800406 default:
407 panic(fmt.Errorf("unknown sanitizerType %d", t))
408 }
409}
410
411func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
412 switch t {
413 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700414 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700415 if !b {
416 sanitize.Properties.Sanitize.Coverage = nil
417 }
Colin Cross16b23492016-01-06 14:41:07 -0800418 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700419 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800420 default:
421 panic(fmt.Errorf("unknown sanitizerType %d", t))
422 }
423 if b {
424 sanitize.Properties.SanitizerEnabled = true
425 }
426}
427
428// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700429func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
430 return func(mctx android.TopDownMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800431 if c, ok := mctx.Module().(*Module); ok && c.sanitize.Sanitizer(t) {
432 mctx.VisitDepsDepthFirst(func(module blueprint.Module) {
433 if d, ok := mctx.Module().(*Module); ok && c.sanitize != nil &&
434 !c.sanitize.Properties.Sanitize.Never {
435 d.sanitize.Properties.SanitizeDep = true
436 }
437 })
438 }
439 }
440}
441
442// Create asan variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700443func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
444 return func(mctx android.BottomUpMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800445 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Colin Crossb916a382016-07-29 17:28:03 -0700446 if c.isDependencyRoot() && c.sanitize.Sanitizer(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700447 modules := mctx.CreateVariations(t.String())
448 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Colin Cross16b23492016-01-06 14:41:07 -0800449 } else if c.sanitize.Properties.SanitizeDep {
Colin Crossb0f28952016-09-19 16:46:53 -0700450 modules := mctx.CreateVariations("", t.String())
451 modules[0].(*Module).sanitize.SetSanitizer(t, false)
452 modules[1].(*Module).sanitize.SetSanitizer(t, true)
453 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
454 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
455 if mctx.Device() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700456 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
Colin Crossb0f28952016-09-19 16:46:53 -0700457 } else {
458 modules[0].(*Module).Properties.PreventInstall = true
459 }
460 if mctx.AConfig().EmbeddedInMake() {
461 modules[0].(*Module).Properties.HideFromMake = true
Colin Cross30d5f512016-05-03 18:02:42 -0700462 }
Colin Cross16b23492016-01-06 14:41:07 -0800463 }
464 c.sanitize.Properties.SanitizeDep = false
465 }
466 }
467}