blob: d7b1adee51b4a7ed88ffa4baca495452d4b0671e [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",
Yi Kong286abc62021-11-04 16:14:14 +080045 }
46
47 // ThinLTO performs codegen during link time, thus these flags need to
48 // passed to both CFLAGS and LDFLAGS.
49 hwasanCommonflags = []string{
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080050 // The following improves debug location information
51 // availability at the cost of its accuracy. It increases
52 // the likelihood of a stack variable's frame offset
53 // to be recorded in the debug info, which is important
54 // for the quality of hwasan reports. The downside is a
55 // higher number of "optimized out" stack variables.
56 // b/112437883.
Yi Kong286abc62021-11-04 16:14:14 +080057 "-instcombine-lower-dbg-declare=0",
Mitch Phillipsb1c574f2020-06-22 13:28:23 -070058 // TODO(b/159343917): HWASan and GlobalISel don't play nicely, and
59 // GlobalISel is the default at -O0 on aarch64.
Yi Kong286abc62021-11-04 16:14:14 +080060 "--aarch64-enable-global-isel-at-O=-1",
61 "-fast-isel=false",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080062 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070063
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000064 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070065 "-fsanitize-ignorelist=external/compiler-rt/lib/cfi/cfi_blocklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070066 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
67 // used, but have no effect on assembly files
68 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070069 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070070 "-Wl,-plugin-opt,O1"}
Inseob Kim74d25562020-08-04 00:41:38 +090071 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070072
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070073 intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080074
Peter Collingbournebd19db02019-03-06 10:38:48 -080075 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070076 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070077 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
78 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070079)
80
Ivan Lozano3968d8f2020-12-14 11:27:52 -050081type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080082
Colin Cross16b23492016-01-06 14:41:07 -080083const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050084 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070085 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080086 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070087 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080088 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -050089 Fuzzer
Ivan Lozano62cd0382021-11-01 10:27:54 -040090 Memtag_heap
Liz Kammer75db9312021-07-07 16:41:50 -040091 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -080092)
93
Liz Kammer75db9312021-07-07 16:41:50 -040094var Sanitizers = []SanitizerType{
95 Asan,
96 Hwasan,
97 tsan,
98 intOverflow,
99 scs,
100 Fuzzer,
Ivan Lozano62cd0382021-11-01 10:27:54 -0400101 Memtag_heap,
Liz Kammer75db9312021-07-07 16:41:50 -0400102 cfi, // cfi is last to prevent it running before incompatible mutators
103}
104
Jiyong Park82226632019-02-01 10:50:50 +0900105// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500106func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800107 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500108 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800109 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700110 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700111 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800112 case tsan:
113 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700114 case intOverflow:
115 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000116 case cfi:
117 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800118 case scs:
119 return "scs"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400120 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700121 return "memtag_heap"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500122 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700123 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800124 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500125 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800126 }
127}
128
Jiyong Park82226632019-02-01 10:50:50 +0900129// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500130func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900131 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500132 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900133 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700134 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900135 return "hwaddress"
Ivan Lozano62cd0382021-11-01 10:27:54 -0400136 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700137 return "memtag_heap"
Jiyong Park82226632019-02-01 10:50:50 +0900138 case tsan:
139 return "thread"
140 case intOverflow:
141 return "integer_overflow"
142 case cfi:
143 return "cfi"
144 case scs:
145 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500146 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700147 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900148 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500149 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900150 }
151}
152
Liz Kammer75db9312021-07-07 16:41:50 -0400153func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
154 switch t {
155 case Asan, Hwasan, Fuzzer, scs, tsan, cfi:
156 ctx.TopDown(t.variationName()+"_deps", sanitizerDepsMutator(t))
157 ctx.BottomUp(t.variationName(), sanitizerMutator(t))
Ivan Lozano62cd0382021-11-01 10:27:54 -0400158 case Memtag_heap, intOverflow:
Liz Kammer75db9312021-07-07 16:41:50 -0400159 // do nothing
160 default:
161 panic(fmt.Errorf("unknown SanitizerType %d", t))
162 }
163}
164
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500165func (*Module) SanitizerSupported(t SanitizerType) bool {
166 switch t {
167 case Asan:
168 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700169 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500170 return true
171 case tsan:
172 return true
173 case intOverflow:
174 return true
175 case cfi:
176 return true
177 case scs:
178 return true
179 case Fuzzer:
180 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400181 case Memtag_heap:
182 return true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500183 default:
184 return false
185 }
186}
187
188// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
189func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700190 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900191}
192
Martin Stjernholmb0249572020-09-15 02:32:35 +0100193type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400194 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100195 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800196
Liz Kammer75b9b402021-06-25 15:19:27 -0400197 // ASan (Address sanitizer), incompatible with static binaries.
198 // Always runs in a diagnostic mode.
199 // Use of address sanitizer disables cfi sanitizer.
200 // Hwaddress sanitizer takes precedence over this sanitizer.
201 Address *bool `android:"arch_variant"`
202 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
203 // Always runs in a diagnostic mode.
204 // Use of thread sanitizer disables cfi and scudo sanitizers.
205 // Hwaddress sanitizer takes precedence over this sanitizer.
206 Thread *bool `android:"arch_variant"`
207 // HWASan (Hardware Address sanitizer).
208 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100209 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800210
Liz Kammer75b9b402021-06-25 15:19:27 -0400211 // Undefined behavior sanitizer
212 All_undefined *bool `android:"arch_variant"`
213 // Subset of undefined behavior sanitizer
214 Undefined *bool `android:"arch_variant"`
215 // List of specific undefined behavior sanitizers to enable
216 Misc_undefined []string `android:"arch_variant"`
217 // Fuzzer, incompatible with static binaries.
218 Fuzzer *bool `android:"arch_variant"`
219 // safe-stack sanitizer, incompatible with 32-bit architectures.
220 Safestack *bool `android:"arch_variant"`
221 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
222 Cfi *bool `android:"arch_variant"`
223 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
224 Integer_overflow *bool `android:"arch_variant"`
225 // scudo sanitizer, incompatible with asan, hwasan, tsan
226 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
227 // deprecated
228 Scudo *bool `android:"arch_variant"`
229 // shadow-call-stack sanitizer, only available on arm64
230 Scs *bool `android:"arch_variant"`
231 // Memory-tagging, only available on arm64
232 // if diag.memtag unset or false, enables async memory tagging
233 Memtag_heap *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100234
235 // A modifier for ASAN and HWASAN for write only instrumentation
236 Writeonly *bool `android:"arch_variant"`
237
238 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
239 // Replaces abort() on error with a human-readable error message.
240 // Address and Thread sanitizers always run in diagnostic mode.
241 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400242 // Undefined behavior sanitizer, diagnostic mode
243 Undefined *bool `android:"arch_variant"`
244 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
245 Cfi *bool `android:"arch_variant"`
246 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
247 Integer_overflow *bool `android:"arch_variant"`
248 // Memory-tagging, only available on arm64
249 // requires sanitizer.memtag: true
250 // if set, enables sync memory tagging
251 Memtag_heap *bool `android:"arch_variant"`
252 // List of specific undefined behavior sanitizers to enable in diagnostic mode
253 Misc_undefined []string `android:"arch_variant"`
254 // List of sanitizers to pass to -fno-sanitize-recover
255 // results in only the first detected error for these sanitizers being reported and program then
256 // exits with a non-zero exit code.
257 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800258 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800259
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800260 // Sanitizers to run with flag configuration specified
261 Config struct {
262 // Enables CFI support flags for assembly-heavy libraries
263 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800264 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800265
Liz Kammer75b9b402021-06-25 15:19:27 -0400266 // List of sanitizers to pass to -fsanitize-recover
267 // allows execution to continue for these sanitizers to detect multiple errors rather than only
268 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100269 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000270
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700271 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100272 Blocklist *string
273}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700274
Martin Stjernholmb0249572020-09-15 02:32:35 +0100275type SanitizeProperties struct {
Martin Stjernholmb0249572020-09-15 02:32:35 +0100276 Sanitize SanitizeUserProps `android:"arch_variant"`
277 SanitizerEnabled bool `blueprint:"mutated"`
278 SanitizeDep bool `blueprint:"mutated"`
279 MinimalRuntimeDep bool `blueprint:"mutated"`
280 BuiltinsDep bool `blueprint:"mutated"`
281 UbsanRuntimeDep bool `blueprint:"mutated"`
282 InSanitizerDir bool `blueprint:"mutated"`
283 Sanitizers []string `blueprint:"mutated"`
284 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800285}
286
287type sanitize struct {
288 Properties SanitizeProperties
289}
290
Cindy Zhou18417cb2020-12-10 07:12:38 -0800291// Mark this tag with a check to see if apex dependency check should be skipped
292func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
293 return t.skipApexAllowedDependenciesCheck
294}
295
296var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
297
Vishwath Mohane7128792017-11-17 11:08:10 -0800298func init() {
299 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700300 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800301}
302
Colin Cross16b23492016-01-06 14:41:07 -0800303func (sanitize *sanitize) props() []interface{} {
304 return []interface{}{&sanitize.Properties}
305}
306
307func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700308 s := &sanitize.Properties.Sanitize
309
Colin Cross16b23492016-01-06 14:41:07 -0800310 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700311 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800312 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800313 }
314
315 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800316 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800317 return
318 }
319
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800320 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400321 if ctx.testBinary() {
322 if s.Memtag_heap == nil {
323 s.Memtag_heap = proptools.BoolPtr(true)
324 }
325 if s.Diag.Memtag_heap == nil {
326 s.Diag.Memtag_heap = proptools.BoolPtr(true)
327 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800328 }
329
Colin Cross16b23492016-01-06 14:41:07 -0800330 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700331 var globalSanitizersDiag []string
332
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700333 if ctx.Host() {
334 if !ctx.Windows() {
335 globalSanitizers = ctx.Config().SanitizeHost()
336 }
337 } else {
338 arches := ctx.Config().SanitizeDeviceArch()
339 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
340 globalSanitizers = ctx.Config().SanitizeDevice()
341 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800342 }
343 }
344
Colin Cross16b23492016-01-06 14:41:07 -0800345 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000346 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700347 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400348 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000349 }
Colin Cross16b23492016-01-06 14:41:07 -0800350
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700351 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400352 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000353 }
354
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700355 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400356 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000357 }
358
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700359 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400360 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000361 }
362
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700363 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400364 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700365 }
366
367 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400368 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000369 }
370
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700371 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800372 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400373 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700374 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700375 }
376
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700377 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700378 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700379 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400380 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700381 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700382 }
383
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700384 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400385 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700386 }
387
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700388 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400389 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700390 }
391
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000392 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
393 // Hwaddress and Address are set before, so we can check them here
394 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
395 if s.Address == nil && s.Hwaddress == nil {
396 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
397 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400398 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000399 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700400 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800401 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400402 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800403 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700404 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000405
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000406 if len(globalSanitizers) > 0 {
407 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
408 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700409
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700410 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700411 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700412 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400413 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700414 }
415
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700416 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
417 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400418 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700419 }
420
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800421 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
422 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400423 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800424 }
425
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700426 if len(globalSanitizersDiag) > 0 {
427 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
428 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700429 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700430
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800431 // Enable Memtag for all components in the include paths (for Aarch64 only)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800432 if ctx.Arch().ArchType == android.Arm64 {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800433 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800434 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400435 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800436 }
437 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400438 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800439 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800440 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800441 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400442 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800443 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800444 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700445 }
446
Elvis Chien9c993542021-06-25 01:15:17 +0800447 // Enable CFI for non-host components in the include paths
448 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400449 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000450 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400451 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700452 }
453 }
454
Elliott Hughesda3a0712020-03-06 16:55:28 -0800455 // Is CFI actually enabled?
456 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400457 s.Cfi = nil
458 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800459 }
460
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700461 // HWASan requires AArch64 hardware feature (top-byte-ignore).
462 if ctx.Arch().ArchType != android.Arm64 {
463 s.Hwaddress = nil
464 }
465
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800466 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800467 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800468 s.Scs = nil
469 }
470
Ivan Lozano62cd0382021-11-01 10:27:54 -0400471 // Memtag_heap is only implemented on AArch64.
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700472 if ctx.Arch().ArchType != android.Arm64 {
473 s.Memtag_heap = nil
474 }
475
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700476 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700477 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400478 s.Cfi = nil
479 s.Diag.Cfi = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700480 }
481
Colin Cross528d67e2021-07-23 22:23:07 +0000482 // Disable sanitizers that depend on the UBSan runtime for windows/darwin/musl builds.
483 if !ctx.Os().Linux() || ctx.Os() == android.LinuxMusl {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400484 s.Cfi = nil
485 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700486 s.Misc_undefined = nil
487 s.Undefined = nil
488 s.All_undefined = nil
489 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800490 }
491
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700492 // Also disable CFI for VNDK variants of components
493 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900494 if ctx.static() {
495 // Cfi variant for static vndk should be captured as vendor snapshot,
496 // so don't strictly disable Cfi.
497 s.Cfi = nil
498 s.Diag.Cfi = nil
499 } else {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400500 s.Cfi = nil
501 s.Diag.Cfi = nil
Inseob Kimc42f2f22020-07-29 20:32:10 +0900502 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900503 }
504
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700505 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700506 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
507 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700508 s.Hwaddress = nil
509 }
510
Colin Cross3c344ef2016-07-18 15:44:56 -0700511 if ctx.staticBinary() {
512 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700513 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700514 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800515 }
516
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700517 if Bool(s.All_undefined) {
518 s.Undefined = nil
519 }
520
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700521 if !ctx.toolchain().Is64Bit() {
522 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700523 s.Thread = nil
524 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800525 // TODO(ccross): error for compile_multilib = "32"?
526 }
527
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800528 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 -0700529 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 -0700530 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700531 sanitize.Properties.SanitizerEnabled = true
532 }
533
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800534 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
535 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700536 s.Scudo = nil
537 }
538
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700539 if Bool(s.Hwaddress) {
540 s.Address = nil
541 s.Thread = nil
Evgenii Stepanovb15a5642021-06-23 12:30:55 -0700542 // Disable ubsan diagnosic as a workaround for a compiler bug.
543 // TODO(b/191808836): re-enable.
544 s.Diag.Undefined = nil
545 s.Diag.Integer_overflow = nil
546 s.Diag.Misc_undefined = nil
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700547 }
548
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700549 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
550 // mutually incompatible.
551 if Bool(s.Fuzzer) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400552 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800553 }
554}
555
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800556func toDisableImplicitIntegerChange(flags []string) bool {
557 // Returns true if any flag is fsanitize*integer, and there is
558 // no explicit flag about sanitize=implicit-integer-sign-change.
559 for _, f := range flags {
560 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
561 return false
562 }
563 }
564 for _, f := range flags {
565 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
566 return true
567 }
568 }
569 return false
570}
571
Yabin Cuidb7dda82020-11-30 15:47:45 -0800572func toDisableUnsignedShiftBaseChange(flags []string) bool {
573 // Returns true if any flag is fsanitize*integer, and there is
574 // no explicit flag about sanitize=unsigned-shift-base.
575 for _, f := range flags {
576 if strings.Contains(f, "sanitize=unsigned-shift-base") {
577 return false
578 }
579 }
580 for _, f := range flags {
581 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
582 return true
583 }
584 }
585 return false
586}
587
Colin Cross16b23492016-01-06 14:41:07 -0800588func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700589 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
590 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500591 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
592 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800593
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500594 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800595 flags.Local.LdFlags = append(flags.Local.LdFlags,
596 minimalRuntimePath,
597 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800598 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500599
600 if sanitize.Properties.BuiltinsDep {
601 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
602 }
603
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700604 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800605 return flags
606 }
607
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700608 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700609 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800610 // Frame pointer based unwinder in ASan requires ARM frame setup.
611 // TODO: put in flags?
612 flags.RequiredInstructionSet = "arm"
613 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800614 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
615 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800616
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000617 if Bool(sanitize.Properties.Sanitize.Writeonly) {
618 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
619 }
620
Colin Cross16b23492016-01-06 14:41:07 -0800621 if ctx.Host() {
622 // -nodefaultlibs (provided with libc++) prevents the driver from linking
623 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800624 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800625 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800626 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900627 if ctx.bootstrap() {
628 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
629 } else {
630 flags.DynamicLinker = "/system/bin/linker_asan"
631 }
Colin Cross16b23492016-01-06 14:41:07 -0800632 if flags.Toolchain.Is64Bit() {
633 flags.DynamicLinker += "64"
634 }
635 }
Colin Cross16b23492016-01-06 14:41:07 -0800636 }
637
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700638 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800639 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yi Kong286abc62021-11-04 16:14:14 +0800640
641 for _, flag := range hwasanCommonflags {
642 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
643 }
644 for _, flag := range hwasanCommonflags {
645 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
646 }
647
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000648 if Bool(sanitize.Properties.Sanitize.Writeonly) {
649 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
650 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700651 }
652
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700653 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800654 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700655
656 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800657 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
658 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
659 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
660 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700661
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700662 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
663 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
664 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800665 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
666 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700667
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700668 // Disable fortify for fuzzing builds. Generally, we'll be building with
669 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800670 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800671
672 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
673 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
674 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
675 // the DT_RUNPATH from the shared library above it, and not the executable,
676 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
677 // DT_RUNPATH here means that transient shared libraries can be found
678 // colocated with their parents.
679 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800680 }
681
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700682 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800683 if ctx.Arch().ArchType == android.Arm {
684 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
685 // to do this on a function basis, so force Thumb on the entire module.
686 flags.RequiredInstructionSet = "thumb"
687 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000688
Colin Cross4af21ed2019-11-04 09:37:55 -0800689 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
690 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800691 if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
692 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
693 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000694 // Only append the default visibility flag if -fvisibility has not already been set
695 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800696 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
697 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000698 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800699 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000700
701 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800702 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
703 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000704 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700705 }
706
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700707 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800708 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700709 }
710
Jiyong Park379de2f2018-12-19 02:47:14 +0900711 if len(sanitize.Properties.Sanitizers) > 0 {
712 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800713
Colin Cross4af21ed2019-11-04 09:37:55 -0800714 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
715 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800716 if ctx.Host() {
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)
720 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500721
722 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
723 if !ctx.toolchain().Bionic() {
724 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
725 }
726 }
727
728 if enableMinimalRuntime(sanitize) {
729 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
730 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
731 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
732 if !ctx.toolchain().Bionic() {
733 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800734 }
Colin Cross16b23492016-01-06 14:41:07 -0800735 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700736
737 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
738 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800739 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700740 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800741 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700742 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800743 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700744 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800745 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800746 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
747 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800748 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800749 // http://b/171275751, Android doesn't build with this sanitizer yet.
750 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
751 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
752 }
Colin Cross16b23492016-01-06 14:41:07 -0800753 }
754
Jiyong Park379de2f2018-12-19 02:47:14 +0900755 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800756 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700757 }
758 // FIXME: enable RTTI if diag + (cfi or vptr)
759
Andreas Gampe97071162017-05-08 13:15:23 -0700760 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800761 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700762 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
763 }
764
Ivan Lozano7929bba2018-12-12 09:36:31 -0800765 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800766 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800767 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
768 }
769
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700770 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
771 if blocklist.Valid() {
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700772 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-ignorelist="+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700773 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
774 }
775
Colin Cross16b23492016-01-06 14:41:07 -0800776 return flags
777}
778
Colin Crossd80cbca2020-02-24 12:01:37 -0800779func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900780 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
781 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800782 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900783 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800784 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900785 }
786 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800787 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900788 }
789 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800790 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900791 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800792 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700793}
794
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700795func (sanitize *sanitize) inSanitizerDir() bool {
796 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700797}
798
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500799// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
800func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000801 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500802 case Asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000803 return sanitize.Properties.Sanitize.Address
Tri Vo6eafc362021-04-01 11:29:09 -0700804 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700805 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000806 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000807 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000808 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000809 return sanitize.Properties.Sanitize.Integer_overflow
810 case cfi:
811 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800812 case scs:
813 return sanitize.Properties.Sanitize.Scs
Ivan Lozano62cd0382021-11-01 10:27:54 -0400814 case Memtag_heap:
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700815 return sanitize.Properties.Sanitize.Memtag_heap
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500816 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700817 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000818 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500819 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000820 }
821}
822
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500823// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800824func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500825 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700826 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800827 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800828 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700829 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano62cd0382021-11-01 10:27:54 -0400830 !sanitize.isSanitizerEnabled(Memtag_heap) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500831 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800832}
833
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500834// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700835func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500836 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700837 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700838 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500839 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700840}
841
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500842func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400843 bPtr := proptools.BoolPtr(b)
844 if !b {
845 bPtr = nil
846 }
Colin Cross16b23492016-01-06 14:41:07 -0800847 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500848 case Asan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400849 sanitize.Properties.Sanitize.Address = bPtr
Tri Vo6eafc362021-04-01 11:29:09 -0700850 case Hwasan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400851 sanitize.Properties.Sanitize.Hwaddress = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800852 case tsan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400853 sanitize.Properties.Sanitize.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700854 case intOverflow:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400855 sanitize.Properties.Sanitize.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000856 case cfi:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400857 sanitize.Properties.Sanitize.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800858 case scs:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400859 sanitize.Properties.Sanitize.Scs = bPtr
Ivan Lozano62cd0382021-11-01 10:27:54 -0400860 case Memtag_heap:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400861 sanitize.Properties.Sanitize.Memtag_heap = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500862 case Fuzzer:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400863 sanitize.Properties.Sanitize.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800864 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500865 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800866 }
867 if b {
868 sanitize.Properties.SanitizerEnabled = true
869 }
870}
871
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000872// Check if the sanitizer is explicitly disabled (as opposed to nil by
873// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500874func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000875 if sanitize == nil {
876 return false
877 }
878
879 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
880 return sanitizerVal != nil && *sanitizerVal == false
881}
882
883// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
884// because enabling a sanitizer either directly (via the blueprint) or
885// indirectly (via a mutator) sets the bool ptr to true, and you can't
886// distinguish between the cases. It isn't needed though - both cases can be
887// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500888func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000889 if sanitize == nil {
890 return false
891 }
892
893 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
894 return sanitizerVal != nil && *sanitizerVal == true
895}
896
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500897// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
898func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700899 switch t := tag.(type) {
900 case dependencyTag:
901 return t == reuseObjTag || t == objDepTag
902 case libraryDependencyTag:
903 return true
904 default:
905 return false
906 }
Colin Cross6b753602018-06-21 13:03:07 -0700907}
908
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500909func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
910 return IsSanitizableDependencyTag
911}
912
Inseob Kimc42f2f22020-07-29 20:32:10 +0900913// Determines if the current module is a static library going to be captured
914// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
915// except for ones which explicitly disable cfi.
916func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
Kiyoung Kim48f37782021-07-07 12:42:39 +0900917 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900918 return false
919 }
920
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500921 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900922
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500923 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900924 return false
925 }
926
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500927 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900928 return false
929 }
930
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500931 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900932 return false
933 }
934
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500935 if !c.SanitizerSupported(cfi) {
936 return false
937 }
938
939 return c.SanitizePropDefined() &&
940 !c.SanitizeNever() &&
941 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900942}
943
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700944// Propagate sanitizer requirements down from binaries
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500945func sanitizerDepsMutator(t SanitizerType) func(android.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700946 return func(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500947 if c, ok := mctx.Module().(PlatformSanitizeable); ok {
948 enabled := c.IsSanitizerEnabled(t)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900949 if t == cfi && needsCfiForVendorSnapshot(mctx) {
950 // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly
951 // determine defaultVariation in sanitizerMutator below.
952 // Instead, just mark SanitizeDep to forcefully create cfi variant.
953 enabled = true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500954 c.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900955 }
956 if enabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500957 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Inseob Kimc42f2f22020-07-29 20:32:10 +0900958 mctx.WalkDeps(func(child, parent android.Module) bool {
959 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
960 return false
961 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500962 if d, ok := child.(PlatformSanitizeable); ok && d.SanitizePropDefined() &&
963 !d.SanitizeNever() &&
964 !d.IsSanitizerExplicitlyDisabled(t) {
Colin Crossaf98f582021-05-12 17:27:32 -0700965 if t == cfi || t == Hwasan || t == scs || t == Asan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500966 if d.StaticallyLinked() && d.SanitizerSupported(t) {
967 // Rust does not support some of these sanitizers, so we need to check if it's
968 // supported before setting this true.
969 d.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900970 }
971 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500972 d.SetSanitizeDep(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700973 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000974 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900975 return true
976 })
977 }
Jiyong Parkf97782b2019-02-13 20:28:58 +0900978 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
979 // If an APEX module includes a lib which is enabled for a sanitizer T, then
980 // the APEX module is also enabled for the same sanitizer type.
981 mctx.VisitDirectDeps(func(child android.Module) {
982 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
983 sanitizeable.EnableSanitizer(t.name())
984 }
985 })
Colin Cross16b23492016-01-06 14:41:07 -0800986 }
987 }
988}
989
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500990func (c *Module) SanitizeNever() bool {
991 return Bool(c.sanitize.Properties.Sanitize.Never)
992}
993
994func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
995 return c.sanitize.isSanitizerExplicitlyDisabled(t)
996}
997
Ivan Lozano30c5db22018-02-21 15:49:20 -0800998// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700999func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001000 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -07001001 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001002 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -07001003 mctx.WalkDeps(func(child, parent android.Module) bool {
1004 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
1005 return false
1006 }
Ivan Lozano30c5db22018-02-21 15:49:20 -08001007
Inseob Kimeec88e12020-01-22 11:11:29 +09001008 d, ok := child.(*Module)
1009 if !ok || !d.static() {
1010 return false
1011 }
1012 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001013 if enableMinimalRuntime(d.sanitize) {
1014 // If a static dependency is built with the minimal runtime,
1015 // make sure we include the ubsan minimal runtime.
1016 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001017 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001018 // If a static dependency runs with full ubsan diagnostics,
1019 // make sure we include the ubsan runtime.
1020 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001021 }
Colin Cross0b908332019-06-19 23:00:20 -07001022
1023 if c.sanitize.Properties.MinimalRuntimeDep &&
1024 c.sanitize.Properties.UbsanRuntimeDep {
1025 // both flags that this mutator might set are true, so don't bother recursing
1026 return false
1027 }
1028
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001029 if c.Os() == android.Linux {
1030 c.sanitize.Properties.BuiltinsDep = true
1031 }
1032
Colin Cross0b908332019-06-19 23:00:20 -07001033 return true
Colin Cross6b753602018-06-21 13:03:07 -07001034 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001035
Jose Galmesf7294582020-11-13 12:07:36 -08001036 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001037 if Bool(p.properties.Sanitize_minimal_dep) {
1038 c.sanitize.Properties.MinimalRuntimeDep = true
1039 }
1040 if Bool(p.properties.Sanitize_ubsan_dep) {
1041 c.sanitize.Properties.UbsanRuntimeDep = true
1042 }
1043 }
1044
1045 return false
Colin Cross6b753602018-06-21 13:03:07 -07001046 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001047 }
1048}
1049
Jiyong Park379de2f2018-12-19 02:47:14 +09001050// Add the dependency to the runtime library for each of the sanitizer variants
1051func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001052 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001053 if !c.Enabled() {
1054 return
1055 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001056 var sanitizers []string
1057 var diagSanitizers []string
1058
1059 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
1060 sanitizers = append(sanitizers, "undefined")
1061 } else {
1062 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
1063 sanitizers = append(sanitizers,
1064 "bool",
1065 "integer-divide-by-zero",
1066 "return",
1067 "returns-nonnull-attribute",
1068 "shift-exponent",
1069 "unreachable",
1070 "vla-bound",
1071 // TODO(danalbert): The following checks currently have compiler performance issues.
1072 //"alignment",
1073 //"bounds",
1074 //"enum",
1075 //"float-cast-overflow",
1076 //"float-divide-by-zero",
1077 //"nonnull-attribute",
1078 //"null",
1079 //"shift-base",
1080 //"signed-integer-overflow",
1081 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1082 // https://llvm.org/PR19302
1083 // http://reviews.llvm.org/D6974
1084 // "object-size",
1085 )
1086 }
1087 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
1088 }
1089
1090 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
1091 diagSanitizers = append(diagSanitizers, "undefined")
1092 }
1093
1094 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
1095
1096 if Bool(c.sanitize.Properties.Sanitize.Address) {
1097 sanitizers = append(sanitizers, "address")
1098 diagSanitizers = append(diagSanitizers, "address")
1099 }
1100
1101 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1102 sanitizers = append(sanitizers, "hwaddress")
1103 }
1104
1105 if Bool(c.sanitize.Properties.Sanitize.Thread) {
1106 sanitizers = append(sanitizers, "thread")
1107 }
1108
1109 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
1110 sanitizers = append(sanitizers, "safe-stack")
1111 }
1112
1113 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
1114 sanitizers = append(sanitizers, "cfi")
1115
1116 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
1117 diagSanitizers = append(diagSanitizers, "cfi")
1118 }
1119 }
1120
1121 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
1122 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1123 sanitizers = append(sanitizers, "signed-integer-overflow")
1124 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
1125 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1126 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1127 }
1128 }
1129
1130 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1131 sanitizers = append(sanitizers, "scudo")
1132 }
1133
1134 if Bool(c.sanitize.Properties.Sanitize.Scs) {
1135 sanitizers = append(sanitizers, "shadow-call-stack")
1136 }
1137
Ivan Lozanod7586b62021-04-01 09:49:36 -04001138 if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.Binary() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001139 noteDep := "note_memtag_heap_async"
1140 if Bool(c.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
1141 noteDep = "note_memtag_heap_sync"
1142 }
Inseob Kim253f5212021-04-08 17:10:31 +09001143 // If we're using snapshots, redirect to snapshot whenever possible
1144 // TODO(b/178470649): clean manual snapshot redirections
1145 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1146 if lib, ok := snapshot.StaticLibs[noteDep]; ok {
1147 noteDep = lib
1148 }
Ivan Lozano62cd0382021-11-01 10:27:54 -04001149 depTag := StaticDepTag(true)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001150 variations := append(mctx.Target().Variations(),
1151 blueprint.Variation{Mutator: "link", Variation: "static"})
1152 if c.Device() {
1153 variations = append(variations, c.ImageVariation())
1154 }
1155 mctx.AddFarVariationDependencies(variations, depTag, noteDep)
1156 }
1157
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001158 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
1159 sanitizers = append(sanitizers, "fuzzer-no-link")
1160 }
1161
Jiyong Park379de2f2018-12-19 02:47:14 +09001162 // Save the list of sanitizers. These will be used again when generating
1163 // the build rules (for Cflags, etc.)
1164 c.sanitize.Properties.Sanitizers = sanitizers
1165 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1166
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001167 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1168 if c.Host() {
1169 diagSanitizers = sanitizers
1170 }
1171
Jiyong Park379de2f2018-12-19 02:47:14 +09001172 // Determine the runtime library required
1173 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001174 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001175 toolchain := c.toolchain(mctx)
1176 if Bool(c.sanitize.Properties.Sanitize.Address) {
1177 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1178 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1179 if c.staticBinary() {
1180 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001181 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001182 } else {
1183 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1184 }
1185 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
1186 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1187 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1188 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1189 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1190 } else {
1191 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1192 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001193 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001194 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
1195 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
1196 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001197 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
Colin Cross32f1de32021-03-29 13:41:37 -07001198 if c.staticBinary() {
1199 runtimeLibrary += ".static"
1200 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001201 }
1202
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001203 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
1204 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001205
1206 // Adding dependency to the runtime library. We are using *FarVariation*
1207 // because the runtime libraries themselves are not mutated by sanitizer
1208 // mutators and thus don't have sanitizer variants whereas this module
1209 // has been already mutated.
1210 //
1211 // Note that by adding dependency with {static|shared}DepTag, the lib is
1212 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
1213 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001214 deps := append(extraStaticDeps, runtimeLibrary)
Colin Crosse0edaf92021-01-11 17:31:17 -08001215 // If we're using snapshots, redirect to snapshot whenever possible
1216 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1217 for idx, dep := range deps {
1218 if lib, ok := snapshot.StaticLibs[dep]; ok {
1219 deps[idx] = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001220 }
1221 }
1222
Jiyong Park379de2f2018-12-19 02:47:14 +09001223 // static executable gets static runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -07001224 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Colin Cross42507332020-08-21 16:15:23 -07001225 variations := append(mctx.Target().Variations(),
1226 blueprint.Variation{Mutator: "link", Variation: "static"})
1227 if c.Device() {
1228 variations = append(variations, c.ImageVariation())
1229 }
1230 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001231 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001232 // If we're using snapshots, redirect to snapshot whenever possible
1233 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1234 if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
1235 runtimeLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001236 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001237
Cindy Zhou18417cb2020-12-10 07:12:38 -08001238 // Skip apex dependency check for sharedLibraryDependency
1239 // when sanitizer diags are enabled. Skipping the check will allow
1240 // building with diag libraries without having to list the
1241 // dependency in Apex's allowed_deps file.
1242 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001243 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001244 depTag := libraryDependencyTag{
1245 Kind: sharedLibraryDependency,
1246 Order: earlyLibraryDependency,
1247
1248 skipApexAllowedDependenciesCheck: diagEnabled,
1249 }
Colin Cross42507332020-08-21 16:15:23 -07001250 variations := append(mctx.Target().Variations(),
1251 blueprint.Variation{Mutator: "link", Variation: "shared"})
1252 if c.Device() {
1253 variations = append(variations, c.ImageVariation())
1254 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001255 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001256 }
1257 // static lib does not have dependency to the runtime library. The
1258 // dependency will be added to the executables or shared libs using
1259 // the static lib.
1260 }
1261 }
1262}
1263
1264type Sanitizeable interface {
1265 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +09001266 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001267 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001268 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001269}
1270
Ivan Lozanod7586b62021-04-01 09:49:36 -04001271func (c *Module) MinimalRuntimeDep() bool {
1272 return c.sanitize.Properties.MinimalRuntimeDep
1273}
1274
1275func (c *Module) UbsanRuntimeDep() bool {
1276 return c.sanitize.Properties.UbsanRuntimeDep
1277}
1278
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001279func (c *Module) SanitizePropDefined() bool {
1280 return c.sanitize != nil
1281}
1282
1283func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1284 return c.sanitize.isSanitizerEnabled(t)
1285}
1286
1287func (c *Module) SanitizeDep() bool {
1288 return c.sanitize.Properties.SanitizeDep
1289}
1290
1291func (c *Module) StaticallyLinked() bool {
1292 return c.static()
1293}
1294
1295func (c *Module) SetInSanitizerDir() {
1296 if c.sanitize != nil {
1297 c.sanitize.Properties.InSanitizerDir = true
1298 }
1299}
1300
1301func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1302 if c.sanitize != nil {
1303 c.sanitize.SetSanitizer(t, b)
1304 }
1305}
1306
1307func (c *Module) SetSanitizeDep(b bool) {
1308 if c.sanitize != nil {
1309 c.sanitize.Properties.SanitizeDep = b
1310 }
1311}
1312
1313var _ PlatformSanitizeable = (*Module)(nil)
1314
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001315// Create sanitized variants for modules that need them
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001316func sanitizerMutator(t SanitizerType) func(android.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -07001317 return func(mctx android.BottomUpMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001318 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
Ivan Lozano5482d6a2021-11-01 10:13:25 -04001319
1320 // Make sure we're not setting CFI to any value if it's not supported.
1321 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1322
Liz Kammer187d5442021-06-25 14:50:12 -04001323 if c.Binary() && c.IsSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001324 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001325 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1326 } else if c.IsSanitizerEnabled(t) || c.SanitizeDep() {
1327 isSanitizerEnabled := c.IsSanitizerEnabled(t)
Colin Crossaf98f582021-05-12 17:27:32 -07001328 if c.StaticallyLinked() || c.Header() || t == Fuzzer {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001329 // Static and header libs are split into non-sanitized and sanitized variants.
1330 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1331 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1332 // that isn't sanitized for asan/fuzzer.
1333 //
1334 // Note for defaultVariation: since we don't split for shared libs but for static/header
1335 // libs, it is possible for the sanitized variant of a static/header lib to depend
1336 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1337 // error when the module is split. defaultVariation is the name of the variation that
1338 // will be used when such a dangling dependency occurs during the split of the current
1339 // module. By setting it to the name of the sanitized variation, the dangling dependency
1340 // is redirected to the sanitized variant of the dependent module.
1341 defaultVariation := t.variationName()
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001342 // Not all PlatformSanitizeable modules support the CFI sanitizer
Jiyong Park1d1119f2019-07-29 21:27:18 +09001343 mctx.SetDefaultDependencyVariation(&defaultVariation)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001344
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001345 modules := mctx.CreateVariations("", t.variationName())
1346 modules[0].(PlatformSanitizeable).SetSanitizer(t, false)
1347 modules[1].(PlatformSanitizeable).SetSanitizer(t, true)
1348 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
1349 modules[1].(PlatformSanitizeable).SetSanitizeDep(false)
1350
1351 if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
Ivan Lozano4774a812020-03-10 16:23:57 -04001352 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1353 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001354 modules[1].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001355 }
1356
Jiyong Park1d1119f2019-07-29 21:27:18 +09001357 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1358 // to Make, because the sanitized version has a different suffix in name.
1359 // For other types of sanitizers, suppress the variation that is disabled.
Tri Vo6eafc362021-04-01 11:29:09 -07001360 if t != cfi && t != scs && t != Hwasan {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001361 if isSanitizerEnabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001362 modules[0].(PlatformSanitizeable).SetPreventInstall()
1363 modules[0].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001364 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001365 modules[1].(PlatformSanitizeable).SetPreventInstall()
1366 modules[1].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001367 }
1368 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001369
Jiyong Park1d1119f2019-07-29 21:27:18 +09001370 // Export the static lib name to make
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001371 if c.StaticallyLinked() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001372 if t == cfi {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001373 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
Tri Vo6eafc362021-04-01 11:29:09 -07001374 } else if t == Hwasan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001375 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001376 }
1377 }
1378 } else {
1379 // Shared libs are not split. Only the sanitized variant is created.
1380 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001381 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1382 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
Vishwath Mohane7128792017-11-17 11:08:10 -08001383
Jiyong Park1d1119f2019-07-29 21:27:18 +09001384 // locate the asan libraries under /data/asan
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001385 if mctx.Device() && t == Asan && isSanitizerEnabled {
1386 modules[0].(PlatformSanitizeable).SetInSanitizerDir()
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001387 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001388
Ivan Lozano5482d6a2021-11-01 10:13:25 -04001389 if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
Ivan Lozano4774a812020-03-10 16:23:57 -04001390 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1391 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001392 modules[0].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001393 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001394 }
Colin Cross16b23492016-01-06 14:41:07 -08001395 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001396 c.SetSanitizeDep(false)
Jiyong Park82226632019-02-01 10:50:50 +09001397 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001398 // APEX modules fall here
Jooyung Han8ce8db92020-05-15 19:05:05 +09001399 sanitizeable.AddSanitizerDependencies(mctx, t.name())
Jiyong Park82226632019-02-01 10:50:50 +09001400 mctx.CreateVariations(t.variationName())
Inseob Kimc42f2f22020-07-29 20:32:10 +09001401 } else if c, ok := mctx.Module().(*Module); ok {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001402 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1403
Inseob Kimc42f2f22020-07-29 20:32:10 +09001404 // Check if it's a snapshot module supporting sanitizer
1405 if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) {
1406 // Set default variation as above.
1407 defaultVariation := t.variationName()
1408 mctx.SetDefaultDependencyVariation(&defaultVariation)
1409 modules := mctx.CreateVariations("", t.variationName())
1410 modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false)
1411 modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true)
1412
1413 // Export the static lib name to make
1414 if c.static() && c.ExportedToMake() {
1415 if t == cfi {
1416 // use BaseModuleName which is the name for Make.
1417 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1418 }
1419 }
1420 }
Colin Cross16b23492016-01-06 14:41:07 -08001421 }
1422 }
1423}
Vishwath Mohane7128792017-11-17 11:08:10 -08001424
Inseob Kim74d25562020-08-04 00:41:38 +09001425type sanitizerStaticLibsMap struct {
1426 // libsMap contains one list of modules per each image and each arch.
1427 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001428 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001429 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001430 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001431}
1432
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001433func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001434 return &sanitizerStaticLibsMap{
1435 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001436 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001437 }
1438}
1439
1440// Add the current module to sanitizer static libs maps
1441// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001442func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1443 image := GetImageVariantType(c)
1444 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001445
1446 s.libsMapLock.Lock()
1447 defer s.libsMapLock.Unlock()
1448
1449 if _, ok := s.libsMap[image]; !ok {
1450 s.libsMap[image] = make(map[string][]string)
1451 }
1452
1453 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1454}
1455
1456// Exports makefile variables in the following format:
1457// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1458// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1459// These are to be used by use_soong_sanitized_static_libraries.
1460// See build/make/core/binary.mk for more details.
1461func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1462 for _, image := range android.SortedStringKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001463 archMap := s.libsMap[ImageVariantType(image)]
Inseob Kim74d25562020-08-04 00:41:38 +09001464 for _, arch := range android.SortedStringKeys(archMap) {
1465 libs := archMap[arch]
1466 sort.Strings(libs)
1467
1468 key := fmt.Sprintf(
1469 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1470 s.sanitizerType.variationName(),
1471 image, // already upper
1472 arch)
1473
1474 ctx.Strict(key, strings.Join(libs, " "))
1475 }
1476 }
1477}
1478
Colin Cross571cccf2019-02-04 11:22:08 -08001479var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1480
Inseob Kim74d25562020-08-04 00:41:38 +09001481func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001482 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001483 return newSanitizerStaticLibsMap(cfi)
1484 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001485}
1486
Colin Cross571cccf2019-02-04 11:22:08 -08001487var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1488
Inseob Kim74d25562020-08-04 00:41:38 +09001489func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001490 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001491 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001492 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001493}
1494
Ivan Lozano30c5db22018-02-21 15:49:20 -08001495func enableMinimalRuntime(sanitize *sanitize) bool {
1496 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001497 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001498 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001499
Ivan Lozano30c5db22018-02-21 15:49:20 -08001500 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001501 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1502 Bool(sanitize.Properties.Sanitize.Undefined) ||
1503 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1504
Ivan Lozano30c5db22018-02-21 15:49:20 -08001505 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1506 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001507 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001508 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001509
Ivan Lozano30c5db22018-02-21 15:49:20 -08001510 return true
1511 }
1512 return false
1513}
1514
Ivan Lozanod7586b62021-04-01 09:49:36 -04001515func (m *Module) UbsanRuntimeNeeded() bool {
1516 return enableUbsanRuntime(m.sanitize)
1517}
1518
1519func (m *Module) MinimalRuntimeNeeded() bool {
1520 return enableMinimalRuntime(m.sanitize)
1521}
1522
Inseob Kim8471cda2019-11-15 09:59:12 +09001523func enableUbsanRuntime(sanitize *sanitize) bool {
1524 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001525 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001526 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1527}
1528
Vishwath Mohane7128792017-11-17 11:08:10 -08001529func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001530 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001531}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001532
1533func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001534 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001535}