blob: b0eb0c6abda3687f72ed4bd346d382bf3b36fcf3 [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",
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",
Pirama Arumuga Nainareb8d4032020-07-27 11:22:35 -070058 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blocklist.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"}
Inseob Kim74d25562020-08-04 00:41:38 +090064 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070065
Pirama Arumuga Nainareb8d4032020-07-27 11:22:35 -070066 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080067
Peter Collingbournebd19db02019-03-06 10:38:48 -080068 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070069 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070070 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
71 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070072)
73
Ivan Lozano3968d8f2020-12-14 11:27:52 -050074type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080075
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070076func boolPtr(v bool) *bool {
77 if v {
78 return &v
79 } else {
80 return nil
81 }
82}
83
Colin Cross16b23492016-01-06 14:41:07 -080084const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050085 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070086 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080087 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070088 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000089 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080090 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -050091 Fuzzer
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -070092 memtag_heap
Colin Cross16b23492016-01-06 14:41:07 -080093)
94
Jiyong Park82226632019-02-01 10:50:50 +090095// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -050096func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080097 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -050098 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -080099 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700100 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700101 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800102 case tsan:
103 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700104 case intOverflow:
105 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000106 case cfi:
107 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800108 case scs:
109 return "scs"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700110 case memtag_heap:
111 return "memtag_heap"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500112 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700113 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800114 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500115 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800116 }
117}
118
Jiyong Park82226632019-02-01 10:50:50 +0900119// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500120func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900121 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500122 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900123 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700124 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900125 return "hwaddress"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700126 case memtag_heap:
127 return "memtag_heap"
Jiyong Park82226632019-02-01 10:50:50 +0900128 case tsan:
129 return "thread"
130 case intOverflow:
131 return "integer_overflow"
132 case cfi:
133 return "cfi"
134 case scs:
135 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500136 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700137 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900138 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500139 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900140 }
141}
142
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500143func (*Module) SanitizerSupported(t SanitizerType) bool {
144 switch t {
145 case Asan:
146 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700147 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500148 return true
149 case tsan:
150 return true
151 case intOverflow:
152 return true
153 case cfi:
154 return true
155 case scs:
156 return true
157 case Fuzzer:
158 return true
159 default:
160 return false
161 }
162}
163
164// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
165func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700166 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900167}
168
Martin Stjernholmb0249572020-09-15 02:32:35 +0100169type SanitizeUserProps struct {
170 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800171
Martin Stjernholmb0249572020-09-15 02:32:35 +0100172 // main sanitizers
173 Address *bool `android:"arch_variant"`
174 Thread *bool `android:"arch_variant"`
175 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800176
Martin Stjernholmb0249572020-09-15 02:32:35 +0100177 // local sanitizers
178 Undefined *bool `android:"arch_variant"`
179 All_undefined *bool `android:"arch_variant"`
180 Misc_undefined []string `android:"arch_variant"`
181 Fuzzer *bool `android:"arch_variant"`
182 Safestack *bool `android:"arch_variant"`
183 Cfi *bool `android:"arch_variant"`
184 Integer_overflow *bool `android:"arch_variant"`
185 Scudo *bool `android:"arch_variant"`
186 Scs *bool `android:"arch_variant"`
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700187 Memtag_heap *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100188
189 // A modifier for ASAN and HWASAN for write only instrumentation
190 Writeonly *bool `android:"arch_variant"`
191
192 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
193 // Replaces abort() on error with a human-readable error message.
194 // Address and Thread sanitizers always run in diagnostic mode.
195 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700196 Undefined *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700197 Cfi *bool `android:"arch_variant"`
198 Integer_overflow *bool `android:"arch_variant"`
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700199 Memtag_heap *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100200 Misc_undefined []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800201 No_recover []string `android:"arch_variant"`
202 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800203
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800204 // Sanitizers to run with flag configuration specified
205 Config struct {
206 // Enables CFI support flags for assembly-heavy libraries
207 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800208 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800209
Martin Stjernholmb0249572020-09-15 02:32:35 +0100210 // value to pass to -fsanitize-recover=
211 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000212
Martin Stjernholmb0249572020-09-15 02:32:35 +0100213 // value to pass to -fsanitize-blacklist
214 Blocklist *string
215}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700216
Martin Stjernholmb0249572020-09-15 02:32:35 +0100217type SanitizeProperties struct {
218 // Enable AddressSanitizer, ThreadSanitizer, UndefinedBehaviorSanitizer, and
219 // others. Please see SanitizerUserProps in build/soong/cc/sanitize.go for
220 // details.
221 Sanitize SanitizeUserProps `android:"arch_variant"`
222 SanitizerEnabled bool `blueprint:"mutated"`
223 SanitizeDep bool `blueprint:"mutated"`
224 MinimalRuntimeDep bool `blueprint:"mutated"`
225 BuiltinsDep bool `blueprint:"mutated"`
226 UbsanRuntimeDep bool `blueprint:"mutated"`
227 InSanitizerDir bool `blueprint:"mutated"`
228 Sanitizers []string `blueprint:"mutated"`
229 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800230}
231
232type sanitize struct {
233 Properties SanitizeProperties
234}
235
Cindy Zhou18417cb2020-12-10 07:12:38 -0800236// Mark this tag with a check to see if apex dependency check should be skipped
237func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
238 return t.skipApexAllowedDependenciesCheck
239}
240
241var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
242
Vishwath Mohane7128792017-11-17 11:08:10 -0800243func init() {
244 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700245 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800246}
247
Colin Cross16b23492016-01-06 14:41:07 -0800248func (sanitize *sanitize) props() []interface{} {
249 return []interface{}{&sanitize.Properties}
250}
251
252func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700253 s := &sanitize.Properties.Sanitize
254
Colin Cross16b23492016-01-06 14:41:07 -0800255 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700256 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800257 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800258 }
259
Doug Hornc32c6b02019-01-17 14:44:05 -0800260 // Sanitizers do not work on Fuchsia yet.
261 if ctx.Fuchsia() {
262 s.Never = BoolPtr(true)
263 }
264
Colin Cross16b23492016-01-06 14:41:07 -0800265 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800266 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800267 return
268 }
269
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800270 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap}).
271 if ctx.testBinary() && s.Memtag_heap == nil {
272 s.Memtag_heap = boolPtr(true)
273 s.Diag.Memtag_heap = boolPtr(true)
274 }
275
Colin Cross16b23492016-01-06 14:41:07 -0800276 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700277 var globalSanitizersDiag []string
278
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700279 if ctx.Host() {
280 if !ctx.Windows() {
281 globalSanitizers = ctx.Config().SanitizeHost()
282 }
283 } else {
284 arches := ctx.Config().SanitizeDeviceArch()
285 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
286 globalSanitizers = ctx.Config().SanitizeDevice()
287 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800288 }
289 }
290
Colin Cross16b23492016-01-06 14:41:07 -0800291 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000292 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700293 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
294 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000295 }
Colin Cross16b23492016-01-06 14:41:07 -0800296
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700297 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
298 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000299 }
300
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700301 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
302 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000303 }
304
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700305 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
306 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000307 }
308
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700309 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
310 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700311 }
312
313 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
314 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000315 }
316
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700317 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800318 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700319 s.Cfi = boolPtr(true)
320 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700321 }
322
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700323 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700324 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700325 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700326 s.Integer_overflow = boolPtr(true)
327 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700328 }
329
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700330 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
331 s.Scudo = boolPtr(true)
332 }
333
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700334 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
335 s.Hwaddress = boolPtr(true)
336 }
337
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000338 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
339 // Hwaddress and Address are set before, so we can check them here
340 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
341 if s.Address == nil && s.Hwaddress == nil {
342 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
343 }
344 s.Writeonly = boolPtr(true)
345 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700346 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800347 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
348 s.Memtag_heap = boolPtr(true)
349 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700350 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000351
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000352 if len(globalSanitizers) > 0 {
353 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
354 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700355
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700356 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700357 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700358 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700359 s.Diag.Integer_overflow = boolPtr(true)
360 }
361
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700362 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
363 s.Diag.Cfi == nil && Bool(s.Cfi) {
364 s.Diag.Cfi = boolPtr(true)
365 }
366
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800367 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
368 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
369 s.Diag.Memtag_heap = boolPtr(true)
370 }
371
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700372 if len(globalSanitizersDiag) > 0 {
373 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
374 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700375 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700376
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800377 // Enable Memtag for all components in the include paths (for Aarch64 only)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800378 if ctx.Arch().ArchType == android.Arm64 {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800379 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800380 if s.Memtag_heap == nil {
381 s.Memtag_heap = boolPtr(true)
382 }
383 if s.Diag.Memtag_heap == nil {
384 s.Diag.Memtag_heap = boolPtr(true)
385 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800386 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800387 if s.Memtag_heap == nil {
388 s.Memtag_heap = boolPtr(true)
389 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800390 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700391 }
392
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700393 // Enable CFI for all components in the include paths (for Aarch64 only)
394 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000395 s.Cfi = boolPtr(true)
396 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
397 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700398 }
399 }
400
Elliott Hughesda3a0712020-03-06 16:55:28 -0800401 // Is CFI actually enabled?
402 if !ctx.Config().EnableCFI() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900403 s.Cfi = boolPtr(false)
404 s.Diag.Cfi = boolPtr(false)
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800405 }
406
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700407 // HWASan requires AArch64 hardware feature (top-byte-ignore).
408 if ctx.Arch().ArchType != android.Arm64 {
409 s.Hwaddress = nil
410 }
411
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800412 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800413 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800414 s.Scs = nil
415 }
416
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700417 // memtag_heap is only implemented on AArch64.
418 if ctx.Arch().ArchType != android.Arm64 {
419 s.Memtag_heap = nil
420 }
421
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700422 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700423 if Bool(s.Address) || Bool(s.Hwaddress) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900424 s.Cfi = boolPtr(false)
425 s.Diag.Cfi = boolPtr(false)
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700426 }
427
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500428 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
429 if !ctx.Os().Linux() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900430 s.Cfi = boolPtr(false)
431 s.Diag.Cfi = boolPtr(false)
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700432 s.Misc_undefined = nil
433 s.Undefined = nil
434 s.All_undefined = nil
435 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800436 }
437
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700438 // Also disable CFI for VNDK variants of components
439 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900440 if ctx.static() {
441 // Cfi variant for static vndk should be captured as vendor snapshot,
442 // so don't strictly disable Cfi.
443 s.Cfi = nil
444 s.Diag.Cfi = nil
445 } else {
446 s.Cfi = boolPtr(false)
447 s.Diag.Cfi = boolPtr(false)
448 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900449 }
450
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700451 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700452 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
453 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700454 s.Hwaddress = nil
455 }
456
Colin Cross3c344ef2016-07-18 15:44:56 -0700457 if ctx.staticBinary() {
458 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700459 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700460 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800461 }
462
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700463 if Bool(s.All_undefined) {
464 s.Undefined = nil
465 }
466
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700467 if !ctx.toolchain().Is64Bit() {
468 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700469 s.Thread = nil
470 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800471 // TODO(ccross): error for compile_multilib = "32"?
472 }
473
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800474 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 -0700475 Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700476 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700477 sanitize.Properties.SanitizerEnabled = true
478 }
479
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800480 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
481 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700482 s.Scudo = nil
483 }
484
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700485 if Bool(s.Hwaddress) {
486 s.Address = nil
487 s.Thread = nil
Evgenii Stepanovb15a5642021-06-23 12:30:55 -0700488 // Disable ubsan diagnosic as a workaround for a compiler bug.
489 // TODO(b/191808836): re-enable.
490 s.Diag.Undefined = nil
491 s.Diag.Integer_overflow = nil
492 s.Diag.Misc_undefined = nil
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700493 }
494
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700495 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
496 // mutually incompatible.
497 if Bool(s.Fuzzer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900498 s.Cfi = boolPtr(false)
Colin Cross16b23492016-01-06 14:41:07 -0800499 }
500}
501
502func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
503 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
504 return deps
505 }
506
Colin Cross16b23492016-01-06 14:41:07 -0800507 return deps
508}
509
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800510func toDisableImplicitIntegerChange(flags []string) bool {
511 // Returns true if any flag is fsanitize*integer, and there is
512 // no explicit flag about sanitize=implicit-integer-sign-change.
513 for _, f := range flags {
514 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
515 return false
516 }
517 }
518 for _, f := range flags {
519 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
520 return true
521 }
522 }
523 return false
524}
525
Yabin Cuidb7dda82020-11-30 15:47:45 -0800526func toDisableUnsignedShiftBaseChange(flags []string) bool {
527 // Returns true if any flag is fsanitize*integer, and there is
528 // no explicit flag about sanitize=unsigned-shift-base.
529 for _, f := range flags {
530 if strings.Contains(f, "sanitize=unsigned-shift-base") {
531 return false
532 }
533 }
534 for _, f := range flags {
535 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
536 return true
537 }
538 }
539 return false
540}
541
Colin Cross16b23492016-01-06 14:41:07 -0800542func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700543 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
544 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500545 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
546 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800547
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500548 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800549 flags.Local.LdFlags = append(flags.Local.LdFlags,
550 minimalRuntimePath,
551 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800552 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500553
554 if sanitize.Properties.BuiltinsDep {
555 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
556 }
557
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700558 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800559 return flags
560 }
561
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700562 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700563 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800564 // Frame pointer based unwinder in ASan requires ARM frame setup.
565 // TODO: put in flags?
566 flags.RequiredInstructionSet = "arm"
567 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800568 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
569 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800570
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000571 if Bool(sanitize.Properties.Sanitize.Writeonly) {
572 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
573 }
574
Colin Cross16b23492016-01-06 14:41:07 -0800575 if ctx.Host() {
576 // -nodefaultlibs (provided with libc++) prevents the driver from linking
577 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800578 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800579 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800580 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900581 if ctx.bootstrap() {
582 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
583 } else {
584 flags.DynamicLinker = "/system/bin/linker_asan"
585 }
Colin Cross16b23492016-01-06 14:41:07 -0800586 if flags.Toolchain.Is64Bit() {
587 flags.DynamicLinker += "64"
588 }
589 }
Colin Cross16b23492016-01-06 14:41:07 -0800590 }
591
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700592 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800593 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000594 if Bool(sanitize.Properties.Sanitize.Writeonly) {
595 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
596 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700597 }
598
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700599 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800600 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700601
602 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800603 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
604 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
605 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
606 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700607
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700608 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
609 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
610 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800611 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
612 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700613
Mitch Phillips74384752019-06-17 10:33:52 -0700614 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800615 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700616
617 // Disable fortify for fuzzing builds. Generally, we'll be building with
618 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800619 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800620
621 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
622 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
623 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
624 // the DT_RUNPATH from the shared library above it, and not the executable,
625 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
626 // DT_RUNPATH here means that transient shared libraries can be found
627 // colocated with their parents.
628 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800629 }
630
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700631 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800632 if ctx.Arch().ArchType == android.Arm {
633 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
634 // to do this on a function basis, so force Thumb on the entire module.
635 flags.RequiredInstructionSet = "thumb"
636 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000637
Colin Cross4af21ed2019-11-04 09:37:55 -0800638 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
639 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800640 if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
641 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
642 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000643 // Only append the default visibility flag if -fvisibility has not already been set
644 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800645 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
646 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000647 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800648 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000649
650 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800651 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
652 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000653 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700654 }
655
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700656 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800657 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700658 }
659
Jiyong Park379de2f2018-12-19 02:47:14 +0900660 if len(sanitize.Properties.Sanitizers) > 0 {
661 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800662
Colin Cross4af21ed2019-11-04 09:37:55 -0800663 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
664 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800665 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800666 // Host sanitizers only link symbols in the final executable, so
667 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800668 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
669 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500670
671 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
672 if !ctx.toolchain().Bionic() {
673 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
674 }
675 }
676
677 if enableMinimalRuntime(sanitize) {
678 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
679 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
680 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
681 if !ctx.toolchain().Bionic() {
682 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800683 }
Colin Cross16b23492016-01-06 14:41:07 -0800684 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700685
686 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
687 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800688 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700689 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800690 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700691 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800692 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700693 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800694 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800695 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
696 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800697 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800698 // http://b/171275751, Android doesn't build with this sanitizer yet.
699 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
700 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
701 }
Colin Cross16b23492016-01-06 14:41:07 -0800702 }
703
Jiyong Park379de2f2018-12-19 02:47:14 +0900704 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800705 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700706 }
707 // FIXME: enable RTTI if diag + (cfi or vptr)
708
Andreas Gampe97071162017-05-08 13:15:23 -0700709 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800710 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700711 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
712 }
713
Ivan Lozano7929bba2018-12-12 09:36:31 -0800714 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800715 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800716 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
717 }
718
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700719 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
720 if blocklist.Valid() {
721 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blocklist.String())
722 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
723 }
724
Colin Cross16b23492016-01-06 14:41:07 -0800725 return flags
726}
727
Colin Crossd80cbca2020-02-24 12:01:37 -0800728func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900729 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
730 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800731 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900732 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800733 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900734 }
735 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800736 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900737 }
738 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800739 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900740 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800741 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700742}
743
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700744func (sanitize *sanitize) inSanitizerDir() bool {
745 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700746}
747
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500748// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
749func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000750 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500751 case Asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000752 return sanitize.Properties.Sanitize.Address
Tri Vo6eafc362021-04-01 11:29:09 -0700753 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700754 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000755 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000756 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000757 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000758 return sanitize.Properties.Sanitize.Integer_overflow
759 case cfi:
760 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800761 case scs:
762 return sanitize.Properties.Sanitize.Scs
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700763 case memtag_heap:
764 return sanitize.Properties.Sanitize.Memtag_heap
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500765 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700766 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000767 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500768 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000769 }
770}
771
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500772// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800773func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500774 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700775 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800776 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800777 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700778 !sanitize.isSanitizerEnabled(scs) &&
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700779 !sanitize.isSanitizerEnabled(memtag_heap) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500780 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800781}
782
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500783// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700784func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500785 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700786 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700787 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500788 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700789}
790
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500791func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Colin Cross16b23492016-01-06 14:41:07 -0800792 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500793 case Asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700794 sanitize.Properties.Sanitize.Address = boolPtr(b)
Tri Vo6eafc362021-04-01 11:29:09 -0700795 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700796 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800797 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700798 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700799 case intOverflow:
800 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000801 case cfi:
802 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800803 case scs:
804 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700805 case memtag_heap:
806 sanitize.Properties.Sanitize.Memtag_heap = boolPtr(b)
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500807 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700808 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800809 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500810 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800811 }
812 if b {
813 sanitize.Properties.SanitizerEnabled = true
814 }
815}
816
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000817// Check if the sanitizer is explicitly disabled (as opposed to nil by
818// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500819func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000820 if sanitize == nil {
821 return false
822 }
823
824 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
825 return sanitizerVal != nil && *sanitizerVal == false
826}
827
828// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
829// because enabling a sanitizer either directly (via the blueprint) or
830// indirectly (via a mutator) sets the bool ptr to true, and you can't
831// distinguish between the cases. It isn't needed though - both cases can be
832// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500833func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000834 if sanitize == nil {
835 return false
836 }
837
838 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
839 return sanitizerVal != nil && *sanitizerVal == true
840}
841
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500842// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
843func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700844 switch t := tag.(type) {
845 case dependencyTag:
846 return t == reuseObjTag || t == objDepTag
847 case libraryDependencyTag:
848 return true
849 default:
850 return false
851 }
Colin Cross6b753602018-06-21 13:03:07 -0700852}
853
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500854func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
855 return IsSanitizableDependencyTag
856}
857
Inseob Kimc42f2f22020-07-29 20:32:10 +0900858// Determines if the current module is a static library going to be captured
859// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
860// except for ones which explicitly disable cfi.
861func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400862 if IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900863 return false
864 }
865
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500866 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900867
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500868 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900869 return false
870 }
871
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500872 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900873 return false
874 }
875
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500876 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900877 return false
878 }
879
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500880 if !c.SanitizerSupported(cfi) {
881 return false
882 }
883
884 return c.SanitizePropDefined() &&
885 !c.SanitizeNever() &&
886 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900887}
888
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700889// Propagate sanitizer requirements down from binaries
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500890func sanitizerDepsMutator(t SanitizerType) func(android.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700891 return func(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500892 if c, ok := mctx.Module().(PlatformSanitizeable); ok {
893 enabled := c.IsSanitizerEnabled(t)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900894 if t == cfi && needsCfiForVendorSnapshot(mctx) {
895 // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly
896 // determine defaultVariation in sanitizerMutator below.
897 // Instead, just mark SanitizeDep to forcefully create cfi variant.
898 enabled = true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500899 c.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900900 }
901 if enabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500902 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Inseob Kimc42f2f22020-07-29 20:32:10 +0900903 mctx.WalkDeps(func(child, parent android.Module) bool {
904 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
905 return false
906 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500907 if d, ok := child.(PlatformSanitizeable); ok && d.SanitizePropDefined() &&
908 !d.SanitizeNever() &&
909 !d.IsSanitizerExplicitlyDisabled(t) {
Colin Crossaf98f582021-05-12 17:27:32 -0700910 if t == cfi || t == Hwasan || t == scs || t == Asan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500911 if d.StaticallyLinked() && d.SanitizerSupported(t) {
912 // Rust does not support some of these sanitizers, so we need to check if it's
913 // supported before setting this true.
914 d.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900915 }
916 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500917 d.SetSanitizeDep(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700918 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000919 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900920 return true
921 })
922 }
Jiyong Parkf97782b2019-02-13 20:28:58 +0900923 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
924 // If an APEX module includes a lib which is enabled for a sanitizer T, then
925 // the APEX module is also enabled for the same sanitizer type.
926 mctx.VisitDirectDeps(func(child android.Module) {
927 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
928 sanitizeable.EnableSanitizer(t.name())
929 }
930 })
Colin Cross16b23492016-01-06 14:41:07 -0800931 }
932 }
933}
934
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500935func (c *Module) SanitizeNever() bool {
936 return Bool(c.sanitize.Properties.Sanitize.Never)
937}
938
939func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
940 return c.sanitize.isSanitizerExplicitlyDisabled(t)
941}
942
Ivan Lozano30c5db22018-02-21 15:49:20 -0800943// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700944func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500945 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -0700946 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500947 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -0700948 mctx.WalkDeps(func(child, parent android.Module) bool {
949 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
950 return false
951 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800952
Inseob Kimeec88e12020-01-22 11:11:29 +0900953 d, ok := child.(*Module)
954 if !ok || !d.static() {
955 return false
956 }
957 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700958 if enableMinimalRuntime(d.sanitize) {
959 // If a static dependency is built with the minimal runtime,
960 // make sure we include the ubsan minimal runtime.
961 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +0900962 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -0700963 // If a static dependency runs with full ubsan diagnostics,
964 // make sure we include the ubsan runtime.
965 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800966 }
Colin Cross0b908332019-06-19 23:00:20 -0700967
968 if c.sanitize.Properties.MinimalRuntimeDep &&
969 c.sanitize.Properties.UbsanRuntimeDep {
970 // both flags that this mutator might set are true, so don't bother recursing
971 return false
972 }
973
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500974 if c.Os() == android.Linux {
975 c.sanitize.Properties.BuiltinsDep = true
976 }
977
Colin Cross0b908332019-06-19 23:00:20 -0700978 return true
Colin Cross6b753602018-06-21 13:03:07 -0700979 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900980
Jose Galmesf7294582020-11-13 12:07:36 -0800981 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +0900982 if Bool(p.properties.Sanitize_minimal_dep) {
983 c.sanitize.Properties.MinimalRuntimeDep = true
984 }
985 if Bool(p.properties.Sanitize_ubsan_dep) {
986 c.sanitize.Properties.UbsanRuntimeDep = true
987 }
988 }
989
990 return false
Colin Cross6b753602018-06-21 13:03:07 -0700991 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800992 }
993}
994
Jiyong Park379de2f2018-12-19 02:47:14 +0900995// Add the dependency to the runtime library for each of the sanitizer variants
996func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900997 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000998 if !c.Enabled() {
999 return
1000 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001001 var sanitizers []string
1002 var diagSanitizers []string
1003
1004 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
1005 sanitizers = append(sanitizers, "undefined")
1006 } else {
1007 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
1008 sanitizers = append(sanitizers,
1009 "bool",
1010 "integer-divide-by-zero",
1011 "return",
1012 "returns-nonnull-attribute",
1013 "shift-exponent",
1014 "unreachable",
1015 "vla-bound",
1016 // TODO(danalbert): The following checks currently have compiler performance issues.
1017 //"alignment",
1018 //"bounds",
1019 //"enum",
1020 //"float-cast-overflow",
1021 //"float-divide-by-zero",
1022 //"nonnull-attribute",
1023 //"null",
1024 //"shift-base",
1025 //"signed-integer-overflow",
1026 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1027 // https://llvm.org/PR19302
1028 // http://reviews.llvm.org/D6974
1029 // "object-size",
1030 )
1031 }
1032 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
1033 }
1034
1035 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
1036 diagSanitizers = append(diagSanitizers, "undefined")
1037 }
1038
1039 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
1040
1041 if Bool(c.sanitize.Properties.Sanitize.Address) {
1042 sanitizers = append(sanitizers, "address")
1043 diagSanitizers = append(diagSanitizers, "address")
1044 }
1045
1046 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1047 sanitizers = append(sanitizers, "hwaddress")
1048 }
1049
1050 if Bool(c.sanitize.Properties.Sanitize.Thread) {
1051 sanitizers = append(sanitizers, "thread")
1052 }
1053
1054 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
1055 sanitizers = append(sanitizers, "safe-stack")
1056 }
1057
1058 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
1059 sanitizers = append(sanitizers, "cfi")
1060
1061 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
1062 diagSanitizers = append(diagSanitizers, "cfi")
1063 }
1064 }
1065
1066 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
1067 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1068 sanitizers = append(sanitizers, "signed-integer-overflow")
1069 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
1070 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1071 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1072 }
1073 }
1074
1075 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1076 sanitizers = append(sanitizers, "scudo")
1077 }
1078
1079 if Bool(c.sanitize.Properties.Sanitize.Scs) {
1080 sanitizers = append(sanitizers, "shadow-call-stack")
1081 }
1082
Ivan Lozanod7586b62021-04-01 09:49:36 -04001083 if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.Binary() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001084 noteDep := "note_memtag_heap_async"
1085 if Bool(c.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
1086 noteDep = "note_memtag_heap_sync"
1087 }
Inseob Kim253f5212021-04-08 17:10:31 +09001088 // If we're using snapshots, redirect to snapshot whenever possible
1089 // TODO(b/178470649): clean manual snapshot redirections
1090 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1091 if lib, ok := snapshot.StaticLibs[noteDep]; ok {
1092 noteDep = lib
1093 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001094 depTag := libraryDependencyTag{Kind: staticLibraryDependency, wholeStatic: true}
1095 variations := append(mctx.Target().Variations(),
1096 blueprint.Variation{Mutator: "link", Variation: "static"})
1097 if c.Device() {
1098 variations = append(variations, c.ImageVariation())
1099 }
1100 mctx.AddFarVariationDependencies(variations, depTag, noteDep)
1101 }
1102
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001103 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
1104 sanitizers = append(sanitizers, "fuzzer-no-link")
1105 }
1106
Jiyong Park379de2f2018-12-19 02:47:14 +09001107 // Save the list of sanitizers. These will be used again when generating
1108 // the build rules (for Cflags, etc.)
1109 c.sanitize.Properties.Sanitizers = sanitizers
1110 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1111
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001112 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1113 if c.Host() {
1114 diagSanitizers = sanitizers
1115 }
1116
Jiyong Park379de2f2018-12-19 02:47:14 +09001117 // Determine the runtime library required
1118 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001119 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001120 toolchain := c.toolchain(mctx)
1121 if Bool(c.sanitize.Properties.Sanitize.Address) {
1122 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1123 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1124 if c.staticBinary() {
1125 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001126 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001127 } else {
1128 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1129 }
1130 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
1131 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1132 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1133 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1134 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1135 } else {
1136 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1137 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001138 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001139 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
1140 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
1141 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001142 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
Colin Cross32f1de32021-03-29 13:41:37 -07001143 if c.staticBinary() {
1144 runtimeLibrary += ".static"
1145 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001146 }
1147
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001148 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
1149 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001150
1151 // Adding dependency to the runtime library. We are using *FarVariation*
1152 // because the runtime libraries themselves are not mutated by sanitizer
1153 // mutators and thus don't have sanitizer variants whereas this module
1154 // has been already mutated.
1155 //
1156 // Note that by adding dependency with {static|shared}DepTag, the lib is
1157 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
1158 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001159 deps := append(extraStaticDeps, runtimeLibrary)
Colin Crosse0edaf92021-01-11 17:31:17 -08001160 // If we're using snapshots, redirect to snapshot whenever possible
1161 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1162 for idx, dep := range deps {
1163 if lib, ok := snapshot.StaticLibs[dep]; ok {
1164 deps[idx] = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001165 }
1166 }
1167
Jiyong Park379de2f2018-12-19 02:47:14 +09001168 // static executable gets static runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -07001169 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Colin Cross42507332020-08-21 16:15:23 -07001170 variations := append(mctx.Target().Variations(),
1171 blueprint.Variation{Mutator: "link", Variation: "static"})
1172 if c.Device() {
1173 variations = append(variations, c.ImageVariation())
1174 }
1175 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001176 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001177 // If we're using snapshots, redirect to snapshot whenever possible
1178 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1179 if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
1180 runtimeLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001181 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001182
Cindy Zhou18417cb2020-12-10 07:12:38 -08001183 // Skip apex dependency check for sharedLibraryDependency
1184 // when sanitizer diags are enabled. Skipping the check will allow
1185 // building with diag libraries without having to list the
1186 // dependency in Apex's allowed_deps file.
1187 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001188 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001189 depTag := libraryDependencyTag{
1190 Kind: sharedLibraryDependency,
1191 Order: earlyLibraryDependency,
1192
1193 skipApexAllowedDependenciesCheck: diagEnabled,
1194 }
Colin Cross42507332020-08-21 16:15:23 -07001195 variations := append(mctx.Target().Variations(),
1196 blueprint.Variation{Mutator: "link", Variation: "shared"})
1197 if c.Device() {
1198 variations = append(variations, c.ImageVariation())
1199 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001200 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001201 }
1202 // static lib does not have dependency to the runtime library. The
1203 // dependency will be added to the executables or shared libs using
1204 // the static lib.
1205 }
1206 }
1207}
1208
1209type Sanitizeable interface {
1210 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +09001211 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001212 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001213 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001214}
1215
Ivan Lozanod7586b62021-04-01 09:49:36 -04001216func (c *Module) MinimalRuntimeDep() bool {
1217 return c.sanitize.Properties.MinimalRuntimeDep
1218}
1219
1220func (c *Module) UbsanRuntimeDep() bool {
1221 return c.sanitize.Properties.UbsanRuntimeDep
1222}
1223
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001224func (c *Module) SanitizePropDefined() bool {
1225 return c.sanitize != nil
1226}
1227
1228func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1229 return c.sanitize.isSanitizerEnabled(t)
1230}
1231
1232func (c *Module) SanitizeDep() bool {
1233 return c.sanitize.Properties.SanitizeDep
1234}
1235
1236func (c *Module) StaticallyLinked() bool {
1237 return c.static()
1238}
1239
1240func (c *Module) SetInSanitizerDir() {
1241 if c.sanitize != nil {
1242 c.sanitize.Properties.InSanitizerDir = true
1243 }
1244}
1245
1246func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1247 if c.sanitize != nil {
1248 c.sanitize.SetSanitizer(t, b)
1249 }
1250}
1251
1252func (c *Module) SetSanitizeDep(b bool) {
1253 if c.sanitize != nil {
1254 c.sanitize.Properties.SanitizeDep = b
1255 }
1256}
1257
1258var _ PlatformSanitizeable = (*Module)(nil)
1259
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001260// Create sanitized variants for modules that need them
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001261func sanitizerMutator(t SanitizerType) func(android.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -07001262 return func(mctx android.BottomUpMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001263 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1264 if c.IsDependencyRoot() && c.IsSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001265 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001266 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1267 } else if c.IsSanitizerEnabled(t) || c.SanitizeDep() {
1268 isSanitizerEnabled := c.IsSanitizerEnabled(t)
Colin Crossaf98f582021-05-12 17:27:32 -07001269 if c.StaticallyLinked() || c.Header() || t == Fuzzer {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001270 // Static and header libs are split into non-sanitized and sanitized variants.
1271 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1272 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1273 // that isn't sanitized for asan/fuzzer.
1274 //
1275 // Note for defaultVariation: since we don't split for shared libs but for static/header
1276 // libs, it is possible for the sanitized variant of a static/header lib to depend
1277 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1278 // error when the module is split. defaultVariation is the name of the variation that
1279 // will be used when such a dangling dependency occurs during the split of the current
1280 // module. By setting it to the name of the sanitized variation, the dangling dependency
1281 // is redirected to the sanitized variant of the dependent module.
1282 defaultVariation := t.variationName()
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001283 // Not all PlatformSanitizeable modules support the CFI sanitizer
1284 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001285 mctx.SetDefaultDependencyVariation(&defaultVariation)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001286
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001287 modules := mctx.CreateVariations("", t.variationName())
1288 modules[0].(PlatformSanitizeable).SetSanitizer(t, false)
1289 modules[1].(PlatformSanitizeable).SetSanitizer(t, true)
1290 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
1291 modules[1].(PlatformSanitizeable).SetSanitizeDep(false)
1292
1293 if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
Ivan Lozano4774a812020-03-10 16:23:57 -04001294 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1295 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001296 modules[1].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001297 }
1298
Jiyong Park1d1119f2019-07-29 21:27:18 +09001299 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1300 // to Make, because the sanitized version has a different suffix in name.
1301 // For other types of sanitizers, suppress the variation that is disabled.
Tri Vo6eafc362021-04-01 11:29:09 -07001302 if t != cfi && t != scs && t != Hwasan {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001303 if isSanitizerEnabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001304 modules[0].(PlatformSanitizeable).SetPreventInstall()
1305 modules[0].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001306 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001307 modules[1].(PlatformSanitizeable).SetPreventInstall()
1308 modules[1].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001309 }
1310 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001311
Jiyong Park1d1119f2019-07-29 21:27:18 +09001312 // Export the static lib name to make
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001313 if c.StaticallyLinked() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001314 if t == cfi {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001315 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
Tri Vo6eafc362021-04-01 11:29:09 -07001316 } else if t == Hwasan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001317 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001318 }
1319 }
1320 } else {
1321 // Shared libs are not split. Only the sanitized variant is created.
1322 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001323 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1324 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
Vishwath Mohane7128792017-11-17 11:08:10 -08001325
Jiyong Park1d1119f2019-07-29 21:27:18 +09001326 // locate the asan libraries under /data/asan
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001327 if mctx.Device() && t == Asan && isSanitizerEnabled {
1328 modules[0].(PlatformSanitizeable).SetInSanitizerDir()
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001329 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001330
1331 if mctx.Device() && t.incompatibleWithCfi() {
1332 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1333 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001334 modules[0].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001335 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001336 }
Colin Cross16b23492016-01-06 14:41:07 -08001337 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001338 c.SetSanitizeDep(false)
Jiyong Park82226632019-02-01 10:50:50 +09001339 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001340 // APEX modules fall here
Jooyung Han8ce8db92020-05-15 19:05:05 +09001341 sanitizeable.AddSanitizerDependencies(mctx, t.name())
Jiyong Park82226632019-02-01 10:50:50 +09001342 mctx.CreateVariations(t.variationName())
Inseob Kimc42f2f22020-07-29 20:32:10 +09001343 } else if c, ok := mctx.Module().(*Module); ok {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001344 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1345
Inseob Kimc42f2f22020-07-29 20:32:10 +09001346 // Check if it's a snapshot module supporting sanitizer
1347 if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) {
1348 // Set default variation as above.
1349 defaultVariation := t.variationName()
1350 mctx.SetDefaultDependencyVariation(&defaultVariation)
1351 modules := mctx.CreateVariations("", t.variationName())
1352 modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false)
1353 modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true)
1354
1355 // Export the static lib name to make
1356 if c.static() && c.ExportedToMake() {
1357 if t == cfi {
1358 // use BaseModuleName which is the name for Make.
1359 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1360 }
1361 }
1362 }
Colin Cross16b23492016-01-06 14:41:07 -08001363 }
1364 }
1365}
Vishwath Mohane7128792017-11-17 11:08:10 -08001366
Inseob Kim74d25562020-08-04 00:41:38 +09001367type sanitizerStaticLibsMap struct {
1368 // libsMap contains one list of modules per each image and each arch.
1369 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001370 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001371 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001372 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001373}
1374
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001375func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001376 return &sanitizerStaticLibsMap{
1377 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001378 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001379 }
1380}
1381
1382// Add the current module to sanitizer static libs maps
1383// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001384func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1385 image := GetImageVariantType(c)
1386 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001387
1388 s.libsMapLock.Lock()
1389 defer s.libsMapLock.Unlock()
1390
1391 if _, ok := s.libsMap[image]; !ok {
1392 s.libsMap[image] = make(map[string][]string)
1393 }
1394
1395 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1396}
1397
1398// Exports makefile variables in the following format:
1399// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1400// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1401// These are to be used by use_soong_sanitized_static_libraries.
1402// See build/make/core/binary.mk for more details.
1403func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1404 for _, image := range android.SortedStringKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001405 archMap := s.libsMap[ImageVariantType(image)]
Inseob Kim74d25562020-08-04 00:41:38 +09001406 for _, arch := range android.SortedStringKeys(archMap) {
1407 libs := archMap[arch]
1408 sort.Strings(libs)
1409
1410 key := fmt.Sprintf(
1411 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1412 s.sanitizerType.variationName(),
1413 image, // already upper
1414 arch)
1415
1416 ctx.Strict(key, strings.Join(libs, " "))
1417 }
1418 }
1419}
1420
Colin Cross571cccf2019-02-04 11:22:08 -08001421var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1422
Inseob Kim74d25562020-08-04 00:41:38 +09001423func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001424 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001425 return newSanitizerStaticLibsMap(cfi)
1426 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001427}
1428
Colin Cross571cccf2019-02-04 11:22:08 -08001429var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1430
Inseob Kim74d25562020-08-04 00:41:38 +09001431func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001432 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001433 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001434 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001435}
1436
Ivan Lozano30c5db22018-02-21 15:49:20 -08001437func enableMinimalRuntime(sanitize *sanitize) bool {
1438 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001439 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001440 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001441
Ivan Lozano30c5db22018-02-21 15:49:20 -08001442 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001443 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1444 Bool(sanitize.Properties.Sanitize.Undefined) ||
1445 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1446
Ivan Lozano30c5db22018-02-21 15:49:20 -08001447 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1448 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001449 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001450 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001451
Ivan Lozano30c5db22018-02-21 15:49:20 -08001452 return true
1453 }
1454 return false
1455}
1456
Ivan Lozanod7586b62021-04-01 09:49:36 -04001457func (m *Module) UbsanRuntimeNeeded() bool {
1458 return enableUbsanRuntime(m.sanitize)
1459}
1460
1461func (m *Module) MinimalRuntimeNeeded() bool {
1462 return enableMinimalRuntime(m.sanitize)
1463}
1464
Inseob Kim8471cda2019-11-15 09:59:12 +09001465func enableUbsanRuntime(sanitize *sanitize) bool {
1466 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001467 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001468 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1469}
1470
Vishwath Mohane7128792017-11-17 11:08:10 -08001471func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001472 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001473}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001474
1475func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001476 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001477}