blob: c1b055afee854ced18caa9b5204c9025aed63c26 [file] [log] [blame]
Colin Cross16b23492016-01-06 14:41:07 -08001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
18 "fmt"
Jeff Gaston72765392017-11-28 16:37:53 -080019 "sort"
Colin Cross16b23492016-01-06 14:41:07 -080020 "strings"
Vishwath Mohane7128792017-11-17 11:08:10 -080021 "sync"
Colin Cross16b23492016-01-06 14:41:07 -080022
Colin Cross6b753602018-06-21 13:03:07 -070023 "github.com/google/blueprint"
24
Colin Cross635c3b02016-05-18 15:37:25 -070025 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070026 "android/soong/cc/config"
Colin Cross16b23492016-01-06 14:41:07 -080027)
28
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070029var (
30 // Any C flags added by sanitizer which libTooling tools may not
31 // understand also need to be added to ClangLibToolingUnknownCflags in
32 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080033
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070034 asanCflags = []string{"-fno-omit-frame-pointer"}
35 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
36 asanLibs = []string{"libasan"}
37
Peter Collingbourne967511a2019-03-19 21:39:54 -070038 // TODO(pcc): Stop passing -hwasan-allow-ifunc here once it has been made
39 // the default.
40 hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
41 "-mllvm", "-hwasan-create-frame-descriptions=0",
Peter Collingbournee726ba52019-03-21 16:21:44 -070042 "-mllvm", "-hwasan-allow-ifunc",
43 "-fsanitize-hwaddress-abi=platform"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070044
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000045 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070046 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070047 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
48 // used, but have no effect on assembly files
49 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070050 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070051 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070052 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
53 cfiStaticLibsMutex sync.Mutex
54 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070055
Evgenii Stepanov98f5b062018-11-29 15:12:51 -080056 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080057
Peter Collingbournebd19db02019-03-06 10:38:48 -080058 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070059 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070060 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
61 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070062)
63
Colin Cross16b23492016-01-06 14:41:07 -080064type sanitizerType int
65
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070066func boolPtr(v bool) *bool {
67 if v {
68 return &v
69 } else {
70 return nil
71 }
72}
73
Colin Cross16b23492016-01-06 14:41:07 -080074const (
75 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070076 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080077 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070078 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000079 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080080 scs
Colin Cross16b23492016-01-06 14:41:07 -080081)
82
Jiyong Park82226632019-02-01 10:50:50 +090083// Name of the sanitizer variation for this sanitizer type
84func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080085 switch t {
86 case asan:
87 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070088 case hwasan:
89 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080090 case tsan:
91 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070092 case intOverflow:
93 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000094 case cfi:
95 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080096 case scs:
97 return "scs"
Colin Cross16b23492016-01-06 14:41:07 -080098 default:
99 panic(fmt.Errorf("unknown sanitizerType %d", t))
100 }
101}
102
Jiyong Park82226632019-02-01 10:50:50 +0900103// This is the sanitizer names in SANITIZE_[TARGET|HOST]
104func (t sanitizerType) name() string {
105 switch t {
106 case asan:
107 return "address"
108 case hwasan:
109 return "hwaddress"
110 case tsan:
111 return "thread"
112 case intOverflow:
113 return "integer_overflow"
114 case cfi:
115 return "cfi"
116 case scs:
117 return "shadow-call-stack"
118 default:
119 panic(fmt.Errorf("unknown sanitizerType %d", t))
120 }
121}
122
Jiyong Parkd5bd6d32019-07-29 21:27:18 +0900123func (t sanitizerType) incompatibleWithCfi() bool {
124 return t == asan || t == hwasan
125}
126
Colin Cross16b23492016-01-06 14:41:07 -0800127type SanitizeProperties struct {
128 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
129 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800130 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800131
132 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700133 Address *bool `android:"arch_variant"`
134 Thread *bool `android:"arch_variant"`
135 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800136
137 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700138 Undefined *bool `android:"arch_variant"`
139 All_undefined *bool `android:"arch_variant"`
140 Misc_undefined []string `android:"arch_variant"`
141 Coverage *bool `android:"arch_variant"`
142 Safestack *bool `android:"arch_variant"`
143 Cfi *bool `android:"arch_variant"`
144 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700145 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800146 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800147
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700148 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
149 // Replaces abort() on error with a human-readable error message.
150 // Address and Thread sanitizers always run in diagnostic mode.
151 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700152 Undefined *bool `android:"arch_variant"`
153 Cfi *bool `android:"arch_variant"`
154 Integer_overflow *bool `android:"arch_variant"`
155 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800156 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700157 }
158
159 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800160 Recover []string
161
162 // value to pass to -fsanitize-blacklist
163 Blacklist *string
164 } `android:"arch_variant"`
165
Jiyong Park379de2f2018-12-19 02:47:14 +0900166 SanitizerEnabled bool `blueprint:"mutated"`
167 SanitizeDep bool `blueprint:"mutated"`
168 MinimalRuntimeDep bool `blueprint:"mutated"`
169 UbsanRuntimeDep bool `blueprint:"mutated"`
170 InSanitizerDir bool `blueprint:"mutated"`
171 Sanitizers []string `blueprint:"mutated"`
172 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800173}
174
175type sanitize struct {
176 Properties SanitizeProperties
177}
178
Vishwath Mohane7128792017-11-17 11:08:10 -0800179func init() {
180 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700181 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800182}
183
Colin Cross16b23492016-01-06 14:41:07 -0800184func (sanitize *sanitize) props() []interface{} {
185 return []interface{}{&sanitize.Properties}
186}
187
188func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700189 s := &sanitize.Properties.Sanitize
190
Colin Cross16b23492016-01-06 14:41:07 -0800191 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700192 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800193 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800194 }
195
Doug Hornc32c6b02019-01-17 14:44:05 -0800196 // Sanitizers do not work on Fuchsia yet.
197 if ctx.Fuchsia() {
198 s.Never = BoolPtr(true)
199 }
200
Colin Cross16b23492016-01-06 14:41:07 -0800201 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800202 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800203 return
204 }
205
Colin Cross16b23492016-01-06 14:41:07 -0800206 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700207 var globalSanitizersDiag []string
208
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700209 if ctx.Host() {
210 if !ctx.Windows() {
211 globalSanitizers = ctx.Config().SanitizeHost()
212 }
213 } else {
214 arches := ctx.Config().SanitizeDeviceArch()
215 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
216 globalSanitizers = ctx.Config().SanitizeDevice()
217 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800218 }
219 }
220
Colin Cross16b23492016-01-06 14:41:07 -0800221 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000222 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700223 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
224 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000225 }
Colin Cross16b23492016-01-06 14:41:07 -0800226
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700227 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
228 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000229 }
230
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800231 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
232 if s.Address == nil {
233 s.Address = boolPtr(true)
234 } else if *s.Address == false {
235 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
236 // disables address, then disable coverage as well.
237 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
238 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000239 }
240
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700241 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
242 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000243 }
244
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700245 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
246 s.Coverage = boolPtr(true)
247 }
248
249 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
250 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000251 }
252
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700253 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800254 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700255 s.Cfi = boolPtr(true)
256 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700257 }
258
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700259 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700260 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700261 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700262 s.Integer_overflow = boolPtr(true)
263 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700264 }
265
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700266 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
267 s.Scudo = boolPtr(true)
268 }
269
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700270 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
271 s.Hwaddress = boolPtr(true)
272 }
273
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000274 if len(globalSanitizers) > 0 {
275 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
276 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700277
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700278 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700279 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700280 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700281 s.Diag.Integer_overflow = boolPtr(true)
282 }
283
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700284 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
285 s.Diag.Cfi == nil && Bool(s.Cfi) {
286 s.Diag.Cfi = boolPtr(true)
287 }
288
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700289 if len(globalSanitizersDiag) > 0 {
290 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
291 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700292 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700293
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700294 // Enable CFI for all components in the include paths (for Aarch64 only)
295 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000296 s.Cfi = boolPtr(true)
297 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
298 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700299 }
300 }
301
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800302 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800303 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800304 s.Cfi = nil
305 s.Diag.Cfi = nil
306 }
307
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800308 // Also disable CFI for arm32 until b/35157333 is fixed.
309 if ctx.Arch().ArchType == android.Arm {
310 s.Cfi = nil
311 s.Diag.Cfi = nil
312 }
313
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700314 // HWASan requires AArch64 hardware feature (top-byte-ignore).
315 if ctx.Arch().ArchType != android.Arm64 {
316 s.Hwaddress = nil
317 }
318
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800319 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800320 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800321 s.Scs = nil
322 }
323
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700324 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700325 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700326 s.Cfi = nil
327 s.Diag.Cfi = nil
328 }
329
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700330 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800331 if ctx.Host() {
332 s.Cfi = nil
333 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700334 s.Misc_undefined = nil
335 s.Undefined = nil
336 s.All_undefined = nil
337 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800338 }
339
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700340 // Also disable CFI for VNDK variants of components
341 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700342 s.Cfi = nil
343 s.Diag.Cfi = nil
344 }
345
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700346 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800347 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
348 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700349 s.Hwaddress = nil
350 }
351
Colin Cross3c344ef2016-07-18 15:44:56 -0700352 if ctx.staticBinary() {
353 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700354 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700355 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800356 }
357
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700358 if Bool(s.All_undefined) {
359 s.Undefined = nil
360 }
361
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700362 if !ctx.toolchain().Is64Bit() {
363 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700364 s.Thread = nil
365 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800366 // TODO(ccross): error for compile_multilib = "32"?
367 }
368
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800369 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700370 Bool(s.Coverage) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800371 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700372 sanitize.Properties.SanitizerEnabled = true
373 }
374
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800375 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
376 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700377 s.Scudo = nil
378 }
379
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700380 if Bool(s.Hwaddress) {
381 s.Address = nil
382 s.Thread = nil
383 }
384
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700385 if Bool(s.Coverage) {
386 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800387 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
388 }
389 }
390}
391
392func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
393 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
394 return deps
395 }
396
397 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700398 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700399 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Christopher Ferris6e2b6aa2019-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 {
453 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
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700469 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400470 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800471 }
472
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700473 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800474 if ctx.Arch().ArchType == android.Arm {
475 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
476 // to do this on a function basis, so force Thumb on the entire module.
477 flags.RequiredInstructionSet = "thumb"
478 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000479
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700480 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700481 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000482 // Only append the default visibility flag if -fvisibility has not already been set
483 // to hidden.
484 if !inList("-fvisibility=hidden", flags.CFlags) {
485 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
486 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700487 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000488
489 if ctx.staticBinary() {
490 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
491 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
492 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700493 }
494
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700495 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700496 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700497 }
498
Jiyong Park379de2f2018-12-19 02:47:14 +0900499 if len(sanitize.Properties.Sanitizers) > 0 {
500 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800501
Colin Cross16b23492016-01-06 14:41:07 -0800502 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700503 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800504 if ctx.Host() {
505 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
506 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800507 // Host sanitizers only link symbols in the final executable, so
508 // there will always be undefined symbols in intermediate libraries.
509 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800510 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700511 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800512
513 if enableMinimalRuntime(sanitize) {
514 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
515 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700516 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800517 }
Colin Cross16b23492016-01-06 14:41:07 -0800518 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800519 // http://b/119329758, Android core does not boot up with this sanitizer yet.
520 if toDisableImplicitIntegerChange(flags.CFlags) {
521 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
522 }
Colin Cross16b23492016-01-06 14:41:07 -0800523 }
524
Jiyong Park379de2f2018-12-19 02:47:14 +0900525 if len(sanitize.Properties.DiagSanitizers) > 0 {
526 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700527 }
528 // FIXME: enable RTTI if diag + (cfi or vptr)
529
Andreas Gampe97071162017-05-08 13:15:23 -0700530 if sanitize.Properties.Sanitize.Recover != nil {
531 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
532 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
533 }
534
Ivan Lozano7929bba2018-12-12 09:36:31 -0800535 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
536 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
537 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
538 }
539
Colin Cross635c3b02016-05-18 15:37:25 -0700540 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800541 if blacklist.Valid() {
542 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
543 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
544 }
545
546 return flags
547}
548
Colin Cross8ff9ef42017-05-08 13:44:11 -0700549func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Jiyong Parkd5bd6d32019-07-29 21:27:18 +0900550 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
551 // both the sanitized and non-sanitized variants to make without a name conflict.
552 if ret.Class == "STATIC_LIBRARIES" || ret.Class == "HEADER_LIBRARIES" {
553 if Bool(sanitize.Properties.Sanitize.Cfi) {
554 ret.SubName += ".cfi"
555 }
556 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
557 ret.SubName += ".hwasan"
558 }
559 if Bool(sanitize.Properties.Sanitize.Scs) {
560 ret.SubName += ".scs"
561 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800562 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700563}
564
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700565func (sanitize *sanitize) inSanitizerDir() bool {
566 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700567}
568
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000569func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000570 switch t {
571 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000572 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700573 case hwasan:
574 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000575 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000576 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000577 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000578 return sanitize.Properties.Sanitize.Integer_overflow
579 case cfi:
580 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800581 case scs:
582 return sanitize.Properties.Sanitize.Scs
Vishwath Mohan95229302017-08-11 00:53:16 +0000583 default:
584 panic(fmt.Errorf("unknown sanitizerType %d", t))
585 }
586}
587
Dan Albert7d1eecf2018-01-19 12:30:45 -0800588func (sanitize *sanitize) isUnsanitizedVariant() bool {
589 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700590 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800591 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800592 !sanitize.isSanitizerEnabled(cfi) &&
593 !sanitize.isSanitizerEnabled(scs)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800594}
595
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700596func (sanitize *sanitize) isVariantOnProductionDevice() bool {
597 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700598 !sanitize.isSanitizerEnabled(hwasan) &&
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700599 !sanitize.isSanitizerEnabled(tsan)
600}
601
Colin Cross16b23492016-01-06 14:41:07 -0800602func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
603 switch t {
604 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700605 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700606 if !b {
607 sanitize.Properties.Sanitize.Coverage = nil
608 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700609 case hwasan:
610 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800611 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700612 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700613 case intOverflow:
614 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000615 case cfi:
616 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800617 case scs:
618 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800619 default:
620 panic(fmt.Errorf("unknown sanitizerType %d", t))
621 }
622 if b {
623 sanitize.Properties.SanitizerEnabled = true
624 }
625}
626
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000627// Check if the sanitizer is explicitly disabled (as opposed to nil by
628// virtue of not being set).
629func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
630 if sanitize == nil {
631 return false
632 }
633
634 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
635 return sanitizerVal != nil && *sanitizerVal == false
636}
637
638// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
639// because enabling a sanitizer either directly (via the blueprint) or
640// indirectly (via a mutator) sets the bool ptr to true, and you can't
641// distinguish between the cases. It isn't needed though - both cases can be
642// treated identically.
643func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
644 if sanitize == nil {
645 return false
646 }
647
648 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
649 return sanitizerVal != nil && *sanitizerVal == true
650}
651
Colin Cross6b753602018-06-21 13:03:07 -0700652func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
653 t, ok := tag.(dependencyTag)
654 return ok && t.library || t == reuseObjTag
655}
656
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700657// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700658func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
659 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000660 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700661 mctx.WalkDeps(func(child, parent android.Module) bool {
662 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
663 return false
664 }
665 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800666 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000667 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800668 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700669 if d.static() {
670 d.sanitize.Properties.SanitizeDep = true
671 }
672 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000673 d.sanitize.Properties.SanitizeDep = true
674 }
Colin Cross16b23492016-01-06 14:41:07 -0800675 }
Colin Cross6b753602018-06-21 13:03:07 -0700676 return true
Colin Cross16b23492016-01-06 14:41:07 -0800677 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900678 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
679 // If an APEX module includes a lib which is enabled for a sanitizer T, then
680 // the APEX module is also enabled for the same sanitizer type.
681 mctx.VisitDirectDeps(func(child android.Module) {
682 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
683 sanitizeable.EnableSanitizer(t.name())
684 }
685 })
Colin Cross16b23492016-01-06 14:41:07 -0800686 }
687 }
688}
689
Ivan Lozano30c5db22018-02-21 15:49:20 -0800690// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700691func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
692 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
693 mctx.WalkDeps(func(child, parent android.Module) bool {
694 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
695 return false
696 }
697 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800698
Colin Cross6b753602018-06-21 13:03:07 -0700699 if enableMinimalRuntime(d.sanitize) {
700 // If a static dependency is built with the minimal runtime,
701 // make sure we include the ubsan minimal runtime.
702 c.sanitize.Properties.MinimalRuntimeDep = true
703 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
704 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
705 // If a static dependency runs with full ubsan diagnostics,
706 // make sure we include the ubsan runtime.
707 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800708 }
Colin Cross6b753602018-06-21 13:03:07 -0700709 }
710 return true
711 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800712 }
713}
714
Jiyong Park379de2f2018-12-19 02:47:14 +0900715// Add the dependency to the runtime library for each of the sanitizer variants
716func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900717 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000718 if !c.Enabled() {
719 return
720 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900721 var sanitizers []string
722 var diagSanitizers []string
723
724 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
725 sanitizers = append(sanitizers, "undefined")
726 } else {
727 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
728 sanitizers = append(sanitizers,
729 "bool",
730 "integer-divide-by-zero",
731 "return",
732 "returns-nonnull-attribute",
733 "shift-exponent",
734 "unreachable",
735 "vla-bound",
736 // TODO(danalbert): The following checks currently have compiler performance issues.
737 //"alignment",
738 //"bounds",
739 //"enum",
740 //"float-cast-overflow",
741 //"float-divide-by-zero",
742 //"nonnull-attribute",
743 //"null",
744 //"shift-base",
745 //"signed-integer-overflow",
746 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
747 // https://llvm.org/PR19302
748 // http://reviews.llvm.org/D6974
749 // "object-size",
750 )
751 }
752 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
753 }
754
755 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
756 diagSanitizers = append(diagSanitizers, "undefined")
757 }
758
759 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
760
761 if Bool(c.sanitize.Properties.Sanitize.Address) {
762 sanitizers = append(sanitizers, "address")
763 diagSanitizers = append(diagSanitizers, "address")
764 }
765
766 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
767 sanitizers = append(sanitizers, "hwaddress")
768 }
769
770 if Bool(c.sanitize.Properties.Sanitize.Thread) {
771 sanitizers = append(sanitizers, "thread")
772 }
773
774 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
775 sanitizers = append(sanitizers, "safe-stack")
776 }
777
778 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
779 sanitizers = append(sanitizers, "cfi")
780
781 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
782 diagSanitizers = append(diagSanitizers, "cfi")
783 }
784 }
785
786 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
787 sanitizers = append(sanitizers, "unsigned-integer-overflow")
788 sanitizers = append(sanitizers, "signed-integer-overflow")
789 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
790 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
791 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
792 }
793 }
794
795 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
796 sanitizers = append(sanitizers, "scudo")
797 }
798
799 if Bool(c.sanitize.Properties.Sanitize.Scs) {
800 sanitizers = append(sanitizers, "shadow-call-stack")
801 }
802
803 // Save the list of sanitizers. These will be used again when generating
804 // the build rules (for Cflags, etc.)
805 c.sanitize.Properties.Sanitizers = sanitizers
806 c.sanitize.Properties.DiagSanitizers = diagSanitizers
807
808 // Determine the runtime library required
809 runtimeLibrary := ""
810 toolchain := c.toolchain(mctx)
811 if Bool(c.sanitize.Properties.Sanitize.Address) {
812 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
813 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
814 if c.staticBinary() {
815 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
816 } else {
817 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
818 }
819 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
820 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
821 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
822 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
823 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
824 } else {
825 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
826 }
827 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
828 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
829 }
830
831 if mctx.Device() && runtimeLibrary != "" {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900832 if inList(runtimeLibrary, llndkLibraries) && !c.static() && c.useVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900833 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
834 }
835
836 // Adding dependency to the runtime library. We are using *FarVariation*
837 // because the runtime libraries themselves are not mutated by sanitizer
838 // mutators and thus don't have sanitizer variants whereas this module
839 // has been already mutated.
840 //
841 // Note that by adding dependency with {static|shared}DepTag, the lib is
842 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
843 if c.staticBinary() {
844 // static executable gets static runtime libs
845 mctx.AddFarVariationDependencies([]blueprint.Variation{
846 {Mutator: "link", Variation: "static"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900847 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900848 {Mutator: "arch", Variation: mctx.Target().String()},
849 }, staticDepTag, runtimeLibrary)
Jiyong Parkd5bd6d32019-07-29 21:27:18 +0900850 } else if !c.static() && !c.header() {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900851 // dynamic executable and shared libs get shared runtime libs
Jiyong Park379de2f2018-12-19 02:47:14 +0900852 mctx.AddFarVariationDependencies([]blueprint.Variation{
853 {Mutator: "link", Variation: "shared"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900854 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900855 {Mutator: "arch", Variation: mctx.Target().String()},
Jiyong Park64a44f22019-01-18 14:37:08 +0900856 }, earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900857 }
858 // static lib does not have dependency to the runtime library. The
859 // dependency will be added to the executables or shared libs using
860 // the static lib.
861 }
862 }
863}
864
865type Sanitizeable interface {
866 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900867 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900868 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900869}
870
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000871// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700872func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
873 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000874 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000875 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900876 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700877 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000878 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000879 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
Jiyong Parkd5bd6d32019-07-29 21:27:18 +0900880 if mctx.Device() && t.incompatibleWithCfi() {
881 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
882 // are incompatible with cfi
883 c.sanitize.SetSanitizer(cfi, false)
884 }
885 if c.static() || c.header() || t == asan {
886 // Static and header libs are split into non-sanitized and sanitized variants.
887 // Shared libs are not split. However, for asan, we split even for shared
888 // libs because a library sanitized for asan can't be linked from a library
889 // that isn't sanitized for asan.
890 //
891 // Note for defaultVariation: since we don't split for shared libs but for static/header
892 // libs, it is possible for the sanitized variant of a static/header lib to depend
893 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
894 // error when the module is split. defaultVariation is the name of the variation that
895 // will be used when such a dangling dependency occurs during the split of the current
896 // module. By setting it to the name of the sanitized variation, the dangling dependency
897 // is redirected to the sanitized variant of the dependent module.
898 defaultVariation := t.variationName()
899 mctx.SetDefaultDependencyVariation(&defaultVariation)
900 modules := mctx.CreateVariations("", t.variationName())
901 modules[0].(*Module).sanitize.SetSanitizer(t, false)
902 modules[1].(*Module).sanitize.SetSanitizer(t, true)
903 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
904 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000905
Jiyong Parkd5bd6d32019-07-29 21:27:18 +0900906 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
907 // to Make, because the sanitized version has a different suffix in name.
908 // For other types of sanitizers, suppress the variation that is disabled.
909 if t != cfi && t != scs && t != hwasan {
910 if isSanitizerEnabled {
911 modules[0].(*Module).Properties.PreventInstall = true
912 modules[0].(*Module).Properties.HideFromMake = true
913 } else {
914 modules[1].(*Module).Properties.PreventInstall = true
915 modules[1].(*Module).Properties.HideFromMake = true
916 }
917 }
918 // Export the static lib name to make
Vishwath Mohane7128792017-11-17 11:08:10 -0800919 if c.static() {
Jiyong Parkd5bd6d32019-07-29 21:27:18 +0900920 if t == cfi {
921 appendStringSync(c.Name(), cfiStaticLibs(mctx.Config()), &cfiStaticLibsMutex)
922 } else if t == hwasan {
923 if c.useVndk() {
924 appendStringSync(c.Name(), hwasanVendorStaticLibs(mctx.Config()),
925 &hwasanStaticLibsMutex)
Vishwath Mohane7128792017-11-17 11:08:10 -0800926 } else {
Jiyong Parkd5bd6d32019-07-29 21:27:18 +0900927 appendStringSync(c.Name(), hwasanStaticLibs(mctx.Config()),
928 &hwasanStaticLibsMutex)
Vishwath Mohane7128792017-11-17 11:08:10 -0800929 }
Jiyong Parkd5bd6d32019-07-29 21:27:18 +0900930 }
931 }
932 } else {
933 // Shared libs are not split. Only the sanitized variant is created.
934 modules := mctx.CreateVariations(t.variationName())
935 modules[0].(*Module).sanitize.SetSanitizer(t, true)
936 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800937
Jiyong Parkd5bd6d32019-07-29 21:27:18 +0900938 // locate the asan libraries under /data/asan
939 if mctx.Device() && t == asan && isSanitizerEnabled {
940 modules[0].(*Module).sanitize.Properties.InSanitizerDir = true
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700941 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700942 }
Colin Cross16b23492016-01-06 14:41:07 -0800943 }
944 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +0900945 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900946 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +0900947 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -0800948 }
949 }
950}
Vishwath Mohane7128792017-11-17 11:08:10 -0800951
Colin Cross571cccf2019-02-04 11:22:08 -0800952var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
953
Vishwath Mohane7128792017-11-17 11:08:10 -0800954func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800955 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -0800956 return &[]string{}
957 }).(*[]string)
958}
959
Colin Cross571cccf2019-02-04 11:22:08 -0800960var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
961
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700962func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800963 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700964 return &[]string{}
965 }).(*[]string)
966}
967
Colin Cross571cccf2019-02-04 11:22:08 -0800968var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
969
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700970func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800971 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700972 return &[]string{}
973 }).(*[]string)
974}
975
Jiyong Parkd5bd6d32019-07-29 21:27:18 +0900976func appendStringSync(item string, list *[]string, mutex *sync.Mutex) {
977 mutex.Lock()
978 *list = append(*list, item)
979 mutex.Unlock()
980}
981
Ivan Lozano30c5db22018-02-21 15:49:20 -0800982func enableMinimalRuntime(sanitize *sanitize) bool {
983 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700984 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -0800985 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
986 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
987 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
988 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
989 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
990 return true
991 }
992 return false
993}
994
Vishwath Mohane7128792017-11-17 11:08:10 -0800995func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
996 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -0800997 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -0800998 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
999}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001000
1001func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1002 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1003 sort.Strings(*hwasanStaticLibs)
1004 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1005
1006 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1007 sort.Strings(*hwasanVendorStaticLibs)
1008 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1009}