blob: a23e2db897e9061af04ae47985859455d4abe58c [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"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070090)
91
Ivan Lozano3968d8f2020-12-14 11:27:52 -050092type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080093
Colin Cross16b23492016-01-06 14:41:07 -080094const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050095 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070096 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080097 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070098 intOverflow
Ivan Lozano88271132023-07-26 13:34:55 -040099 Scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500100 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -0400101 Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +0000102 Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200103 Memtag_globals
Liz Kammer75db9312021-07-07 16:41:50 -0400104 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -0800105)
106
Liz Kammer75db9312021-07-07 16:41:50 -0400107var Sanitizers = []SanitizerType{
108 Asan,
109 Hwasan,
110 tsan,
111 intOverflow,
Ivan Lozano88271132023-07-26 13:34:55 -0400112 Scs,
Liz Kammer75db9312021-07-07 16:41:50 -0400113 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400114 Memtag_heap,
Florian Mayerd8434a42022-08-31 20:57:03 +0000115 Memtag_stack,
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200116 Memtag_globals,
Liz Kammer75db9312021-07-07 16:41:50 -0400117 cfi, // cfi is last to prevent it running before incompatible mutators
118}
119
Jiyong Park82226632019-02-01 10:50:50 +0900120// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500121func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800122 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500123 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800124 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700125 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700126 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800127 case tsan:
128 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700129 case intOverflow:
130 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000131 case cfi:
132 return "cfi"
Ivan Lozano88271132023-07-26 13:34:55 -0400133 case Scs:
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800134 return "scs"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400135 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700136 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000137 case Memtag_stack:
138 return "memtag_stack"
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200139 case Memtag_globals:
140 return "memtag_globals"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500141 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700142 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800143 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500144 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800145 }
146}
147
Jiyong Park82226632019-02-01 10:50:50 +0900148// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500149func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900150 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500151 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900152 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700153 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900154 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400155 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700156 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000157 case Memtag_stack:
158 return "memtag_stack"
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200159 case Memtag_globals:
160 return "memtag_globals"
Jiyong Park82226632019-02-01 10:50:50 +0900161 case tsan:
162 return "thread"
163 case intOverflow:
164 return "integer_overflow"
165 case cfi:
166 return "cfi"
Ivan Lozano88271132023-07-26 13:34:55 -0400167 case Scs:
Jiyong Park82226632019-02-01 10:50:50 +0900168 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500169 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700170 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900171 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500172 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900173 }
174}
175
Liz Kammer75db9312021-07-07 16:41:50 -0400176func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
177 switch t {
Ivan Lozano88271132023-07-26 13:34:55 -0400178 case cfi, Hwasan, Asan, tsan, Fuzzer, Scs:
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200179 sanitizer := &sanitizerSplitMutator{t}
180 ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
181 ctx.Transition(t.variationName(), sanitizer)
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200182 case Memtag_heap, Memtag_stack, Memtag_globals, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400183 // do nothing
184 default:
185 panic(fmt.Errorf("unknown SanitizerType %d", t))
186 }
187}
188
Liz Kammerfd8a49f2022-10-31 10:31:11 -0400189// shouldPropagateToSharedLibraryDeps returns whether a sanitizer type should propagate to share
190// dependencies. In most cases, sanitizers only propagate to static dependencies; however, some
191// sanitizers also must be enabled for shared libraries for linking.
192func (t SanitizerType) shouldPropagateToSharedLibraryDeps() bool {
193 switch t {
194 case Fuzzer:
195 // Typically, shared libs are not split. However, for fuzzer, we split even for shared libs
196 // because a library sanitized for fuzzer can't be linked from a library that isn't sanitized
197 // for fuzzer.
198 return true
199 default:
200 return false
201 }
202}
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500203func (*Module) SanitizerSupported(t SanitizerType) bool {
204 switch t {
205 case Asan:
206 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700207 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500208 return true
209 case tsan:
210 return true
211 case intOverflow:
212 return true
213 case cfi:
214 return true
Ivan Lozano88271132023-07-26 13:34:55 -0400215 case Scs:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500216 return true
217 case Fuzzer:
218 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400219 case Memtag_heap:
220 return true
Florian Mayerd8434a42022-08-31 20:57:03 +0000221 case Memtag_stack:
222 return true
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200223 case Memtag_globals:
224 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500225 default:
226 return false
227 }
228}
229
230// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
231func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700232 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900233}
234
Martin Stjernholmb0249572020-09-15 02:32:35 +0100235type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400236 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100237 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800238
Liz Kammer75b9b402021-06-25 15:19:27 -0400239 // ASan (Address sanitizer), incompatible with static binaries.
240 // Always runs in a diagnostic mode.
241 // Use of address sanitizer disables cfi sanitizer.
242 // Hwaddress sanitizer takes precedence over this sanitizer.
243 Address *bool `android:"arch_variant"`
244 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
245 // Always runs in a diagnostic mode.
246 // Use of thread sanitizer disables cfi and scudo sanitizers.
247 // Hwaddress sanitizer takes precedence over this sanitizer.
248 Thread *bool `android:"arch_variant"`
249 // HWASan (Hardware Address sanitizer).
250 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100251 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800252
Liz Kammer75b9b402021-06-25 15:19:27 -0400253 // Undefined behavior sanitizer
254 All_undefined *bool `android:"arch_variant"`
255 // Subset of undefined behavior sanitizer
256 Undefined *bool `android:"arch_variant"`
257 // List of specific undefined behavior sanitizers to enable
258 Misc_undefined []string `android:"arch_variant"`
259 // Fuzzer, incompatible with static binaries.
260 Fuzzer *bool `android:"arch_variant"`
261 // safe-stack sanitizer, incompatible with 32-bit architectures.
262 Safestack *bool `android:"arch_variant"`
263 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
264 Cfi *bool `android:"arch_variant"`
265 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
266 Integer_overflow *bool `android:"arch_variant"`
267 // scudo sanitizer, incompatible with asan, hwasan, tsan
268 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
269 // deprecated
270 Scudo *bool `android:"arch_variant"`
Elliott Hughese4793bc2023-02-09 21:15:47 +0000271 // shadow-call-stack sanitizer, only available on arm64/riscv64.
Liz Kammer75b9b402021-06-25 15:19:27 -0400272 Scs *bool `android:"arch_variant"`
273 // Memory-tagging, only available on arm64
274 // if diag.memtag unset or false, enables async memory tagging
Florian Mayer00ab5cf2022-08-31 18:30:18 +0000275 Memtag_heap *bool `android:"arch_variant"`
Florian Mayerd8434a42022-08-31 20:57:03 +0000276 // Memory-tagging stack instrumentation, only available on arm64
277 // Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
278 Memtag_stack *bool `android:"arch_variant"`
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200279 // Memory-tagging globals instrumentation, only available on arm64
280 // Adds instrumentation to detect global buffer overflows using MTE.
281 Memtag_globals *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100282
283 // A modifier for ASAN and HWASAN for write only instrumentation
284 Writeonly *bool `android:"arch_variant"`
285
286 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
287 // Replaces abort() on error with a human-readable error message.
288 // Address and Thread sanitizers always run in diagnostic mode.
289 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400290 // Undefined behavior sanitizer, diagnostic mode
291 Undefined *bool `android:"arch_variant"`
292 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
293 Cfi *bool `android:"arch_variant"`
294 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
295 Integer_overflow *bool `android:"arch_variant"`
296 // Memory-tagging, only available on arm64
297 // requires sanitizer.memtag: true
298 // if set, enables sync memory tagging
299 Memtag_heap *bool `android:"arch_variant"`
300 // List of specific undefined behavior sanitizers to enable in diagnostic mode
301 Misc_undefined []string `android:"arch_variant"`
302 // List of sanitizers to pass to -fno-sanitize-recover
303 // results in only the first detected error for these sanitizers being reported and program then
304 // exits with a non-zero exit code.
305 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800306 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800307
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800308 // Sanitizers to run with flag configuration specified
309 Config struct {
310 // Enables CFI support flags for assembly-heavy libraries
311 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800312 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800313
Liz Kammer75b9b402021-06-25 15:19:27 -0400314 // List of sanitizers to pass to -fsanitize-recover
315 // allows execution to continue for these sanitizers to detect multiple errors rather than only
316 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100317 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000318
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700319 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100320 Blocklist *string
321}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700322
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400323type sanitizeMutatedProperties struct {
324 // Whether sanitizers can be enabled on this module
325 Never *bool `blueprint:"mutated"`
326
327 // Whether ASan (Address sanitizer) is enabled for this module.
328 // Hwaddress sanitizer takes precedence over this sanitizer.
329 Address *bool `blueprint:"mutated"`
330 // Whether TSan (Thread sanitizer) is enabled for this module
331 Thread *bool `blueprint:"mutated"`
332 // Whether HWASan (Hardware Address sanitizer) is enabled for this module
333 Hwaddress *bool `blueprint:"mutated"`
334
335 // Whether Undefined behavior sanitizer is enabled for this module
336 All_undefined *bool `blueprint:"mutated"`
337 // Whether undefined behavior sanitizer subset is enabled for this module
338 Undefined *bool `blueprint:"mutated"`
339 // List of specific undefined behavior sanitizers enabled for this module
340 Misc_undefined []string `blueprint:"mutated"`
341 // Whether Fuzzeris enabled for this module
342 Fuzzer *bool `blueprint:"mutated"`
343 // whether safe-stack sanitizer is enabled for this module
344 Safestack *bool `blueprint:"mutated"`
345 // Whether cfi sanitizer is enabled for this module
346 Cfi *bool `blueprint:"mutated"`
347 // Whether signed/unsigned integer overflow sanitizer is enabled for this module
348 Integer_overflow *bool `blueprint:"mutated"`
349 // Whether scudo sanitizer is enabled for this module
350 Scudo *bool `blueprint:"mutated"`
351 // Whether shadow-call-stack sanitizer is enabled for this module.
352 Scs *bool `blueprint:"mutated"`
353 // Whether Memory-tagging is enabled for this module
354 Memtag_heap *bool `blueprint:"mutated"`
355 // Whether Memory-tagging stack instrumentation is enabled for this module
356 Memtag_stack *bool `blueprint:"mutated"`
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200357 // Whether Memory-tagging globals instrumentation is enabled for this module
358 Memtag_globals *bool `android:"arch_variant"`
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400359
360 // Whether a modifier for ASAN and HWASAN for write only instrumentation is enabled for this
361 // module
362 Writeonly *bool `blueprint:"mutated"`
363
364 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
365 Diag struct {
366 // Whether Undefined behavior sanitizer, diagnostic mode is enabled for this module
367 Undefined *bool `blueprint:"mutated"`
368 // Whether cfi sanitizer, diagnostic mode is enabled for this module
369 Cfi *bool `blueprint:"mutated"`
370 // Whether signed/unsigned integer overflow sanitizer, diagnostic mode is enabled for this
371 // module
372 Integer_overflow *bool `blueprint:"mutated"`
373 // Whether Memory-tagging, diagnostic mode is enabled for this module
374 Memtag_heap *bool `blueprint:"mutated"`
375 // List of specific undefined behavior sanitizers enabled in diagnostic mode
376 Misc_undefined []string `blueprint:"mutated"`
377 } `blueprint:"mutated"`
378}
379
Martin Stjernholmb0249572020-09-15 02:32:35 +0100380type SanitizeProperties struct {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400381 Sanitize SanitizeUserProps `android:"arch_variant"`
382 SanitizeMutated sanitizeMutatedProperties `blueprint:"mutated"`
383
384 SanitizerEnabled bool `blueprint:"mutated"`
385 MinimalRuntimeDep bool `blueprint:"mutated"`
386 BuiltinsDep bool `blueprint:"mutated"`
387 UbsanRuntimeDep bool `blueprint:"mutated"`
388 InSanitizerDir bool `blueprint:"mutated"`
389 Sanitizers []string `blueprint:"mutated"`
390 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800391}
392
393type sanitize struct {
394 Properties SanitizeProperties
395}
396
Cindy Zhou18417cb2020-12-10 07:12:38 -0800397// Mark this tag with a check to see if apex dependency check should be skipped
398func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
399 return t.skipApexAllowedDependenciesCheck
400}
401
402var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
403
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000404var exportedVars = android.NewExportedVariables(pctx)
405
Vishwath Mohane7128792017-11-17 11:08:10 -0800406func init() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000407 exportedVars.ExportStringListStaticVariable("HostOnlySanitizeFlags", hostOnlySanitizeFlags)
408 exportedVars.ExportStringList("DeviceOnlySanitizeFlags", deviceOnlySanitizeFlags)
409
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000410 // Leave out "-flto" from the slices exported to bazel, as we will use the
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000411 // dedicated LTO feature for this. For C Flags and Linker Flags, also leave
Trevor Radcliffef1836e42023-06-01 21:12:08 +0000412 // out the cross DSO flag which will be added separately under the correct conditions.
413 exportedVars.ExportStringList("CfiCFlags", append(cfiCflags[2:], cfiEnableFlag))
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000414 exportedVars.ExportStringList("CfiLdFlags", cfiLdflags[2:])
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000415 exportedVars.ExportStringList("CfiAsFlags", cfiAsflags[1:])
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000416
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000417 exportedVars.ExportString("SanitizeIgnorelistPrefix", sanitizeIgnorelistPrefix)
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000418 exportedVars.ExportString("CfiCrossDsoFlag", cfiCrossDsoFlag)
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000419 exportedVars.ExportString("CfiBlocklistPath", cfiBlocklistPath)
420 exportedVars.ExportString("CfiBlocklistFilename", cfiBlocklistFilename)
421 exportedVars.ExportString("CfiExportsMapPath", cfiExportsMapPath)
422 exportedVars.ExportString("CfiExportsMapFilename", cfiExportsMapFilename)
423 exportedVars.ExportString("CfiAssemblySupportFlag", cfiAssemblySupportFlag)
424
Vishwath Mohane7128792017-11-17 11:08:10 -0800425 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700426 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800427}
428
Colin Cross16b23492016-01-06 14:41:07 -0800429func (sanitize *sanitize) props() []interface{} {
430 return []interface{}{&sanitize.Properties}
431}
432
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400433func (p *sanitizeMutatedProperties) copyUserPropertiesToMutated(userProps *SanitizeUserProps) {
434 p.Never = userProps.Never
435 p.Address = userProps.Address
436 p.All_undefined = userProps.All_undefined
437 p.Cfi = userProps.Cfi
438 p.Fuzzer = userProps.Fuzzer
439 p.Hwaddress = userProps.Hwaddress
440 p.Integer_overflow = userProps.Integer_overflow
441 p.Memtag_heap = userProps.Memtag_heap
442 p.Memtag_stack = userProps.Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200443 p.Memtag_globals = userProps.Memtag_globals
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400444 p.Safestack = userProps.Safestack
445 p.Scs = userProps.Scs
446 p.Scudo = userProps.Scudo
447 p.Thread = userProps.Thread
448 p.Undefined = userProps.Undefined
449 p.Writeonly = userProps.Writeonly
450
451 p.Misc_undefined = make([]string, 0, len(userProps.Misc_undefined))
452 for _, v := range userProps.Misc_undefined {
453 p.Misc_undefined = append(p.Misc_undefined, v)
454 }
455
456 p.Diag.Cfi = userProps.Diag.Cfi
457 p.Diag.Integer_overflow = userProps.Diag.Integer_overflow
458 p.Diag.Memtag_heap = userProps.Diag.Memtag_heap
459 p.Diag.Undefined = userProps.Diag.Undefined
460
461 p.Diag.Misc_undefined = make([]string, 0, len(userProps.Diag.Misc_undefined))
462 for _, v := range userProps.Diag.Misc_undefined {
463 p.Diag.Misc_undefined = append(p.Diag.Misc_undefined, v)
464 }
465}
466
Colin Cross16b23492016-01-06 14:41:07 -0800467func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400468 s := &sanitize.Properties.SanitizeMutated
469 s.copyUserPropertiesToMutated(&sanitize.Properties.Sanitize)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700470
Colin Cross16b23492016-01-06 14:41:07 -0800471 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700472 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800473 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800474 }
475
476 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800477 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800478 return
479 }
480
Florian Mayerd8434a42022-08-31 20:57:03 +0000481 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400482 if ctx.testBinary() {
483 if s.Memtag_heap == nil {
484 s.Memtag_heap = proptools.BoolPtr(true)
485 }
486 if s.Diag.Memtag_heap == nil {
487 s.Diag.Memtag_heap = proptools.BoolPtr(true)
488 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800489 }
490
Colin Cross16b23492016-01-06 14:41:07 -0800491 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700492 var globalSanitizersDiag []string
493
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700494 if ctx.Host() {
495 if !ctx.Windows() {
496 globalSanitizers = ctx.Config().SanitizeHost()
497 }
498 } else {
499 arches := ctx.Config().SanitizeDeviceArch()
500 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
501 globalSanitizers = ctx.Config().SanitizeDevice()
502 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800503 }
504 }
505
Colin Cross16b23492016-01-06 14:41:07 -0800506 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000507 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700508 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400509 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000510 }
Colin Cross16b23492016-01-06 14:41:07 -0800511
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700512 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400513 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000514 }
515
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700516 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400517 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000518 }
519
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700520 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400521 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000522 }
523
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700524 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400525 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700526 }
527
528 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400529 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000530 }
531
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700532 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800533 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400534 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700535 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700536 }
537
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700538 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700539 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700540 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400541 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700542 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700543 }
544
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700545 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400546 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700547 }
548
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700549 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400550 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700551 }
552
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000553 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
554 // Hwaddress and Address are set before, so we can check them here
555 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
556 if s.Address == nil && s.Hwaddress == nil {
557 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
558 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400559 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000560 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700561 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800562 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400563 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800564 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700565 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000566
Florian Mayerd8434a42022-08-31 20:57:03 +0000567 if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
568 s.Memtag_stack = proptools.BoolPtr(true)
569 }
570
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200571 if found, globalSanitizers = removeFromList("memtag_globals", globalSanitizers); found && s.Memtag_globals == nil {
572 s.Memtag_globals = proptools.BoolPtr(true)
573 }
574
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000575 if len(globalSanitizers) > 0 {
576 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
577 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700578
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700579 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700580 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700581 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400582 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700583 }
584
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700585 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
586 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400587 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700588 }
589
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800590 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
591 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400592 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800593 }
594
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700595 if len(globalSanitizersDiag) > 0 {
596 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
597 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700598 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700599
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800600 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross88a029f2022-06-23 14:51:20 -0700601 if ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800602 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800603 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400604 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800605 }
606 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400607 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800608 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800609 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800610 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400611 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800612 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800613 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700614 }
615
Hang Lua98aab92023-03-17 13:17:22 +0800616 // Enable HWASan for all components in the include paths (for Aarch64 only)
617 if s.Hwaddress == nil && ctx.Config().HWASanEnabledForPath(ctx.ModuleDir()) &&
618 ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
619 s.Hwaddress = proptools.BoolPtr(true)
620 }
621
Elvis Chien9c993542021-06-25 01:15:17 +0800622 // Enable CFI for non-host components in the include paths
623 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400624 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000625 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400626 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700627 }
628 }
629
Elliott Hughesda3a0712020-03-06 16:55:28 -0800630 // Is CFI actually enabled?
631 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400632 s.Cfi = nil
633 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800634 }
635
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700636 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross88a029f2022-06-23 14:51:20 -0700637 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700638 s.Hwaddress = nil
639 }
640
Elliott Hughese4793bc2023-02-09 21:15:47 +0000641 // SCS is only implemented on AArch64/riscv64.
642 if (ctx.Arch().ArchType != android.Arm64 && ctx.Arch().ArchType != android.Riscv64) || !ctx.toolchain().Bionic() {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800643 s.Scs = nil
644 }
645
Ivan Lozano62cd0382021-11-01 10:27:54 -0400646 // Memtag_heap is only implemented on AArch64.
Florian Mayerd8434a42022-08-31 20:57:03 +0000647 // Memtag ABI is Android specific for now, so disable for host.
648 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() || ctx.Host() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700649 s.Memtag_heap = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000650 s.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200651 s.Memtag_globals = nil
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700652 }
653
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700654 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700655 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400656 s.Cfi = nil
657 s.Diag.Cfi = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000658 // HWASAN and ASAN win against MTE.
659 s.Memtag_heap = nil
660 s.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200661 s.Memtag_globals = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700662 }
663
Colin Crossed12a042022-02-07 13:55:55 -0800664 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
665 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400666 s.Cfi = nil
667 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700668 s.Misc_undefined = nil
669 s.Undefined = nil
670 s.All_undefined = nil
671 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800672 }
673
Colin Crossed12a042022-02-07 13:55:55 -0800674 // Disable CFI for musl
675 if ctx.toolchain().Musl() {
676 s.Cfi = nil
677 s.Diag.Cfi = nil
678 }
679
Colin Cross390fc742023-05-02 13:02:51 -0700680 // TODO(b/280478629): runtimes don't exist for musl arm64 yet.
681 if ctx.toolchain().Musl() && ctx.Arch().ArchType == android.Arm64 {
682 s.Address = nil
683 s.Hwaddress = nil
684 s.Thread = nil
685 s.Scudo = nil
686 s.Fuzzer = nil
687 s.Cfi = nil
688 s.Diag.Cfi = nil
689 s.Misc_undefined = nil
690 s.Undefined = nil
691 s.All_undefined = nil
692 s.Integer_overflow = nil
693 }
694
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700695 // Also disable CFI for VNDK variants of components
696 if ctx.isVndk() && ctx.useVndk() {
Justin Yun08270c62022-12-19 17:01:26 +0900697 s.Cfi = nil
698 s.Diag.Cfi = nil
Inseob Kimeec88e12020-01-22 11:11:29 +0900699 }
700
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700701 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700702 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
703 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700704 s.Hwaddress = nil
705 }
706
Colin Cross3c344ef2016-07-18 15:44:56 -0700707 if ctx.staticBinary() {
708 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700709 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700710 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800711 }
712
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700713 if Bool(s.All_undefined) {
714 s.Undefined = nil
715 }
716
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700717 if !ctx.toolchain().Is64Bit() {
718 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700719 s.Thread = nil
720 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800721 // TODO(ccross): error for compile_multilib = "32"?
722 }
723
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800724 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700725 Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200726 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack) ||
727 Bool(s.Memtag_globals)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700728 sanitize.Properties.SanitizerEnabled = true
729 }
730
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800731 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
732 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700733 s.Scudo = nil
734 }
735
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700736 if Bool(s.Hwaddress) {
737 s.Address = nil
738 s.Thread = nil
739 }
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000740
741 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
742 // mutually incompatible.
743 if Bool(s.Fuzzer) {
744 s.Cfi = nil
745 }
Colin Cross16b23492016-01-06 14:41:07 -0800746}
747
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800748func toDisableImplicitIntegerChange(flags []string) bool {
749 // Returns true if any flag is fsanitize*integer, and there is
750 // no explicit flag about sanitize=implicit-integer-sign-change.
751 for _, f := range flags {
752 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
753 return false
754 }
755 }
756 for _, f := range flags {
757 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
758 return true
759 }
760 }
761 return false
762}
763
Yabin Cuidb7dda82020-11-30 15:47:45 -0800764func toDisableUnsignedShiftBaseChange(flags []string) bool {
765 // Returns true if any flag is fsanitize*integer, and there is
766 // no explicit flag about sanitize=unsigned-shift-base.
767 for _, f := range flags {
768 if strings.Contains(f, "sanitize=unsigned-shift-base") {
769 return false
770 }
771 }
772 for _, f := range flags {
773 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
774 return true
775 }
776 }
777 return false
778}
779
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400780func (s *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
781 if !s.Properties.SanitizerEnabled && !s.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800782 return flags
783 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400784 sanProps := &s.Properties.SanitizeMutated
Colin Cross16b23492016-01-06 14:41:07 -0800785
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400786 if Bool(sanProps.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700787 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800788 // Frame pointer based unwinder in ASan requires ARM frame setup.
789 // TODO: put in flags?
790 flags.RequiredInstructionSet = "arm"
791 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800792 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
793 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800794
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400795 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000796 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
797 }
798
Colin Cross16b23492016-01-06 14:41:07 -0800799 if ctx.Host() {
800 // -nodefaultlibs (provided with libc++) prevents the driver from linking
801 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800802 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800803 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800804 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900805 if ctx.bootstrap() {
806 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
807 } else {
808 flags.DynamicLinker = "/system/bin/linker_asan"
809 }
Colin Cross16b23492016-01-06 14:41:07 -0800810 if flags.Toolchain.Is64Bit() {
811 flags.DynamicLinker += "64"
812 }
813 }
Colin Cross16b23492016-01-06 14:41:07 -0800814 }
815
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400816 if Bool(sanProps.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800817 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800818
819 for _, flag := range hwasanCommonflags {
820 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
821 }
822 for _, flag := range hwasanCommonflags {
823 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
824 }
825
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400826 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000827 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
828 }
Florian Mayer95cd6db2023-03-23 17:48:07 -0700829 if !ctx.staticBinary() && !ctx.Host() {
830 if ctx.bootstrap() {
831 flags.DynamicLinker = "/system/bin/bootstrap/linker_hwasan64"
832 } else {
833 flags.DynamicLinker = "/system/bin/linker_hwasan64"
834 }
835 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700836 }
837
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400838 if Bool(sanProps.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800839 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700840
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000841 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
842 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
843 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
844 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
845 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
846
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700847 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
848 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
849 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800850 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
851 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700852
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700853 // Disable fortify for fuzzing builds. Generally, we'll be building with
854 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800855 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800856
857 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
858 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
859 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
860 // the DT_RUNPATH from the shared library above it, and not the executable,
861 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
862 // DT_RUNPATH here means that transient shared libraries can be found
863 // colocated with their parents.
864 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800865 }
866
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400867 if Bool(sanProps.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800868 if ctx.Arch().ArchType == android.Arm {
869 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
870 // to do this on a function basis, so force Thumb on the entire module.
871 flags.RequiredInstructionSet = "thumb"
872 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000873
Colin Cross4af21ed2019-11-04 09:37:55 -0800874 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
875 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400876 if Bool(s.Properties.Sanitize.Config.Cfi_assembly_support) {
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000877 flags.Local.CFlags = append(flags.Local.CFlags, cfiAssemblySupportFlag)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800878 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000879 // Only append the default visibility flag if -fvisibility has not already been set
880 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800881 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
882 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000883 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800884 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000885
886 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800887 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
888 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000889 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700890 }
891
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400892 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000893 flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
894 flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
895 flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
896 }
897
Mitch Phillips92d19fa2023-06-01 14:23:09 +0200898 if (Bool(sanProps.Memtag_heap) || Bool(sanProps.Memtag_stack) || Bool(sanProps.Memtag_globals)) && ctx.binary() {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400899 if Bool(sanProps.Diag.Memtag_heap) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000900 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
901 } else {
902 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
903 }
904 }
905
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400906 if Bool(sanProps.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800907 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700908 }
909
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400910 if len(s.Properties.Sanitizers) > 0 {
911 sanitizeArg := "-fsanitize=" + strings.Join(s.Properties.Sanitizers, ",")
Colin Cross4af21ed2019-11-04 09:37:55 -0800912 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
913 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross234b01d2022-02-07 13:49:03 -0800914 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
915
Colin Crossed12a042022-02-07 13:55:55 -0800916 if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
917 // Bionic and musl sanitizer runtimes have already been added as dependencies so that
918 // the right variant of the runtime will be used (with the "-android" or "-musl"
919 // suffixes), so don't let clang the runtime library.
Colin Cross234b01d2022-02-07 13:49:03 -0800920 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-link-runtime")
921 } else {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800922 // Host sanitizers only link symbols in the final executable, so
923 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800924 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
Colin Cross6c18d002022-06-02 15:11:50 -0700925 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500926
Colin Cross6c18d002022-06-02 15:11:50 -0700927 if !ctx.toolchain().Bionic() {
928 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function san.
929 // Musl toolchain prebuilts have vptr and function sanitizers, but enabling them
930 // implicitly enables RTTI which causes RTTI mismatch issues with dependencies.
931
Colin Cross234b01d2022-02-07 13:49:03 -0800932 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500933 }
934
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400935 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700936 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800937 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700938 } else if ctx.Host() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000939 flags.Local.CFlags = append(flags.Local.CFlags, hostOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700940 } else {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000941 flags.Local.CFlags = append(flags.Local.CFlags, deviceOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700942 }
Evgenii Stepanov59012812022-06-24 11:09:18 -0700943
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400944 if enableMinimalRuntime(s) {
Evgenii Stepanov59012812022-06-24 11:09:18 -0700945 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
946 }
947
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800948 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800949 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
950 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800951 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800952 // http://b/171275751, Android doesn't build with this sanitizer yet.
953 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
954 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
955 }
Colin Cross16b23492016-01-06 14:41:07 -0800956 }
957
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400958 if len(s.Properties.DiagSanitizers) > 0 {
959 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(s.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700960 }
961 // FIXME: enable RTTI if diag + (cfi or vptr)
962
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400963 if s.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800964 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400965 strings.Join(s.Properties.Sanitize.Recover, ","))
Andreas Gampe97071162017-05-08 13:15:23 -0700966 }
967
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400968 if s.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800969 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400970 strings.Join(s.Properties.Sanitize.Diag.No_recover, ","))
Ivan Lozano7929bba2018-12-12 09:36:31 -0800971 }
972
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400973 blocklist := android.OptionalPathForModuleSrc(ctx, s.Properties.Sanitize.Blocklist)
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700974 if blocklist.Valid() {
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000975 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeIgnorelistPrefix+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700976 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
977 }
978
Colin Cross16b23492016-01-06 14:41:07 -0800979 return flags
980}
981
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400982func (s *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900983 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
984 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800985 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400986 if Bool(s.Properties.SanitizeMutated.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800987 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900988 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400989 if Bool(s.Properties.SanitizeMutated.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800990 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900991 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400992 if Bool(s.Properties.SanitizeMutated.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800993 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900994 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800995 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700996}
997
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400998func (s *sanitize) inSanitizerDir() bool {
999 return s.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -07001000}
1001
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001002// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001003func (s *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +00001004 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001005 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001006 return s.Properties.SanitizeMutated.Address
Tri Vo6eafc362021-04-01 11:29:09 -07001007 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001008 return s.Properties.SanitizeMutated.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +00001009 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001010 return s.Properties.SanitizeMutated.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +00001011 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001012 return s.Properties.SanitizeMutated.Integer_overflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001013 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001014 return s.Properties.SanitizeMutated.Cfi
Ivan Lozano88271132023-07-26 13:34:55 -04001015 case Scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001016 return s.Properties.SanitizeMutated.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -04001017 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001018 return s.Properties.SanitizeMutated.Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +00001019 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001020 return s.Properties.SanitizeMutated.Memtag_stack
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001021 case Memtag_globals:
1022 return s.Properties.SanitizeMutated.Memtag_globals
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001023 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001024 return s.Properties.SanitizeMutated.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +00001025 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001026 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +00001027 }
1028}
1029
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001030// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -08001031func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001032 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001033 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -08001034 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001035 !sanitize.isSanitizerEnabled(cfi) &&
Ivan Lozano88271132023-07-26 13:34:55 -04001036 !sanitize.isSanitizerEnabled(Scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -04001037 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Florian Mayerd8434a42022-08-31 20:57:03 +00001038 !sanitize.isSanitizerEnabled(Memtag_stack) &&
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001039 !sanitize.isSanitizerEnabled(Memtag_globals) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001040 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -08001041}
1042
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001043// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001044func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001045 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001046 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001047 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001048 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001049}
1050
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001051func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -04001052 bPtr := proptools.BoolPtr(b)
1053 if !b {
1054 bPtr = nil
1055 }
Colin Cross16b23492016-01-06 14:41:07 -08001056 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001057 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001058 sanitize.Properties.SanitizeMutated.Address = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001059 // For ASAN variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001060 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001061 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Tri Vo6eafc362021-04-01 11:29:09 -07001062 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001063 sanitize.Properties.SanitizeMutated.Hwaddress = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001064 // For HWAsan variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001065 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001066 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Colin Cross16b23492016-01-06 14:41:07 -08001067 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001068 sanitize.Properties.SanitizeMutated.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -07001069 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001070 sanitize.Properties.SanitizeMutated.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001071 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001072 sanitize.Properties.SanitizeMutated.Cfi = bPtr
Ivan Lozano88271132023-07-26 13:34:55 -04001073 case Scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001074 sanitize.Properties.SanitizeMutated.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -04001075 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001076 sanitize.Properties.SanitizeMutated.Memtag_heap = bPtr
Florian Mayerd8434a42022-08-31 20:57:03 +00001077 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001078 sanitize.Properties.SanitizeMutated.Memtag_stack = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001079 // We do not need to disable ASAN or HWASan here, as there is no Memtag_stack variant.
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001080 case Memtag_globals:
1081 sanitize.Properties.Sanitize.Memtag_globals = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001082 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001083 sanitize.Properties.SanitizeMutated.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -08001084 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001085 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -08001086 }
1087 if b {
1088 sanitize.Properties.SanitizerEnabled = true
1089 }
1090}
1091
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001092// Check if the sanitizer is explicitly disabled (as opposed to nil by
1093// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001094func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001095 if sanitize == nil {
1096 return false
1097 }
1098
1099 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1100 return sanitizerVal != nil && *sanitizerVal == false
1101}
1102
1103// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
1104// because enabling a sanitizer either directly (via the blueprint) or
1105// indirectly (via a mutator) sets the bool ptr to true, and you can't
1106// distinguish between the cases. It isn't needed though - both cases can be
1107// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001108func (sanitize *sanitize) isSanitizerEnabled(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 == true
1115}
1116
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001117// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
1118func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -07001119 switch t := tag.(type) {
1120 case dependencyTag:
1121 return t == reuseObjTag || t == objDepTag
1122 case libraryDependencyTag:
1123 return true
1124 default:
1125 return false
1126 }
Colin Cross6b753602018-06-21 13:03:07 -07001127}
1128
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001129func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
1130 return IsSanitizableDependencyTag
1131}
1132
Inseob Kimc42f2f22020-07-29 20:32:10 +09001133// Determines if the current module is a static library going to be captured
1134// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
1135// except for ones which explicitly disable cfi.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001136func needsCfiForVendorSnapshot(mctx android.BaseModuleContext) bool {
Justin Yun8814fc52022-12-15 21:45:35 +09001137 if inList("hwaddress", mctx.Config().SanitizeDevice()) {
1138 // cfi will not be built if SANITIZE_TARGET=hwaddress is set
1139 return false
1140 }
1141
Kiyoung Kim48f37782021-07-07 12:42:39 +09001142 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001143 return false
1144 }
1145
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001146 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001147
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001148 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001149 return false
1150 }
1151
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001152 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001153 return false
1154 }
1155
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001156 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001157 return false
1158 }
1159
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001160 if !c.SanitizerSupported(cfi) {
1161 return false
1162 }
1163
1164 return c.SanitizePropDefined() &&
1165 !c.SanitizeNever() &&
1166 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001167}
1168
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001169type sanitizerSplitMutator struct {
1170 sanitizer SanitizerType
1171}
1172
1173// If an APEX is sanitized or not depends on whether it contains at least one
1174// sanitized module. Transition mutators cannot propagate information up the
1175// dependency graph this way, so we need an auxiliary mutator to do so.
1176func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
1177 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1178 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1179 ctx.VisitDirectDeps(func(dep android.Module) {
1180 if c, ok := dep.(*Module); ok && c.sanitize.isSanitizerEnabled(s.sanitizer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001181 enabled = true
Inseob Kimc42f2f22020-07-29 20:32:10 +09001182 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001183 })
1184
1185 if enabled {
1186 sanitizeable.EnableSanitizer(s.sanitizer.name())
1187 }
1188 }
1189}
1190
1191func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
1192 if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1193 if s.sanitizer == cfi && needsCfiForVendorSnapshot(ctx) {
1194 return []string{"", s.sanitizer.variationName()}
1195 }
1196
1197 // If the given sanitizer is not requested in the .bp file for a module, it
1198 // won't automatically build the sanitized variation.
1199 if !c.IsSanitizerEnabled(s.sanitizer) {
1200 return []string{""}
1201 }
1202
1203 if c.Binary() {
1204 // If a sanitizer is enabled for a binary, we do not build the version
1205 // without the sanitizer
1206 return []string{s.sanitizer.variationName()}
1207 } else if c.StaticallyLinked() || c.Header() {
1208 // For static libraries, we build both versions. Some Make modules
1209 // apparently depend on this behavior.
1210 return []string{"", s.sanitizer.variationName()}
1211 } else {
1212 // We only build the requested variation of dynamic libraries
1213 return []string{s.sanitizer.variationName()}
1214 }
1215 }
1216
1217 if _, ok := ctx.Module().(JniSanitizeable); ok {
1218 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1219 // that is short-circuited for now
1220 return []string{""}
1221 }
1222
1223 // If an APEX has a sanitized dependency, we build the APEX in the sanitized
1224 // variation. This is useful because such APEXes require extra dependencies.
1225 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1226 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1227 if enabled {
1228 return []string{s.sanitizer.variationName()}
1229 } else {
1230 return []string{""}
1231 }
1232 }
1233
1234 if c, ok := ctx.Module().(*Module); ok {
1235 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1236
1237 // Check if it's a snapshot module supporting sanitizer
Justin Yun08270c62022-12-19 17:01:26 +09001238 if ss, ok := c.linker.(snapshotSanitizer); ok {
1239 if ss.isSanitizerAvailable(s.sanitizer) {
1240 return []string{"", s.sanitizer.variationName()}
1241 } else {
1242 return []string{""}
1243 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001244 }
1245 }
1246
1247 return []string{""}
1248}
1249
1250func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1251 if c, ok := ctx.Module().(PlatformSanitizeable); ok {
1252 if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
1253 // If the dependency is through a non-sanitizable tag, use the
1254 // non-sanitized variation
1255 return ""
1256 }
1257
1258 return sourceVariation
1259 } else if _, ok := ctx.Module().(JniSanitizeable); ok {
1260 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1261 // that is short-circuited for now
1262 return ""
1263 } else {
1264 // Otherwise, do not rock the boat.
1265 return sourceVariation
1266 }
1267}
1268
1269func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1270 if d, ok := ctx.Module().(PlatformSanitizeable); ok {
1271 if dm, ok := ctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001272 if ss, ok := dm.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001273 return incomingVariation
Inseob Kimc42f2f22020-07-29 20:32:10 +09001274 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001275 }
1276
1277 if !d.SanitizePropDefined() ||
1278 d.SanitizeNever() ||
1279 d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
1280 !d.SanitizerSupported(s.sanitizer) {
1281 // If a module opts out of a sanitizer, use its non-sanitized variation
1282 return ""
1283 }
1284
1285 // Binaries are always built in the variation they requested.
1286 if d.Binary() {
1287 if d.IsSanitizerEnabled(s.sanitizer) {
1288 return s.sanitizer.variationName()
1289 } else {
1290 return ""
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001291 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001292 }
1293
1294 // If a shared library requests to be sanitized, it will be built for that
1295 // sanitizer. Otherwise, some sanitizers propagate through shared library
1296 // dependency edges, some do not.
1297 if !d.StaticallyLinked() && !d.Header() {
1298 if d.IsSanitizerEnabled(s.sanitizer) {
1299 return s.sanitizer.variationName()
1300 }
1301
Liz Kammerfd8a49f2022-10-31 10:31:11 -04001302 // Some sanitizers do not propagate to shared dependencies
1303 if !s.sanitizer.shouldPropagateToSharedLibraryDeps() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001304 return ""
1305 }
1306 }
1307
1308 // Static and header libraries inherit whether they are sanitized from the
1309 // module they are linked into
1310 return incomingVariation
1311 } else if d, ok := ctx.Module().(Sanitizeable); ok {
1312 // If an APEX contains a sanitized module, it will be built in the variation
1313 // corresponding to that sanitizer.
1314 enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1315 if enabled {
1316 return s.sanitizer.variationName()
1317 }
1318
1319 return incomingVariation
1320 }
1321
1322 return ""
1323}
1324
1325func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
1326 sanitizerVariation := variationName == s.sanitizer.variationName()
1327
1328 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1329 sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
1330
1331 oneMakeVariation := false
1332 if c.StaticallyLinked() || c.Header() {
Ivan Lozano88271132023-07-26 13:34:55 -04001333 if s.sanitizer != cfi && s.sanitizer != Scs && s.sanitizer != Hwasan {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001334 // These sanitizers export only one variation to Make. For the rest,
1335 // Make targets can depend on both the sanitized and non-sanitized
1336 // versions.
1337 oneMakeVariation = true
1338 }
1339 } else if !c.Binary() {
1340 // Shared library. These are the sanitizers that do propagate through shared
1341 // library dependencies and therefore can cause multiple variations of a
1342 // shared library to be built.
Ivan Lozano88271132023-07-26 13:34:55 -04001343 if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != Scs && s.sanitizer != Asan {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001344 oneMakeVariation = true
1345 }
1346 }
1347
1348 if oneMakeVariation {
1349 if sanitizerEnabled != sanitizerVariation {
1350 c.SetPreventInstall()
1351 c.SetHideFromMake()
1352 }
1353 }
1354
1355 if sanitizerVariation {
1356 c.SetSanitizer(s.sanitizer, true)
1357
1358 // CFI is incompatible with ASAN so disable it in ASAN variations
1359 if s.sanitizer.incompatibleWithCfi() {
1360 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1361 if mctx.Device() && cfiSupported {
1362 c.SetSanitizer(cfi, false)
Jiyong Parkf97782b2019-02-13 20:28:58 +09001363 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001364 }
1365
1366 // locate the asan libraries under /data/asan
1367 if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
1368 c.SetInSanitizerDir()
1369 }
1370
1371 if c.StaticallyLinked() && c.ExportedToMake() {
1372 if s.sanitizer == Hwasan {
1373 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
1374 } else if s.sanitizer == cfi {
1375 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
1376 }
1377 }
1378 } else if c.IsSanitizerEnabled(s.sanitizer) {
1379 // Disable the sanitizer for the non-sanitized variation
1380 c.SetSanitizer(s.sanitizer, false)
1381 }
1382 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
1383 // If an APEX has sanitized dependencies, it gets a few more dependencies
1384 if sanitizerVariation {
1385 sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
1386 }
1387 } else if c, ok := mctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001388 if ss, ok := c.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
1389 if !ss.isUnsanitizedVariant() {
1390 // Snapshot sanitizer may have only one variantion.
1391 // Skip exporting the module if it already has a sanitizer variation.
1392 c.SetPreventInstall()
1393 c.SetHideFromMake()
1394 return
1395 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001396 c.linker.(snapshotSanitizer).setSanitizerVariation(s.sanitizer, sanitizerVariation)
1397
1398 // Export the static lib name to make
1399 if c.static() && c.ExportedToMake() {
Justin Yun39c30312022-11-23 16:20:12 +09001400 // use BaseModuleName which is the name for Make.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001401 if s.sanitizer == cfi {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001402 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Justin Yun39c30312022-11-23 16:20:12 +09001403 } else if s.sanitizer == Hwasan {
1404 hwasanStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001405 }
1406 }
Colin Cross16b23492016-01-06 14:41:07 -08001407 }
1408 }
1409}
1410
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001411func (c *Module) SanitizeNever() bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001412 return Bool(c.sanitize.Properties.SanitizeMutated.Never)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001413}
1414
1415func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
1416 return c.sanitize.isSanitizerExplicitlyDisabled(t)
1417}
1418
Ivan Lozano30c5db22018-02-21 15:49:20 -08001419// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001420func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001421 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001422 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001423 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001424 mctx.WalkDeps(func(child, parent android.Module) bool {
1425 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1426 return false
1427 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001428
Inseob Kimeec88e12020-01-22 11:11:29 +09001429 d, ok := child.(*Module)
1430 if !ok || !d.static() {
1431 return false
1432 }
1433 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001434 if enableMinimalRuntime(d.sanitize) {
1435 // If a static dependency is built with the minimal runtime,
1436 // make sure we include the ubsan minimal runtime.
1437 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001438 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001439 // If a static dependency runs with full ubsan diagnostics,
1440 // make sure we include the ubsan runtime.
1441 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001442 }
Colin Cross0b908332019-06-19 23:00:20 -07001443
1444 if c.sanitize.Properties.MinimalRuntimeDep &&
1445 c.sanitize.Properties.UbsanRuntimeDep {
1446 // both flags that this mutator might set are true, so don't bother recursing
1447 return false
1448 }
1449
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001450 if c.Os() == android.Linux {
1451 c.sanitize.Properties.BuiltinsDep = true
1452 }
1453
Colin Cross0b908332019-06-19 23:00:20 -07001454 return true
Colin Cross6b753602018-06-21 13:03:07 -07001455 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001456
Jose Galmesf7294582020-11-13 12:07:36 -08001457 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001458 if Bool(p.properties.Sanitize_minimal_dep) {
1459 c.sanitize.Properties.MinimalRuntimeDep = true
1460 }
1461 if Bool(p.properties.Sanitize_ubsan_dep) {
1462 c.sanitize.Properties.UbsanRuntimeDep = true
1463 }
1464 }
1465
1466 return false
Colin Cross6b753602018-06-21 13:03:07 -07001467 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001468 }
1469}
1470
Jiyong Park379de2f2018-12-19 02:47:14 +09001471// Add the dependency to the runtime library for each of the sanitizer variants
1472func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001473 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001474 if !c.Enabled() {
1475 return
1476 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001477 var sanitizers []string
1478 var diagSanitizers []string
1479
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001480 sanProps := &c.sanitize.Properties.SanitizeMutated
1481
1482 if Bool(sanProps.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001483 sanitizers = append(sanitizers, "undefined")
1484 } else {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001485 if Bool(sanProps.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001486 sanitizers = append(sanitizers,
1487 "bool",
1488 "integer-divide-by-zero",
1489 "return",
1490 "returns-nonnull-attribute",
1491 "shift-exponent",
1492 "unreachable",
1493 "vla-bound",
1494 // TODO(danalbert): The following checks currently have compiler performance issues.
1495 //"alignment",
1496 //"bounds",
1497 //"enum",
1498 //"float-cast-overflow",
1499 //"float-divide-by-zero",
1500 //"nonnull-attribute",
1501 //"null",
1502 //"shift-base",
1503 //"signed-integer-overflow",
1504 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1505 // https://llvm.org/PR19302
1506 // http://reviews.llvm.org/D6974
1507 // "object-size",
1508 )
1509 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001510 sanitizers = append(sanitizers, sanProps.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001511 }
1512
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001513 if Bool(sanProps.Diag.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001514 diagSanitizers = append(diagSanitizers, "undefined")
1515 }
1516
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001517 diagSanitizers = append(diagSanitizers, sanProps.Diag.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001518
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001519 if Bool(sanProps.Address) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001520 sanitizers = append(sanitizers, "address")
1521 diagSanitizers = append(diagSanitizers, "address")
1522 }
1523
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001524 if Bool(sanProps.Hwaddress) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001525 sanitizers = append(sanitizers, "hwaddress")
1526 }
1527
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001528 if Bool(sanProps.Thread) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001529 sanitizers = append(sanitizers, "thread")
1530 }
1531
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001532 if Bool(sanProps.Safestack) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001533 sanitizers = append(sanitizers, "safe-stack")
1534 }
1535
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001536 if Bool(sanProps.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001537 sanitizers = append(sanitizers, "cfi")
1538
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001539 if Bool(sanProps.Diag.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001540 diagSanitizers = append(diagSanitizers, "cfi")
1541 }
1542 }
1543
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001544 if Bool(sanProps.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001545 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1546 sanitizers = append(sanitizers, "signed-integer-overflow")
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001547 if Bool(sanProps.Diag.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001548 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1549 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1550 }
1551 }
1552
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001553 if Bool(sanProps.Scudo) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001554 sanitizers = append(sanitizers, "scudo")
1555 }
1556
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001557 if Bool(sanProps.Scs) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001558 sanitizers = append(sanitizers, "shadow-call-stack")
1559 }
1560
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001561 if Bool(sanProps.Memtag_heap) && c.Binary() {
Florian Mayerd8434a42022-08-31 20:57:03 +00001562 sanitizers = append(sanitizers, "memtag-heap")
1563 }
1564
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001565 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +00001566 sanitizers = append(sanitizers, "memtag-stack")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001567 }
1568
Mitch Phillips92d19fa2023-06-01 14:23:09 +02001569 if Bool(sanProps.Memtag_globals) {
1570 sanitizers = append(sanitizers, "memtag-globals")
1571 // TODO(mitchp): For now, enable memtag-heap with memtag-globals because the linker
1572 // isn't new enough (https://reviews.llvm.org/differential/changeset/?ref=4243566).
1573 sanitizers = append(sanitizers, "memtag-heap")
1574 }
1575
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001576 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001577 sanitizers = append(sanitizers, "fuzzer-no-link")
1578 }
1579
Jiyong Park379de2f2018-12-19 02:47:14 +09001580 // Save the list of sanitizers. These will be used again when generating
1581 // the build rules (for Cflags, etc.)
1582 c.sanitize.Properties.Sanitizers = sanitizers
1583 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1584
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001585 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1586 if c.Host() {
1587 diagSanitizers = sanitizers
1588 }
1589
Colin Crosse323a792023-02-15 13:57:57 -08001590 addStaticDeps := func(dep string, hideSymbols bool) {
Colin Cross06c80eb2022-02-10 10:34:19 -08001591 // If we're using snapshots, redirect to snapshot whenever possible
1592 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001593 if lib, ok := snapshot.StaticLibs[dep]; ok {
1594 dep = lib
Colin Cross06c80eb2022-02-10 10:34:19 -08001595 }
1596
1597 // static executable gets static runtime libs
Colin Crosse323a792023-02-15 13:57:57 -08001598 depTag := libraryDependencyTag{Kind: staticLibraryDependency, unexportedSymbols: hideSymbols}
Colin Cross06c80eb2022-02-10 10:34:19 -08001599 variations := append(mctx.Target().Variations(),
1600 blueprint.Variation{Mutator: "link", Variation: "static"})
1601 if c.Device() {
1602 variations = append(variations, c.ImageVariation())
1603 }
1604 if c.UseSdk() {
1605 variations = append(variations,
1606 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1607 }
Colin Crosse323a792023-02-15 13:57:57 -08001608 mctx.AddFarVariationDependencies(variations, depTag, dep)
Colin Cross06c80eb2022-02-10 10:34:19 -08001609 }
Colin Crosse323a792023-02-15 13:57:57 -08001610
1611 // Determine the runtime library required
1612 runtimeSharedLibrary := ""
1613 toolchain := c.toolchain(mctx)
1614 if Bool(sanProps.Address) {
Colin Crossb781d232023-02-15 12:40:20 -08001615 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1616 // Use a static runtime for musl to match what clang does for glibc.
1617 addStaticDeps(config.AddressSanitizerStaticRuntimeLibrary(toolchain), false)
1618 addStaticDeps(config.AddressSanitizerCXXStaticRuntimeLibrary(toolchain), false)
1619 } else {
1620 runtimeSharedLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1621 }
Colin Crosse323a792023-02-15 13:57:57 -08001622 } else if Bool(sanProps.Hwaddress) {
1623 if c.staticBinary() {
1624 addStaticDeps(config.HWAddressSanitizerStaticLibrary(toolchain), true)
1625 addStaticDeps("libdl", false)
1626 } else {
1627 runtimeSharedLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1628 }
1629 } else if Bool(sanProps.Thread) {
1630 runtimeSharedLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1631 } else if Bool(sanProps.Scudo) {
1632 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1633 runtimeSharedLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1634 } else {
1635 runtimeSharedLibrary = config.ScudoRuntimeLibrary(toolchain)
1636 }
1637 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
1638 Bool(sanProps.Fuzzer) ||
1639 Bool(sanProps.Undefined) ||
1640 Bool(sanProps.All_undefined) {
1641 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1642 // Use a static runtime for static binaries.
1643 // Also use a static runtime for musl to match
1644 // what clang does for glibc. Otherwise dlopening
1645 // libraries that depend on libclang_rt.ubsan_standalone.so
1646 // fails with:
1647 // Error relocating ...: initial-exec TLS resolves to dynamic definition
1648 addStaticDeps(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)+".static", true)
1649 } else {
1650 runtimeSharedLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
1651 }
1652 }
1653
Colin Cross06c80eb2022-02-10 10:34:19 -08001654 if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
Colin Crosse323a792023-02-15 13:57:57 -08001655 addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001656 }
1657 if c.sanitize.Properties.BuiltinsDep {
Colin Crosse323a792023-02-15 13:57:57 -08001658 addStaticDeps(config.BuiltinsRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001659 }
1660
Colin Crosse323a792023-02-15 13:57:57 -08001661 if runtimeSharedLibrary != "" && (toolchain.Bionic() || toolchain.Musl() || c.sanitize.Properties.UbsanRuntimeDep) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001662 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001663
1664 // Adding dependency to the runtime library. We are using *FarVariation*
1665 // because the runtime libraries themselves are not mutated by sanitizer
1666 // mutators and thus don't have sanitizer variants whereas this module
1667 // has been already mutated.
1668 //
1669 // Note that by adding dependency with {static|shared}DepTag, the lib is
1670 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
Colin Crosse323a792023-02-15 13:57:57 -08001671 if c.staticBinary() {
1672 // Most sanitizers are either disabled for static binaries or have already
1673 // handled the static binary case above through a direct call to addStaticDeps.
1674 // If not, treat the runtime shared library as a static library and hope for
1675 // the best.
1676 addStaticDeps(runtimeSharedLibrary, true)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001677 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001678 // If we're using snapshots, redirect to snapshot whenever possible
1679 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001680 if lib, ok := snapshot.SharedLibs[runtimeSharedLibrary]; ok {
1681 runtimeSharedLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001682 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001683
Cindy Zhou18417cb2020-12-10 07:12:38 -08001684 // Skip apex dependency check for sharedLibraryDependency
1685 // when sanitizer diags are enabled. Skipping the check will allow
1686 // building with diag libraries without having to list the
1687 // dependency in Apex's allowed_deps file.
1688 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001689 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001690 depTag := libraryDependencyTag{
1691 Kind: sharedLibraryDependency,
1692 Order: earlyLibraryDependency,
1693
1694 skipApexAllowedDependenciesCheck: diagEnabled,
1695 }
Colin Cross42507332020-08-21 16:15:23 -07001696 variations := append(mctx.Target().Variations(),
1697 blueprint.Variation{Mutator: "link", Variation: "shared"})
1698 if c.Device() {
1699 variations = append(variations, c.ImageVariation())
1700 }
Colin Cross06c80eb2022-02-10 10:34:19 -08001701 if c.UseSdk() {
1702 variations = append(variations,
1703 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1704 }
Colin Crosse323a792023-02-15 13:57:57 -08001705 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeSharedLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001706 }
1707 // static lib does not have dependency to the runtime library. The
1708 // dependency will be added to the executables or shared libs using
1709 // the static lib.
1710 }
1711 }
1712}
1713
1714type Sanitizeable interface {
1715 android.Module
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001716 IsSanitizerEnabled(config android.Config, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001717 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001718 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001719}
1720
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001721type JniSanitizeable interface {
1722 android.Module
1723 IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
1724}
1725
Ivan Lozanod7586b62021-04-01 09:49:36 -04001726func (c *Module) MinimalRuntimeDep() bool {
1727 return c.sanitize.Properties.MinimalRuntimeDep
1728}
1729
1730func (c *Module) UbsanRuntimeDep() bool {
1731 return c.sanitize.Properties.UbsanRuntimeDep
1732}
1733
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001734func (c *Module) SanitizePropDefined() bool {
1735 return c.sanitize != nil
1736}
1737
1738func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1739 return c.sanitize.isSanitizerEnabled(t)
1740}
1741
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001742func (c *Module) StaticallyLinked() bool {
1743 return c.static()
1744}
1745
1746func (c *Module) SetInSanitizerDir() {
1747 if c.sanitize != nil {
1748 c.sanitize.Properties.InSanitizerDir = true
1749 }
1750}
1751
1752func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1753 if c.sanitize != nil {
1754 c.sanitize.SetSanitizer(t, b)
1755 }
1756}
1757
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001758var _ PlatformSanitizeable = (*Module)(nil)
1759
Inseob Kim74d25562020-08-04 00:41:38 +09001760type sanitizerStaticLibsMap struct {
1761 // libsMap contains one list of modules per each image and each arch.
1762 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001763 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001764 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001765 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001766}
1767
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001768func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001769 return &sanitizerStaticLibsMap{
1770 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001771 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001772 }
1773}
1774
1775// Add the current module to sanitizer static libs maps
1776// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001777func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1778 image := GetImageVariantType(c)
1779 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001780
1781 s.libsMapLock.Lock()
1782 defer s.libsMapLock.Unlock()
1783
1784 if _, ok := s.libsMap[image]; !ok {
1785 s.libsMap[image] = make(map[string][]string)
1786 }
1787
1788 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1789}
1790
1791// Exports makefile variables in the following format:
1792// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1793// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1794// These are to be used by use_soong_sanitized_static_libraries.
1795// See build/make/core/binary.mk for more details.
1796func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
Cole Faust18994c72023-02-28 16:02:16 -08001797 for _, image := range android.SortedKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001798 archMap := s.libsMap[ImageVariantType(image)]
Cole Faust18994c72023-02-28 16:02:16 -08001799 for _, arch := range android.SortedKeys(archMap) {
Inseob Kim74d25562020-08-04 00:41:38 +09001800 libs := archMap[arch]
1801 sort.Strings(libs)
1802
1803 key := fmt.Sprintf(
1804 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1805 s.sanitizerType.variationName(),
1806 image, // already upper
1807 arch)
1808
1809 ctx.Strict(key, strings.Join(libs, " "))
1810 }
1811 }
1812}
1813
Colin Cross571cccf2019-02-04 11:22:08 -08001814var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1815
Inseob Kim74d25562020-08-04 00:41:38 +09001816func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001817 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001818 return newSanitizerStaticLibsMap(cfi)
1819 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001820}
1821
Colin Cross571cccf2019-02-04 11:22:08 -08001822var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1823
Inseob Kim74d25562020-08-04 00:41:38 +09001824func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001825 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001826 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001827 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001828}
1829
Ivan Lozano30c5db22018-02-21 15:49:20 -08001830func enableMinimalRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001831 if sanitize.isSanitizerEnabled(Asan) {
1832 return false
1833 } else if sanitize.isSanitizerEnabled(Hwasan) {
1834 return false
1835 } else if sanitize.isSanitizerEnabled(Fuzzer) {
1836 return false
Ivan Lozano30c5db22018-02-21 15:49:20 -08001837 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001838
1839 if enableUbsanRuntime(sanitize) {
1840 return false
1841 }
1842
1843 sanitizeProps := &sanitize.Properties.SanitizeMutated
1844 if Bool(sanitizeProps.Diag.Cfi) {
1845 return false
1846 }
1847
1848 return Bool(sanitizeProps.Integer_overflow) ||
1849 len(sanitizeProps.Misc_undefined) > 0 ||
1850 Bool(sanitizeProps.Undefined) ||
1851 Bool(sanitizeProps.All_undefined)
Ivan Lozano30c5db22018-02-21 15:49:20 -08001852}
1853
Ivan Lozanod7586b62021-04-01 09:49:36 -04001854func (m *Module) UbsanRuntimeNeeded() bool {
1855 return enableUbsanRuntime(m.sanitize)
1856}
1857
1858func (m *Module) MinimalRuntimeNeeded() bool {
1859 return enableMinimalRuntime(m.sanitize)
1860}
1861
Inseob Kim8471cda2019-11-15 09:59:12 +09001862func enableUbsanRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001863 sanitizeProps := &sanitize.Properties.SanitizeMutated
1864 return Bool(sanitizeProps.Diag.Integer_overflow) ||
1865 Bool(sanitizeProps.Diag.Undefined) ||
1866 len(sanitizeProps.Diag.Misc_undefined) > 0
Inseob Kim8471cda2019-11-15 09:59:12 +09001867}
1868
Vishwath Mohane7128792017-11-17 11:08:10 -08001869func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001870 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001871}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001872
1873func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001874 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001875}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +00001876
1877func BazelCcSanitizerToolchainVars(config android.Config) string {
1878 return android.BazelToolchainVars(config, exportedVars)
1879}