blob: 0eb9a7425d3e51be53ab6207926d1c5d8266c375 [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",
Evgenii Stepanov1c69e832019-06-14 18:37:33 -070043 "-fsanitize-hwaddress-abi=platform",
44 "-fno-experimental-new-pass-manager"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070045
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000046 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070047 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070048 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
49 // used, but have no effect on assembly files
50 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070051 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070052 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070053 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
54 cfiStaticLibsMutex sync.Mutex
55 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070056
Evgenii Stepanov98f5b062018-11-29 15:12:51 -080057 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080058
Peter Collingbournebd19db02019-03-06 10:38:48 -080059 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070060 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov3c5a52a2018-12-18 17:02:44 -080061 hwasanGlobalOptions = []string{"heap_history_size=1023,stack_history_size=512"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070062)
63
Colin Cross16b23492016-01-06 14:41:07 -080064type sanitizerType int
65
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070066func boolPtr(v bool) *bool {
67 if v {
68 return &v
69 } else {
70 return nil
71 }
72}
73
Colin Cross16b23492016-01-06 14:41:07 -080074const (
75 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070076 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080077 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070078 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000079 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080080 scs
Mitch Phillipsbfeade62019-05-01 14:42:05 -070081 fuzzer
Colin Cross16b23492016-01-06 14:41:07 -080082)
83
Jiyong Park82226632019-02-01 10:50:50 +090084// Name of the sanitizer variation for this sanitizer type
85func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080086 switch t {
87 case asan:
88 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070089 case hwasan:
90 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080091 case tsan:
92 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070093 case intOverflow:
94 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000095 case cfi:
96 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080097 case scs:
98 return "scs"
Mitch Phillipsbfeade62019-05-01 14:42:05 -070099 case fuzzer:
100 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800101 default:
102 panic(fmt.Errorf("unknown sanitizerType %d", t))
103 }
104}
105
Jiyong Park82226632019-02-01 10:50:50 +0900106// This is the sanitizer names in SANITIZE_[TARGET|HOST]
107func (t sanitizerType) name() string {
108 switch t {
109 case asan:
110 return "address"
111 case hwasan:
112 return "hwaddress"
113 case tsan:
114 return "thread"
115 case intOverflow:
116 return "integer_overflow"
117 case cfi:
118 return "cfi"
119 case scs:
120 return "shadow-call-stack"
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700121 case fuzzer:
122 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900123 default:
124 panic(fmt.Errorf("unknown sanitizerType %d", t))
125 }
126}
127
Colin Cross16b23492016-01-06 14:41:07 -0800128type SanitizeProperties struct {
129 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
130 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800131 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800132
133 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700134 Address *bool `android:"arch_variant"`
135 Thread *bool `android:"arch_variant"`
136 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800137
138 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700139 Undefined *bool `android:"arch_variant"`
140 All_undefined *bool `android:"arch_variant"`
141 Misc_undefined []string `android:"arch_variant"`
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700142 Fuzzer *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700143 Safestack *bool `android:"arch_variant"`
144 Cfi *bool `android:"arch_variant"`
145 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700146 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800147 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800148
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700149 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
150 // Replaces abort() on error with a human-readable error message.
151 // Address and Thread sanitizers always run in diagnostic mode.
152 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700153 Undefined *bool `android:"arch_variant"`
154 Cfi *bool `android:"arch_variant"`
155 Integer_overflow *bool `android:"arch_variant"`
156 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800157 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700158 }
159
160 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800161 Recover []string
162
163 // value to pass to -fsanitize-blacklist
164 Blacklist *string
165 } `android:"arch_variant"`
166
Jiyong Park379de2f2018-12-19 02:47:14 +0900167 SanitizerEnabled bool `blueprint:"mutated"`
168 SanitizeDep bool `blueprint:"mutated"`
169 MinimalRuntimeDep bool `blueprint:"mutated"`
170 UbsanRuntimeDep bool `blueprint:"mutated"`
171 InSanitizerDir bool `blueprint:"mutated"`
172 Sanitizers []string `blueprint:"mutated"`
173 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800174}
175
176type sanitize struct {
177 Properties SanitizeProperties
178}
179
Vishwath Mohane7128792017-11-17 11:08:10 -0800180func init() {
181 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700182 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800183}
184
Colin Cross16b23492016-01-06 14:41:07 -0800185func (sanitize *sanitize) props() []interface{} {
186 return []interface{}{&sanitize.Properties}
187}
188
189func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700190 s := &sanitize.Properties.Sanitize
191
Colin Cross16b23492016-01-06 14:41:07 -0800192 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700193 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800194 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800195 }
196
Doug Hornc32c6b02019-01-17 14:44:05 -0800197 // Sanitizers do not work on Fuchsia yet.
198 if ctx.Fuchsia() {
199 s.Never = BoolPtr(true)
200 }
201
Colin Cross16b23492016-01-06 14:41:07 -0800202 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800203 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800204 return
205 }
206
Colin Cross16b23492016-01-06 14:41:07 -0800207 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700208 var globalSanitizersDiag []string
209
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700210 if ctx.Host() {
211 if !ctx.Windows() {
212 globalSanitizers = ctx.Config().SanitizeHost()
213 }
214 } else {
215 arches := ctx.Config().SanitizeDeviceArch()
216 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
217 globalSanitizers = ctx.Config().SanitizeDevice()
218 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800219 }
220 }
221
Colin Cross16b23492016-01-06 14:41:07 -0800222 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000223 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700224 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
225 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000226 }
Colin Cross16b23492016-01-06 14:41:07 -0800227
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700228 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
229 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000230 }
231
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700232 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
233 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000234 }
235
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700236 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
237 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000238 }
239
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700240 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
241 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700242 }
243
244 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
245 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000246 }
247
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700248 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800249 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700250 s.Cfi = boolPtr(true)
251 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700252 }
253
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700254 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700255 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700256 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700257 s.Integer_overflow = boolPtr(true)
258 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700259 }
260
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700261 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
262 s.Scudo = boolPtr(true)
263 }
264
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700265 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
266 s.Hwaddress = boolPtr(true)
267 }
268
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000269 if len(globalSanitizers) > 0 {
270 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
271 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700272
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700273 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700274 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700275 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700276 s.Diag.Integer_overflow = boolPtr(true)
277 }
278
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700279 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
280 s.Diag.Cfi == nil && Bool(s.Cfi) {
281 s.Diag.Cfi = boolPtr(true)
282 }
283
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700284 if len(globalSanitizersDiag) > 0 {
285 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
286 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700287 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700288
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700289 // Enable CFI for all components in the include paths (for Aarch64 only)
290 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000291 s.Cfi = boolPtr(true)
292 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
293 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700294 }
295 }
296
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800297 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800298 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800299 s.Cfi = nil
300 s.Diag.Cfi = nil
301 }
302
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800303 // Also disable CFI for arm32 until b/35157333 is fixed.
304 if ctx.Arch().ArchType == android.Arm {
305 s.Cfi = nil
306 s.Diag.Cfi = nil
307 }
308
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700309 // HWASan requires AArch64 hardware feature (top-byte-ignore).
310 if ctx.Arch().ArchType != android.Arm64 {
311 s.Hwaddress = nil
312 }
313
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800314 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800315 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800316 s.Scs = nil
317 }
318
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700319 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700320 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700321 s.Cfi = nil
322 s.Diag.Cfi = nil
323 }
324
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700325 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800326 if ctx.Host() {
327 s.Cfi = nil
328 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700329 s.Misc_undefined = nil
330 s.Undefined = nil
331 s.All_undefined = nil
332 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800333 }
334
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700335 // Also disable CFI for VNDK variants of components
336 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700337 s.Cfi = nil
338 s.Diag.Cfi = nil
339 }
340
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700341 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800342 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
343 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700344 s.Hwaddress = nil
345 }
346
Colin Cross3c344ef2016-07-18 15:44:56 -0700347 if ctx.staticBinary() {
348 s.Address = nil
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700349 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700350 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800351 }
352
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700353 if Bool(s.All_undefined) {
354 s.Undefined = nil
355 }
356
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700357 if !ctx.toolchain().Is64Bit() {
358 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700359 s.Thread = nil
360 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800361 // TODO(ccross): error for compile_multilib = "32"?
362 }
363
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800364 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 -0700365 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 -0800366 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700367 sanitize.Properties.SanitizerEnabled = true
368 }
369
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800370 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
371 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700372 s.Scudo = nil
373 }
374
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700375 if Bool(s.Hwaddress) {
376 s.Address = nil
377 s.Thread = nil
378 }
379
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700380 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
381 // mutually incompatible.
382 if Bool(s.Fuzzer) {
383 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800384 }
385}
386
387func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
388 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
389 return deps
390 }
391
392 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700393 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700394 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Christopher Ferris753d4a62019-05-09 13:27:02 -0700395 // Compiling asan and having libc_scudo in the same
396 // executable will cause the executable to crash.
397 // Remove libc_scudo since it is only used to override
398 // allocation functions which asan already overrides.
399 _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800400 }
401 }
402
403 return deps
404}
405
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800406func toDisableImplicitIntegerChange(flags []string) bool {
407 // Returns true if any flag is fsanitize*integer, and there is
408 // no explicit flag about sanitize=implicit-integer-sign-change.
409 for _, f := range flags {
410 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
411 return false
412 }
413 }
414 for _, f := range flags {
415 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
416 return true
417 }
418 }
419 return false
420}
421
Colin Cross16b23492016-01-06 14:41:07 -0800422func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700423 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
424 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800425
426 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
427 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700428 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800429 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700430 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800431 return flags
432 }
433
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700434 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700435 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800436 // Frame pointer based unwinder in ASan requires ARM frame setup.
437 // TODO: put in flags?
438 flags.RequiredInstructionSet = "arm"
439 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700440 flags.CFlags = append(flags.CFlags, asanCflags...)
441 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800442
Colin Cross16b23492016-01-06 14:41:07 -0800443 if ctx.Host() {
444 // -nodefaultlibs (provided with libc++) prevents the driver from linking
445 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800446 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
447 } else {
Yi Kongda069082019-08-22 00:46:36 +0000448 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900449 if ctx.bootstrap() {
450 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
451 } else {
452 flags.DynamicLinker = "/system/bin/linker_asan"
453 }
Colin Cross16b23492016-01-06 14:41:07 -0800454 if flags.Toolchain.Is64Bit() {
455 flags.DynamicLinker += "64"
456 }
457 }
Colin Cross16b23492016-01-06 14:41:07 -0800458 }
459
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700460 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
461 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700462 }
463
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700464 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
465 flags.CFlags = append(flags.CFlags, "-fsanitize=fuzzer-no-link")
466 flags.LdFlags = append(flags.LdFlags, "-fsanitize=fuzzer-no-link")
467
468 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
469 _, flags.LdFlags = removeFromList("-flto", flags.LdFlags)
470 flags.LdFlags = append(flags.LdFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700471
472 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
473 _, flags.CFlags = removeFromList("-fexperimental-new-pass-manager", flags.CFlags)
474 flags.CFlags = append(flags.CFlags, "-fno-experimental-new-pass-manager")
Colin Cross16b23492016-01-06 14:41:07 -0800475 }
476
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700477 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800478 if ctx.Arch().ArchType == android.Arm {
479 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
480 // to do this on a function basis, so force Thumb on the entire module.
481 flags.RequiredInstructionSet = "thumb"
482 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000483
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700484 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700485 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000486 // Only append the default visibility flag if -fvisibility has not already been set
487 // to hidden.
488 if !inList("-fvisibility=hidden", flags.CFlags) {
489 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
490 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700491 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000492
493 if ctx.staticBinary() {
494 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
495 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
496 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700497 }
498
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700499 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700500 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700501 }
502
Jiyong Park379de2f2018-12-19 02:47:14 +0900503 if len(sanitize.Properties.Sanitizers) > 0 {
504 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800505
Colin Cross16b23492016-01-06 14:41:07 -0800506 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700507 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800508 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800509 // Host sanitizers only link symbols in the final executable, so
510 // there will always be undefined symbols in intermediate libraries.
511 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700512 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800513 } else {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800514 if enableMinimalRuntime(sanitize) {
515 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
516 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700517 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800518 }
Colin Cross16b23492016-01-06 14:41:07 -0800519 }
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700520
521 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
522 // When fuzzing, we wish to crash with diagnostics on any bug.
523 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
524 } else if ctx.Host() {
525 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
526 } else {
527 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
528 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800529 // http://b/119329758, Android core does not boot up with this sanitizer yet.
530 if toDisableImplicitIntegerChange(flags.CFlags) {
531 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
532 }
Colin Cross16b23492016-01-06 14:41:07 -0800533 }
534
Jiyong Park379de2f2018-12-19 02:47:14 +0900535 if len(sanitize.Properties.DiagSanitizers) > 0 {
536 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700537 }
538 // FIXME: enable RTTI if diag + (cfi or vptr)
539
Andreas Gampe97071162017-05-08 13:15:23 -0700540 if sanitize.Properties.Sanitize.Recover != nil {
541 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
542 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
543 }
544
Ivan Lozano7929bba2018-12-12 09:36:31 -0800545 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
546 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
547 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
548 }
549
Colin Cross635c3b02016-05-18 15:37:25 -0700550 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800551 if blacklist.Valid() {
552 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
553 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
554 }
555
556 return flags
557}
558
Colin Cross8ff9ef42017-05-08 13:44:11 -0700559func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Vishwath Mohane7128792017-11-17 11:08:10 -0800560 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
561 // name conflict.
562 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
563 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000564 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700565 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
566 ret.SubName += ".hwasan"
567 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800568 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
569 ret.SubName += ".scs"
570 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700571}
572
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700573func (sanitize *sanitize) inSanitizerDir() bool {
574 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700575}
576
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000577func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000578 switch t {
579 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000580 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700581 case hwasan:
582 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000583 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000584 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000585 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000586 return sanitize.Properties.Sanitize.Integer_overflow
587 case cfi:
588 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800589 case scs:
590 return sanitize.Properties.Sanitize.Scs
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700591 case fuzzer:
592 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000593 default:
594 panic(fmt.Errorf("unknown sanitizerType %d", t))
595 }
596}
597
Dan Albert7d1eecf2018-01-19 12:30:45 -0800598func (sanitize *sanitize) isUnsanitizedVariant() bool {
599 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700600 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800601 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800602 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700603 !sanitize.isSanitizerEnabled(scs) &&
604 !sanitize.isSanitizerEnabled(fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800605}
606
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700607func (sanitize *sanitize) isVariantOnProductionDevice() bool {
608 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700609 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700610 !sanitize.isSanitizerEnabled(tsan) &&
611 !sanitize.isSanitizerEnabled(fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700612}
613
Colin Cross16b23492016-01-06 14:41:07 -0800614func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
615 switch t {
616 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700617 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700618 case hwasan:
619 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800620 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700621 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700622 case intOverflow:
623 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000624 case cfi:
625 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800626 case scs:
627 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700628 case fuzzer:
629 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800630 default:
631 panic(fmt.Errorf("unknown sanitizerType %d", t))
632 }
633 if b {
634 sanitize.Properties.SanitizerEnabled = true
635 }
636}
637
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000638// Check if the sanitizer is explicitly disabled (as opposed to nil by
639// virtue of not being set).
640func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
641 if sanitize == nil {
642 return false
643 }
644
645 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
646 return sanitizerVal != nil && *sanitizerVal == false
647}
648
649// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
650// because enabling a sanitizer either directly (via the blueprint) or
651// indirectly (via a mutator) sets the bool ptr to true, and you can't
652// distinguish between the cases. It isn't needed though - both cases can be
653// treated identically.
654func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
655 if sanitize == nil {
656 return false
657 }
658
659 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
660 return sanitizerVal != nil && *sanitizerVal == true
661}
662
Colin Cross6b753602018-06-21 13:03:07 -0700663func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
664 t, ok := tag.(dependencyTag)
665 return ok && t.library || t == reuseObjTag
666}
667
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700668// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700669func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
670 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000671 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700672 mctx.WalkDeps(func(child, parent android.Module) bool {
673 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
674 return false
675 }
676 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800677 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000678 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800679 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700680 if d.static() {
681 d.sanitize.Properties.SanitizeDep = true
682 }
683 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000684 d.sanitize.Properties.SanitizeDep = true
685 }
Colin Cross16b23492016-01-06 14:41:07 -0800686 }
Colin Cross6b753602018-06-21 13:03:07 -0700687 return true
Colin Cross16b23492016-01-06 14:41:07 -0800688 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900689 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
690 // If an APEX module includes a lib which is enabled for a sanitizer T, then
691 // the APEX module is also enabled for the same sanitizer type.
692 mctx.VisitDirectDeps(func(child android.Module) {
693 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
694 sanitizeable.EnableSanitizer(t.name())
695 }
696 })
Colin Cross16b23492016-01-06 14:41:07 -0800697 }
698 }
699}
700
Ivan Lozano30c5db22018-02-21 15:49:20 -0800701// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700702func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
703 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
704 mctx.WalkDeps(func(child, parent android.Module) bool {
705 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
706 return false
707 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800708
Colin Cross0b908332019-06-19 23:00:20 -0700709 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700710 if enableMinimalRuntime(d.sanitize) {
711 // If a static dependency is built with the minimal runtime,
712 // make sure we include the ubsan minimal runtime.
713 c.sanitize.Properties.MinimalRuntimeDep = true
714 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
715 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
716 // If a static dependency runs with full ubsan diagnostics,
717 // make sure we include the ubsan runtime.
718 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800719 }
Colin Cross0b908332019-06-19 23:00:20 -0700720
721 if c.sanitize.Properties.MinimalRuntimeDep &&
722 c.sanitize.Properties.UbsanRuntimeDep {
723 // both flags that this mutator might set are true, so don't bother recursing
724 return false
725 }
726
727 return true
728 } else {
729 return false
Colin Cross6b753602018-06-21 13:03:07 -0700730 }
Colin Cross6b753602018-06-21 13:03:07 -0700731 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800732 }
733}
734
Jiyong Park379de2f2018-12-19 02:47:14 +0900735// Add the dependency to the runtime library for each of the sanitizer variants
736func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900737 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000738 if !c.Enabled() {
739 return
740 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900741 var sanitizers []string
742 var diagSanitizers []string
743
744 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
745 sanitizers = append(sanitizers, "undefined")
746 } else {
747 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
748 sanitizers = append(sanitizers,
749 "bool",
750 "integer-divide-by-zero",
751 "return",
752 "returns-nonnull-attribute",
753 "shift-exponent",
754 "unreachable",
755 "vla-bound",
756 // TODO(danalbert): The following checks currently have compiler performance issues.
757 //"alignment",
758 //"bounds",
759 //"enum",
760 //"float-cast-overflow",
761 //"float-divide-by-zero",
762 //"nonnull-attribute",
763 //"null",
764 //"shift-base",
765 //"signed-integer-overflow",
766 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
767 // https://llvm.org/PR19302
768 // http://reviews.llvm.org/D6974
769 // "object-size",
770 )
771 }
772 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
773 }
774
775 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
776 diagSanitizers = append(diagSanitizers, "undefined")
777 }
778
779 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
780
781 if Bool(c.sanitize.Properties.Sanitize.Address) {
782 sanitizers = append(sanitizers, "address")
783 diagSanitizers = append(diagSanitizers, "address")
784 }
785
786 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
787 sanitizers = append(sanitizers, "hwaddress")
788 }
789
790 if Bool(c.sanitize.Properties.Sanitize.Thread) {
791 sanitizers = append(sanitizers, "thread")
792 }
793
794 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
795 sanitizers = append(sanitizers, "safe-stack")
796 }
797
798 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
799 sanitizers = append(sanitizers, "cfi")
800
801 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
802 diagSanitizers = append(diagSanitizers, "cfi")
803 }
804 }
805
806 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
807 sanitizers = append(sanitizers, "unsigned-integer-overflow")
808 sanitizers = append(sanitizers, "signed-integer-overflow")
809 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
810 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
811 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
812 }
813 }
814
815 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
816 sanitizers = append(sanitizers, "scudo")
817 }
818
819 if Bool(c.sanitize.Properties.Sanitize.Scs) {
820 sanitizers = append(sanitizers, "shadow-call-stack")
821 }
822
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700823 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
824 sanitizers = append(sanitizers, "fuzzer-no-link")
825 }
826
Jiyong Park379de2f2018-12-19 02:47:14 +0900827 // Save the list of sanitizers. These will be used again when generating
828 // the build rules (for Cflags, etc.)
829 c.sanitize.Properties.Sanitizers = sanitizers
830 c.sanitize.Properties.DiagSanitizers = diagSanitizers
831
832 // Determine the runtime library required
833 runtimeLibrary := ""
834 toolchain := c.toolchain(mctx)
835 if Bool(c.sanitize.Properties.Sanitize.Address) {
836 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
837 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
838 if c.staticBinary() {
839 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
840 } else {
841 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
842 }
843 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
844 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
845 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
846 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
847 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
848 } else {
849 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
850 }
851 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
852 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
853 }
854
855 if mctx.Device() && runtimeLibrary != "" {
Inseob Kim9516ee92019-05-09 10:56:13 +0900856 if inList(runtimeLibrary, *llndkLibraries(mctx.Config())) && !c.static() && c.useVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900857 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
858 }
859
860 // Adding dependency to the runtime library. We are using *FarVariation*
861 // because the runtime libraries themselves are not mutated by sanitizer
862 // mutators and thus don't have sanitizer variants whereas this module
863 // has been already mutated.
864 //
865 // Note that by adding dependency with {static|shared}DepTag, the lib is
866 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
867 if c.staticBinary() {
868 // static executable gets static runtime libs
869 mctx.AddFarVariationDependencies([]blueprint.Variation{
870 {Mutator: "link", Variation: "static"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900871 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900872 {Mutator: "arch", Variation: mctx.Target().String()},
873 }, staticDepTag, runtimeLibrary)
874 } else if !c.static() {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900875 // dynamic executable and shared libs get shared runtime libs
Jiyong Park379de2f2018-12-19 02:47:14 +0900876 mctx.AddFarVariationDependencies([]blueprint.Variation{
877 {Mutator: "link", Variation: "shared"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900878 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900879 {Mutator: "arch", Variation: mctx.Target().String()},
Jiyong Park64a44f22019-01-18 14:37:08 +0900880 }, earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900881 }
882 // static lib does not have dependency to the runtime library. The
883 // dependency will be added to the executables or shared libs using
884 // the static lib.
885 }
886 }
887}
888
889type Sanitizeable interface {
890 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900891 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900892 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900893}
894
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000895// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700896func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
897 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000898 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000899 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900900 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700901 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000902 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
903 // Save original sanitizer status before we assign values to variant
904 // 0 as that overwrites the original.
905 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
906
Jiyong Park82226632019-02-01 10:50:50 +0900907 modules := mctx.CreateVariations("", t.variationName())
Colin Crossb0f28952016-09-19 16:46:53 -0700908 modules[0].(*Module).sanitize.SetSanitizer(t, false)
909 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000910
Colin Crossb0f28952016-09-19 16:46:53 -0700911 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
912 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800913
914 // We don't need both variants active for anything but CFI-enabled
915 // target static libraries, so suppress the appropriate variant in
916 // all other cases.
917 if t == cfi {
918 if c.static() {
919 if !mctx.Device() {
920 if isSanitizerEnabled {
921 modules[0].(*Module).Properties.PreventInstall = true
922 modules[0].(*Module).Properties.HideFromMake = true
923 } else {
924 modules[1].(*Module).Properties.PreventInstall = true
925 modules[1].(*Module).Properties.HideFromMake = true
926 }
927 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800928 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800929
930 cfiStaticLibsMutex.Lock()
931 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
932 cfiStaticLibsMutex.Unlock()
933 }
934 } else {
935 modules[0].(*Module).Properties.PreventInstall = true
936 modules[0].(*Module).Properties.HideFromMake = true
937 }
938 } else if t == asan {
939 if mctx.Device() {
940 // CFI and ASAN are currently mutually exclusive so disable
941 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000942 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
943 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
944 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700945 if isSanitizerEnabled {
946 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800947 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700948 } else {
949 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800950 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700951 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800952 } else if t == scs {
953 // We don't currently link any static libraries built with make into
954 // libraries built with SCS, so we don't need logic for propagating
955 // SCSness of dependencies into make.
956 if !c.static() {
957 if isSanitizerEnabled {
958 modules[0].(*Module).Properties.PreventInstall = true
959 modules[0].(*Module).Properties.HideFromMake = true
960 } else {
961 modules[1].(*Module).Properties.PreventInstall = true
962 modules[1].(*Module).Properties.HideFromMake = true
963 }
964 }
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700965 } else if t == fuzzer {
966 // TODO(b/131771163): CFI and fuzzer support are mutually incompatible
967 // as CFI pulls in LTO.
968 if mctx.Device() {
969 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
970 }
971 if isSanitizerEnabled {
972 modules[0].(*Module).Properties.PreventInstall = true
973 modules[0].(*Module).Properties.HideFromMake = true
974 } else {
975 modules[1].(*Module).Properties.PreventInstall = true
976 modules[1].(*Module).Properties.HideFromMake = true
977 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700978 } else if t == hwasan {
979 if mctx.Device() {
980 // CFI and HWASAN are currently mutually exclusive so disable
981 // CFI if this is an HWASAN variant.
982 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
983 }
984
985 if c.static() {
986 if c.useVndk() {
987 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
988 hwasanStaticLibsMutex.Lock()
989 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
990 hwasanStaticLibsMutex.Unlock()
991 } else {
992 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
993 hwasanStaticLibsMutex.Lock()
994 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
995 hwasanStaticLibsMutex.Unlock()
996 }
997 } else {
998 if isSanitizerEnabled {
999 modules[0].(*Module).Properties.PreventInstall = true
1000 modules[0].(*Module).Properties.HideFromMake = true
1001 } else {
1002 modules[1].(*Module).Properties.PreventInstall = true
1003 modules[1].(*Module).Properties.HideFromMake = true
1004 }
1005 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001006 }
Colin Cross16b23492016-01-06 14:41:07 -08001007 }
1008 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +09001009 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001010 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +09001011 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -08001012 }
1013 }
1014}
Vishwath Mohane7128792017-11-17 11:08:10 -08001015
Colin Cross571cccf2019-02-04 11:22:08 -08001016var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1017
Vishwath Mohane7128792017-11-17 11:08:10 -08001018func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001019 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -08001020 return &[]string{}
1021 }).(*[]string)
1022}
1023
Colin Cross571cccf2019-02-04 11:22:08 -08001024var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1025
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001026func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001027 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001028 return &[]string{}
1029 }).(*[]string)
1030}
1031
Colin Cross571cccf2019-02-04 11:22:08 -08001032var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
1033
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001034func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001035 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001036 return &[]string{}
1037 }).(*[]string)
1038}
1039
Ivan Lozano30c5db22018-02-21 15:49:20 -08001040func enableMinimalRuntime(sanitize *sanitize) bool {
1041 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001042 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillipsbfeade62019-05-01 14:42:05 -07001043 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -08001044 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
1045 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
1046 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1047 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
1048 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
1049 return true
1050 }
1051 return false
1052}
1053
Vishwath Mohane7128792017-11-17 11:08:10 -08001054func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1055 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001056 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001057 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1058}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001059
1060func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1061 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1062 sort.Strings(*hwasanStaticLibs)
1063 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1064
1065 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1066 sort.Strings(*hwasanVendorStaticLibs)
1067 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1068}