blob: f34c6318163ce5cc44380ab2d47025805b350290 [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
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000065 cfiBlocklistPath = "external/compiler-rt/lib/cfi"
66 cfiBlocklistFilename = "cfi_blocklist.txt"
67 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
68 "-fsanitize-ignorelist=" + cfiBlocklistPath + "/" + cfiBlocklistFilename}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070069 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
70 // used, but have no effect on assembly files
71 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070072 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070073 "-Wl,-plugin-opt,O1"}
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000074 cfiExportsMapPath = "build/soong/cc/config"
75 cfiExportsMapFilename = "cfi_exports.map"
76 cfiAssemblySupportFlag = "-fno-sanitize-cfi-canonical-jump-tables"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070077
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070078 intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080079
Peter Collingbournebd19db02019-03-06 10:38:48 -080080 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070081 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070082 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
Christopher Ferris2fc8e032023-01-26 14:19:27 -080083 "export_memory_stats=0", "max_malloc_fill_size=131072", "malloc_fill_byte=0"}
Florian Mayer1866bbe2023-03-11 01:07:40 +000084 memtagStackCommonFlags = []string{"-march=armv8-a+memtag", "-mllvm", "-dom-tree-reachability-max-bbs-to-explore=128"}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +000085
86 hostOnlySanitizeFlags = []string{"-fno-sanitize-recover=all"}
87 deviceOnlySanitizeFlags = []string{"-fsanitize-trap=all", "-ftrap-function=abort"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070088)
89
Ivan Lozano3968d8f2020-12-14 11:27:52 -050090type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080091
Colin Cross16b23492016-01-06 14:41:07 -080092const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050093 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070094 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080095 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070096 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080097 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -050098 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -040099 Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +0000100 Memtag_stack
Liz Kammer75db9312021-07-07 16:41:50 -0400101 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -0800102)
103
Liz Kammer75db9312021-07-07 16:41:50 -0400104var Sanitizers = []SanitizerType{
105 Asan,
106 Hwasan,
107 tsan,
108 intOverflow,
109 scs,
110 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400111 Memtag_heap,
Florian Mayerd8434a42022-08-31 20:57:03 +0000112 Memtag_stack,
Liz Kammer75db9312021-07-07 16:41:50 -0400113 cfi, // cfi is last to prevent it running before incompatible mutators
114}
115
Jiyong Park82226632019-02-01 10:50:50 +0900116// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500117func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800118 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500119 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800120 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700121 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700122 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800123 case tsan:
124 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700125 case intOverflow:
126 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000127 case cfi:
128 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800129 case scs:
130 return "scs"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400131 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700132 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000133 case Memtag_stack:
134 return "memtag_stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500135 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700136 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800137 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500138 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800139 }
140}
141
Jiyong Park82226632019-02-01 10:50:50 +0900142// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500143func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900144 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500145 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900146 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700147 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900148 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400149 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700150 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000151 case Memtag_stack:
152 return "memtag_stack"
Jiyong Park82226632019-02-01 10:50:50 +0900153 case tsan:
154 return "thread"
155 case intOverflow:
156 return "integer_overflow"
157 case cfi:
158 return "cfi"
159 case scs:
160 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500161 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700162 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900163 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500164 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900165 }
166}
167
Liz Kammer75db9312021-07-07 16:41:50 -0400168func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
169 switch t {
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200170 case cfi, Hwasan, Asan, tsan, Fuzzer, scs:
171 sanitizer := &sanitizerSplitMutator{t}
172 ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
173 ctx.Transition(t.variationName(), sanitizer)
Florian Mayerd8434a42022-08-31 20:57:03 +0000174 case Memtag_heap, Memtag_stack, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400175 // do nothing
176 default:
177 panic(fmt.Errorf("unknown SanitizerType %d", t))
178 }
179}
180
Liz Kammerfd8a49f2022-10-31 10:31:11 -0400181// shouldPropagateToSharedLibraryDeps returns whether a sanitizer type should propagate to share
182// dependencies. In most cases, sanitizers only propagate to static dependencies; however, some
183// sanitizers also must be enabled for shared libraries for linking.
184func (t SanitizerType) shouldPropagateToSharedLibraryDeps() bool {
185 switch t {
186 case Fuzzer:
187 // Typically, shared libs are not split. However, for fuzzer, we split even for shared libs
188 // because a library sanitized for fuzzer can't be linked from a library that isn't sanitized
189 // for fuzzer.
190 return true
191 default:
192 return false
193 }
194}
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500195func (*Module) SanitizerSupported(t SanitizerType) bool {
196 switch t {
197 case Asan:
198 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700199 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500200 return true
201 case tsan:
202 return true
203 case intOverflow:
204 return true
205 case cfi:
206 return true
207 case scs:
208 return true
209 case Fuzzer:
210 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400211 case Memtag_heap:
212 return true
Florian Mayerd8434a42022-08-31 20:57:03 +0000213 case Memtag_stack:
214 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500215 default:
216 return false
217 }
218}
219
220// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
221func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700222 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900223}
224
Martin Stjernholmb0249572020-09-15 02:32:35 +0100225type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400226 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100227 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800228
Liz Kammer75b9b402021-06-25 15:19:27 -0400229 // ASan (Address sanitizer), incompatible with static binaries.
230 // Always runs in a diagnostic mode.
231 // Use of address sanitizer disables cfi sanitizer.
232 // Hwaddress sanitizer takes precedence over this sanitizer.
233 Address *bool `android:"arch_variant"`
234 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
235 // Always runs in a diagnostic mode.
236 // Use of thread sanitizer disables cfi and scudo sanitizers.
237 // Hwaddress sanitizer takes precedence over this sanitizer.
238 Thread *bool `android:"arch_variant"`
239 // HWASan (Hardware Address sanitizer).
240 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100241 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800242
Liz Kammer75b9b402021-06-25 15:19:27 -0400243 // Undefined behavior sanitizer
244 All_undefined *bool `android:"arch_variant"`
245 // Subset of undefined behavior sanitizer
246 Undefined *bool `android:"arch_variant"`
247 // List of specific undefined behavior sanitizers to enable
248 Misc_undefined []string `android:"arch_variant"`
249 // Fuzzer, incompatible with static binaries.
250 Fuzzer *bool `android:"arch_variant"`
251 // safe-stack sanitizer, incompatible with 32-bit architectures.
252 Safestack *bool `android:"arch_variant"`
253 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
254 Cfi *bool `android:"arch_variant"`
255 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
256 Integer_overflow *bool `android:"arch_variant"`
257 // scudo sanitizer, incompatible with asan, hwasan, tsan
258 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
259 // deprecated
260 Scudo *bool `android:"arch_variant"`
Elliott Hughese4793bc2023-02-09 21:15:47 +0000261 // shadow-call-stack sanitizer, only available on arm64/riscv64.
Liz Kammer75b9b402021-06-25 15:19:27 -0400262 Scs *bool `android:"arch_variant"`
263 // Memory-tagging, only available on arm64
264 // if diag.memtag unset or false, enables async memory tagging
Florian Mayer00ab5cf2022-08-31 18:30:18 +0000265 Memtag_heap *bool `android:"arch_variant"`
Florian Mayerd8434a42022-08-31 20:57:03 +0000266 // Memory-tagging stack instrumentation, only available on arm64
267 // Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
268 Memtag_stack *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100269
270 // A modifier for ASAN and HWASAN for write only instrumentation
271 Writeonly *bool `android:"arch_variant"`
272
273 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
274 // Replaces abort() on error with a human-readable error message.
275 // Address and Thread sanitizers always run in diagnostic mode.
276 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400277 // Undefined behavior sanitizer, diagnostic mode
278 Undefined *bool `android:"arch_variant"`
279 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
280 Cfi *bool `android:"arch_variant"`
281 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
282 Integer_overflow *bool `android:"arch_variant"`
283 // Memory-tagging, only available on arm64
284 // requires sanitizer.memtag: true
285 // if set, enables sync memory tagging
286 Memtag_heap *bool `android:"arch_variant"`
287 // List of specific undefined behavior sanitizers to enable in diagnostic mode
288 Misc_undefined []string `android:"arch_variant"`
289 // List of sanitizers to pass to -fno-sanitize-recover
290 // results in only the first detected error for these sanitizers being reported and program then
291 // exits with a non-zero exit code.
292 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800293 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800294
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800295 // Sanitizers to run with flag configuration specified
296 Config struct {
297 // Enables CFI support flags for assembly-heavy libraries
298 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800299 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800300
Liz Kammer75b9b402021-06-25 15:19:27 -0400301 // List of sanitizers to pass to -fsanitize-recover
302 // allows execution to continue for these sanitizers to detect multiple errors rather than only
303 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100304 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000305
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700306 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100307 Blocklist *string
308}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700309
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400310type sanitizeMutatedProperties struct {
311 // Whether sanitizers can be enabled on this module
312 Never *bool `blueprint:"mutated"`
313
314 // Whether ASan (Address sanitizer) is enabled for this module.
315 // Hwaddress sanitizer takes precedence over this sanitizer.
316 Address *bool `blueprint:"mutated"`
317 // Whether TSan (Thread sanitizer) is enabled for this module
318 Thread *bool `blueprint:"mutated"`
319 // Whether HWASan (Hardware Address sanitizer) is enabled for this module
320 Hwaddress *bool `blueprint:"mutated"`
321
322 // Whether Undefined behavior sanitizer is enabled for this module
323 All_undefined *bool `blueprint:"mutated"`
324 // Whether undefined behavior sanitizer subset is enabled for this module
325 Undefined *bool `blueprint:"mutated"`
326 // List of specific undefined behavior sanitizers enabled for this module
327 Misc_undefined []string `blueprint:"mutated"`
328 // Whether Fuzzeris enabled for this module
329 Fuzzer *bool `blueprint:"mutated"`
330 // whether safe-stack sanitizer is enabled for this module
331 Safestack *bool `blueprint:"mutated"`
332 // Whether cfi sanitizer is enabled for this module
333 Cfi *bool `blueprint:"mutated"`
334 // Whether signed/unsigned integer overflow sanitizer is enabled for this module
335 Integer_overflow *bool `blueprint:"mutated"`
336 // Whether scudo sanitizer is enabled for this module
337 Scudo *bool `blueprint:"mutated"`
338 // Whether shadow-call-stack sanitizer is enabled for this module.
339 Scs *bool `blueprint:"mutated"`
340 // Whether Memory-tagging is enabled for this module
341 Memtag_heap *bool `blueprint:"mutated"`
342 // Whether Memory-tagging stack instrumentation is enabled for this module
343 Memtag_stack *bool `blueprint:"mutated"`
344
345 // Whether a modifier for ASAN and HWASAN for write only instrumentation is enabled for this
346 // module
347 Writeonly *bool `blueprint:"mutated"`
348
349 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
350 Diag struct {
351 // Whether Undefined behavior sanitizer, diagnostic mode is enabled for this module
352 Undefined *bool `blueprint:"mutated"`
353 // Whether cfi sanitizer, diagnostic mode is enabled for this module
354 Cfi *bool `blueprint:"mutated"`
355 // Whether signed/unsigned integer overflow sanitizer, diagnostic mode is enabled for this
356 // module
357 Integer_overflow *bool `blueprint:"mutated"`
358 // Whether Memory-tagging, diagnostic mode is enabled for this module
359 Memtag_heap *bool `blueprint:"mutated"`
360 // List of specific undefined behavior sanitizers enabled in diagnostic mode
361 Misc_undefined []string `blueprint:"mutated"`
362 } `blueprint:"mutated"`
363}
364
Martin Stjernholmb0249572020-09-15 02:32:35 +0100365type SanitizeProperties struct {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400366 Sanitize SanitizeUserProps `android:"arch_variant"`
367 SanitizeMutated sanitizeMutatedProperties `blueprint:"mutated"`
368
369 SanitizerEnabled bool `blueprint:"mutated"`
370 MinimalRuntimeDep bool `blueprint:"mutated"`
371 BuiltinsDep bool `blueprint:"mutated"`
372 UbsanRuntimeDep bool `blueprint:"mutated"`
373 InSanitizerDir bool `blueprint:"mutated"`
374 Sanitizers []string `blueprint:"mutated"`
375 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800376}
377
378type sanitize struct {
379 Properties SanitizeProperties
380}
381
Cindy Zhou18417cb2020-12-10 07:12:38 -0800382// Mark this tag with a check to see if apex dependency check should be skipped
383func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
384 return t.skipApexAllowedDependenciesCheck
385}
386
387var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
388
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000389var exportedVars = android.NewExportedVariables(pctx)
390
Vishwath Mohane7128792017-11-17 11:08:10 -0800391func init() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000392 exportedVars.ExportStringListStaticVariable("HostOnlySanitizeFlags", hostOnlySanitizeFlags)
393 exportedVars.ExportStringList("DeviceOnlySanitizeFlags", deviceOnlySanitizeFlags)
394
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000395 // Leave out "-flto" from the slices exported to bazel, as we will use the
396 // dedicated LTO feature for this
397 exportedVars.ExportStringList("CfiCFlags", cfiCflags[1:])
398 exportedVars.ExportStringList("CfiAsFlags", cfiAsflags[1:])
399 exportedVars.ExportStringList("CfiLdFlags", cfiLdflags[1:])
400
401 exportedVars.ExportString("CfiBlocklistPath", cfiBlocklistPath)
402 exportedVars.ExportString("CfiBlocklistFilename", cfiBlocklistFilename)
403 exportedVars.ExportString("CfiExportsMapPath", cfiExportsMapPath)
404 exportedVars.ExportString("CfiExportsMapFilename", cfiExportsMapFilename)
405 exportedVars.ExportString("CfiAssemblySupportFlag", cfiAssemblySupportFlag)
406
Vishwath Mohane7128792017-11-17 11:08:10 -0800407 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700408 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800409}
410
Colin Cross16b23492016-01-06 14:41:07 -0800411func (sanitize *sanitize) props() []interface{} {
412 return []interface{}{&sanitize.Properties}
413}
414
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400415func (p *sanitizeMutatedProperties) copyUserPropertiesToMutated(userProps *SanitizeUserProps) {
416 p.Never = userProps.Never
417 p.Address = userProps.Address
418 p.All_undefined = userProps.All_undefined
419 p.Cfi = userProps.Cfi
420 p.Fuzzer = userProps.Fuzzer
421 p.Hwaddress = userProps.Hwaddress
422 p.Integer_overflow = userProps.Integer_overflow
423 p.Memtag_heap = userProps.Memtag_heap
424 p.Memtag_stack = userProps.Memtag_stack
425 p.Safestack = userProps.Safestack
426 p.Scs = userProps.Scs
427 p.Scudo = userProps.Scudo
428 p.Thread = userProps.Thread
429 p.Undefined = userProps.Undefined
430 p.Writeonly = userProps.Writeonly
431
432 p.Misc_undefined = make([]string, 0, len(userProps.Misc_undefined))
433 for _, v := range userProps.Misc_undefined {
434 p.Misc_undefined = append(p.Misc_undefined, v)
435 }
436
437 p.Diag.Cfi = userProps.Diag.Cfi
438 p.Diag.Integer_overflow = userProps.Diag.Integer_overflow
439 p.Diag.Memtag_heap = userProps.Diag.Memtag_heap
440 p.Diag.Undefined = userProps.Diag.Undefined
441
442 p.Diag.Misc_undefined = make([]string, 0, len(userProps.Diag.Misc_undefined))
443 for _, v := range userProps.Diag.Misc_undefined {
444 p.Diag.Misc_undefined = append(p.Diag.Misc_undefined, v)
445 }
446}
447
Colin Cross16b23492016-01-06 14:41:07 -0800448func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400449 s := &sanitize.Properties.SanitizeMutated
450 s.copyUserPropertiesToMutated(&sanitize.Properties.Sanitize)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700451
Colin Cross16b23492016-01-06 14:41:07 -0800452 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700453 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800454 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800455 }
456
457 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800458 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800459 return
460 }
461
Florian Mayerd8434a42022-08-31 20:57:03 +0000462 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400463 if ctx.testBinary() {
464 if s.Memtag_heap == nil {
465 s.Memtag_heap = proptools.BoolPtr(true)
466 }
467 if s.Diag.Memtag_heap == nil {
468 s.Diag.Memtag_heap = proptools.BoolPtr(true)
469 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800470 }
471
Colin Cross16b23492016-01-06 14:41:07 -0800472 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700473 var globalSanitizersDiag []string
474
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700475 if ctx.Host() {
476 if !ctx.Windows() {
477 globalSanitizers = ctx.Config().SanitizeHost()
478 }
479 } else {
480 arches := ctx.Config().SanitizeDeviceArch()
481 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
482 globalSanitizers = ctx.Config().SanitizeDevice()
483 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800484 }
485 }
486
Colin Cross16b23492016-01-06 14:41:07 -0800487 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000488 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700489 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400490 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000491 }
Colin Cross16b23492016-01-06 14:41:07 -0800492
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700493 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400494 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000495 }
496
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700497 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400498 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000499 }
500
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700501 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400502 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000503 }
504
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700505 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400506 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700507 }
508
509 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400510 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000511 }
512
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700513 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800514 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400515 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700516 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700517 }
518
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700519 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700520 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700521 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400522 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700523 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700524 }
525
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700526 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400527 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700528 }
529
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700530 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400531 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700532 }
533
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000534 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
535 // Hwaddress and Address are set before, so we can check them here
536 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
537 if s.Address == nil && s.Hwaddress == nil {
538 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
539 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400540 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000541 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700542 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800543 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400544 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800545 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700546 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000547
Florian Mayerd8434a42022-08-31 20:57:03 +0000548 if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
549 s.Memtag_stack = proptools.BoolPtr(true)
550 }
551
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000552 if len(globalSanitizers) > 0 {
553 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
554 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700555
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700556 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700557 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700558 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400559 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700560 }
561
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700562 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
563 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400564 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700565 }
566
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800567 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
568 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400569 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800570 }
571
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700572 if len(globalSanitizersDiag) > 0 {
573 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
574 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700575 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700576
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800577 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross88a029f2022-06-23 14:51:20 -0700578 if ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800579 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800580 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400581 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800582 }
583 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400584 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800585 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800586 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800587 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400588 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800589 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800590 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700591 }
592
Elvis Chien9c993542021-06-25 01:15:17 +0800593 // Enable CFI for non-host components in the include paths
594 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400595 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000596 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400597 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700598 }
599 }
600
Elliott Hughesda3a0712020-03-06 16:55:28 -0800601 // Is CFI actually enabled?
602 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400603 s.Cfi = nil
604 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800605 }
606
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700607 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross88a029f2022-06-23 14:51:20 -0700608 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700609 s.Hwaddress = nil
610 }
611
Elliott Hughese4793bc2023-02-09 21:15:47 +0000612 // SCS is only implemented on AArch64/riscv64.
613 if (ctx.Arch().ArchType != android.Arm64 && ctx.Arch().ArchType != android.Riscv64) || !ctx.toolchain().Bionic() {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800614 s.Scs = nil
615 }
616
Ivan Lozano62cd0382021-11-01 10:27:54 -0400617 // Memtag_heap is only implemented on AArch64.
Florian Mayerd8434a42022-08-31 20:57:03 +0000618 // Memtag ABI is Android specific for now, so disable for host.
619 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() || ctx.Host() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700620 s.Memtag_heap = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000621 s.Memtag_stack = nil
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700622 }
623
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700624 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700625 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400626 s.Cfi = nil
627 s.Diag.Cfi = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000628 // HWASAN and ASAN win against MTE.
629 s.Memtag_heap = nil
630 s.Memtag_stack = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700631 }
632
Colin Crossed12a042022-02-07 13:55:55 -0800633 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
634 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400635 s.Cfi = nil
636 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700637 s.Misc_undefined = nil
638 s.Undefined = nil
639 s.All_undefined = nil
640 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800641 }
642
Colin Cross2c435a02022-10-20 13:55:53 -0700643 // TODO(b/254713216): CFI doesn't work for riscv64 yet because LTO doesn't work.
Colin Crossecf4e662022-10-20 13:59:17 -0700644 if ctx.Arch().ArchType == android.Riscv64 {
645 s.Cfi = nil
646 s.Diag.Cfi = nil
647 }
648
Colin Crossed12a042022-02-07 13:55:55 -0800649 // Disable CFI for musl
650 if ctx.toolchain().Musl() {
651 s.Cfi = nil
652 s.Diag.Cfi = nil
653 }
654
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700655 // Also disable CFI for VNDK variants of components
656 if ctx.isVndk() && ctx.useVndk() {
Justin Yun08270c62022-12-19 17:01:26 +0900657 s.Cfi = nil
658 s.Diag.Cfi = nil
Inseob Kimeec88e12020-01-22 11:11:29 +0900659 }
660
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700661 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700662 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
663 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700664 s.Hwaddress = nil
665 }
666
Colin Cross3c344ef2016-07-18 15:44:56 -0700667 if ctx.staticBinary() {
668 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700669 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700670 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800671 }
672
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700673 if Bool(s.All_undefined) {
674 s.Undefined = nil
675 }
676
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700677 if !ctx.toolchain().Is64Bit() {
678 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700679 s.Thread = nil
680 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800681 // TODO(ccross): error for compile_multilib = "32"?
682 }
683
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800684 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 -0700685 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 +0000686 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700687 sanitize.Properties.SanitizerEnabled = true
688 }
689
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800690 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
691 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700692 s.Scudo = nil
693 }
694
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700695 if Bool(s.Hwaddress) {
696 s.Address = nil
697 s.Thread = nil
698 }
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000699
700 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
701 // mutually incompatible.
702 if Bool(s.Fuzzer) {
703 s.Cfi = nil
704 }
Colin Cross16b23492016-01-06 14:41:07 -0800705}
706
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800707func toDisableImplicitIntegerChange(flags []string) bool {
708 // Returns true if any flag is fsanitize*integer, and there is
709 // no explicit flag about sanitize=implicit-integer-sign-change.
710 for _, f := range flags {
711 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
712 return false
713 }
714 }
715 for _, f := range flags {
716 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
717 return true
718 }
719 }
720 return false
721}
722
Yabin Cuidb7dda82020-11-30 15:47:45 -0800723func toDisableUnsignedShiftBaseChange(flags []string) bool {
724 // Returns true if any flag is fsanitize*integer, and there is
725 // no explicit flag about sanitize=unsigned-shift-base.
726 for _, f := range flags {
727 if strings.Contains(f, "sanitize=unsigned-shift-base") {
728 return false
729 }
730 }
731 for _, f := range flags {
732 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
733 return true
734 }
735 }
736 return false
737}
738
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400739func (s *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
740 if !s.Properties.SanitizerEnabled && !s.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800741 return flags
742 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400743 sanProps := &s.Properties.SanitizeMutated
Colin Cross16b23492016-01-06 14:41:07 -0800744
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400745 if Bool(sanProps.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700746 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800747 // Frame pointer based unwinder in ASan requires ARM frame setup.
748 // TODO: put in flags?
749 flags.RequiredInstructionSet = "arm"
750 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800751 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
752 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800753
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400754 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000755 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
756 }
757
Colin Cross16b23492016-01-06 14:41:07 -0800758 if ctx.Host() {
759 // -nodefaultlibs (provided with libc++) prevents the driver from linking
760 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800761 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800762 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800763 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900764 if ctx.bootstrap() {
765 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
766 } else {
767 flags.DynamicLinker = "/system/bin/linker_asan"
768 }
Colin Cross16b23492016-01-06 14:41:07 -0800769 if flags.Toolchain.Is64Bit() {
770 flags.DynamicLinker += "64"
771 }
772 }
Colin Cross16b23492016-01-06 14:41:07 -0800773 }
774
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400775 if Bool(sanProps.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800776 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800777
778 for _, flag := range hwasanCommonflags {
779 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
780 }
781 for _, flag := range hwasanCommonflags {
782 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
783 }
784
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400785 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000786 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
787 }
Florian Mayer95cd6db2023-03-23 17:48:07 -0700788 if !ctx.staticBinary() && !ctx.Host() {
789 if ctx.bootstrap() {
790 flags.DynamicLinker = "/system/bin/bootstrap/linker_hwasan64"
791 } else {
792 flags.DynamicLinker = "/system/bin/linker_hwasan64"
793 }
794 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700795 }
796
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400797 if Bool(sanProps.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800798 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700799
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000800 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
801 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
802 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
803 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
804 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
805
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700806 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
807 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
808 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800809 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
810 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700811
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700812 // Disable fortify for fuzzing builds. Generally, we'll be building with
813 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800814 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800815
816 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
817 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
818 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
819 // the DT_RUNPATH from the shared library above it, and not the executable,
820 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
821 // DT_RUNPATH here means that transient shared libraries can be found
822 // colocated with their parents.
823 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800824 }
825
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400826 if Bool(sanProps.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800827 if ctx.Arch().ArchType == android.Arm {
828 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
829 // to do this on a function basis, so force Thumb on the entire module.
830 flags.RequiredInstructionSet = "thumb"
831 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000832
Colin Cross4af21ed2019-11-04 09:37:55 -0800833 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
834 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400835 if Bool(s.Properties.Sanitize.Config.Cfi_assembly_support) {
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000836 flags.Local.CFlags = append(flags.Local.CFlags, cfiAssemblySupportFlag)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800837 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000838 // Only append the default visibility flag if -fvisibility has not already been set
839 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800840 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
841 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000842 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800843 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000844
845 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800846 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
847 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000848 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700849 }
850
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400851 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000852 flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
Florian Mayer25cd9812023-03-21 16:13:36 +0000853 // TODO(fmayer): remove -Wno-error once https://reviews.llvm.org/D127917 is in Android toolchain.
854 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-error=frame-larger-than")
Florian Mayerd8434a42022-08-31 20:57:03 +0000855 flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
856 flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
Florian Mayer25cd9812023-03-21 16:13:36 +0000857 // This works around LLD complaining about the stack frame size.
858 // TODO(fmayer): remove once https://reviews.llvm.org/D127917 is in Android toolchain.
859 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-fatal-warnings")
Florian Mayerd8434a42022-08-31 20:57:03 +0000860 }
861
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400862 if (Bool(sanProps.Memtag_heap) || Bool(sanProps.Memtag_stack)) && ctx.binary() {
863 if Bool(sanProps.Diag.Memtag_heap) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000864 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
865 } else {
866 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
867 }
868 }
869
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400870 if Bool(sanProps.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800871 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700872 }
873
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400874 if len(s.Properties.Sanitizers) > 0 {
875 sanitizeArg := "-fsanitize=" + strings.Join(s.Properties.Sanitizers, ",")
Colin Cross4af21ed2019-11-04 09:37:55 -0800876 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
877 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross234b01d2022-02-07 13:49:03 -0800878 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
879
Colin Crossed12a042022-02-07 13:55:55 -0800880 if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
881 // Bionic and musl sanitizer runtimes have already been added as dependencies so that
882 // the right variant of the runtime will be used (with the "-android" or "-musl"
883 // suffixes), so don't let clang the runtime library.
Colin Cross234b01d2022-02-07 13:49:03 -0800884 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-link-runtime")
885 } else {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800886 // Host sanitizers only link symbols in the final executable, so
887 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800888 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
Colin Cross6c18d002022-06-02 15:11:50 -0700889 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500890
Colin Cross6c18d002022-06-02 15:11:50 -0700891 if !ctx.toolchain().Bionic() {
892 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function san.
893 // Musl toolchain prebuilts have vptr and function sanitizers, but enabling them
894 // implicitly enables RTTI which causes RTTI mismatch issues with dependencies.
895
Colin Cross234b01d2022-02-07 13:49:03 -0800896 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500897 }
898
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400899 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700900 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800901 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700902 } else if ctx.Host() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000903 flags.Local.CFlags = append(flags.Local.CFlags, hostOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700904 } else {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000905 flags.Local.CFlags = append(flags.Local.CFlags, deviceOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700906 }
Evgenii Stepanov59012812022-06-24 11:09:18 -0700907
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400908 if enableMinimalRuntime(s) {
Evgenii Stepanov59012812022-06-24 11:09:18 -0700909 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
910 }
911
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800912 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800913 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
914 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800915 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800916 // http://b/171275751, Android doesn't build with this sanitizer yet.
917 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
918 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
919 }
Colin Cross16b23492016-01-06 14:41:07 -0800920 }
921
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400922 if len(s.Properties.DiagSanitizers) > 0 {
923 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(s.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700924 }
925 // FIXME: enable RTTI if diag + (cfi or vptr)
926
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400927 if s.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800928 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400929 strings.Join(s.Properties.Sanitize.Recover, ","))
Andreas Gampe97071162017-05-08 13:15:23 -0700930 }
931
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400932 if s.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800933 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400934 strings.Join(s.Properties.Sanitize.Diag.No_recover, ","))
Ivan Lozano7929bba2018-12-12 09:36:31 -0800935 }
936
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400937 blocklist := android.OptionalPathForModuleSrc(ctx, s.Properties.Sanitize.Blocklist)
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700938 if blocklist.Valid() {
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700939 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-ignorelist="+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700940 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
941 }
942
Colin Cross16b23492016-01-06 14:41:07 -0800943 return flags
944}
945
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400946func (s *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900947 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
948 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800949 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400950 if Bool(s.Properties.SanitizeMutated.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800951 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900952 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400953 if Bool(s.Properties.SanitizeMutated.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800954 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900955 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400956 if Bool(s.Properties.SanitizeMutated.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800957 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900958 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800959 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700960}
961
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400962func (s *sanitize) inSanitizerDir() bool {
963 return s.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700964}
965
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500966// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400967func (s *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000968 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500969 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400970 return s.Properties.SanitizeMutated.Address
Tri Vo6eafc362021-04-01 11:29:09 -0700971 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400972 return s.Properties.SanitizeMutated.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000973 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400974 return s.Properties.SanitizeMutated.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000975 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400976 return s.Properties.SanitizeMutated.Integer_overflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000977 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400978 return s.Properties.SanitizeMutated.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800979 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400980 return s.Properties.SanitizeMutated.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -0400981 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400982 return s.Properties.SanitizeMutated.Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +0000983 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400984 return s.Properties.SanitizeMutated.Memtag_stack
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500985 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400986 return s.Properties.SanitizeMutated.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000987 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500988 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000989 }
990}
991
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500992// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800993func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500994 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700995 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800996 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800997 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700998 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -0400999 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Florian Mayerd8434a42022-08-31 20:57:03 +00001000 !sanitize.isSanitizerEnabled(Memtag_stack) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001001 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -08001002}
1003
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001004// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001005func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001006 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001007 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001008 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001009 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001010}
1011
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001012func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -04001013 bPtr := proptools.BoolPtr(b)
1014 if !b {
1015 bPtr = nil
1016 }
Colin Cross16b23492016-01-06 14:41:07 -08001017 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001018 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001019 sanitize.Properties.SanitizeMutated.Address = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001020 // For ASAN variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001021 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Tri Vo6eafc362021-04-01 11:29:09 -07001022 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001023 sanitize.Properties.SanitizeMutated.Hwaddress = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001024 // For HWAsan variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001025 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Colin Cross16b23492016-01-06 14:41:07 -08001026 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001027 sanitize.Properties.SanitizeMutated.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -07001028 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001029 sanitize.Properties.SanitizeMutated.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001030 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001031 sanitize.Properties.SanitizeMutated.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001032 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001033 sanitize.Properties.SanitizeMutated.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -04001034 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001035 sanitize.Properties.SanitizeMutated.Memtag_heap = bPtr
Florian Mayerd8434a42022-08-31 20:57:03 +00001036 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001037 sanitize.Properties.SanitizeMutated.Memtag_stack = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001038 // We do not need to disable ASAN or HWASan here, as there is no Memtag_stack variant.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001039 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001040 sanitize.Properties.SanitizeMutated.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -08001041 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001042 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -08001043 }
1044 if b {
1045 sanitize.Properties.SanitizerEnabled = true
1046 }
1047}
1048
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001049// Check if the sanitizer is explicitly disabled (as opposed to nil by
1050// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001051func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001052 if sanitize == nil {
1053 return false
1054 }
1055
1056 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1057 return sanitizerVal != nil && *sanitizerVal == false
1058}
1059
1060// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
1061// because enabling a sanitizer either directly (via the blueprint) or
1062// indirectly (via a mutator) sets the bool ptr to true, and you can't
1063// distinguish between the cases. It isn't needed though - both cases can be
1064// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001065func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001066 if sanitize == nil {
1067 return false
1068 }
1069
1070 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1071 return sanitizerVal != nil && *sanitizerVal == true
1072}
1073
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001074// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
1075func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -07001076 switch t := tag.(type) {
1077 case dependencyTag:
1078 return t == reuseObjTag || t == objDepTag
1079 case libraryDependencyTag:
1080 return true
1081 default:
1082 return false
1083 }
Colin Cross6b753602018-06-21 13:03:07 -07001084}
1085
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001086func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
1087 return IsSanitizableDependencyTag
1088}
1089
Inseob Kimc42f2f22020-07-29 20:32:10 +09001090// Determines if the current module is a static library going to be captured
1091// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
1092// except for ones which explicitly disable cfi.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001093func needsCfiForVendorSnapshot(mctx android.BaseModuleContext) bool {
Justin Yun8814fc52022-12-15 21:45:35 +09001094 if inList("hwaddress", mctx.Config().SanitizeDevice()) {
1095 // cfi will not be built if SANITIZE_TARGET=hwaddress is set
1096 return false
1097 }
1098
Kiyoung Kim48f37782021-07-07 12:42:39 +09001099 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001100 return false
1101 }
1102
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001103 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001104
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001105 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001106 return false
1107 }
1108
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001109 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001110 return false
1111 }
1112
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001113 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001114 return false
1115 }
1116
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001117 if !c.SanitizerSupported(cfi) {
1118 return false
1119 }
1120
1121 return c.SanitizePropDefined() &&
1122 !c.SanitizeNever() &&
1123 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001124}
1125
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001126type sanitizerSplitMutator struct {
1127 sanitizer SanitizerType
1128}
1129
1130// If an APEX is sanitized or not depends on whether it contains at least one
1131// sanitized module. Transition mutators cannot propagate information up the
1132// dependency graph this way, so we need an auxiliary mutator to do so.
1133func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
1134 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1135 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1136 ctx.VisitDirectDeps(func(dep android.Module) {
1137 if c, ok := dep.(*Module); ok && c.sanitize.isSanitizerEnabled(s.sanitizer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001138 enabled = true
Inseob Kimc42f2f22020-07-29 20:32:10 +09001139 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001140 })
1141
1142 if enabled {
1143 sanitizeable.EnableSanitizer(s.sanitizer.name())
1144 }
1145 }
1146}
1147
1148func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
1149 if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1150 if s.sanitizer == cfi && needsCfiForVendorSnapshot(ctx) {
1151 return []string{"", s.sanitizer.variationName()}
1152 }
1153
1154 // If the given sanitizer is not requested in the .bp file for a module, it
1155 // won't automatically build the sanitized variation.
1156 if !c.IsSanitizerEnabled(s.sanitizer) {
1157 return []string{""}
1158 }
1159
1160 if c.Binary() {
1161 // If a sanitizer is enabled for a binary, we do not build the version
1162 // without the sanitizer
1163 return []string{s.sanitizer.variationName()}
1164 } else if c.StaticallyLinked() || c.Header() {
1165 // For static libraries, we build both versions. Some Make modules
1166 // apparently depend on this behavior.
1167 return []string{"", s.sanitizer.variationName()}
1168 } else {
1169 // We only build the requested variation of dynamic libraries
1170 return []string{s.sanitizer.variationName()}
1171 }
1172 }
1173
1174 if _, ok := ctx.Module().(JniSanitizeable); ok {
1175 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1176 // that is short-circuited for now
1177 return []string{""}
1178 }
1179
1180 // If an APEX has a sanitized dependency, we build the APEX in the sanitized
1181 // variation. This is useful because such APEXes require extra dependencies.
1182 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1183 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1184 if enabled {
1185 return []string{s.sanitizer.variationName()}
1186 } else {
1187 return []string{""}
1188 }
1189 }
1190
1191 if c, ok := ctx.Module().(*Module); ok {
1192 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1193
1194 // Check if it's a snapshot module supporting sanitizer
Justin Yun08270c62022-12-19 17:01:26 +09001195 if ss, ok := c.linker.(snapshotSanitizer); ok {
1196 if ss.isSanitizerAvailable(s.sanitizer) {
1197 return []string{"", s.sanitizer.variationName()}
1198 } else {
1199 return []string{""}
1200 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001201 }
1202 }
1203
1204 return []string{""}
1205}
1206
1207func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1208 if c, ok := ctx.Module().(PlatformSanitizeable); ok {
1209 if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
1210 // If the dependency is through a non-sanitizable tag, use the
1211 // non-sanitized variation
1212 return ""
1213 }
1214
1215 return sourceVariation
1216 } else if _, ok := ctx.Module().(JniSanitizeable); ok {
1217 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1218 // that is short-circuited for now
1219 return ""
1220 } else {
1221 // Otherwise, do not rock the boat.
1222 return sourceVariation
1223 }
1224}
1225
1226func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1227 if d, ok := ctx.Module().(PlatformSanitizeable); ok {
1228 if dm, ok := ctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001229 if ss, ok := dm.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001230 return incomingVariation
Inseob Kimc42f2f22020-07-29 20:32:10 +09001231 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001232 }
1233
1234 if !d.SanitizePropDefined() ||
1235 d.SanitizeNever() ||
1236 d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
1237 !d.SanitizerSupported(s.sanitizer) {
1238 // If a module opts out of a sanitizer, use its non-sanitized variation
1239 return ""
1240 }
1241
1242 // Binaries are always built in the variation they requested.
1243 if d.Binary() {
1244 if d.IsSanitizerEnabled(s.sanitizer) {
1245 return s.sanitizer.variationName()
1246 } else {
1247 return ""
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001248 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001249 }
1250
1251 // If a shared library requests to be sanitized, it will be built for that
1252 // sanitizer. Otherwise, some sanitizers propagate through shared library
1253 // dependency edges, some do not.
1254 if !d.StaticallyLinked() && !d.Header() {
1255 if d.IsSanitizerEnabled(s.sanitizer) {
1256 return s.sanitizer.variationName()
1257 }
1258
Liz Kammerfd8a49f2022-10-31 10:31:11 -04001259 // Some sanitizers do not propagate to shared dependencies
1260 if !s.sanitizer.shouldPropagateToSharedLibraryDeps() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001261 return ""
1262 }
1263 }
1264
1265 // Static and header libraries inherit whether they are sanitized from the
1266 // module they are linked into
1267 return incomingVariation
1268 } else if d, ok := ctx.Module().(Sanitizeable); ok {
1269 // If an APEX contains a sanitized module, it will be built in the variation
1270 // corresponding to that sanitizer.
1271 enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1272 if enabled {
1273 return s.sanitizer.variationName()
1274 }
1275
1276 return incomingVariation
1277 }
1278
1279 return ""
1280}
1281
1282func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
1283 sanitizerVariation := variationName == s.sanitizer.variationName()
1284
1285 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1286 sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
1287
1288 oneMakeVariation := false
1289 if c.StaticallyLinked() || c.Header() {
1290 if s.sanitizer != cfi && s.sanitizer != scs && s.sanitizer != Hwasan {
1291 // These sanitizers export only one variation to Make. For the rest,
1292 // Make targets can depend on both the sanitized and non-sanitized
1293 // versions.
1294 oneMakeVariation = true
1295 }
1296 } else if !c.Binary() {
1297 // Shared library. These are the sanitizers that do propagate through shared
1298 // library dependencies and therefore can cause multiple variations of a
1299 // shared library to be built.
1300 if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != scs && s.sanitizer != Asan {
1301 oneMakeVariation = true
1302 }
1303 }
1304
1305 if oneMakeVariation {
1306 if sanitizerEnabled != sanitizerVariation {
1307 c.SetPreventInstall()
1308 c.SetHideFromMake()
1309 }
1310 }
1311
1312 if sanitizerVariation {
1313 c.SetSanitizer(s.sanitizer, true)
1314
1315 // CFI is incompatible with ASAN so disable it in ASAN variations
1316 if s.sanitizer.incompatibleWithCfi() {
1317 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1318 if mctx.Device() && cfiSupported {
1319 c.SetSanitizer(cfi, false)
Jiyong Parkf97782b2019-02-13 20:28:58 +09001320 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001321 }
1322
1323 // locate the asan libraries under /data/asan
1324 if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
1325 c.SetInSanitizerDir()
1326 }
1327
1328 if c.StaticallyLinked() && c.ExportedToMake() {
1329 if s.sanitizer == Hwasan {
1330 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
1331 } else if s.sanitizer == cfi {
1332 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
1333 }
1334 }
1335 } else if c.IsSanitizerEnabled(s.sanitizer) {
1336 // Disable the sanitizer for the non-sanitized variation
1337 c.SetSanitizer(s.sanitizer, false)
1338 }
1339 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
1340 // If an APEX has sanitized dependencies, it gets a few more dependencies
1341 if sanitizerVariation {
1342 sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
1343 }
1344 } else if c, ok := mctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001345 if ss, ok := c.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
1346 if !ss.isUnsanitizedVariant() {
1347 // Snapshot sanitizer may have only one variantion.
1348 // Skip exporting the module if it already has a sanitizer variation.
1349 c.SetPreventInstall()
1350 c.SetHideFromMake()
1351 return
1352 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001353 c.linker.(snapshotSanitizer).setSanitizerVariation(s.sanitizer, sanitizerVariation)
1354
1355 // Export the static lib name to make
1356 if c.static() && c.ExportedToMake() {
Justin Yun39c30312022-11-23 16:20:12 +09001357 // use BaseModuleName which is the name for Make.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001358 if s.sanitizer == cfi {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001359 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Justin Yun39c30312022-11-23 16:20:12 +09001360 } else if s.sanitizer == Hwasan {
1361 hwasanStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001362 }
1363 }
Colin Cross16b23492016-01-06 14:41:07 -08001364 }
1365 }
1366}
1367
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001368func (c *Module) SanitizeNever() bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001369 return Bool(c.sanitize.Properties.SanitizeMutated.Never)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001370}
1371
1372func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
1373 return c.sanitize.isSanitizerExplicitlyDisabled(t)
1374}
1375
Ivan Lozano30c5db22018-02-21 15:49:20 -08001376// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001377func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001378 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001379 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001380 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001381 mctx.WalkDeps(func(child, parent android.Module) bool {
1382 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1383 return false
1384 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001385
Inseob Kimeec88e12020-01-22 11:11:29 +09001386 d, ok := child.(*Module)
1387 if !ok || !d.static() {
1388 return false
1389 }
1390 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001391 if enableMinimalRuntime(d.sanitize) {
1392 // If a static dependency is built with the minimal runtime,
1393 // make sure we include the ubsan minimal runtime.
1394 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001395 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001396 // If a static dependency runs with full ubsan diagnostics,
1397 // make sure we include the ubsan runtime.
1398 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001399 }
Colin Cross0b908332019-06-19 23:00:20 -07001400
1401 if c.sanitize.Properties.MinimalRuntimeDep &&
1402 c.sanitize.Properties.UbsanRuntimeDep {
1403 // both flags that this mutator might set are true, so don't bother recursing
1404 return false
1405 }
1406
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001407 if c.Os() == android.Linux {
1408 c.sanitize.Properties.BuiltinsDep = true
1409 }
1410
Colin Cross0b908332019-06-19 23:00:20 -07001411 return true
Colin Cross6b753602018-06-21 13:03:07 -07001412 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001413
Jose Galmesf7294582020-11-13 12:07:36 -08001414 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001415 if Bool(p.properties.Sanitize_minimal_dep) {
1416 c.sanitize.Properties.MinimalRuntimeDep = true
1417 }
1418 if Bool(p.properties.Sanitize_ubsan_dep) {
1419 c.sanitize.Properties.UbsanRuntimeDep = true
1420 }
1421 }
1422
1423 return false
Colin Cross6b753602018-06-21 13:03:07 -07001424 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001425 }
1426}
1427
Jiyong Park379de2f2018-12-19 02:47:14 +09001428// Add the dependency to the runtime library for each of the sanitizer variants
1429func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001430 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001431 if !c.Enabled() {
1432 return
1433 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001434 var sanitizers []string
1435 var diagSanitizers []string
1436
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001437 sanProps := &c.sanitize.Properties.SanitizeMutated
1438
1439 if Bool(sanProps.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001440 sanitizers = append(sanitizers, "undefined")
1441 } else {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001442 if Bool(sanProps.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001443 sanitizers = append(sanitizers,
1444 "bool",
1445 "integer-divide-by-zero",
1446 "return",
1447 "returns-nonnull-attribute",
1448 "shift-exponent",
1449 "unreachable",
1450 "vla-bound",
1451 // TODO(danalbert): The following checks currently have compiler performance issues.
1452 //"alignment",
1453 //"bounds",
1454 //"enum",
1455 //"float-cast-overflow",
1456 //"float-divide-by-zero",
1457 //"nonnull-attribute",
1458 //"null",
1459 //"shift-base",
1460 //"signed-integer-overflow",
1461 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1462 // https://llvm.org/PR19302
1463 // http://reviews.llvm.org/D6974
1464 // "object-size",
1465 )
1466 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001467 sanitizers = append(sanitizers, sanProps.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001468 }
1469
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001470 if Bool(sanProps.Diag.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001471 diagSanitizers = append(diagSanitizers, "undefined")
1472 }
1473
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001474 diagSanitizers = append(diagSanitizers, sanProps.Diag.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001475
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001476 if Bool(sanProps.Address) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001477 sanitizers = append(sanitizers, "address")
1478 diagSanitizers = append(diagSanitizers, "address")
1479 }
1480
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001481 if Bool(sanProps.Hwaddress) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001482 sanitizers = append(sanitizers, "hwaddress")
1483 }
1484
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001485 if Bool(sanProps.Thread) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001486 sanitizers = append(sanitizers, "thread")
1487 }
1488
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001489 if Bool(sanProps.Safestack) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001490 sanitizers = append(sanitizers, "safe-stack")
1491 }
1492
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001493 if Bool(sanProps.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001494 sanitizers = append(sanitizers, "cfi")
1495
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001496 if Bool(sanProps.Diag.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001497 diagSanitizers = append(diagSanitizers, "cfi")
1498 }
1499 }
1500
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001501 if Bool(sanProps.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001502 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1503 sanitizers = append(sanitizers, "signed-integer-overflow")
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001504 if Bool(sanProps.Diag.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001505 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1506 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1507 }
1508 }
1509
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001510 if Bool(sanProps.Scudo) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001511 sanitizers = append(sanitizers, "scudo")
1512 }
1513
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001514 if Bool(sanProps.Scs) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001515 sanitizers = append(sanitizers, "shadow-call-stack")
1516 }
1517
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001518 if Bool(sanProps.Memtag_heap) && c.Binary() {
Florian Mayerd8434a42022-08-31 20:57:03 +00001519 sanitizers = append(sanitizers, "memtag-heap")
1520 }
1521
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001522 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +00001523 sanitizers = append(sanitizers, "memtag-stack")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001524 }
1525
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001526 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001527 sanitizers = append(sanitizers, "fuzzer-no-link")
1528 }
1529
Jiyong Park379de2f2018-12-19 02:47:14 +09001530 // Save the list of sanitizers. These will be used again when generating
1531 // the build rules (for Cflags, etc.)
1532 c.sanitize.Properties.Sanitizers = sanitizers
1533 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1534
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001535 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1536 if c.Host() {
1537 diagSanitizers = sanitizers
1538 }
1539
Colin Crosse323a792023-02-15 13:57:57 -08001540 addStaticDeps := func(dep string, hideSymbols bool) {
Colin Cross06c80eb2022-02-10 10:34:19 -08001541 // If we're using snapshots, redirect to snapshot whenever possible
1542 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001543 if lib, ok := snapshot.StaticLibs[dep]; ok {
1544 dep = lib
Colin Cross06c80eb2022-02-10 10:34:19 -08001545 }
1546
1547 // static executable gets static runtime libs
Colin Crosse323a792023-02-15 13:57:57 -08001548 depTag := libraryDependencyTag{Kind: staticLibraryDependency, unexportedSymbols: hideSymbols}
Colin Cross06c80eb2022-02-10 10:34:19 -08001549 variations := append(mctx.Target().Variations(),
1550 blueprint.Variation{Mutator: "link", Variation: "static"})
1551 if c.Device() {
1552 variations = append(variations, c.ImageVariation())
1553 }
1554 if c.UseSdk() {
1555 variations = append(variations,
1556 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1557 }
Colin Crosse323a792023-02-15 13:57:57 -08001558 mctx.AddFarVariationDependencies(variations, depTag, dep)
Colin Cross06c80eb2022-02-10 10:34:19 -08001559 }
Colin Crosse323a792023-02-15 13:57:57 -08001560
1561 // Determine the runtime library required
1562 runtimeSharedLibrary := ""
1563 toolchain := c.toolchain(mctx)
1564 if Bool(sanProps.Address) {
Colin Crossb781d232023-02-15 12:40:20 -08001565 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1566 // Use a static runtime for musl to match what clang does for glibc.
1567 addStaticDeps(config.AddressSanitizerStaticRuntimeLibrary(toolchain), false)
1568 addStaticDeps(config.AddressSanitizerCXXStaticRuntimeLibrary(toolchain), false)
1569 } else {
1570 runtimeSharedLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1571 }
Colin Crosse323a792023-02-15 13:57:57 -08001572 } else if Bool(sanProps.Hwaddress) {
1573 if c.staticBinary() {
1574 addStaticDeps(config.HWAddressSanitizerStaticLibrary(toolchain), true)
1575 addStaticDeps("libdl", false)
1576 } else {
1577 runtimeSharedLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1578 }
1579 } else if Bool(sanProps.Thread) {
1580 runtimeSharedLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1581 } else if Bool(sanProps.Scudo) {
1582 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1583 runtimeSharedLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1584 } else {
1585 runtimeSharedLibrary = config.ScudoRuntimeLibrary(toolchain)
1586 }
1587 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
1588 Bool(sanProps.Fuzzer) ||
1589 Bool(sanProps.Undefined) ||
1590 Bool(sanProps.All_undefined) {
1591 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1592 // Use a static runtime for static binaries.
1593 // Also use a static runtime for musl to match
1594 // what clang does for glibc. Otherwise dlopening
1595 // libraries that depend on libclang_rt.ubsan_standalone.so
1596 // fails with:
1597 // Error relocating ...: initial-exec TLS resolves to dynamic definition
1598 addStaticDeps(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)+".static", true)
1599 } else {
1600 runtimeSharedLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
1601 }
1602 }
1603
Colin Cross06c80eb2022-02-10 10:34:19 -08001604 if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
Colin Crosse323a792023-02-15 13:57:57 -08001605 addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001606 }
1607 if c.sanitize.Properties.BuiltinsDep {
Colin Crosse323a792023-02-15 13:57:57 -08001608 addStaticDeps(config.BuiltinsRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001609 }
1610
Colin Crosse323a792023-02-15 13:57:57 -08001611 if runtimeSharedLibrary != "" && (toolchain.Bionic() || toolchain.Musl() || c.sanitize.Properties.UbsanRuntimeDep) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001612 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001613
1614 // Adding dependency to the runtime library. We are using *FarVariation*
1615 // because the runtime libraries themselves are not mutated by sanitizer
1616 // mutators and thus don't have sanitizer variants whereas this module
1617 // has been already mutated.
1618 //
1619 // Note that by adding dependency with {static|shared}DepTag, the lib is
1620 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
Colin Crosse323a792023-02-15 13:57:57 -08001621 if c.staticBinary() {
1622 // Most sanitizers are either disabled for static binaries or have already
1623 // handled the static binary case above through a direct call to addStaticDeps.
1624 // If not, treat the runtime shared library as a static library and hope for
1625 // the best.
1626 addStaticDeps(runtimeSharedLibrary, true)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001627 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001628 // If we're using snapshots, redirect to snapshot whenever possible
1629 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001630 if lib, ok := snapshot.SharedLibs[runtimeSharedLibrary]; ok {
1631 runtimeSharedLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001632 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001633
Cindy Zhou18417cb2020-12-10 07:12:38 -08001634 // Skip apex dependency check for sharedLibraryDependency
1635 // when sanitizer diags are enabled. Skipping the check will allow
1636 // building with diag libraries without having to list the
1637 // dependency in Apex's allowed_deps file.
1638 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001639 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001640 depTag := libraryDependencyTag{
1641 Kind: sharedLibraryDependency,
1642 Order: earlyLibraryDependency,
1643
1644 skipApexAllowedDependenciesCheck: diagEnabled,
1645 }
Colin Cross42507332020-08-21 16:15:23 -07001646 variations := append(mctx.Target().Variations(),
1647 blueprint.Variation{Mutator: "link", Variation: "shared"})
1648 if c.Device() {
1649 variations = append(variations, c.ImageVariation())
1650 }
Colin Cross06c80eb2022-02-10 10:34:19 -08001651 if c.UseSdk() {
1652 variations = append(variations,
1653 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1654 }
Colin Crosse323a792023-02-15 13:57:57 -08001655 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeSharedLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001656 }
1657 // static lib does not have dependency to the runtime library. The
1658 // dependency will be added to the executables or shared libs using
1659 // the static lib.
1660 }
1661 }
1662}
1663
1664type Sanitizeable interface {
1665 android.Module
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001666 IsSanitizerEnabled(config android.Config, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001667 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001668 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001669}
1670
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001671type JniSanitizeable interface {
1672 android.Module
1673 IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
1674}
1675
Ivan Lozanod7586b62021-04-01 09:49:36 -04001676func (c *Module) MinimalRuntimeDep() bool {
1677 return c.sanitize.Properties.MinimalRuntimeDep
1678}
1679
1680func (c *Module) UbsanRuntimeDep() bool {
1681 return c.sanitize.Properties.UbsanRuntimeDep
1682}
1683
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001684func (c *Module) SanitizePropDefined() bool {
1685 return c.sanitize != nil
1686}
1687
1688func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1689 return c.sanitize.isSanitizerEnabled(t)
1690}
1691
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001692func (c *Module) StaticallyLinked() bool {
1693 return c.static()
1694}
1695
1696func (c *Module) SetInSanitizerDir() {
1697 if c.sanitize != nil {
1698 c.sanitize.Properties.InSanitizerDir = true
1699 }
1700}
1701
1702func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1703 if c.sanitize != nil {
1704 c.sanitize.SetSanitizer(t, b)
1705 }
1706}
1707
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001708var _ PlatformSanitizeable = (*Module)(nil)
1709
Inseob Kim74d25562020-08-04 00:41:38 +09001710type sanitizerStaticLibsMap struct {
1711 // libsMap contains one list of modules per each image and each arch.
1712 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001713 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001714 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001715 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001716}
1717
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001718func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001719 return &sanitizerStaticLibsMap{
1720 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001721 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001722 }
1723}
1724
1725// Add the current module to sanitizer static libs maps
1726// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001727func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1728 image := GetImageVariantType(c)
1729 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001730
1731 s.libsMapLock.Lock()
1732 defer s.libsMapLock.Unlock()
1733
1734 if _, ok := s.libsMap[image]; !ok {
1735 s.libsMap[image] = make(map[string][]string)
1736 }
1737
1738 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1739}
1740
1741// Exports makefile variables in the following format:
1742// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1743// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1744// These are to be used by use_soong_sanitized_static_libraries.
1745// See build/make/core/binary.mk for more details.
1746func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
Cole Faust18994c72023-02-28 16:02:16 -08001747 for _, image := range android.SortedKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001748 archMap := s.libsMap[ImageVariantType(image)]
Cole Faust18994c72023-02-28 16:02:16 -08001749 for _, arch := range android.SortedKeys(archMap) {
Inseob Kim74d25562020-08-04 00:41:38 +09001750 libs := archMap[arch]
1751 sort.Strings(libs)
1752
1753 key := fmt.Sprintf(
1754 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1755 s.sanitizerType.variationName(),
1756 image, // already upper
1757 arch)
1758
1759 ctx.Strict(key, strings.Join(libs, " "))
1760 }
1761 }
1762}
1763
Colin Cross571cccf2019-02-04 11:22:08 -08001764var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1765
Inseob Kim74d25562020-08-04 00:41:38 +09001766func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001767 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001768 return newSanitizerStaticLibsMap(cfi)
1769 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001770}
1771
Colin Cross571cccf2019-02-04 11:22:08 -08001772var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1773
Inseob Kim74d25562020-08-04 00:41:38 +09001774func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001775 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001776 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001777 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001778}
1779
Ivan Lozano30c5db22018-02-21 15:49:20 -08001780func enableMinimalRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001781 if sanitize.isSanitizerEnabled(Asan) {
1782 return false
1783 } else if sanitize.isSanitizerEnabled(Hwasan) {
1784 return false
1785 } else if sanitize.isSanitizerEnabled(Fuzzer) {
1786 return false
Ivan Lozano30c5db22018-02-21 15:49:20 -08001787 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001788
1789 if enableUbsanRuntime(sanitize) {
1790 return false
1791 }
1792
1793 sanitizeProps := &sanitize.Properties.SanitizeMutated
1794 if Bool(sanitizeProps.Diag.Cfi) {
1795 return false
1796 }
1797
1798 return Bool(sanitizeProps.Integer_overflow) ||
1799 len(sanitizeProps.Misc_undefined) > 0 ||
1800 Bool(sanitizeProps.Undefined) ||
1801 Bool(sanitizeProps.All_undefined)
Ivan Lozano30c5db22018-02-21 15:49:20 -08001802}
1803
Ivan Lozanod7586b62021-04-01 09:49:36 -04001804func (m *Module) UbsanRuntimeNeeded() bool {
1805 return enableUbsanRuntime(m.sanitize)
1806}
1807
1808func (m *Module) MinimalRuntimeNeeded() bool {
1809 return enableMinimalRuntime(m.sanitize)
1810}
1811
Inseob Kim8471cda2019-11-15 09:59:12 +09001812func enableUbsanRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001813 sanitizeProps := &sanitize.Properties.SanitizeMutated
1814 return Bool(sanitizeProps.Diag.Integer_overflow) ||
1815 Bool(sanitizeProps.Diag.Undefined) ||
1816 len(sanitizeProps.Diag.Misc_undefined) > 0
Inseob Kim8471cda2019-11-15 09:59:12 +09001817}
1818
Vishwath Mohane7128792017-11-17 11:08:10 -08001819func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001820 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001821}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001822
1823func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001824 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001825}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +00001826
1827func BazelCcSanitizerToolchainVars(config android.Config) string {
1828 return android.BazelToolchainVars(config, exportedVars)
1829}