blob: dc073817b068dd6c48983dee8eafa348f082e438 [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",
Yi Kong20233a42019-08-21 01:38:40 -070038 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070039 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070040
Yi Kong286abc62021-11-04 16:14:14 +080041 hwasanCflags = []string{
42 "-fno-omit-frame-pointer",
43 "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000044 "-fsanitize-hwaddress-abi=platform",
Florian Mayer0b981f52022-02-16 23:46:53 +000045 "-mllvm", "-hwasan-use-after-scope=1",
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",
Mitch Phillips59760562022-03-22 18:33:44 +000079 "export_memory_stats=0", "max_malloc_fill_size=4096", "malloc_fill_byte=0"}
Florian Mayerd8434a42022-08-31 20:57:03 +000080 memtagStackCommonFlags = []string{"-march=armv8-a+memtag"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070081)
82
Ivan Lozano3968d8f2020-12-14 11:27:52 -050083type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080084
Colin Cross16b23492016-01-06 14:41:07 -080085const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050086 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070087 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080088 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070089 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080090 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -050091 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -040092 Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +000093 Memtag_stack
Liz Kammer75db9312021-07-07 16:41:50 -040094 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -080095)
96
Liz Kammer75db9312021-07-07 16:41:50 -040097var Sanitizers = []SanitizerType{
98 Asan,
99 Hwasan,
100 tsan,
101 intOverflow,
102 scs,
103 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400104 Memtag_heap,
Florian Mayerd8434a42022-08-31 20:57:03 +0000105 Memtag_stack,
Liz Kammer75db9312021-07-07 16:41:50 -0400106 cfi, // cfi is last to prevent it running before incompatible mutators
107}
108
Jiyong Park82226632019-02-01 10:50:50 +0900109// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500110func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800111 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500112 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800113 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700114 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700115 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800116 case tsan:
117 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700118 case intOverflow:
119 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000120 case cfi:
121 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800122 case scs:
123 return "scs"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400124 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700125 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000126 case Memtag_stack:
127 return "memtag_stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500128 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700129 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800130 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500131 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800132 }
133}
134
Jiyong Park82226632019-02-01 10:50:50 +0900135// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500136func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900137 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500138 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900139 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700140 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900141 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400142 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700143 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000144 case Memtag_stack:
145 return "memtag_stack"
Jiyong Park82226632019-02-01 10:50:50 +0900146 case tsan:
147 return "thread"
148 case intOverflow:
149 return "integer_overflow"
150 case cfi:
151 return "cfi"
152 case scs:
153 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500154 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700155 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900156 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500157 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900158 }
159}
160
Liz Kammer75db9312021-07-07 16:41:50 -0400161func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
162 switch t {
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200163 case cfi, Hwasan, Asan, tsan, Fuzzer, scs:
164 sanitizer := &sanitizerSplitMutator{t}
165 ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
166 ctx.Transition(t.variationName(), sanitizer)
Florian Mayerd8434a42022-08-31 20:57:03 +0000167 case Memtag_heap, Memtag_stack, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400168 // do nothing
169 default:
170 panic(fmt.Errorf("unknown SanitizerType %d", t))
171 }
172}
173
Liz Kammerfd8a49f2022-10-31 10:31:11 -0400174// shouldPropagateToSharedLibraryDeps returns whether a sanitizer type should propagate to share
175// dependencies. In most cases, sanitizers only propagate to static dependencies; however, some
176// sanitizers also must be enabled for shared libraries for linking.
177func (t SanitizerType) shouldPropagateToSharedLibraryDeps() bool {
178 switch t {
179 case Fuzzer:
180 // Typically, shared libs are not split. However, for fuzzer, we split even for shared libs
181 // because a library sanitized for fuzzer can't be linked from a library that isn't sanitized
182 // for fuzzer.
183 return true
184 default:
185 return false
186 }
187}
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500188func (*Module) SanitizerSupported(t SanitizerType) bool {
189 switch t {
190 case Asan:
191 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700192 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500193 return true
194 case tsan:
195 return true
196 case intOverflow:
197 return true
198 case cfi:
199 return true
200 case scs:
201 return true
202 case Fuzzer:
203 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400204 case Memtag_heap:
205 return true
Florian Mayerd8434a42022-08-31 20:57:03 +0000206 case Memtag_stack:
207 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500208 default:
209 return false
210 }
211}
212
213// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
214func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700215 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900216}
217
Martin Stjernholmb0249572020-09-15 02:32:35 +0100218type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400219 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100220 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800221
Liz Kammer75b9b402021-06-25 15:19:27 -0400222 // ASan (Address sanitizer), incompatible with static binaries.
223 // Always runs in a diagnostic mode.
224 // Use of address sanitizer disables cfi sanitizer.
225 // Hwaddress sanitizer takes precedence over this sanitizer.
226 Address *bool `android:"arch_variant"`
227 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
228 // Always runs in a diagnostic mode.
229 // Use of thread sanitizer disables cfi and scudo sanitizers.
230 // Hwaddress sanitizer takes precedence over this sanitizer.
231 Thread *bool `android:"arch_variant"`
232 // HWASan (Hardware Address sanitizer).
233 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100234 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800235
Liz Kammer75b9b402021-06-25 15:19:27 -0400236 // Undefined behavior sanitizer
237 All_undefined *bool `android:"arch_variant"`
238 // Subset of undefined behavior sanitizer
239 Undefined *bool `android:"arch_variant"`
240 // List of specific undefined behavior sanitizers to enable
241 Misc_undefined []string `android:"arch_variant"`
242 // Fuzzer, incompatible with static binaries.
243 Fuzzer *bool `android:"arch_variant"`
244 // safe-stack sanitizer, incompatible with 32-bit architectures.
245 Safestack *bool `android:"arch_variant"`
246 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
247 Cfi *bool `android:"arch_variant"`
248 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
249 Integer_overflow *bool `android:"arch_variant"`
250 // scudo sanitizer, incompatible with asan, hwasan, tsan
251 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
252 // deprecated
253 Scudo *bool `android:"arch_variant"`
254 // shadow-call-stack sanitizer, only available on arm64
255 Scs *bool `android:"arch_variant"`
256 // Memory-tagging, only available on arm64
257 // if diag.memtag unset or false, enables async memory tagging
Florian Mayer00ab5cf2022-08-31 18:30:18 +0000258 Memtag_heap *bool `android:"arch_variant"`
Florian Mayerd8434a42022-08-31 20:57:03 +0000259 // Memory-tagging stack instrumentation, only available on arm64
260 // Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
261 Memtag_stack *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100262
263 // A modifier for ASAN and HWASAN for write only instrumentation
264 Writeonly *bool `android:"arch_variant"`
265
266 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
267 // Replaces abort() on error with a human-readable error message.
268 // Address and Thread sanitizers always run in diagnostic mode.
269 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400270 // Undefined behavior sanitizer, diagnostic mode
271 Undefined *bool `android:"arch_variant"`
272 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
273 Cfi *bool `android:"arch_variant"`
274 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
275 Integer_overflow *bool `android:"arch_variant"`
276 // Memory-tagging, only available on arm64
277 // requires sanitizer.memtag: true
278 // if set, enables sync memory tagging
279 Memtag_heap *bool `android:"arch_variant"`
280 // List of specific undefined behavior sanitizers to enable in diagnostic mode
281 Misc_undefined []string `android:"arch_variant"`
282 // List of sanitizers to pass to -fno-sanitize-recover
283 // results in only the first detected error for these sanitizers being reported and program then
284 // exits with a non-zero exit code.
285 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800286 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800287
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800288 // Sanitizers to run with flag configuration specified
289 Config struct {
290 // Enables CFI support flags for assembly-heavy libraries
291 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800292 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800293
Liz Kammer75b9b402021-06-25 15:19:27 -0400294 // List of sanitizers to pass to -fsanitize-recover
295 // allows execution to continue for these sanitizers to detect multiple errors rather than only
296 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100297 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000298
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700299 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100300 Blocklist *string
301}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700302
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400303type sanitizeMutatedProperties struct {
304 // Whether sanitizers can be enabled on this module
305 Never *bool `blueprint:"mutated"`
306
307 // Whether ASan (Address sanitizer) is enabled for this module.
308 // Hwaddress sanitizer takes precedence over this sanitizer.
309 Address *bool `blueprint:"mutated"`
310 // Whether TSan (Thread sanitizer) is enabled for this module
311 Thread *bool `blueprint:"mutated"`
312 // Whether HWASan (Hardware Address sanitizer) is enabled for this module
313 Hwaddress *bool `blueprint:"mutated"`
314
315 // Whether Undefined behavior sanitizer is enabled for this module
316 All_undefined *bool `blueprint:"mutated"`
317 // Whether undefined behavior sanitizer subset is enabled for this module
318 Undefined *bool `blueprint:"mutated"`
319 // List of specific undefined behavior sanitizers enabled for this module
320 Misc_undefined []string `blueprint:"mutated"`
321 // Whether Fuzzeris enabled for this module
322 Fuzzer *bool `blueprint:"mutated"`
323 // whether safe-stack sanitizer is enabled for this module
324 Safestack *bool `blueprint:"mutated"`
325 // Whether cfi sanitizer is enabled for this module
326 Cfi *bool `blueprint:"mutated"`
327 // Whether signed/unsigned integer overflow sanitizer is enabled for this module
328 Integer_overflow *bool `blueprint:"mutated"`
329 // Whether scudo sanitizer is enabled for this module
330 Scudo *bool `blueprint:"mutated"`
331 // Whether shadow-call-stack sanitizer is enabled for this module.
332 Scs *bool `blueprint:"mutated"`
333 // Whether Memory-tagging is enabled for this module
334 Memtag_heap *bool `blueprint:"mutated"`
335 // Whether Memory-tagging stack instrumentation is enabled for this module
336 Memtag_stack *bool `blueprint:"mutated"`
337
338 // Whether a modifier for ASAN and HWASAN for write only instrumentation is enabled for this
339 // module
340 Writeonly *bool `blueprint:"mutated"`
341
342 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
343 Diag struct {
344 // Whether Undefined behavior sanitizer, diagnostic mode is enabled for this module
345 Undefined *bool `blueprint:"mutated"`
346 // Whether cfi sanitizer, diagnostic mode is enabled for this module
347 Cfi *bool `blueprint:"mutated"`
348 // Whether signed/unsigned integer overflow sanitizer, diagnostic mode is enabled for this
349 // module
350 Integer_overflow *bool `blueprint:"mutated"`
351 // Whether Memory-tagging, diagnostic mode is enabled for this module
352 Memtag_heap *bool `blueprint:"mutated"`
353 // List of specific undefined behavior sanitizers enabled in diagnostic mode
354 Misc_undefined []string `blueprint:"mutated"`
355 } `blueprint:"mutated"`
356}
357
Martin Stjernholmb0249572020-09-15 02:32:35 +0100358type SanitizeProperties struct {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400359 Sanitize SanitizeUserProps `android:"arch_variant"`
360 SanitizeMutated sanitizeMutatedProperties `blueprint:"mutated"`
361
362 SanitizerEnabled bool `blueprint:"mutated"`
363 MinimalRuntimeDep bool `blueprint:"mutated"`
364 BuiltinsDep bool `blueprint:"mutated"`
365 UbsanRuntimeDep bool `blueprint:"mutated"`
366 InSanitizerDir bool `blueprint:"mutated"`
367 Sanitizers []string `blueprint:"mutated"`
368 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800369}
370
371type sanitize struct {
372 Properties SanitizeProperties
373}
374
Cindy Zhou18417cb2020-12-10 07:12:38 -0800375// Mark this tag with a check to see if apex dependency check should be skipped
376func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
377 return t.skipApexAllowedDependenciesCheck
378}
379
380var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
381
Vishwath Mohane7128792017-11-17 11:08:10 -0800382func init() {
383 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700384 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800385}
386
Colin Cross16b23492016-01-06 14:41:07 -0800387func (sanitize *sanitize) props() []interface{} {
388 return []interface{}{&sanitize.Properties}
389}
390
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400391func (p *sanitizeMutatedProperties) copyUserPropertiesToMutated(userProps *SanitizeUserProps) {
392 p.Never = userProps.Never
393 p.Address = userProps.Address
394 p.All_undefined = userProps.All_undefined
395 p.Cfi = userProps.Cfi
396 p.Fuzzer = userProps.Fuzzer
397 p.Hwaddress = userProps.Hwaddress
398 p.Integer_overflow = userProps.Integer_overflow
399 p.Memtag_heap = userProps.Memtag_heap
400 p.Memtag_stack = userProps.Memtag_stack
401 p.Safestack = userProps.Safestack
402 p.Scs = userProps.Scs
403 p.Scudo = userProps.Scudo
404 p.Thread = userProps.Thread
405 p.Undefined = userProps.Undefined
406 p.Writeonly = userProps.Writeonly
407
408 p.Misc_undefined = make([]string, 0, len(userProps.Misc_undefined))
409 for _, v := range userProps.Misc_undefined {
410 p.Misc_undefined = append(p.Misc_undefined, v)
411 }
412
413 p.Diag.Cfi = userProps.Diag.Cfi
414 p.Diag.Integer_overflow = userProps.Diag.Integer_overflow
415 p.Diag.Memtag_heap = userProps.Diag.Memtag_heap
416 p.Diag.Undefined = userProps.Diag.Undefined
417
418 p.Diag.Misc_undefined = make([]string, 0, len(userProps.Diag.Misc_undefined))
419 for _, v := range userProps.Diag.Misc_undefined {
420 p.Diag.Misc_undefined = append(p.Diag.Misc_undefined, v)
421 }
422}
423
Colin Cross16b23492016-01-06 14:41:07 -0800424func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400425 s := &sanitize.Properties.SanitizeMutated
426 s.copyUserPropertiesToMutated(&sanitize.Properties.Sanitize)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700427
Colin Cross16b23492016-01-06 14:41:07 -0800428 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700429 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800430 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800431 }
432
433 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800434 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800435 return
436 }
437
Florian Mayerd8434a42022-08-31 20:57:03 +0000438 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400439 if ctx.testBinary() {
440 if s.Memtag_heap == nil {
441 s.Memtag_heap = proptools.BoolPtr(true)
442 }
443 if s.Diag.Memtag_heap == nil {
444 s.Diag.Memtag_heap = proptools.BoolPtr(true)
445 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800446 }
447
Colin Cross16b23492016-01-06 14:41:07 -0800448 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700449 var globalSanitizersDiag []string
450
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700451 if ctx.Host() {
452 if !ctx.Windows() {
453 globalSanitizers = ctx.Config().SanitizeHost()
454 }
455 } else {
456 arches := ctx.Config().SanitizeDeviceArch()
457 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
458 globalSanitizers = ctx.Config().SanitizeDevice()
459 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800460 }
461 }
462
Colin Cross16b23492016-01-06 14:41:07 -0800463 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000464 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700465 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400466 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000467 }
Colin Cross16b23492016-01-06 14:41:07 -0800468
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700469 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400470 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000471 }
472
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700473 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400474 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000475 }
476
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700477 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400478 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000479 }
480
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700481 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400482 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700483 }
484
485 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400486 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000487 }
488
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700489 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800490 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400491 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700492 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700493 }
494
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700495 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700496 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700497 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400498 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700499 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700500 }
501
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700502 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400503 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700504 }
505
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700506 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400507 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700508 }
509
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000510 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
511 // Hwaddress and Address are set before, so we can check them here
512 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
513 if s.Address == nil && s.Hwaddress == nil {
514 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
515 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400516 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000517 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700518 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800519 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400520 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800521 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700522 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000523
Florian Mayerd8434a42022-08-31 20:57:03 +0000524 if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
525 s.Memtag_stack = proptools.BoolPtr(true)
526 }
527
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000528 if len(globalSanitizers) > 0 {
529 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
530 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700531
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700532 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700533 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700534 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400535 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700536 }
537
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700538 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
539 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400540 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700541 }
542
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800543 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
544 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400545 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800546 }
547
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700548 if len(globalSanitizersDiag) > 0 {
549 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
550 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700551 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700552
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800553 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross88a029f2022-06-23 14:51:20 -0700554 if ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800555 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800556 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400557 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800558 }
559 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400560 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800561 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800562 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800563 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400564 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800565 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800566 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700567 }
568
Elvis Chien9c993542021-06-25 01:15:17 +0800569 // Enable CFI for non-host components in the include paths
570 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400571 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000572 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400573 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700574 }
575 }
576
Elliott Hughesda3a0712020-03-06 16:55:28 -0800577 // Is CFI actually enabled?
578 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400579 s.Cfi = nil
580 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800581 }
582
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700583 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross88a029f2022-06-23 14:51:20 -0700584 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700585 s.Hwaddress = nil
586 }
587
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800588 // SCS is only implemented on AArch64.
Colin Cross88a029f2022-06-23 14:51:20 -0700589 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800590 s.Scs = nil
591 }
592
Ivan Lozano62cd0382021-11-01 10:27:54 -0400593 // Memtag_heap is only implemented on AArch64.
Florian Mayerd8434a42022-08-31 20:57:03 +0000594 // Memtag ABI is Android specific for now, so disable for host.
595 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() || ctx.Host() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700596 s.Memtag_heap = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000597 s.Memtag_stack = nil
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700598 }
599
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700600 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700601 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400602 s.Cfi = nil
603 s.Diag.Cfi = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000604 // HWASAN and ASAN win against MTE.
605 s.Memtag_heap = nil
606 s.Memtag_stack = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700607 }
608
Colin Crossed12a042022-02-07 13:55:55 -0800609 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
610 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400611 s.Cfi = nil
612 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700613 s.Misc_undefined = nil
614 s.Undefined = nil
615 s.All_undefined = nil
616 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800617 }
618
Colin Cross2c435a02022-10-20 13:55:53 -0700619 // TODO(b/254713216): CFI doesn't work for riscv64 yet because LTO doesn't work.
Colin Crossecf4e662022-10-20 13:59:17 -0700620 if ctx.Arch().ArchType == android.Riscv64 {
621 s.Cfi = nil
622 s.Diag.Cfi = nil
623 }
624
Colin Crossed12a042022-02-07 13:55:55 -0800625 // Disable CFI for musl
626 if ctx.toolchain().Musl() {
627 s.Cfi = nil
628 s.Diag.Cfi = nil
629 }
630
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700631 // Also disable CFI for VNDK variants of components
632 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900633 if ctx.static() {
634 // Cfi variant for static vndk should be captured as vendor snapshot,
635 // so don't strictly disable Cfi.
636 s.Cfi = nil
637 s.Diag.Cfi = nil
638 } else {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400639 s.Cfi = nil
640 s.Diag.Cfi = nil
Inseob Kimc42f2f22020-07-29 20:32:10 +0900641 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900642 }
643
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700644 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700645 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
646 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700647 s.Hwaddress = nil
648 }
649
Colin Cross3c344ef2016-07-18 15:44:56 -0700650 if ctx.staticBinary() {
651 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700652 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700653 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800654 }
655
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700656 if Bool(s.All_undefined) {
657 s.Undefined = nil
658 }
659
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700660 if !ctx.toolchain().Is64Bit() {
661 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700662 s.Thread = nil
663 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800664 // TODO(ccross): error for compile_multilib = "32"?
665 }
666
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800667 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 -0700668 Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Florian Mayerd8434a42022-08-31 20:57:03 +0000669 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700670 sanitize.Properties.SanitizerEnabled = true
671 }
672
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800673 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
674 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700675 s.Scudo = nil
676 }
677
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700678 if Bool(s.Hwaddress) {
679 s.Address = nil
680 s.Thread = nil
681 }
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000682
683 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
684 // mutually incompatible.
685 if Bool(s.Fuzzer) {
686 s.Cfi = nil
687 }
Colin Cross16b23492016-01-06 14:41:07 -0800688}
689
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800690func toDisableImplicitIntegerChange(flags []string) bool {
691 // Returns true if any flag is fsanitize*integer, and there is
692 // no explicit flag about sanitize=implicit-integer-sign-change.
693 for _, f := range flags {
694 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
695 return false
696 }
697 }
698 for _, f := range flags {
699 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
700 return true
701 }
702 }
703 return false
704}
705
Yabin Cuidb7dda82020-11-30 15:47:45 -0800706func toDisableUnsignedShiftBaseChange(flags []string) bool {
707 // Returns true if any flag is fsanitize*integer, and there is
708 // no explicit flag about sanitize=unsigned-shift-base.
709 for _, f := range flags {
710 if strings.Contains(f, "sanitize=unsigned-shift-base") {
711 return false
712 }
713 }
714 for _, f := range flags {
715 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
716 return true
717 }
718 }
719 return false
720}
721
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400722func (s *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
723 if !s.Properties.SanitizerEnabled && !s.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800724 return flags
725 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400726 sanProps := &s.Properties.SanitizeMutated
Colin Cross16b23492016-01-06 14:41:07 -0800727
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400728 if Bool(sanProps.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700729 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800730 // Frame pointer based unwinder in ASan requires ARM frame setup.
731 // TODO: put in flags?
732 flags.RequiredInstructionSet = "arm"
733 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800734 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
735 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800736
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400737 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000738 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
739 }
740
Colin Cross16b23492016-01-06 14:41:07 -0800741 if ctx.Host() {
742 // -nodefaultlibs (provided with libc++) prevents the driver from linking
743 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800744 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800745 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800746 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900747 if ctx.bootstrap() {
748 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
749 } else {
750 flags.DynamicLinker = "/system/bin/linker_asan"
751 }
Colin Cross16b23492016-01-06 14:41:07 -0800752 if flags.Toolchain.Is64Bit() {
753 flags.DynamicLinker += "64"
754 }
755 }
Colin Cross16b23492016-01-06 14:41:07 -0800756 }
757
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400758 if Bool(sanProps.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800759 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800760
761 for _, flag := range hwasanCommonflags {
762 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
763 }
764 for _, flag := range hwasanCommonflags {
765 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
766 }
767
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400768 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000769 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
770 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700771 }
772
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400773 if Bool(sanProps.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800774 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700775
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000776 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
777 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
778 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
779 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
780 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
781
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700782 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
783 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
784 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800785 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
786 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700787
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700788 // Disable fortify for fuzzing builds. Generally, we'll be building with
789 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800790 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800791
792 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
793 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
794 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
795 // the DT_RUNPATH from the shared library above it, and not the executable,
796 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
797 // DT_RUNPATH here means that transient shared libraries can be found
798 // colocated with their parents.
799 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800800 }
801
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400802 if Bool(sanProps.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800803 if ctx.Arch().ArchType == android.Arm {
804 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
805 // to do this on a function basis, so force Thumb on the entire module.
806 flags.RequiredInstructionSet = "thumb"
807 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000808
Colin Cross4af21ed2019-11-04 09:37:55 -0800809 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
810 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400811 if Bool(s.Properties.Sanitize.Config.Cfi_assembly_support) {
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800812 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
813 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000814 // Only append the default visibility flag if -fvisibility has not already been set
815 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800816 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
817 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000818 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800819 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000820
821 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800822 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
823 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000824 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700825 }
826
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400827 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000828 flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
Florian Mayer1e4f8992022-09-08 16:47:32 -0700829 // TODO(fmayer): remove -Wno-error once https://reviews.llvm.org/D127917 is in Android toolchain.
830 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-error=frame-larger-than")
Florian Mayerd8434a42022-08-31 20:57:03 +0000831 flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
832 flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
Florian Mayerf79b2102022-09-09 14:23:31 -0700833 // This works around LLD complaining about the stack frame size.
834 // TODO(fmayer): remove once https://reviews.llvm.org/D127917 is in Android toolchain.
835 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-fatal-warnings")
Florian Mayerd8434a42022-08-31 20:57:03 +0000836 }
837
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400838 if (Bool(sanProps.Memtag_heap) || Bool(sanProps.Memtag_stack)) && ctx.binary() {
839 if Bool(sanProps.Diag.Memtag_heap) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000840 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
841 } else {
842 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
843 }
844 }
845
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400846 if Bool(sanProps.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800847 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700848 }
849
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400850 if len(s.Properties.Sanitizers) > 0 {
851 sanitizeArg := "-fsanitize=" + strings.Join(s.Properties.Sanitizers, ",")
Colin Cross4af21ed2019-11-04 09:37:55 -0800852 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
853 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross234b01d2022-02-07 13:49:03 -0800854 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
855
Colin Crossed12a042022-02-07 13:55:55 -0800856 if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
857 // Bionic and musl sanitizer runtimes have already been added as dependencies so that
858 // the right variant of the runtime will be used (with the "-android" or "-musl"
859 // suffixes), so don't let clang the runtime library.
Colin Cross234b01d2022-02-07 13:49:03 -0800860 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-link-runtime")
861 } else {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800862 // Host sanitizers only link symbols in the final executable, so
863 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800864 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
Colin Cross6c18d002022-06-02 15:11:50 -0700865 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500866
Colin Cross6c18d002022-06-02 15:11:50 -0700867 if !ctx.toolchain().Bionic() {
868 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function san.
869 // Musl toolchain prebuilts have vptr and function sanitizers, but enabling them
870 // implicitly enables RTTI which causes RTTI mismatch issues with dependencies.
871
Colin Cross234b01d2022-02-07 13:49:03 -0800872 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500873 }
874
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400875 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700876 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800877 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700878 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800879 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700880 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800881 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700882 }
Evgenii Stepanov59012812022-06-24 11:09:18 -0700883
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400884 if enableMinimalRuntime(s) {
Evgenii Stepanov59012812022-06-24 11:09:18 -0700885 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
886 }
887
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800888 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800889 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
890 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800891 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800892 // http://b/171275751, Android doesn't build with this sanitizer yet.
893 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
894 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
895 }
Colin Cross16b23492016-01-06 14:41:07 -0800896 }
897
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400898 if len(s.Properties.DiagSanitizers) > 0 {
899 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(s.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700900 }
901 // FIXME: enable RTTI if diag + (cfi or vptr)
902
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400903 if s.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800904 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400905 strings.Join(s.Properties.Sanitize.Recover, ","))
Andreas Gampe97071162017-05-08 13:15:23 -0700906 }
907
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400908 if s.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800909 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400910 strings.Join(s.Properties.Sanitize.Diag.No_recover, ","))
Ivan Lozano7929bba2018-12-12 09:36:31 -0800911 }
912
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400913 blocklist := android.OptionalPathForModuleSrc(ctx, s.Properties.Sanitize.Blocklist)
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700914 if blocklist.Valid() {
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700915 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-ignorelist="+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700916 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
917 }
918
Colin Cross16b23492016-01-06 14:41:07 -0800919 return flags
920}
921
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400922func (s *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900923 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
924 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800925 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400926 if Bool(s.Properties.SanitizeMutated.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800927 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900928 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400929 if Bool(s.Properties.SanitizeMutated.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800930 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900931 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400932 if Bool(s.Properties.SanitizeMutated.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800933 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900934 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800935 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700936}
937
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400938func (s *sanitize) inSanitizerDir() bool {
939 return s.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700940}
941
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500942// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400943func (s *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000944 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500945 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400946 return s.Properties.SanitizeMutated.Address
Tri Vo6eafc362021-04-01 11:29:09 -0700947 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400948 return s.Properties.SanitizeMutated.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000949 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400950 return s.Properties.SanitizeMutated.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000951 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400952 return s.Properties.SanitizeMutated.Integer_overflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000953 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400954 return s.Properties.SanitizeMutated.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800955 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400956 return s.Properties.SanitizeMutated.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -0400957 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400958 return s.Properties.SanitizeMutated.Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +0000959 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400960 return s.Properties.SanitizeMutated.Memtag_stack
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500961 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400962 return s.Properties.SanitizeMutated.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000963 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500964 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000965 }
966}
967
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500968// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800969func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500970 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700971 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800972 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800973 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700974 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -0400975 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Florian Mayerd8434a42022-08-31 20:57:03 +0000976 !sanitize.isSanitizerEnabled(Memtag_stack) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500977 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800978}
979
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500980// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700981func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500982 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700983 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700984 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500985 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700986}
987
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500988func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400989 bPtr := proptools.BoolPtr(b)
990 if !b {
991 bPtr = nil
992 }
Colin Cross16b23492016-01-06 14:41:07 -0800993 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500994 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400995 sanitize.Properties.SanitizeMutated.Address = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -0700996 // For ASAN variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400997 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Tri Vo6eafc362021-04-01 11:29:09 -0700998 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400999 sanitize.Properties.SanitizeMutated.Hwaddress = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001000 // For HWAsan variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001001 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Colin Cross16b23492016-01-06 14:41:07 -08001002 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001003 sanitize.Properties.SanitizeMutated.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -07001004 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001005 sanitize.Properties.SanitizeMutated.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001006 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001007 sanitize.Properties.SanitizeMutated.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001008 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001009 sanitize.Properties.SanitizeMutated.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -04001010 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001011 sanitize.Properties.SanitizeMutated.Memtag_heap = bPtr
Florian Mayerd8434a42022-08-31 20:57:03 +00001012 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001013 sanitize.Properties.SanitizeMutated.Memtag_stack = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001014 // We do not need to disable ASAN or HWASan here, as there is no Memtag_stack variant.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001015 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001016 sanitize.Properties.SanitizeMutated.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -08001017 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001018 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -08001019 }
1020 if b {
1021 sanitize.Properties.SanitizerEnabled = true
1022 }
1023}
1024
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001025// Check if the sanitizer is explicitly disabled (as opposed to nil by
1026// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001027func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001028 if sanitize == nil {
1029 return false
1030 }
1031
1032 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1033 return sanitizerVal != nil && *sanitizerVal == false
1034}
1035
1036// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
1037// because enabling a sanitizer either directly (via the blueprint) or
1038// indirectly (via a mutator) sets the bool ptr to true, and you can't
1039// distinguish between the cases. It isn't needed though - both cases can be
1040// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001041func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001042 if sanitize == nil {
1043 return false
1044 }
1045
1046 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1047 return sanitizerVal != nil && *sanitizerVal == true
1048}
1049
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001050// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
1051func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -07001052 switch t := tag.(type) {
1053 case dependencyTag:
1054 return t == reuseObjTag || t == objDepTag
1055 case libraryDependencyTag:
1056 return true
1057 default:
1058 return false
1059 }
Colin Cross6b753602018-06-21 13:03:07 -07001060}
1061
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001062func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
1063 return IsSanitizableDependencyTag
1064}
1065
Inseob Kimc42f2f22020-07-29 20:32:10 +09001066// Determines if the current module is a static library going to be captured
1067// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
1068// except for ones which explicitly disable cfi.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001069func needsCfiForVendorSnapshot(mctx android.BaseModuleContext) bool {
Justin Yun8814fc52022-12-15 21:45:35 +09001070 if inList("hwaddress", mctx.Config().SanitizeDevice()) {
1071 // cfi will not be built if SANITIZE_TARGET=hwaddress is set
1072 return false
1073 }
1074
Kiyoung Kim48f37782021-07-07 12:42:39 +09001075 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001076 return false
1077 }
1078
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001079 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001080
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001081 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001082 return false
1083 }
1084
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001085 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001086 return false
1087 }
1088
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001089 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001090 return false
1091 }
1092
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001093 if !c.SanitizerSupported(cfi) {
1094 return false
1095 }
1096
1097 return c.SanitizePropDefined() &&
1098 !c.SanitizeNever() &&
1099 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001100}
1101
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001102type sanitizerSplitMutator struct {
1103 sanitizer SanitizerType
1104}
1105
1106// If an APEX is sanitized or not depends on whether it contains at least one
1107// sanitized module. Transition mutators cannot propagate information up the
1108// dependency graph this way, so we need an auxiliary mutator to do so.
1109func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
1110 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1111 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1112 ctx.VisitDirectDeps(func(dep android.Module) {
1113 if c, ok := dep.(*Module); ok && c.sanitize.isSanitizerEnabled(s.sanitizer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001114 enabled = true
Inseob Kimc42f2f22020-07-29 20:32:10 +09001115 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001116 })
1117
1118 if enabled {
1119 sanitizeable.EnableSanitizer(s.sanitizer.name())
1120 }
1121 }
1122}
1123
1124func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
1125 if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1126 if s.sanitizer == cfi && needsCfiForVendorSnapshot(ctx) {
1127 return []string{"", s.sanitizer.variationName()}
1128 }
1129
1130 // If the given sanitizer is not requested in the .bp file for a module, it
1131 // won't automatically build the sanitized variation.
1132 if !c.IsSanitizerEnabled(s.sanitizer) {
1133 return []string{""}
1134 }
1135
1136 if c.Binary() {
1137 // If a sanitizer is enabled for a binary, we do not build the version
1138 // without the sanitizer
1139 return []string{s.sanitizer.variationName()}
1140 } else if c.StaticallyLinked() || c.Header() {
1141 // For static libraries, we build both versions. Some Make modules
1142 // apparently depend on this behavior.
1143 return []string{"", s.sanitizer.variationName()}
1144 } else {
1145 // We only build the requested variation of dynamic libraries
1146 return []string{s.sanitizer.variationName()}
1147 }
1148 }
1149
1150 if _, ok := ctx.Module().(JniSanitizeable); ok {
1151 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1152 // that is short-circuited for now
1153 return []string{""}
1154 }
1155
1156 // If an APEX has a sanitized dependency, we build the APEX in the sanitized
1157 // variation. This is useful because such APEXes require extra dependencies.
1158 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1159 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1160 if enabled {
1161 return []string{s.sanitizer.variationName()}
1162 } else {
1163 return []string{""}
1164 }
1165 }
1166
1167 if c, ok := ctx.Module().(*Module); ok {
1168 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1169
1170 // Check if it's a snapshot module supporting sanitizer
Justin Yun39c30312022-11-23 16:20:12 +09001171 if ss, ok := c.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001172 return []string{"", s.sanitizer.variationName()}
1173 } else {
1174 return []string{""}
1175 }
1176 }
1177
1178 return []string{""}
1179}
1180
1181func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1182 if c, ok := ctx.Module().(PlatformSanitizeable); ok {
1183 if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
1184 // If the dependency is through a non-sanitizable tag, use the
1185 // non-sanitized variation
1186 return ""
1187 }
1188
1189 return sourceVariation
1190 } else if _, ok := ctx.Module().(JniSanitizeable); ok {
1191 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1192 // that is short-circuited for now
1193 return ""
1194 } else {
1195 // Otherwise, do not rock the boat.
1196 return sourceVariation
1197 }
1198}
1199
1200func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1201 if d, ok := ctx.Module().(PlatformSanitizeable); ok {
1202 if dm, ok := ctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001203 if ss, ok := dm.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001204 return incomingVariation
Inseob Kimc42f2f22020-07-29 20:32:10 +09001205 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001206 }
1207
1208 if !d.SanitizePropDefined() ||
1209 d.SanitizeNever() ||
1210 d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
1211 !d.SanitizerSupported(s.sanitizer) {
1212 // If a module opts out of a sanitizer, use its non-sanitized variation
1213 return ""
1214 }
1215
1216 // Binaries are always built in the variation they requested.
1217 if d.Binary() {
1218 if d.IsSanitizerEnabled(s.sanitizer) {
1219 return s.sanitizer.variationName()
1220 } else {
1221 return ""
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001222 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001223 }
1224
1225 // If a shared library requests to be sanitized, it will be built for that
1226 // sanitizer. Otherwise, some sanitizers propagate through shared library
1227 // dependency edges, some do not.
1228 if !d.StaticallyLinked() && !d.Header() {
1229 if d.IsSanitizerEnabled(s.sanitizer) {
1230 return s.sanitizer.variationName()
1231 }
1232
Liz Kammerfd8a49f2022-10-31 10:31:11 -04001233 // Some sanitizers do not propagate to shared dependencies
1234 if !s.sanitizer.shouldPropagateToSharedLibraryDeps() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001235 return ""
1236 }
1237 }
1238
1239 // Static and header libraries inherit whether they are sanitized from the
1240 // module they are linked into
1241 return incomingVariation
1242 } else if d, ok := ctx.Module().(Sanitizeable); ok {
1243 // If an APEX contains a sanitized module, it will be built in the variation
1244 // corresponding to that sanitizer.
1245 enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1246 if enabled {
1247 return s.sanitizer.variationName()
1248 }
1249
1250 return incomingVariation
1251 }
1252
1253 return ""
1254}
1255
1256func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
1257 sanitizerVariation := variationName == s.sanitizer.variationName()
1258
1259 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1260 sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
1261
1262 oneMakeVariation := false
1263 if c.StaticallyLinked() || c.Header() {
1264 if s.sanitizer != cfi && s.sanitizer != scs && s.sanitizer != Hwasan {
1265 // These sanitizers export only one variation to Make. For the rest,
1266 // Make targets can depend on both the sanitized and non-sanitized
1267 // versions.
1268 oneMakeVariation = true
1269 }
1270 } else if !c.Binary() {
1271 // Shared library. These are the sanitizers that do propagate through shared
1272 // library dependencies and therefore can cause multiple variations of a
1273 // shared library to be built.
1274 if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != scs && s.sanitizer != Asan {
1275 oneMakeVariation = true
1276 }
1277 }
1278
1279 if oneMakeVariation {
1280 if sanitizerEnabled != sanitizerVariation {
1281 c.SetPreventInstall()
1282 c.SetHideFromMake()
1283 }
1284 }
1285
1286 if sanitizerVariation {
1287 c.SetSanitizer(s.sanitizer, true)
1288
1289 // CFI is incompatible with ASAN so disable it in ASAN variations
1290 if s.sanitizer.incompatibleWithCfi() {
1291 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1292 if mctx.Device() && cfiSupported {
1293 c.SetSanitizer(cfi, false)
Jiyong Parkf97782b2019-02-13 20:28:58 +09001294 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001295 }
1296
1297 // locate the asan libraries under /data/asan
1298 if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
1299 c.SetInSanitizerDir()
1300 }
1301
1302 if c.StaticallyLinked() && c.ExportedToMake() {
1303 if s.sanitizer == Hwasan {
1304 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
1305 } else if s.sanitizer == cfi {
1306 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
1307 }
1308 }
1309 } else if c.IsSanitizerEnabled(s.sanitizer) {
1310 // Disable the sanitizer for the non-sanitized variation
1311 c.SetSanitizer(s.sanitizer, false)
1312 }
1313 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
1314 // If an APEX has sanitized dependencies, it gets a few more dependencies
1315 if sanitizerVariation {
1316 sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
1317 }
1318 } else if c, ok := mctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001319 if ss, ok := c.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
1320 if !ss.isUnsanitizedVariant() {
1321 // Snapshot sanitizer may have only one variantion.
1322 // Skip exporting the module if it already has a sanitizer variation.
1323 c.SetPreventInstall()
1324 c.SetHideFromMake()
1325 return
1326 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001327 c.linker.(snapshotSanitizer).setSanitizerVariation(s.sanitizer, sanitizerVariation)
1328
1329 // Export the static lib name to make
1330 if c.static() && c.ExportedToMake() {
Justin Yun39c30312022-11-23 16:20:12 +09001331 // use BaseModuleName which is the name for Make.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001332 if s.sanitizer == cfi {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001333 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Justin Yun39c30312022-11-23 16:20:12 +09001334 } else if s.sanitizer == Hwasan {
1335 hwasanStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001336 }
1337 }
Colin Cross16b23492016-01-06 14:41:07 -08001338 }
1339 }
1340}
1341
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001342func (c *Module) SanitizeNever() bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001343 return Bool(c.sanitize.Properties.SanitizeMutated.Never)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001344}
1345
1346func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
1347 return c.sanitize.isSanitizerExplicitlyDisabled(t)
1348}
1349
Ivan Lozano30c5db22018-02-21 15:49:20 -08001350// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001351func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001352 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001353 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001354 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001355 mctx.WalkDeps(func(child, parent android.Module) bool {
1356 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1357 return false
1358 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001359
Inseob Kimeec88e12020-01-22 11:11:29 +09001360 d, ok := child.(*Module)
1361 if !ok || !d.static() {
1362 return false
1363 }
1364 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001365 if enableMinimalRuntime(d.sanitize) {
1366 // If a static dependency is built with the minimal runtime,
1367 // make sure we include the ubsan minimal runtime.
1368 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001369 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001370 // If a static dependency runs with full ubsan diagnostics,
1371 // make sure we include the ubsan runtime.
1372 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001373 }
Colin Cross0b908332019-06-19 23:00:20 -07001374
1375 if c.sanitize.Properties.MinimalRuntimeDep &&
1376 c.sanitize.Properties.UbsanRuntimeDep {
1377 // both flags that this mutator might set are true, so don't bother recursing
1378 return false
1379 }
1380
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001381 if c.Os() == android.Linux {
1382 c.sanitize.Properties.BuiltinsDep = true
1383 }
1384
Colin Cross0b908332019-06-19 23:00:20 -07001385 return true
Colin Cross6b753602018-06-21 13:03:07 -07001386 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001387
Jose Galmesf7294582020-11-13 12:07:36 -08001388 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001389 if Bool(p.properties.Sanitize_minimal_dep) {
1390 c.sanitize.Properties.MinimalRuntimeDep = true
1391 }
1392 if Bool(p.properties.Sanitize_ubsan_dep) {
1393 c.sanitize.Properties.UbsanRuntimeDep = true
1394 }
1395 }
1396
1397 return false
Colin Cross6b753602018-06-21 13:03:07 -07001398 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001399 }
1400}
1401
Jiyong Park379de2f2018-12-19 02:47:14 +09001402// Add the dependency to the runtime library for each of the sanitizer variants
1403func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001404 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001405 if !c.Enabled() {
1406 return
1407 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001408 var sanitizers []string
1409 var diagSanitizers []string
1410
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001411 sanProps := &c.sanitize.Properties.SanitizeMutated
1412
1413 if Bool(sanProps.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001414 sanitizers = append(sanitizers, "undefined")
1415 } else {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001416 if Bool(sanProps.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001417 sanitizers = append(sanitizers,
1418 "bool",
1419 "integer-divide-by-zero",
1420 "return",
1421 "returns-nonnull-attribute",
1422 "shift-exponent",
1423 "unreachable",
1424 "vla-bound",
1425 // TODO(danalbert): The following checks currently have compiler performance issues.
1426 //"alignment",
1427 //"bounds",
1428 //"enum",
1429 //"float-cast-overflow",
1430 //"float-divide-by-zero",
1431 //"nonnull-attribute",
1432 //"null",
1433 //"shift-base",
1434 //"signed-integer-overflow",
1435 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1436 // https://llvm.org/PR19302
1437 // http://reviews.llvm.org/D6974
1438 // "object-size",
1439 )
1440 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001441 sanitizers = append(sanitizers, sanProps.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001442 }
1443
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001444 if Bool(sanProps.Diag.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001445 diagSanitizers = append(diagSanitizers, "undefined")
1446 }
1447
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001448 diagSanitizers = append(diagSanitizers, sanProps.Diag.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001449
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001450 if Bool(sanProps.Address) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001451 sanitizers = append(sanitizers, "address")
1452 diagSanitizers = append(diagSanitizers, "address")
1453 }
1454
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001455 if Bool(sanProps.Hwaddress) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001456 sanitizers = append(sanitizers, "hwaddress")
1457 }
1458
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001459 if Bool(sanProps.Thread) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001460 sanitizers = append(sanitizers, "thread")
1461 }
1462
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001463 if Bool(sanProps.Safestack) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001464 sanitizers = append(sanitizers, "safe-stack")
1465 }
1466
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001467 if Bool(sanProps.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001468 sanitizers = append(sanitizers, "cfi")
1469
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001470 if Bool(sanProps.Diag.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001471 diagSanitizers = append(diagSanitizers, "cfi")
1472 }
1473 }
1474
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001475 if Bool(sanProps.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001476 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1477 sanitizers = append(sanitizers, "signed-integer-overflow")
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001478 if Bool(sanProps.Diag.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001479 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1480 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1481 }
1482 }
1483
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001484 if Bool(sanProps.Scudo) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001485 sanitizers = append(sanitizers, "scudo")
1486 }
1487
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001488 if Bool(sanProps.Scs) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001489 sanitizers = append(sanitizers, "shadow-call-stack")
1490 }
1491
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001492 if Bool(sanProps.Memtag_heap) && c.Binary() {
Florian Mayerd8434a42022-08-31 20:57:03 +00001493 sanitizers = append(sanitizers, "memtag-heap")
1494 }
1495
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001496 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +00001497 sanitizers = append(sanitizers, "memtag-stack")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001498 }
1499
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001500 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001501 sanitizers = append(sanitizers, "fuzzer-no-link")
1502 }
1503
Jiyong Park379de2f2018-12-19 02:47:14 +09001504 // Save the list of sanitizers. These will be used again when generating
1505 // the build rules (for Cflags, etc.)
1506 c.sanitize.Properties.Sanitizers = sanitizers
1507 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1508
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001509 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1510 if c.Host() {
1511 diagSanitizers = sanitizers
1512 }
1513
Jiyong Park379de2f2018-12-19 02:47:14 +09001514 // Determine the runtime library required
1515 runtimeLibrary := ""
Colin Crosse725b4e2022-10-19 15:46:53 -07001516 alwaysStaticRuntime := false
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001517 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001518 toolchain := c.toolchain(mctx)
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001519 if Bool(sanProps.Address) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001520 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001521 } else if Bool(sanProps.Hwaddress) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001522 if c.staticBinary() {
1523 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001524 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001525 } else {
1526 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1527 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001528 } else if Bool(sanProps.Thread) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001529 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001530 } else if Bool(sanProps.Scudo) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001531 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1532 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1533 } else {
1534 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1535 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001536 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001537 Bool(sanProps.Fuzzer) ||
1538 Bool(sanProps.Undefined) ||
1539 Bool(sanProps.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001540 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
Colin Crosse725b4e2022-10-19 15:46:53 -07001541 if c.staticBinary() || toolchain.Musl() {
1542 // Use a static runtime for static binaries.
1543 // Also use a static runtime for musl to match
1544 // what clang does for glibc. Otherwise dlopening
1545 // libraries that depend on libclang_rt.ubsan_standalone.so
1546 // fails with:
1547 // Error relocating ...: initial-exec TLS resolves to dynamic definition
Colin Cross32f1de32021-03-29 13:41:37 -07001548 runtimeLibrary += ".static"
Colin Crosse725b4e2022-10-19 15:46:53 -07001549 alwaysStaticRuntime = true
Colin Cross32f1de32021-03-29 13:41:37 -07001550 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001551 }
1552
Colin Cross06c80eb2022-02-10 10:34:19 -08001553 addStaticDeps := func(deps ...string) {
1554 // If we're using snapshots, redirect to snapshot whenever possible
1555 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1556 for idx, dep := range deps {
1557 if lib, ok := snapshot.StaticLibs[dep]; ok {
1558 deps[idx] = lib
1559 }
1560 }
1561
1562 // static executable gets static runtime libs
Colin Cross3e5e7782022-06-17 22:17:05 +00001563 depTag := libraryDependencyTag{Kind: staticLibraryDependency, unexportedSymbols: true}
Colin Cross06c80eb2022-02-10 10:34:19 -08001564 variations := append(mctx.Target().Variations(),
1565 blueprint.Variation{Mutator: "link", Variation: "static"})
1566 if c.Device() {
1567 variations = append(variations, c.ImageVariation())
1568 }
1569 if c.UseSdk() {
1570 variations = append(variations,
1571 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1572 }
1573 mctx.AddFarVariationDependencies(variations, depTag, deps...)
1574
1575 }
1576 if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
1577 addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain))
1578 }
1579 if c.sanitize.Properties.BuiltinsDep {
1580 addStaticDeps(config.BuiltinsRuntimeLibrary(toolchain))
1581 }
1582
Colin Crossed12a042022-02-07 13:55:55 -08001583 if runtimeLibrary != "" && (toolchain.Bionic() || toolchain.Musl() || c.sanitize.Properties.UbsanRuntimeDep) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001584 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001585
1586 // Adding dependency to the runtime library. We are using *FarVariation*
1587 // because the runtime libraries themselves are not mutated by sanitizer
1588 // mutators and thus don't have sanitizer variants whereas this module
1589 // has been already mutated.
1590 //
1591 // Note that by adding dependency with {static|shared}DepTag, the lib is
1592 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
Colin Crosse725b4e2022-10-19 15:46:53 -07001593 if c.staticBinary() || alwaysStaticRuntime {
Colin Cross06c80eb2022-02-10 10:34:19 -08001594 addStaticDeps(runtimeLibrary)
1595 addStaticDeps(extraStaticDeps...)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001596 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001597 // If we're using snapshots, redirect to snapshot whenever possible
1598 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1599 if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
1600 runtimeLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001601 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001602
Cindy Zhou18417cb2020-12-10 07:12:38 -08001603 // Skip apex dependency check for sharedLibraryDependency
1604 // when sanitizer diags are enabled. Skipping the check will allow
1605 // building with diag libraries without having to list the
1606 // dependency in Apex's allowed_deps file.
1607 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001608 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001609 depTag := libraryDependencyTag{
1610 Kind: sharedLibraryDependency,
1611 Order: earlyLibraryDependency,
1612
1613 skipApexAllowedDependenciesCheck: diagEnabled,
1614 }
Colin Cross42507332020-08-21 16:15:23 -07001615 variations := append(mctx.Target().Variations(),
1616 blueprint.Variation{Mutator: "link", Variation: "shared"})
1617 if c.Device() {
1618 variations = append(variations, c.ImageVariation())
1619 }
Colin Cross06c80eb2022-02-10 10:34:19 -08001620 if c.UseSdk() {
1621 variations = append(variations,
1622 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1623 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001624 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001625 }
1626 // static lib does not have dependency to the runtime library. The
1627 // dependency will be added to the executables or shared libs using
1628 // the static lib.
1629 }
1630 }
1631}
1632
1633type Sanitizeable interface {
1634 android.Module
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001635 IsSanitizerEnabled(config android.Config, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001636 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001637 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001638}
1639
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001640type JniSanitizeable interface {
1641 android.Module
1642 IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
1643}
1644
Ivan Lozanod7586b62021-04-01 09:49:36 -04001645func (c *Module) MinimalRuntimeDep() bool {
1646 return c.sanitize.Properties.MinimalRuntimeDep
1647}
1648
1649func (c *Module) UbsanRuntimeDep() bool {
1650 return c.sanitize.Properties.UbsanRuntimeDep
1651}
1652
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001653func (c *Module) SanitizePropDefined() bool {
1654 return c.sanitize != nil
1655}
1656
1657func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1658 return c.sanitize.isSanitizerEnabled(t)
1659}
1660
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001661func (c *Module) StaticallyLinked() bool {
1662 return c.static()
1663}
1664
1665func (c *Module) SetInSanitizerDir() {
1666 if c.sanitize != nil {
1667 c.sanitize.Properties.InSanitizerDir = true
1668 }
1669}
1670
1671func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1672 if c.sanitize != nil {
1673 c.sanitize.SetSanitizer(t, b)
1674 }
1675}
1676
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001677var _ PlatformSanitizeable = (*Module)(nil)
1678
Inseob Kim74d25562020-08-04 00:41:38 +09001679type sanitizerStaticLibsMap struct {
1680 // libsMap contains one list of modules per each image and each arch.
1681 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001682 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001683 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001684 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001685}
1686
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001687func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001688 return &sanitizerStaticLibsMap{
1689 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001690 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001691 }
1692}
1693
1694// Add the current module to sanitizer static libs maps
1695// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001696func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1697 image := GetImageVariantType(c)
1698 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001699
1700 s.libsMapLock.Lock()
1701 defer s.libsMapLock.Unlock()
1702
1703 if _, ok := s.libsMap[image]; !ok {
1704 s.libsMap[image] = make(map[string][]string)
1705 }
1706
1707 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1708}
1709
1710// Exports makefile variables in the following format:
1711// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1712// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1713// These are to be used by use_soong_sanitized_static_libraries.
1714// See build/make/core/binary.mk for more details.
1715func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1716 for _, image := range android.SortedStringKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001717 archMap := s.libsMap[ImageVariantType(image)]
Inseob Kim74d25562020-08-04 00:41:38 +09001718 for _, arch := range android.SortedStringKeys(archMap) {
1719 libs := archMap[arch]
1720 sort.Strings(libs)
1721
1722 key := fmt.Sprintf(
1723 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1724 s.sanitizerType.variationName(),
1725 image, // already upper
1726 arch)
1727
1728 ctx.Strict(key, strings.Join(libs, " "))
1729 }
1730 }
1731}
1732
Colin Cross571cccf2019-02-04 11:22:08 -08001733var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1734
Inseob Kim74d25562020-08-04 00:41:38 +09001735func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001736 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001737 return newSanitizerStaticLibsMap(cfi)
1738 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001739}
1740
Colin Cross571cccf2019-02-04 11:22:08 -08001741var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1742
Inseob Kim74d25562020-08-04 00:41:38 +09001743func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001744 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001745 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001746 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001747}
1748
Ivan Lozano30c5db22018-02-21 15:49:20 -08001749func enableMinimalRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001750 if sanitize.isSanitizerEnabled(Asan) {
1751 return false
1752 } else if sanitize.isSanitizerEnabled(Hwasan) {
1753 return false
1754 } else if sanitize.isSanitizerEnabled(Fuzzer) {
1755 return false
Ivan Lozano30c5db22018-02-21 15:49:20 -08001756 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001757
1758 if enableUbsanRuntime(sanitize) {
1759 return false
1760 }
1761
1762 sanitizeProps := &sanitize.Properties.SanitizeMutated
1763 if Bool(sanitizeProps.Diag.Cfi) {
1764 return false
1765 }
1766
1767 return Bool(sanitizeProps.Integer_overflow) ||
1768 len(sanitizeProps.Misc_undefined) > 0 ||
1769 Bool(sanitizeProps.Undefined) ||
1770 Bool(sanitizeProps.All_undefined)
Ivan Lozano30c5db22018-02-21 15:49:20 -08001771}
1772
Ivan Lozanod7586b62021-04-01 09:49:36 -04001773func (m *Module) UbsanRuntimeNeeded() bool {
1774 return enableUbsanRuntime(m.sanitize)
1775}
1776
1777func (m *Module) MinimalRuntimeNeeded() bool {
1778 return enableMinimalRuntime(m.sanitize)
1779}
1780
Inseob Kim8471cda2019-11-15 09:59:12 +09001781func enableUbsanRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001782 sanitizeProps := &sanitize.Properties.SanitizeMutated
1783 return Bool(sanitizeProps.Diag.Integer_overflow) ||
1784 Bool(sanitizeProps.Diag.Undefined) ||
1785 len(sanitizeProps.Diag.Misc_undefined) > 0
Inseob Kim8471cda2019-11-15 09:59:12 +09001786}
1787
Vishwath Mohane7128792017-11-17 11:08:10 -08001788func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001789 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001790}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001791
1792func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001793 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001794}