blob: 0951efe6aa94e37da144c15787c4c186132764ee [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 Stepanov0a87b662018-12-07 15:33:24 -080038 hwasanCflags = []string{"-mllvm", "-hwasan-with-ifunc=0", "-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
82func (t sanitizerType) String() string {
83 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
101type SanitizeProperties struct {
102 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
103 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800104 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800105
106 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700107 Address *bool `android:"arch_variant"`
108 Thread *bool `android:"arch_variant"`
109 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800110
111 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700112 Undefined *bool `android:"arch_variant"`
113 All_undefined *bool `android:"arch_variant"`
114 Misc_undefined []string `android:"arch_variant"`
115 Coverage *bool `android:"arch_variant"`
116 Safestack *bool `android:"arch_variant"`
117 Cfi *bool `android:"arch_variant"`
118 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700119 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800120 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800121
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700122 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
123 // Replaces abort() on error with a human-readable error message.
124 // Address and Thread sanitizers always run in diagnostic mode.
125 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700126 Undefined *bool `android:"arch_variant"`
127 Cfi *bool `android:"arch_variant"`
128 Integer_overflow *bool `android:"arch_variant"`
129 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800130 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700131 }
132
133 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800134 Recover []string
135
136 // value to pass to -fsanitize-blacklist
137 Blacklist *string
138 } `android:"arch_variant"`
139
Jiyong Park379de2f2018-12-19 02:47:14 +0900140 SanitizerEnabled bool `blueprint:"mutated"`
141 SanitizeDep bool `blueprint:"mutated"`
142 MinimalRuntimeDep bool `blueprint:"mutated"`
143 UbsanRuntimeDep bool `blueprint:"mutated"`
144 InSanitizerDir bool `blueprint:"mutated"`
145 Sanitizers []string `blueprint:"mutated"`
146 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800147}
148
149type sanitize struct {
150 Properties SanitizeProperties
151}
152
Vishwath Mohane7128792017-11-17 11:08:10 -0800153func init() {
154 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700155 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800156}
157
Colin Cross16b23492016-01-06 14:41:07 -0800158func (sanitize *sanitize) props() []interface{} {
159 return []interface{}{&sanitize.Properties}
160}
161
162func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700163 s := &sanitize.Properties.Sanitize
164
Colin Cross16b23492016-01-06 14:41:07 -0800165 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700166 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800167 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800168 }
169
Doug Hornc32c6b02019-01-17 14:44:05 -0800170 // Sanitizers do not work on Fuchsia yet.
171 if ctx.Fuchsia() {
172 s.Never = BoolPtr(true)
173 }
174
Colin Cross16b23492016-01-06 14:41:07 -0800175 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800176 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800177 return
178 }
179
Colin Cross16b23492016-01-06 14:41:07 -0800180 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700181 var globalSanitizersDiag []string
182
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700183 if ctx.Host() {
184 if !ctx.Windows() {
185 globalSanitizers = ctx.Config().SanitizeHost()
186 }
187 } else {
188 arches := ctx.Config().SanitizeDeviceArch()
189 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
190 globalSanitizers = ctx.Config().SanitizeDevice()
191 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800192 }
193 }
194
Colin Cross16b23492016-01-06 14:41:07 -0800195 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000196 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700197 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
198 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000199 }
Colin Cross16b23492016-01-06 14:41:07 -0800200
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700201 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
202 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000203 }
204
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800205 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
206 if s.Address == nil {
207 s.Address = boolPtr(true)
208 } else if *s.Address == false {
209 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
210 // disables address, then disable coverage as well.
211 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
212 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000213 }
214
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700215 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
216 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000217 }
218
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700219 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
220 s.Coverage = boolPtr(true)
221 }
222
223 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
224 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000225 }
226
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700227 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800228 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700229 s.Cfi = boolPtr(true)
230 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700231 }
232
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700233 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700234 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700235 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700236 s.Integer_overflow = boolPtr(true)
237 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700238 }
239
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700240 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
241 s.Scudo = boolPtr(true)
242 }
243
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700244 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
245 s.Hwaddress = boolPtr(true)
246 }
247
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000248 if len(globalSanitizers) > 0 {
249 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
250 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700251
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700252 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700253 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700254 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700255 s.Diag.Integer_overflow = boolPtr(true)
256 }
257
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700258 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
259 s.Diag.Cfi == nil && Bool(s.Cfi) {
260 s.Diag.Cfi = boolPtr(true)
261 }
262
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700263 if len(globalSanitizersDiag) > 0 {
264 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
265 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700266 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700267
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700268 // Enable CFI for all components in the include paths (for Aarch64 only)
269 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000270 s.Cfi = boolPtr(true)
271 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
272 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700273 }
274 }
275
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800276 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800277 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800278 s.Cfi = nil
279 s.Diag.Cfi = nil
280 }
281
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800282 // Also disable CFI for arm32 until b/35157333 is fixed.
283 if ctx.Arch().ArchType == android.Arm {
284 s.Cfi = nil
285 s.Diag.Cfi = nil
286 }
287
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700288 // HWASan requires AArch64 hardware feature (top-byte-ignore).
289 if ctx.Arch().ArchType != android.Arm64 {
290 s.Hwaddress = nil
291 }
292
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800293 // SCS is only implemented on AArch64.
294 // We also disable SCS if ASAN, TSAN or HWASAN are enabled because Clang considers
295 // them to be incompatible, although they are in fact compatible.
296 // TODO(pcc): Remove these checks once r347282 is rolled into the compiler.
297 if ctx.Arch().ArchType != android.Arm64 || Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) {
298 s.Scs = nil
299 }
300
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700301 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700302 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700303 s.Cfi = nil
304 s.Diag.Cfi = nil
305 }
306
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700307 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800308 if ctx.Host() {
309 s.Cfi = nil
310 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700311 s.Misc_undefined = nil
312 s.Undefined = nil
313 s.All_undefined = nil
314 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800315 }
316
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700317 // Also disable CFI for VNDK variants of components
318 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700319 s.Cfi = nil
320 s.Diag.Cfi = nil
321 }
322
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700323 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800324 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
325 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700326 s.Hwaddress = nil
327 }
328
Colin Cross3c344ef2016-07-18 15:44:56 -0700329 if ctx.staticBinary() {
330 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700331 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700332 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800333 }
334
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700335 if Bool(s.All_undefined) {
336 s.Undefined = nil
337 }
338
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700339 if !ctx.toolchain().Is64Bit() {
340 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700341 s.Thread = nil
342 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800343 // TODO(ccross): error for compile_multilib = "32"?
344 }
345
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800346 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 -0700347 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 -0800348 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700349 sanitize.Properties.SanitizerEnabled = true
350 }
351
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700352 // Disable Scudo if ASan or TSan is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700353 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700354 s.Scudo = nil
355 }
356
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700357 if Bool(s.Hwaddress) {
358 s.Address = nil
359 s.Thread = nil
360 }
361
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700362 if Bool(s.Coverage) {
363 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800364 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
365 }
366 }
367}
368
369func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
370 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
371 return deps
372 }
373
374 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700375 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700376 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800377 }
378 }
379
380 return deps
381}
382
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800383func toDisableImplicitIntegerChange(flags []string) bool {
384 // Returns true if any flag is fsanitize*integer, and there is
385 // no explicit flag about sanitize=implicit-integer-sign-change.
386 for _, f := range flags {
387 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
388 return false
389 }
390 }
391 for _, f := range flags {
392 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
393 return true
394 }
395 }
396 return false
397}
398
Colin Cross16b23492016-01-06 14:41:07 -0800399func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700400 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
401 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800402
403 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
404 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700405 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800406 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700407 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800408 return flags
409 }
410
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700411 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700412 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800413 // Frame pointer based unwinder in ASan requires ARM frame setup.
414 // TODO: put in flags?
415 flags.RequiredInstructionSet = "arm"
416 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700417 flags.CFlags = append(flags.CFlags, asanCflags...)
418 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800419
Colin Cross16b23492016-01-06 14:41:07 -0800420 if ctx.Host() {
421 // -nodefaultlibs (provided with libc++) prevents the driver from linking
422 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800423 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
424 } else {
425 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
426 flags.DynamicLinker = "/system/bin/linker_asan"
427 if flags.Toolchain.Is64Bit() {
428 flags.DynamicLinker += "64"
429 }
430 }
Colin Cross16b23492016-01-06 14:41:07 -0800431 }
432
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700433 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
434 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700435 }
436
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700437 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400438 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800439 }
440
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700441 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800442 if ctx.Arch().ArchType == android.Arm {
443 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
444 // to do this on a function basis, so force Thumb on the entire module.
445 flags.RequiredInstructionSet = "thumb"
446 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000447
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700448 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700449 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000450 // Only append the default visibility flag if -fvisibility has not already been set
451 // to hidden.
452 if !inList("-fvisibility=hidden", flags.CFlags) {
453 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
454 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700455 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000456
457 if ctx.staticBinary() {
458 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
459 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
460 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700461 }
462
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700463 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700464 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700465 }
466
Jiyong Park379de2f2018-12-19 02:47:14 +0900467 if len(sanitize.Properties.Sanitizers) > 0 {
468 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800469
Colin Cross16b23492016-01-06 14:41:07 -0800470 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700471 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800472 if ctx.Host() {
473 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
474 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800475 // Host sanitizers only link symbols in the final executable, so
476 // there will always be undefined symbols in intermediate libraries.
477 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800478 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700479 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800480
481 if enableMinimalRuntime(sanitize) {
482 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
483 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700484 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800485 }
Colin Cross16b23492016-01-06 14:41:07 -0800486 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800487 // http://b/119329758, Android core does not boot up with this sanitizer yet.
488 if toDisableImplicitIntegerChange(flags.CFlags) {
489 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
490 }
Colin Cross16b23492016-01-06 14:41:07 -0800491 }
492
Jiyong Park379de2f2018-12-19 02:47:14 +0900493 if len(sanitize.Properties.DiagSanitizers) > 0 {
494 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700495 }
496 // FIXME: enable RTTI if diag + (cfi or vptr)
497
Andreas Gampe97071162017-05-08 13:15:23 -0700498 if sanitize.Properties.Sanitize.Recover != nil {
499 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
500 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
501 }
502
Ivan Lozano7929bba2018-12-12 09:36:31 -0800503 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
504 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
505 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
506 }
507
Colin Cross635c3b02016-05-18 15:37:25 -0700508 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800509 if blacklist.Valid() {
510 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
511 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
512 }
513
514 return flags
515}
516
Colin Cross8ff9ef42017-05-08 13:44:11 -0700517func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Vishwath Mohane7128792017-11-17 11:08:10 -0800518 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
519 // name conflict.
520 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
521 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000522 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700523 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
524 ret.SubName += ".hwasan"
525 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800526 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
527 ret.SubName += ".scs"
528 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700529}
530
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700531func (sanitize *sanitize) inSanitizerDir() bool {
532 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700533}
534
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000535func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000536 switch t {
537 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000538 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700539 case hwasan:
540 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000541 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000542 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000543 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000544 return sanitize.Properties.Sanitize.Integer_overflow
545 case cfi:
546 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800547 case scs:
548 return sanitize.Properties.Sanitize.Scs
Vishwath Mohan95229302017-08-11 00:53:16 +0000549 default:
550 panic(fmt.Errorf("unknown sanitizerType %d", t))
551 }
552}
553
Dan Albert7d1eecf2018-01-19 12:30:45 -0800554func (sanitize *sanitize) isUnsanitizedVariant() bool {
555 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700556 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800557 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800558 !sanitize.isSanitizerEnabled(cfi) &&
559 !sanitize.isSanitizerEnabled(scs)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800560}
561
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700562func (sanitize *sanitize) isVariantOnProductionDevice() bool {
563 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700564 !sanitize.isSanitizerEnabled(hwasan) &&
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700565 !sanitize.isSanitizerEnabled(tsan)
566}
567
Colin Cross16b23492016-01-06 14:41:07 -0800568func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
569 switch t {
570 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700571 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700572 if !b {
573 sanitize.Properties.Sanitize.Coverage = nil
574 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700575 case hwasan:
576 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800577 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700578 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700579 case intOverflow:
580 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000581 case cfi:
582 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800583 case scs:
584 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800585 default:
586 panic(fmt.Errorf("unknown sanitizerType %d", t))
587 }
588 if b {
589 sanitize.Properties.SanitizerEnabled = true
590 }
591}
592
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000593// Check if the sanitizer is explicitly disabled (as opposed to nil by
594// virtue of not being set).
595func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
596 if sanitize == nil {
597 return false
598 }
599
600 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
601 return sanitizerVal != nil && *sanitizerVal == false
602}
603
604// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
605// because enabling a sanitizer either directly (via the blueprint) or
606// indirectly (via a mutator) sets the bool ptr to true, and you can't
607// distinguish between the cases. It isn't needed though - both cases can be
608// treated identically.
609func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
610 if sanitize == nil {
611 return false
612 }
613
614 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
615 return sanitizerVal != nil && *sanitizerVal == true
616}
617
Colin Cross6b753602018-06-21 13:03:07 -0700618func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
619 t, ok := tag.(dependencyTag)
620 return ok && t.library || t == reuseObjTag
621}
622
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700623// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700624func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
625 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000626 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700627 mctx.WalkDeps(func(child, parent android.Module) bool {
628 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
629 return false
630 }
631 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800632 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000633 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800634 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700635 if d.static() {
636 d.sanitize.Properties.SanitizeDep = true
637 }
638 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000639 d.sanitize.Properties.SanitizeDep = true
640 }
Colin Cross16b23492016-01-06 14:41:07 -0800641 }
Colin Cross6b753602018-06-21 13:03:07 -0700642 return true
Colin Cross16b23492016-01-06 14:41:07 -0800643 })
644 }
645 }
646}
647
Ivan Lozano30c5db22018-02-21 15:49:20 -0800648// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700649func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
650 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
651 mctx.WalkDeps(func(child, parent android.Module) bool {
652 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
653 return false
654 }
655 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800656
Colin Cross6b753602018-06-21 13:03:07 -0700657 if enableMinimalRuntime(d.sanitize) {
658 // If a static dependency is built with the minimal runtime,
659 // make sure we include the ubsan minimal runtime.
660 c.sanitize.Properties.MinimalRuntimeDep = true
661 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
662 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
663 // If a static dependency runs with full ubsan diagnostics,
664 // make sure we include the ubsan runtime.
665 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800666 }
Colin Cross6b753602018-06-21 13:03:07 -0700667 }
668 return true
669 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800670 }
671}
672
Jiyong Park379de2f2018-12-19 02:47:14 +0900673// Add the dependency to the runtime library for each of the sanitizer variants
674func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900675 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000676 if !c.Enabled() {
677 return
678 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900679 var sanitizers []string
680 var diagSanitizers []string
681
682 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
683 sanitizers = append(sanitizers, "undefined")
684 } else {
685 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
686 sanitizers = append(sanitizers,
687 "bool",
688 "integer-divide-by-zero",
689 "return",
690 "returns-nonnull-attribute",
691 "shift-exponent",
692 "unreachable",
693 "vla-bound",
694 // TODO(danalbert): The following checks currently have compiler performance issues.
695 //"alignment",
696 //"bounds",
697 //"enum",
698 //"float-cast-overflow",
699 //"float-divide-by-zero",
700 //"nonnull-attribute",
701 //"null",
702 //"shift-base",
703 //"signed-integer-overflow",
704 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
705 // https://llvm.org/PR19302
706 // http://reviews.llvm.org/D6974
707 // "object-size",
708 )
709 }
710 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
711 }
712
713 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
714 diagSanitizers = append(diagSanitizers, "undefined")
715 }
716
717 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
718
719 if Bool(c.sanitize.Properties.Sanitize.Address) {
720 sanitizers = append(sanitizers, "address")
721 diagSanitizers = append(diagSanitizers, "address")
722 }
723
724 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
725 sanitizers = append(sanitizers, "hwaddress")
726 }
727
728 if Bool(c.sanitize.Properties.Sanitize.Thread) {
729 sanitizers = append(sanitizers, "thread")
730 }
731
732 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
733 sanitizers = append(sanitizers, "safe-stack")
734 }
735
736 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
737 sanitizers = append(sanitizers, "cfi")
738
739 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
740 diagSanitizers = append(diagSanitizers, "cfi")
741 }
742 }
743
744 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
745 sanitizers = append(sanitizers, "unsigned-integer-overflow")
746 sanitizers = append(sanitizers, "signed-integer-overflow")
747 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
748 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
749 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
750 }
751 }
752
753 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
754 sanitizers = append(sanitizers, "scudo")
755 }
756
757 if Bool(c.sanitize.Properties.Sanitize.Scs) {
758 sanitizers = append(sanitizers, "shadow-call-stack")
759 }
760
761 // Save the list of sanitizers. These will be used again when generating
762 // the build rules (for Cflags, etc.)
763 c.sanitize.Properties.Sanitizers = sanitizers
764 c.sanitize.Properties.DiagSanitizers = diagSanitizers
765
766 // Determine the runtime library required
767 runtimeLibrary := ""
768 toolchain := c.toolchain(mctx)
769 if Bool(c.sanitize.Properties.Sanitize.Address) {
770 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
771 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
772 if c.staticBinary() {
773 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
774 } else {
775 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
776 }
777 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
778 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
779 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
780 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
781 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
782 } else {
783 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
784 }
785 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
786 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
787 }
788
789 if mctx.Device() && runtimeLibrary != "" {
790 if inList(runtimeLibrary, llndkLibraries) && !c.static() {
791 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
792 }
793
794 // Adding dependency to the runtime library. We are using *FarVariation*
795 // because the runtime libraries themselves are not mutated by sanitizer
796 // mutators and thus don't have sanitizer variants whereas this module
797 // has been already mutated.
798 //
799 // Note that by adding dependency with {static|shared}DepTag, the lib is
800 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
801 if c.staticBinary() {
802 // static executable gets static runtime libs
803 mctx.AddFarVariationDependencies([]blueprint.Variation{
804 {Mutator: "link", Variation: "static"},
805 {Mutator: "arch", Variation: mctx.Target().String()},
806 }, staticDepTag, runtimeLibrary)
807 } else if !c.static() {
808 // dynamic executable andshared libs get shared runtime libs
809 mctx.AddFarVariationDependencies([]blueprint.Variation{
810 {Mutator: "link", Variation: "shared"},
811 {Mutator: "arch", Variation: mctx.Target().String()},
812 }, sharedDepTag, runtimeLibrary)
813 }
814 // static lib does not have dependency to the runtime library. The
815 // dependency will be added to the executables or shared libs using
816 // the static lib.
817 }
818 }
819}
820
821type Sanitizeable interface {
822 android.Module
823 IsSanitizerEnabled() bool
824}
825
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000826// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700827func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
828 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000829 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000830 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700831 modules := mctx.CreateVariations(t.String())
832 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000833 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
834 // Save original sanitizer status before we assign values to variant
835 // 0 as that overwrites the original.
836 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
837
Colin Crossb0f28952016-09-19 16:46:53 -0700838 modules := mctx.CreateVariations("", t.String())
839 modules[0].(*Module).sanitize.SetSanitizer(t, false)
840 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000841
Colin Crossb0f28952016-09-19 16:46:53 -0700842 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
843 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800844
845 // We don't need both variants active for anything but CFI-enabled
846 // target static libraries, so suppress the appropriate variant in
847 // all other cases.
848 if t == cfi {
849 if c.static() {
850 if !mctx.Device() {
851 if isSanitizerEnabled {
852 modules[0].(*Module).Properties.PreventInstall = true
853 modules[0].(*Module).Properties.HideFromMake = true
854 } else {
855 modules[1].(*Module).Properties.PreventInstall = true
856 modules[1].(*Module).Properties.HideFromMake = true
857 }
858 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800859 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800860
861 cfiStaticLibsMutex.Lock()
862 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
863 cfiStaticLibsMutex.Unlock()
864 }
865 } else {
866 modules[0].(*Module).Properties.PreventInstall = true
867 modules[0].(*Module).Properties.HideFromMake = true
868 }
869 } else if t == asan {
870 if mctx.Device() {
871 // CFI and ASAN are currently mutually exclusive so disable
872 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000873 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
874 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
875 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700876 if isSanitizerEnabled {
877 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800878 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700879 } else {
880 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800881 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700882 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800883 } else if t == scs {
884 // We don't currently link any static libraries built with make into
885 // libraries built with SCS, so we don't need logic for propagating
886 // SCSness of dependencies into make.
887 if !c.static() {
888 if isSanitizerEnabled {
889 modules[0].(*Module).Properties.PreventInstall = true
890 modules[0].(*Module).Properties.HideFromMake = true
891 } else {
892 modules[1].(*Module).Properties.PreventInstall = true
893 modules[1].(*Module).Properties.HideFromMake = true
894 }
895 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700896 } else if t == hwasan {
897 if mctx.Device() {
898 // CFI and HWASAN are currently mutually exclusive so disable
899 // CFI if this is an HWASAN variant.
900 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
901 }
902
903 if c.static() {
904 if c.useVndk() {
905 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
906 hwasanStaticLibsMutex.Lock()
907 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
908 hwasanStaticLibsMutex.Unlock()
909 } else {
910 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
911 hwasanStaticLibsMutex.Lock()
912 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
913 hwasanStaticLibsMutex.Unlock()
914 }
915 } else {
916 if isSanitizerEnabled {
917 modules[0].(*Module).Properties.PreventInstall = true
918 modules[0].(*Module).Properties.HideFromMake = true
919 } else {
920 modules[1].(*Module).Properties.PreventInstall = true
921 modules[1].(*Module).Properties.HideFromMake = true
922 }
923 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700924 }
Colin Cross16b23492016-01-06 14:41:07 -0800925 }
926 c.sanitize.Properties.SanitizeDep = false
Jiyong Park379de2f2018-12-19 02:47:14 +0900927 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled() {
928 // APEX modules fall here
929 mctx.CreateVariations(t.String())
Colin Cross16b23492016-01-06 14:41:07 -0800930 }
931 }
932}
Vishwath Mohane7128792017-11-17 11:08:10 -0800933
934func cfiStaticLibs(config android.Config) *[]string {
935 return config.Once("cfiStaticLibs", func() interface{} {
936 return &[]string{}
937 }).(*[]string)
938}
939
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700940func hwasanStaticLibs(config android.Config) *[]string {
941 return config.Once("hwasanStaticLibs", func() interface{} {
942 return &[]string{}
943 }).(*[]string)
944}
945
946func hwasanVendorStaticLibs(config android.Config) *[]string {
947 return config.Once("hwasanVendorStaticLibs", func() interface{} {
948 return &[]string{}
949 }).(*[]string)
950}
951
Ivan Lozano30c5db22018-02-21 15:49:20 -0800952func enableMinimalRuntime(sanitize *sanitize) bool {
953 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700954 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -0800955 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
956 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
957 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
958 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
959 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
960 return true
961 }
962 return false
963}
964
Vishwath Mohane7128792017-11-17 11:08:10 -0800965func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
966 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -0800967 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -0800968 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
969}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700970
971func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
972 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
973 sort.Strings(*hwasanStaticLibs)
974 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
975
976 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
977 sort.Strings(*hwasanVendorStaticLibs)
978 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
979}