blob: 56013b61d726e6ea9beb7b7268ed1c880246db3e [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"
Colin Cross8ff9ef42017-05-08 13:44:11 -070019 "io"
Jeff Gaston72765392017-11-28 16:37:53 -080020 "sort"
Colin Cross16b23492016-01-06 14:41:07 -080021 "strings"
Vishwath Mohane7128792017-11-17 11:08:10 -080022 "sync"
Colin Cross16b23492016-01-06 14:41:07 -080023
Colin Cross6b753602018-06-21 13:03:07 -070024 "github.com/google/blueprint"
25
Colin Cross635c3b02016-05-18 15:37:25 -070026 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070027 "android/soong/cc/config"
Colin Cross16b23492016-01-06 14:41:07 -080028)
29
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070030var (
31 // Any C flags added by sanitizer which libTooling tools may not
32 // understand also need to be added to ClangLibToolingUnknownCflags in
33 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080034
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070035 asanCflags = []string{"-fno-omit-frame-pointer"}
36 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
37 asanLibs = []string{"libasan"}
38
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070039 hwasanCflags = []string{"-mllvm", "-hwasan-with-ifunc=0", "-fno-omit-frame-pointer", "-Wno-frame-larger-than="}
40
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000041 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070042 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070043 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
44 // used, but have no effect on assembly files
45 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070046 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070047 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070048 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
49 cfiStaticLibsMutex sync.Mutex
50 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070051
Evgenii Stepanov98f5b062018-11-29 15:12:51 -080052 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080053
54 // Pass -Xclang before -fsanitize-minimal-runtime to work around a driver
55 // check which rejects -fsanitize-minimal-runtime together with
56 // -fsanitize=shadow-call-stack even though this combination of flags
57 // is valid.
58 // TODO(pcc): Remove the -Xclang once LLVM r346526 is rolled into the compiler.
59 minimalRuntimeFlags = []string{"-Xclang", "-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070060 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov109029f2018-10-03 18:22:57 -070061 hwasanGlobalOptions = []string{"heap_history_size=4095"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070062)
63
Colin Cross16b23492016-01-06 14:41:07 -080064type sanitizerType int
65
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070066func boolPtr(v bool) *bool {
67 if v {
68 return &v
69 } else {
70 return nil
71 }
72}
73
Colin Cross16b23492016-01-06 14:41:07 -080074const (
75 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070076 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080077 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070078 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000079 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080080 scs
Colin Cross16b23492016-01-06 14:41:07 -080081)
82
83func (t sanitizerType) String() string {
84 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
102type SanitizeProperties struct {
103 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
104 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800105 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800106
107 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700108 Address *bool `android:"arch_variant"`
109 Thread *bool `android:"arch_variant"`
110 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800111
112 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700113 Undefined *bool `android:"arch_variant"`
114 All_undefined *bool `android:"arch_variant"`
115 Misc_undefined []string `android:"arch_variant"`
116 Coverage *bool `android:"arch_variant"`
117 Safestack *bool `android:"arch_variant"`
118 Cfi *bool `android:"arch_variant"`
119 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700120 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800121 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800122
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700123 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
124 // Replaces abort() on error with a human-readable error message.
125 // Address and Thread sanitizers always run in diagnostic mode.
126 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700127 Undefined *bool `android:"arch_variant"`
128 Cfi *bool `android:"arch_variant"`
129 Integer_overflow *bool `android:"arch_variant"`
130 Misc_undefined []string `android:"arch_variant"`
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
Ivan Lozano30c5db22018-02-21 15:49:20 -0800140 SanitizerEnabled bool `blueprint:"mutated"`
141 SanitizeDep bool `blueprint:"mutated"`
142 MinimalRuntimeDep bool `blueprint:"mutated"`
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700143 UbsanRuntimeDep bool `blueprint:"mutated"`
Ivan Lozano30c5db22018-02-21 15:49:20 -0800144 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800145}
146
147type sanitize struct {
148 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700149
Jiyong Park27b188b2017-07-18 13:23:39 +0900150 runtimeLibrary string
151 androidMkRuntimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800152}
153
Vishwath Mohane7128792017-11-17 11:08:10 -0800154func init() {
155 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700156 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800157}
158
Colin Cross16b23492016-01-06 14:41:07 -0800159func (sanitize *sanitize) props() []interface{} {
160 return []interface{}{&sanitize.Properties}
161}
162
163func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700164 s := &sanitize.Properties.Sanitize
165
Colin Cross16b23492016-01-06 14:41:07 -0800166 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700167 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800168 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800169 }
170
171 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800172 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800173 return
174 }
175
Colin Cross16b23492016-01-06 14:41:07 -0800176 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700177 var globalSanitizersDiag []string
178
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700179 if ctx.Host() {
180 if !ctx.Windows() {
181 globalSanitizers = ctx.Config().SanitizeHost()
182 }
183 } else {
184 arches := ctx.Config().SanitizeDeviceArch()
185 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
186 globalSanitizers = ctx.Config().SanitizeDevice()
187 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800188 }
189 }
190
Colin Cross16b23492016-01-06 14:41:07 -0800191 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000192 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700193 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
194 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000195 }
Colin Cross16b23492016-01-06 14:41:07 -0800196
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700197 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
198 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000199 }
200
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800201 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
202 if s.Address == nil {
203 s.Address = boolPtr(true)
204 } else if *s.Address == false {
205 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
206 // disables address, then disable coverage as well.
207 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
208 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000209 }
210
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700211 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
212 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000213 }
214
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700215 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
216 s.Coverage = boolPtr(true)
217 }
218
219 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
220 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000221 }
222
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700223 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800224 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700225 s.Cfi = boolPtr(true)
226 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700227 }
228
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700229 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700230 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700231 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700232 s.Integer_overflow = boolPtr(true)
233 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700234 }
235
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700236 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
237 s.Scudo = boolPtr(true)
238 }
239
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700240 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
241 s.Hwaddress = boolPtr(true)
242 }
243
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000244 if len(globalSanitizers) > 0 {
245 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
246 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700247
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700248 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700249 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700250 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700251 s.Diag.Integer_overflow = boolPtr(true)
252 }
253
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700254 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
255 s.Diag.Cfi == nil && Bool(s.Cfi) {
256 s.Diag.Cfi = boolPtr(true)
257 }
258
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700259 if len(globalSanitizersDiag) > 0 {
260 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
261 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700262 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700263
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700264 // Enable CFI for all components in the include paths (for Aarch64 only)
265 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000266 s.Cfi = boolPtr(true)
267 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
268 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700269 }
270 }
271
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800272 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800273 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800274 s.Cfi = nil
275 s.Diag.Cfi = nil
276 }
277
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800278 // Also disable CFI for arm32 until b/35157333 is fixed.
279 if ctx.Arch().ArchType == android.Arm {
280 s.Cfi = nil
281 s.Diag.Cfi = nil
282 }
283
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700284 // HWASan requires AArch64 hardware feature (top-byte-ignore).
285 if ctx.Arch().ArchType != android.Arm64 {
286 s.Hwaddress = nil
287 }
288
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800289 // SCS is only implemented on AArch64.
290 // We also disable SCS if ASAN, TSAN or HWASAN are enabled because Clang considers
291 // them to be incompatible, although they are in fact compatible.
292 // TODO(pcc): Remove these checks once r347282 is rolled into the compiler.
293 if ctx.Arch().ArchType != android.Arm64 || Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) {
294 s.Scs = nil
295 }
296
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700297 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700298 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700299 s.Cfi = nil
300 s.Diag.Cfi = nil
301 }
302
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700303 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800304 if ctx.Host() {
305 s.Cfi = nil
306 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700307 s.Misc_undefined = nil
308 s.Undefined = nil
309 s.All_undefined = nil
310 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800311 }
312
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700313 // Also disable CFI for VNDK variants of components
314 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700315 s.Cfi = nil
316 s.Diag.Cfi = nil
317 }
318
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700319 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800320 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
321 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700322 s.Hwaddress = nil
323 }
324
Colin Cross3c344ef2016-07-18 15:44:56 -0700325 if ctx.staticBinary() {
326 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700327 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700328 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800329 }
330
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700331 if Bool(s.All_undefined) {
332 s.Undefined = nil
333 }
334
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700335 if !ctx.toolchain().Is64Bit() {
336 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700337 s.Thread = nil
338 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800339 // TODO(ccross): error for compile_multilib = "32"?
340 }
341
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800342 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 -0700343 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 -0800344 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700345 sanitize.Properties.SanitizerEnabled = true
346 }
347
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700348 // Disable Scudo if ASan or TSan is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700349 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700350 s.Scudo = nil
351 }
352
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700353 if Bool(s.Hwaddress) {
354 s.Address = nil
355 s.Thread = nil
356 }
357
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700358 if Bool(s.Coverage) {
359 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800360 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
361 }
362 }
363}
364
365func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
366 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
367 return deps
368 }
369
370 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700371 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700372 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800373 }
374 }
375
376 return deps
377}
378
379func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700380 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
381 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800382
383 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
384 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700385 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800386 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700387 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800388 return flags
389 }
390
Colin Cross16b23492016-01-06 14:41:07 -0800391 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700392 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800393
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700394 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800395 sanitizers = append(sanitizers, "undefined")
Colin Cross16b23492016-01-06 14:41:07 -0800396 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700397 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800398 sanitizers = append(sanitizers,
399 "bool",
400 "integer-divide-by-zero",
401 "return",
402 "returns-nonnull-attribute",
403 "shift-exponent",
404 "unreachable",
405 "vla-bound",
406 // TODO(danalbert): The following checks currently have compiler performance issues.
407 //"alignment",
408 //"bounds",
409 //"enum",
410 //"float-cast-overflow",
411 //"float-divide-by-zero",
412 //"nonnull-attribute",
413 //"null",
414 //"shift-base",
415 //"signed-integer-overflow",
416 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
417 // https://llvm.org/PR19302
418 // http://reviews.llvm.org/D6974
419 // "object-size",
420 )
421 }
422 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
423 }
424
Ivan Lozano651275b2017-06-13 10:24:34 -0700425 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700426 diagSanitizers = append(diagSanitizers, "undefined")
427 }
428
Ivan Lozano651275b2017-06-13 10:24:34 -0700429 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
430
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700431 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700432 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800433 // Frame pointer based unwinder in ASan requires ARM frame setup.
434 // TODO: put in flags?
435 flags.RequiredInstructionSet = "arm"
436 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700437 flags.CFlags = append(flags.CFlags, asanCflags...)
438 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800439
Colin Cross16b23492016-01-06 14:41:07 -0800440 if ctx.Host() {
441 // -nodefaultlibs (provided with libc++) prevents the driver from linking
442 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800443 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
444 } else {
445 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
446 flags.DynamicLinker = "/system/bin/linker_asan"
447 if flags.Toolchain.Is64Bit() {
448 flags.DynamicLinker += "64"
449 }
450 }
451 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700452 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800453 }
454
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700455 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
456 flags.CFlags = append(flags.CFlags, hwasanCflags...)
457 sanitizers = append(sanitizers, "hwaddress")
458 }
459
Yabin Cui6be405e2017-10-19 15:52:11 -0700460 if Bool(sanitize.Properties.Sanitize.Thread) {
461 sanitizers = append(sanitizers, "thread")
462 }
463
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700464 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400465 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800466 }
467
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700468 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700469 sanitizers = append(sanitizers, "safe-stack")
470 }
471
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700472 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800473 if ctx.Arch().ArchType == android.Arm {
474 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
475 // to do this on a function basis, so force Thumb on the entire module.
476 flags.RequiredInstructionSet = "thumb"
477 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700478 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000479
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700480 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700481 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000482 // Only append the default visibility flag if -fvisibility has not already been set
483 // to hidden.
484 if !inList("-fvisibility=hidden", flags.CFlags) {
485 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
486 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700487 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700488 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
489 diagSanitizers = append(diagSanitizers, "cfi")
490 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000491
492 if ctx.staticBinary() {
493 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
494 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
495 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700496 }
497
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700498 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700499 sanitizers = append(sanitizers, "unsigned-integer-overflow")
500 sanitizers = append(sanitizers, "signed-integer-overflow")
501 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
502 if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) {
503 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
504 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700505 }
506 }
507
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700508 if Bool(sanitize.Properties.Sanitize.Scudo) {
509 sanitizers = append(sanitizers, "scudo")
510 }
511
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800512 if Bool(sanitize.Properties.Sanitize.Scs) {
513 sanitizers = append(sanitizers, "shadow-call-stack")
514 }
515
Colin Cross16b23492016-01-06 14:41:07 -0800516 if len(sanitizers) > 0 {
517 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800518
Colin Cross16b23492016-01-06 14:41:07 -0800519 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700520 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800521 if ctx.Host() {
522 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
523 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800524 // Host sanitizers only link symbols in the final executable, so
525 // there will always be undefined symbols in intermediate libraries.
526 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800527 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700528 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800529
530 if enableMinimalRuntime(sanitize) {
531 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
532 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700533 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800534 }
Colin Cross16b23492016-01-06 14:41:07 -0800535 }
536 }
537
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700538 if len(diagSanitizers) > 0 {
Ivan Lozanob7d0f522018-01-20 01:44:38 +0000539 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700540 }
541 // FIXME: enable RTTI if diag + (cfi or vptr)
542
Andreas Gampe97071162017-05-08 13:15:23 -0700543 if sanitize.Properties.Sanitize.Recover != nil {
544 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
545 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
546 }
547
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700548 // Link a runtime library if needed.
549 runtimeLibrary := ""
550 if Bool(sanitize.Properties.Sanitize.Address) {
551 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700552 } else if Bool(sanitize.Properties.Sanitize.Hwaddress) {
553 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(ctx.toolchain())
Yabin Cui6be405e2017-10-19 15:52:11 -0700554 } else if Bool(sanitize.Properties.Sanitize.Thread) {
555 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain())
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700556 } else if Bool(sanitize.Properties.Sanitize.Scudo) {
Kostya Kortchinskyad73b2e2018-10-11 08:38:39 -0700557 if len(diagSanitizers) == 0 && !sanitize.Properties.UbsanRuntimeDep {
558 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(ctx.toolchain())
559 } else {
560 runtimeLibrary = config.ScudoRuntimeLibrary(ctx.toolchain())
561 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700562 } else if len(diagSanitizers) > 0 || sanitize.Properties.UbsanRuntimeDep {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700563 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
564 }
565
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700566 if runtimeLibrary != "" {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700567 runtimeLibraryPath := "${config.ClangAsanLibDir}/" + runtimeLibrary
568 if !ctx.static() {
569 runtimeLibraryPath = runtimeLibraryPath + ctx.toolchain().ShlibSuffix()
570 } else {
571 runtimeLibraryPath = runtimeLibraryPath + ".a"
572 }
573
Jiyong Park27b188b2017-07-18 13:23:39 +0900574 // ASan runtime library must be the first in the link order.
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700575 flags.libFlags = append([]string{runtimeLibraryPath}, flags.libFlags...)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700576 sanitize.runtimeLibrary = runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900577
578 // When linking against VNDK, use the vendor variant of the runtime lib
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700579 if ctx.useVndk() {
Jiyong Park27b188b2017-07-18 13:23:39 +0900580 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix
Evgenii Stepanov98f5b062018-11-29 15:12:51 -0800581 } else if ctx.inRecovery() {
582 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + recoverySuffix
583 } else {
584 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900585 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700586 }
587
Colin Cross635c3b02016-05-18 15:37:25 -0700588 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800589 if blacklist.Valid() {
590 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
591 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
592 }
593
594 return flags
595}
596
Colin Cross8ff9ef42017-05-08 13:44:11 -0700597func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross27a4b052017-08-10 16:32:23 -0700598 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Jiyong Park27b188b2017-07-18 13:23:39 +0900599 if sanitize.androidMkRuntimeLibrary != "" {
600 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700601 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700602 })
Vishwath Mohane7128792017-11-17 11:08:10 -0800603
604 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
605 // name conflict.
606 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
607 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000608 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700609 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
610 ret.SubName += ".hwasan"
611 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800612 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
613 ret.SubName += ".scs"
614 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700615}
616
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700617func (sanitize *sanitize) inSanitizerDir() bool {
618 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700619}
620
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000621func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000622 switch t {
623 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000624 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700625 case hwasan:
626 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000627 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000628 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000629 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000630 return sanitize.Properties.Sanitize.Integer_overflow
631 case cfi:
632 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800633 case scs:
634 return sanitize.Properties.Sanitize.Scs
Vishwath Mohan95229302017-08-11 00:53:16 +0000635 default:
636 panic(fmt.Errorf("unknown sanitizerType %d", t))
637 }
638}
639
Dan Albert7d1eecf2018-01-19 12:30:45 -0800640func (sanitize *sanitize) isUnsanitizedVariant() bool {
641 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700642 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800643 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800644 !sanitize.isSanitizerEnabled(cfi) &&
645 !sanitize.isSanitizerEnabled(scs)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800646}
647
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700648func (sanitize *sanitize) isVariantOnProductionDevice() bool {
649 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700650 !sanitize.isSanitizerEnabled(hwasan) &&
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700651 !sanitize.isSanitizerEnabled(tsan)
652}
653
Colin Cross16b23492016-01-06 14:41:07 -0800654func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
655 switch t {
656 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700657 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700658 if !b {
659 sanitize.Properties.Sanitize.Coverage = nil
660 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700661 case hwasan:
662 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800663 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700664 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700665 case intOverflow:
666 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000667 case cfi:
668 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800669 case scs:
670 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800671 default:
672 panic(fmt.Errorf("unknown sanitizerType %d", t))
673 }
674 if b {
675 sanitize.Properties.SanitizerEnabled = true
676 }
677}
678
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000679// Check if the sanitizer is explicitly disabled (as opposed to nil by
680// virtue of not being set).
681func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
682 if sanitize == nil {
683 return false
684 }
685
686 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
687 return sanitizerVal != nil && *sanitizerVal == false
688}
689
690// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
691// because enabling a sanitizer either directly (via the blueprint) or
692// indirectly (via a mutator) sets the bool ptr to true, and you can't
693// distinguish between the cases. It isn't needed though - both cases can be
694// treated identically.
695func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
696 if sanitize == nil {
697 return false
698 }
699
700 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
701 return sanitizerVal != nil && *sanitizerVal == true
702}
703
Colin Cross6b753602018-06-21 13:03:07 -0700704func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
705 t, ok := tag.(dependencyTag)
706 return ok && t.library || t == reuseObjTag
707}
708
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700709// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700710func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
711 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000712 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700713 mctx.WalkDeps(func(child, parent android.Module) bool {
714 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
715 return false
716 }
717 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800718 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000719 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800720 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700721 if d.static() {
722 d.sanitize.Properties.SanitizeDep = true
723 }
724 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000725 d.sanitize.Properties.SanitizeDep = true
726 }
Colin Cross16b23492016-01-06 14:41:07 -0800727 }
Colin Cross6b753602018-06-21 13:03:07 -0700728 return true
Colin Cross16b23492016-01-06 14:41:07 -0800729 })
730 }
731 }
732}
733
Ivan Lozano30c5db22018-02-21 15:49:20 -0800734// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700735func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
736 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
737 mctx.WalkDeps(func(child, parent android.Module) bool {
738 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
739 return false
740 }
741 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800742
Colin Cross6b753602018-06-21 13:03:07 -0700743 if enableMinimalRuntime(d.sanitize) {
744 // If a static dependency is built with the minimal runtime,
745 // make sure we include the ubsan minimal runtime.
746 c.sanitize.Properties.MinimalRuntimeDep = true
747 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
748 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
749 // If a static dependency runs with full ubsan diagnostics,
750 // make sure we include the ubsan runtime.
751 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800752 }
Colin Cross6b753602018-06-21 13:03:07 -0700753 }
754 return true
755 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800756 }
757}
758
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000759// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700760func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
761 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000762 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000763 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700764 modules := mctx.CreateVariations(t.String())
765 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000766 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
767 // Save original sanitizer status before we assign values to variant
768 // 0 as that overwrites the original.
769 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
770
Colin Crossb0f28952016-09-19 16:46:53 -0700771 modules := mctx.CreateVariations("", t.String())
772 modules[0].(*Module).sanitize.SetSanitizer(t, false)
773 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000774
Colin Crossb0f28952016-09-19 16:46:53 -0700775 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
776 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800777
778 // We don't need both variants active for anything but CFI-enabled
779 // target static libraries, so suppress the appropriate variant in
780 // all other cases.
781 if t == cfi {
782 if c.static() {
783 if !mctx.Device() {
784 if isSanitizerEnabled {
785 modules[0].(*Module).Properties.PreventInstall = true
786 modules[0].(*Module).Properties.HideFromMake = true
787 } else {
788 modules[1].(*Module).Properties.PreventInstall = true
789 modules[1].(*Module).Properties.HideFromMake = true
790 }
791 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800792 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800793
794 cfiStaticLibsMutex.Lock()
795 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
796 cfiStaticLibsMutex.Unlock()
797 }
798 } else {
799 modules[0].(*Module).Properties.PreventInstall = true
800 modules[0].(*Module).Properties.HideFromMake = true
801 }
802 } else if t == asan {
803 if mctx.Device() {
804 // CFI and ASAN are currently mutually exclusive so disable
805 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000806 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
807 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
808 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700809 if isSanitizerEnabled {
810 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800811 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700812 } else {
813 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800814 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700815 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800816 } else if t == scs {
817 // We don't currently link any static libraries built with make into
818 // libraries built with SCS, so we don't need logic for propagating
819 // SCSness of dependencies into make.
820 if !c.static() {
821 if isSanitizerEnabled {
822 modules[0].(*Module).Properties.PreventInstall = true
823 modules[0].(*Module).Properties.HideFromMake = true
824 } else {
825 modules[1].(*Module).Properties.PreventInstall = true
826 modules[1].(*Module).Properties.HideFromMake = true
827 }
828 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700829 } else if t == hwasan {
830 if mctx.Device() {
831 // CFI and HWASAN are currently mutually exclusive so disable
832 // CFI if this is an HWASAN variant.
833 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
834 }
835
836 if c.static() {
837 if c.useVndk() {
838 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
839 hwasanStaticLibsMutex.Lock()
840 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
841 hwasanStaticLibsMutex.Unlock()
842 } else {
843 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
844 hwasanStaticLibsMutex.Lock()
845 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
846 hwasanStaticLibsMutex.Unlock()
847 }
848 } else {
849 if isSanitizerEnabled {
850 modules[0].(*Module).Properties.PreventInstall = true
851 modules[0].(*Module).Properties.HideFromMake = true
852 } else {
853 modules[1].(*Module).Properties.PreventInstall = true
854 modules[1].(*Module).Properties.HideFromMake = true
855 }
856 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700857 }
Colin Cross16b23492016-01-06 14:41:07 -0800858 }
859 c.sanitize.Properties.SanitizeDep = false
860 }
861 }
862}
Vishwath Mohane7128792017-11-17 11:08:10 -0800863
864func cfiStaticLibs(config android.Config) *[]string {
865 return config.Once("cfiStaticLibs", func() interface{} {
866 return &[]string{}
867 }).(*[]string)
868}
869
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700870func hwasanStaticLibs(config android.Config) *[]string {
871 return config.Once("hwasanStaticLibs", func() interface{} {
872 return &[]string{}
873 }).(*[]string)
874}
875
876func hwasanVendorStaticLibs(config android.Config) *[]string {
877 return config.Once("hwasanVendorStaticLibs", func() interface{} {
878 return &[]string{}
879 }).(*[]string)
880}
881
Ivan Lozano30c5db22018-02-21 15:49:20 -0800882func enableMinimalRuntime(sanitize *sanitize) bool {
883 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700884 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -0800885 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
886 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
887 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
888 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
889 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
890 return true
891 }
892 return false
893}
894
Vishwath Mohane7128792017-11-17 11:08:10 -0800895func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
896 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -0800897 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -0800898 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
899}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700900
901func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
902 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
903 sort.Strings(*hwasanStaticLibs)
904 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
905
906 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
907 sort.Strings(*hwasanVendorStaticLibs)
908 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
909}