blob: 468778249205533e97c9269fabef9033438d978b [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
Yi Kong20233a42019-08-21 01:38:40 -070034 asanCflags = []string{
35 "-fno-omit-frame-pointer",
36 "-fno-experimental-new-pass-manager",
37 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070038 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070039
Peter Collingbourne967511a2019-03-19 21:39:54 -070040 // TODO(pcc): Stop passing -hwasan-allow-ifunc here once it has been made
41 // the default.
42 hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
43 "-mllvm", "-hwasan-create-frame-descriptions=0",
Peter Collingbournee726ba52019-03-21 16:21:44 -070044 "-mllvm", "-hwasan-allow-ifunc",
Evgenii Stepanov1c69e832019-06-14 18:37:33 -070045 "-fsanitize-hwaddress-abi=platform",
46 "-fno-experimental-new-pass-manager"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070047
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000048 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070049 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070050 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
51 // used, but have no effect on assembly files
52 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070053 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070054 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070055 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
56 cfiStaticLibsMutex sync.Mutex
57 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070058
Evgenii Stepanov98f5b062018-11-29 15:12:51 -080059 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080060
Peter Collingbournebd19db02019-03-06 10:38:48 -080061 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070062 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov3c5a52a2018-12-18 17:02:44 -080063 hwasanGlobalOptions = []string{"heap_history_size=1023,stack_history_size=512"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070064)
65
Colin Cross16b23492016-01-06 14:41:07 -080066type sanitizerType int
67
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070068func boolPtr(v bool) *bool {
69 if v {
70 return &v
71 } else {
72 return nil
73 }
74}
75
Colin Cross16b23492016-01-06 14:41:07 -080076const (
77 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070078 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080079 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070080 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000081 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080082 scs
Mitch Phillipsbfeade62019-05-01 14:42:05 -070083 fuzzer
Colin Cross16b23492016-01-06 14:41:07 -080084)
85
Jiyong Park82226632019-02-01 10:50:50 +090086// Name of the sanitizer variation for this sanitizer type
87func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080088 switch t {
89 case asan:
90 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070091 case hwasan:
92 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080093 case tsan:
94 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070095 case intOverflow:
96 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000097 case cfi:
98 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080099 case scs:
100 return "scs"
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700101 case fuzzer:
102 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800103 default:
104 panic(fmt.Errorf("unknown sanitizerType %d", t))
105 }
106}
107
Jiyong Park82226632019-02-01 10:50:50 +0900108// This is the sanitizer names in SANITIZE_[TARGET|HOST]
109func (t sanitizerType) name() string {
110 switch t {
111 case asan:
112 return "address"
113 case hwasan:
114 return "hwaddress"
115 case tsan:
116 return "thread"
117 case intOverflow:
118 return "integer_overflow"
119 case cfi:
120 return "cfi"
121 case scs:
122 return "shadow-call-stack"
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700123 case fuzzer:
124 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900125 default:
126 panic(fmt.Errorf("unknown sanitizerType %d", t))
127 }
128}
129
Jiyong Park1d1119f2019-07-29 21:27:18 +0900130func (t sanitizerType) incompatibleWithCfi() bool {
131 return t == asan || t == fuzzer || t == hwasan
132}
133
Colin Cross16b23492016-01-06 14:41:07 -0800134type SanitizeProperties struct {
135 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
136 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800137 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800138
139 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700140 Address *bool `android:"arch_variant"`
141 Thread *bool `android:"arch_variant"`
142 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800143
144 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700145 Undefined *bool `android:"arch_variant"`
146 All_undefined *bool `android:"arch_variant"`
147 Misc_undefined []string `android:"arch_variant"`
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700148 Fuzzer *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700149 Safestack *bool `android:"arch_variant"`
150 Cfi *bool `android:"arch_variant"`
151 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700152 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800153 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800154
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700155 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
156 // Replaces abort() on error with a human-readable error message.
157 // Address and Thread sanitizers always run in diagnostic mode.
158 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700159 Undefined *bool `android:"arch_variant"`
160 Cfi *bool `android:"arch_variant"`
161 Integer_overflow *bool `android:"arch_variant"`
162 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800163 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700164 }
165
166 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800167 Recover []string
168
169 // value to pass to -fsanitize-blacklist
170 Blacklist *string
171 } `android:"arch_variant"`
172
Jiyong Park379de2f2018-12-19 02:47:14 +0900173 SanitizerEnabled bool `blueprint:"mutated"`
174 SanitizeDep bool `blueprint:"mutated"`
175 MinimalRuntimeDep bool `blueprint:"mutated"`
176 UbsanRuntimeDep bool `blueprint:"mutated"`
177 InSanitizerDir bool `blueprint:"mutated"`
178 Sanitizers []string `blueprint:"mutated"`
179 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800180}
181
182type sanitize struct {
183 Properties SanitizeProperties
184}
185
Vishwath Mohane7128792017-11-17 11:08:10 -0800186func init() {
187 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700188 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800189}
190
Colin Cross16b23492016-01-06 14:41:07 -0800191func (sanitize *sanitize) props() []interface{} {
192 return []interface{}{&sanitize.Properties}
193}
194
195func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700196 s := &sanitize.Properties.Sanitize
197
Colin Cross16b23492016-01-06 14:41:07 -0800198 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700199 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800200 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800201 }
202
Doug Hornc32c6b02019-01-17 14:44:05 -0800203 // Sanitizers do not work on Fuchsia yet.
204 if ctx.Fuchsia() {
205 s.Never = BoolPtr(true)
206 }
207
Colin Cross16b23492016-01-06 14:41:07 -0800208 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800209 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800210 return
211 }
212
Colin Cross16b23492016-01-06 14:41:07 -0800213 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700214 var globalSanitizersDiag []string
215
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700216 if ctx.Host() {
217 if !ctx.Windows() {
218 globalSanitizers = ctx.Config().SanitizeHost()
219 }
220 } else {
221 arches := ctx.Config().SanitizeDeviceArch()
222 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
223 globalSanitizers = ctx.Config().SanitizeDevice()
224 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800225 }
226 }
227
Colin Cross16b23492016-01-06 14:41:07 -0800228 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000229 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700230 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
231 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000232 }
Colin Cross16b23492016-01-06 14:41:07 -0800233
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700234 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
235 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000236 }
237
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700238 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
239 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000240 }
241
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700242 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
243 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000244 }
245
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700246 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
247 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700248 }
249
250 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
251 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000252 }
253
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700254 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800255 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700256 s.Cfi = boolPtr(true)
257 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700258 }
259
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700260 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700261 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700262 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700263 s.Integer_overflow = boolPtr(true)
264 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700265 }
266
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700267 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
268 s.Scudo = boolPtr(true)
269 }
270
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700271 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
272 s.Hwaddress = boolPtr(true)
273 }
274
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000275 if len(globalSanitizers) > 0 {
276 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
277 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700278
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700279 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700280 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700281 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700282 s.Diag.Integer_overflow = boolPtr(true)
283 }
284
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700285 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
286 s.Diag.Cfi == nil && Bool(s.Cfi) {
287 s.Diag.Cfi = boolPtr(true)
288 }
289
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700290 if len(globalSanitizersDiag) > 0 {
291 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
292 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700293 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700294
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700295 // Enable CFI for all components in the include paths (for Aarch64 only)
296 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000297 s.Cfi = boolPtr(true)
298 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
299 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700300 }
301 }
302
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800303 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800304 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800305 s.Cfi = nil
306 s.Diag.Cfi = nil
307 }
308
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800309 // Also disable CFI for arm32 until b/35157333 is fixed.
310 if ctx.Arch().ArchType == android.Arm {
311 s.Cfi = nil
312 s.Diag.Cfi = nil
313 }
314
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700315 // HWASan requires AArch64 hardware feature (top-byte-ignore).
316 if ctx.Arch().ArchType != android.Arm64 {
317 s.Hwaddress = nil
318 }
319
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800320 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800321 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800322 s.Scs = nil
323 }
324
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700325 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700326 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700327 s.Cfi = nil
328 s.Diag.Cfi = nil
329 }
330
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700331 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800332 if ctx.Host() {
333 s.Cfi = nil
334 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700335 s.Misc_undefined = nil
336 s.Undefined = nil
337 s.All_undefined = nil
338 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800339 }
340
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700341 // Also disable CFI for VNDK variants of components
342 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700343 s.Cfi = nil
344 s.Diag.Cfi = nil
345 }
346
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700347 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800348 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
349 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700350 s.Hwaddress = nil
351 }
352
Colin Cross3c344ef2016-07-18 15:44:56 -0700353 if ctx.staticBinary() {
354 s.Address = nil
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700355 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700356 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800357 }
358
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700359 if Bool(s.All_undefined) {
360 s.Undefined = nil
361 }
362
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700363 if !ctx.toolchain().Is64Bit() {
364 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700365 s.Thread = nil
366 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800367 // TODO(ccross): error for compile_multilib = "32"?
368 }
369
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800370 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 -0700371 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 -0800372 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700373 sanitize.Properties.SanitizerEnabled = true
374 }
375
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800376 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
377 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700378 s.Scudo = nil
379 }
380
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700381 if Bool(s.Hwaddress) {
382 s.Address = nil
383 s.Thread = nil
384 }
385
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700386 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
387 // mutually incompatible.
388 if Bool(s.Fuzzer) {
389 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800390 }
391}
392
393func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
394 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
395 return deps
396 }
397
398 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700399 if Bool(sanitize.Properties.Sanitize.Address) {
Christopher Ferris753d4a62019-05-09 13:27:02 -0700400 // Compiling asan and having libc_scudo in the same
401 // executable will cause the executable to crash.
402 // Remove libc_scudo since it is only used to override
403 // allocation functions which asan already overrides.
404 _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800405 }
406 }
407
408 return deps
409}
410
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800411func toDisableImplicitIntegerChange(flags []string) bool {
412 // Returns true if any flag is fsanitize*integer, and there is
413 // no explicit flag about sanitize=implicit-integer-sign-change.
414 for _, f := range flags {
415 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
416 return false
417 }
418 }
419 for _, f := range flags {
420 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
421 return true
422 }
423 }
424 return false
425}
426
Colin Cross16b23492016-01-06 14:41:07 -0800427func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700428 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
429 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800430
431 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
432 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700433 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800434 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700435 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800436 return flags
437 }
438
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700439 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700440 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800441 // Frame pointer based unwinder in ASan requires ARM frame setup.
442 // TODO: put in flags?
443 flags.RequiredInstructionSet = "arm"
444 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700445 flags.CFlags = append(flags.CFlags, asanCflags...)
446 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800447
Colin Cross16b23492016-01-06 14:41:07 -0800448 if ctx.Host() {
449 // -nodefaultlibs (provided with libc++) prevents the driver from linking
450 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800451 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
452 } else {
Yi Kongda069082019-08-22 00:46:36 +0000453 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900454 if ctx.bootstrap() {
455 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
456 } else {
457 flags.DynamicLinker = "/system/bin/linker_asan"
458 }
Colin Cross16b23492016-01-06 14:41:07 -0800459 if flags.Toolchain.Is64Bit() {
460 flags.DynamicLinker += "64"
461 }
462 }
Colin Cross16b23492016-01-06 14:41:07 -0800463 }
464
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700465 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
466 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700467 }
468
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700469 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
470 flags.CFlags = append(flags.CFlags, "-fsanitize=fuzzer-no-link")
471 flags.LdFlags = append(flags.LdFlags, "-fsanitize=fuzzer-no-link")
472
473 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
474 _, flags.LdFlags = removeFromList("-flto", flags.LdFlags)
Mitch Phillips34b493f2019-08-02 16:57:55 -0700475 _, flags.CFlags = removeFromList("-flto", flags.CFlags)
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700476 flags.LdFlags = append(flags.LdFlags, "-fno-lto")
Mitch Phillips34b493f2019-08-02 16:57:55 -0700477 flags.CFlags = append(flags.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700478
479 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
480 _, flags.CFlags = removeFromList("-fexperimental-new-pass-manager", flags.CFlags)
481 flags.CFlags = append(flags.CFlags, "-fno-experimental-new-pass-manager")
Colin Cross16b23492016-01-06 14:41:07 -0800482 }
483
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700484 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800485 if ctx.Arch().ArchType == android.Arm {
486 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
487 // to do this on a function basis, so force Thumb on the entire module.
488 flags.RequiredInstructionSet = "thumb"
489 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000490
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700491 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700492 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000493 // Only append the default visibility flag if -fvisibility has not already been set
494 // to hidden.
495 if !inList("-fvisibility=hidden", flags.CFlags) {
496 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
497 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700498 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000499
500 if ctx.staticBinary() {
501 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
502 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
503 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700504 }
505
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700506 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700507 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700508 }
509
Jiyong Park379de2f2018-12-19 02:47:14 +0900510 if len(sanitize.Properties.Sanitizers) > 0 {
511 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800512
Colin Cross16b23492016-01-06 14:41:07 -0800513 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700514 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800515 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800516 // Host sanitizers only link symbols in the final executable, so
517 // there will always be undefined symbols in intermediate libraries.
518 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700519 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800520 } else {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800521 if enableMinimalRuntime(sanitize) {
522 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
523 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700524 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800525 }
Colin Cross16b23492016-01-06 14:41:07 -0800526 }
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700527
528 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
529 // When fuzzing, we wish to crash with diagnostics on any bug.
530 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
531 } else if ctx.Host() {
532 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
533 } else {
534 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
535 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800536 // http://b/119329758, Android core does not boot up with this sanitizer yet.
537 if toDisableImplicitIntegerChange(flags.CFlags) {
538 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
539 }
Colin Cross16b23492016-01-06 14:41:07 -0800540 }
541
Jiyong Park379de2f2018-12-19 02:47:14 +0900542 if len(sanitize.Properties.DiagSanitizers) > 0 {
543 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700544 }
545 // FIXME: enable RTTI if diag + (cfi or vptr)
546
Andreas Gampe97071162017-05-08 13:15:23 -0700547 if sanitize.Properties.Sanitize.Recover != nil {
548 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
549 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
550 }
551
Ivan Lozano7929bba2018-12-12 09:36:31 -0800552 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
553 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
554 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
555 }
556
Colin Cross635c3b02016-05-18 15:37:25 -0700557 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800558 if blacklist.Valid() {
559 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
560 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
561 }
562
563 return flags
564}
565
Colin Cross8ff9ef42017-05-08 13:44:11 -0700566func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900567 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
568 // both the sanitized and non-sanitized variants to make without a name conflict.
569 if ret.Class == "STATIC_LIBRARIES" || ret.Class == "HEADER_LIBRARIES" {
570 if Bool(sanitize.Properties.Sanitize.Cfi) {
571 ret.SubName += ".cfi"
572 }
573 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
574 ret.SubName += ".hwasan"
575 }
576 if Bool(sanitize.Properties.Sanitize.Scs) {
577 ret.SubName += ".scs"
578 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800579 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700580}
581
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700582func (sanitize *sanitize) inSanitizerDir() bool {
583 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700584}
585
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000586func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000587 switch t {
588 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000589 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700590 case hwasan:
591 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000592 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000593 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000594 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000595 return sanitize.Properties.Sanitize.Integer_overflow
596 case cfi:
597 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800598 case scs:
599 return sanitize.Properties.Sanitize.Scs
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700600 case fuzzer:
601 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000602 default:
603 panic(fmt.Errorf("unknown sanitizerType %d", t))
604 }
605}
606
Dan Albert7d1eecf2018-01-19 12:30:45 -0800607func (sanitize *sanitize) isUnsanitizedVariant() bool {
608 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700609 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800610 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800611 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700612 !sanitize.isSanitizerEnabled(scs) &&
613 !sanitize.isSanitizerEnabled(fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800614}
615
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700616func (sanitize *sanitize) isVariantOnProductionDevice() bool {
617 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700618 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700619 !sanitize.isSanitizerEnabled(tsan) &&
620 !sanitize.isSanitizerEnabled(fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700621}
622
Colin Cross16b23492016-01-06 14:41:07 -0800623func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
624 switch t {
625 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700626 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700627 case hwasan:
628 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800629 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700630 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700631 case intOverflow:
632 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000633 case cfi:
634 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800635 case scs:
636 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700637 case fuzzer:
638 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800639 default:
640 panic(fmt.Errorf("unknown sanitizerType %d", t))
641 }
642 if b {
643 sanitize.Properties.SanitizerEnabled = true
644 }
645}
646
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000647// Check if the sanitizer is explicitly disabled (as opposed to nil by
648// virtue of not being set).
649func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
650 if sanitize == nil {
651 return false
652 }
653
654 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
655 return sanitizerVal != nil && *sanitizerVal == false
656}
657
658// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
659// because enabling a sanitizer either directly (via the blueprint) or
660// indirectly (via a mutator) sets the bool ptr to true, and you can't
661// distinguish between the cases. It isn't needed though - both cases can be
662// treated identically.
663func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
664 if sanitize == nil {
665 return false
666 }
667
668 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
669 return sanitizerVal != nil && *sanitizerVal == true
670}
671
Colin Cross6b753602018-06-21 13:03:07 -0700672func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
673 t, ok := tag.(dependencyTag)
674 return ok && t.library || t == reuseObjTag
675}
676
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700677// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700678func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
679 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000680 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700681 mctx.WalkDeps(func(child, parent android.Module) bool {
682 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
683 return false
684 }
685 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800686 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000687 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800688 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700689 if d.static() {
690 d.sanitize.Properties.SanitizeDep = true
691 }
692 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000693 d.sanitize.Properties.SanitizeDep = true
694 }
Colin Cross16b23492016-01-06 14:41:07 -0800695 }
Colin Cross6b753602018-06-21 13:03:07 -0700696 return true
Colin Cross16b23492016-01-06 14:41:07 -0800697 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900698 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
699 // If an APEX module includes a lib which is enabled for a sanitizer T, then
700 // the APEX module is also enabled for the same sanitizer type.
701 mctx.VisitDirectDeps(func(child android.Module) {
702 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
703 sanitizeable.EnableSanitizer(t.name())
704 }
705 })
Colin Cross16b23492016-01-06 14:41:07 -0800706 }
707 }
708}
709
Ivan Lozano30c5db22018-02-21 15:49:20 -0800710// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700711func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
712 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
713 mctx.WalkDeps(func(child, parent android.Module) bool {
714 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
715 return false
716 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800717
Colin Cross0b908332019-06-19 23:00:20 -0700718 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700719 if enableMinimalRuntime(d.sanitize) {
720 // If a static dependency is built with the minimal runtime,
721 // make sure we include the ubsan minimal runtime.
722 c.sanitize.Properties.MinimalRuntimeDep = true
723 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
724 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
725 // If a static dependency runs with full ubsan diagnostics,
726 // make sure we include the ubsan runtime.
727 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800728 }
Colin Cross0b908332019-06-19 23:00:20 -0700729
730 if c.sanitize.Properties.MinimalRuntimeDep &&
731 c.sanitize.Properties.UbsanRuntimeDep {
732 // both flags that this mutator might set are true, so don't bother recursing
733 return false
734 }
735
736 return true
737 } else {
738 return false
Colin Cross6b753602018-06-21 13:03:07 -0700739 }
Colin Cross6b753602018-06-21 13:03:07 -0700740 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800741 }
742}
743
Jiyong Park379de2f2018-12-19 02:47:14 +0900744// Add the dependency to the runtime library for each of the sanitizer variants
745func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900746 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000747 if !c.Enabled() {
748 return
749 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900750 var sanitizers []string
751 var diagSanitizers []string
752
753 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
754 sanitizers = append(sanitizers, "undefined")
755 } else {
756 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
757 sanitizers = append(sanitizers,
758 "bool",
759 "integer-divide-by-zero",
760 "return",
761 "returns-nonnull-attribute",
762 "shift-exponent",
763 "unreachable",
764 "vla-bound",
765 // TODO(danalbert): The following checks currently have compiler performance issues.
766 //"alignment",
767 //"bounds",
768 //"enum",
769 //"float-cast-overflow",
770 //"float-divide-by-zero",
771 //"nonnull-attribute",
772 //"null",
773 //"shift-base",
774 //"signed-integer-overflow",
775 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
776 // https://llvm.org/PR19302
777 // http://reviews.llvm.org/D6974
778 // "object-size",
779 )
780 }
781 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
782 }
783
784 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
785 diagSanitizers = append(diagSanitizers, "undefined")
786 }
787
788 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
789
790 if Bool(c.sanitize.Properties.Sanitize.Address) {
791 sanitizers = append(sanitizers, "address")
792 diagSanitizers = append(diagSanitizers, "address")
793 }
794
795 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
796 sanitizers = append(sanitizers, "hwaddress")
797 }
798
799 if Bool(c.sanitize.Properties.Sanitize.Thread) {
800 sanitizers = append(sanitizers, "thread")
801 }
802
803 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
804 sanitizers = append(sanitizers, "safe-stack")
805 }
806
807 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
808 sanitizers = append(sanitizers, "cfi")
809
810 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
811 diagSanitizers = append(diagSanitizers, "cfi")
812 }
813 }
814
815 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
816 sanitizers = append(sanitizers, "unsigned-integer-overflow")
817 sanitizers = append(sanitizers, "signed-integer-overflow")
818 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
819 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
820 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
821 }
822 }
823
824 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
825 sanitizers = append(sanitizers, "scudo")
826 }
827
828 if Bool(c.sanitize.Properties.Sanitize.Scs) {
829 sanitizers = append(sanitizers, "shadow-call-stack")
830 }
831
Mitch Phillipsbfeade62019-05-01 14:42:05 -0700832 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
833 sanitizers = append(sanitizers, "fuzzer-no-link")
834 }
835
Jiyong Park379de2f2018-12-19 02:47:14 +0900836 // Save the list of sanitizers. These will be used again when generating
837 // the build rules (for Cflags, etc.)
838 c.sanitize.Properties.Sanitizers = sanitizers
839 c.sanitize.Properties.DiagSanitizers = diagSanitizers
840
841 // Determine the runtime library required
842 runtimeLibrary := ""
843 toolchain := c.toolchain(mctx)
844 if Bool(c.sanitize.Properties.Sanitize.Address) {
845 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
846 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
847 if c.staticBinary() {
848 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
849 } else {
850 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
851 }
852 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
853 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
854 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
855 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
856 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
857 } else {
858 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
859 }
860 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
861 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
862 }
863
864 if mctx.Device() && runtimeLibrary != "" {
Inseob Kim9516ee92019-05-09 10:56:13 +0900865 if inList(runtimeLibrary, *llndkLibraries(mctx.Config())) && !c.static() && c.useVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900866 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
867 }
868
869 // Adding dependency to the runtime library. We are using *FarVariation*
870 // because the runtime libraries themselves are not mutated by sanitizer
871 // mutators and thus don't have sanitizer variants whereas this module
872 // has been already mutated.
873 //
874 // Note that by adding dependency with {static|shared}DepTag, the lib is
875 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
876 if c.staticBinary() {
877 // static executable gets static runtime libs
878 mctx.AddFarVariationDependencies([]blueprint.Variation{
879 {Mutator: "link", Variation: "static"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900880 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900881 {Mutator: "arch", Variation: mctx.Target().String()},
882 }, staticDepTag, runtimeLibrary)
Jiyong Park1d1119f2019-07-29 21:27:18 +0900883 } else if !c.static() && !c.header() {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900884 // dynamic executable and shared libs get shared runtime libs
Jiyong Park379de2f2018-12-19 02:47:14 +0900885 mctx.AddFarVariationDependencies([]blueprint.Variation{
886 {Mutator: "link", Variation: "shared"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900887 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900888 {Mutator: "arch", Variation: mctx.Target().String()},
Jiyong Park64a44f22019-01-18 14:37:08 +0900889 }, earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900890 }
891 // static lib does not have dependency to the runtime library. The
892 // dependency will be added to the executables or shared libs using
893 // the static lib.
894 }
895 }
896}
897
898type Sanitizeable interface {
899 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900900 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900901 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900902}
903
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000904// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700905func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
906 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000907 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000908 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900909 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700910 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000911 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000912 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
Jiyong Park1d1119f2019-07-29 21:27:18 +0900913 if mctx.Device() && t.incompatibleWithCfi() {
914 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
915 // are incompatible with cfi
916 c.sanitize.SetSanitizer(cfi, false)
917 }
918 if c.static() || c.header() || t == asan || t == fuzzer {
919 // Static and header libs are split into non-sanitized and sanitized variants.
920 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
921 // libs because a library sanitized for asan/fuzzer can't be linked from a library
922 // that isn't sanitized for asan/fuzzer.
923 //
924 // Note for defaultVariation: since we don't split for shared libs but for static/header
925 // libs, it is possible for the sanitized variant of a static/header lib to depend
926 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
927 // error when the module is split. defaultVariation is the name of the variation that
928 // will be used when such a dangling dependency occurs during the split of the current
929 // module. By setting it to the name of the sanitized variation, the dangling dependency
930 // is redirected to the sanitized variant of the dependent module.
931 defaultVariation := t.variationName()
932 mctx.SetDefaultDependencyVariation(&defaultVariation)
933 modules := mctx.CreateVariations("", t.variationName())
934 modules[0].(*Module).sanitize.SetSanitizer(t, false)
935 modules[1].(*Module).sanitize.SetSanitizer(t, true)
936 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
937 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000938
Jiyong Park1d1119f2019-07-29 21:27:18 +0900939 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
940 // to Make, because the sanitized version has a different suffix in name.
941 // For other types of sanitizers, suppress the variation that is disabled.
942 if t != cfi && t != scs && t != hwasan {
943 if isSanitizerEnabled {
944 modules[0].(*Module).Properties.PreventInstall = true
945 modules[0].(*Module).Properties.HideFromMake = true
946 } else {
947 modules[1].(*Module).Properties.PreventInstall = true
948 modules[1].(*Module).Properties.HideFromMake = true
949 }
950 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000951
Jiyong Park1d1119f2019-07-29 21:27:18 +0900952 // Export the static lib name to make
Vishwath Mohane7128792017-11-17 11:08:10 -0800953 if c.static() {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900954 if t == cfi {
955 appendStringSync(c.Name(), cfiStaticLibs(mctx.Config()), &cfiStaticLibsMutex)
956 } else if t == hwasan {
957 if c.useVndk() {
958 appendStringSync(c.Name(), hwasanVendorStaticLibs(mctx.Config()),
959 &hwasanStaticLibsMutex)
Vishwath Mohane7128792017-11-17 11:08:10 -0800960 } else {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900961 appendStringSync(c.Name(), hwasanStaticLibs(mctx.Config()),
962 &hwasanStaticLibsMutex)
Vishwath Mohane7128792017-11-17 11:08:10 -0800963 }
Jiyong Park1d1119f2019-07-29 21:27:18 +0900964 }
965 }
966 } else {
967 // Shared libs are not split. Only the sanitized variant is created.
968 modules := mctx.CreateVariations(t.variationName())
969 modules[0].(*Module).sanitize.SetSanitizer(t, true)
970 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800971
Jiyong Park1d1119f2019-07-29 21:27:18 +0900972 // locate the asan libraries under /data/asan
973 if mctx.Device() && t == asan && isSanitizerEnabled {
974 modules[0].(*Module).sanitize.Properties.InSanitizerDir = true
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700975 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700976 }
Colin Cross16b23492016-01-06 14:41:07 -0800977 }
978 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +0900979 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900980 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +0900981 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -0800982 }
983 }
984}
Vishwath Mohane7128792017-11-17 11:08:10 -0800985
Colin Cross571cccf2019-02-04 11:22:08 -0800986var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
987
Vishwath Mohane7128792017-11-17 11:08:10 -0800988func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800989 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -0800990 return &[]string{}
991 }).(*[]string)
992}
993
Colin Cross571cccf2019-02-04 11:22:08 -0800994var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
995
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700996func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800997 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700998 return &[]string{}
999 }).(*[]string)
1000}
1001
Colin Cross571cccf2019-02-04 11:22:08 -08001002var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
1003
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001004func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001005 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001006 return &[]string{}
1007 }).(*[]string)
1008}
1009
Jiyong Park1d1119f2019-07-29 21:27:18 +09001010func appendStringSync(item string, list *[]string, mutex *sync.Mutex) {
1011 mutex.Lock()
1012 *list = append(*list, item)
1013 mutex.Unlock()
1014}
1015
Ivan Lozano30c5db22018-02-21 15:49:20 -08001016func enableMinimalRuntime(sanitize *sanitize) bool {
1017 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001018 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillipsbfeade62019-05-01 14:42:05 -07001019 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -08001020 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
1021 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
1022 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1023 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
1024 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
1025 return true
1026 }
1027 return false
1028}
1029
Vishwath Mohane7128792017-11-17 11:08:10 -08001030func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1031 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001032 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001033 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1034}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001035
1036func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1037 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1038 sort.Strings(*hwasanStaticLibs)
1039 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1040
1041 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1042 sort.Strings(*hwasanVendorStaticLibs)
1043 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1044}