blob: 463a02ac2590c4fa71abe5989a52dfe28089cd5e [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 Stepanov1c69e832019-06-14 18:37:33 -070041 "-fsanitize-hwaddress-abi=platform",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080042 "-fno-experimental-new-pass-manager",
43 // The following improves debug location information
44 // availability at the cost of its accuracy. It increases
45 // the likelihood of a stack variable's frame offset
46 // to be recorded in the debug info, which is important
47 // for the quality of hwasan reports. The downside is a
48 // higher number of "optimized out" stack variables.
49 // b/112437883.
50 "-mllvm", "-instcombine-lower-dbg-declare=0",
Mitch Phillipsb1c574f2020-06-22 13:28:23 -070051 // TODO(b/159343917): HWASan and GlobalISel don't play nicely, and
52 // GlobalISel is the default at -O0 on aarch64.
53 "-mllvm", "--aarch64-enable-global-isel-at-O=-1",
54 "-mllvm", "-fast-isel=false",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080055 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070056
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000057 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070058 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070059 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
60 // used, but have no effect on assembly files
61 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070062 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070063 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070064 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
65 cfiStaticLibsMutex sync.Mutex
66 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070067
Evgenii Stepanov98f5b062018-11-29 15:12:51 -080068 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080069
Peter Collingbournebd19db02019-03-06 10:38:48 -080070 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070071 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070072 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
73 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070074)
75
Colin Cross16b23492016-01-06 14:41:07 -080076type sanitizerType int
77
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070078func boolPtr(v bool) *bool {
79 if v {
80 return &v
81 } else {
82 return nil
83 }
84}
85
Colin Cross16b23492016-01-06 14:41:07 -080086const (
87 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070088 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080089 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070090 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000091 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080092 scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -070093 fuzzer
Colin Cross16b23492016-01-06 14:41:07 -080094)
95
Jiyong Park82226632019-02-01 10:50:50 +090096// Name of the sanitizer variation for this sanitizer type
97func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080098 switch t {
99 case asan:
100 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700101 case hwasan:
102 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800103 case tsan:
104 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700105 case intOverflow:
106 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000107 case cfi:
108 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800109 case scs:
110 return "scs"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700111 case fuzzer:
112 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800113 default:
114 panic(fmt.Errorf("unknown sanitizerType %d", t))
115 }
116}
117
Jiyong Park82226632019-02-01 10:50:50 +0900118// This is the sanitizer names in SANITIZE_[TARGET|HOST]
119func (t sanitizerType) name() string {
120 switch t {
121 case asan:
122 return "address"
123 case hwasan:
124 return "hwaddress"
125 case tsan:
126 return "thread"
127 case intOverflow:
128 return "integer_overflow"
129 case cfi:
130 return "cfi"
131 case scs:
132 return "shadow-call-stack"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700133 case fuzzer:
134 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900135 default:
136 panic(fmt.Errorf("unknown sanitizerType %d", t))
137 }
138}
139
Jiyong Park1d1119f2019-07-29 21:27:18 +0900140func (t sanitizerType) incompatibleWithCfi() bool {
141 return t == asan || t == fuzzer || t == hwasan
142}
143
Colin Cross16b23492016-01-06 14:41:07 -0800144type SanitizeProperties struct {
145 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
146 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800147 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800148
149 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700150 Address *bool `android:"arch_variant"`
151 Thread *bool `android:"arch_variant"`
152 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800153
154 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700155 Undefined *bool `android:"arch_variant"`
156 All_undefined *bool `android:"arch_variant"`
157 Misc_undefined []string `android:"arch_variant"`
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700158 Fuzzer *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700159 Safestack *bool `android:"arch_variant"`
160 Cfi *bool `android:"arch_variant"`
161 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700162 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800163 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800164
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700165 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
166 // Replaces abort() on error with a human-readable error message.
167 // Address and Thread sanitizers always run in diagnostic mode.
168 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700169 Undefined *bool `android:"arch_variant"`
170 Cfi *bool `android:"arch_variant"`
171 Integer_overflow *bool `android:"arch_variant"`
172 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800173 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700174 }
175
176 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800177 Recover []string
178
179 // value to pass to -fsanitize-blacklist
180 Blacklist *string
181 } `android:"arch_variant"`
182
Jiyong Park379de2f2018-12-19 02:47:14 +0900183 SanitizerEnabled bool `blueprint:"mutated"`
184 SanitizeDep bool `blueprint:"mutated"`
185 MinimalRuntimeDep bool `blueprint:"mutated"`
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500186 BuiltinsDep bool `blueprint:"mutated"`
Jiyong Park379de2f2018-12-19 02:47:14 +0900187 UbsanRuntimeDep bool `blueprint:"mutated"`
188 InSanitizerDir bool `blueprint:"mutated"`
189 Sanitizers []string `blueprint:"mutated"`
190 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800191}
192
193type sanitize struct {
194 Properties SanitizeProperties
195}
196
Vishwath Mohane7128792017-11-17 11:08:10 -0800197func init() {
198 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700199 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800200}
201
Colin Cross16b23492016-01-06 14:41:07 -0800202func (sanitize *sanitize) props() []interface{} {
203 return []interface{}{&sanitize.Properties}
204}
205
206func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700207 s := &sanitize.Properties.Sanitize
208
Colin Cross16b23492016-01-06 14:41:07 -0800209 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700210 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800211 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800212 }
213
Doug Hornc32c6b02019-01-17 14:44:05 -0800214 // Sanitizers do not work on Fuchsia yet.
215 if ctx.Fuchsia() {
216 s.Never = BoolPtr(true)
217 }
218
Colin Cross16b23492016-01-06 14:41:07 -0800219 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800220 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800221 return
222 }
223
Colin Cross16b23492016-01-06 14:41:07 -0800224 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700225 var globalSanitizersDiag []string
226
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700227 if ctx.Host() {
228 if !ctx.Windows() {
229 globalSanitizers = ctx.Config().SanitizeHost()
230 }
231 } else {
232 arches := ctx.Config().SanitizeDeviceArch()
233 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
234 globalSanitizers = ctx.Config().SanitizeDevice()
235 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800236 }
237 }
238
Colin Cross16b23492016-01-06 14:41:07 -0800239 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000240 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700241 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
242 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000243 }
Colin Cross16b23492016-01-06 14:41:07 -0800244
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700245 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
246 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000247 }
248
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700249 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
250 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000251 }
252
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700253 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
254 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000255 }
256
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700257 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
258 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700259 }
260
261 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
262 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000263 }
264
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700265 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800266 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700267 s.Cfi = boolPtr(true)
268 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700269 }
270
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700271 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700272 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700273 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700274 s.Integer_overflow = boolPtr(true)
275 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700276 }
277
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700278 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
279 s.Scudo = boolPtr(true)
280 }
281
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700282 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
283 s.Hwaddress = boolPtr(true)
284 }
285
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000286 if len(globalSanitizers) > 0 {
287 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
288 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700289
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700290 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700291 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700292 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700293 s.Diag.Integer_overflow = boolPtr(true)
294 }
295
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700296 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
297 s.Diag.Cfi == nil && Bool(s.Cfi) {
298 s.Diag.Cfi = boolPtr(true)
299 }
300
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700301 if len(globalSanitizersDiag) > 0 {
302 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
303 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700304 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700305
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700306 // Enable CFI for all components in the include paths (for Aarch64 only)
307 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000308 s.Cfi = boolPtr(true)
309 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
310 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700311 }
312 }
313
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800314 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800315 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800316 s.Cfi = nil
317 s.Diag.Cfi = nil
318 }
319
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800320 // Also disable CFI for arm32 until b/35157333 is fixed.
321 if ctx.Arch().ArchType == android.Arm {
322 s.Cfi = nil
323 s.Diag.Cfi = nil
324 }
325
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700326 // HWASan requires AArch64 hardware feature (top-byte-ignore).
327 if ctx.Arch().ArchType != android.Arm64 {
328 s.Hwaddress = nil
329 }
330
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800331 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800332 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800333 s.Scs = nil
334 }
335
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700336 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700337 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700338 s.Cfi = nil
339 s.Diag.Cfi = nil
340 }
341
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500342 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
343 if !ctx.Os().Linux() {
Vishwath Mohane7128792017-11-17 11:08:10 -0800344 s.Cfi = nil
345 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700346 s.Misc_undefined = nil
347 s.Undefined = nil
348 s.All_undefined = nil
349 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800350 }
351
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700352 // Also disable CFI for VNDK variants of components
353 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700354 s.Cfi = nil
355 s.Diag.Cfi = nil
356 }
357
Inseob Kimeec88e12020-01-22 11:11:29 +0900358 // Also disable CFI if building against snapshot.
359 vndkVersion := ctx.DeviceConfig().VndkVersion()
360 if ctx.useVndk() && vndkVersion != "current" && vndkVersion != "" {
361 s.Cfi = nil
362 }
363
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700364 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong1b3348d2020-01-21 15:53:22 -0800365 // Keep libc instrumented so that ramdisk / recovery can run hwasan-instrumented code if necessary.
366 if (ctx.inRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700367 s.Hwaddress = nil
368 }
369
Colin Cross3c344ef2016-07-18 15:44:56 -0700370 if ctx.staticBinary() {
371 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700372 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700373 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800374 }
375
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700376 if Bool(s.All_undefined) {
377 s.Undefined = nil
378 }
379
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700380 if !ctx.toolchain().Is64Bit() {
381 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700382 s.Thread = nil
383 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800384 // TODO(ccross): error for compile_multilib = "32"?
385 }
386
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800387 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 -0700388 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 -0800389 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700390 sanitize.Properties.SanitizerEnabled = true
391 }
392
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800393 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
394 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700395 s.Scudo = nil
396 }
397
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700398 if Bool(s.Hwaddress) {
399 s.Address = nil
400 s.Thread = nil
401 }
402
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700403 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
404 // mutually incompatible.
405 if Bool(s.Fuzzer) {
406 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800407 }
408}
409
410func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
411 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
412 return deps
413 }
414
415 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700416 if Bool(sanitize.Properties.Sanitize.Address) {
Christopher Ferris753d4a62019-05-09 13:27:02 -0700417 // Compiling asan and having libc_scudo in the same
418 // executable will cause the executable to crash.
419 // Remove libc_scudo since it is only used to override
420 // allocation functions which asan already overrides.
421 _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800422 }
423 }
424
425 return deps
426}
427
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800428func toDisableImplicitIntegerChange(flags []string) bool {
429 // Returns true if any flag is fsanitize*integer, and there is
430 // no explicit flag about sanitize=implicit-integer-sign-change.
431 for _, f := range flags {
432 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
433 return false
434 }
435 }
436 for _, f := range flags {
437 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
438 return true
439 }
440 }
441 return false
442}
443
Colin Cross16b23492016-01-06 14:41:07 -0800444func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700445 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
446 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500447 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
448 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800449
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500450 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800451 flags.Local.LdFlags = append(flags.Local.LdFlags,
452 minimalRuntimePath,
453 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800454 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500455
456 if sanitize.Properties.BuiltinsDep {
457 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
458 }
459
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700460 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800461 return flags
462 }
463
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700464 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700465 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800466 // Frame pointer based unwinder in ASan requires ARM frame setup.
467 // TODO: put in flags?
468 flags.RequiredInstructionSet = "arm"
469 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800470 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
471 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800472
Colin Cross16b23492016-01-06 14:41:07 -0800473 if ctx.Host() {
474 // -nodefaultlibs (provided with libc++) prevents the driver from linking
475 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800476 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800477 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800478 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900479 if ctx.bootstrap() {
480 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
481 } else {
482 flags.DynamicLinker = "/system/bin/linker_asan"
483 }
Colin Cross16b23492016-01-06 14:41:07 -0800484 if flags.Toolchain.Is64Bit() {
485 flags.DynamicLinker += "64"
486 }
487 }
Colin Cross16b23492016-01-06 14:41:07 -0800488 }
489
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700490 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800491 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700492 }
493
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700494 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800495 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700496
497 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800498 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
499 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
500 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
501 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700502
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700503 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
504 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
505 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800506 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
507 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700508
Mitch Phillips74384752019-06-17 10:33:52 -0700509 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800510 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700511
512 // Disable fortify for fuzzing builds. Generally, we'll be building with
513 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800514 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800515
516 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
517 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
518 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
519 // the DT_RUNPATH from the shared library above it, and not the executable,
520 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
521 // DT_RUNPATH here means that transient shared libraries can be found
522 // colocated with their parents.
523 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800524 }
525
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700526 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800527 if ctx.Arch().ArchType == android.Arm {
528 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
529 // to do this on a function basis, so force Thumb on the entire module.
530 flags.RequiredInstructionSet = "thumb"
531 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000532
Colin Cross4af21ed2019-11-04 09:37:55 -0800533 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
534 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000535 // Only append the default visibility flag if -fvisibility has not already been set
536 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800537 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
538 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000539 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800540 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000541
542 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800543 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
544 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000545 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700546 }
547
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700548 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800549 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700550 }
551
Jiyong Park379de2f2018-12-19 02:47:14 +0900552 if len(sanitize.Properties.Sanitizers) > 0 {
553 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800554
Colin Cross4af21ed2019-11-04 09:37:55 -0800555 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
556 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800557 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800558 // Host sanitizers only link symbols in the final executable, so
559 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800560 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
561 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500562
563 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
564 if !ctx.toolchain().Bionic() {
565 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
566 }
567 }
568
569 if enableMinimalRuntime(sanitize) {
570 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
571 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
572 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
573 if !ctx.toolchain().Bionic() {
574 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800575 }
Colin Cross16b23492016-01-06 14:41:07 -0800576 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700577
578 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
579 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800580 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700581 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800582 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700583 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800584 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700585 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800586 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800587 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
588 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800589 }
Colin Cross16b23492016-01-06 14:41:07 -0800590 }
591
Jiyong Park379de2f2018-12-19 02:47:14 +0900592 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800593 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700594 }
595 // FIXME: enable RTTI if diag + (cfi or vptr)
596
Andreas Gampe97071162017-05-08 13:15:23 -0700597 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800598 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700599 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
600 }
601
Ivan Lozano7929bba2018-12-12 09:36:31 -0800602 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800603 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800604 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
605 }
606
Colin Cross635c3b02016-05-18 15:37:25 -0700607 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800608 if blacklist.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800609 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blacklist.String())
Colin Cross16b23492016-01-06 14:41:07 -0800610 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
611 }
612
613 return flags
614}
615
Colin Crossdc7319d2020-02-24 12:01:37 -0800616func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900617 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
618 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossdc7319d2020-02-24 12:01:37 -0800619 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900620 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossdc7319d2020-02-24 12:01:37 -0800621 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900622 }
623 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossdc7319d2020-02-24 12:01:37 -0800624 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900625 }
626 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossdc7319d2020-02-24 12:01:37 -0800627 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900628 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800629 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700630}
631
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700632func (sanitize *sanitize) inSanitizerDir() bool {
633 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700634}
635
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000636func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000637 switch t {
638 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000639 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700640 case hwasan:
641 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000642 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000643 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000644 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000645 return sanitize.Properties.Sanitize.Integer_overflow
646 case cfi:
647 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800648 case scs:
649 return sanitize.Properties.Sanitize.Scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700650 case fuzzer:
651 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000652 default:
653 panic(fmt.Errorf("unknown sanitizerType %d", t))
654 }
655}
656
Dan Albert7d1eecf2018-01-19 12:30:45 -0800657func (sanitize *sanitize) isUnsanitizedVariant() bool {
658 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700659 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800660 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800661 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700662 !sanitize.isSanitizerEnabled(scs) &&
663 !sanitize.isSanitizerEnabled(fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800664}
665
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700666func (sanitize *sanitize) isVariantOnProductionDevice() bool {
667 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700668 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700669 !sanitize.isSanitizerEnabled(tsan) &&
670 !sanitize.isSanitizerEnabled(fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700671}
672
Colin Cross16b23492016-01-06 14:41:07 -0800673func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
674 switch t {
675 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700676 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700677 case hwasan:
678 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800679 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700680 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700681 case intOverflow:
682 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000683 case cfi:
684 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800685 case scs:
686 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700687 case fuzzer:
688 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800689 default:
690 panic(fmt.Errorf("unknown sanitizerType %d", t))
691 }
692 if b {
693 sanitize.Properties.SanitizerEnabled = true
694 }
695}
696
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000697// Check if the sanitizer is explicitly disabled (as opposed to nil by
698// virtue of not being set).
699func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
700 if sanitize == nil {
701 return false
702 }
703
704 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
705 return sanitizerVal != nil && *sanitizerVal == false
706}
707
708// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
709// because enabling a sanitizer either directly (via the blueprint) or
710// indirectly (via a mutator) sets the bool ptr to true, and you can't
711// distinguish between the cases. It isn't needed though - both cases can be
712// treated identically.
713func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
714 if sanitize == nil {
715 return false
716 }
717
718 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
719 return sanitizerVal != nil && *sanitizerVal == true
720}
721
Colin Cross6b753602018-06-21 13:03:07 -0700722func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Ivan Lozano183a3212019-10-18 14:18:45 -0700723 t, ok := tag.(DependencyTag)
724 return ok && t.Library || t == reuseObjTag || t == objDepTag
Colin Cross6b753602018-06-21 13:03:07 -0700725}
726
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700727// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700728func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
729 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000730 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700731 mctx.WalkDeps(func(child, parent android.Module) bool {
732 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
733 return false
734 }
735 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800736 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000737 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800738 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700739 if d.static() {
740 d.sanitize.Properties.SanitizeDep = true
741 }
742 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000743 d.sanitize.Properties.SanitizeDep = true
744 }
Colin Cross16b23492016-01-06 14:41:07 -0800745 }
Colin Cross6b753602018-06-21 13:03:07 -0700746 return true
Colin Cross16b23492016-01-06 14:41:07 -0800747 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900748 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
749 // If an APEX module includes a lib which is enabled for a sanitizer T, then
750 // the APEX module is also enabled for the same sanitizer type.
751 mctx.VisitDirectDeps(func(child android.Module) {
752 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
753 sanitizeable.EnableSanitizer(t.name())
754 }
755 })
Colin Cross16b23492016-01-06 14:41:07 -0800756 }
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 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800767
Inseob Kimeec88e12020-01-22 11:11:29 +0900768 d, ok := child.(*Module)
769 if !ok || !d.static() {
770 return false
771 }
772 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700773 if enableMinimalRuntime(d.sanitize) {
774 // If a static dependency is built with the minimal runtime,
775 // make sure we include the ubsan minimal runtime.
776 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +0900777 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -0700778 // If a static dependency runs with full ubsan diagnostics,
779 // make sure we include the ubsan runtime.
780 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800781 }
Colin Cross0b908332019-06-19 23:00:20 -0700782
783 if c.sanitize.Properties.MinimalRuntimeDep &&
784 c.sanitize.Properties.UbsanRuntimeDep {
785 // both flags that this mutator might set are true, so don't bother recursing
786 return false
787 }
788
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500789 if c.Os() == android.Linux {
790 c.sanitize.Properties.BuiltinsDep = true
791 }
792
Colin Cross0b908332019-06-19 23:00:20 -0700793 return true
Colin Cross6b753602018-06-21 13:03:07 -0700794 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900795
796 if p, ok := d.linker.(*vendorSnapshotLibraryDecorator); ok {
797 if Bool(p.properties.Sanitize_minimal_dep) {
798 c.sanitize.Properties.MinimalRuntimeDep = true
799 }
800 if Bool(p.properties.Sanitize_ubsan_dep) {
801 c.sanitize.Properties.UbsanRuntimeDep = true
802 }
803 }
804
805 return false
Colin Cross6b753602018-06-21 13:03:07 -0700806 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800807 }
808}
809
Jiyong Park379de2f2018-12-19 02:47:14 +0900810// Add the dependency to the runtime library for each of the sanitizer variants
811func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900812 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000813 if !c.Enabled() {
814 return
815 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900816 var sanitizers []string
817 var diagSanitizers []string
818
819 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
820 sanitizers = append(sanitizers, "undefined")
821 } else {
822 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
823 sanitizers = append(sanitizers,
824 "bool",
825 "integer-divide-by-zero",
826 "return",
827 "returns-nonnull-attribute",
828 "shift-exponent",
829 "unreachable",
830 "vla-bound",
831 // TODO(danalbert): The following checks currently have compiler performance issues.
832 //"alignment",
833 //"bounds",
834 //"enum",
835 //"float-cast-overflow",
836 //"float-divide-by-zero",
837 //"nonnull-attribute",
838 //"null",
839 //"shift-base",
840 //"signed-integer-overflow",
841 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
842 // https://llvm.org/PR19302
843 // http://reviews.llvm.org/D6974
844 // "object-size",
845 )
846 }
847 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
848 }
849
850 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
851 diagSanitizers = append(diagSanitizers, "undefined")
852 }
853
854 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
855
856 if Bool(c.sanitize.Properties.Sanitize.Address) {
857 sanitizers = append(sanitizers, "address")
858 diagSanitizers = append(diagSanitizers, "address")
859 }
860
861 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
862 sanitizers = append(sanitizers, "hwaddress")
863 }
864
865 if Bool(c.sanitize.Properties.Sanitize.Thread) {
866 sanitizers = append(sanitizers, "thread")
867 }
868
869 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
870 sanitizers = append(sanitizers, "safe-stack")
871 }
872
873 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
874 sanitizers = append(sanitizers, "cfi")
875
876 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
877 diagSanitizers = append(diagSanitizers, "cfi")
878 }
879 }
880
881 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
882 sanitizers = append(sanitizers, "unsigned-integer-overflow")
883 sanitizers = append(sanitizers, "signed-integer-overflow")
884 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
885 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
886 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
887 }
888 }
889
890 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
891 sanitizers = append(sanitizers, "scudo")
892 }
893
894 if Bool(c.sanitize.Properties.Sanitize.Scs) {
895 sanitizers = append(sanitizers, "shadow-call-stack")
896 }
897
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700898 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
899 sanitizers = append(sanitizers, "fuzzer-no-link")
900 }
901
Jiyong Park379de2f2018-12-19 02:47:14 +0900902 // Save the list of sanitizers. These will be used again when generating
903 // the build rules (for Cflags, etc.)
904 c.sanitize.Properties.Sanitizers = sanitizers
905 c.sanitize.Properties.DiagSanitizers = diagSanitizers
906
Ivan Lozanofadd6352020-03-06 12:01:21 -0500907 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
908 if c.Host() {
909 diagSanitizers = sanitizers
910 }
911
Jiyong Park379de2f2018-12-19 02:47:14 +0900912 // Determine the runtime library required
913 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700914 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +0900915 toolchain := c.toolchain(mctx)
916 if Bool(c.sanitize.Properties.Sanitize.Address) {
917 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
918 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
919 if c.staticBinary() {
920 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700921 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +0900922 } else {
923 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
924 }
925 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
926 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
927 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
928 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
929 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
930 } else {
931 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
932 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700933 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500934 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
935 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
936 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900937 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
938 }
939
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500940 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
941 // UBSan is supported on non-bionic linux host builds as well
Jooyung Han0302a842019-10-30 18:43:49 +0900942 if isLlndkLibrary(runtimeLibrary, mctx.Config()) && !c.static() && c.UseVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900943 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
944 }
945
946 // Adding dependency to the runtime library. We are using *FarVariation*
947 // because the runtime libraries themselves are not mutated by sanitizer
948 // mutators and thus don't have sanitizer variants whereas this module
949 // has been already mutated.
950 //
951 // Note that by adding dependency with {static|shared}DepTag, the lib is
952 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
953 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +0900954 deps := append(extraStaticDeps, runtimeLibrary)
955 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
956 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
957 snapshots := vendorSnapshotStaticLibs(mctx.Config())
958 for idx, dep := range deps {
959 if lib, ok := snapshots.get(dep, mctx.Arch().ArchType); ok {
960 deps[idx] = lib
961 }
962 }
963 }
964
Jiyong Park379de2f2018-12-19 02:47:14 +0900965 // static executable gets static runtime libs
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700966 mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{
Jiyong Park379de2f2018-12-19 02:47:14 +0900967 {Mutator: "link", Variation: "static"},
Colin Cross7228ecd2019-11-18 16:00:16 -0800968 c.ImageVariation(),
Inseob Kimeec88e12020-01-22 11:11:29 +0900969 }...), StaticDepTag, deps...)
Jiyong Park1d1119f2019-07-29 21:27:18 +0900970 } else if !c.static() && !c.header() {
Inseob Kimeec88e12020-01-22 11:11:29 +0900971 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
972 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
973 snapshots := vendorSnapshotSharedLibs(mctx.Config())
974 if lib, ok := snapshots.get(runtimeLibrary, mctx.Arch().ArchType); ok {
975 runtimeLibrary = lib
976 }
977 }
978
Jiyong Park3b1746a2019-01-29 11:15:04 +0900979 // dynamic executable and shared libs get shared runtime libs
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700980 mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{
Jiyong Park379de2f2018-12-19 02:47:14 +0900981 {Mutator: "link", Variation: "shared"},
Colin Cross7228ecd2019-11-18 16:00:16 -0800982 c.ImageVariation(),
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700983 }...), earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900984 }
985 // static lib does not have dependency to the runtime library. The
986 // dependency will be added to the executables or shared libs using
987 // the static lib.
988 }
989 }
990}
991
992type Sanitizeable interface {
993 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900994 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900995 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900996}
997
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000998// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700999func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
1000 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +00001001 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001002 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001003 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -07001004 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001005 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001006 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001007 if c.static() || c.header() || t == asan || t == fuzzer {
1008 // Static and header libs are split into non-sanitized and sanitized variants.
1009 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1010 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1011 // that isn't sanitized for asan/fuzzer.
1012 //
1013 // Note for defaultVariation: since we don't split for shared libs but for static/header
1014 // libs, it is possible for the sanitized variant of a static/header lib to depend
1015 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1016 // error when the module is split. defaultVariation is the name of the variation that
1017 // will be used when such a dangling dependency occurs during the split of the current
1018 // module. By setting it to the name of the sanitized variation, the dangling dependency
1019 // is redirected to the sanitized variant of the dependent module.
1020 defaultVariation := t.variationName()
1021 mctx.SetDefaultDependencyVariation(&defaultVariation)
1022 modules := mctx.CreateVariations("", t.variationName())
1023 modules[0].(*Module).sanitize.SetSanitizer(t, false)
1024 modules[1].(*Module).sanitize.SetSanitizer(t, true)
1025 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
1026 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001027
Ivan Lozano9d790c72020-03-10 16:23:57 -04001028 if mctx.Device() && t.incompatibleWithCfi() {
1029 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1030 // are incompatible with cfi
1031 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
1032 }
1033
Jiyong Park1d1119f2019-07-29 21:27:18 +09001034 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1035 // to Make, because the sanitized version has a different suffix in name.
1036 // For other types of sanitizers, suppress the variation that is disabled.
1037 if t != cfi && t != scs && t != hwasan {
1038 if isSanitizerEnabled {
1039 modules[0].(*Module).Properties.PreventInstall = true
1040 modules[0].(*Module).Properties.HideFromMake = true
1041 } else {
1042 modules[1].(*Module).Properties.PreventInstall = true
1043 modules[1].(*Module).Properties.HideFromMake = true
1044 }
1045 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001046
Jiyong Park1d1119f2019-07-29 21:27:18 +09001047 // Export the static lib name to make
Dan Willemsen7b6af232020-05-03 21:28:32 -07001048 if c.static() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001049 if t == cfi {
1050 appendStringSync(c.Name(), cfiStaticLibs(mctx.Config()), &cfiStaticLibsMutex)
1051 } else if t == hwasan {
Ivan Lozano52767be2019-10-18 14:49:46 -07001052 if c.UseVndk() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001053 appendStringSync(c.Name(), hwasanVendorStaticLibs(mctx.Config()),
1054 &hwasanStaticLibsMutex)
Vishwath Mohane7128792017-11-17 11:08:10 -08001055 } else {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001056 appendStringSync(c.Name(), hwasanStaticLibs(mctx.Config()),
1057 &hwasanStaticLibsMutex)
Vishwath Mohane7128792017-11-17 11:08:10 -08001058 }
Jiyong Park1d1119f2019-07-29 21:27:18 +09001059 }
1060 }
1061 } else {
1062 // Shared libs are not split. Only the sanitized variant is created.
1063 modules := mctx.CreateVariations(t.variationName())
1064 modules[0].(*Module).sanitize.SetSanitizer(t, true)
1065 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -08001066
Jiyong Park1d1119f2019-07-29 21:27:18 +09001067 // locate the asan libraries under /data/asan
1068 if mctx.Device() && t == asan && isSanitizerEnabled {
1069 modules[0].(*Module).sanitize.Properties.InSanitizerDir = true
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001070 }
Ivan Lozano9d790c72020-03-10 16:23:57 -04001071
1072 if mctx.Device() && t.incompatibleWithCfi() {
1073 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1074 // are incompatible with cfi
1075 modules[0].(*Module).sanitize.SetSanitizer(cfi, false)
1076 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001077 }
Colin Cross16b23492016-01-06 14:41:07 -08001078 }
1079 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +09001080 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001081 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +09001082 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -08001083 }
1084 }
1085}
Vishwath Mohane7128792017-11-17 11:08:10 -08001086
Colin Cross571cccf2019-02-04 11:22:08 -08001087var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1088
Vishwath Mohane7128792017-11-17 11:08:10 -08001089func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001090 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -08001091 return &[]string{}
1092 }).(*[]string)
1093}
1094
Colin Cross571cccf2019-02-04 11:22:08 -08001095var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1096
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001097func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001098 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001099 return &[]string{}
1100 }).(*[]string)
1101}
1102
Colin Cross571cccf2019-02-04 11:22:08 -08001103var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
1104
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001105func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001106 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001107 return &[]string{}
1108 }).(*[]string)
1109}
1110
Jiyong Park1d1119f2019-07-29 21:27:18 +09001111func appendStringSync(item string, list *[]string, mutex *sync.Mutex) {
1112 mutex.Lock()
1113 *list = append(*list, item)
1114 mutex.Unlock()
1115}
1116
Ivan Lozano30c5db22018-02-21 15:49:20 -08001117func enableMinimalRuntime(sanitize *sanitize) bool {
1118 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001119 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001120 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001121
Ivan Lozano30c5db22018-02-21 15:49:20 -08001122 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001123 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1124 Bool(sanitize.Properties.Sanitize.Undefined) ||
1125 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1126
Ivan Lozano30c5db22018-02-21 15:49:20 -08001127 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1128 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001129 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001130 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001131
Ivan Lozano30c5db22018-02-21 15:49:20 -08001132 return true
1133 }
1134 return false
1135}
1136
Inseob Kim8471cda2019-11-15 09:59:12 +09001137func enableUbsanRuntime(sanitize *sanitize) bool {
1138 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001139 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001140 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1141}
1142
Vishwath Mohane7128792017-11-17 11:08:10 -08001143func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1144 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001145 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001146 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1147}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001148
1149func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1150 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1151 sort.Strings(*hwasanStaticLibs)
1152 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1153
1154 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1155 sort.Strings(*hwasanVendorStaticLibs)
1156 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1157}