blob: e6f00faa89a65d68aca849eab09eb5b05fc32ed0 [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 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070040
Florian Mayera9984462023-06-16 16:48:51 -070041 // DO NOT ADD MLLVM FLAGS HERE! ADD THEM BELOW TO hwasanCommonFlags.
Yi Kong286abc62021-11-04 16:14:14 +080042 hwasanCflags = []string{
43 "-fno-omit-frame-pointer",
44 "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000045 "-fsanitize-hwaddress-abi=platform",
Yi Kong286abc62021-11-04 16:14:14 +080046 }
47
48 // ThinLTO performs codegen during link time, thus these flags need to
49 // passed to both CFLAGS and LDFLAGS.
50 hwasanCommonflags = []string{
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080051 // The following improves debug location information
52 // availability at the cost of its accuracy. It increases
53 // the likelihood of a stack variable's frame offset
54 // to be recorded in the debug info, which is important
55 // for the quality of hwasan reports. The downside is a
56 // higher number of "optimized out" stack variables.
57 // b/112437883.
Yi Kong286abc62021-11-04 16:14:14 +080058 "-instcombine-lower-dbg-declare=0",
Florian Mayerc7466192023-06-16 16:50:59 -070059 "-dom-tree-reachability-max-bbs-to-explore=128",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080060 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070061
Trevor Radcliffeded095c2023-06-12 19:18:28 +000062 sanitizeIgnorelistPrefix = "-fsanitize-ignorelist="
63
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000064 cfiBlocklistPath = "external/compiler-rt/lib/cfi"
65 cfiBlocklistFilename = "cfi_blocklist.txt"
Trevor Radcliffef1836e42023-06-01 21:12:08 +000066 cfiEnableFlag = "-fsanitize=cfi"
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +000067 cfiCrossDsoFlag = "-fsanitize-cfi-cross-dso"
68 cfiCflags = []string{"-flto", cfiCrossDsoFlag,
Trevor Radcliffeded095c2023-06-12 19:18:28 +000069 sanitizeIgnorelistPrefix + cfiBlocklistPath + "/" + cfiBlocklistFilename}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070070 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
71 // used, but have no effect on assembly files
72 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Trevor Radcliffef1836e42023-06-01 21:12:08 +000073 cfiLdflags = []string{"-flto", cfiCrossDsoFlag, cfiEnableFlag,
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070074 "-Wl,-plugin-opt,O1"}
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000075 cfiExportsMapPath = "build/soong/cc/config"
76 cfiExportsMapFilename = "cfi_exports.map"
77 cfiAssemblySupportFlag = "-fno-sanitize-cfi-canonical-jump-tables"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070078
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070079 intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080080
Peter Collingbournebd19db02019-03-06 10:38:48 -080081 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070082 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070083 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
Christopher Ferris2fc8e032023-01-26 14:19:27 -080084 "export_memory_stats=0", "max_malloc_fill_size=131072", "malloc_fill_byte=0"}
Florian Mayerab97e282024-04-30 17:34:58 -070085 memtagStackCommonFlags = []string{"-march=armv8-a+memtag"}
Ivan Lozano2a2d5092024-05-03 12:10:04 -040086 memtagStackLlvmFlags = []string{"-dom-tree-reachability-max-bbs-to-explore=128"}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +000087
88 hostOnlySanitizeFlags = []string{"-fno-sanitize-recover=all"}
Elliott Hughes3bba0e42023-10-05 14:50:48 -070089 deviceOnlySanitizeFlags = []string{"-fsanitize-trap=all"}
Trevor Radcliffeda64d912023-08-02 20:24:29 +000090
91 noSanitizeLinkRuntimeFlag = "-fno-sanitize-link-runtime"
Dan Willemsencbceaab2016-10-13 16:44:07 -070092)
93
Ivan Lozano3968d8f2020-12-14 11:27:52 -050094type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080095
Colin Cross16b23492016-01-06 14:41:07 -080096const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050097 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070098 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080099 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700100 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800101 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500102 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -0400103 Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +0000104 Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200105 Memtag_globals
Liz Kammer75db9312021-07-07 16:41:50 -0400106 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -0800107)
108
Liz Kammer75db9312021-07-07 16:41:50 -0400109var Sanitizers = []SanitizerType{
110 Asan,
111 Hwasan,
112 tsan,
113 intOverflow,
114 scs,
115 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400116 Memtag_heap,
Florian Mayerd8434a42022-08-31 20:57:03 +0000117 Memtag_stack,
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200118 Memtag_globals,
Liz Kammer75db9312021-07-07 16:41:50 -0400119 cfi, // cfi is last to prevent it running before incompatible mutators
120}
121
Jiyong Park82226632019-02-01 10:50:50 +0900122// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500123func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800124 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500125 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800126 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700127 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700128 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800129 case tsan:
130 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700131 case intOverflow:
132 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000133 case cfi:
134 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800135 case scs:
136 return "scs"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400137 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700138 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000139 case Memtag_stack:
140 return "memtag_stack"
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200141 case Memtag_globals:
142 return "memtag_globals"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500143 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700144 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800145 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500146 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800147 }
148}
149
Jiyong Park82226632019-02-01 10:50:50 +0900150// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500151func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900152 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500153 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900154 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700155 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900156 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400157 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700158 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000159 case Memtag_stack:
160 return "memtag_stack"
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200161 case Memtag_globals:
162 return "memtag_globals"
Jiyong Park82226632019-02-01 10:50:50 +0900163 case tsan:
164 return "thread"
165 case intOverflow:
166 return "integer_overflow"
167 case cfi:
168 return "cfi"
169 case scs:
170 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500171 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700172 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900173 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500174 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900175 }
176}
177
Liz Kammer75db9312021-07-07 16:41:50 -0400178func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
179 switch t {
Florian Mayer5ccc4212024-03-27 13:53:39 -0700180 case cfi, Hwasan, Asan, tsan, Fuzzer, scs, Memtag_stack:
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200181 sanitizer := &sanitizerSplitMutator{t}
182 ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
183 ctx.Transition(t.variationName(), sanitizer)
Florian Mayer5ccc4212024-03-27 13:53:39 -0700184 case Memtag_heap, Memtag_globals, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400185 // do nothing
186 default:
187 panic(fmt.Errorf("unknown SanitizerType %d", t))
188 }
189}
190
Liz Kammerfd8a49f2022-10-31 10:31:11 -0400191// shouldPropagateToSharedLibraryDeps returns whether a sanitizer type should propagate to share
192// dependencies. In most cases, sanitizers only propagate to static dependencies; however, some
193// sanitizers also must be enabled for shared libraries for linking.
194func (t SanitizerType) shouldPropagateToSharedLibraryDeps() bool {
195 switch t {
196 case Fuzzer:
197 // Typically, shared libs are not split. However, for fuzzer, we split even for shared libs
198 // because a library sanitized for fuzzer can't be linked from a library that isn't sanitized
199 // for fuzzer.
200 return true
201 default:
202 return false
203 }
204}
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500205func (*Module) SanitizerSupported(t SanitizerType) bool {
206 switch t {
207 case Asan:
208 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700209 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500210 return true
211 case tsan:
212 return true
213 case intOverflow:
214 return true
215 case cfi:
216 return true
217 case scs:
218 return true
219 case Fuzzer:
220 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400221 case Memtag_heap:
222 return true
Florian Mayerd8434a42022-08-31 20:57:03 +0000223 case Memtag_stack:
224 return true
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200225 case Memtag_globals:
226 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500227 default:
228 return false
229 }
230}
231
232// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
233func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700234 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900235}
236
Martin Stjernholmb0249572020-09-15 02:32:35 +0100237type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400238 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100239 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800240
Liz Kammer75b9b402021-06-25 15:19:27 -0400241 // ASan (Address sanitizer), incompatible with static binaries.
242 // Always runs in a diagnostic mode.
243 // Use of address sanitizer disables cfi sanitizer.
244 // Hwaddress sanitizer takes precedence over this sanitizer.
245 Address *bool `android:"arch_variant"`
246 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
247 // Always runs in a diagnostic mode.
248 // Use of thread sanitizer disables cfi and scudo sanitizers.
249 // Hwaddress sanitizer takes precedence over this sanitizer.
250 Thread *bool `android:"arch_variant"`
251 // HWASan (Hardware Address sanitizer).
252 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100253 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800254
Liz Kammer75b9b402021-06-25 15:19:27 -0400255 // Undefined behavior sanitizer
256 All_undefined *bool `android:"arch_variant"`
257 // Subset of undefined behavior sanitizer
258 Undefined *bool `android:"arch_variant"`
259 // List of specific undefined behavior sanitizers to enable
260 Misc_undefined []string `android:"arch_variant"`
261 // Fuzzer, incompatible with static binaries.
262 Fuzzer *bool `android:"arch_variant"`
263 // safe-stack sanitizer, incompatible with 32-bit architectures.
264 Safestack *bool `android:"arch_variant"`
265 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
266 Cfi *bool `android:"arch_variant"`
267 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
268 Integer_overflow *bool `android:"arch_variant"`
269 // scudo sanitizer, incompatible with asan, hwasan, tsan
270 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
271 // deprecated
272 Scudo *bool `android:"arch_variant"`
Elliott Hughese4793bc2023-02-09 21:15:47 +0000273 // shadow-call-stack sanitizer, only available on arm64/riscv64.
Liz Kammer75b9b402021-06-25 15:19:27 -0400274 Scs *bool `android:"arch_variant"`
275 // Memory-tagging, only available on arm64
276 // if diag.memtag unset or false, enables async memory tagging
Florian Mayer00ab5cf2022-08-31 18:30:18 +0000277 Memtag_heap *bool `android:"arch_variant"`
Florian Mayerd8434a42022-08-31 20:57:03 +0000278 // Memory-tagging stack instrumentation, only available on arm64
279 // Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
280 Memtag_stack *bool `android:"arch_variant"`
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200281 // Memory-tagging globals instrumentation, only available on arm64
282 // Adds instrumentation to detect global buffer overflows using MTE.
283 Memtag_globals *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100284
285 // A modifier for ASAN and HWASAN for write only instrumentation
286 Writeonly *bool `android:"arch_variant"`
287
288 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
289 // Replaces abort() on error with a human-readable error message.
290 // Address and Thread sanitizers always run in diagnostic mode.
291 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400292 // Undefined behavior sanitizer, diagnostic mode
293 Undefined *bool `android:"arch_variant"`
294 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
295 Cfi *bool `android:"arch_variant"`
296 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
297 Integer_overflow *bool `android:"arch_variant"`
298 // Memory-tagging, only available on arm64
299 // requires sanitizer.memtag: true
300 // if set, enables sync memory tagging
301 Memtag_heap *bool `android:"arch_variant"`
302 // List of specific undefined behavior sanitizers to enable in diagnostic mode
303 Misc_undefined []string `android:"arch_variant"`
304 // List of sanitizers to pass to -fno-sanitize-recover
305 // results in only the first detected error for these sanitizers being reported and program then
306 // exits with a non-zero exit code.
307 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800308 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800309
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800310 // Sanitizers to run with flag configuration specified
311 Config struct {
312 // Enables CFI support flags for assembly-heavy libraries
313 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800314 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800315
Liz Kammer75b9b402021-06-25 15:19:27 -0400316 // List of sanitizers to pass to -fsanitize-recover
317 // allows execution to continue for these sanitizers to detect multiple errors rather than only
318 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100319 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000320
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700321 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100322 Blocklist *string
323}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700324
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400325type sanitizeMutatedProperties struct {
326 // Whether sanitizers can be enabled on this module
327 Never *bool `blueprint:"mutated"`
328
329 // Whether ASan (Address sanitizer) is enabled for this module.
330 // Hwaddress sanitizer takes precedence over this sanitizer.
331 Address *bool `blueprint:"mutated"`
332 // Whether TSan (Thread sanitizer) is enabled for this module
333 Thread *bool `blueprint:"mutated"`
334 // Whether HWASan (Hardware Address sanitizer) is enabled for this module
335 Hwaddress *bool `blueprint:"mutated"`
336
337 // Whether Undefined behavior sanitizer is enabled for this module
338 All_undefined *bool `blueprint:"mutated"`
339 // Whether undefined behavior sanitizer subset is enabled for this module
340 Undefined *bool `blueprint:"mutated"`
341 // List of specific undefined behavior sanitizers enabled for this module
342 Misc_undefined []string `blueprint:"mutated"`
343 // Whether Fuzzeris enabled for this module
344 Fuzzer *bool `blueprint:"mutated"`
345 // whether safe-stack sanitizer is enabled for this module
346 Safestack *bool `blueprint:"mutated"`
347 // Whether cfi sanitizer is enabled for this module
348 Cfi *bool `blueprint:"mutated"`
349 // Whether signed/unsigned integer overflow sanitizer is enabled for this module
350 Integer_overflow *bool `blueprint:"mutated"`
351 // Whether scudo sanitizer is enabled for this module
352 Scudo *bool `blueprint:"mutated"`
353 // Whether shadow-call-stack sanitizer is enabled for this module.
354 Scs *bool `blueprint:"mutated"`
355 // Whether Memory-tagging is enabled for this module
356 Memtag_heap *bool `blueprint:"mutated"`
357 // Whether Memory-tagging stack instrumentation is enabled for this module
358 Memtag_stack *bool `blueprint:"mutated"`
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200359 // Whether Memory-tagging globals instrumentation is enabled for this module
360 Memtag_globals *bool `android:"arch_variant"`
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400361
362 // Whether a modifier for ASAN and HWASAN for write only instrumentation is enabled for this
363 // module
364 Writeonly *bool `blueprint:"mutated"`
365
366 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
367 Diag struct {
368 // Whether Undefined behavior sanitizer, diagnostic mode is enabled for this module
369 Undefined *bool `blueprint:"mutated"`
370 // Whether cfi sanitizer, diagnostic mode is enabled for this module
371 Cfi *bool `blueprint:"mutated"`
372 // Whether signed/unsigned integer overflow sanitizer, diagnostic mode is enabled for this
373 // module
374 Integer_overflow *bool `blueprint:"mutated"`
375 // Whether Memory-tagging, diagnostic mode is enabled for this module
376 Memtag_heap *bool `blueprint:"mutated"`
377 // List of specific undefined behavior sanitizers enabled in diagnostic mode
378 Misc_undefined []string `blueprint:"mutated"`
379 } `blueprint:"mutated"`
380}
381
Martin Stjernholmb0249572020-09-15 02:32:35 +0100382type SanitizeProperties struct {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400383 Sanitize SanitizeUserProps `android:"arch_variant"`
384 SanitizeMutated sanitizeMutatedProperties `blueprint:"mutated"`
385
Colin Cross694fced2024-06-25 14:56:42 -0700386 // ForceDisable is set by the version mutator to disable sanitization of stubs variants
387 ForceDisable bool `blueprint:"mutated"`
388
389 // SanitizerEnabled is set by begin() if any of the sanitize boolean properties are set after
390 // applying the logic that enables globally enabled sanitizers and disables any unsupported
391 // sanitizers.
392 // TODO(b/349906293): this has some unintuitive behavior. It is set in begin() before the sanitize
393 // mutator is run if any of the individual sanitizes properties are set, and then the individual
394 // sanitize properties are cleared in the non-sanitized variants, but this value is never cleared.
395 // That results in SanitizerEnabled being set in variants that have no sanitizers enabled, causing
396 // some of the sanitizer logic in flags() to be applied to the non-sanitized variant.
397 SanitizerEnabled bool `blueprint:"mutated"`
398
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400399 MinimalRuntimeDep bool `blueprint:"mutated"`
400 BuiltinsDep bool `blueprint:"mutated"`
401 UbsanRuntimeDep bool `blueprint:"mutated"`
402 InSanitizerDir bool `blueprint:"mutated"`
403 Sanitizers []string `blueprint:"mutated"`
404 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800405}
406
407type sanitize struct {
408 Properties SanitizeProperties
409}
410
Cindy Zhou18417cb2020-12-10 07:12:38 -0800411// Mark this tag with a check to see if apex dependency check should be skipped
412func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
413 return t.skipApexAllowedDependenciesCheck
414}
415
416var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
417
Vishwath Mohane7128792017-11-17 11:08:10 -0800418func init() {
Cole Faust8982b1c2024-04-08 16:54:45 -0700419 pctx.StaticVariable("HostOnlySanitizeFlags", strings.Join(hostOnlySanitizeFlags, " "))
Trevor Radcliffeda64d912023-08-02 20:24:29 +0000420
Vishwath Mohane7128792017-11-17 11:08:10 -0800421 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700422 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Florian Mayer5ccc4212024-03-27 13:53:39 -0700423 android.RegisterMakeVarsProvider(pctx, memtagStackMakeVarsProvider)
Kiyoung Kim0d8908c2024-05-07 14:47:35 +0900424
425 RegisterSanitizerLibrariesTxtType(android.InitRegistrationContext)
Vishwath Mohane7128792017-11-17 11:08:10 -0800426}
427
Colin Cross16b23492016-01-06 14:41:07 -0800428func (sanitize *sanitize) props() []interface{} {
429 return []interface{}{&sanitize.Properties}
430}
431
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400432func (p *sanitizeMutatedProperties) copyUserPropertiesToMutated(userProps *SanitizeUserProps) {
433 p.Never = userProps.Never
434 p.Address = userProps.Address
435 p.All_undefined = userProps.All_undefined
436 p.Cfi = userProps.Cfi
437 p.Fuzzer = userProps.Fuzzer
438 p.Hwaddress = userProps.Hwaddress
439 p.Integer_overflow = userProps.Integer_overflow
440 p.Memtag_heap = userProps.Memtag_heap
441 p.Memtag_stack = userProps.Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200442 p.Memtag_globals = userProps.Memtag_globals
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400443 p.Safestack = userProps.Safestack
444 p.Scs = userProps.Scs
445 p.Scudo = userProps.Scudo
446 p.Thread = userProps.Thread
447 p.Undefined = userProps.Undefined
448 p.Writeonly = userProps.Writeonly
449
450 p.Misc_undefined = make([]string, 0, len(userProps.Misc_undefined))
451 for _, v := range userProps.Misc_undefined {
452 p.Misc_undefined = append(p.Misc_undefined, v)
453 }
454
455 p.Diag.Cfi = userProps.Diag.Cfi
456 p.Diag.Integer_overflow = userProps.Diag.Integer_overflow
457 p.Diag.Memtag_heap = userProps.Diag.Memtag_heap
458 p.Diag.Undefined = userProps.Diag.Undefined
459
460 p.Diag.Misc_undefined = make([]string, 0, len(userProps.Diag.Misc_undefined))
461 for _, v := range userProps.Diag.Misc_undefined {
462 p.Diag.Misc_undefined = append(p.Diag.Misc_undefined, v)
463 }
464}
465
Colin Cross16b23492016-01-06 14:41:07 -0800466func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400467 s := &sanitize.Properties.SanitizeMutated
468 s.copyUserPropertiesToMutated(&sanitize.Properties.Sanitize)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700469
Colin Cross694fced2024-06-25 14:56:42 -0700470 if sanitize.Properties.ForceDisable {
471 return
472 }
473
Colin Cross16b23492016-01-06 14:41:07 -0800474 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700475 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800476 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800477 }
478
479 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800480 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800481 return
482 }
483
Florian Mayerd8434a42022-08-31 20:57:03 +0000484 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400485 if ctx.testBinary() {
486 if s.Memtag_heap == nil {
487 s.Memtag_heap = proptools.BoolPtr(true)
488 }
489 if s.Diag.Memtag_heap == nil {
490 s.Diag.Memtag_heap = proptools.BoolPtr(true)
491 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800492 }
493
Colin Cross16b23492016-01-06 14:41:07 -0800494 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700495 var globalSanitizersDiag []string
496
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700497 if ctx.Host() {
498 if !ctx.Windows() {
499 globalSanitizers = ctx.Config().SanitizeHost()
500 }
501 } else {
502 arches := ctx.Config().SanitizeDeviceArch()
503 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
504 globalSanitizers = ctx.Config().SanitizeDevice()
505 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800506 }
507 }
508
Colin Cross16b23492016-01-06 14:41:07 -0800509 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000510 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700511 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400512 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000513 }
Colin Cross16b23492016-01-06 14:41:07 -0800514
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700515 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400516 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000517 }
518
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700519 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400520 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000521 }
522
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700523 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400524 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000525 }
526
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700527 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400528 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700529 }
530
531 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400532 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000533 }
534
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700535 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800536 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400537 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700538 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700539 }
540
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700541 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700542 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700543 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400544 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700545 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700546 }
547
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700548 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400549 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700550 }
551
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700552 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Tomislav Novakf734f002023-08-22 10:51:44 -0700553 if !ctx.Config().HWASanDisabledForPath(ctx.ModuleDir()) {
554 s.Hwaddress = proptools.BoolPtr(true)
555 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700556 }
557
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000558 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
559 // Hwaddress and Address are set before, so we can check them here
560 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
561 if s.Address == nil && s.Hwaddress == nil {
562 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
563 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400564 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000565 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700566 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800567 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400568 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800569 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700570 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000571
Florian Mayerd8434a42022-08-31 20:57:03 +0000572 if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
573 s.Memtag_stack = proptools.BoolPtr(true)
574 }
575
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200576 if found, globalSanitizers = removeFromList("memtag_globals", globalSanitizers); found && s.Memtag_globals == nil {
577 s.Memtag_globals = proptools.BoolPtr(true)
578 }
579
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000580 if len(globalSanitizers) > 0 {
581 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
582 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700583
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700584 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700585 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700586 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400587 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700588 }
589
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700590 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
591 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400592 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700593 }
594
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800595 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
596 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400597 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800598 }
599
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700600 if len(globalSanitizersDiag) > 0 {
601 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
602 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700603 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700604
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800605 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross88a029f2022-06-23 14:51:20 -0700606 if ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800607 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800608 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400609 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800610 }
611 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400612 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800613 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800614 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800615 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400616 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800617 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800618 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700619 }
620
Hang Lua98aab92023-03-17 13:17:22 +0800621 // Enable HWASan for all components in the include paths (for Aarch64 only)
622 if s.Hwaddress == nil && ctx.Config().HWASanEnabledForPath(ctx.ModuleDir()) &&
623 ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
624 s.Hwaddress = proptools.BoolPtr(true)
625 }
626
Elvis Chien9c993542021-06-25 01:15:17 +0800627 // Enable CFI for non-host components in the include paths
628 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400629 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000630 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400631 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700632 }
633 }
634
Elliott Hughesda3a0712020-03-06 16:55:28 -0800635 // Is CFI actually enabled?
636 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400637 s.Cfi = nil
638 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800639 }
640
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700641 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross88a029f2022-06-23 14:51:20 -0700642 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700643 s.Hwaddress = nil
644 }
645
Elliott Hughese4793bc2023-02-09 21:15:47 +0000646 // SCS is only implemented on AArch64/riscv64.
647 if (ctx.Arch().ArchType != android.Arm64 && ctx.Arch().ArchType != android.Riscv64) || !ctx.toolchain().Bionic() {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800648 s.Scs = nil
649 }
650
Ivan Lozano62cd0382021-11-01 10:27:54 -0400651 // Memtag_heap is only implemented on AArch64.
Florian Mayerd8434a42022-08-31 20:57:03 +0000652 // Memtag ABI is Android specific for now, so disable for host.
653 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() || ctx.Host() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700654 s.Memtag_heap = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000655 s.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200656 s.Memtag_globals = nil
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700657 }
658
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700659 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700660 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400661 s.Cfi = nil
662 s.Diag.Cfi = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000663 // HWASAN and ASAN win against MTE.
664 s.Memtag_heap = nil
665 s.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200666 s.Memtag_globals = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700667 }
668
Colin Crossed12a042022-02-07 13:55:55 -0800669 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
670 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400671 s.Cfi = nil
672 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700673 s.Misc_undefined = nil
674 s.Undefined = nil
675 s.All_undefined = nil
676 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800677 }
678
Colin Crossed12a042022-02-07 13:55:55 -0800679 // Disable CFI for musl
680 if ctx.toolchain().Musl() {
681 s.Cfi = nil
682 s.Diag.Cfi = nil
683 }
684
Colin Cross390fc742023-05-02 13:02:51 -0700685 // TODO(b/280478629): runtimes don't exist for musl arm64 yet.
686 if ctx.toolchain().Musl() && ctx.Arch().ArchType == android.Arm64 {
687 s.Address = nil
688 s.Hwaddress = nil
689 s.Thread = nil
690 s.Scudo = nil
691 s.Fuzzer = nil
692 s.Cfi = nil
693 s.Diag.Cfi = nil
694 s.Misc_undefined = nil
695 s.Undefined = nil
696 s.All_undefined = nil
697 s.Integer_overflow = nil
698 }
699
Florian Mayer5ccc4212024-03-27 13:53:39 -0700700 if ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery() {
701 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
702 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
703 if !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
704 s.Hwaddress = nil
705 }
706 // Memtag stack in ramdisk makes pKVM unhappy.
707 s.Memtag_stack = nil
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700708 }
709
Colin Cross3c344ef2016-07-18 15:44:56 -0700710 if ctx.staticBinary() {
711 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700712 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700713 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800714 }
715
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700716 if Bool(s.All_undefined) {
717 s.Undefined = nil
718 }
719
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700720 if !ctx.toolchain().Is64Bit() {
721 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700722 s.Thread = nil
723 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800724 // TODO(ccross): error for compile_multilib = "32"?
725 }
726
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800727 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 -0700728 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 +0200729 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack) ||
730 Bool(s.Memtag_globals)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700731 sanitize.Properties.SanitizerEnabled = true
732 }
733
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800734 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
735 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700736 s.Scudo = nil
737 }
738
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700739 if Bool(s.Hwaddress) {
740 s.Address = nil
741 s.Thread = nil
742 }
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000743
744 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
745 // mutually incompatible.
746 if Bool(s.Fuzzer) {
747 s.Cfi = nil
748 }
Colin Cross16b23492016-01-06 14:41:07 -0800749}
750
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800751func toDisableImplicitIntegerChange(flags []string) bool {
752 // Returns true if any flag is fsanitize*integer, and there is
753 // no explicit flag about sanitize=implicit-integer-sign-change.
754 for _, f := range flags {
755 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
756 return false
757 }
758 }
759 for _, f := range flags {
760 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
761 return true
762 }
763 }
764 return false
765}
766
Yabin Cuidb7dda82020-11-30 15:47:45 -0800767func toDisableUnsignedShiftBaseChange(flags []string) bool {
768 // Returns true if any flag is fsanitize*integer, and there is
769 // no explicit flag about sanitize=unsigned-shift-base.
770 for _, f := range flags {
771 if strings.Contains(f, "sanitize=unsigned-shift-base") {
772 return false
773 }
774 }
775 for _, f := range flags {
776 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
777 return true
778 }
779 }
780 return false
781}
782
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400783func (s *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Colin Cross694fced2024-06-25 14:56:42 -0700784 if s.Properties.ForceDisable {
785 return flags
786 }
787
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400788 if !s.Properties.SanitizerEnabled && !s.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800789 return flags
790 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400791 sanProps := &s.Properties.SanitizeMutated
Colin Cross16b23492016-01-06 14:41:07 -0800792
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400793 if Bool(sanProps.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700794 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800795 // Frame pointer based unwinder in ASan requires ARM frame setup.
796 // TODO: put in flags?
797 flags.RequiredInstructionSet = "arm"
798 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800799 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
800 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800801
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400802 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000803 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
804 }
805
Colin Cross16b23492016-01-06 14:41:07 -0800806 if ctx.Host() {
807 // -nodefaultlibs (provided with libc++) prevents the driver from linking
808 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800809 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800810 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800811 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900812 if ctx.bootstrap() {
813 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
814 } else {
815 flags.DynamicLinker = "/system/bin/linker_asan"
816 }
Colin Cross16b23492016-01-06 14:41:07 -0800817 if flags.Toolchain.Is64Bit() {
818 flags.DynamicLinker += "64"
819 }
820 }
Colin Cross16b23492016-01-06 14:41:07 -0800821 }
822
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400823 if Bool(sanProps.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800824 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800825
826 for _, flag := range hwasanCommonflags {
827 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
828 }
829 for _, flag := range hwasanCommonflags {
830 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
831 }
832
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400833 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000834 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
835 }
Florian Mayer95cd6db2023-03-23 17:48:07 -0700836 if !ctx.staticBinary() && !ctx.Host() {
837 if ctx.bootstrap() {
838 flags.DynamicLinker = "/system/bin/bootstrap/linker_hwasan64"
839 } else {
840 flags.DynamicLinker = "/system/bin/linker_hwasan64"
841 }
842 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700843 }
844
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400845 if Bool(sanProps.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800846 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700847
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000848 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
849 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
850 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
851 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
852 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
853
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700854 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
855 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
856 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800857 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
858 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700859
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700860 // Disable fortify for fuzzing builds. Generally, we'll be building with
861 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800862 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800863
864 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
865 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
866 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
867 // the DT_RUNPATH from the shared library above it, and not the executable,
868 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
869 // DT_RUNPATH here means that transient shared libraries can be found
870 // colocated with their parents.
871 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800872 }
873
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400874 if Bool(sanProps.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800875 if ctx.Arch().ArchType == android.Arm {
876 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
877 // to do this on a function basis, so force Thumb on the entire module.
878 flags.RequiredInstructionSet = "thumb"
879 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000880
Colin Cross4af21ed2019-11-04 09:37:55 -0800881 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
882 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Ivan Lozano2a2d5092024-05-03 12:10:04 -0400883 flags.CFlagsDeps = append(flags.CFlagsDeps, android.PathForSource(ctx, cfiBlocklistPath+"/"+cfiBlocklistFilename))
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400884 if Bool(s.Properties.Sanitize.Config.Cfi_assembly_support) {
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000885 flags.Local.CFlags = append(flags.Local.CFlags, cfiAssemblySupportFlag)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800886 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000887 // Only append the default visibility flag if -fvisibility has not already been set
888 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800889 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
890 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000891 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800892 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000893
894 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800895 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
896 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000897 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700898 }
899
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400900 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000901 flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
902 flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
903 flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
Florian Mayerab97e282024-04-30 17:34:58 -0700904
905 for _, flag := range memtagStackLlvmFlags {
906 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
907 }
908 for _, flag := range memtagStackLlvmFlags {
909 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
910 }
Florian Mayerd8434a42022-08-31 20:57:03 +0000911 }
912
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200913 if (Bool(sanProps.Memtag_heap) || Bool(sanProps.Memtag_stack) || Bool(sanProps.Memtag_globals)) && ctx.binary() {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400914 if Bool(sanProps.Diag.Memtag_heap) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000915 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
916 } else {
917 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
918 }
919 }
920
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400921 if Bool(sanProps.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800922 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700923 }
924
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400925 if len(s.Properties.Sanitizers) > 0 {
926 sanitizeArg := "-fsanitize=" + strings.Join(s.Properties.Sanitizers, ",")
Colin Cross4af21ed2019-11-04 09:37:55 -0800927 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
928 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross234b01d2022-02-07 13:49:03 -0800929 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
930
Colin Crossed12a042022-02-07 13:55:55 -0800931 if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
932 // Bionic and musl sanitizer runtimes have already been added as dependencies so that
933 // the right variant of the runtime will be used (with the "-android" or "-musl"
934 // suffixes), so don't let clang the runtime library.
Trevor Radcliffeda64d912023-08-02 20:24:29 +0000935 flags.Local.LdFlags = append(flags.Local.LdFlags, noSanitizeLinkRuntimeFlag)
Colin Cross234b01d2022-02-07 13:49:03 -0800936 } else {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800937 // Host sanitizers only link symbols in the final executable, so
938 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800939 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
Colin Cross6c18d002022-06-02 15:11:50 -0700940 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500941
Colin Cross6c18d002022-06-02 15:11:50 -0700942 if !ctx.toolchain().Bionic() {
943 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function san.
944 // Musl toolchain prebuilts have vptr and function sanitizers, but enabling them
945 // implicitly enables RTTI which causes RTTI mismatch issues with dependencies.
946
Colin Cross234b01d2022-02-07 13:49:03 -0800947 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500948 }
949
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400950 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700951 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800952 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700953 } else if ctx.Host() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000954 flags.Local.CFlags = append(flags.Local.CFlags, hostOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700955 } else {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000956 flags.Local.CFlags = append(flags.Local.CFlags, deviceOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700957 }
Evgenii Stepanov59012812022-06-24 11:09:18 -0700958
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400959 if enableMinimalRuntime(s) {
Evgenii Stepanov59012812022-06-24 11:09:18 -0700960 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
961 }
962
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800963 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800964 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
965 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800966 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800967 // http://b/171275751, Android doesn't build with this sanitizer yet.
968 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
969 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
970 }
Colin Cross16b23492016-01-06 14:41:07 -0800971 }
972
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400973 if len(s.Properties.DiagSanitizers) > 0 {
974 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(s.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700975 }
976 // FIXME: enable RTTI if diag + (cfi or vptr)
977
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400978 if s.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800979 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400980 strings.Join(s.Properties.Sanitize.Recover, ","))
Andreas Gampe97071162017-05-08 13:15:23 -0700981 }
982
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400983 if s.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800984 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400985 strings.Join(s.Properties.Sanitize.Diag.No_recover, ","))
Ivan Lozano7929bba2018-12-12 09:36:31 -0800986 }
987
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400988 blocklist := android.OptionalPathForModuleSrc(ctx, s.Properties.Sanitize.Blocklist)
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700989 if blocklist.Valid() {
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000990 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeIgnorelistPrefix+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700991 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
992 }
993
Colin Cross16b23492016-01-06 14:41:07 -0800994 return flags
995}
996
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400997func (s *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900998 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
999 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -08001000 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001001 if Bool(s.Properties.SanitizeMutated.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -08001002 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +09001003 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001004 if Bool(s.Properties.SanitizeMutated.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -08001005 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +09001006 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001007 if Bool(s.Properties.SanitizeMutated.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -08001008 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +09001009 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001010 }
Colin Cross8ff9ef42017-05-08 13:44:11 -07001011}
1012
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001013func (s *sanitize) inSanitizerDir() bool {
1014 return s.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -07001015}
1016
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001017// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001018func (s *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +00001019 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001020 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001021 return s.Properties.SanitizeMutated.Address
Tri Vo6eafc362021-04-01 11:29:09 -07001022 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001023 return s.Properties.SanitizeMutated.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +00001024 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001025 return s.Properties.SanitizeMutated.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +00001026 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001027 return s.Properties.SanitizeMutated.Integer_overflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001028 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001029 return s.Properties.SanitizeMutated.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001030 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001031 return s.Properties.SanitizeMutated.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -04001032 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001033 return s.Properties.SanitizeMutated.Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +00001034 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001035 return s.Properties.SanitizeMutated.Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001036 case Memtag_globals:
1037 return s.Properties.SanitizeMutated.Memtag_globals
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001038 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001039 return s.Properties.SanitizeMutated.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +00001040 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001041 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +00001042 }
1043}
1044
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001045// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -08001046func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001047 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001048 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -08001049 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001050 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001051 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -04001052 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Florian Mayerd8434a42022-08-31 20:57:03 +00001053 !sanitize.isSanitizerEnabled(Memtag_stack) &&
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001054 !sanitize.isSanitizerEnabled(Memtag_globals) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001055 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -08001056}
1057
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001058// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001059func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001060 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001061 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001062 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001063 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001064}
1065
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001066func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -04001067 bPtr := proptools.BoolPtr(b)
1068 if !b {
1069 bPtr = nil
1070 }
Colin Cross16b23492016-01-06 14:41:07 -08001071 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001072 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001073 sanitize.Properties.SanitizeMutated.Address = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001074 // For ASAN variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001075 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001076 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Tri Vo6eafc362021-04-01 11:29:09 -07001077 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001078 sanitize.Properties.SanitizeMutated.Hwaddress = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001079 // For HWAsan variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001080 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001081 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Colin Cross16b23492016-01-06 14:41:07 -08001082 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001083 sanitize.Properties.SanitizeMutated.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -07001084 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001085 sanitize.Properties.SanitizeMutated.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001086 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001087 sanitize.Properties.SanitizeMutated.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001088 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001089 sanitize.Properties.SanitizeMutated.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -04001090 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001091 sanitize.Properties.SanitizeMutated.Memtag_heap = bPtr
Florian Mayerd8434a42022-08-31 20:57:03 +00001092 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001093 sanitize.Properties.SanitizeMutated.Memtag_stack = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001094 // We do not need to disable ASAN or HWASan here, as there is no Memtag_stack variant.
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001095 case Memtag_globals:
1096 sanitize.Properties.Sanitize.Memtag_globals = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001097 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001098 sanitize.Properties.SanitizeMutated.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -08001099 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001100 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -08001101 }
1102 if b {
1103 sanitize.Properties.SanitizerEnabled = true
1104 }
1105}
1106
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001107// Check if the sanitizer is explicitly disabled (as opposed to nil by
1108// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001109func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001110 if sanitize == nil {
1111 return false
1112 }
1113
1114 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1115 return sanitizerVal != nil && *sanitizerVal == false
1116}
1117
1118// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
1119// because enabling a sanitizer either directly (via the blueprint) or
1120// indirectly (via a mutator) sets the bool ptr to true, and you can't
1121// distinguish between the cases. It isn't needed though - both cases can be
1122// treated identically.
Liz Kammerba23cb62023-09-26 16:48:04 -04001123func (s *sanitize) isSanitizerEnabled(t SanitizerType) bool {
1124 if s == nil {
1125 return false
1126 }
Colin Cross694fced2024-06-25 14:56:42 -07001127 if s.Properties.ForceDisable || proptools.Bool(s.Properties.SanitizeMutated.Never) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001128 return false
1129 }
1130
Liz Kammerba23cb62023-09-26 16:48:04 -04001131 sanitizerVal := s.getSanitizerBoolPtr(t)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001132 return sanitizerVal != nil && *sanitizerVal == true
1133}
1134
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001135// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
1136func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -07001137 switch t := tag.(type) {
1138 case dependencyTag:
1139 return t == reuseObjTag || t == objDepTag
1140 case libraryDependencyTag:
1141 return true
1142 default:
1143 return false
1144 }
Colin Cross6b753602018-06-21 13:03:07 -07001145}
1146
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001147func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
1148 return IsSanitizableDependencyTag
1149}
1150
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001151type sanitizerSplitMutator struct {
1152 sanitizer SanitizerType
1153}
1154
1155// If an APEX is sanitized or not depends on whether it contains at least one
1156// sanitized module. Transition mutators cannot propagate information up the
1157// dependency graph this way, so we need an auxiliary mutator to do so.
1158func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
1159 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1160 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1161 ctx.VisitDirectDeps(func(dep android.Module) {
Ivan Lozano5467a392023-08-23 14:20:25 -04001162 if c, ok := dep.(PlatformSanitizeable); ok && c.IsSanitizerEnabled(s.sanitizer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001163 enabled = true
Inseob Kimc42f2f22020-07-29 20:32:10 +09001164 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001165 })
1166
1167 if enabled {
1168 sanitizeable.EnableSanitizer(s.sanitizer.name())
1169 }
1170 }
1171}
1172
1173func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
1174 if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001175 // If the given sanitizer is not requested in the .bp file for a module, it
1176 // won't automatically build the sanitized variation.
1177 if !c.IsSanitizerEnabled(s.sanitizer) {
1178 return []string{""}
1179 }
1180
1181 if c.Binary() {
1182 // If a sanitizer is enabled for a binary, we do not build the version
1183 // without the sanitizer
1184 return []string{s.sanitizer.variationName()}
1185 } else if c.StaticallyLinked() || c.Header() {
1186 // For static libraries, we build both versions. Some Make modules
1187 // apparently depend on this behavior.
1188 return []string{"", s.sanitizer.variationName()}
1189 } else {
1190 // We only build the requested variation of dynamic libraries
1191 return []string{s.sanitizer.variationName()}
1192 }
1193 }
1194
1195 if _, ok := ctx.Module().(JniSanitizeable); ok {
1196 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1197 // that is short-circuited for now
1198 return []string{""}
1199 }
1200
1201 // If an APEX has a sanitized dependency, we build the APEX in the sanitized
1202 // variation. This is useful because such APEXes require extra dependencies.
1203 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1204 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1205 if enabled {
1206 return []string{s.sanitizer.variationName()}
1207 } else {
1208 return []string{""}
1209 }
1210 }
1211
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001212 return []string{""}
1213}
1214
1215func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1216 if c, ok := ctx.Module().(PlatformSanitizeable); ok {
1217 if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
1218 // If the dependency is through a non-sanitizable tag, use the
1219 // non-sanitized variation
1220 return ""
1221 }
1222
1223 return sourceVariation
1224 } else if _, ok := ctx.Module().(JniSanitizeable); ok {
1225 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1226 // that is short-circuited for now
1227 return ""
1228 } else {
1229 // Otherwise, do not rock the boat.
1230 return sourceVariation
1231 }
1232}
1233
1234func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1235 if d, ok := ctx.Module().(PlatformSanitizeable); ok {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001236 if !d.SanitizePropDefined() ||
1237 d.SanitizeNever() ||
1238 d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
1239 !d.SanitizerSupported(s.sanitizer) {
1240 // If a module opts out of a sanitizer, use its non-sanitized variation
1241 return ""
1242 }
1243
1244 // Binaries are always built in the variation they requested.
1245 if d.Binary() {
1246 if d.IsSanitizerEnabled(s.sanitizer) {
1247 return s.sanitizer.variationName()
1248 } else {
1249 return ""
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001250 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001251 }
1252
1253 // If a shared library requests to be sanitized, it will be built for that
1254 // sanitizer. Otherwise, some sanitizers propagate through shared library
1255 // dependency edges, some do not.
1256 if !d.StaticallyLinked() && !d.Header() {
1257 if d.IsSanitizerEnabled(s.sanitizer) {
1258 return s.sanitizer.variationName()
1259 }
1260
Liz Kammerfd8a49f2022-10-31 10:31:11 -04001261 // Some sanitizers do not propagate to shared dependencies
1262 if !s.sanitizer.shouldPropagateToSharedLibraryDeps() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001263 return ""
1264 }
1265 }
1266
1267 // Static and header libraries inherit whether they are sanitized from the
1268 // module they are linked into
1269 return incomingVariation
1270 } else if d, ok := ctx.Module().(Sanitizeable); ok {
1271 // If an APEX contains a sanitized module, it will be built in the variation
1272 // corresponding to that sanitizer.
1273 enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1274 if enabled {
1275 return s.sanitizer.variationName()
1276 }
1277
1278 return incomingVariation
1279 }
1280
1281 return ""
1282}
1283
1284func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
1285 sanitizerVariation := variationName == s.sanitizer.variationName()
1286
1287 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1288 sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
1289
1290 oneMakeVariation := false
1291 if c.StaticallyLinked() || c.Header() {
1292 if s.sanitizer != cfi && s.sanitizer != scs && s.sanitizer != Hwasan {
1293 // These sanitizers export only one variation to Make. For the rest,
1294 // Make targets can depend on both the sanitized and non-sanitized
1295 // versions.
1296 oneMakeVariation = true
1297 }
1298 } else if !c.Binary() {
1299 // Shared library. These are the sanitizers that do propagate through shared
1300 // library dependencies and therefore can cause multiple variations of a
1301 // shared library to be built.
1302 if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != scs && s.sanitizer != Asan {
1303 oneMakeVariation = true
1304 }
1305 }
1306
1307 if oneMakeVariation {
1308 if sanitizerEnabled != sanitizerVariation {
1309 c.SetPreventInstall()
1310 c.SetHideFromMake()
1311 }
1312 }
1313
1314 if sanitizerVariation {
1315 c.SetSanitizer(s.sanitizer, true)
1316
1317 // CFI is incompatible with ASAN so disable it in ASAN variations
1318 if s.sanitizer.incompatibleWithCfi() {
1319 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1320 if mctx.Device() && cfiSupported {
1321 c.SetSanitizer(cfi, false)
Jiyong Parkf97782b2019-02-13 20:28:58 +09001322 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001323 }
1324
1325 // locate the asan libraries under /data/asan
1326 if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
1327 c.SetInSanitizerDir()
1328 }
1329
1330 if c.StaticallyLinked() && c.ExportedToMake() {
1331 if s.sanitizer == Hwasan {
1332 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
1333 } else if s.sanitizer == cfi {
1334 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
Florian Mayer5ccc4212024-03-27 13:53:39 -07001335 } else if s.sanitizer == Memtag_stack {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001336 memtagStackStaticLibs(mctx.Config()).add(c, c.Module().Name())
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001337 }
1338 }
1339 } else if c.IsSanitizerEnabled(s.sanitizer) {
1340 // Disable the sanitizer for the non-sanitized variation
1341 c.SetSanitizer(s.sanitizer, false)
1342 }
1343 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
1344 // If an APEX has sanitized dependencies, it gets a few more dependencies
1345 if sanitizerVariation {
1346 sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
1347 }
Colin Cross16b23492016-01-06 14:41:07 -08001348 }
1349}
1350
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001351func (c *Module) SanitizeNever() bool {
Colin Cross694fced2024-06-25 14:56:42 -07001352 return c.sanitize.Properties.ForceDisable || Bool(c.sanitize.Properties.SanitizeMutated.Never)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001353}
1354
1355func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
1356 return c.sanitize.isSanitizerExplicitlyDisabled(t)
1357}
1358
Ivan Lozano30c5db22018-02-21 15:49:20 -08001359// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001360func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001361 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001362 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Colin Cross694fced2024-06-25 14:56:42 -07001363 if c.sanitize.Properties.ForceDisable {
1364 return
1365 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001366 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001367 mctx.WalkDeps(func(child, parent android.Module) bool {
1368 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1369 return false
1370 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001371
Inseob Kimeec88e12020-01-22 11:11:29 +09001372 d, ok := child.(*Module)
1373 if !ok || !d.static() {
1374 return false
1375 }
Colin Cross694fced2024-06-25 14:56:42 -07001376 if d.sanitize != nil && !d.sanitize.Properties.ForceDisable {
Colin Cross6b753602018-06-21 13:03:07 -07001377 if enableMinimalRuntime(d.sanitize) {
1378 // If a static dependency is built with the minimal runtime,
1379 // make sure we include the ubsan minimal runtime.
1380 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001381 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001382 // If a static dependency runs with full ubsan diagnostics,
1383 // make sure we include the ubsan runtime.
1384 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001385 }
Colin Cross0b908332019-06-19 23:00:20 -07001386
1387 if c.sanitize.Properties.MinimalRuntimeDep &&
1388 c.sanitize.Properties.UbsanRuntimeDep {
1389 // both flags that this mutator might set are true, so don't bother recursing
1390 return false
1391 }
1392
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001393 if c.Os() == android.Linux {
1394 c.sanitize.Properties.BuiltinsDep = true
1395 }
1396
Colin Cross0b908332019-06-19 23:00:20 -07001397 return true
Colin Cross6b753602018-06-21 13:03:07 -07001398 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001399
Inseob Kimeec88e12020-01-22 11:11:29 +09001400 return false
Colin Cross6b753602018-06-21 13:03:07 -07001401 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001402 }
1403}
1404
Jiyong Park379de2f2018-12-19 02:47:14 +09001405// Add the dependency to the runtime library for each of the sanitizer variants
1406func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001407 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Cole Fausta963b942024-04-11 17:43:00 -07001408 if !c.Enabled(mctx) {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001409 return
1410 }
Colin Cross694fced2024-06-25 14:56:42 -07001411 if c.sanitize.Properties.ForceDisable {
1412 return
1413 }
1414
Jiyong Park379de2f2018-12-19 02:47:14 +09001415 var sanitizers []string
1416 var diagSanitizers []string
1417
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001418 sanProps := &c.sanitize.Properties.SanitizeMutated
1419
1420 if Bool(sanProps.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001421 sanitizers = append(sanitizers, "undefined")
1422 } else {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001423 if Bool(sanProps.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001424 sanitizers = append(sanitizers,
1425 "bool",
1426 "integer-divide-by-zero",
1427 "return",
1428 "returns-nonnull-attribute",
1429 "shift-exponent",
1430 "unreachable",
1431 "vla-bound",
1432 // TODO(danalbert): The following checks currently have compiler performance issues.
1433 //"alignment",
1434 //"bounds",
1435 //"enum",
1436 //"float-cast-overflow",
1437 //"float-divide-by-zero",
1438 //"nonnull-attribute",
1439 //"null",
1440 //"shift-base",
1441 //"signed-integer-overflow",
1442 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1443 // https://llvm.org/PR19302
1444 // http://reviews.llvm.org/D6974
1445 // "object-size",
1446 )
1447 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001448 sanitizers = append(sanitizers, sanProps.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001449 }
1450
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001451 if Bool(sanProps.Diag.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001452 diagSanitizers = append(diagSanitizers, "undefined")
1453 }
1454
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001455 diagSanitizers = append(diagSanitizers, sanProps.Diag.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001456
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001457 if Bool(sanProps.Address) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001458 sanitizers = append(sanitizers, "address")
1459 diagSanitizers = append(diagSanitizers, "address")
1460 }
1461
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001462 if Bool(sanProps.Hwaddress) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001463 sanitizers = append(sanitizers, "hwaddress")
1464 }
1465
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001466 if Bool(sanProps.Thread) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001467 sanitizers = append(sanitizers, "thread")
1468 }
1469
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001470 if Bool(sanProps.Safestack) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001471 sanitizers = append(sanitizers, "safe-stack")
1472 }
1473
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001474 if Bool(sanProps.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001475 sanitizers = append(sanitizers, "cfi")
1476
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001477 if Bool(sanProps.Diag.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001478 diagSanitizers = append(diagSanitizers, "cfi")
1479 }
1480 }
1481
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001482 if Bool(sanProps.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001483 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1484 sanitizers = append(sanitizers, "signed-integer-overflow")
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001485 if Bool(sanProps.Diag.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001486 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1487 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1488 }
1489 }
1490
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001491 if Bool(sanProps.Scudo) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001492 sanitizers = append(sanitizers, "scudo")
1493 }
1494
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001495 if Bool(sanProps.Scs) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001496 sanitizers = append(sanitizers, "shadow-call-stack")
1497 }
1498
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001499 if Bool(sanProps.Memtag_heap) && c.Binary() {
Florian Mayerd8434a42022-08-31 20:57:03 +00001500 sanitizers = append(sanitizers, "memtag-heap")
1501 }
1502
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001503 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +00001504 sanitizers = append(sanitizers, "memtag-stack")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001505 }
1506
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001507 if Bool(sanProps.Memtag_globals) {
1508 sanitizers = append(sanitizers, "memtag-globals")
1509 // TODO(mitchp): For now, enable memtag-heap with memtag-globals because the linker
1510 // isn't new enough (https://reviews.llvm.org/differential/changeset/?ref=4243566).
1511 sanitizers = append(sanitizers, "memtag-heap")
1512 }
1513
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001514 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001515 sanitizers = append(sanitizers, "fuzzer-no-link")
1516 }
1517
Jiyong Park379de2f2018-12-19 02:47:14 +09001518 // Save the list of sanitizers. These will be used again when generating
1519 // the build rules (for Cflags, etc.)
1520 c.sanitize.Properties.Sanitizers = sanitizers
1521 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1522
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001523 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1524 if c.Host() {
1525 diagSanitizers = sanitizers
1526 }
1527
Colin Crosse323a792023-02-15 13:57:57 -08001528 addStaticDeps := func(dep string, hideSymbols bool) {
Colin Cross06c80eb2022-02-10 10:34:19 -08001529 // static executable gets static runtime libs
Colin Crosse323a792023-02-15 13:57:57 -08001530 depTag := libraryDependencyTag{Kind: staticLibraryDependency, unexportedSymbols: hideSymbols}
Colin Cross06c80eb2022-02-10 10:34:19 -08001531 variations := append(mctx.Target().Variations(),
1532 blueprint.Variation{Mutator: "link", Variation: "static"})
1533 if c.Device() {
1534 variations = append(variations, c.ImageVariation())
1535 }
1536 if c.UseSdk() {
1537 variations = append(variations,
1538 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1539 }
Colin Crosse323a792023-02-15 13:57:57 -08001540 mctx.AddFarVariationDependencies(variations, depTag, dep)
Colin Cross06c80eb2022-02-10 10:34:19 -08001541 }
Colin Crosse323a792023-02-15 13:57:57 -08001542
1543 // Determine the runtime library required
1544 runtimeSharedLibrary := ""
1545 toolchain := c.toolchain(mctx)
1546 if Bool(sanProps.Address) {
Colin Crossb781d232023-02-15 12:40:20 -08001547 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1548 // Use a static runtime for musl to match what clang does for glibc.
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001549 addStaticDeps(config.AddressSanitizerStaticRuntimeLibrary(), false)
1550 addStaticDeps(config.AddressSanitizerCXXStaticRuntimeLibrary(), false)
Colin Crossb781d232023-02-15 12:40:20 -08001551 } else {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001552 runtimeSharedLibrary = config.AddressSanitizerRuntimeLibrary()
Colin Crossb781d232023-02-15 12:40:20 -08001553 }
Colin Crosse323a792023-02-15 13:57:57 -08001554 } else if Bool(sanProps.Hwaddress) {
1555 if c.staticBinary() {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001556 addStaticDeps(config.HWAddressSanitizerStaticLibrary(), true)
Colin Crosse323a792023-02-15 13:57:57 -08001557 addStaticDeps("libdl", false)
1558 } else {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001559 runtimeSharedLibrary = config.HWAddressSanitizerRuntimeLibrary()
Colin Crosse323a792023-02-15 13:57:57 -08001560 }
1561 } else if Bool(sanProps.Thread) {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001562 runtimeSharedLibrary = config.ThreadSanitizerRuntimeLibrary()
Colin Crosse323a792023-02-15 13:57:57 -08001563 } else if Bool(sanProps.Scudo) {
1564 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001565 runtimeSharedLibrary = config.ScudoMinimalRuntimeLibrary()
Colin Crosse323a792023-02-15 13:57:57 -08001566 } else {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001567 runtimeSharedLibrary = config.ScudoRuntimeLibrary()
Colin Crosse323a792023-02-15 13:57:57 -08001568 }
1569 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
1570 Bool(sanProps.Fuzzer) ||
1571 Bool(sanProps.Undefined) ||
1572 Bool(sanProps.All_undefined) {
Colin Cross0df81532023-08-23 22:20:51 -07001573 if toolchain.Musl() || c.staticBinary() {
1574 // Use a static runtime for static binaries. For sanitized glibc binaries the runtime is
1575 // added automatically by clang, but for static glibc binaries that are not sanitized but
1576 // have a sanitized dependency the runtime needs to be added manually.
1577 // Also manually add a static runtime for musl to match what clang does for glibc.
1578 // Otherwise dlopening libraries that depend on libclang_rt.ubsan_standalone.so fails with:
Colin Crosse323a792023-02-15 13:57:57 -08001579 // Error relocating ...: initial-exec TLS resolves to dynamic definition
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001580 addStaticDeps(config.UndefinedBehaviorSanitizerRuntimeLibrary()+".static", true)
Colin Crosse323a792023-02-15 13:57:57 -08001581 } else {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001582 runtimeSharedLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary()
Colin Crosse323a792023-02-15 13:57:57 -08001583 }
1584 }
1585
Colin Cross06c80eb2022-02-10 10:34:19 -08001586 if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001587 addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001588 }
1589 if c.sanitize.Properties.BuiltinsDep {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001590 addStaticDeps(config.BuiltinsRuntimeLibrary(), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001591 }
1592
Ivan Lozano2a2d5092024-05-03 12:10:04 -04001593 if runtimeSharedLibrary != "" && (toolchain.Bionic() || toolchain.Musl()) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001594 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001595
1596 // Adding dependency to the runtime library. We are using *FarVariation*
1597 // because the runtime libraries themselves are not mutated by sanitizer
1598 // mutators and thus don't have sanitizer variants whereas this module
1599 // has been already mutated.
1600 //
1601 // Note that by adding dependency with {static|shared}DepTag, the lib is
1602 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
Colin Crosse323a792023-02-15 13:57:57 -08001603 if c.staticBinary() {
1604 // Most sanitizers are either disabled for static binaries or have already
1605 // handled the static binary case above through a direct call to addStaticDeps.
1606 // If not, treat the runtime shared library as a static library and hope for
1607 // the best.
1608 addStaticDeps(runtimeSharedLibrary, true)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001609 } else if !c.static() && !c.Header() {
Cindy Zhou18417cb2020-12-10 07:12:38 -08001610 // Skip apex dependency check for sharedLibraryDependency
1611 // when sanitizer diags are enabled. Skipping the check will allow
1612 // building with diag libraries without having to list the
1613 // dependency in Apex's allowed_deps file.
1614 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001615 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001616 depTag := libraryDependencyTag{
1617 Kind: sharedLibraryDependency,
1618 Order: earlyLibraryDependency,
1619
1620 skipApexAllowedDependenciesCheck: diagEnabled,
1621 }
Colin Cross42507332020-08-21 16:15:23 -07001622 variations := append(mctx.Target().Variations(),
1623 blueprint.Variation{Mutator: "link", Variation: "shared"})
1624 if c.Device() {
1625 variations = append(variations, c.ImageVariation())
1626 }
Colin Cross06c80eb2022-02-10 10:34:19 -08001627 if c.UseSdk() {
1628 variations = append(variations,
1629 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1630 }
Colin Crosse323a792023-02-15 13:57:57 -08001631 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeSharedLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001632 }
1633 // static lib does not have dependency to the runtime library. The
1634 // dependency will be added to the executables or shared libs using
1635 // the static lib.
1636 }
1637 }
1638}
1639
1640type Sanitizeable interface {
1641 android.Module
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001642 IsSanitizerEnabled(config android.Config, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001643 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001644 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001645}
1646
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001647type JniSanitizeable interface {
1648 android.Module
1649 IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
1650}
1651
Ivan Lozanod7586b62021-04-01 09:49:36 -04001652func (c *Module) MinimalRuntimeDep() bool {
1653 return c.sanitize.Properties.MinimalRuntimeDep
1654}
1655
1656func (c *Module) UbsanRuntimeDep() bool {
1657 return c.sanitize.Properties.UbsanRuntimeDep
1658}
1659
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001660func (c *Module) SanitizePropDefined() bool {
1661 return c.sanitize != nil
1662}
1663
1664func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1665 return c.sanitize.isSanitizerEnabled(t)
1666}
1667
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001668func (c *Module) StaticallyLinked() bool {
1669 return c.static()
1670}
1671
1672func (c *Module) SetInSanitizerDir() {
1673 if c.sanitize != nil {
1674 c.sanitize.Properties.InSanitizerDir = true
1675 }
1676}
1677
1678func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1679 if c.sanitize != nil {
1680 c.sanitize.SetSanitizer(t, b)
1681 }
1682}
1683
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001684var _ PlatformSanitizeable = (*Module)(nil)
1685
Inseob Kim74d25562020-08-04 00:41:38 +09001686type sanitizerStaticLibsMap struct {
1687 // libsMap contains one list of modules per each image and each arch.
1688 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001689 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001690 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001691 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001692}
1693
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001694func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001695 return &sanitizerStaticLibsMap{
1696 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001697 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001698 }
1699}
1700
1701// Add the current module to sanitizer static libs maps
1702// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001703func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1704 image := GetImageVariantType(c)
1705 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001706
1707 s.libsMapLock.Lock()
1708 defer s.libsMapLock.Unlock()
1709
1710 if _, ok := s.libsMap[image]; !ok {
1711 s.libsMap[image] = make(map[string][]string)
1712 }
1713
1714 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1715}
1716
1717// Exports makefile variables in the following format:
1718// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1719// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1720// These are to be used by use_soong_sanitized_static_libraries.
1721// See build/make/core/binary.mk for more details.
1722func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
Cole Faust18994c72023-02-28 16:02:16 -08001723 for _, image := range android.SortedKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001724 archMap := s.libsMap[ImageVariantType(image)]
Cole Faust18994c72023-02-28 16:02:16 -08001725 for _, arch := range android.SortedKeys(archMap) {
Inseob Kim74d25562020-08-04 00:41:38 +09001726 libs := archMap[arch]
1727 sort.Strings(libs)
1728
1729 key := fmt.Sprintf(
1730 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1731 s.sanitizerType.variationName(),
1732 image, // already upper
1733 arch)
1734
1735 ctx.Strict(key, strings.Join(libs, " "))
1736 }
1737 }
1738}
1739
Colin Cross571cccf2019-02-04 11:22:08 -08001740var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1741
Inseob Kim74d25562020-08-04 00:41:38 +09001742func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001743 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001744 return newSanitizerStaticLibsMap(cfi)
1745 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001746}
1747
Colin Cross571cccf2019-02-04 11:22:08 -08001748var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1749
Inseob Kim74d25562020-08-04 00:41:38 +09001750func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001751 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001752 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001753 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001754}
1755
Florian Mayer5ccc4212024-03-27 13:53:39 -07001756var memtagStackStaticLibsKey = android.NewOnceKey("memtagStackStaticLibs")
1757
1758func memtagStackStaticLibs(config android.Config) *sanitizerStaticLibsMap {
1759 return config.Once(memtagStackStaticLibsKey, func() interface{} {
1760 return newSanitizerStaticLibsMap(Memtag_stack)
1761 }).(*sanitizerStaticLibsMap)
1762}
1763
Ivan Lozano30c5db22018-02-21 15:49:20 -08001764func enableMinimalRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001765 if sanitize.isSanitizerEnabled(Asan) {
1766 return false
1767 } else if sanitize.isSanitizerEnabled(Hwasan) {
1768 return false
1769 } else if sanitize.isSanitizerEnabled(Fuzzer) {
1770 return false
Ivan Lozano30c5db22018-02-21 15:49:20 -08001771 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001772
1773 if enableUbsanRuntime(sanitize) {
1774 return false
1775 }
1776
1777 sanitizeProps := &sanitize.Properties.SanitizeMutated
1778 if Bool(sanitizeProps.Diag.Cfi) {
1779 return false
1780 }
1781
1782 return Bool(sanitizeProps.Integer_overflow) ||
1783 len(sanitizeProps.Misc_undefined) > 0 ||
1784 Bool(sanitizeProps.Undefined) ||
1785 Bool(sanitizeProps.All_undefined)
Ivan Lozano30c5db22018-02-21 15:49:20 -08001786}
1787
Ivan Lozanod7586b62021-04-01 09:49:36 -04001788func (m *Module) UbsanRuntimeNeeded() bool {
1789 return enableUbsanRuntime(m.sanitize)
1790}
1791
1792func (m *Module) MinimalRuntimeNeeded() bool {
1793 return enableMinimalRuntime(m.sanitize)
1794}
1795
Inseob Kim8471cda2019-11-15 09:59:12 +09001796func enableUbsanRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001797 sanitizeProps := &sanitize.Properties.SanitizeMutated
1798 return Bool(sanitizeProps.Diag.Integer_overflow) ||
1799 Bool(sanitizeProps.Diag.Undefined) ||
1800 len(sanitizeProps.Diag.Misc_undefined) > 0
Inseob Kim8471cda2019-11-15 09:59:12 +09001801}
1802
Vishwath Mohane7128792017-11-17 11:08:10 -08001803func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001804 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001805}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001806
1807func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001808 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001809}
Florian Mayer5ccc4212024-03-27 13:53:39 -07001810
1811func memtagStackMakeVarsProvider(ctx android.MakeVarsContext) {
1812 memtagStackStaticLibs(ctx.Config()).exportToMake(ctx)
1813}
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001814
1815type sanitizerLibrariesTxtModule struct {
1816 android.ModuleBase
1817
1818 outputFile android.OutputPath
1819}
1820
1821var _ etc.PrebuiltEtcModule = (*sanitizerLibrariesTxtModule)(nil)
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001822
1823func RegisterSanitizerLibrariesTxtType(ctx android.RegistrationContext) {
1824 ctx.RegisterModuleType("sanitizer_libraries_txt", sanitizerLibrariesTxtFactory)
1825}
1826
1827func sanitizerLibrariesTxtFactory() android.Module {
1828 m := &sanitizerLibrariesTxtModule{}
1829 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
1830 return m
1831}
1832
1833type sanitizerLibraryDependencyTag struct {
1834 blueprint.BaseDependencyTag
1835}
1836
1837func (t sanitizerLibraryDependencyTag) AllowDisabledModuleDependency(target android.Module) bool {
1838 return true
1839}
1840
1841var _ android.AllowDisabledModuleDependency = (*sanitizerLibraryDependencyTag)(nil)
1842
1843func (txt *sanitizerLibrariesTxtModule) DepsMutator(actx android.BottomUpMutatorContext) {
1844 targets := actx.Config().Targets[android.Android]
1845 depTag := sanitizerLibraryDependencyTag{}
1846
1847 for _, target := range targets {
1848 variation := append(target.Variations(),
1849 blueprint.Variation{Mutator: "image", Variation: ""},
1850 blueprint.Variation{Mutator: "sdk", Variation: ""},
1851 blueprint.Variation{Mutator: "link", Variation: "shared"},
1852 )
1853 for _, lib := range android.SortedStringValues(sanitizerVariables) {
1854 if actx.OtherModuleFarDependencyVariantExists(variation, lib) {
1855 actx.AddFarVariationDependencies(variation, depTag, lib)
1856 }
1857
1858 prebuiltLibName := "prebuilt_" + lib
1859 if actx.OtherModuleFarDependencyVariantExists(variation, prebuiltLibName) {
1860 actx.AddFarVariationDependencies(variation, depTag, prebuiltLibName)
1861 }
1862 }
1863 }
1864
1865}
1866
1867func (txt *sanitizerLibrariesTxtModule) getSanitizerLibs(ctx android.ModuleContext) string {
1868 var sanitizerLibStems []string
1869
1870 ctx.VisitDirectDepsIf(func(m android.Module) bool {
1871 if !m.Enabled(ctx) {
1872 return false
1873 }
1874
1875 ccModule, _ := m.(*Module)
1876 if ccModule == nil || ccModule.library == nil || !ccModule.library.shared() {
1877 return false
1878 }
1879
1880 targets := ctx.Config().Targets[android.Android]
1881
1882 for _, target := range targets {
1883 if m.Target().Os == target.Os && m.Target().Arch.ArchType == target.Arch.ArchType {
1884 return true
1885 }
1886 }
1887
1888 return false
1889 }, func(m android.Module) {
1890 ccModule, _ := m.(*Module)
1891 outputFile := ccModule.outputFile
1892 if outputFile.Valid() {
1893 sanitizerLibStems = append(sanitizerLibStems, outputFile.Path().Base())
1894 }
1895 })
1896
1897 sanitizerLibStems = android.SortedUniqueStrings(sanitizerLibStems)
1898 return strings.Join(sanitizerLibStems, "\n")
1899}
1900
1901func (txt *sanitizerLibrariesTxtModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1902 filename := txt.Name()
1903
1904 txt.outputFile = android.PathForModuleOut(ctx, filename).OutputPath
1905 android.WriteFileRule(ctx, txt.outputFile, txt.getSanitizerLibs(ctx))
1906
1907 installPath := android.PathForModuleInstall(ctx, "etc")
1908 ctx.InstallFile(installPath, filename, txt.outputFile)
mrziwange2346b82024-06-10 15:09:45 -07001909
1910 ctx.SetOutputFiles(android.Paths{txt.outputFile}, "")
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001911}
1912
1913func (txt *sanitizerLibrariesTxtModule) AndroidMkEntries() []android.AndroidMkEntries {
1914 return []android.AndroidMkEntries{{
1915 Class: "ETC",
1916 OutputFile: android.OptionalPathForPath(txt.outputFile),
1917 }}
1918}
1919
1920// PrebuiltEtcModule interface
Kiyoung Kim0d8908c2024-05-07 14:47:35 +09001921func (txt *sanitizerLibrariesTxtModule) BaseDir() string {
1922 return "etc"
1923}
1924
1925// PrebuiltEtcModule interface
1926func (txt *sanitizerLibrariesTxtModule) SubDir() string {
1927 return ""
1928}