blob: a2c9c58eba52bcd3fc37fa3b5fdad0ff9328e0a6 [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"
Colin Cross16b23492016-01-06 14:41:07 -080028)
29
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070030var (
31 // Any C flags added by sanitizer which libTooling tools may not
32 // understand also need to be added to ClangLibToolingUnknownCflags in
33 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080034
Yi Kong20233a42019-08-21 01:38:40 -070035 asanCflags = []string{
36 "-fno-omit-frame-pointer",
Yi Kong20233a42019-08-21 01:38:40 -070037 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070038 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070039
Florian Mayera9984462023-06-16 16:48:51 -070040 // DO NOT ADD MLLVM FLAGS HERE! ADD THEM BELOW TO hwasanCommonFlags.
Yi Kong286abc62021-11-04 16:14:14 +080041 hwasanCflags = []string{
42 "-fno-omit-frame-pointer",
43 "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000044 "-fsanitize-hwaddress-abi=platform",
Yi Kong286abc62021-11-04 16:14:14 +080045 }
46
47 // ThinLTO performs codegen during link time, thus these flags need to
48 // passed to both CFLAGS and LDFLAGS.
49 hwasanCommonflags = []string{
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080050 // The following improves debug location information
51 // availability at the cost of its accuracy. It increases
52 // the likelihood of a stack variable's frame offset
53 // to be recorded in the debug info, which is important
54 // for the quality of hwasan reports. The downside is a
55 // higher number of "optimized out" stack variables.
56 // b/112437883.
Yi Kong286abc62021-11-04 16:14:14 +080057 "-instcombine-lower-dbg-declare=0",
Florian Mayera9984462023-06-16 16:48:51 -070058 "-hwasan-use-after-scope=1",
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 Mayer1866bbe2023-03-11 01:07:40 +000085 memtagStackCommonFlags = []string{"-march=armv8-a+memtag", "-mllvm", "-dom-tree-reachability-max-bbs-to-explore=128"}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +000086
87 hostOnlySanitizeFlags = []string{"-fno-sanitize-recover=all"}
Elliott Hughes3bba0e42023-10-05 14:50:48 -070088 deviceOnlySanitizeFlags = []string{"-fsanitize-trap=all"}
Trevor Radcliffeda64d912023-08-02 20:24:29 +000089
90 noSanitizeLinkRuntimeFlag = "-fno-sanitize-link-runtime"
Dan Willemsencbceaab2016-10-13 16:44:07 -070091)
92
Ivan Lozano3968d8f2020-12-14 11:27:52 -050093type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080094
Colin Cross16b23492016-01-06 14:41:07 -080095const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050096 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070097 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080098 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070099 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800100 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500101 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -0400102 Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +0000103 Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200104 Memtag_globals
Liz Kammer75db9312021-07-07 16:41:50 -0400105 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -0800106)
107
Liz Kammer75db9312021-07-07 16:41:50 -0400108var Sanitizers = []SanitizerType{
109 Asan,
110 Hwasan,
111 tsan,
112 intOverflow,
113 scs,
114 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400115 Memtag_heap,
Florian Mayerd8434a42022-08-31 20:57:03 +0000116 Memtag_stack,
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200117 Memtag_globals,
Liz Kammer75db9312021-07-07 16:41:50 -0400118 cfi, // cfi is last to prevent it running before incompatible mutators
119}
120
Jiyong Park82226632019-02-01 10:50:50 +0900121// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500122func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800123 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500124 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800125 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700126 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700127 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800128 case tsan:
129 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700130 case intOverflow:
131 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000132 case cfi:
133 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800134 case scs:
135 return "scs"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400136 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700137 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000138 case Memtag_stack:
139 return "memtag_stack"
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200140 case Memtag_globals:
141 return "memtag_globals"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500142 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700143 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800144 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500145 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800146 }
147}
148
Jiyong Park82226632019-02-01 10:50:50 +0900149// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500150func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900151 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500152 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900153 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700154 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900155 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400156 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700157 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000158 case Memtag_stack:
159 return "memtag_stack"
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200160 case Memtag_globals:
161 return "memtag_globals"
Jiyong Park82226632019-02-01 10:50:50 +0900162 case tsan:
163 return "thread"
164 case intOverflow:
165 return "integer_overflow"
166 case cfi:
167 return "cfi"
168 case scs:
169 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500170 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700171 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900172 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500173 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900174 }
175}
176
Liz Kammer75db9312021-07-07 16:41:50 -0400177func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
178 switch t {
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200179 case cfi, Hwasan, Asan, tsan, Fuzzer, scs:
180 sanitizer := &sanitizerSplitMutator{t}
181 ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
182 ctx.Transition(t.variationName(), sanitizer)
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200183 case Memtag_heap, Memtag_stack, Memtag_globals, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400184 // do nothing
185 default:
186 panic(fmt.Errorf("unknown SanitizerType %d", t))
187 }
188}
189
Liz Kammerfd8a49f2022-10-31 10:31:11 -0400190// shouldPropagateToSharedLibraryDeps returns whether a sanitizer type should propagate to share
191// dependencies. In most cases, sanitizers only propagate to static dependencies; however, some
192// sanitizers also must be enabled for shared libraries for linking.
193func (t SanitizerType) shouldPropagateToSharedLibraryDeps() bool {
194 switch t {
195 case Fuzzer:
196 // Typically, shared libs are not split. However, for fuzzer, we split even for shared libs
197 // because a library sanitized for fuzzer can't be linked from a library that isn't sanitized
198 // for fuzzer.
199 return true
200 default:
201 return false
202 }
203}
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500204func (*Module) SanitizerSupported(t SanitizerType) bool {
205 switch t {
206 case Asan:
207 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700208 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500209 return true
210 case tsan:
211 return true
212 case intOverflow:
213 return true
214 case cfi:
215 return true
216 case scs:
217 return true
218 case Fuzzer:
219 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400220 case Memtag_heap:
221 return true
Florian Mayerd8434a42022-08-31 20:57:03 +0000222 case Memtag_stack:
223 return true
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200224 case Memtag_globals:
225 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500226 default:
227 return false
228 }
229}
230
231// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
232func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700233 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900234}
235
Martin Stjernholmb0249572020-09-15 02:32:35 +0100236type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400237 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100238 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800239
Liz Kammer75b9b402021-06-25 15:19:27 -0400240 // ASan (Address sanitizer), incompatible with static binaries.
241 // Always runs in a diagnostic mode.
242 // Use of address sanitizer disables cfi sanitizer.
243 // Hwaddress sanitizer takes precedence over this sanitizer.
244 Address *bool `android:"arch_variant"`
245 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
246 // Always runs in a diagnostic mode.
247 // Use of thread sanitizer disables cfi and scudo sanitizers.
248 // Hwaddress sanitizer takes precedence over this sanitizer.
249 Thread *bool `android:"arch_variant"`
250 // HWASan (Hardware Address sanitizer).
251 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100252 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800253
Liz Kammer75b9b402021-06-25 15:19:27 -0400254 // Undefined behavior sanitizer
255 All_undefined *bool `android:"arch_variant"`
256 // Subset of undefined behavior sanitizer
257 Undefined *bool `android:"arch_variant"`
258 // List of specific undefined behavior sanitizers to enable
259 Misc_undefined []string `android:"arch_variant"`
260 // Fuzzer, incompatible with static binaries.
261 Fuzzer *bool `android:"arch_variant"`
262 // safe-stack sanitizer, incompatible with 32-bit architectures.
263 Safestack *bool `android:"arch_variant"`
264 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
265 Cfi *bool `android:"arch_variant"`
266 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
267 Integer_overflow *bool `android:"arch_variant"`
268 // scudo sanitizer, incompatible with asan, hwasan, tsan
269 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
270 // deprecated
271 Scudo *bool `android:"arch_variant"`
Elliott Hughese4793bc2023-02-09 21:15:47 +0000272 // shadow-call-stack sanitizer, only available on arm64/riscv64.
Liz Kammer75b9b402021-06-25 15:19:27 -0400273 Scs *bool `android:"arch_variant"`
274 // Memory-tagging, only available on arm64
275 // if diag.memtag unset or false, enables async memory tagging
Florian Mayer00ab5cf2022-08-31 18:30:18 +0000276 Memtag_heap *bool `android:"arch_variant"`
Florian Mayerd8434a42022-08-31 20:57:03 +0000277 // Memory-tagging stack instrumentation, only available on arm64
278 // Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
279 Memtag_stack *bool `android:"arch_variant"`
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200280 // Memory-tagging globals instrumentation, only available on arm64
281 // Adds instrumentation to detect global buffer overflows using MTE.
282 Memtag_globals *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100283
284 // A modifier for ASAN and HWASAN for write only instrumentation
285 Writeonly *bool `android:"arch_variant"`
286
287 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
288 // Replaces abort() on error with a human-readable error message.
289 // Address and Thread sanitizers always run in diagnostic mode.
290 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400291 // Undefined behavior sanitizer, diagnostic mode
292 Undefined *bool `android:"arch_variant"`
293 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
294 Cfi *bool `android:"arch_variant"`
295 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
296 Integer_overflow *bool `android:"arch_variant"`
297 // Memory-tagging, only available on arm64
298 // requires sanitizer.memtag: true
299 // if set, enables sync memory tagging
300 Memtag_heap *bool `android:"arch_variant"`
301 // List of specific undefined behavior sanitizers to enable in diagnostic mode
302 Misc_undefined []string `android:"arch_variant"`
303 // List of sanitizers to pass to -fno-sanitize-recover
304 // results in only the first detected error for these sanitizers being reported and program then
305 // exits with a non-zero exit code.
306 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800307 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800308
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800309 // Sanitizers to run with flag configuration specified
310 Config struct {
311 // Enables CFI support flags for assembly-heavy libraries
312 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800313 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800314
Liz Kammer75b9b402021-06-25 15:19:27 -0400315 // List of sanitizers to pass to -fsanitize-recover
316 // allows execution to continue for these sanitizers to detect multiple errors rather than only
317 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100318 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000319
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700320 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100321 Blocklist *string
322}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700323
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400324type sanitizeMutatedProperties struct {
325 // Whether sanitizers can be enabled on this module
326 Never *bool `blueprint:"mutated"`
327
328 // Whether ASan (Address sanitizer) is enabled for this module.
329 // Hwaddress sanitizer takes precedence over this sanitizer.
330 Address *bool `blueprint:"mutated"`
331 // Whether TSan (Thread sanitizer) is enabled for this module
332 Thread *bool `blueprint:"mutated"`
333 // Whether HWASan (Hardware Address sanitizer) is enabled for this module
334 Hwaddress *bool `blueprint:"mutated"`
335
336 // Whether Undefined behavior sanitizer is enabled for this module
337 All_undefined *bool `blueprint:"mutated"`
338 // Whether undefined behavior sanitizer subset is enabled for this module
339 Undefined *bool `blueprint:"mutated"`
340 // List of specific undefined behavior sanitizers enabled for this module
341 Misc_undefined []string `blueprint:"mutated"`
342 // Whether Fuzzeris enabled for this module
343 Fuzzer *bool `blueprint:"mutated"`
344 // whether safe-stack sanitizer is enabled for this module
345 Safestack *bool `blueprint:"mutated"`
346 // Whether cfi sanitizer is enabled for this module
347 Cfi *bool `blueprint:"mutated"`
348 // Whether signed/unsigned integer overflow sanitizer is enabled for this module
349 Integer_overflow *bool `blueprint:"mutated"`
350 // Whether scudo sanitizer is enabled for this module
351 Scudo *bool `blueprint:"mutated"`
352 // Whether shadow-call-stack sanitizer is enabled for this module.
353 Scs *bool `blueprint:"mutated"`
354 // Whether Memory-tagging is enabled for this module
355 Memtag_heap *bool `blueprint:"mutated"`
356 // Whether Memory-tagging stack instrumentation is enabled for this module
357 Memtag_stack *bool `blueprint:"mutated"`
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200358 // Whether Memory-tagging globals instrumentation is enabled for this module
359 Memtag_globals *bool `android:"arch_variant"`
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400360
361 // Whether a modifier for ASAN and HWASAN for write only instrumentation is enabled for this
362 // module
363 Writeonly *bool `blueprint:"mutated"`
364
365 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
366 Diag struct {
367 // Whether Undefined behavior sanitizer, diagnostic mode is enabled for this module
368 Undefined *bool `blueprint:"mutated"`
369 // Whether cfi sanitizer, diagnostic mode is enabled for this module
370 Cfi *bool `blueprint:"mutated"`
371 // Whether signed/unsigned integer overflow sanitizer, diagnostic mode is enabled for this
372 // module
373 Integer_overflow *bool `blueprint:"mutated"`
374 // Whether Memory-tagging, diagnostic mode is enabled for this module
375 Memtag_heap *bool `blueprint:"mutated"`
376 // List of specific undefined behavior sanitizers enabled in diagnostic mode
377 Misc_undefined []string `blueprint:"mutated"`
378 } `blueprint:"mutated"`
379}
380
Martin Stjernholmb0249572020-09-15 02:32:35 +0100381type SanitizeProperties struct {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400382 Sanitize SanitizeUserProps `android:"arch_variant"`
383 SanitizeMutated sanitizeMutatedProperties `blueprint:"mutated"`
384
385 SanitizerEnabled bool `blueprint:"mutated"`
386 MinimalRuntimeDep bool `blueprint:"mutated"`
387 BuiltinsDep bool `blueprint:"mutated"`
388 UbsanRuntimeDep bool `blueprint:"mutated"`
389 InSanitizerDir bool `blueprint:"mutated"`
390 Sanitizers []string `blueprint:"mutated"`
391 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800392}
393
394type sanitize struct {
395 Properties SanitizeProperties
396}
397
Cindy Zhou18417cb2020-12-10 07:12:38 -0800398// Mark this tag with a check to see if apex dependency check should be skipped
399func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
400 return t.skipApexAllowedDependenciesCheck
401}
402
403var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
404
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000405var exportedVars = android.NewExportedVariables(pctx)
406
Vishwath Mohane7128792017-11-17 11:08:10 -0800407func init() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000408 exportedVars.ExportStringListStaticVariable("HostOnlySanitizeFlags", hostOnlySanitizeFlags)
409 exportedVars.ExportStringList("DeviceOnlySanitizeFlags", deviceOnlySanitizeFlags)
410
Trevor Radcliffe3876c5a2023-08-02 15:41:50 +0000411 exportedVars.ExportStringList("MinimalRuntimeFlags", minimalRuntimeFlags)
412
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000413 // Leave out "-flto" from the slices exported to bazel, as we will use the
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000414 // dedicated LTO feature for this. For C Flags and Linker Flags, also leave
Trevor Radcliffef1836e42023-06-01 21:12:08 +0000415 // out the cross DSO flag which will be added separately under the correct conditions.
416 exportedVars.ExportStringList("CfiCFlags", append(cfiCflags[2:], cfiEnableFlag))
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000417 exportedVars.ExportStringList("CfiLdFlags", cfiLdflags[2:])
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000418 exportedVars.ExportStringList("CfiAsFlags", cfiAsflags[1:])
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000419
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000420 exportedVars.ExportString("SanitizeIgnorelistPrefix", sanitizeIgnorelistPrefix)
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000421 exportedVars.ExportString("CfiCrossDsoFlag", cfiCrossDsoFlag)
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000422 exportedVars.ExportString("CfiBlocklistPath", cfiBlocklistPath)
423 exportedVars.ExportString("CfiBlocklistFilename", cfiBlocklistFilename)
424 exportedVars.ExportString("CfiExportsMapPath", cfiExportsMapPath)
425 exportedVars.ExportString("CfiExportsMapFilename", cfiExportsMapFilename)
426 exportedVars.ExportString("CfiAssemblySupportFlag", cfiAssemblySupportFlag)
427
Trevor Radcliffeda64d912023-08-02 20:24:29 +0000428 exportedVars.ExportString("NoSanitizeLinkRuntimeFlag", noSanitizeLinkRuntimeFlag)
429
Vishwath Mohane7128792017-11-17 11:08:10 -0800430 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700431 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800432}
433
Colin Cross16b23492016-01-06 14:41:07 -0800434func (sanitize *sanitize) props() []interface{} {
435 return []interface{}{&sanitize.Properties}
436}
437
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400438func (p *sanitizeMutatedProperties) copyUserPropertiesToMutated(userProps *SanitizeUserProps) {
439 p.Never = userProps.Never
440 p.Address = userProps.Address
441 p.All_undefined = userProps.All_undefined
442 p.Cfi = userProps.Cfi
443 p.Fuzzer = userProps.Fuzzer
444 p.Hwaddress = userProps.Hwaddress
445 p.Integer_overflow = userProps.Integer_overflow
446 p.Memtag_heap = userProps.Memtag_heap
447 p.Memtag_stack = userProps.Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200448 p.Memtag_globals = userProps.Memtag_globals
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400449 p.Safestack = userProps.Safestack
450 p.Scs = userProps.Scs
451 p.Scudo = userProps.Scudo
452 p.Thread = userProps.Thread
453 p.Undefined = userProps.Undefined
454 p.Writeonly = userProps.Writeonly
455
456 p.Misc_undefined = make([]string, 0, len(userProps.Misc_undefined))
457 for _, v := range userProps.Misc_undefined {
458 p.Misc_undefined = append(p.Misc_undefined, v)
459 }
460
461 p.Diag.Cfi = userProps.Diag.Cfi
462 p.Diag.Integer_overflow = userProps.Diag.Integer_overflow
463 p.Diag.Memtag_heap = userProps.Diag.Memtag_heap
464 p.Diag.Undefined = userProps.Diag.Undefined
465
466 p.Diag.Misc_undefined = make([]string, 0, len(userProps.Diag.Misc_undefined))
467 for _, v := range userProps.Diag.Misc_undefined {
468 p.Diag.Misc_undefined = append(p.Diag.Misc_undefined, v)
469 }
470}
471
Colin Cross16b23492016-01-06 14:41:07 -0800472func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400473 s := &sanitize.Properties.SanitizeMutated
474 s.copyUserPropertiesToMutated(&sanitize.Properties.Sanitize)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700475
Colin Cross16b23492016-01-06 14:41:07 -0800476 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700477 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800478 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800479 }
480
481 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800482 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800483 return
484 }
485
Florian Mayerd8434a42022-08-31 20:57:03 +0000486 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400487 if ctx.testBinary() {
488 if s.Memtag_heap == nil {
489 s.Memtag_heap = proptools.BoolPtr(true)
490 }
491 if s.Diag.Memtag_heap == nil {
492 s.Diag.Memtag_heap = proptools.BoolPtr(true)
493 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800494 }
495
Colin Cross16b23492016-01-06 14:41:07 -0800496 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700497 var globalSanitizersDiag []string
498
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700499 if ctx.Host() {
500 if !ctx.Windows() {
501 globalSanitizers = ctx.Config().SanitizeHost()
502 }
503 } else {
504 arches := ctx.Config().SanitizeDeviceArch()
505 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
506 globalSanitizers = ctx.Config().SanitizeDevice()
507 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800508 }
509 }
510
Colin Cross16b23492016-01-06 14:41:07 -0800511 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000512 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700513 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400514 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000515 }
Colin Cross16b23492016-01-06 14:41:07 -0800516
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700517 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400518 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000519 }
520
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700521 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400522 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000523 }
524
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700525 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400526 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000527 }
528
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700529 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400530 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700531 }
532
533 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400534 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000535 }
536
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700537 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800538 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400539 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700540 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700541 }
542
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700543 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700544 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700545 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400546 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700547 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700548 }
549
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700550 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400551 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700552 }
553
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700554 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Tomislav Novakf734f002023-08-22 10:51:44 -0700555 if !ctx.Config().HWASanDisabledForPath(ctx.ModuleDir()) {
556 s.Hwaddress = proptools.BoolPtr(true)
557 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700558 }
559
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000560 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
561 // Hwaddress and Address are set before, so we can check them here
562 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
563 if s.Address == nil && s.Hwaddress == nil {
564 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
565 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400566 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000567 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700568 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800569 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400570 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800571 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700572 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000573
Florian Mayerd8434a42022-08-31 20:57:03 +0000574 if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
575 s.Memtag_stack = proptools.BoolPtr(true)
576 }
577
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200578 if found, globalSanitizers = removeFromList("memtag_globals", globalSanitizers); found && s.Memtag_globals == nil {
579 s.Memtag_globals = proptools.BoolPtr(true)
580 }
581
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000582 if len(globalSanitizers) > 0 {
583 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
584 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700585
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700586 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700587 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700588 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400589 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700590 }
591
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700592 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
593 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400594 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700595 }
596
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800597 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
598 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400599 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800600 }
601
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700602 if len(globalSanitizersDiag) > 0 {
603 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
604 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700605 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700606
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800607 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross88a029f2022-06-23 14:51:20 -0700608 if ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800609 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800610 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400611 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800612 }
613 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400614 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800615 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800616 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800617 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400618 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800619 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800620 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700621 }
622
Hang Lua98aab92023-03-17 13:17:22 +0800623 // Enable HWASan for all components in the include paths (for Aarch64 only)
624 if s.Hwaddress == nil && ctx.Config().HWASanEnabledForPath(ctx.ModuleDir()) &&
625 ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
626 s.Hwaddress = proptools.BoolPtr(true)
627 }
628
Elvis Chien9c993542021-06-25 01:15:17 +0800629 // Enable CFI for non-host components in the include paths
630 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400631 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000632 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400633 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700634 }
635 }
636
Elliott Hughesda3a0712020-03-06 16:55:28 -0800637 // Is CFI actually enabled?
638 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400639 s.Cfi = nil
640 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800641 }
642
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700643 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross88a029f2022-06-23 14:51:20 -0700644 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700645 s.Hwaddress = nil
646 }
647
Elliott Hughese4793bc2023-02-09 21:15:47 +0000648 // SCS is only implemented on AArch64/riscv64.
649 if (ctx.Arch().ArchType != android.Arm64 && ctx.Arch().ArchType != android.Riscv64) || !ctx.toolchain().Bionic() {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800650 s.Scs = nil
651 }
652
Ivan Lozano62cd0382021-11-01 10:27:54 -0400653 // Memtag_heap is only implemented on AArch64.
Florian Mayerd8434a42022-08-31 20:57:03 +0000654 // Memtag ABI is Android specific for now, so disable for host.
655 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() || ctx.Host() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700656 s.Memtag_heap = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000657 s.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200658 s.Memtag_globals = nil
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700659 }
660
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700661 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700662 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400663 s.Cfi = nil
664 s.Diag.Cfi = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000665 // HWASAN and ASAN win against MTE.
666 s.Memtag_heap = nil
667 s.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200668 s.Memtag_globals = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700669 }
670
Colin Crossed12a042022-02-07 13:55:55 -0800671 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
672 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400673 s.Cfi = nil
674 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700675 s.Misc_undefined = nil
676 s.Undefined = nil
677 s.All_undefined = nil
678 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800679 }
680
Colin Crossed12a042022-02-07 13:55:55 -0800681 // Disable CFI for musl
682 if ctx.toolchain().Musl() {
683 s.Cfi = nil
684 s.Diag.Cfi = nil
685 }
686
Colin Cross390fc742023-05-02 13:02:51 -0700687 // TODO(b/280478629): runtimes don't exist for musl arm64 yet.
688 if ctx.toolchain().Musl() && ctx.Arch().ArchType == android.Arm64 {
689 s.Address = nil
690 s.Hwaddress = nil
691 s.Thread = nil
692 s.Scudo = nil
693 s.Fuzzer = nil
694 s.Cfi = nil
695 s.Diag.Cfi = nil
696 s.Misc_undefined = nil
697 s.Undefined = nil
698 s.All_undefined = nil
699 s.Integer_overflow = nil
700 }
701
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700702 // Also disable CFI for VNDK variants of components
703 if ctx.isVndk() && ctx.useVndk() {
Justin Yun08270c62022-12-19 17:01:26 +0900704 s.Cfi = nil
705 s.Diag.Cfi = nil
Inseob Kimeec88e12020-01-22 11:11:29 +0900706 }
707
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700708 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700709 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
710 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700711 s.Hwaddress = nil
712 }
713
Colin Cross3c344ef2016-07-18 15:44:56 -0700714 if ctx.staticBinary() {
715 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700716 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700717 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800718 }
719
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700720 if Bool(s.All_undefined) {
721 s.Undefined = nil
722 }
723
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700724 if !ctx.toolchain().Is64Bit() {
725 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700726 s.Thread = nil
727 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800728 // TODO(ccross): error for compile_multilib = "32"?
729 }
730
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800731 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 -0700732 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 +0200733 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack) ||
734 Bool(s.Memtag_globals)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700735 sanitize.Properties.SanitizerEnabled = true
736 }
737
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800738 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
739 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700740 s.Scudo = nil
741 }
742
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700743 if Bool(s.Hwaddress) {
744 s.Address = nil
745 s.Thread = nil
746 }
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000747
748 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
749 // mutually incompatible.
750 if Bool(s.Fuzzer) {
751 s.Cfi = nil
752 }
Colin Cross16b23492016-01-06 14:41:07 -0800753}
754
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800755func toDisableImplicitIntegerChange(flags []string) bool {
756 // Returns true if any flag is fsanitize*integer, and there is
757 // no explicit flag about sanitize=implicit-integer-sign-change.
758 for _, f := range flags {
759 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
760 return false
761 }
762 }
763 for _, f := range flags {
764 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
765 return true
766 }
767 }
768 return false
769}
770
Yabin Cuidb7dda82020-11-30 15:47:45 -0800771func toDisableUnsignedShiftBaseChange(flags []string) bool {
772 // Returns true if any flag is fsanitize*integer, and there is
773 // no explicit flag about sanitize=unsigned-shift-base.
774 for _, f := range flags {
775 if strings.Contains(f, "sanitize=unsigned-shift-base") {
776 return false
777 }
778 }
779 for _, f := range flags {
780 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
781 return true
782 }
783 }
784 return false
785}
786
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400787func (s *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
788 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...)
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400883 if Bool(s.Properties.Sanitize.Config.Cfi_assembly_support) {
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000884 flags.Local.CFlags = append(flags.Local.CFlags, cfiAssemblySupportFlag)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800885 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000886 // Only append the default visibility flag if -fvisibility has not already been set
887 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800888 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
889 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000890 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800891 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000892
893 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800894 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
895 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000896 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700897 }
898
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400899 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000900 flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
901 flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
902 flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
903 }
904
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200905 if (Bool(sanProps.Memtag_heap) || Bool(sanProps.Memtag_stack) || Bool(sanProps.Memtag_globals)) && ctx.binary() {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400906 if Bool(sanProps.Diag.Memtag_heap) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000907 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
908 } else {
909 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
910 }
911 }
912
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400913 if Bool(sanProps.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800914 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700915 }
916
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400917 if len(s.Properties.Sanitizers) > 0 {
918 sanitizeArg := "-fsanitize=" + strings.Join(s.Properties.Sanitizers, ",")
Colin Cross4af21ed2019-11-04 09:37:55 -0800919 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
920 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross234b01d2022-02-07 13:49:03 -0800921 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
922
Colin Crossed12a042022-02-07 13:55:55 -0800923 if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
924 // Bionic and musl sanitizer runtimes have already been added as dependencies so that
925 // the right variant of the runtime will be used (with the "-android" or "-musl"
926 // suffixes), so don't let clang the runtime library.
Trevor Radcliffeda64d912023-08-02 20:24:29 +0000927 flags.Local.LdFlags = append(flags.Local.LdFlags, noSanitizeLinkRuntimeFlag)
Colin Cross234b01d2022-02-07 13:49:03 -0800928 } else {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800929 // Host sanitizers only link symbols in the final executable, so
930 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800931 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
Colin Cross6c18d002022-06-02 15:11:50 -0700932 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500933
Colin Cross6c18d002022-06-02 15:11:50 -0700934 if !ctx.toolchain().Bionic() {
935 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function san.
936 // Musl toolchain prebuilts have vptr and function sanitizers, but enabling them
937 // implicitly enables RTTI which causes RTTI mismatch issues with dependencies.
938
Colin Cross234b01d2022-02-07 13:49:03 -0800939 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500940 }
941
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400942 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700943 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800944 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700945 } else if ctx.Host() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000946 flags.Local.CFlags = append(flags.Local.CFlags, hostOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700947 } else {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000948 flags.Local.CFlags = append(flags.Local.CFlags, deviceOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700949 }
Evgenii Stepanov59012812022-06-24 11:09:18 -0700950
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400951 if enableMinimalRuntime(s) {
Evgenii Stepanov59012812022-06-24 11:09:18 -0700952 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
953 }
954
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800955 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800956 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
957 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800958 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800959 // http://b/171275751, Android doesn't build with this sanitizer yet.
960 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
961 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
962 }
Colin Cross16b23492016-01-06 14:41:07 -0800963 }
964
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400965 if len(s.Properties.DiagSanitizers) > 0 {
966 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(s.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700967 }
968 // FIXME: enable RTTI if diag + (cfi or vptr)
969
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400970 if s.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800971 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400972 strings.Join(s.Properties.Sanitize.Recover, ","))
Andreas Gampe97071162017-05-08 13:15:23 -0700973 }
974
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400975 if s.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800976 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400977 strings.Join(s.Properties.Sanitize.Diag.No_recover, ","))
Ivan Lozano7929bba2018-12-12 09:36:31 -0800978 }
979
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400980 blocklist := android.OptionalPathForModuleSrc(ctx, s.Properties.Sanitize.Blocklist)
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700981 if blocklist.Valid() {
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000982 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeIgnorelistPrefix+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700983 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
984 }
985
Colin Cross16b23492016-01-06 14:41:07 -0800986 return flags
987}
988
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400989func (s *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900990 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
991 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800992 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400993 if Bool(s.Properties.SanitizeMutated.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800994 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900995 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400996 if Bool(s.Properties.SanitizeMutated.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800997 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900998 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400999 if Bool(s.Properties.SanitizeMutated.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -08001000 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +09001001 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001002 }
Colin Cross8ff9ef42017-05-08 13:44:11 -07001003}
1004
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001005func (s *sanitize) inSanitizerDir() bool {
1006 return s.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -07001007}
1008
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001009// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001010func (s *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +00001011 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001012 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001013 return s.Properties.SanitizeMutated.Address
Tri Vo6eafc362021-04-01 11:29:09 -07001014 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001015 return s.Properties.SanitizeMutated.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +00001016 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001017 return s.Properties.SanitizeMutated.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +00001018 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001019 return s.Properties.SanitizeMutated.Integer_overflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001020 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001021 return s.Properties.SanitizeMutated.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001022 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001023 return s.Properties.SanitizeMutated.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -04001024 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001025 return s.Properties.SanitizeMutated.Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +00001026 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001027 return s.Properties.SanitizeMutated.Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001028 case Memtag_globals:
1029 return s.Properties.SanitizeMutated.Memtag_globals
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001030 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001031 return s.Properties.SanitizeMutated.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +00001032 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001033 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +00001034 }
1035}
1036
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001037// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -08001038func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001039 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001040 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -08001041 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001042 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001043 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -04001044 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Florian Mayerd8434a42022-08-31 20:57:03 +00001045 !sanitize.isSanitizerEnabled(Memtag_stack) &&
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001046 !sanitize.isSanitizerEnabled(Memtag_globals) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001047 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -08001048}
1049
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001050// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001051func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001052 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001053 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001054 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001055 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001056}
1057
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001058func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -04001059 bPtr := proptools.BoolPtr(b)
1060 if !b {
1061 bPtr = nil
1062 }
Colin Cross16b23492016-01-06 14:41:07 -08001063 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001064 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001065 sanitize.Properties.SanitizeMutated.Address = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001066 // For ASAN variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001067 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001068 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Tri Vo6eafc362021-04-01 11:29:09 -07001069 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001070 sanitize.Properties.SanitizeMutated.Hwaddress = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001071 // For HWAsan variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001072 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001073 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Colin Cross16b23492016-01-06 14:41:07 -08001074 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001075 sanitize.Properties.SanitizeMutated.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -07001076 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001077 sanitize.Properties.SanitizeMutated.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001078 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001079 sanitize.Properties.SanitizeMutated.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001080 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001081 sanitize.Properties.SanitizeMutated.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -04001082 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001083 sanitize.Properties.SanitizeMutated.Memtag_heap = bPtr
Florian Mayerd8434a42022-08-31 20:57:03 +00001084 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001085 sanitize.Properties.SanitizeMutated.Memtag_stack = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001086 // We do not need to disable ASAN or HWASan here, as there is no Memtag_stack variant.
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001087 case Memtag_globals:
1088 sanitize.Properties.Sanitize.Memtag_globals = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001089 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001090 sanitize.Properties.SanitizeMutated.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -08001091 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001092 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -08001093 }
1094 if b {
1095 sanitize.Properties.SanitizerEnabled = true
1096 }
1097}
1098
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001099// Check if the sanitizer is explicitly disabled (as opposed to nil by
1100// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001101func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001102 if sanitize == nil {
1103 return false
1104 }
1105
1106 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1107 return sanitizerVal != nil && *sanitizerVal == false
1108}
1109
1110// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
1111// because enabling a sanitizer either directly (via the blueprint) or
1112// indirectly (via a mutator) sets the bool ptr to true, and you can't
1113// distinguish between the cases. It isn't needed though - both cases can be
1114// treated identically.
Liz Kammerba23cb62023-09-26 16:48:04 -04001115func (s *sanitize) isSanitizerEnabled(t SanitizerType) bool {
1116 if s == nil {
1117 return false
1118 }
1119 if proptools.Bool(s.Properties.SanitizeMutated.Never) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001120 return false
1121 }
1122
Liz Kammerba23cb62023-09-26 16:48:04 -04001123 sanitizerVal := s.getSanitizerBoolPtr(t)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001124 return sanitizerVal != nil && *sanitizerVal == true
1125}
1126
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001127// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
1128func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -07001129 switch t := tag.(type) {
1130 case dependencyTag:
1131 return t == reuseObjTag || t == objDepTag
1132 case libraryDependencyTag:
1133 return true
1134 default:
1135 return false
1136 }
Colin Cross6b753602018-06-21 13:03:07 -07001137}
1138
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001139func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
1140 return IsSanitizableDependencyTag
1141}
1142
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001143type sanitizerSplitMutator struct {
1144 sanitizer SanitizerType
1145}
1146
1147// If an APEX is sanitized or not depends on whether it contains at least one
1148// sanitized module. Transition mutators cannot propagate information up the
1149// dependency graph this way, so we need an auxiliary mutator to do so.
1150func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
1151 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1152 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1153 ctx.VisitDirectDeps(func(dep android.Module) {
Ivan Lozano5467a392023-08-23 14:20:25 -04001154 if c, ok := dep.(PlatformSanitizeable); ok && c.IsSanitizerEnabled(s.sanitizer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001155 enabled = true
Inseob Kimc42f2f22020-07-29 20:32:10 +09001156 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001157 })
1158
1159 if enabled {
1160 sanitizeable.EnableSanitizer(s.sanitizer.name())
1161 }
1162 }
1163}
1164
1165func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
1166 if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001167 // If the given sanitizer is not requested in the .bp file for a module, it
1168 // won't automatically build the sanitized variation.
1169 if !c.IsSanitizerEnabled(s.sanitizer) {
1170 return []string{""}
1171 }
1172
1173 if c.Binary() {
1174 // If a sanitizer is enabled for a binary, we do not build the version
1175 // without the sanitizer
1176 return []string{s.sanitizer.variationName()}
1177 } else if c.StaticallyLinked() || c.Header() {
1178 // For static libraries, we build both versions. Some Make modules
1179 // apparently depend on this behavior.
1180 return []string{"", s.sanitizer.variationName()}
1181 } else {
1182 // We only build the requested variation of dynamic libraries
1183 return []string{s.sanitizer.variationName()}
1184 }
1185 }
1186
1187 if _, ok := ctx.Module().(JniSanitizeable); ok {
1188 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1189 // that is short-circuited for now
1190 return []string{""}
1191 }
1192
1193 // If an APEX has a sanitized dependency, we build the APEX in the sanitized
1194 // variation. This is useful because such APEXes require extra dependencies.
1195 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1196 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1197 if enabled {
1198 return []string{s.sanitizer.variationName()}
1199 } else {
1200 return []string{""}
1201 }
1202 }
1203
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001204 return []string{""}
1205}
1206
1207func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1208 if c, ok := ctx.Module().(PlatformSanitizeable); ok {
1209 if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
1210 // If the dependency is through a non-sanitizable tag, use the
1211 // non-sanitized variation
1212 return ""
1213 }
1214
1215 return sourceVariation
1216 } else if _, ok := ctx.Module().(JniSanitizeable); ok {
1217 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1218 // that is short-circuited for now
1219 return ""
1220 } else {
1221 // Otherwise, do not rock the boat.
1222 return sourceVariation
1223 }
1224}
1225
1226func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1227 if d, ok := ctx.Module().(PlatformSanitizeable); ok {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001228 if !d.SanitizePropDefined() ||
1229 d.SanitizeNever() ||
1230 d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
1231 !d.SanitizerSupported(s.sanitizer) {
1232 // If a module opts out of a sanitizer, use its non-sanitized variation
1233 return ""
1234 }
1235
1236 // Binaries are always built in the variation they requested.
1237 if d.Binary() {
1238 if d.IsSanitizerEnabled(s.sanitizer) {
1239 return s.sanitizer.variationName()
1240 } else {
1241 return ""
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001242 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001243 }
1244
1245 // If a shared library requests to be sanitized, it will be built for that
1246 // sanitizer. Otherwise, some sanitizers propagate through shared library
1247 // dependency edges, some do not.
1248 if !d.StaticallyLinked() && !d.Header() {
1249 if d.IsSanitizerEnabled(s.sanitizer) {
1250 return s.sanitizer.variationName()
1251 }
1252
Liz Kammerfd8a49f2022-10-31 10:31:11 -04001253 // Some sanitizers do not propagate to shared dependencies
1254 if !s.sanitizer.shouldPropagateToSharedLibraryDeps() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001255 return ""
1256 }
1257 }
1258
1259 // Static and header libraries inherit whether they are sanitized from the
1260 // module they are linked into
1261 return incomingVariation
1262 } else if d, ok := ctx.Module().(Sanitizeable); ok {
1263 // If an APEX contains a sanitized module, it will be built in the variation
1264 // corresponding to that sanitizer.
1265 enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1266 if enabled {
1267 return s.sanitizer.variationName()
1268 }
1269
1270 return incomingVariation
1271 }
1272
1273 return ""
1274}
1275
1276func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
1277 sanitizerVariation := variationName == s.sanitizer.variationName()
1278
1279 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1280 sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
1281
1282 oneMakeVariation := false
1283 if c.StaticallyLinked() || c.Header() {
1284 if s.sanitizer != cfi && s.sanitizer != scs && s.sanitizer != Hwasan {
1285 // These sanitizers export only one variation to Make. For the rest,
1286 // Make targets can depend on both the sanitized and non-sanitized
1287 // versions.
1288 oneMakeVariation = true
1289 }
1290 } else if !c.Binary() {
1291 // Shared library. These are the sanitizers that do propagate through shared
1292 // library dependencies and therefore can cause multiple variations of a
1293 // shared library to be built.
1294 if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != scs && s.sanitizer != Asan {
1295 oneMakeVariation = true
1296 }
1297 }
1298
1299 if oneMakeVariation {
1300 if sanitizerEnabled != sanitizerVariation {
1301 c.SetPreventInstall()
1302 c.SetHideFromMake()
1303 }
1304 }
1305
1306 if sanitizerVariation {
1307 c.SetSanitizer(s.sanitizer, true)
1308
1309 // CFI is incompatible with ASAN so disable it in ASAN variations
1310 if s.sanitizer.incompatibleWithCfi() {
1311 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1312 if mctx.Device() && cfiSupported {
1313 c.SetSanitizer(cfi, false)
Jiyong Parkf97782b2019-02-13 20:28:58 +09001314 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001315 }
1316
1317 // locate the asan libraries under /data/asan
1318 if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
1319 c.SetInSanitizerDir()
1320 }
1321
1322 if c.StaticallyLinked() && c.ExportedToMake() {
1323 if s.sanitizer == Hwasan {
1324 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
1325 } else if s.sanitizer == cfi {
1326 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
1327 }
1328 }
1329 } else if c.IsSanitizerEnabled(s.sanitizer) {
1330 // Disable the sanitizer for the non-sanitized variation
1331 c.SetSanitizer(s.sanitizer, false)
1332 }
1333 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
1334 // If an APEX has sanitized dependencies, it gets a few more dependencies
1335 if sanitizerVariation {
1336 sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
1337 }
Colin Cross16b23492016-01-06 14:41:07 -08001338 }
1339}
1340
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001341func (c *Module) SanitizeNever() bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001342 return Bool(c.sanitize.Properties.SanitizeMutated.Never)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001343}
1344
1345func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
1346 return c.sanitize.isSanitizerExplicitlyDisabled(t)
1347}
1348
Ivan Lozano30c5db22018-02-21 15:49:20 -08001349// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001350func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001351 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001352 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001353 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001354 mctx.WalkDeps(func(child, parent android.Module) bool {
1355 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1356 return false
1357 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001358
Inseob Kimeec88e12020-01-22 11:11:29 +09001359 d, ok := child.(*Module)
1360 if !ok || !d.static() {
1361 return false
1362 }
1363 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001364 if enableMinimalRuntime(d.sanitize) {
1365 // If a static dependency is built with the minimal runtime,
1366 // make sure we include the ubsan minimal runtime.
1367 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001368 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001369 // If a static dependency runs with full ubsan diagnostics,
1370 // make sure we include the ubsan runtime.
1371 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001372 }
Colin Cross0b908332019-06-19 23:00:20 -07001373
1374 if c.sanitize.Properties.MinimalRuntimeDep &&
1375 c.sanitize.Properties.UbsanRuntimeDep {
1376 // both flags that this mutator might set are true, so don't bother recursing
1377 return false
1378 }
1379
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001380 if c.Os() == android.Linux {
1381 c.sanitize.Properties.BuiltinsDep = true
1382 }
1383
Colin Cross0b908332019-06-19 23:00:20 -07001384 return true
Colin Cross6b753602018-06-21 13:03:07 -07001385 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001386
Inseob Kimeec88e12020-01-22 11:11:29 +09001387 return false
Colin Cross6b753602018-06-21 13:03:07 -07001388 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001389 }
1390}
1391
Jiyong Park379de2f2018-12-19 02:47:14 +09001392// Add the dependency to the runtime library for each of the sanitizer variants
1393func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001394 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001395 if !c.Enabled() {
1396 return
1397 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001398 var sanitizers []string
1399 var diagSanitizers []string
1400
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001401 sanProps := &c.sanitize.Properties.SanitizeMutated
1402
1403 if Bool(sanProps.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001404 sanitizers = append(sanitizers, "undefined")
1405 } else {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001406 if Bool(sanProps.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001407 sanitizers = append(sanitizers,
1408 "bool",
1409 "integer-divide-by-zero",
1410 "return",
1411 "returns-nonnull-attribute",
1412 "shift-exponent",
1413 "unreachable",
1414 "vla-bound",
1415 // TODO(danalbert): The following checks currently have compiler performance issues.
1416 //"alignment",
1417 //"bounds",
1418 //"enum",
1419 //"float-cast-overflow",
1420 //"float-divide-by-zero",
1421 //"nonnull-attribute",
1422 //"null",
1423 //"shift-base",
1424 //"signed-integer-overflow",
1425 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1426 // https://llvm.org/PR19302
1427 // http://reviews.llvm.org/D6974
1428 // "object-size",
1429 )
1430 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001431 sanitizers = append(sanitizers, sanProps.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001432 }
1433
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001434 if Bool(sanProps.Diag.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001435 diagSanitizers = append(diagSanitizers, "undefined")
1436 }
1437
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001438 diagSanitizers = append(diagSanitizers, sanProps.Diag.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001439
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001440 if Bool(sanProps.Address) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001441 sanitizers = append(sanitizers, "address")
1442 diagSanitizers = append(diagSanitizers, "address")
1443 }
1444
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001445 if Bool(sanProps.Hwaddress) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001446 sanitizers = append(sanitizers, "hwaddress")
1447 }
1448
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001449 if Bool(sanProps.Thread) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001450 sanitizers = append(sanitizers, "thread")
1451 }
1452
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001453 if Bool(sanProps.Safestack) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001454 sanitizers = append(sanitizers, "safe-stack")
1455 }
1456
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001457 if Bool(sanProps.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001458 sanitizers = append(sanitizers, "cfi")
1459
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001460 if Bool(sanProps.Diag.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001461 diagSanitizers = append(diagSanitizers, "cfi")
1462 }
1463 }
1464
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001465 if Bool(sanProps.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001466 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1467 sanitizers = append(sanitizers, "signed-integer-overflow")
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001468 if Bool(sanProps.Diag.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001469 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1470 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1471 }
1472 }
1473
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001474 if Bool(sanProps.Scudo) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001475 sanitizers = append(sanitizers, "scudo")
1476 }
1477
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001478 if Bool(sanProps.Scs) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001479 sanitizers = append(sanitizers, "shadow-call-stack")
1480 }
1481
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001482 if Bool(sanProps.Memtag_heap) && c.Binary() {
Florian Mayerd8434a42022-08-31 20:57:03 +00001483 sanitizers = append(sanitizers, "memtag-heap")
1484 }
1485
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001486 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +00001487 sanitizers = append(sanitizers, "memtag-stack")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001488 }
1489
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001490 if Bool(sanProps.Memtag_globals) {
1491 sanitizers = append(sanitizers, "memtag-globals")
1492 // TODO(mitchp): For now, enable memtag-heap with memtag-globals because the linker
1493 // isn't new enough (https://reviews.llvm.org/differential/changeset/?ref=4243566).
1494 sanitizers = append(sanitizers, "memtag-heap")
1495 }
1496
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001497 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001498 sanitizers = append(sanitizers, "fuzzer-no-link")
1499 }
1500
Jiyong Park379de2f2018-12-19 02:47:14 +09001501 // Save the list of sanitizers. These will be used again when generating
1502 // the build rules (for Cflags, etc.)
1503 c.sanitize.Properties.Sanitizers = sanitizers
1504 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1505
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001506 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1507 if c.Host() {
1508 diagSanitizers = sanitizers
1509 }
1510
Colin Crosse323a792023-02-15 13:57:57 -08001511 addStaticDeps := func(dep string, hideSymbols bool) {
Colin Cross06c80eb2022-02-10 10:34:19 -08001512 // static executable gets static runtime libs
Colin Crosse323a792023-02-15 13:57:57 -08001513 depTag := libraryDependencyTag{Kind: staticLibraryDependency, unexportedSymbols: hideSymbols}
Colin Cross06c80eb2022-02-10 10:34:19 -08001514 variations := append(mctx.Target().Variations(),
1515 blueprint.Variation{Mutator: "link", Variation: "static"})
1516 if c.Device() {
1517 variations = append(variations, c.ImageVariation())
1518 }
1519 if c.UseSdk() {
1520 variations = append(variations,
1521 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1522 }
Colin Crosse323a792023-02-15 13:57:57 -08001523 mctx.AddFarVariationDependencies(variations, depTag, dep)
Colin Cross06c80eb2022-02-10 10:34:19 -08001524 }
Colin Crosse323a792023-02-15 13:57:57 -08001525
1526 // Determine the runtime library required
1527 runtimeSharedLibrary := ""
1528 toolchain := c.toolchain(mctx)
1529 if Bool(sanProps.Address) {
Colin Crossb781d232023-02-15 12:40:20 -08001530 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1531 // Use a static runtime for musl to match what clang does for glibc.
1532 addStaticDeps(config.AddressSanitizerStaticRuntimeLibrary(toolchain), false)
1533 addStaticDeps(config.AddressSanitizerCXXStaticRuntimeLibrary(toolchain), false)
1534 } else {
1535 runtimeSharedLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1536 }
Colin Crosse323a792023-02-15 13:57:57 -08001537 } else if Bool(sanProps.Hwaddress) {
1538 if c.staticBinary() {
1539 addStaticDeps(config.HWAddressSanitizerStaticLibrary(toolchain), true)
1540 addStaticDeps("libdl", false)
1541 } else {
1542 runtimeSharedLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1543 }
1544 } else if Bool(sanProps.Thread) {
1545 runtimeSharedLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1546 } else if Bool(sanProps.Scudo) {
1547 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1548 runtimeSharedLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1549 } else {
1550 runtimeSharedLibrary = config.ScudoRuntimeLibrary(toolchain)
1551 }
1552 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
1553 Bool(sanProps.Fuzzer) ||
1554 Bool(sanProps.Undefined) ||
1555 Bool(sanProps.All_undefined) {
Colin Cross0df81532023-08-23 22:20:51 -07001556 if toolchain.Musl() || c.staticBinary() {
1557 // Use a static runtime for static binaries. For sanitized glibc binaries the runtime is
1558 // added automatically by clang, but for static glibc binaries that are not sanitized but
1559 // have a sanitized dependency the runtime needs to be added manually.
1560 // Also manually add a static runtime for musl to match what clang does for glibc.
1561 // Otherwise dlopening libraries that depend on libclang_rt.ubsan_standalone.so fails with:
Colin Crosse323a792023-02-15 13:57:57 -08001562 // Error relocating ...: initial-exec TLS resolves to dynamic definition
1563 addStaticDeps(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)+".static", true)
1564 } else {
1565 runtimeSharedLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
1566 }
1567 }
1568
Colin Cross06c80eb2022-02-10 10:34:19 -08001569 if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
Colin Crosse323a792023-02-15 13:57:57 -08001570 addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001571 }
1572 if c.sanitize.Properties.BuiltinsDep {
Colin Crosse323a792023-02-15 13:57:57 -08001573 addStaticDeps(config.BuiltinsRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001574 }
1575
Colin Crosse323a792023-02-15 13:57:57 -08001576 if runtimeSharedLibrary != "" && (toolchain.Bionic() || toolchain.Musl() || c.sanitize.Properties.UbsanRuntimeDep) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001577 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001578
1579 // Adding dependency to the runtime library. We are using *FarVariation*
1580 // because the runtime libraries themselves are not mutated by sanitizer
1581 // mutators and thus don't have sanitizer variants whereas this module
1582 // has been already mutated.
1583 //
1584 // Note that by adding dependency with {static|shared}DepTag, the lib is
1585 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
Colin Crosse323a792023-02-15 13:57:57 -08001586 if c.staticBinary() {
1587 // Most sanitizers are either disabled for static binaries or have already
1588 // handled the static binary case above through a direct call to addStaticDeps.
1589 // If not, treat the runtime shared library as a static library and hope for
1590 // the best.
1591 addStaticDeps(runtimeSharedLibrary, true)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001592 } else if !c.static() && !c.Header() {
Cindy Zhou18417cb2020-12-10 07:12:38 -08001593 // Skip apex dependency check for sharedLibraryDependency
1594 // when sanitizer diags are enabled. Skipping the check will allow
1595 // building with diag libraries without having to list the
1596 // dependency in Apex's allowed_deps file.
1597 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001598 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001599 depTag := libraryDependencyTag{
1600 Kind: sharedLibraryDependency,
1601 Order: earlyLibraryDependency,
1602
1603 skipApexAllowedDependenciesCheck: diagEnabled,
1604 }
Colin Cross42507332020-08-21 16:15:23 -07001605 variations := append(mctx.Target().Variations(),
1606 blueprint.Variation{Mutator: "link", Variation: "shared"})
1607 if c.Device() {
1608 variations = append(variations, c.ImageVariation())
1609 }
Colin Cross06c80eb2022-02-10 10:34:19 -08001610 if c.UseSdk() {
1611 variations = append(variations,
1612 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1613 }
Colin Crosse323a792023-02-15 13:57:57 -08001614 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeSharedLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001615 }
1616 // static lib does not have dependency to the runtime library. The
1617 // dependency will be added to the executables or shared libs using
1618 // the static lib.
1619 }
1620 }
1621}
1622
1623type Sanitizeable interface {
1624 android.Module
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001625 IsSanitizerEnabled(config android.Config, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001626 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001627 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001628}
1629
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001630type JniSanitizeable interface {
1631 android.Module
1632 IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
1633}
1634
Ivan Lozanod7586b62021-04-01 09:49:36 -04001635func (c *Module) MinimalRuntimeDep() bool {
1636 return c.sanitize.Properties.MinimalRuntimeDep
1637}
1638
1639func (c *Module) UbsanRuntimeDep() bool {
1640 return c.sanitize.Properties.UbsanRuntimeDep
1641}
1642
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001643func (c *Module) SanitizePropDefined() bool {
1644 return c.sanitize != nil
1645}
1646
1647func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1648 return c.sanitize.isSanitizerEnabled(t)
1649}
1650
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001651func (c *Module) StaticallyLinked() bool {
1652 return c.static()
1653}
1654
1655func (c *Module) SetInSanitizerDir() {
1656 if c.sanitize != nil {
1657 c.sanitize.Properties.InSanitizerDir = true
1658 }
1659}
1660
1661func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1662 if c.sanitize != nil {
1663 c.sanitize.SetSanitizer(t, b)
1664 }
1665}
1666
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001667var _ PlatformSanitizeable = (*Module)(nil)
1668
Inseob Kim74d25562020-08-04 00:41:38 +09001669type sanitizerStaticLibsMap struct {
1670 // libsMap contains one list of modules per each image and each arch.
1671 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001672 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001673 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001674 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001675}
1676
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001677func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001678 return &sanitizerStaticLibsMap{
1679 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001680 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001681 }
1682}
1683
1684// Add the current module to sanitizer static libs maps
1685// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001686func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1687 image := GetImageVariantType(c)
1688 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001689
1690 s.libsMapLock.Lock()
1691 defer s.libsMapLock.Unlock()
1692
1693 if _, ok := s.libsMap[image]; !ok {
1694 s.libsMap[image] = make(map[string][]string)
1695 }
1696
1697 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1698}
1699
1700// Exports makefile variables in the following format:
1701// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1702// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1703// These are to be used by use_soong_sanitized_static_libraries.
1704// See build/make/core/binary.mk for more details.
1705func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
Cole Faust18994c72023-02-28 16:02:16 -08001706 for _, image := range android.SortedKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001707 archMap := s.libsMap[ImageVariantType(image)]
Cole Faust18994c72023-02-28 16:02:16 -08001708 for _, arch := range android.SortedKeys(archMap) {
Inseob Kim74d25562020-08-04 00:41:38 +09001709 libs := archMap[arch]
1710 sort.Strings(libs)
1711
1712 key := fmt.Sprintf(
1713 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1714 s.sanitizerType.variationName(),
1715 image, // already upper
1716 arch)
1717
1718 ctx.Strict(key, strings.Join(libs, " "))
1719 }
1720 }
1721}
1722
Colin Cross571cccf2019-02-04 11:22:08 -08001723var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1724
Inseob Kim74d25562020-08-04 00:41:38 +09001725func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001726 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001727 return newSanitizerStaticLibsMap(cfi)
1728 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001729}
1730
Colin Cross571cccf2019-02-04 11:22:08 -08001731var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1732
Inseob Kim74d25562020-08-04 00:41:38 +09001733func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001734 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001735 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001736 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001737}
1738
Ivan Lozano30c5db22018-02-21 15:49:20 -08001739func enableMinimalRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001740 if sanitize.isSanitizerEnabled(Asan) {
1741 return false
1742 } else if sanitize.isSanitizerEnabled(Hwasan) {
1743 return false
1744 } else if sanitize.isSanitizerEnabled(Fuzzer) {
1745 return false
Ivan Lozano30c5db22018-02-21 15:49:20 -08001746 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001747
1748 if enableUbsanRuntime(sanitize) {
1749 return false
1750 }
1751
1752 sanitizeProps := &sanitize.Properties.SanitizeMutated
1753 if Bool(sanitizeProps.Diag.Cfi) {
1754 return false
1755 }
1756
1757 return Bool(sanitizeProps.Integer_overflow) ||
1758 len(sanitizeProps.Misc_undefined) > 0 ||
1759 Bool(sanitizeProps.Undefined) ||
1760 Bool(sanitizeProps.All_undefined)
Ivan Lozano30c5db22018-02-21 15:49:20 -08001761}
1762
Ivan Lozanod7586b62021-04-01 09:49:36 -04001763func (m *Module) UbsanRuntimeNeeded() bool {
1764 return enableUbsanRuntime(m.sanitize)
1765}
1766
1767func (m *Module) MinimalRuntimeNeeded() bool {
1768 return enableMinimalRuntime(m.sanitize)
1769}
1770
Inseob Kim8471cda2019-11-15 09:59:12 +09001771func enableUbsanRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001772 sanitizeProps := &sanitize.Properties.SanitizeMutated
1773 return Bool(sanitizeProps.Diag.Integer_overflow) ||
1774 Bool(sanitizeProps.Diag.Undefined) ||
1775 len(sanitizeProps.Diag.Misc_undefined) > 0
Inseob Kim8471cda2019-11-15 09:59:12 +09001776}
1777
Vishwath Mohane7128792017-11-17 11:08:10 -08001778func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001779 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001780}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001781
1782func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001783 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001784}