blob: fcea4efb05bc98377f4aef5a77fa0dad26fb45e0 [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"
Vishwath Mohane7128792017-11-17 11:08:10 -080021 "sync"
Colin Cross16b23492016-01-06 14:41:07 -080022
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
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070027var (
28 // Any C flags added by sanitizer which libTooling tools may not
29 // understand also need to be added to ClangLibToolingUnknownCflags in
30 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080031
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070032 asanCflags = []string{"-fno-omit-frame-pointer"}
33 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
34 asanLibs = []string{"libasan"}
35
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000036 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070037 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070038 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070039 "-Wl,-plugin-opt,O1"}
Vishwath Mohane7128792017-11-17 11:08:10 -080040 cfiArflags = []string{"--plugin ${config.ClangBin}/../lib64/LLVMgold.so"}
41 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
42 cfiExportsMap android.Path
43 cfiStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070044
45 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070046)
47
Colin Cross16b23492016-01-06 14:41:07 -080048type sanitizerType int
49
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070050func boolPtr(v bool) *bool {
51 if v {
52 return &v
53 } else {
54 return nil
55 }
56}
57
Colin Cross16b23492016-01-06 14:41:07 -080058const (
59 asan sanitizerType = iota + 1
60 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070061 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000062 cfi
Colin Cross16b23492016-01-06 14:41:07 -080063)
64
65func (t sanitizerType) String() string {
66 switch t {
67 case asan:
68 return "asan"
69 case tsan:
70 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070071 case intOverflow:
72 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000073 case cfi:
74 return "cfi"
Colin Cross16b23492016-01-06 14:41:07 -080075 default:
76 panic(fmt.Errorf("unknown sanitizerType %d", t))
77 }
78}
79
80type SanitizeProperties struct {
81 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
82 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -080083 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080084
85 // main sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070086 Address *bool `android:"arch_variant"`
87 Thread *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080088
89 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070090 Undefined *bool `android:"arch_variant"`
91 All_undefined *bool `android:"arch_variant"`
92 Misc_undefined []string `android:"arch_variant"`
93 Coverage *bool `android:"arch_variant"`
94 Safestack *bool `android:"arch_variant"`
95 Cfi *bool `android:"arch_variant"`
96 Integer_overflow *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080097
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070098 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
99 // Replaces abort() on error with a human-readable error message.
100 // Address and Thread sanitizers always run in diagnostic mode.
101 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700102 Undefined *bool `android:"arch_variant"`
103 Cfi *bool `android:"arch_variant"`
104 Integer_overflow *bool `android:"arch_variant"`
105 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700106 }
107
108 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800109 Recover []string
110
111 // value to pass to -fsanitize-blacklist
112 Blacklist *string
113 } `android:"arch_variant"`
114
115 SanitizerEnabled bool `blueprint:"mutated"`
116 SanitizeDep bool `blueprint:"mutated"`
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700117 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800118}
119
120type sanitize struct {
121 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700122
Jiyong Park27b188b2017-07-18 13:23:39 +0900123 runtimeLibrary string
124 androidMkRuntimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800125}
126
Vishwath Mohane7128792017-11-17 11:08:10 -0800127func init() {
128 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
129}
130
Colin Cross16b23492016-01-06 14:41:07 -0800131func (sanitize *sanitize) props() []interface{} {
132 return []interface{}{&sanitize.Properties}
133}
134
135func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700136 s := &sanitize.Properties.Sanitize
137
Colin Cross16b23492016-01-06 14:41:07 -0800138 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700139 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800140 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800141 }
142
143 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800144 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800145 return
146 }
147
Colin Cross16b23492016-01-06 14:41:07 -0800148 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700149 var globalSanitizersDiag []string
150
Colin Cross16b23492016-01-06 14:41:07 -0800151 if ctx.clang() {
152 if ctx.Host() {
153 globalSanitizers = ctx.AConfig().SanitizeHost()
154 } else {
Colin Cross23ae82a2016-11-02 14:34:39 -0700155 arches := ctx.AConfig().SanitizeDeviceArch()
156 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
157 globalSanitizers = ctx.AConfig().SanitizeDevice()
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700158 globalSanitizersDiag = ctx.AConfig().SanitizeDeviceDiag()
Colin Cross23ae82a2016-11-02 14:34:39 -0700159 }
Colin Cross16b23492016-01-06 14:41:07 -0800160 }
161 }
162
Colin Cross16b23492016-01-06 14:41:07 -0800163 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000164 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700165 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
166 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000167 }
Colin Cross16b23492016-01-06 14:41:07 -0800168
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700169 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
170 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000171 }
172
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800173 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
174 if s.Address == nil {
175 s.Address = boolPtr(true)
176 } else if *s.Address == false {
177 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
178 // disables address, then disable coverage as well.
179 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
180 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000181 }
182
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700183 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
184 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000185 }
186
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700187 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
188 s.Coverage = boolPtr(true)
189 }
190
191 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
192 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000193 }
194
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700195 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700196 if !ctx.AConfig().CFIDisabledForPath(ctx.ModuleDir()) {
197 s.Cfi = boolPtr(true)
198 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700199 }
200
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700201 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozano5f595532017-07-13 14:46:05 -0700202 if !ctx.AConfig().IntegerOverflowDisabledForPath(ctx.ModuleDir()) {
203 s.Integer_overflow = boolPtr(true)
204 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700205 }
206
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000207 if len(globalSanitizers) > 0 {
208 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
209 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700210
211 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
212 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) {
213 s.Diag.Integer_overflow = boolPtr(true)
214 }
215
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700216 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
217 s.Diag.Cfi == nil && Bool(s.Cfi) {
218 s.Diag.Cfi = boolPtr(true)
219 }
220
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700221 if len(globalSanitizersDiag) > 0 {
222 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
223 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700224 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700225
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700226 // Enable CFI for all components in the include paths
227 if s.Cfi == nil && ctx.AConfig().CFIEnabledForPath(ctx.ModuleDir()) {
228 s.Cfi = boolPtr(true)
229 if inList("cfi", ctx.AConfig().SanitizeDeviceDiag()) {
230 s.Diag.Cfi = boolPtr(true)
231 }
232 }
233
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800234 // CFI needs gold linker, and mips toolchain does not have one.
235 if !ctx.AConfig().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800236 s.Cfi = nil
237 s.Diag.Cfi = nil
238 }
239
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800240 // Also disable CFI for arm32 until b/35157333 is fixed.
241 if ctx.Arch().ArchType == android.Arm {
242 s.Cfi = nil
243 s.Diag.Cfi = nil
244 }
245
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700246 // Also disable CFI if ASAN is enabled.
247 if Bool(s.Address) {
248 s.Cfi = nil
249 s.Diag.Cfi = nil
250 }
251
Vishwath Mohane7128792017-11-17 11:08:10 -0800252 // Also disable CFI for host builds.
253 if ctx.Host() {
254 s.Cfi = nil
255 s.Diag.Cfi = nil
256 }
257
Colin Cross3c344ef2016-07-18 15:44:56 -0700258 if ctx.staticBinary() {
259 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700260 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700261 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800262 }
263
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700264 if Bool(s.All_undefined) {
265 s.Undefined = nil
266 }
267
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700268 if !ctx.toolchain().Is64Bit() {
269 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700270 s.Thread = nil
271 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800272 // TODO(ccross): error for compile_multilib = "32"?
273 }
274
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800275 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 -0700276 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 -0700277 sanitize.Properties.SanitizerEnabled = true
278 }
279
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700280 if Bool(s.Coverage) {
281 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800282 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
283 }
284 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000285
286 cfiExportsMap = android.PathForSource(ctx, cfiExportsMapPath)
Colin Cross16b23492016-01-06 14:41:07 -0800287}
288
289func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
290 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
291 return deps
292 }
293
294 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700295 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700296 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800297 }
298 }
299
300 return deps
301}
302
303func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
304 if !sanitize.Properties.SanitizerEnabled {
305 return flags
306 }
307
308 if !ctx.clang() {
309 ctx.ModuleErrorf("Use of sanitizers requires clang")
310 }
311
312 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700313 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800314
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700315 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800316 sanitizers = append(sanitizers, "undefined")
Colin Cross16b23492016-01-06 14:41:07 -0800317 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700318 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800319 sanitizers = append(sanitizers,
320 "bool",
321 "integer-divide-by-zero",
322 "return",
323 "returns-nonnull-attribute",
324 "shift-exponent",
325 "unreachable",
326 "vla-bound",
327 // TODO(danalbert): The following checks currently have compiler performance issues.
328 //"alignment",
329 //"bounds",
330 //"enum",
331 //"float-cast-overflow",
332 //"float-divide-by-zero",
333 //"nonnull-attribute",
334 //"null",
335 //"shift-base",
336 //"signed-integer-overflow",
337 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
338 // https://llvm.org/PR19302
339 // http://reviews.llvm.org/D6974
340 // "object-size",
341 )
342 }
343 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
344 }
345
Ivan Lozano651275b2017-06-13 10:24:34 -0700346 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700347 diagSanitizers = append(diagSanitizers, "undefined")
348 }
349
Ivan Lozano651275b2017-06-13 10:24:34 -0700350 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
351
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700352 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700353 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800354 // Frame pointer based unwinder in ASan requires ARM frame setup.
355 // TODO: put in flags?
356 flags.RequiredInstructionSet = "arm"
357 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700358 flags.CFlags = append(flags.CFlags, asanCflags...)
359 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800360
Colin Cross16b23492016-01-06 14:41:07 -0800361 if ctx.Host() {
362 // -nodefaultlibs (provided with libc++) prevents the driver from linking
363 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800364 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
365 } else {
366 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
367 flags.DynamicLinker = "/system/bin/linker_asan"
368 if flags.Toolchain.Is64Bit() {
369 flags.DynamicLinker += "64"
370 }
371 }
372 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700373 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800374 }
375
Yabin Cui6be405e2017-10-19 15:52:11 -0700376 if Bool(sanitize.Properties.Sanitize.Thread) {
377 sanitizers = append(sanitizers, "thread")
378 }
379
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700380 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400381 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800382 }
383
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700384 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700385 sanitizers = append(sanitizers, "safe-stack")
386 }
387
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700388 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800389 if ctx.Arch().ArchType == android.Arm {
390 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
391 // to do this on a function basis, so force Thumb on the entire module.
392 flags.RequiredInstructionSet = "thumb"
393 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700394 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000395
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700396 flags.CFlags = append(flags.CFlags, cfiCflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000397 // Only append the default visibility flag if -fvisibility has not already been set
398 // to hidden.
399 if !inList("-fvisibility=hidden", flags.CFlags) {
400 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
401 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700402 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
403 flags.ArFlags = append(flags.ArFlags, cfiArflags...)
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700404 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
405 diagSanitizers = append(diagSanitizers, "cfi")
406 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000407
408 if ctx.staticBinary() {
409 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
410 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
411 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700412 }
413
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700414 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
415 if !ctx.static() {
416 sanitizers = append(sanitizers, "unsigned-integer-overflow")
417 sanitizers = append(sanitizers, "signed-integer-overflow")
418 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
419 if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) {
420 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
421 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
422 }
423 }
424 }
425
Colin Cross16b23492016-01-06 14:41:07 -0800426 if len(sanitizers) > 0 {
427 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
428 flags.CFlags = append(flags.CFlags, sanitizeArg)
429 if ctx.Host() {
430 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
431 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800432 // Host sanitizers only link symbols in the final executable, so
433 // there will always be undefined symbols in intermediate libraries.
434 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800435 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700436 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Colin Cross16b23492016-01-06 14:41:07 -0800437 }
438 }
439
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700440 if len(diagSanitizers) > 0 {
441 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
442 }
443 // FIXME: enable RTTI if diag + (cfi or vptr)
444
Andreas Gampe97071162017-05-08 13:15:23 -0700445 if sanitize.Properties.Sanitize.Recover != nil {
446 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
447 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
448 }
449
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700450 // Link a runtime library if needed.
451 runtimeLibrary := ""
452 if Bool(sanitize.Properties.Sanitize.Address) {
453 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Yabin Cui6be405e2017-10-19 15:52:11 -0700454 } else if Bool(sanitize.Properties.Sanitize.Thread) {
455 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain())
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700456 } else if len(diagSanitizers) > 0 {
457 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
458 }
459
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700460 if runtimeLibrary != "" {
Jiyong Park27b188b2017-07-18 13:23:39 +0900461 // ASan runtime library must be the first in the link order.
Colin Cross8ff9ef42017-05-08 13:44:11 -0700462 flags.libFlags = append([]string{
463 "${config.ClangAsanLibDir}/" + runtimeLibrary + ctx.toolchain().ShlibSuffix(),
464 }, flags.libFlags...)
465 sanitize.runtimeLibrary = runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900466
467 // When linking against VNDK, use the vendor variant of the runtime lib
468 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700469 if ctx.useVndk() {
Jiyong Park27b188b2017-07-18 13:23:39 +0900470 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix
471 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700472 }
473
Colin Cross635c3b02016-05-18 15:37:25 -0700474 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800475 if blacklist.Valid() {
476 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
477 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
478 }
479
480 return flags
481}
482
Colin Cross8ff9ef42017-05-08 13:44:11 -0700483func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross27a4b052017-08-10 16:32:23 -0700484 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Jiyong Park27b188b2017-07-18 13:23:39 +0900485 if sanitize.androidMkRuntimeLibrary != "" {
486 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700487 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700488 })
Vishwath Mohane7128792017-11-17 11:08:10 -0800489
490 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
491 // name conflict.
492 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
493 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000494 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700495}
496
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700497func (sanitize *sanitize) inSanitizerDir() bool {
498 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700499}
500
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000501func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000502 switch t {
503 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000504 return sanitize.Properties.Sanitize.Address
Vishwath Mohan95229302017-08-11 00:53:16 +0000505 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000506 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000507 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000508 return sanitize.Properties.Sanitize.Integer_overflow
509 case cfi:
510 return sanitize.Properties.Sanitize.Cfi
Vishwath Mohan95229302017-08-11 00:53:16 +0000511 default:
512 panic(fmt.Errorf("unknown sanitizerType %d", t))
513 }
514}
515
Colin Cross16b23492016-01-06 14:41:07 -0800516func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
517 switch t {
518 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700519 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700520 if !b {
521 sanitize.Properties.Sanitize.Coverage = nil
522 }
Colin Cross16b23492016-01-06 14:41:07 -0800523 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700524 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700525 case intOverflow:
526 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000527 case cfi:
528 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
529 sanitize.Properties.Sanitize.Diag.Cfi = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800530 default:
531 panic(fmt.Errorf("unknown sanitizerType %d", t))
532 }
533 if b {
534 sanitize.Properties.SanitizerEnabled = true
535 }
536}
537
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000538// Check if the sanitizer is explicitly disabled (as opposed to nil by
539// virtue of not being set).
540func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
541 if sanitize == nil {
542 return false
543 }
544
545 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
546 return sanitizerVal != nil && *sanitizerVal == false
547}
548
549// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
550// because enabling a sanitizer either directly (via the blueprint) or
551// indirectly (via a mutator) sets the bool ptr to true, and you can't
552// distinguish between the cases. It isn't needed though - both cases can be
553// treated identically.
554func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
555 if sanitize == nil {
556 return false
557 }
558
559 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
560 return sanitizerVal != nil && *sanitizerVal == true
561}
562
Colin Cross16b23492016-01-06 14:41:07 -0800563// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700564func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
565 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000566 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Crossd11fcda2017-10-23 17:59:01 -0700567 mctx.VisitDepsDepthFirst(func(module android.Module) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000568 if d, ok := module.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800569 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000570 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
571 if (t == cfi && d.static()) || t != cfi {
572 d.sanitize.Properties.SanitizeDep = true
573 }
Colin Cross16b23492016-01-06 14:41:07 -0800574 }
575 })
576 }
577 }
578}
579
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000580// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700581func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
582 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000583 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000584 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700585 modules := mctx.CreateVariations(t.String())
586 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000587 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
588 // Save original sanitizer status before we assign values to variant
589 // 0 as that overwrites the original.
590 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
591
Colin Crossb0f28952016-09-19 16:46:53 -0700592 modules := mctx.CreateVariations("", t.String())
593 modules[0].(*Module).sanitize.SetSanitizer(t, false)
594 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000595
Colin Crossb0f28952016-09-19 16:46:53 -0700596 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
597 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800598
599 // We don't need both variants active for anything but CFI-enabled
600 // target static libraries, so suppress the appropriate variant in
601 // all other cases.
602 if t == cfi {
603 if c.static() {
604 if !mctx.Device() {
605 if isSanitizerEnabled {
606 modules[0].(*Module).Properties.PreventInstall = true
607 modules[0].(*Module).Properties.HideFromMake = true
608 } else {
609 modules[1].(*Module).Properties.PreventInstall = true
610 modules[1].(*Module).Properties.HideFromMake = true
611 }
612 } else {
613 cfiStaticLibs := cfiStaticLibs(mctx.AConfig())
614
615 cfiStaticLibsMutex.Lock()
616 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
617 cfiStaticLibsMutex.Unlock()
618 }
619 } else {
620 modules[0].(*Module).Properties.PreventInstall = true
621 modules[0].(*Module).Properties.HideFromMake = true
622 }
623 } else if t == asan {
624 if mctx.Device() {
625 // CFI and ASAN are currently mutually exclusive so disable
626 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000627 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
628 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
629 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700630 if isSanitizerEnabled {
631 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800632 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700633 } else {
634 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800635 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700636 }
637 }
Colin Cross16b23492016-01-06 14:41:07 -0800638 }
639 c.sanitize.Properties.SanitizeDep = false
640 }
641 }
642}
Vishwath Mohane7128792017-11-17 11:08:10 -0800643
644func cfiStaticLibs(config android.Config) *[]string {
645 return config.Once("cfiStaticLibs", func() interface{} {
646 return &[]string{}
647 }).(*[]string)
648}
649
650func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
651 cfiStaticLibs := cfiStaticLibs(ctx.Config())
652 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
653}