blob: 26418addcabfec8b086980c9626b8e9034bb3f6f [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",
42 "-mllvm", "-hwasan-allow-ifunc"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070043
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000044 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070045 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070046 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
47 // used, but have no effect on assembly files
48 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070049 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070050 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070051 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
52 cfiStaticLibsMutex sync.Mutex
53 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070054
Evgenii Stepanov98f5b062018-11-29 15:12:51 -080055 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080056
Peter Collingbournebd19db02019-03-06 10:38:48 -080057 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070058 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov3c5a52a2018-12-18 17:02:44 -080059 hwasanGlobalOptions = []string{"heap_history_size=1023,stack_history_size=512"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070060)
61
Colin Cross16b23492016-01-06 14:41:07 -080062type sanitizerType int
63
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070064func boolPtr(v bool) *bool {
65 if v {
66 return &v
67 } else {
68 return nil
69 }
70}
71
Colin Cross16b23492016-01-06 14:41:07 -080072const (
73 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070074 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080075 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070076 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000077 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080078 scs
Colin Cross16b23492016-01-06 14:41:07 -080079)
80
Jiyong Park82226632019-02-01 10:50:50 +090081// Name of the sanitizer variation for this sanitizer type
82func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080083 switch t {
84 case asan:
85 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070086 case hwasan:
87 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080088 case tsan:
89 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070090 case intOverflow:
91 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000092 case cfi:
93 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080094 case scs:
95 return "scs"
Colin Cross16b23492016-01-06 14:41:07 -080096 default:
97 panic(fmt.Errorf("unknown sanitizerType %d", t))
98 }
99}
100
Jiyong Park82226632019-02-01 10:50:50 +0900101// This is the sanitizer names in SANITIZE_[TARGET|HOST]
102func (t sanitizerType) name() string {
103 switch t {
104 case asan:
105 return "address"
106 case hwasan:
107 return "hwaddress"
108 case tsan:
109 return "thread"
110 case intOverflow:
111 return "integer_overflow"
112 case cfi:
113 return "cfi"
114 case scs:
115 return "shadow-call-stack"
116 default:
117 panic(fmt.Errorf("unknown sanitizerType %d", t))
118 }
119}
120
Colin Cross16b23492016-01-06 14:41:07 -0800121type SanitizeProperties struct {
122 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
123 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800124 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800125
126 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700127 Address *bool `android:"arch_variant"`
128 Thread *bool `android:"arch_variant"`
129 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800130
131 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700132 Undefined *bool `android:"arch_variant"`
133 All_undefined *bool `android:"arch_variant"`
134 Misc_undefined []string `android:"arch_variant"`
135 Coverage *bool `android:"arch_variant"`
136 Safestack *bool `android:"arch_variant"`
137 Cfi *bool `android:"arch_variant"`
138 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700139 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800140 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800141
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700142 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
143 // Replaces abort() on error with a human-readable error message.
144 // Address and Thread sanitizers always run in diagnostic mode.
145 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700146 Undefined *bool `android:"arch_variant"`
147 Cfi *bool `android:"arch_variant"`
148 Integer_overflow *bool `android:"arch_variant"`
149 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800150 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700151 }
152
153 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800154 Recover []string
155
156 // value to pass to -fsanitize-blacklist
157 Blacklist *string
158 } `android:"arch_variant"`
159
Jiyong Park379de2f2018-12-19 02:47:14 +0900160 SanitizerEnabled bool `blueprint:"mutated"`
161 SanitizeDep bool `blueprint:"mutated"`
162 MinimalRuntimeDep bool `blueprint:"mutated"`
163 UbsanRuntimeDep bool `blueprint:"mutated"`
164 InSanitizerDir bool `blueprint:"mutated"`
165 Sanitizers []string `blueprint:"mutated"`
166 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800167}
168
169type sanitize struct {
170 Properties SanitizeProperties
171}
172
Vishwath Mohane7128792017-11-17 11:08:10 -0800173func init() {
174 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700175 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800176}
177
Colin Cross16b23492016-01-06 14:41:07 -0800178func (sanitize *sanitize) props() []interface{} {
179 return []interface{}{&sanitize.Properties}
180}
181
182func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700183 s := &sanitize.Properties.Sanitize
184
Colin Cross16b23492016-01-06 14:41:07 -0800185 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700186 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800187 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800188 }
189
Doug Hornc32c6b02019-01-17 14:44:05 -0800190 // Sanitizers do not work on Fuchsia yet.
191 if ctx.Fuchsia() {
192 s.Never = BoolPtr(true)
193 }
194
Colin Cross16b23492016-01-06 14:41:07 -0800195 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800196 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800197 return
198 }
199
Colin Cross16b23492016-01-06 14:41:07 -0800200 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700201 var globalSanitizersDiag []string
202
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700203 if ctx.Host() {
204 if !ctx.Windows() {
205 globalSanitizers = ctx.Config().SanitizeHost()
206 }
207 } else {
208 arches := ctx.Config().SanitizeDeviceArch()
209 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
210 globalSanitizers = ctx.Config().SanitizeDevice()
211 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800212 }
213 }
214
Colin Cross16b23492016-01-06 14:41:07 -0800215 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000216 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700217 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
218 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000219 }
Colin Cross16b23492016-01-06 14:41:07 -0800220
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700221 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
222 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000223 }
224
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800225 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
226 if s.Address == nil {
227 s.Address = boolPtr(true)
228 } else if *s.Address == false {
229 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
230 // disables address, then disable coverage as well.
231 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
232 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000233 }
234
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700235 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
236 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000237 }
238
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700239 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
240 s.Coverage = boolPtr(true)
241 }
242
243 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
244 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000245 }
246
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700247 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800248 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700249 s.Cfi = boolPtr(true)
250 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700251 }
252
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700253 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700254 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700255 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700256 s.Integer_overflow = boolPtr(true)
257 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700258 }
259
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700260 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
261 s.Scudo = boolPtr(true)
262 }
263
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700264 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
265 s.Hwaddress = boolPtr(true)
266 }
267
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000268 if len(globalSanitizers) > 0 {
269 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
270 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700271
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700272 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700273 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700274 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700275 s.Diag.Integer_overflow = boolPtr(true)
276 }
277
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700278 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
279 s.Diag.Cfi == nil && Bool(s.Cfi) {
280 s.Diag.Cfi = boolPtr(true)
281 }
282
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700283 if len(globalSanitizersDiag) > 0 {
284 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
285 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700286 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700287
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700288 // Enable CFI for all components in the include paths (for Aarch64 only)
289 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000290 s.Cfi = boolPtr(true)
291 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
292 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700293 }
294 }
295
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800296 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800297 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800298 s.Cfi = nil
299 s.Diag.Cfi = nil
300 }
301
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800302 // Also disable CFI for arm32 until b/35157333 is fixed.
303 if ctx.Arch().ArchType == android.Arm {
304 s.Cfi = nil
305 s.Diag.Cfi = nil
306 }
307
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700308 // HWASan requires AArch64 hardware feature (top-byte-ignore).
309 if ctx.Arch().ArchType != android.Arm64 {
310 s.Hwaddress = nil
311 }
312
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800313 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800314 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800315 s.Scs = nil
316 }
317
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700318 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700319 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700320 s.Cfi = nil
321 s.Diag.Cfi = nil
322 }
323
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700324 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800325 if ctx.Host() {
326 s.Cfi = nil
327 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700328 s.Misc_undefined = nil
329 s.Undefined = nil
330 s.All_undefined = nil
331 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800332 }
333
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700334 // Also disable CFI for VNDK variants of components
335 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700336 s.Cfi = nil
337 s.Diag.Cfi = nil
338 }
339
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700340 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800341 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
342 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700343 s.Hwaddress = nil
344 }
345
Colin Cross3c344ef2016-07-18 15:44:56 -0700346 if ctx.staticBinary() {
347 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700348 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700349 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800350 }
351
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700352 if Bool(s.All_undefined) {
353 s.Undefined = nil
354 }
355
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700356 if !ctx.toolchain().Is64Bit() {
357 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700358 s.Thread = nil
359 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800360 // TODO(ccross): error for compile_multilib = "32"?
361 }
362
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800363 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 -0700364 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 -0800365 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700366 sanitize.Properties.SanitizerEnabled = true
367 }
368
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800369 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
370 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700371 s.Scudo = nil
372 }
373
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700374 if Bool(s.Hwaddress) {
375 s.Address = nil
376 s.Thread = nil
377 }
378
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700379 if Bool(s.Coverage) {
380 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800381 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
382 }
383 }
384}
385
386func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
387 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
388 return deps
389 }
390
391 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700392 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700393 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800394 }
395 }
396
397 return deps
398}
399
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800400func toDisableImplicitIntegerChange(flags []string) bool {
401 // Returns true if any flag is fsanitize*integer, and there is
402 // no explicit flag about sanitize=implicit-integer-sign-change.
403 for _, f := range flags {
404 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
405 return false
406 }
407 }
408 for _, f := range flags {
409 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
410 return true
411 }
412 }
413 return false
414}
415
Colin Cross16b23492016-01-06 14:41:07 -0800416func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700417 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
418 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800419
420 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
421 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700422 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800423 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700424 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800425 return flags
426 }
427
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700428 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700429 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800430 // Frame pointer based unwinder in ASan requires ARM frame setup.
431 // TODO: put in flags?
432 flags.RequiredInstructionSet = "arm"
433 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700434 flags.CFlags = append(flags.CFlags, asanCflags...)
435 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800436
Colin Cross16b23492016-01-06 14:41:07 -0800437 if ctx.Host() {
438 // -nodefaultlibs (provided with libc++) prevents the driver from linking
439 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800440 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
441 } else {
442 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900443 if ctx.bootstrap() {
444 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
445 } else {
446 flags.DynamicLinker = "/system/bin/linker_asan"
447 }
Colin Cross16b23492016-01-06 14:41:07 -0800448 if flags.Toolchain.Is64Bit() {
449 flags.DynamicLinker += "64"
450 }
451 }
Colin Cross16b23492016-01-06 14:41:07 -0800452 }
453
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700454 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
455 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700456 }
457
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700458 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400459 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800460 }
461
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700462 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800463 if ctx.Arch().ArchType == android.Arm {
464 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
465 // to do this on a function basis, so force Thumb on the entire module.
466 flags.RequiredInstructionSet = "thumb"
467 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000468
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700469 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700470 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000471 // Only append the default visibility flag if -fvisibility has not already been set
472 // to hidden.
473 if !inList("-fvisibility=hidden", flags.CFlags) {
474 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
475 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700476 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000477
478 if ctx.staticBinary() {
479 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
480 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
481 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700482 }
483
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700484 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700485 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700486 }
487
Jiyong Park379de2f2018-12-19 02:47:14 +0900488 if len(sanitize.Properties.Sanitizers) > 0 {
489 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800490
Colin Cross16b23492016-01-06 14:41:07 -0800491 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700492 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800493 if ctx.Host() {
494 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
495 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800496 // Host sanitizers only link symbols in the final executable, so
497 // there will always be undefined symbols in intermediate libraries.
498 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800499 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700500 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800501
502 if enableMinimalRuntime(sanitize) {
503 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
504 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700505 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800506 }
Colin Cross16b23492016-01-06 14:41:07 -0800507 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800508 // http://b/119329758, Android core does not boot up with this sanitizer yet.
509 if toDisableImplicitIntegerChange(flags.CFlags) {
510 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
511 }
Colin Cross16b23492016-01-06 14:41:07 -0800512 }
513
Jiyong Park379de2f2018-12-19 02:47:14 +0900514 if len(sanitize.Properties.DiagSanitizers) > 0 {
515 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700516 }
517 // FIXME: enable RTTI if diag + (cfi or vptr)
518
Andreas Gampe97071162017-05-08 13:15:23 -0700519 if sanitize.Properties.Sanitize.Recover != nil {
520 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
521 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
522 }
523
Ivan Lozano7929bba2018-12-12 09:36:31 -0800524 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
525 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
526 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
527 }
528
Colin Cross635c3b02016-05-18 15:37:25 -0700529 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800530 if blacklist.Valid() {
531 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
532 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
533 }
534
535 return flags
536}
537
Colin Cross8ff9ef42017-05-08 13:44:11 -0700538func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Vishwath Mohane7128792017-11-17 11:08:10 -0800539 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
540 // name conflict.
541 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
542 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000543 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700544 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
545 ret.SubName += ".hwasan"
546 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800547 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
548 ret.SubName += ".scs"
549 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700550}
551
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700552func (sanitize *sanitize) inSanitizerDir() bool {
553 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700554}
555
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000556func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000557 switch t {
558 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000559 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700560 case hwasan:
561 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000562 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000563 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000564 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000565 return sanitize.Properties.Sanitize.Integer_overflow
566 case cfi:
567 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800568 case scs:
569 return sanitize.Properties.Sanitize.Scs
Vishwath Mohan95229302017-08-11 00:53:16 +0000570 default:
571 panic(fmt.Errorf("unknown sanitizerType %d", t))
572 }
573}
574
Dan Albert7d1eecf2018-01-19 12:30:45 -0800575func (sanitize *sanitize) isUnsanitizedVariant() bool {
576 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700577 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800578 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800579 !sanitize.isSanitizerEnabled(cfi) &&
580 !sanitize.isSanitizerEnabled(scs)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800581}
582
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700583func (sanitize *sanitize) isVariantOnProductionDevice() bool {
584 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700585 !sanitize.isSanitizerEnabled(hwasan) &&
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700586 !sanitize.isSanitizerEnabled(tsan)
587}
588
Colin Cross16b23492016-01-06 14:41:07 -0800589func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
590 switch t {
591 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700592 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700593 if !b {
594 sanitize.Properties.Sanitize.Coverage = nil
595 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700596 case hwasan:
597 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800598 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700599 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700600 case intOverflow:
601 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000602 case cfi:
603 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800604 case scs:
605 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800606 default:
607 panic(fmt.Errorf("unknown sanitizerType %d", t))
608 }
609 if b {
610 sanitize.Properties.SanitizerEnabled = true
611 }
612}
613
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000614// Check if the sanitizer is explicitly disabled (as opposed to nil by
615// virtue of not being set).
616func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
617 if sanitize == nil {
618 return false
619 }
620
621 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
622 return sanitizerVal != nil && *sanitizerVal == false
623}
624
625// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
626// because enabling a sanitizer either directly (via the blueprint) or
627// indirectly (via a mutator) sets the bool ptr to true, and you can't
628// distinguish between the cases. It isn't needed though - both cases can be
629// treated identically.
630func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
631 if sanitize == nil {
632 return false
633 }
634
635 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
636 return sanitizerVal != nil && *sanitizerVal == true
637}
638
Colin Cross6b753602018-06-21 13:03:07 -0700639func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
640 t, ok := tag.(dependencyTag)
641 return ok && t.library || t == reuseObjTag
642}
643
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700644// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700645func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
646 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000647 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700648 mctx.WalkDeps(func(child, parent android.Module) bool {
649 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
650 return false
651 }
652 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800653 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000654 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800655 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700656 if d.static() {
657 d.sanitize.Properties.SanitizeDep = true
658 }
659 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000660 d.sanitize.Properties.SanitizeDep = true
661 }
Colin Cross16b23492016-01-06 14:41:07 -0800662 }
Colin Cross6b753602018-06-21 13:03:07 -0700663 return true
Colin Cross16b23492016-01-06 14:41:07 -0800664 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900665 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
666 // If an APEX module includes a lib which is enabled for a sanitizer T, then
667 // the APEX module is also enabled for the same sanitizer type.
668 mctx.VisitDirectDeps(func(child android.Module) {
669 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
670 sanitizeable.EnableSanitizer(t.name())
671 }
672 })
Colin Cross16b23492016-01-06 14:41:07 -0800673 }
674 }
675}
676
Ivan Lozano30c5db22018-02-21 15:49:20 -0800677// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700678func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
679 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
680 mctx.WalkDeps(func(child, parent android.Module) bool {
681 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
682 return false
683 }
684 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800685
Colin Cross6b753602018-06-21 13:03:07 -0700686 if enableMinimalRuntime(d.sanitize) {
687 // If a static dependency is built with the minimal runtime,
688 // make sure we include the ubsan minimal runtime.
689 c.sanitize.Properties.MinimalRuntimeDep = true
690 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
691 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
692 // If a static dependency runs with full ubsan diagnostics,
693 // make sure we include the ubsan runtime.
694 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800695 }
Colin Cross6b753602018-06-21 13:03:07 -0700696 }
697 return true
698 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800699 }
700}
701
Jiyong Park379de2f2018-12-19 02:47:14 +0900702// Add the dependency to the runtime library for each of the sanitizer variants
703func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900704 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000705 if !c.Enabled() {
706 return
707 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900708 var sanitizers []string
709 var diagSanitizers []string
710
711 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
712 sanitizers = append(sanitizers, "undefined")
713 } else {
714 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
715 sanitizers = append(sanitizers,
716 "bool",
717 "integer-divide-by-zero",
718 "return",
719 "returns-nonnull-attribute",
720 "shift-exponent",
721 "unreachable",
722 "vla-bound",
723 // TODO(danalbert): The following checks currently have compiler performance issues.
724 //"alignment",
725 //"bounds",
726 //"enum",
727 //"float-cast-overflow",
728 //"float-divide-by-zero",
729 //"nonnull-attribute",
730 //"null",
731 //"shift-base",
732 //"signed-integer-overflow",
733 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
734 // https://llvm.org/PR19302
735 // http://reviews.llvm.org/D6974
736 // "object-size",
737 )
738 }
739 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
740 }
741
742 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
743 diagSanitizers = append(diagSanitizers, "undefined")
744 }
745
746 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
747
748 if Bool(c.sanitize.Properties.Sanitize.Address) {
749 sanitizers = append(sanitizers, "address")
750 diagSanitizers = append(diagSanitizers, "address")
751 }
752
753 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
754 sanitizers = append(sanitizers, "hwaddress")
755 }
756
757 if Bool(c.sanitize.Properties.Sanitize.Thread) {
758 sanitizers = append(sanitizers, "thread")
759 }
760
761 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
762 sanitizers = append(sanitizers, "safe-stack")
763 }
764
765 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
766 sanitizers = append(sanitizers, "cfi")
767
768 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
769 diagSanitizers = append(diagSanitizers, "cfi")
770 }
771 }
772
773 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
774 sanitizers = append(sanitizers, "unsigned-integer-overflow")
775 sanitizers = append(sanitizers, "signed-integer-overflow")
776 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
777 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
778 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
779 }
780 }
781
782 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
783 sanitizers = append(sanitizers, "scudo")
784 }
785
786 if Bool(c.sanitize.Properties.Sanitize.Scs) {
787 sanitizers = append(sanitizers, "shadow-call-stack")
788 }
789
790 // Save the list of sanitizers. These will be used again when generating
791 // the build rules (for Cflags, etc.)
792 c.sanitize.Properties.Sanitizers = sanitizers
793 c.sanitize.Properties.DiagSanitizers = diagSanitizers
794
795 // Determine the runtime library required
796 runtimeLibrary := ""
797 toolchain := c.toolchain(mctx)
798 if Bool(c.sanitize.Properties.Sanitize.Address) {
799 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
800 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
801 if c.staticBinary() {
802 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
803 } else {
804 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
805 }
806 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
807 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
808 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
809 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
810 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
811 } else {
812 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
813 }
814 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
815 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
816 }
817
818 if mctx.Device() && runtimeLibrary != "" {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900819 if inList(runtimeLibrary, llndkLibraries) && !c.static() && c.useVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900820 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
821 }
822
823 // Adding dependency to the runtime library. We are using *FarVariation*
824 // because the runtime libraries themselves are not mutated by sanitizer
825 // mutators and thus don't have sanitizer variants whereas this module
826 // has been already mutated.
827 //
828 // Note that by adding dependency with {static|shared}DepTag, the lib is
829 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
830 if c.staticBinary() {
831 // static executable gets static runtime libs
832 mctx.AddFarVariationDependencies([]blueprint.Variation{
833 {Mutator: "link", Variation: "static"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900834 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900835 {Mutator: "arch", Variation: mctx.Target().String()},
836 }, staticDepTag, runtimeLibrary)
837 } else if !c.static() {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900838 // dynamic executable and shared libs get shared runtime libs
Jiyong Park379de2f2018-12-19 02:47:14 +0900839 mctx.AddFarVariationDependencies([]blueprint.Variation{
840 {Mutator: "link", Variation: "shared"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900841 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900842 {Mutator: "arch", Variation: mctx.Target().String()},
Jiyong Park64a44f22019-01-18 14:37:08 +0900843 }, earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900844 }
845 // static lib does not have dependency to the runtime library. The
846 // dependency will be added to the executables or shared libs using
847 // the static lib.
848 }
849 }
850}
851
852type Sanitizeable interface {
853 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900854 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900855 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900856}
857
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000858// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700859func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
860 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000861 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000862 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900863 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700864 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000865 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
866 // Save original sanitizer status before we assign values to variant
867 // 0 as that overwrites the original.
868 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
869
Jiyong Park82226632019-02-01 10:50:50 +0900870 modules := mctx.CreateVariations("", t.variationName())
Colin Crossb0f28952016-09-19 16:46:53 -0700871 modules[0].(*Module).sanitize.SetSanitizer(t, false)
872 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000873
Colin Crossb0f28952016-09-19 16:46:53 -0700874 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
875 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800876
877 // We don't need both variants active for anything but CFI-enabled
878 // target static libraries, so suppress the appropriate variant in
879 // all other cases.
880 if t == cfi {
881 if c.static() {
882 if !mctx.Device() {
883 if isSanitizerEnabled {
884 modules[0].(*Module).Properties.PreventInstall = true
885 modules[0].(*Module).Properties.HideFromMake = true
886 } else {
887 modules[1].(*Module).Properties.PreventInstall = true
888 modules[1].(*Module).Properties.HideFromMake = true
889 }
890 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800891 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800892
893 cfiStaticLibsMutex.Lock()
894 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
895 cfiStaticLibsMutex.Unlock()
896 }
897 } else {
898 modules[0].(*Module).Properties.PreventInstall = true
899 modules[0].(*Module).Properties.HideFromMake = true
900 }
901 } else if t == asan {
902 if mctx.Device() {
903 // CFI and ASAN are currently mutually exclusive so disable
904 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000905 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
906 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
907 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700908 if isSanitizerEnabled {
909 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800910 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700911 } else {
912 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800913 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700914 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800915 } else if t == scs {
916 // We don't currently link any static libraries built with make into
917 // libraries built with SCS, so we don't need logic for propagating
918 // SCSness of dependencies into make.
919 if !c.static() {
920 if isSanitizerEnabled {
921 modules[0].(*Module).Properties.PreventInstall = true
922 modules[0].(*Module).Properties.HideFromMake = true
923 } else {
924 modules[1].(*Module).Properties.PreventInstall = true
925 modules[1].(*Module).Properties.HideFromMake = true
926 }
927 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700928 } else if t == hwasan {
929 if mctx.Device() {
930 // CFI and HWASAN are currently mutually exclusive so disable
931 // CFI if this is an HWASAN variant.
932 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
933 }
934
935 if c.static() {
936 if c.useVndk() {
937 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
938 hwasanStaticLibsMutex.Lock()
939 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
940 hwasanStaticLibsMutex.Unlock()
941 } else {
942 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
943 hwasanStaticLibsMutex.Lock()
944 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
945 hwasanStaticLibsMutex.Unlock()
946 }
947 } else {
948 if isSanitizerEnabled {
949 modules[0].(*Module).Properties.PreventInstall = true
950 modules[0].(*Module).Properties.HideFromMake = true
951 } else {
952 modules[1].(*Module).Properties.PreventInstall = true
953 modules[1].(*Module).Properties.HideFromMake = true
954 }
955 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700956 }
Colin Cross16b23492016-01-06 14:41:07 -0800957 }
958 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +0900959 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900960 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +0900961 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -0800962 }
963 }
964}
Vishwath Mohane7128792017-11-17 11:08:10 -0800965
Colin Cross571cccf2019-02-04 11:22:08 -0800966var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
967
Vishwath Mohane7128792017-11-17 11:08:10 -0800968func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800969 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -0800970 return &[]string{}
971 }).(*[]string)
972}
973
Colin Cross571cccf2019-02-04 11:22:08 -0800974var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
975
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700976func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800977 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700978 return &[]string{}
979 }).(*[]string)
980}
981
Colin Cross571cccf2019-02-04 11:22:08 -0800982var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
983
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700984func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800985 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700986 return &[]string{}
987 }).(*[]string)
988}
989
Ivan Lozano30c5db22018-02-21 15:49:20 -0800990func enableMinimalRuntime(sanitize *sanitize) bool {
991 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700992 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -0800993 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
994 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
995 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
996 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
997 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
998 return true
999 }
1000 return false
1001}
1002
Vishwath Mohane7128792017-11-17 11:08:10 -08001003func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1004 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001005 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001006 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1007}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001008
1009func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1010 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1011 sort.Strings(*hwasanStaticLibs)
1012 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1013
1014 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1015 sort.Strings(*hwasanVendorStaticLibs)
1016 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1017}