blob: 60ba5dce05093dd1fd9a7e684f2f42c4b887af15 [file] [log] [blame]
Colin Cross16b23492016-01-06 14:41:07 -08001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
18 "fmt"
Jeff Gaston72765392017-11-28 16:37:53 -080019 "sort"
Colin Cross16b23492016-01-06 14:41:07 -080020 "strings"
Vishwath Mohane7128792017-11-17 11:08:10 -080021 "sync"
Colin Cross16b23492016-01-06 14:41:07 -080022
Colin Cross6b753602018-06-21 13:03:07 -070023 "github.com/google/blueprint"
24
Colin Cross635c3b02016-05-18 15:37:25 -070025 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070026 "android/soong/cc/config"
Colin Cross16b23492016-01-06 14:41:07 -080027)
28
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070029var (
30 // Any C flags added by sanitizer which libTooling tools may not
31 // understand also need to be added to ClangLibToolingUnknownCflags in
32 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080033
Yi Kong20233a42019-08-21 01:38:40 -070034 asanCflags = []string{
35 "-fno-omit-frame-pointer",
36 "-fno-experimental-new-pass-manager",
37 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070038 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070039
Peter Collingbourne967511a2019-03-19 21:39:54 -070040 hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080041 // The following improves debug location information
42 // availability at the cost of its accuracy. It increases
43 // the likelihood of a stack variable's frame offset
44 // to be recorded in the debug info, which is important
45 // for the quality of hwasan reports. The downside is a
46 // higher number of "optimized out" stack variables.
47 // b/112437883.
48 "-mllvm", "-instcombine-lower-dbg-declare=0",
49 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070050
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000051 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070052 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070053 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
54 // used, but have no effect on assembly files
55 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070056 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070057 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070058 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
59 cfiStaticLibsMutex sync.Mutex
60 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070061
Evgenii Stepanov98f5b062018-11-29 15:12:51 -080062 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080063
Peter Collingbournebd19db02019-03-06 10:38:48 -080064 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070065 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070066 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
67 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070068)
69
Colin Cross16b23492016-01-06 14:41:07 -080070type sanitizerType int
71
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070072func boolPtr(v bool) *bool {
73 if v {
74 return &v
75 } else {
76 return nil
77 }
78}
79
Colin Cross16b23492016-01-06 14:41:07 -080080const (
81 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070082 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080083 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070084 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000085 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080086 scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -070087 fuzzer
Colin Cross16b23492016-01-06 14:41:07 -080088)
89
Jiyong Park82226632019-02-01 10:50:50 +090090// Name of the sanitizer variation for this sanitizer type
91func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080092 switch t {
93 case asan:
94 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070095 case hwasan:
96 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080097 case tsan:
98 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070099 case intOverflow:
100 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000101 case cfi:
102 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800103 case scs:
104 return "scs"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700105 case fuzzer:
106 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800107 default:
108 panic(fmt.Errorf("unknown sanitizerType %d", t))
109 }
110}
111
Jiyong Park82226632019-02-01 10:50:50 +0900112// This is the sanitizer names in SANITIZE_[TARGET|HOST]
113func (t sanitizerType) name() string {
114 switch t {
115 case asan:
116 return "address"
117 case hwasan:
118 return "hwaddress"
119 case tsan:
120 return "thread"
121 case intOverflow:
122 return "integer_overflow"
123 case cfi:
124 return "cfi"
125 case scs:
126 return "shadow-call-stack"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700127 case fuzzer:
128 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900129 default:
130 panic(fmt.Errorf("unknown sanitizerType %d", t))
131 }
132}
133
Jiyong Park1d1119f2019-07-29 21:27:18 +0900134func (t sanitizerType) incompatibleWithCfi() bool {
135 return t == asan || t == fuzzer || t == hwasan
136}
137
Colin Cross16b23492016-01-06 14:41:07 -0800138type SanitizeProperties struct {
139 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
140 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800141 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800142
143 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700144 Address *bool `android:"arch_variant"`
145 Thread *bool `android:"arch_variant"`
146 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800147
148 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700149 Undefined *bool `android:"arch_variant"`
150 All_undefined *bool `android:"arch_variant"`
151 Misc_undefined []string `android:"arch_variant"`
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700152 Fuzzer *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700153 Safestack *bool `android:"arch_variant"`
154 Cfi *bool `android:"arch_variant"`
155 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700156 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800157 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800158
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700159 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
160 // Replaces abort() on error with a human-readable error message.
161 // Address and Thread sanitizers always run in diagnostic mode.
162 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700163 Undefined *bool `android:"arch_variant"`
164 Cfi *bool `android:"arch_variant"`
165 Integer_overflow *bool `android:"arch_variant"`
166 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800167 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700168 }
169
170 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800171 Recover []string
172
173 // value to pass to -fsanitize-blacklist
174 Blacklist *string
175 } `android:"arch_variant"`
176
Jiyong Park379de2f2018-12-19 02:47:14 +0900177 SanitizerEnabled bool `blueprint:"mutated"`
178 SanitizeDep bool `blueprint:"mutated"`
179 MinimalRuntimeDep bool `blueprint:"mutated"`
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500180 BuiltinsDep bool `blueprint:"mutated"`
Jiyong Park379de2f2018-12-19 02:47:14 +0900181 UbsanRuntimeDep bool `blueprint:"mutated"`
182 InSanitizerDir bool `blueprint:"mutated"`
183 Sanitizers []string `blueprint:"mutated"`
184 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800185}
186
187type sanitize struct {
188 Properties SanitizeProperties
189}
190
Vishwath Mohane7128792017-11-17 11:08:10 -0800191func init() {
192 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700193 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800194}
195
Colin Cross16b23492016-01-06 14:41:07 -0800196func (sanitize *sanitize) props() []interface{} {
197 return []interface{}{&sanitize.Properties}
198}
199
200func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700201 s := &sanitize.Properties.Sanitize
202
Colin Cross16b23492016-01-06 14:41:07 -0800203 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700204 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800205 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800206 }
207
Doug Hornc32c6b02019-01-17 14:44:05 -0800208 // Sanitizers do not work on Fuchsia yet.
209 if ctx.Fuchsia() {
210 s.Never = BoolPtr(true)
211 }
212
Colin Cross16b23492016-01-06 14:41:07 -0800213 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800214 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800215 return
216 }
217
Colin Cross16b23492016-01-06 14:41:07 -0800218 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700219 var globalSanitizersDiag []string
220
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700221 if ctx.Host() {
222 if !ctx.Windows() {
223 globalSanitizers = ctx.Config().SanitizeHost()
224 }
225 } else {
226 arches := ctx.Config().SanitizeDeviceArch()
227 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
228 globalSanitizers = ctx.Config().SanitizeDevice()
229 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800230 }
231 }
232
Colin Cross16b23492016-01-06 14:41:07 -0800233 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000234 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700235 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
236 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000237 }
Colin Cross16b23492016-01-06 14:41:07 -0800238
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700239 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
240 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000241 }
242
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700243 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
244 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000245 }
246
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700247 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
248 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000249 }
250
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700251 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
252 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700253 }
254
255 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
256 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000257 }
258
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700259 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800260 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700261 s.Cfi = boolPtr(true)
262 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700263 }
264
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700265 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700266 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700267 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700268 s.Integer_overflow = boolPtr(true)
269 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700270 }
271
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700272 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
273 s.Scudo = boolPtr(true)
274 }
275
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700276 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
277 s.Hwaddress = boolPtr(true)
278 }
279
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000280 if len(globalSanitizers) > 0 {
281 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
282 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700283
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700284 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700285 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700286 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700287 s.Diag.Integer_overflow = boolPtr(true)
288 }
289
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700290 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
291 s.Diag.Cfi == nil && Bool(s.Cfi) {
292 s.Diag.Cfi = boolPtr(true)
293 }
294
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700295 if len(globalSanitizersDiag) > 0 {
296 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
297 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700298 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700299
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700300 // Enable CFI for all components in the include paths (for Aarch64 only)
301 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000302 s.Cfi = boolPtr(true)
303 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
304 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700305 }
306 }
307
Elliott Hughesda3a0712020-03-06 16:55:28 -0800308 // Is CFI actually enabled?
309 if !ctx.Config().EnableCFI() {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800310 s.Cfi = nil
311 s.Diag.Cfi = nil
312 }
313
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800314 // Also disable CFI for arm32 until b/35157333 is fixed.
315 if ctx.Arch().ArchType == android.Arm {
316 s.Cfi = nil
317 s.Diag.Cfi = nil
318 }
319
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700320 // HWASan requires AArch64 hardware feature (top-byte-ignore).
321 if ctx.Arch().ArchType != android.Arm64 {
322 s.Hwaddress = nil
323 }
324
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800325 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800326 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800327 s.Scs = nil
328 }
329
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700330 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700331 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700332 s.Cfi = nil
333 s.Diag.Cfi = nil
334 }
335
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500336 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
337 if !ctx.Os().Linux() {
Vishwath Mohane7128792017-11-17 11:08:10 -0800338 s.Cfi = nil
339 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700340 s.Misc_undefined = nil
341 s.Undefined = nil
342 s.All_undefined = nil
343 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800344 }
345
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700346 // Also disable CFI for VNDK variants of components
347 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700348 s.Cfi = nil
349 s.Diag.Cfi = nil
350 }
351
Inseob Kimeec88e12020-01-22 11:11:29 +0900352 // Also disable CFI if building against snapshot.
353 vndkVersion := ctx.DeviceConfig().VndkVersion()
354 if ctx.useVndk() && vndkVersion != "current" && vndkVersion != "" {
355 s.Cfi = nil
356 }
357
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700358 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong1b3348d2020-01-21 15:53:22 -0800359 // Keep libc instrumented so that ramdisk / recovery can run hwasan-instrumented code if necessary.
360 if (ctx.inRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700361 s.Hwaddress = nil
362 }
363
Colin Cross3c344ef2016-07-18 15:44:56 -0700364 if ctx.staticBinary() {
365 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700366 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700367 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800368 }
369
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700370 if Bool(s.All_undefined) {
371 s.Undefined = nil
372 }
373
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700374 if !ctx.toolchain().Is64Bit() {
375 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700376 s.Thread = nil
377 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800378 // TODO(ccross): error for compile_multilib = "32"?
379 }
380
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800381 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700382 Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800383 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700384 sanitize.Properties.SanitizerEnabled = true
385 }
386
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800387 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
388 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700389 s.Scudo = nil
390 }
391
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700392 if Bool(s.Hwaddress) {
393 s.Address = nil
394 s.Thread = nil
395 }
396
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700397 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
398 // mutually incompatible.
399 if Bool(s.Fuzzer) {
400 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800401 }
402}
403
404func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
405 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
406 return deps
407 }
408
409 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700410 if Bool(sanitize.Properties.Sanitize.Address) {
Christopher Ferris753d4a62019-05-09 13:27:02 -0700411 // Compiling asan and having libc_scudo in the same
412 // executable will cause the executable to crash.
413 // Remove libc_scudo since it is only used to override
414 // allocation functions which asan already overrides.
415 _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800416 }
417 }
418
419 return deps
420}
421
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800422func toDisableImplicitIntegerChange(flags []string) bool {
423 // Returns true if any flag is fsanitize*integer, and there is
424 // no explicit flag about sanitize=implicit-integer-sign-change.
425 for _, f := range flags {
426 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
427 return false
428 }
429 }
430 for _, f := range flags {
431 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
432 return true
433 }
434 }
435 return false
436}
437
Colin Cross16b23492016-01-06 14:41:07 -0800438func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700439 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
440 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500441 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
442 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800443
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500444 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800445 flags.Local.LdFlags = append(flags.Local.LdFlags,
446 minimalRuntimePath,
447 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800448 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500449
450 if sanitize.Properties.BuiltinsDep {
451 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
452 }
453
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700454 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800455 return flags
456 }
457
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700458 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700459 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800460 // Frame pointer based unwinder in ASan requires ARM frame setup.
461 // TODO: put in flags?
462 flags.RequiredInstructionSet = "arm"
463 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800464 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
465 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800466
Colin Cross16b23492016-01-06 14:41:07 -0800467 if ctx.Host() {
468 // -nodefaultlibs (provided with libc++) prevents the driver from linking
469 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800470 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800471 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800472 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900473 if ctx.bootstrap() {
474 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
475 } else {
476 flags.DynamicLinker = "/system/bin/linker_asan"
477 }
Colin Cross16b23492016-01-06 14:41:07 -0800478 if flags.Toolchain.Is64Bit() {
479 flags.DynamicLinker += "64"
480 }
481 }
Colin Cross16b23492016-01-06 14:41:07 -0800482 }
483
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700484 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800485 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700486 }
487
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700488 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800489 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700490
491 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800492 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
493 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
494 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
495 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700496
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700497 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
498 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
499 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800500 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
501 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700502
Mitch Phillips74384752019-06-17 10:33:52 -0700503 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800504 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700505
506 // Disable fortify for fuzzing builds. Generally, we'll be building with
507 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800508 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800509
510 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
511 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
512 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
513 // the DT_RUNPATH from the shared library above it, and not the executable,
514 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
515 // DT_RUNPATH here means that transient shared libraries can be found
516 // colocated with their parents.
517 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800518 }
519
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700520 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800521 if ctx.Arch().ArchType == android.Arm {
522 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
523 // to do this on a function basis, so force Thumb on the entire module.
524 flags.RequiredInstructionSet = "thumb"
525 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000526
Colin Cross4af21ed2019-11-04 09:37:55 -0800527 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
528 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000529 // Only append the default visibility flag if -fvisibility has not already been set
530 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800531 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
532 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000533 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800534 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000535
536 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800537 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
538 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000539 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700540 }
541
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700542 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800543 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700544 }
545
Jiyong Park379de2f2018-12-19 02:47:14 +0900546 if len(sanitize.Properties.Sanitizers) > 0 {
547 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800548
Colin Cross4af21ed2019-11-04 09:37:55 -0800549 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
550 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800551 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800552 // Host sanitizers only link symbols in the final executable, so
553 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800554 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
555 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500556
557 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
558 if !ctx.toolchain().Bionic() {
559 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
560 }
561 }
562
563 if enableMinimalRuntime(sanitize) {
564 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
565 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
566 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
567 if !ctx.toolchain().Bionic() {
568 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800569 }
Colin Cross16b23492016-01-06 14:41:07 -0800570 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700571
572 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
573 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800574 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700575 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800576 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700577 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800578 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700579 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800580 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800581 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
582 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800583 }
Colin Cross16b23492016-01-06 14:41:07 -0800584 }
585
Jiyong Park379de2f2018-12-19 02:47:14 +0900586 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800587 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700588 }
589 // FIXME: enable RTTI if diag + (cfi or vptr)
590
Andreas Gampe97071162017-05-08 13:15:23 -0700591 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800592 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700593 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
594 }
595
Ivan Lozano7929bba2018-12-12 09:36:31 -0800596 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800597 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800598 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
599 }
600
Colin Cross635c3b02016-05-18 15:37:25 -0700601 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800602 if blacklist.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800603 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blacklist.String())
Colin Cross16b23492016-01-06 14:41:07 -0800604 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
605 }
606
607 return flags
608}
609
Colin Crossd80cbca2020-02-24 12:01:37 -0800610func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900611 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
612 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800613 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900614 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800615 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900616 }
617 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800618 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900619 }
620 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800621 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900622 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800623 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700624}
625
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700626func (sanitize *sanitize) inSanitizerDir() bool {
627 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700628}
629
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000630func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000631 switch t {
632 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000633 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700634 case hwasan:
635 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000636 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000637 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000638 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000639 return sanitize.Properties.Sanitize.Integer_overflow
640 case cfi:
641 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800642 case scs:
643 return sanitize.Properties.Sanitize.Scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700644 case fuzzer:
645 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000646 default:
647 panic(fmt.Errorf("unknown sanitizerType %d", t))
648 }
649}
650
Dan Albert7d1eecf2018-01-19 12:30:45 -0800651func (sanitize *sanitize) isUnsanitizedVariant() bool {
652 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700653 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800654 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800655 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700656 !sanitize.isSanitizerEnabled(scs) &&
657 !sanitize.isSanitizerEnabled(fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800658}
659
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700660func (sanitize *sanitize) isVariantOnProductionDevice() bool {
661 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700662 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700663 !sanitize.isSanitizerEnabled(tsan) &&
664 !sanitize.isSanitizerEnabled(fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700665}
666
Colin Cross16b23492016-01-06 14:41:07 -0800667func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
668 switch t {
669 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700670 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700671 case hwasan:
672 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800673 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700674 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700675 case intOverflow:
676 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000677 case cfi:
678 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800679 case scs:
680 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700681 case fuzzer:
682 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800683 default:
684 panic(fmt.Errorf("unknown sanitizerType %d", t))
685 }
686 if b {
687 sanitize.Properties.SanitizerEnabled = true
688 }
689}
690
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000691// Check if the sanitizer is explicitly disabled (as opposed to nil by
692// virtue of not being set).
693func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
694 if sanitize == nil {
695 return false
696 }
697
698 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
699 return sanitizerVal != nil && *sanitizerVal == false
700}
701
702// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
703// because enabling a sanitizer either directly (via the blueprint) or
704// indirectly (via a mutator) sets the bool ptr to true, and you can't
705// distinguish between the cases. It isn't needed though - both cases can be
706// treated identically.
707func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
708 if sanitize == nil {
709 return false
710 }
711
712 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
713 return sanitizerVal != nil && *sanitizerVal == true
714}
715
Colin Cross6b753602018-06-21 13:03:07 -0700716func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Ivan Lozano183a3212019-10-18 14:18:45 -0700717 t, ok := tag.(DependencyTag)
718 return ok && t.Library || t == reuseObjTag || t == objDepTag
Colin Cross6b753602018-06-21 13:03:07 -0700719}
720
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700721// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700722func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
723 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000724 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700725 mctx.WalkDeps(func(child, parent android.Module) bool {
726 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
727 return false
728 }
729 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800730 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000731 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800732 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700733 if d.static() {
734 d.sanitize.Properties.SanitizeDep = true
735 }
736 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000737 d.sanitize.Properties.SanitizeDep = true
738 }
Colin Cross16b23492016-01-06 14:41:07 -0800739 }
Colin Cross6b753602018-06-21 13:03:07 -0700740 return true
Colin Cross16b23492016-01-06 14:41:07 -0800741 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900742 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
743 // If an APEX module includes a lib which is enabled for a sanitizer T, then
744 // the APEX module is also enabled for the same sanitizer type.
745 mctx.VisitDirectDeps(func(child android.Module) {
746 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
747 sanitizeable.EnableSanitizer(t.name())
748 }
749 })
Colin Cross16b23492016-01-06 14:41:07 -0800750 }
751 }
752}
753
Ivan Lozano30c5db22018-02-21 15:49:20 -0800754// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700755func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
756 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
757 mctx.WalkDeps(func(child, parent android.Module) bool {
758 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
759 return false
760 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800761
Inseob Kimeec88e12020-01-22 11:11:29 +0900762 d, ok := child.(*Module)
763 if !ok || !d.static() {
764 return false
765 }
766 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700767 if enableMinimalRuntime(d.sanitize) {
768 // If a static dependency is built with the minimal runtime,
769 // make sure we include the ubsan minimal runtime.
770 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +0900771 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -0700772 // If a static dependency runs with full ubsan diagnostics,
773 // make sure we include the ubsan runtime.
774 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800775 }
Colin Cross0b908332019-06-19 23:00:20 -0700776
777 if c.sanitize.Properties.MinimalRuntimeDep &&
778 c.sanitize.Properties.UbsanRuntimeDep {
779 // both flags that this mutator might set are true, so don't bother recursing
780 return false
781 }
782
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500783 if c.Os() == android.Linux {
784 c.sanitize.Properties.BuiltinsDep = true
785 }
786
Colin Cross0b908332019-06-19 23:00:20 -0700787 return true
Colin Cross6b753602018-06-21 13:03:07 -0700788 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900789
790 if p, ok := d.linker.(*vendorSnapshotLibraryDecorator); ok {
791 if Bool(p.properties.Sanitize_minimal_dep) {
792 c.sanitize.Properties.MinimalRuntimeDep = true
793 }
794 if Bool(p.properties.Sanitize_ubsan_dep) {
795 c.sanitize.Properties.UbsanRuntimeDep = true
796 }
797 }
798
799 return false
Colin Cross6b753602018-06-21 13:03:07 -0700800 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800801 }
802}
803
Jiyong Park379de2f2018-12-19 02:47:14 +0900804// Add the dependency to the runtime library for each of the sanitizer variants
805func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900806 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000807 if !c.Enabled() {
808 return
809 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900810 var sanitizers []string
811 var diagSanitizers []string
812
813 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
814 sanitizers = append(sanitizers, "undefined")
815 } else {
816 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
817 sanitizers = append(sanitizers,
818 "bool",
819 "integer-divide-by-zero",
820 "return",
821 "returns-nonnull-attribute",
822 "shift-exponent",
823 "unreachable",
824 "vla-bound",
825 // TODO(danalbert): The following checks currently have compiler performance issues.
826 //"alignment",
827 //"bounds",
828 //"enum",
829 //"float-cast-overflow",
830 //"float-divide-by-zero",
831 //"nonnull-attribute",
832 //"null",
833 //"shift-base",
834 //"signed-integer-overflow",
835 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
836 // https://llvm.org/PR19302
837 // http://reviews.llvm.org/D6974
838 // "object-size",
839 )
840 }
841 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
842 }
843
844 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
845 diagSanitizers = append(diagSanitizers, "undefined")
846 }
847
848 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
849
850 if Bool(c.sanitize.Properties.Sanitize.Address) {
851 sanitizers = append(sanitizers, "address")
852 diagSanitizers = append(diagSanitizers, "address")
853 }
854
855 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
856 sanitizers = append(sanitizers, "hwaddress")
857 }
858
859 if Bool(c.sanitize.Properties.Sanitize.Thread) {
860 sanitizers = append(sanitizers, "thread")
861 }
862
863 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
864 sanitizers = append(sanitizers, "safe-stack")
865 }
866
867 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
868 sanitizers = append(sanitizers, "cfi")
869
870 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
871 diagSanitizers = append(diagSanitizers, "cfi")
872 }
873 }
874
875 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
876 sanitizers = append(sanitizers, "unsigned-integer-overflow")
877 sanitizers = append(sanitizers, "signed-integer-overflow")
878 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
879 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
880 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
881 }
882 }
883
884 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
885 sanitizers = append(sanitizers, "scudo")
886 }
887
888 if Bool(c.sanitize.Properties.Sanitize.Scs) {
889 sanitizers = append(sanitizers, "shadow-call-stack")
890 }
891
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700892 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
893 sanitizers = append(sanitizers, "fuzzer-no-link")
894 }
895
Jiyong Park379de2f2018-12-19 02:47:14 +0900896 // Save the list of sanitizers. These will be used again when generating
897 // the build rules (for Cflags, etc.)
898 c.sanitize.Properties.Sanitizers = sanitizers
899 c.sanitize.Properties.DiagSanitizers = diagSanitizers
900
Ivan Lozanof3b190f2020-03-06 12:01:21 -0500901 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
902 if c.Host() {
903 diagSanitizers = sanitizers
904 }
905
Jiyong Park379de2f2018-12-19 02:47:14 +0900906 // Determine the runtime library required
907 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700908 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +0900909 toolchain := c.toolchain(mctx)
910 if Bool(c.sanitize.Properties.Sanitize.Address) {
911 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
912 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
913 if c.staticBinary() {
914 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700915 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +0900916 } else {
917 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
918 }
919 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
920 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
921 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
922 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
923 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
924 } else {
925 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
926 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700927 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500928 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
929 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
930 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900931 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
932 }
933
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500934 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
935 // UBSan is supported on non-bionic linux host builds as well
Jooyung Han0302a842019-10-30 18:43:49 +0900936 if isLlndkLibrary(runtimeLibrary, mctx.Config()) && !c.static() && c.UseVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900937 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
938 }
939
940 // Adding dependency to the runtime library. We are using *FarVariation*
941 // because the runtime libraries themselves are not mutated by sanitizer
942 // mutators and thus don't have sanitizer variants whereas this module
943 // has been already mutated.
944 //
945 // Note that by adding dependency with {static|shared}DepTag, the lib is
946 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
947 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +0900948 deps := append(extraStaticDeps, runtimeLibrary)
949 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
950 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
951 snapshots := vendorSnapshotStaticLibs(mctx.Config())
952 for idx, dep := range deps {
953 if lib, ok := snapshots.get(dep, mctx.Arch().ArchType); ok {
954 deps[idx] = lib
955 }
956 }
957 }
958
Jiyong Park379de2f2018-12-19 02:47:14 +0900959 // static executable gets static runtime libs
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700960 mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{
Jiyong Park379de2f2018-12-19 02:47:14 +0900961 {Mutator: "link", Variation: "static"},
Colin Cross7228ecd2019-11-18 16:00:16 -0800962 c.ImageVariation(),
Inseob Kimeec88e12020-01-22 11:11:29 +0900963 }...), StaticDepTag, deps...)
Jiyong Park1d1119f2019-07-29 21:27:18 +0900964 } else if !c.static() && !c.header() {
Inseob Kimeec88e12020-01-22 11:11:29 +0900965 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
966 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
967 snapshots := vendorSnapshotSharedLibs(mctx.Config())
968 if lib, ok := snapshots.get(runtimeLibrary, mctx.Arch().ArchType); ok {
969 runtimeLibrary = lib
970 }
971 }
972
Jiyong Park3b1746a2019-01-29 11:15:04 +0900973 // dynamic executable and shared libs get shared runtime libs
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700974 mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{
Jiyong Park379de2f2018-12-19 02:47:14 +0900975 {Mutator: "link", Variation: "shared"},
Colin Cross7228ecd2019-11-18 16:00:16 -0800976 c.ImageVariation(),
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700977 }...), earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900978 }
979 // static lib does not have dependency to the runtime library. The
980 // dependency will be added to the executables or shared libs using
981 // the static lib.
982 }
983 }
984}
985
986type Sanitizeable interface {
987 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900988 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900989 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900990}
991
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000992// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700993func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
994 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000995 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000996 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900997 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700998 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000999 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001000 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001001 if c.static() || c.header() || t == asan || t == fuzzer {
1002 // Static and header libs are split into non-sanitized and sanitized variants.
1003 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1004 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1005 // that isn't sanitized for asan/fuzzer.
1006 //
1007 // Note for defaultVariation: since we don't split for shared libs but for static/header
1008 // libs, it is possible for the sanitized variant of a static/header lib to depend
1009 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1010 // error when the module is split. defaultVariation is the name of the variation that
1011 // will be used when such a dangling dependency occurs during the split of the current
1012 // module. By setting it to the name of the sanitized variation, the dangling dependency
1013 // is redirected to the sanitized variant of the dependent module.
1014 defaultVariation := t.variationName()
1015 mctx.SetDefaultDependencyVariation(&defaultVariation)
1016 modules := mctx.CreateVariations("", t.variationName())
1017 modules[0].(*Module).sanitize.SetSanitizer(t, false)
1018 modules[1].(*Module).sanitize.SetSanitizer(t, true)
1019 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
1020 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001021
Ivan Lozano4774a812020-03-10 16:23:57 -04001022 if mctx.Device() && t.incompatibleWithCfi() {
1023 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1024 // are incompatible with cfi
1025 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
1026 }
1027
Jiyong Park1d1119f2019-07-29 21:27:18 +09001028 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1029 // to Make, because the sanitized version has a different suffix in name.
1030 // For other types of sanitizers, suppress the variation that is disabled.
1031 if t != cfi && t != scs && t != hwasan {
1032 if isSanitizerEnabled {
1033 modules[0].(*Module).Properties.PreventInstall = true
1034 modules[0].(*Module).Properties.HideFromMake = true
1035 } else {
1036 modules[1].(*Module).Properties.PreventInstall = true
1037 modules[1].(*Module).Properties.HideFromMake = true
1038 }
1039 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001040
Jiyong Park1d1119f2019-07-29 21:27:18 +09001041 // Export the static lib name to make
Vishwath Mohane7128792017-11-17 11:08:10 -08001042 if c.static() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001043 if t == cfi {
1044 appendStringSync(c.Name(), cfiStaticLibs(mctx.Config()), &cfiStaticLibsMutex)
1045 } else if t == hwasan {
Ivan Lozano52767be2019-10-18 14:49:46 -07001046 if c.UseVndk() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001047 appendStringSync(c.Name(), hwasanVendorStaticLibs(mctx.Config()),
1048 &hwasanStaticLibsMutex)
Vishwath Mohane7128792017-11-17 11:08:10 -08001049 } else {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001050 appendStringSync(c.Name(), hwasanStaticLibs(mctx.Config()),
1051 &hwasanStaticLibsMutex)
Vishwath Mohane7128792017-11-17 11:08:10 -08001052 }
Jiyong Park1d1119f2019-07-29 21:27:18 +09001053 }
1054 }
1055 } else {
1056 // Shared libs are not split. Only the sanitized variant is created.
1057 modules := mctx.CreateVariations(t.variationName())
1058 modules[0].(*Module).sanitize.SetSanitizer(t, true)
1059 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -08001060
Jiyong Park1d1119f2019-07-29 21:27:18 +09001061 // locate the asan libraries under /data/asan
1062 if mctx.Device() && t == asan && isSanitizerEnabled {
1063 modules[0].(*Module).sanitize.Properties.InSanitizerDir = true
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001064 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001065
1066 if mctx.Device() && t.incompatibleWithCfi() {
1067 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1068 // are incompatible with cfi
1069 modules[0].(*Module).sanitize.SetSanitizer(cfi, false)
1070 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001071 }
Colin Cross16b23492016-01-06 14:41:07 -08001072 }
1073 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +09001074 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001075 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +09001076 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -08001077 }
1078 }
1079}
Vishwath Mohane7128792017-11-17 11:08:10 -08001080
Colin Cross571cccf2019-02-04 11:22:08 -08001081var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1082
Vishwath Mohane7128792017-11-17 11:08:10 -08001083func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001084 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -08001085 return &[]string{}
1086 }).(*[]string)
1087}
1088
Colin Cross571cccf2019-02-04 11:22:08 -08001089var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1090
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001091func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001092 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001093 return &[]string{}
1094 }).(*[]string)
1095}
1096
Colin Cross571cccf2019-02-04 11:22:08 -08001097var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
1098
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001099func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001100 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001101 return &[]string{}
1102 }).(*[]string)
1103}
1104
Jiyong Park1d1119f2019-07-29 21:27:18 +09001105func appendStringSync(item string, list *[]string, mutex *sync.Mutex) {
1106 mutex.Lock()
1107 *list = append(*list, item)
1108 mutex.Unlock()
1109}
1110
Ivan Lozano30c5db22018-02-21 15:49:20 -08001111func enableMinimalRuntime(sanitize *sanitize) bool {
1112 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001113 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001114 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001115
Ivan Lozano30c5db22018-02-21 15:49:20 -08001116 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001117 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1118 Bool(sanitize.Properties.Sanitize.Undefined) ||
1119 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1120
Ivan Lozano30c5db22018-02-21 15:49:20 -08001121 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1122 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001123 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001124 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001125
Ivan Lozano30c5db22018-02-21 15:49:20 -08001126 return true
1127 }
1128 return false
1129}
1130
Inseob Kim8471cda2019-11-15 09:59:12 +09001131func enableUbsanRuntime(sanitize *sanitize) bool {
1132 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001133 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001134 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1135}
1136
Vishwath Mohane7128792017-11-17 11:08:10 -08001137func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1138 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001139 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001140 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1141}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001142
1143func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1144 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1145 sort.Strings(*hwasanStaticLibs)
1146 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1147
1148 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1149 sort.Strings(*hwasanVendorStaticLibs)
1150 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1151}