blob: 9754f236956c39b53d2e542e20eae14a44e50d23 [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 Kim0d8908c2024-05-07 14:47:35 +090028 "android/soong/etc"
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
Florian Mayera9984462023-06-16 16:48:51 -070040 // DO NOT ADD MLLVM FLAGS HERE! ADD THEM BELOW TO hwasanCommonFlags.
Yi Kong286abc62021-11-04 16:14:14 +080041 hwasanCflags = []string{
42 "-fno-omit-frame-pointer",
43 "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000044 "-fsanitize-hwaddress-abi=platform",
Yi Kong286abc62021-11-04 16:14:14 +080045 }
46
47 // ThinLTO performs codegen during link time, thus these flags need to
48 // passed to both CFLAGS and LDFLAGS.
49 hwasanCommonflags = []string{
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080050 // The following improves debug location information
51 // availability at the cost of its accuracy. It increases
52 // the likelihood of a stack variable's frame offset
53 // to be recorded in the debug info, which is important
54 // for the quality of hwasan reports. The downside is a
55 // higher number of "optimized out" stack variables.
56 // b/112437883.
Yi Kong286abc62021-11-04 16:14:14 +080057 "-instcombine-lower-dbg-declare=0",
Florian Mayerc7466192023-06-16 16:50:59 -070058 "-dom-tree-reachability-max-bbs-to-explore=128",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080059 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070060
Trevor Radcliffeded095c2023-06-12 19:18:28 +000061 sanitizeIgnorelistPrefix = "-fsanitize-ignorelist="
62
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000063 cfiBlocklistPath = "external/compiler-rt/lib/cfi"
64 cfiBlocklistFilename = "cfi_blocklist.txt"
Trevor Radcliffef1836e42023-06-01 21:12:08 +000065 cfiEnableFlag = "-fsanitize=cfi"
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +000066 cfiCrossDsoFlag = "-fsanitize-cfi-cross-dso"
67 cfiCflags = []string{"-flto", cfiCrossDsoFlag,
Trevor Radcliffeded095c2023-06-12 19:18:28 +000068 sanitizeIgnorelistPrefix + cfiBlocklistPath + "/" + cfiBlocklistFilename}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070069 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
70 // used, but have no effect on assembly files
71 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Trevor Radcliffef1836e42023-06-01 21:12:08 +000072 cfiLdflags = []string{"-flto", cfiCrossDsoFlag, cfiEnableFlag,
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070073 "-Wl,-plugin-opt,O1"}
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000074 cfiExportsMapPath = "build/soong/cc/config"
75 cfiExportsMapFilename = "cfi_exports.map"
76 cfiAssemblySupportFlag = "-fno-sanitize-cfi-canonical-jump-tables"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070077
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070078 intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080079
Peter Collingbournebd19db02019-03-06 10:38:48 -080080 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070081 "-fno-sanitize-recover=integer,undefined"}
Florian Mayerab97e282024-04-30 17:34:58 -070082 memtagStackCommonFlags = []string{"-march=armv8-a+memtag"}
Ivan Lozano2a2d5092024-05-03 12:10:04 -040083 memtagStackLlvmFlags = []string{"-dom-tree-reachability-max-bbs-to-explore=128"}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +000084
85 hostOnlySanitizeFlags = []string{"-fno-sanitize-recover=all"}
Elliott Hughes3bba0e42023-10-05 14:50:48 -070086 deviceOnlySanitizeFlags = []string{"-fsanitize-trap=all"}
Trevor Radcliffeda64d912023-08-02 20:24:29 +000087
88 noSanitizeLinkRuntimeFlag = "-fno-sanitize-link-runtime"
Dan Willemsencbceaab2016-10-13 16:44:07 -070089)
90
Ivan Lozano3968d8f2020-12-14 11:27:52 -050091type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080092
Colin Cross16b23492016-01-06 14:41:07 -080093const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050094 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070095 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080096 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070097 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080098 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -050099 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -0400100 Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +0000101 Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200102 Memtag_globals
Liz Kammer75db9312021-07-07 16:41:50 -0400103 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -0800104)
105
Liz Kammer75db9312021-07-07 16:41:50 -0400106var Sanitizers = []SanitizerType{
107 Asan,
108 Hwasan,
109 tsan,
110 intOverflow,
111 scs,
112 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400113 Memtag_heap,
Florian Mayerd8434a42022-08-31 20:57:03 +0000114 Memtag_stack,
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200115 Memtag_globals,
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"
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200138 case Memtag_globals:
139 return "memtag_globals"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500140 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700141 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800142 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500143 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800144 }
145}
146
Jiyong Park82226632019-02-01 10:50:50 +0900147// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500148func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900149 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500150 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900151 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700152 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900153 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400154 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700155 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000156 case Memtag_stack:
157 return "memtag_stack"
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200158 case Memtag_globals:
159 return "memtag_globals"
Jiyong Park82226632019-02-01 10:50:50 +0900160 case tsan:
161 return "thread"
162 case intOverflow:
163 return "integer_overflow"
164 case cfi:
165 return "cfi"
166 case scs:
167 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500168 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700169 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900170 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500171 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900172 }
173}
174
Liz Kammer75db9312021-07-07 16:41:50 -0400175func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
176 switch t {
Florian Mayer5ccc4212024-03-27 13:53:39 -0700177 case cfi, Hwasan, Asan, tsan, Fuzzer, scs, Memtag_stack:
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200178 sanitizer := &sanitizerSplitMutator{t}
179 ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
180 ctx.Transition(t.variationName(), sanitizer)
Florian Mayer5ccc4212024-03-27 13:53:39 -0700181 case Memtag_heap, Memtag_globals, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400182 // do nothing
183 default:
184 panic(fmt.Errorf("unknown SanitizerType %d", t))
185 }
186}
187
Liz Kammerfd8a49f2022-10-31 10:31:11 -0400188// shouldPropagateToSharedLibraryDeps returns whether a sanitizer type should propagate to share
189// dependencies. In most cases, sanitizers only propagate to static dependencies; however, some
190// sanitizers also must be enabled for shared libraries for linking.
191func (t SanitizerType) shouldPropagateToSharedLibraryDeps() bool {
192 switch t {
193 case Fuzzer:
194 // Typically, shared libs are not split. However, for fuzzer, we split even for shared libs
195 // because a library sanitized for fuzzer can't be linked from a library that isn't sanitized
196 // for fuzzer.
197 return true
198 default:
199 return false
200 }
201}
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500202func (*Module) SanitizerSupported(t SanitizerType) bool {
203 switch t {
204 case Asan:
205 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700206 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500207 return true
208 case tsan:
209 return true
210 case intOverflow:
211 return true
212 case cfi:
213 return true
214 case scs:
215 return true
216 case Fuzzer:
217 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400218 case Memtag_heap:
219 return true
Florian Mayerd8434a42022-08-31 20:57:03 +0000220 case Memtag_stack:
221 return true
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200222 case Memtag_globals:
223 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500224 default:
225 return false
226 }
227}
228
229// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
230func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700231 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900232}
233
Martin Stjernholmb0249572020-09-15 02:32:35 +0100234type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400235 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100236 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800237
Liz Kammer75b9b402021-06-25 15:19:27 -0400238 // ASan (Address sanitizer), incompatible with static binaries.
239 // Always runs in a diagnostic mode.
240 // Use of address sanitizer disables cfi sanitizer.
241 // Hwaddress sanitizer takes precedence over this sanitizer.
242 Address *bool `android:"arch_variant"`
243 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
244 // Always runs in a diagnostic mode.
245 // Use of thread sanitizer disables cfi and scudo sanitizers.
246 // Hwaddress sanitizer takes precedence over this sanitizer.
247 Thread *bool `android:"arch_variant"`
248 // HWASan (Hardware Address sanitizer).
249 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100250 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800251
Liz Kammer75b9b402021-06-25 15:19:27 -0400252 // Undefined behavior sanitizer
253 All_undefined *bool `android:"arch_variant"`
254 // Subset of undefined behavior sanitizer
255 Undefined *bool `android:"arch_variant"`
256 // List of specific undefined behavior sanitizers to enable
257 Misc_undefined []string `android:"arch_variant"`
258 // Fuzzer, incompatible with static binaries.
259 Fuzzer *bool `android:"arch_variant"`
260 // safe-stack sanitizer, incompatible with 32-bit architectures.
261 Safestack *bool `android:"arch_variant"`
262 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
263 Cfi *bool `android:"arch_variant"`
264 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
265 Integer_overflow *bool `android:"arch_variant"`
266 // scudo sanitizer, incompatible with asan, hwasan, tsan
267 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
268 // deprecated
269 Scudo *bool `android:"arch_variant"`
Elliott Hughese4793bc2023-02-09 21:15:47 +0000270 // shadow-call-stack sanitizer, only available on arm64/riscv64.
Liz Kammer75b9b402021-06-25 15:19:27 -0400271 Scs *bool `android:"arch_variant"`
272 // Memory-tagging, only available on arm64
273 // if diag.memtag unset or false, enables async memory tagging
Florian Mayer00ab5cf2022-08-31 18:30:18 +0000274 Memtag_heap *bool `android:"arch_variant"`
Florian Mayerd8434a42022-08-31 20:57:03 +0000275 // Memory-tagging stack instrumentation, only available on arm64
276 // Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
277 Memtag_stack *bool `android:"arch_variant"`
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200278 // Memory-tagging globals instrumentation, only available on arm64
279 // Adds instrumentation to detect global buffer overflows using MTE.
280 Memtag_globals *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100281
282 // A modifier for ASAN and HWASAN for write only instrumentation
283 Writeonly *bool `android:"arch_variant"`
284
285 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
286 // Replaces abort() on error with a human-readable error message.
287 // Address and Thread sanitizers always run in diagnostic mode.
288 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400289 // Undefined behavior sanitizer, diagnostic mode
290 Undefined *bool `android:"arch_variant"`
291 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
292 Cfi *bool `android:"arch_variant"`
293 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
294 Integer_overflow *bool `android:"arch_variant"`
295 // Memory-tagging, only available on arm64
296 // requires sanitizer.memtag: true
297 // if set, enables sync memory tagging
298 Memtag_heap *bool `android:"arch_variant"`
299 // List of specific undefined behavior sanitizers to enable in diagnostic mode
300 Misc_undefined []string `android:"arch_variant"`
301 // List of sanitizers to pass to -fno-sanitize-recover
302 // results in only the first detected error for these sanitizers being reported and program then
303 // exits with a non-zero exit code.
304 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800305 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800306
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800307 // Sanitizers to run with flag configuration specified
308 Config struct {
309 // Enables CFI support flags for assembly-heavy libraries
310 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800311 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800312
Liz Kammer75b9b402021-06-25 15:19:27 -0400313 // List of sanitizers to pass to -fsanitize-recover
314 // allows execution to continue for these sanitizers to detect multiple errors rather than only
315 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100316 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000317
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700318 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100319 Blocklist *string
320}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700321
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400322type sanitizeMutatedProperties struct {
323 // Whether sanitizers can be enabled on this module
324 Never *bool `blueprint:"mutated"`
325
326 // Whether ASan (Address sanitizer) is enabled for this module.
327 // Hwaddress sanitizer takes precedence over this sanitizer.
328 Address *bool `blueprint:"mutated"`
329 // Whether TSan (Thread sanitizer) is enabled for this module
330 Thread *bool `blueprint:"mutated"`
331 // Whether HWASan (Hardware Address sanitizer) is enabled for this module
332 Hwaddress *bool `blueprint:"mutated"`
333
334 // Whether Undefined behavior sanitizer is enabled for this module
335 All_undefined *bool `blueprint:"mutated"`
336 // Whether undefined behavior sanitizer subset is enabled for this module
337 Undefined *bool `blueprint:"mutated"`
338 // List of specific undefined behavior sanitizers enabled for this module
339 Misc_undefined []string `blueprint:"mutated"`
340 // Whether Fuzzeris enabled for this module
341 Fuzzer *bool `blueprint:"mutated"`
342 // whether safe-stack sanitizer is enabled for this module
343 Safestack *bool `blueprint:"mutated"`
344 // Whether cfi sanitizer is enabled for this module
345 Cfi *bool `blueprint:"mutated"`
346 // Whether signed/unsigned integer overflow sanitizer is enabled for this module
347 Integer_overflow *bool `blueprint:"mutated"`
348 // Whether scudo sanitizer is enabled for this module
349 Scudo *bool `blueprint:"mutated"`
350 // Whether shadow-call-stack sanitizer is enabled for this module.
351 Scs *bool `blueprint:"mutated"`
352 // Whether Memory-tagging is enabled for this module
353 Memtag_heap *bool `blueprint:"mutated"`
354 // Whether Memory-tagging stack instrumentation is enabled for this module
355 Memtag_stack *bool `blueprint:"mutated"`
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200356 // Whether Memory-tagging globals instrumentation is enabled for this module
357 Memtag_globals *bool `android:"arch_variant"`
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400358
359 // Whether a modifier for ASAN and HWASAN for write only instrumentation is enabled for this
360 // module
361 Writeonly *bool `blueprint:"mutated"`
362
363 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
364 Diag struct {
365 // Whether Undefined behavior sanitizer, diagnostic mode is enabled for this module
366 Undefined *bool `blueprint:"mutated"`
367 // Whether cfi sanitizer, diagnostic mode is enabled for this module
368 Cfi *bool `blueprint:"mutated"`
369 // Whether signed/unsigned integer overflow sanitizer, diagnostic mode is enabled for this
370 // module
371 Integer_overflow *bool `blueprint:"mutated"`
372 // Whether Memory-tagging, diagnostic mode is enabled for this module
373 Memtag_heap *bool `blueprint:"mutated"`
374 // List of specific undefined behavior sanitizers enabled in diagnostic mode
375 Misc_undefined []string `blueprint:"mutated"`
376 } `blueprint:"mutated"`
377}
378
Martin Stjernholmb0249572020-09-15 02:32:35 +0100379type SanitizeProperties struct {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400380 Sanitize SanitizeUserProps `android:"arch_variant"`
381 SanitizeMutated sanitizeMutatedProperties `blueprint:"mutated"`
382
Colin Cross694fced2024-06-25 14:56:42 -0700383 // ForceDisable is set by the version mutator to disable sanitization of stubs variants
384 ForceDisable bool `blueprint:"mutated"`
385
386 // SanitizerEnabled is set by begin() if any of the sanitize boolean properties are set after
387 // applying the logic that enables globally enabled sanitizers and disables any unsupported
388 // sanitizers.
389 // TODO(b/349906293): this has some unintuitive behavior. It is set in begin() before the sanitize
390 // mutator is run if any of the individual sanitizes properties are set, and then the individual
391 // sanitize properties are cleared in the non-sanitized variants, but this value is never cleared.
392 // That results in SanitizerEnabled being set in variants that have no sanitizers enabled, causing
393 // some of the sanitizer logic in flags() to be applied to the non-sanitized variant.
394 SanitizerEnabled bool `blueprint:"mutated"`
395
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400396 MinimalRuntimeDep bool `blueprint:"mutated"`
397 BuiltinsDep bool `blueprint:"mutated"`
398 UbsanRuntimeDep bool `blueprint:"mutated"`
399 InSanitizerDir bool `blueprint:"mutated"`
400 Sanitizers []string `blueprint:"mutated"`
401 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800402}
403
404type sanitize struct {
405 Properties SanitizeProperties
406}
407
Cindy Zhou18417cb2020-12-10 07:12:38 -0800408// Mark this tag with a check to see if apex dependency check should be skipped
409func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
410 return t.skipApexAllowedDependenciesCheck
411}
412
413var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
414
Vishwath Mohane7128792017-11-17 11:08:10 -0800415func init() {
Cole Faust8982b1c2024-04-08 16:54:45 -0700416 pctx.StaticVariable("HostOnlySanitizeFlags", strings.Join(hostOnlySanitizeFlags, " "))
Trevor Radcliffeda64d912023-08-02 20:24:29 +0000417
Vishwath Mohane7128792017-11-17 11:08:10 -0800418 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700419 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Florian Mayer5ccc4212024-03-27 13:53:39 -0700420 android.RegisterMakeVarsProvider(pctx, memtagStackMakeVarsProvider)
Kiyoung Kim0d8908c2024-05-07 14:47:35 +0900421
422 RegisterSanitizerLibrariesTxtType(android.InitRegistrationContext)
Vishwath Mohane7128792017-11-17 11:08:10 -0800423}
424
Colin Cross16b23492016-01-06 14:41:07 -0800425func (sanitize *sanitize) props() []interface{} {
426 return []interface{}{&sanitize.Properties}
427}
428
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400429func (p *sanitizeMutatedProperties) copyUserPropertiesToMutated(userProps *SanitizeUserProps) {
430 p.Never = userProps.Never
431 p.Address = userProps.Address
432 p.All_undefined = userProps.All_undefined
433 p.Cfi = userProps.Cfi
434 p.Fuzzer = userProps.Fuzzer
435 p.Hwaddress = userProps.Hwaddress
436 p.Integer_overflow = userProps.Integer_overflow
437 p.Memtag_heap = userProps.Memtag_heap
438 p.Memtag_stack = userProps.Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200439 p.Memtag_globals = userProps.Memtag_globals
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400440 p.Safestack = userProps.Safestack
441 p.Scs = userProps.Scs
442 p.Scudo = userProps.Scudo
443 p.Thread = userProps.Thread
444 p.Undefined = userProps.Undefined
445 p.Writeonly = userProps.Writeonly
446
447 p.Misc_undefined = make([]string, 0, len(userProps.Misc_undefined))
448 for _, v := range userProps.Misc_undefined {
449 p.Misc_undefined = append(p.Misc_undefined, v)
450 }
451
452 p.Diag.Cfi = userProps.Diag.Cfi
453 p.Diag.Integer_overflow = userProps.Diag.Integer_overflow
454 p.Diag.Memtag_heap = userProps.Diag.Memtag_heap
455 p.Diag.Undefined = userProps.Diag.Undefined
456
457 p.Diag.Misc_undefined = make([]string, 0, len(userProps.Diag.Misc_undefined))
458 for _, v := range userProps.Diag.Misc_undefined {
459 p.Diag.Misc_undefined = append(p.Diag.Misc_undefined, v)
460 }
461}
462
Colin Cross16b23492016-01-06 14:41:07 -0800463func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400464 s := &sanitize.Properties.SanitizeMutated
465 s.copyUserPropertiesToMutated(&sanitize.Properties.Sanitize)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700466
Colin Cross694fced2024-06-25 14:56:42 -0700467 if sanitize.Properties.ForceDisable {
468 return
469 }
470
Colin Cross16b23492016-01-06 14:41:07 -0800471 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700472 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800473 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800474 }
475
476 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800477 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800478 return
479 }
480
Florian Mayerd8434a42022-08-31 20:57:03 +0000481 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400482 if ctx.testBinary() {
483 if s.Memtag_heap == nil {
484 s.Memtag_heap = proptools.BoolPtr(true)
485 }
486 if s.Diag.Memtag_heap == nil {
487 s.Diag.Memtag_heap = proptools.BoolPtr(true)
488 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800489 }
490
Colin Cross16b23492016-01-06 14:41:07 -0800491 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700492 var globalSanitizersDiag []string
493
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700494 if ctx.Host() {
495 if !ctx.Windows() {
496 globalSanitizers = ctx.Config().SanitizeHost()
497 }
498 } else {
499 arches := ctx.Config().SanitizeDeviceArch()
500 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
501 globalSanitizers = ctx.Config().SanitizeDevice()
502 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800503 }
504 }
505
Colin Cross16b23492016-01-06 14:41:07 -0800506 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000507 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700508 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400509 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000510 }
Colin Cross16b23492016-01-06 14:41:07 -0800511
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700512 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400513 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000514 }
515
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700516 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400517 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000518 }
519
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700520 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400521 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000522 }
523
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700524 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400525 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700526 }
527
528 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400529 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000530 }
531
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700532 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800533 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400534 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700535 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700536 }
537
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700538 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700539 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700540 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400541 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700542 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700543 }
544
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700545 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400546 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700547 }
548
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700549 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Tomislav Novakf734f002023-08-22 10:51:44 -0700550 if !ctx.Config().HWASanDisabledForPath(ctx.ModuleDir()) {
551 s.Hwaddress = proptools.BoolPtr(true)
552 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700553 }
554
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000555 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
556 // Hwaddress and Address are set before, so we can check them here
557 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
558 if s.Address == nil && s.Hwaddress == nil {
559 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
560 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400561 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000562 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700563 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800564 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400565 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800566 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700567 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000568
Florian Mayerd8434a42022-08-31 20:57:03 +0000569 if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
570 s.Memtag_stack = proptools.BoolPtr(true)
571 }
572
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200573 if found, globalSanitizers = removeFromList("memtag_globals", globalSanitizers); found && s.Memtag_globals == nil {
574 s.Memtag_globals = proptools.BoolPtr(true)
575 }
576
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000577 if len(globalSanitizers) > 0 {
578 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
579 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700580
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700581 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700582 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700583 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400584 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700585 }
586
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700587 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
588 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400589 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700590 }
591
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800592 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
593 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400594 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800595 }
596
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700597 if len(globalSanitizersDiag) > 0 {
598 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
599 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700600 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700601
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800602 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross88a029f2022-06-23 14:51:20 -0700603 if ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800604 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800605 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400606 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800607 }
608 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400609 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800610 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800611 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800612 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400613 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800614 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800615 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700616 }
617
Hang Lua98aab92023-03-17 13:17:22 +0800618 // Enable HWASan for all components in the include paths (for Aarch64 only)
619 if s.Hwaddress == nil && ctx.Config().HWASanEnabledForPath(ctx.ModuleDir()) &&
620 ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
621 s.Hwaddress = proptools.BoolPtr(true)
622 }
623
Elvis Chien9c993542021-06-25 01:15:17 +0800624 // Enable CFI for non-host components in the include paths
625 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400626 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000627 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400628 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700629 }
630 }
631
Elliott Hughesda3a0712020-03-06 16:55:28 -0800632 // Is CFI actually enabled?
633 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400634 s.Cfi = nil
635 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800636 }
637
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700638 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross88a029f2022-06-23 14:51:20 -0700639 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700640 s.Hwaddress = nil
641 }
642
Elliott Hughese4793bc2023-02-09 21:15:47 +0000643 // SCS is only implemented on AArch64/riscv64.
644 if (ctx.Arch().ArchType != android.Arm64 && ctx.Arch().ArchType != android.Riscv64) || !ctx.toolchain().Bionic() {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800645 s.Scs = nil
646 }
647
Ivan Lozano62cd0382021-11-01 10:27:54 -0400648 // Memtag_heap is only implemented on AArch64.
Florian Mayerd8434a42022-08-31 20:57:03 +0000649 // Memtag ABI is Android specific for now, so disable for host.
650 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() || ctx.Host() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700651 s.Memtag_heap = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000652 s.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200653 s.Memtag_globals = nil
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700654 }
655
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700656 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700657 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400658 s.Cfi = nil
659 s.Diag.Cfi = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000660 // HWASAN and ASAN win against MTE.
661 s.Memtag_heap = nil
662 s.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200663 s.Memtag_globals = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700664 }
665
Colin Crossed12a042022-02-07 13:55:55 -0800666 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
667 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400668 s.Cfi = nil
669 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700670 s.Misc_undefined = nil
671 s.Undefined = nil
672 s.All_undefined = nil
673 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800674 }
675
Colin Crossed12a042022-02-07 13:55:55 -0800676 // Disable CFI for musl
677 if ctx.toolchain().Musl() {
678 s.Cfi = nil
679 s.Diag.Cfi = nil
680 }
681
Colin Cross390fc742023-05-02 13:02:51 -0700682 // TODO(b/280478629): runtimes don't exist for musl arm64 yet.
683 if ctx.toolchain().Musl() && ctx.Arch().ArchType == android.Arm64 {
684 s.Address = nil
685 s.Hwaddress = nil
686 s.Thread = nil
687 s.Scudo = nil
688 s.Fuzzer = nil
689 s.Cfi = nil
690 s.Diag.Cfi = nil
691 s.Misc_undefined = nil
692 s.Undefined = nil
693 s.All_undefined = nil
694 s.Integer_overflow = nil
695 }
696
Florian Mayer5ccc4212024-03-27 13:53:39 -0700697 if ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery() {
698 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
699 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
700 if !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
701 s.Hwaddress = nil
702 }
703 // Memtag stack in ramdisk makes pKVM unhappy.
704 s.Memtag_stack = nil
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700705 }
706
Colin Cross3c344ef2016-07-18 15:44:56 -0700707 if ctx.staticBinary() {
708 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700709 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700710 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800711 }
712
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700713 if Bool(s.All_undefined) {
714 s.Undefined = nil
715 }
716
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700717 if !ctx.toolchain().Is64Bit() {
718 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700719 s.Thread = nil
720 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800721 // TODO(ccross): error for compile_multilib = "32"?
722 }
723
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800724 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 -0700725 Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200726 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack) ||
727 Bool(s.Memtag_globals)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700728 sanitize.Properties.SanitizerEnabled = true
729 }
730
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800731 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
732 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700733 s.Scudo = nil
734 }
735
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700736 if Bool(s.Hwaddress) {
737 s.Address = nil
738 s.Thread = nil
739 }
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000740
741 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
742 // mutually incompatible.
743 if Bool(s.Fuzzer) {
744 s.Cfi = nil
745 }
Colin Cross16b23492016-01-06 14:41:07 -0800746}
747
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800748func toDisableImplicitIntegerChange(flags []string) bool {
749 // Returns true if any flag is fsanitize*integer, and there is
750 // no explicit flag about sanitize=implicit-integer-sign-change.
751 for _, f := range flags {
752 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
753 return false
754 }
755 }
756 for _, f := range flags {
757 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
758 return true
759 }
760 }
761 return false
762}
763
Yabin Cuidb7dda82020-11-30 15:47:45 -0800764func toDisableUnsignedShiftBaseChange(flags []string) bool {
765 // Returns true if any flag is fsanitize*integer, and there is
766 // no explicit flag about sanitize=unsigned-shift-base.
767 for _, f := range flags {
768 if strings.Contains(f, "sanitize=unsigned-shift-base") {
769 return false
770 }
771 }
772 for _, f := range flags {
773 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
774 return true
775 }
776 }
777 return false
778}
779
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400780func (s *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Colin Cross694fced2024-06-25 14:56:42 -0700781 if s.Properties.ForceDisable {
782 return flags
783 }
784
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400785 if !s.Properties.SanitizerEnabled && !s.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800786 return flags
787 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400788 sanProps := &s.Properties.SanitizeMutated
Colin Cross16b23492016-01-06 14:41:07 -0800789
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400790 if Bool(sanProps.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700791 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800792 // Frame pointer based unwinder in ASan requires ARM frame setup.
793 // TODO: put in flags?
794 flags.RequiredInstructionSet = "arm"
795 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800796 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800797
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400798 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000799 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
800 }
801
Colin Cross16b23492016-01-06 14:41:07 -0800802 if ctx.Host() {
Fabien Sanglard4eea1b82024-07-15 13:19:43 -0700803 if !ctx.Darwin() { // ld64.lld doesn't know about '--no-as-needed'
804 // -nodefaultlibs (provided with libc++) prevents the driver from linking
805 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
806 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
807 }
Colin Cross16b23492016-01-06 14:41:07 -0800808 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800809 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900810 if ctx.bootstrap() {
811 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
812 } else {
813 flags.DynamicLinker = "/system/bin/linker_asan"
814 }
Colin Cross16b23492016-01-06 14:41:07 -0800815 if flags.Toolchain.Is64Bit() {
816 flags.DynamicLinker += "64"
817 }
818 }
Colin Cross16b23492016-01-06 14:41:07 -0800819 }
820
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400821 if Bool(sanProps.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800822 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800823
824 for _, flag := range hwasanCommonflags {
825 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
826 }
827 for _, flag := range hwasanCommonflags {
828 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
829 }
830
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400831 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000832 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
833 }
Florian Mayer95cd6db2023-03-23 17:48:07 -0700834 if !ctx.staticBinary() && !ctx.Host() {
835 if ctx.bootstrap() {
836 flags.DynamicLinker = "/system/bin/bootstrap/linker_hwasan64"
837 } else {
838 flags.DynamicLinker = "/system/bin/linker_hwasan64"
839 }
840 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700841 }
842
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400843 if Bool(sanProps.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800844 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700845
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000846 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
847 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
848 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
849 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
850 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
851
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700852 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
853 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
854 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800855 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
856 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700857
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700858 // Disable fortify for fuzzing builds. Generally, we'll be building with
859 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800860 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800861
862 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
863 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
864 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
865 // the DT_RUNPATH from the shared library above it, and not the executable,
866 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
867 // DT_RUNPATH here means that transient shared libraries can be found
868 // colocated with their parents.
869 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800870 }
871
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400872 if Bool(sanProps.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800873 if ctx.Arch().ArchType == android.Arm {
874 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
875 // to do this on a function basis, so force Thumb on the entire module.
876 flags.RequiredInstructionSet = "thumb"
877 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000878
Colin Cross4af21ed2019-11-04 09:37:55 -0800879 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
880 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Ivan Lozano2a2d5092024-05-03 12:10:04 -0400881 flags.CFlagsDeps = append(flags.CFlagsDeps, android.PathForSource(ctx, cfiBlocklistPath+"/"+cfiBlocklistFilename))
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400882 if Bool(s.Properties.Sanitize.Config.Cfi_assembly_support) {
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000883 flags.Local.CFlags = append(flags.Local.CFlags, cfiAssemblySupportFlag)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800884 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000885 // Only append the default visibility flag if -fvisibility has not already been set
886 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800887 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
888 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000889 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800890 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000891
892 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800893 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
894 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000895 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700896 }
897
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400898 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000899 flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
900 flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
901 flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
Florian Mayerab97e282024-04-30 17:34:58 -0700902
903 for _, flag := range memtagStackLlvmFlags {
904 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
905 }
906 for _, flag := range memtagStackLlvmFlags {
907 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
908 }
Florian Mayerd8434a42022-08-31 20:57:03 +0000909 }
910
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200911 if (Bool(sanProps.Memtag_heap) || Bool(sanProps.Memtag_stack) || Bool(sanProps.Memtag_globals)) && ctx.binary() {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400912 if Bool(sanProps.Diag.Memtag_heap) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000913 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
914 } else {
915 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
916 }
917 }
918
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400919 if Bool(sanProps.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800920 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700921 }
922
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400923 if len(s.Properties.Sanitizers) > 0 {
924 sanitizeArg := "-fsanitize=" + strings.Join(s.Properties.Sanitizers, ",")
Colin Cross4af21ed2019-11-04 09:37:55 -0800925 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
926 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross234b01d2022-02-07 13:49:03 -0800927 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
928
Colin Crossed12a042022-02-07 13:55:55 -0800929 if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
930 // Bionic and musl sanitizer runtimes have already been added as dependencies so that
931 // the right variant of the runtime will be used (with the "-android" or "-musl"
932 // suffixes), so don't let clang the runtime library.
Trevor Radcliffeda64d912023-08-02 20:24:29 +0000933 flags.Local.LdFlags = append(flags.Local.LdFlags, noSanitizeLinkRuntimeFlag)
Colin Cross234b01d2022-02-07 13:49:03 -0800934 } else {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800935 // Host sanitizers only link symbols in the final executable, so
936 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800937 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
Colin Cross6c18d002022-06-02 15:11:50 -0700938 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500939
Colin Cross6c18d002022-06-02 15:11:50 -0700940 if !ctx.toolchain().Bionic() {
941 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function san.
942 // Musl toolchain prebuilts have vptr and function sanitizers, but enabling them
943 // implicitly enables RTTI which causes RTTI mismatch issues with dependencies.
944
Colin Cross234b01d2022-02-07 13:49:03 -0800945 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500946 }
947
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400948 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700949 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800950 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700951 } else if ctx.Host() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000952 flags.Local.CFlags = append(flags.Local.CFlags, hostOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700953 } else {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000954 flags.Local.CFlags = append(flags.Local.CFlags, deviceOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700955 }
Evgenii Stepanov59012812022-06-24 11:09:18 -0700956
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400957 if enableMinimalRuntime(s) {
Evgenii Stepanov59012812022-06-24 11:09:18 -0700958 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
959 }
960
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800961 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800962 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
963 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800964 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800965 // http://b/171275751, Android doesn't build with this sanitizer yet.
966 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
967 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
968 }
Colin Cross16b23492016-01-06 14:41:07 -0800969 }
970
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400971 if len(s.Properties.DiagSanitizers) > 0 {
972 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(s.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700973 }
974 // FIXME: enable RTTI if diag + (cfi or vptr)
975
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400976 if s.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800977 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400978 strings.Join(s.Properties.Sanitize.Recover, ","))
Andreas Gampe97071162017-05-08 13:15:23 -0700979 }
980
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400981 if s.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800982 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400983 strings.Join(s.Properties.Sanitize.Diag.No_recover, ","))
Ivan Lozano7929bba2018-12-12 09:36:31 -0800984 }
985
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400986 blocklist := android.OptionalPathForModuleSrc(ctx, s.Properties.Sanitize.Blocklist)
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700987 if blocklist.Valid() {
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000988 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeIgnorelistPrefix+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700989 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
990 }
991
Colin Cross16b23492016-01-06 14:41:07 -0800992 return flags
993}
994
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400995func (s *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900996 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
997 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800998 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400999 if Bool(s.Properties.SanitizeMutated.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -08001000 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +09001001 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001002 if Bool(s.Properties.SanitizeMutated.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -08001003 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +09001004 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001005 if Bool(s.Properties.SanitizeMutated.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -08001006 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +09001007 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001008 }
Colin Cross8ff9ef42017-05-08 13:44:11 -07001009}
1010
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001011func (s *sanitize) inSanitizerDir() bool {
1012 return s.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -07001013}
1014
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001015// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001016func (s *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +00001017 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001018 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001019 return s.Properties.SanitizeMutated.Address
Tri Vo6eafc362021-04-01 11:29:09 -07001020 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001021 return s.Properties.SanitizeMutated.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +00001022 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001023 return s.Properties.SanitizeMutated.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +00001024 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001025 return s.Properties.SanitizeMutated.Integer_overflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001026 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001027 return s.Properties.SanitizeMutated.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001028 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001029 return s.Properties.SanitizeMutated.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -04001030 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001031 return s.Properties.SanitizeMutated.Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +00001032 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001033 return s.Properties.SanitizeMutated.Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001034 case Memtag_globals:
1035 return s.Properties.SanitizeMutated.Memtag_globals
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001036 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001037 return s.Properties.SanitizeMutated.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +00001038 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001039 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +00001040 }
1041}
1042
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001043// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -08001044func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001045 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001046 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -08001047 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001048 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001049 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -04001050 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Florian Mayerd8434a42022-08-31 20:57:03 +00001051 !sanitize.isSanitizerEnabled(Memtag_stack) &&
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001052 !sanitize.isSanitizerEnabled(Memtag_globals) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001053 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -08001054}
1055
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001056// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001057func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001058 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001059 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001060 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001061 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001062}
1063
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001064func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -04001065 bPtr := proptools.BoolPtr(b)
1066 if !b {
1067 bPtr = nil
1068 }
Colin Cross16b23492016-01-06 14:41:07 -08001069 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001070 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001071 sanitize.Properties.SanitizeMutated.Address = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001072 // For ASAN variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001073 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001074 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Tri Vo6eafc362021-04-01 11:29:09 -07001075 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001076 sanitize.Properties.SanitizeMutated.Hwaddress = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001077 // For HWAsan variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001078 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001079 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Colin Cross16b23492016-01-06 14:41:07 -08001080 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001081 sanitize.Properties.SanitizeMutated.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -07001082 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001083 sanitize.Properties.SanitizeMutated.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001084 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001085 sanitize.Properties.SanitizeMutated.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001086 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001087 sanitize.Properties.SanitizeMutated.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -04001088 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001089 sanitize.Properties.SanitizeMutated.Memtag_heap = bPtr
Florian Mayerd8434a42022-08-31 20:57:03 +00001090 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001091 sanitize.Properties.SanitizeMutated.Memtag_stack = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001092 // We do not need to disable ASAN or HWASan here, as there is no Memtag_stack variant.
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001093 case Memtag_globals:
1094 sanitize.Properties.Sanitize.Memtag_globals = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001095 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001096 sanitize.Properties.SanitizeMutated.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -08001097 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001098 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -08001099 }
1100 if b {
1101 sanitize.Properties.SanitizerEnabled = true
1102 }
1103}
1104
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001105// Check if the sanitizer is explicitly disabled (as opposed to nil by
1106// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001107func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001108 if sanitize == nil {
1109 return false
1110 }
1111
1112 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1113 return sanitizerVal != nil && *sanitizerVal == false
1114}
1115
1116// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
1117// because enabling a sanitizer either directly (via the blueprint) or
1118// indirectly (via a mutator) sets the bool ptr to true, and you can't
1119// distinguish between the cases. It isn't needed though - both cases can be
1120// treated identically.
Liz Kammerba23cb62023-09-26 16:48:04 -04001121func (s *sanitize) isSanitizerEnabled(t SanitizerType) bool {
1122 if s == nil {
1123 return false
1124 }
Colin Cross694fced2024-06-25 14:56:42 -07001125 if s.Properties.ForceDisable || proptools.Bool(s.Properties.SanitizeMutated.Never) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001126 return false
1127 }
1128
Liz Kammerba23cb62023-09-26 16:48:04 -04001129 sanitizerVal := s.getSanitizerBoolPtr(t)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001130 return sanitizerVal != nil && *sanitizerVal == true
1131}
1132
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001133// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
1134func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -07001135 switch t := tag.(type) {
1136 case dependencyTag:
1137 return t == reuseObjTag || t == objDepTag
1138 case libraryDependencyTag:
1139 return true
1140 default:
1141 return false
1142 }
Colin Cross6b753602018-06-21 13:03:07 -07001143}
1144
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001145func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
1146 return IsSanitizableDependencyTag
1147}
1148
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001149type sanitizerSplitMutator struct {
1150 sanitizer SanitizerType
1151}
1152
1153// If an APEX is sanitized or not depends on whether it contains at least one
1154// sanitized module. Transition mutators cannot propagate information up the
1155// dependency graph this way, so we need an auxiliary mutator to do so.
1156func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
1157 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1158 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1159 ctx.VisitDirectDeps(func(dep android.Module) {
Ivan Lozano5467a392023-08-23 14:20:25 -04001160 if c, ok := dep.(PlatformSanitizeable); ok && c.IsSanitizerEnabled(s.sanitizer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001161 enabled = true
Inseob Kimc42f2f22020-07-29 20:32:10 +09001162 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001163 })
1164
1165 if enabled {
1166 sanitizeable.EnableSanitizer(s.sanitizer.name())
1167 }
1168 }
1169}
1170
1171func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
1172 if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001173 // If the given sanitizer is not requested in the .bp file for a module, it
1174 // won't automatically build the sanitized variation.
1175 if !c.IsSanitizerEnabled(s.sanitizer) {
1176 return []string{""}
1177 }
1178
1179 if c.Binary() {
1180 // If a sanitizer is enabled for a binary, we do not build the version
1181 // without the sanitizer
1182 return []string{s.sanitizer.variationName()}
1183 } else if c.StaticallyLinked() || c.Header() {
1184 // For static libraries, we build both versions. Some Make modules
1185 // apparently depend on this behavior.
1186 return []string{"", s.sanitizer.variationName()}
1187 } else {
1188 // We only build the requested variation of dynamic libraries
1189 return []string{s.sanitizer.variationName()}
1190 }
1191 }
1192
1193 if _, ok := ctx.Module().(JniSanitizeable); ok {
1194 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1195 // that is short-circuited for now
1196 return []string{""}
1197 }
1198
1199 // If an APEX has a sanitized dependency, we build the APEX in the sanitized
1200 // variation. This is useful because such APEXes require extra dependencies.
1201 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1202 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1203 if enabled {
1204 return []string{s.sanitizer.variationName()}
1205 } else {
1206 return []string{""}
1207 }
1208 }
1209
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001210 return []string{""}
1211}
1212
1213func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1214 if c, ok := ctx.Module().(PlatformSanitizeable); ok {
1215 if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
1216 // If the dependency is through a non-sanitizable tag, use the
1217 // non-sanitized variation
1218 return ""
1219 }
1220
1221 return sourceVariation
1222 } else if _, ok := ctx.Module().(JniSanitizeable); ok {
1223 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1224 // that is short-circuited for now
1225 return ""
1226 } else {
1227 // Otherwise, do not rock the boat.
1228 return sourceVariation
1229 }
1230}
1231
1232func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1233 if d, ok := ctx.Module().(PlatformSanitizeable); ok {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001234 if !d.SanitizePropDefined() ||
1235 d.SanitizeNever() ||
1236 d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
1237 !d.SanitizerSupported(s.sanitizer) {
1238 // If a module opts out of a sanitizer, use its non-sanitized variation
1239 return ""
1240 }
1241
1242 // Binaries are always built in the variation they requested.
1243 if d.Binary() {
1244 if d.IsSanitizerEnabled(s.sanitizer) {
1245 return s.sanitizer.variationName()
1246 } else {
1247 return ""
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001248 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001249 }
1250
1251 // If a shared library requests to be sanitized, it will be built for that
1252 // sanitizer. Otherwise, some sanitizers propagate through shared library
1253 // dependency edges, some do not.
1254 if !d.StaticallyLinked() && !d.Header() {
1255 if d.IsSanitizerEnabled(s.sanitizer) {
1256 return s.sanitizer.variationName()
1257 }
1258
Liz Kammerfd8a49f2022-10-31 10:31:11 -04001259 // Some sanitizers do not propagate to shared dependencies
1260 if !s.sanitizer.shouldPropagateToSharedLibraryDeps() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001261 return ""
1262 }
1263 }
1264
1265 // Static and header libraries inherit whether they are sanitized from the
1266 // module they are linked into
1267 return incomingVariation
1268 } else if d, ok := ctx.Module().(Sanitizeable); ok {
1269 // If an APEX contains a sanitized module, it will be built in the variation
1270 // corresponding to that sanitizer.
1271 enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1272 if enabled {
1273 return s.sanitizer.variationName()
1274 }
1275
1276 return incomingVariation
1277 }
1278
1279 return ""
1280}
1281
1282func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
1283 sanitizerVariation := variationName == s.sanitizer.variationName()
1284
1285 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1286 sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
1287
1288 oneMakeVariation := false
1289 if c.StaticallyLinked() || c.Header() {
1290 if s.sanitizer != cfi && s.sanitizer != scs && s.sanitizer != Hwasan {
1291 // These sanitizers export only one variation to Make. For the rest,
1292 // Make targets can depend on both the sanitized and non-sanitized
1293 // versions.
1294 oneMakeVariation = true
1295 }
1296 } else if !c.Binary() {
1297 // Shared library. These are the sanitizers that do propagate through shared
1298 // library dependencies and therefore can cause multiple variations of a
1299 // shared library to be built.
1300 if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != scs && s.sanitizer != Asan {
1301 oneMakeVariation = true
1302 }
1303 }
1304
1305 if oneMakeVariation {
1306 if sanitizerEnabled != sanitizerVariation {
1307 c.SetPreventInstall()
1308 c.SetHideFromMake()
1309 }
1310 }
1311
1312 if sanitizerVariation {
1313 c.SetSanitizer(s.sanitizer, true)
1314
1315 // CFI is incompatible with ASAN so disable it in ASAN variations
1316 if s.sanitizer.incompatibleWithCfi() {
1317 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1318 if mctx.Device() && cfiSupported {
1319 c.SetSanitizer(cfi, false)
Jiyong Parkf97782b2019-02-13 20:28:58 +09001320 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001321 }
1322
1323 // locate the asan libraries under /data/asan
1324 if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
1325 c.SetInSanitizerDir()
1326 }
1327
1328 if c.StaticallyLinked() && c.ExportedToMake() {
1329 if s.sanitizer == Hwasan {
1330 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
1331 } else if s.sanitizer == cfi {
1332 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
Florian Mayer5ccc4212024-03-27 13:53:39 -07001333 } else if s.sanitizer == Memtag_stack {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001334 memtagStackStaticLibs(mctx.Config()).add(c, c.Module().Name())
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001335 }
1336 }
1337 } else if c.IsSanitizerEnabled(s.sanitizer) {
1338 // Disable the sanitizer for the non-sanitized variation
1339 c.SetSanitizer(s.sanitizer, false)
1340 }
1341 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
1342 // If an APEX has sanitized dependencies, it gets a few more dependencies
1343 if sanitizerVariation {
1344 sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
1345 }
Colin Cross16b23492016-01-06 14:41:07 -08001346 }
1347}
1348
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001349func (c *Module) SanitizeNever() bool {
Colin Cross694fced2024-06-25 14:56:42 -07001350 return c.sanitize.Properties.ForceDisable || Bool(c.sanitize.Properties.SanitizeMutated.Never)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001351}
1352
1353func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
1354 return c.sanitize.isSanitizerExplicitlyDisabled(t)
1355}
1356
Ivan Lozano30c5db22018-02-21 15:49:20 -08001357// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001358func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001359 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001360 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Colin Cross694fced2024-06-25 14:56:42 -07001361 if c.sanitize.Properties.ForceDisable {
1362 return
1363 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001364 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001365 mctx.WalkDeps(func(child, parent android.Module) bool {
1366 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1367 return false
1368 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001369
Inseob Kimeec88e12020-01-22 11:11:29 +09001370 d, ok := child.(*Module)
1371 if !ok || !d.static() {
1372 return false
1373 }
Colin Cross694fced2024-06-25 14:56:42 -07001374 if d.sanitize != nil && !d.sanitize.Properties.ForceDisable {
Colin Cross6b753602018-06-21 13:03:07 -07001375 if enableMinimalRuntime(d.sanitize) {
1376 // If a static dependency is built with the minimal runtime,
1377 // make sure we include the ubsan minimal runtime.
1378 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001379 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001380 // If a static dependency runs with full ubsan diagnostics,
1381 // make sure we include the ubsan runtime.
1382 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001383 }
Colin Cross0b908332019-06-19 23:00:20 -07001384
1385 if c.sanitize.Properties.MinimalRuntimeDep &&
1386 c.sanitize.Properties.UbsanRuntimeDep {
1387 // both flags that this mutator might set are true, so don't bother recursing
1388 return false
1389 }
1390
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001391 if c.Os() == android.Linux {
1392 c.sanitize.Properties.BuiltinsDep = true
1393 }
1394
Colin Cross0b908332019-06-19 23:00:20 -07001395 return true
Colin Cross6b753602018-06-21 13:03:07 -07001396 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001397
Inseob Kimeec88e12020-01-22 11:11:29 +09001398 return false
Colin Cross6b753602018-06-21 13:03:07 -07001399 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001400 }
1401}
1402
Jiyong Park379de2f2018-12-19 02:47:14 +09001403// Add the dependency to the runtime library for each of the sanitizer variants
1404func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001405 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Cole Fausta963b942024-04-11 17:43:00 -07001406 if !c.Enabled(mctx) {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001407 return
1408 }
Colin Cross694fced2024-06-25 14:56:42 -07001409 if c.sanitize.Properties.ForceDisable {
1410 return
1411 }
1412
Jiyong Park379de2f2018-12-19 02:47:14 +09001413 var sanitizers []string
1414 var diagSanitizers []string
1415
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001416 sanProps := &c.sanitize.Properties.SanitizeMutated
1417
1418 if Bool(sanProps.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001419 sanitizers = append(sanitizers, "undefined")
1420 } else {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001421 if Bool(sanProps.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001422 sanitizers = append(sanitizers,
1423 "bool",
1424 "integer-divide-by-zero",
1425 "return",
1426 "returns-nonnull-attribute",
1427 "shift-exponent",
1428 "unreachable",
1429 "vla-bound",
1430 // TODO(danalbert): The following checks currently have compiler performance issues.
1431 //"alignment",
1432 //"bounds",
1433 //"enum",
1434 //"float-cast-overflow",
1435 //"float-divide-by-zero",
1436 //"nonnull-attribute",
1437 //"null",
1438 //"shift-base",
1439 //"signed-integer-overflow",
Jiyong Park379de2f2018-12-19 02:47:14 +09001440 )
Yi Kong76e99ad2024-09-18 18:43:26 +00001441
1442 if mctx.Config().ReleaseBuildObjectSizeSanitizer() {
1443 sanitizers = append(sanitizers, "object-size")
1444 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001445 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001446 sanitizers = append(sanitizers, sanProps.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001447 }
1448
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001449 if Bool(sanProps.Diag.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001450 diagSanitizers = append(diagSanitizers, "undefined")
1451 }
1452
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001453 diagSanitizers = append(diagSanitizers, sanProps.Diag.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001454
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001455 if Bool(sanProps.Address) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001456 sanitizers = append(sanitizers, "address")
1457 diagSanitizers = append(diagSanitizers, "address")
1458 }
1459
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001460 if Bool(sanProps.Hwaddress) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001461 sanitizers = append(sanitizers, "hwaddress")
1462 }
1463
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001464 if Bool(sanProps.Thread) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001465 sanitizers = append(sanitizers, "thread")
1466 }
1467
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001468 if Bool(sanProps.Safestack) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001469 sanitizers = append(sanitizers, "safe-stack")
1470 }
1471
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001472 if Bool(sanProps.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001473 sanitizers = append(sanitizers, "cfi")
1474
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001475 if Bool(sanProps.Diag.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001476 diagSanitizers = append(diagSanitizers, "cfi")
1477 }
1478 }
1479
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001480 if Bool(sanProps.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001481 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1482 sanitizers = append(sanitizers, "signed-integer-overflow")
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001483 if Bool(sanProps.Diag.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001484 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1485 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1486 }
1487 }
1488
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001489 if Bool(sanProps.Scudo) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001490 sanitizers = append(sanitizers, "scudo")
1491 }
1492
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001493 if Bool(sanProps.Scs) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001494 sanitizers = append(sanitizers, "shadow-call-stack")
1495 }
1496
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001497 if Bool(sanProps.Memtag_heap) && c.Binary() {
Florian Mayerd8434a42022-08-31 20:57:03 +00001498 sanitizers = append(sanitizers, "memtag-heap")
1499 }
1500
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001501 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +00001502 sanitizers = append(sanitizers, "memtag-stack")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001503 }
1504
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001505 if Bool(sanProps.Memtag_globals) {
1506 sanitizers = append(sanitizers, "memtag-globals")
1507 // TODO(mitchp): For now, enable memtag-heap with memtag-globals because the linker
1508 // isn't new enough (https://reviews.llvm.org/differential/changeset/?ref=4243566).
1509 sanitizers = append(sanitizers, "memtag-heap")
1510 }
1511
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001512 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001513 sanitizers = append(sanitizers, "fuzzer-no-link")
1514 }
1515
Jiyong Park379de2f2018-12-19 02:47:14 +09001516 // Save the list of sanitizers. These will be used again when generating
1517 // the build rules (for Cflags, etc.)
1518 c.sanitize.Properties.Sanitizers = sanitizers
1519 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1520
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001521 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1522 if c.Host() {
1523 diagSanitizers = sanitizers
1524 }
1525
Colin Crosse323a792023-02-15 13:57:57 -08001526 addStaticDeps := func(dep string, hideSymbols bool) {
Colin Cross06c80eb2022-02-10 10:34:19 -08001527 // static executable gets static runtime libs
Colin Crosse323a792023-02-15 13:57:57 -08001528 depTag := libraryDependencyTag{Kind: staticLibraryDependency, unexportedSymbols: hideSymbols}
Colin Cross06c80eb2022-02-10 10:34:19 -08001529 variations := append(mctx.Target().Variations(),
1530 blueprint.Variation{Mutator: "link", Variation: "static"})
1531 if c.Device() {
1532 variations = append(variations, c.ImageVariation())
1533 }
1534 if c.UseSdk() {
1535 variations = append(variations,
1536 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1537 }
Colin Crosse323a792023-02-15 13:57:57 -08001538 mctx.AddFarVariationDependencies(variations, depTag, dep)
Colin Cross06c80eb2022-02-10 10:34:19 -08001539 }
Colin Crosse323a792023-02-15 13:57:57 -08001540
1541 // Determine the runtime library required
1542 runtimeSharedLibrary := ""
1543 toolchain := c.toolchain(mctx)
1544 if Bool(sanProps.Address) {
Colin Crossb781d232023-02-15 12:40:20 -08001545 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1546 // Use a static runtime for musl to match what clang does for glibc.
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001547 addStaticDeps(config.AddressSanitizerStaticRuntimeLibrary(), false)
1548 addStaticDeps(config.AddressSanitizerCXXStaticRuntimeLibrary(), false)
Colin Crossb781d232023-02-15 12:40:20 -08001549 } else {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001550 runtimeSharedLibrary = config.AddressSanitizerRuntimeLibrary()
Colin Crossb781d232023-02-15 12:40:20 -08001551 }
Colin Crosse323a792023-02-15 13:57:57 -08001552 } else if Bool(sanProps.Hwaddress) {
1553 if c.staticBinary() {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001554 addStaticDeps(config.HWAddressSanitizerStaticLibrary(), true)
Colin Crosse323a792023-02-15 13:57:57 -08001555 addStaticDeps("libdl", false)
1556 } else {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001557 runtimeSharedLibrary = config.HWAddressSanitizerRuntimeLibrary()
Colin Crosse323a792023-02-15 13:57:57 -08001558 }
1559 } else if Bool(sanProps.Thread) {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001560 runtimeSharedLibrary = config.ThreadSanitizerRuntimeLibrary()
Colin Crosse323a792023-02-15 13:57:57 -08001561 } else if Bool(sanProps.Scudo) {
1562 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001563 runtimeSharedLibrary = config.ScudoMinimalRuntimeLibrary()
Colin Crosse323a792023-02-15 13:57:57 -08001564 } else {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001565 runtimeSharedLibrary = config.ScudoRuntimeLibrary()
Colin Crosse323a792023-02-15 13:57:57 -08001566 }
1567 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
1568 Bool(sanProps.Fuzzer) ||
1569 Bool(sanProps.Undefined) ||
1570 Bool(sanProps.All_undefined) {
Colin Cross0df81532023-08-23 22:20:51 -07001571 if toolchain.Musl() || c.staticBinary() {
1572 // Use a static runtime for static binaries. For sanitized glibc binaries the runtime is
1573 // added automatically by clang, but for static glibc binaries that are not sanitized but
1574 // have a sanitized dependency the runtime needs to be added manually.
1575 // Also manually add a static runtime for musl to match what clang does for glibc.
1576 // Otherwise dlopening libraries that depend on libclang_rt.ubsan_standalone.so fails with:
Colin Crosse323a792023-02-15 13:57:57 -08001577 // Error relocating ...: initial-exec TLS resolves to dynamic definition
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001578 addStaticDeps(config.UndefinedBehaviorSanitizerRuntimeLibrary()+".static", true)
Colin Crosse323a792023-02-15 13:57:57 -08001579 } else {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001580 runtimeSharedLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary()
Colin Crosse323a792023-02-15 13:57:57 -08001581 }
1582 }
1583
Colin Cross06c80eb2022-02-10 10:34:19 -08001584 if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001585 addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001586 }
1587 if c.sanitize.Properties.BuiltinsDep {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001588 addStaticDeps(config.BuiltinsRuntimeLibrary(), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001589 }
1590
Ivan Lozano2a2d5092024-05-03 12:10:04 -04001591 if runtimeSharedLibrary != "" && (toolchain.Bionic() || toolchain.Musl()) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001592 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001593
1594 // Adding dependency to the runtime library. We are using *FarVariation*
1595 // because the runtime libraries themselves are not mutated by sanitizer
1596 // mutators and thus don't have sanitizer variants whereas this module
1597 // has been already mutated.
1598 //
1599 // Note that by adding dependency with {static|shared}DepTag, the lib is
1600 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
Colin Crosse323a792023-02-15 13:57:57 -08001601 if c.staticBinary() {
1602 // Most sanitizers are either disabled for static binaries or have already
1603 // handled the static binary case above through a direct call to addStaticDeps.
1604 // If not, treat the runtime shared library as a static library and hope for
1605 // the best.
1606 addStaticDeps(runtimeSharedLibrary, true)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001607 } else if !c.static() && !c.Header() {
Cindy Zhou18417cb2020-12-10 07:12:38 -08001608 // Skip apex dependency check for sharedLibraryDependency
1609 // when sanitizer diags are enabled. Skipping the check will allow
1610 // building with diag libraries without having to list the
1611 // dependency in Apex's allowed_deps file.
1612 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001613 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001614 depTag := libraryDependencyTag{
1615 Kind: sharedLibraryDependency,
1616 Order: earlyLibraryDependency,
1617
1618 skipApexAllowedDependenciesCheck: diagEnabled,
1619 }
Colin Cross42507332020-08-21 16:15:23 -07001620 variations := append(mctx.Target().Variations(),
1621 blueprint.Variation{Mutator: "link", Variation: "shared"})
1622 if c.Device() {
1623 variations = append(variations, c.ImageVariation())
1624 }
Colin Cross06c80eb2022-02-10 10:34:19 -08001625 if c.UseSdk() {
1626 variations = append(variations,
1627 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1628 }
Colin Crosse323a792023-02-15 13:57:57 -08001629 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeSharedLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001630 }
1631 // static lib does not have dependency to the runtime library. The
1632 // dependency will be added to the executables or shared libs using
1633 // the static lib.
1634 }
1635 }
1636}
1637
1638type Sanitizeable interface {
1639 android.Module
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001640 IsSanitizerEnabled(config android.Config, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001641 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001642 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001643}
1644
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001645type JniSanitizeable interface {
1646 android.Module
1647 IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
1648}
1649
Ivan Lozanod7586b62021-04-01 09:49:36 -04001650func (c *Module) MinimalRuntimeDep() bool {
1651 return c.sanitize.Properties.MinimalRuntimeDep
1652}
1653
1654func (c *Module) UbsanRuntimeDep() bool {
1655 return c.sanitize.Properties.UbsanRuntimeDep
1656}
1657
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001658func (c *Module) SanitizePropDefined() bool {
1659 return c.sanitize != nil
1660}
1661
1662func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1663 return c.sanitize.isSanitizerEnabled(t)
1664}
1665
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001666func (c *Module) StaticallyLinked() bool {
1667 return c.static()
1668}
1669
1670func (c *Module) SetInSanitizerDir() {
1671 if c.sanitize != nil {
1672 c.sanitize.Properties.InSanitizerDir = true
1673 }
1674}
1675
1676func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1677 if c.sanitize != nil {
1678 c.sanitize.SetSanitizer(t, b)
1679 }
1680}
1681
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001682var _ PlatformSanitizeable = (*Module)(nil)
1683
Inseob Kim74d25562020-08-04 00:41:38 +09001684type sanitizerStaticLibsMap struct {
1685 // libsMap contains one list of modules per each image and each arch.
1686 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001687 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001688 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001689 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001690}
1691
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001692func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001693 return &sanitizerStaticLibsMap{
1694 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001695 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001696 }
1697}
1698
1699// Add the current module to sanitizer static libs maps
1700// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001701func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1702 image := GetImageVariantType(c)
1703 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001704
1705 s.libsMapLock.Lock()
1706 defer s.libsMapLock.Unlock()
1707
1708 if _, ok := s.libsMap[image]; !ok {
1709 s.libsMap[image] = make(map[string][]string)
1710 }
1711
1712 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1713}
1714
1715// Exports makefile variables in the following format:
1716// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1717// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1718// These are to be used by use_soong_sanitized_static_libraries.
1719// See build/make/core/binary.mk for more details.
1720func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
Cole Faust18994c72023-02-28 16:02:16 -08001721 for _, image := range android.SortedKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001722 archMap := s.libsMap[ImageVariantType(image)]
Cole Faust18994c72023-02-28 16:02:16 -08001723 for _, arch := range android.SortedKeys(archMap) {
Inseob Kim74d25562020-08-04 00:41:38 +09001724 libs := archMap[arch]
1725 sort.Strings(libs)
1726
1727 key := fmt.Sprintf(
1728 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1729 s.sanitizerType.variationName(),
1730 image, // already upper
1731 arch)
1732
1733 ctx.Strict(key, strings.Join(libs, " "))
1734 }
1735 }
1736}
1737
Colin Cross571cccf2019-02-04 11:22:08 -08001738var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1739
Inseob Kim74d25562020-08-04 00:41:38 +09001740func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001741 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001742 return newSanitizerStaticLibsMap(cfi)
1743 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001744}
1745
Colin Cross571cccf2019-02-04 11:22:08 -08001746var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1747
Inseob Kim74d25562020-08-04 00:41:38 +09001748func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001749 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001750 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001751 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001752}
1753
Florian Mayer5ccc4212024-03-27 13:53:39 -07001754var memtagStackStaticLibsKey = android.NewOnceKey("memtagStackStaticLibs")
1755
1756func memtagStackStaticLibs(config android.Config) *sanitizerStaticLibsMap {
1757 return config.Once(memtagStackStaticLibsKey, func() interface{} {
1758 return newSanitizerStaticLibsMap(Memtag_stack)
1759 }).(*sanitizerStaticLibsMap)
1760}
1761
Ivan Lozano30c5db22018-02-21 15:49:20 -08001762func enableMinimalRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001763 if sanitize.isSanitizerEnabled(Asan) {
1764 return false
1765 } else if sanitize.isSanitizerEnabled(Hwasan) {
1766 return false
1767 } else if sanitize.isSanitizerEnabled(Fuzzer) {
1768 return false
Ivan Lozano30c5db22018-02-21 15:49:20 -08001769 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001770
1771 if enableUbsanRuntime(sanitize) {
1772 return false
1773 }
1774
1775 sanitizeProps := &sanitize.Properties.SanitizeMutated
1776 if Bool(sanitizeProps.Diag.Cfi) {
1777 return false
1778 }
1779
1780 return Bool(sanitizeProps.Integer_overflow) ||
1781 len(sanitizeProps.Misc_undefined) > 0 ||
1782 Bool(sanitizeProps.Undefined) ||
1783 Bool(sanitizeProps.All_undefined)
Ivan Lozano30c5db22018-02-21 15:49:20 -08001784}
1785
Ivan Lozanod7586b62021-04-01 09:49:36 -04001786func (m *Module) UbsanRuntimeNeeded() bool {
1787 return enableUbsanRuntime(m.sanitize)
1788}
1789
1790func (m *Module) MinimalRuntimeNeeded() bool {
1791 return enableMinimalRuntime(m.sanitize)
1792}
1793
Inseob Kim8471cda2019-11-15 09:59:12 +09001794func enableUbsanRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001795 sanitizeProps := &sanitize.Properties.SanitizeMutated
1796 return Bool(sanitizeProps.Diag.Integer_overflow) ||
1797 Bool(sanitizeProps.Diag.Undefined) ||
1798 len(sanitizeProps.Diag.Misc_undefined) > 0
Inseob Kim8471cda2019-11-15 09:59:12 +09001799}
1800
Vishwath Mohane7128792017-11-17 11:08:10 -08001801func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001802 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001803}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001804
1805func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001806 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001807}
Florian Mayer5ccc4212024-03-27 13:53:39 -07001808
1809func memtagStackMakeVarsProvider(ctx android.MakeVarsContext) {
1810 memtagStackStaticLibs(ctx.Config()).exportToMake(ctx)
1811}
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001812
1813type sanitizerLibrariesTxtModule struct {
1814 android.ModuleBase
1815
1816 outputFile android.OutputPath
1817}
1818
1819var _ etc.PrebuiltEtcModule = (*sanitizerLibrariesTxtModule)(nil)
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001820
1821func RegisterSanitizerLibrariesTxtType(ctx android.RegistrationContext) {
1822 ctx.RegisterModuleType("sanitizer_libraries_txt", sanitizerLibrariesTxtFactory)
1823}
1824
1825func sanitizerLibrariesTxtFactory() android.Module {
1826 m := &sanitizerLibrariesTxtModule{}
1827 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
1828 return m
1829}
1830
1831type sanitizerLibraryDependencyTag struct {
1832 blueprint.BaseDependencyTag
1833}
1834
1835func (t sanitizerLibraryDependencyTag) AllowDisabledModuleDependency(target android.Module) bool {
1836 return true
1837}
1838
1839var _ android.AllowDisabledModuleDependency = (*sanitizerLibraryDependencyTag)(nil)
1840
1841func (txt *sanitizerLibrariesTxtModule) DepsMutator(actx android.BottomUpMutatorContext) {
1842 targets := actx.Config().Targets[android.Android]
1843 depTag := sanitizerLibraryDependencyTag{}
1844
1845 for _, target := range targets {
1846 variation := append(target.Variations(),
1847 blueprint.Variation{Mutator: "image", Variation: ""},
1848 blueprint.Variation{Mutator: "sdk", Variation: ""},
1849 blueprint.Variation{Mutator: "link", Variation: "shared"},
1850 )
1851 for _, lib := range android.SortedStringValues(sanitizerVariables) {
1852 if actx.OtherModuleFarDependencyVariantExists(variation, lib) {
1853 actx.AddFarVariationDependencies(variation, depTag, lib)
1854 }
1855
1856 prebuiltLibName := "prebuilt_" + lib
1857 if actx.OtherModuleFarDependencyVariantExists(variation, prebuiltLibName) {
1858 actx.AddFarVariationDependencies(variation, depTag, prebuiltLibName)
1859 }
1860 }
1861 }
1862
1863}
1864
1865func (txt *sanitizerLibrariesTxtModule) getSanitizerLibs(ctx android.ModuleContext) string {
1866 var sanitizerLibStems []string
1867
1868 ctx.VisitDirectDepsIf(func(m android.Module) bool {
1869 if !m.Enabled(ctx) {
1870 return false
1871 }
1872
1873 ccModule, _ := m.(*Module)
1874 if ccModule == nil || ccModule.library == nil || !ccModule.library.shared() {
1875 return false
1876 }
1877
1878 targets := ctx.Config().Targets[android.Android]
1879
1880 for _, target := range targets {
1881 if m.Target().Os == target.Os && m.Target().Arch.ArchType == target.Arch.ArchType {
1882 return true
1883 }
1884 }
1885
1886 return false
1887 }, func(m android.Module) {
1888 ccModule, _ := m.(*Module)
1889 outputFile := ccModule.outputFile
1890 if outputFile.Valid() {
1891 sanitizerLibStems = append(sanitizerLibStems, outputFile.Path().Base())
1892 }
1893 })
1894
1895 sanitizerLibStems = android.SortedUniqueStrings(sanitizerLibStems)
1896 return strings.Join(sanitizerLibStems, "\n")
1897}
1898
1899func (txt *sanitizerLibrariesTxtModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1900 filename := txt.Name()
1901
1902 txt.outputFile = android.PathForModuleOut(ctx, filename).OutputPath
1903 android.WriteFileRule(ctx, txt.outputFile, txt.getSanitizerLibs(ctx))
1904
1905 installPath := android.PathForModuleInstall(ctx, "etc")
1906 ctx.InstallFile(installPath, filename, txt.outputFile)
mrziwange2346b82024-06-10 15:09:45 -07001907
1908 ctx.SetOutputFiles(android.Paths{txt.outputFile}, "")
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001909}
1910
1911func (txt *sanitizerLibrariesTxtModule) AndroidMkEntries() []android.AndroidMkEntries {
1912 return []android.AndroidMkEntries{{
1913 Class: "ETC",
1914 OutputFile: android.OptionalPathForPath(txt.outputFile),
1915 }}
1916}
1917
1918// PrebuiltEtcModule interface
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001919func (txt *sanitizerLibrariesTxtModule) BaseDir() string {
1920 return "etc"
1921}
1922
1923// PrebuiltEtcModule interface
1924func (txt *sanitizerLibrariesTxtModule) SubDir() string {
1925 return ""
1926}