blob: 0199897d43d8d14a11c1a623deb3859b117ac2a0 [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"
Colin Cross16b23492016-01-06 14:41:07 -080028)
29
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070030var (
31 // Any C flags added by sanitizer which libTooling tools may not
32 // understand also need to be added to ClangLibToolingUnknownCflags in
33 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080034
Yi Kong20233a42019-08-21 01:38:40 -070035 asanCflags = []string{
36 "-fno-omit-frame-pointer",
37 "-fno-experimental-new-pass-manager",
38 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070039 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070040
Peter Collingbourne967511a2019-03-19 21:39:54 -070041 hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000042 "-fsanitize-hwaddress-abi=platform",
43 "-fno-experimental-new-pass-manager",
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 Nainareb8d4032020-07-27 11:22:35 -070059 "-fsanitize-blacklist=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 Nainareb8d4032020-07-27 11:22:35 -070067 intOverflowCflags = []string{"-fsanitize-blacklist=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
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000082 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080083 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -050084 Fuzzer
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -070085 memtag_heap
Colin Cross16b23492016-01-06 14:41:07 -080086)
87
Jiyong Park82226632019-02-01 10:50:50 +090088// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -050089func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080090 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -050091 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -080092 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -070093 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070094 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080095 case tsan:
96 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070097 case intOverflow:
98 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000099 case cfi:
100 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800101 case scs:
102 return "scs"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700103 case memtag_heap:
104 return "memtag_heap"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500105 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700106 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800107 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500108 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800109 }
110}
111
Jiyong Park82226632019-02-01 10:50:50 +0900112// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500113func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900114 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500115 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900116 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700117 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900118 return "hwaddress"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700119 case memtag_heap:
120 return "memtag_heap"
Jiyong Park82226632019-02-01 10:50:50 +0900121 case tsan:
122 return "thread"
123 case intOverflow:
124 return "integer_overflow"
125 case cfi:
126 return "cfi"
127 case scs:
128 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500129 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700130 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900131 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500132 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900133 }
134}
135
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500136func (*Module) SanitizerSupported(t SanitizerType) bool {
137 switch t {
138 case Asan:
139 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700140 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500141 return true
142 case tsan:
143 return true
144 case intOverflow:
145 return true
146 case cfi:
147 return true
148 case scs:
149 return true
150 case Fuzzer:
151 return true
152 default:
153 return false
154 }
155}
156
157// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
158func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700159 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900160}
161
Martin Stjernholmb0249572020-09-15 02:32:35 +0100162type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400163 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100164 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800165
Liz Kammer75b9b402021-06-25 15:19:27 -0400166 // ASan (Address sanitizer), incompatible with static binaries.
167 // Always runs in a diagnostic mode.
168 // Use of address sanitizer disables cfi sanitizer.
169 // Hwaddress sanitizer takes precedence over this sanitizer.
170 Address *bool `android:"arch_variant"`
171 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
172 // Always runs in a diagnostic mode.
173 // Use of thread sanitizer disables cfi and scudo sanitizers.
174 // Hwaddress sanitizer takes precedence over this sanitizer.
175 Thread *bool `android:"arch_variant"`
176 // HWASan (Hardware Address sanitizer).
177 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100178 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800179
Liz Kammer75b9b402021-06-25 15:19:27 -0400180 // Undefined behavior sanitizer
181 All_undefined *bool `android:"arch_variant"`
182 // Subset of undefined behavior sanitizer
183 Undefined *bool `android:"arch_variant"`
184 // List of specific undefined behavior sanitizers to enable
185 Misc_undefined []string `android:"arch_variant"`
186 // Fuzzer, incompatible with static binaries.
187 Fuzzer *bool `android:"arch_variant"`
188 // safe-stack sanitizer, incompatible with 32-bit architectures.
189 Safestack *bool `android:"arch_variant"`
190 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
191 Cfi *bool `android:"arch_variant"`
192 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
193 Integer_overflow *bool `android:"arch_variant"`
194 // scudo sanitizer, incompatible with asan, hwasan, tsan
195 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
196 // deprecated
197 Scudo *bool `android:"arch_variant"`
198 // shadow-call-stack sanitizer, only available on arm64
199 Scs *bool `android:"arch_variant"`
200 // Memory-tagging, only available on arm64
201 // if diag.memtag unset or false, enables async memory tagging
202 Memtag_heap *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100203
204 // A modifier for ASAN and HWASAN for write only instrumentation
205 Writeonly *bool `android:"arch_variant"`
206
207 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
208 // Replaces abort() on error with a human-readable error message.
209 // Address and Thread sanitizers always run in diagnostic mode.
210 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400211 // Undefined behavior sanitizer, diagnostic mode
212 Undefined *bool `android:"arch_variant"`
213 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
214 Cfi *bool `android:"arch_variant"`
215 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
216 Integer_overflow *bool `android:"arch_variant"`
217 // Memory-tagging, only available on arm64
218 // requires sanitizer.memtag: true
219 // if set, enables sync memory tagging
220 Memtag_heap *bool `android:"arch_variant"`
221 // List of specific undefined behavior sanitizers to enable in diagnostic mode
222 Misc_undefined []string `android:"arch_variant"`
223 // List of sanitizers to pass to -fno-sanitize-recover
224 // results in only the first detected error for these sanitizers being reported and program then
225 // exits with a non-zero exit code.
226 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800227 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800228
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800229 // Sanitizers to run with flag configuration specified
230 Config struct {
231 // Enables CFI support flags for assembly-heavy libraries
232 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800233 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800234
Liz Kammer75b9b402021-06-25 15:19:27 -0400235 // List of sanitizers to pass to -fsanitize-recover
236 // allows execution to continue for these sanitizers to detect multiple errors rather than only
237 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100238 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000239
Martin Stjernholmb0249572020-09-15 02:32:35 +0100240 // value to pass to -fsanitize-blacklist
241 Blocklist *string
242}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700243
Martin Stjernholmb0249572020-09-15 02:32:35 +0100244type SanitizeProperties struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400245 // Sanitizers are not supported for Fuchsia.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100246 Sanitize SanitizeUserProps `android:"arch_variant"`
247 SanitizerEnabled bool `blueprint:"mutated"`
248 SanitizeDep bool `blueprint:"mutated"`
249 MinimalRuntimeDep bool `blueprint:"mutated"`
250 BuiltinsDep bool `blueprint:"mutated"`
251 UbsanRuntimeDep bool `blueprint:"mutated"`
252 InSanitizerDir bool `blueprint:"mutated"`
253 Sanitizers []string `blueprint:"mutated"`
254 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800255}
256
257type sanitize struct {
258 Properties SanitizeProperties
259}
260
Cindy Zhou18417cb2020-12-10 07:12:38 -0800261// Mark this tag with a check to see if apex dependency check should be skipped
262func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
263 return t.skipApexAllowedDependenciesCheck
264}
265
266var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
267
Vishwath Mohane7128792017-11-17 11:08:10 -0800268func init() {
269 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700270 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800271}
272
Colin Cross16b23492016-01-06 14:41:07 -0800273func (sanitize *sanitize) props() []interface{} {
274 return []interface{}{&sanitize.Properties}
275}
276
277func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700278 s := &sanitize.Properties.Sanitize
279
Colin Cross16b23492016-01-06 14:41:07 -0800280 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700281 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800282 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800283 }
284
Doug Hornc32c6b02019-01-17 14:44:05 -0800285 // Sanitizers do not work on Fuchsia yet.
286 if ctx.Fuchsia() {
287 s.Never = BoolPtr(true)
288 }
289
Colin Cross16b23492016-01-06 14:41:07 -0800290 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800291 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800292 return
293 }
294
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800295 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap}).
296 if ctx.testBinary() && s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400297 s.Memtag_heap = proptools.BoolPtr(true)
298 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800299 }
300
Colin Cross16b23492016-01-06 14:41:07 -0800301 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700302 var globalSanitizersDiag []string
303
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700304 if ctx.Host() {
305 if !ctx.Windows() {
306 globalSanitizers = ctx.Config().SanitizeHost()
307 }
308 } else {
309 arches := ctx.Config().SanitizeDeviceArch()
310 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
311 globalSanitizers = ctx.Config().SanitizeDevice()
312 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800313 }
314 }
315
Colin Cross16b23492016-01-06 14:41:07 -0800316 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000317 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700318 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400319 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000320 }
Colin Cross16b23492016-01-06 14:41:07 -0800321
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700322 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400323 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000324 }
325
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700326 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400327 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000328 }
329
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700330 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400331 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000332 }
333
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700334 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400335 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700336 }
337
338 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400339 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000340 }
341
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700342 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800343 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400344 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700345 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700346 }
347
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700348 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700349 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700350 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400351 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700352 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700353 }
354
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700355 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400356 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700357 }
358
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700359 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400360 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700361 }
362
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000363 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
364 // Hwaddress and Address are set before, so we can check them here
365 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
366 if s.Address == nil && s.Hwaddress == nil {
367 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
368 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400369 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000370 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700371 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800372 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400373 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800374 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700375 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000376
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000377 if len(globalSanitizers) > 0 {
378 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
379 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700380
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700381 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700382 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700383 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400384 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700385 }
386
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700387 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
388 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400389 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700390 }
391
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800392 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
393 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400394 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800395 }
396
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700397 if len(globalSanitizersDiag) > 0 {
398 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
399 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700400 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700401
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800402 // Enable Memtag for all components in the include paths (for Aarch64 only)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800403 if ctx.Arch().ArchType == android.Arm64 {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800404 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800405 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400406 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800407 }
408 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400409 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800410 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800411 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800412 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400413 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800414 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800415 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700416 }
417
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700418 // Enable CFI for all components in the include paths (for Aarch64 only)
419 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400420 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000421 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400422 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700423 }
424 }
425
Elliott Hughesda3a0712020-03-06 16:55:28 -0800426 // Is CFI actually enabled?
427 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400428 s.Cfi = nil
429 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800430 }
431
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700432 // HWASan requires AArch64 hardware feature (top-byte-ignore).
433 if ctx.Arch().ArchType != android.Arm64 {
434 s.Hwaddress = nil
435 }
436
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800437 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800438 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800439 s.Scs = nil
440 }
441
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700442 // memtag_heap is only implemented on AArch64.
443 if ctx.Arch().ArchType != android.Arm64 {
444 s.Memtag_heap = nil
445 }
446
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700447 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700448 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400449 s.Cfi = nil
450 s.Diag.Cfi = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700451 }
452
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500453 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
454 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400455 s.Cfi = nil
456 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700457 s.Misc_undefined = nil
458 s.Undefined = nil
459 s.All_undefined = nil
460 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800461 }
462
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700463 // Also disable CFI for VNDK variants of components
464 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900465 if ctx.static() {
466 // Cfi variant for static vndk should be captured as vendor snapshot,
467 // so don't strictly disable Cfi.
468 s.Cfi = nil
469 s.Diag.Cfi = nil
470 } else {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400471 s.Cfi = nil
472 s.Diag.Cfi = nil
Inseob Kimc42f2f22020-07-29 20:32:10 +0900473 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900474 }
475
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700476 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700477 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
478 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700479 s.Hwaddress = nil
480 }
481
Colin Cross3c344ef2016-07-18 15:44:56 -0700482 if ctx.staticBinary() {
483 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700484 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700485 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800486 }
487
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700488 if Bool(s.All_undefined) {
489 s.Undefined = nil
490 }
491
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700492 if !ctx.toolchain().Is64Bit() {
493 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700494 s.Thread = nil
495 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800496 // TODO(ccross): error for compile_multilib = "32"?
497 }
498
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800499 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 -0700500 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 -0700501 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700502 sanitize.Properties.SanitizerEnabled = true
503 }
504
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800505 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
506 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700507 s.Scudo = nil
508 }
509
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700510 if Bool(s.Hwaddress) {
511 s.Address = nil
512 s.Thread = nil
Evgenii Stepanovb15a5642021-06-23 12:30:55 -0700513 // Disable ubsan diagnosic as a workaround for a compiler bug.
514 // TODO(b/191808836): re-enable.
515 s.Diag.Undefined = nil
516 s.Diag.Integer_overflow = nil
517 s.Diag.Misc_undefined = nil
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700518 }
519
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700520 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
521 // mutually incompatible.
522 if Bool(s.Fuzzer) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400523 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800524 }
525}
526
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800527func toDisableImplicitIntegerChange(flags []string) bool {
528 // Returns true if any flag is fsanitize*integer, and there is
529 // no explicit flag about sanitize=implicit-integer-sign-change.
530 for _, f := range flags {
531 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
532 return false
533 }
534 }
535 for _, f := range flags {
536 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
537 return true
538 }
539 }
540 return false
541}
542
Yabin Cuidb7dda82020-11-30 15:47:45 -0800543func toDisableUnsignedShiftBaseChange(flags []string) bool {
544 // Returns true if any flag is fsanitize*integer, and there is
545 // no explicit flag about sanitize=unsigned-shift-base.
546 for _, f := range flags {
547 if strings.Contains(f, "sanitize=unsigned-shift-base") {
548 return false
549 }
550 }
551 for _, f := range flags {
552 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
553 return true
554 }
555 }
556 return false
557}
558
Colin Cross16b23492016-01-06 14:41:07 -0800559func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700560 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
561 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500562 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
563 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800564
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500565 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800566 flags.Local.LdFlags = append(flags.Local.LdFlags,
567 minimalRuntimePath,
568 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800569 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500570
571 if sanitize.Properties.BuiltinsDep {
572 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
573 }
574
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700575 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800576 return flags
577 }
578
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700579 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700580 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800581 // Frame pointer based unwinder in ASan requires ARM frame setup.
582 // TODO: put in flags?
583 flags.RequiredInstructionSet = "arm"
584 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800585 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
586 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800587
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000588 if Bool(sanitize.Properties.Sanitize.Writeonly) {
589 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
590 }
591
Colin Cross16b23492016-01-06 14:41:07 -0800592 if ctx.Host() {
593 // -nodefaultlibs (provided with libc++) prevents the driver from linking
594 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800595 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800596 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800597 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900598 if ctx.bootstrap() {
599 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
600 } else {
601 flags.DynamicLinker = "/system/bin/linker_asan"
602 }
Colin Cross16b23492016-01-06 14:41:07 -0800603 if flags.Toolchain.Is64Bit() {
604 flags.DynamicLinker += "64"
605 }
606 }
Colin Cross16b23492016-01-06 14:41:07 -0800607 }
608
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700609 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800610 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000611 if Bool(sanitize.Properties.Sanitize.Writeonly) {
612 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
613 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700614 }
615
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700616 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800617 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700618
619 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800620 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
621 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
622 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
623 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700624
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700625 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
626 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
627 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800628 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
629 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700630
Mitch Phillips74384752019-06-17 10:33:52 -0700631 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800632 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700633
634 // Disable fortify for fuzzing builds. Generally, we'll be building with
635 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800636 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800637
638 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
639 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
640 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
641 // the DT_RUNPATH from the shared library above it, and not the executable,
642 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
643 // DT_RUNPATH here means that transient shared libraries can be found
644 // colocated with their parents.
645 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800646 }
647
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700648 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800649 if ctx.Arch().ArchType == android.Arm {
650 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
651 // to do this on a function basis, so force Thumb on the entire module.
652 flags.RequiredInstructionSet = "thumb"
653 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000654
Colin Cross4af21ed2019-11-04 09:37:55 -0800655 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
656 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800657 if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
658 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
659 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000660 // Only append the default visibility flag if -fvisibility has not already been set
661 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800662 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
663 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000664 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800665 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000666
667 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800668 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
669 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000670 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700671 }
672
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700673 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800674 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700675 }
676
Jiyong Park379de2f2018-12-19 02:47:14 +0900677 if len(sanitize.Properties.Sanitizers) > 0 {
678 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800679
Colin Cross4af21ed2019-11-04 09:37:55 -0800680 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
681 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800682 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800683 // Host sanitizers only link symbols in the final executable, so
684 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800685 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
686 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500687
688 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
689 if !ctx.toolchain().Bionic() {
690 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
691 }
692 }
693
694 if enableMinimalRuntime(sanitize) {
695 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
696 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
697 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
698 if !ctx.toolchain().Bionic() {
699 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800700 }
Colin Cross16b23492016-01-06 14:41:07 -0800701 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700702
703 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
704 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800705 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700706 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800707 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700708 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800709 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700710 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800711 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800712 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
713 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800714 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800715 // http://b/171275751, Android doesn't build with this sanitizer yet.
716 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
717 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
718 }
Colin Cross16b23492016-01-06 14:41:07 -0800719 }
720
Jiyong Park379de2f2018-12-19 02:47:14 +0900721 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800722 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700723 }
724 // FIXME: enable RTTI if diag + (cfi or vptr)
725
Andreas Gampe97071162017-05-08 13:15:23 -0700726 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800727 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700728 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
729 }
730
Ivan Lozano7929bba2018-12-12 09:36:31 -0800731 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800732 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800733 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
734 }
735
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700736 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
737 if blocklist.Valid() {
738 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blocklist.String())
739 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
740 }
741
Colin Cross16b23492016-01-06 14:41:07 -0800742 return flags
743}
744
Colin Crossd80cbca2020-02-24 12:01:37 -0800745func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900746 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
747 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800748 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900749 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800750 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900751 }
752 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800753 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900754 }
755 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800756 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900757 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800758 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700759}
760
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700761func (sanitize *sanitize) inSanitizerDir() bool {
762 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700763}
764
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500765// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
766func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000767 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500768 case Asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000769 return sanitize.Properties.Sanitize.Address
Tri Vo6eafc362021-04-01 11:29:09 -0700770 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700771 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000772 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000773 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000774 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000775 return sanitize.Properties.Sanitize.Integer_overflow
776 case cfi:
777 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800778 case scs:
779 return sanitize.Properties.Sanitize.Scs
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700780 case memtag_heap:
781 return sanitize.Properties.Sanitize.Memtag_heap
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500782 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700783 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000784 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500785 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000786 }
787}
788
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500789// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800790func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500791 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700792 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800793 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800794 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700795 !sanitize.isSanitizerEnabled(scs) &&
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700796 !sanitize.isSanitizerEnabled(memtag_heap) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500797 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800798}
799
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500800// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700801func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500802 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700803 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700804 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500805 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700806}
807
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500808func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400809 bPtr := proptools.BoolPtr(b)
810 if !b {
811 bPtr = nil
812 }
Colin Cross16b23492016-01-06 14:41:07 -0800813 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500814 case Asan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400815 sanitize.Properties.Sanitize.Address = bPtr
Tri Vo6eafc362021-04-01 11:29:09 -0700816 case Hwasan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400817 sanitize.Properties.Sanitize.Hwaddress = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800818 case tsan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400819 sanitize.Properties.Sanitize.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700820 case intOverflow:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400821 sanitize.Properties.Sanitize.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000822 case cfi:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400823 sanitize.Properties.Sanitize.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800824 case scs:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400825 sanitize.Properties.Sanitize.Scs = bPtr
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700826 case memtag_heap:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400827 sanitize.Properties.Sanitize.Memtag_heap = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500828 case Fuzzer:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400829 sanitize.Properties.Sanitize.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800830 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500831 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800832 }
833 if b {
834 sanitize.Properties.SanitizerEnabled = true
835 }
836}
837
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000838// Check if the sanitizer is explicitly disabled (as opposed to nil by
839// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500840func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000841 if sanitize == nil {
842 return false
843 }
844
845 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
846 return sanitizerVal != nil && *sanitizerVal == false
847}
848
849// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
850// because enabling a sanitizer either directly (via the blueprint) or
851// indirectly (via a mutator) sets the bool ptr to true, and you can't
852// distinguish between the cases. It isn't needed though - both cases can be
853// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500854func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000855 if sanitize == nil {
856 return false
857 }
858
859 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
860 return sanitizerVal != nil && *sanitizerVal == true
861}
862
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500863// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
864func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700865 switch t := tag.(type) {
866 case dependencyTag:
867 return t == reuseObjTag || t == objDepTag
868 case libraryDependencyTag:
869 return true
870 default:
871 return false
872 }
Colin Cross6b753602018-06-21 13:03:07 -0700873}
874
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500875func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
876 return IsSanitizableDependencyTag
877}
878
Inseob Kimc42f2f22020-07-29 20:32:10 +0900879// Determines if the current module is a static library going to be captured
880// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
881// except for ones which explicitly disable cfi.
882func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400883 if IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900884 return false
885 }
886
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500887 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900888
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500889 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900890 return false
891 }
892
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500893 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900894 return false
895 }
896
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500897 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900898 return false
899 }
900
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500901 if !c.SanitizerSupported(cfi) {
902 return false
903 }
904
905 return c.SanitizePropDefined() &&
906 !c.SanitizeNever() &&
907 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900908}
909
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700910// Propagate sanitizer requirements down from binaries
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500911func sanitizerDepsMutator(t SanitizerType) func(android.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700912 return func(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500913 if c, ok := mctx.Module().(PlatformSanitizeable); ok {
914 enabled := c.IsSanitizerEnabled(t)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900915 if t == cfi && needsCfiForVendorSnapshot(mctx) {
916 // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly
917 // determine defaultVariation in sanitizerMutator below.
918 // Instead, just mark SanitizeDep to forcefully create cfi variant.
919 enabled = true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500920 c.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900921 }
922 if enabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500923 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Inseob Kimc42f2f22020-07-29 20:32:10 +0900924 mctx.WalkDeps(func(child, parent android.Module) bool {
925 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
926 return false
927 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500928 if d, ok := child.(PlatformSanitizeable); ok && d.SanitizePropDefined() &&
929 !d.SanitizeNever() &&
930 !d.IsSanitizerExplicitlyDisabled(t) {
Colin Crossaf98f582021-05-12 17:27:32 -0700931 if t == cfi || t == Hwasan || t == scs || t == Asan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500932 if d.StaticallyLinked() && d.SanitizerSupported(t) {
933 // Rust does not support some of these sanitizers, so we need to check if it's
934 // supported before setting this true.
935 d.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900936 }
937 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500938 d.SetSanitizeDep(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700939 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000940 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900941 return true
942 })
943 }
Jiyong Parkf97782b2019-02-13 20:28:58 +0900944 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
945 // If an APEX module includes a lib which is enabled for a sanitizer T, then
946 // the APEX module is also enabled for the same sanitizer type.
947 mctx.VisitDirectDeps(func(child android.Module) {
948 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
949 sanitizeable.EnableSanitizer(t.name())
950 }
951 })
Colin Cross16b23492016-01-06 14:41:07 -0800952 }
953 }
954}
955
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500956func (c *Module) SanitizeNever() bool {
957 return Bool(c.sanitize.Properties.Sanitize.Never)
958}
959
960func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
961 return c.sanitize.isSanitizerExplicitlyDisabled(t)
962}
963
Ivan Lozano30c5db22018-02-21 15:49:20 -0800964// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700965func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500966 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -0700967 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500968 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -0700969 mctx.WalkDeps(func(child, parent android.Module) bool {
970 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
971 return false
972 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800973
Inseob Kimeec88e12020-01-22 11:11:29 +0900974 d, ok := child.(*Module)
975 if !ok || !d.static() {
976 return false
977 }
978 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700979 if enableMinimalRuntime(d.sanitize) {
980 // If a static dependency is built with the minimal runtime,
981 // make sure we include the ubsan minimal runtime.
982 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +0900983 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -0700984 // If a static dependency runs with full ubsan diagnostics,
985 // make sure we include the ubsan runtime.
986 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800987 }
Colin Cross0b908332019-06-19 23:00:20 -0700988
989 if c.sanitize.Properties.MinimalRuntimeDep &&
990 c.sanitize.Properties.UbsanRuntimeDep {
991 // both flags that this mutator might set are true, so don't bother recursing
992 return false
993 }
994
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500995 if c.Os() == android.Linux {
996 c.sanitize.Properties.BuiltinsDep = true
997 }
998
Colin Cross0b908332019-06-19 23:00:20 -0700999 return true
Colin Cross6b753602018-06-21 13:03:07 -07001000 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001001
Jose Galmesf7294582020-11-13 12:07:36 -08001002 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001003 if Bool(p.properties.Sanitize_minimal_dep) {
1004 c.sanitize.Properties.MinimalRuntimeDep = true
1005 }
1006 if Bool(p.properties.Sanitize_ubsan_dep) {
1007 c.sanitize.Properties.UbsanRuntimeDep = true
1008 }
1009 }
1010
1011 return false
Colin Cross6b753602018-06-21 13:03:07 -07001012 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001013 }
1014}
1015
Jiyong Park379de2f2018-12-19 02:47:14 +09001016// Add the dependency to the runtime library for each of the sanitizer variants
1017func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001018 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001019 if !c.Enabled() {
1020 return
1021 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001022 var sanitizers []string
1023 var diagSanitizers []string
1024
1025 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
1026 sanitizers = append(sanitizers, "undefined")
1027 } else {
1028 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
1029 sanitizers = append(sanitizers,
1030 "bool",
1031 "integer-divide-by-zero",
1032 "return",
1033 "returns-nonnull-attribute",
1034 "shift-exponent",
1035 "unreachable",
1036 "vla-bound",
1037 // TODO(danalbert): The following checks currently have compiler performance issues.
1038 //"alignment",
1039 //"bounds",
1040 //"enum",
1041 //"float-cast-overflow",
1042 //"float-divide-by-zero",
1043 //"nonnull-attribute",
1044 //"null",
1045 //"shift-base",
1046 //"signed-integer-overflow",
1047 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1048 // https://llvm.org/PR19302
1049 // http://reviews.llvm.org/D6974
1050 // "object-size",
1051 )
1052 }
1053 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
1054 }
1055
1056 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
1057 diagSanitizers = append(diagSanitizers, "undefined")
1058 }
1059
1060 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
1061
1062 if Bool(c.sanitize.Properties.Sanitize.Address) {
1063 sanitizers = append(sanitizers, "address")
1064 diagSanitizers = append(diagSanitizers, "address")
1065 }
1066
1067 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1068 sanitizers = append(sanitizers, "hwaddress")
1069 }
1070
1071 if Bool(c.sanitize.Properties.Sanitize.Thread) {
1072 sanitizers = append(sanitizers, "thread")
1073 }
1074
1075 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
1076 sanitizers = append(sanitizers, "safe-stack")
1077 }
1078
1079 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
1080 sanitizers = append(sanitizers, "cfi")
1081
1082 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
1083 diagSanitizers = append(diagSanitizers, "cfi")
1084 }
1085 }
1086
1087 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
1088 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1089 sanitizers = append(sanitizers, "signed-integer-overflow")
1090 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
1091 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1092 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1093 }
1094 }
1095
1096 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1097 sanitizers = append(sanitizers, "scudo")
1098 }
1099
1100 if Bool(c.sanitize.Properties.Sanitize.Scs) {
1101 sanitizers = append(sanitizers, "shadow-call-stack")
1102 }
1103
Ivan Lozanod7586b62021-04-01 09:49:36 -04001104 if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.Binary() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001105 noteDep := "note_memtag_heap_async"
1106 if Bool(c.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
1107 noteDep = "note_memtag_heap_sync"
1108 }
Inseob Kim253f5212021-04-08 17:10:31 +09001109 // If we're using snapshots, redirect to snapshot whenever possible
1110 // TODO(b/178470649): clean manual snapshot redirections
1111 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1112 if lib, ok := snapshot.StaticLibs[noteDep]; ok {
1113 noteDep = lib
1114 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001115 depTag := libraryDependencyTag{Kind: staticLibraryDependency, wholeStatic: true}
1116 variations := append(mctx.Target().Variations(),
1117 blueprint.Variation{Mutator: "link", Variation: "static"})
1118 if c.Device() {
1119 variations = append(variations, c.ImageVariation())
1120 }
1121 mctx.AddFarVariationDependencies(variations, depTag, noteDep)
1122 }
1123
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001124 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
1125 sanitizers = append(sanitizers, "fuzzer-no-link")
1126 }
1127
Jiyong Park379de2f2018-12-19 02:47:14 +09001128 // Save the list of sanitizers. These will be used again when generating
1129 // the build rules (for Cflags, etc.)
1130 c.sanitize.Properties.Sanitizers = sanitizers
1131 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1132
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001133 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1134 if c.Host() {
1135 diagSanitizers = sanitizers
1136 }
1137
Jiyong Park379de2f2018-12-19 02:47:14 +09001138 // Determine the runtime library required
1139 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001140 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001141 toolchain := c.toolchain(mctx)
1142 if Bool(c.sanitize.Properties.Sanitize.Address) {
1143 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1144 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1145 if c.staticBinary() {
1146 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001147 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001148 } else {
1149 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1150 }
1151 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
1152 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1153 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1154 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1155 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1156 } else {
1157 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1158 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001159 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001160 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
1161 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
1162 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001163 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
Colin Cross32f1de32021-03-29 13:41:37 -07001164 if c.staticBinary() {
1165 runtimeLibrary += ".static"
1166 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001167 }
1168
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001169 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
1170 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001171
1172 // Adding dependency to the runtime library. We are using *FarVariation*
1173 // because the runtime libraries themselves are not mutated by sanitizer
1174 // mutators and thus don't have sanitizer variants whereas this module
1175 // has been already mutated.
1176 //
1177 // Note that by adding dependency with {static|shared}DepTag, the lib is
1178 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
1179 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001180 deps := append(extraStaticDeps, runtimeLibrary)
Colin Crosse0edaf92021-01-11 17:31:17 -08001181 // If we're using snapshots, redirect to snapshot whenever possible
1182 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1183 for idx, dep := range deps {
1184 if lib, ok := snapshot.StaticLibs[dep]; ok {
1185 deps[idx] = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001186 }
1187 }
1188
Jiyong Park379de2f2018-12-19 02:47:14 +09001189 // static executable gets static runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -07001190 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Colin Cross42507332020-08-21 16:15:23 -07001191 variations := append(mctx.Target().Variations(),
1192 blueprint.Variation{Mutator: "link", Variation: "static"})
1193 if c.Device() {
1194 variations = append(variations, c.ImageVariation())
1195 }
1196 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001197 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001198 // If we're using snapshots, redirect to snapshot whenever possible
1199 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1200 if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
1201 runtimeLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001202 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001203
Cindy Zhou18417cb2020-12-10 07:12:38 -08001204 // Skip apex dependency check for sharedLibraryDependency
1205 // when sanitizer diags are enabled. Skipping the check will allow
1206 // building with diag libraries without having to list the
1207 // dependency in Apex's allowed_deps file.
1208 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001209 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001210 depTag := libraryDependencyTag{
1211 Kind: sharedLibraryDependency,
1212 Order: earlyLibraryDependency,
1213
1214 skipApexAllowedDependenciesCheck: diagEnabled,
1215 }
Colin Cross42507332020-08-21 16:15:23 -07001216 variations := append(mctx.Target().Variations(),
1217 blueprint.Variation{Mutator: "link", Variation: "shared"})
1218 if c.Device() {
1219 variations = append(variations, c.ImageVariation())
1220 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001221 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001222 }
1223 // static lib does not have dependency to the runtime library. The
1224 // dependency will be added to the executables or shared libs using
1225 // the static lib.
1226 }
1227 }
1228}
1229
1230type Sanitizeable interface {
1231 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +09001232 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001233 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001234 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001235}
1236
Ivan Lozanod7586b62021-04-01 09:49:36 -04001237func (c *Module) MinimalRuntimeDep() bool {
1238 return c.sanitize.Properties.MinimalRuntimeDep
1239}
1240
1241func (c *Module) UbsanRuntimeDep() bool {
1242 return c.sanitize.Properties.UbsanRuntimeDep
1243}
1244
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001245func (c *Module) SanitizePropDefined() bool {
1246 return c.sanitize != nil
1247}
1248
1249func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1250 return c.sanitize.isSanitizerEnabled(t)
1251}
1252
1253func (c *Module) SanitizeDep() bool {
1254 return c.sanitize.Properties.SanitizeDep
1255}
1256
1257func (c *Module) StaticallyLinked() bool {
1258 return c.static()
1259}
1260
1261func (c *Module) SetInSanitizerDir() {
1262 if c.sanitize != nil {
1263 c.sanitize.Properties.InSanitizerDir = true
1264 }
1265}
1266
1267func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1268 if c.sanitize != nil {
1269 c.sanitize.SetSanitizer(t, b)
1270 }
1271}
1272
1273func (c *Module) SetSanitizeDep(b bool) {
1274 if c.sanitize != nil {
1275 c.sanitize.Properties.SanitizeDep = b
1276 }
1277}
1278
1279var _ PlatformSanitizeable = (*Module)(nil)
1280
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001281// Create sanitized variants for modules that need them
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001282func sanitizerMutator(t SanitizerType) func(android.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -07001283 return func(mctx android.BottomUpMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001284 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
Liz Kammer187d5442021-06-25 14:50:12 -04001285 if c.Binary() && c.IsSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001286 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001287 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1288 } else if c.IsSanitizerEnabled(t) || c.SanitizeDep() {
1289 isSanitizerEnabled := c.IsSanitizerEnabled(t)
Colin Crossaf98f582021-05-12 17:27:32 -07001290 if c.StaticallyLinked() || c.Header() || t == Fuzzer {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001291 // Static and header libs are split into non-sanitized and sanitized variants.
1292 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1293 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1294 // that isn't sanitized for asan/fuzzer.
1295 //
1296 // Note for defaultVariation: since we don't split for shared libs but for static/header
1297 // libs, it is possible for the sanitized variant of a static/header lib to depend
1298 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1299 // error when the module is split. defaultVariation is the name of the variation that
1300 // will be used when such a dangling dependency occurs during the split of the current
1301 // module. By setting it to the name of the sanitized variation, the dangling dependency
1302 // is redirected to the sanitized variant of the dependent module.
1303 defaultVariation := t.variationName()
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001304 // Not all PlatformSanitizeable modules support the CFI sanitizer
1305 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001306 mctx.SetDefaultDependencyVariation(&defaultVariation)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001307
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001308 modules := mctx.CreateVariations("", t.variationName())
1309 modules[0].(PlatformSanitizeable).SetSanitizer(t, false)
1310 modules[1].(PlatformSanitizeable).SetSanitizer(t, true)
1311 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
1312 modules[1].(PlatformSanitizeable).SetSanitizeDep(false)
1313
1314 if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
Ivan Lozano4774a812020-03-10 16:23:57 -04001315 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1316 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001317 modules[1].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001318 }
1319
Jiyong Park1d1119f2019-07-29 21:27:18 +09001320 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1321 // to Make, because the sanitized version has a different suffix in name.
1322 // For other types of sanitizers, suppress the variation that is disabled.
Tri Vo6eafc362021-04-01 11:29:09 -07001323 if t != cfi && t != scs && t != Hwasan {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001324 if isSanitizerEnabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001325 modules[0].(PlatformSanitizeable).SetPreventInstall()
1326 modules[0].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001327 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001328 modules[1].(PlatformSanitizeable).SetPreventInstall()
1329 modules[1].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001330 }
1331 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001332
Jiyong Park1d1119f2019-07-29 21:27:18 +09001333 // Export the static lib name to make
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001334 if c.StaticallyLinked() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001335 if t == cfi {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001336 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
Tri Vo6eafc362021-04-01 11:29:09 -07001337 } else if t == Hwasan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001338 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001339 }
1340 }
1341 } else {
1342 // Shared libs are not split. Only the sanitized variant is created.
1343 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001344 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1345 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
Vishwath Mohane7128792017-11-17 11:08:10 -08001346
Jiyong Park1d1119f2019-07-29 21:27:18 +09001347 // locate the asan libraries under /data/asan
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001348 if mctx.Device() && t == Asan && isSanitizerEnabled {
1349 modules[0].(PlatformSanitizeable).SetInSanitizerDir()
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001350 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001351
1352 if mctx.Device() && t.incompatibleWithCfi() {
1353 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1354 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001355 modules[0].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001356 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001357 }
Colin Cross16b23492016-01-06 14:41:07 -08001358 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001359 c.SetSanitizeDep(false)
Jiyong Park82226632019-02-01 10:50:50 +09001360 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001361 // APEX modules fall here
Jooyung Han8ce8db92020-05-15 19:05:05 +09001362 sanitizeable.AddSanitizerDependencies(mctx, t.name())
Jiyong Park82226632019-02-01 10:50:50 +09001363 mctx.CreateVariations(t.variationName())
Inseob Kimc42f2f22020-07-29 20:32:10 +09001364 } else if c, ok := mctx.Module().(*Module); ok {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001365 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1366
Inseob Kimc42f2f22020-07-29 20:32:10 +09001367 // Check if it's a snapshot module supporting sanitizer
1368 if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) {
1369 // Set default variation as above.
1370 defaultVariation := t.variationName()
1371 mctx.SetDefaultDependencyVariation(&defaultVariation)
1372 modules := mctx.CreateVariations("", t.variationName())
1373 modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false)
1374 modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true)
1375
1376 // Export the static lib name to make
1377 if c.static() && c.ExportedToMake() {
1378 if t == cfi {
1379 // use BaseModuleName which is the name for Make.
1380 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1381 }
1382 }
1383 }
Colin Cross16b23492016-01-06 14:41:07 -08001384 }
1385 }
1386}
Vishwath Mohane7128792017-11-17 11:08:10 -08001387
Inseob Kim74d25562020-08-04 00:41:38 +09001388type sanitizerStaticLibsMap struct {
1389 // libsMap contains one list of modules per each image and each arch.
1390 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001391 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001392 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001393 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001394}
1395
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001396func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001397 return &sanitizerStaticLibsMap{
1398 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001399 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001400 }
1401}
1402
1403// Add the current module to sanitizer static libs maps
1404// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001405func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1406 image := GetImageVariantType(c)
1407 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001408
1409 s.libsMapLock.Lock()
1410 defer s.libsMapLock.Unlock()
1411
1412 if _, ok := s.libsMap[image]; !ok {
1413 s.libsMap[image] = make(map[string][]string)
1414 }
1415
1416 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1417}
1418
1419// Exports makefile variables in the following format:
1420// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1421// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1422// These are to be used by use_soong_sanitized_static_libraries.
1423// See build/make/core/binary.mk for more details.
1424func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1425 for _, image := range android.SortedStringKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001426 archMap := s.libsMap[ImageVariantType(image)]
Inseob Kim74d25562020-08-04 00:41:38 +09001427 for _, arch := range android.SortedStringKeys(archMap) {
1428 libs := archMap[arch]
1429 sort.Strings(libs)
1430
1431 key := fmt.Sprintf(
1432 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1433 s.sanitizerType.variationName(),
1434 image, // already upper
1435 arch)
1436
1437 ctx.Strict(key, strings.Join(libs, " "))
1438 }
1439 }
1440}
1441
Colin Cross571cccf2019-02-04 11:22:08 -08001442var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1443
Inseob Kim74d25562020-08-04 00:41:38 +09001444func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001445 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001446 return newSanitizerStaticLibsMap(cfi)
1447 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001448}
1449
Colin Cross571cccf2019-02-04 11:22:08 -08001450var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1451
Inseob Kim74d25562020-08-04 00:41:38 +09001452func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001453 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001454 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001455 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001456}
1457
Ivan Lozano30c5db22018-02-21 15:49:20 -08001458func enableMinimalRuntime(sanitize *sanitize) bool {
1459 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001460 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001461 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001462
Ivan Lozano30c5db22018-02-21 15:49:20 -08001463 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001464 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1465 Bool(sanitize.Properties.Sanitize.Undefined) ||
1466 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1467
Ivan Lozano30c5db22018-02-21 15:49:20 -08001468 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1469 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001470 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001471 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001472
Ivan Lozano30c5db22018-02-21 15:49:20 -08001473 return true
1474 }
1475 return false
1476}
1477
Ivan Lozanod7586b62021-04-01 09:49:36 -04001478func (m *Module) UbsanRuntimeNeeded() bool {
1479 return enableUbsanRuntime(m.sanitize)
1480}
1481
1482func (m *Module) MinimalRuntimeNeeded() bool {
1483 return enableMinimalRuntime(m.sanitize)
1484}
1485
Inseob Kim8471cda2019-11-15 09:59:12 +09001486func enableUbsanRuntime(sanitize *sanitize) bool {
1487 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001488 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001489 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1490}
1491
Vishwath Mohane7128792017-11-17 11:08:10 -08001492func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001493 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001494}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001495
1496func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001497 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001498}