blob: b238b7e53ad5d730b2c0bf63e9809271fc7c995e [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"
Jeff Gaston72765392017-11-28 16:37:53 -080019 "sort"
Colin Cross16b23492016-01-06 14:41:07 -080020 "strings"
Vishwath Mohane7128792017-11-17 11:08:10 -080021 "sync"
Colin Cross16b23492016-01-06 14:41:07 -080022
Colin Cross6b753602018-06-21 13:03:07 -070023 "github.com/google/blueprint"
24
Colin Cross635c3b02016-05-18 15:37:25 -070025 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070026 "android/soong/cc/config"
Colin Cross16b23492016-01-06 14:41:07 -080027)
28
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070029var (
30 // Any C flags added by sanitizer which libTooling tools may not
31 // understand also need to be added to ClangLibToolingUnknownCflags in
32 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080033
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070034 asanCflags = []string{"-fno-omit-frame-pointer"}
35 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070036
Peter Collingbourne967511a2019-03-19 21:39:54 -070037 // TODO(pcc): Stop passing -hwasan-allow-ifunc here once it has been made
38 // the default.
39 hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
40 "-mllvm", "-hwasan-create-frame-descriptions=0",
Peter Collingbournee726ba52019-03-21 16:21:44 -070041 "-mllvm", "-hwasan-allow-ifunc",
Evgenii Stepanov1c69e832019-06-14 18:37:33 -070042 "-fsanitize-hwaddress-abi=platform",
43 "-fno-experimental-new-pass-manager"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070044
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000045 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070046 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070047 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
48 // used, but have no effect on assembly files
49 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070050 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070051 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070052 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
53 cfiStaticLibsMutex sync.Mutex
54 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070055
Evgenii Stepanov98f5b062018-11-29 15:12:51 -080056 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080057
Peter Collingbournebd19db02019-03-06 10:38:48 -080058 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070059 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov3c5a52a2018-12-18 17:02:44 -080060 hwasanGlobalOptions = []string{"heap_history_size=1023,stack_history_size=512"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070061)
62
Colin Cross16b23492016-01-06 14:41:07 -080063type sanitizerType int
64
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070065func boolPtr(v bool) *bool {
66 if v {
67 return &v
68 } else {
69 return nil
70 }
71}
72
Colin Cross16b23492016-01-06 14:41:07 -080073const (
74 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070075 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080076 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070077 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000078 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080079 scs
Mitch Phillipsbfeade62019-05-01 14:42:05 -070080 fuzzer
Colin Cross16b23492016-01-06 14:41:07 -080081)
82
Jiyong Park82226632019-02-01 10:50:50 +090083// Name of the sanitizer variation for this sanitizer type
84func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080085 switch t {
86 case asan:
87 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070088 case hwasan:
89 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080090 case tsan:
91 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070092 case intOverflow:
93 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000094 case cfi:
95 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080096 case scs:
97 return "scs"
Mitch Phillipsbfeade62019-05-01 14:42:05 -070098 case fuzzer:
99 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800100 default:
101 panic(fmt.Errorf("unknown sanitizerType %d", t))
102 }
103}
104
Jiyong Park82226632019-02-01 10:50:50 +0900105// This is the sanitizer names in SANITIZE_[TARGET|HOST]
106func (t sanitizerType) name() string {
107 switch t {
108 case asan:
109 return "address"
110 case hwasan:
111 return "hwaddress"
112 case tsan:
113 return "thread"
114 case intOverflow:
115 return "integer_overflow"
116 case cfi:
117 return "cfi"
118 case scs:
119 return "shadow-call-stack"
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700120 case fuzzer:
121 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900122 default:
123 panic(fmt.Errorf("unknown sanitizerType %d", t))
124 }
125}
126
Colin Cross16b23492016-01-06 14:41:07 -0800127type SanitizeProperties struct {
128 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
129 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800130 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800131
132 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700133 Address *bool `android:"arch_variant"`
134 Thread *bool `android:"arch_variant"`
135 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800136
137 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700138 Undefined *bool `android:"arch_variant"`
139 All_undefined *bool `android:"arch_variant"`
140 Misc_undefined []string `android:"arch_variant"`
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700141 Fuzzer *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700142 Safestack *bool `android:"arch_variant"`
143 Cfi *bool `android:"arch_variant"`
144 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700145 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800146 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800147
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700148 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
149 // Replaces abort() on error with a human-readable error message.
150 // Address and Thread sanitizers always run in diagnostic mode.
151 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700152 Undefined *bool `android:"arch_variant"`
153 Cfi *bool `android:"arch_variant"`
154 Integer_overflow *bool `android:"arch_variant"`
155 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800156 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700157 }
158
159 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800160 Recover []string
161
162 // value to pass to -fsanitize-blacklist
163 Blacklist *string
164 } `android:"arch_variant"`
165
Jiyong Park379de2f2018-12-19 02:47:14 +0900166 SanitizerEnabled bool `blueprint:"mutated"`
167 SanitizeDep bool `blueprint:"mutated"`
168 MinimalRuntimeDep bool `blueprint:"mutated"`
169 UbsanRuntimeDep bool `blueprint:"mutated"`
170 InSanitizerDir bool `blueprint:"mutated"`
171 Sanitizers []string `blueprint:"mutated"`
172 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800173}
174
175type sanitize struct {
176 Properties SanitizeProperties
177}
178
Vishwath Mohane7128792017-11-17 11:08:10 -0800179func init() {
180 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700181 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800182}
183
Colin Cross16b23492016-01-06 14:41:07 -0800184func (sanitize *sanitize) props() []interface{} {
185 return []interface{}{&sanitize.Properties}
186}
187
188func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700189 s := &sanitize.Properties.Sanitize
190
Colin Cross16b23492016-01-06 14:41:07 -0800191 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700192 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800193 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800194 }
195
Doug Hornc32c6b02019-01-17 14:44:05 -0800196 // Sanitizers do not work on Fuchsia yet.
197 if ctx.Fuchsia() {
198 s.Never = BoolPtr(true)
199 }
200
Colin Cross16b23492016-01-06 14:41:07 -0800201 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800202 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800203 return
204 }
205
Colin Cross16b23492016-01-06 14:41:07 -0800206 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700207 var globalSanitizersDiag []string
208
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700209 if ctx.Host() {
210 if !ctx.Windows() {
211 globalSanitizers = ctx.Config().SanitizeHost()
212 }
213 } else {
214 arches := ctx.Config().SanitizeDeviceArch()
215 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
216 globalSanitizers = ctx.Config().SanitizeDevice()
217 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800218 }
219 }
220
Colin Cross16b23492016-01-06 14:41:07 -0800221 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000222 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700223 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
224 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000225 }
Colin Cross16b23492016-01-06 14:41:07 -0800226
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700227 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
228 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000229 }
230
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700231 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
232 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000233 }
234
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700235 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
236 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000237 }
238
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700239 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
240 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700241 }
242
243 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
244 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000245 }
246
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700247 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800248 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700249 s.Cfi = boolPtr(true)
250 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700251 }
252
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700253 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700254 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700255 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700256 s.Integer_overflow = boolPtr(true)
257 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700258 }
259
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700260 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
261 s.Scudo = boolPtr(true)
262 }
263
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700264 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
265 s.Hwaddress = boolPtr(true)
266 }
267
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000268 if len(globalSanitizers) > 0 {
269 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
270 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700271
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700272 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700273 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700274 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700275 s.Diag.Integer_overflow = boolPtr(true)
276 }
277
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700278 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
279 s.Diag.Cfi == nil && Bool(s.Cfi) {
280 s.Diag.Cfi = boolPtr(true)
281 }
282
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700283 if len(globalSanitizersDiag) > 0 {
284 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
285 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700286 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700287
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700288 // Enable CFI for all components in the include paths (for Aarch64 only)
289 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000290 s.Cfi = boolPtr(true)
291 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
292 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700293 }
294 }
295
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800296 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800297 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800298 s.Cfi = nil
299 s.Diag.Cfi = nil
300 }
301
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800302 // Also disable CFI for arm32 until b/35157333 is fixed.
303 if ctx.Arch().ArchType == android.Arm {
304 s.Cfi = nil
305 s.Diag.Cfi = nil
306 }
307
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700308 // HWASan requires AArch64 hardware feature (top-byte-ignore).
309 if ctx.Arch().ArchType != android.Arm64 {
310 s.Hwaddress = nil
311 }
312
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800313 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800314 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800315 s.Scs = nil
316 }
317
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700318 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700319 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700320 s.Cfi = nil
321 s.Diag.Cfi = nil
322 }
323
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700324 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800325 if ctx.Host() {
326 s.Cfi = nil
327 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700328 s.Misc_undefined = nil
329 s.Undefined = nil
330 s.All_undefined = nil
331 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800332 }
333
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700334 // Also disable CFI for VNDK variants of components
335 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700336 s.Cfi = nil
337 s.Diag.Cfi = nil
338 }
339
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700340 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800341 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
342 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700343 s.Hwaddress = nil
344 }
345
Colin Cross3c344ef2016-07-18 15:44:56 -0700346 if ctx.staticBinary() {
347 s.Address = nil
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700348 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700349 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800350 }
351
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700352 if Bool(s.All_undefined) {
353 s.Undefined = nil
354 }
355
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700356 if !ctx.toolchain().Is64Bit() {
357 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700358 s.Thread = nil
359 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800360 // TODO(ccross): error for compile_multilib = "32"?
361 }
362
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800363 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700364 Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800365 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700366 sanitize.Properties.SanitizerEnabled = true
367 }
368
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800369 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
370 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700371 s.Scudo = nil
372 }
373
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700374 if Bool(s.Hwaddress) {
375 s.Address = nil
376 s.Thread = nil
377 }
378
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700379 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
380 // mutually incompatible.
381 if Bool(s.Fuzzer) {
382 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800383 }
384}
385
386func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
387 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
388 return deps
389 }
390
391 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700392 if Bool(sanitize.Properties.Sanitize.Address) {
Christopher Ferris753d4a62019-05-09 13:27:02 -0700393 // Compiling asan and having libc_scudo in the same
394 // executable will cause the executable to crash.
395 // Remove libc_scudo since it is only used to override
396 // allocation functions which asan already overrides.
397 _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800398 }
399 }
400
401 return deps
402}
403
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800404func toDisableImplicitIntegerChange(flags []string) bool {
405 // Returns true if any flag is fsanitize*integer, and there is
406 // no explicit flag about sanitize=implicit-integer-sign-change.
407 for _, f := range flags {
408 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
409 return false
410 }
411 }
412 for _, f := range flags {
413 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
414 return true
415 }
416 }
417 return false
418}
419
Colin Cross16b23492016-01-06 14:41:07 -0800420func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700421 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
422 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800423
424 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
425 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700426 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800427 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700428 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800429 return flags
430 }
431
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700432 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700433 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800434 // Frame pointer based unwinder in ASan requires ARM frame setup.
435 // TODO: put in flags?
436 flags.RequiredInstructionSet = "arm"
437 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700438 flags.CFlags = append(flags.CFlags, asanCflags...)
439 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800440
Colin Cross16b23492016-01-06 14:41:07 -0800441 if ctx.Host() {
442 // -nodefaultlibs (provided with libc++) prevents the driver from linking
443 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800444 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
445 } else {
Jiyong Parka2aca282019-02-02 13:13:38 +0900446 if ctx.bootstrap() {
447 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
448 } else {
449 flags.DynamicLinker = "/system/bin/linker_asan"
450 }
Colin Cross16b23492016-01-06 14:41:07 -0800451 if flags.Toolchain.Is64Bit() {
452 flags.DynamicLinker += "64"
453 }
454 }
Colin Cross16b23492016-01-06 14:41:07 -0800455 }
456
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700457 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
458 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700459 }
460
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700461 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
462 flags.CFlags = append(flags.CFlags, "-fsanitize=fuzzer-no-link")
463 flags.LdFlags = append(flags.LdFlags, "-fsanitize=fuzzer-no-link")
464
465 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
466 _, flags.LdFlags = removeFromList("-flto", flags.LdFlags)
467 flags.LdFlags = append(flags.LdFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700468
469 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
470 _, flags.CFlags = removeFromList("-fexperimental-new-pass-manager", flags.CFlags)
471 flags.CFlags = append(flags.CFlags, "-fno-experimental-new-pass-manager")
Colin Cross16b23492016-01-06 14:41:07 -0800472 }
473
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700474 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800475 if ctx.Arch().ArchType == android.Arm {
476 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
477 // to do this on a function basis, so force Thumb on the entire module.
478 flags.RequiredInstructionSet = "thumb"
479 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000480
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700481 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700482 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000483 // Only append the default visibility flag if -fvisibility has not already been set
484 // to hidden.
485 if !inList("-fvisibility=hidden", flags.CFlags) {
486 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
487 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700488 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000489
490 if ctx.staticBinary() {
491 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
492 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
493 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700494 }
495
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700496 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700497 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700498 }
499
Jiyong Park379de2f2018-12-19 02:47:14 +0900500 if len(sanitize.Properties.Sanitizers) > 0 {
501 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800502
Colin Cross16b23492016-01-06 14:41:07 -0800503 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700504 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800505 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800506 // Host sanitizers only link symbols in the final executable, so
507 // there will always be undefined symbols in intermediate libraries.
508 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700509 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800510 } else {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800511 if enableMinimalRuntime(sanitize) {
512 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
513 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700514 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800515 }
Colin Cross16b23492016-01-06 14:41:07 -0800516 }
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700517
518 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
519 // When fuzzing, we wish to crash with diagnostics on any bug.
520 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
521 } else if ctx.Host() {
522 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
523 } else {
524 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
525 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800526 // http://b/119329758, Android core does not boot up with this sanitizer yet.
527 if toDisableImplicitIntegerChange(flags.CFlags) {
528 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
529 }
Colin Cross16b23492016-01-06 14:41:07 -0800530 }
531
Jiyong Park379de2f2018-12-19 02:47:14 +0900532 if len(sanitize.Properties.DiagSanitizers) > 0 {
533 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700534 }
535 // FIXME: enable RTTI if diag + (cfi or vptr)
536
Andreas Gampe97071162017-05-08 13:15:23 -0700537 if sanitize.Properties.Sanitize.Recover != nil {
538 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
539 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
540 }
541
Ivan Lozano7929bba2018-12-12 09:36:31 -0800542 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
543 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
544 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
545 }
546
Colin Cross635c3b02016-05-18 15:37:25 -0700547 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800548 if blacklist.Valid() {
549 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
550 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
551 }
552
553 return flags
554}
555
Colin Cross8ff9ef42017-05-08 13:44:11 -0700556func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Vishwath Mohane7128792017-11-17 11:08:10 -0800557 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
558 // name conflict.
559 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
560 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000561 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700562 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
563 ret.SubName += ".hwasan"
564 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800565 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
566 ret.SubName += ".scs"
567 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700568}
569
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700570func (sanitize *sanitize) inSanitizerDir() bool {
571 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700572}
573
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000574func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000575 switch t {
576 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000577 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700578 case hwasan:
579 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000580 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000581 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000582 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000583 return sanitize.Properties.Sanitize.Integer_overflow
584 case cfi:
585 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800586 case scs:
587 return sanitize.Properties.Sanitize.Scs
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700588 case fuzzer:
589 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000590 default:
591 panic(fmt.Errorf("unknown sanitizerType %d", t))
592 }
593}
594
Dan Albert7d1eecf2018-01-19 12:30:45 -0800595func (sanitize *sanitize) isUnsanitizedVariant() bool {
596 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700597 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800598 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800599 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700600 !sanitize.isSanitizerEnabled(scs) &&
601 !sanitize.isSanitizerEnabled(fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800602}
603
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700604func (sanitize *sanitize) isVariantOnProductionDevice() bool {
605 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700606 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700607 !sanitize.isSanitizerEnabled(tsan) &&
608 !sanitize.isSanitizerEnabled(fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700609}
610
Colin Cross16b23492016-01-06 14:41:07 -0800611func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
612 switch t {
613 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700614 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700615 case hwasan:
616 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800617 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700618 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700619 case intOverflow:
620 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000621 case cfi:
622 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800623 case scs:
624 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700625 case fuzzer:
626 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800627 default:
628 panic(fmt.Errorf("unknown sanitizerType %d", t))
629 }
630 if b {
631 sanitize.Properties.SanitizerEnabled = true
632 }
633}
634
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000635// Check if the sanitizer is explicitly disabled (as opposed to nil by
636// virtue of not being set).
637func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
638 if sanitize == nil {
639 return false
640 }
641
642 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
643 return sanitizerVal != nil && *sanitizerVal == false
644}
645
646// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
647// because enabling a sanitizer either directly (via the blueprint) or
648// indirectly (via a mutator) sets the bool ptr to true, and you can't
649// distinguish between the cases. It isn't needed though - both cases can be
650// treated identically.
651func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
652 if sanitize == nil {
653 return false
654 }
655
656 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
657 return sanitizerVal != nil && *sanitizerVal == true
658}
659
Colin Cross6b753602018-06-21 13:03:07 -0700660func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
661 t, ok := tag.(dependencyTag)
662 return ok && t.library || t == reuseObjTag
663}
664
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700665// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700666func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
667 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000668 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700669 mctx.WalkDeps(func(child, parent android.Module) bool {
670 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
671 return false
672 }
673 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800674 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000675 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800676 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700677 if d.static() {
678 d.sanitize.Properties.SanitizeDep = true
679 }
680 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000681 d.sanitize.Properties.SanitizeDep = true
682 }
Colin Cross16b23492016-01-06 14:41:07 -0800683 }
Colin Cross6b753602018-06-21 13:03:07 -0700684 return true
Colin Cross16b23492016-01-06 14:41:07 -0800685 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900686 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
687 // If an APEX module includes a lib which is enabled for a sanitizer T, then
688 // the APEX module is also enabled for the same sanitizer type.
689 mctx.VisitDirectDeps(func(child android.Module) {
690 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
691 sanitizeable.EnableSanitizer(t.name())
692 }
693 })
Colin Cross16b23492016-01-06 14:41:07 -0800694 }
695 }
696}
697
Ivan Lozano30c5db22018-02-21 15:49:20 -0800698// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700699func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
700 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
701 mctx.WalkDeps(func(child, parent android.Module) bool {
702 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
703 return false
704 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800705
Colin Cross0b908332019-06-19 23:00:20 -0700706 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700707 if enableMinimalRuntime(d.sanitize) {
708 // If a static dependency is built with the minimal runtime,
709 // make sure we include the ubsan minimal runtime.
710 c.sanitize.Properties.MinimalRuntimeDep = true
711 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
712 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
713 // If a static dependency runs with full ubsan diagnostics,
714 // make sure we include the ubsan runtime.
715 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800716 }
Colin Cross0b908332019-06-19 23:00:20 -0700717
718 if c.sanitize.Properties.MinimalRuntimeDep &&
719 c.sanitize.Properties.UbsanRuntimeDep {
720 // both flags that this mutator might set are true, so don't bother recursing
721 return false
722 }
723
724 return true
725 } else {
726 return false
Colin Cross6b753602018-06-21 13:03:07 -0700727 }
Colin Cross6b753602018-06-21 13:03:07 -0700728 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800729 }
730}
731
Jiyong Park379de2f2018-12-19 02:47:14 +0900732// Add the dependency to the runtime library for each of the sanitizer variants
733func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900734 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000735 if !c.Enabled() {
736 return
737 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900738 var sanitizers []string
739 var diagSanitizers []string
740
741 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
742 sanitizers = append(sanitizers, "undefined")
743 } else {
744 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
745 sanitizers = append(sanitizers,
746 "bool",
747 "integer-divide-by-zero",
748 "return",
749 "returns-nonnull-attribute",
750 "shift-exponent",
751 "unreachable",
752 "vla-bound",
753 // TODO(danalbert): The following checks currently have compiler performance issues.
754 //"alignment",
755 //"bounds",
756 //"enum",
757 //"float-cast-overflow",
758 //"float-divide-by-zero",
759 //"nonnull-attribute",
760 //"null",
761 //"shift-base",
762 //"signed-integer-overflow",
763 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
764 // https://llvm.org/PR19302
765 // http://reviews.llvm.org/D6974
766 // "object-size",
767 )
768 }
769 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
770 }
771
772 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
773 diagSanitizers = append(diagSanitizers, "undefined")
774 }
775
776 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
777
778 if Bool(c.sanitize.Properties.Sanitize.Address) {
779 sanitizers = append(sanitizers, "address")
780 diagSanitizers = append(diagSanitizers, "address")
781 }
782
783 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
784 sanitizers = append(sanitizers, "hwaddress")
785 }
786
787 if Bool(c.sanitize.Properties.Sanitize.Thread) {
788 sanitizers = append(sanitizers, "thread")
789 }
790
791 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
792 sanitizers = append(sanitizers, "safe-stack")
793 }
794
795 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
796 sanitizers = append(sanitizers, "cfi")
797
798 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
799 diagSanitizers = append(diagSanitizers, "cfi")
800 }
801 }
802
803 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
804 sanitizers = append(sanitizers, "unsigned-integer-overflow")
805 sanitizers = append(sanitizers, "signed-integer-overflow")
806 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
807 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
808 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
809 }
810 }
811
812 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
813 sanitizers = append(sanitizers, "scudo")
814 }
815
816 if Bool(c.sanitize.Properties.Sanitize.Scs) {
817 sanitizers = append(sanitizers, "shadow-call-stack")
818 }
819
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700820 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
821 sanitizers = append(sanitizers, "fuzzer-no-link")
822 }
823
Jiyong Park379de2f2018-12-19 02:47:14 +0900824 // Save the list of sanitizers. These will be used again when generating
825 // the build rules (for Cflags, etc.)
826 c.sanitize.Properties.Sanitizers = sanitizers
827 c.sanitize.Properties.DiagSanitizers = diagSanitizers
828
829 // Determine the runtime library required
830 runtimeLibrary := ""
831 toolchain := c.toolchain(mctx)
832 if Bool(c.sanitize.Properties.Sanitize.Address) {
833 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
834 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
835 if c.staticBinary() {
836 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
837 } else {
838 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
839 }
840 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
841 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
842 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
843 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
844 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
845 } else {
846 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
847 }
848 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
849 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
850 }
851
852 if mctx.Device() && runtimeLibrary != "" {
Inseob Kim9516ee92019-05-09 10:56:13 +0900853 if inList(runtimeLibrary, *llndkLibraries(mctx.Config())) && !c.static() && c.useVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900854 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
855 }
856
857 // Adding dependency to the runtime library. We are using *FarVariation*
858 // because the runtime libraries themselves are not mutated by sanitizer
859 // mutators and thus don't have sanitizer variants whereas this module
860 // has been already mutated.
861 //
862 // Note that by adding dependency with {static|shared}DepTag, the lib is
863 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
864 if c.staticBinary() {
865 // static executable gets static runtime libs
866 mctx.AddFarVariationDependencies([]blueprint.Variation{
867 {Mutator: "link", Variation: "static"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900868 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900869 {Mutator: "arch", Variation: mctx.Target().String()},
870 }, staticDepTag, runtimeLibrary)
871 } else if !c.static() {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900872 // dynamic executable and shared libs get shared runtime libs
Jiyong Park379de2f2018-12-19 02:47:14 +0900873 mctx.AddFarVariationDependencies([]blueprint.Variation{
874 {Mutator: "link", Variation: "shared"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900875 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900876 {Mutator: "arch", Variation: mctx.Target().String()},
Jiyong Park64a44f22019-01-18 14:37:08 +0900877 }, earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900878 }
879 // static lib does not have dependency to the runtime library. The
880 // dependency will be added to the executables or shared libs using
881 // the static lib.
882 }
883 }
884}
885
886type Sanitizeable interface {
887 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900888 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900889 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900890}
891
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000892// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700893func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
894 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000895 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000896 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900897 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700898 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000899 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
900 // Save original sanitizer status before we assign values to variant
901 // 0 as that overwrites the original.
902 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
903
Jiyong Park82226632019-02-01 10:50:50 +0900904 modules := mctx.CreateVariations("", t.variationName())
Colin Crossb0f28952016-09-19 16:46:53 -0700905 modules[0].(*Module).sanitize.SetSanitizer(t, false)
906 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000907
Colin Crossb0f28952016-09-19 16:46:53 -0700908 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
909 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800910
911 // We don't need both variants active for anything but CFI-enabled
912 // target static libraries, so suppress the appropriate variant in
913 // all other cases.
914 if t == cfi {
915 if c.static() {
916 if !mctx.Device() {
917 if isSanitizerEnabled {
918 modules[0].(*Module).Properties.PreventInstall = true
919 modules[0].(*Module).Properties.HideFromMake = true
920 } else {
921 modules[1].(*Module).Properties.PreventInstall = true
922 modules[1].(*Module).Properties.HideFromMake = true
923 }
924 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800925 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800926
927 cfiStaticLibsMutex.Lock()
928 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
929 cfiStaticLibsMutex.Unlock()
930 }
931 } else {
932 modules[0].(*Module).Properties.PreventInstall = true
933 modules[0].(*Module).Properties.HideFromMake = true
934 }
935 } else if t == asan {
936 if mctx.Device() {
937 // CFI and ASAN are currently mutually exclusive so disable
938 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000939 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
940 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
941 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700942 if isSanitizerEnabled {
943 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800944 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700945 } else {
946 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800947 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700948 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800949 } else if t == scs {
950 // We don't currently link any static libraries built with make into
951 // libraries built with SCS, so we don't need logic for propagating
952 // SCSness of dependencies into make.
953 if !c.static() {
954 if isSanitizerEnabled {
955 modules[0].(*Module).Properties.PreventInstall = true
956 modules[0].(*Module).Properties.HideFromMake = true
957 } else {
958 modules[1].(*Module).Properties.PreventInstall = true
959 modules[1].(*Module).Properties.HideFromMake = true
960 }
961 }
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700962 } else if t == fuzzer {
963 // TODO(b/131771163): CFI and fuzzer support are mutually incompatible
964 // as CFI pulls in LTO.
965 if mctx.Device() {
966 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
967 }
968 if isSanitizerEnabled {
969 modules[0].(*Module).Properties.PreventInstall = true
970 modules[0].(*Module).Properties.HideFromMake = true
971 } else {
972 modules[1].(*Module).Properties.PreventInstall = true
973 modules[1].(*Module).Properties.HideFromMake = true
974 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700975 } else if t == hwasan {
976 if mctx.Device() {
977 // CFI and HWASAN are currently mutually exclusive so disable
978 // CFI if this is an HWASAN variant.
979 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
980 }
981
982 if c.static() {
983 if c.useVndk() {
984 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
985 hwasanStaticLibsMutex.Lock()
986 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
987 hwasanStaticLibsMutex.Unlock()
988 } else {
989 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
990 hwasanStaticLibsMutex.Lock()
991 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
992 hwasanStaticLibsMutex.Unlock()
993 }
994 } else {
995 if isSanitizerEnabled {
996 modules[0].(*Module).Properties.PreventInstall = true
997 modules[0].(*Module).Properties.HideFromMake = true
998 } else {
999 modules[1].(*Module).Properties.PreventInstall = true
1000 modules[1].(*Module).Properties.HideFromMake = true
1001 }
1002 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001003 }
Colin Cross16b23492016-01-06 14:41:07 -08001004 }
1005 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +09001006 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001007 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +09001008 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -08001009 }
1010 }
1011}
Vishwath Mohane7128792017-11-17 11:08:10 -08001012
Colin Cross571cccf2019-02-04 11:22:08 -08001013var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1014
Vishwath Mohane7128792017-11-17 11:08:10 -08001015func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001016 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -08001017 return &[]string{}
1018 }).(*[]string)
1019}
1020
Colin Cross571cccf2019-02-04 11:22:08 -08001021var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1022
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001023func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001024 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001025 return &[]string{}
1026 }).(*[]string)
1027}
1028
Colin Cross571cccf2019-02-04 11:22:08 -08001029var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
1030
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001031func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001032 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001033 return &[]string{}
1034 }).(*[]string)
1035}
1036
Ivan Lozano30c5db22018-02-21 15:49:20 -08001037func enableMinimalRuntime(sanitize *sanitize) bool {
1038 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001039 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillipsbfeade62019-05-01 14:42:05 -07001040 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -08001041 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
1042 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
1043 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1044 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
1045 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
1046 return true
1047 }
1048 return false
1049}
1050
Vishwath Mohane7128792017-11-17 11:08:10 -08001051func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1052 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001053 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001054 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1055}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001056
1057func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1058 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1059 sort.Strings(*hwasanStaticLibs)
1060 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1061
1062 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1063 sort.Strings(*hwasanVendorStaticLibs)
1064 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1065}