blob: aeaaf16fabcd32ed11b016c8364798bc653a4bc7 [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"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070037 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070038 "-Wl,-plugin-opt,O1"}
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000039 cfiArflags = []string{"--plugin ${config.ClangBin}/../lib64/LLVMgold.so"}
40 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
41 cfiExportsMap android.Path
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070042
43 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070044)
45
Colin Cross16b23492016-01-06 14:41:07 -080046type sanitizerType int
47
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070048func boolPtr(v bool) *bool {
49 if v {
50 return &v
51 } else {
52 return nil
53 }
54}
55
Colin Cross16b23492016-01-06 14:41:07 -080056const (
57 asan sanitizerType = iota + 1
58 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070059 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000060 cfi
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"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000071 case cfi:
72 return "cfi"
Colin Cross16b23492016-01-06 14:41:07 -080073 default:
74 panic(fmt.Errorf("unknown sanitizerType %d", t))
75 }
76}
77
78type SanitizeProperties struct {
79 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
80 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -080081 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080082
83 // main sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070084 Address *bool `android:"arch_variant"`
85 Thread *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080086
87 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070088 Undefined *bool `android:"arch_variant"`
89 All_undefined *bool `android:"arch_variant"`
90 Misc_undefined []string `android:"arch_variant"`
91 Coverage *bool `android:"arch_variant"`
92 Safestack *bool `android:"arch_variant"`
93 Cfi *bool `android:"arch_variant"`
94 Integer_overflow *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080095
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070096 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
97 // Replaces abort() on error with a human-readable error message.
98 // Address and Thread sanitizers always run in diagnostic mode.
99 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700100 Undefined *bool `android:"arch_variant"`
101 Cfi *bool `android:"arch_variant"`
102 Integer_overflow *bool `android:"arch_variant"`
103 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700104 }
105
106 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800107 Recover []string
108
109 // value to pass to -fsanitize-blacklist
110 Blacklist *string
111 } `android:"arch_variant"`
112
113 SanitizerEnabled bool `blueprint:"mutated"`
114 SanitizeDep bool `blueprint:"mutated"`
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700115 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800116}
117
118type sanitize struct {
119 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700120
Jiyong Park27b188b2017-07-18 13:23:39 +0900121 runtimeLibrary string
122 androidMkRuntimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800123}
124
125func (sanitize *sanitize) props() []interface{} {
126 return []interface{}{&sanitize.Properties}
127}
128
129func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700130 s := &sanitize.Properties.Sanitize
131
Colin Cross16b23492016-01-06 14:41:07 -0800132 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700133 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800134 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800135 }
136
137 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800138 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800139 return
140 }
141
Colin Cross16b23492016-01-06 14:41:07 -0800142 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700143 var globalSanitizersDiag []string
144
Colin Cross16b23492016-01-06 14:41:07 -0800145 if ctx.clang() {
146 if ctx.Host() {
147 globalSanitizers = ctx.AConfig().SanitizeHost()
148 } else {
Colin Cross23ae82a2016-11-02 14:34:39 -0700149 arches := ctx.AConfig().SanitizeDeviceArch()
150 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
151 globalSanitizers = ctx.AConfig().SanitizeDevice()
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700152 globalSanitizersDiag = ctx.AConfig().SanitizeDeviceDiag()
Colin Cross23ae82a2016-11-02 14:34:39 -0700153 }
Colin Cross16b23492016-01-06 14:41:07 -0800154 }
155 }
156
Colin Cross16b23492016-01-06 14:41:07 -0800157 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000158 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700159 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
160 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000161 }
Colin Cross16b23492016-01-06 14:41:07 -0800162
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700163 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
164 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000165 }
166
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800167 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
168 if s.Address == nil {
169 s.Address = boolPtr(true)
170 } else if *s.Address == false {
171 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
172 // disables address, then disable coverage as well.
173 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
174 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000175 }
176
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700177 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
178 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000179 }
180
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700181 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
182 s.Coverage = boolPtr(true)
183 }
184
185 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
186 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000187 }
188
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700189 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
190 s.Cfi = boolPtr(true)
191 }
192
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700193 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozano5f595532017-07-13 14:46:05 -0700194 if !ctx.AConfig().IntegerOverflowDisabledForPath(ctx.ModuleDir()) {
195 s.Integer_overflow = boolPtr(true)
196 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700197 }
198
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000199 if len(globalSanitizers) > 0 {
200 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
201 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700202
203 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
204 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) {
205 s.Diag.Integer_overflow = boolPtr(true)
206 }
207
208 if len(globalSanitizersDiag) > 0 {
209 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
210 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700211 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700212
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800213 // CFI needs gold linker, and mips toolchain does not have one.
214 if !ctx.AConfig().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800215 s.Cfi = nil
216 s.Diag.Cfi = nil
217 }
218
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800219 // Also disable CFI for arm32 until b/35157333 is fixed.
220 if ctx.Arch().ArchType == android.Arm {
221 s.Cfi = nil
222 s.Diag.Cfi = nil
223 }
224
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700225 // Also disable CFI if ASAN is enabled.
226 if Bool(s.Address) {
227 s.Cfi = nil
228 s.Diag.Cfi = nil
229 }
230
Colin Cross3c344ef2016-07-18 15:44:56 -0700231 if ctx.staticBinary() {
232 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700233 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700234 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800235 }
236
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700237 if Bool(s.All_undefined) {
238 s.Undefined = nil
239 }
240
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700241 if !ctx.toolchain().Is64Bit() {
242 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700243 s.Thread = nil
244 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800245 // TODO(ccross): error for compile_multilib = "32"?
246 }
247
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800248 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 -0700249 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 -0700250 sanitize.Properties.SanitizerEnabled = true
251 }
252
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700253 if Bool(s.Coverage) {
254 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800255 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
256 }
257 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000258
259 cfiExportsMap = android.PathForSource(ctx, cfiExportsMapPath)
Colin Cross16b23492016-01-06 14:41:07 -0800260}
261
262func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
263 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
264 return deps
265 }
266
267 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700268 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700269 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800270 }
271 }
272
273 return deps
274}
275
276func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
277 if !sanitize.Properties.SanitizerEnabled {
278 return flags
279 }
280
281 if !ctx.clang() {
282 ctx.ModuleErrorf("Use of sanitizers requires clang")
283 }
284
285 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700286 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800287
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700288 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800289 sanitizers = append(sanitizers, "undefined")
Colin Cross16b23492016-01-06 14:41:07 -0800290 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700291 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800292 sanitizers = append(sanitizers,
293 "bool",
294 "integer-divide-by-zero",
295 "return",
296 "returns-nonnull-attribute",
297 "shift-exponent",
298 "unreachable",
299 "vla-bound",
300 // TODO(danalbert): The following checks currently have compiler performance issues.
301 //"alignment",
302 //"bounds",
303 //"enum",
304 //"float-cast-overflow",
305 //"float-divide-by-zero",
306 //"nonnull-attribute",
307 //"null",
308 //"shift-base",
309 //"signed-integer-overflow",
310 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
311 // https://llvm.org/PR19302
312 // http://reviews.llvm.org/D6974
313 // "object-size",
314 )
315 }
316 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
317 }
318
Ivan Lozano651275b2017-06-13 10:24:34 -0700319 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700320 diagSanitizers = append(diagSanitizers, "undefined")
321 }
322
Ivan Lozano651275b2017-06-13 10:24:34 -0700323 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
324
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700325 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700326 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800327 // Frame pointer based unwinder in ASan requires ARM frame setup.
328 // TODO: put in flags?
329 flags.RequiredInstructionSet = "arm"
330 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700331 flags.CFlags = append(flags.CFlags, asanCflags...)
332 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800333
Colin Cross16b23492016-01-06 14:41:07 -0800334 if ctx.Host() {
335 // -nodefaultlibs (provided with libc++) prevents the driver from linking
336 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800337 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
338 } else {
339 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
340 flags.DynamicLinker = "/system/bin/linker_asan"
341 if flags.Toolchain.Is64Bit() {
342 flags.DynamicLinker += "64"
343 }
344 }
345 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700346 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800347 }
348
Yabin Cui6be405e2017-10-19 15:52:11 -0700349 if Bool(sanitize.Properties.Sanitize.Thread) {
350 sanitizers = append(sanitizers, "thread")
351 }
352
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700353 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400354 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800355 }
356
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700357 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700358 sanitizers = append(sanitizers, "safe-stack")
359 }
360
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700361 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800362 if ctx.Arch().ArchType == android.Arm {
363 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
364 // to do this on a function basis, so force Thumb on the entire module.
365 flags.RequiredInstructionSet = "thumb"
366 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700367 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000368
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700369 flags.CFlags = append(flags.CFlags, cfiCflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000370 // Only append the default visibility flag if -fvisibility has not already been set
371 // to hidden.
372 if !inList("-fvisibility=hidden", flags.CFlags) {
373 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
374 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700375 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
376 flags.ArFlags = append(flags.ArFlags, cfiArflags...)
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700377 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
378 diagSanitizers = append(diagSanitizers, "cfi")
379 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000380
381 if ctx.staticBinary() {
382 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
383 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
384 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700385 }
386
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700387 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
388 if !ctx.static() {
389 sanitizers = append(sanitizers, "unsigned-integer-overflow")
390 sanitizers = append(sanitizers, "signed-integer-overflow")
391 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
392 if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) {
393 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
394 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
395 }
396 }
397 }
398
Colin Cross16b23492016-01-06 14:41:07 -0800399 if len(sanitizers) > 0 {
400 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
401 flags.CFlags = append(flags.CFlags, sanitizeArg)
402 if ctx.Host() {
403 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
404 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800405 // Host sanitizers only link symbols in the final executable, so
406 // there will always be undefined symbols in intermediate libraries.
407 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800408 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700409 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Colin Cross16b23492016-01-06 14:41:07 -0800410 }
411 }
412
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700413 if len(diagSanitizers) > 0 {
414 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
415 }
416 // FIXME: enable RTTI if diag + (cfi or vptr)
417
Andreas Gampe97071162017-05-08 13:15:23 -0700418 if sanitize.Properties.Sanitize.Recover != nil {
419 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
420 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
421 }
422
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700423 // Link a runtime library if needed.
424 runtimeLibrary := ""
425 if Bool(sanitize.Properties.Sanitize.Address) {
426 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Yabin Cui6be405e2017-10-19 15:52:11 -0700427 } else if Bool(sanitize.Properties.Sanitize.Thread) {
428 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain())
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700429 } else if len(diagSanitizers) > 0 {
430 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
431 }
432
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700433 if runtimeLibrary != "" {
Jiyong Park27b188b2017-07-18 13:23:39 +0900434 // ASan runtime library must be the first in the link order.
Colin Cross8ff9ef42017-05-08 13:44:11 -0700435 flags.libFlags = append([]string{
436 "${config.ClangAsanLibDir}/" + runtimeLibrary + ctx.toolchain().ShlibSuffix(),
437 }, flags.libFlags...)
438 sanitize.runtimeLibrary = runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900439
440 // When linking against VNDK, use the vendor variant of the runtime lib
441 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700442 if ctx.useVndk() {
Jiyong Park27b188b2017-07-18 13:23:39 +0900443 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix
444 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700445 }
446
Colin Cross635c3b02016-05-18 15:37:25 -0700447 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800448 if blacklist.Valid() {
449 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
450 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
451 }
452
453 return flags
454}
455
Colin Cross8ff9ef42017-05-08 13:44:11 -0700456func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross27a4b052017-08-10 16:32:23 -0700457 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Jiyong Park27b188b2017-07-18 13:23:39 +0900458 if sanitize.androidMkRuntimeLibrary != "" {
459 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700460 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700461 })
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000462 if ctx.Target().Os.Class == android.Device {
463 if Bool(sanitize.Properties.Sanitize.Cfi) {
464 ret.SubName += ".cfi"
465 } else if Bool(sanitize.Properties.Sanitize.Address) {
466 ret.SubName += ".asan"
467 }
468 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700469}
470
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700471func (sanitize *sanitize) inSanitizerDir() bool {
472 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700473}
474
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000475func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000476 switch t {
477 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000478 return sanitize.Properties.Sanitize.Address
Vishwath Mohan95229302017-08-11 00:53:16 +0000479 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000480 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000481 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000482 return sanitize.Properties.Sanitize.Integer_overflow
483 case cfi:
484 return sanitize.Properties.Sanitize.Cfi
Vishwath Mohan95229302017-08-11 00:53:16 +0000485 default:
486 panic(fmt.Errorf("unknown sanitizerType %d", t))
487 }
488}
489
Colin Cross16b23492016-01-06 14:41:07 -0800490func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
491 switch t {
492 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700493 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700494 if !b {
495 sanitize.Properties.Sanitize.Coverage = nil
496 }
Colin Cross16b23492016-01-06 14:41:07 -0800497 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700498 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700499 case intOverflow:
500 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000501 case cfi:
502 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
503 sanitize.Properties.Sanitize.Diag.Cfi = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800504 default:
505 panic(fmt.Errorf("unknown sanitizerType %d", t))
506 }
507 if b {
508 sanitize.Properties.SanitizerEnabled = true
509 }
510}
511
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000512// Check if the sanitizer is explicitly disabled (as opposed to nil by
513// virtue of not being set).
514func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
515 if sanitize == nil {
516 return false
517 }
518
519 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
520 return sanitizerVal != nil && *sanitizerVal == false
521}
522
523// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
524// because enabling a sanitizer either directly (via the blueprint) or
525// indirectly (via a mutator) sets the bool ptr to true, and you can't
526// distinguish between the cases. It isn't needed though - both cases can be
527// treated identically.
528func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
529 if sanitize == nil {
530 return false
531 }
532
533 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
534 return sanitizerVal != nil && *sanitizerVal == true
535}
536
Colin Cross16b23492016-01-06 14:41:07 -0800537// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700538func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
539 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000540 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Crossd11fcda2017-10-23 17:59:01 -0700541 mctx.VisitDepsDepthFirst(func(module android.Module) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000542 if d, ok := module.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800543 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000544 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
545 if (t == cfi && d.static()) || t != cfi {
546 d.sanitize.Properties.SanitizeDep = true
547 }
Colin Cross16b23492016-01-06 14:41:07 -0800548 }
549 })
550 }
551 }
552}
553
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000554// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700555func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
556 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000557 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000558 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700559 modules := mctx.CreateVariations(t.String())
560 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000561 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
562 // Save original sanitizer status before we assign values to variant
563 // 0 as that overwrites the original.
564 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
565
Colin Crossb0f28952016-09-19 16:46:53 -0700566 modules := mctx.CreateVariations("", t.String())
567 modules[0].(*Module).sanitize.SetSanitizer(t, false)
568 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000569
Colin Crossb0f28952016-09-19 16:46:53 -0700570 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
571 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane6153452017-08-11 00:52:44 +0000572 if mctx.Device() {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000573 // CFI and ASAN are currently mutually exclusive so disable
574 // CFI if this is an ASAN variant.
575 if t == asan {
576 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
577 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
578 }
Orion Hodsonda11d742017-10-31 17:31:00 +0000579 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000580 if mctx.AConfig().EmbeddedInMake() {
581 if isSanitizerEnabled {
582 modules[0].(*Module).Properties.HideFromMake = true
583 } else {
584 modules[1].(*Module).Properties.HideFromMake = true
585 }
586 }
Orion Hodsonda11d742017-10-31 17:31:00 +0000587 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700588 if !mctx.AConfig().EmbeddedInMake() || !mctx.Device() {
589 if isSanitizerEnabled {
590 modules[0].(*Module).Properties.PreventInstall = true
591 } else {
592 modules[1].(*Module).Properties.PreventInstall = true
593 }
594 }
Colin Cross16b23492016-01-06 14:41:07 -0800595 }
596 c.sanitize.Properties.SanitizeDep = false
597 }
598 }
599}