blob: e28f53718645e0423cf0f1d4a4dd3ecf48451e50 [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 Radcliffe391a25d2023-03-22 20:22:27 +000065 cfiBlocklistPath = "external/compiler-rt/lib/cfi"
66 cfiBlocklistFilename = "cfi_blocklist.txt"
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +000067 cfiCrossDsoFlag = "-fsanitize-cfi-cross-dso"
68 cfiCflags = []string{"-flto", cfiCrossDsoFlag,
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000069 "-fsanitize-ignorelist=" + cfiBlocklistPath + "/" + cfiBlocklistFilename}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070070 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
71 // used, but have no effect on assembly files
72 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +000073 cfiLdflags = []string{"-flto", cfiCrossDsoFlag, "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070074 "-Wl,-plugin-opt,O1"}
Trevor Radcliffe391a25d2023-03-22 20:22:27 +000075 cfiExportsMapPath = "build/soong/cc/config"
76 cfiExportsMapFilename = "cfi_exports.map"
77 cfiAssemblySupportFlag = "-fno-sanitize-cfi-canonical-jump-tables"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070078
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070079 intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080080
Peter Collingbournebd19db02019-03-06 10:38:48 -080081 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070082 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070083 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
Christopher Ferris2fc8e032023-01-26 14:19:27 -080084 "export_memory_stats=0", "max_malloc_fill_size=131072", "malloc_fill_byte=0"}
Florian Mayer1866bbe2023-03-11 01:07:40 +000085 memtagStackCommonFlags = []string{"-march=armv8-a+memtag", "-mllvm", "-dom-tree-reachability-max-bbs-to-explore=128"}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +000086
87 hostOnlySanitizeFlags = []string{"-fno-sanitize-recover=all"}
88 deviceOnlySanitizeFlags = []string{"-fsanitize-trap=all", "-ftrap-function=abort"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070089)
90
Ivan Lozano3968d8f2020-12-14 11:27:52 -050091type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080092
Colin Cross16b23492016-01-06 14:41:07 -080093const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050094 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070095 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080096 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070097 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080098 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -050099 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -0400100 Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +0000101 Memtag_stack
Mitch Phillips9036f732023-06-26 08:08:56 +0000102 Memtag_globals
Liz Kammer75db9312021-07-07 16:41:50 -0400103 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -0800104)
105
Liz Kammer75db9312021-07-07 16:41:50 -0400106var Sanitizers = []SanitizerType{
107 Asan,
108 Hwasan,
109 tsan,
110 intOverflow,
111 scs,
112 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400113 Memtag_heap,
Florian Mayerd8434a42022-08-31 20:57:03 +0000114 Memtag_stack,
Mitch Phillips9036f732023-06-26 08:08:56 +0000115 Memtag_globals,
Liz Kammer75db9312021-07-07 16:41:50 -0400116 cfi, // cfi is last to prevent it running before incompatible mutators
117}
118
Jiyong Park82226632019-02-01 10:50:50 +0900119// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500120func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800121 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500122 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800123 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700124 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700125 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800126 case tsan:
127 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700128 case intOverflow:
129 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000130 case cfi:
131 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800132 case scs:
133 return "scs"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400134 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700135 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000136 case Memtag_stack:
137 return "memtag_stack"
Mitch Phillips9036f732023-06-26 08:08:56 +0000138 case Memtag_globals:
139 return "memtag_globals"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500140 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700141 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800142 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500143 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800144 }
145}
146
Jiyong Park82226632019-02-01 10:50:50 +0900147// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500148func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900149 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500150 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900151 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700152 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900153 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400154 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700155 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000156 case Memtag_stack:
157 return "memtag_stack"
Mitch Phillips9036f732023-06-26 08:08:56 +0000158 case Memtag_globals:
159 return "memtag_globals"
Jiyong Park82226632019-02-01 10:50:50 +0900160 case tsan:
161 return "thread"
162 case intOverflow:
163 return "integer_overflow"
164 case cfi:
165 return "cfi"
166 case scs:
167 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500168 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700169 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900170 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500171 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900172 }
173}
174
Liz Kammer75db9312021-07-07 16:41:50 -0400175func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
176 switch t {
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200177 case cfi, Hwasan, Asan, tsan, Fuzzer, scs:
178 sanitizer := &sanitizerSplitMutator{t}
179 ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
180 ctx.Transition(t.variationName(), sanitizer)
Mitch Phillips9036f732023-06-26 08:08:56 +0000181 case Memtag_heap, Memtag_stack, Memtag_globals, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400182 // do nothing
183 default:
184 panic(fmt.Errorf("unknown SanitizerType %d", t))
185 }
186}
187
Liz Kammerfd8a49f2022-10-31 10:31:11 -0400188// shouldPropagateToSharedLibraryDeps returns whether a sanitizer type should propagate to share
189// dependencies. In most cases, sanitizers only propagate to static dependencies; however, some
190// sanitizers also must be enabled for shared libraries for linking.
191func (t SanitizerType) shouldPropagateToSharedLibraryDeps() bool {
192 switch t {
193 case Fuzzer:
194 // Typically, shared libs are not split. However, for fuzzer, we split even for shared libs
195 // because a library sanitized for fuzzer can't be linked from a library that isn't sanitized
196 // for fuzzer.
197 return true
198 default:
199 return false
200 }
201}
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500202func (*Module) SanitizerSupported(t SanitizerType) bool {
203 switch t {
204 case Asan:
205 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700206 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500207 return true
208 case tsan:
209 return true
210 case intOverflow:
211 return true
212 case cfi:
213 return true
214 case scs:
215 return true
216 case Fuzzer:
217 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400218 case Memtag_heap:
219 return true
Florian Mayerd8434a42022-08-31 20:57:03 +0000220 case Memtag_stack:
221 return true
Mitch Phillips9036f732023-06-26 08:08:56 +0000222 case Memtag_globals:
223 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500224 default:
225 return false
226 }
227}
228
229// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
230func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700231 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900232}
233
Martin Stjernholmb0249572020-09-15 02:32:35 +0100234type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400235 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100236 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800237
Liz Kammer75b9b402021-06-25 15:19:27 -0400238 // ASan (Address sanitizer), incompatible with static binaries.
239 // Always runs in a diagnostic mode.
240 // Use of address sanitizer disables cfi sanitizer.
241 // Hwaddress sanitizer takes precedence over this sanitizer.
242 Address *bool `android:"arch_variant"`
243 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
244 // Always runs in a diagnostic mode.
245 // Use of thread sanitizer disables cfi and scudo sanitizers.
246 // Hwaddress sanitizer takes precedence over this sanitizer.
247 Thread *bool `android:"arch_variant"`
248 // HWASan (Hardware Address sanitizer).
249 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100250 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800251
Liz Kammer75b9b402021-06-25 15:19:27 -0400252 // Undefined behavior sanitizer
253 All_undefined *bool `android:"arch_variant"`
254 // Subset of undefined behavior sanitizer
255 Undefined *bool `android:"arch_variant"`
256 // List of specific undefined behavior sanitizers to enable
257 Misc_undefined []string `android:"arch_variant"`
258 // Fuzzer, incompatible with static binaries.
259 Fuzzer *bool `android:"arch_variant"`
260 // safe-stack sanitizer, incompatible with 32-bit architectures.
261 Safestack *bool `android:"arch_variant"`
262 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
263 Cfi *bool `android:"arch_variant"`
264 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
265 Integer_overflow *bool `android:"arch_variant"`
266 // scudo sanitizer, incompatible with asan, hwasan, tsan
267 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
268 // deprecated
269 Scudo *bool `android:"arch_variant"`
Elliott Hughese4793bc2023-02-09 21:15:47 +0000270 // shadow-call-stack sanitizer, only available on arm64/riscv64.
Liz Kammer75b9b402021-06-25 15:19:27 -0400271 Scs *bool `android:"arch_variant"`
272 // Memory-tagging, only available on arm64
273 // if diag.memtag unset or false, enables async memory tagging
Florian Mayer00ab5cf2022-08-31 18:30:18 +0000274 Memtag_heap *bool `android:"arch_variant"`
Florian Mayerd8434a42022-08-31 20:57:03 +0000275 // Memory-tagging stack instrumentation, only available on arm64
276 // Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
277 Memtag_stack *bool `android:"arch_variant"`
Mitch Phillips9036f732023-06-26 08:08:56 +0000278 // Memory-tagging globals instrumentation, only available on arm64
279 // Adds instrumentation to detect global buffer overflows using MTE.
280 Memtag_globals *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100281
282 // A modifier for ASAN and HWASAN for write only instrumentation
283 Writeonly *bool `android:"arch_variant"`
284
285 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
286 // Replaces abort() on error with a human-readable error message.
287 // Address and Thread sanitizers always run in diagnostic mode.
288 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400289 // Undefined behavior sanitizer, diagnostic mode
290 Undefined *bool `android:"arch_variant"`
291 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
292 Cfi *bool `android:"arch_variant"`
293 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
294 Integer_overflow *bool `android:"arch_variant"`
295 // Memory-tagging, only available on arm64
296 // requires sanitizer.memtag: true
297 // if set, enables sync memory tagging
298 Memtag_heap *bool `android:"arch_variant"`
299 // List of specific undefined behavior sanitizers to enable in diagnostic mode
300 Misc_undefined []string `android:"arch_variant"`
301 // List of sanitizers to pass to -fno-sanitize-recover
302 // results in only the first detected error for these sanitizers being reported and program then
303 // exits with a non-zero exit code.
304 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800305 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800306
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800307 // Sanitizers to run with flag configuration specified
308 Config struct {
309 // Enables CFI support flags for assembly-heavy libraries
310 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800311 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800312
Liz Kammer75b9b402021-06-25 15:19:27 -0400313 // List of sanitizers to pass to -fsanitize-recover
314 // allows execution to continue for these sanitizers to detect multiple errors rather than only
315 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100316 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000317
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700318 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100319 Blocklist *string
320}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700321
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400322type sanitizeMutatedProperties struct {
323 // Whether sanitizers can be enabled on this module
324 Never *bool `blueprint:"mutated"`
325
326 // Whether ASan (Address sanitizer) is enabled for this module.
327 // Hwaddress sanitizer takes precedence over this sanitizer.
328 Address *bool `blueprint:"mutated"`
329 // Whether TSan (Thread sanitizer) is enabled for this module
330 Thread *bool `blueprint:"mutated"`
331 // Whether HWASan (Hardware Address sanitizer) is enabled for this module
332 Hwaddress *bool `blueprint:"mutated"`
333
334 // Whether Undefined behavior sanitizer is enabled for this module
335 All_undefined *bool `blueprint:"mutated"`
336 // Whether undefined behavior sanitizer subset is enabled for this module
337 Undefined *bool `blueprint:"mutated"`
338 // List of specific undefined behavior sanitizers enabled for this module
339 Misc_undefined []string `blueprint:"mutated"`
340 // Whether Fuzzeris enabled for this module
341 Fuzzer *bool `blueprint:"mutated"`
342 // whether safe-stack sanitizer is enabled for this module
343 Safestack *bool `blueprint:"mutated"`
344 // Whether cfi sanitizer is enabled for this module
345 Cfi *bool `blueprint:"mutated"`
346 // Whether signed/unsigned integer overflow sanitizer is enabled for this module
347 Integer_overflow *bool `blueprint:"mutated"`
348 // Whether scudo sanitizer is enabled for this module
349 Scudo *bool `blueprint:"mutated"`
350 // Whether shadow-call-stack sanitizer is enabled for this module.
351 Scs *bool `blueprint:"mutated"`
352 // Whether Memory-tagging is enabled for this module
353 Memtag_heap *bool `blueprint:"mutated"`
354 // Whether Memory-tagging stack instrumentation is enabled for this module
355 Memtag_stack *bool `blueprint:"mutated"`
Mitch Phillips9036f732023-06-26 08:08:56 +0000356 // Whether Memory-tagging globals instrumentation is enabled for this module
357 Memtag_globals *bool `android:"arch_variant"`
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400358
359 // Whether a modifier for ASAN and HWASAN for write only instrumentation is enabled for this
360 // module
361 Writeonly *bool `blueprint:"mutated"`
362
363 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
364 Diag struct {
365 // Whether Undefined behavior sanitizer, diagnostic mode is enabled for this module
366 Undefined *bool `blueprint:"mutated"`
367 // Whether cfi sanitizer, diagnostic mode is enabled for this module
368 Cfi *bool `blueprint:"mutated"`
369 // Whether signed/unsigned integer overflow sanitizer, diagnostic mode is enabled for this
370 // module
371 Integer_overflow *bool `blueprint:"mutated"`
372 // Whether Memory-tagging, diagnostic mode is enabled for this module
373 Memtag_heap *bool `blueprint:"mutated"`
374 // List of specific undefined behavior sanitizers enabled in diagnostic mode
375 Misc_undefined []string `blueprint:"mutated"`
376 } `blueprint:"mutated"`
377}
378
Martin Stjernholmb0249572020-09-15 02:32:35 +0100379type SanitizeProperties struct {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400380 Sanitize SanitizeUserProps `android:"arch_variant"`
381 SanitizeMutated sanitizeMutatedProperties `blueprint:"mutated"`
382
383 SanitizerEnabled bool `blueprint:"mutated"`
384 MinimalRuntimeDep bool `blueprint:"mutated"`
385 BuiltinsDep bool `blueprint:"mutated"`
386 UbsanRuntimeDep bool `blueprint:"mutated"`
387 InSanitizerDir bool `blueprint:"mutated"`
388 Sanitizers []string `blueprint:"mutated"`
389 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800390}
391
392type sanitize struct {
393 Properties SanitizeProperties
394}
395
Cindy Zhou18417cb2020-12-10 07:12:38 -0800396// Mark this tag with a check to see if apex dependency check should be skipped
397func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
398 return t.skipApexAllowedDependenciesCheck
399}
400
401var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
402
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000403var exportedVars = android.NewExportedVariables(pctx)
404
Vishwath Mohane7128792017-11-17 11:08:10 -0800405func init() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000406 exportedVars.ExportStringListStaticVariable("HostOnlySanitizeFlags", hostOnlySanitizeFlags)
407 exportedVars.ExportStringList("DeviceOnlySanitizeFlags", deviceOnlySanitizeFlags)
408
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000409 // Leave out "-flto" from the slices exported to bazel, as we will use the
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000410 // dedicated LTO feature for this. For C Flags and Linker Flags, also leave
411 // out the cross DSO flag which will be added separately by transitions.
412 exportedVars.ExportStringList("CfiCFlags", cfiCflags[2:])
413 exportedVars.ExportStringList("CfiLdFlags", cfiLdflags[2:])
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000414 exportedVars.ExportStringList("CfiAsFlags", cfiAsflags[1:])
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000415
Trevor Radcliffe9f4b4762023-04-04 20:13:42 +0000416 exportedVars.ExportString("CfiCrossDsoFlag", cfiCrossDsoFlag)
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000417 exportedVars.ExportString("CfiBlocklistPath", cfiBlocklistPath)
418 exportedVars.ExportString("CfiBlocklistFilename", cfiBlocklistFilename)
419 exportedVars.ExportString("CfiExportsMapPath", cfiExportsMapPath)
420 exportedVars.ExportString("CfiExportsMapFilename", cfiExportsMapFilename)
421 exportedVars.ExportString("CfiAssemblySupportFlag", cfiAssemblySupportFlag)
422
Vishwath Mohane7128792017-11-17 11:08:10 -0800423 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700424 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800425}
426
Colin Cross16b23492016-01-06 14:41:07 -0800427func (sanitize *sanitize) props() []interface{} {
428 return []interface{}{&sanitize.Properties}
429}
430
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400431func (p *sanitizeMutatedProperties) copyUserPropertiesToMutated(userProps *SanitizeUserProps) {
432 p.Never = userProps.Never
433 p.Address = userProps.Address
434 p.All_undefined = userProps.All_undefined
435 p.Cfi = userProps.Cfi
436 p.Fuzzer = userProps.Fuzzer
437 p.Hwaddress = userProps.Hwaddress
438 p.Integer_overflow = userProps.Integer_overflow
439 p.Memtag_heap = userProps.Memtag_heap
440 p.Memtag_stack = userProps.Memtag_stack
Mitch Phillips9036f732023-06-26 08:08:56 +0000441 p.Memtag_globals = userProps.Memtag_globals
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400442 p.Safestack = userProps.Safestack
443 p.Scs = userProps.Scs
444 p.Scudo = userProps.Scudo
445 p.Thread = userProps.Thread
446 p.Undefined = userProps.Undefined
447 p.Writeonly = userProps.Writeonly
448
449 p.Misc_undefined = make([]string, 0, len(userProps.Misc_undefined))
450 for _, v := range userProps.Misc_undefined {
451 p.Misc_undefined = append(p.Misc_undefined, v)
452 }
453
454 p.Diag.Cfi = userProps.Diag.Cfi
455 p.Diag.Integer_overflow = userProps.Diag.Integer_overflow
456 p.Diag.Memtag_heap = userProps.Diag.Memtag_heap
457 p.Diag.Undefined = userProps.Diag.Undefined
458
459 p.Diag.Misc_undefined = make([]string, 0, len(userProps.Diag.Misc_undefined))
460 for _, v := range userProps.Diag.Misc_undefined {
461 p.Diag.Misc_undefined = append(p.Diag.Misc_undefined, v)
462 }
463}
464
Colin Cross16b23492016-01-06 14:41:07 -0800465func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400466 s := &sanitize.Properties.SanitizeMutated
467 s.copyUserPropertiesToMutated(&sanitize.Properties.Sanitize)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700468
Colin Cross16b23492016-01-06 14:41:07 -0800469 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700470 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800471 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800472 }
473
474 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800475 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800476 return
477 }
478
Florian Mayerd8434a42022-08-31 20:57:03 +0000479 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400480 if ctx.testBinary() {
481 if s.Memtag_heap == nil {
482 s.Memtag_heap = proptools.BoolPtr(true)
483 }
484 if s.Diag.Memtag_heap == nil {
485 s.Diag.Memtag_heap = proptools.BoolPtr(true)
486 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800487 }
488
Colin Cross16b23492016-01-06 14:41:07 -0800489 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700490 var globalSanitizersDiag []string
491
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700492 if ctx.Host() {
493 if !ctx.Windows() {
494 globalSanitizers = ctx.Config().SanitizeHost()
495 }
496 } else {
497 arches := ctx.Config().SanitizeDeviceArch()
498 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
499 globalSanitizers = ctx.Config().SanitizeDevice()
500 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800501 }
502 }
503
Colin Cross16b23492016-01-06 14:41:07 -0800504 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000505 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700506 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400507 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000508 }
Colin Cross16b23492016-01-06 14:41:07 -0800509
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700510 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400511 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000512 }
513
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700514 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400515 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000516 }
517
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700518 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400519 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000520 }
521
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700522 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400523 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700524 }
525
526 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400527 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000528 }
529
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700530 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800531 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400532 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700533 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700534 }
535
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700536 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700537 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700538 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400539 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700540 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700541 }
542
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700543 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400544 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700545 }
546
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700547 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400548 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700549 }
550
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000551 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
552 // Hwaddress and Address are set before, so we can check them here
553 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
554 if s.Address == nil && s.Hwaddress == nil {
555 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
556 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400557 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000558 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700559 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800560 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400561 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800562 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700563 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000564
Florian Mayerd8434a42022-08-31 20:57:03 +0000565 if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
566 s.Memtag_stack = proptools.BoolPtr(true)
567 }
568
Mitch Phillips9036f732023-06-26 08:08:56 +0000569 if found, globalSanitizers = removeFromList("memtag_globals", globalSanitizers); found && s.Memtag_globals == nil {
570 s.Memtag_globals = proptools.BoolPtr(true)
571 }
572
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000573 if len(globalSanitizers) > 0 {
574 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
575 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700576
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700577 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700578 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700579 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400580 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700581 }
582
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700583 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
584 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400585 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700586 }
587
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800588 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
589 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400590 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800591 }
592
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700593 if len(globalSanitizersDiag) > 0 {
594 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
595 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700596 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700597
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800598 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross88a029f2022-06-23 14:51:20 -0700599 if ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800600 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800601 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400602 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800603 }
604 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400605 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800606 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800607 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800608 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400609 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800610 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800611 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700612 }
613
Hang Lu88086622023-03-17 13:17:22 +0800614 // Enable HWASan for all components in the include paths (for Aarch64 only)
615 if s.Hwaddress == nil && ctx.Config().HWASanEnabledForPath(ctx.ModuleDir()) &&
616 ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
617 s.Hwaddress = proptools.BoolPtr(true)
618 }
619
Elvis Chien9c993542021-06-25 01:15:17 +0800620 // Enable CFI for non-host components in the include paths
621 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400622 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000623 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400624 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700625 }
626 }
627
Elliott Hughesda3a0712020-03-06 16:55:28 -0800628 // Is CFI actually enabled?
629 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400630 s.Cfi = nil
631 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800632 }
633
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700634 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross88a029f2022-06-23 14:51:20 -0700635 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700636 s.Hwaddress = nil
637 }
638
Elliott Hughese4793bc2023-02-09 21:15:47 +0000639 // SCS is only implemented on AArch64/riscv64.
640 if (ctx.Arch().ArchType != android.Arm64 && ctx.Arch().ArchType != android.Riscv64) || !ctx.toolchain().Bionic() {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800641 s.Scs = nil
642 }
Elliott Hughes5beb42f2023-04-12 13:08:58 -0700643 // ...but temporarily globally disabled on riscv64 (http://b/277909695).
644 if ctx.Arch().ArchType == android.Riscv64 {
645 s.Scs = nil
646 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800647
Ivan Lozano62cd0382021-11-01 10:27:54 -0400648 // Memtag_heap is only implemented on AArch64.
Florian Mayerd8434a42022-08-31 20:57:03 +0000649 // Memtag ABI is Android specific for now, so disable for host.
650 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() || ctx.Host() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700651 s.Memtag_heap = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000652 s.Memtag_stack = nil
Mitch Phillips9036f732023-06-26 08:08:56 +0000653 s.Memtag_globals = nil
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700654 }
655
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700656 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700657 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400658 s.Cfi = nil
659 s.Diag.Cfi = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000660 // HWASAN and ASAN win against MTE.
661 s.Memtag_heap = nil
662 s.Memtag_stack = nil
Mitch Phillips9036f732023-06-26 08:08:56 +0000663 s.Memtag_globals = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700664 }
665
Colin Crossed12a042022-02-07 13:55:55 -0800666 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
667 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400668 s.Cfi = nil
669 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700670 s.Misc_undefined = nil
671 s.Undefined = nil
672 s.All_undefined = nil
673 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800674 }
675
Colin Cross2c435a02022-10-20 13:55:53 -0700676 // TODO(b/254713216): CFI doesn't work for riscv64 yet because LTO doesn't work.
Colin Crossecf4e662022-10-20 13:59:17 -0700677 if ctx.Arch().ArchType == android.Riscv64 {
678 s.Cfi = nil
679 s.Diag.Cfi = nil
680 }
681
Colin Crossed12a042022-02-07 13:55:55 -0800682 // Disable CFI for musl
683 if ctx.toolchain().Musl() {
684 s.Cfi = nil
685 s.Diag.Cfi = nil
686 }
687
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700688 // Also disable CFI for VNDK variants of components
689 if ctx.isVndk() && ctx.useVndk() {
Justin Yun08270c62022-12-19 17:01:26 +0900690 s.Cfi = nil
691 s.Diag.Cfi = nil
Inseob Kimeec88e12020-01-22 11:11:29 +0900692 }
693
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700694 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700695 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
696 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700697 s.Hwaddress = nil
698 }
699
Colin Cross3c344ef2016-07-18 15:44:56 -0700700 if ctx.staticBinary() {
701 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700702 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700703 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800704 }
705
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700706 if Bool(s.All_undefined) {
707 s.Undefined = nil
708 }
709
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700710 if !ctx.toolchain().Is64Bit() {
711 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700712 s.Thread = nil
713 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800714 // TODO(ccross): error for compile_multilib = "32"?
715 }
716
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800717 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 -0700718 Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Mitch Phillips9036f732023-06-26 08:08:56 +0000719 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack) ||
720 Bool(s.Memtag_globals)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700721 sanitize.Properties.SanitizerEnabled = true
722 }
723
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800724 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
725 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700726 s.Scudo = nil
727 }
728
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700729 if Bool(s.Hwaddress) {
730 s.Address = nil
731 s.Thread = nil
732 }
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000733
734 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
735 // mutually incompatible.
736 if Bool(s.Fuzzer) {
737 s.Cfi = nil
738 }
Colin Cross16b23492016-01-06 14:41:07 -0800739}
740
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800741func toDisableImplicitIntegerChange(flags []string) bool {
742 // Returns true if any flag is fsanitize*integer, and there is
743 // no explicit flag about sanitize=implicit-integer-sign-change.
744 for _, f := range flags {
745 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
746 return false
747 }
748 }
749 for _, f := range flags {
750 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
751 return true
752 }
753 }
754 return false
755}
756
Yabin Cuidb7dda82020-11-30 15:47:45 -0800757func toDisableUnsignedShiftBaseChange(flags []string) bool {
758 // Returns true if any flag is fsanitize*integer, and there is
759 // no explicit flag about sanitize=unsigned-shift-base.
760 for _, f := range flags {
761 if strings.Contains(f, "sanitize=unsigned-shift-base") {
762 return false
763 }
764 }
765 for _, f := range flags {
766 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
767 return true
768 }
769 }
770 return false
771}
772
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400773func (s *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
774 if !s.Properties.SanitizerEnabled && !s.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800775 return flags
776 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400777 sanProps := &s.Properties.SanitizeMutated
Colin Cross16b23492016-01-06 14:41:07 -0800778
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400779 if Bool(sanProps.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700780 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800781 // Frame pointer based unwinder in ASan requires ARM frame setup.
782 // TODO: put in flags?
783 flags.RequiredInstructionSet = "arm"
784 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800785 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
786 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800787
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400788 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000789 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
790 }
791
Colin Cross16b23492016-01-06 14:41:07 -0800792 if ctx.Host() {
793 // -nodefaultlibs (provided with libc++) prevents the driver from linking
794 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800795 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800796 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800797 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900798 if ctx.bootstrap() {
799 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
800 } else {
801 flags.DynamicLinker = "/system/bin/linker_asan"
802 }
Colin Cross16b23492016-01-06 14:41:07 -0800803 if flags.Toolchain.Is64Bit() {
804 flags.DynamicLinker += "64"
805 }
806 }
Colin Cross16b23492016-01-06 14:41:07 -0800807 }
808
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400809 if Bool(sanProps.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800810 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800811
812 for _, flag := range hwasanCommonflags {
813 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
814 }
815 for _, flag := range hwasanCommonflags {
816 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
817 }
818
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400819 if Bool(sanProps.Writeonly) {
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000820 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
821 }
Florian Mayer95cd6db2023-03-23 17:48:07 -0700822 if !ctx.staticBinary() && !ctx.Host() {
823 if ctx.bootstrap() {
824 flags.DynamicLinker = "/system/bin/bootstrap/linker_hwasan64"
825 } else {
826 flags.DynamicLinker = "/system/bin/linker_hwasan64"
827 }
828 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700829 }
830
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400831 if Bool(sanProps.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800832 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700833
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000834 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
835 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
836 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
837 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
838 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
839
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700840 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
841 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
842 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800843 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
844 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700845
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700846 // Disable fortify for fuzzing builds. Generally, we'll be building with
847 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800848 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800849
850 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
851 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
852 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
853 // the DT_RUNPATH from the shared library above it, and not the executable,
854 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
855 // DT_RUNPATH here means that transient shared libraries can be found
856 // colocated with their parents.
857 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800858 }
859
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400860 if Bool(sanProps.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800861 if ctx.Arch().ArchType == android.Arm {
862 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
863 // to do this on a function basis, so force Thumb on the entire module.
864 flags.RequiredInstructionSet = "thumb"
865 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000866
Colin Cross4af21ed2019-11-04 09:37:55 -0800867 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
868 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400869 if Bool(s.Properties.Sanitize.Config.Cfi_assembly_support) {
Trevor Radcliffe391a25d2023-03-22 20:22:27 +0000870 flags.Local.CFlags = append(flags.Local.CFlags, cfiAssemblySupportFlag)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800871 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000872 // Only append the default visibility flag if -fvisibility has not already been set
873 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800874 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
875 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000876 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800877 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000878
879 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800880 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
881 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000882 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700883 }
884
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400885 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000886 flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
Florian Mayer25cd9812023-03-21 16:13:36 +0000887 // TODO(fmayer): remove -Wno-error once https://reviews.llvm.org/D127917 is in Android toolchain.
888 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-error=frame-larger-than")
Florian Mayerd8434a42022-08-31 20:57:03 +0000889 flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
890 flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
Florian Mayer25cd9812023-03-21 16:13:36 +0000891 // This works around LLD complaining about the stack frame size.
892 // TODO(fmayer): remove once https://reviews.llvm.org/D127917 is in Android toolchain.
893 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-fatal-warnings")
Florian Mayerd8434a42022-08-31 20:57:03 +0000894 }
895
Mitch Phillips9036f732023-06-26 08:08:56 +0000896 if (Bool(sanProps.Memtag_heap) || Bool(sanProps.Memtag_stack) || Bool(sanProps.Memtag_globals)) && ctx.binary() {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400897 if Bool(sanProps.Diag.Memtag_heap) {
Florian Mayerd8434a42022-08-31 20:57:03 +0000898 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
899 } else {
900 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
901 }
902 }
903
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400904 if Bool(sanProps.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800905 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700906 }
907
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400908 if len(s.Properties.Sanitizers) > 0 {
909 sanitizeArg := "-fsanitize=" + strings.Join(s.Properties.Sanitizers, ",")
Colin Cross4af21ed2019-11-04 09:37:55 -0800910 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
911 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross234b01d2022-02-07 13:49:03 -0800912 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
913
Colin Crossed12a042022-02-07 13:55:55 -0800914 if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
915 // Bionic and musl sanitizer runtimes have already been added as dependencies so that
916 // the right variant of the runtime will be used (with the "-android" or "-musl"
917 // suffixes), so don't let clang the runtime library.
Colin Cross234b01d2022-02-07 13:49:03 -0800918 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-link-runtime")
919 } else {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800920 // Host sanitizers only link symbols in the final executable, so
921 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800922 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
Colin Cross6c18d002022-06-02 15:11:50 -0700923 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500924
Colin Cross6c18d002022-06-02 15:11:50 -0700925 if !ctx.toolchain().Bionic() {
926 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function san.
927 // Musl toolchain prebuilts have vptr and function sanitizers, but enabling them
928 // implicitly enables RTTI which causes RTTI mismatch issues with dependencies.
929
Colin Cross234b01d2022-02-07 13:49:03 -0800930 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500931 }
932
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400933 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700934 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800935 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700936 } else if ctx.Host() {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000937 flags.Local.CFlags = append(flags.Local.CFlags, hostOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700938 } else {
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +0000939 flags.Local.CFlags = append(flags.Local.CFlags, deviceOnlySanitizeFlags...)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700940 }
Evgenii Stepanov59012812022-06-24 11:09:18 -0700941
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400942 if enableMinimalRuntime(s) {
Evgenii Stepanov59012812022-06-24 11:09:18 -0700943 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
944 }
945
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800946 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800947 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
948 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800949 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800950 // http://b/171275751, Android doesn't build with this sanitizer yet.
951 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
952 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
953 }
Colin Cross16b23492016-01-06 14:41:07 -0800954 }
955
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400956 if len(s.Properties.DiagSanitizers) > 0 {
957 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(s.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700958 }
959 // FIXME: enable RTTI if diag + (cfi or vptr)
960
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400961 if s.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800962 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400963 strings.Join(s.Properties.Sanitize.Recover, ","))
Andreas Gampe97071162017-05-08 13:15:23 -0700964 }
965
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400966 if s.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800967 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400968 strings.Join(s.Properties.Sanitize.Diag.No_recover, ","))
Ivan Lozano7929bba2018-12-12 09:36:31 -0800969 }
970
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400971 blocklist := android.OptionalPathForModuleSrc(ctx, s.Properties.Sanitize.Blocklist)
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700972 if blocklist.Valid() {
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700973 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-ignorelist="+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700974 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
975 }
976
Colin Cross16b23492016-01-06 14:41:07 -0800977 return flags
978}
979
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400980func (s *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900981 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
982 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800983 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400984 if Bool(s.Properties.SanitizeMutated.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800985 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900986 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400987 if Bool(s.Properties.SanitizeMutated.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800988 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900989 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400990 if Bool(s.Properties.SanitizeMutated.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800991 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900992 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800993 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700994}
995
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400996func (s *sanitize) inSanitizerDir() bool {
997 return s.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700998}
999
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001000// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001001func (s *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +00001002 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001003 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001004 return s.Properties.SanitizeMutated.Address
Tri Vo6eafc362021-04-01 11:29:09 -07001005 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001006 return s.Properties.SanitizeMutated.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +00001007 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001008 return s.Properties.SanitizeMutated.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +00001009 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001010 return s.Properties.SanitizeMutated.Integer_overflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001011 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001012 return s.Properties.SanitizeMutated.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001013 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001014 return s.Properties.SanitizeMutated.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -04001015 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001016 return s.Properties.SanitizeMutated.Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +00001017 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001018 return s.Properties.SanitizeMutated.Memtag_stack
Mitch Phillips9036f732023-06-26 08:08:56 +00001019 case Memtag_globals:
1020 return s.Properties.SanitizeMutated.Memtag_globals
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001021 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001022 return s.Properties.SanitizeMutated.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +00001023 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001024 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +00001025 }
1026}
1027
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001028// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -08001029func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001030 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001031 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -08001032 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001033 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001034 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -04001035 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Florian Mayerd8434a42022-08-31 20:57:03 +00001036 !sanitize.isSanitizerEnabled(Memtag_stack) &&
Mitch Phillips9036f732023-06-26 08:08:56 +00001037 !sanitize.isSanitizerEnabled(Memtag_globals) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001038 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -08001039}
1040
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001041// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001042func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001043 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -07001044 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001045 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001046 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -07001047}
1048
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001049func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -04001050 bPtr := proptools.BoolPtr(b)
1051 if !b {
1052 bPtr = nil
1053 }
Colin Cross16b23492016-01-06 14:41:07 -08001054 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001055 case Asan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001056 sanitize.Properties.SanitizeMutated.Address = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001057 // For ASAN variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001058 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips9036f732023-06-26 08:08:56 +00001059 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Tri Vo6eafc362021-04-01 11:29:09 -07001060 case Hwasan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001061 sanitize.Properties.SanitizeMutated.Hwaddress = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001062 // For HWAsan variant, we need to disable Memtag_stack
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001063 sanitize.Properties.SanitizeMutated.Memtag_stack = nil
Mitch Phillips9036f732023-06-26 08:08:56 +00001064 sanitize.Properties.SanitizeMutated.Memtag_globals = nil
Colin Cross16b23492016-01-06 14:41:07 -08001065 case tsan:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001066 sanitize.Properties.SanitizeMutated.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -07001067 case intOverflow:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001068 sanitize.Properties.SanitizeMutated.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001069 case cfi:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001070 sanitize.Properties.SanitizeMutated.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -08001071 case scs:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001072 sanitize.Properties.SanitizeMutated.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -04001073 case Memtag_heap:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001074 sanitize.Properties.SanitizeMutated.Memtag_heap = bPtr
Florian Mayerd8434a42022-08-31 20:57:03 +00001075 case Memtag_stack:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001076 sanitize.Properties.SanitizeMutated.Memtag_stack = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -07001077 // We do not need to disable ASAN or HWASan here, as there is no Memtag_stack variant.
Mitch Phillips9036f732023-06-26 08:08:56 +00001078 case Memtag_globals:
1079 sanitize.Properties.Sanitize.Memtag_globals = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001080 case Fuzzer:
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001081 sanitize.Properties.SanitizeMutated.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -08001082 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001083 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -08001084 }
1085 if b {
1086 sanitize.Properties.SanitizerEnabled = true
1087 }
1088}
1089
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001090// Check if the sanitizer is explicitly disabled (as opposed to nil by
1091// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001092func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001093 if sanitize == nil {
1094 return false
1095 }
1096
1097 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1098 return sanitizerVal != nil && *sanitizerVal == false
1099}
1100
1101// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
1102// because enabling a sanitizer either directly (via the blueprint) or
1103// indirectly (via a mutator) sets the bool ptr to true, and you can't
1104// distinguish between the cases. It isn't needed though - both cases can be
1105// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001106func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001107 if sanitize == nil {
1108 return false
1109 }
1110
1111 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
1112 return sanitizerVal != nil && *sanitizerVal == true
1113}
1114
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001115// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
1116func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -07001117 switch t := tag.(type) {
1118 case dependencyTag:
1119 return t == reuseObjTag || t == objDepTag
1120 case libraryDependencyTag:
1121 return true
1122 default:
1123 return false
1124 }
Colin Cross6b753602018-06-21 13:03:07 -07001125}
1126
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001127func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
1128 return IsSanitizableDependencyTag
1129}
1130
Inseob Kimc42f2f22020-07-29 20:32:10 +09001131// Determines if the current module is a static library going to be captured
1132// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
1133// except for ones which explicitly disable cfi.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001134func needsCfiForVendorSnapshot(mctx android.BaseModuleContext) bool {
Justin Yun8814fc52022-12-15 21:45:35 +09001135 if inList("hwaddress", mctx.Config().SanitizeDevice()) {
1136 // cfi will not be built if SANITIZE_TARGET=hwaddress is set
1137 return false
1138 }
1139
Kiyoung Kim48f37782021-07-07 12:42:39 +09001140 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001141 return false
1142 }
1143
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001144 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001145
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001146 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001147 return false
1148 }
1149
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001150 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001151 return false
1152 }
1153
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001154 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001155 return false
1156 }
1157
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001158 if !c.SanitizerSupported(cfi) {
1159 return false
1160 }
1161
1162 return c.SanitizePropDefined() &&
1163 !c.SanitizeNever() &&
1164 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001165}
1166
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001167type sanitizerSplitMutator struct {
1168 sanitizer SanitizerType
1169}
1170
1171// If an APEX is sanitized or not depends on whether it contains at least one
1172// sanitized module. Transition mutators cannot propagate information up the
1173// dependency graph this way, so we need an auxiliary mutator to do so.
1174func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
1175 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1176 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1177 ctx.VisitDirectDeps(func(dep android.Module) {
1178 if c, ok := dep.(*Module); ok && c.sanitize.isSanitizerEnabled(s.sanitizer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001179 enabled = true
Inseob Kimc42f2f22020-07-29 20:32:10 +09001180 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001181 })
1182
1183 if enabled {
1184 sanitizeable.EnableSanitizer(s.sanitizer.name())
1185 }
1186 }
1187}
1188
1189func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
1190 if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1191 if s.sanitizer == cfi && needsCfiForVendorSnapshot(ctx) {
1192 return []string{"", s.sanitizer.variationName()}
1193 }
1194
1195 // If the given sanitizer is not requested in the .bp file for a module, it
1196 // won't automatically build the sanitized variation.
1197 if !c.IsSanitizerEnabled(s.sanitizer) {
1198 return []string{""}
1199 }
1200
1201 if c.Binary() {
1202 // If a sanitizer is enabled for a binary, we do not build the version
1203 // without the sanitizer
1204 return []string{s.sanitizer.variationName()}
1205 } else if c.StaticallyLinked() || c.Header() {
1206 // For static libraries, we build both versions. Some Make modules
1207 // apparently depend on this behavior.
1208 return []string{"", s.sanitizer.variationName()}
1209 } else {
1210 // We only build the requested variation of dynamic libraries
1211 return []string{s.sanitizer.variationName()}
1212 }
1213 }
1214
1215 if _, ok := ctx.Module().(JniSanitizeable); ok {
1216 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1217 // that is short-circuited for now
1218 return []string{""}
1219 }
1220
1221 // If an APEX has a sanitized dependency, we build the APEX in the sanitized
1222 // variation. This is useful because such APEXes require extra dependencies.
1223 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1224 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1225 if enabled {
1226 return []string{s.sanitizer.variationName()}
1227 } else {
1228 return []string{""}
1229 }
1230 }
1231
1232 if c, ok := ctx.Module().(*Module); ok {
1233 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1234
1235 // Check if it's a snapshot module supporting sanitizer
Justin Yun08270c62022-12-19 17:01:26 +09001236 if ss, ok := c.linker.(snapshotSanitizer); ok {
1237 if ss.isSanitizerAvailable(s.sanitizer) {
1238 return []string{"", s.sanitizer.variationName()}
1239 } else {
1240 return []string{""}
1241 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001242 }
1243 }
1244
1245 return []string{""}
1246}
1247
1248func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1249 if c, ok := ctx.Module().(PlatformSanitizeable); ok {
1250 if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
1251 // If the dependency is through a non-sanitizable tag, use the
1252 // non-sanitized variation
1253 return ""
1254 }
1255
1256 return sourceVariation
1257 } else if _, ok := ctx.Module().(JniSanitizeable); ok {
1258 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1259 // that is short-circuited for now
1260 return ""
1261 } else {
1262 // Otherwise, do not rock the boat.
1263 return sourceVariation
1264 }
1265}
1266
1267func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1268 if d, ok := ctx.Module().(PlatformSanitizeable); ok {
1269 if dm, ok := ctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001270 if ss, ok := dm.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001271 return incomingVariation
Inseob Kimc42f2f22020-07-29 20:32:10 +09001272 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001273 }
1274
1275 if !d.SanitizePropDefined() ||
1276 d.SanitizeNever() ||
1277 d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
1278 !d.SanitizerSupported(s.sanitizer) {
1279 // If a module opts out of a sanitizer, use its non-sanitized variation
1280 return ""
1281 }
1282
1283 // Binaries are always built in the variation they requested.
1284 if d.Binary() {
1285 if d.IsSanitizerEnabled(s.sanitizer) {
1286 return s.sanitizer.variationName()
1287 } else {
1288 return ""
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001289 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001290 }
1291
1292 // If a shared library requests to be sanitized, it will be built for that
1293 // sanitizer. Otherwise, some sanitizers propagate through shared library
1294 // dependency edges, some do not.
1295 if !d.StaticallyLinked() && !d.Header() {
1296 if d.IsSanitizerEnabled(s.sanitizer) {
1297 return s.sanitizer.variationName()
1298 }
1299
Liz Kammerfd8a49f2022-10-31 10:31:11 -04001300 // Some sanitizers do not propagate to shared dependencies
1301 if !s.sanitizer.shouldPropagateToSharedLibraryDeps() {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001302 return ""
1303 }
1304 }
1305
1306 // Static and header libraries inherit whether they are sanitized from the
1307 // module they are linked into
1308 return incomingVariation
1309 } else if d, ok := ctx.Module().(Sanitizeable); ok {
1310 // If an APEX contains a sanitized module, it will be built in the variation
1311 // corresponding to that sanitizer.
1312 enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1313 if enabled {
1314 return s.sanitizer.variationName()
1315 }
1316
1317 return incomingVariation
1318 }
1319
1320 return ""
1321}
1322
1323func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
1324 sanitizerVariation := variationName == s.sanitizer.variationName()
1325
1326 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1327 sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
1328
1329 oneMakeVariation := false
1330 if c.StaticallyLinked() || c.Header() {
1331 if s.sanitizer != cfi && s.sanitizer != scs && s.sanitizer != Hwasan {
1332 // These sanitizers export only one variation to Make. For the rest,
1333 // Make targets can depend on both the sanitized and non-sanitized
1334 // versions.
1335 oneMakeVariation = true
1336 }
1337 } else if !c.Binary() {
1338 // Shared library. These are the sanitizers that do propagate through shared
1339 // library dependencies and therefore can cause multiple variations of a
1340 // shared library to be built.
1341 if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != scs && s.sanitizer != Asan {
1342 oneMakeVariation = true
1343 }
1344 }
1345
1346 if oneMakeVariation {
1347 if sanitizerEnabled != sanitizerVariation {
1348 c.SetPreventInstall()
1349 c.SetHideFromMake()
1350 }
1351 }
1352
1353 if sanitizerVariation {
1354 c.SetSanitizer(s.sanitizer, true)
1355
1356 // CFI is incompatible with ASAN so disable it in ASAN variations
1357 if s.sanitizer.incompatibleWithCfi() {
1358 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1359 if mctx.Device() && cfiSupported {
1360 c.SetSanitizer(cfi, false)
Jiyong Parkf97782b2019-02-13 20:28:58 +09001361 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001362 }
1363
1364 // locate the asan libraries under /data/asan
1365 if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
1366 c.SetInSanitizerDir()
1367 }
1368
1369 if c.StaticallyLinked() && c.ExportedToMake() {
1370 if s.sanitizer == Hwasan {
1371 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
1372 } else if s.sanitizer == cfi {
1373 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
1374 }
1375 }
1376 } else if c.IsSanitizerEnabled(s.sanitizer) {
1377 // Disable the sanitizer for the non-sanitized variation
1378 c.SetSanitizer(s.sanitizer, false)
1379 }
1380 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
1381 // If an APEX has sanitized dependencies, it gets a few more dependencies
1382 if sanitizerVariation {
1383 sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
1384 }
1385 } else if c, ok := mctx.Module().(*Module); ok {
Justin Yun39c30312022-11-23 16:20:12 +09001386 if ss, ok := c.linker.(snapshotSanitizer); ok && ss.isSanitizerAvailable(s.sanitizer) {
1387 if !ss.isUnsanitizedVariant() {
1388 // Snapshot sanitizer may have only one variantion.
1389 // Skip exporting the module if it already has a sanitizer variation.
1390 c.SetPreventInstall()
1391 c.SetHideFromMake()
1392 return
1393 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001394 c.linker.(snapshotSanitizer).setSanitizerVariation(s.sanitizer, sanitizerVariation)
1395
1396 // Export the static lib name to make
1397 if c.static() && c.ExportedToMake() {
Justin Yun39c30312022-11-23 16:20:12 +09001398 // use BaseModuleName which is the name for Make.
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001399 if s.sanitizer == cfi {
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001400 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Justin Yun39c30312022-11-23 16:20:12 +09001401 } else if s.sanitizer == Hwasan {
1402 hwasanStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001403 }
1404 }
Colin Cross16b23492016-01-06 14:41:07 -08001405 }
1406 }
1407}
1408
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001409func (c *Module) SanitizeNever() bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001410 return Bool(c.sanitize.Properties.SanitizeMutated.Never)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001411}
1412
1413func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
1414 return c.sanitize.isSanitizerExplicitlyDisabled(t)
1415}
1416
Ivan Lozano30c5db22018-02-21 15:49:20 -08001417// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001418func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001419 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001420 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001421 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001422 mctx.WalkDeps(func(child, parent android.Module) bool {
1423 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1424 return false
1425 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001426
Inseob Kimeec88e12020-01-22 11:11:29 +09001427 d, ok := child.(*Module)
1428 if !ok || !d.static() {
1429 return false
1430 }
1431 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001432 if enableMinimalRuntime(d.sanitize) {
1433 // If a static dependency is built with the minimal runtime,
1434 // make sure we include the ubsan minimal runtime.
1435 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001436 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001437 // If a static dependency runs with full ubsan diagnostics,
1438 // make sure we include the ubsan runtime.
1439 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001440 }
Colin Cross0b908332019-06-19 23:00:20 -07001441
1442 if c.sanitize.Properties.MinimalRuntimeDep &&
1443 c.sanitize.Properties.UbsanRuntimeDep {
1444 // both flags that this mutator might set are true, so don't bother recursing
1445 return false
1446 }
1447
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001448 if c.Os() == android.Linux {
1449 c.sanitize.Properties.BuiltinsDep = true
1450 }
1451
Colin Cross0b908332019-06-19 23:00:20 -07001452 return true
Colin Cross6b753602018-06-21 13:03:07 -07001453 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001454
Jose Galmesf7294582020-11-13 12:07:36 -08001455 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001456 if Bool(p.properties.Sanitize_minimal_dep) {
1457 c.sanitize.Properties.MinimalRuntimeDep = true
1458 }
1459 if Bool(p.properties.Sanitize_ubsan_dep) {
1460 c.sanitize.Properties.UbsanRuntimeDep = true
1461 }
1462 }
1463
1464 return false
Colin Cross6b753602018-06-21 13:03:07 -07001465 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001466 }
1467}
1468
Jiyong Park379de2f2018-12-19 02:47:14 +09001469// Add the dependency to the runtime library for each of the sanitizer variants
1470func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001471 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001472 if !c.Enabled() {
1473 return
1474 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001475 var sanitizers []string
1476 var diagSanitizers []string
1477
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001478 sanProps := &c.sanitize.Properties.SanitizeMutated
1479
1480 if Bool(sanProps.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001481 sanitizers = append(sanitizers, "undefined")
1482 } else {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001483 if Bool(sanProps.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001484 sanitizers = append(sanitizers,
1485 "bool",
1486 "integer-divide-by-zero",
1487 "return",
1488 "returns-nonnull-attribute",
1489 "shift-exponent",
1490 "unreachable",
1491 "vla-bound",
1492 // TODO(danalbert): The following checks currently have compiler performance issues.
1493 //"alignment",
1494 //"bounds",
1495 //"enum",
1496 //"float-cast-overflow",
1497 //"float-divide-by-zero",
1498 //"nonnull-attribute",
1499 //"null",
1500 //"shift-base",
1501 //"signed-integer-overflow",
1502 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1503 // https://llvm.org/PR19302
1504 // http://reviews.llvm.org/D6974
1505 // "object-size",
1506 )
1507 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001508 sanitizers = append(sanitizers, sanProps.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001509 }
1510
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001511 if Bool(sanProps.Diag.Undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001512 diagSanitizers = append(diagSanitizers, "undefined")
1513 }
1514
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001515 diagSanitizers = append(diagSanitizers, sanProps.Diag.Misc_undefined...)
Jiyong Park379de2f2018-12-19 02:47:14 +09001516
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001517 if Bool(sanProps.Address) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001518 sanitizers = append(sanitizers, "address")
1519 diagSanitizers = append(diagSanitizers, "address")
1520 }
1521
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001522 if Bool(sanProps.Hwaddress) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001523 sanitizers = append(sanitizers, "hwaddress")
1524 }
1525
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001526 if Bool(sanProps.Thread) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001527 sanitizers = append(sanitizers, "thread")
1528 }
1529
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001530 if Bool(sanProps.Safestack) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001531 sanitizers = append(sanitizers, "safe-stack")
1532 }
1533
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001534 if Bool(sanProps.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001535 sanitizers = append(sanitizers, "cfi")
1536
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001537 if Bool(sanProps.Diag.Cfi) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001538 diagSanitizers = append(diagSanitizers, "cfi")
1539 }
1540 }
1541
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001542 if Bool(sanProps.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001543 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1544 sanitizers = append(sanitizers, "signed-integer-overflow")
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001545 if Bool(sanProps.Diag.Integer_overflow) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001546 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1547 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1548 }
1549 }
1550
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001551 if Bool(sanProps.Scudo) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001552 sanitizers = append(sanitizers, "scudo")
1553 }
1554
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001555 if Bool(sanProps.Scs) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001556 sanitizers = append(sanitizers, "shadow-call-stack")
1557 }
1558
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001559 if Bool(sanProps.Memtag_heap) && c.Binary() {
Florian Mayerd8434a42022-08-31 20:57:03 +00001560 sanitizers = append(sanitizers, "memtag-heap")
1561 }
1562
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001563 if Bool(sanProps.Memtag_stack) {
Florian Mayerd8434a42022-08-31 20:57:03 +00001564 sanitizers = append(sanitizers, "memtag-stack")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001565 }
1566
Mitch Phillips9036f732023-06-26 08:08:56 +00001567 if Bool(sanProps.Memtag_globals) {
1568 sanitizers = append(sanitizers, "memtag-globals")
1569 // TODO(mitchp): For now, enable memtag-heap with memtag-globals because the linker
1570 // isn't new enough (https://reviews.llvm.org/differential/changeset/?ref=4243566).
1571 sanitizers = append(sanitizers, "memtag-heap")
1572 }
1573
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001574 if Bool(sanProps.Fuzzer) {
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001575 sanitizers = append(sanitizers, "fuzzer-no-link")
1576 }
1577
Jiyong Park379de2f2018-12-19 02:47:14 +09001578 // Save the list of sanitizers. These will be used again when generating
1579 // the build rules (for Cflags, etc.)
1580 c.sanitize.Properties.Sanitizers = sanitizers
1581 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1582
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001583 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1584 if c.Host() {
1585 diagSanitizers = sanitizers
1586 }
1587
Colin Crosse323a792023-02-15 13:57:57 -08001588 addStaticDeps := func(dep string, hideSymbols bool) {
Colin Cross06c80eb2022-02-10 10:34:19 -08001589 // If we're using snapshots, redirect to snapshot whenever possible
1590 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001591 if lib, ok := snapshot.StaticLibs[dep]; ok {
1592 dep = lib
Colin Cross06c80eb2022-02-10 10:34:19 -08001593 }
1594
1595 // static executable gets static runtime libs
Colin Crosse323a792023-02-15 13:57:57 -08001596 depTag := libraryDependencyTag{Kind: staticLibraryDependency, unexportedSymbols: hideSymbols}
Colin Cross06c80eb2022-02-10 10:34:19 -08001597 variations := append(mctx.Target().Variations(),
1598 blueprint.Variation{Mutator: "link", Variation: "static"})
1599 if c.Device() {
1600 variations = append(variations, c.ImageVariation())
1601 }
1602 if c.UseSdk() {
1603 variations = append(variations,
1604 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1605 }
Colin Crosse323a792023-02-15 13:57:57 -08001606 mctx.AddFarVariationDependencies(variations, depTag, dep)
Colin Cross06c80eb2022-02-10 10:34:19 -08001607 }
Colin Crosse323a792023-02-15 13:57:57 -08001608
1609 // Determine the runtime library required
1610 runtimeSharedLibrary := ""
1611 toolchain := c.toolchain(mctx)
1612 if Bool(sanProps.Address) {
Colin Crossb781d232023-02-15 12:40:20 -08001613 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1614 // Use a static runtime for musl to match what clang does for glibc.
1615 addStaticDeps(config.AddressSanitizerStaticRuntimeLibrary(toolchain), false)
1616 addStaticDeps(config.AddressSanitizerCXXStaticRuntimeLibrary(toolchain), false)
1617 } else {
1618 runtimeSharedLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1619 }
Colin Crosse323a792023-02-15 13:57:57 -08001620 } else if Bool(sanProps.Hwaddress) {
1621 if c.staticBinary() {
1622 addStaticDeps(config.HWAddressSanitizerStaticLibrary(toolchain), true)
1623 addStaticDeps("libdl", false)
1624 } else {
1625 runtimeSharedLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1626 }
1627 } else if Bool(sanProps.Thread) {
1628 runtimeSharedLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1629 } else if Bool(sanProps.Scudo) {
1630 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1631 runtimeSharedLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1632 } else {
1633 runtimeSharedLibrary = config.ScudoRuntimeLibrary(toolchain)
1634 }
1635 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
1636 Bool(sanProps.Fuzzer) ||
1637 Bool(sanProps.Undefined) ||
1638 Bool(sanProps.All_undefined) {
1639 if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
1640 // Use a static runtime for static binaries.
1641 // Also use a static runtime for musl to match
1642 // what clang does for glibc. Otherwise dlopening
1643 // libraries that depend on libclang_rt.ubsan_standalone.so
1644 // fails with:
1645 // Error relocating ...: initial-exec TLS resolves to dynamic definition
1646 addStaticDeps(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)+".static", true)
1647 } else {
1648 runtimeSharedLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
1649 }
1650 }
1651
Colin Cross06c80eb2022-02-10 10:34:19 -08001652 if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
Colin Crosse323a792023-02-15 13:57:57 -08001653 addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001654 }
1655 if c.sanitize.Properties.BuiltinsDep {
Colin Crosse323a792023-02-15 13:57:57 -08001656 addStaticDeps(config.BuiltinsRuntimeLibrary(toolchain), true)
Colin Cross06c80eb2022-02-10 10:34:19 -08001657 }
1658
Colin Crosse323a792023-02-15 13:57:57 -08001659 if runtimeSharedLibrary != "" && (toolchain.Bionic() || toolchain.Musl() || c.sanitize.Properties.UbsanRuntimeDep) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001660 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001661
1662 // Adding dependency to the runtime library. We are using *FarVariation*
1663 // because the runtime libraries themselves are not mutated by sanitizer
1664 // mutators and thus don't have sanitizer variants whereas this module
1665 // has been already mutated.
1666 //
1667 // Note that by adding dependency with {static|shared}DepTag, the lib is
1668 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
Colin Crosse323a792023-02-15 13:57:57 -08001669 if c.staticBinary() {
1670 // Most sanitizers are either disabled for static binaries or have already
1671 // handled the static binary case above through a direct call to addStaticDeps.
1672 // If not, treat the runtime shared library as a static library and hope for
1673 // the best.
1674 addStaticDeps(runtimeSharedLibrary, true)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001675 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001676 // If we're using snapshots, redirect to snapshot whenever possible
1677 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
Colin Crosse323a792023-02-15 13:57:57 -08001678 if lib, ok := snapshot.SharedLibs[runtimeSharedLibrary]; ok {
1679 runtimeSharedLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001680 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001681
Cindy Zhou18417cb2020-12-10 07:12:38 -08001682 // Skip apex dependency check for sharedLibraryDependency
1683 // when sanitizer diags are enabled. Skipping the check will allow
1684 // building with diag libraries without having to list the
1685 // dependency in Apex's allowed_deps file.
1686 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001687 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001688 depTag := libraryDependencyTag{
1689 Kind: sharedLibraryDependency,
1690 Order: earlyLibraryDependency,
1691
1692 skipApexAllowedDependenciesCheck: diagEnabled,
1693 }
Colin Cross42507332020-08-21 16:15:23 -07001694 variations := append(mctx.Target().Variations(),
1695 blueprint.Variation{Mutator: "link", Variation: "shared"})
1696 if c.Device() {
1697 variations = append(variations, c.ImageVariation())
1698 }
Colin Cross06c80eb2022-02-10 10:34:19 -08001699 if c.UseSdk() {
1700 variations = append(variations,
1701 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1702 }
Colin Crosse323a792023-02-15 13:57:57 -08001703 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeSharedLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001704 }
1705 // static lib does not have dependency to the runtime library. The
1706 // dependency will be added to the executables or shared libs using
1707 // the static lib.
1708 }
1709 }
1710}
1711
1712type Sanitizeable interface {
1713 android.Module
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001714 IsSanitizerEnabled(config android.Config, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001715 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001716 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001717}
1718
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001719type JniSanitizeable interface {
1720 android.Module
1721 IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
1722}
1723
Ivan Lozanod7586b62021-04-01 09:49:36 -04001724func (c *Module) MinimalRuntimeDep() bool {
1725 return c.sanitize.Properties.MinimalRuntimeDep
1726}
1727
1728func (c *Module) UbsanRuntimeDep() bool {
1729 return c.sanitize.Properties.UbsanRuntimeDep
1730}
1731
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001732func (c *Module) SanitizePropDefined() bool {
1733 return c.sanitize != nil
1734}
1735
1736func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1737 return c.sanitize.isSanitizerEnabled(t)
1738}
1739
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001740func (c *Module) StaticallyLinked() bool {
1741 return c.static()
1742}
1743
1744func (c *Module) SetInSanitizerDir() {
1745 if c.sanitize != nil {
1746 c.sanitize.Properties.InSanitizerDir = true
1747 }
1748}
1749
1750func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1751 if c.sanitize != nil {
1752 c.sanitize.SetSanitizer(t, b)
1753 }
1754}
1755
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001756var _ PlatformSanitizeable = (*Module)(nil)
1757
Inseob Kim74d25562020-08-04 00:41:38 +09001758type sanitizerStaticLibsMap struct {
1759 // libsMap contains one list of modules per each image and each arch.
1760 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001761 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001762 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001763 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001764}
1765
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001766func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001767 return &sanitizerStaticLibsMap{
1768 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001769 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001770 }
1771}
1772
1773// Add the current module to sanitizer static libs maps
1774// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001775func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1776 image := GetImageVariantType(c)
1777 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001778
1779 s.libsMapLock.Lock()
1780 defer s.libsMapLock.Unlock()
1781
1782 if _, ok := s.libsMap[image]; !ok {
1783 s.libsMap[image] = make(map[string][]string)
1784 }
1785
1786 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1787}
1788
1789// Exports makefile variables in the following format:
1790// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1791// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1792// These are to be used by use_soong_sanitized_static_libraries.
1793// See build/make/core/binary.mk for more details.
1794func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
Cole Faust18994c72023-02-28 16:02:16 -08001795 for _, image := range android.SortedKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001796 archMap := s.libsMap[ImageVariantType(image)]
Cole Faust18994c72023-02-28 16:02:16 -08001797 for _, arch := range android.SortedKeys(archMap) {
Inseob Kim74d25562020-08-04 00:41:38 +09001798 libs := archMap[arch]
1799 sort.Strings(libs)
1800
1801 key := fmt.Sprintf(
1802 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1803 s.sanitizerType.variationName(),
1804 image, // already upper
1805 arch)
1806
1807 ctx.Strict(key, strings.Join(libs, " "))
1808 }
1809 }
1810}
1811
Colin Cross571cccf2019-02-04 11:22:08 -08001812var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1813
Inseob Kim74d25562020-08-04 00:41:38 +09001814func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001815 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001816 return newSanitizerStaticLibsMap(cfi)
1817 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001818}
1819
Colin Cross571cccf2019-02-04 11:22:08 -08001820var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1821
Inseob Kim74d25562020-08-04 00:41:38 +09001822func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001823 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001824 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001825 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001826}
1827
Ivan Lozano30c5db22018-02-21 15:49:20 -08001828func enableMinimalRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001829 if sanitize.isSanitizerEnabled(Asan) {
1830 return false
1831 } else if sanitize.isSanitizerEnabled(Hwasan) {
1832 return false
1833 } else if sanitize.isSanitizerEnabled(Fuzzer) {
1834 return false
Ivan Lozano30c5db22018-02-21 15:49:20 -08001835 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001836
1837 if enableUbsanRuntime(sanitize) {
1838 return false
1839 }
1840
1841 sanitizeProps := &sanitize.Properties.SanitizeMutated
1842 if Bool(sanitizeProps.Diag.Cfi) {
1843 return false
1844 }
1845
1846 return Bool(sanitizeProps.Integer_overflow) ||
1847 len(sanitizeProps.Misc_undefined) > 0 ||
1848 Bool(sanitizeProps.Undefined) ||
1849 Bool(sanitizeProps.All_undefined)
Ivan Lozano30c5db22018-02-21 15:49:20 -08001850}
1851
Ivan Lozanod7586b62021-04-01 09:49:36 -04001852func (m *Module) UbsanRuntimeNeeded() bool {
1853 return enableUbsanRuntime(m.sanitize)
1854}
1855
1856func (m *Module) MinimalRuntimeNeeded() bool {
1857 return enableMinimalRuntime(m.sanitize)
1858}
1859
Inseob Kim8471cda2019-11-15 09:59:12 +09001860func enableUbsanRuntime(sanitize *sanitize) bool {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -04001861 sanitizeProps := &sanitize.Properties.SanitizeMutated
1862 return Bool(sanitizeProps.Diag.Integer_overflow) ||
1863 Bool(sanitizeProps.Diag.Undefined) ||
1864 len(sanitizeProps.Diag.Misc_undefined) > 0
Inseob Kim8471cda2019-11-15 09:59:12 +09001865}
1866
Vishwath Mohane7128792017-11-17 11:08:10 -08001867func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001868 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001869}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001870
1871func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001872 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001873}
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +00001874
1875func BazelCcSanitizerToolchainVars(config android.Config) string {
1876 return android.BazelToolchainVars(config, exportedVars)
1877}