blob: 2243082adc92cb340b015e15d5afb09c26173568 [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 Stepanov96fa3dd2020-03-27 19:38:42 +000041 "-fsanitize-hwaddress-abi=platform",
42 "-fno-experimental-new-pass-manager",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080043 // 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",
51 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070052
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000053 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Pirama Arumuga Nainareb8d4032020-07-27 11:22:35 -070054 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blocklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070055 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
56 // used, but have no effect on assembly files
57 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070058 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070059 "-Wl,-plugin-opt,O1"}
Inseob Kim74d25562020-08-04 00:41:38 +090060 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070061
Pirama Arumuga Nainareb8d4032020-07-27 11:22:35 -070062 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blocklist.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
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000159 // A modifier for ASAN and HWASAN for write only instrumentation
160 Writeonly *bool `android:"arch_variant"`
161
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700162 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
163 // Replaces abort() on error with a human-readable error message.
164 // Address and Thread sanitizers always run in diagnostic mode.
165 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700166 Undefined *bool `android:"arch_variant"`
167 Cfi *bool `android:"arch_variant"`
168 Integer_overflow *bool `android:"arch_variant"`
169 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800170 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700171 }
172
173 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800174 Recover []string
175
176 // value to pass to -fsanitize-blacklist
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700177 Blocklist *string
Colin Cross16b23492016-01-06 14:41:07 -0800178 } `android:"arch_variant"`
179
Jiyong Park379de2f2018-12-19 02:47:14 +0900180 SanitizerEnabled bool `blueprint:"mutated"`
181 SanitizeDep bool `blueprint:"mutated"`
182 MinimalRuntimeDep bool `blueprint:"mutated"`
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500183 BuiltinsDep bool `blueprint:"mutated"`
Jiyong Park379de2f2018-12-19 02:47:14 +0900184 UbsanRuntimeDep bool `blueprint:"mutated"`
185 InSanitizerDir bool `blueprint:"mutated"`
186 Sanitizers []string `blueprint:"mutated"`
187 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800188}
189
190type sanitize struct {
191 Properties SanitizeProperties
192}
193
Vishwath Mohane7128792017-11-17 11:08:10 -0800194func init() {
195 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700196 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800197}
198
Colin Cross16b23492016-01-06 14:41:07 -0800199func (sanitize *sanitize) props() []interface{} {
200 return []interface{}{&sanitize.Properties}
201}
202
203func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700204 s := &sanitize.Properties.Sanitize
205
Colin Cross16b23492016-01-06 14:41:07 -0800206 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700207 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800208 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800209 }
210
Doug Hornc32c6b02019-01-17 14:44:05 -0800211 // Sanitizers do not work on Fuchsia yet.
212 if ctx.Fuchsia() {
213 s.Never = BoolPtr(true)
214 }
215
Colin Cross16b23492016-01-06 14:41:07 -0800216 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800217 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800218 return
219 }
220
Colin Cross16b23492016-01-06 14:41:07 -0800221 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700222 var globalSanitizersDiag []string
223
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700224 if ctx.Host() {
225 if !ctx.Windows() {
226 globalSanitizers = ctx.Config().SanitizeHost()
227 }
228 } else {
229 arches := ctx.Config().SanitizeDeviceArch()
230 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
231 globalSanitizers = ctx.Config().SanitizeDevice()
232 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800233 }
234 }
235
Colin Cross16b23492016-01-06 14:41:07 -0800236 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000237 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700238 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
239 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000240 }
Colin Cross16b23492016-01-06 14:41:07 -0800241
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700242 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
243 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000244 }
245
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700246 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
247 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000248 }
249
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700250 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
251 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000252 }
253
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700254 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
255 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700256 }
257
258 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
259 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000260 }
261
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700262 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800263 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700264 s.Cfi = boolPtr(true)
265 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700266 }
267
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700268 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700269 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700270 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700271 s.Integer_overflow = boolPtr(true)
272 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700273 }
274
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700275 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
276 s.Scudo = boolPtr(true)
277 }
278
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700279 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
280 s.Hwaddress = boolPtr(true)
281 }
282
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000283 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
284 // Hwaddress and Address are set before, so we can check them here
285 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
286 if s.Address == nil && s.Hwaddress == nil {
287 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
288 }
289 s.Writeonly = boolPtr(true)
290 }
291
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000292 if len(globalSanitizers) > 0 {
293 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
294 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700295
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700296 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700297 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700298 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700299 s.Diag.Integer_overflow = boolPtr(true)
300 }
301
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700302 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
303 s.Diag.Cfi == nil && Bool(s.Cfi) {
304 s.Diag.Cfi = boolPtr(true)
305 }
306
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700307 if len(globalSanitizersDiag) > 0 {
308 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
309 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700310 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700311
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700312 // Enable CFI for all components in the include paths (for Aarch64 only)
313 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000314 s.Cfi = boolPtr(true)
315 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
316 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700317 }
318 }
319
Elliott Hughesda3a0712020-03-06 16:55:28 -0800320 // Is CFI actually enabled?
321 if !ctx.Config().EnableCFI() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900322 s.Cfi = boolPtr(false)
323 s.Diag.Cfi = boolPtr(false)
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800324 }
325
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800326 // Also disable CFI for arm32 until b/35157333 is fixed.
327 if ctx.Arch().ArchType == android.Arm {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900328 s.Cfi = boolPtr(false)
329 s.Diag.Cfi = boolPtr(false)
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800330 }
331
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700332 // HWASan requires AArch64 hardware feature (top-byte-ignore).
333 if ctx.Arch().ArchType != android.Arm64 {
334 s.Hwaddress = nil
335 }
336
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800337 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800338 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800339 s.Scs = nil
340 }
341
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700342 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700343 if Bool(s.Address) || Bool(s.Hwaddress) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900344 s.Cfi = boolPtr(false)
345 s.Diag.Cfi = boolPtr(false)
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700346 }
347
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500348 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
349 if !ctx.Os().Linux() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900350 s.Cfi = boolPtr(false)
351 s.Diag.Cfi = boolPtr(false)
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700352 s.Misc_undefined = nil
353 s.Undefined = nil
354 s.All_undefined = nil
355 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800356 }
357
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700358 // Also disable CFI for VNDK variants of components
359 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900360 if ctx.static() {
361 // Cfi variant for static vndk should be captured as vendor snapshot,
362 // so don't strictly disable Cfi.
363 s.Cfi = nil
364 s.Diag.Cfi = nil
365 } else {
366 s.Cfi = boolPtr(false)
367 s.Diag.Cfi = boolPtr(false)
368 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900369 }
370
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700371 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong1b3348d2020-01-21 15:53:22 -0800372 // Keep libc instrumented so that ramdisk / recovery can run hwasan-instrumented code if necessary.
373 if (ctx.inRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700374 s.Hwaddress = nil
375 }
376
Colin Cross3c344ef2016-07-18 15:44:56 -0700377 if ctx.staticBinary() {
378 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700379 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700380 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800381 }
382
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700383 if Bool(s.All_undefined) {
384 s.Undefined = nil
385 }
386
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700387 if !ctx.toolchain().Is64Bit() {
388 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700389 s.Thread = nil
390 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800391 // TODO(ccross): error for compile_multilib = "32"?
392 }
393
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800394 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 -0700395 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 -0800396 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700397 sanitize.Properties.SanitizerEnabled = true
398 }
399
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800400 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
401 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700402 s.Scudo = nil
403 }
404
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700405 if Bool(s.Hwaddress) {
406 s.Address = nil
407 s.Thread = nil
408 }
409
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700410 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
411 // mutually incompatible.
412 if Bool(s.Fuzzer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900413 s.Cfi = boolPtr(false)
Colin Cross16b23492016-01-06 14:41:07 -0800414 }
415}
416
417func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
418 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
419 return deps
420 }
421
Colin Cross16b23492016-01-06 14:41:07 -0800422 return deps
423}
424
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800425func toDisableImplicitIntegerChange(flags []string) bool {
426 // Returns true if any flag is fsanitize*integer, and there is
427 // no explicit flag about sanitize=implicit-integer-sign-change.
428 for _, f := range flags {
429 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
430 return false
431 }
432 }
433 for _, f := range flags {
434 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
435 return true
436 }
437 }
438 return false
439}
440
Colin Cross16b23492016-01-06 14:41:07 -0800441func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700442 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
443 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500444 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
445 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800446
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500447 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800448 flags.Local.LdFlags = append(flags.Local.LdFlags,
449 minimalRuntimePath,
450 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800451 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500452
453 if sanitize.Properties.BuiltinsDep {
454 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
455 }
456
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700457 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800458 return flags
459 }
460
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700461 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700462 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800463 // Frame pointer based unwinder in ASan requires ARM frame setup.
464 // TODO: put in flags?
465 flags.RequiredInstructionSet = "arm"
466 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800467 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
468 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800469
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000470 if Bool(sanitize.Properties.Sanitize.Writeonly) {
471 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
472 }
473
Colin Cross16b23492016-01-06 14:41:07 -0800474 if ctx.Host() {
475 // -nodefaultlibs (provided with libc++) prevents the driver from linking
476 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800477 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800478 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800479 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900480 if ctx.bootstrap() {
481 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
482 } else {
483 flags.DynamicLinker = "/system/bin/linker_asan"
484 }
Colin Cross16b23492016-01-06 14:41:07 -0800485 if flags.Toolchain.Is64Bit() {
486 flags.DynamicLinker += "64"
487 }
488 }
Colin Cross16b23492016-01-06 14:41:07 -0800489 }
490
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700491 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800492 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000493 if Bool(sanitize.Properties.Sanitize.Writeonly) {
494 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
495 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700496 }
497
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700498 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800499 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700500
501 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800502 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
503 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
504 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
505 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700506
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700507 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
508 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
509 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800510 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
511 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700512
Mitch Phillips74384752019-06-17 10:33:52 -0700513 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800514 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700515
516 // Disable fortify for fuzzing builds. Generally, we'll be building with
517 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800518 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800519
520 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
521 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
522 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
523 // the DT_RUNPATH from the shared library above it, and not the executable,
524 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
525 // DT_RUNPATH here means that transient shared libraries can be found
526 // colocated with their parents.
527 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800528 }
529
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700530 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800531 if ctx.Arch().ArchType == android.Arm {
532 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
533 // to do this on a function basis, so force Thumb on the entire module.
534 flags.RequiredInstructionSet = "thumb"
535 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000536
Colin Cross4af21ed2019-11-04 09:37:55 -0800537 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
538 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000539 // Only append the default visibility flag if -fvisibility has not already been set
540 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800541 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
542 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000543 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800544 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000545
546 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800547 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
548 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000549 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700550 }
551
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700552 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800553 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700554 }
555
Jiyong Park379de2f2018-12-19 02:47:14 +0900556 if len(sanitize.Properties.Sanitizers) > 0 {
557 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800558
Colin Cross4af21ed2019-11-04 09:37:55 -0800559 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
560 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800561 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800562 // Host sanitizers only link symbols in the final executable, so
563 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800564 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
565 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500566
567 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
568 if !ctx.toolchain().Bionic() {
569 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
570 }
571 }
572
573 if enableMinimalRuntime(sanitize) {
574 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
575 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
576 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
577 if !ctx.toolchain().Bionic() {
578 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800579 }
Colin Cross16b23492016-01-06 14:41:07 -0800580 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700581
582 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
583 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800584 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700585 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800586 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700587 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800588 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700589 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800590 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800591 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
592 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800593 }
Colin Cross16b23492016-01-06 14:41:07 -0800594 }
595
Jiyong Park379de2f2018-12-19 02:47:14 +0900596 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800597 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700598 }
599 // FIXME: enable RTTI if diag + (cfi or vptr)
600
Andreas Gampe97071162017-05-08 13:15:23 -0700601 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800602 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700603 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
604 }
605
Ivan Lozano7929bba2018-12-12 09:36:31 -0800606 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800607 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800608 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
609 }
610
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700611 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
612 if blocklist.Valid() {
613 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blocklist.String())
614 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
615 }
616
Colin Cross16b23492016-01-06 14:41:07 -0800617 return flags
618}
619
Colin Crossd80cbca2020-02-24 12:01:37 -0800620func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900621 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
622 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800623 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900624 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800625 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900626 }
627 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800628 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900629 }
630 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800631 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900632 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800633 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700634}
635
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700636func (sanitize *sanitize) inSanitizerDir() bool {
637 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700638}
639
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000640func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000641 switch t {
642 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000643 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700644 case hwasan:
645 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000646 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000647 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000648 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000649 return sanitize.Properties.Sanitize.Integer_overflow
650 case cfi:
651 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800652 case scs:
653 return sanitize.Properties.Sanitize.Scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700654 case fuzzer:
655 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000656 default:
657 panic(fmt.Errorf("unknown sanitizerType %d", t))
658 }
659}
660
Dan Albert7d1eecf2018-01-19 12:30:45 -0800661func (sanitize *sanitize) isUnsanitizedVariant() bool {
662 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700663 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800664 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800665 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700666 !sanitize.isSanitizerEnabled(scs) &&
667 !sanitize.isSanitizerEnabled(fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800668}
669
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700670func (sanitize *sanitize) isVariantOnProductionDevice() bool {
671 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700672 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700673 !sanitize.isSanitizerEnabled(tsan) &&
674 !sanitize.isSanitizerEnabled(fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700675}
676
Colin Cross16b23492016-01-06 14:41:07 -0800677func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
678 switch t {
679 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700680 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700681 case hwasan:
682 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800683 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700684 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700685 case intOverflow:
686 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000687 case cfi:
688 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800689 case scs:
690 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700691 case fuzzer:
692 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800693 default:
694 panic(fmt.Errorf("unknown sanitizerType %d", t))
695 }
696 if b {
697 sanitize.Properties.SanitizerEnabled = true
698 }
699}
700
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000701// Check if the sanitizer is explicitly disabled (as opposed to nil by
702// virtue of not being set).
703func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
704 if sanitize == nil {
705 return false
706 }
707
708 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
709 return sanitizerVal != nil && *sanitizerVal == false
710}
711
712// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
713// because enabling a sanitizer either directly (via the blueprint) or
714// indirectly (via a mutator) sets the bool ptr to true, and you can't
715// distinguish between the cases. It isn't needed though - both cases can be
716// treated identically.
717func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
718 if sanitize == nil {
719 return false
720 }
721
722 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
723 return sanitizerVal != nil && *sanitizerVal == true
724}
725
Colin Cross6b753602018-06-21 13:03:07 -0700726func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700727 switch t := tag.(type) {
728 case dependencyTag:
729 return t == reuseObjTag || t == objDepTag
730 case libraryDependencyTag:
731 return true
732 default:
733 return false
734 }
Colin Cross6b753602018-06-21 13:03:07 -0700735}
736
Inseob Kimc42f2f22020-07-29 20:32:10 +0900737// Determines if the current module is a static library going to be captured
738// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
739// except for ones which explicitly disable cfi.
740func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
741 if isVendorProprietaryPath(mctx.ModuleDir()) {
742 return false
743 }
744
745 c := mctx.Module().(*Module)
746
747 if !c.inVendor() {
748 return false
749 }
750
751 if !c.static() {
752 return false
753 }
754
755 if c.Prebuilt() != nil {
756 return false
757 }
758
759 return c.sanitize != nil &&
760 !Bool(c.sanitize.Properties.Sanitize.Never) &&
761 !c.sanitize.isSanitizerExplicitlyDisabled(cfi)
762}
763
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700764// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700765func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
766 return func(mctx android.TopDownMutatorContext) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900767 if c, ok := mctx.Module().(*Module); ok {
768 enabled := c.sanitize.isSanitizerEnabled(t)
769 if t == cfi && needsCfiForVendorSnapshot(mctx) {
770 // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly
771 // determine defaultVariation in sanitizerMutator below.
772 // Instead, just mark SanitizeDep to forcefully create cfi variant.
773 enabled = true
774 c.sanitize.Properties.SanitizeDep = true
775 }
776 if enabled {
777 mctx.WalkDeps(func(child, parent android.Module) bool {
778 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
779 return false
780 }
781 if d, ok := child.(*Module); ok && d.sanitize != nil &&
782 !Bool(d.sanitize.Properties.Sanitize.Never) &&
783 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
784 if t == cfi || t == hwasan || t == scs {
785 if d.static() {
786 d.sanitize.Properties.SanitizeDep = true
787 }
788 } else {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700789 d.sanitize.Properties.SanitizeDep = true
790 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000791 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900792 return true
793 })
794 }
Jiyong Parkf97782b2019-02-13 20:28:58 +0900795 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
796 // If an APEX module includes a lib which is enabled for a sanitizer T, then
797 // the APEX module is also enabled for the same sanitizer type.
798 mctx.VisitDirectDeps(func(child android.Module) {
799 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
800 sanitizeable.EnableSanitizer(t.name())
801 }
802 })
Colin Cross16b23492016-01-06 14:41:07 -0800803 }
804 }
805}
806
Ivan Lozano30c5db22018-02-21 15:49:20 -0800807// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700808func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
809 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
810 mctx.WalkDeps(func(child, parent android.Module) bool {
811 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
812 return false
813 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800814
Inseob Kimeec88e12020-01-22 11:11:29 +0900815 d, ok := child.(*Module)
816 if !ok || !d.static() {
817 return false
818 }
819 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700820 if enableMinimalRuntime(d.sanitize) {
821 // If a static dependency is built with the minimal runtime,
822 // make sure we include the ubsan minimal runtime.
823 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +0900824 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -0700825 // If a static dependency runs with full ubsan diagnostics,
826 // make sure we include the ubsan runtime.
827 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800828 }
Colin Cross0b908332019-06-19 23:00:20 -0700829
830 if c.sanitize.Properties.MinimalRuntimeDep &&
831 c.sanitize.Properties.UbsanRuntimeDep {
832 // both flags that this mutator might set are true, so don't bother recursing
833 return false
834 }
835
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500836 if c.Os() == android.Linux {
837 c.sanitize.Properties.BuiltinsDep = true
838 }
839
Colin Cross0b908332019-06-19 23:00:20 -0700840 return true
Colin Cross6b753602018-06-21 13:03:07 -0700841 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900842
843 if p, ok := d.linker.(*vendorSnapshotLibraryDecorator); ok {
844 if Bool(p.properties.Sanitize_minimal_dep) {
845 c.sanitize.Properties.MinimalRuntimeDep = true
846 }
847 if Bool(p.properties.Sanitize_ubsan_dep) {
848 c.sanitize.Properties.UbsanRuntimeDep = true
849 }
850 }
851
852 return false
Colin Cross6b753602018-06-21 13:03:07 -0700853 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800854 }
855}
856
Jiyong Park379de2f2018-12-19 02:47:14 +0900857// Add the dependency to the runtime library for each of the sanitizer variants
858func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900859 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000860 if !c.Enabled() {
861 return
862 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900863 var sanitizers []string
864 var diagSanitizers []string
865
866 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
867 sanitizers = append(sanitizers, "undefined")
868 } else {
869 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
870 sanitizers = append(sanitizers,
871 "bool",
872 "integer-divide-by-zero",
873 "return",
874 "returns-nonnull-attribute",
875 "shift-exponent",
876 "unreachable",
877 "vla-bound",
878 // TODO(danalbert): The following checks currently have compiler performance issues.
879 //"alignment",
880 //"bounds",
881 //"enum",
882 //"float-cast-overflow",
883 //"float-divide-by-zero",
884 //"nonnull-attribute",
885 //"null",
886 //"shift-base",
887 //"signed-integer-overflow",
888 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
889 // https://llvm.org/PR19302
890 // http://reviews.llvm.org/D6974
891 // "object-size",
892 )
893 }
894 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
895 }
896
897 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
898 diagSanitizers = append(diagSanitizers, "undefined")
899 }
900
901 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
902
903 if Bool(c.sanitize.Properties.Sanitize.Address) {
904 sanitizers = append(sanitizers, "address")
905 diagSanitizers = append(diagSanitizers, "address")
906 }
907
908 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
909 sanitizers = append(sanitizers, "hwaddress")
910 }
911
912 if Bool(c.sanitize.Properties.Sanitize.Thread) {
913 sanitizers = append(sanitizers, "thread")
914 }
915
916 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
917 sanitizers = append(sanitizers, "safe-stack")
918 }
919
920 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
921 sanitizers = append(sanitizers, "cfi")
922
923 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
924 diagSanitizers = append(diagSanitizers, "cfi")
925 }
926 }
927
928 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
929 sanitizers = append(sanitizers, "unsigned-integer-overflow")
930 sanitizers = append(sanitizers, "signed-integer-overflow")
931 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
932 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
933 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
934 }
935 }
936
937 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
938 sanitizers = append(sanitizers, "scudo")
939 }
940
941 if Bool(c.sanitize.Properties.Sanitize.Scs) {
942 sanitizers = append(sanitizers, "shadow-call-stack")
943 }
944
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700945 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
946 sanitizers = append(sanitizers, "fuzzer-no-link")
947 }
948
Jiyong Park379de2f2018-12-19 02:47:14 +0900949 // Save the list of sanitizers. These will be used again when generating
950 // the build rules (for Cflags, etc.)
951 c.sanitize.Properties.Sanitizers = sanitizers
952 c.sanitize.Properties.DiagSanitizers = diagSanitizers
953
Ivan Lozanof3b190f2020-03-06 12:01:21 -0500954 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
955 if c.Host() {
956 diagSanitizers = sanitizers
957 }
958
Jiyong Park379de2f2018-12-19 02:47:14 +0900959 // Determine the runtime library required
960 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700961 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +0900962 toolchain := c.toolchain(mctx)
963 if Bool(c.sanitize.Properties.Sanitize.Address) {
964 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
965 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
966 if c.staticBinary() {
967 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700968 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +0900969 } else {
970 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
971 }
972 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
973 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
974 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
975 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
976 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
977 } else {
978 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
979 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700980 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500981 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
982 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
983 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900984 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
985 }
986
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500987 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
988 // UBSan is supported on non-bionic linux host builds as well
Jooyung Han0302a842019-10-30 18:43:49 +0900989 if isLlndkLibrary(runtimeLibrary, mctx.Config()) && !c.static() && c.UseVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900990 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
991 }
992
993 // Adding dependency to the runtime library. We are using *FarVariation*
994 // because the runtime libraries themselves are not mutated by sanitizer
995 // mutators and thus don't have sanitizer variants whereas this module
996 // has been already mutated.
997 //
998 // Note that by adding dependency with {static|shared}DepTag, the lib is
999 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
1000 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001001 deps := append(extraStaticDeps, runtimeLibrary)
1002 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
1003 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
1004 snapshots := vendorSnapshotStaticLibs(mctx.Config())
1005 for idx, dep := range deps {
1006 if lib, ok := snapshots.get(dep, mctx.Arch().ArchType); ok {
1007 deps[idx] = lib
1008 }
1009 }
1010 }
1011
Jiyong Park379de2f2018-12-19 02:47:14 +09001012 // static executable gets static runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -07001013 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001014 mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{
Jiyong Park379de2f2018-12-19 02:47:14 +09001015 {Mutator: "link", Variation: "static"},
Colin Cross7228ecd2019-11-18 16:00:16 -08001016 c.ImageVariation(),
Colin Cross6e511a92020-07-27 21:26:48 -07001017 }...), depTag, deps...)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001018 } else if !c.static() && !c.header() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001019 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
1020 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
1021 snapshots := vendorSnapshotSharedLibs(mctx.Config())
1022 if lib, ok := snapshots.get(runtimeLibrary, mctx.Arch().ArchType); ok {
1023 runtimeLibrary = lib
1024 }
1025 }
1026
Jiyong Park3b1746a2019-01-29 11:15:04 +09001027 // dynamic executable and shared libs get shared runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -07001028 depTag := libraryDependencyTag{Kind: sharedLibraryDependency, Order: earlyLibraryDependency}
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001029 mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{
Jiyong Park379de2f2018-12-19 02:47:14 +09001030 {Mutator: "link", Variation: "shared"},
Colin Cross7228ecd2019-11-18 16:00:16 -08001031 c.ImageVariation(),
Colin Cross6e511a92020-07-27 21:26:48 -07001032 }...), depTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +09001033 }
1034 // static lib does not have dependency to the runtime library. The
1035 // dependency will be added to the executables or shared libs using
1036 // the static lib.
1037 }
1038 }
1039}
1040
1041type Sanitizeable interface {
1042 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +09001043 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001044 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001045 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001046}
1047
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001048// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -07001049func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
1050 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +00001051 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001052 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001053 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -07001054 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001055 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001056 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001057 if c.static() || c.header() || t == asan || t == fuzzer {
1058 // Static and header libs are split into non-sanitized and sanitized variants.
1059 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1060 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1061 // that isn't sanitized for asan/fuzzer.
1062 //
1063 // Note for defaultVariation: since we don't split for shared libs but for static/header
1064 // libs, it is possible for the sanitized variant of a static/header lib to depend
1065 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1066 // error when the module is split. defaultVariation is the name of the variation that
1067 // will be used when such a dangling dependency occurs during the split of the current
1068 // module. By setting it to the name of the sanitized variation, the dangling dependency
1069 // is redirected to the sanitized variant of the dependent module.
1070 defaultVariation := t.variationName()
1071 mctx.SetDefaultDependencyVariation(&defaultVariation)
1072 modules := mctx.CreateVariations("", t.variationName())
1073 modules[0].(*Module).sanitize.SetSanitizer(t, false)
1074 modules[1].(*Module).sanitize.SetSanitizer(t, true)
1075 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
1076 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001077
Ivan Lozano4774a812020-03-10 16:23:57 -04001078 if mctx.Device() && t.incompatibleWithCfi() {
1079 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1080 // are incompatible with cfi
1081 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
1082 }
1083
Jiyong Park1d1119f2019-07-29 21:27:18 +09001084 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1085 // to Make, because the sanitized version has a different suffix in name.
1086 // For other types of sanitizers, suppress the variation that is disabled.
1087 if t != cfi && t != scs && t != hwasan {
1088 if isSanitizerEnabled {
1089 modules[0].(*Module).Properties.PreventInstall = true
1090 modules[0].(*Module).Properties.HideFromMake = true
1091 } else {
1092 modules[1].(*Module).Properties.PreventInstall = true
1093 modules[1].(*Module).Properties.HideFromMake = true
1094 }
1095 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001096
Jiyong Park1d1119f2019-07-29 21:27:18 +09001097 // Export the static lib name to make
Dan Willemsenb5b2aba2020-05-03 21:28:32 -07001098 if c.static() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001099 if t == cfi {
Inseob Kim74d25562020-08-04 00:41:38 +09001100 cfiStaticLibs(mctx.Config()).add(c, c.Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001101 } else if t == hwasan {
Inseob Kim74d25562020-08-04 00:41:38 +09001102 hwasanStaticLibs(mctx.Config()).add(c, c.Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001103 }
1104 }
1105 } else {
1106 // Shared libs are not split. Only the sanitized variant is created.
1107 modules := mctx.CreateVariations(t.variationName())
1108 modules[0].(*Module).sanitize.SetSanitizer(t, true)
1109 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -08001110
Jiyong Park1d1119f2019-07-29 21:27:18 +09001111 // locate the asan libraries under /data/asan
1112 if mctx.Device() && t == asan && isSanitizerEnabled {
1113 modules[0].(*Module).sanitize.Properties.InSanitizerDir = true
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001114 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001115
1116 if mctx.Device() && t.incompatibleWithCfi() {
1117 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1118 // are incompatible with cfi
1119 modules[0].(*Module).sanitize.SetSanitizer(cfi, false)
1120 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001121 }
Colin Cross16b23492016-01-06 14:41:07 -08001122 }
1123 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +09001124 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001125 // APEX modules fall here
Jooyung Han8ce8db92020-05-15 19:05:05 +09001126 sanitizeable.AddSanitizerDependencies(mctx, t.name())
Jiyong Park82226632019-02-01 10:50:50 +09001127 mctx.CreateVariations(t.variationName())
Inseob Kimc42f2f22020-07-29 20:32:10 +09001128 } else if c, ok := mctx.Module().(*Module); ok {
1129 // Check if it's a snapshot module supporting sanitizer
1130 if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) {
1131 // Set default variation as above.
1132 defaultVariation := t.variationName()
1133 mctx.SetDefaultDependencyVariation(&defaultVariation)
1134 modules := mctx.CreateVariations("", t.variationName())
1135 modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false)
1136 modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true)
1137
1138 // Export the static lib name to make
1139 if c.static() && c.ExportedToMake() {
1140 if t == cfi {
1141 // use BaseModuleName which is the name for Make.
1142 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1143 }
1144 }
1145 }
Colin Cross16b23492016-01-06 14:41:07 -08001146 }
1147 }
1148}
Vishwath Mohane7128792017-11-17 11:08:10 -08001149
Inseob Kim74d25562020-08-04 00:41:38 +09001150type sanitizerStaticLibsMap struct {
1151 // libsMap contains one list of modules per each image and each arch.
1152 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
1153 libsMap map[imageVariantType]map[string][]string
1154 libsMapLock sync.Mutex
1155 sanitizerType sanitizerType
1156}
1157
1158func newSanitizerStaticLibsMap(t sanitizerType) *sanitizerStaticLibsMap {
1159 return &sanitizerStaticLibsMap{
1160 sanitizerType: t,
1161 libsMap: make(map[imageVariantType]map[string][]string),
1162 }
1163}
1164
1165// Add the current module to sanitizer static libs maps
1166// Each module should pass its exported name as names of Make and Soong can differ.
1167func (s *sanitizerStaticLibsMap) add(c *Module, name string) {
1168 image := c.getImageVariantType()
1169 arch := c.Arch().ArchType.String()
1170
1171 s.libsMapLock.Lock()
1172 defer s.libsMapLock.Unlock()
1173
1174 if _, ok := s.libsMap[image]; !ok {
1175 s.libsMap[image] = make(map[string][]string)
1176 }
1177
1178 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1179}
1180
1181// Exports makefile variables in the following format:
1182// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1183// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1184// These are to be used by use_soong_sanitized_static_libraries.
1185// See build/make/core/binary.mk for more details.
1186func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1187 for _, image := range android.SortedStringKeys(s.libsMap) {
1188 archMap := s.libsMap[imageVariantType(image)]
1189 for _, arch := range android.SortedStringKeys(archMap) {
1190 libs := archMap[arch]
1191 sort.Strings(libs)
1192
1193 key := fmt.Sprintf(
1194 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1195 s.sanitizerType.variationName(),
1196 image, // already upper
1197 arch)
1198
1199 ctx.Strict(key, strings.Join(libs, " "))
1200 }
1201 }
1202}
1203
Colin Cross571cccf2019-02-04 11:22:08 -08001204var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1205
Inseob Kim74d25562020-08-04 00:41:38 +09001206func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001207 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001208 return newSanitizerStaticLibsMap(cfi)
1209 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001210}
1211
Colin Cross571cccf2019-02-04 11:22:08 -08001212var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1213
Inseob Kim74d25562020-08-04 00:41:38 +09001214func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001215 return config.Once(hwasanStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001216 return newSanitizerStaticLibsMap(hwasan)
1217 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001218}
1219
Ivan Lozano30c5db22018-02-21 15:49:20 -08001220func enableMinimalRuntime(sanitize *sanitize) bool {
1221 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001222 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001223 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001224
Ivan Lozano30c5db22018-02-21 15:49:20 -08001225 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001226 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1227 Bool(sanitize.Properties.Sanitize.Undefined) ||
1228 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1229
Ivan Lozano30c5db22018-02-21 15:49:20 -08001230 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1231 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001232 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001233 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001234
Ivan Lozano30c5db22018-02-21 15:49:20 -08001235 return true
1236 }
1237 return false
1238}
1239
Inseob Kim8471cda2019-11-15 09:59:12 +09001240func enableUbsanRuntime(sanitize *sanitize) bool {
1241 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001242 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001243 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1244}
1245
Vishwath Mohane7128792017-11-17 11:08:10 -08001246func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001247 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001248}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001249
1250func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001251 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001252}