blob: 535d28ff8111d203215d2a99125d9e19da492418 [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"
42 cfiExportsMap android.Path
43 cfiStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070044
Ivan Lozano30c5db22018-02-21 15:49:20 -080045 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
46 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer", "-fno-sanitize-recover=integer"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070047)
48
Colin Cross16b23492016-01-06 14:41:07 -080049type sanitizerType int
50
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070051func boolPtr(v bool) *bool {
52 if v {
53 return &v
54 } else {
55 return nil
56 }
57}
58
Colin Cross16b23492016-01-06 14:41:07 -080059const (
60 asan sanitizerType = iota + 1
61 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070062 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000063 cfi
Colin Cross16b23492016-01-06 14:41:07 -080064)
65
66func (t sanitizerType) String() string {
67 switch t {
68 case asan:
69 return "asan"
70 case tsan:
71 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070072 case intOverflow:
73 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000074 case cfi:
75 return "cfi"
Colin Cross16b23492016-01-06 14:41:07 -080076 default:
77 panic(fmt.Errorf("unknown sanitizerType %d", t))
78 }
79}
80
81type SanitizeProperties struct {
82 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
83 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -080084 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080085
86 // main sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070087 Address *bool `android:"arch_variant"`
88 Thread *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080089
90 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070091 Undefined *bool `android:"arch_variant"`
92 All_undefined *bool `android:"arch_variant"`
93 Misc_undefined []string `android:"arch_variant"`
94 Coverage *bool `android:"arch_variant"`
95 Safestack *bool `android:"arch_variant"`
96 Cfi *bool `android:"arch_variant"`
97 Integer_overflow *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"`
119 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 Lozano0c3a1ef2017-06-28 09:10:48 -0700203 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800204 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) {
Ivan Lozano5f595532017-07-13 14:46:05 -0700205 s.Integer_overflow = boolPtr(true)
206 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700207 }
208
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000209 if len(globalSanitizers) > 0 {
210 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
211 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700212
213 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
214 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) {
215 s.Diag.Integer_overflow = boolPtr(true)
216 }
217
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700218 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
219 s.Diag.Cfi == nil && Bool(s.Cfi) {
220 s.Diag.Cfi = boolPtr(true)
221 }
222
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700223 if len(globalSanitizersDiag) > 0 {
224 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
225 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700226 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700227
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700228 // Enable CFI for all components in the include paths
Colin Cross6510f912017-11-29 00:27:14 -0800229 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700230 s.Cfi = boolPtr(true)
Colin Cross6510f912017-11-29 00:27:14 -0800231 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700232 s.Diag.Cfi = boolPtr(true)
233 }
234 }
235
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800236 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800237 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800238 s.Cfi = nil
239 s.Diag.Cfi = nil
240 }
241
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800242 // Also disable CFI for arm32 until b/35157333 is fixed.
243 if ctx.Arch().ArchType == android.Arm {
244 s.Cfi = nil
245 s.Diag.Cfi = nil
246 }
247
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700248 // Also disable CFI if ASAN is enabled.
249 if Bool(s.Address) {
250 s.Cfi = nil
251 s.Diag.Cfi = nil
252 }
253
Vishwath Mohane7128792017-11-17 11:08:10 -0800254 // Also disable CFI for host builds.
255 if ctx.Host() {
256 s.Cfi = nil
257 s.Diag.Cfi = nil
258 }
259
Colin Cross3c344ef2016-07-18 15:44:56 -0700260 if ctx.staticBinary() {
261 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700262 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700263 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800264 }
265
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700266 if Bool(s.All_undefined) {
267 s.Undefined = nil
268 }
269
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700270 if !ctx.toolchain().Is64Bit() {
271 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700272 s.Thread = nil
273 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800274 // TODO(ccross): error for compile_multilib = "32"?
275 }
276
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800277 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 -0700278 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 -0700279 sanitize.Properties.SanitizerEnabled = true
280 }
281
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700282 if Bool(s.Coverage) {
283 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800284 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
285 }
286 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000287
288 cfiExportsMap = android.PathForSource(ctx, cfiExportsMapPath)
Colin Cross16b23492016-01-06 14:41:07 -0800289}
290
291func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
292 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
293 return deps
294 }
295
296 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700297 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700298 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800299 }
300 }
301
302 return deps
303}
304
305func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800306 minimalRuntimePath := "${config.ClangAsanLibDir}/" + config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
307
308 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
309 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
310 }
Colin Cross16b23492016-01-06 14:41:07 -0800311 if !sanitize.Properties.SanitizerEnabled {
312 return flags
313 }
314
315 if !ctx.clang() {
316 ctx.ModuleErrorf("Use of sanitizers requires clang")
317 }
318
319 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700320 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800321
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700322 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800323 sanitizers = append(sanitizers, "undefined")
Colin Cross16b23492016-01-06 14:41:07 -0800324 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700325 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800326 sanitizers = append(sanitizers,
327 "bool",
328 "integer-divide-by-zero",
329 "return",
330 "returns-nonnull-attribute",
331 "shift-exponent",
332 "unreachable",
333 "vla-bound",
334 // TODO(danalbert): The following checks currently have compiler performance issues.
335 //"alignment",
336 //"bounds",
337 //"enum",
338 //"float-cast-overflow",
339 //"float-divide-by-zero",
340 //"nonnull-attribute",
341 //"null",
342 //"shift-base",
343 //"signed-integer-overflow",
344 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
345 // https://llvm.org/PR19302
346 // http://reviews.llvm.org/D6974
347 // "object-size",
348 )
349 }
350 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
351 }
352
Ivan Lozano651275b2017-06-13 10:24:34 -0700353 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700354 diagSanitizers = append(diagSanitizers, "undefined")
355 }
356
Ivan Lozano651275b2017-06-13 10:24:34 -0700357 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
358
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700359 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700360 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800361 // Frame pointer based unwinder in ASan requires ARM frame setup.
362 // TODO: put in flags?
363 flags.RequiredInstructionSet = "arm"
364 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700365 flags.CFlags = append(flags.CFlags, asanCflags...)
366 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800367
Colin Cross16b23492016-01-06 14:41:07 -0800368 if ctx.Host() {
369 // -nodefaultlibs (provided with libc++) prevents the driver from linking
370 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800371 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
372 } else {
373 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
374 flags.DynamicLinker = "/system/bin/linker_asan"
375 if flags.Toolchain.Is64Bit() {
376 flags.DynamicLinker += "64"
377 }
378 }
379 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700380 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800381 }
382
Yabin Cui6be405e2017-10-19 15:52:11 -0700383 if Bool(sanitize.Properties.Sanitize.Thread) {
384 sanitizers = append(sanitizers, "thread")
385 }
386
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700387 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400388 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800389 }
390
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700391 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700392 sanitizers = append(sanitizers, "safe-stack")
393 }
394
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700395 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800396 if ctx.Arch().ArchType == android.Arm {
397 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
398 // to do this on a function basis, so force Thumb on the entire module.
399 flags.RequiredInstructionSet = "thumb"
400 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700401 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000402
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700403 flags.CFlags = append(flags.CFlags, cfiCflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000404 // Only append the default visibility flag if -fvisibility has not already been set
405 // to hidden.
406 if !inList("-fvisibility=hidden", flags.CFlags) {
407 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
408 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700409 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Ryan Prichard3ed1f702018-02-05 23:45:16 -0800410 if ctx.Device() {
411 // Work around a bug in Clang. The CFI sanitizer requires LTO, and when
412 // LTO is enabled, the Clang driver fails to enable emutls for Android.
413 // See b/72706604 or https://github.com/android-ndk/ndk/issues/498.
414 flags.LdFlags = append(flags.LdFlags, "-Wl,-plugin-opt,-emulated-tls")
415 }
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) {
428 if !ctx.static() {
429 sanitizers = append(sanitizers, "unsigned-integer-overflow")
430 sanitizers = append(sanitizers, "signed-integer-overflow")
431 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
432 if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) {
433 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
434 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
435 }
436 }
437 }
438
Colin Cross16b23492016-01-06 14:41:07 -0800439 if len(sanitizers) > 0 {
440 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800441
Colin Cross16b23492016-01-06 14:41:07 -0800442 flags.CFlags = append(flags.CFlags, sanitizeArg)
443 if ctx.Host() {
444 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
445 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800446 // Host sanitizers only link symbols in the final executable, so
447 // there will always be undefined symbols in intermediate libraries.
448 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800449 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700450 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800451
452 if enableMinimalRuntime(sanitize) {
453 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
454 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
455 }
Colin Cross16b23492016-01-06 14:41:07 -0800456 }
457 }
458
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700459 if len(diagSanitizers) > 0 {
Ivan Lozanob7d0f522018-01-20 01:44:38 +0000460 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700461 }
462 // FIXME: enable RTTI if diag + (cfi or vptr)
463
Andreas Gampe97071162017-05-08 13:15:23 -0700464 if sanitize.Properties.Sanitize.Recover != nil {
465 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
466 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
467 }
468
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700469 // Link a runtime library if needed.
470 runtimeLibrary := ""
471 if Bool(sanitize.Properties.Sanitize.Address) {
472 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Yabin Cui6be405e2017-10-19 15:52:11 -0700473 } else if Bool(sanitize.Properties.Sanitize.Thread) {
474 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain())
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700475 } else if len(diagSanitizers) > 0 {
476 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
477 }
478
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700479 if runtimeLibrary != "" {
Jiyong Park27b188b2017-07-18 13:23:39 +0900480 // ASan runtime library must be the first in the link order.
Colin Cross8ff9ef42017-05-08 13:44:11 -0700481 flags.libFlags = append([]string{
482 "${config.ClangAsanLibDir}/" + runtimeLibrary + ctx.toolchain().ShlibSuffix(),
483 }, flags.libFlags...)
484 sanitize.runtimeLibrary = runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900485
486 // When linking against VNDK, use the vendor variant of the runtime lib
487 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700488 if ctx.useVndk() {
Jiyong Park27b188b2017-07-18 13:23:39 +0900489 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix
490 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700491 }
492
Colin Cross635c3b02016-05-18 15:37:25 -0700493 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800494 if blacklist.Valid() {
495 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
496 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
497 }
498
499 return flags
500}
501
Colin Cross8ff9ef42017-05-08 13:44:11 -0700502func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross27a4b052017-08-10 16:32:23 -0700503 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Jiyong Park27b188b2017-07-18 13:23:39 +0900504 if sanitize.androidMkRuntimeLibrary != "" {
505 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700506 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700507 })
Vishwath Mohane7128792017-11-17 11:08:10 -0800508
509 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
510 // name conflict.
511 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
512 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000513 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700514}
515
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700516func (sanitize *sanitize) inSanitizerDir() bool {
517 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700518}
519
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000520func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000521 switch t {
522 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000523 return sanitize.Properties.Sanitize.Address
Vishwath Mohan95229302017-08-11 00:53:16 +0000524 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000525 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000526 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000527 return sanitize.Properties.Sanitize.Integer_overflow
528 case cfi:
529 return sanitize.Properties.Sanitize.Cfi
Vishwath Mohan95229302017-08-11 00:53:16 +0000530 default:
531 panic(fmt.Errorf("unknown sanitizerType %d", t))
532 }
533}
534
Dan Albert7d1eecf2018-01-19 12:30:45 -0800535func (sanitize *sanitize) isUnsanitizedVariant() bool {
536 return !sanitize.isSanitizerEnabled(asan) &&
537 !sanitize.isSanitizerEnabled(tsan) &&
538 !sanitize.isSanitizerEnabled(cfi)
539}
540
Colin Cross16b23492016-01-06 14:41:07 -0800541func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
542 switch t {
543 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700544 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700545 if !b {
546 sanitize.Properties.Sanitize.Coverage = nil
547 }
Colin Cross16b23492016-01-06 14:41:07 -0800548 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700549 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700550 case intOverflow:
551 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000552 case cfi:
553 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
554 sanitize.Properties.Sanitize.Diag.Cfi = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800555 default:
556 panic(fmt.Errorf("unknown sanitizerType %d", t))
557 }
558 if b {
559 sanitize.Properties.SanitizerEnabled = true
560 }
561}
562
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000563// Check if the sanitizer is explicitly disabled (as opposed to nil by
564// virtue of not being set).
565func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
566 if sanitize == nil {
567 return false
568 }
569
570 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
571 return sanitizerVal != nil && *sanitizerVal == false
572}
573
574// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
575// because enabling a sanitizer either directly (via the blueprint) or
576// indirectly (via a mutator) sets the bool ptr to true, and you can't
577// distinguish between the cases. It isn't needed though - both cases can be
578// treated identically.
579func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
580 if sanitize == nil {
581 return false
582 }
583
584 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
585 return sanitizerVal != nil && *sanitizerVal == true
586}
587
Colin Cross16b23492016-01-06 14:41:07 -0800588// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700589func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
590 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000591 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Crossd11fcda2017-10-23 17:59:01 -0700592 mctx.VisitDepsDepthFirst(func(module android.Module) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000593 if d, ok := module.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800594 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000595 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
596 if (t == cfi && d.static()) || t != cfi {
597 d.sanitize.Properties.SanitizeDep = true
598 }
Colin Cross16b23492016-01-06 14:41:07 -0800599 }
600 })
601 }
602 }
603}
604
Ivan Lozano30c5db22018-02-21 15:49:20 -0800605// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
606func minimalRuntimeDepsMutator() func(android.TopDownMutatorContext) {
607 return func(mctx android.TopDownMutatorContext) {
608 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
609 mctx.VisitDepsDepthFirst(func(module android.Module) {
610 if d, ok := module.(*Module); ok && d.static() && d.sanitize != nil {
611
612 // If a static dependency will be built with the minimal runtime,
613 // make sure we include the ubsan minimal runtime.
614 if enableMinimalRuntime(d.sanitize) {
615 c.sanitize.Properties.MinimalRuntimeDep = true
616 }
617 }
618 })
619 }
620 }
621}
622
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000623// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700624func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
625 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000626 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000627 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700628 modules := mctx.CreateVariations(t.String())
629 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000630 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
631 // Save original sanitizer status before we assign values to variant
632 // 0 as that overwrites the original.
633 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
634
Colin Crossb0f28952016-09-19 16:46:53 -0700635 modules := mctx.CreateVariations("", t.String())
636 modules[0].(*Module).sanitize.SetSanitizer(t, false)
637 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000638
Colin Crossb0f28952016-09-19 16:46:53 -0700639 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
640 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800641
642 // We don't need both variants active for anything but CFI-enabled
643 // target static libraries, so suppress the appropriate variant in
644 // all other cases.
645 if t == cfi {
646 if c.static() {
647 if !mctx.Device() {
648 if isSanitizerEnabled {
649 modules[0].(*Module).Properties.PreventInstall = true
650 modules[0].(*Module).Properties.HideFromMake = true
651 } else {
652 modules[1].(*Module).Properties.PreventInstall = true
653 modules[1].(*Module).Properties.HideFromMake = true
654 }
655 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800656 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800657
658 cfiStaticLibsMutex.Lock()
659 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
660 cfiStaticLibsMutex.Unlock()
661 }
662 } else {
663 modules[0].(*Module).Properties.PreventInstall = true
664 modules[0].(*Module).Properties.HideFromMake = true
665 }
666 } else if t == asan {
667 if mctx.Device() {
668 // CFI and ASAN are currently mutually exclusive so disable
669 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000670 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
671 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
672 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700673 if isSanitizerEnabled {
674 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800675 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700676 } else {
677 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800678 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700679 }
680 }
Colin Cross16b23492016-01-06 14:41:07 -0800681 }
682 c.sanitize.Properties.SanitizeDep = false
683 }
684 }
685}
Vishwath Mohane7128792017-11-17 11:08:10 -0800686
687func cfiStaticLibs(config android.Config) *[]string {
688 return config.Once("cfiStaticLibs", func() interface{} {
689 return &[]string{}
690 }).(*[]string)
691}
692
Ivan Lozano30c5db22018-02-21 15:49:20 -0800693func enableMinimalRuntime(sanitize *sanitize) bool {
694 if !Bool(sanitize.Properties.Sanitize.Address) &&
695 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
696 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
697 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
698 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
699 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
700 return true
701 }
702 return false
703}
704
Vishwath Mohane7128792017-11-17 11:08:10 -0800705func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
706 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -0800707 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -0800708 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
709}