blob: e6df44f3cee7392d13028d0bfcf6822e995005fb [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
Evgenii Stepanov93c3f532019-02-01 14:53:12 -080038 hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=", "-mllvm", "-hwasan-create-frame-descriptions=0"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070039
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000040 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070041 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070042 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
43 // used, but have no effect on assembly files
44 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070045 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070046 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070047 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
48 cfiStaticLibsMutex sync.Mutex
49 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070050
Evgenii Stepanov98f5b062018-11-29 15:12:51 -080051 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080052
Peter Collingbournebd19db02019-03-06 10:38:48 -080053 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070054 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov3c5a52a2018-12-18 17:02:44 -080055 hwasanGlobalOptions = []string{"heap_history_size=1023,stack_history_size=512"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070056)
57
Colin Cross16b23492016-01-06 14:41:07 -080058type sanitizerType int
59
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070060func boolPtr(v bool) *bool {
61 if v {
62 return &v
63 } else {
64 return nil
65 }
66}
67
Colin Cross16b23492016-01-06 14:41:07 -080068const (
69 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070070 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080071 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070072 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000073 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080074 scs
Colin Cross16b23492016-01-06 14:41:07 -080075)
76
Jiyong Park82226632019-02-01 10:50:50 +090077// Name of the sanitizer variation for this sanitizer type
78func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080079 switch t {
80 case asan:
81 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070082 case hwasan:
83 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080084 case tsan:
85 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070086 case intOverflow:
87 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000088 case cfi:
89 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080090 case scs:
91 return "scs"
Colin Cross16b23492016-01-06 14:41:07 -080092 default:
93 panic(fmt.Errorf("unknown sanitizerType %d", t))
94 }
95}
96
Jiyong Park82226632019-02-01 10:50:50 +090097// This is the sanitizer names in SANITIZE_[TARGET|HOST]
98func (t sanitizerType) name() string {
99 switch t {
100 case asan:
101 return "address"
102 case hwasan:
103 return "hwaddress"
104 case tsan:
105 return "thread"
106 case intOverflow:
107 return "integer_overflow"
108 case cfi:
109 return "cfi"
110 case scs:
111 return "shadow-call-stack"
112 default:
113 panic(fmt.Errorf("unknown sanitizerType %d", t))
114 }
115}
116
Colin Cross16b23492016-01-06 14:41:07 -0800117type SanitizeProperties struct {
118 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
119 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800120 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800121
122 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700123 Address *bool `android:"arch_variant"`
124 Thread *bool `android:"arch_variant"`
125 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800126
127 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700128 Undefined *bool `android:"arch_variant"`
129 All_undefined *bool `android:"arch_variant"`
130 Misc_undefined []string `android:"arch_variant"`
131 Coverage *bool `android:"arch_variant"`
132 Safestack *bool `android:"arch_variant"`
133 Cfi *bool `android:"arch_variant"`
134 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700135 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800136 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800137
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700138 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
139 // Replaces abort() on error with a human-readable error message.
140 // Address and Thread sanitizers always run in diagnostic mode.
141 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700142 Undefined *bool `android:"arch_variant"`
143 Cfi *bool `android:"arch_variant"`
144 Integer_overflow *bool `android:"arch_variant"`
145 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800146 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700147 }
148
149 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800150 Recover []string
151
152 // value to pass to -fsanitize-blacklist
153 Blacklist *string
154 } `android:"arch_variant"`
155
Jiyong Park379de2f2018-12-19 02:47:14 +0900156 SanitizerEnabled bool `blueprint:"mutated"`
157 SanitizeDep bool `blueprint:"mutated"`
158 MinimalRuntimeDep bool `blueprint:"mutated"`
159 UbsanRuntimeDep bool `blueprint:"mutated"`
160 InSanitizerDir bool `blueprint:"mutated"`
161 Sanitizers []string `blueprint:"mutated"`
162 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800163}
164
165type sanitize struct {
166 Properties SanitizeProperties
167}
168
Vishwath Mohane7128792017-11-17 11:08:10 -0800169func init() {
170 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700171 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800172}
173
Colin Cross16b23492016-01-06 14:41:07 -0800174func (sanitize *sanitize) props() []interface{} {
175 return []interface{}{&sanitize.Properties}
176}
177
178func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700179 s := &sanitize.Properties.Sanitize
180
Colin Cross16b23492016-01-06 14:41:07 -0800181 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700182 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800183 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800184 }
185
Doug Hornc32c6b02019-01-17 14:44:05 -0800186 // Sanitizers do not work on Fuchsia yet.
187 if ctx.Fuchsia() {
188 s.Never = BoolPtr(true)
189 }
190
Colin Cross16b23492016-01-06 14:41:07 -0800191 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800192 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800193 return
194 }
195
Colin Cross16b23492016-01-06 14:41:07 -0800196 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700197 var globalSanitizersDiag []string
198
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700199 if ctx.Host() {
200 if !ctx.Windows() {
201 globalSanitizers = ctx.Config().SanitizeHost()
202 }
203 } else {
204 arches := ctx.Config().SanitizeDeviceArch()
205 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
206 globalSanitizers = ctx.Config().SanitizeDevice()
207 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800208 }
209 }
210
Colin Cross16b23492016-01-06 14:41:07 -0800211 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000212 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700213 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
214 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000215 }
Colin Cross16b23492016-01-06 14:41:07 -0800216
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700217 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
218 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000219 }
220
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800221 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
222 if s.Address == nil {
223 s.Address = boolPtr(true)
224 } else if *s.Address == false {
225 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
226 // disables address, then disable coverage as well.
227 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
228 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000229 }
230
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700231 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
232 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000233 }
234
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700235 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
236 s.Coverage = boolPtr(true)
237 }
238
239 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
240 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000241 }
242
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700243 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800244 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700245 s.Cfi = boolPtr(true)
246 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700247 }
248
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700249 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700250 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700251 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700252 s.Integer_overflow = boolPtr(true)
253 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700254 }
255
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700256 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
257 s.Scudo = boolPtr(true)
258 }
259
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700260 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
261 s.Hwaddress = boolPtr(true)
262 }
263
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000264 if len(globalSanitizers) > 0 {
265 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
266 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700267
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700268 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700269 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700270 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700271 s.Diag.Integer_overflow = boolPtr(true)
272 }
273
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700274 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
275 s.Diag.Cfi == nil && Bool(s.Cfi) {
276 s.Diag.Cfi = boolPtr(true)
277 }
278
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700279 if len(globalSanitizersDiag) > 0 {
280 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
281 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700282 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700283
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700284 // Enable CFI for all components in the include paths (for Aarch64 only)
285 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000286 s.Cfi = boolPtr(true)
287 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
288 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700289 }
290 }
291
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800292 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800293 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800294 s.Cfi = nil
295 s.Diag.Cfi = nil
296 }
297
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800298 // Also disable CFI for arm32 until b/35157333 is fixed.
299 if ctx.Arch().ArchType == android.Arm {
300 s.Cfi = nil
301 s.Diag.Cfi = nil
302 }
303
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700304 // HWASan requires AArch64 hardware feature (top-byte-ignore).
305 if ctx.Arch().ArchType != android.Arm64 {
306 s.Hwaddress = nil
307 }
308
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800309 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800310 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800311 s.Scs = nil
312 }
313
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700314 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700315 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700316 s.Cfi = nil
317 s.Diag.Cfi = nil
318 }
319
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700320 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800321 if ctx.Host() {
322 s.Cfi = nil
323 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700324 s.Misc_undefined = nil
325 s.Undefined = nil
326 s.All_undefined = nil
327 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800328 }
329
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700330 // Also disable CFI for VNDK variants of components
331 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700332 s.Cfi = nil
333 s.Diag.Cfi = nil
334 }
335
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700336 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800337 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
338 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700339 s.Hwaddress = nil
340 }
341
Colin Cross3c344ef2016-07-18 15:44:56 -0700342 if ctx.staticBinary() {
343 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700344 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700345 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800346 }
347
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700348 if Bool(s.All_undefined) {
349 s.Undefined = nil
350 }
351
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700352 if !ctx.toolchain().Is64Bit() {
353 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700354 s.Thread = nil
355 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800356 // TODO(ccross): error for compile_multilib = "32"?
357 }
358
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800359 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 -0700360 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 -0800361 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700362 sanitize.Properties.SanitizerEnabled = true
363 }
364
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800365 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
366 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700367 s.Scudo = nil
368 }
369
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700370 if Bool(s.Hwaddress) {
371 s.Address = nil
372 s.Thread = nil
373 }
374
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700375 if Bool(s.Coverage) {
376 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800377 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
378 }
379 }
380}
381
382func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
383 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
384 return deps
385 }
386
387 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700388 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700389 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800390 }
391 }
392
393 return deps
394}
395
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800396func toDisableImplicitIntegerChange(flags []string) bool {
397 // Returns true if any flag is fsanitize*integer, and there is
398 // no explicit flag about sanitize=implicit-integer-sign-change.
399 for _, f := range flags {
400 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
401 return false
402 }
403 }
404 for _, f := range flags {
405 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
406 return true
407 }
408 }
409 return false
410}
411
Colin Cross16b23492016-01-06 14:41:07 -0800412func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700413 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
414 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800415
416 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
417 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700418 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800419 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700420 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800421 return flags
422 }
423
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700424 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700425 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800426 // Frame pointer based unwinder in ASan requires ARM frame setup.
427 // TODO: put in flags?
428 flags.RequiredInstructionSet = "arm"
429 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700430 flags.CFlags = append(flags.CFlags, asanCflags...)
431 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800432
Colin Cross16b23492016-01-06 14:41:07 -0800433 if ctx.Host() {
434 // -nodefaultlibs (provided with libc++) prevents the driver from linking
435 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800436 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
437 } else {
438 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900439 if ctx.bootstrap() {
440 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
441 } else {
442 flags.DynamicLinker = "/system/bin/linker_asan"
443 }
Colin Cross16b23492016-01-06 14:41:07 -0800444 if flags.Toolchain.Is64Bit() {
445 flags.DynamicLinker += "64"
446 }
447 }
Colin Cross16b23492016-01-06 14:41:07 -0800448 }
449
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700450 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
451 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700452 }
453
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700454 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400455 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800456 }
457
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700458 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800459 if ctx.Arch().ArchType == android.Arm {
460 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
461 // to do this on a function basis, so force Thumb on the entire module.
462 flags.RequiredInstructionSet = "thumb"
463 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000464
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700465 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700466 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000467 // Only append the default visibility flag if -fvisibility has not already been set
468 // to hidden.
469 if !inList("-fvisibility=hidden", flags.CFlags) {
470 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
471 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700472 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000473
474 if ctx.staticBinary() {
475 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
476 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
477 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700478 }
479
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700480 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700481 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700482 }
483
Jiyong Park379de2f2018-12-19 02:47:14 +0900484 if len(sanitize.Properties.Sanitizers) > 0 {
485 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800486
Colin Cross16b23492016-01-06 14:41:07 -0800487 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700488 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800489 if ctx.Host() {
490 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
491 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800492 // Host sanitizers only link symbols in the final executable, so
493 // there will always be undefined symbols in intermediate libraries.
494 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800495 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700496 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800497
498 if enableMinimalRuntime(sanitize) {
499 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
500 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700501 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800502 }
Colin Cross16b23492016-01-06 14:41:07 -0800503 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800504 // http://b/119329758, Android core does not boot up with this sanitizer yet.
505 if toDisableImplicitIntegerChange(flags.CFlags) {
506 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
507 }
Colin Cross16b23492016-01-06 14:41:07 -0800508 }
509
Jiyong Park379de2f2018-12-19 02:47:14 +0900510 if len(sanitize.Properties.DiagSanitizers) > 0 {
511 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700512 }
513 // FIXME: enable RTTI if diag + (cfi or vptr)
514
Andreas Gampe97071162017-05-08 13:15:23 -0700515 if sanitize.Properties.Sanitize.Recover != nil {
516 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
517 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
518 }
519
Ivan Lozano7929bba2018-12-12 09:36:31 -0800520 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
521 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
522 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
523 }
524
Colin Cross635c3b02016-05-18 15:37:25 -0700525 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800526 if blacklist.Valid() {
527 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
528 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
529 }
530
531 return flags
532}
533
Colin Cross8ff9ef42017-05-08 13:44:11 -0700534func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Vishwath Mohane7128792017-11-17 11:08:10 -0800535 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
536 // name conflict.
537 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
538 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000539 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700540 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
541 ret.SubName += ".hwasan"
542 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800543 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
544 ret.SubName += ".scs"
545 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700546}
547
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700548func (sanitize *sanitize) inSanitizerDir() bool {
549 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700550}
551
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000552func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000553 switch t {
554 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000555 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700556 case hwasan:
557 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000558 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000559 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000560 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000561 return sanitize.Properties.Sanitize.Integer_overflow
562 case cfi:
563 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800564 case scs:
565 return sanitize.Properties.Sanitize.Scs
Vishwath Mohan95229302017-08-11 00:53:16 +0000566 default:
567 panic(fmt.Errorf("unknown sanitizerType %d", t))
568 }
569}
570
Dan Albert7d1eecf2018-01-19 12:30:45 -0800571func (sanitize *sanitize) isUnsanitizedVariant() bool {
572 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700573 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800574 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800575 !sanitize.isSanitizerEnabled(cfi) &&
576 !sanitize.isSanitizerEnabled(scs)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800577}
578
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700579func (sanitize *sanitize) isVariantOnProductionDevice() bool {
580 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700581 !sanitize.isSanitizerEnabled(hwasan) &&
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700582 !sanitize.isSanitizerEnabled(tsan)
583}
584
Colin Cross16b23492016-01-06 14:41:07 -0800585func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
586 switch t {
587 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700588 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700589 if !b {
590 sanitize.Properties.Sanitize.Coverage = nil
591 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700592 case hwasan:
593 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800594 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700595 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700596 case intOverflow:
597 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000598 case cfi:
599 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800600 case scs:
601 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800602 default:
603 panic(fmt.Errorf("unknown sanitizerType %d", t))
604 }
605 if b {
606 sanitize.Properties.SanitizerEnabled = true
607 }
608}
609
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000610// Check if the sanitizer is explicitly disabled (as opposed to nil by
611// virtue of not being set).
612func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
613 if sanitize == nil {
614 return false
615 }
616
617 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
618 return sanitizerVal != nil && *sanitizerVal == false
619}
620
621// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
622// because enabling a sanitizer either directly (via the blueprint) or
623// indirectly (via a mutator) sets the bool ptr to true, and you can't
624// distinguish between the cases. It isn't needed though - both cases can be
625// treated identically.
626func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
627 if sanitize == nil {
628 return false
629 }
630
631 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
632 return sanitizerVal != nil && *sanitizerVal == true
633}
634
Colin Cross6b753602018-06-21 13:03:07 -0700635func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
636 t, ok := tag.(dependencyTag)
637 return ok && t.library || t == reuseObjTag
638}
639
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700640// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700641func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
642 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000643 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700644 mctx.WalkDeps(func(child, parent android.Module) bool {
645 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
646 return false
647 }
648 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800649 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000650 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800651 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700652 if d.static() {
653 d.sanitize.Properties.SanitizeDep = true
654 }
655 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000656 d.sanitize.Properties.SanitizeDep = true
657 }
Colin Cross16b23492016-01-06 14:41:07 -0800658 }
Colin Cross6b753602018-06-21 13:03:07 -0700659 return true
Colin Cross16b23492016-01-06 14:41:07 -0800660 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900661 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
662 // If an APEX module includes a lib which is enabled for a sanitizer T, then
663 // the APEX module is also enabled for the same sanitizer type.
664 mctx.VisitDirectDeps(func(child android.Module) {
665 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
666 sanitizeable.EnableSanitizer(t.name())
667 }
668 })
Colin Cross16b23492016-01-06 14:41:07 -0800669 }
670 }
671}
672
Ivan Lozano30c5db22018-02-21 15:49:20 -0800673// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700674func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
675 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
676 mctx.WalkDeps(func(child, parent android.Module) bool {
677 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
678 return false
679 }
680 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800681
Colin Cross6b753602018-06-21 13:03:07 -0700682 if enableMinimalRuntime(d.sanitize) {
683 // If a static dependency is built with the minimal runtime,
684 // make sure we include the ubsan minimal runtime.
685 c.sanitize.Properties.MinimalRuntimeDep = true
686 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
687 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
688 // If a static dependency runs with full ubsan diagnostics,
689 // make sure we include the ubsan runtime.
690 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800691 }
Colin Cross6b753602018-06-21 13:03:07 -0700692 }
693 return true
694 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800695 }
696}
697
Jiyong Park379de2f2018-12-19 02:47:14 +0900698// Add the dependency to the runtime library for each of the sanitizer variants
699func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900700 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000701 if !c.Enabled() {
702 return
703 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900704 var sanitizers []string
705 var diagSanitizers []string
706
707 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
708 sanitizers = append(sanitizers, "undefined")
709 } else {
710 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
711 sanitizers = append(sanitizers,
712 "bool",
713 "integer-divide-by-zero",
714 "return",
715 "returns-nonnull-attribute",
716 "shift-exponent",
717 "unreachable",
718 "vla-bound",
719 // TODO(danalbert): The following checks currently have compiler performance issues.
720 //"alignment",
721 //"bounds",
722 //"enum",
723 //"float-cast-overflow",
724 //"float-divide-by-zero",
725 //"nonnull-attribute",
726 //"null",
727 //"shift-base",
728 //"signed-integer-overflow",
729 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
730 // https://llvm.org/PR19302
731 // http://reviews.llvm.org/D6974
732 // "object-size",
733 )
734 }
735 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
736 }
737
738 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
739 diagSanitizers = append(diagSanitizers, "undefined")
740 }
741
742 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
743
744 if Bool(c.sanitize.Properties.Sanitize.Address) {
745 sanitizers = append(sanitizers, "address")
746 diagSanitizers = append(diagSanitizers, "address")
747 }
748
749 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
750 sanitizers = append(sanitizers, "hwaddress")
751 }
752
753 if Bool(c.sanitize.Properties.Sanitize.Thread) {
754 sanitizers = append(sanitizers, "thread")
755 }
756
757 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
758 sanitizers = append(sanitizers, "safe-stack")
759 }
760
761 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
762 sanitizers = append(sanitizers, "cfi")
763
764 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
765 diagSanitizers = append(diagSanitizers, "cfi")
766 }
767 }
768
769 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
770 sanitizers = append(sanitizers, "unsigned-integer-overflow")
771 sanitizers = append(sanitizers, "signed-integer-overflow")
772 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
773 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
774 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
775 }
776 }
777
778 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
779 sanitizers = append(sanitizers, "scudo")
780 }
781
782 if Bool(c.sanitize.Properties.Sanitize.Scs) {
783 sanitizers = append(sanitizers, "shadow-call-stack")
784 }
785
786 // Save the list of sanitizers. These will be used again when generating
787 // the build rules (for Cflags, etc.)
788 c.sanitize.Properties.Sanitizers = sanitizers
789 c.sanitize.Properties.DiagSanitizers = diagSanitizers
790
791 // Determine the runtime library required
792 runtimeLibrary := ""
793 toolchain := c.toolchain(mctx)
794 if Bool(c.sanitize.Properties.Sanitize.Address) {
795 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
796 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
797 if c.staticBinary() {
798 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
799 } else {
800 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
801 }
802 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
803 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
804 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
805 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
806 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
807 } else {
808 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
809 }
810 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
811 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
812 }
813
814 if mctx.Device() && runtimeLibrary != "" {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900815 if inList(runtimeLibrary, llndkLibraries) && !c.static() && c.useVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900816 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
817 }
818
819 // Adding dependency to the runtime library. We are using *FarVariation*
820 // because the runtime libraries themselves are not mutated by sanitizer
821 // mutators and thus don't have sanitizer variants whereas this module
822 // has been already mutated.
823 //
824 // Note that by adding dependency with {static|shared}DepTag, the lib is
825 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
826 if c.staticBinary() {
827 // static executable gets static runtime libs
828 mctx.AddFarVariationDependencies([]blueprint.Variation{
829 {Mutator: "link", Variation: "static"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900830 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900831 {Mutator: "arch", Variation: mctx.Target().String()},
832 }, staticDepTag, runtimeLibrary)
833 } else if !c.static() {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900834 // dynamic executable and shared libs get shared runtime libs
Jiyong Park379de2f2018-12-19 02:47:14 +0900835 mctx.AddFarVariationDependencies([]blueprint.Variation{
836 {Mutator: "link", Variation: "shared"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900837 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900838 {Mutator: "arch", Variation: mctx.Target().String()},
Jiyong Park64a44f22019-01-18 14:37:08 +0900839 }, earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900840 }
841 // static lib does not have dependency to the runtime library. The
842 // dependency will be added to the executables or shared libs using
843 // the static lib.
844 }
845 }
846}
847
848type Sanitizeable interface {
849 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900850 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900851 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900852}
853
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000854// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700855func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
856 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000857 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000858 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900859 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700860 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000861 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
862 // Save original sanitizer status before we assign values to variant
863 // 0 as that overwrites the original.
864 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
865
Jiyong Park82226632019-02-01 10:50:50 +0900866 modules := mctx.CreateVariations("", t.variationName())
Colin Crossb0f28952016-09-19 16:46:53 -0700867 modules[0].(*Module).sanitize.SetSanitizer(t, false)
868 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000869
Colin Crossb0f28952016-09-19 16:46:53 -0700870 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
871 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800872
873 // We don't need both variants active for anything but CFI-enabled
874 // target static libraries, so suppress the appropriate variant in
875 // all other cases.
876 if t == cfi {
877 if c.static() {
878 if !mctx.Device() {
879 if isSanitizerEnabled {
880 modules[0].(*Module).Properties.PreventInstall = true
881 modules[0].(*Module).Properties.HideFromMake = true
882 } else {
883 modules[1].(*Module).Properties.PreventInstall = true
884 modules[1].(*Module).Properties.HideFromMake = true
885 }
886 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800887 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800888
889 cfiStaticLibsMutex.Lock()
890 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
891 cfiStaticLibsMutex.Unlock()
892 }
893 } else {
894 modules[0].(*Module).Properties.PreventInstall = true
895 modules[0].(*Module).Properties.HideFromMake = true
896 }
897 } else if t == asan {
898 if mctx.Device() {
899 // CFI and ASAN are currently mutually exclusive so disable
900 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000901 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
902 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
903 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700904 if isSanitizerEnabled {
905 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800906 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700907 } else {
908 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800909 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700910 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800911 } else if t == scs {
912 // We don't currently link any static libraries built with make into
913 // libraries built with SCS, so we don't need logic for propagating
914 // SCSness of dependencies into make.
915 if !c.static() {
916 if isSanitizerEnabled {
917 modules[0].(*Module).Properties.PreventInstall = true
918 modules[0].(*Module).Properties.HideFromMake = true
919 } else {
920 modules[1].(*Module).Properties.PreventInstall = true
921 modules[1].(*Module).Properties.HideFromMake = true
922 }
923 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700924 } else if t == hwasan {
925 if mctx.Device() {
926 // CFI and HWASAN are currently mutually exclusive so disable
927 // CFI if this is an HWASAN variant.
928 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
929 }
930
931 if c.static() {
932 if c.useVndk() {
933 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
934 hwasanStaticLibsMutex.Lock()
935 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
936 hwasanStaticLibsMutex.Unlock()
937 } else {
938 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
939 hwasanStaticLibsMutex.Lock()
940 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
941 hwasanStaticLibsMutex.Unlock()
942 }
943 } else {
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 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700952 }
Colin Cross16b23492016-01-06 14:41:07 -0800953 }
954 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +0900955 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900956 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +0900957 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -0800958 }
959 }
960}
Vishwath Mohane7128792017-11-17 11:08:10 -0800961
Colin Cross571cccf2019-02-04 11:22:08 -0800962var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
963
Vishwath Mohane7128792017-11-17 11:08:10 -0800964func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800965 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -0800966 return &[]string{}
967 }).(*[]string)
968}
969
Colin Cross571cccf2019-02-04 11:22:08 -0800970var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
971
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700972func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800973 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700974 return &[]string{}
975 }).(*[]string)
976}
977
Colin Cross571cccf2019-02-04 11:22:08 -0800978var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
979
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700980func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800981 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700982 return &[]string{}
983 }).(*[]string)
984}
985
Ivan Lozano30c5db22018-02-21 15:49:20 -0800986func enableMinimalRuntime(sanitize *sanitize) bool {
987 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700988 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -0800989 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
990 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
991 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
992 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
993 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
994 return true
995 }
996 return false
997}
998
Vishwath Mohane7128792017-11-17 11:08:10 -0800999func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1000 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001001 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001002 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1003}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001004
1005func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1006 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1007 sort.Strings(*hwasanStaticLibs)
1008 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1009
1010 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1011 sort.Strings(*hwasanVendorStaticLibs)
1012 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1013}