blob: 626005b448cb886a2fdeb0adb15208b94840ed2d [file] [log] [blame]
Colin Cross16b23492016-01-06 14:41:07 -08001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
18 "fmt"
Jeff Gaston72765392017-11-28 16:37:53 -080019 "sort"
Colin Cross16b23492016-01-06 14:41:07 -080020 "strings"
Vishwath Mohane7128792017-11-17 11:08:10 -080021 "sync"
Colin Cross16b23492016-01-06 14:41:07 -080022
Colin Cross6b753602018-06-21 13:03:07 -070023 "github.com/google/blueprint"
Liz Kammerb2fc4702021-06-25 14:53:40 -040024 "github.com/google/blueprint/proptools"
Colin Cross6b753602018-06-21 13:03:07 -070025
Colin Cross635c3b02016-05-18 15:37:25 -070026 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070027 "android/soong/cc/config"
Kiyoung Kim48f37782021-07-07 12:42:39 +090028 "android/soong/snapshot"
Colin Cross16b23492016-01-06 14:41:07 -080029)
30
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070031var (
32 // Any C flags added by sanitizer which libTooling tools may not
33 // understand also need to be added to ClangLibToolingUnknownCflags in
34 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080035
Yi Kong20233a42019-08-21 01:38:40 -070036 asanCflags = []string{
37 "-fno-omit-frame-pointer",
Yi Kong20233a42019-08-21 01:38:40 -070038 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070039 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070040
Florian Mayera9984462023-06-16 16:48:51 -070041 // DO NOT ADD MLLVM FLAGS HERE! ADD THEM BELOW TO hwasanCommonFlags.
Yi Kong286abc62021-11-04 16:14:14 +080042 hwasanCflags = []string{
43 "-fno-omit-frame-pointer",
44 "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000045 "-fsanitize-hwaddress-abi=platform",
Yi Kong286abc62021-11-04 16:14:14 +080046 }
47
48 // ThinLTO performs codegen during link time, thus these flags need to
49 // passed to both CFLAGS and LDFLAGS.
50 hwasanCommonflags = []string{
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080051 // The following improves debug location information
52 // availability at the cost of its accuracy. It increases
53 // the likelihood of a stack variable's frame offset
54 // to be recorded in the debug info, which is important
55 // for the quality of hwasan reports. The downside is a
56 // higher number of "optimized out" stack variables.
57 // b/112437883.
Yi Kong286abc62021-11-04 16:14:14 +080058 "-instcombine-lower-dbg-declare=0",
Mitch Phillipsb1c574f2020-06-22 13:28:23 -070059 // TODO(b/159343917): HWASan and GlobalISel don't play nicely, and
60 // GlobalISel is the default at -O0 on aarch64.
Yi Kong286abc62021-11-04 16:14:14 +080061 "--aarch64-enable-global-isel-at-O=-1",
62 "-fast-isel=false",
Florian Mayera9984462023-06-16 16:48:51 -070063 "-hwasan-use-after-scope=1",
Florian Mayerc7466192023-06-16 16:50:59 -070064 "-dom-tree-reachability-max-bbs-to-explore=128",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080065 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070066
Trevor Radcliffeded095c2023-06-12 19:18:28 +000067 sanitizeIgnorelistPrefix = "-fsanitize-ignorelist="
68
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000069 cfiBlocklistPath = "external/compiler-rt/lib/cfi"
70 cfiBlocklistFilename = "cfi_blocklist.txt"
Trevor Radcliffef1836e42023-06-01 21:12:08 +000071 cfiEnableFlag = "-fsanitize=cfi"
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +000072 cfiCrossDsoFlag = "-fsanitize-cfi-cross-dso"
73 cfiCflags = []string{"-flto", cfiCrossDsoFlag,
Trevor Radcliffeded095c2023-06-12 19:18:28 +000074 sanitizeIgnorelistPrefix + cfiBlocklistPath + "/" + cfiBlocklistFilename}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070075 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
76 // used, but have no effect on assembly files
77 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Trevor Radcliffef1836e42023-06-01 21:12:08 +000078 cfiLdflags = []string{"-flto", cfiCrossDsoFlag, cfiEnableFlag,
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070079 "-Wl,-plugin-opt,O1"}
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000080 cfiExportsMapPath = "build/soong/cc/config"
81 cfiExportsMapFilename = "cfi_exports.map"
82 cfiAssemblySupportFlag = "-fno-sanitize-cfi-canonical-jump-tables"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070083
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070084 intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080085
Peter Collingbournebd19db02019-03-06 10:38:48 -080086 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070087 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070088 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
Christopher Ferris2fc8e032023-01-26 14:19:27 -080089 "export_memory_stats=0", "max_malloc_fill_size=131072", "malloc_fill_byte=0"}
Florian Mayer1866bbe2023-03-11 01:07:40 +000090 memtagStackCommonFlags = []string{"-march=armv8-a+memtag", "-mllvm", "-dom-tree-reachability-max-bbs-to-explore=128"}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +000091
92 hostOnlySanitizeFlags = []string{"-fno-sanitize-recover=all"}
93 deviceOnlySanitizeFlags = []string{"-fsanitize-trap=all", "-ftrap-function=abort"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070094)
95
Ivan Lozano3968d8f2020-12-14 11:27:52 -050096type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080097
Colin Cross16b23492016-01-06 14:41:07 -080098const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050099 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -0700100 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -0800101 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700102 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800103 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500104 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -0400105 Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +0000106 Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200107 Memtag_globals
Liz Kammer75db9312021-07-07 16:41:50 -0400108 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -0800109)
110
Liz Kammer75db9312021-07-07 16:41:50 -0400111var Sanitizers = []SanitizerType{
112 Asan,
113 Hwasan,
114 tsan,
115 intOverflow,
116 scs,
117 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400118 Memtag_heap,
Florian Mayerd8434a42022-08-31 20:57:03 +0000119 Memtag_stack,
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200120 Memtag_globals,
Liz Kammer75db9312021-07-07 16:41:50 -0400121 cfi, // cfi is last to prevent it running before incompatible mutators
122}
123
Jiyong Park82226632019-02-01 10:50:50 +0900124// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500125func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800126 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500127 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800128 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700129 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700130 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800131 case tsan:
132 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700133 case intOverflow:
134 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000135 case cfi:
136 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800137 case scs:
138 return "scs"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400139 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700140 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000141 case Memtag_stack:
142 return "memtag_stack"
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200143 case Memtag_globals:
144 return "memtag_globals"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500145 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700146 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800147 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500148 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800149 }
150}
151
Jiyong Park82226632019-02-01 10:50:50 +0900152// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500153func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900154 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500155 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900156 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700157 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900158 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400159 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700160 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000161 case Memtag_stack:
162 return "memtag_stack"
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200163 case Memtag_globals:
164 return "memtag_globals"
Jiyong Park82226632019-02-01 10:50:50 +0900165 case tsan:
166 return "thread"
167 case intOverflow:
168 return "integer_overflow"
169 case cfi:
170 return "cfi"
171 case scs:
172 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500173 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700174 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900175 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500176 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900177 }
178}
179
Liz Kammer75db9312021-07-07 16:41:50 -0400180func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
181 switch t {
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200182 case cfi, Hwasan, Asan, tsan, Fuzzer, scs:
183 sanitizer := &sanitizerSplitMutator{t}
184 ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
185 ctx.Transition(t.variationName(), sanitizer)
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200186 case Memtag_heap, Memtag_stack, Memtag_globals, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400187 // do nothing
188 default:
189 panic(fmt.Errorf("unknown SanitizerType %d", t))
190 }
191}
192
Liz Kammerfd8a49f2022-10-31 10:31:11 -0400193// shouldPropagateToSharedLibraryDeps returns whether a sanitizer type should propagate to share
194// dependencies. In most cases, sanitizers only propagate to static dependencies; however, some
195// sanitizers also must be enabled for shared libraries for linking.
196func (t SanitizerType) shouldPropagateToSharedLibraryDeps() bool {
197 switch t {
198 case Fuzzer:
199 // Typically, shared libs are not split. However, for fuzzer, we split even for shared libs
200 // because a library sanitized for fuzzer can't be linked from a library that isn't sanitized
201 // for fuzzer.
202 return true
203 default:
204 return false
205 }
206}
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500207func (*Module) SanitizerSupported(t SanitizerType) bool {
208 switch t {
209 case Asan:
210 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700211 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500212 return true
213 case tsan:
214 return true
215 case intOverflow:
216 return true
217 case cfi:
218 return true
219 case scs:
220 return true
221 case Fuzzer:
222 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400223 case Memtag_heap:
224 return true
Florian Mayerd8434a42022-08-31 20:57:03 +0000225 case Memtag_stack:
226 return true
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200227 case Memtag_globals:
228 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500229 default:
230 return false
231 }
232}
233
234// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
235func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700236 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900237}
238
Martin Stjernholmb0249572020-09-15 02:32:35 +0100239type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400240 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100241 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800242
Liz Kammer75b9b402021-06-25 15:19:27 -0400243 // ASan (Address sanitizer), incompatible with static binaries.
244 // Always runs in a diagnostic mode.
245 // Use of address sanitizer disables cfi sanitizer.
246 // Hwaddress sanitizer takes precedence over this sanitizer.
247 Address *bool `android:"arch_variant"`
248 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
249 // Always runs in a diagnostic mode.
250 // Use of thread sanitizer disables cfi and scudo sanitizers.
251 // Hwaddress sanitizer takes precedence over this sanitizer.
252 Thread *bool `android:"arch_variant"`
253 // HWASan (Hardware Address sanitizer).
254 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100255 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800256
Liz Kammer75b9b402021-06-25 15:19:27 -0400257 // Undefined behavior sanitizer
258 All_undefined *bool `android:"arch_variant"`
259 // Subset of undefined behavior sanitizer
260 Undefined *bool `android:"arch_variant"`
261 // List of specific undefined behavior sanitizers to enable
262 Misc_undefined []string `android:"arch_variant"`
263 // Fuzzer, incompatible with static binaries.
264 Fuzzer *bool `android:"arch_variant"`
265 // safe-stack sanitizer, incompatible with 32-bit architectures.
266 Safestack *bool `android:"arch_variant"`
267 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
268 Cfi *bool `android:"arch_variant"`
269 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
270 Integer_overflow *bool `android:"arch_variant"`
271 // scudo sanitizer, incompatible with asan, hwasan, tsan
272 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
273 // deprecated
274 Scudo *bool `android:"arch_variant"`
Elliott Hughese4793bc2023-02-09 21:15:47 +0000275 // shadow-call-stack sanitizer, only available on arm64/riscv64.
Liz Kammer75b9b402021-06-25 15:19:27 -0400276 Scs *bool `android:"arch_variant"`
277 // Memory-tagging, only available on arm64
278 // if diag.memtag unset or false, enables async memory tagging
Florian Mayer00ab5cf2022-08-31 18:30:18 +0000279 Memtag_heap *bool `android:"arch_variant"`
Florian Mayerd8434a42022-08-31 20:57:03 +0000280 // Memory-tagging stack instrumentation, only available on arm64
281 // Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
282 Memtag_stack *bool `android:"arch_variant"`
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200283 // Memory-tagging globals instrumentation, only available on arm64
284 // Adds instrumentation to detect global buffer overflows using MTE.
285 Memtag_globals *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100286
287 // A modifier for ASAN and HWASAN for write only instrumentation
288 Writeonly *bool `android:"arch_variant"`
289
290 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
291 // Replaces abort() on error with a human-readable error message.
292 // Address and Thread sanitizers always run in diagnostic mode.
293 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400294 // Undefined behavior sanitizer, diagnostic mode
295 Undefined *bool `android:"arch_variant"`
296 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
297 Cfi *bool `android:"arch_variant"`
298 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
299 Integer_overflow *bool `android:"arch_variant"`
300 // Memory-tagging, only available on arm64
301 // requires sanitizer.memtag: true
302 // if set, enables sync memory tagging
303 Memtag_heap *bool `android:"arch_variant"`
304 // List of specific undefined behavior sanitizers to enable in diagnostic mode
305 Misc_undefined []string `android:"arch_variant"`
306 // List of sanitizers to pass to -fno-sanitize-recover
307 // results in only the first detected error for these sanitizers being reported and program then
308 // exits with a non-zero exit code.
309 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800310 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800311
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800312 // Sanitizers to run with flag configuration specified
313 Config struct {
314 // Enables CFI support flags for assembly-heavy libraries
315 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800316 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800317
Liz Kammer75b9b402021-06-25 15:19:27 -0400318 // List of sanitizers to pass to -fsanitize-recover
319 // allows execution to continue for these sanitizers to detect multiple errors rather than only
320 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100321 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000322
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700323 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100324 Blocklist *string
325}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700326
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400327type sanitizeMutatedProperties struct {
328 // Whether sanitizers can be enabled on this module
329 Never *bool `blueprint:"mutated"`
330
331 // Whether ASan (Address sanitizer) is enabled for this module.
332 // Hwaddress sanitizer takes precedence over this sanitizer.
333 Address *bool `blueprint:"mutated"`
334 // Whether TSan (Thread sanitizer) is enabled for this module
335 Thread *bool `blueprint:"mutated"`
336 // Whether HWASan (Hardware Address sanitizer) is enabled for this module
337 Hwaddress *bool `blueprint:"mutated"`
338
339 // Whether Undefined behavior sanitizer is enabled for this module
340 All_undefined *bool `blueprint:"mutated"`
341 // Whether undefined behavior sanitizer subset is enabled for this module
342 Undefined *bool `blueprint:"mutated"`
343 // List of specific undefined behavior sanitizers enabled for this module
344 Misc_undefined []string `blueprint:"mutated"`
345 // Whether Fuzzeris enabled for this module
346 Fuzzer *bool `blueprint:"mutated"`
347 // whether safe-stack sanitizer is enabled for this module
348 Safestack *bool `blueprint:"mutated"`
349 // Whether cfi sanitizer is enabled for this module
350 Cfi *bool `blueprint:"mutated"`
351 // Whether signed/unsigned integer overflow sanitizer is enabled for this module
352 Integer_overflow *bool `blueprint:"mutated"`
353 // Whether scudo sanitizer is enabled for this module
354 Scudo *bool `blueprint:"mutated"`
355 // Whether shadow-call-stack sanitizer is enabled for this module.
356 Scs *bool `blueprint:"mutated"`
357 // Whether Memory-tagging is enabled for this module
358 Memtag_heap *bool `blueprint:"mutated"`
359 // Whether Memory-tagging stack instrumentation is enabled for this module
360 Memtag_stack *bool `blueprint:"mutated"`
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200361 // Whether Memory-tagging globals instrumentation is enabled for this module
362 Memtag_globals *bool `android:"arch_variant"`
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400363
364 // Whether a modifier for ASAN and HWASAN for write only instrumentation is enabled for this
365 // module
366 Writeonly *bool `blueprint:"mutated"`
367
368 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
369 Diag struct {
370 // Whether Undefined behavior sanitizer, diagnostic mode is enabled for this module
371 Undefined *bool `blueprint:"mutated"`
372 // Whether cfi sanitizer, diagnostic mode is enabled for this module
373 Cfi *bool `blueprint:"mutated"`
374 // Whether signed/unsigned integer overflow sanitizer, diagnostic mode is enabled for this
375 // module
376 Integer_overflow *bool `blueprint:"mutated"`
377 // Whether Memory-tagging, diagnostic mode is enabled for this module
378 Memtag_heap *bool `blueprint:"mutated"`
379 // List of specific undefined behavior sanitizers enabled in diagnostic mode
380 Misc_undefined []string `blueprint:"mutated"`
381 } `blueprint:"mutated"`
382}
383
Martin Stjernholmb0249572020-09-15 02:32:35 +0100384type SanitizeProperties struct {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400385 Sanitize SanitizeUserProps `android:"arch_variant"`
386 SanitizeMutated sanitizeMutatedProperties `blueprint:"mutated"`
387
388 SanitizerEnabled bool `blueprint:"mutated"`
389 MinimalRuntimeDep bool `blueprint:"mutated"`
390 BuiltinsDep bool `blueprint:"mutated"`
391 UbsanRuntimeDep bool `blueprint:"mutated"`
392 InSanitizerDir bool `blueprint:"mutated"`
393 Sanitizers []string `blueprint:"mutated"`
394 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800395}
396
397type sanitize struct {
398 Properties SanitizeProperties
399}
400
Cindy Zhou18417cb2020-12-10 07:12:38 -0800401// Mark this tag with a check to see if apex dependency check should be skipped
402func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
403 return t.skipApexAllowedDependenciesCheck
404}
405
406var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
407
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000408var exportedVars = android.NewExportedVariables(pctx)
409
Vishwath Mohane7128792017-11-17 11:08:10 -0800410func init() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000411 exportedVars.ExportStringListStaticVariable("HostOnlySanitizeFlags", hostOnlySanitizeFlags)
412 exportedVars.ExportStringList("DeviceOnlySanitizeFlags", deviceOnlySanitizeFlags)
413
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000414 // Leave out "-flto" from the slices exported to bazel, as we will use the
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000415 // dedicated LTO feature for this. For C Flags and Linker Flags, also leave
Trevor Radcliffef1836e42023-06-01 21:12:08 +0000416 // out the cross DSO flag which will be added separately under the correct conditions.
417 exportedVars.ExportStringList("CfiCFlags", append(cfiCflags[2:], cfiEnableFlag))
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000418 exportedVars.ExportStringList("CfiLdFlags", cfiLdflags[2:])
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000419 exportedVars.ExportStringList("CfiAsFlags", cfiAsflags[1:])
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000420
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000421 exportedVars.ExportString("SanitizeIgnorelistPrefix", sanitizeIgnorelistPrefix)
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000422 exportedVars.ExportString("CfiCrossDsoFlag", cfiCrossDsoFlag)
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000423 exportedVars.ExportString("CfiBlocklistPath", cfiBlocklistPath)
424 exportedVars.ExportString("CfiBlocklistFilename", cfiBlocklistFilename)
425 exportedVars.ExportString("CfiExportsMapPath", cfiExportsMapPath)
426 exportedVars.ExportString("CfiExportsMapFilename", cfiExportsMapFilename)
427 exportedVars.ExportString("CfiAssemblySupportFlag", cfiAssemblySupportFlag)
428
Vishwath Mohane7128792017-11-17 11:08:10 -0800429 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700430 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800431}
432
Colin Cross16b23492016-01-06 14:41:07 -0800433func (sanitize *sanitize) props() []interface{} {
434 return []interface{}{&sanitize.Properties}
435}
436
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400437func (p *sanitizeMutatedProperties) copyUserPropertiesToMutated(userProps *SanitizeUserProps) {
438 p.Never = userProps.Never
439 p.Address = userProps.Address
440 p.All_undefined = userProps.All_undefined
441 p.Cfi = userProps.Cfi
442 p.Fuzzer = userProps.Fuzzer
443 p.Hwaddress = userProps.Hwaddress
444 p.Integer_overflow = userProps.Integer_overflow
445 p.Memtag_heap = userProps.Memtag_heap
446 p.Memtag_stack = userProps.Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200447 p.Memtag_globals = userProps.Memtag_globals
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400448 p.Safestack = userProps.Safestack
449 p.Scs = userProps.Scs
450 p.Scudo = userProps.Scudo
451 p.Thread = userProps.Thread
452 p.Undefined = userProps.Undefined
453 p.Writeonly = userProps.Writeonly
454
455 p.Misc_undefined = make([]string, 0, len(userProps.Misc_undefined))
456 for _, v := range userProps.Misc_undefined {
457 p.Misc_undefined = append(p.Misc_undefined, v)
458 }
459
460 p.Diag.Cfi = userProps.Diag.Cfi
461 p.Diag.Integer_overflow = userProps.Diag.Integer_overflow
462 p.Diag.Memtag_heap = userProps.Diag.Memtag_heap
463 p.Diag.Undefined = userProps.Diag.Undefined
464
465 p.Diag.Misc_undefined = make([]string, 0, len(userProps.Diag.Misc_undefined))
466 for _, v := range userProps.Diag.Misc_undefined {
467 p.Diag.Misc_undefined = append(p.Diag.Misc_undefined, v)
468 }
469}
470
Colin Cross16b23492016-01-06 14:41:07 -0800471func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400472 s := &sanitize.Properties.SanitizeMutated
473 s.copyUserPropertiesToMutated(&sanitize.Properties.Sanitize)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700474
Colin Cross16b23492016-01-06 14:41:07 -0800475 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700476 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800477 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800478 }
479
480 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800481 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800482 return
483 }
484
Florian Mayerd8434a42022-08-31 20:57:03 +0000485 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400486 if ctx.testBinary() {
487 if s.Memtag_heap == nil {
488 s.Memtag_heap = proptools.BoolPtr(true)
489 }
490 if s.Diag.Memtag_heap == nil {
491 s.Diag.Memtag_heap = proptools.BoolPtr(true)
492 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800493 }
494
Colin Cross16b23492016-01-06 14:41:07 -0800495 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700496 var globalSanitizersDiag []string
497
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700498 if ctx.Host() {
499 if !ctx.Windows() {
500 globalSanitizers = ctx.Config().SanitizeHost()
501 }
502 } else {
503 arches := ctx.Config().SanitizeDeviceArch()
504 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
505 globalSanitizers = ctx.Config().SanitizeDevice()
506 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800507 }
508 }
509
Colin Cross16b23492016-01-06 14:41:07 -0800510 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000511 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700512 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400513 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000514 }
Colin Cross16b23492016-01-06 14:41:07 -0800515
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700516 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400517 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000518 }
519
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700520 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400521 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000522 }
523
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700524 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400525 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000526 }
527
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700528 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400529 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700530 }
531
532 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400533 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000534 }
535
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700536 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800537 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400538 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700539 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700540 }
541
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700542 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700543 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700544 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400545 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700546 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700547 }
548
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700549 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400550 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700551 }
552
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700553 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400554 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700555 }
556
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000557 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
558 // Hwaddress and Address are set before, so we can check them here
559 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
560 if s.Address == nil && s.Hwaddress == nil {
561 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
562 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400563 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000564 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700565 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800566 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400567 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800568 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700569 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000570
Florian Mayerd8434a42022-08-31 20:57:03 +0000571 if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
572 s.Memtag_stack = proptools.BoolPtr(true)
573 }
574
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200575 if found, globalSanitizers = removeFromList("memtag_globals", globalSanitizers); found && s.Memtag_globals == nil {
576 s.Memtag_globals = proptools.BoolPtr(true)
577 }
578
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000579 if len(globalSanitizers) > 0 {
580 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
581 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700582
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700583 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700584 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700585 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400586 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700587 }
588
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700589 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
590 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400591 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700592 }
593
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800594 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
595 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400596 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800597 }
598
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700599 if len(globalSanitizersDiag) > 0 {
600 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
601 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700602 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700603
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800604 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross88a029f2022-06-23 14:51:20 -0700605 if ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800606 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800607 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400608 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800609 }
610 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400611 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800612 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800613 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800614 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400615 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800616 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800617 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700618 }
619
Hang Lua98aab92023-03-17 13:17:22 +0800620 // Enable HWASan for all components in the include paths (for Aarch64 only)
621 if s.Hwaddress == nil && ctx.Config().HWASanEnabledForPath(ctx.ModuleDir()) &&
622 ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
623 s.Hwaddress = proptools.BoolPtr(true)
624 }
625
Elvis Chien9c993542021-06-25 01:15:17 +0800626 // Enable CFI for non-host components in the include paths
627 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400628 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000629 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400630 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700631 }
632 }
633
Elliott Hughesda3a0712020-03-06 16:55:28 -0800634 // Is CFI actually enabled?
635 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400636 s.Cfi = nil
637 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800638 }
639
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700640 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross88a029f2022-06-23 14:51:20 -0700641 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700642 s.Hwaddress = nil
643 }
644
Elliott Hughese4793bc2023-02-09 21:15:47 +0000645 // SCS is only implemented on AArch64/riscv64.
646 if (ctx.Arch().ArchType != android.Arm64 && ctx.Arch().ArchType != android.Riscv64) || !ctx.toolchain().Bionic() {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800647 s.Scs = nil
648 }
649
Ivan Lozano62cd0382021-11-01 10:27:54 -0400650 // Memtag_heap is only implemented on AArch64.
Florian Mayerd8434a42022-08-31 20:57:03 +0000651 // Memtag ABI is Android specific for now, so disable for host.
652 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() || ctx.Host() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700653 s.Memtag_heap = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000654 s.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200655 s.Memtag_globals = nil
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700656 }
657
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700658 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700659 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400660 s.Cfi = nil
661 s.Diag.Cfi = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000662 // HWASAN and ASAN win against MTE.
663 s.Memtag_heap = nil
664 s.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200665 s.Memtag_globals = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700666 }
667
Colin Crossed12a042022-02-07 13:55:55 -0800668 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
669 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400670 s.Cfi = nil
671 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700672 s.Misc_undefined = nil
673 s.Undefined = nil
674 s.All_undefined = nil
675 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800676 }
677
Aditya Kumar1281b992023-05-16 01:15:24 +0000678 // TODO(b/254713216): CFI doesn't work for riscv64 yet because LTO doesn't work.
679 if ctx.Arch().ArchType == android.Riscv64 {
680 s.Cfi = nil
681 s.Diag.Cfi = nil
682 }
683
Colin Crossed12a042022-02-07 13:55:55 -0800684 // Disable CFI for musl
685 if ctx.toolchain().Musl() {
686 s.Cfi = nil
687 s.Diag.Cfi = nil
688 }
689
Colin Cross390fc742023-05-02 13:02:51 -0700690 // TODO(b/280478629): runtimes don't exist for musl arm64 yet.
691 if ctx.toolchain().Musl() && ctx.Arch().ArchType == android.Arm64 {
692 s.Address = nil
693 s.Hwaddress = nil
694 s.Thread = nil
695 s.Scudo = nil
696 s.Fuzzer = nil
697 s.Cfi = nil
698 s.Diag.Cfi = nil
699 s.Misc_undefined = nil
700 s.Undefined = nil
701 s.All_undefined = nil
702 s.Integer_overflow = nil
703 }
704
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700705 // Also disable CFI for VNDK variants of components
706 if ctx.isVndk() && ctx.useVndk() {
Justin Yun08270c62022-12-19 17:01:26 +0900707 s.Cfi = nil
708 s.Diag.Cfi = nil
Inseob Kimeec88e12020-01-22 11:11:29 +0900709 }
710
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700711 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700712 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
713 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700714 s.Hwaddress = nil
715 }
716
Colin Cross3c344ef2016-07-18 15:44:56 -0700717 if ctx.staticBinary() {
718 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700719 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700720 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800721 }
722
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700723 if Bool(s.All_undefined) {
724 s.Undefined = nil
725 }
726
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700727 if !ctx.toolchain().Is64Bit() {
728 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700729 s.Thread = nil
730 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800731 // TODO(ccross): error for compile_multilib = "32"?
732 }
733
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800734 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 -0700735 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 +0200736 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack) ||
737 Bool(s.Memtag_globals)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700738 sanitize.Properties.SanitizerEnabled = true
739 }
740
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800741 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
742 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700743 s.Scudo = nil
744 }
745
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700746 if Bool(s.Hwaddress) {
747 s.Address = nil
748 s.Thread = nil
749 }
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000750
751 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
752 // mutually incompatible.
753 if Bool(s.Fuzzer) {
754 s.Cfi = nil
755 }
Colin Cross16b23492016-01-06 14:41:07 -0800756}
757
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800758func toDisableImplicitIntegerChange(flags []string) bool {
759 // Returns true if any flag is fsanitize*integer, and there is
760 // no explicit flag about sanitize=implicit-integer-sign-change.
761 for _, f := range flags {
762 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
763 return false
764 }
765 }
766 for _, f := range flags {
767 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
768 return true
769 }
770 }
771 return false
772}
773
Yabin Cuidb7dda82020-11-30 15:47:45 -0800774func toDisableUnsignedShiftBaseChange(flags []string) bool {
775 // Returns true if any flag is fsanitize*integer, and there is
776 // no explicit flag about sanitize=unsigned-shift-base.
777 for _, f := range flags {
778 if strings.Contains(f, "sanitize=unsigned-shift-base") {
779 return false
780 }
781 }
782 for _, f := range flags {
783 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
784 return true
785 }
786 }
787 return false
788}
789
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400790func (s *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
791 if !s.Properties.SanitizerEnabled && !s.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800792 return flags
793 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400794 sanProps := &s.Properties.SanitizeMutated
Colin Cross16b23492016-01-06 14:41:07 -0800795
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400796 if Bool(sanProps.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700797 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800798 // Frame pointer based unwinder in ASan requires ARM frame setup.
799 // TODO: put in flags?
800 flags.RequiredInstructionSet = "arm"
801 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800802 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
803 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800804
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400805 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000806 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
807 }
808
Colin Cross16b23492016-01-06 14:41:07 -0800809 if ctx.Host() {
810 // -nodefaultlibs (provided with libc++) prevents the driver from linking
811 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800812 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800813 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800814 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900815 if ctx.bootstrap() {
816 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
817 } else {
818 flags.DynamicLinker = "/system/bin/linker_asan"
819 }
Colin Cross16b23492016-01-06 14:41:07 -0800820 if flags.Toolchain.Is64Bit() {
821 flags.DynamicLinker += "64"
822 }
823 }
Colin Cross16b23492016-01-06 14:41:07 -0800824 }
825
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400826 if Bool(sanProps.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800827 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800828
829 for _, flag := range hwasanCommonflags {
830 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
831 }
832 for _, flag := range hwasanCommonflags {
833 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
834 }
835
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400836 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000837 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
838 }
Florian Mayer95cd6db2023-03-23 17:48:07 -0700839 if !ctx.staticBinary() && !ctx.Host() {
840 if ctx.bootstrap() {
841 flags.DynamicLinker = "/system/bin/bootstrap/linker_hwasan64"
842 } else {
843 flags.DynamicLinker = "/system/bin/linker_hwasan64"
844 }
845 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700846 }
847
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400848 if Bool(sanProps.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800849 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700850
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000851 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
852 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
853 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
854 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
855 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
856
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700857 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
858 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
859 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800860 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
861 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700862
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700863 // Disable fortify for fuzzing builds. Generally, we'll be building with
864 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800865 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800866
867 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
868 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
869 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
870 // the DT_RUNPATH from the shared library above it, and not the executable,
871 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
872 // DT_RUNPATH here means that transient shared libraries can be found
873 // colocated with their parents.
874 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800875 }
876
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400877 if Bool(sanProps.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800878 if ctx.Arch().ArchType == android.Arm {
879 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
880 // to do this on a function basis, so force Thumb on the entire module.
881 flags.RequiredInstructionSet = "thumb"
882 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000883
Colin Cross4af21ed2019-11-04 09:37:55 -0800884 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
885 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400886 if Bool(s.Properties.Sanitize.Config.Cfi_assembly_support) {
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000887 flags.Local.CFlags = append(flags.Local.CFlags, cfiAssemblySupportFlag)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800888 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000889 // Only append the default visibility flag if -fvisibility has not already been set
890 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800891 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
892 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000893 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800894 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000895
896 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800897 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
898 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000899 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700900 }
901
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400902 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000903 flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
904 flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
905 flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
906 }
907
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200908 if (Bool(sanProps.Memtag_heap) || Bool(sanProps.Memtag_stack) || Bool(sanProps.Memtag_globals)) && ctx.binary() {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400909 if Bool(sanProps.Diag.Memtag_heap) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000910 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
911 } else {
912 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
913 }
914 }
915
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400916 if Bool(sanProps.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800917 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700918 }
919
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400920 if len(s.Properties.Sanitizers) > 0 {
921 sanitizeArg := "-fsanitize=" + strings.Join(s.Properties.Sanitizers, ",")
Colin Cross4af21ed2019-11-04 09:37:55 -0800922 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
923 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross234b01d2022-02-07 13:49:03 -0800924 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
925
Colin Crossed12a042022-02-07 13:55:55 -0800926 if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
927 // Bionic and musl sanitizer runtimes have already been added as dependencies so that
928 // the right variant of the runtime will be used (with the "-android" or "-musl"
929 // suffixes), so don't let clang the runtime library.
Colin Cross234b01d2022-02-07 13:49:03 -0800930 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-link-runtime")
931 } else {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800932 // Host sanitizers only link symbols in the final executable, so
933 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800934 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
Colin Cross6c18d002022-06-02 15:11:50 -0700935 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500936
Colin Cross6c18d002022-06-02 15:11:50 -0700937 if !ctx.toolchain().Bionic() {
938 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function san.
939 // Musl toolchain prebuilts have vptr and function sanitizers, but enabling them
940 // implicitly enables RTTI which causes RTTI mismatch issues with dependencies.
941
Colin Cross234b01d2022-02-07 13:49:03 -0800942 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500943 }
944
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400945 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700946 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800947 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700948 } else if ctx.Host() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000949 flags.Local.CFlags = append(flags.Local.CFlags, hostOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700950 } else {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000951 flags.Local.CFlags = append(flags.Local.CFlags, deviceOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700952 }
Evgenii Stepanov59012812022-06-24 11:09:18 -0700953
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400954 if enableMinimalRuntime(s) {
Evgenii Stepanov59012812022-06-24 11:09:18 -0700955 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
956 }
957
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800958 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800959 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
960 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800961 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800962 // http://b/171275751, Android doesn't build with this sanitizer yet.
963 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
964 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
965 }
Colin Cross16b23492016-01-06 14:41:07 -0800966 }
967
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400968 if len(s.Properties.DiagSanitizers) > 0 {
969 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(s.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700970 }
971 // FIXME: enable RTTI if diag + (cfi or vptr)
972
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400973 if s.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800974 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400975 strings.Join(s.Properties.Sanitize.Recover, ","))
Andreas Gampe97071162017-05-08 13:15:23 -0700976 }
977
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400978 if s.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800979 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400980 strings.Join(s.Properties.Sanitize.Diag.No_recover, ","))
Ivan Lozano7929bba2018-12-12 09:36:31 -0800981 }
982
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400983 blocklist := android.OptionalPathForModuleSrc(ctx, s.Properties.Sanitize.Blocklist)
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700984 if blocklist.Valid() {
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000985 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeIgnorelistPrefix+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700986 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
987 }
988
Colin Cross16b23492016-01-06 14:41:07 -0800989 return flags
990}
991
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400992func (s *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900993 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
994 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800995 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400996 if Bool(s.Properties.SanitizeMutated.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800997 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900998 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400999 if Bool(s.Properties.SanitizeMutated.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -08001000 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +09001001 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001002 if Bool(s.Properties.SanitizeMutated.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -08001003 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +09001004 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001005 }
Colin Cross8ff9ef42017-05-08 13:44:11 -07001006}
1007
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001008func (s *sanitize) inSanitizerDir() bool {
1009 return s.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -07001010}
1011
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001012// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001013func (s *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +00001014 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001015 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001016 return s.Properties.SanitizeMutated.Address
Tri Vo6eafc362021-04-01 11:29:09 -07001017 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001018 return s.Properties.SanitizeMutated.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +00001019 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001020 return s.Properties.SanitizeMutated.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +00001021 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001022 return s.Properties.SanitizeMutated.Integer_overflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001023 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001024 return s.Properties.SanitizeMutated.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001025 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001026 return s.Properties.SanitizeMutated.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -04001027 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001028 return s.Properties.SanitizeMutated.Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +00001029 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001030 return s.Properties.SanitizeMutated.Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001031 case Memtag_globals:
1032 return s.Properties.SanitizeMutated.Memtag_globals
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001033 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001034 return s.Properties.SanitizeMutated.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +00001035 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001036 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +00001037 }
1038}
1039
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001040// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -08001041func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001042 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001043 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -08001044 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001045 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001046 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -04001047 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Florian Mayerd8434a42022-08-31 20:57:03 +00001048 !sanitize.isSanitizerEnabled(Memtag_stack) &&
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001049 !sanitize.isSanitizerEnabled(Memtag_globals) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001050 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -08001051}
1052
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001053// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001054func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001055 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001056 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001057 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001058 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001059}
1060
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001061func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -04001062 bPtr := proptools.BoolPtr(b)
1063 if !b {
1064 bPtr = nil
1065 }
Colin Cross16b23492016-01-06 14:41:07 -08001066 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001067 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001068 sanitize.Properties.SanitizeMutated.Address = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001069 // For ASAN variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001070 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001071 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Tri Vo6eafc362021-04-01 11:29:09 -07001072 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001073 sanitize.Properties.SanitizeMutated.Hwaddress = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001074 // For HWAsan variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001075 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001076 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Colin Cross16b23492016-01-06 14:41:07 -08001077 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001078 sanitize.Properties.SanitizeMutated.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -07001079 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001080 sanitize.Properties.SanitizeMutated.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001081 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001082 sanitize.Properties.SanitizeMutated.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001083 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001084 sanitize.Properties.SanitizeMutated.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -04001085 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001086 sanitize.Properties.SanitizeMutated.Memtag_heap = bPtr
Florian Mayerd8434a42022-08-31 20:57:03 +00001087 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001088 sanitize.Properties.SanitizeMutated.Memtag_stack = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001089 // We do not need to disable ASAN or HWASan here, as there is no Memtag_stack variant.
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001090 case Memtag_globals:
1091 sanitize.Properties.Sanitize.Memtag_globals = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001092 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001093 sanitize.Properties.SanitizeMutated.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -08001094 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001095 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -08001096 }
1097 if b {
1098 sanitize.Properties.SanitizerEnabled = true
1099 }
1100}
1101
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001102// Check if the sanitizer is explicitly disabled (as opposed to nil by
1103// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001104func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001105 if sanitize == nil {
1106 return false
1107 }
1108
1109 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1110 return sanitizerVal != nil && *sanitizerVal == false
1111}
1112
1113// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
1114// because enabling a sanitizer either directly (via the blueprint) or
1115// indirectly (via a mutator) sets the bool ptr to true, and you can't
1116// distinguish between the cases. It isn't needed though - both cases can be
1117// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001118func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001119 if sanitize == nil {
1120 return false
1121 }
1122
1123 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1124 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
Inseob Kimc42f2f22020-07-29 20:32:10 +09001143// Determines if the current module is a static library going to be captured
1144// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
1145// except for ones which explicitly disable cfi.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001146func needsCfiForVendorSnapshot(mctx android.BaseModuleContext) bool {
Justin Yun8814fc52022-12-15 21:45:35 +09001147 if inList("hwaddress", mctx.Config().SanitizeDevice()) {
1148 // cfi will not be built if SANITIZE_TARGET=hwaddress is set
1149 return false
1150 }
1151
Kiyoung Kim48f37782021-07-07 12:42:39 +09001152 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001153 return false
1154 }
1155
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001156 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001157
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001158 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001159 return false
1160 }
1161
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001162 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001163 return false
1164 }
1165
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001166 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001167 return false
1168 }
1169
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001170 if !c.SanitizerSupported(cfi) {
1171 return false
1172 }
1173
1174 return c.SanitizePropDefined() &&
1175 !c.SanitizeNever() &&
1176 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001177}
1178
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001179type sanitizerSplitMutator struct {
1180 sanitizer SanitizerType
1181}
1182
1183// If an APEX is sanitized or not depends on whether it contains at least one
1184// sanitized module. Transition mutators cannot propagate information up the
1185// dependency graph this way, so we need an auxiliary mutator to do so.
1186func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
1187 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1188 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1189 ctx.VisitDirectDeps(func(dep android.Module) {
1190 if c, ok := dep.(*Module); ok && c.sanitize.isSanitizerEnabled(s.sanitizer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001191 enabled = true
Inseob Kimc42f2f22020-07-29 20:32:10 +09001192 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001193 })
1194
1195 if enabled {
1196 sanitizeable.EnableSanitizer(s.sanitizer.name())
1197 }
1198 }
1199}
1200
1201func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
1202 if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1203 if s.sanitizer == cfi && needsCfiForVendorSnapshot(ctx) {
1204 return []string{"", s.sanitizer.variationName()}
1205 }
1206
1207 // If the given sanitizer is not requested in the .bp file for a module, it
1208 // won't automatically build the sanitized variation.
1209 if !c.IsSanitizerEnabled(s.sanitizer) {
1210 return []string{""}
1211 }
1212
1213 if c.Binary() {
1214 // If a sanitizer is enabled for a binary, we do not build the version
1215 // without the sanitizer
1216 return []string{s.sanitizer.variationName()}
1217 } else if c.StaticallyLinked() || c.Header() {
1218 // For static libraries, we build both versions. Some Make modules
1219 // apparently depend on this behavior.
1220 return []string{"", s.sanitizer.variationName()}
1221 } else {
1222 // We only build the requested variation of dynamic libraries
1223 return []string{s.sanitizer.variationName()}
1224 }
1225 }
1226
1227 if _, ok := ctx.Module().(JniSanitizeable); ok {
1228 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1229 // that is short-circuited for now
1230 return []string{""}
1231 }
1232
1233 // If an APEX has a sanitized dependency, we build the APEX in the sanitized
1234 // variation. This is useful because such APEXes require extra dependencies.
1235 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1236 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1237 if enabled {
1238 return []string{s.sanitizer.variationName()}
1239 } else {
1240 return []string{""}
1241 }
1242 }
1243
1244 if c, ok := ctx.Module().(*Module); ok {
1245 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1246
1247 // Check if it's a snapshot module supporting sanitizer
Justin Yun08270c62022-12-19 17:01:26 +09001248 if ss, ok := c.linker.(snapshotSanitizer); ok {
1249 if ss.isSanitizerAvailable(s.sanitizer) {
1250 return []string{"", s.sanitizer.variationName()}
1251 } else {
1252 return []string{""}
1253 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001254 }
1255 }
1256
1257 return []string{""}
1258}
1259
1260func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1261 if c, ok := ctx.Module().(PlatformSanitizeable); ok {
1262 if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
1263 // If the dependency is through a non-sanitizable tag, use the
1264 // non-sanitized variation
1265 return ""
1266 }
1267
1268 return sourceVariation
1269 } else if _, ok := ctx.Module().(JniSanitizeable); ok {
1270 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1271 // that is short-circuited for now
1272 return ""
1273 } else {
1274 // Otherwise, do not rock the boat.
1275 return sourceVariation
1276 }
1277}
1278
1279func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1280 if d, ok := ctx.Module().(PlatformSanitizeable); ok {
1281 if dm, ok := ctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001282 if ss, ok := dm.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001283 return incomingVariation
Inseob Kimc42f2f22020-07-29 20:32:10 +09001284 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001285 }
1286
1287 if !d.SanitizePropDefined() ||
1288 d.SanitizeNever() ||
1289 d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
1290 !d.SanitizerSupported(s.sanitizer) {
1291 // If a module opts out of a sanitizer, use its non-sanitized variation
1292 return ""
1293 }
1294
1295 // Binaries are always built in the variation they requested.
1296 if d.Binary() {
1297 if d.IsSanitizerEnabled(s.sanitizer) {
1298 return s.sanitizer.variationName()
1299 } else {
1300 return ""
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001301 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001302 }
1303
1304 // If a shared library requests to be sanitized, it will be built for that
1305 // sanitizer. Otherwise, some sanitizers propagate through shared library
1306 // dependency edges, some do not.
1307 if !d.StaticallyLinked() && !d.Header() {
1308 if d.IsSanitizerEnabled(s.sanitizer) {
1309 return s.sanitizer.variationName()
1310 }
1311
Liz Kammerfd8a49f2022-10-31 10:31:11 -04001312 // Some sanitizers do not propagate to shared dependencies
1313 if !s.sanitizer.shouldPropagateToSharedLibraryDeps() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001314 return ""
1315 }
1316 }
1317
1318 // Static and header libraries inherit whether they are sanitized from the
1319 // module they are linked into
1320 return incomingVariation
1321 } else if d, ok := ctx.Module().(Sanitizeable); ok {
1322 // If an APEX contains a sanitized module, it will be built in the variation
1323 // corresponding to that sanitizer.
1324 enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1325 if enabled {
1326 return s.sanitizer.variationName()
1327 }
1328
1329 return incomingVariation
1330 }
1331
1332 return ""
1333}
1334
1335func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
1336 sanitizerVariation := variationName == s.sanitizer.variationName()
1337
1338 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1339 sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
1340
1341 oneMakeVariation := false
1342 if c.StaticallyLinked() || c.Header() {
1343 if s.sanitizer != cfi && s.sanitizer != scs && s.sanitizer != Hwasan {
1344 // These sanitizers export only one variation to Make. For the rest,
1345 // Make targets can depend on both the sanitized and non-sanitized
1346 // versions.
1347 oneMakeVariation = true
1348 }
1349 } else if !c.Binary() {
1350 // Shared library. These are the sanitizers that do propagate through shared
1351 // library dependencies and therefore can cause multiple variations of a
1352 // shared library to be built.
1353 if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != scs && s.sanitizer != Asan {
1354 oneMakeVariation = true
1355 }
1356 }
1357
1358 if oneMakeVariation {
1359 if sanitizerEnabled != sanitizerVariation {
1360 c.SetPreventInstall()
1361 c.SetHideFromMake()
1362 }
1363 }
1364
1365 if sanitizerVariation {
1366 c.SetSanitizer(s.sanitizer, true)
1367
1368 // CFI is incompatible with ASAN so disable it in ASAN variations
1369 if s.sanitizer.incompatibleWithCfi() {
1370 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1371 if mctx.Device() && cfiSupported {
1372 c.SetSanitizer(cfi, false)
Jiyong Parkf97782b2019-02-13 20:28:58 +09001373 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001374 }
1375
1376 // locate the asan libraries under /data/asan
1377 if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
1378 c.SetInSanitizerDir()
1379 }
1380
1381 if c.StaticallyLinked() && c.ExportedToMake() {
1382 if s.sanitizer == Hwasan {
1383 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
1384 } else if s.sanitizer == cfi {
1385 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
1386 }
1387 }
1388 } else if c.IsSanitizerEnabled(s.sanitizer) {
1389 // Disable the sanitizer for the non-sanitized variation
1390 c.SetSanitizer(s.sanitizer, false)
1391 }
1392 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
1393 // If an APEX has sanitized dependencies, it gets a few more dependencies
1394 if sanitizerVariation {
1395 sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
1396 }
1397 } else if c, ok := mctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001398 if ss, ok := c.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
1399 if !ss.isUnsanitizedVariant() {
1400 // Snapshot sanitizer may have only one variantion.
1401 // Skip exporting the module if it already has a sanitizer variation.
1402 c.SetPreventInstall()
1403 c.SetHideFromMake()
1404 return
1405 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001406 c.linker.(snapshotSanitizer).setSanitizerVariation(s.sanitizer, sanitizerVariation)
1407
1408 // Export the static lib name to make
1409 if c.static() && c.ExportedToMake() {
Justin Yun39c30312022-11-23 16:20:12 +09001410 // use BaseModuleName which is the name for Make.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001411 if s.sanitizer == cfi {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001412 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Justin Yun39c30312022-11-23 16:20:12 +09001413 } else if s.sanitizer == Hwasan {
1414 hwasanStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001415 }
1416 }
Colin Cross16b23492016-01-06 14:41:07 -08001417 }
1418 }
1419}
1420
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001421func (c *Module) SanitizeNever() bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001422 return Bool(c.sanitize.Properties.SanitizeMutated.Never)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001423}
1424
1425func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
1426 return c.sanitize.isSanitizerExplicitlyDisabled(t)
1427}
1428
Ivan Lozano30c5db22018-02-21 15:49:20 -08001429// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001430func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001431 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001432 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001433 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001434 mctx.WalkDeps(func(child, parent android.Module) bool {
1435 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1436 return false
1437 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001438
Inseob Kimeec88e12020-01-22 11:11:29 +09001439 d, ok := child.(*Module)
1440 if !ok || !d.static() {
1441 return false
1442 }
1443 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001444 if enableMinimalRuntime(d.sanitize) {
1445 // If a static dependency is built with the minimal runtime,
1446 // make sure we include the ubsan minimal runtime.
1447 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001448 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001449 // If a static dependency runs with full ubsan diagnostics,
1450 // make sure we include the ubsan runtime.
1451 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001452 }
Colin Cross0b908332019-06-19 23:00:20 -07001453
1454 if c.sanitize.Properties.MinimalRuntimeDep &&
1455 c.sanitize.Properties.UbsanRuntimeDep {
1456 // both flags that this mutator might set are true, so don't bother recursing
1457 return false
1458 }
1459
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001460 if c.Os() == android.Linux {
1461 c.sanitize.Properties.BuiltinsDep = true
1462 }
1463
Colin Cross0b908332019-06-19 23:00:20 -07001464 return true
Colin Cross6b753602018-06-21 13:03:07 -07001465 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001466
Jose Galmesf7294582020-11-13 12:07:36 -08001467 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001468 if Bool(p.properties.Sanitize_minimal_dep) {
1469 c.sanitize.Properties.MinimalRuntimeDep = true
1470 }
1471 if Bool(p.properties.Sanitize_ubsan_dep) {
1472 c.sanitize.Properties.UbsanRuntimeDep = true
1473 }
1474 }
1475
1476 return false
Colin Cross6b753602018-06-21 13:03:07 -07001477 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001478 }
1479}
1480
Jiyong Park379de2f2018-12-19 02:47:14 +09001481// Add the dependency to the runtime library for each of the sanitizer variants
1482func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001483 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001484 if !c.Enabled() {
1485 return
1486 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001487 var sanitizers []string
1488 var diagSanitizers []string
1489
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001490 sanProps := &c.sanitize.Properties.SanitizeMutated
1491
1492 if Bool(sanProps.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001493 sanitizers = append(sanitizers, "undefined")
1494 } else {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001495 if Bool(sanProps.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001496 sanitizers = append(sanitizers,
1497 "bool",
1498 "integer-divide-by-zero",
1499 "return",
1500 "returns-nonnull-attribute",
1501 "shift-exponent",
1502 "unreachable",
1503 "vla-bound",
1504 // TODO(danalbert): The following checks currently have compiler performance issues.
1505 //"alignment",
1506 //"bounds",
1507 //"enum",
1508 //"float-cast-overflow",
1509 //"float-divide-by-zero",
1510 //"nonnull-attribute",
1511 //"null",
1512 //"shift-base",
1513 //"signed-integer-overflow",
1514 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1515 // https://llvm.org/PR19302
1516 // http://reviews.llvm.org/D6974
1517 // "object-size",
1518 )
1519 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001520 sanitizers = append(sanitizers, sanProps.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001521 }
1522
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001523 if Bool(sanProps.Diag.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001524 diagSanitizers = append(diagSanitizers, "undefined")
1525 }
1526
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001527 diagSanitizers = append(diagSanitizers, sanProps.Diag.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001528
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001529 if Bool(sanProps.Address) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001530 sanitizers = append(sanitizers, "address")
1531 diagSanitizers = append(diagSanitizers, "address")
1532 }
1533
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001534 if Bool(sanProps.Hwaddress) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001535 sanitizers = append(sanitizers, "hwaddress")
1536 }
1537
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001538 if Bool(sanProps.Thread) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001539 sanitizers = append(sanitizers, "thread")
1540 }
1541
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001542 if Bool(sanProps.Safestack) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001543 sanitizers = append(sanitizers, "safe-stack")
1544 }
1545
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001546 if Bool(sanProps.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001547 sanitizers = append(sanitizers, "cfi")
1548
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001549 if Bool(sanProps.Diag.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001550 diagSanitizers = append(diagSanitizers, "cfi")
1551 }
1552 }
1553
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001554 if Bool(sanProps.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001555 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1556 sanitizers = append(sanitizers, "signed-integer-overflow")
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001557 if Bool(sanProps.Diag.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001558 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1559 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1560 }
1561 }
1562
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001563 if Bool(sanProps.Scudo) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001564 sanitizers = append(sanitizers, "scudo")
1565 }
1566
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001567 if Bool(sanProps.Scs) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001568 sanitizers = append(sanitizers, "shadow-call-stack")
1569 }
1570
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001571 if Bool(sanProps.Memtag_heap) && c.Binary() {
Florian Mayerd8434a42022-08-31 20:57:03 +00001572 sanitizers = append(sanitizers, "memtag-heap")
1573 }
1574
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001575 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +00001576 sanitizers = append(sanitizers, "memtag-stack")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001577 }
1578
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001579 if Bool(sanProps.Memtag_globals) {
1580 sanitizers = append(sanitizers, "memtag-globals")
1581 // TODO(mitchp): For now, enable memtag-heap with memtag-globals because the linker
1582 // isn't new enough (https://reviews.llvm.org/differential/changeset/?ref=4243566).
1583 sanitizers = append(sanitizers, "memtag-heap")
1584 }
1585
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001586 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001587 sanitizers = append(sanitizers, "fuzzer-no-link")
1588 }
1589
Jiyong Park379de2f2018-12-19 02:47:14 +09001590 // Save the list of sanitizers. These will be used again when generating
1591 // the build rules (for Cflags, etc.)
1592 c.sanitize.Properties.Sanitizers = sanitizers
1593 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1594
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001595 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1596 if c.Host() {
1597 diagSanitizers = sanitizers
1598 }
1599
Colin Crosse323a792023-02-15 13:57:57 -08001600 addStaticDeps := func(dep string, hideSymbols bool) {
Colin Cross06c80eb2022-02-10 10:34:19 -08001601 // If we're using snapshots, redirect to snapshot whenever possible
1602 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001603 if lib, ok := snapshot.StaticLibs[dep]; ok {
1604 dep = lib
Colin Cross06c80eb2022-02-10 10:34:19 -08001605 }
1606
1607 // static executable gets static runtime libs
Colin Crosse323a792023-02-15 13:57:57 -08001608 depTag := libraryDependencyTag{Kind: staticLibraryDependency, unexportedSymbols: hideSymbols}
Colin Cross06c80eb2022-02-10 10:34:19 -08001609 variations := append(mctx.Target().Variations(),
1610 blueprint.Variation{Mutator: "link", Variation: "static"})
1611 if c.Device() {
1612 variations = append(variations, c.ImageVariation())
1613 }
1614 if c.UseSdk() {
1615 variations = append(variations,
1616 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1617 }
Colin Crosse323a792023-02-15 13:57:57 -08001618 mctx.AddFarVariationDependencies(variations, depTag, dep)
Colin Cross06c80eb2022-02-10 10:34:19 -08001619 }
Colin Crosse323a792023-02-15 13:57:57 -08001620
1621 // Determine the runtime library required
1622 runtimeSharedLibrary := ""
1623 toolchain := c.toolchain(mctx)
1624 if Bool(sanProps.Address) {
Colin Crossb781d232023-02-15 12:40:20 -08001625 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1626 // Use a static runtime for musl to match what clang does for glibc.
1627 addStaticDeps(config.AddressSanitizerStaticRuntimeLibrary(toolchain), false)
1628 addStaticDeps(config.AddressSanitizerCXXStaticRuntimeLibrary(toolchain), false)
1629 } else {
1630 runtimeSharedLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1631 }
Colin Crosse323a792023-02-15 13:57:57 -08001632 } else if Bool(sanProps.Hwaddress) {
1633 if c.staticBinary() {
1634 addStaticDeps(config.HWAddressSanitizerStaticLibrary(toolchain), true)
1635 addStaticDeps("libdl", false)
1636 } else {
1637 runtimeSharedLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1638 }
1639 } else if Bool(sanProps.Thread) {
1640 runtimeSharedLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1641 } else if Bool(sanProps.Scudo) {
1642 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1643 runtimeSharedLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1644 } else {
1645 runtimeSharedLibrary = config.ScudoRuntimeLibrary(toolchain)
1646 }
1647 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
1648 Bool(sanProps.Fuzzer) ||
1649 Bool(sanProps.Undefined) ||
1650 Bool(sanProps.All_undefined) {
1651 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1652 // Use a static runtime for static binaries.
1653 // Also use a static runtime for musl to match
1654 // what clang does for glibc. Otherwise dlopening
1655 // libraries that depend on libclang_rt.ubsan_standalone.so
1656 // fails with:
1657 // Error relocating ...: initial-exec TLS resolves to dynamic definition
1658 addStaticDeps(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)+".static", true)
1659 } else {
1660 runtimeSharedLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
1661 }
1662 }
1663
Colin Cross06c80eb2022-02-10 10:34:19 -08001664 if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
Colin Crosse323a792023-02-15 13:57:57 -08001665 addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001666 }
1667 if c.sanitize.Properties.BuiltinsDep {
Colin Crosse323a792023-02-15 13:57:57 -08001668 addStaticDeps(config.BuiltinsRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001669 }
1670
Colin Crosse323a792023-02-15 13:57:57 -08001671 if runtimeSharedLibrary != "" && (toolchain.Bionic() || toolchain.Musl() || c.sanitize.Properties.UbsanRuntimeDep) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001672 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001673
1674 // Adding dependency to the runtime library. We are using *FarVariation*
1675 // because the runtime libraries themselves are not mutated by sanitizer
1676 // mutators and thus don't have sanitizer variants whereas this module
1677 // has been already mutated.
1678 //
1679 // Note that by adding dependency with {static|shared}DepTag, the lib is
1680 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
Colin Crosse323a792023-02-15 13:57:57 -08001681 if c.staticBinary() {
1682 // Most sanitizers are either disabled for static binaries or have already
1683 // handled the static binary case above through a direct call to addStaticDeps.
1684 // If not, treat the runtime shared library as a static library and hope for
1685 // the best.
1686 addStaticDeps(runtimeSharedLibrary, true)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001687 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001688 // If we're using snapshots, redirect to snapshot whenever possible
1689 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001690 if lib, ok := snapshot.SharedLibs[runtimeSharedLibrary]; ok {
1691 runtimeSharedLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001692 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001693
Cindy Zhou18417cb2020-12-10 07:12:38 -08001694 // Skip apex dependency check for sharedLibraryDependency
1695 // when sanitizer diags are enabled. Skipping the check will allow
1696 // building with diag libraries without having to list the
1697 // dependency in Apex's allowed_deps file.
1698 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001699 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001700 depTag := libraryDependencyTag{
1701 Kind: sharedLibraryDependency,
1702 Order: earlyLibraryDependency,
1703
1704 skipApexAllowedDependenciesCheck: diagEnabled,
1705 }
Colin Cross42507332020-08-21 16:15:23 -07001706 variations := append(mctx.Target().Variations(),
1707 blueprint.Variation{Mutator: "link", Variation: "shared"})
1708 if c.Device() {
1709 variations = append(variations, c.ImageVariation())
1710 }
Colin Cross06c80eb2022-02-10 10:34:19 -08001711 if c.UseSdk() {
1712 variations = append(variations,
1713 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1714 }
Colin Crosse323a792023-02-15 13:57:57 -08001715 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeSharedLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001716 }
1717 // static lib does not have dependency to the runtime library. The
1718 // dependency will be added to the executables or shared libs using
1719 // the static lib.
1720 }
1721 }
1722}
1723
1724type Sanitizeable interface {
1725 android.Module
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001726 IsSanitizerEnabled(config android.Config, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001727 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001728 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001729}
1730
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001731type JniSanitizeable interface {
1732 android.Module
1733 IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
1734}
1735
Ivan Lozanod7586b62021-04-01 09:49:36 -04001736func (c *Module) MinimalRuntimeDep() bool {
1737 return c.sanitize.Properties.MinimalRuntimeDep
1738}
1739
1740func (c *Module) UbsanRuntimeDep() bool {
1741 return c.sanitize.Properties.UbsanRuntimeDep
1742}
1743
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001744func (c *Module) SanitizePropDefined() bool {
1745 return c.sanitize != nil
1746}
1747
1748func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1749 return c.sanitize.isSanitizerEnabled(t)
1750}
1751
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001752func (c *Module) StaticallyLinked() bool {
1753 return c.static()
1754}
1755
1756func (c *Module) SetInSanitizerDir() {
1757 if c.sanitize != nil {
1758 c.sanitize.Properties.InSanitizerDir = true
1759 }
1760}
1761
1762func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1763 if c.sanitize != nil {
1764 c.sanitize.SetSanitizer(t, b)
1765 }
1766}
1767
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001768var _ PlatformSanitizeable = (*Module)(nil)
1769
Inseob Kim74d25562020-08-04 00:41:38 +09001770type sanitizerStaticLibsMap struct {
1771 // libsMap contains one list of modules per each image and each arch.
1772 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001773 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001774 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001775 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001776}
1777
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001778func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001779 return &sanitizerStaticLibsMap{
1780 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001781 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001782 }
1783}
1784
1785// Add the current module to sanitizer static libs maps
1786// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001787func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1788 image := GetImageVariantType(c)
1789 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001790
1791 s.libsMapLock.Lock()
1792 defer s.libsMapLock.Unlock()
1793
1794 if _, ok := s.libsMap[image]; !ok {
1795 s.libsMap[image] = make(map[string][]string)
1796 }
1797
1798 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1799}
1800
1801// Exports makefile variables in the following format:
1802// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1803// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1804// These are to be used by use_soong_sanitized_static_libraries.
1805// See build/make/core/binary.mk for more details.
1806func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
Cole Faust18994c72023-02-28 16:02:16 -08001807 for _, image := range android.SortedKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001808 archMap := s.libsMap[ImageVariantType(image)]
Cole Faust18994c72023-02-28 16:02:16 -08001809 for _, arch := range android.SortedKeys(archMap) {
Inseob Kim74d25562020-08-04 00:41:38 +09001810 libs := archMap[arch]
1811 sort.Strings(libs)
1812
1813 key := fmt.Sprintf(
1814 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1815 s.sanitizerType.variationName(),
1816 image, // already upper
1817 arch)
1818
1819 ctx.Strict(key, strings.Join(libs, " "))
1820 }
1821 }
1822}
1823
Colin Cross571cccf2019-02-04 11:22:08 -08001824var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1825
Inseob Kim74d25562020-08-04 00:41:38 +09001826func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001827 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001828 return newSanitizerStaticLibsMap(cfi)
1829 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001830}
1831
Colin Cross571cccf2019-02-04 11:22:08 -08001832var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1833
Inseob Kim74d25562020-08-04 00:41:38 +09001834func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001835 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001836 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001837 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001838}
1839
Ivan Lozano30c5db22018-02-21 15:49:20 -08001840func enableMinimalRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001841 if sanitize.isSanitizerEnabled(Asan) {
1842 return false
1843 } else if sanitize.isSanitizerEnabled(Hwasan) {
1844 return false
1845 } else if sanitize.isSanitizerEnabled(Fuzzer) {
1846 return false
Ivan Lozano30c5db22018-02-21 15:49:20 -08001847 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001848
1849 if enableUbsanRuntime(sanitize) {
1850 return false
1851 }
1852
1853 sanitizeProps := &sanitize.Properties.SanitizeMutated
1854 if Bool(sanitizeProps.Diag.Cfi) {
1855 return false
1856 }
1857
1858 return Bool(sanitizeProps.Integer_overflow) ||
1859 len(sanitizeProps.Misc_undefined) > 0 ||
1860 Bool(sanitizeProps.Undefined) ||
1861 Bool(sanitizeProps.All_undefined)
Ivan Lozano30c5db22018-02-21 15:49:20 -08001862}
1863
Ivan Lozanod7586b62021-04-01 09:49:36 -04001864func (m *Module) UbsanRuntimeNeeded() bool {
1865 return enableUbsanRuntime(m.sanitize)
1866}
1867
1868func (m *Module) MinimalRuntimeNeeded() bool {
1869 return enableMinimalRuntime(m.sanitize)
1870}
1871
Inseob Kim8471cda2019-11-15 09:59:12 +09001872func enableUbsanRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001873 sanitizeProps := &sanitize.Properties.SanitizeMutated
1874 return Bool(sanitizeProps.Diag.Integer_overflow) ||
1875 Bool(sanitizeProps.Diag.Undefined) ||
1876 len(sanitizeProps.Diag.Misc_undefined) > 0
Inseob Kim8471cda2019-11-15 09:59:12 +09001877}
1878
Vishwath Mohane7128792017-11-17 11:08:10 -08001879func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001880 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001881}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001882
1883func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001884 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001885}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +00001886
1887func BazelCcSanitizerToolchainVars(config android.Config) string {
1888 return android.BazelToolchainVars(config, exportedVars)
1889}