blob: 0ed0d47c5762ee3fbd526d6a0b6d450c44b5995d [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}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400296 if ctx.testBinary() {
297 if s.Memtag_heap == nil {
298 s.Memtag_heap = proptools.BoolPtr(true)
299 }
300 if s.Diag.Memtag_heap == nil {
301 s.Diag.Memtag_heap = proptools.BoolPtr(true)
302 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800303 }
304
Colin Cross16b23492016-01-06 14:41:07 -0800305 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700306 var globalSanitizersDiag []string
307
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700308 if ctx.Host() {
309 if !ctx.Windows() {
310 globalSanitizers = ctx.Config().SanitizeHost()
311 }
312 } else {
313 arches := ctx.Config().SanitizeDeviceArch()
314 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
315 globalSanitizers = ctx.Config().SanitizeDevice()
316 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800317 }
318 }
319
Colin Cross16b23492016-01-06 14:41:07 -0800320 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000321 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700322 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400323 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000324 }
Colin Cross16b23492016-01-06 14:41:07 -0800325
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700326 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400327 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000328 }
329
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700330 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400331 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000332 }
333
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700334 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400335 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000336 }
337
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700338 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400339 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700340 }
341
342 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400343 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000344 }
345
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700346 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800347 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400348 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700349 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700350 }
351
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700352 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700353 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700354 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400355 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700356 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700357 }
358
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700359 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400360 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700361 }
362
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700363 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400364 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700365 }
366
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000367 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
368 // Hwaddress and Address are set before, so we can check them here
369 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
370 if s.Address == nil && s.Hwaddress == nil {
371 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
372 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400373 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000374 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700375 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800376 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400377 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800378 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700379 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000380
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000381 if len(globalSanitizers) > 0 {
382 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
383 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700384
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700385 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700386 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700387 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400388 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700389 }
390
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700391 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
392 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400393 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700394 }
395
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800396 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
397 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400398 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800399 }
400
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700401 if len(globalSanitizersDiag) > 0 {
402 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
403 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700404 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700405
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800406 // Enable Memtag for all components in the include paths (for Aarch64 only)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800407 if ctx.Arch().ArchType == android.Arm64 {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800408 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800409 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400410 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800411 }
412 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400413 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800414 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800415 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800416 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400417 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800418 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800419 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700420 }
421
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700422 // Enable CFI for all components in the include paths (for Aarch64 only)
423 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400424 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000425 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400426 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700427 }
428 }
429
Elliott Hughesda3a0712020-03-06 16:55:28 -0800430 // Is CFI actually enabled?
431 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400432 s.Cfi = nil
433 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800434 }
435
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700436 // HWASan requires AArch64 hardware feature (top-byte-ignore).
437 if ctx.Arch().ArchType != android.Arm64 {
438 s.Hwaddress = nil
439 }
440
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800441 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800442 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800443 s.Scs = nil
444 }
445
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700446 // memtag_heap is only implemented on AArch64.
447 if ctx.Arch().ArchType != android.Arm64 {
448 s.Memtag_heap = nil
449 }
450
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700451 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700452 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400453 s.Cfi = nil
454 s.Diag.Cfi = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700455 }
456
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500457 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
458 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400459 s.Cfi = nil
460 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700461 s.Misc_undefined = nil
462 s.Undefined = nil
463 s.All_undefined = nil
464 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800465 }
466
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700467 // Also disable CFI for VNDK variants of components
468 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900469 if ctx.static() {
470 // Cfi variant for static vndk should be captured as vendor snapshot,
471 // so don't strictly disable Cfi.
472 s.Cfi = nil
473 s.Diag.Cfi = nil
474 } else {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400475 s.Cfi = nil
476 s.Diag.Cfi = nil
Inseob Kimc42f2f22020-07-29 20:32:10 +0900477 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900478 }
479
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700480 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700481 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
482 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700483 s.Hwaddress = nil
484 }
485
Colin Cross3c344ef2016-07-18 15:44:56 -0700486 if ctx.staticBinary() {
487 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700488 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700489 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800490 }
491
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700492 if Bool(s.All_undefined) {
493 s.Undefined = nil
494 }
495
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700496 if !ctx.toolchain().Is64Bit() {
497 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700498 s.Thread = nil
499 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800500 // TODO(ccross): error for compile_multilib = "32"?
501 }
502
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800503 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 -0700504 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 -0700505 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700506 sanitize.Properties.SanitizerEnabled = true
507 }
508
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800509 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
510 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700511 s.Scudo = nil
512 }
513
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700514 if Bool(s.Hwaddress) {
515 s.Address = nil
516 s.Thread = nil
Evgenii Stepanovb15a5642021-06-23 12:30:55 -0700517 // Disable ubsan diagnosic as a workaround for a compiler bug.
518 // TODO(b/191808836): re-enable.
519 s.Diag.Undefined = nil
520 s.Diag.Integer_overflow = nil
521 s.Diag.Misc_undefined = nil
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700522 }
523
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700524 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
525 // mutually incompatible.
526 if Bool(s.Fuzzer) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400527 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800528 }
529}
530
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800531func toDisableImplicitIntegerChange(flags []string) bool {
532 // Returns true if any flag is fsanitize*integer, and there is
533 // no explicit flag about sanitize=implicit-integer-sign-change.
534 for _, f := range flags {
535 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
536 return false
537 }
538 }
539 for _, f := range flags {
540 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
541 return true
542 }
543 }
544 return false
545}
546
Yabin Cuidb7dda82020-11-30 15:47:45 -0800547func toDisableUnsignedShiftBaseChange(flags []string) bool {
548 // Returns true if any flag is fsanitize*integer, and there is
549 // no explicit flag about sanitize=unsigned-shift-base.
550 for _, f := range flags {
551 if strings.Contains(f, "sanitize=unsigned-shift-base") {
552 return false
553 }
554 }
555 for _, f := range flags {
556 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
557 return true
558 }
559 }
560 return false
561}
562
Colin Cross16b23492016-01-06 14:41:07 -0800563func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700564 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
565 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500566 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
567 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800568
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500569 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800570 flags.Local.LdFlags = append(flags.Local.LdFlags,
571 minimalRuntimePath,
572 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800573 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500574
575 if sanitize.Properties.BuiltinsDep {
576 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
577 }
578
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700579 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800580 return flags
581 }
582
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700583 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700584 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800585 // Frame pointer based unwinder in ASan requires ARM frame setup.
586 // TODO: put in flags?
587 flags.RequiredInstructionSet = "arm"
588 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800589 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
590 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800591
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000592 if Bool(sanitize.Properties.Sanitize.Writeonly) {
593 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
594 }
595
Colin Cross16b23492016-01-06 14:41:07 -0800596 if ctx.Host() {
597 // -nodefaultlibs (provided with libc++) prevents the driver from linking
598 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800599 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800600 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800601 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900602 if ctx.bootstrap() {
603 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
604 } else {
605 flags.DynamicLinker = "/system/bin/linker_asan"
606 }
Colin Cross16b23492016-01-06 14:41:07 -0800607 if flags.Toolchain.Is64Bit() {
608 flags.DynamicLinker += "64"
609 }
610 }
Colin Cross16b23492016-01-06 14:41:07 -0800611 }
612
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700613 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800614 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000615 if Bool(sanitize.Properties.Sanitize.Writeonly) {
616 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
617 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700618 }
619
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700620 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800621 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700622
623 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800624 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
625 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
626 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
627 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700628
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700629 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
630 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
631 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800632 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
633 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700634
Mitch Phillips74384752019-06-17 10:33:52 -0700635 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800636 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700637
638 // Disable fortify for fuzzing builds. Generally, we'll be building with
639 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800640 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800641
642 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
643 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
644 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
645 // the DT_RUNPATH from the shared library above it, and not the executable,
646 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
647 // DT_RUNPATH here means that transient shared libraries can be found
648 // colocated with their parents.
649 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800650 }
651
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700652 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800653 if ctx.Arch().ArchType == android.Arm {
654 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
655 // to do this on a function basis, so force Thumb on the entire module.
656 flags.RequiredInstructionSet = "thumb"
657 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000658
Colin Cross4af21ed2019-11-04 09:37:55 -0800659 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
660 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800661 if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
662 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
663 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000664 // Only append the default visibility flag if -fvisibility has not already been set
665 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800666 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
667 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000668 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800669 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000670
671 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800672 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
673 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000674 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700675 }
676
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700677 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800678 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700679 }
680
Jiyong Park379de2f2018-12-19 02:47:14 +0900681 if len(sanitize.Properties.Sanitizers) > 0 {
682 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800683
Colin Cross4af21ed2019-11-04 09:37:55 -0800684 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
685 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800686 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800687 // Host sanitizers only link symbols in the final executable, so
688 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800689 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
690 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500691
692 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
693 if !ctx.toolchain().Bionic() {
694 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
695 }
696 }
697
698 if enableMinimalRuntime(sanitize) {
699 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
700 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
701 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
702 if !ctx.toolchain().Bionic() {
703 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800704 }
Colin Cross16b23492016-01-06 14:41:07 -0800705 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700706
707 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
708 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800709 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700710 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800711 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700712 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800713 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700714 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800715 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800716 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
717 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800718 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800719 // http://b/171275751, Android doesn't build with this sanitizer yet.
720 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
721 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
722 }
Colin Cross16b23492016-01-06 14:41:07 -0800723 }
724
Jiyong Park379de2f2018-12-19 02:47:14 +0900725 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800726 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700727 }
728 // FIXME: enable RTTI if diag + (cfi or vptr)
729
Andreas Gampe97071162017-05-08 13:15:23 -0700730 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800731 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700732 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
733 }
734
Ivan Lozano7929bba2018-12-12 09:36:31 -0800735 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800736 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800737 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
738 }
739
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700740 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
741 if blocklist.Valid() {
742 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blocklist.String())
743 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
744 }
745
Colin Cross16b23492016-01-06 14:41:07 -0800746 return flags
747}
748
Colin Crossd80cbca2020-02-24 12:01:37 -0800749func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900750 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
751 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800752 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900753 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800754 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900755 }
756 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800757 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900758 }
759 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800760 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900761 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800762 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700763}
764
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700765func (sanitize *sanitize) inSanitizerDir() bool {
766 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700767}
768
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500769// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
770func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000771 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500772 case Asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000773 return sanitize.Properties.Sanitize.Address
Tri Vo6eafc362021-04-01 11:29:09 -0700774 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700775 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000776 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000777 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000778 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000779 return sanitize.Properties.Sanitize.Integer_overflow
780 case cfi:
781 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800782 case scs:
783 return sanitize.Properties.Sanitize.Scs
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700784 case memtag_heap:
785 return sanitize.Properties.Sanitize.Memtag_heap
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500786 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700787 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000788 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500789 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000790 }
791}
792
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500793// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800794func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500795 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700796 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800797 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800798 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700799 !sanitize.isSanitizerEnabled(scs) &&
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700800 !sanitize.isSanitizerEnabled(memtag_heap) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500801 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800802}
803
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500804// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700805func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500806 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700807 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700808 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500809 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700810}
811
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500812func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400813 bPtr := proptools.BoolPtr(b)
814 if !b {
815 bPtr = nil
816 }
Colin Cross16b23492016-01-06 14:41:07 -0800817 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500818 case Asan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400819 sanitize.Properties.Sanitize.Address = bPtr
Tri Vo6eafc362021-04-01 11:29:09 -0700820 case Hwasan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400821 sanitize.Properties.Sanitize.Hwaddress = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800822 case tsan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400823 sanitize.Properties.Sanitize.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700824 case intOverflow:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400825 sanitize.Properties.Sanitize.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000826 case cfi:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400827 sanitize.Properties.Sanitize.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800828 case scs:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400829 sanitize.Properties.Sanitize.Scs = bPtr
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700830 case memtag_heap:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400831 sanitize.Properties.Sanitize.Memtag_heap = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500832 case Fuzzer:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400833 sanitize.Properties.Sanitize.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800834 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500835 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800836 }
837 if b {
838 sanitize.Properties.SanitizerEnabled = true
839 }
840}
841
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000842// Check if the sanitizer is explicitly disabled (as opposed to nil by
843// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500844func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000845 if sanitize == nil {
846 return false
847 }
848
849 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
850 return sanitizerVal != nil && *sanitizerVal == false
851}
852
853// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
854// because enabling a sanitizer either directly (via the blueprint) or
855// indirectly (via a mutator) sets the bool ptr to true, and you can't
856// distinguish between the cases. It isn't needed though - both cases can be
857// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500858func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000859 if sanitize == nil {
860 return false
861 }
862
863 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
864 return sanitizerVal != nil && *sanitizerVal == true
865}
866
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500867// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
868func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700869 switch t := tag.(type) {
870 case dependencyTag:
871 return t == reuseObjTag || t == objDepTag
872 case libraryDependencyTag:
873 return true
874 default:
875 return false
876 }
Colin Cross6b753602018-06-21 13:03:07 -0700877}
878
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500879func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
880 return IsSanitizableDependencyTag
881}
882
Inseob Kimc42f2f22020-07-29 20:32:10 +0900883// Determines if the current module is a static library going to be captured
884// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
885// except for ones which explicitly disable cfi.
886func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400887 if IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900888 return false
889 }
890
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500891 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900892
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500893 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900894 return false
895 }
896
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500897 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900898 return false
899 }
900
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500901 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900902 return false
903 }
904
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500905 if !c.SanitizerSupported(cfi) {
906 return false
907 }
908
909 return c.SanitizePropDefined() &&
910 !c.SanitizeNever() &&
911 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900912}
913
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700914// Propagate sanitizer requirements down from binaries
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500915func sanitizerDepsMutator(t SanitizerType) func(android.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700916 return func(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500917 if c, ok := mctx.Module().(PlatformSanitizeable); ok {
918 enabled := c.IsSanitizerEnabled(t)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900919 if t == cfi && needsCfiForVendorSnapshot(mctx) {
920 // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly
921 // determine defaultVariation in sanitizerMutator below.
922 // Instead, just mark SanitizeDep to forcefully create cfi variant.
923 enabled = true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500924 c.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900925 }
926 if enabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500927 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Inseob Kimc42f2f22020-07-29 20:32:10 +0900928 mctx.WalkDeps(func(child, parent android.Module) bool {
929 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
930 return false
931 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500932 if d, ok := child.(PlatformSanitizeable); ok && d.SanitizePropDefined() &&
933 !d.SanitizeNever() &&
934 !d.IsSanitizerExplicitlyDisabled(t) {
Colin Crossaf98f582021-05-12 17:27:32 -0700935 if t == cfi || t == Hwasan || t == scs || t == Asan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500936 if d.StaticallyLinked() && d.SanitizerSupported(t) {
937 // Rust does not support some of these sanitizers, so we need to check if it's
938 // supported before setting this true.
939 d.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900940 }
941 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500942 d.SetSanitizeDep(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700943 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000944 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900945 return true
946 })
947 }
Jiyong Parkf97782b2019-02-13 20:28:58 +0900948 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
949 // If an APEX module includes a lib which is enabled for a sanitizer T, then
950 // the APEX module is also enabled for the same sanitizer type.
951 mctx.VisitDirectDeps(func(child android.Module) {
952 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
953 sanitizeable.EnableSanitizer(t.name())
954 }
955 })
Colin Cross16b23492016-01-06 14:41:07 -0800956 }
957 }
958}
959
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500960func (c *Module) SanitizeNever() bool {
961 return Bool(c.sanitize.Properties.Sanitize.Never)
962}
963
964func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
965 return c.sanitize.isSanitizerExplicitlyDisabled(t)
966}
967
Ivan Lozano30c5db22018-02-21 15:49:20 -0800968// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700969func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500970 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -0700971 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500972 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -0700973 mctx.WalkDeps(func(child, parent android.Module) bool {
974 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
975 return false
976 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800977
Inseob Kimeec88e12020-01-22 11:11:29 +0900978 d, ok := child.(*Module)
979 if !ok || !d.static() {
980 return false
981 }
982 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700983 if enableMinimalRuntime(d.sanitize) {
984 // If a static dependency is built with the minimal runtime,
985 // make sure we include the ubsan minimal runtime.
986 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +0900987 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -0700988 // If a static dependency runs with full ubsan diagnostics,
989 // make sure we include the ubsan runtime.
990 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800991 }
Colin Cross0b908332019-06-19 23:00:20 -0700992
993 if c.sanitize.Properties.MinimalRuntimeDep &&
994 c.sanitize.Properties.UbsanRuntimeDep {
995 // both flags that this mutator might set are true, so don't bother recursing
996 return false
997 }
998
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500999 if c.Os() == android.Linux {
1000 c.sanitize.Properties.BuiltinsDep = true
1001 }
1002
Colin Cross0b908332019-06-19 23:00:20 -07001003 return true
Colin Cross6b753602018-06-21 13:03:07 -07001004 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001005
Jose Galmesf7294582020-11-13 12:07:36 -08001006 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001007 if Bool(p.properties.Sanitize_minimal_dep) {
1008 c.sanitize.Properties.MinimalRuntimeDep = true
1009 }
1010 if Bool(p.properties.Sanitize_ubsan_dep) {
1011 c.sanitize.Properties.UbsanRuntimeDep = true
1012 }
1013 }
1014
1015 return false
Colin Cross6b753602018-06-21 13:03:07 -07001016 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001017 }
1018}
1019
Jiyong Park379de2f2018-12-19 02:47:14 +09001020// Add the dependency to the runtime library for each of the sanitizer variants
1021func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001022 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001023 if !c.Enabled() {
1024 return
1025 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001026 var sanitizers []string
1027 var diagSanitizers []string
1028
1029 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
1030 sanitizers = append(sanitizers, "undefined")
1031 } else {
1032 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
1033 sanitizers = append(sanitizers,
1034 "bool",
1035 "integer-divide-by-zero",
1036 "return",
1037 "returns-nonnull-attribute",
1038 "shift-exponent",
1039 "unreachable",
1040 "vla-bound",
1041 // TODO(danalbert): The following checks currently have compiler performance issues.
1042 //"alignment",
1043 //"bounds",
1044 //"enum",
1045 //"float-cast-overflow",
1046 //"float-divide-by-zero",
1047 //"nonnull-attribute",
1048 //"null",
1049 //"shift-base",
1050 //"signed-integer-overflow",
1051 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1052 // https://llvm.org/PR19302
1053 // http://reviews.llvm.org/D6974
1054 // "object-size",
1055 )
1056 }
1057 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
1058 }
1059
1060 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
1061 diagSanitizers = append(diagSanitizers, "undefined")
1062 }
1063
1064 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
1065
1066 if Bool(c.sanitize.Properties.Sanitize.Address) {
1067 sanitizers = append(sanitizers, "address")
1068 diagSanitizers = append(diagSanitizers, "address")
1069 }
1070
1071 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1072 sanitizers = append(sanitizers, "hwaddress")
1073 }
1074
1075 if Bool(c.sanitize.Properties.Sanitize.Thread) {
1076 sanitizers = append(sanitizers, "thread")
1077 }
1078
1079 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
1080 sanitizers = append(sanitizers, "safe-stack")
1081 }
1082
1083 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
1084 sanitizers = append(sanitizers, "cfi")
1085
1086 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
1087 diagSanitizers = append(diagSanitizers, "cfi")
1088 }
1089 }
1090
1091 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
1092 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1093 sanitizers = append(sanitizers, "signed-integer-overflow")
1094 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
1095 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1096 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1097 }
1098 }
1099
1100 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1101 sanitizers = append(sanitizers, "scudo")
1102 }
1103
1104 if Bool(c.sanitize.Properties.Sanitize.Scs) {
1105 sanitizers = append(sanitizers, "shadow-call-stack")
1106 }
1107
Ivan Lozanod7586b62021-04-01 09:49:36 -04001108 if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.Binary() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001109 noteDep := "note_memtag_heap_async"
1110 if Bool(c.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
1111 noteDep = "note_memtag_heap_sync"
1112 }
Inseob Kim253f5212021-04-08 17:10:31 +09001113 // If we're using snapshots, redirect to snapshot whenever possible
1114 // TODO(b/178470649): clean manual snapshot redirections
1115 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1116 if lib, ok := snapshot.StaticLibs[noteDep]; ok {
1117 noteDep = lib
1118 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001119 depTag := libraryDependencyTag{Kind: staticLibraryDependency, wholeStatic: true}
1120 variations := append(mctx.Target().Variations(),
1121 blueprint.Variation{Mutator: "link", Variation: "static"})
1122 if c.Device() {
1123 variations = append(variations, c.ImageVariation())
1124 }
1125 mctx.AddFarVariationDependencies(variations, depTag, noteDep)
1126 }
1127
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001128 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
1129 sanitizers = append(sanitizers, "fuzzer-no-link")
1130 }
1131
Jiyong Park379de2f2018-12-19 02:47:14 +09001132 // Save the list of sanitizers. These will be used again when generating
1133 // the build rules (for Cflags, etc.)
1134 c.sanitize.Properties.Sanitizers = sanitizers
1135 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1136
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001137 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1138 if c.Host() {
1139 diagSanitizers = sanitizers
1140 }
1141
Jiyong Park379de2f2018-12-19 02:47:14 +09001142 // Determine the runtime library required
1143 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001144 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001145 toolchain := c.toolchain(mctx)
1146 if Bool(c.sanitize.Properties.Sanitize.Address) {
1147 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1148 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1149 if c.staticBinary() {
1150 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001151 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001152 } else {
1153 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1154 }
1155 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
1156 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1157 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1158 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1159 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1160 } else {
1161 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1162 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001163 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001164 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
1165 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
1166 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001167 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
Colin Cross32f1de32021-03-29 13:41:37 -07001168 if c.staticBinary() {
1169 runtimeLibrary += ".static"
1170 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001171 }
1172
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001173 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
1174 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001175
1176 // Adding dependency to the runtime library. We are using *FarVariation*
1177 // because the runtime libraries themselves are not mutated by sanitizer
1178 // mutators and thus don't have sanitizer variants whereas this module
1179 // has been already mutated.
1180 //
1181 // Note that by adding dependency with {static|shared}DepTag, the lib is
1182 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
1183 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001184 deps := append(extraStaticDeps, runtimeLibrary)
Colin Crosse0edaf92021-01-11 17:31:17 -08001185 // If we're using snapshots, redirect to snapshot whenever possible
1186 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1187 for idx, dep := range deps {
1188 if lib, ok := snapshot.StaticLibs[dep]; ok {
1189 deps[idx] = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001190 }
1191 }
1192
Jiyong Park379de2f2018-12-19 02:47:14 +09001193 // static executable gets static runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -07001194 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Colin Cross42507332020-08-21 16:15:23 -07001195 variations := append(mctx.Target().Variations(),
1196 blueprint.Variation{Mutator: "link", Variation: "static"})
1197 if c.Device() {
1198 variations = append(variations, c.ImageVariation())
1199 }
1200 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001201 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001202 // If we're using snapshots, redirect to snapshot whenever possible
1203 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1204 if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
1205 runtimeLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001206 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001207
Cindy Zhou18417cb2020-12-10 07:12:38 -08001208 // Skip apex dependency check for sharedLibraryDependency
1209 // when sanitizer diags are enabled. Skipping the check will allow
1210 // building with diag libraries without having to list the
1211 // dependency in Apex's allowed_deps file.
1212 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001213 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001214 depTag := libraryDependencyTag{
1215 Kind: sharedLibraryDependency,
1216 Order: earlyLibraryDependency,
1217
1218 skipApexAllowedDependenciesCheck: diagEnabled,
1219 }
Colin Cross42507332020-08-21 16:15:23 -07001220 variations := append(mctx.Target().Variations(),
1221 blueprint.Variation{Mutator: "link", Variation: "shared"})
1222 if c.Device() {
1223 variations = append(variations, c.ImageVariation())
1224 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001225 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001226 }
1227 // static lib does not have dependency to the runtime library. The
1228 // dependency will be added to the executables or shared libs using
1229 // the static lib.
1230 }
1231 }
1232}
1233
1234type Sanitizeable interface {
1235 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +09001236 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001237 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001238 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001239}
1240
Ivan Lozanod7586b62021-04-01 09:49:36 -04001241func (c *Module) MinimalRuntimeDep() bool {
1242 return c.sanitize.Properties.MinimalRuntimeDep
1243}
1244
1245func (c *Module) UbsanRuntimeDep() bool {
1246 return c.sanitize.Properties.UbsanRuntimeDep
1247}
1248
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001249func (c *Module) SanitizePropDefined() bool {
1250 return c.sanitize != nil
1251}
1252
1253func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1254 return c.sanitize.isSanitizerEnabled(t)
1255}
1256
1257func (c *Module) SanitizeDep() bool {
1258 return c.sanitize.Properties.SanitizeDep
1259}
1260
1261func (c *Module) StaticallyLinked() bool {
1262 return c.static()
1263}
1264
1265func (c *Module) SetInSanitizerDir() {
1266 if c.sanitize != nil {
1267 c.sanitize.Properties.InSanitizerDir = true
1268 }
1269}
1270
1271func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1272 if c.sanitize != nil {
1273 c.sanitize.SetSanitizer(t, b)
1274 }
1275}
1276
1277func (c *Module) SetSanitizeDep(b bool) {
1278 if c.sanitize != nil {
1279 c.sanitize.Properties.SanitizeDep = b
1280 }
1281}
1282
1283var _ PlatformSanitizeable = (*Module)(nil)
1284
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001285// Create sanitized variants for modules that need them
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001286func sanitizerMutator(t SanitizerType) func(android.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -07001287 return func(mctx android.BottomUpMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001288 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
Liz Kammer187d5442021-06-25 14:50:12 -04001289 if c.Binary() && c.IsSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001290 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001291 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1292 } else if c.IsSanitizerEnabled(t) || c.SanitizeDep() {
1293 isSanitizerEnabled := c.IsSanitizerEnabled(t)
Colin Crossaf98f582021-05-12 17:27:32 -07001294 if c.StaticallyLinked() || c.Header() || t == Fuzzer {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001295 // Static and header libs are split into non-sanitized and sanitized variants.
1296 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1297 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1298 // that isn't sanitized for asan/fuzzer.
1299 //
1300 // Note for defaultVariation: since we don't split for shared libs but for static/header
1301 // libs, it is possible for the sanitized variant of a static/header lib to depend
1302 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1303 // error when the module is split. defaultVariation is the name of the variation that
1304 // will be used when such a dangling dependency occurs during the split of the current
1305 // module. By setting it to the name of the sanitized variation, the dangling dependency
1306 // is redirected to the sanitized variant of the dependent module.
1307 defaultVariation := t.variationName()
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001308 // Not all PlatformSanitizeable modules support the CFI sanitizer
1309 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001310 mctx.SetDefaultDependencyVariation(&defaultVariation)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001311
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001312 modules := mctx.CreateVariations("", t.variationName())
1313 modules[0].(PlatformSanitizeable).SetSanitizer(t, false)
1314 modules[1].(PlatformSanitizeable).SetSanitizer(t, true)
1315 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
1316 modules[1].(PlatformSanitizeable).SetSanitizeDep(false)
1317
1318 if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
Ivan Lozano4774a812020-03-10 16:23:57 -04001319 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1320 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001321 modules[1].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001322 }
1323
Jiyong Park1d1119f2019-07-29 21:27:18 +09001324 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1325 // to Make, because the sanitized version has a different suffix in name.
1326 // For other types of sanitizers, suppress the variation that is disabled.
Tri Vo6eafc362021-04-01 11:29:09 -07001327 if t != cfi && t != scs && t != Hwasan {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001328 if isSanitizerEnabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001329 modules[0].(PlatformSanitizeable).SetPreventInstall()
1330 modules[0].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001331 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001332 modules[1].(PlatformSanitizeable).SetPreventInstall()
1333 modules[1].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001334 }
1335 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001336
Jiyong Park1d1119f2019-07-29 21:27:18 +09001337 // Export the static lib name to make
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001338 if c.StaticallyLinked() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001339 if t == cfi {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001340 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
Tri Vo6eafc362021-04-01 11:29:09 -07001341 } else if t == Hwasan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001342 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001343 }
1344 }
1345 } else {
1346 // Shared libs are not split. Only the sanitized variant is created.
1347 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001348 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1349 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
Vishwath Mohane7128792017-11-17 11:08:10 -08001350
Jiyong Park1d1119f2019-07-29 21:27:18 +09001351 // locate the asan libraries under /data/asan
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001352 if mctx.Device() && t == Asan && isSanitizerEnabled {
1353 modules[0].(PlatformSanitizeable).SetInSanitizerDir()
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001354 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001355
1356 if mctx.Device() && t.incompatibleWithCfi() {
1357 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1358 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001359 modules[0].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001360 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001361 }
Colin Cross16b23492016-01-06 14:41:07 -08001362 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001363 c.SetSanitizeDep(false)
Jiyong Park82226632019-02-01 10:50:50 +09001364 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001365 // APEX modules fall here
Jooyung Han8ce8db92020-05-15 19:05:05 +09001366 sanitizeable.AddSanitizerDependencies(mctx, t.name())
Jiyong Park82226632019-02-01 10:50:50 +09001367 mctx.CreateVariations(t.variationName())
Inseob Kimc42f2f22020-07-29 20:32:10 +09001368 } else if c, ok := mctx.Module().(*Module); ok {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001369 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1370
Inseob Kimc42f2f22020-07-29 20:32:10 +09001371 // Check if it's a snapshot module supporting sanitizer
1372 if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) {
1373 // Set default variation as above.
1374 defaultVariation := t.variationName()
1375 mctx.SetDefaultDependencyVariation(&defaultVariation)
1376 modules := mctx.CreateVariations("", t.variationName())
1377 modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false)
1378 modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true)
1379
1380 // Export the static lib name to make
1381 if c.static() && c.ExportedToMake() {
1382 if t == cfi {
1383 // use BaseModuleName which is the name for Make.
1384 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1385 }
1386 }
1387 }
Colin Cross16b23492016-01-06 14:41:07 -08001388 }
1389 }
1390}
Vishwath Mohane7128792017-11-17 11:08:10 -08001391
Inseob Kim74d25562020-08-04 00:41:38 +09001392type sanitizerStaticLibsMap struct {
1393 // libsMap contains one list of modules per each image and each arch.
1394 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001395 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001396 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001397 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001398}
1399
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001400func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001401 return &sanitizerStaticLibsMap{
1402 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001403 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001404 }
1405}
1406
1407// Add the current module to sanitizer static libs maps
1408// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001409func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1410 image := GetImageVariantType(c)
1411 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001412
1413 s.libsMapLock.Lock()
1414 defer s.libsMapLock.Unlock()
1415
1416 if _, ok := s.libsMap[image]; !ok {
1417 s.libsMap[image] = make(map[string][]string)
1418 }
1419
1420 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1421}
1422
1423// Exports makefile variables in the following format:
1424// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1425// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1426// These are to be used by use_soong_sanitized_static_libraries.
1427// See build/make/core/binary.mk for more details.
1428func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1429 for _, image := range android.SortedStringKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001430 archMap := s.libsMap[ImageVariantType(image)]
Inseob Kim74d25562020-08-04 00:41:38 +09001431 for _, arch := range android.SortedStringKeys(archMap) {
1432 libs := archMap[arch]
1433 sort.Strings(libs)
1434
1435 key := fmt.Sprintf(
1436 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1437 s.sanitizerType.variationName(),
1438 image, // already upper
1439 arch)
1440
1441 ctx.Strict(key, strings.Join(libs, " "))
1442 }
1443 }
1444}
1445
Colin Cross571cccf2019-02-04 11:22:08 -08001446var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1447
Inseob Kim74d25562020-08-04 00:41:38 +09001448func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001449 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001450 return newSanitizerStaticLibsMap(cfi)
1451 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001452}
1453
Colin Cross571cccf2019-02-04 11:22:08 -08001454var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1455
Inseob Kim74d25562020-08-04 00:41:38 +09001456func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001457 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001458 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001459 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001460}
1461
Ivan Lozano30c5db22018-02-21 15:49:20 -08001462func enableMinimalRuntime(sanitize *sanitize) bool {
1463 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001464 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001465 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001466
Ivan Lozano30c5db22018-02-21 15:49:20 -08001467 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001468 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1469 Bool(sanitize.Properties.Sanitize.Undefined) ||
1470 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1471
Ivan Lozano30c5db22018-02-21 15:49:20 -08001472 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1473 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001474 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001475 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001476
Ivan Lozano30c5db22018-02-21 15:49:20 -08001477 return true
1478 }
1479 return false
1480}
1481
Ivan Lozanod7586b62021-04-01 09:49:36 -04001482func (m *Module) UbsanRuntimeNeeded() bool {
1483 return enableUbsanRuntime(m.sanitize)
1484}
1485
1486func (m *Module) MinimalRuntimeNeeded() bool {
1487 return enableMinimalRuntime(m.sanitize)
1488}
1489
Inseob Kim8471cda2019-11-15 09:59:12 +09001490func enableUbsanRuntime(sanitize *sanitize) bool {
1491 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001492 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001493 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1494}
1495
Vishwath Mohane7128792017-11-17 11:08:10 -08001496func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001497 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001498}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001499
1500func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001501 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001502}