blob: c9fcafc97bdc08f6a7aae8d727f185fda730a280 [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"`
118 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800119}
120
121type sanitize struct {
122 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700123
Jiyong Park27b188b2017-07-18 13:23:39 +0900124 runtimeLibrary string
125 androidMkRuntimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800126}
127
Vishwath Mohane7128792017-11-17 11:08:10 -0800128func init() {
129 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
130}
131
Colin Cross16b23492016-01-06 14:41:07 -0800132func (sanitize *sanitize) props() []interface{} {
133 return []interface{}{&sanitize.Properties}
134}
135
136func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700137 s := &sanitize.Properties.Sanitize
138
Colin Cross16b23492016-01-06 14:41:07 -0800139 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700140 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800141 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800142 }
143
144 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800145 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800146 return
147 }
148
Colin Cross16b23492016-01-06 14:41:07 -0800149 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700150 var globalSanitizersDiag []string
151
Colin Cross16b23492016-01-06 14:41:07 -0800152 if ctx.clang() {
153 if ctx.Host() {
Colin Cross6510f912017-11-29 00:27:14 -0800154 globalSanitizers = ctx.Config().SanitizeHost()
Colin Cross16b23492016-01-06 14:41:07 -0800155 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800156 arches := ctx.Config().SanitizeDeviceArch()
Colin Cross23ae82a2016-11-02 14:34:39 -0700157 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
Colin Cross6510f912017-11-29 00:27:14 -0800158 globalSanitizers = ctx.Config().SanitizeDevice()
159 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross23ae82a2016-11-02 14:34:39 -0700160 }
Colin Cross16b23492016-01-06 14:41:07 -0800161 }
162 }
163
Colin Cross16b23492016-01-06 14:41:07 -0800164 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000165 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700166 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
167 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000168 }
Colin Cross16b23492016-01-06 14:41:07 -0800169
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700170 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
171 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000172 }
173
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800174 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
175 if s.Address == nil {
176 s.Address = boolPtr(true)
177 } else if *s.Address == false {
178 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
179 // disables address, then disable coverage as well.
180 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
181 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000182 }
183
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700184 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
185 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000186 }
187
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700188 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
189 s.Coverage = boolPtr(true)
190 }
191
192 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
193 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000194 }
195
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700196 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800197 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700198 s.Cfi = boolPtr(true)
199 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700200 }
201
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700202 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800203 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) {
Ivan Lozano5f595532017-07-13 14:46:05 -0700204 s.Integer_overflow = boolPtr(true)
205 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700206 }
207
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000208 if len(globalSanitizers) > 0 {
209 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
210 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700211
212 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
213 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) {
214 s.Diag.Integer_overflow = boolPtr(true)
215 }
216
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700217 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
218 s.Diag.Cfi == nil && Bool(s.Cfi) {
219 s.Diag.Cfi = boolPtr(true)
220 }
221
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700222 if len(globalSanitizersDiag) > 0 {
223 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
224 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700225 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700226
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700227 // Enable CFI for all components in the include paths
Colin Cross6510f912017-11-29 00:27:14 -0800228 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000229 s.Cfi = boolPtr(true)
230 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
231 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700232 }
233 }
234
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800235 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800236 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800237 s.Cfi = nil
238 s.Diag.Cfi = nil
239 }
240
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800241 // Also disable CFI for arm32 until b/35157333 is fixed.
242 if ctx.Arch().ArchType == android.Arm {
243 s.Cfi = nil
244 s.Diag.Cfi = nil
245 }
246
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700247 // Also disable CFI if ASAN is enabled.
248 if Bool(s.Address) {
249 s.Cfi = nil
250 s.Diag.Cfi = nil
251 }
252
Vishwath Mohane7128792017-11-17 11:08:10 -0800253 // Also disable CFI for host builds.
254 if ctx.Host() {
255 s.Cfi = nil
256 s.Diag.Cfi = nil
257 }
258
Colin Cross3c344ef2016-07-18 15:44:56 -0700259 if ctx.staticBinary() {
260 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700261 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700262 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800263 }
264
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700265 if Bool(s.All_undefined) {
266 s.Undefined = nil
267 }
268
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700269 if !ctx.toolchain().Is64Bit() {
270 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700271 s.Thread = nil
272 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800273 // TODO(ccross): error for compile_multilib = "32"?
274 }
275
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800276 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 -0700277 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 -0700278 sanitize.Properties.SanitizerEnabled = true
279 }
280
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700281 if Bool(s.Coverage) {
282 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800283 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
284 }
285 }
286}
287
288func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
289 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
290 return deps
291 }
292
293 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700294 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700295 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800296 }
297 }
298
299 return deps
300}
301
302func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800303 minimalRuntimePath := "${config.ClangAsanLibDir}/" + config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
304
305 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
306 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
307 }
Colin Cross16b23492016-01-06 14:41:07 -0800308 if !sanitize.Properties.SanitizerEnabled {
309 return flags
310 }
311
312 if !ctx.clang() {
313 ctx.ModuleErrorf("Use of sanitizers requires clang")
314 }
315
316 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700317 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800318
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700319 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800320 sanitizers = append(sanitizers, "undefined")
Colin Cross16b23492016-01-06 14:41:07 -0800321 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700322 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800323 sanitizers = append(sanitizers,
324 "bool",
325 "integer-divide-by-zero",
326 "return",
327 "returns-nonnull-attribute",
328 "shift-exponent",
329 "unreachable",
330 "vla-bound",
331 // TODO(danalbert): The following checks currently have compiler performance issues.
332 //"alignment",
333 //"bounds",
334 //"enum",
335 //"float-cast-overflow",
336 //"float-divide-by-zero",
337 //"nonnull-attribute",
338 //"null",
339 //"shift-base",
340 //"signed-integer-overflow",
341 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
342 // https://llvm.org/PR19302
343 // http://reviews.llvm.org/D6974
344 // "object-size",
345 )
346 }
347 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
348 }
349
Ivan Lozano651275b2017-06-13 10:24:34 -0700350 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700351 diagSanitizers = append(diagSanitizers, "undefined")
352 }
353
Ivan Lozano651275b2017-06-13 10:24:34 -0700354 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
355
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700356 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700357 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800358 // Frame pointer based unwinder in ASan requires ARM frame setup.
359 // TODO: put in flags?
360 flags.RequiredInstructionSet = "arm"
361 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700362 flags.CFlags = append(flags.CFlags, asanCflags...)
363 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800364
Colin Cross16b23492016-01-06 14:41:07 -0800365 if ctx.Host() {
366 // -nodefaultlibs (provided with libc++) prevents the driver from linking
367 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800368 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
369 } else {
370 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
371 flags.DynamicLinker = "/system/bin/linker_asan"
372 if flags.Toolchain.Is64Bit() {
373 flags.DynamicLinker += "64"
374 }
375 }
376 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700377 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800378 }
379
Yabin Cui6be405e2017-10-19 15:52:11 -0700380 if Bool(sanitize.Properties.Sanitize.Thread) {
381 sanitizers = append(sanitizers, "thread")
382 }
383
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700384 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400385 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800386 }
387
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700388 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700389 sanitizers = append(sanitizers, "safe-stack")
390 }
391
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700392 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800393 if ctx.Arch().ArchType == android.Arm {
394 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
395 // to do this on a function basis, so force Thumb on the entire module.
396 flags.RequiredInstructionSet = "thumb"
397 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700398 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000399
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700400 flags.CFlags = append(flags.CFlags, cfiCflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000401 // Only append the default visibility flag if -fvisibility has not already been set
402 // to hidden.
403 if !inList("-fvisibility=hidden", flags.CFlags) {
404 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
405 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700406 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Zhizhou Yang51be6322018-02-08 18:32:11 -0800407 flags.ArGoldPlugin = true
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700408 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
409 diagSanitizers = append(diagSanitizers, "cfi")
410 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000411
412 if ctx.staticBinary() {
413 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
414 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
415 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700416 }
417
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700418 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
419 if !ctx.static() {
420 sanitizers = append(sanitizers, "unsigned-integer-overflow")
421 sanitizers = append(sanitizers, "signed-integer-overflow")
422 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
423 if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) {
424 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
425 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
426 }
427 }
428 }
429
Colin Cross16b23492016-01-06 14:41:07 -0800430 if len(sanitizers) > 0 {
431 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800432
Colin Cross16b23492016-01-06 14:41:07 -0800433 flags.CFlags = append(flags.CFlags, sanitizeArg)
434 if ctx.Host() {
435 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
436 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800437 // Host sanitizers only link symbols in the final executable, so
438 // there will always be undefined symbols in intermediate libraries.
439 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800440 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700441 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800442
443 if enableMinimalRuntime(sanitize) {
444 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
445 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
446 }
Colin Cross16b23492016-01-06 14:41:07 -0800447 }
448 }
449
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700450 if len(diagSanitizers) > 0 {
Ivan Lozanob7d0f522018-01-20 01:44:38 +0000451 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700452 }
453 // FIXME: enable RTTI if diag + (cfi or vptr)
454
Andreas Gampe97071162017-05-08 13:15:23 -0700455 if sanitize.Properties.Sanitize.Recover != nil {
456 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
457 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
458 }
459
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700460 // Link a runtime library if needed.
461 runtimeLibrary := ""
462 if Bool(sanitize.Properties.Sanitize.Address) {
463 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Yabin Cui6be405e2017-10-19 15:52:11 -0700464 } else if Bool(sanitize.Properties.Sanitize.Thread) {
465 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain())
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700466 } else if len(diagSanitizers) > 0 {
467 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
468 }
469
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700470 if runtimeLibrary != "" {
Jiyong Park27b188b2017-07-18 13:23:39 +0900471 // ASan runtime library must be the first in the link order.
Colin Cross8ff9ef42017-05-08 13:44:11 -0700472 flags.libFlags = append([]string{
473 "${config.ClangAsanLibDir}/" + runtimeLibrary + ctx.toolchain().ShlibSuffix(),
474 }, flags.libFlags...)
475 sanitize.runtimeLibrary = runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900476
477 // When linking against VNDK, use the vendor variant of the runtime lib
478 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700479 if ctx.useVndk() {
Jiyong Park27b188b2017-07-18 13:23:39 +0900480 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix
481 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700482 }
483
Colin Cross635c3b02016-05-18 15:37:25 -0700484 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800485 if blacklist.Valid() {
486 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
487 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
488 }
489
490 return flags
491}
492
Colin Cross8ff9ef42017-05-08 13:44:11 -0700493func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross27a4b052017-08-10 16:32:23 -0700494 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Jiyong Park27b188b2017-07-18 13:23:39 +0900495 if sanitize.androidMkRuntimeLibrary != "" {
496 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700497 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700498 })
Vishwath Mohane7128792017-11-17 11:08:10 -0800499
500 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
501 // name conflict.
502 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
503 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000504 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700505}
506
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700507func (sanitize *sanitize) inSanitizerDir() bool {
508 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700509}
510
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000511func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000512 switch t {
513 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000514 return sanitize.Properties.Sanitize.Address
Vishwath Mohan95229302017-08-11 00:53:16 +0000515 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000516 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000517 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000518 return sanitize.Properties.Sanitize.Integer_overflow
519 case cfi:
520 return sanitize.Properties.Sanitize.Cfi
Vishwath Mohan95229302017-08-11 00:53:16 +0000521 default:
522 panic(fmt.Errorf("unknown sanitizerType %d", t))
523 }
524}
525
Dan Albert7d1eecf2018-01-19 12:30:45 -0800526func (sanitize *sanitize) isUnsanitizedVariant() bool {
527 return !sanitize.isSanitizerEnabled(asan) &&
528 !sanitize.isSanitizerEnabled(tsan) &&
529 !sanitize.isSanitizerEnabled(cfi)
530}
531
Colin Cross16b23492016-01-06 14:41:07 -0800532func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
533 switch t {
534 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700535 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700536 if !b {
537 sanitize.Properties.Sanitize.Coverage = nil
538 }
Colin Cross16b23492016-01-06 14:41:07 -0800539 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700540 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700541 case intOverflow:
542 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000543 case cfi:
544 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
545 sanitize.Properties.Sanitize.Diag.Cfi = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800546 default:
547 panic(fmt.Errorf("unknown sanitizerType %d", t))
548 }
549 if b {
550 sanitize.Properties.SanitizerEnabled = true
551 }
552}
553
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000554// Check if the sanitizer is explicitly disabled (as opposed to nil by
555// virtue of not being set).
556func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
557 if sanitize == nil {
558 return false
559 }
560
561 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
562 return sanitizerVal != nil && *sanitizerVal == false
563}
564
565// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
566// because enabling a sanitizer either directly (via the blueprint) or
567// indirectly (via a mutator) sets the bool ptr to true, and you can't
568// distinguish between the cases. It isn't needed though - both cases can be
569// treated identically.
570func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
571 if sanitize == nil {
572 return false
573 }
574
575 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
576 return sanitizerVal != nil && *sanitizerVal == true
577}
578
Colin Cross16b23492016-01-06 14:41:07 -0800579// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700580func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
581 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000582 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Crossd11fcda2017-10-23 17:59:01 -0700583 mctx.VisitDepsDepthFirst(func(module android.Module) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000584 if d, ok := module.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800585 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000586 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
587 if (t == cfi && d.static()) || t != cfi {
588 d.sanitize.Properties.SanitizeDep = true
589 }
Colin Cross16b23492016-01-06 14:41:07 -0800590 }
591 })
592 }
593 }
594}
595
Ivan Lozano30c5db22018-02-21 15:49:20 -0800596// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
597func minimalRuntimeDepsMutator() func(android.TopDownMutatorContext) {
598 return func(mctx android.TopDownMutatorContext) {
599 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
600 mctx.VisitDepsDepthFirst(func(module android.Module) {
601 if d, ok := module.(*Module); ok && d.static() && d.sanitize != nil {
602
603 // If a static dependency will be built with the minimal runtime,
604 // make sure we include the ubsan minimal runtime.
605 if enableMinimalRuntime(d.sanitize) {
606 c.sanitize.Properties.MinimalRuntimeDep = true
607 }
608 }
609 })
610 }
611 }
612}
613
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000614// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700615func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
616 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000617 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000618 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700619 modules := mctx.CreateVariations(t.String())
620 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000621 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
622 // Save original sanitizer status before we assign values to variant
623 // 0 as that overwrites the original.
624 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
625
Colin Crossb0f28952016-09-19 16:46:53 -0700626 modules := mctx.CreateVariations("", t.String())
627 modules[0].(*Module).sanitize.SetSanitizer(t, false)
628 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000629
Colin Crossb0f28952016-09-19 16:46:53 -0700630 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
631 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800632
633 // We don't need both variants active for anything but CFI-enabled
634 // target static libraries, so suppress the appropriate variant in
635 // all other cases.
636 if t == cfi {
637 if c.static() {
638 if !mctx.Device() {
639 if isSanitizerEnabled {
640 modules[0].(*Module).Properties.PreventInstall = true
641 modules[0].(*Module).Properties.HideFromMake = true
642 } else {
643 modules[1].(*Module).Properties.PreventInstall = true
644 modules[1].(*Module).Properties.HideFromMake = true
645 }
646 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800647 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800648
649 cfiStaticLibsMutex.Lock()
650 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
651 cfiStaticLibsMutex.Unlock()
652 }
653 } else {
654 modules[0].(*Module).Properties.PreventInstall = true
655 modules[0].(*Module).Properties.HideFromMake = true
656 }
657 } else if t == asan {
658 if mctx.Device() {
659 // CFI and ASAN are currently mutually exclusive so disable
660 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000661 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
662 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
663 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700664 if isSanitizerEnabled {
665 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800666 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700667 } else {
668 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800669 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700670 }
671 }
Colin Cross16b23492016-01-06 14:41:07 -0800672 }
673 c.sanitize.Properties.SanitizeDep = false
674 }
675 }
676}
Vishwath Mohane7128792017-11-17 11:08:10 -0800677
678func cfiStaticLibs(config android.Config) *[]string {
679 return config.Once("cfiStaticLibs", func() interface{} {
680 return &[]string{}
681 }).(*[]string)
682}
683
Ivan Lozano30c5db22018-02-21 15:49:20 -0800684func enableMinimalRuntime(sanitize *sanitize) bool {
685 if !Bool(sanitize.Properties.Sanitize.Address) &&
686 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
687 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
688 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
689 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
690 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
691 return true
692 }
693 return false
694}
695
Vishwath Mohane7128792017-11-17 11:08:10 -0800696func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
697 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -0800698 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -0800699 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
700}