blob: d391cf50d4690d48dcce2e32e3a7eee155ec1a81 [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
Yi Kong286abc62021-11-04 16:14:14 +080041 hwasanCflags = []string{
42 "-fno-omit-frame-pointer",
43 "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000044 "-fsanitize-hwaddress-abi=platform",
Florian Mayer0b981f52022-02-16 23:46:53 +000045 "-mllvm", "-hwasan-use-after-scope=1",
Yi Kong286abc62021-11-04 16:14:14 +080046 }
47
48 // ThinLTO performs codegen during link time, thus these flags need to
49 // passed to both CFLAGS and LDFLAGS.
50 hwasanCommonflags = []string{
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080051 // The following improves debug location information
52 // availability at the cost of its accuracy. It increases
53 // the likelihood of a stack variable's frame offset
54 // to be recorded in the debug info, which is important
55 // for the quality of hwasan reports. The downside is a
56 // higher number of "optimized out" stack variables.
57 // b/112437883.
Yi Kong286abc62021-11-04 16:14:14 +080058 "-instcombine-lower-dbg-declare=0",
Mitch Phillipsb1c574f2020-06-22 13:28:23 -070059 // TODO(b/159343917): HWASan and GlobalISel don't play nicely, and
60 // GlobalISel is the default at -O0 on aarch64.
Yi Kong286abc62021-11-04 16:14:14 +080061 "--aarch64-enable-global-isel-at-O=-1",
62 "-fast-isel=false",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080063 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070064
Trevor Radcliffeded095c2023-06-12 19:18:28 +000065 sanitizeIgnorelistPrefix = "-fsanitize-ignorelist="
66
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000067 cfiBlocklistPath = "external/compiler-rt/lib/cfi"
68 cfiBlocklistFilename = "cfi_blocklist.txt"
Trevor Radcliffef1836e42023-06-01 21:12:08 +000069 cfiEnableFlag = "-fsanitize=cfi"
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +000070 cfiCrossDsoFlag = "-fsanitize-cfi-cross-dso"
71 cfiCflags = []string{"-flto", cfiCrossDsoFlag,
Trevor Radcliffeded095c2023-06-12 19:18:28 +000072 sanitizeIgnorelistPrefix + cfiBlocklistPath + "/" + cfiBlocklistFilename}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070073 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
74 // used, but have no effect on assembly files
75 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Trevor Radcliffef1836e42023-06-01 21:12:08 +000076 cfiLdflags = []string{"-flto", cfiCrossDsoFlag, cfiEnableFlag,
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070077 "-Wl,-plugin-opt,O1"}
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000078 cfiExportsMapPath = "build/soong/cc/config"
79 cfiExportsMapFilename = "cfi_exports.map"
80 cfiAssemblySupportFlag = "-fno-sanitize-cfi-canonical-jump-tables"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070081
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070082 intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080083
Peter Collingbournebd19db02019-03-06 10:38:48 -080084 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070085 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070086 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
Christopher Ferris2fc8e032023-01-26 14:19:27 -080087 "export_memory_stats=0", "max_malloc_fill_size=131072", "malloc_fill_byte=0"}
Florian Mayer1866bbe2023-03-11 01:07:40 +000088 memtagStackCommonFlags = []string{"-march=armv8-a+memtag", "-mllvm", "-dom-tree-reachability-max-bbs-to-explore=128"}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +000089
90 hostOnlySanitizeFlags = []string{"-fno-sanitize-recover=all"}
91 deviceOnlySanitizeFlags = []string{"-fsanitize-trap=all", "-ftrap-function=abort"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070092)
93
Ivan Lozano3968d8f2020-12-14 11:27:52 -050094type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080095
Colin Cross16b23492016-01-06 14:41:07 -080096const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050097 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070098 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080099 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700100 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800101 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500102 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -0400103 Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +0000104 Memtag_stack
Liz Kammer75db9312021-07-07 16:41:50 -0400105 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -0800106)
107
Liz Kammer75db9312021-07-07 16:41:50 -0400108var Sanitizers = []SanitizerType{
109 Asan,
110 Hwasan,
111 tsan,
112 intOverflow,
113 scs,
114 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400115 Memtag_heap,
Florian Mayerd8434a42022-08-31 20:57:03 +0000116 Memtag_stack,
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"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800133 case scs:
134 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"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500139 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700140 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800141 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500142 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800143 }
144}
145
Jiyong Park82226632019-02-01 10:50:50 +0900146// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500147func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900148 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500149 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900150 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700151 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900152 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400153 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700154 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000155 case Memtag_stack:
156 return "memtag_stack"
Jiyong Park82226632019-02-01 10:50:50 +0900157 case tsan:
158 return "thread"
159 case intOverflow:
160 return "integer_overflow"
161 case cfi:
162 return "cfi"
163 case scs:
164 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500165 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700166 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900167 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500168 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900169 }
170}
171
Liz Kammer75db9312021-07-07 16:41:50 -0400172func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
173 switch t {
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200174 case cfi, Hwasan, Asan, tsan, Fuzzer, scs:
175 sanitizer := &sanitizerSplitMutator{t}
176 ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
177 ctx.Transition(t.variationName(), sanitizer)
Florian Mayerd8434a42022-08-31 20:57:03 +0000178 case Memtag_heap, Memtag_stack, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400179 // do nothing
180 default:
181 panic(fmt.Errorf("unknown SanitizerType %d", t))
182 }
183}
184
Liz Kammerfd8a49f2022-10-31 10:31:11 -0400185// shouldPropagateToSharedLibraryDeps returns whether a sanitizer type should propagate to share
186// dependencies. In most cases, sanitizers only propagate to static dependencies; however, some
187// sanitizers also must be enabled for shared libraries for linking.
188func (t SanitizerType) shouldPropagateToSharedLibraryDeps() bool {
189 switch t {
190 case Fuzzer:
191 // Typically, shared libs are not split. However, for fuzzer, we split even for shared libs
192 // because a library sanitized for fuzzer can't be linked from a library that isn't sanitized
193 // for fuzzer.
194 return true
195 default:
196 return false
197 }
198}
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500199func (*Module) SanitizerSupported(t SanitizerType) bool {
200 switch t {
201 case Asan:
202 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700203 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500204 return true
205 case tsan:
206 return true
207 case intOverflow:
208 return true
209 case cfi:
210 return true
211 case scs:
212 return true
213 case Fuzzer:
214 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400215 case Memtag_heap:
216 return true
Florian Mayerd8434a42022-08-31 20:57:03 +0000217 case Memtag_stack:
218 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500219 default:
220 return false
221 }
222}
223
224// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
225func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700226 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900227}
228
Martin Stjernholmb0249572020-09-15 02:32:35 +0100229type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400230 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100231 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800232
Liz Kammer75b9b402021-06-25 15:19:27 -0400233 // ASan (Address sanitizer), incompatible with static binaries.
234 // Always runs in a diagnostic mode.
235 // Use of address sanitizer disables cfi sanitizer.
236 // Hwaddress sanitizer takes precedence over this sanitizer.
237 Address *bool `android:"arch_variant"`
238 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
239 // Always runs in a diagnostic mode.
240 // Use of thread sanitizer disables cfi and scudo sanitizers.
241 // Hwaddress sanitizer takes precedence over this sanitizer.
242 Thread *bool `android:"arch_variant"`
243 // HWASan (Hardware Address sanitizer).
244 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100245 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800246
Liz Kammer75b9b402021-06-25 15:19:27 -0400247 // Undefined behavior sanitizer
248 All_undefined *bool `android:"arch_variant"`
249 // Subset of undefined behavior sanitizer
250 Undefined *bool `android:"arch_variant"`
251 // List of specific undefined behavior sanitizers to enable
252 Misc_undefined []string `android:"arch_variant"`
253 // Fuzzer, incompatible with static binaries.
254 Fuzzer *bool `android:"arch_variant"`
255 // safe-stack sanitizer, incompatible with 32-bit architectures.
256 Safestack *bool `android:"arch_variant"`
257 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
258 Cfi *bool `android:"arch_variant"`
259 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
260 Integer_overflow *bool `android:"arch_variant"`
261 // scudo sanitizer, incompatible with asan, hwasan, tsan
262 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
263 // deprecated
264 Scudo *bool `android:"arch_variant"`
Elliott Hughese4793bc2023-02-09 21:15:47 +0000265 // shadow-call-stack sanitizer, only available on arm64/riscv64.
Liz Kammer75b9b402021-06-25 15:19:27 -0400266 Scs *bool `android:"arch_variant"`
267 // Memory-tagging, only available on arm64
268 // if diag.memtag unset or false, enables async memory tagging
Florian Mayer00ab5cf2022-08-31 18:30:18 +0000269 Memtag_heap *bool `android:"arch_variant"`
Florian Mayerd8434a42022-08-31 20:57:03 +0000270 // Memory-tagging stack instrumentation, only available on arm64
271 // Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
272 Memtag_stack *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100273
274 // A modifier for ASAN and HWASAN for write only instrumentation
275 Writeonly *bool `android:"arch_variant"`
276
277 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
278 // Replaces abort() on error with a human-readable error message.
279 // Address and Thread sanitizers always run in diagnostic mode.
280 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400281 // Undefined behavior sanitizer, diagnostic mode
282 Undefined *bool `android:"arch_variant"`
283 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
284 Cfi *bool `android:"arch_variant"`
285 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
286 Integer_overflow *bool `android:"arch_variant"`
287 // Memory-tagging, only available on arm64
288 // requires sanitizer.memtag: true
289 // if set, enables sync memory tagging
290 Memtag_heap *bool `android:"arch_variant"`
291 // List of specific undefined behavior sanitizers to enable in diagnostic mode
292 Misc_undefined []string `android:"arch_variant"`
293 // List of sanitizers to pass to -fno-sanitize-recover
294 // results in only the first detected error for these sanitizers being reported and program then
295 // exits with a non-zero exit code.
296 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800297 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800298
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800299 // Sanitizers to run with flag configuration specified
300 Config struct {
301 // Enables CFI support flags for assembly-heavy libraries
302 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800303 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800304
Liz Kammer75b9b402021-06-25 15:19:27 -0400305 // List of sanitizers to pass to -fsanitize-recover
306 // allows execution to continue for these sanitizers to detect multiple errors rather than only
307 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100308 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000309
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700310 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100311 Blocklist *string
312}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700313
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400314type sanitizeMutatedProperties struct {
315 // Whether sanitizers can be enabled on this module
316 Never *bool `blueprint:"mutated"`
317
318 // Whether ASan (Address sanitizer) is enabled for this module.
319 // Hwaddress sanitizer takes precedence over this sanitizer.
320 Address *bool `blueprint:"mutated"`
321 // Whether TSan (Thread sanitizer) is enabled for this module
322 Thread *bool `blueprint:"mutated"`
323 // Whether HWASan (Hardware Address sanitizer) is enabled for this module
324 Hwaddress *bool `blueprint:"mutated"`
325
326 // Whether Undefined behavior sanitizer is enabled for this module
327 All_undefined *bool `blueprint:"mutated"`
328 // Whether undefined behavior sanitizer subset is enabled for this module
329 Undefined *bool `blueprint:"mutated"`
330 // List of specific undefined behavior sanitizers enabled for this module
331 Misc_undefined []string `blueprint:"mutated"`
332 // Whether Fuzzeris enabled for this module
333 Fuzzer *bool `blueprint:"mutated"`
334 // whether safe-stack sanitizer is enabled for this module
335 Safestack *bool `blueprint:"mutated"`
336 // Whether cfi sanitizer is enabled for this module
337 Cfi *bool `blueprint:"mutated"`
338 // Whether signed/unsigned integer overflow sanitizer is enabled for this module
339 Integer_overflow *bool `blueprint:"mutated"`
340 // Whether scudo sanitizer is enabled for this module
341 Scudo *bool `blueprint:"mutated"`
342 // Whether shadow-call-stack sanitizer is enabled for this module.
343 Scs *bool `blueprint:"mutated"`
344 // Whether Memory-tagging is enabled for this module
345 Memtag_heap *bool `blueprint:"mutated"`
346 // Whether Memory-tagging stack instrumentation is enabled for this module
347 Memtag_stack *bool `blueprint:"mutated"`
348
349 // Whether a modifier for ASAN and HWASAN for write only instrumentation is enabled for this
350 // module
351 Writeonly *bool `blueprint:"mutated"`
352
353 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
354 Diag struct {
355 // Whether Undefined behavior sanitizer, diagnostic mode is enabled for this module
356 Undefined *bool `blueprint:"mutated"`
357 // Whether cfi sanitizer, diagnostic mode is enabled for this module
358 Cfi *bool `blueprint:"mutated"`
359 // Whether signed/unsigned integer overflow sanitizer, diagnostic mode is enabled for this
360 // module
361 Integer_overflow *bool `blueprint:"mutated"`
362 // Whether Memory-tagging, diagnostic mode is enabled for this module
363 Memtag_heap *bool `blueprint:"mutated"`
364 // List of specific undefined behavior sanitizers enabled in diagnostic mode
365 Misc_undefined []string `blueprint:"mutated"`
366 } `blueprint:"mutated"`
367}
368
Martin Stjernholmb0249572020-09-15 02:32:35 +0100369type SanitizeProperties struct {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400370 Sanitize SanitizeUserProps `android:"arch_variant"`
371 SanitizeMutated sanitizeMutatedProperties `blueprint:"mutated"`
372
373 SanitizerEnabled bool `blueprint:"mutated"`
374 MinimalRuntimeDep bool `blueprint:"mutated"`
375 BuiltinsDep bool `blueprint:"mutated"`
376 UbsanRuntimeDep bool `blueprint:"mutated"`
377 InSanitizerDir bool `blueprint:"mutated"`
378 Sanitizers []string `blueprint:"mutated"`
379 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800380}
381
382type sanitize struct {
383 Properties SanitizeProperties
384}
385
Cindy Zhou18417cb2020-12-10 07:12:38 -0800386// Mark this tag with a check to see if apex dependency check should be skipped
387func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
388 return t.skipApexAllowedDependenciesCheck
389}
390
391var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
392
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000393var exportedVars = android.NewExportedVariables(pctx)
394
Vishwath Mohane7128792017-11-17 11:08:10 -0800395func init() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000396 exportedVars.ExportStringListStaticVariable("HostOnlySanitizeFlags", hostOnlySanitizeFlags)
397 exportedVars.ExportStringList("DeviceOnlySanitizeFlags", deviceOnlySanitizeFlags)
398
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000399 // Leave out "-flto" from the slices exported to bazel, as we will use the
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000400 // dedicated LTO feature for this. For C Flags and Linker Flags, also leave
Trevor Radcliffef1836e42023-06-01 21:12:08 +0000401 // out the cross DSO flag which will be added separately under the correct conditions.
402 exportedVars.ExportStringList("CfiCFlags", append(cfiCflags[2:], cfiEnableFlag))
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000403 exportedVars.ExportStringList("CfiLdFlags", cfiLdflags[2:])
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000404 exportedVars.ExportStringList("CfiAsFlags", cfiAsflags[1:])
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000405
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000406 exportedVars.ExportString("SanitizeIgnorelistPrefix", sanitizeIgnorelistPrefix)
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000407 exportedVars.ExportString("CfiCrossDsoFlag", cfiCrossDsoFlag)
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000408 exportedVars.ExportString("CfiBlocklistPath", cfiBlocklistPath)
409 exportedVars.ExportString("CfiBlocklistFilename", cfiBlocklistFilename)
410 exportedVars.ExportString("CfiExportsMapPath", cfiExportsMapPath)
411 exportedVars.ExportString("CfiExportsMapFilename", cfiExportsMapFilename)
412 exportedVars.ExportString("CfiAssemblySupportFlag", cfiAssemblySupportFlag)
413
Vishwath Mohane7128792017-11-17 11:08:10 -0800414 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700415 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800416}
417
Colin Cross16b23492016-01-06 14:41:07 -0800418func (sanitize *sanitize) props() []interface{} {
419 return []interface{}{&sanitize.Properties}
420}
421
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400422func (p *sanitizeMutatedProperties) copyUserPropertiesToMutated(userProps *SanitizeUserProps) {
423 p.Never = userProps.Never
424 p.Address = userProps.Address
425 p.All_undefined = userProps.All_undefined
426 p.Cfi = userProps.Cfi
427 p.Fuzzer = userProps.Fuzzer
428 p.Hwaddress = userProps.Hwaddress
429 p.Integer_overflow = userProps.Integer_overflow
430 p.Memtag_heap = userProps.Memtag_heap
431 p.Memtag_stack = userProps.Memtag_stack
432 p.Safestack = userProps.Safestack
433 p.Scs = userProps.Scs
434 p.Scudo = userProps.Scudo
435 p.Thread = userProps.Thread
436 p.Undefined = userProps.Undefined
437 p.Writeonly = userProps.Writeonly
438
439 p.Misc_undefined = make([]string, 0, len(userProps.Misc_undefined))
440 for _, v := range userProps.Misc_undefined {
441 p.Misc_undefined = append(p.Misc_undefined, v)
442 }
443
444 p.Diag.Cfi = userProps.Diag.Cfi
445 p.Diag.Integer_overflow = userProps.Diag.Integer_overflow
446 p.Diag.Memtag_heap = userProps.Diag.Memtag_heap
447 p.Diag.Undefined = userProps.Diag.Undefined
448
449 p.Diag.Misc_undefined = make([]string, 0, len(userProps.Diag.Misc_undefined))
450 for _, v := range userProps.Diag.Misc_undefined {
451 p.Diag.Misc_undefined = append(p.Diag.Misc_undefined, v)
452 }
453}
454
Colin Cross16b23492016-01-06 14:41:07 -0800455func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400456 s := &sanitize.Properties.SanitizeMutated
457 s.copyUserPropertiesToMutated(&sanitize.Properties.Sanitize)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700458
Colin Cross16b23492016-01-06 14:41:07 -0800459 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700460 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800461 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800462 }
463
464 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800465 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800466 return
467 }
468
Florian Mayerd8434a42022-08-31 20:57:03 +0000469 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400470 if ctx.testBinary() {
471 if s.Memtag_heap == nil {
472 s.Memtag_heap = proptools.BoolPtr(true)
473 }
474 if s.Diag.Memtag_heap == nil {
475 s.Diag.Memtag_heap = proptools.BoolPtr(true)
476 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800477 }
478
Colin Cross16b23492016-01-06 14:41:07 -0800479 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700480 var globalSanitizersDiag []string
481
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700482 if ctx.Host() {
483 if !ctx.Windows() {
484 globalSanitizers = ctx.Config().SanitizeHost()
485 }
486 } else {
487 arches := ctx.Config().SanitizeDeviceArch()
488 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
489 globalSanitizers = ctx.Config().SanitizeDevice()
490 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800491 }
492 }
493
Colin Cross16b23492016-01-06 14:41:07 -0800494 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000495 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700496 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400497 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000498 }
Colin Cross16b23492016-01-06 14:41:07 -0800499
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700500 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400501 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000502 }
503
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700504 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400505 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000506 }
507
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700508 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400509 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000510 }
511
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700512 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400513 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700514 }
515
516 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400517 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000518 }
519
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700520 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800521 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400522 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700523 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700524 }
525
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700526 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700527 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700528 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400529 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700530 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700531 }
532
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700533 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400534 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700535 }
536
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700537 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400538 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700539 }
540
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000541 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
542 // Hwaddress and Address are set before, so we can check them here
543 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
544 if s.Address == nil && s.Hwaddress == nil {
545 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
546 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400547 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000548 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700549 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800550 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400551 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800552 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700553 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000554
Florian Mayerd8434a42022-08-31 20:57:03 +0000555 if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
556 s.Memtag_stack = proptools.BoolPtr(true)
557 }
558
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000559 if len(globalSanitizers) > 0 {
560 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
561 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700562
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700563 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700564 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700565 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400566 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700567 }
568
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700569 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
570 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400571 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700572 }
573
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800574 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
575 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400576 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800577 }
578
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700579 if len(globalSanitizersDiag) > 0 {
580 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
581 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700582 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700583
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800584 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross88a029f2022-06-23 14:51:20 -0700585 if ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800586 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800587 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400588 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800589 }
590 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400591 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800592 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800593 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800594 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400595 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800596 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800597 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700598 }
599
Hang Lua98aab92023-03-17 13:17:22 +0800600 // Enable HWASan for all components in the include paths (for Aarch64 only)
601 if s.Hwaddress == nil && ctx.Config().HWASanEnabledForPath(ctx.ModuleDir()) &&
602 ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
603 s.Hwaddress = proptools.BoolPtr(true)
604 }
605
Elvis Chien9c993542021-06-25 01:15:17 +0800606 // Enable CFI for non-host components in the include paths
607 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400608 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000609 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400610 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700611 }
612 }
613
Elliott Hughesda3a0712020-03-06 16:55:28 -0800614 // Is CFI actually enabled?
615 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400616 s.Cfi = nil
617 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800618 }
619
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700620 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross88a029f2022-06-23 14:51:20 -0700621 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700622 s.Hwaddress = nil
623 }
624
Elliott Hughese4793bc2023-02-09 21:15:47 +0000625 // SCS is only implemented on AArch64/riscv64.
626 if (ctx.Arch().ArchType != android.Arm64 && ctx.Arch().ArchType != android.Riscv64) || !ctx.toolchain().Bionic() {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800627 s.Scs = nil
628 }
Elliott Hughes5beb42f2023-04-12 13:08:58 -0700629 // ...but temporarily globally disabled on riscv64 (http://b/277909695).
630 if ctx.Arch().ArchType == android.Riscv64 {
631 s.Scs = nil
632 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800633
Ivan Lozano62cd0382021-11-01 10:27:54 -0400634 // Memtag_heap is only implemented on AArch64.
Florian Mayerd8434a42022-08-31 20:57:03 +0000635 // Memtag ABI is Android specific for now, so disable for host.
636 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() || ctx.Host() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700637 s.Memtag_heap = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000638 s.Memtag_stack = nil
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700639 }
640
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700641 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700642 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400643 s.Cfi = nil
644 s.Diag.Cfi = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000645 // HWASAN and ASAN win against MTE.
646 s.Memtag_heap = nil
647 s.Memtag_stack = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700648 }
649
Colin Crossed12a042022-02-07 13:55:55 -0800650 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
651 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400652 s.Cfi = nil
653 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700654 s.Misc_undefined = nil
655 s.Undefined = nil
656 s.All_undefined = nil
657 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800658 }
659
Aditya Kumar1281b992023-05-16 01:15:24 +0000660 // TODO(b/254713216): CFI doesn't work for riscv64 yet because LTO doesn't work.
661 if ctx.Arch().ArchType == android.Riscv64 {
662 s.Cfi = nil
663 s.Diag.Cfi = nil
664 }
665
Colin Crossed12a042022-02-07 13:55:55 -0800666 // Disable CFI for musl
667 if ctx.toolchain().Musl() {
668 s.Cfi = nil
669 s.Diag.Cfi = nil
670 }
671
Colin Cross390fc742023-05-02 13:02:51 -0700672 // TODO(b/280478629): runtimes don't exist for musl arm64 yet.
673 if ctx.toolchain().Musl() && ctx.Arch().ArchType == android.Arm64 {
674 s.Address = nil
675 s.Hwaddress = nil
676 s.Thread = nil
677 s.Scudo = nil
678 s.Fuzzer = nil
679 s.Cfi = nil
680 s.Diag.Cfi = nil
681 s.Misc_undefined = nil
682 s.Undefined = nil
683 s.All_undefined = nil
684 s.Integer_overflow = nil
685 }
686
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700687 // Also disable CFI for VNDK variants of components
688 if ctx.isVndk() && ctx.useVndk() {
Justin Yun08270c62022-12-19 17:01:26 +0900689 s.Cfi = nil
690 s.Diag.Cfi = nil
Inseob Kimeec88e12020-01-22 11:11:29 +0900691 }
692
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700693 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700694 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
695 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700696 s.Hwaddress = nil
697 }
698
Colin Cross3c344ef2016-07-18 15:44:56 -0700699 if ctx.staticBinary() {
700 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700701 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700702 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800703 }
704
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700705 if Bool(s.All_undefined) {
706 s.Undefined = nil
707 }
708
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700709 if !ctx.toolchain().Is64Bit() {
710 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700711 s.Thread = nil
712 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800713 // TODO(ccross): error for compile_multilib = "32"?
714 }
715
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800716 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 -0700717 Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Florian Mayerd8434a42022-08-31 20:57:03 +0000718 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700719 sanitize.Properties.SanitizerEnabled = true
720 }
721
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800722 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
723 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700724 s.Scudo = nil
725 }
726
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700727 if Bool(s.Hwaddress) {
728 s.Address = nil
729 s.Thread = nil
730 }
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000731
732 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
733 // mutually incompatible.
734 if Bool(s.Fuzzer) {
735 s.Cfi = nil
736 }
Colin Cross16b23492016-01-06 14:41:07 -0800737}
738
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800739func toDisableImplicitIntegerChange(flags []string) bool {
740 // Returns true if any flag is fsanitize*integer, and there is
741 // no explicit flag about sanitize=implicit-integer-sign-change.
742 for _, f := range flags {
743 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
744 return false
745 }
746 }
747 for _, f := range flags {
748 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
749 return true
750 }
751 }
752 return false
753}
754
Yabin Cuidb7dda82020-11-30 15:47:45 -0800755func toDisableUnsignedShiftBaseChange(flags []string) bool {
756 // Returns true if any flag is fsanitize*integer, and there is
757 // no explicit flag about sanitize=unsigned-shift-base.
758 for _, f := range flags {
759 if strings.Contains(f, "sanitize=unsigned-shift-base") {
760 return false
761 }
762 }
763 for _, f := range flags {
764 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
765 return true
766 }
767 }
768 return false
769}
770
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400771func (s *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
772 if !s.Properties.SanitizerEnabled && !s.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800773 return flags
774 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400775 sanProps := &s.Properties.SanitizeMutated
Colin Cross16b23492016-01-06 14:41:07 -0800776
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400777 if Bool(sanProps.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700778 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800779 // Frame pointer based unwinder in ASan requires ARM frame setup.
780 // TODO: put in flags?
781 flags.RequiredInstructionSet = "arm"
782 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800783 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
784 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800785
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400786 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000787 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
788 }
789
Colin Cross16b23492016-01-06 14:41:07 -0800790 if ctx.Host() {
791 // -nodefaultlibs (provided with libc++) prevents the driver from linking
792 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800793 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800794 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800795 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900796 if ctx.bootstrap() {
797 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
798 } else {
799 flags.DynamicLinker = "/system/bin/linker_asan"
800 }
Colin Cross16b23492016-01-06 14:41:07 -0800801 if flags.Toolchain.Is64Bit() {
802 flags.DynamicLinker += "64"
803 }
804 }
Colin Cross16b23492016-01-06 14:41:07 -0800805 }
806
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400807 if Bool(sanProps.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800808 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800809
810 for _, flag := range hwasanCommonflags {
811 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
812 }
813 for _, flag := range hwasanCommonflags {
814 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
815 }
816
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400817 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000818 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
819 }
Florian Mayer95cd6db2023-03-23 17:48:07 -0700820 if !ctx.staticBinary() && !ctx.Host() {
821 if ctx.bootstrap() {
822 flags.DynamicLinker = "/system/bin/bootstrap/linker_hwasan64"
823 } else {
824 flags.DynamicLinker = "/system/bin/linker_hwasan64"
825 }
826 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700827 }
828
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400829 if Bool(sanProps.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800830 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700831
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000832 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
833 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
834 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
835 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
836 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
837
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700838 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
839 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
840 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800841 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
842 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700843
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700844 // Disable fortify for fuzzing builds. Generally, we'll be building with
845 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800846 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800847
848 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
849 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
850 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
851 // the DT_RUNPATH from the shared library above it, and not the executable,
852 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
853 // DT_RUNPATH here means that transient shared libraries can be found
854 // colocated with their parents.
855 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800856 }
857
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400858 if Bool(sanProps.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800859 if ctx.Arch().ArchType == android.Arm {
860 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
861 // to do this on a function basis, so force Thumb on the entire module.
862 flags.RequiredInstructionSet = "thumb"
863 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000864
Colin Cross4af21ed2019-11-04 09:37:55 -0800865 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
866 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400867 if Bool(s.Properties.Sanitize.Config.Cfi_assembly_support) {
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000868 flags.Local.CFlags = append(flags.Local.CFlags, cfiAssemblySupportFlag)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800869 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000870 // Only append the default visibility flag if -fvisibility has not already been set
871 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800872 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
873 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000874 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800875 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000876
877 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800878 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
879 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000880 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700881 }
882
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400883 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000884 flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
Florian Mayer25cd9812023-03-21 16:13:36 +0000885 // TODO(fmayer): remove -Wno-error once https://reviews.llvm.org/D127917 is in Android toolchain.
886 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-error=frame-larger-than")
Florian Mayerd8434a42022-08-31 20:57:03 +0000887 flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
888 flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
Florian Mayer25cd9812023-03-21 16:13:36 +0000889 // This works around LLD complaining about the stack frame size.
890 // TODO(fmayer): remove once https://reviews.llvm.org/D127917 is in Android toolchain.
891 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-fatal-warnings")
Florian Mayerd8434a42022-08-31 20:57:03 +0000892 }
893
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400894 if (Bool(sanProps.Memtag_heap) || Bool(sanProps.Memtag_stack)) && ctx.binary() {
895 if Bool(sanProps.Diag.Memtag_heap) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000896 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
897 } else {
898 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
899 }
900 }
901
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400902 if Bool(sanProps.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800903 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700904 }
905
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400906 if len(s.Properties.Sanitizers) > 0 {
907 sanitizeArg := "-fsanitize=" + strings.Join(s.Properties.Sanitizers, ",")
Colin Cross4af21ed2019-11-04 09:37:55 -0800908 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
909 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross234b01d2022-02-07 13:49:03 -0800910 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
911
Colin Crossed12a042022-02-07 13:55:55 -0800912 if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
913 // Bionic and musl sanitizer runtimes have already been added as dependencies so that
914 // the right variant of the runtime will be used (with the "-android" or "-musl"
915 // suffixes), so don't let clang the runtime library.
Colin Cross234b01d2022-02-07 13:49:03 -0800916 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-link-runtime")
917 } else {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800918 // Host sanitizers only link symbols in the final executable, so
919 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800920 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
Colin Cross6c18d002022-06-02 15:11:50 -0700921 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500922
Colin Cross6c18d002022-06-02 15:11:50 -0700923 if !ctx.toolchain().Bionic() {
924 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function san.
925 // Musl toolchain prebuilts have vptr and function sanitizers, but enabling them
926 // implicitly enables RTTI which causes RTTI mismatch issues with dependencies.
927
Colin Cross234b01d2022-02-07 13:49:03 -0800928 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500929 }
930
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400931 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700932 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800933 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700934 } else if ctx.Host() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000935 flags.Local.CFlags = append(flags.Local.CFlags, hostOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700936 } else {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000937 flags.Local.CFlags = append(flags.Local.CFlags, deviceOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700938 }
Evgenii Stepanov59012812022-06-24 11:09:18 -0700939
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400940 if enableMinimalRuntime(s) {
Evgenii Stepanov59012812022-06-24 11:09:18 -0700941 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
942 }
943
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800944 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800945 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
946 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800947 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800948 // http://b/171275751, Android doesn't build with this sanitizer yet.
949 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
950 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
951 }
Colin Cross16b23492016-01-06 14:41:07 -0800952 }
953
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400954 if len(s.Properties.DiagSanitizers) > 0 {
955 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(s.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700956 }
957 // FIXME: enable RTTI if diag + (cfi or vptr)
958
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400959 if s.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800960 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400961 strings.Join(s.Properties.Sanitize.Recover, ","))
Andreas Gampe97071162017-05-08 13:15:23 -0700962 }
963
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400964 if s.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800965 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400966 strings.Join(s.Properties.Sanitize.Diag.No_recover, ","))
Ivan Lozano7929bba2018-12-12 09:36:31 -0800967 }
968
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400969 blocklist := android.OptionalPathForModuleSrc(ctx, s.Properties.Sanitize.Blocklist)
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700970 if blocklist.Valid() {
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000971 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeIgnorelistPrefix+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700972 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
973 }
974
Colin Cross16b23492016-01-06 14:41:07 -0800975 return flags
976}
977
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400978func (s *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900979 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
980 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800981 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400982 if Bool(s.Properties.SanitizeMutated.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800983 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900984 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400985 if Bool(s.Properties.SanitizeMutated.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800986 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900987 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400988 if Bool(s.Properties.SanitizeMutated.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800989 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900990 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800991 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700992}
993
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400994func (s *sanitize) inSanitizerDir() bool {
995 return s.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700996}
997
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500998// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400999func (s *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +00001000 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001001 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001002 return s.Properties.SanitizeMutated.Address
Tri Vo6eafc362021-04-01 11:29:09 -07001003 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001004 return s.Properties.SanitizeMutated.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +00001005 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001006 return s.Properties.SanitizeMutated.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +00001007 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001008 return s.Properties.SanitizeMutated.Integer_overflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001009 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001010 return s.Properties.SanitizeMutated.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001011 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001012 return s.Properties.SanitizeMutated.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -04001013 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001014 return s.Properties.SanitizeMutated.Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +00001015 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001016 return s.Properties.SanitizeMutated.Memtag_stack
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001017 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001018 return s.Properties.SanitizeMutated.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +00001019 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001020 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +00001021 }
1022}
1023
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001024// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -08001025func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001026 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001027 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -08001028 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001029 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001030 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -04001031 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Florian Mayerd8434a42022-08-31 20:57:03 +00001032 !sanitize.isSanitizerEnabled(Memtag_stack) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001033 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -08001034}
1035
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001036// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001037func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001038 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001039 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001040 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001041 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001042}
1043
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001044func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -04001045 bPtr := proptools.BoolPtr(b)
1046 if !b {
1047 bPtr = nil
1048 }
Colin Cross16b23492016-01-06 14:41:07 -08001049 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001050 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001051 sanitize.Properties.SanitizeMutated.Address = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001052 // For ASAN variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001053 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Tri Vo6eafc362021-04-01 11:29:09 -07001054 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001055 sanitize.Properties.SanitizeMutated.Hwaddress = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001056 // For HWAsan variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001057 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Colin Cross16b23492016-01-06 14:41:07 -08001058 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001059 sanitize.Properties.SanitizeMutated.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -07001060 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001061 sanitize.Properties.SanitizeMutated.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001062 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001063 sanitize.Properties.SanitizeMutated.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001064 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001065 sanitize.Properties.SanitizeMutated.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -04001066 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001067 sanitize.Properties.SanitizeMutated.Memtag_heap = bPtr
Florian Mayerd8434a42022-08-31 20:57:03 +00001068 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001069 sanitize.Properties.SanitizeMutated.Memtag_stack = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001070 // We do not need to disable ASAN or HWASan here, as there is no Memtag_stack variant.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001071 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001072 sanitize.Properties.SanitizeMutated.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -08001073 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001074 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -08001075 }
1076 if b {
1077 sanitize.Properties.SanitizerEnabled = true
1078 }
1079}
1080
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001081// Check if the sanitizer is explicitly disabled (as opposed to nil by
1082// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001083func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001084 if sanitize == nil {
1085 return false
1086 }
1087
1088 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1089 return sanitizerVal != nil && *sanitizerVal == false
1090}
1091
1092// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
1093// because enabling a sanitizer either directly (via the blueprint) or
1094// indirectly (via a mutator) sets the bool ptr to true, and you can't
1095// distinguish between the cases. It isn't needed though - both cases can be
1096// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001097func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001098 if sanitize == nil {
1099 return false
1100 }
1101
1102 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1103 return sanitizerVal != nil && *sanitizerVal == true
1104}
1105
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001106// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
1107func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -07001108 switch t := tag.(type) {
1109 case dependencyTag:
1110 return t == reuseObjTag || t == objDepTag
1111 case libraryDependencyTag:
1112 return true
1113 default:
1114 return false
1115 }
Colin Cross6b753602018-06-21 13:03:07 -07001116}
1117
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001118func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
1119 return IsSanitizableDependencyTag
1120}
1121
Inseob Kimc42f2f22020-07-29 20:32:10 +09001122// Determines if the current module is a static library going to be captured
1123// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
1124// except for ones which explicitly disable cfi.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001125func needsCfiForVendorSnapshot(mctx android.BaseModuleContext) bool {
Justin Yun8814fc52022-12-15 21:45:35 +09001126 if inList("hwaddress", mctx.Config().SanitizeDevice()) {
1127 // cfi will not be built if SANITIZE_TARGET=hwaddress is set
1128 return false
1129 }
1130
Kiyoung Kim48f37782021-07-07 12:42:39 +09001131 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001132 return false
1133 }
1134
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001135 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001136
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001137 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001138 return false
1139 }
1140
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001141 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001142 return false
1143 }
1144
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001145 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001146 return false
1147 }
1148
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001149 if !c.SanitizerSupported(cfi) {
1150 return false
1151 }
1152
1153 return c.SanitizePropDefined() &&
1154 !c.SanitizeNever() &&
1155 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001156}
1157
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001158type sanitizerSplitMutator struct {
1159 sanitizer SanitizerType
1160}
1161
1162// If an APEX is sanitized or not depends on whether it contains at least one
1163// sanitized module. Transition mutators cannot propagate information up the
1164// dependency graph this way, so we need an auxiliary mutator to do so.
1165func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
1166 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1167 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1168 ctx.VisitDirectDeps(func(dep android.Module) {
1169 if c, ok := dep.(*Module); ok && c.sanitize.isSanitizerEnabled(s.sanitizer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001170 enabled = true
Inseob Kimc42f2f22020-07-29 20:32:10 +09001171 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001172 })
1173
1174 if enabled {
1175 sanitizeable.EnableSanitizer(s.sanitizer.name())
1176 }
1177 }
1178}
1179
1180func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
1181 if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1182 if s.sanitizer == cfi && needsCfiForVendorSnapshot(ctx) {
1183 return []string{"", s.sanitizer.variationName()}
1184 }
1185
1186 // If the given sanitizer is not requested in the .bp file for a module, it
1187 // won't automatically build the sanitized variation.
1188 if !c.IsSanitizerEnabled(s.sanitizer) {
1189 return []string{""}
1190 }
1191
1192 if c.Binary() {
1193 // If a sanitizer is enabled for a binary, we do not build the version
1194 // without the sanitizer
1195 return []string{s.sanitizer.variationName()}
1196 } else if c.StaticallyLinked() || c.Header() {
1197 // For static libraries, we build both versions. Some Make modules
1198 // apparently depend on this behavior.
1199 return []string{"", s.sanitizer.variationName()}
1200 } else {
1201 // We only build the requested variation of dynamic libraries
1202 return []string{s.sanitizer.variationName()}
1203 }
1204 }
1205
1206 if _, ok := ctx.Module().(JniSanitizeable); ok {
1207 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1208 // that is short-circuited for now
1209 return []string{""}
1210 }
1211
1212 // If an APEX has a sanitized dependency, we build the APEX in the sanitized
1213 // variation. This is useful because such APEXes require extra dependencies.
1214 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1215 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1216 if enabled {
1217 return []string{s.sanitizer.variationName()}
1218 } else {
1219 return []string{""}
1220 }
1221 }
1222
1223 if c, ok := ctx.Module().(*Module); ok {
1224 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1225
1226 // Check if it's a snapshot module supporting sanitizer
Justin Yun08270c62022-12-19 17:01:26 +09001227 if ss, ok := c.linker.(snapshotSanitizer); ok {
1228 if ss.isSanitizerAvailable(s.sanitizer) {
1229 return []string{"", s.sanitizer.variationName()}
1230 } else {
1231 return []string{""}
1232 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001233 }
1234 }
1235
1236 return []string{""}
1237}
1238
1239func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1240 if c, ok := ctx.Module().(PlatformSanitizeable); ok {
1241 if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
1242 // If the dependency is through a non-sanitizable tag, use the
1243 // non-sanitized variation
1244 return ""
1245 }
1246
1247 return sourceVariation
1248 } else if _, ok := ctx.Module().(JniSanitizeable); ok {
1249 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1250 // that is short-circuited for now
1251 return ""
1252 } else {
1253 // Otherwise, do not rock the boat.
1254 return sourceVariation
1255 }
1256}
1257
1258func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1259 if d, ok := ctx.Module().(PlatformSanitizeable); ok {
1260 if dm, ok := ctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001261 if ss, ok := dm.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001262 return incomingVariation
Inseob Kimc42f2f22020-07-29 20:32:10 +09001263 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001264 }
1265
1266 if !d.SanitizePropDefined() ||
1267 d.SanitizeNever() ||
1268 d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
1269 !d.SanitizerSupported(s.sanitizer) {
1270 // If a module opts out of a sanitizer, use its non-sanitized variation
1271 return ""
1272 }
1273
1274 // Binaries are always built in the variation they requested.
1275 if d.Binary() {
1276 if d.IsSanitizerEnabled(s.sanitizer) {
1277 return s.sanitizer.variationName()
1278 } else {
1279 return ""
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001280 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001281 }
1282
1283 // If a shared library requests to be sanitized, it will be built for that
1284 // sanitizer. Otherwise, some sanitizers propagate through shared library
1285 // dependency edges, some do not.
1286 if !d.StaticallyLinked() && !d.Header() {
1287 if d.IsSanitizerEnabled(s.sanitizer) {
1288 return s.sanitizer.variationName()
1289 }
1290
Liz Kammerfd8a49f2022-10-31 10:31:11 -04001291 // Some sanitizers do not propagate to shared dependencies
1292 if !s.sanitizer.shouldPropagateToSharedLibraryDeps() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001293 return ""
1294 }
1295 }
1296
1297 // Static and header libraries inherit whether they are sanitized from the
1298 // module they are linked into
1299 return incomingVariation
1300 } else if d, ok := ctx.Module().(Sanitizeable); ok {
1301 // If an APEX contains a sanitized module, it will be built in the variation
1302 // corresponding to that sanitizer.
1303 enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1304 if enabled {
1305 return s.sanitizer.variationName()
1306 }
1307
1308 return incomingVariation
1309 }
1310
1311 return ""
1312}
1313
1314func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
1315 sanitizerVariation := variationName == s.sanitizer.variationName()
1316
1317 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1318 sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
1319
1320 oneMakeVariation := false
1321 if c.StaticallyLinked() || c.Header() {
1322 if s.sanitizer != cfi && s.sanitizer != scs && s.sanitizer != Hwasan {
1323 // These sanitizers export only one variation to Make. For the rest,
1324 // Make targets can depend on both the sanitized and non-sanitized
1325 // versions.
1326 oneMakeVariation = true
1327 }
1328 } else if !c.Binary() {
1329 // Shared library. These are the sanitizers that do propagate through shared
1330 // library dependencies and therefore can cause multiple variations of a
1331 // shared library to be built.
1332 if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != scs && s.sanitizer != Asan {
1333 oneMakeVariation = true
1334 }
1335 }
1336
1337 if oneMakeVariation {
1338 if sanitizerEnabled != sanitizerVariation {
1339 c.SetPreventInstall()
1340 c.SetHideFromMake()
1341 }
1342 }
1343
1344 if sanitizerVariation {
1345 c.SetSanitizer(s.sanitizer, true)
1346
1347 // CFI is incompatible with ASAN so disable it in ASAN variations
1348 if s.sanitizer.incompatibleWithCfi() {
1349 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1350 if mctx.Device() && cfiSupported {
1351 c.SetSanitizer(cfi, false)
Jiyong Parkf97782b2019-02-13 20:28:58 +09001352 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001353 }
1354
1355 // locate the asan libraries under /data/asan
1356 if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
1357 c.SetInSanitizerDir()
1358 }
1359
1360 if c.StaticallyLinked() && c.ExportedToMake() {
1361 if s.sanitizer == Hwasan {
1362 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
1363 } else if s.sanitizer == cfi {
1364 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
1365 }
1366 }
1367 } else if c.IsSanitizerEnabled(s.sanitizer) {
1368 // Disable the sanitizer for the non-sanitized variation
1369 c.SetSanitizer(s.sanitizer, false)
1370 }
1371 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
1372 // If an APEX has sanitized dependencies, it gets a few more dependencies
1373 if sanitizerVariation {
1374 sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
1375 }
1376 } else if c, ok := mctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001377 if ss, ok := c.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
1378 if !ss.isUnsanitizedVariant() {
1379 // Snapshot sanitizer may have only one variantion.
1380 // Skip exporting the module if it already has a sanitizer variation.
1381 c.SetPreventInstall()
1382 c.SetHideFromMake()
1383 return
1384 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001385 c.linker.(snapshotSanitizer).setSanitizerVariation(s.sanitizer, sanitizerVariation)
1386
1387 // Export the static lib name to make
1388 if c.static() && c.ExportedToMake() {
Justin Yun39c30312022-11-23 16:20:12 +09001389 // use BaseModuleName which is the name for Make.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001390 if s.sanitizer == cfi {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001391 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Justin Yun39c30312022-11-23 16:20:12 +09001392 } else if s.sanitizer == Hwasan {
1393 hwasanStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001394 }
1395 }
Colin Cross16b23492016-01-06 14:41:07 -08001396 }
1397 }
1398}
1399
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001400func (c *Module) SanitizeNever() bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001401 return Bool(c.sanitize.Properties.SanitizeMutated.Never)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001402}
1403
1404func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
1405 return c.sanitize.isSanitizerExplicitlyDisabled(t)
1406}
1407
Ivan Lozano30c5db22018-02-21 15:49:20 -08001408// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001409func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001410 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001411 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001412 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001413 mctx.WalkDeps(func(child, parent android.Module) bool {
1414 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1415 return false
1416 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001417
Inseob Kimeec88e12020-01-22 11:11:29 +09001418 d, ok := child.(*Module)
1419 if !ok || !d.static() {
1420 return false
1421 }
1422 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001423 if enableMinimalRuntime(d.sanitize) {
1424 // If a static dependency is built with the minimal runtime,
1425 // make sure we include the ubsan minimal runtime.
1426 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001427 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001428 // If a static dependency runs with full ubsan diagnostics,
1429 // make sure we include the ubsan runtime.
1430 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001431 }
Colin Cross0b908332019-06-19 23:00:20 -07001432
1433 if c.sanitize.Properties.MinimalRuntimeDep &&
1434 c.sanitize.Properties.UbsanRuntimeDep {
1435 // both flags that this mutator might set are true, so don't bother recursing
1436 return false
1437 }
1438
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001439 if c.Os() == android.Linux {
1440 c.sanitize.Properties.BuiltinsDep = true
1441 }
1442
Colin Cross0b908332019-06-19 23:00:20 -07001443 return true
Colin Cross6b753602018-06-21 13:03:07 -07001444 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001445
Jose Galmesf7294582020-11-13 12:07:36 -08001446 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001447 if Bool(p.properties.Sanitize_minimal_dep) {
1448 c.sanitize.Properties.MinimalRuntimeDep = true
1449 }
1450 if Bool(p.properties.Sanitize_ubsan_dep) {
1451 c.sanitize.Properties.UbsanRuntimeDep = true
1452 }
1453 }
1454
1455 return false
Colin Cross6b753602018-06-21 13:03:07 -07001456 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001457 }
1458}
1459
Jiyong Park379de2f2018-12-19 02:47:14 +09001460// Add the dependency to the runtime library for each of the sanitizer variants
1461func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001462 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001463 if !c.Enabled() {
1464 return
1465 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001466 var sanitizers []string
1467 var diagSanitizers []string
1468
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001469 sanProps := &c.sanitize.Properties.SanitizeMutated
1470
1471 if Bool(sanProps.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001472 sanitizers = append(sanitizers, "undefined")
1473 } else {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001474 if Bool(sanProps.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001475 sanitizers = append(sanitizers,
1476 "bool",
1477 "integer-divide-by-zero",
1478 "return",
1479 "returns-nonnull-attribute",
1480 "shift-exponent",
1481 "unreachable",
1482 "vla-bound",
1483 // TODO(danalbert): The following checks currently have compiler performance issues.
1484 //"alignment",
1485 //"bounds",
1486 //"enum",
1487 //"float-cast-overflow",
1488 //"float-divide-by-zero",
1489 //"nonnull-attribute",
1490 //"null",
1491 //"shift-base",
1492 //"signed-integer-overflow",
1493 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1494 // https://llvm.org/PR19302
1495 // http://reviews.llvm.org/D6974
1496 // "object-size",
1497 )
1498 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001499 sanitizers = append(sanitizers, sanProps.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001500 }
1501
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001502 if Bool(sanProps.Diag.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001503 diagSanitizers = append(diagSanitizers, "undefined")
1504 }
1505
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001506 diagSanitizers = append(diagSanitizers, sanProps.Diag.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001507
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001508 if Bool(sanProps.Address) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001509 sanitizers = append(sanitizers, "address")
1510 diagSanitizers = append(diagSanitizers, "address")
1511 }
1512
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001513 if Bool(sanProps.Hwaddress) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001514 sanitizers = append(sanitizers, "hwaddress")
1515 }
1516
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001517 if Bool(sanProps.Thread) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001518 sanitizers = append(sanitizers, "thread")
1519 }
1520
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001521 if Bool(sanProps.Safestack) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001522 sanitizers = append(sanitizers, "safe-stack")
1523 }
1524
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001525 if Bool(sanProps.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001526 sanitizers = append(sanitizers, "cfi")
1527
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001528 if Bool(sanProps.Diag.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001529 diagSanitizers = append(diagSanitizers, "cfi")
1530 }
1531 }
1532
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001533 if Bool(sanProps.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001534 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1535 sanitizers = append(sanitizers, "signed-integer-overflow")
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001536 if Bool(sanProps.Diag.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001537 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1538 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1539 }
1540 }
1541
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001542 if Bool(sanProps.Scudo) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001543 sanitizers = append(sanitizers, "scudo")
1544 }
1545
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001546 if Bool(sanProps.Scs) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001547 sanitizers = append(sanitizers, "shadow-call-stack")
1548 }
1549
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001550 if Bool(sanProps.Memtag_heap) && c.Binary() {
Florian Mayerd8434a42022-08-31 20:57:03 +00001551 sanitizers = append(sanitizers, "memtag-heap")
1552 }
1553
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001554 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +00001555 sanitizers = append(sanitizers, "memtag-stack")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001556 }
1557
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001558 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001559 sanitizers = append(sanitizers, "fuzzer-no-link")
1560 }
1561
Jiyong Park379de2f2018-12-19 02:47:14 +09001562 // Save the list of sanitizers. These will be used again when generating
1563 // the build rules (for Cflags, etc.)
1564 c.sanitize.Properties.Sanitizers = sanitizers
1565 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1566
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001567 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1568 if c.Host() {
1569 diagSanitizers = sanitizers
1570 }
1571
Colin Crosse323a792023-02-15 13:57:57 -08001572 addStaticDeps := func(dep string, hideSymbols bool) {
Colin Cross06c80eb2022-02-10 10:34:19 -08001573 // If we're using snapshots, redirect to snapshot whenever possible
1574 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001575 if lib, ok := snapshot.StaticLibs[dep]; ok {
1576 dep = lib
Colin Cross06c80eb2022-02-10 10:34:19 -08001577 }
1578
1579 // static executable gets static runtime libs
Colin Crosse323a792023-02-15 13:57:57 -08001580 depTag := libraryDependencyTag{Kind: staticLibraryDependency, unexportedSymbols: hideSymbols}
Colin Cross06c80eb2022-02-10 10:34:19 -08001581 variations := append(mctx.Target().Variations(),
1582 blueprint.Variation{Mutator: "link", Variation: "static"})
1583 if c.Device() {
1584 variations = append(variations, c.ImageVariation())
1585 }
1586 if c.UseSdk() {
1587 variations = append(variations,
1588 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1589 }
Colin Crosse323a792023-02-15 13:57:57 -08001590 mctx.AddFarVariationDependencies(variations, depTag, dep)
Colin Cross06c80eb2022-02-10 10:34:19 -08001591 }
Colin Crosse323a792023-02-15 13:57:57 -08001592
1593 // Determine the runtime library required
1594 runtimeSharedLibrary := ""
1595 toolchain := c.toolchain(mctx)
1596 if Bool(sanProps.Address) {
Colin Crossb781d232023-02-15 12:40:20 -08001597 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1598 // Use a static runtime for musl to match what clang does for glibc.
1599 addStaticDeps(config.AddressSanitizerStaticRuntimeLibrary(toolchain), false)
1600 addStaticDeps(config.AddressSanitizerCXXStaticRuntimeLibrary(toolchain), false)
1601 } else {
1602 runtimeSharedLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1603 }
Colin Crosse323a792023-02-15 13:57:57 -08001604 } else if Bool(sanProps.Hwaddress) {
1605 if c.staticBinary() {
1606 addStaticDeps(config.HWAddressSanitizerStaticLibrary(toolchain), true)
1607 addStaticDeps("libdl", false)
1608 } else {
1609 runtimeSharedLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1610 }
1611 } else if Bool(sanProps.Thread) {
1612 runtimeSharedLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1613 } else if Bool(sanProps.Scudo) {
1614 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1615 runtimeSharedLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1616 } else {
1617 runtimeSharedLibrary = config.ScudoRuntimeLibrary(toolchain)
1618 }
1619 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
1620 Bool(sanProps.Fuzzer) ||
1621 Bool(sanProps.Undefined) ||
1622 Bool(sanProps.All_undefined) {
1623 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1624 // Use a static runtime for static binaries.
1625 // Also use a static runtime for musl to match
1626 // what clang does for glibc. Otherwise dlopening
1627 // libraries that depend on libclang_rt.ubsan_standalone.so
1628 // fails with:
1629 // Error relocating ...: initial-exec TLS resolves to dynamic definition
1630 addStaticDeps(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)+".static", true)
1631 } else {
1632 runtimeSharedLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
1633 }
1634 }
1635
Colin Cross06c80eb2022-02-10 10:34:19 -08001636 if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
Colin Crosse323a792023-02-15 13:57:57 -08001637 addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001638 }
1639 if c.sanitize.Properties.BuiltinsDep {
Colin Crosse323a792023-02-15 13:57:57 -08001640 addStaticDeps(config.BuiltinsRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001641 }
1642
Colin Crosse323a792023-02-15 13:57:57 -08001643 if runtimeSharedLibrary != "" && (toolchain.Bionic() || toolchain.Musl() || c.sanitize.Properties.UbsanRuntimeDep) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001644 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001645
1646 // Adding dependency to the runtime library. We are using *FarVariation*
1647 // because the runtime libraries themselves are not mutated by sanitizer
1648 // mutators and thus don't have sanitizer variants whereas this module
1649 // has been already mutated.
1650 //
1651 // Note that by adding dependency with {static|shared}DepTag, the lib is
1652 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
Colin Crosse323a792023-02-15 13:57:57 -08001653 if c.staticBinary() {
1654 // Most sanitizers are either disabled for static binaries or have already
1655 // handled the static binary case above through a direct call to addStaticDeps.
1656 // If not, treat the runtime shared library as a static library and hope for
1657 // the best.
1658 addStaticDeps(runtimeSharedLibrary, true)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001659 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001660 // If we're using snapshots, redirect to snapshot whenever possible
1661 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001662 if lib, ok := snapshot.SharedLibs[runtimeSharedLibrary]; ok {
1663 runtimeSharedLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001664 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001665
Cindy Zhou18417cb2020-12-10 07:12:38 -08001666 // Skip apex dependency check for sharedLibraryDependency
1667 // when sanitizer diags are enabled. Skipping the check will allow
1668 // building with diag libraries without having to list the
1669 // dependency in Apex's allowed_deps file.
1670 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001671 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001672 depTag := libraryDependencyTag{
1673 Kind: sharedLibraryDependency,
1674 Order: earlyLibraryDependency,
1675
1676 skipApexAllowedDependenciesCheck: diagEnabled,
1677 }
Colin Cross42507332020-08-21 16:15:23 -07001678 variations := append(mctx.Target().Variations(),
1679 blueprint.Variation{Mutator: "link", Variation: "shared"})
1680 if c.Device() {
1681 variations = append(variations, c.ImageVariation())
1682 }
Colin Cross06c80eb2022-02-10 10:34:19 -08001683 if c.UseSdk() {
1684 variations = append(variations,
1685 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1686 }
Colin Crosse323a792023-02-15 13:57:57 -08001687 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeSharedLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001688 }
1689 // static lib does not have dependency to the runtime library. The
1690 // dependency will be added to the executables or shared libs using
1691 // the static lib.
1692 }
1693 }
1694}
1695
1696type Sanitizeable interface {
1697 android.Module
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001698 IsSanitizerEnabled(config android.Config, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001699 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001700 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001701}
1702
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001703type JniSanitizeable interface {
1704 android.Module
1705 IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
1706}
1707
Ivan Lozanod7586b62021-04-01 09:49:36 -04001708func (c *Module) MinimalRuntimeDep() bool {
1709 return c.sanitize.Properties.MinimalRuntimeDep
1710}
1711
1712func (c *Module) UbsanRuntimeDep() bool {
1713 return c.sanitize.Properties.UbsanRuntimeDep
1714}
1715
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001716func (c *Module) SanitizePropDefined() bool {
1717 return c.sanitize != nil
1718}
1719
1720func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1721 return c.sanitize.isSanitizerEnabled(t)
1722}
1723
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001724func (c *Module) StaticallyLinked() bool {
1725 return c.static()
1726}
1727
1728func (c *Module) SetInSanitizerDir() {
1729 if c.sanitize != nil {
1730 c.sanitize.Properties.InSanitizerDir = true
1731 }
1732}
1733
1734func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1735 if c.sanitize != nil {
1736 c.sanitize.SetSanitizer(t, b)
1737 }
1738}
1739
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001740var _ PlatformSanitizeable = (*Module)(nil)
1741
Inseob Kim74d25562020-08-04 00:41:38 +09001742type sanitizerStaticLibsMap struct {
1743 // libsMap contains one list of modules per each image and each arch.
1744 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001745 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001746 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001747 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001748}
1749
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001750func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001751 return &sanitizerStaticLibsMap{
1752 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001753 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001754 }
1755}
1756
1757// Add the current module to sanitizer static libs maps
1758// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001759func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1760 image := GetImageVariantType(c)
1761 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001762
1763 s.libsMapLock.Lock()
1764 defer s.libsMapLock.Unlock()
1765
1766 if _, ok := s.libsMap[image]; !ok {
1767 s.libsMap[image] = make(map[string][]string)
1768 }
1769
1770 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1771}
1772
1773// Exports makefile variables in the following format:
1774// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1775// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1776// These are to be used by use_soong_sanitized_static_libraries.
1777// See build/make/core/binary.mk for more details.
1778func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
Cole Faust18994c72023-02-28 16:02:16 -08001779 for _, image := range android.SortedKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001780 archMap := s.libsMap[ImageVariantType(image)]
Cole Faust18994c72023-02-28 16:02:16 -08001781 for _, arch := range android.SortedKeys(archMap) {
Inseob Kim74d25562020-08-04 00:41:38 +09001782 libs := archMap[arch]
1783 sort.Strings(libs)
1784
1785 key := fmt.Sprintf(
1786 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1787 s.sanitizerType.variationName(),
1788 image, // already upper
1789 arch)
1790
1791 ctx.Strict(key, strings.Join(libs, " "))
1792 }
1793 }
1794}
1795
Colin Cross571cccf2019-02-04 11:22:08 -08001796var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1797
Inseob Kim74d25562020-08-04 00:41:38 +09001798func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001799 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001800 return newSanitizerStaticLibsMap(cfi)
1801 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001802}
1803
Colin Cross571cccf2019-02-04 11:22:08 -08001804var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1805
Inseob Kim74d25562020-08-04 00:41:38 +09001806func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001807 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001808 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001809 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001810}
1811
Ivan Lozano30c5db22018-02-21 15:49:20 -08001812func enableMinimalRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001813 if sanitize.isSanitizerEnabled(Asan) {
1814 return false
1815 } else if sanitize.isSanitizerEnabled(Hwasan) {
1816 return false
1817 } else if sanitize.isSanitizerEnabled(Fuzzer) {
1818 return false
Ivan Lozano30c5db22018-02-21 15:49:20 -08001819 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001820
1821 if enableUbsanRuntime(sanitize) {
1822 return false
1823 }
1824
1825 sanitizeProps := &sanitize.Properties.SanitizeMutated
1826 if Bool(sanitizeProps.Diag.Cfi) {
1827 return false
1828 }
1829
1830 return Bool(sanitizeProps.Integer_overflow) ||
1831 len(sanitizeProps.Misc_undefined) > 0 ||
1832 Bool(sanitizeProps.Undefined) ||
1833 Bool(sanitizeProps.All_undefined)
Ivan Lozano30c5db22018-02-21 15:49:20 -08001834}
1835
Ivan Lozanod7586b62021-04-01 09:49:36 -04001836func (m *Module) UbsanRuntimeNeeded() bool {
1837 return enableUbsanRuntime(m.sanitize)
1838}
1839
1840func (m *Module) MinimalRuntimeNeeded() bool {
1841 return enableMinimalRuntime(m.sanitize)
1842}
1843
Inseob Kim8471cda2019-11-15 09:59:12 +09001844func enableUbsanRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001845 sanitizeProps := &sanitize.Properties.SanitizeMutated
1846 return Bool(sanitizeProps.Diag.Integer_overflow) ||
1847 Bool(sanitizeProps.Diag.Undefined) ||
1848 len(sanitizeProps.Diag.Misc_undefined) > 0
Inseob Kim8471cda2019-11-15 09:59:12 +09001849}
1850
Vishwath Mohane7128792017-11-17 11:08:10 -08001851func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001852 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001853}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001854
1855func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001856 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001857}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +00001858
1859func BazelCcSanitizerToolchainVars(config android.Config) string {
1860 return android.BazelToolchainVars(config, exportedVars)
1861}