blob: 729771839956d3fb45b598ce06eefe48a1778c97 [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
53 // Pass -Xclang before -fsanitize-minimal-runtime to work around a driver
54 // check which rejects -fsanitize-minimal-runtime together with
55 // -fsanitize=shadow-call-stack even though this combination of flags
56 // is valid.
57 // TODO(pcc): Remove the -Xclang once LLVM r346526 is rolled into the compiler.
58 minimalRuntimeFlags = []string{"-Xclang", "-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070059 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov3c5a52a2018-12-18 17:02:44 -080060 hwasanGlobalOptions = []string{"heap_history_size=1023,stack_history_size=512"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070061)
62
Colin Cross16b23492016-01-06 14:41:07 -080063type sanitizerType int
64
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070065func boolPtr(v bool) *bool {
66 if v {
67 return &v
68 } else {
69 return nil
70 }
71}
72
Colin Cross16b23492016-01-06 14:41:07 -080073const (
74 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070075 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080076 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070077 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000078 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080079 scs
Colin Cross16b23492016-01-06 14:41:07 -080080)
81
Jiyong Park82226632019-02-01 10:50:50 +090082// Name of the sanitizer variation for this sanitizer type
83func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080084 switch t {
85 case asan:
86 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070087 case hwasan:
88 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080089 case tsan:
90 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070091 case intOverflow:
92 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000093 case cfi:
94 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080095 case scs:
96 return "scs"
Colin Cross16b23492016-01-06 14:41:07 -080097 default:
98 panic(fmt.Errorf("unknown sanitizerType %d", t))
99 }
100}
101
Jiyong Park82226632019-02-01 10:50:50 +0900102// This is the sanitizer names in SANITIZE_[TARGET|HOST]
103func (t sanitizerType) name() string {
104 switch t {
105 case asan:
106 return "address"
107 case hwasan:
108 return "hwaddress"
109 case tsan:
110 return "thread"
111 case intOverflow:
112 return "integer_overflow"
113 case cfi:
114 return "cfi"
115 case scs:
116 return "shadow-call-stack"
117 default:
118 panic(fmt.Errorf("unknown sanitizerType %d", t))
119 }
120}
121
Colin Cross16b23492016-01-06 14:41:07 -0800122type SanitizeProperties struct {
123 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
124 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800125 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800126
127 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700128 Address *bool `android:"arch_variant"`
129 Thread *bool `android:"arch_variant"`
130 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800131
132 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700133 Undefined *bool `android:"arch_variant"`
134 All_undefined *bool `android:"arch_variant"`
135 Misc_undefined []string `android:"arch_variant"`
136 Coverage *bool `android:"arch_variant"`
137 Safestack *bool `android:"arch_variant"`
138 Cfi *bool `android:"arch_variant"`
139 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700140 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800141 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800142
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700143 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
144 // Replaces abort() on error with a human-readable error message.
145 // Address and Thread sanitizers always run in diagnostic mode.
146 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700147 Undefined *bool `android:"arch_variant"`
148 Cfi *bool `android:"arch_variant"`
149 Integer_overflow *bool `android:"arch_variant"`
150 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800151 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700152 }
153
154 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800155 Recover []string
156
157 // value to pass to -fsanitize-blacklist
158 Blacklist *string
159 } `android:"arch_variant"`
160
Jiyong Park379de2f2018-12-19 02:47:14 +0900161 SanitizerEnabled bool `blueprint:"mutated"`
162 SanitizeDep bool `blueprint:"mutated"`
163 MinimalRuntimeDep bool `blueprint:"mutated"`
164 UbsanRuntimeDep bool `blueprint:"mutated"`
165 InSanitizerDir bool `blueprint:"mutated"`
166 Sanitizers []string `blueprint:"mutated"`
167 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800168}
169
170type sanitize struct {
171 Properties SanitizeProperties
172}
173
Vishwath Mohane7128792017-11-17 11:08:10 -0800174func init() {
175 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700176 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800177}
178
Colin Cross16b23492016-01-06 14:41:07 -0800179func (sanitize *sanitize) props() []interface{} {
180 return []interface{}{&sanitize.Properties}
181}
182
183func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700184 s := &sanitize.Properties.Sanitize
185
Colin Cross16b23492016-01-06 14:41:07 -0800186 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700187 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800188 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800189 }
190
Doug Hornc32c6b02019-01-17 14:44:05 -0800191 // Sanitizers do not work on Fuchsia yet.
192 if ctx.Fuchsia() {
193 s.Never = BoolPtr(true)
194 }
195
Colin Cross16b23492016-01-06 14:41:07 -0800196 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800197 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800198 return
199 }
200
Colin Cross16b23492016-01-06 14:41:07 -0800201 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700202 var globalSanitizersDiag []string
203
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700204 if ctx.Host() {
205 if !ctx.Windows() {
206 globalSanitizers = ctx.Config().SanitizeHost()
207 }
208 } else {
209 arches := ctx.Config().SanitizeDeviceArch()
210 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
211 globalSanitizers = ctx.Config().SanitizeDevice()
212 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800213 }
214 }
215
Colin Cross16b23492016-01-06 14:41:07 -0800216 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000217 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700218 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
219 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000220 }
Colin Cross16b23492016-01-06 14:41:07 -0800221
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700222 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
223 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000224 }
225
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800226 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
227 if s.Address == nil {
228 s.Address = boolPtr(true)
229 } else if *s.Address == false {
230 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
231 // disables address, then disable coverage as well.
232 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
233 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000234 }
235
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700236 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
237 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000238 }
239
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700240 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
241 s.Coverage = boolPtr(true)
242 }
243
244 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
245 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000246 }
247
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700248 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800249 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700250 s.Cfi = boolPtr(true)
251 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700252 }
253
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700254 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700255 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700256 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700257 s.Integer_overflow = boolPtr(true)
258 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700259 }
260
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700261 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
262 s.Scudo = boolPtr(true)
263 }
264
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700265 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
266 s.Hwaddress = boolPtr(true)
267 }
268
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000269 if len(globalSanitizers) > 0 {
270 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
271 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700272
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700273 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700274 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700275 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700276 s.Diag.Integer_overflow = boolPtr(true)
277 }
278
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700279 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
280 s.Diag.Cfi == nil && Bool(s.Cfi) {
281 s.Diag.Cfi = boolPtr(true)
282 }
283
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700284 if len(globalSanitizersDiag) > 0 {
285 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
286 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700287 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700288
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700289 // Enable CFI for all components in the include paths (for Aarch64 only)
290 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000291 s.Cfi = boolPtr(true)
292 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
293 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700294 }
295 }
296
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800297 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800298 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800299 s.Cfi = nil
300 s.Diag.Cfi = nil
301 }
302
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800303 // Also disable CFI for arm32 until b/35157333 is fixed.
304 if ctx.Arch().ArchType == android.Arm {
305 s.Cfi = nil
306 s.Diag.Cfi = nil
307 }
308
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700309 // HWASan requires AArch64 hardware feature (top-byte-ignore).
310 if ctx.Arch().ArchType != android.Arm64 {
311 s.Hwaddress = nil
312 }
313
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800314 // SCS is only implemented on AArch64.
315 // We also disable SCS if ASAN, TSAN or HWASAN are enabled because Clang considers
316 // them to be incompatible, although they are in fact compatible.
317 // TODO(pcc): Remove these checks once r347282 is rolled into the compiler.
318 if ctx.Arch().ArchType != android.Arm64 || Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) {
319 s.Scs = nil
320 }
321
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700322 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700323 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700324 s.Cfi = nil
325 s.Diag.Cfi = nil
326 }
327
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700328 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800329 if ctx.Host() {
330 s.Cfi = nil
331 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700332 s.Misc_undefined = nil
333 s.Undefined = nil
334 s.All_undefined = nil
335 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800336 }
337
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700338 // Also disable CFI for VNDK variants of components
339 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700340 s.Cfi = nil
341 s.Diag.Cfi = nil
342 }
343
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700344 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800345 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
346 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700347 s.Hwaddress = nil
348 }
349
Colin Cross3c344ef2016-07-18 15:44:56 -0700350 if ctx.staticBinary() {
351 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700352 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700353 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800354 }
355
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700356 if Bool(s.All_undefined) {
357 s.Undefined = nil
358 }
359
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700360 if !ctx.toolchain().Is64Bit() {
361 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700362 s.Thread = nil
363 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800364 // TODO(ccross): error for compile_multilib = "32"?
365 }
366
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800367 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 -0700368 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 -0800369 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700370 sanitize.Properties.SanitizerEnabled = true
371 }
372
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800373 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
374 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700375 s.Scudo = nil
376 }
377
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700378 if Bool(s.Hwaddress) {
379 s.Address = nil
380 s.Thread = nil
381 }
382
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700383 if Bool(s.Coverage) {
384 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800385 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
386 }
387 }
388}
389
390func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
391 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
392 return deps
393 }
394
395 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700396 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700397 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800398 }
399 }
400
401 return deps
402}
403
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800404func toDisableImplicitIntegerChange(flags []string) bool {
405 // Returns true if any flag is fsanitize*integer, and there is
406 // no explicit flag about sanitize=implicit-integer-sign-change.
407 for _, f := range flags {
408 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
409 return false
410 }
411 }
412 for _, f := range flags {
413 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
414 return true
415 }
416 }
417 return false
418}
419
Colin Cross16b23492016-01-06 14:41:07 -0800420func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700421 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
422 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800423
424 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
425 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700426 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800427 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700428 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800429 return flags
430 }
431
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700432 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700433 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800434 // Frame pointer based unwinder in ASan requires ARM frame setup.
435 // TODO: put in flags?
436 flags.RequiredInstructionSet = "arm"
437 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700438 flags.CFlags = append(flags.CFlags, asanCflags...)
439 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800440
Colin Cross16b23492016-01-06 14:41:07 -0800441 if ctx.Host() {
442 // -nodefaultlibs (provided with libc++) prevents the driver from linking
443 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800444 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
445 } else {
446 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900447 if ctx.bootstrap() {
448 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
449 } else {
450 flags.DynamicLinker = "/system/bin/linker_asan"
451 }
Colin Cross16b23492016-01-06 14:41:07 -0800452 if flags.Toolchain.Is64Bit() {
453 flags.DynamicLinker += "64"
454 }
455 }
Colin Cross16b23492016-01-06 14:41:07 -0800456 }
457
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700458 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
459 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700460 }
461
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700462 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400463 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800464 }
465
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700466 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800467 if ctx.Arch().ArchType == android.Arm {
468 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
469 // to do this on a function basis, so force Thumb on the entire module.
470 flags.RequiredInstructionSet = "thumb"
471 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000472
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700473 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700474 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000475 // Only append the default visibility flag if -fvisibility has not already been set
476 // to hidden.
477 if !inList("-fvisibility=hidden", flags.CFlags) {
478 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
479 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700480 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000481
482 if ctx.staticBinary() {
483 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
484 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
485 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700486 }
487
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700488 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700489 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700490 }
491
Jiyong Park379de2f2018-12-19 02:47:14 +0900492 if len(sanitize.Properties.Sanitizers) > 0 {
493 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800494
Colin Cross16b23492016-01-06 14:41:07 -0800495 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700496 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800497 if ctx.Host() {
498 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
499 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800500 // Host sanitizers only link symbols in the final executable, so
501 // there will always be undefined symbols in intermediate libraries.
502 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800503 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700504 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800505
506 if enableMinimalRuntime(sanitize) {
507 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
508 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700509 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800510 }
Colin Cross16b23492016-01-06 14:41:07 -0800511 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800512 // http://b/119329758, Android core does not boot up with this sanitizer yet.
513 if toDisableImplicitIntegerChange(flags.CFlags) {
514 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
515 }
Colin Cross16b23492016-01-06 14:41:07 -0800516 }
517
Jiyong Park379de2f2018-12-19 02:47:14 +0900518 if len(sanitize.Properties.DiagSanitizers) > 0 {
519 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700520 }
521 // FIXME: enable RTTI if diag + (cfi or vptr)
522
Andreas Gampe97071162017-05-08 13:15:23 -0700523 if sanitize.Properties.Sanitize.Recover != nil {
524 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
525 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
526 }
527
Ivan Lozano7929bba2018-12-12 09:36:31 -0800528 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
529 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
530 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
531 }
532
Colin Cross635c3b02016-05-18 15:37:25 -0700533 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800534 if blacklist.Valid() {
535 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
536 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
537 }
538
539 return flags
540}
541
Colin Cross8ff9ef42017-05-08 13:44:11 -0700542func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Vishwath Mohane7128792017-11-17 11:08:10 -0800543 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
544 // name conflict.
545 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
546 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000547 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700548 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
549 ret.SubName += ".hwasan"
550 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800551 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
552 ret.SubName += ".scs"
553 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700554}
555
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700556func (sanitize *sanitize) inSanitizerDir() bool {
557 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700558}
559
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000560func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000561 switch t {
562 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000563 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700564 case hwasan:
565 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000566 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000567 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000568 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000569 return sanitize.Properties.Sanitize.Integer_overflow
570 case cfi:
571 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800572 case scs:
573 return sanitize.Properties.Sanitize.Scs
Vishwath Mohan95229302017-08-11 00:53:16 +0000574 default:
575 panic(fmt.Errorf("unknown sanitizerType %d", t))
576 }
577}
578
Dan Albert7d1eecf2018-01-19 12:30:45 -0800579func (sanitize *sanitize) isUnsanitizedVariant() bool {
580 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700581 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800582 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800583 !sanitize.isSanitizerEnabled(cfi) &&
584 !sanitize.isSanitizerEnabled(scs)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800585}
586
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700587func (sanitize *sanitize) isVariantOnProductionDevice() bool {
588 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700589 !sanitize.isSanitizerEnabled(hwasan) &&
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700590 !sanitize.isSanitizerEnabled(tsan)
591}
592
Colin Cross16b23492016-01-06 14:41:07 -0800593func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
594 switch t {
595 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700596 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700597 if !b {
598 sanitize.Properties.Sanitize.Coverage = nil
599 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700600 case hwasan:
601 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800602 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700603 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700604 case intOverflow:
605 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000606 case cfi:
607 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800608 case scs:
609 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800610 default:
611 panic(fmt.Errorf("unknown sanitizerType %d", t))
612 }
613 if b {
614 sanitize.Properties.SanitizerEnabled = true
615 }
616}
617
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000618// Check if the sanitizer is explicitly disabled (as opposed to nil by
619// virtue of not being set).
620func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
621 if sanitize == nil {
622 return false
623 }
624
625 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
626 return sanitizerVal != nil && *sanitizerVal == false
627}
628
629// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
630// because enabling a sanitizer either directly (via the blueprint) or
631// indirectly (via a mutator) sets the bool ptr to true, and you can't
632// distinguish between the cases. It isn't needed though - both cases can be
633// treated identically.
634func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
635 if sanitize == nil {
636 return false
637 }
638
639 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
640 return sanitizerVal != nil && *sanitizerVal == true
641}
642
Colin Cross6b753602018-06-21 13:03:07 -0700643func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
644 t, ok := tag.(dependencyTag)
645 return ok && t.library || t == reuseObjTag
646}
647
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700648// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700649func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
650 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000651 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700652 mctx.WalkDeps(func(child, parent android.Module) bool {
653 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
654 return false
655 }
656 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800657 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000658 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800659 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700660 if d.static() {
661 d.sanitize.Properties.SanitizeDep = true
662 }
663 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000664 d.sanitize.Properties.SanitizeDep = true
665 }
Colin Cross16b23492016-01-06 14:41:07 -0800666 }
Colin Cross6b753602018-06-21 13:03:07 -0700667 return true
Colin Cross16b23492016-01-06 14:41:07 -0800668 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900669 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
670 // If an APEX module includes a lib which is enabled for a sanitizer T, then
671 // the APEX module is also enabled for the same sanitizer type.
672 mctx.VisitDirectDeps(func(child android.Module) {
673 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
674 sanitizeable.EnableSanitizer(t.name())
675 }
676 })
Colin Cross16b23492016-01-06 14:41:07 -0800677 }
678 }
679}
680
Ivan Lozano30c5db22018-02-21 15:49:20 -0800681// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700682func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
683 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
684 mctx.WalkDeps(func(child, parent android.Module) bool {
685 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
686 return false
687 }
688 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800689
Colin Cross6b753602018-06-21 13:03:07 -0700690 if enableMinimalRuntime(d.sanitize) {
691 // If a static dependency is built with the minimal runtime,
692 // make sure we include the ubsan minimal runtime.
693 c.sanitize.Properties.MinimalRuntimeDep = true
694 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
695 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
696 // If a static dependency runs with full ubsan diagnostics,
697 // make sure we include the ubsan runtime.
698 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800699 }
Colin Cross6b753602018-06-21 13:03:07 -0700700 }
701 return true
702 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800703 }
704}
705
Jiyong Park379de2f2018-12-19 02:47:14 +0900706// Add the dependency to the runtime library for each of the sanitizer variants
707func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900708 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000709 if !c.Enabled() {
710 return
711 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900712 var sanitizers []string
713 var diagSanitizers []string
714
715 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
716 sanitizers = append(sanitizers, "undefined")
717 } else {
718 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
719 sanitizers = append(sanitizers,
720 "bool",
721 "integer-divide-by-zero",
722 "return",
723 "returns-nonnull-attribute",
724 "shift-exponent",
725 "unreachable",
726 "vla-bound",
727 // TODO(danalbert): The following checks currently have compiler performance issues.
728 //"alignment",
729 //"bounds",
730 //"enum",
731 //"float-cast-overflow",
732 //"float-divide-by-zero",
733 //"nonnull-attribute",
734 //"null",
735 //"shift-base",
736 //"signed-integer-overflow",
737 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
738 // https://llvm.org/PR19302
739 // http://reviews.llvm.org/D6974
740 // "object-size",
741 )
742 }
743 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
744 }
745
746 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
747 diagSanitizers = append(diagSanitizers, "undefined")
748 }
749
750 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
751
752 if Bool(c.sanitize.Properties.Sanitize.Address) {
753 sanitizers = append(sanitizers, "address")
754 diagSanitizers = append(diagSanitizers, "address")
755 }
756
757 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
758 sanitizers = append(sanitizers, "hwaddress")
759 }
760
761 if Bool(c.sanitize.Properties.Sanitize.Thread) {
762 sanitizers = append(sanitizers, "thread")
763 }
764
765 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
766 sanitizers = append(sanitizers, "safe-stack")
767 }
768
769 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
770 sanitizers = append(sanitizers, "cfi")
771
772 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
773 diagSanitizers = append(diagSanitizers, "cfi")
774 }
775 }
776
777 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
778 sanitizers = append(sanitizers, "unsigned-integer-overflow")
779 sanitizers = append(sanitizers, "signed-integer-overflow")
780 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
781 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
782 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
783 }
784 }
785
786 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
787 sanitizers = append(sanitizers, "scudo")
788 }
789
790 if Bool(c.sanitize.Properties.Sanitize.Scs) {
791 sanitizers = append(sanitizers, "shadow-call-stack")
792 }
793
794 // Save the list of sanitizers. These will be used again when generating
795 // the build rules (for Cflags, etc.)
796 c.sanitize.Properties.Sanitizers = sanitizers
797 c.sanitize.Properties.DiagSanitizers = diagSanitizers
798
799 // Determine the runtime library required
800 runtimeLibrary := ""
801 toolchain := c.toolchain(mctx)
802 if Bool(c.sanitize.Properties.Sanitize.Address) {
803 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
804 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
805 if c.staticBinary() {
806 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
807 } else {
808 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
809 }
810 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
811 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
812 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
813 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
814 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
815 } else {
816 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
817 }
818 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
819 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
820 }
821
822 if mctx.Device() && runtimeLibrary != "" {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900823 if inList(runtimeLibrary, llndkLibraries) && !c.static() && c.useVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900824 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
825 }
826
827 // Adding dependency to the runtime library. We are using *FarVariation*
828 // because the runtime libraries themselves are not mutated by sanitizer
829 // mutators and thus don't have sanitizer variants whereas this module
830 // has been already mutated.
831 //
832 // Note that by adding dependency with {static|shared}DepTag, the lib is
833 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
834 if c.staticBinary() {
835 // static executable gets static runtime libs
836 mctx.AddFarVariationDependencies([]blueprint.Variation{
837 {Mutator: "link", Variation: "static"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900838 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900839 {Mutator: "arch", Variation: mctx.Target().String()},
840 }, staticDepTag, runtimeLibrary)
841 } else if !c.static() {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900842 // dynamic executable and shared libs get shared runtime libs
Jiyong Park379de2f2018-12-19 02:47:14 +0900843 mctx.AddFarVariationDependencies([]blueprint.Variation{
844 {Mutator: "link", Variation: "shared"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900845 {Mutator: "image", Variation: c.imageVariation()},
Jiyong Park379de2f2018-12-19 02:47:14 +0900846 {Mutator: "arch", Variation: mctx.Target().String()},
Jiyong Park64a44f22019-01-18 14:37:08 +0900847 }, earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900848 }
849 // static lib does not have dependency to the runtime library. The
850 // dependency will be added to the executables or shared libs using
851 // the static lib.
852 }
853 }
854}
855
856type Sanitizeable interface {
857 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900858 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900859 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900860}
861
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000862// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700863func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
864 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000865 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000866 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900867 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700868 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000869 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
870 // Save original sanitizer status before we assign values to variant
871 // 0 as that overwrites the original.
872 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
873
Jiyong Park82226632019-02-01 10:50:50 +0900874 modules := mctx.CreateVariations("", t.variationName())
Colin Crossb0f28952016-09-19 16:46:53 -0700875 modules[0].(*Module).sanitize.SetSanitizer(t, false)
876 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000877
Colin Crossb0f28952016-09-19 16:46:53 -0700878 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
879 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800880
881 // We don't need both variants active for anything but CFI-enabled
882 // target static libraries, so suppress the appropriate variant in
883 // all other cases.
884 if t == cfi {
885 if c.static() {
886 if !mctx.Device() {
887 if isSanitizerEnabled {
888 modules[0].(*Module).Properties.PreventInstall = true
889 modules[0].(*Module).Properties.HideFromMake = true
890 } else {
891 modules[1].(*Module).Properties.PreventInstall = true
892 modules[1].(*Module).Properties.HideFromMake = true
893 }
894 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800895 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800896
897 cfiStaticLibsMutex.Lock()
898 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
899 cfiStaticLibsMutex.Unlock()
900 }
901 } else {
902 modules[0].(*Module).Properties.PreventInstall = true
903 modules[0].(*Module).Properties.HideFromMake = true
904 }
905 } else if t == asan {
906 if mctx.Device() {
907 // CFI and ASAN are currently mutually exclusive so disable
908 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000909 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
910 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
911 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700912 if isSanitizerEnabled {
913 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800914 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700915 } else {
916 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800917 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700918 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800919 } else if t == scs {
920 // We don't currently link any static libraries built with make into
921 // libraries built with SCS, so we don't need logic for propagating
922 // SCSness of dependencies into make.
923 if !c.static() {
924 if isSanitizerEnabled {
925 modules[0].(*Module).Properties.PreventInstall = true
926 modules[0].(*Module).Properties.HideFromMake = true
927 } else {
928 modules[1].(*Module).Properties.PreventInstall = true
929 modules[1].(*Module).Properties.HideFromMake = true
930 }
931 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700932 } else if t == hwasan {
933 if mctx.Device() {
934 // CFI and HWASAN are currently mutually exclusive so disable
935 // CFI if this is an HWASAN variant.
936 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
937 }
938
939 if c.static() {
940 if c.useVndk() {
941 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
942 hwasanStaticLibsMutex.Lock()
943 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
944 hwasanStaticLibsMutex.Unlock()
945 } else {
946 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
947 hwasanStaticLibsMutex.Lock()
948 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
949 hwasanStaticLibsMutex.Unlock()
950 }
951 } else {
952 if isSanitizerEnabled {
953 modules[0].(*Module).Properties.PreventInstall = true
954 modules[0].(*Module).Properties.HideFromMake = true
955 } else {
956 modules[1].(*Module).Properties.PreventInstall = true
957 modules[1].(*Module).Properties.HideFromMake = true
958 }
959 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700960 }
Colin Cross16b23492016-01-06 14:41:07 -0800961 }
962 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +0900963 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900964 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +0900965 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -0800966 }
967 }
968}
Vishwath Mohane7128792017-11-17 11:08:10 -0800969
Colin Cross571cccf2019-02-04 11:22:08 -0800970var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
971
Vishwath Mohane7128792017-11-17 11:08:10 -0800972func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800973 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -0800974 return &[]string{}
975 }).(*[]string)
976}
977
Colin Cross571cccf2019-02-04 11:22:08 -0800978var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
979
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700980func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800981 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700982 return &[]string{}
983 }).(*[]string)
984}
985
Colin Cross571cccf2019-02-04 11:22:08 -0800986var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
987
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700988func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800989 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700990 return &[]string{}
991 }).(*[]string)
992}
993
Ivan Lozano30c5db22018-02-21 15:49:20 -0800994func enableMinimalRuntime(sanitize *sanitize) bool {
995 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700996 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -0800997 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
998 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
999 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1000 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
1001 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
1002 return true
1003 }
1004 return false
1005}
1006
Vishwath Mohane7128792017-11-17 11:08:10 -08001007func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1008 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001009 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001010 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1011}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001012
1013func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1014 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1015 sort.Strings(*hwasanStaticLibs)
1016 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1017
1018 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1019 sort.Strings(*hwasanVendorStaticLibs)
1020 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1021}