blob: 5dcaf76c976732a88de38da20976f9261d1deaca [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"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070043 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
44 // used, but have no effect on assembly files
45 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070046 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070047 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070048 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
49 cfiStaticLibsMutex sync.Mutex
50 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070051
Ivan Lozano30c5db22018-02-21 15:49:20 -080052 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
53 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer", "-fno-sanitize-recover=integer"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070054)
55
Colin Cross16b23492016-01-06 14:41:07 -080056type sanitizerType int
57
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070058func boolPtr(v bool) *bool {
59 if v {
60 return &v
61 } else {
62 return nil
63 }
64}
65
Colin Cross16b23492016-01-06 14:41:07 -080066const (
67 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070068 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080069 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070070 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000071 cfi
Colin Cross16b23492016-01-06 14:41:07 -080072)
73
74func (t sanitizerType) String() string {
75 switch t {
76 case asan:
77 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070078 case hwasan:
79 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080080 case tsan:
81 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070082 case intOverflow:
83 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000084 case cfi:
85 return "cfi"
Colin Cross16b23492016-01-06 14:41:07 -080086 default:
87 panic(fmt.Errorf("unknown sanitizerType %d", t))
88 }
89}
90
91type SanitizeProperties struct {
92 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
93 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -080094 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080095
96 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070097 Address *bool `android:"arch_variant"`
98 Thread *bool `android:"arch_variant"`
99 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800100
101 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700102 Undefined *bool `android:"arch_variant"`
103 All_undefined *bool `android:"arch_variant"`
104 Misc_undefined []string `android:"arch_variant"`
105 Coverage *bool `android:"arch_variant"`
106 Safestack *bool `android:"arch_variant"`
107 Cfi *bool `android:"arch_variant"`
108 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700109 Scudo *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800110
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700111 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
112 // Replaces abort() on error with a human-readable error message.
113 // Address and Thread sanitizers always run in diagnostic mode.
114 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700115 Undefined *bool `android:"arch_variant"`
116 Cfi *bool `android:"arch_variant"`
117 Integer_overflow *bool `android:"arch_variant"`
118 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700119 }
120
121 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800122 Recover []string
123
124 // value to pass to -fsanitize-blacklist
125 Blacklist *string
126 } `android:"arch_variant"`
127
Ivan Lozano30c5db22018-02-21 15:49:20 -0800128 SanitizerEnabled bool `blueprint:"mutated"`
129 SanitizeDep bool `blueprint:"mutated"`
130 MinimalRuntimeDep bool `blueprint:"mutated"`
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700131 UbsanRuntimeDep bool `blueprint:"mutated"`
Ivan Lozano30c5db22018-02-21 15:49:20 -0800132 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800133}
134
135type sanitize struct {
136 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700137
Jiyong Park27b188b2017-07-18 13:23:39 +0900138 runtimeLibrary string
139 androidMkRuntimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800140}
141
Vishwath Mohane7128792017-11-17 11:08:10 -0800142func init() {
143 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700144 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800145}
146
Colin Cross16b23492016-01-06 14:41:07 -0800147func (sanitize *sanitize) props() []interface{} {
148 return []interface{}{&sanitize.Properties}
149}
150
151func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700152 s := &sanitize.Properties.Sanitize
153
Colin Cross16b23492016-01-06 14:41:07 -0800154 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700155 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800156 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800157 }
158
159 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800160 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800161 return
162 }
163
Colin Cross16b23492016-01-06 14:41:07 -0800164 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700165 var globalSanitizersDiag []string
166
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700167 if ctx.Host() {
168 if !ctx.Windows() {
169 globalSanitizers = ctx.Config().SanitizeHost()
170 }
171 } else {
172 arches := ctx.Config().SanitizeDeviceArch()
173 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
174 globalSanitizers = ctx.Config().SanitizeDevice()
175 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800176 }
177 }
178
Colin Cross16b23492016-01-06 14:41:07 -0800179 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000180 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700181 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
182 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000183 }
Colin Cross16b23492016-01-06 14:41:07 -0800184
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700185 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
186 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000187 }
188
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800189 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
190 if s.Address == nil {
191 s.Address = boolPtr(true)
192 } else if *s.Address == false {
193 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
194 // disables address, then disable coverage as well.
195 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
196 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000197 }
198
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700199 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
200 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000201 }
202
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700203 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
204 s.Coverage = boolPtr(true)
205 }
206
207 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
208 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000209 }
210
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700211 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800212 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700213 s.Cfi = boolPtr(true)
214 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700215 }
216
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700217 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700218 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700219 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700220 s.Integer_overflow = boolPtr(true)
221 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700222 }
223
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700224 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
225 s.Scudo = boolPtr(true)
226 }
227
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700228 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
229 s.Hwaddress = boolPtr(true)
230 }
231
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000232 if len(globalSanitizers) > 0 {
233 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
234 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700235
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700236 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700237 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700238 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700239 s.Diag.Integer_overflow = boolPtr(true)
240 }
241
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700242 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
243 s.Diag.Cfi == nil && Bool(s.Cfi) {
244 s.Diag.Cfi = boolPtr(true)
245 }
246
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700247 if len(globalSanitizersDiag) > 0 {
248 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
249 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700250 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700251
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700252 // Enable CFI for all components in the include paths (for Aarch64 only)
253 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000254 s.Cfi = boolPtr(true)
255 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
256 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700257 }
258 }
259
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800260 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800261 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800262 s.Cfi = nil
263 s.Diag.Cfi = nil
264 }
265
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800266 // Also disable CFI for arm32 until b/35157333 is fixed.
267 if ctx.Arch().ArchType == android.Arm {
268 s.Cfi = nil
269 s.Diag.Cfi = nil
270 }
271
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700272 // HWASan requires AArch64 hardware feature (top-byte-ignore).
273 if ctx.Arch().ArchType != android.Arm64 {
274 s.Hwaddress = nil
275 }
276
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700277 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700278 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700279 s.Cfi = nil
280 s.Diag.Cfi = nil
281 }
282
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700283 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800284 if ctx.Host() {
285 s.Cfi = nil
286 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700287 s.Misc_undefined = nil
288 s.Undefined = nil
289 s.All_undefined = nil
290 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800291 }
292
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700293 // Also disable CFI for VNDK variants of components
294 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700295 s.Cfi = nil
296 s.Diag.Cfi = nil
297 }
298
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700299 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
300 if ctx.inRecovery() {
301 s.Hwaddress = nil
302 }
303
Colin Cross3c344ef2016-07-18 15:44:56 -0700304 if ctx.staticBinary() {
305 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700306 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700307 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800308 }
309
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700310 if Bool(s.All_undefined) {
311 s.Undefined = nil
312 }
313
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700314 if !ctx.toolchain().Is64Bit() {
315 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700316 s.Thread = nil
317 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800318 // TODO(ccross): error for compile_multilib = "32"?
319 }
320
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800321 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 -0700322 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 -0700323 Bool(s.Scudo) || Bool(s.Hwaddress)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700324 sanitize.Properties.SanitizerEnabled = true
325 }
326
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700327 // Disable Scudo if ASan or TSan is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700328 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700329 s.Scudo = nil
330 }
331
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700332 if Bool(s.Hwaddress) {
333 s.Address = nil
334 s.Thread = nil
335 }
336
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700337 if Bool(s.Coverage) {
338 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800339 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
340 }
341 }
342}
343
344func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
345 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
346 return deps
347 }
348
349 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700350 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700351 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800352 }
353 }
354
355 return deps
356}
357
358func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700359 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
360 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800361
362 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
363 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700364 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800365 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700366 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800367 return flags
368 }
369
Colin Cross16b23492016-01-06 14:41:07 -0800370 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700371 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800372
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700373 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800374 sanitizers = append(sanitizers, "undefined")
Colin Cross16b23492016-01-06 14:41:07 -0800375 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700376 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800377 sanitizers = append(sanitizers,
378 "bool",
379 "integer-divide-by-zero",
380 "return",
381 "returns-nonnull-attribute",
382 "shift-exponent",
383 "unreachable",
384 "vla-bound",
385 // TODO(danalbert): The following checks currently have compiler performance issues.
386 //"alignment",
387 //"bounds",
388 //"enum",
389 //"float-cast-overflow",
390 //"float-divide-by-zero",
391 //"nonnull-attribute",
392 //"null",
393 //"shift-base",
394 //"signed-integer-overflow",
395 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
396 // https://llvm.org/PR19302
397 // http://reviews.llvm.org/D6974
398 // "object-size",
399 )
400 }
401 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
402 }
403
Ivan Lozano651275b2017-06-13 10:24:34 -0700404 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700405 diagSanitizers = append(diagSanitizers, "undefined")
406 }
407
Ivan Lozano651275b2017-06-13 10:24:34 -0700408 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
409
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700410 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700411 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800412 // Frame pointer based unwinder in ASan requires ARM frame setup.
413 // TODO: put in flags?
414 flags.RequiredInstructionSet = "arm"
415 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700416 flags.CFlags = append(flags.CFlags, asanCflags...)
417 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800418
Colin Cross16b23492016-01-06 14:41:07 -0800419 if ctx.Host() {
420 // -nodefaultlibs (provided with libc++) prevents the driver from linking
421 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800422 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
423 } else {
424 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
425 flags.DynamicLinker = "/system/bin/linker_asan"
426 if flags.Toolchain.Is64Bit() {
427 flags.DynamicLinker += "64"
428 }
429 }
430 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700431 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800432 }
433
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700434 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
435 flags.CFlags = append(flags.CFlags, hwasanCflags...)
436 sanitizers = append(sanitizers, "hwaddress")
437 }
438
Yabin Cui6be405e2017-10-19 15:52:11 -0700439 if Bool(sanitize.Properties.Sanitize.Thread) {
440 sanitizers = append(sanitizers, "thread")
441 }
442
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700443 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400444 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800445 }
446
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700447 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700448 sanitizers = append(sanitizers, "safe-stack")
449 }
450
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700451 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800452 if ctx.Arch().ArchType == android.Arm {
453 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
454 // to do this on a function basis, so force Thumb on the entire module.
455 flags.RequiredInstructionSet = "thumb"
456 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700457 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000458
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700459 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700460 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000461 // Only append the default visibility flag if -fvisibility has not already been set
462 // to hidden.
463 if !inList("-fvisibility=hidden", flags.CFlags) {
464 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
465 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700466 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Zhizhou Yang51be6322018-02-08 18:32:11 -0800467 flags.ArGoldPlugin = true
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700468 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
469 diagSanitizers = append(diagSanitizers, "cfi")
470 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000471
472 if ctx.staticBinary() {
473 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
474 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
475 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700476 }
477
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700478 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700479 sanitizers = append(sanitizers, "unsigned-integer-overflow")
480 sanitizers = append(sanitizers, "signed-integer-overflow")
481 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
482 if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) {
483 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
484 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700485 }
486 }
487
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700488 if Bool(sanitize.Properties.Sanitize.Scudo) {
489 sanitizers = append(sanitizers, "scudo")
490 }
491
Colin Cross16b23492016-01-06 14:41:07 -0800492 if len(sanitizers) > 0 {
493 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800494
Colin Cross16b23492016-01-06 14:41:07 -0800495 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700496 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800497 if ctx.Host() {
498 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
499 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800500 // Host sanitizers only link symbols in the final executable, so
501 // there will always be undefined symbols in intermediate libraries.
502 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800503 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700504 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800505
506 if enableMinimalRuntime(sanitize) {
507 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
508 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700509 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800510 }
Colin Cross16b23492016-01-06 14:41:07 -0800511 }
512 }
513
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700514 if len(diagSanitizers) > 0 {
Ivan Lozanob7d0f522018-01-20 01:44:38 +0000515 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700516 }
517 // FIXME: enable RTTI if diag + (cfi or vptr)
518
Andreas Gampe97071162017-05-08 13:15:23 -0700519 if sanitize.Properties.Sanitize.Recover != nil {
520 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
521 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
522 }
523
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700524 // Link a runtime library if needed.
525 runtimeLibrary := ""
526 if Bool(sanitize.Properties.Sanitize.Address) {
527 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700528 } else if Bool(sanitize.Properties.Sanitize.Hwaddress) {
529 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(ctx.toolchain())
Yabin Cui6be405e2017-10-19 15:52:11 -0700530 } else if Bool(sanitize.Properties.Sanitize.Thread) {
531 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain())
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700532 } else if Bool(sanitize.Properties.Sanitize.Scudo) {
533 runtimeLibrary = config.ScudoRuntimeLibrary(ctx.toolchain())
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700534 } else if len(diagSanitizers) > 0 || sanitize.Properties.UbsanRuntimeDep {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700535 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
536 }
537
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700538 if runtimeLibrary != "" {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700539 runtimeLibraryPath := "${config.ClangAsanLibDir}/" + runtimeLibrary
540 if !ctx.static() {
541 runtimeLibraryPath = runtimeLibraryPath + ctx.toolchain().ShlibSuffix()
542 } else {
543 runtimeLibraryPath = runtimeLibraryPath + ".a"
544 }
545
Jiyong Park27b188b2017-07-18 13:23:39 +0900546 // ASan runtime library must be the first in the link order.
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700547 flags.libFlags = append([]string{runtimeLibraryPath}, flags.libFlags...)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700548 sanitize.runtimeLibrary = runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900549
550 // When linking against VNDK, use the vendor variant of the runtime lib
551 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700552 if ctx.useVndk() {
Jiyong Park27b188b2017-07-18 13:23:39 +0900553 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix
554 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700555 }
556
Colin Cross635c3b02016-05-18 15:37:25 -0700557 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800558 if blacklist.Valid() {
559 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
560 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
561 }
562
563 return flags
564}
565
Colin Cross8ff9ef42017-05-08 13:44:11 -0700566func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross27a4b052017-08-10 16:32:23 -0700567 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Jiyong Park27b188b2017-07-18 13:23:39 +0900568 if sanitize.androidMkRuntimeLibrary != "" {
569 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700570 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700571 })
Vishwath Mohane7128792017-11-17 11:08:10 -0800572
573 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
574 // name conflict.
575 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
576 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000577 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700578 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
579 ret.SubName += ".hwasan"
580 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700581}
582
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700583func (sanitize *sanitize) inSanitizerDir() bool {
584 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700585}
586
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000587func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000588 switch t {
589 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000590 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700591 case hwasan:
592 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000593 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000594 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000595 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000596 return sanitize.Properties.Sanitize.Integer_overflow
597 case cfi:
598 return sanitize.Properties.Sanitize.Cfi
Vishwath Mohan95229302017-08-11 00:53:16 +0000599 default:
600 panic(fmt.Errorf("unknown sanitizerType %d", t))
601 }
602}
603
Dan Albert7d1eecf2018-01-19 12:30:45 -0800604func (sanitize *sanitize) isUnsanitizedVariant() bool {
605 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700606 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800607 !sanitize.isSanitizerEnabled(tsan) &&
608 !sanitize.isSanitizerEnabled(cfi)
609}
610
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700611func (sanitize *sanitize) isVariantOnProductionDevice() bool {
612 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700613 !sanitize.isSanitizerEnabled(hwasan) &&
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700614 !sanitize.isSanitizerEnabled(tsan)
615}
616
Colin Cross16b23492016-01-06 14:41:07 -0800617func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
618 switch t {
619 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700620 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700621 if !b {
622 sanitize.Properties.Sanitize.Coverage = nil
623 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700624 case hwasan:
625 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800626 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700627 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700628 case intOverflow:
629 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000630 case cfi:
631 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800632 default:
633 panic(fmt.Errorf("unknown sanitizerType %d", t))
634 }
635 if b {
636 sanitize.Properties.SanitizerEnabled = true
637 }
638}
639
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000640// Check if the sanitizer is explicitly disabled (as opposed to nil by
641// virtue of not being set).
642func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
643 if sanitize == nil {
644 return false
645 }
646
647 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
648 return sanitizerVal != nil && *sanitizerVal == false
649}
650
651// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
652// because enabling a sanitizer either directly (via the blueprint) or
653// indirectly (via a mutator) sets the bool ptr to true, and you can't
654// distinguish between the cases. It isn't needed though - both cases can be
655// treated identically.
656func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
657 if sanitize == nil {
658 return false
659 }
660
661 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
662 return sanitizerVal != nil && *sanitizerVal == true
663}
664
Colin Cross6b753602018-06-21 13:03:07 -0700665func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
666 t, ok := tag.(dependencyTag)
667 return ok && t.library || t == reuseObjTag
668}
669
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700670// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700671func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
672 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000673 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700674 mctx.WalkDeps(func(child, parent android.Module) bool {
675 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
676 return false
677 }
678 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800679 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000680 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700681 if t == cfi || t == hwasan {
682 if d.static() {
683 d.sanitize.Properties.SanitizeDep = true
684 }
685 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000686 d.sanitize.Properties.SanitizeDep = true
687 }
Colin Cross16b23492016-01-06 14:41:07 -0800688 }
Colin Cross6b753602018-06-21 13:03:07 -0700689 return true
Colin Cross16b23492016-01-06 14:41:07 -0800690 })
691 }
692 }
693}
694
Ivan Lozano30c5db22018-02-21 15:49:20 -0800695// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700696func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
697 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
698 mctx.WalkDeps(func(child, parent android.Module) bool {
699 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
700 return false
701 }
702 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800703
Colin Cross6b753602018-06-21 13:03:07 -0700704 if enableMinimalRuntime(d.sanitize) {
705 // If a static dependency is built with the minimal runtime,
706 // make sure we include the ubsan minimal runtime.
707 c.sanitize.Properties.MinimalRuntimeDep = true
708 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
709 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
710 // If a static dependency runs with full ubsan diagnostics,
711 // make sure we include the ubsan runtime.
712 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800713 }
Colin Cross6b753602018-06-21 13:03:07 -0700714 }
715 return true
716 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800717 }
718}
719
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000720// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700721func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
722 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000723 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000724 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700725 modules := mctx.CreateVariations(t.String())
726 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000727 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
728 // Save original sanitizer status before we assign values to variant
729 // 0 as that overwrites the original.
730 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
731
Colin Crossb0f28952016-09-19 16:46:53 -0700732 modules := mctx.CreateVariations("", t.String())
733 modules[0].(*Module).sanitize.SetSanitizer(t, false)
734 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000735
Colin Crossb0f28952016-09-19 16:46:53 -0700736 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
737 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800738
739 // We don't need both variants active for anything but CFI-enabled
740 // target static libraries, so suppress the appropriate variant in
741 // all other cases.
742 if t == cfi {
743 if c.static() {
744 if !mctx.Device() {
745 if isSanitizerEnabled {
746 modules[0].(*Module).Properties.PreventInstall = true
747 modules[0].(*Module).Properties.HideFromMake = true
748 } else {
749 modules[1].(*Module).Properties.PreventInstall = true
750 modules[1].(*Module).Properties.HideFromMake = true
751 }
752 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800753 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800754
755 cfiStaticLibsMutex.Lock()
756 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
757 cfiStaticLibsMutex.Unlock()
758 }
759 } else {
760 modules[0].(*Module).Properties.PreventInstall = true
761 modules[0].(*Module).Properties.HideFromMake = true
762 }
763 } else if t == asan {
764 if mctx.Device() {
765 // CFI and ASAN are currently mutually exclusive so disable
766 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000767 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
768 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
769 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700770 if isSanitizerEnabled {
771 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800772 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700773 } else {
774 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800775 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700776 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700777 } else if t == hwasan {
778 if mctx.Device() {
779 // CFI and HWASAN are currently mutually exclusive so disable
780 // CFI if this is an HWASAN variant.
781 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
782 }
783
784 if c.static() {
785 if c.useVndk() {
786 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
787 hwasanStaticLibsMutex.Lock()
788 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
789 hwasanStaticLibsMutex.Unlock()
790 } else {
791 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
792 hwasanStaticLibsMutex.Lock()
793 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
794 hwasanStaticLibsMutex.Unlock()
795 }
796 } else {
797 if isSanitizerEnabled {
798 modules[0].(*Module).Properties.PreventInstall = true
799 modules[0].(*Module).Properties.HideFromMake = true
800 } else {
801 modules[1].(*Module).Properties.PreventInstall = true
802 modules[1].(*Module).Properties.HideFromMake = true
803 }
804 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700805 }
Colin Cross16b23492016-01-06 14:41:07 -0800806 }
807 c.sanitize.Properties.SanitizeDep = false
808 }
809 }
810}
Vishwath Mohane7128792017-11-17 11:08:10 -0800811
812func cfiStaticLibs(config android.Config) *[]string {
813 return config.Once("cfiStaticLibs", func() interface{} {
814 return &[]string{}
815 }).(*[]string)
816}
817
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700818func hwasanStaticLibs(config android.Config) *[]string {
819 return config.Once("hwasanStaticLibs", func() interface{} {
820 return &[]string{}
821 }).(*[]string)
822}
823
824func hwasanVendorStaticLibs(config android.Config) *[]string {
825 return config.Once("hwasanVendorStaticLibs", func() interface{} {
826 return &[]string{}
827 }).(*[]string)
828}
829
Ivan Lozano30c5db22018-02-21 15:49:20 -0800830func enableMinimalRuntime(sanitize *sanitize) bool {
831 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700832 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700833 !Bool(sanitize.Properties.Sanitize.Scudo) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -0800834 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
835 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
836 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
837 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
838 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
839 return true
840 }
841 return false
842}
843
Vishwath Mohane7128792017-11-17 11:08:10 -0800844func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
845 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -0800846 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -0800847 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
848}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700849
850func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
851 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
852 sort.Strings(*hwasanStaticLibs)
853 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
854
855 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
856 sort.Strings(*hwasanVendorStaticLibs)
857 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
858}