blob: 08736e0e8fc71eaa09e1da9af8ebdc9f823f6713 [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 Stepanov2c6484e2019-05-15 12:49:54 -070061 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
62 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070063)
64
Colin Cross16b23492016-01-06 14:41:07 -080065type sanitizerType int
66
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070067func boolPtr(v bool) *bool {
68 if v {
69 return &v
70 } else {
71 return nil
72 }
73}
74
Colin Cross16b23492016-01-06 14:41:07 -080075const (
76 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070077 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080078 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070079 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000080 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080081 scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -070082 fuzzer
Colin Cross16b23492016-01-06 14:41:07 -080083)
84
Jiyong Park82226632019-02-01 10:50:50 +090085// Name of the sanitizer variation for this sanitizer type
86func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080087 switch t {
88 case asan:
89 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070090 case hwasan:
91 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080092 case tsan:
93 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070094 case intOverflow:
95 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000096 case cfi:
97 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080098 case scs:
99 return "scs"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700100 case fuzzer:
101 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800102 default:
103 panic(fmt.Errorf("unknown sanitizerType %d", t))
104 }
105}
106
Jiyong Park82226632019-02-01 10:50:50 +0900107// This is the sanitizer names in SANITIZE_[TARGET|HOST]
108func (t sanitizerType) name() string {
109 switch t {
110 case asan:
111 return "address"
112 case hwasan:
113 return "hwaddress"
114 case tsan:
115 return "thread"
116 case intOverflow:
117 return "integer_overflow"
118 case cfi:
119 return "cfi"
120 case scs:
121 return "shadow-call-stack"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700122 case fuzzer:
123 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900124 default:
125 panic(fmt.Errorf("unknown sanitizerType %d", t))
126 }
127}
128
Colin Cross16b23492016-01-06 14:41:07 -0800129type SanitizeProperties struct {
130 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
131 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800132 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800133
134 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700135 Address *bool `android:"arch_variant"`
136 Thread *bool `android:"arch_variant"`
137 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800138
139 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700140 Undefined *bool `android:"arch_variant"`
141 All_undefined *bool `android:"arch_variant"`
142 Misc_undefined []string `android:"arch_variant"`
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700143 Fuzzer *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700144 Safestack *bool `android:"arch_variant"`
145 Cfi *bool `android:"arch_variant"`
146 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700147 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800148 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800149
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700150 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
151 // Replaces abort() on error with a human-readable error message.
152 // Address and Thread sanitizers always run in diagnostic mode.
153 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700154 Undefined *bool `android:"arch_variant"`
155 Cfi *bool `android:"arch_variant"`
156 Integer_overflow *bool `android:"arch_variant"`
157 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800158 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700159 }
160
161 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800162 Recover []string
163
164 // value to pass to -fsanitize-blacklist
165 Blacklist *string
166 } `android:"arch_variant"`
167
Jiyong Park379de2f2018-12-19 02:47:14 +0900168 SanitizerEnabled bool `blueprint:"mutated"`
169 SanitizeDep bool `blueprint:"mutated"`
170 MinimalRuntimeDep bool `blueprint:"mutated"`
171 UbsanRuntimeDep bool `blueprint:"mutated"`
172 InSanitizerDir bool `blueprint:"mutated"`
173 Sanitizers []string `blueprint:"mutated"`
174 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800175}
176
177type sanitize struct {
178 Properties SanitizeProperties
179}
180
Vishwath Mohane7128792017-11-17 11:08:10 -0800181func init() {
182 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700183 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800184}
185
Colin Cross16b23492016-01-06 14:41:07 -0800186func (sanitize *sanitize) props() []interface{} {
187 return []interface{}{&sanitize.Properties}
188}
189
190func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700191 s := &sanitize.Properties.Sanitize
192
Colin Cross16b23492016-01-06 14:41:07 -0800193 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700194 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800195 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800196 }
197
Doug Hornc32c6b02019-01-17 14:44:05 -0800198 // Sanitizers do not work on Fuchsia yet.
199 if ctx.Fuchsia() {
200 s.Never = BoolPtr(true)
201 }
202
Colin Cross16b23492016-01-06 14:41:07 -0800203 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800204 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800205 return
206 }
207
Colin Cross16b23492016-01-06 14:41:07 -0800208 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700209 var globalSanitizersDiag []string
210
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700211 if ctx.Host() {
212 if !ctx.Windows() {
213 globalSanitizers = ctx.Config().SanitizeHost()
214 }
215 } else {
216 arches := ctx.Config().SanitizeDeviceArch()
217 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
218 globalSanitizers = ctx.Config().SanitizeDevice()
219 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800220 }
221 }
222
Colin Cross16b23492016-01-06 14:41:07 -0800223 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000224 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700225 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
226 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000227 }
Colin Cross16b23492016-01-06 14:41:07 -0800228
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700229 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
230 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000231 }
232
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700233 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
234 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000235 }
236
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700237 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
238 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000239 }
240
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700241 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
242 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700243 }
244
245 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
246 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000247 }
248
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700249 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800250 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700251 s.Cfi = boolPtr(true)
252 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700253 }
254
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700255 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700256 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700257 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700258 s.Integer_overflow = boolPtr(true)
259 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700260 }
261
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700262 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
263 s.Scudo = boolPtr(true)
264 }
265
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700266 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
267 s.Hwaddress = boolPtr(true)
268 }
269
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000270 if len(globalSanitizers) > 0 {
271 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
272 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700273
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700274 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700275 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700276 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700277 s.Diag.Integer_overflow = boolPtr(true)
278 }
279
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700280 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
281 s.Diag.Cfi == nil && Bool(s.Cfi) {
282 s.Diag.Cfi = boolPtr(true)
283 }
284
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700285 if len(globalSanitizersDiag) > 0 {
286 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
287 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700288 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700289
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700290 // Enable CFI for all components in the include paths (for Aarch64 only)
291 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000292 s.Cfi = boolPtr(true)
293 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
294 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700295 }
296 }
297
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800298 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800299 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800300 s.Cfi = nil
301 s.Diag.Cfi = nil
302 }
303
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800304 // Also disable CFI for arm32 until b/35157333 is fixed.
305 if ctx.Arch().ArchType == android.Arm {
306 s.Cfi = nil
307 s.Diag.Cfi = nil
308 }
309
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700310 // HWASan requires AArch64 hardware feature (top-byte-ignore).
311 if ctx.Arch().ArchType != android.Arm64 {
312 s.Hwaddress = nil
313 }
314
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800315 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800316 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800317 s.Scs = nil
318 }
319
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700320 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700321 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700322 s.Cfi = nil
323 s.Diag.Cfi = nil
324 }
325
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700326 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800327 if ctx.Host() {
328 s.Cfi = nil
329 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700330 s.Misc_undefined = nil
331 s.Undefined = nil
332 s.All_undefined = nil
333 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800334 }
335
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700336 // Also disable CFI for VNDK variants of components
337 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700338 s.Cfi = nil
339 s.Diag.Cfi = nil
340 }
341
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700342 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800343 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
344 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700345 s.Hwaddress = nil
346 }
347
Colin Cross3c344ef2016-07-18 15:44:56 -0700348 if ctx.staticBinary() {
349 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700350 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700351 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800352 }
353
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700354 if Bool(s.All_undefined) {
355 s.Undefined = nil
356 }
357
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700358 if !ctx.toolchain().Is64Bit() {
359 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700360 s.Thread = nil
361 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800362 // TODO(ccross): error for compile_multilib = "32"?
363 }
364
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800365 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700366 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 -0800367 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700368 sanitize.Properties.SanitizerEnabled = true
369 }
370
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800371 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
372 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700373 s.Scudo = nil
374 }
375
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700376 if Bool(s.Hwaddress) {
377 s.Address = nil
378 s.Thread = nil
379 }
380
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700381 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
382 // mutually incompatible.
383 if Bool(s.Fuzzer) {
384 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800385 }
386}
387
388func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
389 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
390 return deps
391 }
392
393 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700394 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700395 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Christopher Ferris753d4a62019-05-09 13:27:02 -0700396 // Compiling asan and having libc_scudo in the same
397 // executable will cause the executable to crash.
398 // Remove libc_scudo since it is only used to override
399 // allocation functions which asan already overrides.
400 _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800401 }
402 }
403
404 return deps
405}
406
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800407func toDisableImplicitIntegerChange(flags []string) bool {
408 // Returns true if any flag is fsanitize*integer, and there is
409 // no explicit flag about sanitize=implicit-integer-sign-change.
410 for _, f := range flags {
411 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
412 return false
413 }
414 }
415 for _, f := range flags {
416 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
417 return true
418 }
419 }
420 return false
421}
422
Colin Cross16b23492016-01-06 14:41:07 -0800423func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700424 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
425 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800426
427 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
428 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700429 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800430 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700431 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800432 return flags
433 }
434
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700435 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700436 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800437 // Frame pointer based unwinder in ASan requires ARM frame setup.
438 // TODO: put in flags?
439 flags.RequiredInstructionSet = "arm"
440 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700441 flags.CFlags = append(flags.CFlags, asanCflags...)
442 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800443
Colin Cross16b23492016-01-06 14:41:07 -0800444 if ctx.Host() {
445 // -nodefaultlibs (provided with libc++) prevents the driver from linking
446 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800447 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
448 } else {
449 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900450 if ctx.bootstrap() {
451 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
452 } else {
453 flags.DynamicLinker = "/system/bin/linker_asan"
454 }
Colin Cross16b23492016-01-06 14:41:07 -0800455 if flags.Toolchain.Is64Bit() {
456 flags.DynamicLinker += "64"
457 }
458 }
Colin Cross16b23492016-01-06 14:41:07 -0800459 }
460
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700461 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
462 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700463 }
464
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700465 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
466 flags.CFlags = append(flags.CFlags, "-fsanitize=fuzzer-no-link")
467 flags.LdFlags = append(flags.LdFlags, "-fsanitize=fuzzer-no-link")
468
469 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
470 _, flags.LdFlags = removeFromList("-flto", flags.LdFlags)
471 flags.LdFlags = append(flags.LdFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700472
473 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
474 _, flags.CFlags = removeFromList("-fexperimental-new-pass-manager", flags.CFlags)
475 flags.CFlags = append(flags.CFlags, "-fno-experimental-new-pass-manager")
Colin Cross16b23492016-01-06 14:41:07 -0800476 }
477
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700478 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800479 if ctx.Arch().ArchType == android.Arm {
480 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
481 // to do this on a function basis, so force Thumb on the entire module.
482 flags.RequiredInstructionSet = "thumb"
483 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000484
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700485 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700486 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000487 // Only append the default visibility flag if -fvisibility has not already been set
488 // to hidden.
489 if !inList("-fvisibility=hidden", flags.CFlags) {
490 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
491 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700492 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000493
494 if ctx.staticBinary() {
495 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
496 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
497 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700498 }
499
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700500 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700501 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700502 }
503
Jiyong Park379de2f2018-12-19 02:47:14 +0900504 if len(sanitize.Properties.Sanitizers) > 0 {
505 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800506
Colin Cross16b23492016-01-06 14:41:07 -0800507 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700508 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800509 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800510 // Host sanitizers only link symbols in the final executable, so
511 // there will always be undefined symbols in intermediate libraries.
512 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700513 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800514 } else {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800515 if enableMinimalRuntime(sanitize) {
516 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
517 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700518 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800519 }
Colin Cross16b23492016-01-06 14:41:07 -0800520 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700521
522 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
523 // When fuzzing, we wish to crash with diagnostics on any bug.
524 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
525 } else if ctx.Host() {
526 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
527 } else {
528 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
529 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800530 // http://b/119329758, Android core does not boot up with this sanitizer yet.
531 if toDisableImplicitIntegerChange(flags.CFlags) {
532 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
533 }
Colin Cross16b23492016-01-06 14:41:07 -0800534 }
535
Jiyong Park379de2f2018-12-19 02:47:14 +0900536 if len(sanitize.Properties.DiagSanitizers) > 0 {
537 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700538 }
539 // FIXME: enable RTTI if diag + (cfi or vptr)
540
Andreas Gampe97071162017-05-08 13:15:23 -0700541 if sanitize.Properties.Sanitize.Recover != nil {
542 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
543 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
544 }
545
Ivan Lozano7929bba2018-12-12 09:36:31 -0800546 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
547 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
548 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
549 }
550
Colin Cross635c3b02016-05-18 15:37:25 -0700551 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800552 if blacklist.Valid() {
553 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
554 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
555 }
556
557 return flags
558}
559
Colin Cross8ff9ef42017-05-08 13:44:11 -0700560func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Vishwath Mohane7128792017-11-17 11:08:10 -0800561 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
562 // name conflict.
563 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
564 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000565 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700566 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
567 ret.SubName += ".hwasan"
568 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800569 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
570 ret.SubName += ".scs"
571 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700572}
573
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700574func (sanitize *sanitize) inSanitizerDir() bool {
575 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700576}
577
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000578func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000579 switch t {
580 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000581 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700582 case hwasan:
583 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000584 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000585 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000586 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000587 return sanitize.Properties.Sanitize.Integer_overflow
588 case cfi:
589 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800590 case scs:
591 return sanitize.Properties.Sanitize.Scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700592 case fuzzer:
593 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000594 default:
595 panic(fmt.Errorf("unknown sanitizerType %d", t))
596 }
597}
598
Dan Albert7d1eecf2018-01-19 12:30:45 -0800599func (sanitize *sanitize) isUnsanitizedVariant() bool {
600 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700601 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800602 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800603 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700604 !sanitize.isSanitizerEnabled(scs) &&
605 !sanitize.isSanitizerEnabled(fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800606}
607
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700608func (sanitize *sanitize) isVariantOnProductionDevice() bool {
609 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700610 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700611 !sanitize.isSanitizerEnabled(tsan) &&
612 !sanitize.isSanitizerEnabled(fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700613}
614
Colin Cross16b23492016-01-06 14:41:07 -0800615func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
616 switch t {
617 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700618 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700619 case hwasan:
620 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800621 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700622 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700623 case intOverflow:
624 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000625 case cfi:
626 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800627 case scs:
628 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700629 case fuzzer:
630 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800631 default:
632 panic(fmt.Errorf("unknown sanitizerType %d", t))
633 }
634 if b {
635 sanitize.Properties.SanitizerEnabled = true
636 }
637}
638
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000639// Check if the sanitizer is explicitly disabled (as opposed to nil by
640// virtue of not being set).
641func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
642 if sanitize == nil {
643 return false
644 }
645
646 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
647 return sanitizerVal != nil && *sanitizerVal == false
648}
649
650// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
651// because enabling a sanitizer either directly (via the blueprint) or
652// indirectly (via a mutator) sets the bool ptr to true, and you can't
653// distinguish between the cases. It isn't needed though - both cases can be
654// treated identically.
655func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
656 if sanitize == nil {
657 return false
658 }
659
660 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
661 return sanitizerVal != nil && *sanitizerVal == true
662}
663
Colin Cross6b753602018-06-21 13:03:07 -0700664func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
665 t, ok := tag.(dependencyTag)
666 return ok && t.library || t == reuseObjTag
667}
668
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700669// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700670func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
671 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000672 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700673 mctx.WalkDeps(func(child, parent android.Module) bool {
674 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
675 return false
676 }
677 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800678 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000679 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800680 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700681 if d.static() {
682 d.sanitize.Properties.SanitizeDep = true
683 }
684 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000685 d.sanitize.Properties.SanitizeDep = true
686 }
Colin Cross16b23492016-01-06 14:41:07 -0800687 }
Colin Cross6b753602018-06-21 13:03:07 -0700688 return true
Colin Cross16b23492016-01-06 14:41:07 -0800689 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900690 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
691 // If an APEX module includes a lib which is enabled for a sanitizer T, then
692 // the APEX module is also enabled for the same sanitizer type.
693 mctx.VisitDirectDeps(func(child android.Module) {
694 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
695 sanitizeable.EnableSanitizer(t.name())
696 }
697 })
Colin Cross16b23492016-01-06 14:41:07 -0800698 }
699 }
700}
701
Ivan Lozano30c5db22018-02-21 15:49:20 -0800702// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700703func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
704 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
705 mctx.WalkDeps(func(child, parent android.Module) bool {
706 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
707 return false
708 }
709 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800710
Colin Cross6b753602018-06-21 13:03:07 -0700711 if enableMinimalRuntime(d.sanitize) {
712 // If a static dependency is built with the minimal runtime,
713 // make sure we include the ubsan minimal runtime.
714 c.sanitize.Properties.MinimalRuntimeDep = true
715 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
716 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
717 // If a static dependency runs with full ubsan diagnostics,
718 // make sure we include the ubsan runtime.
719 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800720 }
Colin Cross6b753602018-06-21 13:03:07 -0700721 }
722 return true
723 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800724 }
725}
726
Jiyong Park379de2f2018-12-19 02:47:14 +0900727// Add the dependency to the runtime library for each of the sanitizer variants
728func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900729 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000730 if !c.Enabled() {
731 return
732 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900733 var sanitizers []string
734 var diagSanitizers []string
735
736 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
737 sanitizers = append(sanitizers, "undefined")
738 } else {
739 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
740 sanitizers = append(sanitizers,
741 "bool",
742 "integer-divide-by-zero",
743 "return",
744 "returns-nonnull-attribute",
745 "shift-exponent",
746 "unreachable",
747 "vla-bound",
748 // TODO(danalbert): The following checks currently have compiler performance issues.
749 //"alignment",
750 //"bounds",
751 //"enum",
752 //"float-cast-overflow",
753 //"float-divide-by-zero",
754 //"nonnull-attribute",
755 //"null",
756 //"shift-base",
757 //"signed-integer-overflow",
758 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
759 // https://llvm.org/PR19302
760 // http://reviews.llvm.org/D6974
761 // "object-size",
762 )
763 }
764 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
765 }
766
767 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
768 diagSanitizers = append(diagSanitizers, "undefined")
769 }
770
771 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
772
773 if Bool(c.sanitize.Properties.Sanitize.Address) {
774 sanitizers = append(sanitizers, "address")
775 diagSanitizers = append(diagSanitizers, "address")
776 }
777
778 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
779 sanitizers = append(sanitizers, "hwaddress")
780 }
781
782 if Bool(c.sanitize.Properties.Sanitize.Thread) {
783 sanitizers = append(sanitizers, "thread")
784 }
785
786 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
787 sanitizers = append(sanitizers, "safe-stack")
788 }
789
790 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
791 sanitizers = append(sanitizers, "cfi")
792
793 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
794 diagSanitizers = append(diagSanitizers, "cfi")
795 }
796 }
797
798 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
799 sanitizers = append(sanitizers, "unsigned-integer-overflow")
800 sanitizers = append(sanitizers, "signed-integer-overflow")
801 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
802 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
803 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
804 }
805 }
806
807 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
808 sanitizers = append(sanitizers, "scudo")
809 }
810
811 if Bool(c.sanitize.Properties.Sanitize.Scs) {
812 sanitizers = append(sanitizers, "shadow-call-stack")
813 }
814
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700815 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
816 sanitizers = append(sanitizers, "fuzzer-no-link")
817 }
818
Jiyong Park379de2f2018-12-19 02:47:14 +0900819 // Save the list of sanitizers. These will be used again when generating
820 // the build rules (for Cflags, etc.)
821 c.sanitize.Properties.Sanitizers = sanitizers
822 c.sanitize.Properties.DiagSanitizers = diagSanitizers
823
824 // Determine the runtime library required
825 runtimeLibrary := ""
826 toolchain := c.toolchain(mctx)
827 if Bool(c.sanitize.Properties.Sanitize.Address) {
828 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
829 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
830 if c.staticBinary() {
831 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
832 } else {
833 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
834 }
835 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
836 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
837 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
838 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
839 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
840 } else {
841 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
842 }
843 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
844 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
845 }
846
847 if mctx.Device() && runtimeLibrary != "" {
Inseob Kim9516ee92019-05-09 10:56:13 +0900848 if inList(runtimeLibrary, *llndkLibraries(mctx.Config())) && !c.static() && c.useVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900849 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
850 }
851
852 // Adding dependency to the runtime library. We are using *FarVariation*
853 // because the runtime libraries themselves are not mutated by sanitizer
854 // mutators and thus don't have sanitizer variants whereas this module
855 // has been already mutated.
856 //
857 // Note that by adding dependency with {static|shared}DepTag, the lib is
858 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
859 if c.staticBinary() {
860 // static executable gets static runtime libs
861 mctx.AddFarVariationDependencies([]blueprint.Variation{
862 {Mutator: "link", Variation: "static"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900863 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900864 {Mutator: "arch", Variation: mctx.Target().String()},
865 }, staticDepTag, runtimeLibrary)
866 } else if !c.static() {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900867 // dynamic executable and shared libs get shared runtime libs
Jiyong Park379de2f2018-12-19 02:47:14 +0900868 mctx.AddFarVariationDependencies([]blueprint.Variation{
869 {Mutator: "link", Variation: "shared"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900870 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900871 {Mutator: "arch", Variation: mctx.Target().String()},
Jiyong Park64a44f22019-01-18 14:37:08 +0900872 }, earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900873 }
874 // static lib does not have dependency to the runtime library. The
875 // dependency will be added to the executables or shared libs using
876 // the static lib.
877 }
878 }
879}
880
881type Sanitizeable interface {
882 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900883 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900884 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900885}
886
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000887// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700888func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
889 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000890 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000891 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900892 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700893 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000894 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
895 // Save original sanitizer status before we assign values to variant
896 // 0 as that overwrites the original.
897 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
898
Jiyong Park82226632019-02-01 10:50:50 +0900899 modules := mctx.CreateVariations("", t.variationName())
Colin Crossb0f28952016-09-19 16:46:53 -0700900 modules[0].(*Module).sanitize.SetSanitizer(t, false)
901 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000902
Colin Crossb0f28952016-09-19 16:46:53 -0700903 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
904 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800905
906 // We don't need both variants active for anything but CFI-enabled
907 // target static libraries, so suppress the appropriate variant in
908 // all other cases.
909 if t == cfi {
910 if c.static() {
911 if !mctx.Device() {
912 if isSanitizerEnabled {
913 modules[0].(*Module).Properties.PreventInstall = true
914 modules[0].(*Module).Properties.HideFromMake = true
915 } else {
916 modules[1].(*Module).Properties.PreventInstall = true
917 modules[1].(*Module).Properties.HideFromMake = true
918 }
919 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800920 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800921
922 cfiStaticLibsMutex.Lock()
923 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
924 cfiStaticLibsMutex.Unlock()
925 }
926 } else {
927 modules[0].(*Module).Properties.PreventInstall = true
928 modules[0].(*Module).Properties.HideFromMake = true
929 }
930 } else if t == asan {
931 if mctx.Device() {
932 // CFI and ASAN are currently mutually exclusive so disable
933 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000934 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
935 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
936 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700937 if isSanitizerEnabled {
938 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800939 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700940 } else {
941 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800942 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700943 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800944 } else if t == scs {
945 // We don't currently link any static libraries built with make into
946 // libraries built with SCS, so we don't need logic for propagating
947 // SCSness of dependencies into make.
948 if !c.static() {
949 if isSanitizerEnabled {
950 modules[0].(*Module).Properties.PreventInstall = true
951 modules[0].(*Module).Properties.HideFromMake = true
952 } else {
953 modules[1].(*Module).Properties.PreventInstall = true
954 modules[1].(*Module).Properties.HideFromMake = true
955 }
956 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700957 } else if t == fuzzer {
958 // TODO(b/131771163): CFI and fuzzer support are mutually incompatible
959 // as CFI pulls in LTO.
960 if mctx.Device() {
961 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
962 }
963 if isSanitizerEnabled {
964 modules[0].(*Module).Properties.PreventInstall = true
965 modules[0].(*Module).Properties.HideFromMake = true
966 } else {
967 modules[1].(*Module).Properties.PreventInstall = true
968 modules[1].(*Module).Properties.HideFromMake = true
969 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700970 } else if t == hwasan {
971 if mctx.Device() {
972 // CFI and HWASAN are currently mutually exclusive so disable
973 // CFI if this is an HWASAN variant.
974 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
975 }
976
977 if c.static() {
978 if c.useVndk() {
979 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
980 hwasanStaticLibsMutex.Lock()
981 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
982 hwasanStaticLibsMutex.Unlock()
983 } else {
984 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
985 hwasanStaticLibsMutex.Lock()
986 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
987 hwasanStaticLibsMutex.Unlock()
988 }
989 } else {
990 if isSanitizerEnabled {
991 modules[0].(*Module).Properties.PreventInstall = true
992 modules[0].(*Module).Properties.HideFromMake = true
993 } else {
994 modules[1].(*Module).Properties.PreventInstall = true
995 modules[1].(*Module).Properties.HideFromMake = true
996 }
997 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700998 }
Colin Cross16b23492016-01-06 14:41:07 -0800999 }
1000 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +09001001 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001002 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +09001003 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -08001004 }
1005 }
1006}
Vishwath Mohane7128792017-11-17 11:08:10 -08001007
Colin Cross571cccf2019-02-04 11:22:08 -08001008var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1009
Vishwath Mohane7128792017-11-17 11:08:10 -08001010func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001011 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -08001012 return &[]string{}
1013 }).(*[]string)
1014}
1015
Colin Cross571cccf2019-02-04 11:22:08 -08001016var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1017
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001018func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001019 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001020 return &[]string{}
1021 }).(*[]string)
1022}
1023
Colin Cross571cccf2019-02-04 11:22:08 -08001024var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
1025
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001026func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001027 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001028 return &[]string{}
1029 }).(*[]string)
1030}
1031
Ivan Lozano30c5db22018-02-21 15:49:20 -08001032func enableMinimalRuntime(sanitize *sanitize) bool {
1033 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001034 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001035 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -08001036 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
1037 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
1038 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1039 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
1040 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
1041 return true
1042 }
1043 return false
1044}
1045
Vishwath Mohane7128792017-11-17 11:08:10 -08001046func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1047 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001048 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001049 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1050}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001051
1052func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1053 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1054 sort.Strings(*hwasanStaticLibs)
1055 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1056
1057 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1058 sort.Strings(*hwasanVendorStaticLibs)
1059 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1060}