blob: 89ef7e6154db1f5c32c5ed381c36f46b0050d3d6 [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
Colin Cross16b23492016-01-06 14:41:07 -0800123type SanitizeProperties struct {
124 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
125 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800126 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800127
128 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700129 Address *bool `android:"arch_variant"`
130 Thread *bool `android:"arch_variant"`
131 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800132
133 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700134 Undefined *bool `android:"arch_variant"`
135 All_undefined *bool `android:"arch_variant"`
136 Misc_undefined []string `android:"arch_variant"`
137 Coverage *bool `android:"arch_variant"`
138 Safestack *bool `android:"arch_variant"`
139 Cfi *bool `android:"arch_variant"`
140 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700141 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800142 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800143
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700144 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
145 // Replaces abort() on error with a human-readable error message.
146 // Address and Thread sanitizers always run in diagnostic mode.
147 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700148 Undefined *bool `android:"arch_variant"`
149 Cfi *bool `android:"arch_variant"`
150 Integer_overflow *bool `android:"arch_variant"`
151 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800152 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700153 }
154
155 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800156 Recover []string
157
158 // value to pass to -fsanitize-blacklist
159 Blacklist *string
160 } `android:"arch_variant"`
161
Jiyong Park379de2f2018-12-19 02:47:14 +0900162 SanitizerEnabled bool `blueprint:"mutated"`
163 SanitizeDep bool `blueprint:"mutated"`
164 MinimalRuntimeDep bool `blueprint:"mutated"`
165 UbsanRuntimeDep bool `blueprint:"mutated"`
166 InSanitizerDir bool `blueprint:"mutated"`
167 Sanitizers []string `blueprint:"mutated"`
168 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800169}
170
171type sanitize struct {
172 Properties SanitizeProperties
173}
174
Vishwath Mohane7128792017-11-17 11:08:10 -0800175func init() {
176 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700177 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800178}
179
Colin Cross16b23492016-01-06 14:41:07 -0800180func (sanitize *sanitize) props() []interface{} {
181 return []interface{}{&sanitize.Properties}
182}
183
184func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700185 s := &sanitize.Properties.Sanitize
186
Colin Cross16b23492016-01-06 14:41:07 -0800187 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700188 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800189 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800190 }
191
Doug Hornc32c6b02019-01-17 14:44:05 -0800192 // Sanitizers do not work on Fuchsia yet.
193 if ctx.Fuchsia() {
194 s.Never = BoolPtr(true)
195 }
196
Colin Cross16b23492016-01-06 14:41:07 -0800197 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800198 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800199 return
200 }
201
Colin Cross16b23492016-01-06 14:41:07 -0800202 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700203 var globalSanitizersDiag []string
204
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700205 if ctx.Host() {
206 if !ctx.Windows() {
207 globalSanitizers = ctx.Config().SanitizeHost()
208 }
209 } else {
210 arches := ctx.Config().SanitizeDeviceArch()
211 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
212 globalSanitizers = ctx.Config().SanitizeDevice()
213 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800214 }
215 }
216
Colin Cross16b23492016-01-06 14:41:07 -0800217 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000218 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700219 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
220 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000221 }
Colin Cross16b23492016-01-06 14:41:07 -0800222
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700223 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
224 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000225 }
226
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800227 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
228 if s.Address == nil {
229 s.Address = boolPtr(true)
230 } else if *s.Address == false {
231 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
232 // disables address, then disable coverage as well.
233 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
234 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000235 }
236
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700237 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
238 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000239 }
240
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700241 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
242 s.Coverage = boolPtr(true)
243 }
244
245 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
246 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000247 }
248
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700249 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800250 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700251 s.Cfi = boolPtr(true)
252 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700253 }
254
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700255 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700256 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700257 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700258 s.Integer_overflow = boolPtr(true)
259 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700260 }
261
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700262 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
263 s.Scudo = boolPtr(true)
264 }
265
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700266 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
267 s.Hwaddress = boolPtr(true)
268 }
269
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000270 if len(globalSanitizers) > 0 {
271 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
272 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700273
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700274 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700275 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700276 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700277 s.Diag.Integer_overflow = boolPtr(true)
278 }
279
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700280 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
281 s.Diag.Cfi == nil && Bool(s.Cfi) {
282 s.Diag.Cfi = boolPtr(true)
283 }
284
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700285 if len(globalSanitizersDiag) > 0 {
286 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
287 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700288 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700289
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700290 // Enable CFI for all components in the include paths (for Aarch64 only)
291 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000292 s.Cfi = boolPtr(true)
293 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
294 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700295 }
296 }
297
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800298 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800299 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800300 s.Cfi = nil
301 s.Diag.Cfi = nil
302 }
303
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800304 // Also disable CFI for arm32 until b/35157333 is fixed.
305 if ctx.Arch().ArchType == android.Arm {
306 s.Cfi = nil
307 s.Diag.Cfi = nil
308 }
309
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700310 // HWASan requires AArch64 hardware feature (top-byte-ignore).
311 if ctx.Arch().ArchType != android.Arm64 {
312 s.Hwaddress = nil
313 }
314
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800315 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800316 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800317 s.Scs = nil
318 }
319
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700320 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700321 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700322 s.Cfi = nil
323 s.Diag.Cfi = nil
324 }
325
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700326 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800327 if ctx.Host() {
328 s.Cfi = nil
329 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700330 s.Misc_undefined = nil
331 s.Undefined = nil
332 s.All_undefined = nil
333 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800334 }
335
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700336 // Also disable CFI for VNDK variants of components
337 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700338 s.Cfi = nil
339 s.Diag.Cfi = nil
340 }
341
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700342 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800343 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
344 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700345 s.Hwaddress = nil
346 }
347
Colin Cross3c344ef2016-07-18 15:44:56 -0700348 if ctx.staticBinary() {
349 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700350 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700351 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800352 }
353
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700354 if Bool(s.All_undefined) {
355 s.Undefined = nil
356 }
357
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700358 if !ctx.toolchain().Is64Bit() {
359 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700360 s.Thread = nil
361 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800362 // TODO(ccross): error for compile_multilib = "32"?
363 }
364
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800365 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700366 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 -0800367 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700368 sanitize.Properties.SanitizerEnabled = true
369 }
370
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800371 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
372 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700373 s.Scudo = nil
374 }
375
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700376 if Bool(s.Hwaddress) {
377 s.Address = nil
378 s.Thread = nil
379 }
380
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700381 if Bool(s.Coverage) {
382 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800383 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
384 }
385 }
386}
387
388func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
389 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
390 return deps
391 }
392
393 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700394 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700395 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800396 }
397 }
398
399 return deps
400}
401
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800402func toDisableImplicitIntegerChange(flags []string) bool {
403 // Returns true if any flag is fsanitize*integer, and there is
404 // no explicit flag about sanitize=implicit-integer-sign-change.
405 for _, f := range flags {
406 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
407 return false
408 }
409 }
410 for _, f := range flags {
411 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
412 return true
413 }
414 }
415 return false
416}
417
Colin Cross16b23492016-01-06 14:41:07 -0800418func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700419 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
420 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800421
422 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
423 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700424 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800425 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700426 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800427 return flags
428 }
429
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700430 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700431 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800432 // Frame pointer based unwinder in ASan requires ARM frame setup.
433 // TODO: put in flags?
434 flags.RequiredInstructionSet = "arm"
435 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700436 flags.CFlags = append(flags.CFlags, asanCflags...)
437 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800438
Colin Cross16b23492016-01-06 14:41:07 -0800439 if ctx.Host() {
440 // -nodefaultlibs (provided with libc++) prevents the driver from linking
441 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800442 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
443 } else {
444 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900445 if ctx.bootstrap() {
446 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
447 } else {
448 flags.DynamicLinker = "/system/bin/linker_asan"
449 }
Colin Cross16b23492016-01-06 14:41:07 -0800450 if flags.Toolchain.Is64Bit() {
451 flags.DynamicLinker += "64"
452 }
453 }
Colin Cross16b23492016-01-06 14:41:07 -0800454 }
455
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700456 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
457 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700458 }
459
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700460 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400461 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800462 }
463
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700464 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800465 if ctx.Arch().ArchType == android.Arm {
466 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
467 // to do this on a function basis, so force Thumb on the entire module.
468 flags.RequiredInstructionSet = "thumb"
469 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000470
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700471 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700472 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000473 // Only append the default visibility flag if -fvisibility has not already been set
474 // to hidden.
475 if !inList("-fvisibility=hidden", flags.CFlags) {
476 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
477 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700478 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000479
480 if ctx.staticBinary() {
481 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
482 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
483 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700484 }
485
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700486 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700487 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700488 }
489
Jiyong Park379de2f2018-12-19 02:47:14 +0900490 if len(sanitize.Properties.Sanitizers) > 0 {
491 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800492
Colin Cross16b23492016-01-06 14:41:07 -0800493 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700494 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800495 if ctx.Host() {
496 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
497 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800498 // Host sanitizers only link symbols in the final executable, so
499 // there will always be undefined symbols in intermediate libraries.
500 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800501 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700502 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800503
504 if enableMinimalRuntime(sanitize) {
505 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
506 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700507 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800508 }
Colin Cross16b23492016-01-06 14:41:07 -0800509 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800510 // http://b/119329758, Android core does not boot up with this sanitizer yet.
511 if toDisableImplicitIntegerChange(flags.CFlags) {
512 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
513 }
Colin Cross16b23492016-01-06 14:41:07 -0800514 }
515
Jiyong Park379de2f2018-12-19 02:47:14 +0900516 if len(sanitize.Properties.DiagSanitizers) > 0 {
517 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700518 }
519 // FIXME: enable RTTI if diag + (cfi or vptr)
520
Andreas Gampe97071162017-05-08 13:15:23 -0700521 if sanitize.Properties.Sanitize.Recover != nil {
522 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
523 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
524 }
525
Ivan Lozano7929bba2018-12-12 09:36:31 -0800526 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
527 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
528 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
529 }
530
Colin Cross635c3b02016-05-18 15:37:25 -0700531 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800532 if blacklist.Valid() {
533 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
534 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
535 }
536
537 return flags
538}
539
Colin Cross8ff9ef42017-05-08 13:44:11 -0700540func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Vishwath Mohane7128792017-11-17 11:08:10 -0800541 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
542 // name conflict.
543 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
544 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000545 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700546 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
547 ret.SubName += ".hwasan"
548 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800549 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
550 ret.SubName += ".scs"
551 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700552}
553
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700554func (sanitize *sanitize) inSanitizerDir() bool {
555 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700556}
557
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000558func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000559 switch t {
560 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000561 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700562 case hwasan:
563 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000564 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000565 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000566 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000567 return sanitize.Properties.Sanitize.Integer_overflow
568 case cfi:
569 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800570 case scs:
571 return sanitize.Properties.Sanitize.Scs
Vishwath Mohan95229302017-08-11 00:53:16 +0000572 default:
573 panic(fmt.Errorf("unknown sanitizerType %d", t))
574 }
575}
576
Dan Albert7d1eecf2018-01-19 12:30:45 -0800577func (sanitize *sanitize) isUnsanitizedVariant() bool {
578 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700579 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800580 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800581 !sanitize.isSanitizerEnabled(cfi) &&
582 !sanitize.isSanitizerEnabled(scs)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800583}
584
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700585func (sanitize *sanitize) isVariantOnProductionDevice() bool {
586 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700587 !sanitize.isSanitizerEnabled(hwasan) &&
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700588 !sanitize.isSanitizerEnabled(tsan)
589}
590
Colin Cross16b23492016-01-06 14:41:07 -0800591func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
592 switch t {
593 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700594 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700595 if !b {
596 sanitize.Properties.Sanitize.Coverage = nil
597 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700598 case hwasan:
599 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800600 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700601 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700602 case intOverflow:
603 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000604 case cfi:
605 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800606 case scs:
607 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800608 default:
609 panic(fmt.Errorf("unknown sanitizerType %d", t))
610 }
611 if b {
612 sanitize.Properties.SanitizerEnabled = true
613 }
614}
615
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000616// Check if the sanitizer is explicitly disabled (as opposed to nil by
617// virtue of not being set).
618func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
619 if sanitize == nil {
620 return false
621 }
622
623 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
624 return sanitizerVal != nil && *sanitizerVal == false
625}
626
627// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
628// because enabling a sanitizer either directly (via the blueprint) or
629// indirectly (via a mutator) sets the bool ptr to true, and you can't
630// distinguish between the cases. It isn't needed though - both cases can be
631// treated identically.
632func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
633 if sanitize == nil {
634 return false
635 }
636
637 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
638 return sanitizerVal != nil && *sanitizerVal == true
639}
640
Colin Cross6b753602018-06-21 13:03:07 -0700641func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
642 t, ok := tag.(dependencyTag)
643 return ok && t.library || t == reuseObjTag
644}
645
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700646// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700647func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
648 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000649 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700650 mctx.WalkDeps(func(child, parent android.Module) bool {
651 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
652 return false
653 }
654 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800655 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000656 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800657 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700658 if d.static() {
659 d.sanitize.Properties.SanitizeDep = true
660 }
661 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000662 d.sanitize.Properties.SanitizeDep = true
663 }
Colin Cross16b23492016-01-06 14:41:07 -0800664 }
Colin Cross6b753602018-06-21 13:03:07 -0700665 return true
Colin Cross16b23492016-01-06 14:41:07 -0800666 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900667 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
668 // If an APEX module includes a lib which is enabled for a sanitizer T, then
669 // the APEX module is also enabled for the same sanitizer type.
670 mctx.VisitDirectDeps(func(child android.Module) {
671 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
672 sanitizeable.EnableSanitizer(t.name())
673 }
674 })
Colin Cross16b23492016-01-06 14:41:07 -0800675 }
676 }
677}
678
Ivan Lozano30c5db22018-02-21 15:49:20 -0800679// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700680func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
681 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
682 mctx.WalkDeps(func(child, parent android.Module) bool {
683 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
684 return false
685 }
686 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800687
Colin Cross6b753602018-06-21 13:03:07 -0700688 if enableMinimalRuntime(d.sanitize) {
689 // If a static dependency is built with the minimal runtime,
690 // make sure we include the ubsan minimal runtime.
691 c.sanitize.Properties.MinimalRuntimeDep = true
692 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
693 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
694 // If a static dependency runs with full ubsan diagnostics,
695 // make sure we include the ubsan runtime.
696 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800697 }
Colin Cross6b753602018-06-21 13:03:07 -0700698 }
699 return true
700 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800701 }
702}
703
Jiyong Park379de2f2018-12-19 02:47:14 +0900704// Add the dependency to the runtime library for each of the sanitizer variants
705func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900706 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000707 if !c.Enabled() {
708 return
709 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900710 var sanitizers []string
711 var diagSanitizers []string
712
713 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
714 sanitizers = append(sanitizers, "undefined")
715 } else {
716 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
717 sanitizers = append(sanitizers,
718 "bool",
719 "integer-divide-by-zero",
720 "return",
721 "returns-nonnull-attribute",
722 "shift-exponent",
723 "unreachable",
724 "vla-bound",
725 // TODO(danalbert): The following checks currently have compiler performance issues.
726 //"alignment",
727 //"bounds",
728 //"enum",
729 //"float-cast-overflow",
730 //"float-divide-by-zero",
731 //"nonnull-attribute",
732 //"null",
733 //"shift-base",
734 //"signed-integer-overflow",
735 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
736 // https://llvm.org/PR19302
737 // http://reviews.llvm.org/D6974
738 // "object-size",
739 )
740 }
741 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
742 }
743
744 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
745 diagSanitizers = append(diagSanitizers, "undefined")
746 }
747
748 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
749
750 if Bool(c.sanitize.Properties.Sanitize.Address) {
751 sanitizers = append(sanitizers, "address")
752 diagSanitizers = append(diagSanitizers, "address")
753 }
754
755 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
756 sanitizers = append(sanitizers, "hwaddress")
757 }
758
759 if Bool(c.sanitize.Properties.Sanitize.Thread) {
760 sanitizers = append(sanitizers, "thread")
761 }
762
763 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
764 sanitizers = append(sanitizers, "safe-stack")
765 }
766
767 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
768 sanitizers = append(sanitizers, "cfi")
769
770 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
771 diagSanitizers = append(diagSanitizers, "cfi")
772 }
773 }
774
775 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
776 sanitizers = append(sanitizers, "unsigned-integer-overflow")
777 sanitizers = append(sanitizers, "signed-integer-overflow")
778 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
779 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
780 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
781 }
782 }
783
784 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
785 sanitizers = append(sanitizers, "scudo")
786 }
787
788 if Bool(c.sanitize.Properties.Sanitize.Scs) {
789 sanitizers = append(sanitizers, "shadow-call-stack")
790 }
791
792 // Save the list of sanitizers. These will be used again when generating
793 // the build rules (for Cflags, etc.)
794 c.sanitize.Properties.Sanitizers = sanitizers
795 c.sanitize.Properties.DiagSanitizers = diagSanitizers
796
797 // Determine the runtime library required
798 runtimeLibrary := ""
799 toolchain := c.toolchain(mctx)
800 if Bool(c.sanitize.Properties.Sanitize.Address) {
801 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
802 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
803 if c.staticBinary() {
804 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
805 } else {
806 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
807 }
808 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
809 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
810 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
811 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
812 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
813 } else {
814 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
815 }
816 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
817 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
818 }
819
820 if mctx.Device() && runtimeLibrary != "" {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900821 if inList(runtimeLibrary, llndkLibraries) && !c.static() && c.useVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900822 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
823 }
824
825 // Adding dependency to the runtime library. We are using *FarVariation*
826 // because the runtime libraries themselves are not mutated by sanitizer
827 // mutators and thus don't have sanitizer variants whereas this module
828 // has been already mutated.
829 //
830 // Note that by adding dependency with {static|shared}DepTag, the lib is
831 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
832 if c.staticBinary() {
833 // static executable gets static runtime libs
834 mctx.AddFarVariationDependencies([]blueprint.Variation{
835 {Mutator: "link", Variation: "static"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900836 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900837 {Mutator: "arch", Variation: mctx.Target().String()},
838 }, staticDepTag, runtimeLibrary)
839 } else if !c.static() {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900840 // dynamic executable and shared libs get shared runtime libs
Jiyong Park379de2f2018-12-19 02:47:14 +0900841 mctx.AddFarVariationDependencies([]blueprint.Variation{
842 {Mutator: "link", Variation: "shared"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900843 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900844 {Mutator: "arch", Variation: mctx.Target().String()},
Jiyong Park64a44f22019-01-18 14:37:08 +0900845 }, earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900846 }
847 // static lib does not have dependency to the runtime library. The
848 // dependency will be added to the executables or shared libs using
849 // the static lib.
850 }
851 }
852}
853
854type Sanitizeable interface {
855 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900856 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900857 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900858}
859
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000860// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700861func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
862 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000863 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000864 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900865 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700866 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000867 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
868 // Save original sanitizer status before we assign values to variant
869 // 0 as that overwrites the original.
870 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
871
Jiyong Park82226632019-02-01 10:50:50 +0900872 modules := mctx.CreateVariations("", t.variationName())
Colin Crossb0f28952016-09-19 16:46:53 -0700873 modules[0].(*Module).sanitize.SetSanitizer(t, false)
874 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000875
Colin Crossb0f28952016-09-19 16:46:53 -0700876 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
877 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800878
879 // We don't need both variants active for anything but CFI-enabled
880 // target static libraries, so suppress the appropriate variant in
881 // all other cases.
882 if t == cfi {
883 if c.static() {
884 if !mctx.Device() {
885 if isSanitizerEnabled {
886 modules[0].(*Module).Properties.PreventInstall = true
887 modules[0].(*Module).Properties.HideFromMake = true
888 } else {
889 modules[1].(*Module).Properties.PreventInstall = true
890 modules[1].(*Module).Properties.HideFromMake = true
891 }
892 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800893 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800894
895 cfiStaticLibsMutex.Lock()
896 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
897 cfiStaticLibsMutex.Unlock()
898 }
899 } else {
900 modules[0].(*Module).Properties.PreventInstall = true
901 modules[0].(*Module).Properties.HideFromMake = true
902 }
903 } else if t == asan {
904 if mctx.Device() {
905 // CFI and ASAN are currently mutually exclusive so disable
906 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000907 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
908 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
909 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700910 if isSanitizerEnabled {
911 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800912 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700913 } else {
914 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800915 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700916 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800917 } else if t == scs {
918 // We don't currently link any static libraries built with make into
919 // libraries built with SCS, so we don't need logic for propagating
920 // SCSness of dependencies into make.
921 if !c.static() {
922 if isSanitizerEnabled {
923 modules[0].(*Module).Properties.PreventInstall = true
924 modules[0].(*Module).Properties.HideFromMake = true
925 } else {
926 modules[1].(*Module).Properties.PreventInstall = true
927 modules[1].(*Module).Properties.HideFromMake = true
928 }
929 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700930 } else if t == hwasan {
931 if mctx.Device() {
932 // CFI and HWASAN are currently mutually exclusive so disable
933 // CFI if this is an HWASAN variant.
934 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
935 }
936
937 if c.static() {
938 if c.useVndk() {
939 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
940 hwasanStaticLibsMutex.Lock()
941 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
942 hwasanStaticLibsMutex.Unlock()
943 } else {
944 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
945 hwasanStaticLibsMutex.Lock()
946 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
947 hwasanStaticLibsMutex.Unlock()
948 }
949 } else {
950 if isSanitizerEnabled {
951 modules[0].(*Module).Properties.PreventInstall = true
952 modules[0].(*Module).Properties.HideFromMake = true
953 } else {
954 modules[1].(*Module).Properties.PreventInstall = true
955 modules[1].(*Module).Properties.HideFromMake = true
956 }
957 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700958 }
Colin Cross16b23492016-01-06 14:41:07 -0800959 }
960 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +0900961 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900962 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +0900963 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -0800964 }
965 }
966}
Vishwath Mohane7128792017-11-17 11:08:10 -0800967
Colin Cross571cccf2019-02-04 11:22:08 -0800968var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
969
Vishwath Mohane7128792017-11-17 11:08:10 -0800970func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800971 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -0800972 return &[]string{}
973 }).(*[]string)
974}
975
Colin Cross571cccf2019-02-04 11:22:08 -0800976var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
977
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700978func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800979 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700980 return &[]string{}
981 }).(*[]string)
982}
983
Colin Cross571cccf2019-02-04 11:22:08 -0800984var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
985
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700986func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800987 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700988 return &[]string{}
989 }).(*[]string)
990}
991
Ivan Lozano30c5db22018-02-21 15:49:20 -0800992func enableMinimalRuntime(sanitize *sanitize) bool {
993 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700994 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -0800995 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
996 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
997 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
998 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
999 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
1000 return true
1001 }
1002 return false
1003}
1004
Vishwath Mohane7128792017-11-17 11:08:10 -08001005func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1006 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001007 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001008 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1009}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001010
1011func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1012 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1013 sort.Strings(*hwasanStaticLibs)
1014 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1015
1016 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1017 sort.Strings(*hwasanVendorStaticLibs)
1018 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1019}