blob: 4f58602fd094ff8e418b794ecc77ac9c86d6b7cb [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 Chowdhary0d58f982017-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 Chowdhary0d58f982017-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 Chowdhary0d58f982017-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 {
92 Undefined *bool `android:"arch_variant"`
93 Cfi *bool `android:"arch_variant"`
94 }
95
96 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -080097 Recover []string
98
99 // value to pass to -fsanitize-blacklist
100 Blacklist *string
101 } `android:"arch_variant"`
102
103 SanitizerEnabled bool `blueprint:"mutated"`
104 SanitizeDep bool `blueprint:"mutated"`
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700105 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800106}
107
108type sanitize struct {
109 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700110
111 runtimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800112}
113
114func (sanitize *sanitize) props() []interface{} {
115 return []interface{}{&sanitize.Properties}
116}
117
118func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700119 s := &sanitize.Properties.Sanitize
120
Colin Cross16b23492016-01-06 14:41:07 -0800121 // Don't apply sanitizers to NDK code.
122 if ctx.sdk() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700123 s.Never = true
Colin Cross16b23492016-01-06 14:41:07 -0800124 }
125
126 // Never always wins.
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700127 if s.Never {
Colin Cross16b23492016-01-06 14:41:07 -0800128 return
129 }
130
Colin Cross16b23492016-01-06 14:41:07 -0800131 var globalSanitizers []string
132 if ctx.clang() {
133 if ctx.Host() {
134 globalSanitizers = ctx.AConfig().SanitizeHost()
135 } else {
Colin Cross23ae82a2016-11-02 14:34:39 -0700136 arches := ctx.AConfig().SanitizeDeviceArch()
137 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
138 globalSanitizers = ctx.AConfig().SanitizeDevice()
139 }
Colin Cross16b23492016-01-06 14:41:07 -0800140 }
141 }
142
Colin Cross16b23492016-01-06 14:41:07 -0800143 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000144 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700145 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
146 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000147 }
Colin Cross16b23492016-01-06 14:41:07 -0800148
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700149 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
150 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000151 }
152
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800153 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
154 if s.Address == nil {
155 s.Address = boolPtr(true)
156 } else if *s.Address == false {
157 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
158 // disables address, then disable coverage as well.
159 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
160 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000161 }
162
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700163 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
164 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000165 }
166
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700167 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
168 s.Coverage = boolPtr(true)
169 }
170
171 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
172 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000173 }
174
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700175 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
176 s.Cfi = boolPtr(true)
177 }
178
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000179 if len(globalSanitizers) > 0 {
180 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
181 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700182 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700183
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800184 // CFI needs gold linker, and mips toolchain does not have one.
185 if !ctx.AConfig().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800186 s.Cfi = nil
187 s.Diag.Cfi = nil
188 }
189
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800190 // Also disable CFI for arm32 until b/35157333 is fixed.
191 if ctx.Arch().ArchType == android.Arm {
192 s.Cfi = nil
193 s.Diag.Cfi = nil
194 }
195
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700196 // Also disable CFI if ASAN is enabled.
197 if Bool(s.Address) {
198 s.Cfi = nil
199 s.Diag.Cfi = nil
200 }
201
Colin Cross3c344ef2016-07-18 15:44:56 -0700202 if ctx.staticBinary() {
203 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700204 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700205 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800206 }
207
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700208 if Bool(s.All_undefined) {
209 s.Undefined = nil
210 }
211
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700212 if !ctx.toolchain().Is64Bit() {
213 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700214 s.Thread = nil
215 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800216 // TODO(ccross): error for compile_multilib = "32"?
217 }
218
Evgenii Stepanovfe9bc1d2017-01-27 15:44:44 -0800219 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
220 Bool(s.Coverage) || Bool(s.Safestack) || Bool(s.Cfi) || len(s.Misc_undefined) > 0) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700221 sanitize.Properties.SanitizerEnabled = true
222 }
223
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700224 if Bool(s.Coverage) {
225 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800226 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
227 }
228 }
229}
230
231func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
232 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
233 return deps
234 }
235
236 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700237 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary0d58f982017-06-15 14:45:18 -0700238 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800239 }
Colin Cross263abbd2016-07-15 13:10:48 -0700240 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
241 deps.SharedLibs = append(deps.SharedLibs, "libdl")
242 }
Colin Cross16b23492016-01-06 14:41:07 -0800243 }
244
245 return deps
246}
247
248func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
249 if !sanitize.Properties.SanitizerEnabled {
250 return flags
251 }
252
253 if !ctx.clang() {
254 ctx.ModuleErrorf("Use of sanitizers requires clang")
255 }
256
257 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700258 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800259
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700260 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800261 sanitizers = append(sanitizers, "undefined")
262 if ctx.Device() {
263 ctx.ModuleErrorf("ubsan is not yet supported on the device")
264 }
265 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700266 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800267 sanitizers = append(sanitizers,
268 "bool",
269 "integer-divide-by-zero",
270 "return",
271 "returns-nonnull-attribute",
272 "shift-exponent",
273 "unreachable",
274 "vla-bound",
275 // TODO(danalbert): The following checks currently have compiler performance issues.
276 //"alignment",
277 //"bounds",
278 //"enum",
279 //"float-cast-overflow",
280 //"float-divide-by-zero",
281 //"nonnull-attribute",
282 //"null",
283 //"shift-base",
284 //"signed-integer-overflow",
285 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
286 // https://llvm.org/PR19302
287 // http://reviews.llvm.org/D6974
288 // "object-size",
289 )
290 }
291 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
292 }
293
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700294 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) &&
295 (Bool(sanitize.Properties.Sanitize.All_undefined) ||
296 Bool(sanitize.Properties.Sanitize.Undefined) ||
297 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) {
298 diagSanitizers = append(diagSanitizers, "undefined")
299 }
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 Chowdhary0d58f982017-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) {
Colin Cross16b23492016-01-06 14:41:07 -0800327 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp")
328 }
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 Chowdhary0d58f982017-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 Stepanov30451952017-06-07 15:34:41 -0700358 if ctx.Os() == android.Linux {
359 flags.LdFlags = append(flags.LdFlags, "-lrt")
360 }
361 flags.LdFlags = append(flags.LdFlags, "-ldl")
Evgenii Stepanovfe9bc1d2017-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}