blob: f302c720d6e59464afb378180f69ddffa07db636 [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",
38 "-fno-experimental-new-pass-manager",
39 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070040 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070041
Peter Collingbourne967511a2019-03-19 21:39:54 -070042 hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000043 "-fsanitize-hwaddress-abi=platform",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080044 // The following improves debug location information
45 // availability at the cost of its accuracy. It increases
46 // the likelihood of a stack variable's frame offset
47 // to be recorded in the debug info, which is important
48 // for the quality of hwasan reports. The downside is a
49 // higher number of "optimized out" stack variables.
50 // b/112437883.
51 "-mllvm", "-instcombine-lower-dbg-declare=0",
Mitch Phillipsb1c574f2020-06-22 13:28:23 -070052 // TODO(b/159343917): HWASan and GlobalISel don't play nicely, and
53 // GlobalISel is the default at -O0 on aarch64.
54 "-mllvm", "--aarch64-enable-global-isel-at-O=-1",
55 "-mllvm", "-fast-isel=false",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080056 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070057
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000058 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070059 "-fsanitize-ignorelist=external/compiler-rt/lib/cfi/cfi_blocklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070060 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
61 // used, but have no effect on assembly files
62 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070063 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070064 "-Wl,-plugin-opt,O1"}
Inseob Kim74d25562020-08-04 00:41:38 +090065 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070066
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070067 intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080068
Peter Collingbournebd19db02019-03-06 10:38:48 -080069 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070070 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070071 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
72 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070073)
74
Ivan Lozano3968d8f2020-12-14 11:27:52 -050075type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080076
Colin Cross16b23492016-01-06 14:41:07 -080077const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050078 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070079 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080080 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070081 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080082 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -050083 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -040084 Memtag_heap
Liz Kammer75db9312021-07-07 16:41:50 -040085 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -080086)
87
Liz Kammer75db9312021-07-07 16:41:50 -040088var Sanitizers = []SanitizerType{
89 Asan,
90 Hwasan,
91 tsan,
92 intOverflow,
93 scs,
94 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -040095 Memtag_heap,
Liz Kammer75db9312021-07-07 16:41:50 -040096 cfi, // cfi is last to prevent it running before incompatible mutators
97}
98
Jiyong Park82226632019-02-01 10:50:50 +090099// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500100func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800101 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500102 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800103 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700104 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700105 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800106 case tsan:
107 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700108 case intOverflow:
109 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000110 case cfi:
111 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800112 case scs:
113 return "scs"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400114 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700115 return "memtag_heap"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500116 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700117 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800118 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500119 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800120 }
121}
122
Jiyong Park82226632019-02-01 10:50:50 +0900123// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500124func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900125 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500126 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900127 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700128 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900129 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400130 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700131 return "memtag_heap"
Jiyong Park82226632019-02-01 10:50:50 +0900132 case tsan:
133 return "thread"
134 case intOverflow:
135 return "integer_overflow"
136 case cfi:
137 return "cfi"
138 case scs:
139 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500140 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700141 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900142 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500143 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900144 }
145}
146
Liz Kammer75db9312021-07-07 16:41:50 -0400147func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
148 switch t {
149 case Asan, Hwasan, Fuzzer, scs, tsan, cfi:
150 ctx.TopDown(t.variationName()+"_deps", sanitizerDepsMutator(t))
151 ctx.BottomUp(t.variationName(), sanitizerMutator(t))
Ivan Lozano62cd0382021-11-01 10:27:54 -0400152 case Memtag_heap, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400153 // do nothing
154 default:
155 panic(fmt.Errorf("unknown SanitizerType %d", t))
156 }
157}
158
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500159func (*Module) SanitizerSupported(t SanitizerType) bool {
160 switch t {
161 case Asan:
162 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700163 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500164 return true
165 case tsan:
166 return true
167 case intOverflow:
168 return true
169 case cfi:
170 return true
171 case scs:
172 return true
173 case Fuzzer:
174 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400175 case Memtag_heap:
176 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500177 default:
178 return false
179 }
180}
181
182// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
183func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700184 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900185}
186
Martin Stjernholmb0249572020-09-15 02:32:35 +0100187type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400188 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100189 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800190
Liz Kammer75b9b402021-06-25 15:19:27 -0400191 // ASan (Address sanitizer), incompatible with static binaries.
192 // Always runs in a diagnostic mode.
193 // Use of address sanitizer disables cfi sanitizer.
194 // Hwaddress sanitizer takes precedence over this sanitizer.
195 Address *bool `android:"arch_variant"`
196 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
197 // Always runs in a diagnostic mode.
198 // Use of thread sanitizer disables cfi and scudo sanitizers.
199 // Hwaddress sanitizer takes precedence over this sanitizer.
200 Thread *bool `android:"arch_variant"`
201 // HWASan (Hardware Address sanitizer).
202 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100203 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800204
Liz Kammer75b9b402021-06-25 15:19:27 -0400205 // Undefined behavior sanitizer
206 All_undefined *bool `android:"arch_variant"`
207 // Subset of undefined behavior sanitizer
208 Undefined *bool `android:"arch_variant"`
209 // List of specific undefined behavior sanitizers to enable
210 Misc_undefined []string `android:"arch_variant"`
211 // Fuzzer, incompatible with static binaries.
212 Fuzzer *bool `android:"arch_variant"`
213 // safe-stack sanitizer, incompatible with 32-bit architectures.
214 Safestack *bool `android:"arch_variant"`
215 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
216 Cfi *bool `android:"arch_variant"`
217 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
218 Integer_overflow *bool `android:"arch_variant"`
219 // scudo sanitizer, incompatible with asan, hwasan, tsan
220 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
221 // deprecated
222 Scudo *bool `android:"arch_variant"`
223 // shadow-call-stack sanitizer, only available on arm64
224 Scs *bool `android:"arch_variant"`
225 // Memory-tagging, only available on arm64
226 // if diag.memtag unset or false, enables async memory tagging
227 Memtag_heap *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100228
229 // A modifier for ASAN and HWASAN for write only instrumentation
230 Writeonly *bool `android:"arch_variant"`
231
232 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
233 // Replaces abort() on error with a human-readable error message.
234 // Address and Thread sanitizers always run in diagnostic mode.
235 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400236 // Undefined behavior sanitizer, diagnostic mode
237 Undefined *bool `android:"arch_variant"`
238 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
239 Cfi *bool `android:"arch_variant"`
240 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
241 Integer_overflow *bool `android:"arch_variant"`
242 // Memory-tagging, only available on arm64
243 // requires sanitizer.memtag: true
244 // if set, enables sync memory tagging
245 Memtag_heap *bool `android:"arch_variant"`
246 // List of specific undefined behavior sanitizers to enable in diagnostic mode
247 Misc_undefined []string `android:"arch_variant"`
248 // List of sanitizers to pass to -fno-sanitize-recover
249 // results in only the first detected error for these sanitizers being reported and program then
250 // exits with a non-zero exit code.
251 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800252 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800253
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800254 // Sanitizers to run with flag configuration specified
255 Config struct {
256 // Enables CFI support flags for assembly-heavy libraries
257 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800258 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800259
Liz Kammer75b9b402021-06-25 15:19:27 -0400260 // List of sanitizers to pass to -fsanitize-recover
261 // allows execution to continue for these sanitizers to detect multiple errors rather than only
262 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100263 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000264
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700265 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100266 Blocklist *string
267}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700268
Martin Stjernholmb0249572020-09-15 02:32:35 +0100269type SanitizeProperties struct {
Martin Stjernholmb0249572020-09-15 02:32:35 +0100270 Sanitize SanitizeUserProps `android:"arch_variant"`
271 SanitizerEnabled bool `blueprint:"mutated"`
272 SanitizeDep bool `blueprint:"mutated"`
273 MinimalRuntimeDep bool `blueprint:"mutated"`
274 BuiltinsDep bool `blueprint:"mutated"`
275 UbsanRuntimeDep bool `blueprint:"mutated"`
276 InSanitizerDir bool `blueprint:"mutated"`
277 Sanitizers []string `blueprint:"mutated"`
278 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800279}
280
281type sanitize struct {
282 Properties SanitizeProperties
283}
284
Cindy Zhou18417cb2020-12-10 07:12:38 -0800285// Mark this tag with a check to see if apex dependency check should be skipped
286func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
287 return t.skipApexAllowedDependenciesCheck
288}
289
290var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
291
Vishwath Mohane7128792017-11-17 11:08:10 -0800292func init() {
293 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700294 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800295}
296
Colin Cross16b23492016-01-06 14:41:07 -0800297func (sanitize *sanitize) props() []interface{} {
298 return []interface{}{&sanitize.Properties}
299}
300
301func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700302 s := &sanitize.Properties.Sanitize
303
Colin Cross16b23492016-01-06 14:41:07 -0800304 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700305 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800306 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800307 }
308
309 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800310 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800311 return
312 }
313
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800314 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400315 if ctx.testBinary() {
316 if s.Memtag_heap == nil {
317 s.Memtag_heap = proptools.BoolPtr(true)
318 }
319 if s.Diag.Memtag_heap == nil {
320 s.Diag.Memtag_heap = proptools.BoolPtr(true)
321 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800322 }
323
Colin Cross16b23492016-01-06 14:41:07 -0800324 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700325 var globalSanitizersDiag []string
326
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700327 if ctx.Host() {
328 if !ctx.Windows() {
329 globalSanitizers = ctx.Config().SanitizeHost()
330 }
331 } else {
332 arches := ctx.Config().SanitizeDeviceArch()
333 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
334 globalSanitizers = ctx.Config().SanitizeDevice()
335 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800336 }
337 }
338
Colin Cross16b23492016-01-06 14:41:07 -0800339 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000340 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700341 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400342 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000343 }
Colin Cross16b23492016-01-06 14:41:07 -0800344
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700345 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400346 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000347 }
348
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700349 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400350 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000351 }
352
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700353 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400354 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000355 }
356
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700357 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400358 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700359 }
360
361 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400362 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000363 }
364
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700365 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800366 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400367 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700368 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700369 }
370
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700371 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700372 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700373 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400374 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700375 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700376 }
377
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700378 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400379 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700380 }
381
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700382 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400383 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700384 }
385
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000386 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
387 // Hwaddress and Address are set before, so we can check them here
388 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
389 if s.Address == nil && s.Hwaddress == nil {
390 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
391 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400392 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000393 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700394 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800395 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400396 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800397 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700398 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000399
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000400 if len(globalSanitizers) > 0 {
401 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
402 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700403
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700404 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700405 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700406 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400407 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700408 }
409
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700410 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
411 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400412 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700413 }
414
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800415 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
416 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400417 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800418 }
419
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700420 if len(globalSanitizersDiag) > 0 {
421 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
422 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700423 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700424
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800425 // Enable Memtag for all components in the include paths (for Aarch64 only)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800426 if ctx.Arch().ArchType == android.Arm64 {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800427 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800428 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400429 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800430 }
431 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400432 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800433 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800434 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(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 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800438 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700439 }
440
Elvis Chien9c993542021-06-25 01:15:17 +0800441 // Enable CFI for non-host components in the include paths
442 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400443 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000444 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400445 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700446 }
447 }
448
Elliott Hughesda3a0712020-03-06 16:55:28 -0800449 // Is CFI actually enabled?
450 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400451 s.Cfi = nil
452 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800453 }
454
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700455 // HWASan requires AArch64 hardware feature (top-byte-ignore).
456 if ctx.Arch().ArchType != android.Arm64 {
457 s.Hwaddress = nil
458 }
459
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800460 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800461 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800462 s.Scs = nil
463 }
464
Ivan Lozano62cd0382021-11-01 10:27:54 -0400465 // Memtag_heap is only implemented on AArch64.
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700466 if ctx.Arch().ArchType != android.Arm64 {
467 s.Memtag_heap = nil
468 }
469
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700470 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700471 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400472 s.Cfi = nil
473 s.Diag.Cfi = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700474 }
475
Colin Cross528d67e2021-07-23 22:23:07 +0000476 // Disable sanitizers that depend on the UBSan runtime for windows/darwin/musl builds.
477 if !ctx.Os().Linux() || ctx.Os() == android.LinuxMusl {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400478 s.Cfi = nil
479 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700480 s.Misc_undefined = nil
481 s.Undefined = nil
482 s.All_undefined = nil
483 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800484 }
485
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700486 // Also disable CFI for VNDK variants of components
487 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900488 if ctx.static() {
489 // Cfi variant for static vndk should be captured as vendor snapshot,
490 // so don't strictly disable Cfi.
491 s.Cfi = nil
492 s.Diag.Cfi = nil
493 } else {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400494 s.Cfi = nil
495 s.Diag.Cfi = nil
Inseob Kimc42f2f22020-07-29 20:32:10 +0900496 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900497 }
498
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700499 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700500 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
501 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700502 s.Hwaddress = nil
503 }
504
Colin Cross3c344ef2016-07-18 15:44:56 -0700505 if ctx.staticBinary() {
506 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700507 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700508 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800509 }
510
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700511 if Bool(s.All_undefined) {
512 s.Undefined = nil
513 }
514
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700515 if !ctx.toolchain().Is64Bit() {
516 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700517 s.Thread = nil
518 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800519 // TODO(ccross): error for compile_multilib = "32"?
520 }
521
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800522 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 -0700523 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 -0700524 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700525 sanitize.Properties.SanitizerEnabled = true
526 }
527
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800528 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
529 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700530 s.Scudo = nil
531 }
532
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700533 if Bool(s.Hwaddress) {
534 s.Address = nil
535 s.Thread = nil
Evgenii Stepanovb15a5642021-06-23 12:30:55 -0700536 // Disable ubsan diagnosic as a workaround for a compiler bug.
537 // TODO(b/191808836): re-enable.
538 s.Diag.Undefined = nil
539 s.Diag.Integer_overflow = nil
540 s.Diag.Misc_undefined = nil
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700541 }
542
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700543 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
544 // mutually incompatible.
545 if Bool(s.Fuzzer) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400546 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800547 }
548}
549
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800550func toDisableImplicitIntegerChange(flags []string) bool {
551 // Returns true if any flag is fsanitize*integer, and there is
552 // no explicit flag about sanitize=implicit-integer-sign-change.
553 for _, f := range flags {
554 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
555 return false
556 }
557 }
558 for _, f := range flags {
559 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
560 return true
561 }
562 }
563 return false
564}
565
Yabin Cuidb7dda82020-11-30 15:47:45 -0800566func toDisableUnsignedShiftBaseChange(flags []string) bool {
567 // Returns true if any flag is fsanitize*integer, and there is
568 // no explicit flag about sanitize=unsigned-shift-base.
569 for _, f := range flags {
570 if strings.Contains(f, "sanitize=unsigned-shift-base") {
571 return false
572 }
573 }
574 for _, f := range flags {
575 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
576 return true
577 }
578 }
579 return false
580}
581
Colin Cross16b23492016-01-06 14:41:07 -0800582func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700583 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
584 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500585 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
586 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800587
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500588 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800589 flags.Local.LdFlags = append(flags.Local.LdFlags,
590 minimalRuntimePath,
591 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800592 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500593
594 if sanitize.Properties.BuiltinsDep {
595 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
596 }
597
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...)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000634 if Bool(sanitize.Properties.Sanitize.Writeonly) {
635 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
636 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700637 }
638
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700639 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800640 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700641
642 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800643 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
644 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
645 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
646 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700647
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700648 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
649 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
650 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800651 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
652 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700653
Mitch Phillips74384752019-06-17 10:33:52 -0700654 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800655 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700656
657 // Disable fortify for fuzzing builds. Generally, we'll be building with
658 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800659 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800660
661 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
662 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
663 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
664 // the DT_RUNPATH from the shared library above it, and not the executable,
665 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
666 // DT_RUNPATH here means that transient shared libraries can be found
667 // colocated with their parents.
668 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800669 }
670
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700671 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800672 if ctx.Arch().ArchType == android.Arm {
673 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
674 // to do this on a function basis, so force Thumb on the entire module.
675 flags.RequiredInstructionSet = "thumb"
676 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000677
Colin Cross4af21ed2019-11-04 09:37:55 -0800678 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
679 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800680 if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
681 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
682 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000683 // Only append the default visibility flag if -fvisibility has not already been set
684 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800685 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
686 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000687 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800688 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000689
690 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800691 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
692 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000693 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700694 }
695
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700696 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800697 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700698 }
699
Jiyong Park379de2f2018-12-19 02:47:14 +0900700 if len(sanitize.Properties.Sanitizers) > 0 {
701 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800702
Colin Cross4af21ed2019-11-04 09:37:55 -0800703 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
704 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800705 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800706 // Host sanitizers only link symbols in the final executable, so
707 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800708 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
709 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500710
711 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
712 if !ctx.toolchain().Bionic() {
713 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
714 }
715 }
716
717 if enableMinimalRuntime(sanitize) {
718 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
719 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
720 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
721 if !ctx.toolchain().Bionic() {
722 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800723 }
Colin Cross16b23492016-01-06 14:41:07 -0800724 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700725
726 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
727 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800728 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700729 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800730 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700731 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800732 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700733 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800734 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800735 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
736 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800737 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800738 // http://b/171275751, Android doesn't build with this sanitizer yet.
739 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
740 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
741 }
Colin Cross16b23492016-01-06 14:41:07 -0800742 }
743
Jiyong Park379de2f2018-12-19 02:47:14 +0900744 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800745 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700746 }
747 // FIXME: enable RTTI if diag + (cfi or vptr)
748
Andreas Gampe97071162017-05-08 13:15:23 -0700749 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800750 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700751 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
752 }
753
Ivan Lozano7929bba2018-12-12 09:36:31 -0800754 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800755 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800756 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
757 }
758
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700759 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
760 if blocklist.Valid() {
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700761 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-ignorelist="+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700762 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
763 }
764
Colin Cross16b23492016-01-06 14:41:07 -0800765 return flags
766}
767
Colin Crossd80cbca2020-02-24 12:01:37 -0800768func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900769 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
770 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800771 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900772 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800773 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900774 }
775 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800776 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900777 }
778 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800779 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900780 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800781 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700782}
783
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700784func (sanitize *sanitize) inSanitizerDir() bool {
785 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700786}
787
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500788// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
789func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000790 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500791 case Asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000792 return sanitize.Properties.Sanitize.Address
Tri Vo6eafc362021-04-01 11:29:09 -0700793 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700794 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000795 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000796 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000797 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000798 return sanitize.Properties.Sanitize.Integer_overflow
799 case cfi:
800 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800801 case scs:
802 return sanitize.Properties.Sanitize.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -0400803 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700804 return sanitize.Properties.Sanitize.Memtag_heap
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500805 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700806 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000807 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500808 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000809 }
810}
811
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500812// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800813func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500814 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700815 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800816 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800817 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700818 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -0400819 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500820 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800821}
822
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500823// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700824func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500825 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700826 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700827 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500828 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700829}
830
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500831func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400832 bPtr := proptools.BoolPtr(b)
833 if !b {
834 bPtr = nil
835 }
Colin Cross16b23492016-01-06 14:41:07 -0800836 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500837 case Asan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400838 sanitize.Properties.Sanitize.Address = bPtr
Tri Vo6eafc362021-04-01 11:29:09 -0700839 case Hwasan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400840 sanitize.Properties.Sanitize.Hwaddress = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800841 case tsan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400842 sanitize.Properties.Sanitize.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700843 case intOverflow:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400844 sanitize.Properties.Sanitize.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000845 case cfi:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400846 sanitize.Properties.Sanitize.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800847 case scs:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400848 sanitize.Properties.Sanitize.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -0400849 case Memtag_heap:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400850 sanitize.Properties.Sanitize.Memtag_heap = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500851 case Fuzzer:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400852 sanitize.Properties.Sanitize.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800853 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500854 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800855 }
856 if b {
857 sanitize.Properties.SanitizerEnabled = true
858 }
859}
860
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000861// Check if the sanitizer is explicitly disabled (as opposed to nil by
862// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500863func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000864 if sanitize == nil {
865 return false
866 }
867
868 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
869 return sanitizerVal != nil && *sanitizerVal == false
870}
871
872// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
873// because enabling a sanitizer either directly (via the blueprint) or
874// indirectly (via a mutator) sets the bool ptr to true, and you can't
875// distinguish between the cases. It isn't needed though - both cases can be
876// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500877func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000878 if sanitize == nil {
879 return false
880 }
881
882 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
883 return sanitizerVal != nil && *sanitizerVal == true
884}
885
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500886// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
887func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700888 switch t := tag.(type) {
889 case dependencyTag:
890 return t == reuseObjTag || t == objDepTag
891 case libraryDependencyTag:
892 return true
893 default:
894 return false
895 }
Colin Cross6b753602018-06-21 13:03:07 -0700896}
897
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500898func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
899 return IsSanitizableDependencyTag
900}
901
Inseob Kimc42f2f22020-07-29 20:32:10 +0900902// Determines if the current module is a static library going to be captured
903// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
904// except for ones which explicitly disable cfi.
905func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
Kiyoung Kim48f37782021-07-07 12:42:39 +0900906 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900907 return false
908 }
909
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500910 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900911
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500912 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900913 return false
914 }
915
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500916 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900917 return false
918 }
919
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500920 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900921 return false
922 }
923
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500924 if !c.SanitizerSupported(cfi) {
925 return false
926 }
927
928 return c.SanitizePropDefined() &&
929 !c.SanitizeNever() &&
930 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900931}
932
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700933// Propagate sanitizer requirements down from binaries
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500934func sanitizerDepsMutator(t SanitizerType) func(android.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700935 return func(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500936 if c, ok := mctx.Module().(PlatformSanitizeable); ok {
937 enabled := c.IsSanitizerEnabled(t)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900938 if t == cfi && needsCfiForVendorSnapshot(mctx) {
939 // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly
940 // determine defaultVariation in sanitizerMutator below.
941 // Instead, just mark SanitizeDep to forcefully create cfi variant.
942 enabled = true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500943 c.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900944 }
945 if enabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500946 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Inseob Kimc42f2f22020-07-29 20:32:10 +0900947 mctx.WalkDeps(func(child, parent android.Module) bool {
948 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
949 return false
950 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500951 if d, ok := child.(PlatformSanitizeable); ok && d.SanitizePropDefined() &&
952 !d.SanitizeNever() &&
953 !d.IsSanitizerExplicitlyDisabled(t) {
Colin Crossaf98f582021-05-12 17:27:32 -0700954 if t == cfi || t == Hwasan || t == scs || t == Asan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500955 if d.StaticallyLinked() && d.SanitizerSupported(t) {
956 // Rust does not support some of these sanitizers, so we need to check if it's
957 // supported before setting this true.
958 d.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900959 }
960 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500961 d.SetSanitizeDep(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700962 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000963 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900964 return true
965 })
966 }
Jiyong Parkf97782b2019-02-13 20:28:58 +0900967 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
968 // If an APEX module includes a lib which is enabled for a sanitizer T, then
969 // the APEX module is also enabled for the same sanitizer type.
970 mctx.VisitDirectDeps(func(child android.Module) {
971 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
972 sanitizeable.EnableSanitizer(t.name())
973 }
974 })
Colin Cross16b23492016-01-06 14:41:07 -0800975 }
976 }
977}
978
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500979func (c *Module) SanitizeNever() bool {
980 return Bool(c.sanitize.Properties.Sanitize.Never)
981}
982
983func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
984 return c.sanitize.isSanitizerExplicitlyDisabled(t)
985}
986
Ivan Lozano30c5db22018-02-21 15:49:20 -0800987// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700988func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500989 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -0700990 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500991 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -0700992 mctx.WalkDeps(func(child, parent android.Module) bool {
993 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
994 return false
995 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800996
Inseob Kimeec88e12020-01-22 11:11:29 +0900997 d, ok := child.(*Module)
998 if !ok || !d.static() {
999 return false
1000 }
1001 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001002 if enableMinimalRuntime(d.sanitize) {
1003 // If a static dependency is built with the minimal runtime,
1004 // make sure we include the ubsan minimal runtime.
1005 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001006 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001007 // If a static dependency runs with full ubsan diagnostics,
1008 // make sure we include the ubsan runtime.
1009 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001010 }
Colin Cross0b908332019-06-19 23:00:20 -07001011
1012 if c.sanitize.Properties.MinimalRuntimeDep &&
1013 c.sanitize.Properties.UbsanRuntimeDep {
1014 // both flags that this mutator might set are true, so don't bother recursing
1015 return false
1016 }
1017
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001018 if c.Os() == android.Linux {
1019 c.sanitize.Properties.BuiltinsDep = true
1020 }
1021
Colin Cross0b908332019-06-19 23:00:20 -07001022 return true
Colin Cross6b753602018-06-21 13:03:07 -07001023 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001024
Jose Galmesf7294582020-11-13 12:07:36 -08001025 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001026 if Bool(p.properties.Sanitize_minimal_dep) {
1027 c.sanitize.Properties.MinimalRuntimeDep = true
1028 }
1029 if Bool(p.properties.Sanitize_ubsan_dep) {
1030 c.sanitize.Properties.UbsanRuntimeDep = true
1031 }
1032 }
1033
1034 return false
Colin Cross6b753602018-06-21 13:03:07 -07001035 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001036 }
1037}
1038
Jiyong Park379de2f2018-12-19 02:47:14 +09001039// Add the dependency to the runtime library for each of the sanitizer variants
1040func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001041 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001042 if !c.Enabled() {
1043 return
1044 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001045 var sanitizers []string
1046 var diagSanitizers []string
1047
1048 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
1049 sanitizers = append(sanitizers, "undefined")
1050 } else {
1051 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
1052 sanitizers = append(sanitizers,
1053 "bool",
1054 "integer-divide-by-zero",
1055 "return",
1056 "returns-nonnull-attribute",
1057 "shift-exponent",
1058 "unreachable",
1059 "vla-bound",
1060 // TODO(danalbert): The following checks currently have compiler performance issues.
1061 //"alignment",
1062 //"bounds",
1063 //"enum",
1064 //"float-cast-overflow",
1065 //"float-divide-by-zero",
1066 //"nonnull-attribute",
1067 //"null",
1068 //"shift-base",
1069 //"signed-integer-overflow",
1070 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1071 // https://llvm.org/PR19302
1072 // http://reviews.llvm.org/D6974
1073 // "object-size",
1074 )
1075 }
1076 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
1077 }
1078
1079 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
1080 diagSanitizers = append(diagSanitizers, "undefined")
1081 }
1082
1083 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
1084
1085 if Bool(c.sanitize.Properties.Sanitize.Address) {
1086 sanitizers = append(sanitizers, "address")
1087 diagSanitizers = append(diagSanitizers, "address")
1088 }
1089
1090 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1091 sanitizers = append(sanitizers, "hwaddress")
1092 }
1093
1094 if Bool(c.sanitize.Properties.Sanitize.Thread) {
1095 sanitizers = append(sanitizers, "thread")
1096 }
1097
1098 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
1099 sanitizers = append(sanitizers, "safe-stack")
1100 }
1101
1102 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
1103 sanitizers = append(sanitizers, "cfi")
1104
1105 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
1106 diagSanitizers = append(diagSanitizers, "cfi")
1107 }
1108 }
1109
1110 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
1111 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1112 sanitizers = append(sanitizers, "signed-integer-overflow")
1113 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
1114 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1115 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1116 }
1117 }
1118
1119 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1120 sanitizers = append(sanitizers, "scudo")
1121 }
1122
1123 if Bool(c.sanitize.Properties.Sanitize.Scs) {
1124 sanitizers = append(sanitizers, "shadow-call-stack")
1125 }
1126
Ivan Lozanod7586b62021-04-01 09:49:36 -04001127 if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.Binary() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001128 noteDep := "note_memtag_heap_async"
1129 if Bool(c.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
1130 noteDep = "note_memtag_heap_sync"
1131 }
Inseob Kim253f5212021-04-08 17:10:31 +09001132 // If we're using snapshots, redirect to snapshot whenever possible
1133 // TODO(b/178470649): clean manual snapshot redirections
1134 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1135 if lib, ok := snapshot.StaticLibs[noteDep]; ok {
1136 noteDep = lib
1137 }
Ivan Lozano62cd0382021-11-01 10:27:54 -04001138 depTag := StaticDepTag(true)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001139 variations := append(mctx.Target().Variations(),
1140 blueprint.Variation{Mutator: "link", Variation: "static"})
1141 if c.Device() {
1142 variations = append(variations, c.ImageVariation())
1143 }
1144 mctx.AddFarVariationDependencies(variations, depTag, noteDep)
1145 }
1146
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001147 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
1148 sanitizers = append(sanitizers, "fuzzer-no-link")
1149 }
1150
Jiyong Park379de2f2018-12-19 02:47:14 +09001151 // Save the list of sanitizers. These will be used again when generating
1152 // the build rules (for Cflags, etc.)
1153 c.sanitize.Properties.Sanitizers = sanitizers
1154 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1155
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001156 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1157 if c.Host() {
1158 diagSanitizers = sanitizers
1159 }
1160
Jiyong Park379de2f2018-12-19 02:47:14 +09001161 // Determine the runtime library required
1162 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001163 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001164 toolchain := c.toolchain(mctx)
1165 if Bool(c.sanitize.Properties.Sanitize.Address) {
1166 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1167 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1168 if c.staticBinary() {
1169 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001170 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001171 } else {
1172 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1173 }
1174 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
1175 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1176 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1177 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1178 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1179 } else {
1180 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1181 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001182 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001183 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
1184 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
1185 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001186 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
Colin Cross32f1de32021-03-29 13:41:37 -07001187 if c.staticBinary() {
1188 runtimeLibrary += ".static"
1189 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001190 }
1191
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001192 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
1193 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001194
1195 // Adding dependency to the runtime library. We are using *FarVariation*
1196 // because the runtime libraries themselves are not mutated by sanitizer
1197 // mutators and thus don't have sanitizer variants whereas this module
1198 // has been already mutated.
1199 //
1200 // Note that by adding dependency with {static|shared}DepTag, the lib is
1201 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
1202 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001203 deps := append(extraStaticDeps, runtimeLibrary)
Colin Crosse0edaf92021-01-11 17:31:17 -08001204 // If we're using snapshots, redirect to snapshot whenever possible
1205 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1206 for idx, dep := range deps {
1207 if lib, ok := snapshot.StaticLibs[dep]; ok {
1208 deps[idx] = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001209 }
1210 }
1211
Jiyong Park379de2f2018-12-19 02:47:14 +09001212 // static executable gets static runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -07001213 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Colin Cross42507332020-08-21 16:15:23 -07001214 variations := append(mctx.Target().Variations(),
1215 blueprint.Variation{Mutator: "link", Variation: "static"})
1216 if c.Device() {
1217 variations = append(variations, c.ImageVariation())
1218 }
1219 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001220 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001221 // If we're using snapshots, redirect to snapshot whenever possible
1222 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1223 if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
1224 runtimeLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001225 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001226
Cindy Zhou18417cb2020-12-10 07:12:38 -08001227 // Skip apex dependency check for sharedLibraryDependency
1228 // when sanitizer diags are enabled. Skipping the check will allow
1229 // building with diag libraries without having to list the
1230 // dependency in Apex's allowed_deps file.
1231 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001232 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001233 depTag := libraryDependencyTag{
1234 Kind: sharedLibraryDependency,
1235 Order: earlyLibraryDependency,
1236
1237 skipApexAllowedDependenciesCheck: diagEnabled,
1238 }
Colin Cross42507332020-08-21 16:15:23 -07001239 variations := append(mctx.Target().Variations(),
1240 blueprint.Variation{Mutator: "link", Variation: "shared"})
1241 if c.Device() {
1242 variations = append(variations, c.ImageVariation())
1243 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001244 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001245 }
1246 // static lib does not have dependency to the runtime library. The
1247 // dependency will be added to the executables or shared libs using
1248 // the static lib.
1249 }
1250 }
1251}
1252
1253type Sanitizeable interface {
1254 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +09001255 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001256 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001257 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001258}
1259
Ivan Lozanod7586b62021-04-01 09:49:36 -04001260func (c *Module) MinimalRuntimeDep() bool {
1261 return c.sanitize.Properties.MinimalRuntimeDep
1262}
1263
1264func (c *Module) UbsanRuntimeDep() bool {
1265 return c.sanitize.Properties.UbsanRuntimeDep
1266}
1267
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001268func (c *Module) SanitizePropDefined() bool {
1269 return c.sanitize != nil
1270}
1271
1272func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1273 return c.sanitize.isSanitizerEnabled(t)
1274}
1275
1276func (c *Module) SanitizeDep() bool {
1277 return c.sanitize.Properties.SanitizeDep
1278}
1279
1280func (c *Module) StaticallyLinked() bool {
1281 return c.static()
1282}
1283
1284func (c *Module) SetInSanitizerDir() {
1285 if c.sanitize != nil {
1286 c.sanitize.Properties.InSanitizerDir = true
1287 }
1288}
1289
1290func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1291 if c.sanitize != nil {
1292 c.sanitize.SetSanitizer(t, b)
1293 }
1294}
1295
1296func (c *Module) SetSanitizeDep(b bool) {
1297 if c.sanitize != nil {
1298 c.sanitize.Properties.SanitizeDep = b
1299 }
1300}
1301
1302var _ PlatformSanitizeable = (*Module)(nil)
1303
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001304// Create sanitized variants for modules that need them
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001305func sanitizerMutator(t SanitizerType) func(android.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -07001306 return func(mctx android.BottomUpMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001307 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
Ivan Lozano5482d6a2021-11-01 10:13:25 -04001308
1309 // Make sure we're not setting CFI to any value if it's not supported.
1310 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1311
Liz Kammer187d5442021-06-25 14:50:12 -04001312 if c.Binary() && c.IsSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001313 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001314 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1315 } else if c.IsSanitizerEnabled(t) || c.SanitizeDep() {
1316 isSanitizerEnabled := c.IsSanitizerEnabled(t)
Colin Crossaf98f582021-05-12 17:27:32 -07001317 if c.StaticallyLinked() || c.Header() || t == Fuzzer {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001318 // Static and header libs are split into non-sanitized and sanitized variants.
1319 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1320 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1321 // that isn't sanitized for asan/fuzzer.
1322 //
1323 // Note for defaultVariation: since we don't split for shared libs but for static/header
1324 // libs, it is possible for the sanitized variant of a static/header lib to depend
1325 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1326 // error when the module is split. defaultVariation is the name of the variation that
1327 // will be used when such a dangling dependency occurs during the split of the current
1328 // module. By setting it to the name of the sanitized variation, the dangling dependency
1329 // is redirected to the sanitized variant of the dependent module.
1330 defaultVariation := t.variationName()
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001331 // Not all PlatformSanitizeable modules support the CFI sanitizer
Jiyong Park1d1119f2019-07-29 21:27:18 +09001332 mctx.SetDefaultDependencyVariation(&defaultVariation)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001333
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001334 modules := mctx.CreateVariations("", t.variationName())
1335 modules[0].(PlatformSanitizeable).SetSanitizer(t, false)
1336 modules[1].(PlatformSanitizeable).SetSanitizer(t, true)
1337 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
1338 modules[1].(PlatformSanitizeable).SetSanitizeDep(false)
1339
1340 if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
Ivan Lozano4774a812020-03-10 16:23:57 -04001341 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1342 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001343 modules[1].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001344 }
1345
Jiyong Park1d1119f2019-07-29 21:27:18 +09001346 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1347 // to Make, because the sanitized version has a different suffix in name.
1348 // For other types of sanitizers, suppress the variation that is disabled.
Tri Vo6eafc362021-04-01 11:29:09 -07001349 if t != cfi && t != scs && t != Hwasan {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001350 if isSanitizerEnabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001351 modules[0].(PlatformSanitizeable).SetPreventInstall()
1352 modules[0].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001353 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001354 modules[1].(PlatformSanitizeable).SetPreventInstall()
1355 modules[1].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001356 }
1357 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001358
Jiyong Park1d1119f2019-07-29 21:27:18 +09001359 // Export the static lib name to make
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001360 if c.StaticallyLinked() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001361 if t == cfi {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001362 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
Tri Vo6eafc362021-04-01 11:29:09 -07001363 } else if t == Hwasan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001364 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001365 }
1366 }
1367 } else {
1368 // Shared libs are not split. Only the sanitized variant is created.
1369 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001370 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1371 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
Vishwath Mohane7128792017-11-17 11:08:10 -08001372
Jiyong Park1d1119f2019-07-29 21:27:18 +09001373 // locate the asan libraries under /data/asan
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001374 if mctx.Device() && t == Asan && isSanitizerEnabled {
1375 modules[0].(PlatformSanitizeable).SetInSanitizerDir()
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001376 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001377
Ivan Lozano5482d6a2021-11-01 10:13:25 -04001378 if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
Ivan Lozano4774a812020-03-10 16:23:57 -04001379 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1380 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001381 modules[0].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001382 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001383 }
Colin Cross16b23492016-01-06 14:41:07 -08001384 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001385 c.SetSanitizeDep(false)
Jiyong Park82226632019-02-01 10:50:50 +09001386 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001387 // APEX modules fall here
Jooyung Han8ce8db92020-05-15 19:05:05 +09001388 sanitizeable.AddSanitizerDependencies(mctx, t.name())
Jiyong Park82226632019-02-01 10:50:50 +09001389 mctx.CreateVariations(t.variationName())
Inseob Kimc42f2f22020-07-29 20:32:10 +09001390 } else if c, ok := mctx.Module().(*Module); ok {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001391 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1392
Inseob Kimc42f2f22020-07-29 20:32:10 +09001393 // Check if it's a snapshot module supporting sanitizer
1394 if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) {
1395 // Set default variation as above.
1396 defaultVariation := t.variationName()
1397 mctx.SetDefaultDependencyVariation(&defaultVariation)
1398 modules := mctx.CreateVariations("", t.variationName())
1399 modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false)
1400 modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true)
1401
1402 // Export the static lib name to make
1403 if c.static() && c.ExportedToMake() {
1404 if t == cfi {
1405 // use BaseModuleName which is the name for Make.
1406 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1407 }
1408 }
1409 }
Colin Cross16b23492016-01-06 14:41:07 -08001410 }
1411 }
1412}
Vishwath Mohane7128792017-11-17 11:08:10 -08001413
Inseob Kim74d25562020-08-04 00:41:38 +09001414type sanitizerStaticLibsMap struct {
1415 // libsMap contains one list of modules per each image and each arch.
1416 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001417 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001418 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001419 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001420}
1421
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001422func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001423 return &sanitizerStaticLibsMap{
1424 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001425 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001426 }
1427}
1428
1429// Add the current module to sanitizer static libs maps
1430// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001431func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1432 image := GetImageVariantType(c)
1433 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001434
1435 s.libsMapLock.Lock()
1436 defer s.libsMapLock.Unlock()
1437
1438 if _, ok := s.libsMap[image]; !ok {
1439 s.libsMap[image] = make(map[string][]string)
1440 }
1441
1442 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1443}
1444
1445// Exports makefile variables in the following format:
1446// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1447// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1448// These are to be used by use_soong_sanitized_static_libraries.
1449// See build/make/core/binary.mk for more details.
1450func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1451 for _, image := range android.SortedStringKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001452 archMap := s.libsMap[ImageVariantType(image)]
Inseob Kim74d25562020-08-04 00:41:38 +09001453 for _, arch := range android.SortedStringKeys(archMap) {
1454 libs := archMap[arch]
1455 sort.Strings(libs)
1456
1457 key := fmt.Sprintf(
1458 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1459 s.sanitizerType.variationName(),
1460 image, // already upper
1461 arch)
1462
1463 ctx.Strict(key, strings.Join(libs, " "))
1464 }
1465 }
1466}
1467
Colin Cross571cccf2019-02-04 11:22:08 -08001468var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1469
Inseob Kim74d25562020-08-04 00:41:38 +09001470func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001471 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001472 return newSanitizerStaticLibsMap(cfi)
1473 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001474}
1475
Colin Cross571cccf2019-02-04 11:22:08 -08001476var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1477
Inseob Kim74d25562020-08-04 00:41:38 +09001478func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001479 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001480 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001481 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001482}
1483
Ivan Lozano30c5db22018-02-21 15:49:20 -08001484func enableMinimalRuntime(sanitize *sanitize) bool {
1485 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001486 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001487 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001488
Ivan Lozano30c5db22018-02-21 15:49:20 -08001489 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001490 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1491 Bool(sanitize.Properties.Sanitize.Undefined) ||
1492 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1493
Ivan Lozano30c5db22018-02-21 15:49:20 -08001494 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1495 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001496 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001497 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001498
Ivan Lozano30c5db22018-02-21 15:49:20 -08001499 return true
1500 }
1501 return false
1502}
1503
Ivan Lozanod7586b62021-04-01 09:49:36 -04001504func (m *Module) UbsanRuntimeNeeded() bool {
1505 return enableUbsanRuntime(m.sanitize)
1506}
1507
1508func (m *Module) MinimalRuntimeNeeded() bool {
1509 return enableMinimalRuntime(m.sanitize)
1510}
1511
Inseob Kim8471cda2019-11-15 09:59:12 +09001512func enableUbsanRuntime(sanitize *sanitize) bool {
1513 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001514 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001515 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1516}
1517
Vishwath Mohane7128792017-11-17 11:08:10 -08001518func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001519 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001520}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001521
1522func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001523 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001524}