blob: 70aa41238478f9729f3149f1544fd0f07dd13916 [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 {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700190 if !ctx.AConfig().CFIDisabledForPath(ctx.ModuleDir()) {
191 s.Cfi = boolPtr(true)
192 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700193 }
194
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700195 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozano5f595532017-07-13 14:46:05 -0700196 if !ctx.AConfig().IntegerOverflowDisabledForPath(ctx.ModuleDir()) {
197 s.Integer_overflow = boolPtr(true)
198 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700199 }
200
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000201 if len(globalSanitizers) > 0 {
202 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
203 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700204
205 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
206 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) {
207 s.Diag.Integer_overflow = boolPtr(true)
208 }
209
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700210 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
211 s.Diag.Cfi == nil && Bool(s.Cfi) {
212 s.Diag.Cfi = boolPtr(true)
213 }
214
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700215 if len(globalSanitizersDiag) > 0 {
216 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
217 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700218 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700219
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700220 // Enable CFI for all components in the include paths
221 if s.Cfi == nil && ctx.AConfig().CFIEnabledForPath(ctx.ModuleDir()) {
222 s.Cfi = boolPtr(true)
223 if inList("cfi", ctx.AConfig().SanitizeDeviceDiag()) {
224 s.Diag.Cfi = boolPtr(true)
225 }
226 }
227
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800228 // CFI needs gold linker, and mips toolchain does not have one.
229 if !ctx.AConfig().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800230 s.Cfi = nil
231 s.Diag.Cfi = nil
232 }
233
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800234 // Also disable CFI for arm32 until b/35157333 is fixed.
235 if ctx.Arch().ArchType == android.Arm {
236 s.Cfi = nil
237 s.Diag.Cfi = nil
238 }
239
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700240 // Also disable CFI if ASAN is enabled.
241 if Bool(s.Address) {
242 s.Cfi = nil
243 s.Diag.Cfi = nil
244 }
245
Colin Cross3c344ef2016-07-18 15:44:56 -0700246 if ctx.staticBinary() {
247 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700248 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700249 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800250 }
251
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700252 if Bool(s.All_undefined) {
253 s.Undefined = nil
254 }
255
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700256 if !ctx.toolchain().Is64Bit() {
257 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700258 s.Thread = nil
259 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800260 // TODO(ccross): error for compile_multilib = "32"?
261 }
262
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800263 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 -0700264 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 -0700265 sanitize.Properties.SanitizerEnabled = true
266 }
267
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700268 if Bool(s.Coverage) {
269 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800270 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
271 }
272 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000273
274 cfiExportsMap = android.PathForSource(ctx, cfiExportsMapPath)
Colin Cross16b23492016-01-06 14:41:07 -0800275}
276
277func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
278 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
279 return deps
280 }
281
282 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700283 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700284 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800285 }
286 }
287
288 return deps
289}
290
291func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
292 if !sanitize.Properties.SanitizerEnabled {
293 return flags
294 }
295
296 if !ctx.clang() {
297 ctx.ModuleErrorf("Use of sanitizers requires clang")
298 }
299
300 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700301 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800302
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700303 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800304 sanitizers = append(sanitizers, "undefined")
Colin Cross16b23492016-01-06 14:41:07 -0800305 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700306 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800307 sanitizers = append(sanitizers,
308 "bool",
309 "integer-divide-by-zero",
310 "return",
311 "returns-nonnull-attribute",
312 "shift-exponent",
313 "unreachable",
314 "vla-bound",
315 // TODO(danalbert): The following checks currently have compiler performance issues.
316 //"alignment",
317 //"bounds",
318 //"enum",
319 //"float-cast-overflow",
320 //"float-divide-by-zero",
321 //"nonnull-attribute",
322 //"null",
323 //"shift-base",
324 //"signed-integer-overflow",
325 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
326 // https://llvm.org/PR19302
327 // http://reviews.llvm.org/D6974
328 // "object-size",
329 )
330 }
331 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
332 }
333
Ivan Lozano651275b2017-06-13 10:24:34 -0700334 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700335 diagSanitizers = append(diagSanitizers, "undefined")
336 }
337
Ivan Lozano651275b2017-06-13 10:24:34 -0700338 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
339
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700340 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700341 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800342 // Frame pointer based unwinder in ASan requires ARM frame setup.
343 // TODO: put in flags?
344 flags.RequiredInstructionSet = "arm"
345 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700346 flags.CFlags = append(flags.CFlags, asanCflags...)
347 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800348
Colin Cross16b23492016-01-06 14:41:07 -0800349 if ctx.Host() {
350 // -nodefaultlibs (provided with libc++) prevents the driver from linking
351 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800352 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
353 } else {
354 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
355 flags.DynamicLinker = "/system/bin/linker_asan"
356 if flags.Toolchain.Is64Bit() {
357 flags.DynamicLinker += "64"
358 }
359 }
360 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700361 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800362 }
363
Yabin Cui6be405e2017-10-19 15:52:11 -0700364 if Bool(sanitize.Properties.Sanitize.Thread) {
365 sanitizers = append(sanitizers, "thread")
366 }
367
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700368 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400369 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800370 }
371
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700372 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700373 sanitizers = append(sanitizers, "safe-stack")
374 }
375
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700376 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800377 if ctx.Arch().ArchType == android.Arm {
378 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
379 // to do this on a function basis, so force Thumb on the entire module.
380 flags.RequiredInstructionSet = "thumb"
381 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700382 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000383
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700384 flags.CFlags = append(flags.CFlags, cfiCflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000385 // Only append the default visibility flag if -fvisibility has not already been set
386 // to hidden.
387 if !inList("-fvisibility=hidden", flags.CFlags) {
388 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
389 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700390 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
391 flags.ArFlags = append(flags.ArFlags, cfiArflags...)
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700392 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
393 diagSanitizers = append(diagSanitizers, "cfi")
394 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000395
396 if ctx.staticBinary() {
397 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
398 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
399 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700400 }
401
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700402 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
403 if !ctx.static() {
404 sanitizers = append(sanitizers, "unsigned-integer-overflow")
405 sanitizers = append(sanitizers, "signed-integer-overflow")
406 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
407 if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) {
408 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
409 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
410 }
411 }
412 }
413
Colin Cross16b23492016-01-06 14:41:07 -0800414 if len(sanitizers) > 0 {
415 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
416 flags.CFlags = append(flags.CFlags, sanitizeArg)
417 if ctx.Host() {
418 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
419 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800420 // Host sanitizers only link symbols in the final executable, so
421 // there will always be undefined symbols in intermediate libraries.
422 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800423 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700424 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Colin Cross16b23492016-01-06 14:41:07 -0800425 }
426 }
427
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700428 if len(diagSanitizers) > 0 {
429 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
430 }
431 // FIXME: enable RTTI if diag + (cfi or vptr)
432
Andreas Gampe97071162017-05-08 13:15:23 -0700433 if sanitize.Properties.Sanitize.Recover != nil {
434 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
435 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
436 }
437
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700438 // Link a runtime library if needed.
439 runtimeLibrary := ""
440 if Bool(sanitize.Properties.Sanitize.Address) {
441 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Yabin Cui6be405e2017-10-19 15:52:11 -0700442 } else if Bool(sanitize.Properties.Sanitize.Thread) {
443 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain())
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700444 } else if len(diagSanitizers) > 0 {
445 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
446 }
447
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700448 if runtimeLibrary != "" {
Jiyong Park27b188b2017-07-18 13:23:39 +0900449 // ASan runtime library must be the first in the link order.
Colin Cross8ff9ef42017-05-08 13:44:11 -0700450 flags.libFlags = append([]string{
451 "${config.ClangAsanLibDir}/" + runtimeLibrary + ctx.toolchain().ShlibSuffix(),
452 }, flags.libFlags...)
453 sanitize.runtimeLibrary = runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900454
455 // When linking against VNDK, use the vendor variant of the runtime lib
456 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700457 if ctx.useVndk() {
Jiyong Park27b188b2017-07-18 13:23:39 +0900458 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix
459 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700460 }
461
Colin Cross635c3b02016-05-18 15:37:25 -0700462 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800463 if blacklist.Valid() {
464 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
465 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
466 }
467
468 return flags
469}
470
Colin Cross8ff9ef42017-05-08 13:44:11 -0700471func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross27a4b052017-08-10 16:32:23 -0700472 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Jiyong Park27b188b2017-07-18 13:23:39 +0900473 if sanitize.androidMkRuntimeLibrary != "" {
474 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700475 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700476 })
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000477 if ctx.Target().Os.Class == android.Device {
478 if Bool(sanitize.Properties.Sanitize.Cfi) {
479 ret.SubName += ".cfi"
480 } else if Bool(sanitize.Properties.Sanitize.Address) {
481 ret.SubName += ".asan"
482 }
483 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700484}
485
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700486func (sanitize *sanitize) inSanitizerDir() bool {
487 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700488}
489
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000490func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000491 switch t {
492 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000493 return sanitize.Properties.Sanitize.Address
Vishwath Mohan95229302017-08-11 00:53:16 +0000494 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000495 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000496 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000497 return sanitize.Properties.Sanitize.Integer_overflow
498 case cfi:
499 return sanitize.Properties.Sanitize.Cfi
Vishwath Mohan95229302017-08-11 00:53:16 +0000500 default:
501 panic(fmt.Errorf("unknown sanitizerType %d", t))
502 }
503}
504
Colin Cross16b23492016-01-06 14:41:07 -0800505func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
506 switch t {
507 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700508 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700509 if !b {
510 sanitize.Properties.Sanitize.Coverage = nil
511 }
Colin Cross16b23492016-01-06 14:41:07 -0800512 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700513 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700514 case intOverflow:
515 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000516 case cfi:
517 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
518 sanitize.Properties.Sanitize.Diag.Cfi = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800519 default:
520 panic(fmt.Errorf("unknown sanitizerType %d", t))
521 }
522 if b {
523 sanitize.Properties.SanitizerEnabled = true
524 }
525}
526
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000527// Check if the sanitizer is explicitly disabled (as opposed to nil by
528// virtue of not being set).
529func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
530 if sanitize == nil {
531 return false
532 }
533
534 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
535 return sanitizerVal != nil && *sanitizerVal == false
536}
537
538// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
539// because enabling a sanitizer either directly (via the blueprint) or
540// indirectly (via a mutator) sets the bool ptr to true, and you can't
541// distinguish between the cases. It isn't needed though - both cases can be
542// treated identically.
543func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
544 if sanitize == nil {
545 return false
546 }
547
548 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
549 return sanitizerVal != nil && *sanitizerVal == true
550}
551
Colin Cross16b23492016-01-06 14:41:07 -0800552// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700553func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
554 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000555 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Crossd11fcda2017-10-23 17:59:01 -0700556 mctx.VisitDepsDepthFirst(func(module android.Module) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000557 if d, ok := module.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800558 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000559 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
560 if (t == cfi && d.static()) || t != cfi {
561 d.sanitize.Properties.SanitizeDep = true
562 }
Colin Cross16b23492016-01-06 14:41:07 -0800563 }
564 })
565 }
566 }
567}
568
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000569// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700570func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
571 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000572 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000573 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700574 modules := mctx.CreateVariations(t.String())
575 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000576 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
577 // Save original sanitizer status before we assign values to variant
578 // 0 as that overwrites the original.
579 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
580
Colin Crossb0f28952016-09-19 16:46:53 -0700581 modules := mctx.CreateVariations("", t.String())
582 modules[0].(*Module).sanitize.SetSanitizer(t, false)
583 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000584
Colin Crossb0f28952016-09-19 16:46:53 -0700585 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
586 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane6153452017-08-11 00:52:44 +0000587 if mctx.Device() {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000588 // CFI and ASAN are currently mutually exclusive so disable
589 // CFI if this is an ASAN variant.
590 if t == asan {
591 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
592 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
593 }
Orion Hodsonda11d742017-10-31 17:31:00 +0000594 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000595 if mctx.AConfig().EmbeddedInMake() {
596 if isSanitizerEnabled {
597 modules[0].(*Module).Properties.HideFromMake = true
598 } else {
599 modules[1].(*Module).Properties.HideFromMake = true
600 }
601 }
Orion Hodsonda11d742017-10-31 17:31:00 +0000602 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700603 if !mctx.AConfig().EmbeddedInMake() || !mctx.Device() {
604 if isSanitizerEnabled {
605 modules[0].(*Module).Properties.PreventInstall = true
606 } else {
607 modules[1].(*Module).Properties.PreventInstall = true
608 }
609 }
Colin Cross16b23492016-01-06 14:41:07 -0800610 }
611 c.sanitize.Properties.SanitizeDep = false
612 }
613 }
614}