blob: 6859f392ec770c52f8425dfb94295dfde16e477a [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
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -070081 fuzzer
Colin Cross16b23492016-01-06 14:41:07 -080082)
83
Jiyong Park82226632019-02-01 10:50:50 +090084// Name of the sanitizer variation for this sanitizer type
85func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080086 switch t {
87 case asan:
88 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070089 case hwasan:
90 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080091 case tsan:
92 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070093 case intOverflow:
94 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000095 case cfi:
96 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080097 case scs:
98 return "scs"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -070099 case fuzzer:
100 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800101 default:
102 panic(fmt.Errorf("unknown sanitizerType %d", t))
103 }
104}
105
Jiyong Park82226632019-02-01 10:50:50 +0900106// This is the sanitizer names in SANITIZE_[TARGET|HOST]
107func (t sanitizerType) name() string {
108 switch t {
109 case asan:
110 return "address"
111 case hwasan:
112 return "hwaddress"
113 case tsan:
114 return "thread"
115 case intOverflow:
116 return "integer_overflow"
117 case cfi:
118 return "cfi"
119 case scs:
120 return "shadow-call-stack"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700121 case fuzzer:
122 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900123 default:
124 panic(fmt.Errorf("unknown sanitizerType %d", t))
125 }
126}
127
Colin Cross16b23492016-01-06 14:41:07 -0800128type SanitizeProperties struct {
129 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
130 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800131 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800132
133 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700134 Address *bool `android:"arch_variant"`
135 Thread *bool `android:"arch_variant"`
136 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800137
138 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700139 Undefined *bool `android:"arch_variant"`
140 All_undefined *bool `android:"arch_variant"`
141 Misc_undefined []string `android:"arch_variant"`
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700142 Fuzzer *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700143 Safestack *bool `android:"arch_variant"`
144 Cfi *bool `android:"arch_variant"`
145 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700146 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800147 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800148
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700149 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
150 // Replaces abort() on error with a human-readable error message.
151 // Address and Thread sanitizers always run in diagnostic mode.
152 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700153 Undefined *bool `android:"arch_variant"`
154 Cfi *bool `android:"arch_variant"`
155 Integer_overflow *bool `android:"arch_variant"`
156 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800157 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700158 }
159
160 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800161 Recover []string
162
163 // value to pass to -fsanitize-blacklist
164 Blacklist *string
165 } `android:"arch_variant"`
166
Jiyong Park379de2f2018-12-19 02:47:14 +0900167 SanitizerEnabled bool `blueprint:"mutated"`
168 SanitizeDep bool `blueprint:"mutated"`
169 MinimalRuntimeDep bool `blueprint:"mutated"`
170 UbsanRuntimeDep bool `blueprint:"mutated"`
171 InSanitizerDir bool `blueprint:"mutated"`
172 Sanitizers []string `blueprint:"mutated"`
173 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800174}
175
176type sanitize struct {
177 Properties SanitizeProperties
178}
179
Vishwath Mohane7128792017-11-17 11:08:10 -0800180func init() {
181 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700182 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800183}
184
Colin Cross16b23492016-01-06 14:41:07 -0800185func (sanitize *sanitize) props() []interface{} {
186 return []interface{}{&sanitize.Properties}
187}
188
189func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700190 s := &sanitize.Properties.Sanitize
191
Colin Cross16b23492016-01-06 14:41:07 -0800192 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700193 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800194 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800195 }
196
Doug Hornc32c6b02019-01-17 14:44:05 -0800197 // Sanitizers do not work on Fuchsia yet.
198 if ctx.Fuchsia() {
199 s.Never = BoolPtr(true)
200 }
201
Colin Cross16b23492016-01-06 14:41:07 -0800202 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800203 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800204 return
205 }
206
Colin Cross16b23492016-01-06 14:41:07 -0800207 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700208 var globalSanitizersDiag []string
209
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700210 if ctx.Host() {
211 if !ctx.Windows() {
212 globalSanitizers = ctx.Config().SanitizeHost()
213 }
214 } else {
215 arches := ctx.Config().SanitizeDeviceArch()
216 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
217 globalSanitizers = ctx.Config().SanitizeDevice()
218 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800219 }
220 }
221
Colin Cross16b23492016-01-06 14:41:07 -0800222 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000223 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700224 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
225 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000226 }
Colin Cross16b23492016-01-06 14:41:07 -0800227
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700228 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
229 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000230 }
231
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700232 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
233 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000234 }
235
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700236 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
237 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000238 }
239
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700240 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
241 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700242 }
243
244 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
245 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000246 }
247
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700248 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800249 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700250 s.Cfi = boolPtr(true)
251 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700252 }
253
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700254 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700255 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700256 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700257 s.Integer_overflow = boolPtr(true)
258 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700259 }
260
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700261 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
262 s.Scudo = boolPtr(true)
263 }
264
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700265 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
266 s.Hwaddress = boolPtr(true)
267 }
268
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000269 if len(globalSanitizers) > 0 {
270 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
271 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700272
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700273 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700274 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700275 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700276 s.Diag.Integer_overflow = boolPtr(true)
277 }
278
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700279 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
280 s.Diag.Cfi == nil && Bool(s.Cfi) {
281 s.Diag.Cfi = boolPtr(true)
282 }
283
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700284 if len(globalSanitizersDiag) > 0 {
285 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
286 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700287 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700288
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700289 // Enable CFI for all components in the include paths (for Aarch64 only)
290 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000291 s.Cfi = boolPtr(true)
292 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
293 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700294 }
295 }
296
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800297 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800298 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800299 s.Cfi = nil
300 s.Diag.Cfi = nil
301 }
302
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800303 // Also disable CFI for arm32 until b/35157333 is fixed.
304 if ctx.Arch().ArchType == android.Arm {
305 s.Cfi = nil
306 s.Diag.Cfi = nil
307 }
308
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700309 // HWASan requires AArch64 hardware feature (top-byte-ignore).
310 if ctx.Arch().ArchType != android.Arm64 {
311 s.Hwaddress = nil
312 }
313
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800314 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800315 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800316 s.Scs = nil
317 }
318
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700319 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700320 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700321 s.Cfi = nil
322 s.Diag.Cfi = nil
323 }
324
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700325 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800326 if ctx.Host() {
327 s.Cfi = nil
328 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700329 s.Misc_undefined = nil
330 s.Undefined = nil
331 s.All_undefined = nil
332 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800333 }
334
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700335 // Also disable CFI for VNDK variants of components
336 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700337 s.Cfi = nil
338 s.Diag.Cfi = nil
339 }
340
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700341 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800342 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
343 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700344 s.Hwaddress = nil
345 }
346
Colin Cross3c344ef2016-07-18 15:44:56 -0700347 if ctx.staticBinary() {
348 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700349 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700350 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800351 }
352
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700353 if Bool(s.All_undefined) {
354 s.Undefined = nil
355 }
356
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700357 if !ctx.toolchain().Is64Bit() {
358 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700359 s.Thread = nil
360 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800361 // TODO(ccross): error for compile_multilib = "32"?
362 }
363
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800364 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700365 Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800366 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700367 sanitize.Properties.SanitizerEnabled = true
368 }
369
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800370 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
371 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700372 s.Scudo = nil
373 }
374
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700375 if Bool(s.Hwaddress) {
376 s.Address = nil
377 s.Thread = nil
378 }
379
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700380 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
381 // mutually incompatible.
382 if Bool(s.Fuzzer) {
383 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800384 }
385}
386
387func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
388 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
389 return deps
390 }
391
392 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700393 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700394 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Christopher Ferris753d4a62019-05-09 13:27:02 -0700395 // Compiling asan and having libc_scudo in the same
396 // executable will cause the executable to crash.
397 // Remove libc_scudo since it is only used to override
398 // allocation functions which asan already overrides.
399 _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800400 }
401 }
402
403 return deps
404}
405
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800406func toDisableImplicitIntegerChange(flags []string) bool {
407 // Returns true if any flag is fsanitize*integer, and there is
408 // no explicit flag about sanitize=implicit-integer-sign-change.
409 for _, f := range flags {
410 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
411 return false
412 }
413 }
414 for _, f := range flags {
415 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
416 return true
417 }
418 }
419 return false
420}
421
Colin Cross16b23492016-01-06 14:41:07 -0800422func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700423 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
424 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800425
426 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
427 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700428 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800429 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700430 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800431 return flags
432 }
433
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700434 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700435 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800436 // Frame pointer based unwinder in ASan requires ARM frame setup.
437 // TODO: put in flags?
438 flags.RequiredInstructionSet = "arm"
439 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700440 flags.CFlags = append(flags.CFlags, asanCflags...)
441 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800442
Colin Cross16b23492016-01-06 14:41:07 -0800443 if ctx.Host() {
444 // -nodefaultlibs (provided with libc++) prevents the driver from linking
445 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800446 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
447 } else {
448 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900449 if ctx.bootstrap() {
450 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
451 } else {
452 flags.DynamicLinker = "/system/bin/linker_asan"
453 }
Colin Cross16b23492016-01-06 14:41:07 -0800454 if flags.Toolchain.Is64Bit() {
455 flags.DynamicLinker += "64"
456 }
457 }
Colin Cross16b23492016-01-06 14:41:07 -0800458 }
459
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700460 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
461 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700462 }
463
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700464 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
465 flags.CFlags = append(flags.CFlags, "-fsanitize=fuzzer-no-link")
466 flags.LdFlags = append(flags.LdFlags, "-fsanitize=fuzzer-no-link")
467
468 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
469 _, flags.LdFlags = removeFromList("-flto", flags.LdFlags)
470 flags.LdFlags = append(flags.LdFlags, "-fno-lto")
Colin Cross16b23492016-01-06 14:41:07 -0800471 }
472
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700473 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800474 if ctx.Arch().ArchType == android.Arm {
475 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
476 // to do this on a function basis, so force Thumb on the entire module.
477 flags.RequiredInstructionSet = "thumb"
478 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000479
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700480 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700481 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000482 // Only append the default visibility flag if -fvisibility has not already been set
483 // to hidden.
484 if !inList("-fvisibility=hidden", flags.CFlags) {
485 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
486 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700487 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000488
489 if ctx.staticBinary() {
490 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
491 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
492 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700493 }
494
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700495 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700496 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700497 }
498
Jiyong Park379de2f2018-12-19 02:47:14 +0900499 if len(sanitize.Properties.Sanitizers) > 0 {
500 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800501
Colin Cross16b23492016-01-06 14:41:07 -0800502 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700503 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800504 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800505 // Host sanitizers only link symbols in the final executable, so
506 // there will always be undefined symbols in intermediate libraries.
507 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700508 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800509 } else {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800510 if enableMinimalRuntime(sanitize) {
511 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
512 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700513 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800514 }
Colin Cross16b23492016-01-06 14:41:07 -0800515 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700516
517 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
518 // When fuzzing, we wish to crash with diagnostics on any bug.
519 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
520 } else if ctx.Host() {
521 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
522 } else {
523 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
524 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800525 // http://b/119329758, Android core does not boot up with this sanitizer yet.
526 if toDisableImplicitIntegerChange(flags.CFlags) {
527 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
528 }
Colin Cross16b23492016-01-06 14:41:07 -0800529 }
530
Jiyong Park379de2f2018-12-19 02:47:14 +0900531 if len(sanitize.Properties.DiagSanitizers) > 0 {
532 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700533 }
534 // FIXME: enable RTTI if diag + (cfi or vptr)
535
Andreas Gampe97071162017-05-08 13:15:23 -0700536 if sanitize.Properties.Sanitize.Recover != nil {
537 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
538 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
539 }
540
Ivan Lozano7929bba2018-12-12 09:36:31 -0800541 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
542 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
543 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
544 }
545
Colin Cross635c3b02016-05-18 15:37:25 -0700546 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800547 if blacklist.Valid() {
548 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
549 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
550 }
551
552 return flags
553}
554
Colin Cross8ff9ef42017-05-08 13:44:11 -0700555func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Vishwath Mohane7128792017-11-17 11:08:10 -0800556 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
557 // name conflict.
558 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
559 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000560 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700561 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
562 ret.SubName += ".hwasan"
563 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800564 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
565 ret.SubName += ".scs"
566 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700567}
568
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700569func (sanitize *sanitize) inSanitizerDir() bool {
570 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700571}
572
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000573func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000574 switch t {
575 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000576 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700577 case hwasan:
578 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000579 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000580 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000581 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000582 return sanitize.Properties.Sanitize.Integer_overflow
583 case cfi:
584 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800585 case scs:
586 return sanitize.Properties.Sanitize.Scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700587 case fuzzer:
588 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000589 default:
590 panic(fmt.Errorf("unknown sanitizerType %d", t))
591 }
592}
593
Dan Albert7d1eecf2018-01-19 12:30:45 -0800594func (sanitize *sanitize) isUnsanitizedVariant() bool {
595 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700596 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800597 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800598 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700599 !sanitize.isSanitizerEnabled(scs) &&
600 !sanitize.isSanitizerEnabled(fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800601}
602
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700603func (sanitize *sanitize) isVariantOnProductionDevice() bool {
604 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700605 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700606 !sanitize.isSanitizerEnabled(tsan) &&
607 !sanitize.isSanitizerEnabled(fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700608}
609
Colin Cross16b23492016-01-06 14:41:07 -0800610func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
611 switch t {
612 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700613 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700614 case hwasan:
615 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800616 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700617 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700618 case intOverflow:
619 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000620 case cfi:
621 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800622 case scs:
623 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700624 case fuzzer:
625 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800626 default:
627 panic(fmt.Errorf("unknown sanitizerType %d", t))
628 }
629 if b {
630 sanitize.Properties.SanitizerEnabled = true
631 }
632}
633
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000634// Check if the sanitizer is explicitly disabled (as opposed to nil by
635// virtue of not being set).
636func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
637 if sanitize == nil {
638 return false
639 }
640
641 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
642 return sanitizerVal != nil && *sanitizerVal == false
643}
644
645// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
646// because enabling a sanitizer either directly (via the blueprint) or
647// indirectly (via a mutator) sets the bool ptr to true, and you can't
648// distinguish between the cases. It isn't needed though - both cases can be
649// treated identically.
650func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
651 if sanitize == nil {
652 return false
653 }
654
655 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
656 return sanitizerVal != nil && *sanitizerVal == true
657}
658
Colin Cross6b753602018-06-21 13:03:07 -0700659func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
660 t, ok := tag.(dependencyTag)
661 return ok && t.library || t == reuseObjTag
662}
663
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700664// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700665func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
666 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000667 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700668 mctx.WalkDeps(func(child, parent android.Module) bool {
669 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
670 return false
671 }
672 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800673 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000674 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800675 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700676 if d.static() {
677 d.sanitize.Properties.SanitizeDep = true
678 }
679 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000680 d.sanitize.Properties.SanitizeDep = true
681 }
Colin Cross16b23492016-01-06 14:41:07 -0800682 }
Colin Cross6b753602018-06-21 13:03:07 -0700683 return true
Colin Cross16b23492016-01-06 14:41:07 -0800684 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900685 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
686 // If an APEX module includes a lib which is enabled for a sanitizer T, then
687 // the APEX module is also enabled for the same sanitizer type.
688 mctx.VisitDirectDeps(func(child android.Module) {
689 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
690 sanitizeable.EnableSanitizer(t.name())
691 }
692 })
Colin Cross16b23492016-01-06 14:41:07 -0800693 }
694 }
695}
696
Ivan Lozano30c5db22018-02-21 15:49:20 -0800697// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700698func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
699 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
700 mctx.WalkDeps(func(child, parent android.Module) bool {
701 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
702 return false
703 }
704 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800705
Colin Cross6b753602018-06-21 13:03:07 -0700706 if enableMinimalRuntime(d.sanitize) {
707 // If a static dependency is built with the minimal runtime,
708 // make sure we include the ubsan minimal runtime.
709 c.sanitize.Properties.MinimalRuntimeDep = true
710 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
711 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
712 // If a static dependency runs with full ubsan diagnostics,
713 // make sure we include the ubsan runtime.
714 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800715 }
Colin Cross6b753602018-06-21 13:03:07 -0700716 }
717 return true
718 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800719 }
720}
721
Jiyong Park379de2f2018-12-19 02:47:14 +0900722// Add the dependency to the runtime library for each of the sanitizer variants
723func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900724 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000725 if !c.Enabled() {
726 return
727 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900728 var sanitizers []string
729 var diagSanitizers []string
730
731 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
732 sanitizers = append(sanitizers, "undefined")
733 } else {
734 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
735 sanitizers = append(sanitizers,
736 "bool",
737 "integer-divide-by-zero",
738 "return",
739 "returns-nonnull-attribute",
740 "shift-exponent",
741 "unreachable",
742 "vla-bound",
743 // TODO(danalbert): The following checks currently have compiler performance issues.
744 //"alignment",
745 //"bounds",
746 //"enum",
747 //"float-cast-overflow",
748 //"float-divide-by-zero",
749 //"nonnull-attribute",
750 //"null",
751 //"shift-base",
752 //"signed-integer-overflow",
753 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
754 // https://llvm.org/PR19302
755 // http://reviews.llvm.org/D6974
756 // "object-size",
757 )
758 }
759 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
760 }
761
762 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
763 diagSanitizers = append(diagSanitizers, "undefined")
764 }
765
766 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
767
768 if Bool(c.sanitize.Properties.Sanitize.Address) {
769 sanitizers = append(sanitizers, "address")
770 diagSanitizers = append(diagSanitizers, "address")
771 }
772
773 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
774 sanitizers = append(sanitizers, "hwaddress")
775 }
776
777 if Bool(c.sanitize.Properties.Sanitize.Thread) {
778 sanitizers = append(sanitizers, "thread")
779 }
780
781 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
782 sanitizers = append(sanitizers, "safe-stack")
783 }
784
785 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
786 sanitizers = append(sanitizers, "cfi")
787
788 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
789 diagSanitizers = append(diagSanitizers, "cfi")
790 }
791 }
792
793 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
794 sanitizers = append(sanitizers, "unsigned-integer-overflow")
795 sanitizers = append(sanitizers, "signed-integer-overflow")
796 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
797 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
798 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
799 }
800 }
801
802 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
803 sanitizers = append(sanitizers, "scudo")
804 }
805
806 if Bool(c.sanitize.Properties.Sanitize.Scs) {
807 sanitizers = append(sanitizers, "shadow-call-stack")
808 }
809
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700810 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
811 sanitizers = append(sanitizers, "fuzzer-no-link")
812 }
813
Jiyong Park379de2f2018-12-19 02:47:14 +0900814 // Save the list of sanitizers. These will be used again when generating
815 // the build rules (for Cflags, etc.)
816 c.sanitize.Properties.Sanitizers = sanitizers
817 c.sanitize.Properties.DiagSanitizers = diagSanitizers
818
819 // Determine the runtime library required
820 runtimeLibrary := ""
821 toolchain := c.toolchain(mctx)
822 if Bool(c.sanitize.Properties.Sanitize.Address) {
823 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
824 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
825 if c.staticBinary() {
826 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
827 } else {
828 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
829 }
830 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
831 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
832 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
833 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
834 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
835 } else {
836 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
837 }
838 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
839 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
840 }
841
842 if mctx.Device() && runtimeLibrary != "" {
Inseob Kim9516ee92019-05-09 10:56:13 +0900843 if inList(runtimeLibrary, *llndkLibraries(mctx.Config())) && !c.static() && c.useVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900844 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
845 }
846
847 // Adding dependency to the runtime library. We are using *FarVariation*
848 // because the runtime libraries themselves are not mutated by sanitizer
849 // mutators and thus don't have sanitizer variants whereas this module
850 // has been already mutated.
851 //
852 // Note that by adding dependency with {static|shared}DepTag, the lib is
853 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
854 if c.staticBinary() {
855 // static executable gets static runtime libs
856 mctx.AddFarVariationDependencies([]blueprint.Variation{
857 {Mutator: "link", Variation: "static"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900858 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900859 {Mutator: "arch", Variation: mctx.Target().String()},
860 }, staticDepTag, runtimeLibrary)
861 } else if !c.static() {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900862 // dynamic executable and shared libs get shared runtime libs
Jiyong Park379de2f2018-12-19 02:47:14 +0900863 mctx.AddFarVariationDependencies([]blueprint.Variation{
864 {Mutator: "link", Variation: "shared"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900865 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900866 {Mutator: "arch", Variation: mctx.Target().String()},
Jiyong Park64a44f22019-01-18 14:37:08 +0900867 }, earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900868 }
869 // static lib does not have dependency to the runtime library. The
870 // dependency will be added to the executables or shared libs using
871 // the static lib.
872 }
873 }
874}
875
876type Sanitizeable interface {
877 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900878 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900879 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900880}
881
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000882// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700883func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
884 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000885 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000886 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900887 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700888 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000889 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
890 // Save original sanitizer status before we assign values to variant
891 // 0 as that overwrites the original.
892 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
893
Jiyong Park82226632019-02-01 10:50:50 +0900894 modules := mctx.CreateVariations("", t.variationName())
Colin Crossb0f28952016-09-19 16:46:53 -0700895 modules[0].(*Module).sanitize.SetSanitizer(t, false)
896 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000897
Colin Crossb0f28952016-09-19 16:46:53 -0700898 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
899 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800900
901 // We don't need both variants active for anything but CFI-enabled
902 // target static libraries, so suppress the appropriate variant in
903 // all other cases.
904 if t == cfi {
905 if c.static() {
906 if !mctx.Device() {
907 if isSanitizerEnabled {
908 modules[0].(*Module).Properties.PreventInstall = true
909 modules[0].(*Module).Properties.HideFromMake = true
910 } else {
911 modules[1].(*Module).Properties.PreventInstall = true
912 modules[1].(*Module).Properties.HideFromMake = true
913 }
914 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800915 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800916
917 cfiStaticLibsMutex.Lock()
918 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
919 cfiStaticLibsMutex.Unlock()
920 }
921 } else {
922 modules[0].(*Module).Properties.PreventInstall = true
923 modules[0].(*Module).Properties.HideFromMake = true
924 }
925 } else if t == asan {
926 if mctx.Device() {
927 // CFI and ASAN are currently mutually exclusive so disable
928 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000929 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
930 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
931 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700932 if isSanitizerEnabled {
933 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800934 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700935 } else {
936 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800937 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700938 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800939 } else if t == scs {
940 // We don't currently link any static libraries built with make into
941 // libraries built with SCS, so we don't need logic for propagating
942 // SCSness of dependencies into make.
943 if !c.static() {
944 if isSanitizerEnabled {
945 modules[0].(*Module).Properties.PreventInstall = true
946 modules[0].(*Module).Properties.HideFromMake = true
947 } else {
948 modules[1].(*Module).Properties.PreventInstall = true
949 modules[1].(*Module).Properties.HideFromMake = true
950 }
951 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700952 } else if t == fuzzer {
953 // TODO(b/131771163): CFI and fuzzer support are mutually incompatible
954 // as CFI pulls in LTO.
955 if mctx.Device() {
956 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
957 }
958 if isSanitizerEnabled {
959 modules[0].(*Module).Properties.PreventInstall = true
960 modules[0].(*Module).Properties.HideFromMake = true
961 } else {
962 modules[1].(*Module).Properties.PreventInstall = true
963 modules[1].(*Module).Properties.HideFromMake = true
964 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700965 } else if t == hwasan {
966 if mctx.Device() {
967 // CFI and HWASAN are currently mutually exclusive so disable
968 // CFI if this is an HWASAN variant.
969 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
970 }
971
972 if c.static() {
973 if c.useVndk() {
974 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
975 hwasanStaticLibsMutex.Lock()
976 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
977 hwasanStaticLibsMutex.Unlock()
978 } else {
979 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
980 hwasanStaticLibsMutex.Lock()
981 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
982 hwasanStaticLibsMutex.Unlock()
983 }
984 } else {
985 if isSanitizerEnabled {
986 modules[0].(*Module).Properties.PreventInstall = true
987 modules[0].(*Module).Properties.HideFromMake = true
988 } else {
989 modules[1].(*Module).Properties.PreventInstall = true
990 modules[1].(*Module).Properties.HideFromMake = true
991 }
992 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700993 }
Colin Cross16b23492016-01-06 14:41:07 -0800994 }
995 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +0900996 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900997 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +0900998 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -0800999 }
1000 }
1001}
Vishwath Mohane7128792017-11-17 11:08:10 -08001002
Colin Cross571cccf2019-02-04 11:22:08 -08001003var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1004
Vishwath Mohane7128792017-11-17 11:08:10 -08001005func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001006 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -08001007 return &[]string{}
1008 }).(*[]string)
1009}
1010
Colin Cross571cccf2019-02-04 11:22:08 -08001011var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1012
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001013func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001014 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001015 return &[]string{}
1016 }).(*[]string)
1017}
1018
Colin Cross571cccf2019-02-04 11:22:08 -08001019var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
1020
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001021func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001022 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001023 return &[]string{}
1024 }).(*[]string)
1025}
1026
Ivan Lozano30c5db22018-02-21 15:49:20 -08001027func enableMinimalRuntime(sanitize *sanitize) bool {
1028 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001029 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001030 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -08001031 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
1032 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
1033 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1034 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
1035 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
1036 return true
1037 }
1038 return false
1039}
1040
Vishwath Mohane7128792017-11-17 11:08:10 -08001041func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1042 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001043 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001044 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1045}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001046
1047func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1048 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1049 sort.Strings(*hwasanStaticLibs)
1050 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1051
1052 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1053 sort.Strings(*hwasanVendorStaticLibs)
1054 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1055}