blob: a704cf1e5ce2048b11b0e0a99fc5d75e60991573 [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"`
Colin Cross16b23492016-01-06 14:41:07 -080097
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070098 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
99 // Replaces abort() on error with a human-readable error message.
100 // Address and Thread sanitizers always run in diagnostic mode.
101 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700102 Undefined *bool `android:"arch_variant"`
103 Cfi *bool `android:"arch_variant"`
104 Integer_overflow *bool `android:"arch_variant"`
105 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700106 }
107
108 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800109 Recover []string
110
111 // value to pass to -fsanitize-blacklist
112 Blacklist *string
113 } `android:"arch_variant"`
114
Ivan Lozano30c5db22018-02-21 15:49:20 -0800115 SanitizerEnabled bool `blueprint:"mutated"`
116 SanitizeDep bool `blueprint:"mutated"`
117 MinimalRuntimeDep bool `blueprint:"mutated"`
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700118 UbsanRuntimeDep bool `blueprint:"mutated"`
Ivan Lozano30c5db22018-02-21 15:49:20 -0800119 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800120}
121
122type sanitize struct {
123 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700124
Jiyong Park27b188b2017-07-18 13:23:39 +0900125 runtimeLibrary string
126 androidMkRuntimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800127}
128
Vishwath Mohane7128792017-11-17 11:08:10 -0800129func init() {
130 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
131}
132
Colin Cross16b23492016-01-06 14:41:07 -0800133func (sanitize *sanitize) props() []interface{} {
134 return []interface{}{&sanitize.Properties}
135}
136
137func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700138 s := &sanitize.Properties.Sanitize
139
Colin Cross16b23492016-01-06 14:41:07 -0800140 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700141 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800142 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800143 }
144
145 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800146 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800147 return
148 }
149
Colin Cross16b23492016-01-06 14:41:07 -0800150 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700151 var globalSanitizersDiag []string
152
Colin Cross16b23492016-01-06 14:41:07 -0800153 if ctx.clang() {
154 if ctx.Host() {
Colin Cross6510f912017-11-29 00:27:14 -0800155 globalSanitizers = ctx.Config().SanitizeHost()
Colin Cross16b23492016-01-06 14:41:07 -0800156 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800157 arches := ctx.Config().SanitizeDeviceArch()
Colin Cross23ae82a2016-11-02 14:34:39 -0700158 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
Colin Cross6510f912017-11-29 00:27:14 -0800159 globalSanitizers = ctx.Config().SanitizeDevice()
160 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross23ae82a2016-11-02 14:34:39 -0700161 }
Colin Cross16b23492016-01-06 14:41:07 -0800162 }
163 }
164
Colin Cross16b23492016-01-06 14:41:07 -0800165 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000166 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700167 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
168 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000169 }
Colin Cross16b23492016-01-06 14:41:07 -0800170
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700171 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
172 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000173 }
174
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800175 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
176 if s.Address == nil {
177 s.Address = boolPtr(true)
178 } else if *s.Address == false {
179 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
180 // disables address, then disable coverage as well.
181 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
182 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000183 }
184
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700185 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
186 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000187 }
188
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700189 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
190 s.Coverage = boolPtr(true)
191 }
192
193 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
194 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000195 }
196
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700197 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800198 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700199 s.Cfi = boolPtr(true)
200 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700201 }
202
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700203 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700204 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700205 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700206 s.Integer_overflow = boolPtr(true)
207 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700208 }
209
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000210 if len(globalSanitizers) > 0 {
211 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
212 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700213
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700214 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700215 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700216 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700217 s.Diag.Integer_overflow = boolPtr(true)
218 }
219
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700220 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
221 s.Diag.Cfi == nil && Bool(s.Cfi) {
222 s.Diag.Cfi = boolPtr(true)
223 }
224
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700225 if len(globalSanitizersDiag) > 0 {
226 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
227 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700228 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700229
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700230 // Enable CFI for all components in the include paths
Colin Cross6510f912017-11-29 00:27:14 -0800231 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000232 s.Cfi = boolPtr(true)
233 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
234 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700235 }
236 }
237
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800238 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800239 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800240 s.Cfi = nil
241 s.Diag.Cfi = nil
242 }
243
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800244 // Also disable CFI for arm32 until b/35157333 is fixed.
245 if ctx.Arch().ArchType == android.Arm {
246 s.Cfi = nil
247 s.Diag.Cfi = nil
248 }
249
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700250 // Also disable CFI if ASAN is enabled.
251 if Bool(s.Address) {
252 s.Cfi = nil
253 s.Diag.Cfi = nil
254 }
255
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700256 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800257 if ctx.Host() {
258 s.Cfi = nil
259 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700260 s.Misc_undefined = nil
261 s.Undefined = nil
262 s.All_undefined = nil
263 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800264 }
265
Colin Cross3c344ef2016-07-18 15:44:56 -0700266 if ctx.staticBinary() {
267 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700268 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700269 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800270 }
271
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700272 if Bool(s.All_undefined) {
273 s.Undefined = nil
274 }
275
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700276 if !ctx.toolchain().Is64Bit() {
277 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700278 s.Thread = nil
279 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800280 // TODO(ccross): error for compile_multilib = "32"?
281 }
282
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800283 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 -0700284 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 -0700285 sanitize.Properties.SanitizerEnabled = true
286 }
287
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700288 if Bool(s.Coverage) {
289 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800290 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
291 }
292 }
293}
294
295func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
296 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
297 return deps
298 }
299
300 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700301 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700302 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800303 }
304 }
305
306 return deps
307}
308
309func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700310 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
311 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800312
313 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
314 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700315 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800316 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700317 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800318 return flags
319 }
320
321 if !ctx.clang() {
322 ctx.ModuleErrorf("Use of sanitizers requires clang")
323 }
324
325 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700326 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800327
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700328 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800329 sanitizers = append(sanitizers, "undefined")
Colin Cross16b23492016-01-06 14:41:07 -0800330 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700331 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800332 sanitizers = append(sanitizers,
333 "bool",
334 "integer-divide-by-zero",
335 "return",
336 "returns-nonnull-attribute",
337 "shift-exponent",
338 "unreachable",
339 "vla-bound",
340 // TODO(danalbert): The following checks currently have compiler performance issues.
341 //"alignment",
342 //"bounds",
343 //"enum",
344 //"float-cast-overflow",
345 //"float-divide-by-zero",
346 //"nonnull-attribute",
347 //"null",
348 //"shift-base",
349 //"signed-integer-overflow",
350 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
351 // https://llvm.org/PR19302
352 // http://reviews.llvm.org/D6974
353 // "object-size",
354 )
355 }
356 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
357 }
358
Ivan Lozano651275b2017-06-13 10:24:34 -0700359 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700360 diagSanitizers = append(diagSanitizers, "undefined")
361 }
362
Ivan Lozano651275b2017-06-13 10:24:34 -0700363 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
364
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700365 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700366 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800367 // Frame pointer based unwinder in ASan requires ARM frame setup.
368 // TODO: put in flags?
369 flags.RequiredInstructionSet = "arm"
370 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700371 flags.CFlags = append(flags.CFlags, asanCflags...)
372 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800373
Colin Cross16b23492016-01-06 14:41:07 -0800374 if ctx.Host() {
375 // -nodefaultlibs (provided with libc++) prevents the driver from linking
376 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800377 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
378 } else {
379 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
380 flags.DynamicLinker = "/system/bin/linker_asan"
381 if flags.Toolchain.Is64Bit() {
382 flags.DynamicLinker += "64"
383 }
384 }
385 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700386 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800387 }
388
Yabin Cui6be405e2017-10-19 15:52:11 -0700389 if Bool(sanitize.Properties.Sanitize.Thread) {
390 sanitizers = append(sanitizers, "thread")
391 }
392
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700393 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400394 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800395 }
396
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700397 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700398 sanitizers = append(sanitizers, "safe-stack")
399 }
400
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700401 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800402 if ctx.Arch().ArchType == android.Arm {
403 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
404 // to do this on a function basis, so force Thumb on the entire module.
405 flags.RequiredInstructionSet = "thumb"
406 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700407 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000408
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700409 flags.CFlags = append(flags.CFlags, cfiCflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000410 // Only append the default visibility flag if -fvisibility has not already been set
411 // to hidden.
412 if !inList("-fvisibility=hidden", flags.CFlags) {
413 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
414 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700415 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Zhizhou Yang51be6322018-02-08 18:32:11 -0800416 flags.ArGoldPlugin = true
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700417 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
418 diagSanitizers = append(diagSanitizers, "cfi")
419 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000420
421 if ctx.staticBinary() {
422 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
423 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
424 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700425 }
426
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700427 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700428 sanitizers = append(sanitizers, "unsigned-integer-overflow")
429 sanitizers = append(sanitizers, "signed-integer-overflow")
430 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
431 if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) {
432 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
433 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700434 }
435 }
436
Colin Cross16b23492016-01-06 14:41:07 -0800437 if len(sanitizers) > 0 {
438 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800439
Colin Cross16b23492016-01-06 14:41:07 -0800440 flags.CFlags = append(flags.CFlags, sanitizeArg)
441 if ctx.Host() {
442 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
443 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800444 // Host sanitizers only link symbols in the final executable, so
445 // there will always be undefined symbols in intermediate libraries.
446 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800447 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700448 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800449
450 if enableMinimalRuntime(sanitize) {
451 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
452 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700453 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800454 }
Colin Cross16b23492016-01-06 14:41:07 -0800455 }
456 }
457
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700458 if len(diagSanitizers) > 0 {
Ivan Lozanob7d0f522018-01-20 01:44:38 +0000459 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700460 }
461 // FIXME: enable RTTI if diag + (cfi or vptr)
462
Andreas Gampe97071162017-05-08 13:15:23 -0700463 if sanitize.Properties.Sanitize.Recover != nil {
464 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
465 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
466 }
467
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700468 // Link a runtime library if needed.
469 runtimeLibrary := ""
470 if Bool(sanitize.Properties.Sanitize.Address) {
471 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Yabin Cui6be405e2017-10-19 15:52:11 -0700472 } else if Bool(sanitize.Properties.Sanitize.Thread) {
473 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain())
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700474 } else if len(diagSanitizers) > 0 || sanitize.Properties.UbsanRuntimeDep {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700475 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
476 }
477
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700478 if runtimeLibrary != "" {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700479 runtimeLibraryPath := "${config.ClangAsanLibDir}/" + runtimeLibrary
480 if !ctx.static() {
481 runtimeLibraryPath = runtimeLibraryPath + ctx.toolchain().ShlibSuffix()
482 } else {
483 runtimeLibraryPath = runtimeLibraryPath + ".a"
484 }
485
Jiyong Park27b188b2017-07-18 13:23:39 +0900486 // ASan runtime library must be the first in the link order.
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700487 flags.libFlags = append([]string{runtimeLibraryPath}, flags.libFlags...)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700488 sanitize.runtimeLibrary = runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900489
490 // When linking against VNDK, use the vendor variant of the runtime lib
491 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700492 if ctx.useVndk() {
Jiyong Park27b188b2017-07-18 13:23:39 +0900493 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix
494 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700495 }
496
Colin Cross635c3b02016-05-18 15:37:25 -0700497 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800498 if blacklist.Valid() {
499 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
500 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
501 }
502
503 return flags
504}
505
Colin Cross8ff9ef42017-05-08 13:44:11 -0700506func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross27a4b052017-08-10 16:32:23 -0700507 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Jiyong Park27b188b2017-07-18 13:23:39 +0900508 if sanitize.androidMkRuntimeLibrary != "" {
509 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700510 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700511 })
Vishwath Mohane7128792017-11-17 11:08:10 -0800512
513 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
514 // name conflict.
515 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
516 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000517 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700518}
519
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700520func (sanitize *sanitize) inSanitizerDir() bool {
521 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700522}
523
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000524func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000525 switch t {
526 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000527 return sanitize.Properties.Sanitize.Address
Vishwath Mohan95229302017-08-11 00:53:16 +0000528 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000529 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000530 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000531 return sanitize.Properties.Sanitize.Integer_overflow
532 case cfi:
533 return sanitize.Properties.Sanitize.Cfi
Vishwath Mohan95229302017-08-11 00:53:16 +0000534 default:
535 panic(fmt.Errorf("unknown sanitizerType %d", t))
536 }
537}
538
Dan Albert7d1eecf2018-01-19 12:30:45 -0800539func (sanitize *sanitize) isUnsanitizedVariant() bool {
540 return !sanitize.isSanitizerEnabled(asan) &&
541 !sanitize.isSanitizerEnabled(tsan) &&
542 !sanitize.isSanitizerEnabled(cfi)
543}
544
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700545func (sanitize *sanitize) isVariantOnProductionDevice() bool {
546 return !sanitize.isSanitizerEnabled(asan) &&
547 !sanitize.isSanitizerEnabled(tsan)
548}
549
Colin Cross16b23492016-01-06 14:41:07 -0800550func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
551 switch t {
552 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700553 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700554 if !b {
555 sanitize.Properties.Sanitize.Coverage = nil
556 }
Colin Cross16b23492016-01-06 14:41:07 -0800557 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700558 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700559 case intOverflow:
560 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000561 case cfi:
562 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
563 sanitize.Properties.Sanitize.Diag.Cfi = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800564 default:
565 panic(fmt.Errorf("unknown sanitizerType %d", t))
566 }
567 if b {
568 sanitize.Properties.SanitizerEnabled = true
569 }
570}
571
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000572// Check if the sanitizer is explicitly disabled (as opposed to nil by
573// virtue of not being set).
574func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
575 if sanitize == nil {
576 return false
577 }
578
579 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
580 return sanitizerVal != nil && *sanitizerVal == false
581}
582
583// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
584// because enabling a sanitizer either directly (via the blueprint) or
585// indirectly (via a mutator) sets the bool ptr to true, and you can't
586// distinguish between the cases. It isn't needed though - both cases can be
587// treated identically.
588func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
589 if sanitize == nil {
590 return false
591 }
592
593 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
594 return sanitizerVal != nil && *sanitizerVal == true
595}
596
Colin Cross16b23492016-01-06 14:41:07 -0800597// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700598func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
599 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000600 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Crossd11fcda2017-10-23 17:59:01 -0700601 mctx.VisitDepsDepthFirst(func(module android.Module) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000602 if d, ok := module.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800603 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000604 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
605 if (t == cfi && d.static()) || t != cfi {
606 d.sanitize.Properties.SanitizeDep = true
607 }
Colin Cross16b23492016-01-06 14:41:07 -0800608 }
609 })
610 }
611 }
612}
613
Ivan Lozano30c5db22018-02-21 15:49:20 -0800614// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700615func sanitizerRuntimeDepsMutator() func(android.TopDownMutatorContext) {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800616 return func(mctx android.TopDownMutatorContext) {
617 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
618 mctx.VisitDepsDepthFirst(func(module android.Module) {
619 if d, ok := module.(*Module); ok && d.static() && d.sanitize != nil {
620
Ivan Lozano30c5db22018-02-21 15:49:20 -0800621 if enableMinimalRuntime(d.sanitize) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700622 // If a static dependency is built with the minimal runtime,
623 // make sure we include the ubsan minimal runtime.
Ivan Lozano30c5db22018-02-21 15:49:20 -0800624 c.sanitize.Properties.MinimalRuntimeDep = true
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700625 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
626 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
627 // If a static dependency runs with full ubsan diagnostics,
628 // make sure we include the ubsan runtime.
629 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800630 }
631 }
632 })
633 }
634 }
635}
636
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000637// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700638func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
639 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000640 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000641 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700642 modules := mctx.CreateVariations(t.String())
643 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000644 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
645 // Save original sanitizer status before we assign values to variant
646 // 0 as that overwrites the original.
647 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
648
Colin Crossb0f28952016-09-19 16:46:53 -0700649 modules := mctx.CreateVariations("", t.String())
650 modules[0].(*Module).sanitize.SetSanitizer(t, false)
651 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000652
Colin Crossb0f28952016-09-19 16:46:53 -0700653 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
654 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800655
656 // We don't need both variants active for anything but CFI-enabled
657 // target static libraries, so suppress the appropriate variant in
658 // all other cases.
659 if t == cfi {
660 if c.static() {
661 if !mctx.Device() {
662 if isSanitizerEnabled {
663 modules[0].(*Module).Properties.PreventInstall = true
664 modules[0].(*Module).Properties.HideFromMake = true
665 } else {
666 modules[1].(*Module).Properties.PreventInstall = true
667 modules[1].(*Module).Properties.HideFromMake = true
668 }
669 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800670 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800671
672 cfiStaticLibsMutex.Lock()
673 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
674 cfiStaticLibsMutex.Unlock()
675 }
676 } else {
677 modules[0].(*Module).Properties.PreventInstall = true
678 modules[0].(*Module).Properties.HideFromMake = true
679 }
680 } else if t == asan {
681 if mctx.Device() {
682 // CFI and ASAN are currently mutually exclusive so disable
683 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000684 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
685 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
686 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700687 if isSanitizerEnabled {
688 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800689 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700690 } else {
691 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800692 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700693 }
694 }
Colin Cross16b23492016-01-06 14:41:07 -0800695 }
696 c.sanitize.Properties.SanitizeDep = false
697 }
698 }
699}
Vishwath Mohane7128792017-11-17 11:08:10 -0800700
701func cfiStaticLibs(config android.Config) *[]string {
702 return config.Once("cfiStaticLibs", func() interface{} {
703 return &[]string{}
704 }).(*[]string)
705}
706
Ivan Lozano30c5db22018-02-21 15:49:20 -0800707func enableMinimalRuntime(sanitize *sanitize) bool {
708 if !Bool(sanitize.Properties.Sanitize.Address) &&
709 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
710 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
711 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
712 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
713 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
714 return true
715 }
716 return false
717}
718
Vishwath Mohane7128792017-11-17 11:08:10 -0800719func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
720 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -0800721 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -0800722 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
723}