blob: 9e1f02f7c5327b9174b2bcd7d7641153c9bd93c6 [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
Colin Cross635c3b02016-05-18 15:37:25 -070022 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070023 "android/soong/cc/config"
Colin Cross16b23492016-01-06 14:41:07 -080024)
25
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070026var (
27 // Any C flags added by sanitizer which libTooling tools may not
28 // understand also need to be added to ClangLibToolingUnknownCflags in
29 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080030
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070031 asanCflags = []string{"-fno-omit-frame-pointer"}
32 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
33 asanLibs = []string{"libasan"}
34
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000035 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070036 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Vishwath Mohanf3918d32017-02-14 07:59:33 -080037 // FIXME: revert the __cfi_check flag when clang is updated to r280031.
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070038 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
39 "-Wl,-plugin-opt,O1 -Wl,-export-dynamic-symbol=__cfi_check"}
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000040 cfiArflags = []string{"--plugin ${config.ClangBin}/../lib64/LLVMgold.so"}
41 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
42 cfiExportsMap android.Path
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
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000061 cfi
Colin Cross16b23492016-01-06 14:41:07 -080062)
63
64func (t sanitizerType) String() string {
65 switch t {
66 case asan:
67 return "asan"
68 case tsan:
69 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070070 case intOverflow:
71 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000072 case cfi:
73 return "cfi"
Colin Cross16b23492016-01-06 14:41:07 -080074 default:
75 panic(fmt.Errorf("unknown sanitizerType %d", t))
76 }
77}
78
79type SanitizeProperties struct {
80 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
81 Sanitize struct {
82 Never bool `android:"arch_variant"`
83
84 // main sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070085 Address *bool `android:"arch_variant"`
86 Thread *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080087
88 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070089 Undefined *bool `android:"arch_variant"`
90 All_undefined *bool `android:"arch_variant"`
91 Misc_undefined []string `android:"arch_variant"`
92 Coverage *bool `android:"arch_variant"`
93 Safestack *bool `android:"arch_variant"`
94 Cfi *bool `android:"arch_variant"`
95 Integer_overflow *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080096
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070097 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
98 // Replaces abort() on error with a human-readable error message.
99 // Address and Thread sanitizers always run in diagnostic mode.
100 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700101 Undefined *bool `android:"arch_variant"`
102 Cfi *bool `android:"arch_variant"`
103 Integer_overflow *bool `android:"arch_variant"`
104 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700105 }
106
107 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800108 Recover []string
109
110 // value to pass to -fsanitize-blacklist
111 Blacklist *string
112 } `android:"arch_variant"`
113
114 SanitizerEnabled bool `blueprint:"mutated"`
115 SanitizeDep bool `blueprint:"mutated"`
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700116 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800117}
118
119type sanitize struct {
120 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700121
Jiyong Park27b188b2017-07-18 13:23:39 +0900122 runtimeLibrary string
123 androidMkRuntimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800124}
125
126func (sanitize *sanitize) props() []interface{} {
127 return []interface{}{&sanitize.Properties}
128}
129
130func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700131 s := &sanitize.Properties.Sanitize
132
Colin Cross16b23492016-01-06 14:41:07 -0800133 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700134 if ctx.useSdk() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700135 s.Never = true
Colin Cross16b23492016-01-06 14:41:07 -0800136 }
137
138 // Never always wins.
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700139 if s.Never {
Colin Cross16b23492016-01-06 14:41:07 -0800140 return
141 }
142
Colin Cross16b23492016-01-06 14:41:07 -0800143 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700144 var globalSanitizersDiag []string
145
Colin Cross16b23492016-01-06 14:41:07 -0800146 if ctx.clang() {
147 if ctx.Host() {
148 globalSanitizers = ctx.AConfig().SanitizeHost()
149 } else {
Colin Cross23ae82a2016-11-02 14:34:39 -0700150 arches := ctx.AConfig().SanitizeDeviceArch()
151 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
152 globalSanitizers = ctx.AConfig().SanitizeDevice()
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700153 globalSanitizersDiag = ctx.AConfig().SanitizeDeviceDiag()
Colin Cross23ae82a2016-11-02 14:34:39 -0700154 }
Colin Cross16b23492016-01-06 14:41:07 -0800155 }
156 }
157
Colin Cross16b23492016-01-06 14:41:07 -0800158 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000159 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700160 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
161 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000162 }
Colin Cross16b23492016-01-06 14:41:07 -0800163
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700164 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
165 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000166 }
167
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800168 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
169 if s.Address == nil {
170 s.Address = boolPtr(true)
171 } else if *s.Address == false {
172 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
173 // disables address, then disable coverage as well.
174 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
175 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000176 }
177
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700178 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
179 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000180 }
181
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700182 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
183 s.Coverage = boolPtr(true)
184 }
185
186 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
187 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000188 }
189
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700190 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
191 s.Cfi = boolPtr(true)
192 }
193
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700194 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozano5f595532017-07-13 14:46:05 -0700195 if !ctx.AConfig().IntegerOverflowDisabledForPath(ctx.ModuleDir()) {
196 s.Integer_overflow = boolPtr(true)
197 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700198 }
199
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000200 if len(globalSanitizers) > 0 {
201 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
202 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700203
204 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
205 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) {
206 s.Diag.Integer_overflow = boolPtr(true)
207 }
208
209 if len(globalSanitizersDiag) > 0 {
210 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
211 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700212 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700213
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800214 // CFI needs gold linker, and mips toolchain does not have one.
215 if !ctx.AConfig().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800216 s.Cfi = nil
217 s.Diag.Cfi = nil
218 }
219
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800220 // Also disable CFI for arm32 until b/35157333 is fixed.
221 if ctx.Arch().ArchType == android.Arm {
222 s.Cfi = nil
223 s.Diag.Cfi = nil
224 }
225
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700226 // Also disable CFI if ASAN is enabled.
227 if Bool(s.Address) {
228 s.Cfi = nil
229 s.Diag.Cfi = nil
230 }
231
Colin Cross3c344ef2016-07-18 15:44:56 -0700232 if ctx.staticBinary() {
233 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700234 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700235 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800236 }
237
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700238 if Bool(s.All_undefined) {
239 s.Undefined = nil
240 }
241
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700242 if !ctx.toolchain().Is64Bit() {
243 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700244 s.Thread = nil
245 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800246 // TODO(ccross): error for compile_multilib = "32"?
247 }
248
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800249 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 -0700250 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 -0700251 sanitize.Properties.SanitizerEnabled = true
252 }
253
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700254 if Bool(s.Coverage) {
255 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800256 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
257 }
258 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000259
260 cfiExportsMap = android.PathForSource(ctx, cfiExportsMapPath)
Colin Cross16b23492016-01-06 14:41:07 -0800261}
262
263func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
264 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
265 return deps
266 }
267
268 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700269 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700270 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800271 }
272 }
273
274 return deps
275}
276
277func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
278 if !sanitize.Properties.SanitizerEnabled {
279 return flags
280 }
281
282 if !ctx.clang() {
283 ctx.ModuleErrorf("Use of sanitizers requires clang")
284 }
285
286 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700287 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800288
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700289 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800290 sanitizers = append(sanitizers, "undefined")
Colin Cross16b23492016-01-06 14:41:07 -0800291 } 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)
Colin Cross16b23492016-01-06 14:41:07 -0800338 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
339 } else {
340 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
341 flags.DynamicLinker = "/system/bin/linker_asan"
342 if flags.Toolchain.Is64Bit() {
343 flags.DynamicLinker += "64"
344 }
345 }
346 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700347 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800348 }
349
Yabin Cui6be405e2017-10-19 15:52:11 -0700350 if Bool(sanitize.Properties.Sanitize.Thread) {
351 sanitizers = append(sanitizers, "thread")
352 }
353
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700354 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400355 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800356 }
357
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700358 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700359 sanitizers = append(sanitizers, "safe-stack")
360 }
361
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700362 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800363 if ctx.Arch().ArchType == android.Arm {
364 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
365 // to do this on a function basis, so force Thumb on the entire module.
366 flags.RequiredInstructionSet = "thumb"
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800367 // Workaround for b/33678192. CFI jumptables need Thumb2 codegen. Revert when
368 // Clang is updated past r290384.
369 flags.LdFlags = append(flags.LdFlags, "-march=armv7-a")
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800370 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700371 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000372
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700373 flags.CFlags = append(flags.CFlags, cfiCflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000374 // Only append the default visibility flag if -fvisibility has not already been set
375 // to hidden.
376 if !inList("-fvisibility=hidden", flags.CFlags) {
377 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
378 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700379 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
380 flags.ArFlags = append(flags.ArFlags, cfiArflags...)
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700381 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
382 diagSanitizers = append(diagSanitizers, "cfi")
383 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000384
385 if ctx.staticBinary() {
386 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
387 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
388 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700389 }
390
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700391 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
392 if !ctx.static() {
393 sanitizers = append(sanitizers, "unsigned-integer-overflow")
394 sanitizers = append(sanitizers, "signed-integer-overflow")
395 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
396 if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) {
397 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
398 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
399 }
400 }
401 }
402
Colin Cross16b23492016-01-06 14:41:07 -0800403 if len(sanitizers) > 0 {
404 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
405 flags.CFlags = append(flags.CFlags, sanitizeArg)
406 if ctx.Host() {
407 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
408 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800409 // Host sanitizers only link symbols in the final executable, so
410 // there will always be undefined symbols in intermediate libraries.
411 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800412 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700413 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Colin Cross16b23492016-01-06 14:41:07 -0800414 }
415 }
416
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700417 if len(diagSanitizers) > 0 {
418 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
419 }
420 // FIXME: enable RTTI if diag + (cfi or vptr)
421
Andreas Gampe97071162017-05-08 13:15:23 -0700422 if sanitize.Properties.Sanitize.Recover != nil {
423 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
424 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
425 }
426
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700427 // Link a runtime library if needed.
428 runtimeLibrary := ""
429 if Bool(sanitize.Properties.Sanitize.Address) {
430 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Yabin Cui6be405e2017-10-19 15:52:11 -0700431 } else if Bool(sanitize.Properties.Sanitize.Thread) {
432 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain())
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700433 } else if len(diagSanitizers) > 0 {
434 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
435 }
436
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700437 if runtimeLibrary != "" {
Jiyong Park27b188b2017-07-18 13:23:39 +0900438 // ASan runtime library must be the first in the link order.
Colin Cross8ff9ef42017-05-08 13:44:11 -0700439 flags.libFlags = append([]string{
440 "${config.ClangAsanLibDir}/" + runtimeLibrary + ctx.toolchain().ShlibSuffix(),
441 }, flags.libFlags...)
442 sanitize.runtimeLibrary = runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900443
444 // When linking against VNDK, use the vendor variant of the runtime lib
445 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700446 if ctx.useVndk() {
Jiyong Park27b188b2017-07-18 13:23:39 +0900447 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix
448 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700449 }
450
Colin Cross635c3b02016-05-18 15:37:25 -0700451 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800452 if blacklist.Valid() {
453 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
454 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
455 }
456
457 return flags
458}
459
Colin Cross8ff9ef42017-05-08 13:44:11 -0700460func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross27a4b052017-08-10 16:32:23 -0700461 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Jiyong Park27b188b2017-07-18 13:23:39 +0900462 if sanitize.androidMkRuntimeLibrary != "" {
463 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700464 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700465 })
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000466 if ctx.Target().Os.Class == android.Device {
467 if Bool(sanitize.Properties.Sanitize.Cfi) {
468 ret.SubName += ".cfi"
469 } else if Bool(sanitize.Properties.Sanitize.Address) {
470 ret.SubName += ".asan"
471 }
472 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700473}
474
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700475func (sanitize *sanitize) inSanitizerDir() bool {
476 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700477}
478
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000479func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000480 switch t {
481 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000482 return sanitize.Properties.Sanitize.Address
Vishwath Mohan95229302017-08-11 00:53:16 +0000483 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000484 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000485 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000486 return sanitize.Properties.Sanitize.Integer_overflow
487 case cfi:
488 return sanitize.Properties.Sanitize.Cfi
Vishwath Mohan95229302017-08-11 00:53:16 +0000489 default:
490 panic(fmt.Errorf("unknown sanitizerType %d", t))
491 }
492}
493
Colin Cross16b23492016-01-06 14:41:07 -0800494func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
495 switch t {
496 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700497 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700498 if !b {
499 sanitize.Properties.Sanitize.Coverage = nil
500 }
Colin Cross16b23492016-01-06 14:41:07 -0800501 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700502 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700503 case intOverflow:
504 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000505 case cfi:
506 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
507 sanitize.Properties.Sanitize.Diag.Cfi = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800508 default:
509 panic(fmt.Errorf("unknown sanitizerType %d", t))
510 }
511 if b {
512 sanitize.Properties.SanitizerEnabled = true
513 }
514}
515
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000516// Check if the sanitizer is explicitly disabled (as opposed to nil by
517// virtue of not being set).
518func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
519 if sanitize == nil {
520 return false
521 }
522
523 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
524 return sanitizerVal != nil && *sanitizerVal == false
525}
526
527// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
528// because enabling a sanitizer either directly (via the blueprint) or
529// indirectly (via a mutator) sets the bool ptr to true, and you can't
530// distinguish between the cases. It isn't needed though - both cases can be
531// treated identically.
532func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
533 if sanitize == nil {
534 return false
535 }
536
537 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
538 return sanitizerVal != nil && *sanitizerVal == true
539}
540
Colin Cross16b23492016-01-06 14:41:07 -0800541// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700542func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
543 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000544 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Crossd11fcda2017-10-23 17:59:01 -0700545 mctx.VisitDepsDepthFirst(func(module android.Module) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000546 if d, ok := module.(*Module); ok && d.sanitize != nil &&
547 !d.sanitize.Properties.Sanitize.Never &&
548 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
549 if (t == cfi && d.static()) || t != cfi {
550 d.sanitize.Properties.SanitizeDep = true
551 }
Colin Cross16b23492016-01-06 14:41:07 -0800552 }
553 })
554 }
555 }
556}
557
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000558// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700559func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
560 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000561 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000562 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700563 modules := mctx.CreateVariations(t.String())
564 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000565 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
566 // Save original sanitizer status before we assign values to variant
567 // 0 as that overwrites the original.
568 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
569
Colin Crossb0f28952016-09-19 16:46:53 -0700570 modules := mctx.CreateVariations("", t.String())
571 modules[0].(*Module).sanitize.SetSanitizer(t, false)
572 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000573
Colin Crossb0f28952016-09-19 16:46:53 -0700574 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
575 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane6153452017-08-11 00:52:44 +0000576 if mctx.Device() {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000577 // CFI and ASAN are currently mutually exclusive so disable
578 // CFI if this is an ASAN variant.
579 if t == asan {
580 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
581 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
582 }
Orion Hodsonda11d742017-10-31 17:31:00 +0000583 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000584 if mctx.AConfig().EmbeddedInMake() {
585 if isSanitizerEnabled {
586 modules[0].(*Module).Properties.HideFromMake = true
587 } else {
588 modules[1].(*Module).Properties.HideFromMake = true
589 }
590 }
Orion Hodsonda11d742017-10-31 17:31:00 +0000591 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700592 if !mctx.AConfig().EmbeddedInMake() || !mctx.Device() {
593 if isSanitizerEnabled {
594 modules[0].(*Module).Properties.PreventInstall = true
595 } else {
596 modules[1].(*Module).Properties.PreventInstall = true
597 }
598 }
Colin Cross16b23492016-01-06 14:41:07 -0800599 }
600 c.sanitize.Properties.SanitizeDep = false
601 }
602 }
603}