blob: d3fc221b419825abaf16715e942386dbdb0aafbe [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
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000065 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070066 "-fsanitize-ignorelist=external/compiler-rt/lib/cfi/cfi_blocklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070067 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
68 // used, but have no effect on assembly files
69 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070070 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070071 "-Wl,-plugin-opt,O1"}
Inseob Kim74d25562020-08-04 00:41:38 +090072 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070073
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070074 intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080075
Peter Collingbournebd19db02019-03-06 10:38:48 -080076 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070077 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070078 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
Mitch Phillips59760562022-03-22 18:33:44 +000079 "export_memory_stats=0", "max_malloc_fill_size=4096", "malloc_fill_byte=0"}
Florian Mayerd8434a42022-08-31 20:57:03 +000080 memtagStackCommonFlags = []string{"-march=armv8-a+memtag"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070081)
82
Ivan Lozano3968d8f2020-12-14 11:27:52 -050083type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080084
Colin Cross16b23492016-01-06 14:41:07 -080085const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050086 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070087 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080088 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070089 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080090 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -050091 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -040092 Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +000093 Memtag_stack
Liz Kammer75db9312021-07-07 16:41:50 -040094 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -080095)
96
Liz Kammer75db9312021-07-07 16:41:50 -040097var Sanitizers = []SanitizerType{
98 Asan,
99 Hwasan,
100 tsan,
101 intOverflow,
102 scs,
103 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400104 Memtag_heap,
Florian Mayerd8434a42022-08-31 20:57:03 +0000105 Memtag_stack,
Liz Kammer75db9312021-07-07 16:41:50 -0400106 cfi, // cfi is last to prevent it running before incompatible mutators
107}
108
Jiyong Park82226632019-02-01 10:50:50 +0900109// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500110func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800111 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500112 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800113 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700114 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700115 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800116 case tsan:
117 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700118 case intOverflow:
119 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000120 case cfi:
121 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800122 case scs:
123 return "scs"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400124 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700125 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000126 case Memtag_stack:
127 return "memtag_stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500128 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700129 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800130 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500131 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800132 }
133}
134
Jiyong Park82226632019-02-01 10:50:50 +0900135// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500136func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900137 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500138 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900139 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700140 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900141 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400142 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700143 return "memtag_heap"
Florian Mayerd8434a42022-08-31 20:57:03 +0000144 case Memtag_stack:
145 return "memtag_stack"
Jiyong Park82226632019-02-01 10:50:50 +0900146 case tsan:
147 return "thread"
148 case intOverflow:
149 return "integer_overflow"
150 case cfi:
151 return "cfi"
152 case scs:
153 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500154 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700155 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900156 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500157 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900158 }
159}
160
Liz Kammer75db9312021-07-07 16:41:50 -0400161func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
162 switch t {
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200163 case cfi, Hwasan, Asan, tsan, Fuzzer, scs:
164 sanitizer := &sanitizerSplitMutator{t}
165 ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
166 ctx.Transition(t.variationName(), sanitizer)
Florian Mayerd8434a42022-08-31 20:57:03 +0000167 case Memtag_heap, Memtag_stack, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400168 // do nothing
169 default:
170 panic(fmt.Errorf("unknown SanitizerType %d", t))
171 }
172}
173
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500174func (*Module) SanitizerSupported(t SanitizerType) bool {
175 switch t {
176 case Asan:
177 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700178 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500179 return true
180 case tsan:
181 return true
182 case intOverflow:
183 return true
184 case cfi:
185 return true
186 case scs:
187 return true
188 case Fuzzer:
189 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400190 case Memtag_heap:
191 return true
Florian Mayerd8434a42022-08-31 20:57:03 +0000192 case Memtag_stack:
193 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500194 default:
195 return false
196 }
197}
198
199// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
200func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700201 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900202}
203
Martin Stjernholmb0249572020-09-15 02:32:35 +0100204type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400205 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100206 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800207
Liz Kammer75b9b402021-06-25 15:19:27 -0400208 // ASan (Address sanitizer), incompatible with static binaries.
209 // Always runs in a diagnostic mode.
210 // Use of address sanitizer disables cfi sanitizer.
211 // Hwaddress sanitizer takes precedence over this sanitizer.
212 Address *bool `android:"arch_variant"`
213 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
214 // Always runs in a diagnostic mode.
215 // Use of thread sanitizer disables cfi and scudo sanitizers.
216 // Hwaddress sanitizer takes precedence over this sanitizer.
217 Thread *bool `android:"arch_variant"`
218 // HWASan (Hardware Address sanitizer).
219 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100220 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800221
Liz Kammer75b9b402021-06-25 15:19:27 -0400222 // Undefined behavior sanitizer
223 All_undefined *bool `android:"arch_variant"`
224 // Subset of undefined behavior sanitizer
225 Undefined *bool `android:"arch_variant"`
226 // List of specific undefined behavior sanitizers to enable
227 Misc_undefined []string `android:"arch_variant"`
228 // Fuzzer, incompatible with static binaries.
229 Fuzzer *bool `android:"arch_variant"`
230 // safe-stack sanitizer, incompatible with 32-bit architectures.
231 Safestack *bool `android:"arch_variant"`
232 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
233 Cfi *bool `android:"arch_variant"`
234 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
235 Integer_overflow *bool `android:"arch_variant"`
236 // scudo sanitizer, incompatible with asan, hwasan, tsan
237 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
238 // deprecated
239 Scudo *bool `android:"arch_variant"`
240 // shadow-call-stack sanitizer, only available on arm64
241 Scs *bool `android:"arch_variant"`
242 // Memory-tagging, only available on arm64
243 // if diag.memtag unset or false, enables async memory tagging
Florian Mayer00ab5cf2022-08-31 18:30:18 +0000244 Memtag_heap *bool `android:"arch_variant"`
Florian Mayerd8434a42022-08-31 20:57:03 +0000245 // Memory-tagging stack instrumentation, only available on arm64
246 // Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
247 Memtag_stack *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100248
249 // A modifier for ASAN and HWASAN for write only instrumentation
250 Writeonly *bool `android:"arch_variant"`
251
252 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
253 // Replaces abort() on error with a human-readable error message.
254 // Address and Thread sanitizers always run in diagnostic mode.
255 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400256 // Undefined behavior sanitizer, diagnostic mode
257 Undefined *bool `android:"arch_variant"`
258 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
259 Cfi *bool `android:"arch_variant"`
260 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
261 Integer_overflow *bool `android:"arch_variant"`
262 // Memory-tagging, only available on arm64
263 // requires sanitizer.memtag: true
264 // if set, enables sync memory tagging
265 Memtag_heap *bool `android:"arch_variant"`
266 // List of specific undefined behavior sanitizers to enable in diagnostic mode
267 Misc_undefined []string `android:"arch_variant"`
268 // List of sanitizers to pass to -fno-sanitize-recover
269 // results in only the first detected error for these sanitizers being reported and program then
270 // exits with a non-zero exit code.
271 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800272 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800273
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800274 // Sanitizers to run with flag configuration specified
275 Config struct {
276 // Enables CFI support flags for assembly-heavy libraries
277 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800278 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800279
Liz Kammer75b9b402021-06-25 15:19:27 -0400280 // List of sanitizers to pass to -fsanitize-recover
281 // allows execution to continue for these sanitizers to detect multiple errors rather than only
282 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100283 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000284
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700285 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100286 Blocklist *string
287}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700288
Martin Stjernholmb0249572020-09-15 02:32:35 +0100289type SanitizeProperties struct {
Martin Stjernholmb0249572020-09-15 02:32:35 +0100290 Sanitize SanitizeUserProps `android:"arch_variant"`
291 SanitizerEnabled bool `blueprint:"mutated"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100292 MinimalRuntimeDep bool `blueprint:"mutated"`
293 BuiltinsDep bool `blueprint:"mutated"`
294 UbsanRuntimeDep bool `blueprint:"mutated"`
295 InSanitizerDir bool `blueprint:"mutated"`
296 Sanitizers []string `blueprint:"mutated"`
297 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800298}
299
300type sanitize struct {
301 Properties SanitizeProperties
302}
303
Cindy Zhou18417cb2020-12-10 07:12:38 -0800304// Mark this tag with a check to see if apex dependency check should be skipped
305func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
306 return t.skipApexAllowedDependenciesCheck
307}
308
309var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
310
Vishwath Mohane7128792017-11-17 11:08:10 -0800311func init() {
312 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700313 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800314}
315
Colin Cross16b23492016-01-06 14:41:07 -0800316func (sanitize *sanitize) props() []interface{} {
317 return []interface{}{&sanitize.Properties}
318}
319
320func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700321 s := &sanitize.Properties.Sanitize
322
Colin Cross16b23492016-01-06 14:41:07 -0800323 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700324 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800325 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800326 }
327
328 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800329 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800330 return
331 }
332
Florian Mayerd8434a42022-08-31 20:57:03 +0000333 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400334 if ctx.testBinary() {
335 if s.Memtag_heap == nil {
336 s.Memtag_heap = proptools.BoolPtr(true)
337 }
338 if s.Diag.Memtag_heap == nil {
339 s.Diag.Memtag_heap = proptools.BoolPtr(true)
340 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800341 }
342
Colin Cross16b23492016-01-06 14:41:07 -0800343 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700344 var globalSanitizersDiag []string
345
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700346 if ctx.Host() {
347 if !ctx.Windows() {
348 globalSanitizers = ctx.Config().SanitizeHost()
349 }
350 } else {
351 arches := ctx.Config().SanitizeDeviceArch()
352 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
353 globalSanitizers = ctx.Config().SanitizeDevice()
354 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800355 }
356 }
357
Colin Cross16b23492016-01-06 14:41:07 -0800358 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000359 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700360 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400361 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000362 }
Colin Cross16b23492016-01-06 14:41:07 -0800363
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700364 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400365 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000366 }
367
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700368 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400369 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000370 }
371
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700372 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400373 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000374 }
375
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700376 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400377 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700378 }
379
380 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400381 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000382 }
383
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700384 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800385 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400386 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700387 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700388 }
389
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700390 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700391 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700392 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400393 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700394 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700395 }
396
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700397 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400398 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700399 }
400
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700401 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400402 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700403 }
404
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000405 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
406 // Hwaddress and Address are set before, so we can check them here
407 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
408 if s.Address == nil && s.Hwaddress == nil {
409 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
410 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400411 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000412 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700413 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800414 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400415 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800416 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700417 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000418
Florian Mayerd8434a42022-08-31 20:57:03 +0000419 if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
420 s.Memtag_stack = proptools.BoolPtr(true)
421 }
422
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000423 if len(globalSanitizers) > 0 {
424 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
425 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700426
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700427 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700428 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700429 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400430 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700431 }
432
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700433 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
434 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400435 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700436 }
437
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800438 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
439 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400440 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800441 }
442
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700443 if len(globalSanitizersDiag) > 0 {
444 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
445 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700446 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700447
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800448 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross88a029f2022-06-23 14:51:20 -0700449 if ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800450 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800451 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400452 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800453 }
454 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400455 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800456 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800457 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800458 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400459 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800460 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800461 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700462 }
463
Elvis Chien9c993542021-06-25 01:15:17 +0800464 // Enable CFI for non-host components in the include paths
465 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400466 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000467 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400468 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700469 }
470 }
471
Elliott Hughesda3a0712020-03-06 16:55:28 -0800472 // Is CFI actually enabled?
473 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400474 s.Cfi = nil
475 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800476 }
477
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700478 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross88a029f2022-06-23 14:51:20 -0700479 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700480 s.Hwaddress = nil
481 }
482
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800483 // SCS is only implemented on AArch64.
Colin Cross88a029f2022-06-23 14:51:20 -0700484 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800485 s.Scs = nil
486 }
487
Ivan Lozano62cd0382021-11-01 10:27:54 -0400488 // Memtag_heap is only implemented on AArch64.
Florian Mayerd8434a42022-08-31 20:57:03 +0000489 // Memtag ABI is Android specific for now, so disable for host.
490 if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() || ctx.Host() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700491 s.Memtag_heap = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000492 s.Memtag_stack = nil
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700493 }
494
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700495 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700496 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400497 s.Cfi = nil
498 s.Diag.Cfi = nil
Florian Mayerd8434a42022-08-31 20:57:03 +0000499 // HWASAN and ASAN win against MTE.
500 s.Memtag_heap = nil
501 s.Memtag_stack = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700502 }
503
Colin Crossed12a042022-02-07 13:55:55 -0800504 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
505 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400506 s.Cfi = nil
507 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700508 s.Misc_undefined = nil
509 s.Undefined = nil
510 s.All_undefined = nil
511 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800512 }
513
Colin Cross2c435a02022-10-20 13:55:53 -0700514 // TODO(b/254713216): CFI doesn't work for riscv64 yet because LTO doesn't work.
Colin Crossecf4e662022-10-20 13:59:17 -0700515 if ctx.Arch().ArchType == android.Riscv64 {
516 s.Cfi = nil
517 s.Diag.Cfi = nil
518 }
519
Colin Crossed12a042022-02-07 13:55:55 -0800520 // Disable CFI for musl
521 if ctx.toolchain().Musl() {
522 s.Cfi = nil
523 s.Diag.Cfi = nil
524 }
525
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700526 // Also disable CFI for VNDK variants of components
527 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900528 if ctx.static() {
529 // Cfi variant for static vndk should be captured as vendor snapshot,
530 // so don't strictly disable Cfi.
531 s.Cfi = nil
532 s.Diag.Cfi = nil
533 } else {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400534 s.Cfi = nil
535 s.Diag.Cfi = nil
Inseob Kimc42f2f22020-07-29 20:32:10 +0900536 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900537 }
538
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700539 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700540 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
541 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700542 s.Hwaddress = nil
543 }
544
Colin Cross3c344ef2016-07-18 15:44:56 -0700545 if ctx.staticBinary() {
546 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700547 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700548 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800549 }
550
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700551 if Bool(s.All_undefined) {
552 s.Undefined = nil
553 }
554
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700555 if !ctx.toolchain().Is64Bit() {
556 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700557 s.Thread = nil
558 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800559 // TODO(ccross): error for compile_multilib = "32"?
560 }
561
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800562 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 -0700563 Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Florian Mayerd8434a42022-08-31 20:57:03 +0000564 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700565 sanitize.Properties.SanitizerEnabled = true
566 }
567
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800568 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
569 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700570 s.Scudo = nil
571 }
572
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700573 if Bool(s.Hwaddress) {
574 s.Address = nil
575 s.Thread = nil
576 }
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000577
578 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
579 // mutually incompatible.
580 if Bool(s.Fuzzer) {
581 s.Cfi = nil
582 }
Colin Cross16b23492016-01-06 14:41:07 -0800583}
584
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800585func toDisableImplicitIntegerChange(flags []string) bool {
586 // Returns true if any flag is fsanitize*integer, and there is
587 // no explicit flag about sanitize=implicit-integer-sign-change.
588 for _, f := range flags {
589 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
590 return false
591 }
592 }
593 for _, f := range flags {
594 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
595 return true
596 }
597 }
598 return false
599}
600
Yabin Cuidb7dda82020-11-30 15:47:45 -0800601func toDisableUnsignedShiftBaseChange(flags []string) bool {
602 // Returns true if any flag is fsanitize*integer, and there is
603 // no explicit flag about sanitize=unsigned-shift-base.
604 for _, f := range flags {
605 if strings.Contains(f, "sanitize=unsigned-shift-base") {
606 return false
607 }
608 }
609 for _, f := range flags {
610 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
611 return true
612 }
613 }
614 return false
615}
616
Colin Cross16b23492016-01-06 14:41:07 -0800617func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700618 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800619 return flags
620 }
621
Florian Mayer7410d282022-10-19 14:06:26 -0700622 // Currently unwinding through tagged frames for exceptions is broken, so disable memtag stack
623 // in that case, so we don't end up tagging those.
624 // TODO(b/174878242): Remove once https://r.android.com/2251926 is included in toolchain.
625 if android.InList("-fexceptions", flags.Local.CFlags) || android.InList("-fexceptions", flags.Global.CFlags) {
626 sanitize.Properties.Sanitize.Memtag_stack = nil
627 _, sanitize.Properties.Sanitizers = android.RemoveFromList("memtag-stack", sanitize.Properties.Sanitizers)
628 }
629
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700630 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700631 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800632 // Frame pointer based unwinder in ASan requires ARM frame setup.
633 // TODO: put in flags?
634 flags.RequiredInstructionSet = "arm"
635 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800636 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
637 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800638
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000639 if Bool(sanitize.Properties.Sanitize.Writeonly) {
640 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
641 }
642
Colin Cross16b23492016-01-06 14:41:07 -0800643 if ctx.Host() {
644 // -nodefaultlibs (provided with libc++) prevents the driver from linking
645 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800646 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800647 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800648 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900649 if ctx.bootstrap() {
650 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
651 } else {
652 flags.DynamicLinker = "/system/bin/linker_asan"
653 }
Colin Cross16b23492016-01-06 14:41:07 -0800654 if flags.Toolchain.Is64Bit() {
655 flags.DynamicLinker += "64"
656 }
657 }
Colin Cross16b23492016-01-06 14:41:07 -0800658 }
659
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700660 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800661 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800662
663 for _, flag := range hwasanCommonflags {
664 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
665 }
666 for _, flag := range hwasanCommonflags {
667 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
668 }
669
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000670 if Bool(sanitize.Properties.Sanitize.Writeonly) {
671 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
672 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700673 }
674
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700675 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800676 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700677
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000678 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
679 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
680 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
681 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
682 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
683
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700684 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
685 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
686 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800687 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
688 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700689
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700690 // Disable fortify for fuzzing builds. Generally, we'll be building with
691 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800692 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800693
694 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
695 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
696 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
697 // the DT_RUNPATH from the shared library above it, and not the executable,
698 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
699 // DT_RUNPATH here means that transient shared libraries can be found
700 // colocated with their parents.
701 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800702 }
703
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700704 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800705 if ctx.Arch().ArchType == android.Arm {
706 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
707 // to do this on a function basis, so force Thumb on the entire module.
708 flags.RequiredInstructionSet = "thumb"
709 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000710
Colin Cross4af21ed2019-11-04 09:37:55 -0800711 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
712 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800713 if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
714 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
715 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000716 // Only append the default visibility flag if -fvisibility has not already been set
717 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800718 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
719 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000720 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800721 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000722
723 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800724 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
725 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000726 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700727 }
728
Florian Mayerd8434a42022-08-31 20:57:03 +0000729 if Bool(sanitize.Properties.Sanitize.Memtag_stack) {
730 flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
Florian Mayer1e4f8992022-09-08 16:47:32 -0700731 // TODO(fmayer): remove -Wno-error once https://reviews.llvm.org/D127917 is in Android toolchain.
732 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-error=frame-larger-than")
Florian Mayerd8434a42022-08-31 20:57:03 +0000733 flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
734 flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
Florian Mayerf79b2102022-09-09 14:23:31 -0700735 // This works around LLD complaining about the stack frame size.
736 // TODO(fmayer): remove once https://reviews.llvm.org/D127917 is in Android toolchain.
737 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-fatal-warnings")
Florian Mayerd8434a42022-08-31 20:57:03 +0000738 }
739
740 if (Bool(sanitize.Properties.Sanitize.Memtag_heap) || Bool(sanitize.Properties.Sanitize.Memtag_stack)) && ctx.binary() {
741 if Bool(sanitize.Properties.Sanitize.Diag.Memtag_heap) {
742 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
743 } else {
744 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
745 }
746 }
747
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700748 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800749 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700750 }
751
Jiyong Park379de2f2018-12-19 02:47:14 +0900752 if len(sanitize.Properties.Sanitizers) > 0 {
753 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Colin Cross4af21ed2019-11-04 09:37:55 -0800754 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
755 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross234b01d2022-02-07 13:49:03 -0800756 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
757
Colin Crossed12a042022-02-07 13:55:55 -0800758 if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
759 // Bionic and musl sanitizer runtimes have already been added as dependencies so that
760 // the right variant of the runtime will be used (with the "-android" or "-musl"
761 // suffixes), so don't let clang the runtime library.
Colin Cross234b01d2022-02-07 13:49:03 -0800762 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-link-runtime")
763 } else {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800764 // Host sanitizers only link symbols in the final executable, so
765 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800766 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
Colin Cross6c18d002022-06-02 15:11:50 -0700767 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500768
Colin Cross6c18d002022-06-02 15:11:50 -0700769 if !ctx.toolchain().Bionic() {
770 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function san.
771 // Musl toolchain prebuilts have vptr and function sanitizers, but enabling them
772 // implicitly enables RTTI which causes RTTI mismatch issues with dependencies.
773
Colin Cross234b01d2022-02-07 13:49:03 -0800774 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500775 }
776
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700777 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
778 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800779 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700780 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800781 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700782 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800783 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700784 }
Evgenii Stepanov59012812022-06-24 11:09:18 -0700785
786 if enableMinimalRuntime(sanitize) {
787 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
788 }
789
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800790 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800791 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
792 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800793 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800794 // http://b/171275751, Android doesn't build with this sanitizer yet.
795 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
796 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
797 }
Colin Cross16b23492016-01-06 14:41:07 -0800798 }
799
Jiyong Park379de2f2018-12-19 02:47:14 +0900800 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800801 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700802 }
803 // FIXME: enable RTTI if diag + (cfi or vptr)
804
Andreas Gampe97071162017-05-08 13:15:23 -0700805 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800806 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700807 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
808 }
809
Ivan Lozano7929bba2018-12-12 09:36:31 -0800810 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800811 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800812 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
813 }
814
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700815 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
816 if blocklist.Valid() {
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700817 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-ignorelist="+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700818 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
819 }
820
Colin Cross16b23492016-01-06 14:41:07 -0800821 return flags
822}
823
Colin Crossd80cbca2020-02-24 12:01:37 -0800824func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900825 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
826 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800827 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900828 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800829 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900830 }
831 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800832 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900833 }
834 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800835 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900836 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800837 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700838}
839
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700840func (sanitize *sanitize) inSanitizerDir() bool {
841 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700842}
843
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500844// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
845func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000846 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500847 case Asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000848 return sanitize.Properties.Sanitize.Address
Tri Vo6eafc362021-04-01 11:29:09 -0700849 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700850 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000851 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000852 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000853 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000854 return sanitize.Properties.Sanitize.Integer_overflow
855 case cfi:
856 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800857 case scs:
858 return sanitize.Properties.Sanitize.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -0400859 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700860 return sanitize.Properties.Sanitize.Memtag_heap
Florian Mayerd8434a42022-08-31 20:57:03 +0000861 case Memtag_stack:
862 return sanitize.Properties.Sanitize.Memtag_stack
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500863 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700864 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000865 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500866 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000867 }
868}
869
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500870// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800871func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500872 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700873 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800874 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800875 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700876 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -0400877 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Florian Mayerd8434a42022-08-31 20:57:03 +0000878 !sanitize.isSanitizerEnabled(Memtag_stack) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500879 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800880}
881
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500882// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700883func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500884 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700885 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700886 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500887 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700888}
889
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500890func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400891 bPtr := proptools.BoolPtr(b)
892 if !b {
893 bPtr = nil
894 }
Colin Cross16b23492016-01-06 14:41:07 -0800895 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500896 case Asan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400897 sanitize.Properties.Sanitize.Address = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -0700898 // For ASAN variant, we need to disable Memtag_stack
899 sanitize.Properties.Sanitize.Memtag_stack = nil
Tri Vo6eafc362021-04-01 11:29:09 -0700900 case Hwasan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400901 sanitize.Properties.Sanitize.Hwaddress = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -0700902 // For HWAsan variant, we need to disable Memtag_stack
903 sanitize.Properties.Sanitize.Memtag_stack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800904 case tsan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400905 sanitize.Properties.Sanitize.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700906 case intOverflow:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400907 sanitize.Properties.Sanitize.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000908 case cfi:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400909 sanitize.Properties.Sanitize.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800910 case scs:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400911 sanitize.Properties.Sanitize.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -0400912 case Memtag_heap:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400913 sanitize.Properties.Sanitize.Memtag_heap = bPtr
Florian Mayerd8434a42022-08-31 20:57:03 +0000914 case Memtag_stack:
915 sanitize.Properties.Sanitize.Memtag_stack = bPtr
Florian Mayer1bda2462022-09-29 15:48:08 -0700916 // We do not need to disable ASAN or HWASan here, as there is no Memtag_stack variant.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500917 case Fuzzer:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400918 sanitize.Properties.Sanitize.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800919 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500920 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800921 }
922 if b {
923 sanitize.Properties.SanitizerEnabled = true
924 }
925}
926
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000927// Check if the sanitizer is explicitly disabled (as opposed to nil by
928// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500929func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000930 if sanitize == nil {
931 return false
932 }
933
934 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
935 return sanitizerVal != nil && *sanitizerVal == false
936}
937
938// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
939// because enabling a sanitizer either directly (via the blueprint) or
940// indirectly (via a mutator) sets the bool ptr to true, and you can't
941// distinguish between the cases. It isn't needed though - both cases can be
942// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500943func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000944 if sanitize == nil {
945 return false
946 }
947
948 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
949 return sanitizerVal != nil && *sanitizerVal == true
950}
951
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500952// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
953func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700954 switch t := tag.(type) {
955 case dependencyTag:
956 return t == reuseObjTag || t == objDepTag
957 case libraryDependencyTag:
958 return true
959 default:
960 return false
961 }
Colin Cross6b753602018-06-21 13:03:07 -0700962}
963
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500964func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
965 return IsSanitizableDependencyTag
966}
967
Inseob Kimc42f2f22020-07-29 20:32:10 +0900968// Determines if the current module is a static library going to be captured
969// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
970// except for ones which explicitly disable cfi.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200971func needsCfiForVendorSnapshot(mctx android.BaseModuleContext) bool {
Kiyoung Kim48f37782021-07-07 12:42:39 +0900972 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900973 return false
974 }
975
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500976 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900977
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500978 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900979 return false
980 }
981
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500982 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900983 return false
984 }
985
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500986 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900987 return false
988 }
989
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500990 if !c.SanitizerSupported(cfi) {
991 return false
992 }
993
994 return c.SanitizePropDefined() &&
995 !c.SanitizeNever() &&
996 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900997}
998
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200999type sanitizerSplitMutator struct {
1000 sanitizer SanitizerType
1001}
1002
1003// If an APEX is sanitized or not depends on whether it contains at least one
1004// sanitized module. Transition mutators cannot propagate information up the
1005// dependency graph this way, so we need an auxiliary mutator to do so.
1006func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
1007 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1008 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1009 ctx.VisitDirectDeps(func(dep android.Module) {
1010 if c, ok := dep.(*Module); ok && c.sanitize.isSanitizerEnabled(s.sanitizer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +09001011 enabled = true
Inseob Kimc42f2f22020-07-29 20:32:10 +09001012 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001013 })
1014
1015 if enabled {
1016 sanitizeable.EnableSanitizer(s.sanitizer.name())
1017 }
1018 }
1019}
1020
1021func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
1022 if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1023 if s.sanitizer == cfi && needsCfiForVendorSnapshot(ctx) {
1024 return []string{"", s.sanitizer.variationName()}
1025 }
1026
1027 // If the given sanitizer is not requested in the .bp file for a module, it
1028 // won't automatically build the sanitized variation.
1029 if !c.IsSanitizerEnabled(s.sanitizer) {
1030 return []string{""}
1031 }
1032
1033 if c.Binary() {
1034 // If a sanitizer is enabled for a binary, we do not build the version
1035 // without the sanitizer
1036 return []string{s.sanitizer.variationName()}
1037 } else if c.StaticallyLinked() || c.Header() {
1038 // For static libraries, we build both versions. Some Make modules
1039 // apparently depend on this behavior.
1040 return []string{"", s.sanitizer.variationName()}
1041 } else {
1042 // We only build the requested variation of dynamic libraries
1043 return []string{s.sanitizer.variationName()}
1044 }
1045 }
1046
1047 if _, ok := ctx.Module().(JniSanitizeable); ok {
1048 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1049 // that is short-circuited for now
1050 return []string{""}
1051 }
1052
1053 // If an APEX has a sanitized dependency, we build the APEX in the sanitized
1054 // variation. This is useful because such APEXes require extra dependencies.
1055 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
1056 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1057 if enabled {
1058 return []string{s.sanitizer.variationName()}
1059 } else {
1060 return []string{""}
1061 }
1062 }
1063
1064 if c, ok := ctx.Module().(*Module); ok {
1065 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1066
1067 // Check if it's a snapshot module supporting sanitizer
1068 if ss, ok := c.linker.(snapshotSanitizer); ok && ss.isSanitizerEnabled(s.sanitizer) {
1069 return []string{"", s.sanitizer.variationName()}
1070 } else {
1071 return []string{""}
1072 }
1073 }
1074
1075 return []string{""}
1076}
1077
1078func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1079 if c, ok := ctx.Module().(PlatformSanitizeable); ok {
1080 if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
1081 // If the dependency is through a non-sanitizable tag, use the
1082 // non-sanitized variation
1083 return ""
1084 }
1085
1086 return sourceVariation
1087 } else if _, ok := ctx.Module().(JniSanitizeable); ok {
1088 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1089 // that is short-circuited for now
1090 return ""
1091 } else {
1092 // Otherwise, do not rock the boat.
1093 return sourceVariation
1094 }
1095}
1096
1097func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1098 if d, ok := ctx.Module().(PlatformSanitizeable); ok {
1099 if dm, ok := ctx.Module().(*Module); ok {
1100 if ss, ok := dm.linker.(snapshotSanitizer); ok && ss.isSanitizerEnabled(s.sanitizer) {
1101 return incomingVariation
Inseob Kimc42f2f22020-07-29 20:32:10 +09001102 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001103 }
1104
1105 if !d.SanitizePropDefined() ||
1106 d.SanitizeNever() ||
1107 d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
1108 !d.SanitizerSupported(s.sanitizer) {
1109 // If a module opts out of a sanitizer, use its non-sanitized variation
1110 return ""
1111 }
1112
1113 // Binaries are always built in the variation they requested.
1114 if d.Binary() {
1115 if d.IsSanitizerEnabled(s.sanitizer) {
1116 return s.sanitizer.variationName()
1117 } else {
1118 return ""
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001119 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001120 }
1121
1122 // If a shared library requests to be sanitized, it will be built for that
1123 // sanitizer. Otherwise, some sanitizers propagate through shared library
1124 // dependency edges, some do not.
1125 if !d.StaticallyLinked() && !d.Header() {
1126 if d.IsSanitizerEnabled(s.sanitizer) {
1127 return s.sanitizer.variationName()
1128 }
1129
1130 if s.sanitizer == cfi || s.sanitizer == Hwasan || s.sanitizer == scs || s.sanitizer == Asan {
1131 return ""
1132 }
1133 }
1134
1135 // Static and header libraries inherit whether they are sanitized from the
1136 // module they are linked into
1137 return incomingVariation
1138 } else if d, ok := ctx.Module().(Sanitizeable); ok {
1139 // If an APEX contains a sanitized module, it will be built in the variation
1140 // corresponding to that sanitizer.
1141 enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1142 if enabled {
1143 return s.sanitizer.variationName()
1144 }
1145
1146 return incomingVariation
1147 }
1148
1149 return ""
1150}
1151
1152func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
1153 sanitizerVariation := variationName == s.sanitizer.variationName()
1154
1155 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1156 sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
1157
1158 oneMakeVariation := false
1159 if c.StaticallyLinked() || c.Header() {
1160 if s.sanitizer != cfi && s.sanitizer != scs && s.sanitizer != Hwasan {
1161 // These sanitizers export only one variation to Make. For the rest,
1162 // Make targets can depend on both the sanitized and non-sanitized
1163 // versions.
1164 oneMakeVariation = true
1165 }
1166 } else if !c.Binary() {
1167 // Shared library. These are the sanitizers that do propagate through shared
1168 // library dependencies and therefore can cause multiple variations of a
1169 // shared library to be built.
1170 if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != scs && s.sanitizer != Asan {
1171 oneMakeVariation = true
1172 }
1173 }
1174
1175 if oneMakeVariation {
1176 if sanitizerEnabled != sanitizerVariation {
1177 c.SetPreventInstall()
1178 c.SetHideFromMake()
1179 }
1180 }
1181
1182 if sanitizerVariation {
1183 c.SetSanitizer(s.sanitizer, true)
1184
1185 // CFI is incompatible with ASAN so disable it in ASAN variations
1186 if s.sanitizer.incompatibleWithCfi() {
1187 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1188 if mctx.Device() && cfiSupported {
1189 c.SetSanitizer(cfi, false)
Jiyong Parkf97782b2019-02-13 20:28:58 +09001190 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001191 }
1192
1193 // locate the asan libraries under /data/asan
1194 if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
1195 c.SetInSanitizerDir()
1196 }
1197
1198 if c.StaticallyLinked() && c.ExportedToMake() {
1199 if s.sanitizer == Hwasan {
1200 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
1201 } else if s.sanitizer == cfi {
1202 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
1203 }
1204 }
1205 } else if c.IsSanitizerEnabled(s.sanitizer) {
1206 // Disable the sanitizer for the non-sanitized variation
1207 c.SetSanitizer(s.sanitizer, false)
1208 }
1209 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
1210 // If an APEX has sanitized dependencies, it gets a few more dependencies
1211 if sanitizerVariation {
1212 sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
1213 }
1214 } else if c, ok := mctx.Module().(*Module); ok {
1215 if ss, ok := c.linker.(snapshotSanitizer); ok && ss.isSanitizerEnabled(s.sanitizer) {
1216 c.linker.(snapshotSanitizer).setSanitizerVariation(s.sanitizer, sanitizerVariation)
1217
1218 // Export the static lib name to make
1219 if c.static() && c.ExportedToMake() {
1220 if s.sanitizer == cfi {
1221 // use BaseModuleName which is the name for Make.
1222 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1223 }
1224 }
Colin Cross16b23492016-01-06 14:41:07 -08001225 }
1226 }
1227}
1228
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001229func (c *Module) SanitizeNever() bool {
1230 return Bool(c.sanitize.Properties.Sanitize.Never)
1231}
1232
1233func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
1234 return c.sanitize.isSanitizerExplicitlyDisabled(t)
1235}
1236
Ivan Lozano30c5db22018-02-21 15:49:20 -08001237// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001238func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001239 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001240 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001241 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001242 mctx.WalkDeps(func(child, parent android.Module) bool {
1243 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1244 return false
1245 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001246
Inseob Kimeec88e12020-01-22 11:11:29 +09001247 d, ok := child.(*Module)
1248 if !ok || !d.static() {
1249 return false
1250 }
1251 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001252 if enableMinimalRuntime(d.sanitize) {
1253 // If a static dependency is built with the minimal runtime,
1254 // make sure we include the ubsan minimal runtime.
1255 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001256 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001257 // If a static dependency runs with full ubsan diagnostics,
1258 // make sure we include the ubsan runtime.
1259 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001260 }
Colin Cross0b908332019-06-19 23:00:20 -07001261
1262 if c.sanitize.Properties.MinimalRuntimeDep &&
1263 c.sanitize.Properties.UbsanRuntimeDep {
1264 // both flags that this mutator might set are true, so don't bother recursing
1265 return false
1266 }
1267
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001268 if c.Os() == android.Linux {
1269 c.sanitize.Properties.BuiltinsDep = true
1270 }
1271
Colin Cross0b908332019-06-19 23:00:20 -07001272 return true
Colin Cross6b753602018-06-21 13:03:07 -07001273 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001274
Jose Galmesf7294582020-11-13 12:07:36 -08001275 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001276 if Bool(p.properties.Sanitize_minimal_dep) {
1277 c.sanitize.Properties.MinimalRuntimeDep = true
1278 }
1279 if Bool(p.properties.Sanitize_ubsan_dep) {
1280 c.sanitize.Properties.UbsanRuntimeDep = true
1281 }
1282 }
1283
1284 return false
Colin Cross6b753602018-06-21 13:03:07 -07001285 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001286 }
1287}
1288
Jiyong Park379de2f2018-12-19 02:47:14 +09001289// Add the dependency to the runtime library for each of the sanitizer variants
1290func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001291 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001292 if !c.Enabled() {
1293 return
1294 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001295 var sanitizers []string
1296 var diagSanitizers []string
1297
1298 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
1299 sanitizers = append(sanitizers, "undefined")
1300 } else {
1301 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
1302 sanitizers = append(sanitizers,
1303 "bool",
1304 "integer-divide-by-zero",
1305 "return",
1306 "returns-nonnull-attribute",
1307 "shift-exponent",
1308 "unreachable",
1309 "vla-bound",
1310 // TODO(danalbert): The following checks currently have compiler performance issues.
1311 //"alignment",
1312 //"bounds",
1313 //"enum",
1314 //"float-cast-overflow",
1315 //"float-divide-by-zero",
1316 //"nonnull-attribute",
1317 //"null",
1318 //"shift-base",
1319 //"signed-integer-overflow",
1320 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1321 // https://llvm.org/PR19302
1322 // http://reviews.llvm.org/D6974
1323 // "object-size",
1324 )
1325 }
1326 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
1327 }
1328
1329 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
1330 diagSanitizers = append(diagSanitizers, "undefined")
1331 }
1332
1333 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
1334
1335 if Bool(c.sanitize.Properties.Sanitize.Address) {
1336 sanitizers = append(sanitizers, "address")
1337 diagSanitizers = append(diagSanitizers, "address")
1338 }
1339
1340 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1341 sanitizers = append(sanitizers, "hwaddress")
1342 }
1343
1344 if Bool(c.sanitize.Properties.Sanitize.Thread) {
1345 sanitizers = append(sanitizers, "thread")
1346 }
1347
1348 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
1349 sanitizers = append(sanitizers, "safe-stack")
1350 }
1351
1352 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
1353 sanitizers = append(sanitizers, "cfi")
1354
1355 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
1356 diagSanitizers = append(diagSanitizers, "cfi")
1357 }
1358 }
1359
1360 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
1361 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1362 sanitizers = append(sanitizers, "signed-integer-overflow")
1363 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
1364 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1365 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1366 }
1367 }
1368
1369 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1370 sanitizers = append(sanitizers, "scudo")
1371 }
1372
1373 if Bool(c.sanitize.Properties.Sanitize.Scs) {
1374 sanitizers = append(sanitizers, "shadow-call-stack")
1375 }
1376
Ivan Lozanod7586b62021-04-01 09:49:36 -04001377 if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.Binary() {
Florian Mayerd8434a42022-08-31 20:57:03 +00001378 sanitizers = append(sanitizers, "memtag-heap")
1379 }
1380
1381 if Bool(c.sanitize.Properties.Sanitize.Memtag_stack) {
1382 sanitizers = append(sanitizers, "memtag-stack")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001383 }
1384
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001385 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
1386 sanitizers = append(sanitizers, "fuzzer-no-link")
1387 }
1388
Jiyong Park379de2f2018-12-19 02:47:14 +09001389 // Save the list of sanitizers. These will be used again when generating
1390 // the build rules (for Cflags, etc.)
1391 c.sanitize.Properties.Sanitizers = sanitizers
1392 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1393
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001394 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1395 if c.Host() {
1396 diagSanitizers = sanitizers
1397 }
1398
Jiyong Park379de2f2018-12-19 02:47:14 +09001399 // Determine the runtime library required
1400 runtimeLibrary := ""
Colin Crosse725b4e2022-10-19 15:46:53 -07001401 alwaysStaticRuntime := false
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001402 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001403 toolchain := c.toolchain(mctx)
1404 if Bool(c.sanitize.Properties.Sanitize.Address) {
1405 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1406 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1407 if c.staticBinary() {
1408 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001409 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001410 } else {
1411 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1412 }
1413 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
1414 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1415 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1416 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1417 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1418 } else {
1419 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1420 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001421 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001422 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
1423 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
1424 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001425 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
Colin Crosse725b4e2022-10-19 15:46:53 -07001426 if c.staticBinary() || toolchain.Musl() {
1427 // Use a static runtime for static binaries.
1428 // Also use a static runtime for musl to match
1429 // what clang does for glibc. Otherwise dlopening
1430 // libraries that depend on libclang_rt.ubsan_standalone.so
1431 // fails with:
1432 // Error relocating ...: initial-exec TLS resolves to dynamic definition
Colin Cross32f1de32021-03-29 13:41:37 -07001433 runtimeLibrary += ".static"
Colin Crosse725b4e2022-10-19 15:46:53 -07001434 alwaysStaticRuntime = true
Colin Cross32f1de32021-03-29 13:41:37 -07001435 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001436 }
1437
Colin Cross06c80eb2022-02-10 10:34:19 -08001438 addStaticDeps := func(deps ...string) {
1439 // If we're using snapshots, redirect to snapshot whenever possible
1440 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1441 for idx, dep := range deps {
1442 if lib, ok := snapshot.StaticLibs[dep]; ok {
1443 deps[idx] = lib
1444 }
1445 }
1446
1447 // static executable gets static runtime libs
Colin Cross3e5e7782022-06-17 22:17:05 +00001448 depTag := libraryDependencyTag{Kind: staticLibraryDependency, unexportedSymbols: true}
Colin Cross06c80eb2022-02-10 10:34:19 -08001449 variations := append(mctx.Target().Variations(),
1450 blueprint.Variation{Mutator: "link", Variation: "static"})
1451 if c.Device() {
1452 variations = append(variations, c.ImageVariation())
1453 }
1454 if c.UseSdk() {
1455 variations = append(variations,
1456 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1457 }
1458 mctx.AddFarVariationDependencies(variations, depTag, deps...)
1459
1460 }
1461 if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
1462 addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain))
1463 }
1464 if c.sanitize.Properties.BuiltinsDep {
1465 addStaticDeps(config.BuiltinsRuntimeLibrary(toolchain))
1466 }
1467
Colin Crossed12a042022-02-07 13:55:55 -08001468 if runtimeLibrary != "" && (toolchain.Bionic() || toolchain.Musl() || c.sanitize.Properties.UbsanRuntimeDep) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001469 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001470
1471 // Adding dependency to the runtime library. We are using *FarVariation*
1472 // because the runtime libraries themselves are not mutated by sanitizer
1473 // mutators and thus don't have sanitizer variants whereas this module
1474 // has been already mutated.
1475 //
1476 // Note that by adding dependency with {static|shared}DepTag, the lib is
1477 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
Colin Crosse725b4e2022-10-19 15:46:53 -07001478 if c.staticBinary() || alwaysStaticRuntime {
Colin Cross06c80eb2022-02-10 10:34:19 -08001479 addStaticDeps(runtimeLibrary)
1480 addStaticDeps(extraStaticDeps...)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001481 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001482 // If we're using snapshots, redirect to snapshot whenever possible
1483 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1484 if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
1485 runtimeLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001486 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001487
Cindy Zhou18417cb2020-12-10 07:12:38 -08001488 // Skip apex dependency check for sharedLibraryDependency
1489 // when sanitizer diags are enabled. Skipping the check will allow
1490 // building with diag libraries without having to list the
1491 // dependency in Apex's allowed_deps file.
1492 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001493 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001494 depTag := libraryDependencyTag{
1495 Kind: sharedLibraryDependency,
1496 Order: earlyLibraryDependency,
1497
1498 skipApexAllowedDependenciesCheck: diagEnabled,
1499 }
Colin Cross42507332020-08-21 16:15:23 -07001500 variations := append(mctx.Target().Variations(),
1501 blueprint.Variation{Mutator: "link", Variation: "shared"})
1502 if c.Device() {
1503 variations = append(variations, c.ImageVariation())
1504 }
Colin Cross06c80eb2022-02-10 10:34:19 -08001505 if c.UseSdk() {
1506 variations = append(variations,
1507 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1508 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001509 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001510 }
1511 // static lib does not have dependency to the runtime library. The
1512 // dependency will be added to the executables or shared libs using
1513 // the static lib.
1514 }
1515 }
1516}
1517
1518type Sanitizeable interface {
1519 android.Module
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001520 IsSanitizerEnabled(config android.Config, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001521 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001522 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001523}
1524
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001525type JniSanitizeable interface {
1526 android.Module
1527 IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
1528}
1529
Ivan Lozanod7586b62021-04-01 09:49:36 -04001530func (c *Module) MinimalRuntimeDep() bool {
1531 return c.sanitize.Properties.MinimalRuntimeDep
1532}
1533
1534func (c *Module) UbsanRuntimeDep() bool {
1535 return c.sanitize.Properties.UbsanRuntimeDep
1536}
1537
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001538func (c *Module) SanitizePropDefined() bool {
1539 return c.sanitize != nil
1540}
1541
1542func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1543 return c.sanitize.isSanitizerEnabled(t)
1544}
1545
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001546func (c *Module) StaticallyLinked() bool {
1547 return c.static()
1548}
1549
1550func (c *Module) SetInSanitizerDir() {
1551 if c.sanitize != nil {
1552 c.sanitize.Properties.InSanitizerDir = true
1553 }
1554}
1555
1556func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1557 if c.sanitize != nil {
1558 c.sanitize.SetSanitizer(t, b)
1559 }
1560}
1561
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001562var _ PlatformSanitizeable = (*Module)(nil)
1563
Inseob Kim74d25562020-08-04 00:41:38 +09001564type sanitizerStaticLibsMap struct {
1565 // libsMap contains one list of modules per each image and each arch.
1566 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001567 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001568 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001569 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001570}
1571
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001572func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001573 return &sanitizerStaticLibsMap{
1574 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001575 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001576 }
1577}
1578
1579// Add the current module to sanitizer static libs maps
1580// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001581func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1582 image := GetImageVariantType(c)
1583 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001584
1585 s.libsMapLock.Lock()
1586 defer s.libsMapLock.Unlock()
1587
1588 if _, ok := s.libsMap[image]; !ok {
1589 s.libsMap[image] = make(map[string][]string)
1590 }
1591
1592 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1593}
1594
1595// Exports makefile variables in the following format:
1596// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1597// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1598// These are to be used by use_soong_sanitized_static_libraries.
1599// See build/make/core/binary.mk for more details.
1600func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1601 for _, image := range android.SortedStringKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001602 archMap := s.libsMap[ImageVariantType(image)]
Inseob Kim74d25562020-08-04 00:41:38 +09001603 for _, arch := range android.SortedStringKeys(archMap) {
1604 libs := archMap[arch]
1605 sort.Strings(libs)
1606
1607 key := fmt.Sprintf(
1608 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1609 s.sanitizerType.variationName(),
1610 image, // already upper
1611 arch)
1612
1613 ctx.Strict(key, strings.Join(libs, " "))
1614 }
1615 }
1616}
1617
Colin Cross571cccf2019-02-04 11:22:08 -08001618var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1619
Inseob Kim74d25562020-08-04 00:41:38 +09001620func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001621 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001622 return newSanitizerStaticLibsMap(cfi)
1623 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001624}
1625
Colin Cross571cccf2019-02-04 11:22:08 -08001626var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1627
Inseob Kim74d25562020-08-04 00:41:38 +09001628func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001629 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001630 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001631 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001632}
1633
Ivan Lozano30c5db22018-02-21 15:49:20 -08001634func enableMinimalRuntime(sanitize *sanitize) bool {
1635 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001636 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001637 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -08001638 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001639 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1640 Bool(sanitize.Properties.Sanitize.Undefined) ||
1641 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -08001642 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1643 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001644 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001645 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001646
Ivan Lozano30c5db22018-02-21 15:49:20 -08001647 return true
1648 }
1649 return false
1650}
1651
Ivan Lozanod7586b62021-04-01 09:49:36 -04001652func (m *Module) UbsanRuntimeNeeded() bool {
1653 return enableUbsanRuntime(m.sanitize)
1654}
1655
1656func (m *Module) MinimalRuntimeNeeded() bool {
1657 return enableMinimalRuntime(m.sanitize)
1658}
1659
Inseob Kim8471cda2019-11-15 09:59:12 +09001660func enableUbsanRuntime(sanitize *sanitize) bool {
1661 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001662 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001663 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1664}
1665
Vishwath Mohane7128792017-11-17 11:08:10 -08001666func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001667 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001668}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001669
1670func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001671 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001672}