blob: c7e841122fa49a9b56454ca385c140876a80de11 [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"
Liz Kammerb2fc4702021-06-25 14:53:40 -040024 "github.com/google/blueprint/proptools"
Colin Cross6b753602018-06-21 13:03:07 -070025
Colin Cross635c3b02016-05-18 15:37:25 -070026 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070027 "android/soong/cc/config"
Kiyoung Kim48f37782021-07-07 12:42:39 +090028 "android/soong/snapshot"
Colin Cross16b23492016-01-06 14:41:07 -080029)
30
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070031var (
32 // Any C flags added by sanitizer which libTooling tools may not
33 // understand also need to be added to ClangLibToolingUnknownCflags in
34 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080035
Yi Kong20233a42019-08-21 01:38:40 -070036 asanCflags = []string{
37 "-fno-omit-frame-pointer",
38 "-fno-experimental-new-pass-manager",
39 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070040 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070041
Yi Kong286abc62021-11-04 16:14:14 +080042 hwasanCflags = []string{
43 "-fno-omit-frame-pointer",
44 "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000045 "-fsanitize-hwaddress-abi=platform",
Yi Kong286abc62021-11-04 16:14:14 +080046 }
47
48 // ThinLTO performs codegen during link time, thus these flags need to
49 // passed to both CFLAGS and LDFLAGS.
50 hwasanCommonflags = []string{
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080051 // The following improves debug location information
52 // availability at the cost of its accuracy. It increases
53 // the likelihood of a stack variable's frame offset
54 // to be recorded in the debug info, which is important
55 // for the quality of hwasan reports. The downside is a
56 // higher number of "optimized out" stack variables.
57 // b/112437883.
Yi Kong286abc62021-11-04 16:14:14 +080058 "-instcombine-lower-dbg-declare=0",
Mitch Phillipsb1c574f2020-06-22 13:28:23 -070059 // TODO(b/159343917): HWASan and GlobalISel don't play nicely, and
60 // GlobalISel is the default at -O0 on aarch64.
Yi Kong286abc62021-11-04 16:14:14 +080061 "--aarch64-enable-global-isel-at-O=-1",
62 "-fast-isel=false",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080063 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070064
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000065 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070066 "-fsanitize-ignorelist=external/compiler-rt/lib/cfi/cfi_blocklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070067 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
68 // used, but have no effect on assembly files
69 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070070 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070071 "-Wl,-plugin-opt,O1"}
Inseob Kim74d25562020-08-04 00:41:38 +090072 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070073
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070074 intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080075
Peter Collingbournebd19db02019-03-06 10:38:48 -080076 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070077 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070078 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
79 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070080)
81
Ivan Lozano3968d8f2020-12-14 11:27:52 -050082type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080083
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
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080089 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -050090 Fuzzer
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -070091 memtag_heap
Liz Kammer75db9312021-07-07 16:41:50 -040092 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -080093)
94
Liz Kammer75db9312021-07-07 16:41:50 -040095var Sanitizers = []SanitizerType{
96 Asan,
97 Hwasan,
98 tsan,
99 intOverflow,
100 scs,
101 Fuzzer,
102 memtag_heap,
103 cfi, // cfi is last to prevent it running before incompatible mutators
104}
105
Jiyong Park82226632019-02-01 10:50:50 +0900106// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500107func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800108 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500109 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800110 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700111 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700112 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800113 case tsan:
114 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700115 case intOverflow:
116 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000117 case cfi:
118 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800119 case scs:
120 return "scs"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700121 case memtag_heap:
122 return "memtag_heap"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500123 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700124 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800125 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500126 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800127 }
128}
129
Jiyong Park82226632019-02-01 10:50:50 +0900130// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500131func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900132 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500133 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900134 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700135 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900136 return "hwaddress"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700137 case memtag_heap:
138 return "memtag_heap"
Jiyong Park82226632019-02-01 10:50:50 +0900139 case tsan:
140 return "thread"
141 case intOverflow:
142 return "integer_overflow"
143 case cfi:
144 return "cfi"
145 case scs:
146 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500147 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700148 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900149 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500150 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900151 }
152}
153
Liz Kammer75db9312021-07-07 16:41:50 -0400154func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
155 switch t {
156 case Asan, Hwasan, Fuzzer, scs, tsan, cfi:
157 ctx.TopDown(t.variationName()+"_deps", sanitizerDepsMutator(t))
158 ctx.BottomUp(t.variationName(), sanitizerMutator(t))
159 case memtag_heap, intOverflow:
160 // do nothing
161 default:
162 panic(fmt.Errorf("unknown SanitizerType %d", t))
163 }
164}
165
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500166func (*Module) SanitizerSupported(t SanitizerType) bool {
167 switch t {
168 case Asan:
169 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700170 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500171 return true
172 case tsan:
173 return true
174 case intOverflow:
175 return true
176 case cfi:
177 return true
178 case scs:
179 return true
180 case Fuzzer:
181 return true
182 default:
183 return false
184 }
185}
186
187// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
188func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700189 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900190}
191
Martin Stjernholmb0249572020-09-15 02:32:35 +0100192type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400193 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100194 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800195
Liz Kammer75b9b402021-06-25 15:19:27 -0400196 // ASan (Address sanitizer), incompatible with static binaries.
197 // Always runs in a diagnostic mode.
198 // Use of address sanitizer disables cfi sanitizer.
199 // Hwaddress sanitizer takes precedence over this sanitizer.
200 Address *bool `android:"arch_variant"`
201 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
202 // Always runs in a diagnostic mode.
203 // Use of thread sanitizer disables cfi and scudo sanitizers.
204 // Hwaddress sanitizer takes precedence over this sanitizer.
205 Thread *bool `android:"arch_variant"`
206 // HWASan (Hardware Address sanitizer).
207 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100208 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800209
Liz Kammer75b9b402021-06-25 15:19:27 -0400210 // Undefined behavior sanitizer
211 All_undefined *bool `android:"arch_variant"`
212 // Subset of undefined behavior sanitizer
213 Undefined *bool `android:"arch_variant"`
214 // List of specific undefined behavior sanitizers to enable
215 Misc_undefined []string `android:"arch_variant"`
216 // Fuzzer, incompatible with static binaries.
217 Fuzzer *bool `android:"arch_variant"`
218 // safe-stack sanitizer, incompatible with 32-bit architectures.
219 Safestack *bool `android:"arch_variant"`
220 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
221 Cfi *bool `android:"arch_variant"`
222 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
223 Integer_overflow *bool `android:"arch_variant"`
224 // scudo sanitizer, incompatible with asan, hwasan, tsan
225 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
226 // deprecated
227 Scudo *bool `android:"arch_variant"`
228 // shadow-call-stack sanitizer, only available on arm64
229 Scs *bool `android:"arch_variant"`
230 // Memory-tagging, only available on arm64
231 // if diag.memtag unset or false, enables async memory tagging
232 Memtag_heap *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100233
234 // A modifier for ASAN and HWASAN for write only instrumentation
235 Writeonly *bool `android:"arch_variant"`
236
237 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
238 // Replaces abort() on error with a human-readable error message.
239 // Address and Thread sanitizers always run in diagnostic mode.
240 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400241 // Undefined behavior sanitizer, diagnostic mode
242 Undefined *bool `android:"arch_variant"`
243 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
244 Cfi *bool `android:"arch_variant"`
245 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
246 Integer_overflow *bool `android:"arch_variant"`
247 // Memory-tagging, only available on arm64
248 // requires sanitizer.memtag: true
249 // if set, enables sync memory tagging
250 Memtag_heap *bool `android:"arch_variant"`
251 // List of specific undefined behavior sanitizers to enable in diagnostic mode
252 Misc_undefined []string `android:"arch_variant"`
253 // List of sanitizers to pass to -fno-sanitize-recover
254 // results in only the first detected error for these sanitizers being reported and program then
255 // exits with a non-zero exit code.
256 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800257 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800258
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800259 // Sanitizers to run with flag configuration specified
260 Config struct {
261 // Enables CFI support flags for assembly-heavy libraries
262 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800263 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800264
Liz Kammer75b9b402021-06-25 15:19:27 -0400265 // List of sanitizers to pass to -fsanitize-recover
266 // allows execution to continue for these sanitizers to detect multiple errors rather than only
267 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100268 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000269
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700270 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100271 Blocklist *string
272}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700273
Martin Stjernholmb0249572020-09-15 02:32:35 +0100274type SanitizeProperties struct {
Martin Stjernholmb0249572020-09-15 02:32:35 +0100275 Sanitize SanitizeUserProps `android:"arch_variant"`
276 SanitizerEnabled bool `blueprint:"mutated"`
277 SanitizeDep bool `blueprint:"mutated"`
278 MinimalRuntimeDep bool `blueprint:"mutated"`
279 BuiltinsDep bool `blueprint:"mutated"`
280 UbsanRuntimeDep bool `blueprint:"mutated"`
281 InSanitizerDir bool `blueprint:"mutated"`
282 Sanitizers []string `blueprint:"mutated"`
283 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800284}
285
286type sanitize struct {
287 Properties SanitizeProperties
288}
289
Cindy Zhou18417cb2020-12-10 07:12:38 -0800290// Mark this tag with a check to see if apex dependency check should be skipped
291func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
292 return t.skipApexAllowedDependenciesCheck
293}
294
295var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
296
Vishwath Mohane7128792017-11-17 11:08:10 -0800297func init() {
298 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700299 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800300}
301
Colin Cross16b23492016-01-06 14:41:07 -0800302func (sanitize *sanitize) props() []interface{} {
303 return []interface{}{&sanitize.Properties}
304}
305
306func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700307 s := &sanitize.Properties.Sanitize
308
Colin Cross16b23492016-01-06 14:41:07 -0800309 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700310 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800311 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800312 }
313
314 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800315 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800316 return
317 }
318
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800319 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400320 if ctx.testBinary() {
321 if s.Memtag_heap == nil {
322 s.Memtag_heap = proptools.BoolPtr(true)
323 }
324 if s.Diag.Memtag_heap == nil {
325 s.Diag.Memtag_heap = proptools.BoolPtr(true)
326 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800327 }
328
Colin Cross16b23492016-01-06 14:41:07 -0800329 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700330 var globalSanitizersDiag []string
331
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700332 if ctx.Host() {
333 if !ctx.Windows() {
334 globalSanitizers = ctx.Config().SanitizeHost()
335 }
336 } else {
337 arches := ctx.Config().SanitizeDeviceArch()
338 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
339 globalSanitizers = ctx.Config().SanitizeDevice()
340 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800341 }
342 }
343
Colin Cross16b23492016-01-06 14:41:07 -0800344 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000345 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700346 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400347 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000348 }
Colin Cross16b23492016-01-06 14:41:07 -0800349
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700350 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400351 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000352 }
353
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700354 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400355 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000356 }
357
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700358 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400359 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000360 }
361
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700362 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400363 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700364 }
365
366 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400367 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000368 }
369
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700370 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800371 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400372 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700373 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700374 }
375
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700376 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700377 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700378 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400379 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700380 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700381 }
382
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700383 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400384 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700385 }
386
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700387 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400388 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700389 }
390
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000391 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
392 // Hwaddress and Address are set before, so we can check them here
393 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
394 if s.Address == nil && s.Hwaddress == nil {
395 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
396 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400397 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000398 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700399 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800400 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400401 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800402 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700403 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000404
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000405 if len(globalSanitizers) > 0 {
406 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
407 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700408
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700409 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700410 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700411 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400412 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700413 }
414
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700415 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
416 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400417 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700418 }
419
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800420 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
421 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400422 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800423 }
424
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700425 if len(globalSanitizersDiag) > 0 {
426 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
427 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700428 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700429
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800430 // Enable Memtag for all components in the include paths (for Aarch64 only)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800431 if ctx.Arch().ArchType == android.Arm64 {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800432 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800433 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400434 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800435 }
436 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400437 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800438 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800439 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800440 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400441 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800442 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800443 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700444 }
445
Elvis Chien9c993542021-06-25 01:15:17 +0800446 // Enable CFI for non-host components in the include paths
447 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400448 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000449 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400450 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700451 }
452 }
453
Elliott Hughesda3a0712020-03-06 16:55:28 -0800454 // Is CFI actually enabled?
455 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400456 s.Cfi = nil
457 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800458 }
459
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700460 // HWASan requires AArch64 hardware feature (top-byte-ignore).
461 if ctx.Arch().ArchType != android.Arm64 {
462 s.Hwaddress = nil
463 }
464
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800465 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800466 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800467 s.Scs = nil
468 }
469
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700470 // memtag_heap is only implemented on AArch64.
471 if ctx.Arch().ArchType != android.Arm64 {
472 s.Memtag_heap = nil
473 }
474
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700475 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700476 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400477 s.Cfi = nil
478 s.Diag.Cfi = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700479 }
480
Colin Cross528d67e2021-07-23 22:23:07 +0000481 // Disable sanitizers that depend on the UBSan runtime for windows/darwin/musl builds.
482 if !ctx.Os().Linux() || ctx.Os() == android.LinuxMusl {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400483 s.Cfi = nil
484 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700485 s.Misc_undefined = nil
486 s.Undefined = nil
487 s.All_undefined = nil
488 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800489 }
490
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700491 // Also disable CFI for VNDK variants of components
492 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900493 if ctx.static() {
494 // Cfi variant for static vndk should be captured as vendor snapshot,
495 // so don't strictly disable Cfi.
496 s.Cfi = nil
497 s.Diag.Cfi = nil
498 } else {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400499 s.Cfi = nil
500 s.Diag.Cfi = nil
Inseob Kimc42f2f22020-07-29 20:32:10 +0900501 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900502 }
503
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700504 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700505 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
506 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700507 s.Hwaddress = nil
508 }
509
Colin Cross3c344ef2016-07-18 15:44:56 -0700510 if ctx.staticBinary() {
511 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700512 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700513 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800514 }
515
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700516 if Bool(s.All_undefined) {
517 s.Undefined = nil
518 }
519
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700520 if !ctx.toolchain().Is64Bit() {
521 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700522 s.Thread = nil
523 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800524 // TODO(ccross): error for compile_multilib = "32"?
525 }
526
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800527 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 -0700528 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 -0700529 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700530 sanitize.Properties.SanitizerEnabled = true
531 }
532
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800533 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
534 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700535 s.Scudo = nil
536 }
537
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700538 if Bool(s.Hwaddress) {
539 s.Address = nil
540 s.Thread = nil
Evgenii Stepanovb15a5642021-06-23 12:30:55 -0700541 // Disable ubsan diagnosic as a workaround for a compiler bug.
542 // TODO(b/191808836): re-enable.
543 s.Diag.Undefined = nil
544 s.Diag.Integer_overflow = nil
545 s.Diag.Misc_undefined = nil
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700546 }
547
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700548 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
549 // mutually incompatible.
550 if Bool(s.Fuzzer) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400551 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800552 }
553}
554
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800555func toDisableImplicitIntegerChange(flags []string) bool {
556 // Returns true if any flag is fsanitize*integer, and there is
557 // no explicit flag about sanitize=implicit-integer-sign-change.
558 for _, f := range flags {
559 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
560 return false
561 }
562 }
563 for _, f := range flags {
564 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
565 return true
566 }
567 }
568 return false
569}
570
Yabin Cuidb7dda82020-11-30 15:47:45 -0800571func toDisableUnsignedShiftBaseChange(flags []string) bool {
572 // Returns true if any flag is fsanitize*integer, and there is
573 // no explicit flag about sanitize=unsigned-shift-base.
574 for _, f := range flags {
575 if strings.Contains(f, "sanitize=unsigned-shift-base") {
576 return false
577 }
578 }
579 for _, f := range flags {
580 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
581 return true
582 }
583 }
584 return false
585}
586
Colin Cross16b23492016-01-06 14:41:07 -0800587func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700588 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
589 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500590 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
591 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800592
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500593 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800594 flags.Local.LdFlags = append(flags.Local.LdFlags,
595 minimalRuntimePath,
596 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800597 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500598
599 if sanitize.Properties.BuiltinsDep {
600 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
601 }
602
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700603 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800604 return flags
605 }
606
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700607 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700608 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800609 // Frame pointer based unwinder in ASan requires ARM frame setup.
610 // TODO: put in flags?
611 flags.RequiredInstructionSet = "arm"
612 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800613 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
614 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800615
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000616 if Bool(sanitize.Properties.Sanitize.Writeonly) {
617 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
618 }
619
Colin Cross16b23492016-01-06 14:41:07 -0800620 if ctx.Host() {
621 // -nodefaultlibs (provided with libc++) prevents the driver from linking
622 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800623 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800624 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800625 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900626 if ctx.bootstrap() {
627 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
628 } else {
629 flags.DynamicLinker = "/system/bin/linker_asan"
630 }
Colin Cross16b23492016-01-06 14:41:07 -0800631 if flags.Toolchain.Is64Bit() {
632 flags.DynamicLinker += "64"
633 }
634 }
Colin Cross16b23492016-01-06 14:41:07 -0800635 }
636
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700637 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800638 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800639
640 for _, flag := range hwasanCommonflags {
641 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
642 }
643 for _, flag := range hwasanCommonflags {
644 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
645 }
646
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000647 if Bool(sanitize.Properties.Sanitize.Writeonly) {
648 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
649 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700650 }
651
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700652 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800653 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700654
655 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800656 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
657 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
658 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
659 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700660
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700661 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
662 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
663 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800664 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
665 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700666
Mitch Phillips74384752019-06-17 10:33:52 -0700667 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800668 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700669
670 // Disable fortify for fuzzing builds. Generally, we'll be building with
671 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800672 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800673
674 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
675 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
676 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
677 // the DT_RUNPATH from the shared library above it, and not the executable,
678 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
679 // DT_RUNPATH here means that transient shared libraries can be found
680 // colocated with their parents.
681 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800682 }
683
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700684 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800685 if ctx.Arch().ArchType == android.Arm {
686 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
687 // to do this on a function basis, so force Thumb on the entire module.
688 flags.RequiredInstructionSet = "thumb"
689 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000690
Colin Cross4af21ed2019-11-04 09:37:55 -0800691 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
692 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800693 if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
694 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
695 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000696 // Only append the default visibility flag if -fvisibility has not already been set
697 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800698 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
699 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000700 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800701 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000702
703 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800704 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
705 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000706 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700707 }
708
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700709 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800710 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700711 }
712
Jiyong Park379de2f2018-12-19 02:47:14 +0900713 if len(sanitize.Properties.Sanitizers) > 0 {
714 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800715
Colin Cross4af21ed2019-11-04 09:37:55 -0800716 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
717 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800718 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800719 // Host sanitizers only link symbols in the final executable, so
720 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800721 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
722 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500723
724 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
725 if !ctx.toolchain().Bionic() {
726 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
727 }
728 }
729
730 if enableMinimalRuntime(sanitize) {
731 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
732 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
733 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
734 if !ctx.toolchain().Bionic() {
735 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800736 }
Colin Cross16b23492016-01-06 14:41:07 -0800737 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700738
739 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
740 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800741 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700742 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800743 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700744 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800745 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700746 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800747 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800748 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
749 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800750 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800751 // http://b/171275751, Android doesn't build with this sanitizer yet.
752 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
753 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
754 }
Colin Cross16b23492016-01-06 14:41:07 -0800755 }
756
Jiyong Park379de2f2018-12-19 02:47:14 +0900757 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800758 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700759 }
760 // FIXME: enable RTTI if diag + (cfi or vptr)
761
Andreas Gampe97071162017-05-08 13:15:23 -0700762 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800763 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700764 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
765 }
766
Ivan Lozano7929bba2018-12-12 09:36:31 -0800767 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800768 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800769 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
770 }
771
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700772 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
773 if blocklist.Valid() {
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700774 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-ignorelist="+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700775 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
776 }
777
Colin Cross16b23492016-01-06 14:41:07 -0800778 return flags
779}
780
Colin Crossd80cbca2020-02-24 12:01:37 -0800781func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900782 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
783 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800784 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900785 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800786 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900787 }
788 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800789 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900790 }
791 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800792 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900793 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800794 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700795}
796
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700797func (sanitize *sanitize) inSanitizerDir() bool {
798 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700799}
800
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500801// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
802func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000803 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500804 case Asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000805 return sanitize.Properties.Sanitize.Address
Tri Vo6eafc362021-04-01 11:29:09 -0700806 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700807 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000808 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000809 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000810 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000811 return sanitize.Properties.Sanitize.Integer_overflow
812 case cfi:
813 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800814 case scs:
815 return sanitize.Properties.Sanitize.Scs
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700816 case memtag_heap:
817 return sanitize.Properties.Sanitize.Memtag_heap
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500818 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700819 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000820 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500821 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000822 }
823}
824
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500825// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800826func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500827 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700828 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800829 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800830 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700831 !sanitize.isSanitizerEnabled(scs) &&
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700832 !sanitize.isSanitizerEnabled(memtag_heap) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500833 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800834}
835
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500836// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700837func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500838 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700839 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700840 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500841 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700842}
843
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500844func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400845 bPtr := proptools.BoolPtr(b)
846 if !b {
847 bPtr = nil
848 }
Colin Cross16b23492016-01-06 14:41:07 -0800849 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500850 case Asan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400851 sanitize.Properties.Sanitize.Address = bPtr
Tri Vo6eafc362021-04-01 11:29:09 -0700852 case Hwasan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400853 sanitize.Properties.Sanitize.Hwaddress = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800854 case tsan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400855 sanitize.Properties.Sanitize.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700856 case intOverflow:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400857 sanitize.Properties.Sanitize.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000858 case cfi:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400859 sanitize.Properties.Sanitize.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800860 case scs:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400861 sanitize.Properties.Sanitize.Scs = bPtr
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700862 case memtag_heap:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400863 sanitize.Properties.Sanitize.Memtag_heap = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500864 case Fuzzer:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400865 sanitize.Properties.Sanitize.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800866 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500867 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800868 }
869 if b {
870 sanitize.Properties.SanitizerEnabled = true
871 }
872}
873
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000874// Check if the sanitizer is explicitly disabled (as opposed to nil by
875// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500876func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000877 if sanitize == nil {
878 return false
879 }
880
881 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
882 return sanitizerVal != nil && *sanitizerVal == false
883}
884
885// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
886// because enabling a sanitizer either directly (via the blueprint) or
887// indirectly (via a mutator) sets the bool ptr to true, and you can't
888// distinguish between the cases. It isn't needed though - both cases can be
889// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500890func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000891 if sanitize == nil {
892 return false
893 }
894
895 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
896 return sanitizerVal != nil && *sanitizerVal == true
897}
898
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500899// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
900func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700901 switch t := tag.(type) {
902 case dependencyTag:
903 return t == reuseObjTag || t == objDepTag
904 case libraryDependencyTag:
905 return true
906 default:
907 return false
908 }
Colin Cross6b753602018-06-21 13:03:07 -0700909}
910
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500911func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
912 return IsSanitizableDependencyTag
913}
914
Inseob Kimc42f2f22020-07-29 20:32:10 +0900915// Determines if the current module is a static library going to be captured
916// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
917// except for ones which explicitly disable cfi.
918func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
Kiyoung Kim48f37782021-07-07 12:42:39 +0900919 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900920 return false
921 }
922
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500923 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900924
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500925 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900926 return false
927 }
928
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500929 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900930 return false
931 }
932
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500933 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900934 return false
935 }
936
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500937 if !c.SanitizerSupported(cfi) {
938 return false
939 }
940
941 return c.SanitizePropDefined() &&
942 !c.SanitizeNever() &&
943 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900944}
945
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700946// Propagate sanitizer requirements down from binaries
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500947func sanitizerDepsMutator(t SanitizerType) func(android.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700948 return func(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500949 if c, ok := mctx.Module().(PlatformSanitizeable); ok {
950 enabled := c.IsSanitizerEnabled(t)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900951 if t == cfi && needsCfiForVendorSnapshot(mctx) {
952 // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly
953 // determine defaultVariation in sanitizerMutator below.
954 // Instead, just mark SanitizeDep to forcefully create cfi variant.
955 enabled = true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500956 c.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900957 }
958 if enabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500959 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Inseob Kimc42f2f22020-07-29 20:32:10 +0900960 mctx.WalkDeps(func(child, parent android.Module) bool {
961 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
962 return false
963 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500964 if d, ok := child.(PlatformSanitizeable); ok && d.SanitizePropDefined() &&
965 !d.SanitizeNever() &&
966 !d.IsSanitizerExplicitlyDisabled(t) {
Colin Crossaf98f582021-05-12 17:27:32 -0700967 if t == cfi || t == Hwasan || t == scs || t == Asan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500968 if d.StaticallyLinked() && d.SanitizerSupported(t) {
969 // Rust does not support some of these sanitizers, so we need to check if it's
970 // supported before setting this true.
971 d.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900972 }
973 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500974 d.SetSanitizeDep(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700975 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000976 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900977 return true
978 })
979 }
Jiyong Parkf97782b2019-02-13 20:28:58 +0900980 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
981 // If an APEX module includes a lib which is enabled for a sanitizer T, then
982 // the APEX module is also enabled for the same sanitizer type.
983 mctx.VisitDirectDeps(func(child android.Module) {
984 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
985 sanitizeable.EnableSanitizer(t.name())
986 }
987 })
Colin Cross16b23492016-01-06 14:41:07 -0800988 }
989 }
990}
991
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500992func (c *Module) SanitizeNever() bool {
993 return Bool(c.sanitize.Properties.Sanitize.Never)
994}
995
996func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
997 return c.sanitize.isSanitizerExplicitlyDisabled(t)
998}
999
Ivan Lozano30c5db22018-02-21 15:49:20 -08001000// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001001func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001002 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001003 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001004 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001005 mctx.WalkDeps(func(child, parent android.Module) bool {
1006 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1007 return false
1008 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001009
Inseob Kimeec88e12020-01-22 11:11:29 +09001010 d, ok := child.(*Module)
1011 if !ok || !d.static() {
1012 return false
1013 }
1014 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001015 if enableMinimalRuntime(d.sanitize) {
1016 // If a static dependency is built with the minimal runtime,
1017 // make sure we include the ubsan minimal runtime.
1018 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001019 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001020 // If a static dependency runs with full ubsan diagnostics,
1021 // make sure we include the ubsan runtime.
1022 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001023 }
Colin Cross0b908332019-06-19 23:00:20 -07001024
1025 if c.sanitize.Properties.MinimalRuntimeDep &&
1026 c.sanitize.Properties.UbsanRuntimeDep {
1027 // both flags that this mutator might set are true, so don't bother recursing
1028 return false
1029 }
1030
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001031 if c.Os() == android.Linux {
1032 c.sanitize.Properties.BuiltinsDep = true
1033 }
1034
Colin Cross0b908332019-06-19 23:00:20 -07001035 return true
Colin Cross6b753602018-06-21 13:03:07 -07001036 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001037
Jose Galmesf7294582020-11-13 12:07:36 -08001038 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001039 if Bool(p.properties.Sanitize_minimal_dep) {
1040 c.sanitize.Properties.MinimalRuntimeDep = true
1041 }
1042 if Bool(p.properties.Sanitize_ubsan_dep) {
1043 c.sanitize.Properties.UbsanRuntimeDep = true
1044 }
1045 }
1046
1047 return false
Colin Cross6b753602018-06-21 13:03:07 -07001048 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001049 }
1050}
1051
Jiyong Park379de2f2018-12-19 02:47:14 +09001052// Add the dependency to the runtime library for each of the sanitizer variants
1053func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001054 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001055 if !c.Enabled() {
1056 return
1057 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001058 var sanitizers []string
1059 var diagSanitizers []string
1060
1061 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
1062 sanitizers = append(sanitizers, "undefined")
1063 } else {
1064 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
1065 sanitizers = append(sanitizers,
1066 "bool",
1067 "integer-divide-by-zero",
1068 "return",
1069 "returns-nonnull-attribute",
1070 "shift-exponent",
1071 "unreachable",
1072 "vla-bound",
1073 // TODO(danalbert): The following checks currently have compiler performance issues.
1074 //"alignment",
1075 //"bounds",
1076 //"enum",
1077 //"float-cast-overflow",
1078 //"float-divide-by-zero",
1079 //"nonnull-attribute",
1080 //"null",
1081 //"shift-base",
1082 //"signed-integer-overflow",
1083 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1084 // https://llvm.org/PR19302
1085 // http://reviews.llvm.org/D6974
1086 // "object-size",
1087 )
1088 }
1089 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
1090 }
1091
1092 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
1093 diagSanitizers = append(diagSanitizers, "undefined")
1094 }
1095
1096 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
1097
1098 if Bool(c.sanitize.Properties.Sanitize.Address) {
1099 sanitizers = append(sanitizers, "address")
1100 diagSanitizers = append(diagSanitizers, "address")
1101 }
1102
1103 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1104 sanitizers = append(sanitizers, "hwaddress")
1105 }
1106
1107 if Bool(c.sanitize.Properties.Sanitize.Thread) {
1108 sanitizers = append(sanitizers, "thread")
1109 }
1110
1111 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
1112 sanitizers = append(sanitizers, "safe-stack")
1113 }
1114
1115 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
1116 sanitizers = append(sanitizers, "cfi")
1117
1118 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
1119 diagSanitizers = append(diagSanitizers, "cfi")
1120 }
1121 }
1122
1123 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
1124 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1125 sanitizers = append(sanitizers, "signed-integer-overflow")
1126 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
1127 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1128 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1129 }
1130 }
1131
1132 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1133 sanitizers = append(sanitizers, "scudo")
1134 }
1135
1136 if Bool(c.sanitize.Properties.Sanitize.Scs) {
1137 sanitizers = append(sanitizers, "shadow-call-stack")
1138 }
1139
Ivan Lozanod7586b62021-04-01 09:49:36 -04001140 if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.Binary() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001141 noteDep := "note_memtag_heap_async"
1142 if Bool(c.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
1143 noteDep = "note_memtag_heap_sync"
1144 }
Inseob Kim253f5212021-04-08 17:10:31 +09001145 // If we're using snapshots, redirect to snapshot whenever possible
1146 // TODO(b/178470649): clean manual snapshot redirections
1147 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1148 if lib, ok := snapshot.StaticLibs[noteDep]; ok {
1149 noteDep = lib
1150 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001151 depTag := libraryDependencyTag{Kind: staticLibraryDependency, wholeStatic: true}
1152 variations := append(mctx.Target().Variations(),
1153 blueprint.Variation{Mutator: "link", Variation: "static"})
1154 if c.Device() {
1155 variations = append(variations, c.ImageVariation())
1156 }
1157 mctx.AddFarVariationDependencies(variations, depTag, noteDep)
1158 }
1159
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001160 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
1161 sanitizers = append(sanitizers, "fuzzer-no-link")
1162 }
1163
Jiyong Park379de2f2018-12-19 02:47:14 +09001164 // Save the list of sanitizers. These will be used again when generating
1165 // the build rules (for Cflags, etc.)
1166 c.sanitize.Properties.Sanitizers = sanitizers
1167 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1168
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001169 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1170 if c.Host() {
1171 diagSanitizers = sanitizers
1172 }
1173
Jiyong Park379de2f2018-12-19 02:47:14 +09001174 // Determine the runtime library required
1175 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001176 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001177 toolchain := c.toolchain(mctx)
1178 if Bool(c.sanitize.Properties.Sanitize.Address) {
1179 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1180 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1181 if c.staticBinary() {
1182 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001183 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001184 } else {
1185 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1186 }
1187 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
1188 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1189 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1190 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1191 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1192 } else {
1193 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1194 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001195 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001196 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
1197 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
1198 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001199 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
Colin Cross32f1de32021-03-29 13:41:37 -07001200 if c.staticBinary() {
1201 runtimeLibrary += ".static"
1202 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001203 }
1204
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001205 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
1206 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001207
1208 // Adding dependency to the runtime library. We are using *FarVariation*
1209 // because the runtime libraries themselves are not mutated by sanitizer
1210 // mutators and thus don't have sanitizer variants whereas this module
1211 // has been already mutated.
1212 //
1213 // Note that by adding dependency with {static|shared}DepTag, the lib is
1214 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
1215 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001216 deps := append(extraStaticDeps, runtimeLibrary)
Colin Crosse0edaf92021-01-11 17:31:17 -08001217 // If we're using snapshots, redirect to snapshot whenever possible
1218 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1219 for idx, dep := range deps {
1220 if lib, ok := snapshot.StaticLibs[dep]; ok {
1221 deps[idx] = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001222 }
1223 }
1224
Jiyong Park379de2f2018-12-19 02:47:14 +09001225 // static executable gets static runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -07001226 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Colin Cross42507332020-08-21 16:15:23 -07001227 variations := append(mctx.Target().Variations(),
1228 blueprint.Variation{Mutator: "link", Variation: "static"})
1229 if c.Device() {
1230 variations = append(variations, c.ImageVariation())
1231 }
1232 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001233 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001234 // If we're using snapshots, redirect to snapshot whenever possible
1235 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1236 if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
1237 runtimeLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001238 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001239
Cindy Zhou18417cb2020-12-10 07:12:38 -08001240 // Skip apex dependency check for sharedLibraryDependency
1241 // when sanitizer diags are enabled. Skipping the check will allow
1242 // building with diag libraries without having to list the
1243 // dependency in Apex's allowed_deps file.
1244 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001245 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001246 depTag := libraryDependencyTag{
1247 Kind: sharedLibraryDependency,
1248 Order: earlyLibraryDependency,
1249
1250 skipApexAllowedDependenciesCheck: diagEnabled,
1251 }
Colin Cross42507332020-08-21 16:15:23 -07001252 variations := append(mctx.Target().Variations(),
1253 blueprint.Variation{Mutator: "link", Variation: "shared"})
1254 if c.Device() {
1255 variations = append(variations, c.ImageVariation())
1256 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001257 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001258 }
1259 // static lib does not have dependency to the runtime library. The
1260 // dependency will be added to the executables or shared libs using
1261 // the static lib.
1262 }
1263 }
1264}
1265
1266type Sanitizeable interface {
1267 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +09001268 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001269 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001270 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001271}
1272
Ivan Lozanod7586b62021-04-01 09:49:36 -04001273func (c *Module) MinimalRuntimeDep() bool {
1274 return c.sanitize.Properties.MinimalRuntimeDep
1275}
1276
1277func (c *Module) UbsanRuntimeDep() bool {
1278 return c.sanitize.Properties.UbsanRuntimeDep
1279}
1280
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001281func (c *Module) SanitizePropDefined() bool {
1282 return c.sanitize != nil
1283}
1284
1285func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1286 return c.sanitize.isSanitizerEnabled(t)
1287}
1288
1289func (c *Module) SanitizeDep() bool {
1290 return c.sanitize.Properties.SanitizeDep
1291}
1292
1293func (c *Module) StaticallyLinked() bool {
1294 return c.static()
1295}
1296
1297func (c *Module) SetInSanitizerDir() {
1298 if c.sanitize != nil {
1299 c.sanitize.Properties.InSanitizerDir = true
1300 }
1301}
1302
1303func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1304 if c.sanitize != nil {
1305 c.sanitize.SetSanitizer(t, b)
1306 }
1307}
1308
1309func (c *Module) SetSanitizeDep(b bool) {
1310 if c.sanitize != nil {
1311 c.sanitize.Properties.SanitizeDep = b
1312 }
1313}
1314
1315var _ PlatformSanitizeable = (*Module)(nil)
1316
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001317// Create sanitized variants for modules that need them
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001318func sanitizerMutator(t SanitizerType) func(android.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -07001319 return func(mctx android.BottomUpMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001320 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
Liz Kammer187d5442021-06-25 14:50:12 -04001321 if c.Binary() && c.IsSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001322 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001323 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1324 } else if c.IsSanitizerEnabled(t) || c.SanitizeDep() {
1325 isSanitizerEnabled := c.IsSanitizerEnabled(t)
Colin Crossaf98f582021-05-12 17:27:32 -07001326 if c.StaticallyLinked() || c.Header() || t == Fuzzer {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001327 // Static and header libs are split into non-sanitized and sanitized variants.
1328 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1329 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1330 // that isn't sanitized for asan/fuzzer.
1331 //
1332 // Note for defaultVariation: since we don't split for shared libs but for static/header
1333 // libs, it is possible for the sanitized variant of a static/header lib to depend
1334 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1335 // error when the module is split. defaultVariation is the name of the variation that
1336 // will be used when such a dangling dependency occurs during the split of the current
1337 // module. By setting it to the name of the sanitized variation, the dangling dependency
1338 // is redirected to the sanitized variant of the dependent module.
1339 defaultVariation := t.variationName()
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001340 // Not all PlatformSanitizeable modules support the CFI sanitizer
1341 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001342 mctx.SetDefaultDependencyVariation(&defaultVariation)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001343
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001344 modules := mctx.CreateVariations("", t.variationName())
1345 modules[0].(PlatformSanitizeable).SetSanitizer(t, false)
1346 modules[1].(PlatformSanitizeable).SetSanitizer(t, true)
1347 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
1348 modules[1].(PlatformSanitizeable).SetSanitizeDep(false)
1349
1350 if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
Ivan Lozano4774a812020-03-10 16:23:57 -04001351 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1352 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001353 modules[1].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001354 }
1355
Jiyong Park1d1119f2019-07-29 21:27:18 +09001356 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1357 // to Make, because the sanitized version has a different suffix in name.
1358 // For other types of sanitizers, suppress the variation that is disabled.
Tri Vo6eafc362021-04-01 11:29:09 -07001359 if t != cfi && t != scs && t != Hwasan {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001360 if isSanitizerEnabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001361 modules[0].(PlatformSanitizeable).SetPreventInstall()
1362 modules[0].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001363 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001364 modules[1].(PlatformSanitizeable).SetPreventInstall()
1365 modules[1].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001366 }
1367 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001368
Jiyong Park1d1119f2019-07-29 21:27:18 +09001369 // Export the static lib name to make
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001370 if c.StaticallyLinked() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001371 if t == cfi {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001372 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
Tri Vo6eafc362021-04-01 11:29:09 -07001373 } else if t == Hwasan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001374 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001375 }
1376 }
1377 } else {
1378 // Shared libs are not split. Only the sanitized variant is created.
1379 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001380 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1381 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
Vishwath Mohane7128792017-11-17 11:08:10 -08001382
Jiyong Park1d1119f2019-07-29 21:27:18 +09001383 // locate the asan libraries under /data/asan
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001384 if mctx.Device() && t == Asan && isSanitizerEnabled {
1385 modules[0].(PlatformSanitizeable).SetInSanitizerDir()
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001386 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001387
1388 if mctx.Device() && t.incompatibleWithCfi() {
1389 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1390 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001391 modules[0].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001392 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001393 }
Colin Cross16b23492016-01-06 14:41:07 -08001394 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001395 c.SetSanitizeDep(false)
Jiyong Park82226632019-02-01 10:50:50 +09001396 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001397 // APEX modules fall here
Jooyung Han8ce8db92020-05-15 19:05:05 +09001398 sanitizeable.AddSanitizerDependencies(mctx, t.name())
Jiyong Park82226632019-02-01 10:50:50 +09001399 mctx.CreateVariations(t.variationName())
Inseob Kimc42f2f22020-07-29 20:32:10 +09001400 } else if c, ok := mctx.Module().(*Module); ok {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001401 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1402
Inseob Kimc42f2f22020-07-29 20:32:10 +09001403 // Check if it's a snapshot module supporting sanitizer
1404 if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) {
1405 // Set default variation as above.
1406 defaultVariation := t.variationName()
1407 mctx.SetDefaultDependencyVariation(&defaultVariation)
1408 modules := mctx.CreateVariations("", t.variationName())
1409 modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false)
1410 modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true)
1411
1412 // Export the static lib name to make
1413 if c.static() && c.ExportedToMake() {
1414 if t == cfi {
1415 // use BaseModuleName which is the name for Make.
1416 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1417 }
1418 }
1419 }
Colin Cross16b23492016-01-06 14:41:07 -08001420 }
1421 }
1422}
Vishwath Mohane7128792017-11-17 11:08:10 -08001423
Inseob Kim74d25562020-08-04 00:41:38 +09001424type sanitizerStaticLibsMap struct {
1425 // libsMap contains one list of modules per each image and each arch.
1426 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001427 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001428 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001429 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001430}
1431
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001432func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001433 return &sanitizerStaticLibsMap{
1434 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001435 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001436 }
1437}
1438
1439// Add the current module to sanitizer static libs maps
1440// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001441func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1442 image := GetImageVariantType(c)
1443 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001444
1445 s.libsMapLock.Lock()
1446 defer s.libsMapLock.Unlock()
1447
1448 if _, ok := s.libsMap[image]; !ok {
1449 s.libsMap[image] = make(map[string][]string)
1450 }
1451
1452 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1453}
1454
1455// Exports makefile variables in the following format:
1456// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1457// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1458// These are to be used by use_soong_sanitized_static_libraries.
1459// See build/make/core/binary.mk for more details.
1460func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1461 for _, image := range android.SortedStringKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001462 archMap := s.libsMap[ImageVariantType(image)]
Inseob Kim74d25562020-08-04 00:41:38 +09001463 for _, arch := range android.SortedStringKeys(archMap) {
1464 libs := archMap[arch]
1465 sort.Strings(libs)
1466
1467 key := fmt.Sprintf(
1468 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1469 s.sanitizerType.variationName(),
1470 image, // already upper
1471 arch)
1472
1473 ctx.Strict(key, strings.Join(libs, " "))
1474 }
1475 }
1476}
1477
Colin Cross571cccf2019-02-04 11:22:08 -08001478var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1479
Inseob Kim74d25562020-08-04 00:41:38 +09001480func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001481 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001482 return newSanitizerStaticLibsMap(cfi)
1483 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001484}
1485
Colin Cross571cccf2019-02-04 11:22:08 -08001486var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1487
Inseob Kim74d25562020-08-04 00:41:38 +09001488func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001489 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001490 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001491 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001492}
1493
Ivan Lozano30c5db22018-02-21 15:49:20 -08001494func enableMinimalRuntime(sanitize *sanitize) bool {
1495 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001496 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001497 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001498
Ivan Lozano30c5db22018-02-21 15:49:20 -08001499 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001500 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1501 Bool(sanitize.Properties.Sanitize.Undefined) ||
1502 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1503
Ivan Lozano30c5db22018-02-21 15:49:20 -08001504 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1505 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001506 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001507 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001508
Ivan Lozano30c5db22018-02-21 15:49:20 -08001509 return true
1510 }
1511 return false
1512}
1513
Ivan Lozanod7586b62021-04-01 09:49:36 -04001514func (m *Module) UbsanRuntimeNeeded() bool {
1515 return enableUbsanRuntime(m.sanitize)
1516}
1517
1518func (m *Module) MinimalRuntimeNeeded() bool {
1519 return enableMinimalRuntime(m.sanitize)
1520}
1521
Inseob Kim8471cda2019-11-15 09:59:12 +09001522func enableUbsanRuntime(sanitize *sanitize) bool {
1523 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001524 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001525 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1526}
1527
Vishwath Mohane7128792017-11-17 11:08:10 -08001528func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001529 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001530}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001531
1532func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001533 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001534}