blob: eccd25587dc3a9f4fd4d93598018fd391fb7eb94 [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 Chowdhary9677e8c2017-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 Chowdhary9677e8c2017-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 Chowdhary9677e8c2017-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"}
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070043
44 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070045)
46
Colin Cross16b23492016-01-06 14:41:07 -080047type sanitizerType int
48
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070049func boolPtr(v bool) *bool {
50 if v {
51 return &v
52 } else {
53 return nil
54 }
55}
56
Colin Cross16b23492016-01-06 14:41:07 -080057const (
58 asan sanitizerType = iota + 1
59 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070060 intOverflow
Colin Cross16b23492016-01-06 14:41:07 -080061)
62
63func (t sanitizerType) String() string {
64 switch t {
65 case asan:
66 return "asan"
67 case tsan:
68 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070069 case intOverflow:
70 return "intOverflow"
Colin Cross16b23492016-01-06 14:41:07 -080071 default:
72 panic(fmt.Errorf("unknown sanitizerType %d", t))
73 }
74}
75
76type SanitizeProperties struct {
77 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
78 Sanitize struct {
79 Never bool `android:"arch_variant"`
80
81 // main sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070082 Address *bool `android:"arch_variant"`
83 Thread *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080084
85 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070086 Undefined *bool `android:"arch_variant"`
87 All_undefined *bool `android:"arch_variant"`
88 Misc_undefined []string `android:"arch_variant"`
89 Coverage *bool `android:"arch_variant"`
90 Safestack *bool `android:"arch_variant"`
91 Cfi *bool `android:"arch_variant"`
92 Integer_overflow *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080093
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070094 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
95 // Replaces abort() on error with a human-readable error message.
96 // Address and Thread sanitizers always run in diagnostic mode.
97 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070098 Undefined *bool `android:"arch_variant"`
99 Cfi *bool `android:"arch_variant"`
100 Integer_overflow *bool `android:"arch_variant"`
101 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700102 }
103
104 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800105 Recover []string
106
107 // value to pass to -fsanitize-blacklist
108 Blacklist *string
109 } `android:"arch_variant"`
110
111 SanitizerEnabled bool `blueprint:"mutated"`
112 SanitizeDep bool `blueprint:"mutated"`
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700113 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800114}
115
116type sanitize struct {
117 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700118
119 runtimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800120}
121
122func (sanitize *sanitize) props() []interface{} {
123 return []interface{}{&sanitize.Properties}
124}
125
126func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700127 s := &sanitize.Properties.Sanitize
128
Colin Cross16b23492016-01-06 14:41:07 -0800129 // Don't apply sanitizers to NDK code.
130 if ctx.sdk() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700131 s.Never = true
Colin Cross16b23492016-01-06 14:41:07 -0800132 }
133
134 // Never always wins.
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700135 if s.Never {
Colin Cross16b23492016-01-06 14:41:07 -0800136 return
137 }
138
Colin Cross16b23492016-01-06 14:41:07 -0800139 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700140 var globalSanitizersDiag []string
141
Colin Cross16b23492016-01-06 14:41:07 -0800142 if ctx.clang() {
143 if ctx.Host() {
144 globalSanitizers = ctx.AConfig().SanitizeHost()
145 } else {
Colin Cross23ae82a2016-11-02 14:34:39 -0700146 arches := ctx.AConfig().SanitizeDeviceArch()
147 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
148 globalSanitizers = ctx.AConfig().SanitizeDevice()
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700149 globalSanitizersDiag = ctx.AConfig().SanitizeDeviceDiag()
Colin Cross23ae82a2016-11-02 14:34:39 -0700150 }
Colin Cross16b23492016-01-06 14:41:07 -0800151 }
152 }
153
Colin Cross16b23492016-01-06 14:41:07 -0800154 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000155 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700156 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
157 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000158 }
Colin Cross16b23492016-01-06 14:41:07 -0800159
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700160 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
161 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000162 }
163
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800164 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
165 if s.Address == nil {
166 s.Address = boolPtr(true)
167 } else if *s.Address == false {
168 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
169 // disables address, then disable coverage as well.
170 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
171 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000172 }
173
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700174 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
175 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000176 }
177
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700178 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
179 s.Coverage = boolPtr(true)
180 }
181
182 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
183 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000184 }
185
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700186 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
187 s.Cfi = boolPtr(true)
188 }
189
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700190 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozano5f595532017-07-13 14:46:05 -0700191 if !ctx.AConfig().IntegerOverflowDisabledForPath(ctx.ModuleDir()) {
192 s.Integer_overflow = boolPtr(true)
193 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700194 }
195
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000196 if len(globalSanitizers) > 0 {
197 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
198 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700199
200 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
201 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) {
202 s.Diag.Integer_overflow = boolPtr(true)
203 }
204
205 if len(globalSanitizersDiag) > 0 {
206 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
207 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700208 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700209
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800210 // CFI needs gold linker, and mips toolchain does not have one.
211 if !ctx.AConfig().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800212 s.Cfi = nil
213 s.Diag.Cfi = nil
214 }
215
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800216 // Also disable CFI for arm32 until b/35157333 is fixed.
217 if ctx.Arch().ArchType == android.Arm {
218 s.Cfi = nil
219 s.Diag.Cfi = nil
220 }
221
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700222 // Also disable CFI if ASAN is enabled.
223 if Bool(s.Address) {
224 s.Cfi = nil
225 s.Diag.Cfi = nil
226 }
227
Colin Cross3c344ef2016-07-18 15:44:56 -0700228 if ctx.staticBinary() {
229 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700230 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700231 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800232 }
233
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700234 if Bool(s.All_undefined) {
235 s.Undefined = nil
236 }
237
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700238 if !ctx.toolchain().Is64Bit() {
239 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700240 s.Thread = nil
241 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800242 // TODO(ccross): error for compile_multilib = "32"?
243 }
244
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800245 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700246 Bool(s.Coverage) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700247 sanitize.Properties.SanitizerEnabled = true
248 }
249
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700250 if Bool(s.Coverage) {
251 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800252 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
253 }
254 }
255}
256
257func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
258 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
259 return deps
260 }
261
262 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700263 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700264 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800265 }
Colin Cross263abbd2016-07-15 13:10:48 -0700266 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
267 deps.SharedLibs = append(deps.SharedLibs, "libdl")
268 }
Colin Cross16b23492016-01-06 14:41:07 -0800269 }
270
271 return deps
272}
273
274func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
275 if !sanitize.Properties.SanitizerEnabled {
276 return flags
277 }
278
279 if !ctx.clang() {
280 ctx.ModuleErrorf("Use of sanitizers requires clang")
281 }
282
283 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700284 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800285
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700286 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800287 sanitizers = append(sanitizers, "undefined")
288 if ctx.Device() {
289 ctx.ModuleErrorf("ubsan is not yet supported on the device")
290 }
291 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700292 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800293 sanitizers = append(sanitizers,
294 "bool",
295 "integer-divide-by-zero",
296 "return",
297 "returns-nonnull-attribute",
298 "shift-exponent",
299 "unreachable",
300 "vla-bound",
301 // TODO(danalbert): The following checks currently have compiler performance issues.
302 //"alignment",
303 //"bounds",
304 //"enum",
305 //"float-cast-overflow",
306 //"float-divide-by-zero",
307 //"nonnull-attribute",
308 //"null",
309 //"shift-base",
310 //"signed-integer-overflow",
311 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
312 // https://llvm.org/PR19302
313 // http://reviews.llvm.org/D6974
314 // "object-size",
315 )
316 }
317 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
318 }
319
Ivan Lozano651275b2017-06-13 10:24:34 -0700320 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700321 diagSanitizers = append(diagSanitizers, "undefined")
322 }
323
Ivan Lozano651275b2017-06-13 10:24:34 -0700324 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
325
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700326 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700327 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800328 // Frame pointer based unwinder in ASan requires ARM frame setup.
329 // TODO: put in flags?
330 flags.RequiredInstructionSet = "arm"
331 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700332 flags.CFlags = append(flags.CFlags, asanCflags...)
333 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800334
Colin Cross16b23492016-01-06 14:41:07 -0800335 if ctx.Host() {
336 // -nodefaultlibs (provided with libc++) prevents the driver from linking
337 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
338 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
339 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
340 } else {
341 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
342 flags.DynamicLinker = "/system/bin/linker_asan"
343 if flags.Toolchain.Is64Bit() {
344 flags.DynamicLinker += "64"
345 }
346 }
347 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700348 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800349 }
350
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700351 if Bool(sanitize.Properties.Sanitize.Coverage) {
Dan Austin8241abb2017-06-28 15:29:09 -0700352 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard")
Colin Cross16b23492016-01-06 14:41:07 -0800353 }
354
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700355 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700356 sanitizers = append(sanitizers, "safe-stack")
357 }
358
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700359 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800360 if ctx.Arch().ArchType == android.Arm {
361 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
362 // to do this on a function basis, so force Thumb on the entire module.
363 flags.RequiredInstructionSet = "thumb"
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800364 // Workaround for b/33678192. CFI jumptables need Thumb2 codegen. Revert when
365 // Clang is updated past r290384.
366 flags.LdFlags = append(flags.LdFlags, "-march=armv7-a")
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800367 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700368 sanitizers = append(sanitizers, "cfi")
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700369 flags.CFlags = append(flags.CFlags, cfiCflags...)
370 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
371 flags.ArFlags = append(flags.ArFlags, cfiArflags...)
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700372 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
373 diagSanitizers = append(diagSanitizers, "cfi")
374 }
375 }
376
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700377 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
378 if !ctx.static() {
379 sanitizers = append(sanitizers, "unsigned-integer-overflow")
380 sanitizers = append(sanitizers, "signed-integer-overflow")
381 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
382 if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) {
383 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
384 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
385 }
386 }
387 }
388
Colin Cross16b23492016-01-06 14:41:07 -0800389 if len(sanitizers) > 0 {
390 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
391 flags.CFlags = append(flags.CFlags, sanitizeArg)
392 if ctx.Host() {
393 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
394 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanovc6482d62017-06-07 15:46:40 -0700395 if ctx.Os() == android.Linux {
396 flags.LdFlags = append(flags.LdFlags, "-lrt")
397 }
398 flags.LdFlags = append(flags.LdFlags, "-ldl")
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800399 // Host sanitizers only link symbols in the final executable, so
400 // there will always be undefined symbols in intermediate libraries.
401 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800402 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700403 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Colin Cross16b23492016-01-06 14:41:07 -0800404 }
405 }
406
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700407 if len(diagSanitizers) > 0 {
408 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
409 }
410 // FIXME: enable RTTI if diag + (cfi or vptr)
411
Andreas Gampe97071162017-05-08 13:15:23 -0700412 if sanitize.Properties.Sanitize.Recover != nil {
413 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
414 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
415 }
416
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700417 // Link a runtime library if needed.
418 runtimeLibrary := ""
419 if Bool(sanitize.Properties.Sanitize.Address) {
420 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
421 } else if len(diagSanitizers) > 0 {
422 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
423 }
424
425 // ASan runtime library must be the first in the link order.
426 if runtimeLibrary != "" {
Colin Cross8ff9ef42017-05-08 13:44:11 -0700427 flags.libFlags = append([]string{
428 "${config.ClangAsanLibDir}/" + runtimeLibrary + ctx.toolchain().ShlibSuffix(),
429 }, flags.libFlags...)
430 sanitize.runtimeLibrary = runtimeLibrary
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700431 }
432
Colin Cross635c3b02016-05-18 15:37:25 -0700433 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800434 if blacklist.Valid() {
435 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
436 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
437 }
438
439 return flags
440}
441
Colin Cross8ff9ef42017-05-08 13:44:11 -0700442func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
443 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
444 if sanitize.runtimeLibrary != "" {
445 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.runtimeLibrary)
446 }
447
448 return nil
449 })
450}
451
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700452func (sanitize *sanitize) inSanitizerDir() bool {
453 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700454}
455
Colin Cross16b23492016-01-06 14:41:07 -0800456func (sanitize *sanitize) Sanitizer(t sanitizerType) bool {
457 if sanitize == nil {
458 return false
459 }
460
461 switch t {
462 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700463 return Bool(sanitize.Properties.Sanitize.Address)
Colin Cross16b23492016-01-06 14:41:07 -0800464 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700465 return Bool(sanitize.Properties.Sanitize.Thread)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700466 case intOverflow:
467 return Bool(sanitize.Properties.Sanitize.Integer_overflow)
Colin Cross16b23492016-01-06 14:41:07 -0800468 default:
469 panic(fmt.Errorf("unknown sanitizerType %d", t))
470 }
471}
472
473func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
474 switch t {
475 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700476 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700477 if !b {
478 sanitize.Properties.Sanitize.Coverage = nil
479 }
Colin Cross16b23492016-01-06 14:41:07 -0800480 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700481 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700482 case intOverflow:
483 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800484 default:
485 panic(fmt.Errorf("unknown sanitizerType %d", t))
486 }
487 if b {
488 sanitize.Properties.SanitizerEnabled = true
489 }
490}
491
492// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700493func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
494 return func(mctx android.TopDownMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800495 if c, ok := mctx.Module().(*Module); ok && c.sanitize.Sanitizer(t) {
496 mctx.VisitDepsDepthFirst(func(module blueprint.Module) {
497 if d, ok := mctx.Module().(*Module); ok && c.sanitize != nil &&
498 !c.sanitize.Properties.Sanitize.Never {
499 d.sanitize.Properties.SanitizeDep = true
500 }
501 })
502 }
503 }
504}
505
506// Create asan variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700507func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
508 return func(mctx android.BottomUpMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800509 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Colin Crossb916a382016-07-29 17:28:03 -0700510 if c.isDependencyRoot() && c.sanitize.Sanitizer(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700511 modules := mctx.CreateVariations(t.String())
512 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Colin Cross16b23492016-01-06 14:41:07 -0800513 } else if c.sanitize.Properties.SanitizeDep {
Colin Crossb0f28952016-09-19 16:46:53 -0700514 modules := mctx.CreateVariations("", t.String())
515 modules[0].(*Module).sanitize.SetSanitizer(t, false)
516 modules[1].(*Module).sanitize.SetSanitizer(t, true)
517 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
518 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
519 if mctx.Device() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700520 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
Colin Crossb0f28952016-09-19 16:46:53 -0700521 } else {
522 modules[0].(*Module).Properties.PreventInstall = true
523 }
524 if mctx.AConfig().EmbeddedInMake() {
525 modules[0].(*Module).Properties.HideFromMake = true
Colin Cross30d5f512016-05-03 18:02:42 -0700526 }
Colin Cross16b23492016-01-06 14:41:07 -0800527 }
528 c.sanitize.Properties.SanitizeDep = false
529 }
530 }
531}