blob: 38f4fc28db260c18f64cdfa987af3dcc63971619 [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"
Jeff Gaston72765392017-11-28 16:37:53 -080020 "sort"
Colin Cross16b23492016-01-06 14:41:07 -080021 "strings"
Vishwath Mohane7128792017-11-17 11:08:10 -080022 "sync"
Colin Cross16b23492016-01-06 14:41:07 -080023
Colin Cross635c3b02016-05-18 15:37:25 -070024 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070025 "android/soong/cc/config"
Colin Cross16b23492016-01-06 14:41:07 -080026)
27
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070028var (
29 // Any C flags added by sanitizer which libTooling tools may not
30 // understand also need to be added to ClangLibToolingUnknownCflags in
31 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080032
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070033 asanCflags = []string{"-fno-omit-frame-pointer"}
34 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
35 asanLibs = []string{"libasan"}
36
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000037 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070038 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070039 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070040 "-Wl,-plugin-opt,O1"}
Vishwath Mohane7128792017-11-17 11:08:10 -080041 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
Vishwath Mohane7128792017-11-17 11:08:10 -080042 cfiStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070043
Ivan Lozano30c5db22018-02-21 15:49:20 -080044 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
45 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer", "-fno-sanitize-recover=integer"}
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"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -070097 Scudo *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080098
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070099 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
100 // Replaces abort() on error with a human-readable error message.
101 // Address and Thread sanitizers always run in diagnostic mode.
102 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700103 Undefined *bool `android:"arch_variant"`
104 Cfi *bool `android:"arch_variant"`
105 Integer_overflow *bool `android:"arch_variant"`
106 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700107 }
108
109 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800110 Recover []string
111
112 // value to pass to -fsanitize-blacklist
113 Blacklist *string
114 } `android:"arch_variant"`
115
Ivan Lozano30c5db22018-02-21 15:49:20 -0800116 SanitizerEnabled bool `blueprint:"mutated"`
117 SanitizeDep bool `blueprint:"mutated"`
118 MinimalRuntimeDep bool `blueprint:"mutated"`
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700119 UbsanRuntimeDep bool `blueprint:"mutated"`
Ivan Lozano30c5db22018-02-21 15:49:20 -0800120 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800121}
122
123type sanitize struct {
124 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700125
Jiyong Park27b188b2017-07-18 13:23:39 +0900126 runtimeLibrary string
127 androidMkRuntimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800128}
129
Vishwath Mohane7128792017-11-17 11:08:10 -0800130func init() {
131 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
132}
133
Colin Cross16b23492016-01-06 14:41:07 -0800134func (sanitize *sanitize) props() []interface{} {
135 return []interface{}{&sanitize.Properties}
136}
137
138func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700139 s := &sanitize.Properties.Sanitize
140
Colin Cross16b23492016-01-06 14:41:07 -0800141 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700142 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800143 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800144 }
145
146 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800147 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800148 return
149 }
150
Colin Cross16b23492016-01-06 14:41:07 -0800151 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700152 var globalSanitizersDiag []string
153
Colin Cross16b23492016-01-06 14:41:07 -0800154 if ctx.clang() {
155 if ctx.Host() {
Colin Cross6510f912017-11-29 00:27:14 -0800156 globalSanitizers = ctx.Config().SanitizeHost()
Colin Cross16b23492016-01-06 14:41:07 -0800157 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800158 arches := ctx.Config().SanitizeDeviceArch()
Colin Cross23ae82a2016-11-02 14:34:39 -0700159 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
Colin Cross6510f912017-11-29 00:27:14 -0800160 globalSanitizers = ctx.Config().SanitizeDevice()
161 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross23ae82a2016-11-02 14:34:39 -0700162 }
Colin Cross16b23492016-01-06 14:41:07 -0800163 }
164 }
165
Colin Cross16b23492016-01-06 14:41:07 -0800166 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000167 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700168 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
169 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000170 }
Colin Cross16b23492016-01-06 14:41:07 -0800171
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700172 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
173 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000174 }
175
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800176 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
177 if s.Address == nil {
178 s.Address = boolPtr(true)
179 } else if *s.Address == false {
180 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
181 // disables address, then disable coverage as well.
182 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
183 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000184 }
185
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700186 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
187 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000188 }
189
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700190 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
191 s.Coverage = boolPtr(true)
192 }
193
194 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
195 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000196 }
197
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700198 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800199 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700200 s.Cfi = boolPtr(true)
201 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700202 }
203
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700204 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700205 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700206 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700207 s.Integer_overflow = boolPtr(true)
208 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700209 }
210
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700211 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
212 s.Scudo = boolPtr(true)
213 }
214
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000215 if len(globalSanitizers) > 0 {
216 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
217 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700218
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700219 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700220 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700221 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700222 s.Diag.Integer_overflow = boolPtr(true)
223 }
224
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700225 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
226 s.Diag.Cfi == nil && Bool(s.Cfi) {
227 s.Diag.Cfi = boolPtr(true)
228 }
229
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700230 if len(globalSanitizersDiag) > 0 {
231 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
232 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700233 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700234
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700235 // Enable CFI for all components in the include paths
Colin Cross6510f912017-11-29 00:27:14 -0800236 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000237 s.Cfi = boolPtr(true)
238 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
239 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700240 }
241 }
242
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800243 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800244 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800245 s.Cfi = nil
246 s.Diag.Cfi = nil
247 }
248
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800249 // Also disable CFI for arm32 until b/35157333 is fixed.
250 if ctx.Arch().ArchType == android.Arm {
251 s.Cfi = nil
252 s.Diag.Cfi = nil
253 }
254
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700255 // Also disable CFI if ASAN is enabled.
256 if Bool(s.Address) {
257 s.Cfi = nil
258 s.Diag.Cfi = nil
259 }
260
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700261 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800262 if ctx.Host() {
263 s.Cfi = nil
264 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700265 s.Misc_undefined = nil
266 s.Undefined = nil
267 s.All_undefined = nil
268 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800269 }
270
Colin Cross3c344ef2016-07-18 15:44:56 -0700271 if ctx.staticBinary() {
272 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700273 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700274 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800275 }
276
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700277 if Bool(s.All_undefined) {
278 s.Undefined = nil
279 }
280
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700281 if !ctx.toolchain().Is64Bit() {
282 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700283 s.Thread = nil
284 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800285 // TODO(ccross): error for compile_multilib = "32"?
286 }
287
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800288 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700289 Bool(s.Coverage) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
290 Bool(s.Scudo)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700291 sanitize.Properties.SanitizerEnabled = true
292 }
293
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700294 // Disable Scudo if ASan or TSan is enabled.
295 if Bool(s.Address) || Bool(s.Thread) {
296 s.Scudo = nil
297 }
298
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700299 if Bool(s.Coverage) {
300 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800301 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
302 }
303 }
304}
305
306func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
307 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
308 return deps
309 }
310
311 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700312 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700313 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800314 }
315 }
316
317 return deps
318}
319
320func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700321 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
322 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800323
324 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
325 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700326 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800327 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700328 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800329 return flags
330 }
331
332 if !ctx.clang() {
333 ctx.ModuleErrorf("Use of sanitizers requires clang")
334 }
335
336 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700337 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800338
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700339 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800340 sanitizers = append(sanitizers, "undefined")
Colin Cross16b23492016-01-06 14:41:07 -0800341 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700342 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800343 sanitizers = append(sanitizers,
344 "bool",
345 "integer-divide-by-zero",
346 "return",
347 "returns-nonnull-attribute",
348 "shift-exponent",
349 "unreachable",
350 "vla-bound",
351 // TODO(danalbert): The following checks currently have compiler performance issues.
352 //"alignment",
353 //"bounds",
354 //"enum",
355 //"float-cast-overflow",
356 //"float-divide-by-zero",
357 //"nonnull-attribute",
358 //"null",
359 //"shift-base",
360 //"signed-integer-overflow",
361 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
362 // https://llvm.org/PR19302
363 // http://reviews.llvm.org/D6974
364 // "object-size",
365 )
366 }
367 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
368 }
369
Ivan Lozano651275b2017-06-13 10:24:34 -0700370 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700371 diagSanitizers = append(diagSanitizers, "undefined")
372 }
373
Ivan Lozano651275b2017-06-13 10:24:34 -0700374 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
375
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700376 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700377 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800378 // Frame pointer based unwinder in ASan requires ARM frame setup.
379 // TODO: put in flags?
380 flags.RequiredInstructionSet = "arm"
381 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700382 flags.CFlags = append(flags.CFlags, asanCflags...)
383 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800384
Colin Cross16b23492016-01-06 14:41:07 -0800385 if ctx.Host() {
386 // -nodefaultlibs (provided with libc++) prevents the driver from linking
387 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800388 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
389 } else {
390 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
391 flags.DynamicLinker = "/system/bin/linker_asan"
392 if flags.Toolchain.Is64Bit() {
393 flags.DynamicLinker += "64"
394 }
395 }
396 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700397 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800398 }
399
Yabin Cui6be405e2017-10-19 15:52:11 -0700400 if Bool(sanitize.Properties.Sanitize.Thread) {
401 sanitizers = append(sanitizers, "thread")
402 }
403
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700404 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400405 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800406 }
407
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700408 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700409 sanitizers = append(sanitizers, "safe-stack")
410 }
411
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700412 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800413 if ctx.Arch().ArchType == android.Arm {
414 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
415 // to do this on a function basis, so force Thumb on the entire module.
416 flags.RequiredInstructionSet = "thumb"
417 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700418 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000419
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700420 flags.CFlags = append(flags.CFlags, cfiCflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000421 // Only append the default visibility flag if -fvisibility has not already been set
422 // to hidden.
423 if !inList("-fvisibility=hidden", flags.CFlags) {
424 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
425 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700426 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Zhizhou Yang51be6322018-02-08 18:32:11 -0800427 flags.ArGoldPlugin = true
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700428 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
429 diagSanitizers = append(diagSanitizers, "cfi")
430 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000431
432 if ctx.staticBinary() {
433 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
434 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
435 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700436 }
437
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700438 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700439 sanitizers = append(sanitizers, "unsigned-integer-overflow")
440 sanitizers = append(sanitizers, "signed-integer-overflow")
441 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
442 if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) {
443 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
444 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700445 }
446 }
447
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700448 if Bool(sanitize.Properties.Sanitize.Scudo) {
449 sanitizers = append(sanitizers, "scudo")
450 }
451
Colin Cross16b23492016-01-06 14:41:07 -0800452 if len(sanitizers) > 0 {
453 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800454
Colin Cross16b23492016-01-06 14:41:07 -0800455 flags.CFlags = append(flags.CFlags, sanitizeArg)
456 if ctx.Host() {
457 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
458 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800459 // Host sanitizers only link symbols in the final executable, so
460 // there will always be undefined symbols in intermediate libraries.
461 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800462 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700463 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800464
465 if enableMinimalRuntime(sanitize) {
466 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
467 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700468 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800469 }
Colin Cross16b23492016-01-06 14:41:07 -0800470 }
471 }
472
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700473 if len(diagSanitizers) > 0 {
Ivan Lozanob7d0f522018-01-20 01:44:38 +0000474 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700475 }
476 // FIXME: enable RTTI if diag + (cfi or vptr)
477
Andreas Gampe97071162017-05-08 13:15:23 -0700478 if sanitize.Properties.Sanitize.Recover != nil {
479 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
480 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
481 }
482
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700483 // Link a runtime library if needed.
484 runtimeLibrary := ""
485 if Bool(sanitize.Properties.Sanitize.Address) {
486 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Yabin Cui6be405e2017-10-19 15:52:11 -0700487 } else if Bool(sanitize.Properties.Sanitize.Thread) {
488 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain())
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700489 } else if Bool(sanitize.Properties.Sanitize.Scudo) {
490 runtimeLibrary = config.ScudoRuntimeLibrary(ctx.toolchain())
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700491 } else if len(diagSanitizers) > 0 || sanitize.Properties.UbsanRuntimeDep {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700492 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
493 }
494
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700495 if runtimeLibrary != "" {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700496 runtimeLibraryPath := "${config.ClangAsanLibDir}/" + runtimeLibrary
497 if !ctx.static() {
498 runtimeLibraryPath = runtimeLibraryPath + ctx.toolchain().ShlibSuffix()
499 } else {
500 runtimeLibraryPath = runtimeLibraryPath + ".a"
501 }
502
Jiyong Park27b188b2017-07-18 13:23:39 +0900503 // ASan runtime library must be the first in the link order.
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700504 flags.libFlags = append([]string{runtimeLibraryPath}, flags.libFlags...)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700505 sanitize.runtimeLibrary = runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900506
507 // When linking against VNDK, use the vendor variant of the runtime lib
508 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700509 if ctx.useVndk() {
Jiyong Park27b188b2017-07-18 13:23:39 +0900510 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix
511 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700512 }
513
Colin Cross635c3b02016-05-18 15:37:25 -0700514 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800515 if blacklist.Valid() {
516 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
517 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
518 }
519
520 return flags
521}
522
Colin Cross8ff9ef42017-05-08 13:44:11 -0700523func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross27a4b052017-08-10 16:32:23 -0700524 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Jiyong Park27b188b2017-07-18 13:23:39 +0900525 if sanitize.androidMkRuntimeLibrary != "" {
526 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700527 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700528 })
Vishwath Mohane7128792017-11-17 11:08:10 -0800529
530 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
531 // name conflict.
532 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
533 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000534 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700535}
536
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700537func (sanitize *sanitize) inSanitizerDir() bool {
538 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700539}
540
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000541func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000542 switch t {
543 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000544 return sanitize.Properties.Sanitize.Address
Vishwath Mohan95229302017-08-11 00:53:16 +0000545 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000546 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000547 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000548 return sanitize.Properties.Sanitize.Integer_overflow
549 case cfi:
550 return sanitize.Properties.Sanitize.Cfi
Vishwath Mohan95229302017-08-11 00:53:16 +0000551 default:
552 panic(fmt.Errorf("unknown sanitizerType %d", t))
553 }
554}
555
Dan Albert7d1eecf2018-01-19 12:30:45 -0800556func (sanitize *sanitize) isUnsanitizedVariant() bool {
557 return !sanitize.isSanitizerEnabled(asan) &&
558 !sanitize.isSanitizerEnabled(tsan) &&
559 !sanitize.isSanitizerEnabled(cfi)
560}
561
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700562func (sanitize *sanitize) isVariantOnProductionDevice() bool {
563 return !sanitize.isSanitizerEnabled(asan) &&
564 !sanitize.isSanitizerEnabled(tsan)
565}
566
Colin Cross16b23492016-01-06 14:41:07 -0800567func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
568 switch t {
569 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700570 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700571 if !b {
572 sanitize.Properties.Sanitize.Coverage = nil
573 }
Colin Cross16b23492016-01-06 14:41:07 -0800574 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700575 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700576 case intOverflow:
577 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000578 case cfi:
579 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800580 default:
581 panic(fmt.Errorf("unknown sanitizerType %d", t))
582 }
583 if b {
584 sanitize.Properties.SanitizerEnabled = true
585 }
586}
587
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000588// Check if the sanitizer is explicitly disabled (as opposed to nil by
589// virtue of not being set).
590func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
591 if sanitize == nil {
592 return false
593 }
594
595 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
596 return sanitizerVal != nil && *sanitizerVal == false
597}
598
599// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
600// because enabling a sanitizer either directly (via the blueprint) or
601// indirectly (via a mutator) sets the bool ptr to true, and you can't
602// distinguish between the cases. It isn't needed though - both cases can be
603// treated identically.
604func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
605 if sanitize == nil {
606 return false
607 }
608
609 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
610 return sanitizerVal != nil && *sanitizerVal == true
611}
612
Colin Cross16b23492016-01-06 14:41:07 -0800613// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700614func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
615 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000616 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Crossd11fcda2017-10-23 17:59:01 -0700617 mctx.VisitDepsDepthFirst(func(module android.Module) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000618 if d, ok := module.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800619 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000620 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
621 if (t == cfi && d.static()) || t != cfi {
622 d.sanitize.Properties.SanitizeDep = true
623 }
Colin Cross16b23492016-01-06 14:41:07 -0800624 }
625 })
626 }
627 }
628}
629
Ivan Lozano30c5db22018-02-21 15:49:20 -0800630// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700631func sanitizerRuntimeDepsMutator() func(android.TopDownMutatorContext) {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800632 return func(mctx android.TopDownMutatorContext) {
633 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
634 mctx.VisitDepsDepthFirst(func(module android.Module) {
635 if d, ok := module.(*Module); ok && d.static() && d.sanitize != nil {
636
Ivan Lozano30c5db22018-02-21 15:49:20 -0800637 if enableMinimalRuntime(d.sanitize) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700638 // If a static dependency is built with the minimal runtime,
639 // make sure we include the ubsan minimal runtime.
Ivan Lozano30c5db22018-02-21 15:49:20 -0800640 c.sanitize.Properties.MinimalRuntimeDep = true
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700641 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
642 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
643 // If a static dependency runs with full ubsan diagnostics,
644 // make sure we include the ubsan runtime.
645 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800646 }
647 }
648 })
649 }
650 }
651}
652
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000653// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700654func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
655 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000656 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000657 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700658 modules := mctx.CreateVariations(t.String())
659 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000660 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
661 // Save original sanitizer status before we assign values to variant
662 // 0 as that overwrites the original.
663 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
664
Colin Crossb0f28952016-09-19 16:46:53 -0700665 modules := mctx.CreateVariations("", t.String())
666 modules[0].(*Module).sanitize.SetSanitizer(t, false)
667 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000668
Colin Crossb0f28952016-09-19 16:46:53 -0700669 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
670 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800671
672 // We don't need both variants active for anything but CFI-enabled
673 // target static libraries, so suppress the appropriate variant in
674 // all other cases.
675 if t == cfi {
676 if c.static() {
677 if !mctx.Device() {
678 if isSanitizerEnabled {
679 modules[0].(*Module).Properties.PreventInstall = true
680 modules[0].(*Module).Properties.HideFromMake = true
681 } else {
682 modules[1].(*Module).Properties.PreventInstall = true
683 modules[1].(*Module).Properties.HideFromMake = true
684 }
685 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800686 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800687
688 cfiStaticLibsMutex.Lock()
689 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
690 cfiStaticLibsMutex.Unlock()
691 }
692 } else {
693 modules[0].(*Module).Properties.PreventInstall = true
694 modules[0].(*Module).Properties.HideFromMake = true
695 }
696 } else if t == asan {
697 if mctx.Device() {
698 // CFI and ASAN are currently mutually exclusive so disable
699 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000700 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
701 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
702 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700703 if isSanitizerEnabled {
704 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800705 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700706 } else {
707 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800708 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700709 }
710 }
Colin Cross16b23492016-01-06 14:41:07 -0800711 }
712 c.sanitize.Properties.SanitizeDep = false
713 }
714 }
715}
Vishwath Mohane7128792017-11-17 11:08:10 -0800716
717func cfiStaticLibs(config android.Config) *[]string {
718 return config.Once("cfiStaticLibs", func() interface{} {
719 return &[]string{}
720 }).(*[]string)
721}
722
Ivan Lozano30c5db22018-02-21 15:49:20 -0800723func enableMinimalRuntime(sanitize *sanitize) bool {
724 if !Bool(sanitize.Properties.Sanitize.Address) &&
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700725 !Bool(sanitize.Properties.Sanitize.Scudo) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -0800726 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
727 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
728 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
729 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
730 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
731 return true
732 }
733 return false
734}
735
Vishwath Mohane7128792017-11-17 11:08:10 -0800736func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
737 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -0800738 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -0800739 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
740}