blob: 60542526882f5b66a557b9ee7a971f5046ecf5c0 [file] [log] [blame]
Colin Cross16b23492016-01-06 14:41:07 -08001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
18 "fmt"
Jeff Gaston72765392017-11-28 16:37:53 -080019 "sort"
Colin Cross16b23492016-01-06 14:41:07 -080020 "strings"
Vishwath Mohane7128792017-11-17 11:08:10 -080021 "sync"
Colin Cross16b23492016-01-06 14:41:07 -080022
Colin Cross6b753602018-06-21 13:03:07 -070023 "github.com/google/blueprint"
Liz Kammerb2fc4702021-06-25 14:53:40 -040024 "github.com/google/blueprint/proptools"
Colin Cross6b753602018-06-21 13:03:07 -070025
Colin Cross635c3b02016-05-18 15:37:25 -070026 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070027 "android/soong/cc/config"
Kiyoung Kim48f37782021-07-07 12:42:39 +090028 "android/soong/snapshot"
Colin Cross16b23492016-01-06 14:41:07 -080029)
30
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070031var (
32 // Any C flags added by sanitizer which libTooling tools may not
33 // understand also need to be added to ClangLibToolingUnknownCflags in
34 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080035
Yi Kong20233a42019-08-21 01:38:40 -070036 asanCflags = []string{
37 "-fno-omit-frame-pointer",
38 "-fno-experimental-new-pass-manager",
39 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070040 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070041
Peter Collingbourne967511a2019-03-19 21:39:54 -070042 hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000043 "-fsanitize-hwaddress-abi=platform",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080044 // The following improves debug location information
45 // availability at the cost of its accuracy. It increases
46 // the likelihood of a stack variable's frame offset
47 // to be recorded in the debug info, which is important
48 // for the quality of hwasan reports. The downside is a
49 // higher number of "optimized out" stack variables.
50 // b/112437883.
51 "-mllvm", "-instcombine-lower-dbg-declare=0",
Mitch Phillipsb1c574f2020-06-22 13:28:23 -070052 // TODO(b/159343917): HWASan and GlobalISel don't play nicely, and
53 // GlobalISel is the default at -O0 on aarch64.
54 "-mllvm", "--aarch64-enable-global-isel-at-O=-1",
55 "-mllvm", "-fast-isel=false",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080056 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070057
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000058 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070059 "-fsanitize-ignorelist=external/compiler-rt/lib/cfi/cfi_blocklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070060 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
61 // used, but have no effect on assembly files
62 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070063 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070064 "-Wl,-plugin-opt,O1"}
Inseob Kim74d25562020-08-04 00:41:38 +090065 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070066
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -070067 intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080068
Peter Collingbournebd19db02019-03-06 10:38:48 -080069 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070070 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070071 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
72 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070073)
74
Ivan Lozano3968d8f2020-12-14 11:27:52 -050075type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080076
Colin Cross16b23492016-01-06 14:41:07 -080077const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050078 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070079 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080080 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070081 intOverflow
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080082 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -050083 Fuzzer
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -070084 memtag_heap
Liz Kammer75db9312021-07-07 16:41:50 -040085 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -080086)
87
Liz Kammer75db9312021-07-07 16:41:50 -040088var Sanitizers = []SanitizerType{
89 Asan,
90 Hwasan,
91 tsan,
92 intOverflow,
93 scs,
94 Fuzzer,
95 memtag_heap,
96 cfi, // cfi is last to prevent it running before incompatible mutators
97}
98
Jiyong Park82226632019-02-01 10:50:50 +090099// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500100func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800101 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500102 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800103 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700104 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700105 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800106 case tsan:
107 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700108 case intOverflow:
109 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000110 case cfi:
111 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800112 case scs:
113 return "scs"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700114 case memtag_heap:
115 return "memtag_heap"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500116 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700117 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800118 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500119 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800120 }
121}
122
Jiyong Park82226632019-02-01 10:50:50 +0900123// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500124func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900125 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500126 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900127 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700128 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900129 return "hwaddress"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700130 case memtag_heap:
131 return "memtag_heap"
Jiyong Park82226632019-02-01 10:50:50 +0900132 case tsan:
133 return "thread"
134 case intOverflow:
135 return "integer_overflow"
136 case cfi:
137 return "cfi"
138 case scs:
139 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500140 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700141 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900142 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500143 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900144 }
145}
146
Liz Kammer75db9312021-07-07 16:41:50 -0400147func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
148 switch t {
149 case Asan, Hwasan, Fuzzer, scs, tsan, cfi:
150 ctx.TopDown(t.variationName()+"_deps", sanitizerDepsMutator(t))
151 ctx.BottomUp(t.variationName(), sanitizerMutator(t))
152 case memtag_heap, intOverflow:
153 // do nothing
154 default:
155 panic(fmt.Errorf("unknown SanitizerType %d", t))
156 }
157}
158
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500159func (*Module) SanitizerSupported(t SanitizerType) bool {
160 switch t {
161 case Asan:
162 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700163 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500164 return true
165 case tsan:
166 return true
167 case intOverflow:
168 return true
169 case cfi:
170 return true
171 case scs:
172 return true
173 case Fuzzer:
174 return true
175 default:
176 return false
177 }
178}
179
180// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
181func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700182 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900183}
184
Martin Stjernholmb0249572020-09-15 02:32:35 +0100185type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400186 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100187 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800188
Liz Kammer75b9b402021-06-25 15:19:27 -0400189 // ASan (Address sanitizer), incompatible with static binaries.
190 // Always runs in a diagnostic mode.
191 // Use of address sanitizer disables cfi sanitizer.
192 // Hwaddress sanitizer takes precedence over this sanitizer.
193 Address *bool `android:"arch_variant"`
194 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
195 // Always runs in a diagnostic mode.
196 // Use of thread sanitizer disables cfi and scudo sanitizers.
197 // Hwaddress sanitizer takes precedence over this sanitizer.
198 Thread *bool `android:"arch_variant"`
199 // HWASan (Hardware Address sanitizer).
200 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100201 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800202
Liz Kammer75b9b402021-06-25 15:19:27 -0400203 // Undefined behavior sanitizer
204 All_undefined *bool `android:"arch_variant"`
205 // Subset of undefined behavior sanitizer
206 Undefined *bool `android:"arch_variant"`
207 // List of specific undefined behavior sanitizers to enable
208 Misc_undefined []string `android:"arch_variant"`
209 // Fuzzer, incompatible with static binaries.
210 Fuzzer *bool `android:"arch_variant"`
211 // safe-stack sanitizer, incompatible with 32-bit architectures.
212 Safestack *bool `android:"arch_variant"`
213 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
214 Cfi *bool `android:"arch_variant"`
215 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
216 Integer_overflow *bool `android:"arch_variant"`
217 // scudo sanitizer, incompatible with asan, hwasan, tsan
218 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
219 // deprecated
220 Scudo *bool `android:"arch_variant"`
221 // shadow-call-stack sanitizer, only available on arm64
222 Scs *bool `android:"arch_variant"`
223 // Memory-tagging, only available on arm64
224 // if diag.memtag unset or false, enables async memory tagging
225 Memtag_heap *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100226
227 // A modifier for ASAN and HWASAN for write only instrumentation
228 Writeonly *bool `android:"arch_variant"`
229
230 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
231 // Replaces abort() on error with a human-readable error message.
232 // Address and Thread sanitizers always run in diagnostic mode.
233 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400234 // Undefined behavior sanitizer, diagnostic mode
235 Undefined *bool `android:"arch_variant"`
236 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
237 Cfi *bool `android:"arch_variant"`
238 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
239 Integer_overflow *bool `android:"arch_variant"`
240 // Memory-tagging, only available on arm64
241 // requires sanitizer.memtag: true
242 // if set, enables sync memory tagging
243 Memtag_heap *bool `android:"arch_variant"`
244 // List of specific undefined behavior sanitizers to enable in diagnostic mode
245 Misc_undefined []string `android:"arch_variant"`
246 // List of sanitizers to pass to -fno-sanitize-recover
247 // results in only the first detected error for these sanitizers being reported and program then
248 // exits with a non-zero exit code.
249 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800250 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800251
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800252 // Sanitizers to run with flag configuration specified
253 Config struct {
254 // Enables CFI support flags for assembly-heavy libraries
255 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800256 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800257
Liz Kammer75b9b402021-06-25 15:19:27 -0400258 // List of sanitizers to pass to -fsanitize-recover
259 // allows execution to continue for these sanitizers to detect multiple errors rather than only
260 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100261 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000262
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700263 // value to pass to -fsanitize-ignorelist
Martin Stjernholmb0249572020-09-15 02:32:35 +0100264 Blocklist *string
265}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700266
Martin Stjernholmb0249572020-09-15 02:32:35 +0100267type SanitizeProperties struct {
Martin Stjernholmb0249572020-09-15 02:32:35 +0100268 Sanitize SanitizeUserProps `android:"arch_variant"`
269 SanitizerEnabled bool `blueprint:"mutated"`
270 SanitizeDep bool `blueprint:"mutated"`
271 MinimalRuntimeDep bool `blueprint:"mutated"`
272 BuiltinsDep bool `blueprint:"mutated"`
273 UbsanRuntimeDep bool `blueprint:"mutated"`
274 InSanitizerDir bool `blueprint:"mutated"`
275 Sanitizers []string `blueprint:"mutated"`
276 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800277}
278
279type sanitize struct {
280 Properties SanitizeProperties
281}
282
Cindy Zhou18417cb2020-12-10 07:12:38 -0800283// Mark this tag with a check to see if apex dependency check should be skipped
284func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
285 return t.skipApexAllowedDependenciesCheck
286}
287
288var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
289
Vishwath Mohane7128792017-11-17 11:08:10 -0800290func init() {
291 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700292 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800293}
294
Colin Cross16b23492016-01-06 14:41:07 -0800295func (sanitize *sanitize) props() []interface{} {
296 return []interface{}{&sanitize.Properties}
297}
298
299func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700300 s := &sanitize.Properties.Sanitize
301
Colin Cross16b23492016-01-06 14:41:07 -0800302 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700303 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800304 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800305 }
306
307 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800308 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800309 return
310 }
311
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800312 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400313 if ctx.testBinary() {
314 if s.Memtag_heap == nil {
315 s.Memtag_heap = proptools.BoolPtr(true)
316 }
317 if s.Diag.Memtag_heap == nil {
318 s.Diag.Memtag_heap = proptools.BoolPtr(true)
319 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800320 }
321
Colin Cross16b23492016-01-06 14:41:07 -0800322 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700323 var globalSanitizersDiag []string
324
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700325 if ctx.Host() {
326 if !ctx.Windows() {
327 globalSanitizers = ctx.Config().SanitizeHost()
328 }
329 } else {
330 arches := ctx.Config().SanitizeDeviceArch()
331 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
332 globalSanitizers = ctx.Config().SanitizeDevice()
333 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800334 }
335 }
336
Colin Cross16b23492016-01-06 14:41:07 -0800337 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000338 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700339 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400340 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000341 }
Colin Cross16b23492016-01-06 14:41:07 -0800342
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700343 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400344 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000345 }
346
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700347 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400348 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000349 }
350
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700351 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400352 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000353 }
354
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700355 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400356 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700357 }
358
359 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400360 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000361 }
362
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700363 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800364 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400365 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700366 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700367 }
368
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700369 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700370 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700371 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400372 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700373 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700374 }
375
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700376 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400377 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700378 }
379
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700380 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400381 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700382 }
383
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000384 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
385 // Hwaddress and Address are set before, so we can check them here
386 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
387 if s.Address == nil && s.Hwaddress == nil {
388 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
389 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400390 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000391 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700392 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800393 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400394 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800395 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700396 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000397
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000398 if len(globalSanitizers) > 0 {
399 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
400 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700401
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700402 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700403 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700404 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400405 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700406 }
407
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700408 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
409 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400410 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700411 }
412
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800413 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
414 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400415 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800416 }
417
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700418 if len(globalSanitizersDiag) > 0 {
419 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
420 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700421 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700422
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800423 // Enable Memtag for all components in the include paths (for Aarch64 only)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800424 if ctx.Arch().ArchType == android.Arm64 {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800425 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800426 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400427 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800428 }
429 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400430 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800431 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800432 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800433 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400434 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800435 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800436 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700437 }
438
Elvis Chien9c993542021-06-25 01:15:17 +0800439 // Enable CFI for non-host components in the include paths
440 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400441 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000442 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400443 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700444 }
445 }
446
Elliott Hughesda3a0712020-03-06 16:55:28 -0800447 // Is CFI actually enabled?
448 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400449 s.Cfi = nil
450 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800451 }
452
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700453 // HWASan requires AArch64 hardware feature (top-byte-ignore).
454 if ctx.Arch().ArchType != android.Arm64 {
455 s.Hwaddress = nil
456 }
457
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800458 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800459 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800460 s.Scs = nil
461 }
462
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700463 // memtag_heap is only implemented on AArch64.
464 if ctx.Arch().ArchType != android.Arm64 {
465 s.Memtag_heap = nil
466 }
467
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700468 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700469 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400470 s.Cfi = nil
471 s.Diag.Cfi = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700472 }
473
Colin Cross528d67e2021-07-23 22:23:07 +0000474 // Disable sanitizers that depend on the UBSan runtime for windows/darwin/musl builds.
475 if !ctx.Os().Linux() || ctx.Os() == android.LinuxMusl {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400476 s.Cfi = nil
477 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700478 s.Misc_undefined = nil
479 s.Undefined = nil
480 s.All_undefined = nil
481 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800482 }
483
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700484 // Also disable CFI for VNDK variants of components
485 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900486 if ctx.static() {
487 // Cfi variant for static vndk should be captured as vendor snapshot,
488 // so don't strictly disable Cfi.
489 s.Cfi = nil
490 s.Diag.Cfi = nil
491 } else {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400492 s.Cfi = nil
493 s.Diag.Cfi = nil
Inseob Kimc42f2f22020-07-29 20:32:10 +0900494 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900495 }
496
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700497 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700498 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
499 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700500 s.Hwaddress = nil
501 }
502
Colin Cross3c344ef2016-07-18 15:44:56 -0700503 if ctx.staticBinary() {
504 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700505 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700506 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800507 }
508
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700509 if Bool(s.All_undefined) {
510 s.Undefined = nil
511 }
512
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700513 if !ctx.toolchain().Is64Bit() {
514 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700515 s.Thread = nil
516 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800517 // TODO(ccross): error for compile_multilib = "32"?
518 }
519
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800520 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 -0700521 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 -0700522 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700523 sanitize.Properties.SanitizerEnabled = true
524 }
525
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800526 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
527 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700528 s.Scudo = nil
529 }
530
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700531 if Bool(s.Hwaddress) {
532 s.Address = nil
533 s.Thread = nil
Evgenii Stepanovb15a5642021-06-23 12:30:55 -0700534 // Disable ubsan diagnosic as a workaround for a compiler bug.
535 // TODO(b/191808836): re-enable.
536 s.Diag.Undefined = nil
537 s.Diag.Integer_overflow = nil
538 s.Diag.Misc_undefined = nil
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700539 }
540
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700541 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
542 // mutually incompatible.
543 if Bool(s.Fuzzer) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400544 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800545 }
546}
547
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800548func toDisableImplicitIntegerChange(flags []string) bool {
549 // Returns true if any flag is fsanitize*integer, and there is
550 // no explicit flag about sanitize=implicit-integer-sign-change.
551 for _, f := range flags {
552 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
553 return false
554 }
555 }
556 for _, f := range flags {
557 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
558 return true
559 }
560 }
561 return false
562}
563
Yabin Cuidb7dda82020-11-30 15:47:45 -0800564func toDisableUnsignedShiftBaseChange(flags []string) bool {
565 // Returns true if any flag is fsanitize*integer, and there is
566 // no explicit flag about sanitize=unsigned-shift-base.
567 for _, f := range flags {
568 if strings.Contains(f, "sanitize=unsigned-shift-base") {
569 return false
570 }
571 }
572 for _, f := range flags {
573 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
574 return true
575 }
576 }
577 return false
578}
579
Colin Cross16b23492016-01-06 14:41:07 -0800580func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700581 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
582 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500583 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
584 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800585
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500586 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800587 flags.Local.LdFlags = append(flags.Local.LdFlags,
588 minimalRuntimePath,
589 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800590 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500591
592 if sanitize.Properties.BuiltinsDep {
593 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
594 }
595
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700596 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800597 return flags
598 }
599
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700600 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700601 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800602 // Frame pointer based unwinder in ASan requires ARM frame setup.
603 // TODO: put in flags?
604 flags.RequiredInstructionSet = "arm"
605 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800606 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
607 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800608
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000609 if Bool(sanitize.Properties.Sanitize.Writeonly) {
610 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
611 }
612
Colin Cross16b23492016-01-06 14:41:07 -0800613 if ctx.Host() {
614 // -nodefaultlibs (provided with libc++) prevents the driver from linking
615 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800616 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800617 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800618 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900619 if ctx.bootstrap() {
620 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
621 } else {
622 flags.DynamicLinker = "/system/bin/linker_asan"
623 }
Colin Cross16b23492016-01-06 14:41:07 -0800624 if flags.Toolchain.Is64Bit() {
625 flags.DynamicLinker += "64"
626 }
627 }
Colin Cross16b23492016-01-06 14:41:07 -0800628 }
629
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700630 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800631 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000632 if Bool(sanitize.Properties.Sanitize.Writeonly) {
633 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
634 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700635 }
636
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700637 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800638 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700639
640 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800641 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
642 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
643 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
644 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700645
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700646 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
647 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
648 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800649 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
650 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700651
Mitch Phillips74384752019-06-17 10:33:52 -0700652 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800653 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700654
655 // Disable fortify for fuzzing builds. Generally, we'll be building with
656 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800657 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800658
659 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
660 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
661 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
662 // the DT_RUNPATH from the shared library above it, and not the executable,
663 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
664 // DT_RUNPATH here means that transient shared libraries can be found
665 // colocated with their parents.
666 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800667 }
668
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700669 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800670 if ctx.Arch().ArchType == android.Arm {
671 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
672 // to do this on a function basis, so force Thumb on the entire module.
673 flags.RequiredInstructionSet = "thumb"
674 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000675
Colin Cross4af21ed2019-11-04 09:37:55 -0800676 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
677 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800678 if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
679 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
680 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000681 // Only append the default visibility flag if -fvisibility has not already been set
682 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800683 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
684 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000685 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800686 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000687
688 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800689 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
690 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000691 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700692 }
693
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700694 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800695 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700696 }
697
Jiyong Park379de2f2018-12-19 02:47:14 +0900698 if len(sanitize.Properties.Sanitizers) > 0 {
699 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800700
Colin Cross4af21ed2019-11-04 09:37:55 -0800701 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
702 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800703 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800704 // Host sanitizers only link symbols in the final executable, so
705 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800706 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
707 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500708
709 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
710 if !ctx.toolchain().Bionic() {
711 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
712 }
713 }
714
715 if enableMinimalRuntime(sanitize) {
716 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
717 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
718 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
719 if !ctx.toolchain().Bionic() {
720 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800721 }
Colin Cross16b23492016-01-06 14:41:07 -0800722 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700723
724 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
725 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800726 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700727 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800728 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700729 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800730 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700731 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800732 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800733 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
734 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800735 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800736 // http://b/171275751, Android doesn't build with this sanitizer yet.
737 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
738 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
739 }
Colin Cross16b23492016-01-06 14:41:07 -0800740 }
741
Jiyong Park379de2f2018-12-19 02:47:14 +0900742 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800743 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700744 }
745 // FIXME: enable RTTI if diag + (cfi or vptr)
746
Andreas Gampe97071162017-05-08 13:15:23 -0700747 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800748 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700749 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
750 }
751
Ivan Lozano7929bba2018-12-12 09:36:31 -0800752 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800753 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800754 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
755 }
756
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700757 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
758 if blocklist.Valid() {
Pirama Arumuga Nainar582fc2d2021-08-27 15:12:56 -0700759 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-ignorelist="+blocklist.String())
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700760 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
761 }
762
Colin Cross16b23492016-01-06 14:41:07 -0800763 return flags
764}
765
Colin Crossd80cbca2020-02-24 12:01:37 -0800766func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900767 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
768 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800769 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900770 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800771 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900772 }
773 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800774 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900775 }
776 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800777 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900778 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800779 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700780}
781
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700782func (sanitize *sanitize) inSanitizerDir() bool {
783 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700784}
785
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500786// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
787func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000788 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500789 case Asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000790 return sanitize.Properties.Sanitize.Address
Tri Vo6eafc362021-04-01 11:29:09 -0700791 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700792 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000793 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000794 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000795 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000796 return sanitize.Properties.Sanitize.Integer_overflow
797 case cfi:
798 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800799 case scs:
800 return sanitize.Properties.Sanitize.Scs
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700801 case memtag_heap:
802 return sanitize.Properties.Sanitize.Memtag_heap
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500803 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700804 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000805 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500806 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000807 }
808}
809
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500810// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800811func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500812 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700813 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800814 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800815 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700816 !sanitize.isSanitizerEnabled(scs) &&
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700817 !sanitize.isSanitizerEnabled(memtag_heap) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500818 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800819}
820
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500821// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700822func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500823 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700824 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700825 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500826 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700827}
828
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500829func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400830 bPtr := proptools.BoolPtr(b)
831 if !b {
832 bPtr = nil
833 }
Colin Cross16b23492016-01-06 14:41:07 -0800834 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500835 case Asan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400836 sanitize.Properties.Sanitize.Address = bPtr
Tri Vo6eafc362021-04-01 11:29:09 -0700837 case Hwasan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400838 sanitize.Properties.Sanitize.Hwaddress = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800839 case tsan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400840 sanitize.Properties.Sanitize.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700841 case intOverflow:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400842 sanitize.Properties.Sanitize.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000843 case cfi:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400844 sanitize.Properties.Sanitize.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800845 case scs:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400846 sanitize.Properties.Sanitize.Scs = bPtr
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700847 case memtag_heap:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400848 sanitize.Properties.Sanitize.Memtag_heap = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500849 case Fuzzer:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400850 sanitize.Properties.Sanitize.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800851 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500852 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800853 }
854 if b {
855 sanitize.Properties.SanitizerEnabled = true
856 }
857}
858
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000859// Check if the sanitizer is explicitly disabled (as opposed to nil by
860// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500861func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000862 if sanitize == nil {
863 return false
864 }
865
866 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
867 return sanitizerVal != nil && *sanitizerVal == false
868}
869
870// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
871// because enabling a sanitizer either directly (via the blueprint) or
872// indirectly (via a mutator) sets the bool ptr to true, and you can't
873// distinguish between the cases. It isn't needed though - both cases can be
874// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500875func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000876 if sanitize == nil {
877 return false
878 }
879
880 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
881 return sanitizerVal != nil && *sanitizerVal == true
882}
883
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500884// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
885func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700886 switch t := tag.(type) {
887 case dependencyTag:
888 return t == reuseObjTag || t == objDepTag
889 case libraryDependencyTag:
890 return true
891 default:
892 return false
893 }
Colin Cross6b753602018-06-21 13:03:07 -0700894}
895
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500896func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
897 return IsSanitizableDependencyTag
898}
899
Inseob Kimc42f2f22020-07-29 20:32:10 +0900900// Determines if the current module is a static library going to be captured
901// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
902// except for ones which explicitly disable cfi.
903func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
Kiyoung Kim48f37782021-07-07 12:42:39 +0900904 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900905 return false
906 }
907
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500908 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900909
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500910 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900911 return false
912 }
913
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500914 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900915 return false
916 }
917
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500918 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900919 return false
920 }
921
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500922 if !c.SanitizerSupported(cfi) {
923 return false
924 }
925
926 return c.SanitizePropDefined() &&
927 !c.SanitizeNever() &&
928 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900929}
930
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700931// Propagate sanitizer requirements down from binaries
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500932func sanitizerDepsMutator(t SanitizerType) func(android.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700933 return func(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500934 if c, ok := mctx.Module().(PlatformSanitizeable); ok {
935 enabled := c.IsSanitizerEnabled(t)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900936 if t == cfi && needsCfiForVendorSnapshot(mctx) {
937 // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly
938 // determine defaultVariation in sanitizerMutator below.
939 // Instead, just mark SanitizeDep to forcefully create cfi variant.
940 enabled = true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500941 c.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900942 }
943 if enabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500944 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Inseob Kimc42f2f22020-07-29 20:32:10 +0900945 mctx.WalkDeps(func(child, parent android.Module) bool {
946 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
947 return false
948 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500949 if d, ok := child.(PlatformSanitizeable); ok && d.SanitizePropDefined() &&
950 !d.SanitizeNever() &&
951 !d.IsSanitizerExplicitlyDisabled(t) {
Colin Crossaf98f582021-05-12 17:27:32 -0700952 if t == cfi || t == Hwasan || t == scs || t == Asan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500953 if d.StaticallyLinked() && d.SanitizerSupported(t) {
954 // Rust does not support some of these sanitizers, so we need to check if it's
955 // supported before setting this true.
956 d.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900957 }
958 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500959 d.SetSanitizeDep(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700960 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000961 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900962 return true
963 })
964 }
Jiyong Parkf97782b2019-02-13 20:28:58 +0900965 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
966 // If an APEX module includes a lib which is enabled for a sanitizer T, then
967 // the APEX module is also enabled for the same sanitizer type.
968 mctx.VisitDirectDeps(func(child android.Module) {
969 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
970 sanitizeable.EnableSanitizer(t.name())
971 }
972 })
Colin Cross16b23492016-01-06 14:41:07 -0800973 }
974 }
975}
976
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500977func (c *Module) SanitizeNever() bool {
978 return Bool(c.sanitize.Properties.Sanitize.Never)
979}
980
981func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
982 return c.sanitize.isSanitizerExplicitlyDisabled(t)
983}
984
Ivan Lozano30c5db22018-02-21 15:49:20 -0800985// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700986func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500987 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -0700988 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500989 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -0700990 mctx.WalkDeps(func(child, parent android.Module) bool {
991 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
992 return false
993 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800994
Inseob Kimeec88e12020-01-22 11:11:29 +0900995 d, ok := child.(*Module)
996 if !ok || !d.static() {
997 return false
998 }
999 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001000 if enableMinimalRuntime(d.sanitize) {
1001 // If a static dependency is built with the minimal runtime,
1002 // make sure we include the ubsan minimal runtime.
1003 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001004 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001005 // If a static dependency runs with full ubsan diagnostics,
1006 // make sure we include the ubsan runtime.
1007 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001008 }
Colin Cross0b908332019-06-19 23:00:20 -07001009
1010 if c.sanitize.Properties.MinimalRuntimeDep &&
1011 c.sanitize.Properties.UbsanRuntimeDep {
1012 // both flags that this mutator might set are true, so don't bother recursing
1013 return false
1014 }
1015
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001016 if c.Os() == android.Linux {
1017 c.sanitize.Properties.BuiltinsDep = true
1018 }
1019
Colin Cross0b908332019-06-19 23:00:20 -07001020 return true
Colin Cross6b753602018-06-21 13:03:07 -07001021 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001022
Jose Galmesf7294582020-11-13 12:07:36 -08001023 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001024 if Bool(p.properties.Sanitize_minimal_dep) {
1025 c.sanitize.Properties.MinimalRuntimeDep = true
1026 }
1027 if Bool(p.properties.Sanitize_ubsan_dep) {
1028 c.sanitize.Properties.UbsanRuntimeDep = true
1029 }
1030 }
1031
1032 return false
Colin Cross6b753602018-06-21 13:03:07 -07001033 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001034 }
1035}
1036
Jiyong Park379de2f2018-12-19 02:47:14 +09001037// Add the dependency to the runtime library for each of the sanitizer variants
1038func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001039 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001040 if !c.Enabled() {
1041 return
1042 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001043 var sanitizers []string
1044 var diagSanitizers []string
1045
1046 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
1047 sanitizers = append(sanitizers, "undefined")
1048 } else {
1049 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
1050 sanitizers = append(sanitizers,
1051 "bool",
1052 "integer-divide-by-zero",
1053 "return",
1054 "returns-nonnull-attribute",
1055 "shift-exponent",
1056 "unreachable",
1057 "vla-bound",
1058 // TODO(danalbert): The following checks currently have compiler performance issues.
1059 //"alignment",
1060 //"bounds",
1061 //"enum",
1062 //"float-cast-overflow",
1063 //"float-divide-by-zero",
1064 //"nonnull-attribute",
1065 //"null",
1066 //"shift-base",
1067 //"signed-integer-overflow",
1068 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1069 // https://llvm.org/PR19302
1070 // http://reviews.llvm.org/D6974
1071 // "object-size",
1072 )
1073 }
1074 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
1075 }
1076
1077 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
1078 diagSanitizers = append(diagSanitizers, "undefined")
1079 }
1080
1081 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
1082
1083 if Bool(c.sanitize.Properties.Sanitize.Address) {
1084 sanitizers = append(sanitizers, "address")
1085 diagSanitizers = append(diagSanitizers, "address")
1086 }
1087
1088 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1089 sanitizers = append(sanitizers, "hwaddress")
1090 }
1091
1092 if Bool(c.sanitize.Properties.Sanitize.Thread) {
1093 sanitizers = append(sanitizers, "thread")
1094 }
1095
1096 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
1097 sanitizers = append(sanitizers, "safe-stack")
1098 }
1099
1100 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
1101 sanitizers = append(sanitizers, "cfi")
1102
1103 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
1104 diagSanitizers = append(diagSanitizers, "cfi")
1105 }
1106 }
1107
1108 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
1109 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1110 sanitizers = append(sanitizers, "signed-integer-overflow")
1111 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
1112 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1113 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1114 }
1115 }
1116
1117 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1118 sanitizers = append(sanitizers, "scudo")
1119 }
1120
1121 if Bool(c.sanitize.Properties.Sanitize.Scs) {
1122 sanitizers = append(sanitizers, "shadow-call-stack")
1123 }
1124
Ivan Lozanod7586b62021-04-01 09:49:36 -04001125 if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.Binary() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001126 noteDep := "note_memtag_heap_async"
1127 if Bool(c.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
1128 noteDep = "note_memtag_heap_sync"
1129 }
Inseob Kim253f5212021-04-08 17:10:31 +09001130 // If we're using snapshots, redirect to snapshot whenever possible
1131 // TODO(b/178470649): clean manual snapshot redirections
1132 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1133 if lib, ok := snapshot.StaticLibs[noteDep]; ok {
1134 noteDep = lib
1135 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001136 depTag := libraryDependencyTag{Kind: staticLibraryDependency, wholeStatic: true}
1137 variations := append(mctx.Target().Variations(),
1138 blueprint.Variation{Mutator: "link", Variation: "static"})
1139 if c.Device() {
1140 variations = append(variations, c.ImageVariation())
1141 }
1142 mctx.AddFarVariationDependencies(variations, depTag, noteDep)
1143 }
1144
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001145 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
1146 sanitizers = append(sanitizers, "fuzzer-no-link")
1147 }
1148
Jiyong Park379de2f2018-12-19 02:47:14 +09001149 // Save the list of sanitizers. These will be used again when generating
1150 // the build rules (for Cflags, etc.)
1151 c.sanitize.Properties.Sanitizers = sanitizers
1152 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1153
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001154 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1155 if c.Host() {
1156 diagSanitizers = sanitizers
1157 }
1158
Jiyong Park379de2f2018-12-19 02:47:14 +09001159 // Determine the runtime library required
1160 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001161 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001162 toolchain := c.toolchain(mctx)
1163 if Bool(c.sanitize.Properties.Sanitize.Address) {
1164 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1165 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1166 if c.staticBinary() {
1167 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001168 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001169 } else {
1170 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1171 }
1172 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
1173 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1174 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1175 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1176 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1177 } else {
1178 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1179 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001180 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001181 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
1182 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
1183 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001184 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
Colin Cross32f1de32021-03-29 13:41:37 -07001185 if c.staticBinary() {
1186 runtimeLibrary += ".static"
1187 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001188 }
1189
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001190 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
1191 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001192
1193 // Adding dependency to the runtime library. We are using *FarVariation*
1194 // because the runtime libraries themselves are not mutated by sanitizer
1195 // mutators and thus don't have sanitizer variants whereas this module
1196 // has been already mutated.
1197 //
1198 // Note that by adding dependency with {static|shared}DepTag, the lib is
1199 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
1200 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001201 deps := append(extraStaticDeps, runtimeLibrary)
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 for idx, dep := range deps {
1205 if lib, ok := snapshot.StaticLibs[dep]; ok {
1206 deps[idx] = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001207 }
1208 }
1209
Jiyong Park379de2f2018-12-19 02:47:14 +09001210 // static executable gets static runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -07001211 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Colin Cross42507332020-08-21 16:15:23 -07001212 variations := append(mctx.Target().Variations(),
1213 blueprint.Variation{Mutator: "link", Variation: "static"})
1214 if c.Device() {
1215 variations = append(variations, c.ImageVariation())
1216 }
1217 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001218 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001219 // If we're using snapshots, redirect to snapshot whenever possible
1220 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1221 if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
1222 runtimeLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001223 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001224
Cindy Zhou18417cb2020-12-10 07:12:38 -08001225 // Skip apex dependency check for sharedLibraryDependency
1226 // when sanitizer diags are enabled. Skipping the check will allow
1227 // building with diag libraries without having to list the
1228 // dependency in Apex's allowed_deps file.
1229 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001230 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001231 depTag := libraryDependencyTag{
1232 Kind: sharedLibraryDependency,
1233 Order: earlyLibraryDependency,
1234
1235 skipApexAllowedDependenciesCheck: diagEnabled,
1236 }
Colin Cross42507332020-08-21 16:15:23 -07001237 variations := append(mctx.Target().Variations(),
1238 blueprint.Variation{Mutator: "link", Variation: "shared"})
1239 if c.Device() {
1240 variations = append(variations, c.ImageVariation())
1241 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001242 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001243 }
1244 // static lib does not have dependency to the runtime library. The
1245 // dependency will be added to the executables or shared libs using
1246 // the static lib.
1247 }
1248 }
1249}
1250
1251type Sanitizeable interface {
1252 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +09001253 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001254 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001255 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001256}
1257
Ivan Lozanod7586b62021-04-01 09:49:36 -04001258func (c *Module) MinimalRuntimeDep() bool {
1259 return c.sanitize.Properties.MinimalRuntimeDep
1260}
1261
1262func (c *Module) UbsanRuntimeDep() bool {
1263 return c.sanitize.Properties.UbsanRuntimeDep
1264}
1265
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001266func (c *Module) SanitizePropDefined() bool {
1267 return c.sanitize != nil
1268}
1269
1270func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1271 return c.sanitize.isSanitizerEnabled(t)
1272}
1273
1274func (c *Module) SanitizeDep() bool {
1275 return c.sanitize.Properties.SanitizeDep
1276}
1277
1278func (c *Module) StaticallyLinked() bool {
1279 return c.static()
1280}
1281
1282func (c *Module) SetInSanitizerDir() {
1283 if c.sanitize != nil {
1284 c.sanitize.Properties.InSanitizerDir = true
1285 }
1286}
1287
1288func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1289 if c.sanitize != nil {
1290 c.sanitize.SetSanitizer(t, b)
1291 }
1292}
1293
1294func (c *Module) SetSanitizeDep(b bool) {
1295 if c.sanitize != nil {
1296 c.sanitize.Properties.SanitizeDep = b
1297 }
1298}
1299
1300var _ PlatformSanitizeable = (*Module)(nil)
1301
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001302// Create sanitized variants for modules that need them
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001303func sanitizerMutator(t SanitizerType) func(android.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -07001304 return func(mctx android.BottomUpMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001305 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
Ivan Lozano5482d6a2021-11-01 10:13:25 -04001306
1307 // Make sure we're not setting CFI to any value if it's not supported.
1308 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
1309
Liz Kammer187d5442021-06-25 14:50:12 -04001310 if c.Binary() && c.IsSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001311 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001312 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1313 } else if c.IsSanitizerEnabled(t) || c.SanitizeDep() {
1314 isSanitizerEnabled := c.IsSanitizerEnabled(t)
Colin Crossaf98f582021-05-12 17:27:32 -07001315 if c.StaticallyLinked() || c.Header() || t == Fuzzer {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001316 // Static and header libs are split into non-sanitized and sanitized variants.
1317 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1318 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1319 // that isn't sanitized for asan/fuzzer.
1320 //
1321 // Note for defaultVariation: since we don't split for shared libs but for static/header
1322 // libs, it is possible for the sanitized variant of a static/header lib to depend
1323 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1324 // error when the module is split. defaultVariation is the name of the variation that
1325 // will be used when such a dangling dependency occurs during the split of the current
1326 // module. By setting it to the name of the sanitized variation, the dangling dependency
1327 // is redirected to the sanitized variant of the dependent module.
1328 defaultVariation := t.variationName()
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001329 // Not all PlatformSanitizeable modules support the CFI sanitizer
Jiyong Park1d1119f2019-07-29 21:27:18 +09001330 mctx.SetDefaultDependencyVariation(&defaultVariation)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001331
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001332 modules := mctx.CreateVariations("", t.variationName())
1333 modules[0].(PlatformSanitizeable).SetSanitizer(t, false)
1334 modules[1].(PlatformSanitizeable).SetSanitizer(t, true)
1335 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
1336 modules[1].(PlatformSanitizeable).SetSanitizeDep(false)
1337
1338 if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
Ivan Lozano4774a812020-03-10 16:23:57 -04001339 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1340 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001341 modules[1].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001342 }
1343
Jiyong Park1d1119f2019-07-29 21:27:18 +09001344 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1345 // to Make, because the sanitized version has a different suffix in name.
1346 // For other types of sanitizers, suppress the variation that is disabled.
Tri Vo6eafc362021-04-01 11:29:09 -07001347 if t != cfi && t != scs && t != Hwasan {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001348 if isSanitizerEnabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001349 modules[0].(PlatformSanitizeable).SetPreventInstall()
1350 modules[0].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001351 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001352 modules[1].(PlatformSanitizeable).SetPreventInstall()
1353 modules[1].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001354 }
1355 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001356
Jiyong Park1d1119f2019-07-29 21:27:18 +09001357 // Export the static lib name to make
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001358 if c.StaticallyLinked() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001359 if t == cfi {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001360 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
Tri Vo6eafc362021-04-01 11:29:09 -07001361 } else if t == Hwasan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001362 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001363 }
1364 }
1365 } else {
1366 // Shared libs are not split. Only the sanitized variant is created.
1367 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001368 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1369 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
Vishwath Mohane7128792017-11-17 11:08:10 -08001370
Jiyong Park1d1119f2019-07-29 21:27:18 +09001371 // locate the asan libraries under /data/asan
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001372 if mctx.Device() && t == Asan && isSanitizerEnabled {
1373 modules[0].(PlatformSanitizeable).SetInSanitizerDir()
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001374 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001375
Ivan Lozano5482d6a2021-11-01 10:13:25 -04001376 if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
Ivan Lozano4774a812020-03-10 16:23:57 -04001377 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1378 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001379 modules[0].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001380 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001381 }
Colin Cross16b23492016-01-06 14:41:07 -08001382 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001383 c.SetSanitizeDep(false)
Jiyong Park82226632019-02-01 10:50:50 +09001384 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001385 // APEX modules fall here
Jooyung Han8ce8db92020-05-15 19:05:05 +09001386 sanitizeable.AddSanitizerDependencies(mctx, t.name())
Jiyong Park82226632019-02-01 10:50:50 +09001387 mctx.CreateVariations(t.variationName())
Inseob Kimc42f2f22020-07-29 20:32:10 +09001388 } else if c, ok := mctx.Module().(*Module); ok {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001389 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1390
Inseob Kimc42f2f22020-07-29 20:32:10 +09001391 // Check if it's a snapshot module supporting sanitizer
1392 if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) {
1393 // Set default variation as above.
1394 defaultVariation := t.variationName()
1395 mctx.SetDefaultDependencyVariation(&defaultVariation)
1396 modules := mctx.CreateVariations("", t.variationName())
1397 modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false)
1398 modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true)
1399
1400 // Export the static lib name to make
1401 if c.static() && c.ExportedToMake() {
1402 if t == cfi {
1403 // use BaseModuleName which is the name for Make.
1404 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1405 }
1406 }
1407 }
Colin Cross16b23492016-01-06 14:41:07 -08001408 }
1409 }
1410}
Vishwath Mohane7128792017-11-17 11:08:10 -08001411
Inseob Kim74d25562020-08-04 00:41:38 +09001412type sanitizerStaticLibsMap struct {
1413 // libsMap contains one list of modules per each image and each arch.
1414 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001415 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001416 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001417 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001418}
1419
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001420func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001421 return &sanitizerStaticLibsMap{
1422 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001423 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001424 }
1425}
1426
1427// Add the current module to sanitizer static libs maps
1428// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001429func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1430 image := GetImageVariantType(c)
1431 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001432
1433 s.libsMapLock.Lock()
1434 defer s.libsMapLock.Unlock()
1435
1436 if _, ok := s.libsMap[image]; !ok {
1437 s.libsMap[image] = make(map[string][]string)
1438 }
1439
1440 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1441}
1442
1443// Exports makefile variables in the following format:
1444// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1445// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1446// These are to be used by use_soong_sanitized_static_libraries.
1447// See build/make/core/binary.mk for more details.
1448func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1449 for _, image := range android.SortedStringKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001450 archMap := s.libsMap[ImageVariantType(image)]
Inseob Kim74d25562020-08-04 00:41:38 +09001451 for _, arch := range android.SortedStringKeys(archMap) {
1452 libs := archMap[arch]
1453 sort.Strings(libs)
1454
1455 key := fmt.Sprintf(
1456 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1457 s.sanitizerType.variationName(),
1458 image, // already upper
1459 arch)
1460
1461 ctx.Strict(key, strings.Join(libs, " "))
1462 }
1463 }
1464}
1465
Colin Cross571cccf2019-02-04 11:22:08 -08001466var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1467
Inseob Kim74d25562020-08-04 00:41:38 +09001468func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001469 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001470 return newSanitizerStaticLibsMap(cfi)
1471 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001472}
1473
Colin Cross571cccf2019-02-04 11:22:08 -08001474var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1475
Inseob Kim74d25562020-08-04 00:41:38 +09001476func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001477 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001478 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001479 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001480}
1481
Ivan Lozano30c5db22018-02-21 15:49:20 -08001482func enableMinimalRuntime(sanitize *sanitize) bool {
1483 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001484 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001485 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001486
Ivan Lozano30c5db22018-02-21 15:49:20 -08001487 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001488 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1489 Bool(sanitize.Properties.Sanitize.Undefined) ||
1490 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1491
Ivan Lozano30c5db22018-02-21 15:49:20 -08001492 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1493 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001494 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001495 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001496
Ivan Lozano30c5db22018-02-21 15:49:20 -08001497 return true
1498 }
1499 return false
1500}
1501
Ivan Lozanod7586b62021-04-01 09:49:36 -04001502func (m *Module) UbsanRuntimeNeeded() bool {
1503 return enableUbsanRuntime(m.sanitize)
1504}
1505
1506func (m *Module) MinimalRuntimeNeeded() bool {
1507 return enableMinimalRuntime(m.sanitize)
1508}
1509
Inseob Kim8471cda2019-11-15 09:59:12 +09001510func enableUbsanRuntime(sanitize *sanitize) bool {
1511 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001512 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001513 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1514}
1515
Vishwath Mohane7128792017-11-17 11:08:10 -08001516func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001517 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001518}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001519
1520func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001521 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001522}