blob: e1d0990346f3e660683798c24ff6e12a8c6f005c [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"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070080)
81
Ivan Lozano3968d8f2020-12-14 11:27:52 -050082type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080083
Colin Cross16b23492016-01-06 14:41:07 -080084const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050085 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070086 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080087 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070088 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080089 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -050090 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -040091 Memtag_heap
Liz Kammer75db9312021-07-07 16:41:50 -040092 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -080093)
94
Liz Kammer75db9312021-07-07 16:41:50 -040095var Sanitizers = []SanitizerType{
96 Asan,
97 Hwasan,
98 tsan,
99 intOverflow,
100 scs,
101 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400102 Memtag_heap,
Liz Kammer75db9312021-07-07 16:41:50 -0400103 cfi, // cfi is last to prevent it running before incompatible mutators
104}
105
Jiyong Park82226632019-02-01 10:50:50 +0900106// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500107func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800108 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500109 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800110 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700111 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700112 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800113 case tsan:
114 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700115 case intOverflow:
116 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000117 case cfi:
118 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800119 case scs:
120 return "scs"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400121 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700122 return "memtag_heap"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500123 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700124 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800125 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500126 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800127 }
128}
129
Jiyong Park82226632019-02-01 10:50:50 +0900130// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500131func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900132 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500133 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900134 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700135 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900136 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400137 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700138 return "memtag_heap"
Jiyong Park82226632019-02-01 10:50:50 +0900139 case tsan:
140 return "thread"
141 case intOverflow:
142 return "integer_overflow"
143 case cfi:
144 return "cfi"
145 case scs:
146 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500147 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700148 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900149 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500150 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900151 }
152}
153
Liz Kammer75db9312021-07-07 16:41:50 -0400154func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
155 switch t {
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200156 case cfi, Hwasan, Asan, tsan, Fuzzer, scs:
157 sanitizer := &sanitizerSplitMutator{t}
158 ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
159 ctx.Transition(t.variationName(), sanitizer)
Ivan Lozano62cd0382021-11-01 10:27:54 -0400160 case Memtag_heap, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400161 // do nothing
162 default:
163 panic(fmt.Errorf("unknown SanitizerType %d", t))
164 }
165}
166
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500167func (*Module) SanitizerSupported(t SanitizerType) bool {
168 switch t {
169 case Asan:
170 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700171 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500172 return true
173 case tsan:
174 return true
175 case intOverflow:
176 return true
177 case cfi:
178 return true
179 case scs:
180 return true
181 case Fuzzer:
182 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400183 case Memtag_heap:
184 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500185 default:
186 return false
187 }
188}
189
190// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
191func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700192 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900193}
194
Martin Stjernholmb0249572020-09-15 02:32:35 +0100195type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400196 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100197 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800198
Liz Kammer75b9b402021-06-25 15:19:27 -0400199 // ASan (Address sanitizer), incompatible with static binaries.
200 // Always runs in a diagnostic mode.
201 // Use of address sanitizer disables cfi sanitizer.
202 // Hwaddress sanitizer takes precedence over this sanitizer.
203 Address *bool `android:"arch_variant"`
204 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
205 // Always runs in a diagnostic mode.
206 // Use of thread sanitizer disables cfi and scudo sanitizers.
207 // Hwaddress sanitizer takes precedence over this sanitizer.
208 Thread *bool `android:"arch_variant"`
209 // HWASan (Hardware Address sanitizer).
210 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100211 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800212
Liz Kammer75b9b402021-06-25 15:19:27 -0400213 // Undefined behavior sanitizer
214 All_undefined *bool `android:"arch_variant"`
215 // Subset of undefined behavior sanitizer
216 Undefined *bool `android:"arch_variant"`
217 // List of specific undefined behavior sanitizers to enable
218 Misc_undefined []string `android:"arch_variant"`
219 // Fuzzer, incompatible with static binaries.
220 Fuzzer *bool `android:"arch_variant"`
221 // safe-stack sanitizer, incompatible with 32-bit architectures.
222 Safestack *bool `android:"arch_variant"`
223 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
224 Cfi *bool `android:"arch_variant"`
225 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
226 Integer_overflow *bool `android:"arch_variant"`
227 // scudo sanitizer, incompatible with asan, hwasan, tsan
228 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
229 // deprecated
230 Scudo *bool `android:"arch_variant"`
231 // shadow-call-stack sanitizer, only available on arm64
232 Scs *bool `android:"arch_variant"`
233 // Memory-tagging, only available on arm64
234 // if diag.memtag unset or false, enables async memory tagging
235 Memtag_heap *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100236
237 // A modifier for ASAN and HWASAN for write only instrumentation
238 Writeonly *bool `android:"arch_variant"`
239
240 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
241 // Replaces abort() on error with a human-readable error message.
242 // Address and Thread sanitizers always run in diagnostic mode.
243 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400244 // Undefined behavior sanitizer, diagnostic mode
245 Undefined *bool `android:"arch_variant"`
246 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
247 Cfi *bool `android:"arch_variant"`
248 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
249 Integer_overflow *bool `android:"arch_variant"`
250 // Memory-tagging, only available on arm64
251 // requires sanitizer.memtag: true
252 // if set, enables sync memory tagging
253 Memtag_heap *bool `android:"arch_variant"`
254 // List of specific undefined behavior sanitizers to enable in diagnostic mode
255 Misc_undefined []string `android:"arch_variant"`
256 // List of sanitizers to pass to -fno-sanitize-recover
257 // results in only the first detected error for these sanitizers being reported and program then
258 // exits with a non-zero exit code.
259 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800260 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800261
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800262 // Sanitizers to run with flag configuration specified
263 Config struct {
264 // Enables CFI support flags for assembly-heavy libraries
265 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800266 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800267
Liz Kammer75b9b402021-06-25 15:19:27 -0400268 // List of sanitizers to pass to -fsanitize-recover
269 // allows execution to continue for these sanitizers to detect multiple errors rather than only
270 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100271 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000272
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700273 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100274 Blocklist *string
275}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700276
Martin Stjernholmb0249572020-09-15 02:32:35 +0100277type SanitizeProperties struct {
Martin Stjernholmb0249572020-09-15 02:32:35 +0100278 Sanitize SanitizeUserProps `android:"arch_variant"`
279 SanitizerEnabled bool `blueprint:"mutated"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100280 MinimalRuntimeDep bool `blueprint:"mutated"`
281 BuiltinsDep bool `blueprint:"mutated"`
282 UbsanRuntimeDep bool `blueprint:"mutated"`
283 InSanitizerDir bool `blueprint:"mutated"`
284 Sanitizers []string `blueprint:"mutated"`
285 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800286}
287
288type sanitize struct {
289 Properties SanitizeProperties
290}
291
Cindy Zhou18417cb2020-12-10 07:12:38 -0800292// Mark this tag with a check to see if apex dependency check should be skipped
293func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
294 return t.skipApexAllowedDependenciesCheck
295}
296
297var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
298
Vishwath Mohane7128792017-11-17 11:08:10 -0800299func init() {
300 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700301 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800302}
303
Colin Cross16b23492016-01-06 14:41:07 -0800304func (sanitize *sanitize) props() []interface{} {
305 return []interface{}{&sanitize.Properties}
306}
307
308func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700309 s := &sanitize.Properties.Sanitize
310
Colin Cross16b23492016-01-06 14:41:07 -0800311 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700312 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800313 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800314 }
315
316 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800317 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800318 return
319 }
320
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800321 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400322 if ctx.testBinary() {
323 if s.Memtag_heap == nil {
324 s.Memtag_heap = proptools.BoolPtr(true)
325 }
326 if s.Diag.Memtag_heap == nil {
327 s.Diag.Memtag_heap = proptools.BoolPtr(true)
328 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800329 }
330
Colin Cross16b23492016-01-06 14:41:07 -0800331 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700332 var globalSanitizersDiag []string
333
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700334 if ctx.Host() {
335 if !ctx.Windows() {
336 globalSanitizers = ctx.Config().SanitizeHost()
337 }
338 } else {
339 arches := ctx.Config().SanitizeDeviceArch()
340 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
341 globalSanitizers = ctx.Config().SanitizeDevice()
342 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800343 }
344 }
345
Colin Cross16b23492016-01-06 14:41:07 -0800346 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000347 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700348 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400349 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000350 }
Colin Cross16b23492016-01-06 14:41:07 -0800351
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700352 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400353 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000354 }
355
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700356 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400357 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000358 }
359
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700360 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400361 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000362 }
363
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700364 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400365 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700366 }
367
368 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400369 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000370 }
371
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700372 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800373 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400374 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700375 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700376 }
377
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700378 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700379 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700380 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400381 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700382 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700383 }
384
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700385 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400386 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700387 }
388
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700389 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400390 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700391 }
392
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000393 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
394 // Hwaddress and Address are set before, so we can check them here
395 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
396 if s.Address == nil && s.Hwaddress == nil {
397 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
398 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400399 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000400 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700401 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800402 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400403 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800404 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700405 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000406
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000407 if len(globalSanitizers) > 0 {
408 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
409 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700410
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700411 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700412 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700413 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400414 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700415 }
416
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700417 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
418 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400419 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700420 }
421
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800422 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
423 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400424 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800425 }
426
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700427 if len(globalSanitizersDiag) > 0 {
428 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
429 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700430 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700431
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800432 // Enable Memtag for all components in the include paths (for Aarch64 only)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800433 if ctx.Arch().ArchType == android.Arm64 {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800434 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800435 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400436 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800437 }
438 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400439 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800440 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800441 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800442 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400443 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800444 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800445 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700446 }
447
Elvis Chien9c993542021-06-25 01:15:17 +0800448 // Enable CFI for non-host components in the include paths
449 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400450 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000451 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400452 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700453 }
454 }
455
Elliott Hughesda3a0712020-03-06 16:55:28 -0800456 // Is CFI actually enabled?
457 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400458 s.Cfi = nil
459 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800460 }
461
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700462 // HWASan requires AArch64 hardware feature (top-byte-ignore).
463 if ctx.Arch().ArchType != android.Arm64 {
464 s.Hwaddress = nil
465 }
466
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800467 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800468 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800469 s.Scs = nil
470 }
471
Ivan Lozano62cd0382021-11-01 10:27:54 -0400472 // Memtag_heap is only implemented on AArch64.
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700473 if ctx.Arch().ArchType != android.Arm64 {
474 s.Memtag_heap = nil
475 }
476
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700477 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700478 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400479 s.Cfi = nil
480 s.Diag.Cfi = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700481 }
482
Colin Crossed12a042022-02-07 13:55:55 -0800483 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
484 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400485 s.Cfi = nil
486 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700487 s.Misc_undefined = nil
488 s.Undefined = nil
489 s.All_undefined = nil
490 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800491 }
492
Colin Crossed12a042022-02-07 13:55:55 -0800493 // Disable CFI for musl
494 if ctx.toolchain().Musl() {
495 s.Cfi = nil
496 s.Diag.Cfi = nil
497 }
498
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700499 // Also disable CFI for VNDK variants of components
500 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900501 if ctx.static() {
502 // Cfi variant for static vndk should be captured as vendor snapshot,
503 // so don't strictly disable Cfi.
504 s.Cfi = nil
505 s.Diag.Cfi = nil
506 } else {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400507 s.Cfi = nil
508 s.Diag.Cfi = nil
Inseob Kimc42f2f22020-07-29 20:32:10 +0900509 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900510 }
511
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700512 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700513 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
514 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700515 s.Hwaddress = nil
516 }
517
Colin Cross3c344ef2016-07-18 15:44:56 -0700518 if ctx.staticBinary() {
519 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700520 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700521 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800522 }
523
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700524 if Bool(s.All_undefined) {
525 s.Undefined = nil
526 }
527
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700528 if !ctx.toolchain().Is64Bit() {
529 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700530 s.Thread = nil
531 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800532 // TODO(ccross): error for compile_multilib = "32"?
533 }
534
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800535 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 -0700536 Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700537 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700538 sanitize.Properties.SanitizerEnabled = true
539 }
540
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800541 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
542 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700543 s.Scudo = nil
544 }
545
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700546 if Bool(s.Hwaddress) {
547 s.Address = nil
548 s.Thread = nil
549 }
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000550
551 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
552 // mutually incompatible.
553 if Bool(s.Fuzzer) {
554 s.Cfi = nil
555 }
Colin Cross16b23492016-01-06 14:41:07 -0800556}
557
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800558func toDisableImplicitIntegerChange(flags []string) bool {
559 // Returns true if any flag is fsanitize*integer, and there is
560 // no explicit flag about sanitize=implicit-integer-sign-change.
561 for _, f := range flags {
562 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
563 return false
564 }
565 }
566 for _, f := range flags {
567 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
568 return true
569 }
570 }
571 return false
572}
573
Yabin Cuidb7dda82020-11-30 15:47:45 -0800574func toDisableUnsignedShiftBaseChange(flags []string) bool {
575 // Returns true if any flag is fsanitize*integer, and there is
576 // no explicit flag about sanitize=unsigned-shift-base.
577 for _, f := range flags {
578 if strings.Contains(f, "sanitize=unsigned-shift-base") {
579 return false
580 }
581 }
582 for _, f := range flags {
583 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
584 return true
585 }
586 }
587 return false
588}
589
Colin Cross16b23492016-01-06 14:41:07 -0800590func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700591 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
Ivan Lozano30c5db22018-02-21 15:49:20 -0800592
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500593 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800594 flags.Local.LdFlags = append(flags.Local.LdFlags,
Colin Cross4af21ed2019-11-04 09:37:55 -0800595 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800596 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500597
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700598 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800599 return flags
600 }
601
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700602 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700603 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800604 // Frame pointer based unwinder in ASan requires ARM frame setup.
605 // TODO: put in flags?
606 flags.RequiredInstructionSet = "arm"
607 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800608 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
609 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800610
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000611 if Bool(sanitize.Properties.Sanitize.Writeonly) {
612 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
613 }
614
Colin Cross16b23492016-01-06 14:41:07 -0800615 if ctx.Host() {
616 // -nodefaultlibs (provided with libc++) prevents the driver from linking
617 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800618 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800619 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800620 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900621 if ctx.bootstrap() {
622 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
623 } else {
624 flags.DynamicLinker = "/system/bin/linker_asan"
625 }
Colin Cross16b23492016-01-06 14:41:07 -0800626 if flags.Toolchain.Is64Bit() {
627 flags.DynamicLinker += "64"
628 }
629 }
Colin Cross16b23492016-01-06 14:41:07 -0800630 }
631
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700632 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800633 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800634
635 for _, flag := range hwasanCommonflags {
636 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
637 }
638 for _, flag := range hwasanCommonflags {
639 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
640 }
641
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000642 if Bool(sanitize.Properties.Sanitize.Writeonly) {
643 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
644 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700645 }
646
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700647 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800648 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700649
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000650 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
651 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
652 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
653 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
654 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
655
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700656 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
657 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
658 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800659 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
660 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700661
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700662 // Disable fortify for fuzzing builds. Generally, we'll be building with
663 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800664 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800665
666 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
667 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
668 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
669 // the DT_RUNPATH from the shared library above it, and not the executable,
670 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
671 // DT_RUNPATH here means that transient shared libraries can be found
672 // colocated with their parents.
673 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800674 }
675
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700676 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800677 if ctx.Arch().ArchType == android.Arm {
678 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
679 // to do this on a function basis, so force Thumb on the entire module.
680 flags.RequiredInstructionSet = "thumb"
681 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000682
Colin Cross4af21ed2019-11-04 09:37:55 -0800683 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
684 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800685 if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
686 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
687 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000688 // Only append the default visibility flag if -fvisibility has not already been set
689 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800690 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
691 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000692 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800693 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000694
695 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800696 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
697 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000698 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700699 }
700
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700701 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800702 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700703 }
704
Jiyong Park379de2f2018-12-19 02:47:14 +0900705 if len(sanitize.Properties.Sanitizers) > 0 {
706 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Colin Cross4af21ed2019-11-04 09:37:55 -0800707 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
708 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross234b01d2022-02-07 13:49:03 -0800709 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
710
Colin Crossed12a042022-02-07 13:55:55 -0800711 if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
712 // Bionic and musl sanitizer runtimes have already been added as dependencies so that
713 // the right variant of the runtime will be used (with the "-android" or "-musl"
714 // suffixes), so don't let clang the runtime library.
Colin Cross234b01d2022-02-07 13:49:03 -0800715 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-link-runtime")
716 } else {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800717 // Host sanitizers only link symbols in the final executable, so
718 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800719 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500720
Colin Cross234b01d2022-02-07 13:49:03 -0800721 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function san
722 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500723 }
724
Evgenii Stepanovc480fbc2022-06-10 19:45:59 +0000725 if enableMinimalRuntime(sanitize) {
726 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
727 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
728 }
729
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700730 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
731 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800732 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700733 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800734 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700735 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800736 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700737 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800738 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800739 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
740 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800741 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800742 // http://b/171275751, Android doesn't build with this sanitizer yet.
743 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
744 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
745 }
Colin Cross16b23492016-01-06 14:41:07 -0800746 }
747
Jiyong Park379de2f2018-12-19 02:47:14 +0900748 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800749 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700750 }
751 // FIXME: enable RTTI if diag + (cfi or vptr)
752
Andreas Gampe97071162017-05-08 13:15:23 -0700753 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800754 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700755 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
756 }
757
Ivan Lozano7929bba2018-12-12 09:36:31 -0800758 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800759 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800760 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
761 }
762
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700763 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
764 if blocklist.Valid() {
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700765 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-ignorelist="+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700766 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
767 }
768
Colin Cross16b23492016-01-06 14:41:07 -0800769 return flags
770}
771
Colin Crossd80cbca2020-02-24 12:01:37 -0800772func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900773 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
774 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800775 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900776 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800777 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900778 }
779 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800780 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900781 }
782 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800783 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900784 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800785 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700786}
787
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700788func (sanitize *sanitize) inSanitizerDir() bool {
789 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700790}
791
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500792// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
793func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000794 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500795 case Asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000796 return sanitize.Properties.Sanitize.Address
Tri Vo6eafc362021-04-01 11:29:09 -0700797 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700798 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000799 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000800 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000801 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000802 return sanitize.Properties.Sanitize.Integer_overflow
803 case cfi:
804 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800805 case scs:
806 return sanitize.Properties.Sanitize.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -0400807 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700808 return sanitize.Properties.Sanitize.Memtag_heap
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500809 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700810 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000811 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500812 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000813 }
814}
815
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500816// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800817func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500818 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700819 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800820 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800821 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700822 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -0400823 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500824 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800825}
826
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500827// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700828func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500829 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700830 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700831 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500832 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700833}
834
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500835func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400836 bPtr := proptools.BoolPtr(b)
837 if !b {
838 bPtr = nil
839 }
Colin Cross16b23492016-01-06 14:41:07 -0800840 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500841 case Asan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400842 sanitize.Properties.Sanitize.Address = bPtr
Tri Vo6eafc362021-04-01 11:29:09 -0700843 case Hwasan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400844 sanitize.Properties.Sanitize.Hwaddress = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800845 case tsan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400846 sanitize.Properties.Sanitize.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700847 case intOverflow:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400848 sanitize.Properties.Sanitize.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000849 case cfi:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400850 sanitize.Properties.Sanitize.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800851 case scs:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400852 sanitize.Properties.Sanitize.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -0400853 case Memtag_heap:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400854 sanitize.Properties.Sanitize.Memtag_heap = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500855 case Fuzzer:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400856 sanitize.Properties.Sanitize.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800857 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500858 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800859 }
860 if b {
861 sanitize.Properties.SanitizerEnabled = true
862 }
863}
864
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000865// Check if the sanitizer is explicitly disabled (as opposed to nil by
866// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500867func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000868 if sanitize == nil {
869 return false
870 }
871
872 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
873 return sanitizerVal != nil && *sanitizerVal == false
874}
875
876// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
877// because enabling a sanitizer either directly (via the blueprint) or
878// indirectly (via a mutator) sets the bool ptr to true, and you can't
879// distinguish between the cases. It isn't needed though - both cases can be
880// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500881func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000882 if sanitize == nil {
883 return false
884 }
885
886 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
887 return sanitizerVal != nil && *sanitizerVal == true
888}
889
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500890// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
891func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700892 switch t := tag.(type) {
893 case dependencyTag:
894 return t == reuseObjTag || t == objDepTag
895 case libraryDependencyTag:
896 return true
897 default:
898 return false
899 }
Colin Cross6b753602018-06-21 13:03:07 -0700900}
901
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500902func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
903 return IsSanitizableDependencyTag
904}
905
Inseob Kimc42f2f22020-07-29 20:32:10 +0900906// Determines if the current module is a static library going to be captured
907// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
908// except for ones which explicitly disable cfi.
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200909func needsCfiForVendorSnapshot(mctx android.BaseModuleContext) bool {
Kiyoung Kim48f37782021-07-07 12:42:39 +0900910 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900911 return false
912 }
913
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500914 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900915
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500916 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900917 return false
918 }
919
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500920 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900921 return false
922 }
923
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500924 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900925 return false
926 }
927
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500928 if !c.SanitizerSupported(cfi) {
929 return false
930 }
931
932 return c.SanitizePropDefined() &&
933 !c.SanitizeNever() &&
934 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900935}
936
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200937type sanitizerSplitMutator struct {
938 sanitizer SanitizerType
939}
940
941// If an APEX is sanitized or not depends on whether it contains at least one
942// sanitized module. Transition mutators cannot propagate information up the
943// dependency graph this way, so we need an auxiliary mutator to do so.
944func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
945 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
946 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
947 ctx.VisitDirectDeps(func(dep android.Module) {
948 if c, ok := dep.(*Module); ok && c.sanitize.isSanitizerEnabled(s.sanitizer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900949 enabled = true
Inseob Kimc42f2f22020-07-29 20:32:10 +0900950 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +0200951 })
952
953 if enabled {
954 sanitizeable.EnableSanitizer(s.sanitizer.name())
955 }
956 }
957}
958
959func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
960 if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
961 if s.sanitizer == cfi && needsCfiForVendorSnapshot(ctx) {
962 return []string{"", s.sanitizer.variationName()}
963 }
964
965 // If the given sanitizer is not requested in the .bp file for a module, it
966 // won't automatically build the sanitized variation.
967 if !c.IsSanitizerEnabled(s.sanitizer) {
968 return []string{""}
969 }
970
971 if c.Binary() {
972 // If a sanitizer is enabled for a binary, we do not build the version
973 // without the sanitizer
974 return []string{s.sanitizer.variationName()}
975 } else if c.StaticallyLinked() || c.Header() {
976 // For static libraries, we build both versions. Some Make modules
977 // apparently depend on this behavior.
978 return []string{"", s.sanitizer.variationName()}
979 } else {
980 // We only build the requested variation of dynamic libraries
981 return []string{s.sanitizer.variationName()}
982 }
983 }
984
985 if _, ok := ctx.Module().(JniSanitizeable); ok {
986 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
987 // that is short-circuited for now
988 return []string{""}
989 }
990
991 // If an APEX has a sanitized dependency, we build the APEX in the sanitized
992 // variation. This is useful because such APEXes require extra dependencies.
993 if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
994 enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
995 if enabled {
996 return []string{s.sanitizer.variationName()}
997 } else {
998 return []string{""}
999 }
1000 }
1001
1002 if c, ok := ctx.Module().(*Module); ok {
1003 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1004
1005 // Check if it's a snapshot module supporting sanitizer
1006 if ss, ok := c.linker.(snapshotSanitizer); ok && ss.isSanitizerEnabled(s.sanitizer) {
1007 return []string{"", s.sanitizer.variationName()}
1008 } else {
1009 return []string{""}
1010 }
1011 }
1012
1013 return []string{""}
1014}
1015
1016func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
1017 if c, ok := ctx.Module().(PlatformSanitizeable); ok {
1018 if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
1019 // If the dependency is through a non-sanitizable tag, use the
1020 // non-sanitized variation
1021 return ""
1022 }
1023
1024 return sourceVariation
1025 } else if _, ok := ctx.Module().(JniSanitizeable); ok {
1026 // TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
1027 // that is short-circuited for now
1028 return ""
1029 } else {
1030 // Otherwise, do not rock the boat.
1031 return sourceVariation
1032 }
1033}
1034
1035func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
1036 if d, ok := ctx.Module().(PlatformSanitizeable); ok {
1037 if dm, ok := ctx.Module().(*Module); ok {
1038 if ss, ok := dm.linker.(snapshotSanitizer); ok && ss.isSanitizerEnabled(s.sanitizer) {
1039 return incomingVariation
Inseob Kimc42f2f22020-07-29 20:32:10 +09001040 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001041 }
1042
1043 if !d.SanitizePropDefined() ||
1044 d.SanitizeNever() ||
1045 d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
1046 !d.SanitizerSupported(s.sanitizer) {
1047 // If a module opts out of a sanitizer, use its non-sanitized variation
1048 return ""
1049 }
1050
1051 // Binaries are always built in the variation they requested.
1052 if d.Binary() {
1053 if d.IsSanitizerEnabled(s.sanitizer) {
1054 return s.sanitizer.variationName()
1055 } else {
1056 return ""
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001057 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001058 }
1059
1060 // If a shared library requests to be sanitized, it will be built for that
1061 // sanitizer. Otherwise, some sanitizers propagate through shared library
1062 // dependency edges, some do not.
1063 if !d.StaticallyLinked() && !d.Header() {
1064 if d.IsSanitizerEnabled(s.sanitizer) {
1065 return s.sanitizer.variationName()
1066 }
1067
1068 if s.sanitizer == cfi || s.sanitizer == Hwasan || s.sanitizer == scs || s.sanitizer == Asan {
1069 return ""
1070 }
1071 }
1072
1073 // Static and header libraries inherit whether they are sanitized from the
1074 // module they are linked into
1075 return incomingVariation
1076 } else if d, ok := ctx.Module().(Sanitizeable); ok {
1077 // If an APEX contains a sanitized module, it will be built in the variation
1078 // corresponding to that sanitizer.
1079 enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
1080 if enabled {
1081 return s.sanitizer.variationName()
1082 }
1083
1084 return incomingVariation
1085 }
1086
1087 return ""
1088}
1089
1090func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
1091 sanitizerVariation := variationName == s.sanitizer.variationName()
1092
1093 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1094 sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
1095
1096 oneMakeVariation := false
1097 if c.StaticallyLinked() || c.Header() {
1098 if s.sanitizer != cfi && s.sanitizer != scs && s.sanitizer != Hwasan {
1099 // These sanitizers export only one variation to Make. For the rest,
1100 // Make targets can depend on both the sanitized and non-sanitized
1101 // versions.
1102 oneMakeVariation = true
1103 }
1104 } else if !c.Binary() {
1105 // Shared library. These are the sanitizers that do propagate through shared
1106 // library dependencies and therefore can cause multiple variations of a
1107 // shared library to be built.
1108 if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != scs && s.sanitizer != Asan {
1109 oneMakeVariation = true
1110 }
1111 }
1112
1113 if oneMakeVariation {
1114 if sanitizerEnabled != sanitizerVariation {
1115 c.SetPreventInstall()
1116 c.SetHideFromMake()
1117 }
1118 }
1119
1120 if sanitizerVariation {
1121 c.SetSanitizer(s.sanitizer, true)
1122
1123 // CFI is incompatible with ASAN so disable it in ASAN variations
1124 if s.sanitizer.incompatibleWithCfi() {
1125 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1126 if mctx.Device() && cfiSupported {
1127 c.SetSanitizer(cfi, false)
Jiyong Parkf97782b2019-02-13 20:28:58 +09001128 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +02001129 }
1130
1131 // locate the asan libraries under /data/asan
1132 if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
1133 c.SetInSanitizerDir()
1134 }
1135
1136 if c.StaticallyLinked() && c.ExportedToMake() {
1137 if s.sanitizer == Hwasan {
1138 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
1139 } else if s.sanitizer == cfi {
1140 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
1141 }
1142 }
1143 } else if c.IsSanitizerEnabled(s.sanitizer) {
1144 // Disable the sanitizer for the non-sanitized variation
1145 c.SetSanitizer(s.sanitizer, false)
1146 }
1147 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
1148 // If an APEX has sanitized dependencies, it gets a few more dependencies
1149 if sanitizerVariation {
1150 sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
1151 }
1152 } else if c, ok := mctx.Module().(*Module); ok {
1153 if ss, ok := c.linker.(snapshotSanitizer); ok && ss.isSanitizerEnabled(s.sanitizer) {
1154 c.linker.(snapshotSanitizer).setSanitizerVariation(s.sanitizer, sanitizerVariation)
1155
1156 // Export the static lib name to make
1157 if c.static() && c.ExportedToMake() {
1158 if s.sanitizer == cfi {
1159 // use BaseModuleName which is the name for Make.
1160 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1161 }
1162 }
Colin Cross16b23492016-01-06 14:41:07 -08001163 }
1164 }
1165}
1166
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001167func (c *Module) SanitizeNever() bool {
1168 return Bool(c.sanitize.Properties.Sanitize.Never)
1169}
1170
1171func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
1172 return c.sanitize.isSanitizerExplicitlyDisabled(t)
1173}
1174
Ivan Lozano30c5db22018-02-21 15:49:20 -08001175// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -07001176func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001177 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001178 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001179 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001180 mctx.WalkDeps(func(child, parent android.Module) bool {
1181 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1182 return false
1183 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001184
Inseob Kimeec88e12020-01-22 11:11:29 +09001185 d, ok := child.(*Module)
1186 if !ok || !d.static() {
1187 return false
1188 }
1189 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001190 if enableMinimalRuntime(d.sanitize) {
1191 // If a static dependency is built with the minimal runtime,
1192 // make sure we include the ubsan minimal runtime.
1193 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001194 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001195 // If a static dependency runs with full ubsan diagnostics,
1196 // make sure we include the ubsan runtime.
1197 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001198 }
Colin Cross0b908332019-06-19 23:00:20 -07001199
1200 if c.sanitize.Properties.MinimalRuntimeDep &&
1201 c.sanitize.Properties.UbsanRuntimeDep {
1202 // both flags that this mutator might set are true, so don't bother recursing
1203 return false
1204 }
1205
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001206 if c.Os() == android.Linux {
1207 c.sanitize.Properties.BuiltinsDep = true
1208 }
1209
Colin Cross0b908332019-06-19 23:00:20 -07001210 return true
Colin Cross6b753602018-06-21 13:03:07 -07001211 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001212
Jose Galmesf7294582020-11-13 12:07:36 -08001213 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001214 if Bool(p.properties.Sanitize_minimal_dep) {
1215 c.sanitize.Properties.MinimalRuntimeDep = true
1216 }
1217 if Bool(p.properties.Sanitize_ubsan_dep) {
1218 c.sanitize.Properties.UbsanRuntimeDep = true
1219 }
1220 }
1221
1222 return false
Colin Cross6b753602018-06-21 13:03:07 -07001223 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001224 }
1225}
1226
Jiyong Park379de2f2018-12-19 02:47:14 +09001227// Add the dependency to the runtime library for each of the sanitizer variants
1228func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001229 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001230 if !c.Enabled() {
1231 return
1232 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001233 var sanitizers []string
1234 var diagSanitizers []string
1235
1236 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
1237 sanitizers = append(sanitizers, "undefined")
1238 } else {
1239 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
1240 sanitizers = append(sanitizers,
1241 "bool",
1242 "integer-divide-by-zero",
1243 "return",
1244 "returns-nonnull-attribute",
1245 "shift-exponent",
1246 "unreachable",
1247 "vla-bound",
1248 // TODO(danalbert): The following checks currently have compiler performance issues.
1249 //"alignment",
1250 //"bounds",
1251 //"enum",
1252 //"float-cast-overflow",
1253 //"float-divide-by-zero",
1254 //"nonnull-attribute",
1255 //"null",
1256 //"shift-base",
1257 //"signed-integer-overflow",
1258 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1259 // https://llvm.org/PR19302
1260 // http://reviews.llvm.org/D6974
1261 // "object-size",
1262 )
1263 }
1264 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
1265 }
1266
1267 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
1268 diagSanitizers = append(diagSanitizers, "undefined")
1269 }
1270
1271 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
1272
1273 if Bool(c.sanitize.Properties.Sanitize.Address) {
1274 sanitizers = append(sanitizers, "address")
1275 diagSanitizers = append(diagSanitizers, "address")
1276 }
1277
1278 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1279 sanitizers = append(sanitizers, "hwaddress")
1280 }
1281
1282 if Bool(c.sanitize.Properties.Sanitize.Thread) {
1283 sanitizers = append(sanitizers, "thread")
1284 }
1285
1286 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
1287 sanitizers = append(sanitizers, "safe-stack")
1288 }
1289
1290 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
1291 sanitizers = append(sanitizers, "cfi")
1292
1293 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
1294 diagSanitizers = append(diagSanitizers, "cfi")
1295 }
1296 }
1297
1298 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
1299 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1300 sanitizers = append(sanitizers, "signed-integer-overflow")
1301 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
1302 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1303 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1304 }
1305 }
1306
1307 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1308 sanitizers = append(sanitizers, "scudo")
1309 }
1310
1311 if Bool(c.sanitize.Properties.Sanitize.Scs) {
1312 sanitizers = append(sanitizers, "shadow-call-stack")
1313 }
1314
Ivan Lozanod7586b62021-04-01 09:49:36 -04001315 if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.Binary() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001316 noteDep := "note_memtag_heap_async"
1317 if Bool(c.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
1318 noteDep = "note_memtag_heap_sync"
1319 }
Inseob Kim253f5212021-04-08 17:10:31 +09001320 // If we're using snapshots, redirect to snapshot whenever possible
1321 // TODO(b/178470649): clean manual snapshot redirections
1322 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1323 if lib, ok := snapshot.StaticLibs[noteDep]; ok {
1324 noteDep = lib
1325 }
Ivan Lozano62cd0382021-11-01 10:27:54 -04001326 depTag := StaticDepTag(true)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001327 variations := append(mctx.Target().Variations(),
1328 blueprint.Variation{Mutator: "link", Variation: "static"})
1329 if c.Device() {
1330 variations = append(variations, c.ImageVariation())
1331 }
1332 mctx.AddFarVariationDependencies(variations, depTag, noteDep)
1333 }
1334
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001335 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
1336 sanitizers = append(sanitizers, "fuzzer-no-link")
1337 }
1338
Jiyong Park379de2f2018-12-19 02:47:14 +09001339 // Save the list of sanitizers. These will be used again when generating
1340 // the build rules (for Cflags, etc.)
1341 c.sanitize.Properties.Sanitizers = sanitizers
1342 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1343
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001344 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1345 if c.Host() {
1346 diagSanitizers = sanitizers
1347 }
1348
Jiyong Park379de2f2018-12-19 02:47:14 +09001349 // Determine the runtime library required
1350 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001351 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001352 toolchain := c.toolchain(mctx)
1353 if Bool(c.sanitize.Properties.Sanitize.Address) {
1354 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1355 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1356 if c.staticBinary() {
1357 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001358 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001359 } else {
1360 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1361 }
1362 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
1363 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1364 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1365 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1366 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1367 } else {
1368 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1369 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001370 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001371 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
1372 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
1373 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001374 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
Colin Cross32f1de32021-03-29 13:41:37 -07001375 if c.staticBinary() {
1376 runtimeLibrary += ".static"
1377 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001378 }
1379
Colin Cross06c80eb2022-02-10 10:34:19 -08001380 addStaticDeps := func(deps ...string) {
1381 // If we're using snapshots, redirect to snapshot whenever possible
1382 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1383 for idx, dep := range deps {
1384 if lib, ok := snapshot.StaticLibs[dep]; ok {
1385 deps[idx] = lib
1386 }
1387 }
1388
1389 // static executable gets static runtime libs
1390 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
1391 variations := append(mctx.Target().Variations(),
1392 blueprint.Variation{Mutator: "link", Variation: "static"})
1393 if c.Device() {
1394 variations = append(variations, c.ImageVariation())
1395 }
1396 if c.UseSdk() {
1397 variations = append(variations,
1398 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1399 }
1400 mctx.AddFarVariationDependencies(variations, depTag, deps...)
1401
1402 }
1403 if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
1404 addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain))
1405 }
1406 if c.sanitize.Properties.BuiltinsDep {
1407 addStaticDeps(config.BuiltinsRuntimeLibrary(toolchain))
1408 }
1409
Colin Crossed12a042022-02-07 13:55:55 -08001410 if runtimeLibrary != "" && (toolchain.Bionic() || toolchain.Musl() || c.sanitize.Properties.UbsanRuntimeDep) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001411 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001412
1413 // Adding dependency to the runtime library. We are using *FarVariation*
1414 // because the runtime libraries themselves are not mutated by sanitizer
1415 // mutators and thus don't have sanitizer variants whereas this module
1416 // has been already mutated.
1417 //
1418 // Note that by adding dependency with {static|shared}DepTag, the lib is
1419 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
1420 if c.staticBinary() {
Colin Cross06c80eb2022-02-10 10:34:19 -08001421 addStaticDeps(runtimeLibrary)
1422 addStaticDeps(extraStaticDeps...)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001423 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001424 // If we're using snapshots, redirect to snapshot whenever possible
1425 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1426 if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
1427 runtimeLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001428 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001429
Cindy Zhou18417cb2020-12-10 07:12:38 -08001430 // Skip apex dependency check for sharedLibraryDependency
1431 // when sanitizer diags are enabled. Skipping the check will allow
1432 // building with diag libraries without having to list the
1433 // dependency in Apex's allowed_deps file.
1434 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001435 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001436 depTag := libraryDependencyTag{
1437 Kind: sharedLibraryDependency,
1438 Order: earlyLibraryDependency,
1439
1440 skipApexAllowedDependenciesCheck: diagEnabled,
1441 }
Colin Cross42507332020-08-21 16:15:23 -07001442 variations := append(mctx.Target().Variations(),
1443 blueprint.Variation{Mutator: "link", Variation: "shared"})
1444 if c.Device() {
1445 variations = append(variations, c.ImageVariation())
1446 }
Colin Cross06c80eb2022-02-10 10:34:19 -08001447 if c.UseSdk() {
1448 variations = append(variations,
1449 blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
1450 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001451 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001452 }
1453 // static lib does not have dependency to the runtime library. The
1454 // dependency will be added to the executables or shared libs using
1455 // the static lib.
1456 }
1457 }
1458}
1459
1460type Sanitizeable interface {
1461 android.Module
Lukacs T. Berki01a648a2022-06-17 08:59:37 +02001462 IsSanitizerEnabled(config android.Config, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001463 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001464 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001465}
1466
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +00001467type JniSanitizeable interface {
1468 android.Module
1469 IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
1470}
1471
Ivan Lozanod7586b62021-04-01 09:49:36 -04001472func (c *Module) MinimalRuntimeDep() bool {
1473 return c.sanitize.Properties.MinimalRuntimeDep
1474}
1475
1476func (c *Module) UbsanRuntimeDep() bool {
1477 return c.sanitize.Properties.UbsanRuntimeDep
1478}
1479
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001480func (c *Module) SanitizePropDefined() bool {
1481 return c.sanitize != nil
1482}
1483
1484func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1485 return c.sanitize.isSanitizerEnabled(t)
1486}
1487
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001488func (c *Module) StaticallyLinked() bool {
1489 return c.static()
1490}
1491
1492func (c *Module) SetInSanitizerDir() {
1493 if c.sanitize != nil {
1494 c.sanitize.Properties.InSanitizerDir = true
1495 }
1496}
1497
1498func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1499 if c.sanitize != nil {
1500 c.sanitize.SetSanitizer(t, b)
1501 }
1502}
1503
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001504var _ PlatformSanitizeable = (*Module)(nil)
1505
Inseob Kim74d25562020-08-04 00:41:38 +09001506type sanitizerStaticLibsMap struct {
1507 // libsMap contains one list of modules per each image and each arch.
1508 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001509 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001510 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001511 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001512}
1513
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001514func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001515 return &sanitizerStaticLibsMap{
1516 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001517 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001518 }
1519}
1520
1521// Add the current module to sanitizer static libs maps
1522// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001523func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1524 image := GetImageVariantType(c)
1525 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001526
1527 s.libsMapLock.Lock()
1528 defer s.libsMapLock.Unlock()
1529
1530 if _, ok := s.libsMap[image]; !ok {
1531 s.libsMap[image] = make(map[string][]string)
1532 }
1533
1534 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1535}
1536
1537// Exports makefile variables in the following format:
1538// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1539// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1540// These are to be used by use_soong_sanitized_static_libraries.
1541// See build/make/core/binary.mk for more details.
1542func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1543 for _, image := range android.SortedStringKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001544 archMap := s.libsMap[ImageVariantType(image)]
Inseob Kim74d25562020-08-04 00:41:38 +09001545 for _, arch := range android.SortedStringKeys(archMap) {
1546 libs := archMap[arch]
1547 sort.Strings(libs)
1548
1549 key := fmt.Sprintf(
1550 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1551 s.sanitizerType.variationName(),
1552 image, // already upper
1553 arch)
1554
1555 ctx.Strict(key, strings.Join(libs, " "))
1556 }
1557 }
1558}
1559
Colin Cross571cccf2019-02-04 11:22:08 -08001560var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1561
Inseob Kim74d25562020-08-04 00:41:38 +09001562func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001563 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001564 return newSanitizerStaticLibsMap(cfi)
1565 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001566}
1567
Colin Cross571cccf2019-02-04 11:22:08 -08001568var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1569
Inseob Kim74d25562020-08-04 00:41:38 +09001570func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001571 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001572 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001573 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001574}
1575
Ivan Lozano30c5db22018-02-21 15:49:20 -08001576func enableMinimalRuntime(sanitize *sanitize) bool {
1577 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001578 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001579 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -08001580 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001581 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1582 Bool(sanitize.Properties.Sanitize.Undefined) ||
1583 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -08001584 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1585 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001586 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001587 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001588
Ivan Lozano30c5db22018-02-21 15:49:20 -08001589 return true
1590 }
1591 return false
1592}
1593
Ivan Lozanod7586b62021-04-01 09:49:36 -04001594func (m *Module) UbsanRuntimeNeeded() bool {
1595 return enableUbsanRuntime(m.sanitize)
1596}
1597
1598func (m *Module) MinimalRuntimeNeeded() bool {
1599 return enableMinimalRuntime(m.sanitize)
1600}
1601
Inseob Kim8471cda2019-11-15 09:59:12 +09001602func enableUbsanRuntime(sanitize *sanitize) bool {
1603 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001604 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001605 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1606}
1607
Vishwath Mohane7128792017-11-17 11:08:10 -08001608func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001609 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001610}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001611
1612func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001613 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001614}