blob: d594cc719ec1c44ee77ef6d4461f75e66e4f1dce [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"}
36 asanLibs = []string{"libasan"}
37
Peter Collingbourne967511a2019-03-19 21:39:54 -070038 // TODO(pcc): Stop passing -hwasan-allow-ifunc here once it has been made
39 // the default.
40 hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
41 "-mllvm", "-hwasan-create-frame-descriptions=0",
Peter Collingbournee726ba52019-03-21 16:21:44 -070042 "-mllvm", "-hwasan-allow-ifunc",
43 "-fsanitize-hwaddress-abi=platform"}
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) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700393 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Christopher Ferris753d4a62019-05-09 13:27:02 -0700394 // Compiling asan and having libc_scudo in the same
395 // executable will cause the executable to crash.
396 // Remove libc_scudo since it is only used to override
397 // allocation functions which asan already overrides.
398 _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800399 }
400 }
401
402 return deps
403}
404
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800405func toDisableImplicitIntegerChange(flags []string) bool {
406 // Returns true if any flag is fsanitize*integer, and there is
407 // no explicit flag about sanitize=implicit-integer-sign-change.
408 for _, f := range flags {
409 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
410 return false
411 }
412 }
413 for _, f := range flags {
414 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
415 return true
416 }
417 }
418 return false
419}
420
Colin Cross16b23492016-01-06 14:41:07 -0800421func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700422 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
423 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800424
425 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
426 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700427 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800428 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700429 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800430 return flags
431 }
432
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700433 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700434 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800435 // Frame pointer based unwinder in ASan requires ARM frame setup.
436 // TODO: put in flags?
437 flags.RequiredInstructionSet = "arm"
438 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700439 flags.CFlags = append(flags.CFlags, asanCflags...)
440 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800441
Colin Cross16b23492016-01-06 14:41:07 -0800442 if ctx.Host() {
443 // -nodefaultlibs (provided with libc++) prevents the driver from linking
444 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800445 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
446 } else {
447 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900448 if ctx.bootstrap() {
449 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
450 } else {
451 flags.DynamicLinker = "/system/bin/linker_asan"
452 }
Colin Cross16b23492016-01-06 14:41:07 -0800453 if flags.Toolchain.Is64Bit() {
454 flags.DynamicLinker += "64"
455 }
456 }
Colin Cross16b23492016-01-06 14:41:07 -0800457 }
458
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700459 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
460 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700461 }
462
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700463 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
464 flags.CFlags = append(flags.CFlags, "-fsanitize=fuzzer-no-link")
465 flags.LdFlags = append(flags.LdFlags, "-fsanitize=fuzzer-no-link")
466
467 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
468 _, flags.LdFlags = removeFromList("-flto", flags.LdFlags)
469 flags.LdFlags = append(flags.LdFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700470
471 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
472 _, flags.CFlags = removeFromList("-fexperimental-new-pass-manager", flags.CFlags)
473 flags.CFlags = append(flags.CFlags, "-fno-experimental-new-pass-manager")
Colin Cross16b23492016-01-06 14:41:07 -0800474 }
475
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700476 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800477 if ctx.Arch().ArchType == android.Arm {
478 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
479 // to do this on a function basis, so force Thumb on the entire module.
480 flags.RequiredInstructionSet = "thumb"
481 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000482
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700483 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700484 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000485 // Only append the default visibility flag if -fvisibility has not already been set
486 // to hidden.
487 if !inList("-fvisibility=hidden", flags.CFlags) {
488 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
489 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700490 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000491
492 if ctx.staticBinary() {
493 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
494 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
495 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700496 }
497
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700498 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700499 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700500 }
501
Jiyong Park379de2f2018-12-19 02:47:14 +0900502 if len(sanitize.Properties.Sanitizers) > 0 {
503 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800504
Colin Cross16b23492016-01-06 14:41:07 -0800505 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700506 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800507 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800508 // Host sanitizers only link symbols in the final executable, so
509 // there will always be undefined symbols in intermediate libraries.
510 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700511 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800512 } else {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800513 if enableMinimalRuntime(sanitize) {
514 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
515 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700516 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800517 }
Colin Cross16b23492016-01-06 14:41:07 -0800518 }
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700519
520 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
521 // When fuzzing, we wish to crash with diagnostics on any bug.
522 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
523 } else if ctx.Host() {
524 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
525 } else {
526 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
527 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800528 // http://b/119329758, Android core does not boot up with this sanitizer yet.
529 if toDisableImplicitIntegerChange(flags.CFlags) {
530 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
531 }
Colin Cross16b23492016-01-06 14:41:07 -0800532 }
533
Jiyong Park379de2f2018-12-19 02:47:14 +0900534 if len(sanitize.Properties.DiagSanitizers) > 0 {
535 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700536 }
537 // FIXME: enable RTTI if diag + (cfi or vptr)
538
Andreas Gampe97071162017-05-08 13:15:23 -0700539 if sanitize.Properties.Sanitize.Recover != nil {
540 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
541 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
542 }
543
Ivan Lozano7929bba2018-12-12 09:36:31 -0800544 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
545 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
546 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
547 }
548
Colin Cross635c3b02016-05-18 15:37:25 -0700549 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800550 if blacklist.Valid() {
551 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
552 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
553 }
554
555 return flags
556}
557
Colin Cross8ff9ef42017-05-08 13:44:11 -0700558func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Vishwath Mohane7128792017-11-17 11:08:10 -0800559 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
560 // name conflict.
561 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
562 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000563 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700564 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
565 ret.SubName += ".hwasan"
566 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800567 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
568 ret.SubName += ".scs"
569 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700570}
571
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700572func (sanitize *sanitize) inSanitizerDir() bool {
573 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700574}
575
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000576func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000577 switch t {
578 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000579 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700580 case hwasan:
581 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000582 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000583 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000584 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000585 return sanitize.Properties.Sanitize.Integer_overflow
586 case cfi:
587 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800588 case scs:
589 return sanitize.Properties.Sanitize.Scs
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700590 case fuzzer:
591 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000592 default:
593 panic(fmt.Errorf("unknown sanitizerType %d", t))
594 }
595}
596
Dan Albert7d1eecf2018-01-19 12:30:45 -0800597func (sanitize *sanitize) isUnsanitizedVariant() bool {
598 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700599 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800600 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800601 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700602 !sanitize.isSanitizerEnabled(scs) &&
603 !sanitize.isSanitizerEnabled(fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800604}
605
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700606func (sanitize *sanitize) isVariantOnProductionDevice() bool {
607 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700608 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700609 !sanitize.isSanitizerEnabled(tsan) &&
610 !sanitize.isSanitizerEnabled(fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700611}
612
Colin Cross16b23492016-01-06 14:41:07 -0800613func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
614 switch t {
615 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700616 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700617 case hwasan:
618 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800619 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700620 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700621 case intOverflow:
622 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000623 case cfi:
624 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800625 case scs:
626 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700627 case fuzzer:
628 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800629 default:
630 panic(fmt.Errorf("unknown sanitizerType %d", t))
631 }
632 if b {
633 sanitize.Properties.SanitizerEnabled = true
634 }
635}
636
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000637// Check if the sanitizer is explicitly disabled (as opposed to nil by
638// virtue of not being set).
639func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
640 if sanitize == nil {
641 return false
642 }
643
644 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
645 return sanitizerVal != nil && *sanitizerVal == false
646}
647
648// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
649// because enabling a sanitizer either directly (via the blueprint) or
650// indirectly (via a mutator) sets the bool ptr to true, and you can't
651// distinguish between the cases. It isn't needed though - both cases can be
652// treated identically.
653func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
654 if sanitize == nil {
655 return false
656 }
657
658 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
659 return sanitizerVal != nil && *sanitizerVal == true
660}
661
Colin Cross6b753602018-06-21 13:03:07 -0700662func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
663 t, ok := tag.(dependencyTag)
664 return ok && t.library || t == reuseObjTag
665}
666
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700667// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700668func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
669 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000670 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700671 mctx.WalkDeps(func(child, parent android.Module) bool {
672 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
673 return false
674 }
675 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800676 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000677 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800678 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700679 if d.static() {
680 d.sanitize.Properties.SanitizeDep = true
681 }
682 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000683 d.sanitize.Properties.SanitizeDep = true
684 }
Colin Cross16b23492016-01-06 14:41:07 -0800685 }
Colin Cross6b753602018-06-21 13:03:07 -0700686 return true
Colin Cross16b23492016-01-06 14:41:07 -0800687 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900688 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
689 // If an APEX module includes a lib which is enabled for a sanitizer T, then
690 // the APEX module is also enabled for the same sanitizer type.
691 mctx.VisitDirectDeps(func(child android.Module) {
692 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
693 sanitizeable.EnableSanitizer(t.name())
694 }
695 })
Colin Cross16b23492016-01-06 14:41:07 -0800696 }
697 }
698}
699
Ivan Lozano30c5db22018-02-21 15:49:20 -0800700// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700701func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
702 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
703 mctx.WalkDeps(func(child, parent android.Module) bool {
704 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
705 return false
706 }
707 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800708
Colin Cross6b753602018-06-21 13:03:07 -0700709 if enableMinimalRuntime(d.sanitize) {
710 // If a static dependency is built with the minimal runtime,
711 // make sure we include the ubsan minimal runtime.
712 c.sanitize.Properties.MinimalRuntimeDep = true
713 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
714 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
715 // If a static dependency runs with full ubsan diagnostics,
716 // make sure we include the ubsan runtime.
717 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800718 }
Colin Cross6b753602018-06-21 13:03:07 -0700719 }
720 return true
721 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800722 }
723}
724
Jiyong Park379de2f2018-12-19 02:47:14 +0900725// Add the dependency to the runtime library for each of the sanitizer variants
726func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900727 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000728 if !c.Enabled() {
729 return
730 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900731 var sanitizers []string
732 var diagSanitizers []string
733
734 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
735 sanitizers = append(sanitizers, "undefined")
736 } else {
737 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
738 sanitizers = append(sanitizers,
739 "bool",
740 "integer-divide-by-zero",
741 "return",
742 "returns-nonnull-attribute",
743 "shift-exponent",
744 "unreachable",
745 "vla-bound",
746 // TODO(danalbert): The following checks currently have compiler performance issues.
747 //"alignment",
748 //"bounds",
749 //"enum",
750 //"float-cast-overflow",
751 //"float-divide-by-zero",
752 //"nonnull-attribute",
753 //"null",
754 //"shift-base",
755 //"signed-integer-overflow",
756 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
757 // https://llvm.org/PR19302
758 // http://reviews.llvm.org/D6974
759 // "object-size",
760 )
761 }
762 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
763 }
764
765 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
766 diagSanitizers = append(diagSanitizers, "undefined")
767 }
768
769 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
770
771 if Bool(c.sanitize.Properties.Sanitize.Address) {
772 sanitizers = append(sanitizers, "address")
773 diagSanitizers = append(diagSanitizers, "address")
774 }
775
776 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
777 sanitizers = append(sanitizers, "hwaddress")
778 }
779
780 if Bool(c.sanitize.Properties.Sanitize.Thread) {
781 sanitizers = append(sanitizers, "thread")
782 }
783
784 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
785 sanitizers = append(sanitizers, "safe-stack")
786 }
787
788 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
789 sanitizers = append(sanitizers, "cfi")
790
791 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
792 diagSanitizers = append(diagSanitizers, "cfi")
793 }
794 }
795
796 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
797 sanitizers = append(sanitizers, "unsigned-integer-overflow")
798 sanitizers = append(sanitizers, "signed-integer-overflow")
799 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
800 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
801 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
802 }
803 }
804
805 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
806 sanitizers = append(sanitizers, "scudo")
807 }
808
809 if Bool(c.sanitize.Properties.Sanitize.Scs) {
810 sanitizers = append(sanitizers, "shadow-call-stack")
811 }
812
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700813 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
814 sanitizers = append(sanitizers, "fuzzer-no-link")
815 }
816
Jiyong Park379de2f2018-12-19 02:47:14 +0900817 // Save the list of sanitizers. These will be used again when generating
818 // the build rules (for Cflags, etc.)
819 c.sanitize.Properties.Sanitizers = sanitizers
820 c.sanitize.Properties.DiagSanitizers = diagSanitizers
821
822 // Determine the runtime library required
823 runtimeLibrary := ""
824 toolchain := c.toolchain(mctx)
825 if Bool(c.sanitize.Properties.Sanitize.Address) {
826 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
827 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
828 if c.staticBinary() {
829 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
830 } else {
831 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
832 }
833 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
834 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
835 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
836 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
837 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
838 } else {
839 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
840 }
841 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
842 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
843 }
844
845 if mctx.Device() && runtimeLibrary != "" {
Inseob Kim9516ee92019-05-09 10:56:13 +0900846 if inList(runtimeLibrary, *llndkLibraries(mctx.Config())) && !c.static() && c.useVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900847 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
848 }
849
850 // Adding dependency to the runtime library. We are using *FarVariation*
851 // because the runtime libraries themselves are not mutated by sanitizer
852 // mutators and thus don't have sanitizer variants whereas this module
853 // has been already mutated.
854 //
855 // Note that by adding dependency with {static|shared}DepTag, the lib is
856 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
857 if c.staticBinary() {
858 // static executable gets static runtime libs
859 mctx.AddFarVariationDependencies([]blueprint.Variation{
860 {Mutator: "link", Variation: "static"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900861 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900862 {Mutator: "arch", Variation: mctx.Target().String()},
863 }, staticDepTag, runtimeLibrary)
864 } else if !c.static() {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900865 // dynamic executable and shared libs get shared runtime libs
Jiyong Park379de2f2018-12-19 02:47:14 +0900866 mctx.AddFarVariationDependencies([]blueprint.Variation{
867 {Mutator: "link", Variation: "shared"},
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()},
Jiyong Park64a44f22019-01-18 14:37:08 +0900870 }, earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900871 }
872 // static lib does not have dependency to the runtime library. The
873 // dependency will be added to the executables or shared libs using
874 // the static lib.
875 }
876 }
877}
878
879type Sanitizeable interface {
880 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900881 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900882 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900883}
884
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000885// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700886func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
887 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000888 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000889 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900890 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700891 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000892 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
893 // Save original sanitizer status before we assign values to variant
894 // 0 as that overwrites the original.
895 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
896
Jiyong Park82226632019-02-01 10:50:50 +0900897 modules := mctx.CreateVariations("", t.variationName())
Colin Crossb0f28952016-09-19 16:46:53 -0700898 modules[0].(*Module).sanitize.SetSanitizer(t, false)
899 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000900
Colin Crossb0f28952016-09-19 16:46:53 -0700901 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
902 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800903
904 // We don't need both variants active for anything but CFI-enabled
905 // target static libraries, so suppress the appropriate variant in
906 // all other cases.
907 if t == cfi {
908 if c.static() {
909 if !mctx.Device() {
910 if isSanitizerEnabled {
911 modules[0].(*Module).Properties.PreventInstall = true
912 modules[0].(*Module).Properties.HideFromMake = true
913 } else {
914 modules[1].(*Module).Properties.PreventInstall = true
915 modules[1].(*Module).Properties.HideFromMake = true
916 }
917 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800918 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800919
920 cfiStaticLibsMutex.Lock()
921 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
922 cfiStaticLibsMutex.Unlock()
923 }
924 } else {
925 modules[0].(*Module).Properties.PreventInstall = true
926 modules[0].(*Module).Properties.HideFromMake = true
927 }
928 } else if t == asan {
929 if mctx.Device() {
930 // CFI and ASAN are currently mutually exclusive so disable
931 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000932 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
933 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
934 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700935 if isSanitizerEnabled {
936 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800937 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700938 } else {
939 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800940 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700941 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800942 } else if t == scs {
943 // We don't currently link any static libraries built with make into
944 // libraries built with SCS, so we don't need logic for propagating
945 // SCSness of dependencies into make.
946 if !c.static() {
947 if isSanitizerEnabled {
948 modules[0].(*Module).Properties.PreventInstall = true
949 modules[0].(*Module).Properties.HideFromMake = true
950 } else {
951 modules[1].(*Module).Properties.PreventInstall = true
952 modules[1].(*Module).Properties.HideFromMake = true
953 }
954 }
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700955 } else if t == fuzzer {
956 // TODO(b/131771163): CFI and fuzzer support are mutually incompatible
957 // as CFI pulls in LTO.
958 if mctx.Device() {
959 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
960 }
961 if isSanitizerEnabled {
962 modules[0].(*Module).Properties.PreventInstall = true
963 modules[0].(*Module).Properties.HideFromMake = true
964 } else {
965 modules[1].(*Module).Properties.PreventInstall = true
966 modules[1].(*Module).Properties.HideFromMake = true
967 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700968 } else if t == hwasan {
969 if mctx.Device() {
970 // CFI and HWASAN are currently mutually exclusive so disable
971 // CFI if this is an HWASAN variant.
972 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
973 }
974
975 if c.static() {
976 if c.useVndk() {
977 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
978 hwasanStaticLibsMutex.Lock()
979 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
980 hwasanStaticLibsMutex.Unlock()
981 } else {
982 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
983 hwasanStaticLibsMutex.Lock()
984 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
985 hwasanStaticLibsMutex.Unlock()
986 }
987 } else {
988 if isSanitizerEnabled {
989 modules[0].(*Module).Properties.PreventInstall = true
990 modules[0].(*Module).Properties.HideFromMake = true
991 } else {
992 modules[1].(*Module).Properties.PreventInstall = true
993 modules[1].(*Module).Properties.HideFromMake = true
994 }
995 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700996 }
Colin Cross16b23492016-01-06 14:41:07 -0800997 }
998 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +0900999 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001000 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +09001001 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -08001002 }
1003 }
1004}
Vishwath Mohane7128792017-11-17 11:08:10 -08001005
Colin Cross571cccf2019-02-04 11:22:08 -08001006var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1007
Vishwath Mohane7128792017-11-17 11:08:10 -08001008func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001009 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -08001010 return &[]string{}
1011 }).(*[]string)
1012}
1013
Colin Cross571cccf2019-02-04 11:22:08 -08001014var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1015
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001016func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001017 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001018 return &[]string{}
1019 }).(*[]string)
1020}
1021
Colin Cross571cccf2019-02-04 11:22:08 -08001022var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
1023
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001024func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001025 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001026 return &[]string{}
1027 }).(*[]string)
1028}
1029
Ivan Lozano30c5db22018-02-21 15:49:20 -08001030func enableMinimalRuntime(sanitize *sanitize) bool {
1031 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001032 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillipsbfeade62019-05-01 14:42:05 -07001033 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -08001034 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
1035 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
1036 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1037 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
1038 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
1039 return true
1040 }
1041 return false
1042}
1043
Vishwath Mohane7128792017-11-17 11:08:10 -08001044func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1045 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001046 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001047 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1048}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001049
1050func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1051 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1052 sort.Strings(*hwasanStaticLibs)
1053 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1054
1055 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1056 sort.Strings(*hwasanVendorStaticLibs)
1057 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1058}