blob: 68ac78e9538bd2a22eca3b292586f518b1efaeb6 [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",
Florian Mayera9984462023-06-16 16:48:51 -070059 "-hwasan-use-after-scope=1",
Florian Mayerc7466192023-06-16 16:50:59 -070060 "-dom-tree-reachability-max-bbs-to-explore=128",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080061 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070062
Trevor Radcliffeded095c2023-06-12 19:18:28 +000063 sanitizeIgnorelistPrefix = "-fsanitize-ignorelist="
64
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000065 cfiBlocklistPath = "external/compiler-rt/lib/cfi"
66 cfiBlocklistFilename = "cfi_blocklist.txt"
Trevor Radcliffef1836e42023-06-01 21:12:08 +000067 cfiEnableFlag = "-fsanitize=cfi"
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +000068 cfiCrossDsoFlag = "-fsanitize-cfi-cross-dso"
69 cfiCflags = []string{"-flto", cfiCrossDsoFlag,
Trevor Radcliffeded095c2023-06-12 19:18:28 +000070 sanitizeIgnorelistPrefix + cfiBlocklistPath + "/" + cfiBlocklistFilename}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070071 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
72 // used, but have no effect on assembly files
73 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Trevor Radcliffef1836e42023-06-01 21:12:08 +000074 cfiLdflags = []string{"-flto", cfiCrossDsoFlag, cfiEnableFlag,
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070075 "-Wl,-plugin-opt,O1"}
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000076 cfiExportsMapPath = "build/soong/cc/config"
77 cfiExportsMapFilename = "cfi_exports.map"
78 cfiAssemblySupportFlag = "-fno-sanitize-cfi-canonical-jump-tables"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070079
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070080 intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080081
Peter Collingbournebd19db02019-03-06 10:38:48 -080082 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070083 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070084 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
Christopher Ferris2fc8e032023-01-26 14:19:27 -080085 "export_memory_stats=0", "max_malloc_fill_size=131072", "malloc_fill_byte=0"}
Florian Mayer1866bbe2023-03-11 01:07:40 +000086 memtagStackCommonFlags = []string{"-march=armv8-a+memtag", "-mllvm", "-dom-tree-reachability-max-bbs-to-explore=128"}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +000087
88 hostOnlySanitizeFlags = []string{"-fno-sanitize-recover=all"}
89 deviceOnlySanitizeFlags = []string{"-fsanitize-trap=all", "-ftrap-function=abort"}
Trevor Radcliffeda64d912023-08-02 20:24:29 +000090
91 noSanitizeLinkRuntimeFlag = "-fno-sanitize-link-runtime"
Dan Willemsencbceaab2016-10-13 16:44:07 -070092)
93
Ivan Lozano3968d8f2020-12-14 11:27:52 -050094type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080095
Colin Cross16b23492016-01-06 14:41:07 -080096const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050097 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070098 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080099 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700100 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800101 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500102 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -0400103 Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +0000104 Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200105 Memtag_globals
Liz Kammer75db9312021-07-07 16:41:50 -0400106 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -0800107)
108
Liz Kammer75db9312021-07-07 16:41:50 -0400109var Sanitizers = []SanitizerType{
110 Asan,
111 Hwasan,
112 tsan,
113 intOverflow,
114 scs,
115 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400116 Memtag_heap,
Florian Mayerd8434a42022-08-31 20:57:03 +0000117 Memtag_stack,
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200118 Memtag_globals,
Liz Kammer75db9312021-07-07 16:41:50 -0400119 cfi, // cfi is last to prevent it running before incompatible mutators
120}
121
Jiyong Park82226632019-02-01 10:50:50 +0900122// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500123func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800124 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500125 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800126 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700127 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700128 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800129 case tsan:
130 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700131 case intOverflow:
132 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000133 case cfi:
134 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800135 case scs:
136 return "scs"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400137 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700138 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000139 case Memtag_stack:
140 return "memtag_stack"
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200141 case Memtag_globals:
142 return "memtag_globals"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500143 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700144 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800145 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500146 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800147 }
148}
149
Jiyong Park82226632019-02-01 10:50:50 +0900150// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500151func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900152 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500153 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900154 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700155 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900156 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400157 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700158 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000159 case Memtag_stack:
160 return "memtag_stack"
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200161 case Memtag_globals:
162 return "memtag_globals"
Jiyong Park82226632019-02-01 10:50:50 +0900163 case tsan:
164 return "thread"
165 case intOverflow:
166 return "integer_overflow"
167 case cfi:
168 return "cfi"
169 case scs:
170 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500171 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700172 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900173 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500174 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900175 }
176}
177
Liz Kammer75db9312021-07-07 16:41:50 -0400178func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
179 switch t {
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200180 case cfi, Hwasan, Asan, tsan, Fuzzer, scs:
181 sanitizer := &sanitizerSplitMutator{t}
182 ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
183 ctx.Transition(t.variationName(), sanitizer)
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200184 case Memtag_heap, Memtag_stack, Memtag_globals, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400185 // do nothing
186 default:
187 panic(fmt.Errorf("unknown SanitizerType %d", t))
188 }
189}
190
Liz Kammerfd8a49f2022-10-31 10:31:11 -0400191// shouldPropagateToSharedLibraryDeps returns whether a sanitizer type should propagate to share
192// dependencies. In most cases, sanitizers only propagate to static dependencies; however, some
193// sanitizers also must be enabled for shared libraries for linking.
194func (t SanitizerType) shouldPropagateToSharedLibraryDeps() bool {
195 switch t {
196 case Fuzzer:
197 // Typically, shared libs are not split. However, for fuzzer, we split even for shared libs
198 // because a library sanitized for fuzzer can't be linked from a library that isn't sanitized
199 // for fuzzer.
200 return true
201 default:
202 return false
203 }
204}
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500205func (*Module) SanitizerSupported(t SanitizerType) bool {
206 switch t {
207 case Asan:
208 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700209 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500210 return true
211 case tsan:
212 return true
213 case intOverflow:
214 return true
215 case cfi:
216 return true
217 case scs:
218 return true
219 case Fuzzer:
220 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400221 case Memtag_heap:
222 return true
Florian Mayerd8434a42022-08-31 20:57:03 +0000223 case Memtag_stack:
224 return true
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200225 case Memtag_globals:
226 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500227 default:
228 return false
229 }
230}
231
232// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
233func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700234 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900235}
236
Martin Stjernholmb0249572020-09-15 02:32:35 +0100237type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400238 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100239 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800240
Liz Kammer75b9b402021-06-25 15:19:27 -0400241 // ASan (Address sanitizer), incompatible with static binaries.
242 // Always runs in a diagnostic mode.
243 // Use of address sanitizer disables cfi sanitizer.
244 // Hwaddress sanitizer takes precedence over this sanitizer.
245 Address *bool `android:"arch_variant"`
246 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
247 // Always runs in a diagnostic mode.
248 // Use of thread sanitizer disables cfi and scudo sanitizers.
249 // Hwaddress sanitizer takes precedence over this sanitizer.
250 Thread *bool `android:"arch_variant"`
251 // HWASan (Hardware Address sanitizer).
252 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100253 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800254
Liz Kammer75b9b402021-06-25 15:19:27 -0400255 // Undefined behavior sanitizer
256 All_undefined *bool `android:"arch_variant"`
257 // Subset of undefined behavior sanitizer
258 Undefined *bool `android:"arch_variant"`
259 // List of specific undefined behavior sanitizers to enable
260 Misc_undefined []string `android:"arch_variant"`
261 // Fuzzer, incompatible with static binaries.
262 Fuzzer *bool `android:"arch_variant"`
263 // safe-stack sanitizer, incompatible with 32-bit architectures.
264 Safestack *bool `android:"arch_variant"`
265 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
266 Cfi *bool `android:"arch_variant"`
267 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
268 Integer_overflow *bool `android:"arch_variant"`
269 // scudo sanitizer, incompatible with asan, hwasan, tsan
270 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
271 // deprecated
272 Scudo *bool `android:"arch_variant"`
Elliott Hughese4793bc2023-02-09 21:15:47 +0000273 // shadow-call-stack sanitizer, only available on arm64/riscv64.
Liz Kammer75b9b402021-06-25 15:19:27 -0400274 Scs *bool `android:"arch_variant"`
275 // Memory-tagging, only available on arm64
276 // if diag.memtag unset or false, enables async memory tagging
Florian Mayer00ab5cf2022-08-31 18:30:18 +0000277 Memtag_heap *bool `android:"arch_variant"`
Florian Mayerd8434a42022-08-31 20:57:03 +0000278 // Memory-tagging stack instrumentation, only available on arm64
279 // Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
280 Memtag_stack *bool `android:"arch_variant"`
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200281 // Memory-tagging globals instrumentation, only available on arm64
282 // Adds instrumentation to detect global buffer overflows using MTE.
283 Memtag_globals *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100284
285 // A modifier for ASAN and HWASAN for write only instrumentation
286 Writeonly *bool `android:"arch_variant"`
287
288 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
289 // Replaces abort() on error with a human-readable error message.
290 // Address and Thread sanitizers always run in diagnostic mode.
291 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400292 // Undefined behavior sanitizer, diagnostic mode
293 Undefined *bool `android:"arch_variant"`
294 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
295 Cfi *bool `android:"arch_variant"`
296 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
297 Integer_overflow *bool `android:"arch_variant"`
298 // Memory-tagging, only available on arm64
299 // requires sanitizer.memtag: true
300 // if set, enables sync memory tagging
301 Memtag_heap *bool `android:"arch_variant"`
302 // List of specific undefined behavior sanitizers to enable in diagnostic mode
303 Misc_undefined []string `android:"arch_variant"`
304 // List of sanitizers to pass to -fno-sanitize-recover
305 // results in only the first detected error for these sanitizers being reported and program then
306 // exits with a non-zero exit code.
307 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800308 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800309
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800310 // Sanitizers to run with flag configuration specified
311 Config struct {
312 // Enables CFI support flags for assembly-heavy libraries
313 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800314 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800315
Liz Kammer75b9b402021-06-25 15:19:27 -0400316 // List of sanitizers to pass to -fsanitize-recover
317 // allows execution to continue for these sanitizers to detect multiple errors rather than only
318 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100319 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000320
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700321 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100322 Blocklist *string
323}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700324
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400325type sanitizeMutatedProperties struct {
326 // Whether sanitizers can be enabled on this module
327 Never *bool `blueprint:"mutated"`
328
329 // Whether ASan (Address sanitizer) is enabled for this module.
330 // Hwaddress sanitizer takes precedence over this sanitizer.
331 Address *bool `blueprint:"mutated"`
332 // Whether TSan (Thread sanitizer) is enabled for this module
333 Thread *bool `blueprint:"mutated"`
334 // Whether HWASan (Hardware Address sanitizer) is enabled for this module
335 Hwaddress *bool `blueprint:"mutated"`
336
337 // Whether Undefined behavior sanitizer is enabled for this module
338 All_undefined *bool `blueprint:"mutated"`
339 // Whether undefined behavior sanitizer subset is enabled for this module
340 Undefined *bool `blueprint:"mutated"`
341 // List of specific undefined behavior sanitizers enabled for this module
342 Misc_undefined []string `blueprint:"mutated"`
343 // Whether Fuzzeris enabled for this module
344 Fuzzer *bool `blueprint:"mutated"`
345 // whether safe-stack sanitizer is enabled for this module
346 Safestack *bool `blueprint:"mutated"`
347 // Whether cfi sanitizer is enabled for this module
348 Cfi *bool `blueprint:"mutated"`
349 // Whether signed/unsigned integer overflow sanitizer is enabled for this module
350 Integer_overflow *bool `blueprint:"mutated"`
351 // Whether scudo sanitizer is enabled for this module
352 Scudo *bool `blueprint:"mutated"`
353 // Whether shadow-call-stack sanitizer is enabled for this module.
354 Scs *bool `blueprint:"mutated"`
355 // Whether Memory-tagging is enabled for this module
356 Memtag_heap *bool `blueprint:"mutated"`
357 // Whether Memory-tagging stack instrumentation is enabled for this module
358 Memtag_stack *bool `blueprint:"mutated"`
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200359 // Whether Memory-tagging globals instrumentation is enabled for this module
360 Memtag_globals *bool `android:"arch_variant"`
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400361
362 // Whether a modifier for ASAN and HWASAN for write only instrumentation is enabled for this
363 // module
364 Writeonly *bool `blueprint:"mutated"`
365
366 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
367 Diag struct {
368 // Whether Undefined behavior sanitizer, diagnostic mode is enabled for this module
369 Undefined *bool `blueprint:"mutated"`
370 // Whether cfi sanitizer, diagnostic mode is enabled for this module
371 Cfi *bool `blueprint:"mutated"`
372 // Whether signed/unsigned integer overflow sanitizer, diagnostic mode is enabled for this
373 // module
374 Integer_overflow *bool `blueprint:"mutated"`
375 // Whether Memory-tagging, diagnostic mode is enabled for this module
376 Memtag_heap *bool `blueprint:"mutated"`
377 // List of specific undefined behavior sanitizers enabled in diagnostic mode
378 Misc_undefined []string `blueprint:"mutated"`
379 } `blueprint:"mutated"`
380}
381
Martin Stjernholmb0249572020-09-15 02:32:35 +0100382type SanitizeProperties struct {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400383 Sanitize SanitizeUserProps `android:"arch_variant"`
384 SanitizeMutated sanitizeMutatedProperties `blueprint:"mutated"`
385
386 SanitizerEnabled bool `blueprint:"mutated"`
387 MinimalRuntimeDep bool `blueprint:"mutated"`
388 BuiltinsDep bool `blueprint:"mutated"`
389 UbsanRuntimeDep bool `blueprint:"mutated"`
390 InSanitizerDir bool `blueprint:"mutated"`
391 Sanitizers []string `blueprint:"mutated"`
392 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800393}
394
395type sanitize struct {
396 Properties SanitizeProperties
397}
398
Cindy Zhou18417cb2020-12-10 07:12:38 -0800399// Mark this tag with a check to see if apex dependency check should be skipped
400func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
401 return t.skipApexAllowedDependenciesCheck
402}
403
404var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
405
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000406var exportedVars = android.NewExportedVariables(pctx)
407
Vishwath Mohane7128792017-11-17 11:08:10 -0800408func init() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000409 exportedVars.ExportStringListStaticVariable("HostOnlySanitizeFlags", hostOnlySanitizeFlags)
410 exportedVars.ExportStringList("DeviceOnlySanitizeFlags", deviceOnlySanitizeFlags)
411
Trevor Radcliffe3876c5a2023-08-02 15:41:50 +0000412 exportedVars.ExportStringList("MinimalRuntimeFlags", minimalRuntimeFlags)
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
Trevor Radcliffeda64d912023-08-02 20:24:29 +0000429 exportedVars.ExportString("NoSanitizeLinkRuntimeFlag", noSanitizeLinkRuntimeFlag)
430
Vishwath Mohane7128792017-11-17 11:08:10 -0800431 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700432 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800433}
434
Colin Cross16b23492016-01-06 14:41:07 -0800435func (sanitize *sanitize) props() []interface{} {
436 return []interface{}{&sanitize.Properties}
437}
438
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400439func (p *sanitizeMutatedProperties) copyUserPropertiesToMutated(userProps *SanitizeUserProps) {
440 p.Never = userProps.Never
441 p.Address = userProps.Address
442 p.All_undefined = userProps.All_undefined
443 p.Cfi = userProps.Cfi
444 p.Fuzzer = userProps.Fuzzer
445 p.Hwaddress = userProps.Hwaddress
446 p.Integer_overflow = userProps.Integer_overflow
447 p.Memtag_heap = userProps.Memtag_heap
448 p.Memtag_stack = userProps.Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200449 p.Memtag_globals = userProps.Memtag_globals
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400450 p.Safestack = userProps.Safestack
451 p.Scs = userProps.Scs
452 p.Scudo = userProps.Scudo
453 p.Thread = userProps.Thread
454 p.Undefined = userProps.Undefined
455 p.Writeonly = userProps.Writeonly
456
457 p.Misc_undefined = make([]string, 0, len(userProps.Misc_undefined))
458 for _, v := range userProps.Misc_undefined {
459 p.Misc_undefined = append(p.Misc_undefined, v)
460 }
461
462 p.Diag.Cfi = userProps.Diag.Cfi
463 p.Diag.Integer_overflow = userProps.Diag.Integer_overflow
464 p.Diag.Memtag_heap = userProps.Diag.Memtag_heap
465 p.Diag.Undefined = userProps.Diag.Undefined
466
467 p.Diag.Misc_undefined = make([]string, 0, len(userProps.Diag.Misc_undefined))
468 for _, v := range userProps.Diag.Misc_undefined {
469 p.Diag.Misc_undefined = append(p.Diag.Misc_undefined, v)
470 }
471}
472
Colin Cross16b23492016-01-06 14:41:07 -0800473func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400474 s := &sanitize.Properties.SanitizeMutated
475 s.copyUserPropertiesToMutated(&sanitize.Properties.Sanitize)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700476
Colin Cross16b23492016-01-06 14:41:07 -0800477 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700478 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800479 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800480 }
481
482 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800483 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800484 return
485 }
486
Florian Mayerd8434a42022-08-31 20:57:03 +0000487 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400488 if ctx.testBinary() {
489 if s.Memtag_heap == nil {
490 s.Memtag_heap = proptools.BoolPtr(true)
491 }
492 if s.Diag.Memtag_heap == nil {
493 s.Diag.Memtag_heap = proptools.BoolPtr(true)
494 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800495 }
496
Colin Cross16b23492016-01-06 14:41:07 -0800497 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700498 var globalSanitizersDiag []string
499
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700500 if ctx.Host() {
501 if !ctx.Windows() {
502 globalSanitizers = ctx.Config().SanitizeHost()
503 }
504 } else {
505 arches := ctx.Config().SanitizeDeviceArch()
506 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
507 globalSanitizers = ctx.Config().SanitizeDevice()
508 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800509 }
510 }
511
Colin Cross16b23492016-01-06 14:41:07 -0800512 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000513 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700514 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400515 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000516 }
Colin Cross16b23492016-01-06 14:41:07 -0800517
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700518 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400519 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000520 }
521
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700522 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400523 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000524 }
525
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700526 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400527 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000528 }
529
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700530 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400531 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700532 }
533
534 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400535 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000536 }
537
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700538 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800539 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400540 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700541 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700542 }
543
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700544 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700545 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700546 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400547 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700548 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700549 }
550
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700551 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400552 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700553 }
554
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700555 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Tomislav Novakf734f002023-08-22 10:51:44 -0700556 if !ctx.Config().HWASanDisabledForPath(ctx.ModuleDir()) {
557 s.Hwaddress = proptools.BoolPtr(true)
558 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700559 }
560
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000561 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
562 // Hwaddress and Address are set before, so we can check them here
563 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
564 if s.Address == nil && s.Hwaddress == nil {
565 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
566 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400567 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000568 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700569 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800570 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400571 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800572 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700573 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000574
Florian Mayerd8434a42022-08-31 20:57:03 +0000575 if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
576 s.Memtag_stack = proptools.BoolPtr(true)
577 }
578
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200579 if found, globalSanitizers = removeFromList("memtag_globals", globalSanitizers); found && s.Memtag_globals == nil {
580 s.Memtag_globals = proptools.BoolPtr(true)
581 }
582
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000583 if len(globalSanitizers) > 0 {
584 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
585 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700586
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700587 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700588 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700589 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400590 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700591 }
592
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700593 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
594 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400595 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700596 }
597
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800598 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
599 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400600 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800601 }
602
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700603 if len(globalSanitizersDiag) > 0 {
604 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
605 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700606 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700607
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800608 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross88a029f2022-06-23 14:51:20 -0700609 if ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800610 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800611 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400612 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800613 }
614 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400615 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800616 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800617 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800618 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400619 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800620 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800621 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700622 }
623
Hang Lua98aab92023-03-17 13:17:22 +0800624 // Enable HWASan for all components in the include paths (for Aarch64 only)
625 if s.Hwaddress == nil && ctx.Config().HWASanEnabledForPath(ctx.ModuleDir()) &&
626 ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
627 s.Hwaddress = proptools.BoolPtr(true)
628 }
629
Elvis Chien9c993542021-06-25 01:15:17 +0800630 // Enable CFI for non-host components in the include paths
631 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400632 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000633 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400634 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700635 }
636 }
637
Elliott Hughesda3a0712020-03-06 16:55:28 -0800638 // Is CFI actually enabled?
639 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400640 s.Cfi = nil
641 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800642 }
643
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700644 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross88a029f2022-06-23 14:51:20 -0700645 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700646 s.Hwaddress = nil
647 }
648
Elliott Hughese4793bc2023-02-09 21:15:47 +0000649 // SCS is only implemented on AArch64/riscv64.
650 if (ctx.Arch().ArchType != android.Arm64 && ctx.Arch().ArchType != android.Riscv64) || !ctx.toolchain().Bionic() {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800651 s.Scs = nil
652 }
653
Ivan Lozano62cd0382021-11-01 10:27:54 -0400654 // Memtag_heap is only implemented on AArch64.
Florian Mayerd8434a42022-08-31 20:57:03 +0000655 // Memtag ABI is Android specific for now, so disable for host.
656 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() || ctx.Host() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700657 s.Memtag_heap = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000658 s.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200659 s.Memtag_globals = nil
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700660 }
661
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700662 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700663 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400664 s.Cfi = nil
665 s.Diag.Cfi = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000666 // HWASAN and ASAN win against MTE.
667 s.Memtag_heap = nil
668 s.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200669 s.Memtag_globals = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700670 }
671
Colin Crossed12a042022-02-07 13:55:55 -0800672 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
673 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400674 s.Cfi = nil
675 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700676 s.Misc_undefined = nil
677 s.Undefined = nil
678 s.All_undefined = nil
679 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800680 }
681
Aditya Kumar1281b992023-05-16 01:15:24 +0000682 // TODO(b/254713216): CFI doesn't work for riscv64 yet because LTO doesn't work.
683 if ctx.Arch().ArchType == android.Riscv64 {
684 s.Cfi = nil
685 s.Diag.Cfi = nil
686 }
687
Colin Crossed12a042022-02-07 13:55:55 -0800688 // Disable CFI for musl
689 if ctx.toolchain().Musl() {
690 s.Cfi = nil
691 s.Diag.Cfi = nil
692 }
693
Colin Cross390fc742023-05-02 13:02:51 -0700694 // TODO(b/280478629): runtimes don't exist for musl arm64 yet.
695 if ctx.toolchain().Musl() && ctx.Arch().ArchType == android.Arm64 {
696 s.Address = nil
697 s.Hwaddress = nil
698 s.Thread = nil
699 s.Scudo = nil
700 s.Fuzzer = nil
701 s.Cfi = nil
702 s.Diag.Cfi = nil
703 s.Misc_undefined = nil
704 s.Undefined = nil
705 s.All_undefined = nil
706 s.Integer_overflow = nil
707 }
708
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700709 // Also disable CFI for VNDK variants of components
710 if ctx.isVndk() && ctx.useVndk() {
Justin Yun08270c62022-12-19 17:01:26 +0900711 s.Cfi = nil
712 s.Diag.Cfi = nil
Inseob Kimeec88e12020-01-22 11:11:29 +0900713 }
714
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700715 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700716 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
717 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700718 s.Hwaddress = nil
719 }
720
Colin Cross3c344ef2016-07-18 15:44:56 -0700721 if ctx.staticBinary() {
722 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700723 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700724 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800725 }
726
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700727 if Bool(s.All_undefined) {
728 s.Undefined = nil
729 }
730
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700731 if !ctx.toolchain().Is64Bit() {
732 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700733 s.Thread = nil
734 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800735 // TODO(ccross): error for compile_multilib = "32"?
736 }
737
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800738 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 -0700739 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 +0200740 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack) ||
741 Bool(s.Memtag_globals)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700742 sanitize.Properties.SanitizerEnabled = true
743 }
744
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800745 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
746 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700747 s.Scudo = nil
748 }
749
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700750 if Bool(s.Hwaddress) {
751 s.Address = nil
752 s.Thread = nil
753 }
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000754
755 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
756 // mutually incompatible.
757 if Bool(s.Fuzzer) {
758 s.Cfi = nil
759 }
Colin Cross16b23492016-01-06 14:41:07 -0800760}
761
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800762func toDisableImplicitIntegerChange(flags []string) bool {
763 // Returns true if any flag is fsanitize*integer, and there is
764 // no explicit flag about sanitize=implicit-integer-sign-change.
765 for _, f := range flags {
766 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
767 return false
768 }
769 }
770 for _, f := range flags {
771 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
772 return true
773 }
774 }
775 return false
776}
777
Yabin Cuidb7dda82020-11-30 15:47:45 -0800778func toDisableUnsignedShiftBaseChange(flags []string) bool {
779 // Returns true if any flag is fsanitize*integer, and there is
780 // no explicit flag about sanitize=unsigned-shift-base.
781 for _, f := range flags {
782 if strings.Contains(f, "sanitize=unsigned-shift-base") {
783 return false
784 }
785 }
786 for _, f := range flags {
787 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
788 return true
789 }
790 }
791 return false
792}
793
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400794func (s *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
795 if !s.Properties.SanitizerEnabled && !s.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800796 return flags
797 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400798 sanProps := &s.Properties.SanitizeMutated
Colin Cross16b23492016-01-06 14:41:07 -0800799
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400800 if Bool(sanProps.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700801 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800802 // Frame pointer based unwinder in ASan requires ARM frame setup.
803 // TODO: put in flags?
804 flags.RequiredInstructionSet = "arm"
805 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800806 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
807 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800808
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400809 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000810 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
811 }
812
Colin Cross16b23492016-01-06 14:41:07 -0800813 if ctx.Host() {
814 // -nodefaultlibs (provided with libc++) prevents the driver from linking
815 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800816 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800817 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800818 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900819 if ctx.bootstrap() {
820 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
821 } else {
822 flags.DynamicLinker = "/system/bin/linker_asan"
823 }
Colin Cross16b23492016-01-06 14:41:07 -0800824 if flags.Toolchain.Is64Bit() {
825 flags.DynamicLinker += "64"
826 }
827 }
Colin Cross16b23492016-01-06 14:41:07 -0800828 }
829
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400830 if Bool(sanProps.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800831 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800832
833 for _, flag := range hwasanCommonflags {
834 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
835 }
836 for _, flag := range hwasanCommonflags {
837 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
838 }
839
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400840 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000841 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
842 }
Florian Mayer95cd6db2023-03-23 17:48:07 -0700843 if !ctx.staticBinary() && !ctx.Host() {
844 if ctx.bootstrap() {
845 flags.DynamicLinker = "/system/bin/bootstrap/linker_hwasan64"
846 } else {
847 flags.DynamicLinker = "/system/bin/linker_hwasan64"
848 }
849 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700850 }
851
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400852 if Bool(sanProps.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800853 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700854
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000855 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
856 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
857 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
858 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
859 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
860
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700861 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
862 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
863 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800864 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
865 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700866
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700867 // Disable fortify for fuzzing builds. Generally, we'll be building with
868 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800869 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800870
871 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
872 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
873 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
874 // the DT_RUNPATH from the shared library above it, and not the executable,
875 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
876 // DT_RUNPATH here means that transient shared libraries can be found
877 // colocated with their parents.
878 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800879 }
880
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400881 if Bool(sanProps.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800882 if ctx.Arch().ArchType == android.Arm {
883 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
884 // to do this on a function basis, so force Thumb on the entire module.
885 flags.RequiredInstructionSet = "thumb"
886 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000887
Colin Cross4af21ed2019-11-04 09:37:55 -0800888 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
889 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400890 if Bool(s.Properties.Sanitize.Config.Cfi_assembly_support) {
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000891 flags.Local.CFlags = append(flags.Local.CFlags, cfiAssemblySupportFlag)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800892 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000893 // Only append the default visibility flag if -fvisibility has not already been set
894 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800895 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
896 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000897 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800898 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000899
900 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800901 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
902 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000903 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700904 }
905
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400906 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000907 flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
908 flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
909 flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
910 }
911
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200912 if (Bool(sanProps.Memtag_heap) || Bool(sanProps.Memtag_stack) || Bool(sanProps.Memtag_globals)) && ctx.binary() {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400913 if Bool(sanProps.Diag.Memtag_heap) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000914 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
915 } else {
916 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
917 }
918 }
919
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400920 if Bool(sanProps.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800921 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700922 }
923
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400924 if len(s.Properties.Sanitizers) > 0 {
925 sanitizeArg := "-fsanitize=" + strings.Join(s.Properties.Sanitizers, ",")
Colin Cross4af21ed2019-11-04 09:37:55 -0800926 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
927 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross234b01d2022-02-07 13:49:03 -0800928 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
929
Colin Crossed12a042022-02-07 13:55:55 -0800930 if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
931 // Bionic and musl sanitizer runtimes have already been added as dependencies so that
932 // the right variant of the runtime will be used (with the "-android" or "-musl"
933 // suffixes), so don't let clang the runtime library.
Trevor Radcliffeda64d912023-08-02 20:24:29 +0000934 flags.Local.LdFlags = append(flags.Local.LdFlags, noSanitizeLinkRuntimeFlag)
Colin Cross234b01d2022-02-07 13:49:03 -0800935 } else {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800936 // Host sanitizers only link symbols in the final executable, so
937 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800938 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
Colin Cross6c18d002022-06-02 15:11:50 -0700939 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500940
Colin Cross6c18d002022-06-02 15:11:50 -0700941 if !ctx.toolchain().Bionic() {
942 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function san.
943 // Musl toolchain prebuilts have vptr and function sanitizers, but enabling them
944 // implicitly enables RTTI which causes RTTI mismatch issues with dependencies.
945
Colin Cross234b01d2022-02-07 13:49:03 -0800946 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500947 }
948
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400949 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700950 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800951 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700952 } else if ctx.Host() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000953 flags.Local.CFlags = append(flags.Local.CFlags, hostOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700954 } else {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000955 flags.Local.CFlags = append(flags.Local.CFlags, deviceOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700956 }
Evgenii Stepanov59012812022-06-24 11:09:18 -0700957
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400958 if enableMinimalRuntime(s) {
Evgenii Stepanov59012812022-06-24 11:09:18 -0700959 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
960 }
961
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800962 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800963 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
964 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800965 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800966 // http://b/171275751, Android doesn't build with this sanitizer yet.
967 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
968 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
969 }
Colin Cross16b23492016-01-06 14:41:07 -0800970 }
971
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400972 if len(s.Properties.DiagSanitizers) > 0 {
973 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(s.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700974 }
975 // FIXME: enable RTTI if diag + (cfi or vptr)
976
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400977 if s.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800978 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400979 strings.Join(s.Properties.Sanitize.Recover, ","))
Andreas Gampe97071162017-05-08 13:15:23 -0700980 }
981
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400982 if s.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800983 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400984 strings.Join(s.Properties.Sanitize.Diag.No_recover, ","))
Ivan Lozano7929bba2018-12-12 09:36:31 -0800985 }
986
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400987 blocklist := android.OptionalPathForModuleSrc(ctx, s.Properties.Sanitize.Blocklist)
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700988 if blocklist.Valid() {
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000989 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeIgnorelistPrefix+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700990 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
991 }
992
Colin Cross16b23492016-01-06 14:41:07 -0800993 return flags
994}
995
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400996func (s *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900997 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
998 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800999 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001000 if Bool(s.Properties.SanitizeMutated.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -08001001 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +09001002 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001003 if Bool(s.Properties.SanitizeMutated.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -08001004 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +09001005 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001006 if Bool(s.Properties.SanitizeMutated.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -08001007 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +09001008 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001009 }
Colin Cross8ff9ef42017-05-08 13:44:11 -07001010}
1011
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001012func (s *sanitize) inSanitizerDir() bool {
1013 return s.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -07001014}
1015
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001016// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001017func (s *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +00001018 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001019 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001020 return s.Properties.SanitizeMutated.Address
Tri Vo6eafc362021-04-01 11:29:09 -07001021 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001022 return s.Properties.SanitizeMutated.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +00001023 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001024 return s.Properties.SanitizeMutated.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +00001025 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001026 return s.Properties.SanitizeMutated.Integer_overflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001027 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001028 return s.Properties.SanitizeMutated.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001029 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001030 return s.Properties.SanitizeMutated.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -04001031 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001032 return s.Properties.SanitizeMutated.Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +00001033 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001034 return s.Properties.SanitizeMutated.Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001035 case Memtag_globals:
1036 return s.Properties.SanitizeMutated.Memtag_globals
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001037 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001038 return s.Properties.SanitizeMutated.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +00001039 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001040 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +00001041 }
1042}
1043
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001044// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -08001045func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001046 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001047 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -08001048 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001049 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001050 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -04001051 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Florian Mayerd8434a42022-08-31 20:57:03 +00001052 !sanitize.isSanitizerEnabled(Memtag_stack) &&
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001053 !sanitize.isSanitizerEnabled(Memtag_globals) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001054 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -08001055}
1056
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001057// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001058func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001059 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001060 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001061 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001062 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001063}
1064
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001065func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -04001066 bPtr := proptools.BoolPtr(b)
1067 if !b {
1068 bPtr = nil
1069 }
Colin Cross16b23492016-01-06 14:41:07 -08001070 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001071 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001072 sanitize.Properties.SanitizeMutated.Address = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001073 // For ASAN variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001074 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001075 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Tri Vo6eafc362021-04-01 11:29:09 -07001076 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001077 sanitize.Properties.SanitizeMutated.Hwaddress = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001078 // For HWAsan variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001079 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001080 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Colin Cross16b23492016-01-06 14:41:07 -08001081 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001082 sanitize.Properties.SanitizeMutated.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -07001083 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001084 sanitize.Properties.SanitizeMutated.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001085 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001086 sanitize.Properties.SanitizeMutated.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001087 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001088 sanitize.Properties.SanitizeMutated.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -04001089 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001090 sanitize.Properties.SanitizeMutated.Memtag_heap = bPtr
Florian Mayerd8434a42022-08-31 20:57:03 +00001091 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001092 sanitize.Properties.SanitizeMutated.Memtag_stack = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001093 // We do not need to disable ASAN or HWASan here, as there is no Memtag_stack variant.
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001094 case Memtag_globals:
1095 sanitize.Properties.Sanitize.Memtag_globals = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001096 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001097 sanitize.Properties.SanitizeMutated.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -08001098 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001099 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -08001100 }
1101 if b {
1102 sanitize.Properties.SanitizerEnabled = true
1103 }
1104}
1105
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001106// Check if the sanitizer is explicitly disabled (as opposed to nil by
1107// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001108func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001109 if sanitize == nil {
1110 return false
1111 }
1112
1113 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1114 return sanitizerVal != nil && *sanitizerVal == false
1115}
1116
1117// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
1118// because enabling a sanitizer either directly (via the blueprint) or
1119// indirectly (via a mutator) sets the bool ptr to true, and you can't
1120// distinguish between the cases. It isn't needed though - both cases can be
1121// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001122func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001123 if sanitize == nil {
1124 return false
1125 }
1126
1127 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1128 return sanitizerVal != nil && *sanitizerVal == true
1129}
1130
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001131// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
1132func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -07001133 switch t := tag.(type) {
1134 case dependencyTag:
1135 return t == reuseObjTag || t == objDepTag
1136 case libraryDependencyTag:
1137 return true
1138 default:
1139 return false
1140 }
Colin Cross6b753602018-06-21 13:03:07 -07001141}
1142
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001143func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
1144 return IsSanitizableDependencyTag
1145}
1146
Inseob Kimc42f2f22020-07-29 20:32:10 +09001147// Determines if the current module is a static library going to be captured
1148// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
1149// except for ones which explicitly disable cfi.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001150func needsCfiForVendorSnapshot(mctx android.BaseModuleContext) bool {
Justin Yun8814fc52022-12-15 21:45:35 +09001151 if inList("hwaddress", mctx.Config().SanitizeDevice()) {
1152 // cfi will not be built if SANITIZE_TARGET=hwaddress is set
1153 return false
1154 }
1155
Kiyoung Kim48f37782021-07-07 12:42:39 +09001156 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001157 return false
1158 }
1159
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001160 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001161
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001162 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001163 return false
1164 }
1165
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001166 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001167 return false
1168 }
1169
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001170 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001171 return false
1172 }
1173
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001174 if !c.SanitizerSupported(cfi) {
1175 return false
1176 }
1177
1178 return c.SanitizePropDefined() &&
1179 !c.SanitizeNever() &&
1180 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001181}
1182
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001183type sanitizerSplitMutator struct {
1184 sanitizer SanitizerType
1185}
1186
1187// If an APEX is sanitized or not depends on whether it contains at least one
1188// sanitized module. Transition mutators cannot propagate information up the
1189// dependency graph this way, so we need an auxiliary mutator to do so.
1190func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
1191 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1192 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1193 ctx.VisitDirectDeps(func(dep android.Module) {
1194 if c, ok := dep.(*Module); ok && c.sanitize.isSanitizerEnabled(s.sanitizer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001195 enabled = true
Inseob Kimc42f2f22020-07-29 20:32:10 +09001196 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001197 })
1198
1199 if enabled {
1200 sanitizeable.EnableSanitizer(s.sanitizer.name())
1201 }
1202 }
1203}
1204
1205func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
1206 if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1207 if s.sanitizer == cfi && needsCfiForVendorSnapshot(ctx) {
1208 return []string{"", s.sanitizer.variationName()}
1209 }
1210
1211 // If the given sanitizer is not requested in the .bp file for a module, it
1212 // won't automatically build the sanitized variation.
1213 if !c.IsSanitizerEnabled(s.sanitizer) {
1214 return []string{""}
1215 }
1216
1217 if c.Binary() {
1218 // If a sanitizer is enabled for a binary, we do not build the version
1219 // without the sanitizer
1220 return []string{s.sanitizer.variationName()}
1221 } else if c.StaticallyLinked() || c.Header() {
1222 // For static libraries, we build both versions. Some Make modules
1223 // apparently depend on this behavior.
1224 return []string{"", s.sanitizer.variationName()}
1225 } else {
1226 // We only build the requested variation of dynamic libraries
1227 return []string{s.sanitizer.variationName()}
1228 }
1229 }
1230
1231 if _, ok := ctx.Module().(JniSanitizeable); ok {
1232 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1233 // that is short-circuited for now
1234 return []string{""}
1235 }
1236
1237 // If an APEX has a sanitized dependency, we build the APEX in the sanitized
1238 // variation. This is useful because such APEXes require extra dependencies.
1239 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1240 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1241 if enabled {
1242 return []string{s.sanitizer.variationName()}
1243 } else {
1244 return []string{""}
1245 }
1246 }
1247
1248 if c, ok := ctx.Module().(*Module); ok {
1249 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1250
1251 // Check if it's a snapshot module supporting sanitizer
Justin Yun08270c62022-12-19 17:01:26 +09001252 if ss, ok := c.linker.(snapshotSanitizer); ok {
1253 if ss.isSanitizerAvailable(s.sanitizer) {
1254 return []string{"", s.sanitizer.variationName()}
1255 } else {
1256 return []string{""}
1257 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001258 }
1259 }
1260
1261 return []string{""}
1262}
1263
1264func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1265 if c, ok := ctx.Module().(PlatformSanitizeable); ok {
1266 if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
1267 // If the dependency is through a non-sanitizable tag, use the
1268 // non-sanitized variation
1269 return ""
1270 }
1271
1272 return sourceVariation
1273 } else if _, ok := ctx.Module().(JniSanitizeable); ok {
1274 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1275 // that is short-circuited for now
1276 return ""
1277 } else {
1278 // Otherwise, do not rock the boat.
1279 return sourceVariation
1280 }
1281}
1282
1283func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1284 if d, ok := ctx.Module().(PlatformSanitizeable); ok {
1285 if dm, ok := ctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001286 if ss, ok := dm.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001287 return incomingVariation
Inseob Kimc42f2f22020-07-29 20:32:10 +09001288 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001289 }
1290
1291 if !d.SanitizePropDefined() ||
1292 d.SanitizeNever() ||
1293 d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
1294 !d.SanitizerSupported(s.sanitizer) {
1295 // If a module opts out of a sanitizer, use its non-sanitized variation
1296 return ""
1297 }
1298
1299 // Binaries are always built in the variation they requested.
1300 if d.Binary() {
1301 if d.IsSanitizerEnabled(s.sanitizer) {
1302 return s.sanitizer.variationName()
1303 } else {
1304 return ""
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001305 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001306 }
1307
1308 // If a shared library requests to be sanitized, it will be built for that
1309 // sanitizer. Otherwise, some sanitizers propagate through shared library
1310 // dependency edges, some do not.
1311 if !d.StaticallyLinked() && !d.Header() {
1312 if d.IsSanitizerEnabled(s.sanitizer) {
1313 return s.sanitizer.variationName()
1314 }
1315
Liz Kammerfd8a49f2022-10-31 10:31:11 -04001316 // Some sanitizers do not propagate to shared dependencies
1317 if !s.sanitizer.shouldPropagateToSharedLibraryDeps() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001318 return ""
1319 }
1320 }
1321
1322 // Static and header libraries inherit whether they are sanitized from the
1323 // module they are linked into
1324 return incomingVariation
1325 } else if d, ok := ctx.Module().(Sanitizeable); ok {
1326 // If an APEX contains a sanitized module, it will be built in the variation
1327 // corresponding to that sanitizer.
1328 enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1329 if enabled {
1330 return s.sanitizer.variationName()
1331 }
1332
1333 return incomingVariation
1334 }
1335
1336 return ""
1337}
1338
1339func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
1340 sanitizerVariation := variationName == s.sanitizer.variationName()
1341
1342 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1343 sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
1344
1345 oneMakeVariation := false
1346 if c.StaticallyLinked() || c.Header() {
1347 if s.sanitizer != cfi && s.sanitizer != scs && s.sanitizer != Hwasan {
1348 // These sanitizers export only one variation to Make. For the rest,
1349 // Make targets can depend on both the sanitized and non-sanitized
1350 // versions.
1351 oneMakeVariation = true
1352 }
1353 } else if !c.Binary() {
1354 // Shared library. These are the sanitizers that do propagate through shared
1355 // library dependencies and therefore can cause multiple variations of a
1356 // shared library to be built.
1357 if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != scs && s.sanitizer != Asan {
1358 oneMakeVariation = true
1359 }
1360 }
1361
1362 if oneMakeVariation {
1363 if sanitizerEnabled != sanitizerVariation {
1364 c.SetPreventInstall()
1365 c.SetHideFromMake()
1366 }
1367 }
1368
1369 if sanitizerVariation {
1370 c.SetSanitizer(s.sanitizer, true)
1371
1372 // CFI is incompatible with ASAN so disable it in ASAN variations
1373 if s.sanitizer.incompatibleWithCfi() {
1374 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1375 if mctx.Device() && cfiSupported {
1376 c.SetSanitizer(cfi, false)
Jiyong Parkf97782b2019-02-13 20:28:58 +09001377 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001378 }
1379
1380 // locate the asan libraries under /data/asan
1381 if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
1382 c.SetInSanitizerDir()
1383 }
1384
1385 if c.StaticallyLinked() && c.ExportedToMake() {
1386 if s.sanitizer == Hwasan {
1387 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
1388 } else if s.sanitizer == cfi {
1389 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
1390 }
1391 }
1392 } else if c.IsSanitizerEnabled(s.sanitizer) {
1393 // Disable the sanitizer for the non-sanitized variation
1394 c.SetSanitizer(s.sanitizer, false)
1395 }
1396 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
1397 // If an APEX has sanitized dependencies, it gets a few more dependencies
1398 if sanitizerVariation {
1399 sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
1400 }
1401 } else if c, ok := mctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001402 if ss, ok := c.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
1403 if !ss.isUnsanitizedVariant() {
1404 // Snapshot sanitizer may have only one variantion.
1405 // Skip exporting the module if it already has a sanitizer variation.
1406 c.SetPreventInstall()
1407 c.SetHideFromMake()
1408 return
1409 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001410 c.linker.(snapshotSanitizer).setSanitizerVariation(s.sanitizer, sanitizerVariation)
1411
1412 // Export the static lib name to make
1413 if c.static() && c.ExportedToMake() {
Justin Yun39c30312022-11-23 16:20:12 +09001414 // use BaseModuleName which is the name for Make.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001415 if s.sanitizer == cfi {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001416 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Justin Yun39c30312022-11-23 16:20:12 +09001417 } else if s.sanitizer == Hwasan {
1418 hwasanStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001419 }
1420 }
Colin Cross16b23492016-01-06 14:41:07 -08001421 }
1422 }
1423}
1424
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001425func (c *Module) SanitizeNever() bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001426 return Bool(c.sanitize.Properties.SanitizeMutated.Never)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001427}
1428
1429func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
1430 return c.sanitize.isSanitizerExplicitlyDisabled(t)
1431}
1432
Ivan Lozano30c5db22018-02-21 15:49:20 -08001433// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001434func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001435 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001436 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001437 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001438 mctx.WalkDeps(func(child, parent android.Module) bool {
1439 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1440 return false
1441 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001442
Inseob Kimeec88e12020-01-22 11:11:29 +09001443 d, ok := child.(*Module)
1444 if !ok || !d.static() {
1445 return false
1446 }
1447 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001448 if enableMinimalRuntime(d.sanitize) {
1449 // If a static dependency is built with the minimal runtime,
1450 // make sure we include the ubsan minimal runtime.
1451 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001452 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001453 // If a static dependency runs with full ubsan diagnostics,
1454 // make sure we include the ubsan runtime.
1455 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001456 }
Colin Cross0b908332019-06-19 23:00:20 -07001457
1458 if c.sanitize.Properties.MinimalRuntimeDep &&
1459 c.sanitize.Properties.UbsanRuntimeDep {
1460 // both flags that this mutator might set are true, so don't bother recursing
1461 return false
1462 }
1463
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001464 if c.Os() == android.Linux {
1465 c.sanitize.Properties.BuiltinsDep = true
1466 }
1467
Colin Cross0b908332019-06-19 23:00:20 -07001468 return true
Colin Cross6b753602018-06-21 13:03:07 -07001469 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001470
Jose Galmesf7294582020-11-13 12:07:36 -08001471 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001472 if Bool(p.properties.Sanitize_minimal_dep) {
1473 c.sanitize.Properties.MinimalRuntimeDep = true
1474 }
1475 if Bool(p.properties.Sanitize_ubsan_dep) {
1476 c.sanitize.Properties.UbsanRuntimeDep = true
1477 }
1478 }
1479
1480 return false
Colin Cross6b753602018-06-21 13:03:07 -07001481 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001482 }
1483}
1484
Jiyong Park379de2f2018-12-19 02:47:14 +09001485// Add the dependency to the runtime library for each of the sanitizer variants
1486func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001487 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001488 if !c.Enabled() {
1489 return
1490 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001491 var sanitizers []string
1492 var diagSanitizers []string
1493
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001494 sanProps := &c.sanitize.Properties.SanitizeMutated
1495
1496 if Bool(sanProps.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001497 sanitizers = append(sanitizers, "undefined")
1498 } else {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001499 if Bool(sanProps.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001500 sanitizers = append(sanitizers,
1501 "bool",
1502 "integer-divide-by-zero",
1503 "return",
1504 "returns-nonnull-attribute",
1505 "shift-exponent",
1506 "unreachable",
1507 "vla-bound",
1508 // TODO(danalbert): The following checks currently have compiler performance issues.
1509 //"alignment",
1510 //"bounds",
1511 //"enum",
1512 //"float-cast-overflow",
1513 //"float-divide-by-zero",
1514 //"nonnull-attribute",
1515 //"null",
1516 //"shift-base",
1517 //"signed-integer-overflow",
1518 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1519 // https://llvm.org/PR19302
1520 // http://reviews.llvm.org/D6974
1521 // "object-size",
1522 )
1523 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001524 sanitizers = append(sanitizers, sanProps.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001525 }
1526
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001527 if Bool(sanProps.Diag.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001528 diagSanitizers = append(diagSanitizers, "undefined")
1529 }
1530
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001531 diagSanitizers = append(diagSanitizers, sanProps.Diag.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001532
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001533 if Bool(sanProps.Address) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001534 sanitizers = append(sanitizers, "address")
1535 diagSanitizers = append(diagSanitizers, "address")
1536 }
1537
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001538 if Bool(sanProps.Hwaddress) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001539 sanitizers = append(sanitizers, "hwaddress")
1540 }
1541
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001542 if Bool(sanProps.Thread) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001543 sanitizers = append(sanitizers, "thread")
1544 }
1545
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001546 if Bool(sanProps.Safestack) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001547 sanitizers = append(sanitizers, "safe-stack")
1548 }
1549
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001550 if Bool(sanProps.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001551 sanitizers = append(sanitizers, "cfi")
1552
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001553 if Bool(sanProps.Diag.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001554 diagSanitizers = append(diagSanitizers, "cfi")
1555 }
1556 }
1557
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001558 if Bool(sanProps.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001559 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1560 sanitizers = append(sanitizers, "signed-integer-overflow")
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001561 if Bool(sanProps.Diag.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001562 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1563 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1564 }
1565 }
1566
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001567 if Bool(sanProps.Scudo) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001568 sanitizers = append(sanitizers, "scudo")
1569 }
1570
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001571 if Bool(sanProps.Scs) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001572 sanitizers = append(sanitizers, "shadow-call-stack")
1573 }
1574
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001575 if Bool(sanProps.Memtag_heap) && c.Binary() {
Florian Mayerd8434a42022-08-31 20:57:03 +00001576 sanitizers = append(sanitizers, "memtag-heap")
1577 }
1578
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001579 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +00001580 sanitizers = append(sanitizers, "memtag-stack")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001581 }
1582
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001583 if Bool(sanProps.Memtag_globals) {
1584 sanitizers = append(sanitizers, "memtag-globals")
1585 // TODO(mitchp): For now, enable memtag-heap with memtag-globals because the linker
1586 // isn't new enough (https://reviews.llvm.org/differential/changeset/?ref=4243566).
1587 sanitizers = append(sanitizers, "memtag-heap")
1588 }
1589
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001590 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001591 sanitizers = append(sanitizers, "fuzzer-no-link")
1592 }
1593
Jiyong Park379de2f2018-12-19 02:47:14 +09001594 // Save the list of sanitizers. These will be used again when generating
1595 // the build rules (for Cflags, etc.)
1596 c.sanitize.Properties.Sanitizers = sanitizers
1597 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1598
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001599 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1600 if c.Host() {
1601 diagSanitizers = sanitizers
1602 }
1603
Colin Crosse323a792023-02-15 13:57:57 -08001604 addStaticDeps := func(dep string, hideSymbols bool) {
Colin Cross06c80eb2022-02-10 10:34:19 -08001605 // If we're using snapshots, redirect to snapshot whenever possible
1606 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001607 if lib, ok := snapshot.StaticLibs[dep]; ok {
1608 dep = lib
Colin Cross06c80eb2022-02-10 10:34:19 -08001609 }
1610
1611 // static executable gets static runtime libs
Colin Crosse323a792023-02-15 13:57:57 -08001612 depTag := libraryDependencyTag{Kind: staticLibraryDependency, unexportedSymbols: hideSymbols}
Colin Cross06c80eb2022-02-10 10:34:19 -08001613 variations := append(mctx.Target().Variations(),
1614 blueprint.Variation{Mutator: "link", Variation: "static"})
1615 if c.Device() {
1616 variations = append(variations, c.ImageVariation())
1617 }
1618 if c.UseSdk() {
1619 variations = append(variations,
1620 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1621 }
Colin Crosse323a792023-02-15 13:57:57 -08001622 mctx.AddFarVariationDependencies(variations, depTag, dep)
Colin Cross06c80eb2022-02-10 10:34:19 -08001623 }
Colin Crosse323a792023-02-15 13:57:57 -08001624
1625 // Determine the runtime library required
1626 runtimeSharedLibrary := ""
1627 toolchain := c.toolchain(mctx)
1628 if Bool(sanProps.Address) {
Colin Crossb781d232023-02-15 12:40:20 -08001629 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1630 // Use a static runtime for musl to match what clang does for glibc.
1631 addStaticDeps(config.AddressSanitizerStaticRuntimeLibrary(toolchain), false)
1632 addStaticDeps(config.AddressSanitizerCXXStaticRuntimeLibrary(toolchain), false)
1633 } else {
1634 runtimeSharedLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1635 }
Colin Crosse323a792023-02-15 13:57:57 -08001636 } else if Bool(sanProps.Hwaddress) {
1637 if c.staticBinary() {
1638 addStaticDeps(config.HWAddressSanitizerStaticLibrary(toolchain), true)
1639 addStaticDeps("libdl", false)
1640 } else {
1641 runtimeSharedLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1642 }
1643 } else if Bool(sanProps.Thread) {
1644 runtimeSharedLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1645 } else if Bool(sanProps.Scudo) {
1646 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1647 runtimeSharedLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1648 } else {
1649 runtimeSharedLibrary = config.ScudoRuntimeLibrary(toolchain)
1650 }
1651 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
1652 Bool(sanProps.Fuzzer) ||
1653 Bool(sanProps.Undefined) ||
1654 Bool(sanProps.All_undefined) {
1655 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1656 // Use a static runtime for static binaries.
1657 // Also use a static runtime for musl to match
1658 // what clang does for glibc. Otherwise dlopening
1659 // libraries that depend on libclang_rt.ubsan_standalone.so
1660 // fails with:
1661 // Error relocating ...: initial-exec TLS resolves to dynamic definition
1662 addStaticDeps(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)+".static", true)
1663 } else {
1664 runtimeSharedLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
1665 }
1666 }
1667
Colin Cross06c80eb2022-02-10 10:34:19 -08001668 if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
Colin Crosse323a792023-02-15 13:57:57 -08001669 addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001670 }
1671 if c.sanitize.Properties.BuiltinsDep {
Colin Crosse323a792023-02-15 13:57:57 -08001672 addStaticDeps(config.BuiltinsRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001673 }
1674
Colin Crosse323a792023-02-15 13:57:57 -08001675 if runtimeSharedLibrary != "" && (toolchain.Bionic() || toolchain.Musl() || c.sanitize.Properties.UbsanRuntimeDep) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001676 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001677
1678 // Adding dependency to the runtime library. We are using *FarVariation*
1679 // because the runtime libraries themselves are not mutated by sanitizer
1680 // mutators and thus don't have sanitizer variants whereas this module
1681 // has been already mutated.
1682 //
1683 // Note that by adding dependency with {static|shared}DepTag, the lib is
1684 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
Colin Crosse323a792023-02-15 13:57:57 -08001685 if c.staticBinary() {
1686 // Most sanitizers are either disabled for static binaries or have already
1687 // handled the static binary case above through a direct call to addStaticDeps.
1688 // If not, treat the runtime shared library as a static library and hope for
1689 // the best.
1690 addStaticDeps(runtimeSharedLibrary, true)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001691 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001692 // If we're using snapshots, redirect to snapshot whenever possible
1693 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001694 if lib, ok := snapshot.SharedLibs[runtimeSharedLibrary]; ok {
1695 runtimeSharedLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001696 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001697
Cindy Zhou18417cb2020-12-10 07:12:38 -08001698 // Skip apex dependency check for sharedLibraryDependency
1699 // when sanitizer diags are enabled. Skipping the check will allow
1700 // building with diag libraries without having to list the
1701 // dependency in Apex's allowed_deps file.
1702 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001703 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001704 depTag := libraryDependencyTag{
1705 Kind: sharedLibraryDependency,
1706 Order: earlyLibraryDependency,
1707
1708 skipApexAllowedDependenciesCheck: diagEnabled,
1709 }
Colin Cross42507332020-08-21 16:15:23 -07001710 variations := append(mctx.Target().Variations(),
1711 blueprint.Variation{Mutator: "link", Variation: "shared"})
1712 if c.Device() {
1713 variations = append(variations, c.ImageVariation())
1714 }
Colin Cross06c80eb2022-02-10 10:34:19 -08001715 if c.UseSdk() {
1716 variations = append(variations,
1717 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1718 }
Colin Crosse323a792023-02-15 13:57:57 -08001719 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeSharedLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001720 }
1721 // static lib does not have dependency to the runtime library. The
1722 // dependency will be added to the executables or shared libs using
1723 // the static lib.
1724 }
1725 }
1726}
1727
1728type Sanitizeable interface {
1729 android.Module
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001730 IsSanitizerEnabled(config android.Config, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001731 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001732 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001733}
1734
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001735type JniSanitizeable interface {
1736 android.Module
1737 IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
1738}
1739
Ivan Lozanod7586b62021-04-01 09:49:36 -04001740func (c *Module) MinimalRuntimeDep() bool {
1741 return c.sanitize.Properties.MinimalRuntimeDep
1742}
1743
1744func (c *Module) UbsanRuntimeDep() bool {
1745 return c.sanitize.Properties.UbsanRuntimeDep
1746}
1747
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001748func (c *Module) SanitizePropDefined() bool {
1749 return c.sanitize != nil
1750}
1751
1752func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1753 return c.sanitize.isSanitizerEnabled(t)
1754}
1755
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001756func (c *Module) StaticallyLinked() bool {
1757 return c.static()
1758}
1759
1760func (c *Module) SetInSanitizerDir() {
1761 if c.sanitize != nil {
1762 c.sanitize.Properties.InSanitizerDir = true
1763 }
1764}
1765
1766func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1767 if c.sanitize != nil {
1768 c.sanitize.SetSanitizer(t, b)
1769 }
1770}
1771
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001772var _ PlatformSanitizeable = (*Module)(nil)
1773
Inseob Kim74d25562020-08-04 00:41:38 +09001774type sanitizerStaticLibsMap struct {
1775 // libsMap contains one list of modules per each image and each arch.
1776 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001777 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001778 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001779 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001780}
1781
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001782func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001783 return &sanitizerStaticLibsMap{
1784 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001785 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001786 }
1787}
1788
1789// Add the current module to sanitizer static libs maps
1790// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001791func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1792 image := GetImageVariantType(c)
1793 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001794
1795 s.libsMapLock.Lock()
1796 defer s.libsMapLock.Unlock()
1797
1798 if _, ok := s.libsMap[image]; !ok {
1799 s.libsMap[image] = make(map[string][]string)
1800 }
1801
1802 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1803}
1804
1805// Exports makefile variables in the following format:
1806// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1807// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1808// These are to be used by use_soong_sanitized_static_libraries.
1809// See build/make/core/binary.mk for more details.
1810func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
Cole Faust18994c72023-02-28 16:02:16 -08001811 for _, image := range android.SortedKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001812 archMap := s.libsMap[ImageVariantType(image)]
Cole Faust18994c72023-02-28 16:02:16 -08001813 for _, arch := range android.SortedKeys(archMap) {
Inseob Kim74d25562020-08-04 00:41:38 +09001814 libs := archMap[arch]
1815 sort.Strings(libs)
1816
1817 key := fmt.Sprintf(
1818 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1819 s.sanitizerType.variationName(),
1820 image, // already upper
1821 arch)
1822
1823 ctx.Strict(key, strings.Join(libs, " "))
1824 }
1825 }
1826}
1827
Colin Cross571cccf2019-02-04 11:22:08 -08001828var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1829
Inseob Kim74d25562020-08-04 00:41:38 +09001830func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001831 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001832 return newSanitizerStaticLibsMap(cfi)
1833 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001834}
1835
Colin Cross571cccf2019-02-04 11:22:08 -08001836var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1837
Inseob Kim74d25562020-08-04 00:41:38 +09001838func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001839 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001840 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001841 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001842}
1843
Ivan Lozano30c5db22018-02-21 15:49:20 -08001844func enableMinimalRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001845 if sanitize.isSanitizerEnabled(Asan) {
1846 return false
1847 } else if sanitize.isSanitizerEnabled(Hwasan) {
1848 return false
1849 } else if sanitize.isSanitizerEnabled(Fuzzer) {
1850 return false
Ivan Lozano30c5db22018-02-21 15:49:20 -08001851 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001852
1853 if enableUbsanRuntime(sanitize) {
1854 return false
1855 }
1856
1857 sanitizeProps := &sanitize.Properties.SanitizeMutated
1858 if Bool(sanitizeProps.Diag.Cfi) {
1859 return false
1860 }
1861
1862 return Bool(sanitizeProps.Integer_overflow) ||
1863 len(sanitizeProps.Misc_undefined) > 0 ||
1864 Bool(sanitizeProps.Undefined) ||
1865 Bool(sanitizeProps.All_undefined)
Ivan Lozano30c5db22018-02-21 15:49:20 -08001866}
1867
Ivan Lozanod7586b62021-04-01 09:49:36 -04001868func (m *Module) UbsanRuntimeNeeded() bool {
1869 return enableUbsanRuntime(m.sanitize)
1870}
1871
1872func (m *Module) MinimalRuntimeNeeded() bool {
1873 return enableMinimalRuntime(m.sanitize)
1874}
1875
Inseob Kim8471cda2019-11-15 09:59:12 +09001876func enableUbsanRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001877 sanitizeProps := &sanitize.Properties.SanitizeMutated
1878 return Bool(sanitizeProps.Diag.Integer_overflow) ||
1879 Bool(sanitizeProps.Diag.Undefined) ||
1880 len(sanitizeProps.Diag.Misc_undefined) > 0
Inseob Kim8471cda2019-11-15 09:59:12 +09001881}
1882
Vishwath Mohane7128792017-11-17 11:08:10 -08001883func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001884 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001885}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001886
1887func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001888 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001889}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +00001890
1891func BazelCcSanitizerToolchainVars(config android.Config) string {
1892 return android.BazelToolchainVars(config, exportedVars)
1893}