blob: 5db6bdfaeac097c967faaf82bbe7dda3281b7198 [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 Stepanov0a87b662018-12-07 15:33:24 -080039 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 -070040
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"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800131 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700132 }
133
134 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800135 Recover []string
136
137 // value to pass to -fsanitize-blacklist
138 Blacklist *string
139 } `android:"arch_variant"`
140
Ivan Lozano30c5db22018-02-21 15:49:20 -0800141 SanitizerEnabled bool `blueprint:"mutated"`
142 SanitizeDep bool `blueprint:"mutated"`
143 MinimalRuntimeDep bool `blueprint:"mutated"`
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700144 UbsanRuntimeDep bool `blueprint:"mutated"`
Ivan Lozano30c5db22018-02-21 15:49:20 -0800145 InSanitizerDir bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800146}
147
148type sanitize struct {
149 Properties SanitizeProperties
Colin Cross8ff9ef42017-05-08 13:44:11 -0700150
Jiyong Park27b188b2017-07-18 13:23:39 +0900151 runtimeLibrary string
152 androidMkRuntimeLibrary string
Colin Cross16b23492016-01-06 14:41:07 -0800153}
154
Vishwath Mohane7128792017-11-17 11:08:10 -0800155func init() {
156 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700157 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800158}
159
Colin Cross16b23492016-01-06 14:41:07 -0800160func (sanitize *sanitize) props() []interface{} {
161 return []interface{}{&sanitize.Properties}
162}
163
164func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700165 s := &sanitize.Properties.Sanitize
166
Colin Cross16b23492016-01-06 14:41:07 -0800167 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700168 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800169 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800170 }
171
172 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800173 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800174 return
175 }
176
Colin Cross16b23492016-01-06 14:41:07 -0800177 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700178 var globalSanitizersDiag []string
179
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700180 if ctx.Host() {
181 if !ctx.Windows() {
182 globalSanitizers = ctx.Config().SanitizeHost()
183 }
184 } else {
185 arches := ctx.Config().SanitizeDeviceArch()
186 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
187 globalSanitizers = ctx.Config().SanitizeDevice()
188 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800189 }
190 }
191
Colin Cross16b23492016-01-06 14:41:07 -0800192 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000193 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700194 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
195 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000196 }
Colin Cross16b23492016-01-06 14:41:07 -0800197
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700198 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
199 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000200 }
201
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800202 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
203 if s.Address == nil {
204 s.Address = boolPtr(true)
205 } else if *s.Address == false {
206 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
207 // disables address, then disable coverage as well.
208 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
209 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000210 }
211
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700212 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
213 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000214 }
215
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700216 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
217 s.Coverage = boolPtr(true)
218 }
219
220 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
221 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000222 }
223
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700224 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800225 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700226 s.Cfi = boolPtr(true)
227 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700228 }
229
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700230 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700231 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700232 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700233 s.Integer_overflow = boolPtr(true)
234 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700235 }
236
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700237 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
238 s.Scudo = boolPtr(true)
239 }
240
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700241 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
242 s.Hwaddress = boolPtr(true)
243 }
244
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000245 if len(globalSanitizers) > 0 {
246 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
247 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700248
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700249 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700250 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700251 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700252 s.Diag.Integer_overflow = boolPtr(true)
253 }
254
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700255 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
256 s.Diag.Cfi == nil && Bool(s.Cfi) {
257 s.Diag.Cfi = boolPtr(true)
258 }
259
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700260 if len(globalSanitizersDiag) > 0 {
261 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
262 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700263 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700264
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700265 // Enable CFI for all components in the include paths (for Aarch64 only)
266 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000267 s.Cfi = boolPtr(true)
268 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
269 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700270 }
271 }
272
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800273 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800274 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800275 s.Cfi = nil
276 s.Diag.Cfi = nil
277 }
278
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800279 // Also disable CFI for arm32 until b/35157333 is fixed.
280 if ctx.Arch().ArchType == android.Arm {
281 s.Cfi = nil
282 s.Diag.Cfi = nil
283 }
284
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700285 // HWASan requires AArch64 hardware feature (top-byte-ignore).
286 if ctx.Arch().ArchType != android.Arm64 {
287 s.Hwaddress = nil
288 }
289
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800290 // SCS is only implemented on AArch64.
291 // We also disable SCS if ASAN, TSAN or HWASAN are enabled because Clang considers
292 // them to be incompatible, although they are in fact compatible.
293 // TODO(pcc): Remove these checks once r347282 is rolled into the compiler.
294 if ctx.Arch().ArchType != android.Arm64 || Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) {
295 s.Scs = nil
296 }
297
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700298 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700299 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700300 s.Cfi = nil
301 s.Diag.Cfi = nil
302 }
303
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700304 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800305 if ctx.Host() {
306 s.Cfi = nil
307 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700308 s.Misc_undefined = nil
309 s.Undefined = nil
310 s.All_undefined = nil
311 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800312 }
313
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700314 // Also disable CFI for VNDK variants of components
315 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700316 s.Cfi = nil
317 s.Diag.Cfi = nil
318 }
319
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700320 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800321 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
322 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700323 s.Hwaddress = nil
324 }
325
Colin Cross3c344ef2016-07-18 15:44:56 -0700326 if ctx.staticBinary() {
327 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700328 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700329 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800330 }
331
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700332 if Bool(s.All_undefined) {
333 s.Undefined = nil
334 }
335
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700336 if !ctx.toolchain().Is64Bit() {
337 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700338 s.Thread = nil
339 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800340 // TODO(ccross): error for compile_multilib = "32"?
341 }
342
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800343 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 -0700344 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 -0800345 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700346 sanitize.Properties.SanitizerEnabled = true
347 }
348
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700349 // Disable Scudo if ASan or TSan is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700350 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700351 s.Scudo = nil
352 }
353
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700354 if Bool(s.Hwaddress) {
355 s.Address = nil
356 s.Thread = nil
357 }
358
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700359 if Bool(s.Coverage) {
360 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800361 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
362 }
363 }
364}
365
366func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
367 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
368 return deps
369 }
370
371 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700372 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700373 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800374 }
375 }
376
377 return deps
378}
379
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800380func toDisableImplicitIntegerChange(flags []string) bool {
381 // Returns true if any flag is fsanitize*integer, and there is
382 // no explicit flag about sanitize=implicit-integer-sign-change.
383 for _, f := range flags {
384 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
385 return false
386 }
387 }
388 for _, f := range flags {
389 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
390 return true
391 }
392 }
393 return false
394}
395
Colin Cross16b23492016-01-06 14:41:07 -0800396func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700397 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
398 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800399
400 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
401 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700402 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800403 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700404 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800405 return flags
406 }
407
Colin Cross16b23492016-01-06 14:41:07 -0800408 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700409 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800410
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700411 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800412 sanitizers = append(sanitizers, "undefined")
Colin Cross16b23492016-01-06 14:41:07 -0800413 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700414 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800415 sanitizers = append(sanitizers,
416 "bool",
417 "integer-divide-by-zero",
418 "return",
419 "returns-nonnull-attribute",
420 "shift-exponent",
421 "unreachable",
422 "vla-bound",
423 // TODO(danalbert): The following checks currently have compiler performance issues.
424 //"alignment",
425 //"bounds",
426 //"enum",
427 //"float-cast-overflow",
428 //"float-divide-by-zero",
429 //"nonnull-attribute",
430 //"null",
431 //"shift-base",
432 //"signed-integer-overflow",
433 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
434 // https://llvm.org/PR19302
435 // http://reviews.llvm.org/D6974
436 // "object-size",
437 )
438 }
439 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
440 }
441
Ivan Lozano651275b2017-06-13 10:24:34 -0700442 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700443 diagSanitizers = append(diagSanitizers, "undefined")
444 }
445
Ivan Lozano651275b2017-06-13 10:24:34 -0700446 diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...)
447
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700448 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700449 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800450 // Frame pointer based unwinder in ASan requires ARM frame setup.
451 // TODO: put in flags?
452 flags.RequiredInstructionSet = "arm"
453 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700454 flags.CFlags = append(flags.CFlags, asanCflags...)
455 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800456
Colin Cross16b23492016-01-06 14:41:07 -0800457 if ctx.Host() {
458 // -nodefaultlibs (provided with libc++) prevents the driver from linking
459 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800460 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
461 } else {
462 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
463 flags.DynamicLinker = "/system/bin/linker_asan"
464 if flags.Toolchain.Is64Bit() {
465 flags.DynamicLinker += "64"
466 }
467 }
468 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700469 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800470 }
471
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700472 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
473 flags.CFlags = append(flags.CFlags, hwasanCflags...)
474 sanitizers = append(sanitizers, "hwaddress")
475 }
476
Yabin Cui6be405e2017-10-19 15:52:11 -0700477 if Bool(sanitize.Properties.Sanitize.Thread) {
478 sanitizers = append(sanitizers, "thread")
479 }
480
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700481 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400482 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800483 }
484
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700485 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700486 sanitizers = append(sanitizers, "safe-stack")
487 }
488
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700489 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800490 if ctx.Arch().ArchType == android.Arm {
491 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
492 // to do this on a function basis, so force Thumb on the entire module.
493 flags.RequiredInstructionSet = "thumb"
494 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700495 sanitizers = append(sanitizers, "cfi")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000496
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700497 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700498 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000499 // Only append the default visibility flag if -fvisibility has not already been set
500 // to hidden.
501 if !inList("-fvisibility=hidden", flags.CFlags) {
502 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
503 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700504 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700505 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
506 diagSanitizers = append(diagSanitizers, "cfi")
507 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000508
509 if ctx.staticBinary() {
510 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
511 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
512 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700513 }
514
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700515 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700516 sanitizers = append(sanitizers, "unsigned-integer-overflow")
517 sanitizers = append(sanitizers, "signed-integer-overflow")
518 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
519 if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) {
520 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
521 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700522 }
523 }
524
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700525 if Bool(sanitize.Properties.Sanitize.Scudo) {
526 sanitizers = append(sanitizers, "scudo")
527 }
528
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800529 if Bool(sanitize.Properties.Sanitize.Scs) {
530 sanitizers = append(sanitizers, "shadow-call-stack")
531 }
532
Colin Cross16b23492016-01-06 14:41:07 -0800533 if len(sanitizers) > 0 {
534 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800535
Colin Cross16b23492016-01-06 14:41:07 -0800536 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700537 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800538 if ctx.Host() {
539 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
540 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800541 // Host sanitizers only link symbols in the final executable, so
542 // there will always be undefined symbols in intermediate libraries.
543 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800544 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700545 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800546
547 if enableMinimalRuntime(sanitize) {
548 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
549 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700550 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800551 }
Colin Cross16b23492016-01-06 14:41:07 -0800552 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800553 // http://b/119329758, Android core does not boot up with this sanitizer yet.
554 if toDisableImplicitIntegerChange(flags.CFlags) {
555 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
556 }
Colin Cross16b23492016-01-06 14:41:07 -0800557 }
558
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700559 if len(diagSanitizers) > 0 {
Ivan Lozanob7d0f522018-01-20 01:44:38 +0000560 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700561 }
562 // FIXME: enable RTTI if diag + (cfi or vptr)
563
Andreas Gampe97071162017-05-08 13:15:23 -0700564 if sanitize.Properties.Sanitize.Recover != nil {
565 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
566 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
567 }
568
Ivan Lozano7929bba2018-12-12 09:36:31 -0800569 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
570 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
571 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
572 }
573
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700574 // Link a runtime library if needed.
575 runtimeLibrary := ""
576 if Bool(sanitize.Properties.Sanitize.Address) {
577 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700578 } else if Bool(sanitize.Properties.Sanitize.Hwaddress) {
579 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(ctx.toolchain())
Yabin Cui6be405e2017-10-19 15:52:11 -0700580 } else if Bool(sanitize.Properties.Sanitize.Thread) {
581 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain())
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700582 } else if Bool(sanitize.Properties.Sanitize.Scudo) {
Kostya Kortchinskyad73b2e2018-10-11 08:38:39 -0700583 if len(diagSanitizers) == 0 && !sanitize.Properties.UbsanRuntimeDep {
584 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(ctx.toolchain())
585 } else {
586 runtimeLibrary = config.ScudoRuntimeLibrary(ctx.toolchain())
587 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700588 } else if len(diagSanitizers) > 0 || sanitize.Properties.UbsanRuntimeDep {
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700589 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
590 }
591
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700592 if runtimeLibrary != "" {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700593 runtimeLibraryPath := "${config.ClangAsanLibDir}/" + runtimeLibrary
594 if !ctx.static() {
595 runtimeLibraryPath = runtimeLibraryPath + ctx.toolchain().ShlibSuffix()
596 } else {
597 runtimeLibraryPath = runtimeLibraryPath + ".a"
598 }
599
Jiyong Park27b188b2017-07-18 13:23:39 +0900600 // ASan runtime library must be the first in the link order.
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700601 flags.libFlags = append([]string{runtimeLibraryPath}, flags.libFlags...)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700602 sanitize.runtimeLibrary = runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900603
604 // When linking against VNDK, use the vendor variant of the runtime lib
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700605 if ctx.useVndk() {
Jiyong Park27b188b2017-07-18 13:23:39 +0900606 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix
Evgenii Stepanov98f5b062018-11-29 15:12:51 -0800607 } else if ctx.inRecovery() {
608 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + recoverySuffix
609 } else {
610 sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
Jiyong Park27b188b2017-07-18 13:23:39 +0900611 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700612 }
613
Colin Cross635c3b02016-05-18 15:37:25 -0700614 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800615 if blacklist.Valid() {
616 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
617 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
618 }
619
620 return flags
621}
622
Colin Cross8ff9ef42017-05-08 13:44:11 -0700623func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross27a4b052017-08-10 16:32:23 -0700624 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Jiyong Park27b188b2017-07-18 13:23:39 +0900625 if sanitize.androidMkRuntimeLibrary != "" {
626 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary)
Colin Cross8ff9ef42017-05-08 13:44:11 -0700627 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700628 })
Vishwath Mohane7128792017-11-17 11:08:10 -0800629
630 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
631 // name conflict.
632 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
633 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000634 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700635 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
636 ret.SubName += ".hwasan"
637 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800638 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
639 ret.SubName += ".scs"
640 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700641}
642
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700643func (sanitize *sanitize) inSanitizerDir() bool {
644 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700645}
646
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000647func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000648 switch t {
649 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000650 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700651 case hwasan:
652 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000653 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000654 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000655 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000656 return sanitize.Properties.Sanitize.Integer_overflow
657 case cfi:
658 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800659 case scs:
660 return sanitize.Properties.Sanitize.Scs
Vishwath Mohan95229302017-08-11 00:53:16 +0000661 default:
662 panic(fmt.Errorf("unknown sanitizerType %d", t))
663 }
664}
665
Dan Albert7d1eecf2018-01-19 12:30:45 -0800666func (sanitize *sanitize) isUnsanitizedVariant() bool {
667 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700668 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800669 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800670 !sanitize.isSanitizerEnabled(cfi) &&
671 !sanitize.isSanitizerEnabled(scs)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800672}
673
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700674func (sanitize *sanitize) isVariantOnProductionDevice() bool {
675 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700676 !sanitize.isSanitizerEnabled(hwasan) &&
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700677 !sanitize.isSanitizerEnabled(tsan)
678}
679
Colin Cross16b23492016-01-06 14:41:07 -0800680func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
681 switch t {
682 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700683 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700684 if !b {
685 sanitize.Properties.Sanitize.Coverage = nil
686 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700687 case hwasan:
688 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800689 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700690 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700691 case intOverflow:
692 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000693 case cfi:
694 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800695 case scs:
696 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800697 default:
698 panic(fmt.Errorf("unknown sanitizerType %d", t))
699 }
700 if b {
701 sanitize.Properties.SanitizerEnabled = true
702 }
703}
704
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000705// Check if the sanitizer is explicitly disabled (as opposed to nil by
706// virtue of not being set).
707func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
708 if sanitize == nil {
709 return false
710 }
711
712 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
713 return sanitizerVal != nil && *sanitizerVal == false
714}
715
716// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
717// because enabling a sanitizer either directly (via the blueprint) or
718// indirectly (via a mutator) sets the bool ptr to true, and you can't
719// distinguish between the cases. It isn't needed though - both cases can be
720// treated identically.
721func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
722 if sanitize == nil {
723 return false
724 }
725
726 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
727 return sanitizerVal != nil && *sanitizerVal == true
728}
729
Colin Cross6b753602018-06-21 13:03:07 -0700730func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
731 t, ok := tag.(dependencyTag)
732 return ok && t.library || t == reuseObjTag
733}
734
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700735// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700736func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
737 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000738 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700739 mctx.WalkDeps(func(child, parent android.Module) bool {
740 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
741 return false
742 }
743 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800744 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000745 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800746 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700747 if d.static() {
748 d.sanitize.Properties.SanitizeDep = true
749 }
750 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000751 d.sanitize.Properties.SanitizeDep = true
752 }
Colin Cross16b23492016-01-06 14:41:07 -0800753 }
Colin Cross6b753602018-06-21 13:03:07 -0700754 return true
Colin Cross16b23492016-01-06 14:41:07 -0800755 })
756 }
757 }
758}
759
Ivan Lozano30c5db22018-02-21 15:49:20 -0800760// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700761func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
762 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
763 mctx.WalkDeps(func(child, parent android.Module) bool {
764 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
765 return false
766 }
767 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800768
Colin Cross6b753602018-06-21 13:03:07 -0700769 if enableMinimalRuntime(d.sanitize) {
770 // If a static dependency is built with the minimal runtime,
771 // make sure we include the ubsan minimal runtime.
772 c.sanitize.Properties.MinimalRuntimeDep = true
773 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
774 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
775 // If a static dependency runs with full ubsan diagnostics,
776 // make sure we include the ubsan runtime.
777 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800778 }
Colin Cross6b753602018-06-21 13:03:07 -0700779 }
780 return true
781 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800782 }
783}
784
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000785// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700786func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
787 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000788 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000789 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700790 modules := mctx.CreateVariations(t.String())
791 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000792 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
793 // Save original sanitizer status before we assign values to variant
794 // 0 as that overwrites the original.
795 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
796
Colin Crossb0f28952016-09-19 16:46:53 -0700797 modules := mctx.CreateVariations("", t.String())
798 modules[0].(*Module).sanitize.SetSanitizer(t, false)
799 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000800
Colin Crossb0f28952016-09-19 16:46:53 -0700801 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
802 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800803
804 // We don't need both variants active for anything but CFI-enabled
805 // target static libraries, so suppress the appropriate variant in
806 // all other cases.
807 if t == cfi {
808 if c.static() {
809 if !mctx.Device() {
810 if isSanitizerEnabled {
811 modules[0].(*Module).Properties.PreventInstall = true
812 modules[0].(*Module).Properties.HideFromMake = true
813 } else {
814 modules[1].(*Module).Properties.PreventInstall = true
815 modules[1].(*Module).Properties.HideFromMake = true
816 }
817 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800818 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800819
820 cfiStaticLibsMutex.Lock()
821 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
822 cfiStaticLibsMutex.Unlock()
823 }
824 } else {
825 modules[0].(*Module).Properties.PreventInstall = true
826 modules[0].(*Module).Properties.HideFromMake = true
827 }
828 } else if t == asan {
829 if mctx.Device() {
830 // CFI and ASAN are currently mutually exclusive so disable
831 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000832 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
833 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
834 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700835 if isSanitizerEnabled {
836 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800837 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700838 } else {
839 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800840 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700841 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800842 } else if t == scs {
843 // We don't currently link any static libraries built with make into
844 // libraries built with SCS, so we don't need logic for propagating
845 // SCSness of dependencies into make.
846 if !c.static() {
847 if isSanitizerEnabled {
848 modules[0].(*Module).Properties.PreventInstall = true
849 modules[0].(*Module).Properties.HideFromMake = true
850 } else {
851 modules[1].(*Module).Properties.PreventInstall = true
852 modules[1].(*Module).Properties.HideFromMake = true
853 }
854 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700855 } else if t == hwasan {
856 if mctx.Device() {
857 // CFI and HWASAN are currently mutually exclusive so disable
858 // CFI if this is an HWASAN variant.
859 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
860 }
861
862 if c.static() {
863 if c.useVndk() {
864 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
865 hwasanStaticLibsMutex.Lock()
866 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
867 hwasanStaticLibsMutex.Unlock()
868 } else {
869 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
870 hwasanStaticLibsMutex.Lock()
871 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
872 hwasanStaticLibsMutex.Unlock()
873 }
874 } else {
875 if isSanitizerEnabled {
876 modules[0].(*Module).Properties.PreventInstall = true
877 modules[0].(*Module).Properties.HideFromMake = true
878 } else {
879 modules[1].(*Module).Properties.PreventInstall = true
880 modules[1].(*Module).Properties.HideFromMake = true
881 }
882 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700883 }
Colin Cross16b23492016-01-06 14:41:07 -0800884 }
885 c.sanitize.Properties.SanitizeDep = false
886 }
887 }
888}
Vishwath Mohane7128792017-11-17 11:08:10 -0800889
890func cfiStaticLibs(config android.Config) *[]string {
891 return config.Once("cfiStaticLibs", func() interface{} {
892 return &[]string{}
893 }).(*[]string)
894}
895
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700896func hwasanStaticLibs(config android.Config) *[]string {
897 return config.Once("hwasanStaticLibs", func() interface{} {
898 return &[]string{}
899 }).(*[]string)
900}
901
902func hwasanVendorStaticLibs(config android.Config) *[]string {
903 return config.Once("hwasanVendorStaticLibs", func() interface{} {
904 return &[]string{}
905 }).(*[]string)
906}
907
Ivan Lozano30c5db22018-02-21 15:49:20 -0800908func enableMinimalRuntime(sanitize *sanitize) bool {
909 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700910 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -0800911 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
912 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
913 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
914 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
915 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
916 return true
917 }
918 return false
919}
920
Vishwath Mohane7128792017-11-17 11:08:10 -0800921func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
922 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -0800923 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -0800924 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
925}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700926
927func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
928 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
929 sort.Strings(*hwasanStaticLibs)
930 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
931
932 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
933 sort.Strings(*hwasanVendorStaticLibs)
934 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
935}