blob: a522a6057cc93b0cc83759bb489cef131e7780d4 [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 Cross6b753602018-06-21 13:03:07 -070024 "github.com/google/blueprint"
25
Colin Cross635c3b02016-05-18 15:37:25 -070026 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070027 "android/soong/cc/config"
Colin Cross16b23492016-01-06 14:41:07 -080028)
29
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070030var (
31 // Any C flags added by sanitizer which libTooling tools may not
32 // understand also need to be added to ClangLibToolingUnknownCflags in
33 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080034
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070035 asanCflags = []string{"-fno-omit-frame-pointer"}
36 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
37 asanLibs = []string{"libasan"}
38
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070039 hwasanCflags = []string{"-mllvm", "-hwasan-with-ifunc=0", "-fno-omit-frame-pointer", "-Wno-frame-larger-than="}
40
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000041 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070042 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070043 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070044 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070045 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
46 cfiStaticLibsMutex sync.Mutex
47 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070048
Ivan Lozano30c5db22018-02-21 15:49:20 -080049 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
50 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer", "-fno-sanitize-recover=integer"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070051)
52
Colin Cross16b23492016-01-06 14:41:07 -080053type sanitizerType int
54
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070055func boolPtr(v bool) *bool {
56 if v {
57 return &v
58 } else {
59 return nil
60 }
61}
62
Colin Cross16b23492016-01-06 14:41:07 -080063const (
64 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070065 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080066 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070067 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000068 cfi
Colin Cross16b23492016-01-06 14:41:07 -080069)
70
71func (t sanitizerType) String() string {
72 switch t {
73 case asan:
74 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070075 case hwasan:
76 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080077 case tsan:
78 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070079 case intOverflow:
80 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000081 case cfi:
82 return "cfi"
Colin Cross16b23492016-01-06 14:41:07 -080083 default:
84 panic(fmt.Errorf("unknown sanitizerType %d", t))
85 }
86}
87
88type SanitizeProperties struct {
89 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
90 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -080091 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080092
93 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070094 Address *bool `android:"arch_variant"`
95 Thread *bool `android:"arch_variant"`
96 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080097
98 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070099 Undefined *bool `android:"arch_variant"`
100 All_undefined *bool `android:"arch_variant"`
101 Misc_undefined []string `android:"arch_variant"`
102 Coverage *bool `android:"arch_variant"`
103 Safestack *bool `android:"arch_variant"`
104 Cfi *bool `android:"arch_variant"`
105 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700106 Scudo *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800107
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700108 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
109 // Replaces abort() on error with a human-readable error message.
110 // Address and Thread sanitizers always run in diagnostic mode.
111 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700112 Undefined *bool `android:"arch_variant"`
113 Cfi *bool `android:"arch_variant"`
114 Integer_overflow *bool `android:"arch_variant"`
115 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700116 }
117
118 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800119 Recover []string
120
121 // value to pass to -fsanitize-blacklist
122 Blacklist *string
123 } `android:"arch_variant"`
124
Ivan Lozano30c5db22018-02-21 15:49:20 -0800125 SanitizerEnabled bool `blueprint:"mutated"`
126 SanitizeDep bool `blueprint:"mutated"`
127 MinimalRuntimeDep bool `blueprint:"mutated"`
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700128 UbsanRuntimeDep bool `blueprint:"mutated"`
Ivan Lozano30c5db22018-02-21 15:49:20 -0800129 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800130}
131
132type sanitize struct {
133 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700134
Jiyong Park27b188b2017-07-18 13:23:39 +0900135 runtimeLibrary string
136 androidMkRuntimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800137}
138
Vishwath Mohane7128792017-11-17 11:08:10 -0800139func init() {
140 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700141 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800142}
143
Colin Cross16b23492016-01-06 14:41:07 -0800144func (sanitize *sanitize) props() []interface{} {
145 return []interface{}{&sanitize.Properties}
146}
147
148func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700149 s := &sanitize.Properties.Sanitize
150
Colin Cross16b23492016-01-06 14:41:07 -0800151 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700152 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800153 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800154 }
155
156 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800157 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800158 return
159 }
160
Colin Cross16b23492016-01-06 14:41:07 -0800161 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700162 var globalSanitizersDiag []string
163
Colin Cross16b23492016-01-06 14:41:07 -0800164 if ctx.clang() {
165 if ctx.Host() {
Pirama Arumuga Nainar83d716c2018-06-26 14:01:40 -0700166 if !ctx.Windows() {
167 globalSanitizers = ctx.Config().SanitizeHost()
168 }
Colin Cross16b23492016-01-06 14:41:07 -0800169 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800170 arches := ctx.Config().SanitizeDeviceArch()
Colin Cross23ae82a2016-11-02 14:34:39 -0700171 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
Colin Cross6510f912017-11-29 00:27:14 -0800172 globalSanitizers = ctx.Config().SanitizeDevice()
173 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross23ae82a2016-11-02 14:34:39 -0700174 }
Colin Cross16b23492016-01-06 14:41:07 -0800175 }
176 }
177
Colin Cross16b23492016-01-06 14:41:07 -0800178 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000179 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700180 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
181 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000182 }
Colin Cross16b23492016-01-06 14:41:07 -0800183
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700184 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
185 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000186 }
187
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800188 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
189 if s.Address == nil {
190 s.Address = boolPtr(true)
191 } else if *s.Address == false {
192 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
193 // disables address, then disable coverage as well.
194 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
195 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000196 }
197
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700198 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
199 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000200 }
201
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700202 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
203 s.Coverage = boolPtr(true)
204 }
205
206 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
207 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000208 }
209
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700210 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800211 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700212 s.Cfi = boolPtr(true)
213 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700214 }
215
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700216 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700217 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700218 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700219 s.Integer_overflow = boolPtr(true)
220 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700221 }
222
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700223 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
224 s.Scudo = boolPtr(true)
225 }
226
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700227 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
228 s.Hwaddress = boolPtr(true)
229 }
230
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000231 if len(globalSanitizers) > 0 {
232 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
233 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700234
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700235 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700236 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700237 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700238 s.Diag.Integer_overflow = boolPtr(true)
239 }
240
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700241 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
242 s.Diag.Cfi == nil && Bool(s.Cfi) {
243 s.Diag.Cfi = boolPtr(true)
244 }
245
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700246 if len(globalSanitizersDiag) > 0 {
247 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
248 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700249 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700250
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700251 // Enable CFI for all components in the include paths (for Aarch64 only)
252 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000253 s.Cfi = boolPtr(true)
254 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
255 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700256 }
257 }
258
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800259 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800260 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800261 s.Cfi = nil
262 s.Diag.Cfi = nil
263 }
264
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800265 // Also disable CFI for arm32 until b/35157333 is fixed.
266 if ctx.Arch().ArchType == android.Arm {
267 s.Cfi = nil
268 s.Diag.Cfi = nil
269 }
270
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700271 // HWASan requires AArch64 hardware feature (top-byte-ignore).
272 if ctx.Arch().ArchType != android.Arm64 {
273 s.Hwaddress = nil
274 }
275
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700276 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700277 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700278 s.Cfi = nil
279 s.Diag.Cfi = nil
280 }
281
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700282 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800283 if ctx.Host() {
284 s.Cfi = nil
285 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700286 s.Misc_undefined = nil
287 s.Undefined = nil
288 s.All_undefined = nil
289 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800290 }
291
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700292 // Also disable CFI for VNDK variants of components
293 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700294 s.Cfi = nil
295 s.Diag.Cfi = nil
296 }
297
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700298 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
299 if ctx.inRecovery() {
300 s.Hwaddress = nil
301 }
302
Colin Cross3c344ef2016-07-18 15:44:56 -0700303 if ctx.staticBinary() {
304 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700305 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700306 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800307 }
308
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700309 if Bool(s.All_undefined) {
310 s.Undefined = nil
311 }
312
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700313 if !ctx.toolchain().Is64Bit() {
314 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700315 s.Thread = nil
316 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800317 // TODO(ccross): error for compile_multilib = "32"?
318 }
319
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800320 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700321 Bool(s.Coverage) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700322 Bool(s.Scudo) || Bool(s.Hwaddress)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700323 sanitize.Properties.SanitizerEnabled = true
324 }
325
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700326 // Disable Scudo if ASan or TSan is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700327 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700328 s.Scudo = nil
329 }
330
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700331 if Bool(s.Hwaddress) {
332 s.Address = nil
333 s.Thread = nil
334 }
335
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700336 if Bool(s.Coverage) {
337 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800338 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
339 }
340 }
341}
342
343func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
344 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
345 return deps
346 }
347
348 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700349 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700350 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800351 }
352 }
353
354 return deps
355}
356
357func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700358 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
359 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800360
361 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
362 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700363 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800364 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700365 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800366 return flags
367 }
368
369 if !ctx.clang() {
370 ctx.ModuleErrorf("Use of sanitizers requires clang")
371 }
372
373 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700374 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800375
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700376 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800377 sanitizers = append(sanitizers, "undefined")
Colin Cross16b23492016-01-06 14:41:07 -0800378 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700379 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800380 sanitizers = append(sanitizers,
381 "bool",
382 "integer-divide-by-zero",
383 "return",
384 "returns-nonnull-attribute",
385 "shift-exponent",
386 "unreachable",
387 "vla-bound",
388 // TODO(danalbert): The following checks currently have compiler performance issues.
389 //"alignment",
390 //"bounds",
391 //"enum",
392 //"float-cast-overflow",
393 //"float-divide-by-zero",
394 //"nonnull-attribute",
395 //"null",
396 //"shift-base",
397 //"signed-integer-overflow",
398 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
399 // https://llvm.org/PR19302
400 // http://reviews.llvm.org/D6974
401 // "object-size",
402 )
403 }
404 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
405 }
406
Ivan Lozano651275b2017-06-13 10:24:34 -0700407 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700408 diagSanitizers = append(diagSanitizers, "undefined")
409 }
410
Ivan Lozano651275b2017-06-13 10:24:34 -0700411 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
412
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700413 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700414 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800415 // Frame pointer based unwinder in ASan requires ARM frame setup.
416 // TODO: put in flags?
417 flags.RequiredInstructionSet = "arm"
418 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700419 flags.CFlags = append(flags.CFlags, asanCflags...)
420 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800421
Colin Cross16b23492016-01-06 14:41:07 -0800422 if ctx.Host() {
423 // -nodefaultlibs (provided with libc++) prevents the driver from linking
424 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800425 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
426 } else {
427 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
428 flags.DynamicLinker = "/system/bin/linker_asan"
429 if flags.Toolchain.Is64Bit() {
430 flags.DynamicLinker += "64"
431 }
432 }
433 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700434 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800435 }
436
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700437 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
438 flags.CFlags = append(flags.CFlags, hwasanCflags...)
439 sanitizers = append(sanitizers, "hwaddress")
440 }
441
Yabin Cui6be405e2017-10-19 15:52:11 -0700442 if Bool(sanitize.Properties.Sanitize.Thread) {
443 sanitizers = append(sanitizers, "thread")
444 }
445
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700446 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400447 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800448 }
449
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700450 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700451 sanitizers = append(sanitizers, "safe-stack")
452 }
453
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700454 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800455 if ctx.Arch().ArchType == android.Arm {
456 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
457 // to do this on a function basis, so force Thumb on the entire module.
458 flags.RequiredInstructionSet = "thumb"
459 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700460 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000461
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700462 flags.CFlags = append(flags.CFlags, cfiCflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000463 // Only append the default visibility flag if -fvisibility has not already been set
464 // to hidden.
465 if !inList("-fvisibility=hidden", flags.CFlags) {
466 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
467 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700468 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Zhizhou Yang51be6322018-02-08 18:32:11 -0800469 flags.ArGoldPlugin = true
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700470 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
471 diagSanitizers = append(diagSanitizers, "cfi")
472 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000473
474 if ctx.staticBinary() {
475 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
476 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
477 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700478 }
479
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700480 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700481 sanitizers = append(sanitizers, "unsigned-integer-overflow")
482 sanitizers = append(sanitizers, "signed-integer-overflow")
483 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
484 if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) {
485 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
486 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700487 }
488 }
489
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700490 if Bool(sanitize.Properties.Sanitize.Scudo) {
491 sanitizers = append(sanitizers, "scudo")
492 }
493
Colin Cross16b23492016-01-06 14:41:07 -0800494 if len(sanitizers) > 0 {
495 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800496
Colin Cross16b23492016-01-06 14:41:07 -0800497 flags.CFlags = append(flags.CFlags, sanitizeArg)
498 if ctx.Host() {
499 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
500 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800501 // Host sanitizers only link symbols in the final executable, so
502 // there will always be undefined symbols in intermediate libraries.
503 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800504 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700505 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800506
507 if enableMinimalRuntime(sanitize) {
508 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
509 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700510 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800511 }
Colin Cross16b23492016-01-06 14:41:07 -0800512 }
513 }
514
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700515 if len(diagSanitizers) > 0 {
Ivan Lozanob7d0f522018-01-20 01:44:38 +0000516 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700517 }
518 // FIXME: enable RTTI if diag + (cfi or vptr)
519
Andreas Gampe97071162017-05-08 13:15:23 -0700520 if sanitize.Properties.Sanitize.Recover != nil {
521 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
522 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
523 }
524
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700525 // Link a runtime library if needed.
526 runtimeLibrary := ""
527 if Bool(sanitize.Properties.Sanitize.Address) {
528 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700529 } else if Bool(sanitize.Properties.Sanitize.Hwaddress) {
530 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(ctx.toolchain())
Yabin Cui6be405e2017-10-19 15:52:11 -0700531 } else if Bool(sanitize.Properties.Sanitize.Thread) {
532 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain())
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700533 } else if Bool(sanitize.Properties.Sanitize.Scudo) {
534 runtimeLibrary = config.ScudoRuntimeLibrary(ctx.toolchain())
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700535 } else if len(diagSanitizers) > 0 || sanitize.Properties.UbsanRuntimeDep {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700536 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
537 }
538
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700539 if runtimeLibrary != "" {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700540 runtimeLibraryPath := "${config.ClangAsanLibDir}/" + runtimeLibrary
541 if !ctx.static() {
542 runtimeLibraryPath = runtimeLibraryPath + ctx.toolchain().ShlibSuffix()
543 } else {
544 runtimeLibraryPath = runtimeLibraryPath + ".a"
545 }
546
Jiyong Park27b188b2017-07-18 13:23:39 +0900547 // ASan runtime library must be the first in the link order.
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700548 flags.libFlags = append([]string{runtimeLibraryPath}, flags.libFlags...)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700549 sanitize.runtimeLibrary = runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900550
551 // When linking against VNDK, use the vendor variant of the runtime lib
552 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700553 if ctx.useVndk() {
Jiyong Park27b188b2017-07-18 13:23:39 +0900554 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix
555 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700556 }
557
Colin Cross635c3b02016-05-18 15:37:25 -0700558 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800559 if blacklist.Valid() {
560 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
561 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
562 }
563
564 return flags
565}
566
Colin Cross8ff9ef42017-05-08 13:44:11 -0700567func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross27a4b052017-08-10 16:32:23 -0700568 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Jiyong Park27b188b2017-07-18 13:23:39 +0900569 if sanitize.androidMkRuntimeLibrary != "" {
570 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700571 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700572 })
Vishwath Mohane7128792017-11-17 11:08:10 -0800573
574 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
575 // name conflict.
576 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
577 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000578 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700579 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
580 ret.SubName += ".hwasan"
581 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700582}
583
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700584func (sanitize *sanitize) inSanitizerDir() bool {
585 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700586}
587
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000588func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000589 switch t {
590 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000591 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700592 case hwasan:
593 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000594 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000595 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000596 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000597 return sanitize.Properties.Sanitize.Integer_overflow
598 case cfi:
599 return sanitize.Properties.Sanitize.Cfi
Vishwath Mohan95229302017-08-11 00:53:16 +0000600 default:
601 panic(fmt.Errorf("unknown sanitizerType %d", t))
602 }
603}
604
Dan Albert7d1eecf2018-01-19 12:30:45 -0800605func (sanitize *sanitize) isUnsanitizedVariant() bool {
606 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700607 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800608 !sanitize.isSanitizerEnabled(tsan) &&
609 !sanitize.isSanitizerEnabled(cfi)
610}
611
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700612func (sanitize *sanitize) isVariantOnProductionDevice() bool {
613 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700614 !sanitize.isSanitizerEnabled(hwasan) &&
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700615 !sanitize.isSanitizerEnabled(tsan)
616}
617
Colin Cross16b23492016-01-06 14:41:07 -0800618func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
619 switch t {
620 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700621 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700622 if !b {
623 sanitize.Properties.Sanitize.Coverage = nil
624 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700625 case hwasan:
626 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800627 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700628 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700629 case intOverflow:
630 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000631 case cfi:
632 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800633 default:
634 panic(fmt.Errorf("unknown sanitizerType %d", t))
635 }
636 if b {
637 sanitize.Properties.SanitizerEnabled = true
638 }
639}
640
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000641// Check if the sanitizer is explicitly disabled (as opposed to nil by
642// virtue of not being set).
643func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
644 if sanitize == nil {
645 return false
646 }
647
648 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
649 return sanitizerVal != nil && *sanitizerVal == false
650}
651
652// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
653// because enabling a sanitizer either directly (via the blueprint) or
654// indirectly (via a mutator) sets the bool ptr to true, and you can't
655// distinguish between the cases. It isn't needed though - both cases can be
656// treated identically.
657func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
658 if sanitize == nil {
659 return false
660 }
661
662 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
663 return sanitizerVal != nil && *sanitizerVal == true
664}
665
Colin Cross6b753602018-06-21 13:03:07 -0700666func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
667 t, ok := tag.(dependencyTag)
668 return ok && t.library || t == reuseObjTag
669}
670
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700671// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700672func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
673 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000674 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700675 mctx.WalkDeps(func(child, parent android.Module) bool {
676 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
677 return false
678 }
679 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800680 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000681 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700682 if t == cfi || t == hwasan {
683 if d.static() {
684 d.sanitize.Properties.SanitizeDep = true
685 }
686 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000687 d.sanitize.Properties.SanitizeDep = true
688 }
Colin Cross16b23492016-01-06 14:41:07 -0800689 }
Colin Cross6b753602018-06-21 13:03:07 -0700690 return true
Colin Cross16b23492016-01-06 14:41:07 -0800691 })
692 }
693 }
694}
695
Ivan Lozano30c5db22018-02-21 15:49:20 -0800696// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700697func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
698 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
699 mctx.WalkDeps(func(child, parent android.Module) bool {
700 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
701 return false
702 }
703 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800704
Colin Cross6b753602018-06-21 13:03:07 -0700705 if enableMinimalRuntime(d.sanitize) {
706 // If a static dependency is built with the minimal runtime,
707 // make sure we include the ubsan minimal runtime.
708 c.sanitize.Properties.MinimalRuntimeDep = true
709 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
710 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
711 // If a static dependency runs with full ubsan diagnostics,
712 // make sure we include the ubsan runtime.
713 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800714 }
Colin Cross6b753602018-06-21 13:03:07 -0700715 }
716 return true
717 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800718 }
719}
720
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000721// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700722func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
723 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000724 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000725 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700726 modules := mctx.CreateVariations(t.String())
727 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000728 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
729 // Save original sanitizer status before we assign values to variant
730 // 0 as that overwrites the original.
731 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
732
Colin Crossb0f28952016-09-19 16:46:53 -0700733 modules := mctx.CreateVariations("", t.String())
734 modules[0].(*Module).sanitize.SetSanitizer(t, false)
735 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000736
Colin Crossb0f28952016-09-19 16:46:53 -0700737 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
738 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800739
740 // We don't need both variants active for anything but CFI-enabled
741 // target static libraries, so suppress the appropriate variant in
742 // all other cases.
743 if t == cfi {
744 if c.static() {
745 if !mctx.Device() {
746 if isSanitizerEnabled {
747 modules[0].(*Module).Properties.PreventInstall = true
748 modules[0].(*Module).Properties.HideFromMake = true
749 } else {
750 modules[1].(*Module).Properties.PreventInstall = true
751 modules[1].(*Module).Properties.HideFromMake = true
752 }
753 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800754 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800755
756 cfiStaticLibsMutex.Lock()
757 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
758 cfiStaticLibsMutex.Unlock()
759 }
760 } else {
761 modules[0].(*Module).Properties.PreventInstall = true
762 modules[0].(*Module).Properties.HideFromMake = true
763 }
764 } else if t == asan {
765 if mctx.Device() {
766 // CFI and ASAN are currently mutually exclusive so disable
767 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000768 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
769 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
770 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700771 if isSanitizerEnabled {
772 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800773 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700774 } else {
775 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800776 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700777 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700778 } else if t == hwasan {
779 if mctx.Device() {
780 // CFI and HWASAN are currently mutually exclusive so disable
781 // CFI if this is an HWASAN variant.
782 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
783 }
784
785 if c.static() {
786 if c.useVndk() {
787 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
788 hwasanStaticLibsMutex.Lock()
789 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
790 hwasanStaticLibsMutex.Unlock()
791 } else {
792 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
793 hwasanStaticLibsMutex.Lock()
794 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
795 hwasanStaticLibsMutex.Unlock()
796 }
797 } else {
798 if isSanitizerEnabled {
799 modules[0].(*Module).Properties.PreventInstall = true
800 modules[0].(*Module).Properties.HideFromMake = true
801 } else {
802 modules[1].(*Module).Properties.PreventInstall = true
803 modules[1].(*Module).Properties.HideFromMake = true
804 }
805 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700806 }
Colin Cross16b23492016-01-06 14:41:07 -0800807 }
808 c.sanitize.Properties.SanitizeDep = false
809 }
810 }
811}
Vishwath Mohane7128792017-11-17 11:08:10 -0800812
813func cfiStaticLibs(config android.Config) *[]string {
814 return config.Once("cfiStaticLibs", func() interface{} {
815 return &[]string{}
816 }).(*[]string)
817}
818
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700819func hwasanStaticLibs(config android.Config) *[]string {
820 return config.Once("hwasanStaticLibs", func() interface{} {
821 return &[]string{}
822 }).(*[]string)
823}
824
825func hwasanVendorStaticLibs(config android.Config) *[]string {
826 return config.Once("hwasanVendorStaticLibs", func() interface{} {
827 return &[]string{}
828 }).(*[]string)
829}
830
Ivan Lozano30c5db22018-02-21 15:49:20 -0800831func enableMinimalRuntime(sanitize *sanitize) bool {
832 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700833 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700834 !Bool(sanitize.Properties.Sanitize.Scudo) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -0800835 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
836 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
837 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
838 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
839 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
840 return true
841 }
842 return false
843}
844
Vishwath Mohane7128792017-11-17 11:08:10 -0800845func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
846 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -0800847 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -0800848 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
849}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700850
851func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
852 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
853 sort.Strings(*hwasanStaticLibs)
854 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
855
856 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
857 sort.Strings(*hwasanVendorStaticLibs)
858 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
859}