blob: b7f12c9ddf17ed57bbd7306e1c19763713a9b7aa [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...)
Christopher Ferris753d4a62019-05-09 13:27:02 -0700396 // Compiling asan and having libc_scudo in the same
397 // executable will cause the executable to crash.
398 // Remove libc_scudo since it is only used to override
399 // allocation functions which asan already overrides.
400 _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800401 }
402 }
403
404 return deps
405}
406
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800407func toDisableImplicitIntegerChange(flags []string) bool {
408 // Returns true if any flag is fsanitize*integer, and there is
409 // no explicit flag about sanitize=implicit-integer-sign-change.
410 for _, f := range flags {
411 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
412 return false
413 }
414 }
415 for _, f := range flags {
416 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
417 return true
418 }
419 }
420 return false
421}
422
Colin Cross16b23492016-01-06 14:41:07 -0800423func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700424 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
425 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800426
427 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
428 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700429 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800430 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700431 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800432 return flags
433 }
434
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700435 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700436 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800437 // Frame pointer based unwinder in ASan requires ARM frame setup.
438 // TODO: put in flags?
439 flags.RequiredInstructionSet = "arm"
440 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700441 flags.CFlags = append(flags.CFlags, asanCflags...)
442 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800443
Colin Cross16b23492016-01-06 14:41:07 -0800444 if ctx.Host() {
445 // -nodefaultlibs (provided with libc++) prevents the driver from linking
446 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800447 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
448 } else {
449 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900450 if ctx.bootstrap() {
451 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
452 } else {
453 flags.DynamicLinker = "/system/bin/linker_asan"
454 }
Colin Cross16b23492016-01-06 14:41:07 -0800455 if flags.Toolchain.Is64Bit() {
456 flags.DynamicLinker += "64"
457 }
458 }
Colin Cross16b23492016-01-06 14:41:07 -0800459 }
460
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700461 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
462 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700463 }
464
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700465 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400466 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800467 }
468
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700469 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800470 if ctx.Arch().ArchType == android.Arm {
471 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
472 // to do this on a function basis, so force Thumb on the entire module.
473 flags.RequiredInstructionSet = "thumb"
474 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000475
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700476 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700477 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000478 // Only append the default visibility flag if -fvisibility has not already been set
479 // to hidden.
480 if !inList("-fvisibility=hidden", flags.CFlags) {
481 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
482 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700483 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000484
485 if ctx.staticBinary() {
486 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
487 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
488 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700489 }
490
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700491 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700492 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700493 }
494
Jiyong Park379de2f2018-12-19 02:47:14 +0900495 if len(sanitize.Properties.Sanitizers) > 0 {
496 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800497
Colin Cross16b23492016-01-06 14:41:07 -0800498 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700499 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800500 if ctx.Host() {
501 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
502 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800503 // Host sanitizers only link symbols in the final executable, so
504 // there will always be undefined symbols in intermediate libraries.
505 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800506 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700507 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800508
509 if enableMinimalRuntime(sanitize) {
510 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
511 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700512 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800513 }
Colin Cross16b23492016-01-06 14:41:07 -0800514 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800515 // http://b/119329758, Android core does not boot up with this sanitizer yet.
516 if toDisableImplicitIntegerChange(flags.CFlags) {
517 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
518 }
Colin Cross16b23492016-01-06 14:41:07 -0800519 }
520
Jiyong Park379de2f2018-12-19 02:47:14 +0900521 if len(sanitize.Properties.DiagSanitizers) > 0 {
522 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700523 }
524 // FIXME: enable RTTI if diag + (cfi or vptr)
525
Andreas Gampe97071162017-05-08 13:15:23 -0700526 if sanitize.Properties.Sanitize.Recover != nil {
527 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
528 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
529 }
530
Ivan Lozano7929bba2018-12-12 09:36:31 -0800531 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
532 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
533 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
534 }
535
Colin Cross635c3b02016-05-18 15:37:25 -0700536 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800537 if blacklist.Valid() {
538 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
539 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
540 }
541
542 return flags
543}
544
Colin Cross8ff9ef42017-05-08 13:44:11 -0700545func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Vishwath Mohane7128792017-11-17 11:08:10 -0800546 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
547 // name conflict.
548 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
549 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000550 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700551 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
552 ret.SubName += ".hwasan"
553 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800554 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
555 ret.SubName += ".scs"
556 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700557}
558
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700559func (sanitize *sanitize) inSanitizerDir() bool {
560 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700561}
562
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000563func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000564 switch t {
565 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000566 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700567 case hwasan:
568 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000569 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000570 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000571 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000572 return sanitize.Properties.Sanitize.Integer_overflow
573 case cfi:
574 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800575 case scs:
576 return sanitize.Properties.Sanitize.Scs
Vishwath Mohan95229302017-08-11 00:53:16 +0000577 default:
578 panic(fmt.Errorf("unknown sanitizerType %d", t))
579 }
580}
581
Dan Albert7d1eecf2018-01-19 12:30:45 -0800582func (sanitize *sanitize) isUnsanitizedVariant() bool {
583 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700584 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800585 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800586 !sanitize.isSanitizerEnabled(cfi) &&
587 !sanitize.isSanitizerEnabled(scs)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800588}
589
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700590func (sanitize *sanitize) isVariantOnProductionDevice() bool {
591 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700592 !sanitize.isSanitizerEnabled(hwasan) &&
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700593 !sanitize.isSanitizerEnabled(tsan)
594}
595
Colin Cross16b23492016-01-06 14:41:07 -0800596func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
597 switch t {
598 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700599 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700600 if !b {
601 sanitize.Properties.Sanitize.Coverage = nil
602 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700603 case hwasan:
604 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800605 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700606 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700607 case intOverflow:
608 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000609 case cfi:
610 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800611 case scs:
612 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800613 default:
614 panic(fmt.Errorf("unknown sanitizerType %d", t))
615 }
616 if b {
617 sanitize.Properties.SanitizerEnabled = true
618 }
619}
620
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000621// Check if the sanitizer is explicitly disabled (as opposed to nil by
622// virtue of not being set).
623func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
624 if sanitize == nil {
625 return false
626 }
627
628 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
629 return sanitizerVal != nil && *sanitizerVal == false
630}
631
632// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
633// because enabling a sanitizer either directly (via the blueprint) or
634// indirectly (via a mutator) sets the bool ptr to true, and you can't
635// distinguish between the cases. It isn't needed though - both cases can be
636// treated identically.
637func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
638 if sanitize == nil {
639 return false
640 }
641
642 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
643 return sanitizerVal != nil && *sanitizerVal == true
644}
645
Colin Cross6b753602018-06-21 13:03:07 -0700646func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
647 t, ok := tag.(dependencyTag)
648 return ok && t.library || t == reuseObjTag
649}
650
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700651// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700652func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
653 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000654 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700655 mctx.WalkDeps(func(child, parent android.Module) bool {
656 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
657 return false
658 }
659 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800660 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000661 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800662 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700663 if d.static() {
664 d.sanitize.Properties.SanitizeDep = true
665 }
666 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000667 d.sanitize.Properties.SanitizeDep = true
668 }
Colin Cross16b23492016-01-06 14:41:07 -0800669 }
Colin Cross6b753602018-06-21 13:03:07 -0700670 return true
Colin Cross16b23492016-01-06 14:41:07 -0800671 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900672 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
673 // If an APEX module includes a lib which is enabled for a sanitizer T, then
674 // the APEX module is also enabled for the same sanitizer type.
675 mctx.VisitDirectDeps(func(child android.Module) {
676 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
677 sanitizeable.EnableSanitizer(t.name())
678 }
679 })
Colin Cross16b23492016-01-06 14:41:07 -0800680 }
681 }
682}
683
Ivan Lozano30c5db22018-02-21 15:49:20 -0800684// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700685func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
686 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
687 mctx.WalkDeps(func(child, parent android.Module) bool {
688 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
689 return false
690 }
691 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800692
Colin Cross6b753602018-06-21 13:03:07 -0700693 if enableMinimalRuntime(d.sanitize) {
694 // If a static dependency is built with the minimal runtime,
695 // make sure we include the ubsan minimal runtime.
696 c.sanitize.Properties.MinimalRuntimeDep = true
697 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
698 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
699 // If a static dependency runs with full ubsan diagnostics,
700 // make sure we include the ubsan runtime.
701 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800702 }
Colin Cross6b753602018-06-21 13:03:07 -0700703 }
704 return true
705 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800706 }
707}
708
Jiyong Park379de2f2018-12-19 02:47:14 +0900709// Add the dependency to the runtime library for each of the sanitizer variants
710func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900711 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000712 if !c.Enabled() {
713 return
714 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900715 var sanitizers []string
716 var diagSanitizers []string
717
718 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
719 sanitizers = append(sanitizers, "undefined")
720 } else {
721 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
722 sanitizers = append(sanitizers,
723 "bool",
724 "integer-divide-by-zero",
725 "return",
726 "returns-nonnull-attribute",
727 "shift-exponent",
728 "unreachable",
729 "vla-bound",
730 // TODO(danalbert): The following checks currently have compiler performance issues.
731 //"alignment",
732 //"bounds",
733 //"enum",
734 //"float-cast-overflow",
735 //"float-divide-by-zero",
736 //"nonnull-attribute",
737 //"null",
738 //"shift-base",
739 //"signed-integer-overflow",
740 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
741 // https://llvm.org/PR19302
742 // http://reviews.llvm.org/D6974
743 // "object-size",
744 )
745 }
746 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
747 }
748
749 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
750 diagSanitizers = append(diagSanitizers, "undefined")
751 }
752
753 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
754
755 if Bool(c.sanitize.Properties.Sanitize.Address) {
756 sanitizers = append(sanitizers, "address")
757 diagSanitizers = append(diagSanitizers, "address")
758 }
759
760 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
761 sanitizers = append(sanitizers, "hwaddress")
762 }
763
764 if Bool(c.sanitize.Properties.Sanitize.Thread) {
765 sanitizers = append(sanitizers, "thread")
766 }
767
768 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
769 sanitizers = append(sanitizers, "safe-stack")
770 }
771
772 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
773 sanitizers = append(sanitizers, "cfi")
774
775 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
776 diagSanitizers = append(diagSanitizers, "cfi")
777 }
778 }
779
780 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
781 sanitizers = append(sanitizers, "unsigned-integer-overflow")
782 sanitizers = append(sanitizers, "signed-integer-overflow")
783 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
784 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
785 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
786 }
787 }
788
789 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
790 sanitizers = append(sanitizers, "scudo")
791 }
792
793 if Bool(c.sanitize.Properties.Sanitize.Scs) {
794 sanitizers = append(sanitizers, "shadow-call-stack")
795 }
796
797 // Save the list of sanitizers. These will be used again when generating
798 // the build rules (for Cflags, etc.)
799 c.sanitize.Properties.Sanitizers = sanitizers
800 c.sanitize.Properties.DiagSanitizers = diagSanitizers
801
802 // Determine the runtime library required
803 runtimeLibrary := ""
804 toolchain := c.toolchain(mctx)
805 if Bool(c.sanitize.Properties.Sanitize.Address) {
806 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
807 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
808 if c.staticBinary() {
809 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
810 } else {
811 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
812 }
813 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
814 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
815 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
816 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
817 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
818 } else {
819 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
820 }
821 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
822 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
823 }
824
825 if mctx.Device() && runtimeLibrary != "" {
Inseob Kim9516ee92019-05-09 10:56:13 +0900826 if inList(runtimeLibrary, *llndkLibraries(mctx.Config())) && !c.static() && c.useVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900827 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
828 }
829
830 // Adding dependency to the runtime library. We are using *FarVariation*
831 // because the runtime libraries themselves are not mutated by sanitizer
832 // mutators and thus don't have sanitizer variants whereas this module
833 // has been already mutated.
834 //
835 // Note that by adding dependency with {static|shared}DepTag, the lib is
836 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
837 if c.staticBinary() {
838 // static executable gets static runtime libs
839 mctx.AddFarVariationDependencies([]blueprint.Variation{
840 {Mutator: "link", Variation: "static"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900841 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900842 {Mutator: "arch", Variation: mctx.Target().String()},
843 }, staticDepTag, runtimeLibrary)
844 } else if !c.static() {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900845 // dynamic executable and shared libs get shared runtime libs
Jiyong Park379de2f2018-12-19 02:47:14 +0900846 mctx.AddFarVariationDependencies([]blueprint.Variation{
847 {Mutator: "link", Variation: "shared"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900848 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900849 {Mutator: "arch", Variation: mctx.Target().String()},
Jiyong Park64a44f22019-01-18 14:37:08 +0900850 }, earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900851 }
852 // static lib does not have dependency to the runtime library. The
853 // dependency will be added to the executables or shared libs using
854 // the static lib.
855 }
856 }
857}
858
859type Sanitizeable interface {
860 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900861 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900862 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900863}
864
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000865// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700866func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
867 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000868 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000869 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900870 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700871 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000872 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
873 // Save original sanitizer status before we assign values to variant
874 // 0 as that overwrites the original.
875 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
876
Jiyong Park82226632019-02-01 10:50:50 +0900877 modules := mctx.CreateVariations("", t.variationName())
Colin Crossb0f28952016-09-19 16:46:53 -0700878 modules[0].(*Module).sanitize.SetSanitizer(t, false)
879 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000880
Colin Crossb0f28952016-09-19 16:46:53 -0700881 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
882 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800883
884 // We don't need both variants active for anything but CFI-enabled
885 // target static libraries, so suppress the appropriate variant in
886 // all other cases.
887 if t == cfi {
888 if c.static() {
889 if !mctx.Device() {
890 if isSanitizerEnabled {
891 modules[0].(*Module).Properties.PreventInstall = true
892 modules[0].(*Module).Properties.HideFromMake = true
893 } else {
894 modules[1].(*Module).Properties.PreventInstall = true
895 modules[1].(*Module).Properties.HideFromMake = true
896 }
897 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800898 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800899
900 cfiStaticLibsMutex.Lock()
901 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
902 cfiStaticLibsMutex.Unlock()
903 }
904 } else {
905 modules[0].(*Module).Properties.PreventInstall = true
906 modules[0].(*Module).Properties.HideFromMake = true
907 }
908 } else if t == asan {
909 if mctx.Device() {
910 // CFI and ASAN are currently mutually exclusive so disable
911 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000912 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
913 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
914 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700915 if isSanitizerEnabled {
916 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800917 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700918 } else {
919 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800920 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700921 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800922 } else if t == scs {
923 // We don't currently link any static libraries built with make into
924 // libraries built with SCS, so we don't need logic for propagating
925 // SCSness of dependencies into make.
926 if !c.static() {
927 if isSanitizerEnabled {
928 modules[0].(*Module).Properties.PreventInstall = true
929 modules[0].(*Module).Properties.HideFromMake = true
930 } else {
931 modules[1].(*Module).Properties.PreventInstall = true
932 modules[1].(*Module).Properties.HideFromMake = true
933 }
934 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700935 } else if t == hwasan {
936 if mctx.Device() {
937 // CFI and HWASAN are currently mutually exclusive so disable
938 // CFI if this is an HWASAN variant.
939 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
940 }
941
942 if c.static() {
943 if c.useVndk() {
944 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
945 hwasanStaticLibsMutex.Lock()
946 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
947 hwasanStaticLibsMutex.Unlock()
948 } else {
949 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
950 hwasanStaticLibsMutex.Lock()
951 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
952 hwasanStaticLibsMutex.Unlock()
953 }
954 } else {
955 if isSanitizerEnabled {
956 modules[0].(*Module).Properties.PreventInstall = true
957 modules[0].(*Module).Properties.HideFromMake = true
958 } else {
959 modules[1].(*Module).Properties.PreventInstall = true
960 modules[1].(*Module).Properties.HideFromMake = true
961 }
962 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700963 }
Colin Cross16b23492016-01-06 14:41:07 -0800964 }
965 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +0900966 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900967 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +0900968 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -0800969 }
970 }
971}
Vishwath Mohane7128792017-11-17 11:08:10 -0800972
Colin Cross571cccf2019-02-04 11:22:08 -0800973var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
974
Vishwath Mohane7128792017-11-17 11:08:10 -0800975func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800976 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -0800977 return &[]string{}
978 }).(*[]string)
979}
980
Colin Cross571cccf2019-02-04 11:22:08 -0800981var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
982
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700983func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800984 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700985 return &[]string{}
986 }).(*[]string)
987}
988
Colin Cross571cccf2019-02-04 11:22:08 -0800989var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
990
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700991func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800992 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700993 return &[]string{}
994 }).(*[]string)
995}
996
Ivan Lozano30c5db22018-02-21 15:49:20 -0800997func enableMinimalRuntime(sanitize *sanitize) bool {
998 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700999 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -08001000 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
1001 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
1002 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1003 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
1004 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
1005 return true
1006 }
1007 return false
1008}
1009
Vishwath Mohane7128792017-11-17 11:08:10 -08001010func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1011 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001012 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001013 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1014}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001015
1016func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1017 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1018 sort.Strings(*hwasanStaticLibs)
1019 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1020
1021 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1022 sort.Strings(*hwasanVendorStaticLibs)
1023 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1024}