blob: 281c8c11c02067104517faf142182b77eb41b58a [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
Florian Mayera9984462023-06-16 16:48:51 -070041 // DO NOT ADD MLLVM FLAGS HERE! ADD THEM BELOW TO hwasanCommonFlags.
Yi Kong286abc62021-11-04 16:14:14 +080042 hwasanCflags = []string{
43 "-fno-omit-frame-pointer",
44 "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000045 "-fsanitize-hwaddress-abi=platform",
Yi Kong286abc62021-11-04 16:14:14 +080046 }
47
48 // ThinLTO performs codegen during link time, thus these flags need to
49 // passed to both CFLAGS and LDFLAGS.
50 hwasanCommonflags = []string{
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080051 // The following improves debug location information
52 // availability at the cost of its accuracy. It increases
53 // the likelihood of a stack variable's frame offset
54 // to be recorded in the debug info, which is important
55 // for the quality of hwasan reports. The downside is a
56 // higher number of "optimized out" stack variables.
57 // b/112437883.
Yi Kong286abc62021-11-04 16:14:14 +080058 "-instcombine-lower-dbg-declare=0",
Mitch Phillipsb1c574f2020-06-22 13:28:23 -070059 // TODO(b/159343917): HWASan and GlobalISel don't play nicely, and
60 // GlobalISel is the default at -O0 on aarch64.
Yi Kong286abc62021-11-04 16:14:14 +080061 "--aarch64-enable-global-isel-at-O=-1",
62 "-fast-isel=false",
Florian Mayera9984462023-06-16 16:48:51 -070063 "-hwasan-use-after-scope=1",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080064 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070065
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000066 cfiBlocklistPath = "external/compiler-rt/lib/cfi"
67 cfiBlocklistFilename = "cfi_blocklist.txt"
Trevor Radcliffef1836e42023-06-01 21:12:08 +000068 cfiEnableFlag = "-fsanitize=cfi"
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +000069 cfiCrossDsoFlag = "-fsanitize-cfi-cross-dso"
70 cfiCflags = []string{"-flto", cfiCrossDsoFlag,
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000071 "-fsanitize-ignorelist=" + cfiBlocklistPath + "/" + cfiBlocklistFilename}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070072 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
73 // used, but have no effect on assembly files
74 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Trevor Radcliffef1836e42023-06-01 21:12:08 +000075 cfiLdflags = []string{"-flto", cfiCrossDsoFlag, cfiEnableFlag,
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070076 "-Wl,-plugin-opt,O1"}
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000077 cfiExportsMapPath = "build/soong/cc/config"
78 cfiExportsMapFilename = "cfi_exports.map"
79 cfiAssemblySupportFlag = "-fno-sanitize-cfi-canonical-jump-tables"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070080
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070081 intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080082
Peter Collingbournebd19db02019-03-06 10:38:48 -080083 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070084 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070085 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
Christopher Ferris2fc8e032023-01-26 14:19:27 -080086 "export_memory_stats=0", "max_malloc_fill_size=131072", "malloc_fill_byte=0"}
Florian Mayer1866bbe2023-03-11 01:07:40 +000087 memtagStackCommonFlags = []string{"-march=armv8-a+memtag", "-mllvm", "-dom-tree-reachability-max-bbs-to-explore=128"}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +000088
89 hostOnlySanitizeFlags = []string{"-fno-sanitize-recover=all"}
90 deviceOnlySanitizeFlags = []string{"-fsanitize-trap=all", "-ftrap-function=abort"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070091)
92
Ivan Lozano3968d8f2020-12-14 11:27:52 -050093type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080094
Colin Cross16b23492016-01-06 14:41:07 -080095const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050096 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070097 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080098 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070099 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800100 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500101 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -0400102 Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +0000103 Memtag_stack
Liz Kammer75db9312021-07-07 16:41:50 -0400104 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -0800105)
106
Liz Kammer75db9312021-07-07 16:41:50 -0400107var Sanitizers = []SanitizerType{
108 Asan,
109 Hwasan,
110 tsan,
111 intOverflow,
112 scs,
113 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400114 Memtag_heap,
Florian Mayerd8434a42022-08-31 20:57:03 +0000115 Memtag_stack,
Liz Kammer75db9312021-07-07 16:41:50 -0400116 cfi, // cfi is last to prevent it running before incompatible mutators
117}
118
Jiyong Park82226632019-02-01 10:50:50 +0900119// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500120func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800121 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500122 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800123 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700124 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700125 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800126 case tsan:
127 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700128 case intOverflow:
129 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000130 case cfi:
131 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800132 case scs:
133 return "scs"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400134 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700135 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000136 case Memtag_stack:
137 return "memtag_stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500138 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700139 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800140 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500141 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800142 }
143}
144
Jiyong Park82226632019-02-01 10:50:50 +0900145// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500146func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900147 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500148 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900149 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700150 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900151 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400152 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700153 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000154 case Memtag_stack:
155 return "memtag_stack"
Jiyong Park82226632019-02-01 10:50:50 +0900156 case tsan:
157 return "thread"
158 case intOverflow:
159 return "integer_overflow"
160 case cfi:
161 return "cfi"
162 case scs:
163 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500164 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700165 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900166 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500167 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900168 }
169}
170
Liz Kammer75db9312021-07-07 16:41:50 -0400171func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
172 switch t {
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200173 case cfi, Hwasan, Asan, tsan, Fuzzer, scs:
174 sanitizer := &sanitizerSplitMutator{t}
175 ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
176 ctx.Transition(t.variationName(), sanitizer)
Florian Mayerd8434a42022-08-31 20:57:03 +0000177 case Memtag_heap, Memtag_stack, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400178 // do nothing
179 default:
180 panic(fmt.Errorf("unknown SanitizerType %d", t))
181 }
182}
183
Liz Kammerfd8a49f2022-10-31 10:31:11 -0400184// shouldPropagateToSharedLibraryDeps returns whether a sanitizer type should propagate to share
185// dependencies. In most cases, sanitizers only propagate to static dependencies; however, some
186// sanitizers also must be enabled for shared libraries for linking.
187func (t SanitizerType) shouldPropagateToSharedLibraryDeps() bool {
188 switch t {
189 case Fuzzer:
190 // Typically, shared libs are not split. However, for fuzzer, we split even for shared libs
191 // because a library sanitized for fuzzer can't be linked from a library that isn't sanitized
192 // for fuzzer.
193 return true
194 default:
195 return false
196 }
197}
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500198func (*Module) SanitizerSupported(t SanitizerType) bool {
199 switch t {
200 case Asan:
201 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700202 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500203 return true
204 case tsan:
205 return true
206 case intOverflow:
207 return true
208 case cfi:
209 return true
210 case scs:
211 return true
212 case Fuzzer:
213 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400214 case Memtag_heap:
215 return true
Florian Mayerd8434a42022-08-31 20:57:03 +0000216 case Memtag_stack:
217 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500218 default:
219 return false
220 }
221}
222
223// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
224func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700225 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900226}
227
Martin Stjernholmb0249572020-09-15 02:32:35 +0100228type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400229 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100230 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800231
Liz Kammer75b9b402021-06-25 15:19:27 -0400232 // ASan (Address sanitizer), incompatible with static binaries.
233 // Always runs in a diagnostic mode.
234 // Use of address sanitizer disables cfi sanitizer.
235 // Hwaddress sanitizer takes precedence over this sanitizer.
236 Address *bool `android:"arch_variant"`
237 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
238 // Always runs in a diagnostic mode.
239 // Use of thread sanitizer disables cfi and scudo sanitizers.
240 // Hwaddress sanitizer takes precedence over this sanitizer.
241 Thread *bool `android:"arch_variant"`
242 // HWASan (Hardware Address sanitizer).
243 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100244 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800245
Liz Kammer75b9b402021-06-25 15:19:27 -0400246 // Undefined behavior sanitizer
247 All_undefined *bool `android:"arch_variant"`
248 // Subset of undefined behavior sanitizer
249 Undefined *bool `android:"arch_variant"`
250 // List of specific undefined behavior sanitizers to enable
251 Misc_undefined []string `android:"arch_variant"`
252 // Fuzzer, incompatible with static binaries.
253 Fuzzer *bool `android:"arch_variant"`
254 // safe-stack sanitizer, incompatible with 32-bit architectures.
255 Safestack *bool `android:"arch_variant"`
256 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
257 Cfi *bool `android:"arch_variant"`
258 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
259 Integer_overflow *bool `android:"arch_variant"`
260 // scudo sanitizer, incompatible with asan, hwasan, tsan
261 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
262 // deprecated
263 Scudo *bool `android:"arch_variant"`
Elliott Hughese4793bc2023-02-09 21:15:47 +0000264 // shadow-call-stack sanitizer, only available on arm64/riscv64.
Liz Kammer75b9b402021-06-25 15:19:27 -0400265 Scs *bool `android:"arch_variant"`
266 // Memory-tagging, only available on arm64
267 // if diag.memtag unset or false, enables async memory tagging
Florian Mayer00ab5cf2022-08-31 18:30:18 +0000268 Memtag_heap *bool `android:"arch_variant"`
Florian Mayerd8434a42022-08-31 20:57:03 +0000269 // Memory-tagging stack instrumentation, only available on arm64
270 // Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
271 Memtag_stack *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100272
273 // A modifier for ASAN and HWASAN for write only instrumentation
274 Writeonly *bool `android:"arch_variant"`
275
276 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
277 // Replaces abort() on error with a human-readable error message.
278 // Address and Thread sanitizers always run in diagnostic mode.
279 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400280 // Undefined behavior sanitizer, diagnostic mode
281 Undefined *bool `android:"arch_variant"`
282 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
283 Cfi *bool `android:"arch_variant"`
284 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
285 Integer_overflow *bool `android:"arch_variant"`
286 // Memory-tagging, only available on arm64
287 // requires sanitizer.memtag: true
288 // if set, enables sync memory tagging
289 Memtag_heap *bool `android:"arch_variant"`
290 // List of specific undefined behavior sanitizers to enable in diagnostic mode
291 Misc_undefined []string `android:"arch_variant"`
292 // List of sanitizers to pass to -fno-sanitize-recover
293 // results in only the first detected error for these sanitizers being reported and program then
294 // exits with a non-zero exit code.
295 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800296 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800297
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800298 // Sanitizers to run with flag configuration specified
299 Config struct {
300 // Enables CFI support flags for assembly-heavy libraries
301 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800302 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800303
Liz Kammer75b9b402021-06-25 15:19:27 -0400304 // List of sanitizers to pass to -fsanitize-recover
305 // allows execution to continue for these sanitizers to detect multiple errors rather than only
306 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100307 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000308
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700309 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100310 Blocklist *string
311}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700312
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400313type sanitizeMutatedProperties struct {
314 // Whether sanitizers can be enabled on this module
315 Never *bool `blueprint:"mutated"`
316
317 // Whether ASan (Address sanitizer) is enabled for this module.
318 // Hwaddress sanitizer takes precedence over this sanitizer.
319 Address *bool `blueprint:"mutated"`
320 // Whether TSan (Thread sanitizer) is enabled for this module
321 Thread *bool `blueprint:"mutated"`
322 // Whether HWASan (Hardware Address sanitizer) is enabled for this module
323 Hwaddress *bool `blueprint:"mutated"`
324
325 // Whether Undefined behavior sanitizer is enabled for this module
326 All_undefined *bool `blueprint:"mutated"`
327 // Whether undefined behavior sanitizer subset is enabled for this module
328 Undefined *bool `blueprint:"mutated"`
329 // List of specific undefined behavior sanitizers enabled for this module
330 Misc_undefined []string `blueprint:"mutated"`
331 // Whether Fuzzeris enabled for this module
332 Fuzzer *bool `blueprint:"mutated"`
333 // whether safe-stack sanitizer is enabled for this module
334 Safestack *bool `blueprint:"mutated"`
335 // Whether cfi sanitizer is enabled for this module
336 Cfi *bool `blueprint:"mutated"`
337 // Whether signed/unsigned integer overflow sanitizer is enabled for this module
338 Integer_overflow *bool `blueprint:"mutated"`
339 // Whether scudo sanitizer is enabled for this module
340 Scudo *bool `blueprint:"mutated"`
341 // Whether shadow-call-stack sanitizer is enabled for this module.
342 Scs *bool `blueprint:"mutated"`
343 // Whether Memory-tagging is enabled for this module
344 Memtag_heap *bool `blueprint:"mutated"`
345 // Whether Memory-tagging stack instrumentation is enabled for this module
346 Memtag_stack *bool `blueprint:"mutated"`
347
348 // Whether a modifier for ASAN and HWASAN for write only instrumentation is enabled for this
349 // module
350 Writeonly *bool `blueprint:"mutated"`
351
352 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
353 Diag struct {
354 // Whether Undefined behavior sanitizer, diagnostic mode is enabled for this module
355 Undefined *bool `blueprint:"mutated"`
356 // Whether cfi sanitizer, diagnostic mode is enabled for this module
357 Cfi *bool `blueprint:"mutated"`
358 // Whether signed/unsigned integer overflow sanitizer, diagnostic mode is enabled for this
359 // module
360 Integer_overflow *bool `blueprint:"mutated"`
361 // Whether Memory-tagging, diagnostic mode is enabled for this module
362 Memtag_heap *bool `blueprint:"mutated"`
363 // List of specific undefined behavior sanitizers enabled in diagnostic mode
364 Misc_undefined []string `blueprint:"mutated"`
365 } `blueprint:"mutated"`
366}
367
Martin Stjernholmb0249572020-09-15 02:32:35 +0100368type SanitizeProperties struct {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400369 Sanitize SanitizeUserProps `android:"arch_variant"`
370 SanitizeMutated sanitizeMutatedProperties `blueprint:"mutated"`
371
372 SanitizerEnabled bool `blueprint:"mutated"`
373 MinimalRuntimeDep bool `blueprint:"mutated"`
374 BuiltinsDep bool `blueprint:"mutated"`
375 UbsanRuntimeDep bool `blueprint:"mutated"`
376 InSanitizerDir bool `blueprint:"mutated"`
377 Sanitizers []string `blueprint:"mutated"`
378 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800379}
380
381type sanitize struct {
382 Properties SanitizeProperties
383}
384
Cindy Zhou18417cb2020-12-10 07:12:38 -0800385// Mark this tag with a check to see if apex dependency check should be skipped
386func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
387 return t.skipApexAllowedDependenciesCheck
388}
389
390var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
391
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000392var exportedVars = android.NewExportedVariables(pctx)
393
Vishwath Mohane7128792017-11-17 11:08:10 -0800394func init() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000395 exportedVars.ExportStringListStaticVariable("HostOnlySanitizeFlags", hostOnlySanitizeFlags)
396 exportedVars.ExportStringList("DeviceOnlySanitizeFlags", deviceOnlySanitizeFlags)
397
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000398 // Leave out "-flto" from the slices exported to bazel, as we will use the
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000399 // dedicated LTO feature for this. For C Flags and Linker Flags, also leave
Trevor Radcliffef1836e42023-06-01 21:12:08 +0000400 // out the cross DSO flag which will be added separately under the correct conditions.
401 exportedVars.ExportStringList("CfiCFlags", append(cfiCflags[2:], cfiEnableFlag))
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000402 exportedVars.ExportStringList("CfiLdFlags", cfiLdflags[2:])
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000403 exportedVars.ExportStringList("CfiAsFlags", cfiAsflags[1:])
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000404
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000405 exportedVars.ExportString("CfiCrossDsoFlag", cfiCrossDsoFlag)
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000406 exportedVars.ExportString("CfiBlocklistPath", cfiBlocklistPath)
407 exportedVars.ExportString("CfiBlocklistFilename", cfiBlocklistFilename)
408 exportedVars.ExportString("CfiExportsMapPath", cfiExportsMapPath)
409 exportedVars.ExportString("CfiExportsMapFilename", cfiExportsMapFilename)
410 exportedVars.ExportString("CfiAssemblySupportFlag", cfiAssemblySupportFlag)
411
Vishwath Mohane7128792017-11-17 11:08:10 -0800412 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700413 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800414}
415
Colin Cross16b23492016-01-06 14:41:07 -0800416func (sanitize *sanitize) props() []interface{} {
417 return []interface{}{&sanitize.Properties}
418}
419
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400420func (p *sanitizeMutatedProperties) copyUserPropertiesToMutated(userProps *SanitizeUserProps) {
421 p.Never = userProps.Never
422 p.Address = userProps.Address
423 p.All_undefined = userProps.All_undefined
424 p.Cfi = userProps.Cfi
425 p.Fuzzer = userProps.Fuzzer
426 p.Hwaddress = userProps.Hwaddress
427 p.Integer_overflow = userProps.Integer_overflow
428 p.Memtag_heap = userProps.Memtag_heap
429 p.Memtag_stack = userProps.Memtag_stack
430 p.Safestack = userProps.Safestack
431 p.Scs = userProps.Scs
432 p.Scudo = userProps.Scudo
433 p.Thread = userProps.Thread
434 p.Undefined = userProps.Undefined
435 p.Writeonly = userProps.Writeonly
436
437 p.Misc_undefined = make([]string, 0, len(userProps.Misc_undefined))
438 for _, v := range userProps.Misc_undefined {
439 p.Misc_undefined = append(p.Misc_undefined, v)
440 }
441
442 p.Diag.Cfi = userProps.Diag.Cfi
443 p.Diag.Integer_overflow = userProps.Diag.Integer_overflow
444 p.Diag.Memtag_heap = userProps.Diag.Memtag_heap
445 p.Diag.Undefined = userProps.Diag.Undefined
446
447 p.Diag.Misc_undefined = make([]string, 0, len(userProps.Diag.Misc_undefined))
448 for _, v := range userProps.Diag.Misc_undefined {
449 p.Diag.Misc_undefined = append(p.Diag.Misc_undefined, v)
450 }
451}
452
Colin Cross16b23492016-01-06 14:41:07 -0800453func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400454 s := &sanitize.Properties.SanitizeMutated
455 s.copyUserPropertiesToMutated(&sanitize.Properties.Sanitize)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700456
Colin Cross16b23492016-01-06 14:41:07 -0800457 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700458 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800459 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800460 }
461
462 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800463 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800464 return
465 }
466
Florian Mayerd8434a42022-08-31 20:57:03 +0000467 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400468 if ctx.testBinary() {
469 if s.Memtag_heap == nil {
470 s.Memtag_heap = proptools.BoolPtr(true)
471 }
472 if s.Diag.Memtag_heap == nil {
473 s.Diag.Memtag_heap = proptools.BoolPtr(true)
474 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800475 }
476
Colin Cross16b23492016-01-06 14:41:07 -0800477 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700478 var globalSanitizersDiag []string
479
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700480 if ctx.Host() {
481 if !ctx.Windows() {
482 globalSanitizers = ctx.Config().SanitizeHost()
483 }
484 } else {
485 arches := ctx.Config().SanitizeDeviceArch()
486 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
487 globalSanitizers = ctx.Config().SanitizeDevice()
488 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800489 }
490 }
491
Colin Cross16b23492016-01-06 14:41:07 -0800492 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000493 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700494 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400495 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000496 }
Colin Cross16b23492016-01-06 14:41:07 -0800497
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700498 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400499 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000500 }
501
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700502 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400503 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000504 }
505
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700506 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400507 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000508 }
509
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700510 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400511 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700512 }
513
514 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400515 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000516 }
517
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700518 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800519 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400520 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700521 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700522 }
523
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700524 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700525 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700526 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400527 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700528 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700529 }
530
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700531 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400532 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700533 }
534
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700535 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400536 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700537 }
538
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000539 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
540 // Hwaddress and Address are set before, so we can check them here
541 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
542 if s.Address == nil && s.Hwaddress == nil {
543 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
544 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400545 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000546 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700547 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800548 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400549 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800550 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700551 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000552
Florian Mayerd8434a42022-08-31 20:57:03 +0000553 if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
554 s.Memtag_stack = proptools.BoolPtr(true)
555 }
556
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000557 if len(globalSanitizers) > 0 {
558 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
559 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700560
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700561 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700562 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700563 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400564 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700565 }
566
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700567 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
568 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400569 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700570 }
571
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800572 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
573 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400574 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800575 }
576
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700577 if len(globalSanitizersDiag) > 0 {
578 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
579 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700580 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700581
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800582 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross88a029f2022-06-23 14:51:20 -0700583 if ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800584 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800585 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400586 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800587 }
588 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400589 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800590 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800591 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800592 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400593 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800594 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800595 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700596 }
597
Hang Lua98aab92023-03-17 13:17:22 +0800598 // Enable HWASan for all components in the include paths (for Aarch64 only)
599 if s.Hwaddress == nil && ctx.Config().HWASanEnabledForPath(ctx.ModuleDir()) &&
600 ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
601 s.Hwaddress = proptools.BoolPtr(true)
602 }
603
Elvis Chien9c993542021-06-25 01:15:17 +0800604 // Enable CFI for non-host components in the include paths
605 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400606 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000607 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400608 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700609 }
610 }
611
Elliott Hughesda3a0712020-03-06 16:55:28 -0800612 // Is CFI actually enabled?
613 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400614 s.Cfi = nil
615 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800616 }
617
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700618 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross88a029f2022-06-23 14:51:20 -0700619 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700620 s.Hwaddress = nil
621 }
622
Elliott Hughese4793bc2023-02-09 21:15:47 +0000623 // SCS is only implemented on AArch64/riscv64.
624 if (ctx.Arch().ArchType != android.Arm64 && ctx.Arch().ArchType != android.Riscv64) || !ctx.toolchain().Bionic() {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800625 s.Scs = nil
626 }
Elliott Hughes5beb42f2023-04-12 13:08:58 -0700627 // ...but temporarily globally disabled on riscv64 (http://b/277909695).
628 if ctx.Arch().ArchType == android.Riscv64 {
629 s.Scs = nil
630 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800631
Ivan Lozano62cd0382021-11-01 10:27:54 -0400632 // Memtag_heap is only implemented on AArch64.
Florian Mayerd8434a42022-08-31 20:57:03 +0000633 // Memtag ABI is Android specific for now, so disable for host.
634 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() || ctx.Host() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700635 s.Memtag_heap = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000636 s.Memtag_stack = nil
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700637 }
638
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700639 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700640 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400641 s.Cfi = nil
642 s.Diag.Cfi = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000643 // HWASAN and ASAN win against MTE.
644 s.Memtag_heap = nil
645 s.Memtag_stack = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700646 }
647
Colin Crossed12a042022-02-07 13:55:55 -0800648 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
649 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400650 s.Cfi = nil
651 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700652 s.Misc_undefined = nil
653 s.Undefined = nil
654 s.All_undefined = nil
655 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800656 }
657
Aditya Kumar1281b992023-05-16 01:15:24 +0000658 // TODO(b/254713216): CFI doesn't work for riscv64 yet because LTO doesn't work.
659 if ctx.Arch().ArchType == android.Riscv64 {
660 s.Cfi = nil
661 s.Diag.Cfi = nil
662 }
663
Colin Crossed12a042022-02-07 13:55:55 -0800664 // Disable CFI for musl
665 if ctx.toolchain().Musl() {
666 s.Cfi = nil
667 s.Diag.Cfi = nil
668 }
669
Colin Cross390fc742023-05-02 13:02:51 -0700670 // TODO(b/280478629): runtimes don't exist for musl arm64 yet.
671 if ctx.toolchain().Musl() && ctx.Arch().ArchType == android.Arm64 {
672 s.Address = nil
673 s.Hwaddress = nil
674 s.Thread = nil
675 s.Scudo = nil
676 s.Fuzzer = nil
677 s.Cfi = nil
678 s.Diag.Cfi = nil
679 s.Misc_undefined = nil
680 s.Undefined = nil
681 s.All_undefined = nil
682 s.Integer_overflow = nil
683 }
684
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700685 // Also disable CFI for VNDK variants of components
686 if ctx.isVndk() && ctx.useVndk() {
Justin Yun08270c62022-12-19 17:01:26 +0900687 s.Cfi = nil
688 s.Diag.Cfi = nil
Inseob Kimeec88e12020-01-22 11:11:29 +0900689 }
690
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700691 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700692 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
693 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700694 s.Hwaddress = nil
695 }
696
Colin Cross3c344ef2016-07-18 15:44:56 -0700697 if ctx.staticBinary() {
698 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700699 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700700 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800701 }
702
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700703 if Bool(s.All_undefined) {
704 s.Undefined = nil
705 }
706
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700707 if !ctx.toolchain().Is64Bit() {
708 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700709 s.Thread = nil
710 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800711 // TODO(ccross): error for compile_multilib = "32"?
712 }
713
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800714 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 -0700715 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 +0000716 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700717 sanitize.Properties.SanitizerEnabled = true
718 }
719
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800720 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
721 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700722 s.Scudo = nil
723 }
724
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700725 if Bool(s.Hwaddress) {
726 s.Address = nil
727 s.Thread = nil
728 }
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000729
730 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
731 // mutually incompatible.
732 if Bool(s.Fuzzer) {
733 s.Cfi = nil
734 }
Colin Cross16b23492016-01-06 14:41:07 -0800735}
736
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800737func toDisableImplicitIntegerChange(flags []string) bool {
738 // Returns true if any flag is fsanitize*integer, and there is
739 // no explicit flag about sanitize=implicit-integer-sign-change.
740 for _, f := range flags {
741 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
742 return false
743 }
744 }
745 for _, f := range flags {
746 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
747 return true
748 }
749 }
750 return false
751}
752
Yabin Cuidb7dda82020-11-30 15:47:45 -0800753func toDisableUnsignedShiftBaseChange(flags []string) bool {
754 // Returns true if any flag is fsanitize*integer, and there is
755 // no explicit flag about sanitize=unsigned-shift-base.
756 for _, f := range flags {
757 if strings.Contains(f, "sanitize=unsigned-shift-base") {
758 return false
759 }
760 }
761 for _, f := range flags {
762 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
763 return true
764 }
765 }
766 return false
767}
768
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400769func (s *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
770 if !s.Properties.SanitizerEnabled && !s.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800771 return flags
772 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400773 sanProps := &s.Properties.SanitizeMutated
Colin Cross16b23492016-01-06 14:41:07 -0800774
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400775 if Bool(sanProps.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700776 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800777 // Frame pointer based unwinder in ASan requires ARM frame setup.
778 // TODO: put in flags?
779 flags.RequiredInstructionSet = "arm"
780 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800781 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
782 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800783
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400784 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000785 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
786 }
787
Colin Cross16b23492016-01-06 14:41:07 -0800788 if ctx.Host() {
789 // -nodefaultlibs (provided with libc++) prevents the driver from linking
790 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800791 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800792 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800793 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900794 if ctx.bootstrap() {
795 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
796 } else {
797 flags.DynamicLinker = "/system/bin/linker_asan"
798 }
Colin Cross16b23492016-01-06 14:41:07 -0800799 if flags.Toolchain.Is64Bit() {
800 flags.DynamicLinker += "64"
801 }
802 }
Colin Cross16b23492016-01-06 14:41:07 -0800803 }
804
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400805 if Bool(sanProps.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800806 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800807
808 for _, flag := range hwasanCommonflags {
809 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
810 }
811 for _, flag := range hwasanCommonflags {
812 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
813 }
814
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400815 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000816 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
817 }
Florian Mayer95cd6db2023-03-23 17:48:07 -0700818 if !ctx.staticBinary() && !ctx.Host() {
819 if ctx.bootstrap() {
820 flags.DynamicLinker = "/system/bin/bootstrap/linker_hwasan64"
821 } else {
822 flags.DynamicLinker = "/system/bin/linker_hwasan64"
823 }
824 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700825 }
826
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400827 if Bool(sanProps.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800828 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700829
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000830 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
831 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
832 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
833 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
834 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
835
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700836 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
837 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
838 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800839 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
840 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700841
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700842 // Disable fortify for fuzzing builds. Generally, we'll be building with
843 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800844 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800845
846 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
847 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
848 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
849 // the DT_RUNPATH from the shared library above it, and not the executable,
850 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
851 // DT_RUNPATH here means that transient shared libraries can be found
852 // colocated with their parents.
853 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800854 }
855
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400856 if Bool(sanProps.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800857 if ctx.Arch().ArchType == android.Arm {
858 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
859 // to do this on a function basis, so force Thumb on the entire module.
860 flags.RequiredInstructionSet = "thumb"
861 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000862
Colin Cross4af21ed2019-11-04 09:37:55 -0800863 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
864 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400865 if Bool(s.Properties.Sanitize.Config.Cfi_assembly_support) {
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000866 flags.Local.CFlags = append(flags.Local.CFlags, cfiAssemblySupportFlag)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800867 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000868 // Only append the default visibility flag if -fvisibility has not already been set
869 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800870 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
871 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000872 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800873 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000874
875 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800876 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
877 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000878 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700879 }
880
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400881 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000882 flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
Florian Mayer25cd9812023-03-21 16:13:36 +0000883 // TODO(fmayer): remove -Wno-error once https://reviews.llvm.org/D127917 is in Android toolchain.
884 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-error=frame-larger-than")
Florian Mayerd8434a42022-08-31 20:57:03 +0000885 flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
886 flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
Florian Mayer25cd9812023-03-21 16:13:36 +0000887 // This works around LLD complaining about the stack frame size.
888 // TODO(fmayer): remove once https://reviews.llvm.org/D127917 is in Android toolchain.
889 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-fatal-warnings")
Florian Mayerd8434a42022-08-31 20:57:03 +0000890 }
891
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400892 if (Bool(sanProps.Memtag_heap) || Bool(sanProps.Memtag_stack)) && ctx.binary() {
893 if Bool(sanProps.Diag.Memtag_heap) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000894 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
895 } else {
896 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
897 }
898 }
899
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400900 if Bool(sanProps.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800901 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700902 }
903
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400904 if len(s.Properties.Sanitizers) > 0 {
905 sanitizeArg := "-fsanitize=" + strings.Join(s.Properties.Sanitizers, ",")
Colin Cross4af21ed2019-11-04 09:37:55 -0800906 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
907 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross234b01d2022-02-07 13:49:03 -0800908 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
909
Colin Crossed12a042022-02-07 13:55:55 -0800910 if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
911 // Bionic and musl sanitizer runtimes have already been added as dependencies so that
912 // the right variant of the runtime will be used (with the "-android" or "-musl"
913 // suffixes), so don't let clang the runtime library.
Colin Cross234b01d2022-02-07 13:49:03 -0800914 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-link-runtime")
915 } else {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800916 // Host sanitizers only link symbols in the final executable, so
917 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800918 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
Colin Cross6c18d002022-06-02 15:11:50 -0700919 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500920
Colin Cross6c18d002022-06-02 15:11:50 -0700921 if !ctx.toolchain().Bionic() {
922 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function san.
923 // Musl toolchain prebuilts have vptr and function sanitizers, but enabling them
924 // implicitly enables RTTI which causes RTTI mismatch issues with dependencies.
925
Colin Cross234b01d2022-02-07 13:49:03 -0800926 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500927 }
928
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400929 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700930 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800931 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700932 } else if ctx.Host() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000933 flags.Local.CFlags = append(flags.Local.CFlags, hostOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700934 } else {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000935 flags.Local.CFlags = append(flags.Local.CFlags, deviceOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700936 }
Evgenii Stepanov59012812022-06-24 11:09:18 -0700937
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400938 if enableMinimalRuntime(s) {
Evgenii Stepanov59012812022-06-24 11:09:18 -0700939 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
940 }
941
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800942 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800943 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
944 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800945 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800946 // http://b/171275751, Android doesn't build with this sanitizer yet.
947 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
948 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
949 }
Colin Cross16b23492016-01-06 14:41:07 -0800950 }
951
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400952 if len(s.Properties.DiagSanitizers) > 0 {
953 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(s.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700954 }
955 // FIXME: enable RTTI if diag + (cfi or vptr)
956
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400957 if s.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800958 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400959 strings.Join(s.Properties.Sanitize.Recover, ","))
Andreas Gampe97071162017-05-08 13:15:23 -0700960 }
961
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400962 if s.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800963 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400964 strings.Join(s.Properties.Sanitize.Diag.No_recover, ","))
Ivan Lozano7929bba2018-12-12 09:36:31 -0800965 }
966
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400967 blocklist := android.OptionalPathForModuleSrc(ctx, s.Properties.Sanitize.Blocklist)
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700968 if blocklist.Valid() {
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700969 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-ignorelist="+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700970 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
971 }
972
Colin Cross16b23492016-01-06 14:41:07 -0800973 return flags
974}
975
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400976func (s *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900977 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
978 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800979 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400980 if Bool(s.Properties.SanitizeMutated.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800981 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900982 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400983 if Bool(s.Properties.SanitizeMutated.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800984 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900985 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400986 if Bool(s.Properties.SanitizeMutated.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800987 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900988 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800989 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700990}
991
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400992func (s *sanitize) inSanitizerDir() bool {
993 return s.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700994}
995
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500996// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400997func (s *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000998 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500999 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001000 return s.Properties.SanitizeMutated.Address
Tri Vo6eafc362021-04-01 11:29:09 -07001001 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001002 return s.Properties.SanitizeMutated.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +00001003 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001004 return s.Properties.SanitizeMutated.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +00001005 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001006 return s.Properties.SanitizeMutated.Integer_overflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001007 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001008 return s.Properties.SanitizeMutated.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001009 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001010 return s.Properties.SanitizeMutated.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -04001011 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001012 return s.Properties.SanitizeMutated.Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +00001013 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001014 return s.Properties.SanitizeMutated.Memtag_stack
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001015 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001016 return s.Properties.SanitizeMutated.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +00001017 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001018 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +00001019 }
1020}
1021
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001022// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -08001023func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001024 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001025 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -08001026 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001027 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001028 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -04001029 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Florian Mayerd8434a42022-08-31 20:57:03 +00001030 !sanitize.isSanitizerEnabled(Memtag_stack) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001031 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -08001032}
1033
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001034// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001035func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001036 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001037 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001038 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001039 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001040}
1041
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001042func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -04001043 bPtr := proptools.BoolPtr(b)
1044 if !b {
1045 bPtr = nil
1046 }
Colin Cross16b23492016-01-06 14:41:07 -08001047 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001048 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001049 sanitize.Properties.SanitizeMutated.Address = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001050 // For ASAN variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001051 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Tri Vo6eafc362021-04-01 11:29:09 -07001052 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001053 sanitize.Properties.SanitizeMutated.Hwaddress = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001054 // For HWAsan variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001055 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Colin Cross16b23492016-01-06 14:41:07 -08001056 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001057 sanitize.Properties.SanitizeMutated.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -07001058 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001059 sanitize.Properties.SanitizeMutated.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001060 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001061 sanitize.Properties.SanitizeMutated.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001062 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001063 sanitize.Properties.SanitizeMutated.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -04001064 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001065 sanitize.Properties.SanitizeMutated.Memtag_heap = bPtr
Florian Mayerd8434a42022-08-31 20:57:03 +00001066 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001067 sanitize.Properties.SanitizeMutated.Memtag_stack = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001068 // We do not need to disable ASAN or HWASan here, as there is no Memtag_stack variant.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001069 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001070 sanitize.Properties.SanitizeMutated.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -08001071 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001072 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -08001073 }
1074 if b {
1075 sanitize.Properties.SanitizerEnabled = true
1076 }
1077}
1078
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001079// Check if the sanitizer is explicitly disabled (as opposed to nil by
1080// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001081func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001082 if sanitize == nil {
1083 return false
1084 }
1085
1086 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1087 return sanitizerVal != nil && *sanitizerVal == false
1088}
1089
1090// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
1091// because enabling a sanitizer either directly (via the blueprint) or
1092// indirectly (via a mutator) sets the bool ptr to true, and you can't
1093// distinguish between the cases. It isn't needed though - both cases can be
1094// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001095func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001096 if sanitize == nil {
1097 return false
1098 }
1099
1100 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1101 return sanitizerVal != nil && *sanitizerVal == true
1102}
1103
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001104// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
1105func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -07001106 switch t := tag.(type) {
1107 case dependencyTag:
1108 return t == reuseObjTag || t == objDepTag
1109 case libraryDependencyTag:
1110 return true
1111 default:
1112 return false
1113 }
Colin Cross6b753602018-06-21 13:03:07 -07001114}
1115
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001116func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
1117 return IsSanitizableDependencyTag
1118}
1119
Inseob Kimc42f2f22020-07-29 20:32:10 +09001120// Determines if the current module is a static library going to be captured
1121// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
1122// except for ones which explicitly disable cfi.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001123func needsCfiForVendorSnapshot(mctx android.BaseModuleContext) bool {
Justin Yun8814fc52022-12-15 21:45:35 +09001124 if inList("hwaddress", mctx.Config().SanitizeDevice()) {
1125 // cfi will not be built if SANITIZE_TARGET=hwaddress is set
1126 return false
1127 }
1128
Kiyoung Kim48f37782021-07-07 12:42:39 +09001129 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001130 return false
1131 }
1132
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001133 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001134
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001135 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001136 return false
1137 }
1138
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001139 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001140 return false
1141 }
1142
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001143 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001144 return false
1145 }
1146
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001147 if !c.SanitizerSupported(cfi) {
1148 return false
1149 }
1150
1151 return c.SanitizePropDefined() &&
1152 !c.SanitizeNever() &&
1153 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001154}
1155
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001156type sanitizerSplitMutator struct {
1157 sanitizer SanitizerType
1158}
1159
1160// If an APEX is sanitized or not depends on whether it contains at least one
1161// sanitized module. Transition mutators cannot propagate information up the
1162// dependency graph this way, so we need an auxiliary mutator to do so.
1163func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
1164 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1165 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1166 ctx.VisitDirectDeps(func(dep android.Module) {
1167 if c, ok := dep.(*Module); ok && c.sanitize.isSanitizerEnabled(s.sanitizer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001168 enabled = true
Inseob Kimc42f2f22020-07-29 20:32:10 +09001169 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001170 })
1171
1172 if enabled {
1173 sanitizeable.EnableSanitizer(s.sanitizer.name())
1174 }
1175 }
1176}
1177
1178func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
1179 if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1180 if s.sanitizer == cfi && needsCfiForVendorSnapshot(ctx) {
1181 return []string{"", s.sanitizer.variationName()}
1182 }
1183
1184 // If the given sanitizer is not requested in the .bp file for a module, it
1185 // won't automatically build the sanitized variation.
1186 if !c.IsSanitizerEnabled(s.sanitizer) {
1187 return []string{""}
1188 }
1189
1190 if c.Binary() {
1191 // If a sanitizer is enabled for a binary, we do not build the version
1192 // without the sanitizer
1193 return []string{s.sanitizer.variationName()}
1194 } else if c.StaticallyLinked() || c.Header() {
1195 // For static libraries, we build both versions. Some Make modules
1196 // apparently depend on this behavior.
1197 return []string{"", s.sanitizer.variationName()}
1198 } else {
1199 // We only build the requested variation of dynamic libraries
1200 return []string{s.sanitizer.variationName()}
1201 }
1202 }
1203
1204 if _, ok := ctx.Module().(JniSanitizeable); ok {
1205 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1206 // that is short-circuited for now
1207 return []string{""}
1208 }
1209
1210 // If an APEX has a sanitized dependency, we build the APEX in the sanitized
1211 // variation. This is useful because such APEXes require extra dependencies.
1212 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1213 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1214 if enabled {
1215 return []string{s.sanitizer.variationName()}
1216 } else {
1217 return []string{""}
1218 }
1219 }
1220
1221 if c, ok := ctx.Module().(*Module); ok {
1222 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1223
1224 // Check if it's a snapshot module supporting sanitizer
Justin Yun08270c62022-12-19 17:01:26 +09001225 if ss, ok := c.linker.(snapshotSanitizer); ok {
1226 if ss.isSanitizerAvailable(s.sanitizer) {
1227 return []string{"", s.sanitizer.variationName()}
1228 } else {
1229 return []string{""}
1230 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001231 }
1232 }
1233
1234 return []string{""}
1235}
1236
1237func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1238 if c, ok := ctx.Module().(PlatformSanitizeable); ok {
1239 if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
1240 // If the dependency is through a non-sanitizable tag, use the
1241 // non-sanitized variation
1242 return ""
1243 }
1244
1245 return sourceVariation
1246 } else if _, ok := ctx.Module().(JniSanitizeable); ok {
1247 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1248 // that is short-circuited for now
1249 return ""
1250 } else {
1251 // Otherwise, do not rock the boat.
1252 return sourceVariation
1253 }
1254}
1255
1256func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1257 if d, ok := ctx.Module().(PlatformSanitizeable); ok {
1258 if dm, ok := ctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001259 if ss, ok := dm.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001260 return incomingVariation
Inseob Kimc42f2f22020-07-29 20:32:10 +09001261 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001262 }
1263
1264 if !d.SanitizePropDefined() ||
1265 d.SanitizeNever() ||
1266 d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
1267 !d.SanitizerSupported(s.sanitizer) {
1268 // If a module opts out of a sanitizer, use its non-sanitized variation
1269 return ""
1270 }
1271
1272 // Binaries are always built in the variation they requested.
1273 if d.Binary() {
1274 if d.IsSanitizerEnabled(s.sanitizer) {
1275 return s.sanitizer.variationName()
1276 } else {
1277 return ""
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001278 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001279 }
1280
1281 // If a shared library requests to be sanitized, it will be built for that
1282 // sanitizer. Otherwise, some sanitizers propagate through shared library
1283 // dependency edges, some do not.
1284 if !d.StaticallyLinked() && !d.Header() {
1285 if d.IsSanitizerEnabled(s.sanitizer) {
1286 return s.sanitizer.variationName()
1287 }
1288
Liz Kammerfd8a49f2022-10-31 10:31:11 -04001289 // Some sanitizers do not propagate to shared dependencies
1290 if !s.sanitizer.shouldPropagateToSharedLibraryDeps() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001291 return ""
1292 }
1293 }
1294
1295 // Static and header libraries inherit whether they are sanitized from the
1296 // module they are linked into
1297 return incomingVariation
1298 } else if d, ok := ctx.Module().(Sanitizeable); ok {
1299 // If an APEX contains a sanitized module, it will be built in the variation
1300 // corresponding to that sanitizer.
1301 enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1302 if enabled {
1303 return s.sanitizer.variationName()
1304 }
1305
1306 return incomingVariation
1307 }
1308
1309 return ""
1310}
1311
1312func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
1313 sanitizerVariation := variationName == s.sanitizer.variationName()
1314
1315 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1316 sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
1317
1318 oneMakeVariation := false
1319 if c.StaticallyLinked() || c.Header() {
1320 if s.sanitizer != cfi && s.sanitizer != scs && s.sanitizer != Hwasan {
1321 // These sanitizers export only one variation to Make. For the rest,
1322 // Make targets can depend on both the sanitized and non-sanitized
1323 // versions.
1324 oneMakeVariation = true
1325 }
1326 } else if !c.Binary() {
1327 // Shared library. These are the sanitizers that do propagate through shared
1328 // library dependencies and therefore can cause multiple variations of a
1329 // shared library to be built.
1330 if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != scs && s.sanitizer != Asan {
1331 oneMakeVariation = true
1332 }
1333 }
1334
1335 if oneMakeVariation {
1336 if sanitizerEnabled != sanitizerVariation {
1337 c.SetPreventInstall()
1338 c.SetHideFromMake()
1339 }
1340 }
1341
1342 if sanitizerVariation {
1343 c.SetSanitizer(s.sanitizer, true)
1344
1345 // CFI is incompatible with ASAN so disable it in ASAN variations
1346 if s.sanitizer.incompatibleWithCfi() {
1347 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1348 if mctx.Device() && cfiSupported {
1349 c.SetSanitizer(cfi, false)
Jiyong Parkf97782b2019-02-13 20:28:58 +09001350 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001351 }
1352
1353 // locate the asan libraries under /data/asan
1354 if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
1355 c.SetInSanitizerDir()
1356 }
1357
1358 if c.StaticallyLinked() && c.ExportedToMake() {
1359 if s.sanitizer == Hwasan {
1360 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
1361 } else if s.sanitizer == cfi {
1362 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
1363 }
1364 }
1365 } else if c.IsSanitizerEnabled(s.sanitizer) {
1366 // Disable the sanitizer for the non-sanitized variation
1367 c.SetSanitizer(s.sanitizer, false)
1368 }
1369 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
1370 // If an APEX has sanitized dependencies, it gets a few more dependencies
1371 if sanitizerVariation {
1372 sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
1373 }
1374 } else if c, ok := mctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001375 if ss, ok := c.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
1376 if !ss.isUnsanitizedVariant() {
1377 // Snapshot sanitizer may have only one variantion.
1378 // Skip exporting the module if it already has a sanitizer variation.
1379 c.SetPreventInstall()
1380 c.SetHideFromMake()
1381 return
1382 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001383 c.linker.(snapshotSanitizer).setSanitizerVariation(s.sanitizer, sanitizerVariation)
1384
1385 // Export the static lib name to make
1386 if c.static() && c.ExportedToMake() {
Justin Yun39c30312022-11-23 16:20:12 +09001387 // use BaseModuleName which is the name for Make.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001388 if s.sanitizer == cfi {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001389 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Justin Yun39c30312022-11-23 16:20:12 +09001390 } else if s.sanitizer == Hwasan {
1391 hwasanStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001392 }
1393 }
Colin Cross16b23492016-01-06 14:41:07 -08001394 }
1395 }
1396}
1397
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001398func (c *Module) SanitizeNever() bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001399 return Bool(c.sanitize.Properties.SanitizeMutated.Never)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001400}
1401
1402func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
1403 return c.sanitize.isSanitizerExplicitlyDisabled(t)
1404}
1405
Ivan Lozano30c5db22018-02-21 15:49:20 -08001406// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001407func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001408 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001409 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001410 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001411 mctx.WalkDeps(func(child, parent android.Module) bool {
1412 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1413 return false
1414 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001415
Inseob Kimeec88e12020-01-22 11:11:29 +09001416 d, ok := child.(*Module)
1417 if !ok || !d.static() {
1418 return false
1419 }
1420 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001421 if enableMinimalRuntime(d.sanitize) {
1422 // If a static dependency is built with the minimal runtime,
1423 // make sure we include the ubsan minimal runtime.
1424 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001425 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001426 // If a static dependency runs with full ubsan diagnostics,
1427 // make sure we include the ubsan runtime.
1428 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001429 }
Colin Cross0b908332019-06-19 23:00:20 -07001430
1431 if c.sanitize.Properties.MinimalRuntimeDep &&
1432 c.sanitize.Properties.UbsanRuntimeDep {
1433 // both flags that this mutator might set are true, so don't bother recursing
1434 return false
1435 }
1436
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001437 if c.Os() == android.Linux {
1438 c.sanitize.Properties.BuiltinsDep = true
1439 }
1440
Colin Cross0b908332019-06-19 23:00:20 -07001441 return true
Colin Cross6b753602018-06-21 13:03:07 -07001442 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001443
Jose Galmesf7294582020-11-13 12:07:36 -08001444 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001445 if Bool(p.properties.Sanitize_minimal_dep) {
1446 c.sanitize.Properties.MinimalRuntimeDep = true
1447 }
1448 if Bool(p.properties.Sanitize_ubsan_dep) {
1449 c.sanitize.Properties.UbsanRuntimeDep = true
1450 }
1451 }
1452
1453 return false
Colin Cross6b753602018-06-21 13:03:07 -07001454 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001455 }
1456}
1457
Jiyong Park379de2f2018-12-19 02:47:14 +09001458// Add the dependency to the runtime library for each of the sanitizer variants
1459func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001460 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001461 if !c.Enabled() {
1462 return
1463 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001464 var sanitizers []string
1465 var diagSanitizers []string
1466
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001467 sanProps := &c.sanitize.Properties.SanitizeMutated
1468
1469 if Bool(sanProps.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001470 sanitizers = append(sanitizers, "undefined")
1471 } else {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001472 if Bool(sanProps.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001473 sanitizers = append(sanitizers,
1474 "bool",
1475 "integer-divide-by-zero",
1476 "return",
1477 "returns-nonnull-attribute",
1478 "shift-exponent",
1479 "unreachable",
1480 "vla-bound",
1481 // TODO(danalbert): The following checks currently have compiler performance issues.
1482 //"alignment",
1483 //"bounds",
1484 //"enum",
1485 //"float-cast-overflow",
1486 //"float-divide-by-zero",
1487 //"nonnull-attribute",
1488 //"null",
1489 //"shift-base",
1490 //"signed-integer-overflow",
1491 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1492 // https://llvm.org/PR19302
1493 // http://reviews.llvm.org/D6974
1494 // "object-size",
1495 )
1496 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001497 sanitizers = append(sanitizers, sanProps.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001498 }
1499
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001500 if Bool(sanProps.Diag.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001501 diagSanitizers = append(diagSanitizers, "undefined")
1502 }
1503
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001504 diagSanitizers = append(diagSanitizers, sanProps.Diag.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001505
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001506 if Bool(sanProps.Address) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001507 sanitizers = append(sanitizers, "address")
1508 diagSanitizers = append(diagSanitizers, "address")
1509 }
1510
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001511 if Bool(sanProps.Hwaddress) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001512 sanitizers = append(sanitizers, "hwaddress")
1513 }
1514
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001515 if Bool(sanProps.Thread) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001516 sanitizers = append(sanitizers, "thread")
1517 }
1518
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001519 if Bool(sanProps.Safestack) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001520 sanitizers = append(sanitizers, "safe-stack")
1521 }
1522
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001523 if Bool(sanProps.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001524 sanitizers = append(sanitizers, "cfi")
1525
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001526 if Bool(sanProps.Diag.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001527 diagSanitizers = append(diagSanitizers, "cfi")
1528 }
1529 }
1530
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001531 if Bool(sanProps.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001532 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1533 sanitizers = append(sanitizers, "signed-integer-overflow")
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001534 if Bool(sanProps.Diag.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001535 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1536 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1537 }
1538 }
1539
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001540 if Bool(sanProps.Scudo) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001541 sanitizers = append(sanitizers, "scudo")
1542 }
1543
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001544 if Bool(sanProps.Scs) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001545 sanitizers = append(sanitizers, "shadow-call-stack")
1546 }
1547
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001548 if Bool(sanProps.Memtag_heap) && c.Binary() {
Florian Mayerd8434a42022-08-31 20:57:03 +00001549 sanitizers = append(sanitizers, "memtag-heap")
1550 }
1551
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001552 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +00001553 sanitizers = append(sanitizers, "memtag-stack")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001554 }
1555
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001556 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001557 sanitizers = append(sanitizers, "fuzzer-no-link")
1558 }
1559
Jiyong Park379de2f2018-12-19 02:47:14 +09001560 // Save the list of sanitizers. These will be used again when generating
1561 // the build rules (for Cflags, etc.)
1562 c.sanitize.Properties.Sanitizers = sanitizers
1563 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1564
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001565 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1566 if c.Host() {
1567 diagSanitizers = sanitizers
1568 }
1569
Colin Crosse323a792023-02-15 13:57:57 -08001570 addStaticDeps := func(dep string, hideSymbols bool) {
Colin Cross06c80eb2022-02-10 10:34:19 -08001571 // If we're using snapshots, redirect to snapshot whenever possible
1572 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001573 if lib, ok := snapshot.StaticLibs[dep]; ok {
1574 dep = lib
Colin Cross06c80eb2022-02-10 10:34:19 -08001575 }
1576
1577 // static executable gets static runtime libs
Colin Crosse323a792023-02-15 13:57:57 -08001578 depTag := libraryDependencyTag{Kind: staticLibraryDependency, unexportedSymbols: hideSymbols}
Colin Cross06c80eb2022-02-10 10:34:19 -08001579 variations := append(mctx.Target().Variations(),
1580 blueprint.Variation{Mutator: "link", Variation: "static"})
1581 if c.Device() {
1582 variations = append(variations, c.ImageVariation())
1583 }
1584 if c.UseSdk() {
1585 variations = append(variations,
1586 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1587 }
Colin Crosse323a792023-02-15 13:57:57 -08001588 mctx.AddFarVariationDependencies(variations, depTag, dep)
Colin Cross06c80eb2022-02-10 10:34:19 -08001589 }
Colin Crosse323a792023-02-15 13:57:57 -08001590
1591 // Determine the runtime library required
1592 runtimeSharedLibrary := ""
1593 toolchain := c.toolchain(mctx)
1594 if Bool(sanProps.Address) {
Colin Crossb781d232023-02-15 12:40:20 -08001595 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1596 // Use a static runtime for musl to match what clang does for glibc.
1597 addStaticDeps(config.AddressSanitizerStaticRuntimeLibrary(toolchain), false)
1598 addStaticDeps(config.AddressSanitizerCXXStaticRuntimeLibrary(toolchain), false)
1599 } else {
1600 runtimeSharedLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1601 }
Colin Crosse323a792023-02-15 13:57:57 -08001602 } else if Bool(sanProps.Hwaddress) {
1603 if c.staticBinary() {
1604 addStaticDeps(config.HWAddressSanitizerStaticLibrary(toolchain), true)
1605 addStaticDeps("libdl", false)
1606 } else {
1607 runtimeSharedLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1608 }
1609 } else if Bool(sanProps.Thread) {
1610 runtimeSharedLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1611 } else if Bool(sanProps.Scudo) {
1612 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1613 runtimeSharedLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1614 } else {
1615 runtimeSharedLibrary = config.ScudoRuntimeLibrary(toolchain)
1616 }
1617 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
1618 Bool(sanProps.Fuzzer) ||
1619 Bool(sanProps.Undefined) ||
1620 Bool(sanProps.All_undefined) {
1621 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1622 // Use a static runtime for static binaries.
1623 // Also use a static runtime for musl to match
1624 // what clang does for glibc. Otherwise dlopening
1625 // libraries that depend on libclang_rt.ubsan_standalone.so
1626 // fails with:
1627 // Error relocating ...: initial-exec TLS resolves to dynamic definition
1628 addStaticDeps(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)+".static", true)
1629 } else {
1630 runtimeSharedLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
1631 }
1632 }
1633
Colin Cross06c80eb2022-02-10 10:34:19 -08001634 if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
Colin Crosse323a792023-02-15 13:57:57 -08001635 addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001636 }
1637 if c.sanitize.Properties.BuiltinsDep {
Colin Crosse323a792023-02-15 13:57:57 -08001638 addStaticDeps(config.BuiltinsRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001639 }
1640
Colin Crosse323a792023-02-15 13:57:57 -08001641 if runtimeSharedLibrary != "" && (toolchain.Bionic() || toolchain.Musl() || c.sanitize.Properties.UbsanRuntimeDep) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001642 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001643
1644 // Adding dependency to the runtime library. We are using *FarVariation*
1645 // because the runtime libraries themselves are not mutated by sanitizer
1646 // mutators and thus don't have sanitizer variants whereas this module
1647 // has been already mutated.
1648 //
1649 // Note that by adding dependency with {static|shared}DepTag, the lib is
1650 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
Colin Crosse323a792023-02-15 13:57:57 -08001651 if c.staticBinary() {
1652 // Most sanitizers are either disabled for static binaries or have already
1653 // handled the static binary case above through a direct call to addStaticDeps.
1654 // If not, treat the runtime shared library as a static library and hope for
1655 // the best.
1656 addStaticDeps(runtimeSharedLibrary, true)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001657 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001658 // If we're using snapshots, redirect to snapshot whenever possible
1659 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001660 if lib, ok := snapshot.SharedLibs[runtimeSharedLibrary]; ok {
1661 runtimeSharedLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001662 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001663
Cindy Zhou18417cb2020-12-10 07:12:38 -08001664 // Skip apex dependency check for sharedLibraryDependency
1665 // when sanitizer diags are enabled. Skipping the check will allow
1666 // building with diag libraries without having to list the
1667 // dependency in Apex's allowed_deps file.
1668 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001669 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001670 depTag := libraryDependencyTag{
1671 Kind: sharedLibraryDependency,
1672 Order: earlyLibraryDependency,
1673
1674 skipApexAllowedDependenciesCheck: diagEnabled,
1675 }
Colin Cross42507332020-08-21 16:15:23 -07001676 variations := append(mctx.Target().Variations(),
1677 blueprint.Variation{Mutator: "link", Variation: "shared"})
1678 if c.Device() {
1679 variations = append(variations, c.ImageVariation())
1680 }
Colin Cross06c80eb2022-02-10 10:34:19 -08001681 if c.UseSdk() {
1682 variations = append(variations,
1683 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1684 }
Colin Crosse323a792023-02-15 13:57:57 -08001685 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeSharedLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001686 }
1687 // static lib does not have dependency to the runtime library. The
1688 // dependency will be added to the executables or shared libs using
1689 // the static lib.
1690 }
1691 }
1692}
1693
1694type Sanitizeable interface {
1695 android.Module
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001696 IsSanitizerEnabled(config android.Config, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001697 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001698 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001699}
1700
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001701type JniSanitizeable interface {
1702 android.Module
1703 IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
1704}
1705
Ivan Lozanod7586b62021-04-01 09:49:36 -04001706func (c *Module) MinimalRuntimeDep() bool {
1707 return c.sanitize.Properties.MinimalRuntimeDep
1708}
1709
1710func (c *Module) UbsanRuntimeDep() bool {
1711 return c.sanitize.Properties.UbsanRuntimeDep
1712}
1713
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001714func (c *Module) SanitizePropDefined() bool {
1715 return c.sanitize != nil
1716}
1717
1718func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1719 return c.sanitize.isSanitizerEnabled(t)
1720}
1721
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001722func (c *Module) StaticallyLinked() bool {
1723 return c.static()
1724}
1725
1726func (c *Module) SetInSanitizerDir() {
1727 if c.sanitize != nil {
1728 c.sanitize.Properties.InSanitizerDir = true
1729 }
1730}
1731
1732func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1733 if c.sanitize != nil {
1734 c.sanitize.SetSanitizer(t, b)
1735 }
1736}
1737
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001738var _ PlatformSanitizeable = (*Module)(nil)
1739
Inseob Kim74d25562020-08-04 00:41:38 +09001740type sanitizerStaticLibsMap struct {
1741 // libsMap contains one list of modules per each image and each arch.
1742 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001743 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001744 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001745 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001746}
1747
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001748func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001749 return &sanitizerStaticLibsMap{
1750 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001751 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001752 }
1753}
1754
1755// Add the current module to sanitizer static libs maps
1756// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001757func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1758 image := GetImageVariantType(c)
1759 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001760
1761 s.libsMapLock.Lock()
1762 defer s.libsMapLock.Unlock()
1763
1764 if _, ok := s.libsMap[image]; !ok {
1765 s.libsMap[image] = make(map[string][]string)
1766 }
1767
1768 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1769}
1770
1771// Exports makefile variables in the following format:
1772// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1773// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1774// These are to be used by use_soong_sanitized_static_libraries.
1775// See build/make/core/binary.mk for more details.
1776func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
Cole Faust18994c72023-02-28 16:02:16 -08001777 for _, image := range android.SortedKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001778 archMap := s.libsMap[ImageVariantType(image)]
Cole Faust18994c72023-02-28 16:02:16 -08001779 for _, arch := range android.SortedKeys(archMap) {
Inseob Kim74d25562020-08-04 00:41:38 +09001780 libs := archMap[arch]
1781 sort.Strings(libs)
1782
1783 key := fmt.Sprintf(
1784 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1785 s.sanitizerType.variationName(),
1786 image, // already upper
1787 arch)
1788
1789 ctx.Strict(key, strings.Join(libs, " "))
1790 }
1791 }
1792}
1793
Colin Cross571cccf2019-02-04 11:22:08 -08001794var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1795
Inseob Kim74d25562020-08-04 00:41:38 +09001796func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001797 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001798 return newSanitizerStaticLibsMap(cfi)
1799 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001800}
1801
Colin Cross571cccf2019-02-04 11:22:08 -08001802var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1803
Inseob Kim74d25562020-08-04 00:41:38 +09001804func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001805 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001806 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001807 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001808}
1809
Ivan Lozano30c5db22018-02-21 15:49:20 -08001810func enableMinimalRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001811 if sanitize.isSanitizerEnabled(Asan) {
1812 return false
1813 } else if sanitize.isSanitizerEnabled(Hwasan) {
1814 return false
1815 } else if sanitize.isSanitizerEnabled(Fuzzer) {
1816 return false
Ivan Lozano30c5db22018-02-21 15:49:20 -08001817 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001818
1819 if enableUbsanRuntime(sanitize) {
1820 return false
1821 }
1822
1823 sanitizeProps := &sanitize.Properties.SanitizeMutated
1824 if Bool(sanitizeProps.Diag.Cfi) {
1825 return false
1826 }
1827
1828 return Bool(sanitizeProps.Integer_overflow) ||
1829 len(sanitizeProps.Misc_undefined) > 0 ||
1830 Bool(sanitizeProps.Undefined) ||
1831 Bool(sanitizeProps.All_undefined)
Ivan Lozano30c5db22018-02-21 15:49:20 -08001832}
1833
Ivan Lozanod7586b62021-04-01 09:49:36 -04001834func (m *Module) UbsanRuntimeNeeded() bool {
1835 return enableUbsanRuntime(m.sanitize)
1836}
1837
1838func (m *Module) MinimalRuntimeNeeded() bool {
1839 return enableMinimalRuntime(m.sanitize)
1840}
1841
Inseob Kim8471cda2019-11-15 09:59:12 +09001842func enableUbsanRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001843 sanitizeProps := &sanitize.Properties.SanitizeMutated
1844 return Bool(sanitizeProps.Diag.Integer_overflow) ||
1845 Bool(sanitizeProps.Diag.Undefined) ||
1846 len(sanitizeProps.Diag.Misc_undefined) > 0
Inseob Kim8471cda2019-11-15 09:59:12 +09001847}
1848
Vishwath Mohane7128792017-11-17 11:08:10 -08001849func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001850 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001851}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001852
1853func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001854 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001855}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +00001856
1857func BazelCcSanitizerToolchainVars(config android.Config) string {
1858 return android.BazelToolchainVars(config, exportedVars)
1859}