blob: dd15ae13dd919b9cf163c24c050868d204cf9f61 [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",
44 "-fno-experimental-new-pass-manager",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080045 // The following improves debug location information
46 // availability at the cost of its accuracy. It increases
47 // the likelihood of a stack variable's frame offset
48 // to be recorded in the debug info, which is important
49 // for the quality of hwasan reports. The downside is a
50 // higher number of "optimized out" stack variables.
51 // b/112437883.
52 "-mllvm", "-instcombine-lower-dbg-declare=0",
Mitch Phillipsb1c574f2020-06-22 13:28:23 -070053 // TODO(b/159343917): HWASan and GlobalISel don't play nicely, and
54 // GlobalISel is the default at -O0 on aarch64.
55 "-mllvm", "--aarch64-enable-global-isel-at-O=-1",
56 "-mllvm", "-fast-isel=false",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080057 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070058
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000059 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Pirama Arumuga Nainareb8d4032020-07-27 11:22:35 -070060 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blocklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070061 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
62 // used, but have no effect on assembly files
63 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070064 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070065 "-Wl,-plugin-opt,O1"}
Inseob Kim74d25562020-08-04 00:41:38 +090066 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070067
Pirama Arumuga Nainareb8d4032020-07-27 11:22:35 -070068 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080069
Peter Collingbournebd19db02019-03-06 10:38:48 -080070 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070071 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070072 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
73 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070074)
75
Ivan Lozano3968d8f2020-12-14 11:27:52 -050076type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080077
Colin Cross16b23492016-01-06 14:41:07 -080078const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050079 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070080 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080081 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070082 intOverflow
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
Liz Kammer75db9312021-07-07 16:41:50 -040086 cfi // cfi is last to prevent it running before incompatible mutators
Colin Cross16b23492016-01-06 14:41:07 -080087)
88
Liz Kammer75db9312021-07-07 16:41:50 -040089var Sanitizers = []SanitizerType{
90 Asan,
91 Hwasan,
92 tsan,
93 intOverflow,
94 scs,
95 Fuzzer,
96 memtag_heap,
97 cfi, // cfi is last to prevent it running before incompatible mutators
98}
99
Jiyong Park82226632019-02-01 10:50:50 +0900100// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500101func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -0800102 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500103 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -0800104 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -0700105 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700106 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800107 case tsan:
108 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700109 case intOverflow:
110 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000111 case cfi:
112 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800113 case scs:
114 return "scs"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700115 case memtag_heap:
116 return "memtag_heap"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500117 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700118 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800119 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500120 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800121 }
122}
123
Jiyong Park82226632019-02-01 10:50:50 +0900124// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500125func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900126 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500127 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900128 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700129 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900130 return "hwaddress"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700131 case memtag_heap:
132 return "memtag_heap"
Jiyong Park82226632019-02-01 10:50:50 +0900133 case tsan:
134 return "thread"
135 case intOverflow:
136 return "integer_overflow"
137 case cfi:
138 return "cfi"
139 case scs:
140 return "shadow-call-stack"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500141 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700142 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900143 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500144 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900145 }
146}
147
Liz Kammer75db9312021-07-07 16:41:50 -0400148func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
149 switch t {
150 case Asan, Hwasan, Fuzzer, scs, tsan, cfi:
151 ctx.TopDown(t.variationName()+"_deps", sanitizerDepsMutator(t))
152 ctx.BottomUp(t.variationName(), sanitizerMutator(t))
153 case memtag_heap, intOverflow:
154 // do nothing
155 default:
156 panic(fmt.Errorf("unknown SanitizerType %d", t))
157 }
158}
159
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500160func (*Module) SanitizerSupported(t SanitizerType) bool {
161 switch t {
162 case Asan:
163 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700164 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500165 return true
166 case tsan:
167 return true
168 case intOverflow:
169 return true
170 case cfi:
171 return true
172 case scs:
173 return true
174 case Fuzzer:
175 return true
176 default:
177 return false
178 }
179}
180
181// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
182func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700183 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900184}
185
Martin Stjernholmb0249572020-09-15 02:32:35 +0100186type SanitizeUserProps struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400187 // Prevent use of any sanitizers on this module
Martin Stjernholmb0249572020-09-15 02:32:35 +0100188 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800189
Liz Kammer75b9b402021-06-25 15:19:27 -0400190 // ASan (Address sanitizer), incompatible with static binaries.
191 // Always runs in a diagnostic mode.
192 // Use of address sanitizer disables cfi sanitizer.
193 // Hwaddress sanitizer takes precedence over this sanitizer.
194 Address *bool `android:"arch_variant"`
195 // TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
196 // Always runs in a diagnostic mode.
197 // Use of thread sanitizer disables cfi and scudo sanitizers.
198 // Hwaddress sanitizer takes precedence over this sanitizer.
199 Thread *bool `android:"arch_variant"`
200 // HWASan (Hardware Address sanitizer).
201 // Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
Martin Stjernholmb0249572020-09-15 02:32:35 +0100202 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800203
Liz Kammer75b9b402021-06-25 15:19:27 -0400204 // Undefined behavior sanitizer
205 All_undefined *bool `android:"arch_variant"`
206 // Subset of undefined behavior sanitizer
207 Undefined *bool `android:"arch_variant"`
208 // List of specific undefined behavior sanitizers to enable
209 Misc_undefined []string `android:"arch_variant"`
210 // Fuzzer, incompatible with static binaries.
211 Fuzzer *bool `android:"arch_variant"`
212 // safe-stack sanitizer, incompatible with 32-bit architectures.
213 Safestack *bool `android:"arch_variant"`
214 // cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
215 Cfi *bool `android:"arch_variant"`
216 // signed/unsigned integer overflow sanitizer, incompatible with Darwin.
217 Integer_overflow *bool `android:"arch_variant"`
218 // scudo sanitizer, incompatible with asan, hwasan, tsan
219 // This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
220 // deprecated
221 Scudo *bool `android:"arch_variant"`
222 // shadow-call-stack sanitizer, only available on arm64
223 Scs *bool `android:"arch_variant"`
224 // Memory-tagging, only available on arm64
225 // if diag.memtag unset or false, enables async memory tagging
226 Memtag_heap *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100227
228 // A modifier for ASAN and HWASAN for write only instrumentation
229 Writeonly *bool `android:"arch_variant"`
230
231 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
232 // Replaces abort() on error with a human-readable error message.
233 // Address and Thread sanitizers always run in diagnostic mode.
234 Diag struct {
Liz Kammer75b9b402021-06-25 15:19:27 -0400235 // Undefined behavior sanitizer, diagnostic mode
236 Undefined *bool `android:"arch_variant"`
237 // cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
238 Cfi *bool `android:"arch_variant"`
239 // signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
240 Integer_overflow *bool `android:"arch_variant"`
241 // Memory-tagging, only available on arm64
242 // requires sanitizer.memtag: true
243 // if set, enables sync memory tagging
244 Memtag_heap *bool `android:"arch_variant"`
245 // List of specific undefined behavior sanitizers to enable in diagnostic mode
246 Misc_undefined []string `android:"arch_variant"`
247 // List of sanitizers to pass to -fno-sanitize-recover
248 // results in only the first detected error for these sanitizers being reported and program then
249 // exits with a non-zero exit code.
250 No_recover []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800251 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800252
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800253 // Sanitizers to run with flag configuration specified
254 Config struct {
255 // Enables CFI support flags for assembly-heavy libraries
256 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800257 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800258
Liz Kammer75b9b402021-06-25 15:19:27 -0400259 // List of sanitizers to pass to -fsanitize-recover
260 // allows execution to continue for these sanitizers to detect multiple errors rather than only
261 // the first one
Martin Stjernholmb0249572020-09-15 02:32:35 +0100262 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000263
Martin Stjernholmb0249572020-09-15 02:32:35 +0100264 // value to pass to -fsanitize-blacklist
265 Blocklist *string
266}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700267
Martin Stjernholmb0249572020-09-15 02:32:35 +0100268type SanitizeProperties struct {
Martin Stjernholmb0249572020-09-15 02:32:35 +0100269 Sanitize SanitizeUserProps `android:"arch_variant"`
270 SanitizerEnabled bool `blueprint:"mutated"`
271 SanitizeDep bool `blueprint:"mutated"`
272 MinimalRuntimeDep bool `blueprint:"mutated"`
273 BuiltinsDep bool `blueprint:"mutated"`
274 UbsanRuntimeDep bool `blueprint:"mutated"`
275 InSanitizerDir bool `blueprint:"mutated"`
276 Sanitizers []string `blueprint:"mutated"`
277 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800278}
279
280type sanitize struct {
281 Properties SanitizeProperties
282}
283
Cindy Zhou18417cb2020-12-10 07:12:38 -0800284// Mark this tag with a check to see if apex dependency check should be skipped
285func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
286 return t.skipApexAllowedDependenciesCheck
287}
288
289var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
290
Vishwath Mohane7128792017-11-17 11:08:10 -0800291func init() {
292 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700293 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800294}
295
Colin Cross16b23492016-01-06 14:41:07 -0800296func (sanitize *sanitize) props() []interface{} {
297 return []interface{}{&sanitize.Properties}
298}
299
300func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700301 s := &sanitize.Properties.Sanitize
302
Colin Cross16b23492016-01-06 14:41:07 -0800303 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700304 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800305 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800306 }
307
308 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800309 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800310 return
311 }
312
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800313 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap}).
Liz Kammer7b920b42021-06-22 16:57:27 -0400314 if ctx.testBinary() {
315 if s.Memtag_heap == nil {
316 s.Memtag_heap = proptools.BoolPtr(true)
317 }
318 if s.Diag.Memtag_heap == nil {
319 s.Diag.Memtag_heap = proptools.BoolPtr(true)
320 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800321 }
322
Colin Cross16b23492016-01-06 14:41:07 -0800323 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700324 var globalSanitizersDiag []string
325
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700326 if ctx.Host() {
327 if !ctx.Windows() {
328 globalSanitizers = ctx.Config().SanitizeHost()
329 }
330 } else {
331 arches := ctx.Config().SanitizeDeviceArch()
332 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
333 globalSanitizers = ctx.Config().SanitizeDevice()
334 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800335 }
336 }
337
Colin Cross16b23492016-01-06 14:41:07 -0800338 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000339 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700340 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400341 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000342 }
Colin Cross16b23492016-01-06 14:41:07 -0800343
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700344 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400345 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000346 }
347
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700348 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400349 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000350 }
351
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700352 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400353 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000354 }
355
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700356 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400357 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700358 }
359
360 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400361 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000362 }
363
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700364 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800365 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400366 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700367 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700368 }
369
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700370 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700371 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700372 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400373 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700374 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700375 }
376
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700377 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400378 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700379 }
380
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700381 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400382 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700383 }
384
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000385 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
386 // Hwaddress and Address are set before, so we can check them here
387 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
388 if s.Address == nil && s.Hwaddress == nil {
389 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
390 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400391 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000392 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700393 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800394 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400395 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800396 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700397 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000398
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000399 if len(globalSanitizers) > 0 {
400 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
401 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700402
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700403 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700404 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700405 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400406 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700407 }
408
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700409 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
410 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400411 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700412 }
413
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800414 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
415 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400416 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800417 }
418
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700419 if len(globalSanitizersDiag) > 0 {
420 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
421 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700422 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700423
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800424 // Enable Memtag for all components in the include paths (for Aarch64 only)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800425 if ctx.Arch().ArchType == android.Arm64 {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800426 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800427 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400428 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800429 }
430 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400431 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800432 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800433 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800434 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400435 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800436 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800437 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700438 }
439
Elvis Chien9c993542021-06-25 01:15:17 +0800440 // Enable CFI for non-host components in the include paths
441 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400442 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000443 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400444 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700445 }
446 }
447
Elliott Hughesda3a0712020-03-06 16:55:28 -0800448 // Is CFI actually enabled?
449 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400450 s.Cfi = nil
451 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800452 }
453
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700454 // HWASan requires AArch64 hardware feature (top-byte-ignore).
455 if ctx.Arch().ArchType != android.Arm64 {
456 s.Hwaddress = nil
457 }
458
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800459 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800460 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800461 s.Scs = nil
462 }
463
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700464 // memtag_heap is only implemented on AArch64.
465 if ctx.Arch().ArchType != android.Arm64 {
466 s.Memtag_heap = nil
467 }
468
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700469 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700470 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400471 s.Cfi = nil
472 s.Diag.Cfi = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700473 }
474
Colin Cross528d67e2021-07-23 22:23:07 +0000475 // Disable sanitizers that depend on the UBSan runtime for windows/darwin/musl builds.
476 if !ctx.Os().Linux() || ctx.Os() == android.LinuxMusl {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400477 s.Cfi = nil
478 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700479 s.Misc_undefined = nil
480 s.Undefined = nil
481 s.All_undefined = nil
482 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800483 }
484
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700485 // Also disable CFI for VNDK variants of components
486 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900487 if ctx.static() {
488 // Cfi variant for static vndk should be captured as vendor snapshot,
489 // so don't strictly disable Cfi.
490 s.Cfi = nil
491 s.Diag.Cfi = nil
492 } else {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400493 s.Cfi = nil
494 s.Diag.Cfi = nil
Inseob Kimc42f2f22020-07-29 20:32:10 +0900495 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900496 }
497
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700498 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700499 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
500 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700501 s.Hwaddress = nil
502 }
503
Colin Cross3c344ef2016-07-18 15:44:56 -0700504 if ctx.staticBinary() {
505 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700506 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700507 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800508 }
509
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700510 if Bool(s.All_undefined) {
511 s.Undefined = nil
512 }
513
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700514 if !ctx.toolchain().Is64Bit() {
515 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700516 s.Thread = nil
517 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800518 // TODO(ccross): error for compile_multilib = "32"?
519 }
520
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800521 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 -0700522 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 -0700523 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700524 sanitize.Properties.SanitizerEnabled = true
525 }
526
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800527 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
528 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700529 s.Scudo = nil
530 }
531
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700532 if Bool(s.Hwaddress) {
533 s.Address = nil
534 s.Thread = nil
Evgenii Stepanovb15a5642021-06-23 12:30:55 -0700535 // Disable ubsan diagnosic as a workaround for a compiler bug.
536 // TODO(b/191808836): re-enable.
537 s.Diag.Undefined = nil
538 s.Diag.Integer_overflow = nil
539 s.Diag.Misc_undefined = nil
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700540 }
541
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700542 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
543 // mutually incompatible.
544 if Bool(s.Fuzzer) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400545 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800546 }
547}
548
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800549func toDisableImplicitIntegerChange(flags []string) bool {
550 // Returns true if any flag is fsanitize*integer, and there is
551 // no explicit flag about sanitize=implicit-integer-sign-change.
552 for _, f := range flags {
553 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
554 return false
555 }
556 }
557 for _, f := range flags {
558 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
559 return true
560 }
561 }
562 return false
563}
564
Yabin Cuidb7dda82020-11-30 15:47:45 -0800565func toDisableUnsignedShiftBaseChange(flags []string) bool {
566 // Returns true if any flag is fsanitize*integer, and there is
567 // no explicit flag about sanitize=unsigned-shift-base.
568 for _, f := range flags {
569 if strings.Contains(f, "sanitize=unsigned-shift-base") {
570 return false
571 }
572 }
573 for _, f := range flags {
574 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
575 return true
576 }
577 }
578 return false
579}
580
Colin Cross16b23492016-01-06 14:41:07 -0800581func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700582 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
583 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500584 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
585 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800586
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500587 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800588 flags.Local.LdFlags = append(flags.Local.LdFlags,
589 minimalRuntimePath,
590 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800591 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500592
593 if sanitize.Properties.BuiltinsDep {
594 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
595 }
596
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700597 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800598 return flags
599 }
600
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700601 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700602 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800603 // Frame pointer based unwinder in ASan requires ARM frame setup.
604 // TODO: put in flags?
605 flags.RequiredInstructionSet = "arm"
606 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800607 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
608 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800609
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000610 if Bool(sanitize.Properties.Sanitize.Writeonly) {
611 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
612 }
613
Colin Cross16b23492016-01-06 14:41:07 -0800614 if ctx.Host() {
615 // -nodefaultlibs (provided with libc++) prevents the driver from linking
616 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800617 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800618 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800619 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900620 if ctx.bootstrap() {
621 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
622 } else {
623 flags.DynamicLinker = "/system/bin/linker_asan"
624 }
Colin Cross16b23492016-01-06 14:41:07 -0800625 if flags.Toolchain.Is64Bit() {
626 flags.DynamicLinker += "64"
627 }
628 }
Colin Cross16b23492016-01-06 14:41:07 -0800629 }
630
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700631 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800632 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000633 if Bool(sanitize.Properties.Sanitize.Writeonly) {
634 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
635 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700636 }
637
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700638 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800639 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700640
641 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800642 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
643 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
644 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
645 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700646
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700647 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
648 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
649 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800650 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
651 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700652
Mitch Phillips74384752019-06-17 10:33:52 -0700653 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800654 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700655
656 // Disable fortify for fuzzing builds. Generally, we'll be building with
657 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800658 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800659
660 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
661 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
662 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
663 // the DT_RUNPATH from the shared library above it, and not the executable,
664 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
665 // DT_RUNPATH here means that transient shared libraries can be found
666 // colocated with their parents.
667 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800668 }
669
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700670 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800671 if ctx.Arch().ArchType == android.Arm {
672 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
673 // to do this on a function basis, so force Thumb on the entire module.
674 flags.RequiredInstructionSet = "thumb"
675 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000676
Colin Cross4af21ed2019-11-04 09:37:55 -0800677 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
678 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800679 if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
680 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
681 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000682 // Only append the default visibility flag if -fvisibility has not already been set
683 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800684 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
685 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000686 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800687 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000688
689 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800690 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
691 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000692 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700693 }
694
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700695 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800696 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700697 }
698
Jiyong Park379de2f2018-12-19 02:47:14 +0900699 if len(sanitize.Properties.Sanitizers) > 0 {
700 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800701
Colin Cross4af21ed2019-11-04 09:37:55 -0800702 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
703 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800704 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800705 // Host sanitizers only link symbols in the final executable, so
706 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800707 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
708 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500709
710 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
711 if !ctx.toolchain().Bionic() {
712 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
713 }
714 }
715
716 if enableMinimalRuntime(sanitize) {
717 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
718 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
719 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
720 if !ctx.toolchain().Bionic() {
721 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800722 }
Colin Cross16b23492016-01-06 14:41:07 -0800723 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700724
725 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
726 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800727 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700728 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800729 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700730 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800731 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700732 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800733 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800734 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
735 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800736 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800737 // http://b/171275751, Android doesn't build with this sanitizer yet.
738 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
739 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
740 }
Colin Cross16b23492016-01-06 14:41:07 -0800741 }
742
Jiyong Park379de2f2018-12-19 02:47:14 +0900743 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800744 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700745 }
746 // FIXME: enable RTTI if diag + (cfi or vptr)
747
Andreas Gampe97071162017-05-08 13:15:23 -0700748 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800749 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700750 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
751 }
752
Ivan Lozano7929bba2018-12-12 09:36:31 -0800753 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800754 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800755 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
756 }
757
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700758 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
759 if blocklist.Valid() {
760 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blocklist.String())
761 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
762 }
763
Colin Cross16b23492016-01-06 14:41:07 -0800764 return flags
765}
766
Colin Crossd80cbca2020-02-24 12:01:37 -0800767func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900768 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
769 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800770 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900771 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800772 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900773 }
774 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800775 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900776 }
777 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800778 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900779 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800780 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700781}
782
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700783func (sanitize *sanitize) inSanitizerDir() bool {
784 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700785}
786
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500787// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
788func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000789 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500790 case Asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000791 return sanitize.Properties.Sanitize.Address
Tri Vo6eafc362021-04-01 11:29:09 -0700792 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700793 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000794 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000795 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000796 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000797 return sanitize.Properties.Sanitize.Integer_overflow
798 case cfi:
799 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800800 case scs:
801 return sanitize.Properties.Sanitize.Scs
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700802 case memtag_heap:
803 return sanitize.Properties.Sanitize.Memtag_heap
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500804 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700805 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000806 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500807 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000808 }
809}
810
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500811// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800812func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500813 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700814 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800815 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800816 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700817 !sanitize.isSanitizerEnabled(scs) &&
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700818 !sanitize.isSanitizerEnabled(memtag_heap) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500819 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800820}
821
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500822// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700823func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500824 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700825 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700826 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500827 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700828}
829
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500830func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400831 bPtr := proptools.BoolPtr(b)
832 if !b {
833 bPtr = nil
834 }
Colin Cross16b23492016-01-06 14:41:07 -0800835 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500836 case Asan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400837 sanitize.Properties.Sanitize.Address = bPtr
Tri Vo6eafc362021-04-01 11:29:09 -0700838 case Hwasan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400839 sanitize.Properties.Sanitize.Hwaddress = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800840 case tsan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400841 sanitize.Properties.Sanitize.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700842 case intOverflow:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400843 sanitize.Properties.Sanitize.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000844 case cfi:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400845 sanitize.Properties.Sanitize.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800846 case scs:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400847 sanitize.Properties.Sanitize.Scs = bPtr
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700848 case memtag_heap:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400849 sanitize.Properties.Sanitize.Memtag_heap = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500850 case Fuzzer:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400851 sanitize.Properties.Sanitize.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800852 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500853 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800854 }
855 if b {
856 sanitize.Properties.SanitizerEnabled = true
857 }
858}
859
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000860// Check if the sanitizer is explicitly disabled (as opposed to nil by
861// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500862func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000863 if sanitize == nil {
864 return false
865 }
866
867 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
868 return sanitizerVal != nil && *sanitizerVal == false
869}
870
871// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
872// because enabling a sanitizer either directly (via the blueprint) or
873// indirectly (via a mutator) sets the bool ptr to true, and you can't
874// distinguish between the cases. It isn't needed though - both cases can be
875// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500876func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000877 if sanitize == nil {
878 return false
879 }
880
881 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
882 return sanitizerVal != nil && *sanitizerVal == true
883}
884
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500885// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
886func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700887 switch t := tag.(type) {
888 case dependencyTag:
889 return t == reuseObjTag || t == objDepTag
890 case libraryDependencyTag:
891 return true
892 default:
893 return false
894 }
Colin Cross6b753602018-06-21 13:03:07 -0700895}
896
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500897func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
898 return IsSanitizableDependencyTag
899}
900
Inseob Kimc42f2f22020-07-29 20:32:10 +0900901// Determines if the current module is a static library going to be captured
902// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
903// except for ones which explicitly disable cfi.
904func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
Kiyoung Kim48f37782021-07-07 12:42:39 +0900905 if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900906 return false
907 }
908
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500909 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900910
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500911 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900912 return false
913 }
914
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500915 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900916 return false
917 }
918
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500919 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900920 return false
921 }
922
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500923 if !c.SanitizerSupported(cfi) {
924 return false
925 }
926
927 return c.SanitizePropDefined() &&
928 !c.SanitizeNever() &&
929 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900930}
931
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700932// Propagate sanitizer requirements down from binaries
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500933func sanitizerDepsMutator(t SanitizerType) func(android.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700934 return func(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500935 if c, ok := mctx.Module().(PlatformSanitizeable); ok {
936 enabled := c.IsSanitizerEnabled(t)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900937 if t == cfi && needsCfiForVendorSnapshot(mctx) {
938 // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly
939 // determine defaultVariation in sanitizerMutator below.
940 // Instead, just mark SanitizeDep to forcefully create cfi variant.
941 enabled = true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500942 c.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900943 }
944 if enabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500945 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Inseob Kimc42f2f22020-07-29 20:32:10 +0900946 mctx.WalkDeps(func(child, parent android.Module) bool {
947 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
948 return false
949 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500950 if d, ok := child.(PlatformSanitizeable); ok && d.SanitizePropDefined() &&
951 !d.SanitizeNever() &&
952 !d.IsSanitizerExplicitlyDisabled(t) {
Colin Crossaf98f582021-05-12 17:27:32 -0700953 if t == cfi || t == Hwasan || t == scs || t == Asan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500954 if d.StaticallyLinked() && d.SanitizerSupported(t) {
955 // Rust does not support some of these sanitizers, so we need to check if it's
956 // supported before setting this true.
957 d.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900958 }
959 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500960 d.SetSanitizeDep(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700961 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000962 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900963 return true
964 })
965 }
Jiyong Parkf97782b2019-02-13 20:28:58 +0900966 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
967 // If an APEX module includes a lib which is enabled for a sanitizer T, then
968 // the APEX module is also enabled for the same sanitizer type.
969 mctx.VisitDirectDeps(func(child android.Module) {
970 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
971 sanitizeable.EnableSanitizer(t.name())
972 }
973 })
Colin Cross16b23492016-01-06 14:41:07 -0800974 }
975 }
976}
977
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500978func (c *Module) SanitizeNever() bool {
979 return Bool(c.sanitize.Properties.Sanitize.Never)
980}
981
982func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
983 return c.sanitize.isSanitizerExplicitlyDisabled(t)
984}
985
Ivan Lozano30c5db22018-02-21 15:49:20 -0800986// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700987func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500988 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -0700989 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500990 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -0700991 mctx.WalkDeps(func(child, parent android.Module) bool {
992 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
993 return false
994 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800995
Inseob Kimeec88e12020-01-22 11:11:29 +0900996 d, ok := child.(*Module)
997 if !ok || !d.static() {
998 return false
999 }
1000 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -07001001 if enableMinimalRuntime(d.sanitize) {
1002 // If a static dependency is built with the minimal runtime,
1003 // make sure we include the ubsan minimal runtime.
1004 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +09001005 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -07001006 // If a static dependency runs with full ubsan diagnostics,
1007 // make sure we include the ubsan runtime.
1008 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -08001009 }
Colin Cross0b908332019-06-19 23:00:20 -07001010
1011 if c.sanitize.Properties.MinimalRuntimeDep &&
1012 c.sanitize.Properties.UbsanRuntimeDep {
1013 // both flags that this mutator might set are true, so don't bother recursing
1014 return false
1015 }
1016
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001017 if c.Os() == android.Linux {
1018 c.sanitize.Properties.BuiltinsDep = true
1019 }
1020
Colin Cross0b908332019-06-19 23:00:20 -07001021 return true
Colin Cross6b753602018-06-21 13:03:07 -07001022 }
Inseob Kimeec88e12020-01-22 11:11:29 +09001023
Jose Galmesf7294582020-11-13 12:07:36 -08001024 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +09001025 if Bool(p.properties.Sanitize_minimal_dep) {
1026 c.sanitize.Properties.MinimalRuntimeDep = true
1027 }
1028 if Bool(p.properties.Sanitize_ubsan_dep) {
1029 c.sanitize.Properties.UbsanRuntimeDep = true
1030 }
1031 }
1032
1033 return false
Colin Cross6b753602018-06-21 13:03:07 -07001034 })
Ivan Lozano30c5db22018-02-21 15:49:20 -08001035 }
1036}
1037
Jiyong Park379de2f2018-12-19 02:47:14 +09001038// Add the dependency to the runtime library for each of the sanitizer variants
1039func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001040 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +00001041 if !c.Enabled() {
1042 return
1043 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001044 var sanitizers []string
1045 var diagSanitizers []string
1046
1047 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
1048 sanitizers = append(sanitizers, "undefined")
1049 } else {
1050 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
1051 sanitizers = append(sanitizers,
1052 "bool",
1053 "integer-divide-by-zero",
1054 "return",
1055 "returns-nonnull-attribute",
1056 "shift-exponent",
1057 "unreachable",
1058 "vla-bound",
1059 // TODO(danalbert): The following checks currently have compiler performance issues.
1060 //"alignment",
1061 //"bounds",
1062 //"enum",
1063 //"float-cast-overflow",
1064 //"float-divide-by-zero",
1065 //"nonnull-attribute",
1066 //"null",
1067 //"shift-base",
1068 //"signed-integer-overflow",
1069 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1070 // https://llvm.org/PR19302
1071 // http://reviews.llvm.org/D6974
1072 // "object-size",
1073 )
1074 }
1075 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
1076 }
1077
1078 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
1079 diagSanitizers = append(diagSanitizers, "undefined")
1080 }
1081
1082 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
1083
1084 if Bool(c.sanitize.Properties.Sanitize.Address) {
1085 sanitizers = append(sanitizers, "address")
1086 diagSanitizers = append(diagSanitizers, "address")
1087 }
1088
1089 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1090 sanitizers = append(sanitizers, "hwaddress")
1091 }
1092
1093 if Bool(c.sanitize.Properties.Sanitize.Thread) {
1094 sanitizers = append(sanitizers, "thread")
1095 }
1096
1097 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
1098 sanitizers = append(sanitizers, "safe-stack")
1099 }
1100
1101 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
1102 sanitizers = append(sanitizers, "cfi")
1103
1104 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
1105 diagSanitizers = append(diagSanitizers, "cfi")
1106 }
1107 }
1108
1109 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
1110 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1111 sanitizers = append(sanitizers, "signed-integer-overflow")
1112 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
1113 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1114 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1115 }
1116 }
1117
1118 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1119 sanitizers = append(sanitizers, "scudo")
1120 }
1121
1122 if Bool(c.sanitize.Properties.Sanitize.Scs) {
1123 sanitizers = append(sanitizers, "shadow-call-stack")
1124 }
1125
Ivan Lozanod7586b62021-04-01 09:49:36 -04001126 if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.Binary() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001127 noteDep := "note_memtag_heap_async"
1128 if Bool(c.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
1129 noteDep = "note_memtag_heap_sync"
1130 }
Inseob Kim253f5212021-04-08 17:10:31 +09001131 // If we're using snapshots, redirect to snapshot whenever possible
1132 // TODO(b/178470649): clean manual snapshot redirections
1133 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1134 if lib, ok := snapshot.StaticLibs[noteDep]; ok {
1135 noteDep = lib
1136 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001137 depTag := libraryDependencyTag{Kind: staticLibraryDependency, wholeStatic: true}
1138 variations := append(mctx.Target().Variations(),
1139 blueprint.Variation{Mutator: "link", Variation: "static"})
1140 if c.Device() {
1141 variations = append(variations, c.ImageVariation())
1142 }
1143 mctx.AddFarVariationDependencies(variations, depTag, noteDep)
1144 }
1145
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001146 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
1147 sanitizers = append(sanitizers, "fuzzer-no-link")
1148 }
1149
Jiyong Park379de2f2018-12-19 02:47:14 +09001150 // Save the list of sanitizers. These will be used again when generating
1151 // the build rules (for Cflags, etc.)
1152 c.sanitize.Properties.Sanitizers = sanitizers
1153 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1154
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001155 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1156 if c.Host() {
1157 diagSanitizers = sanitizers
1158 }
1159
Jiyong Park379de2f2018-12-19 02:47:14 +09001160 // Determine the runtime library required
1161 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001162 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001163 toolchain := c.toolchain(mctx)
1164 if Bool(c.sanitize.Properties.Sanitize.Address) {
1165 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1166 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1167 if c.staticBinary() {
1168 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001169 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001170 } else {
1171 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1172 }
1173 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
1174 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1175 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1176 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1177 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1178 } else {
1179 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1180 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001181 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001182 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
1183 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
1184 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001185 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
Colin Cross32f1de32021-03-29 13:41:37 -07001186 if c.staticBinary() {
1187 runtimeLibrary += ".static"
1188 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001189 }
1190
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001191 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
1192 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001193
1194 // Adding dependency to the runtime library. We are using *FarVariation*
1195 // because the runtime libraries themselves are not mutated by sanitizer
1196 // mutators and thus don't have sanitizer variants whereas this module
1197 // has been already mutated.
1198 //
1199 // Note that by adding dependency with {static|shared}DepTag, the lib is
1200 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
1201 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001202 deps := append(extraStaticDeps, runtimeLibrary)
Colin Crosse0edaf92021-01-11 17:31:17 -08001203 // If we're using snapshots, redirect to snapshot whenever possible
1204 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1205 for idx, dep := range deps {
1206 if lib, ok := snapshot.StaticLibs[dep]; ok {
1207 deps[idx] = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001208 }
1209 }
1210
Jiyong Park379de2f2018-12-19 02:47:14 +09001211 // static executable gets static runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -07001212 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Colin Cross42507332020-08-21 16:15:23 -07001213 variations := append(mctx.Target().Variations(),
1214 blueprint.Variation{Mutator: "link", Variation: "static"})
1215 if c.Device() {
1216 variations = append(variations, c.ImageVariation())
1217 }
1218 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001219 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001220 // If we're using snapshots, redirect to snapshot whenever possible
1221 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1222 if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
1223 runtimeLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001224 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001225
Cindy Zhou18417cb2020-12-10 07:12:38 -08001226 // Skip apex dependency check for sharedLibraryDependency
1227 // when sanitizer diags are enabled. Skipping the check will allow
1228 // building with diag libraries without having to list the
1229 // dependency in Apex's allowed_deps file.
1230 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001231 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001232 depTag := libraryDependencyTag{
1233 Kind: sharedLibraryDependency,
1234 Order: earlyLibraryDependency,
1235
1236 skipApexAllowedDependenciesCheck: diagEnabled,
1237 }
Colin Cross42507332020-08-21 16:15:23 -07001238 variations := append(mctx.Target().Variations(),
1239 blueprint.Variation{Mutator: "link", Variation: "shared"})
1240 if c.Device() {
1241 variations = append(variations, c.ImageVariation())
1242 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001243 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001244 }
1245 // static lib does not have dependency to the runtime library. The
1246 // dependency will be added to the executables or shared libs using
1247 // the static lib.
1248 }
1249 }
1250}
1251
1252type Sanitizeable interface {
1253 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +09001254 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001255 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001256 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001257}
1258
Ivan Lozanod7586b62021-04-01 09:49:36 -04001259func (c *Module) MinimalRuntimeDep() bool {
1260 return c.sanitize.Properties.MinimalRuntimeDep
1261}
1262
1263func (c *Module) UbsanRuntimeDep() bool {
1264 return c.sanitize.Properties.UbsanRuntimeDep
1265}
1266
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001267func (c *Module) SanitizePropDefined() bool {
1268 return c.sanitize != nil
1269}
1270
1271func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1272 return c.sanitize.isSanitizerEnabled(t)
1273}
1274
1275func (c *Module) SanitizeDep() bool {
1276 return c.sanitize.Properties.SanitizeDep
1277}
1278
1279func (c *Module) StaticallyLinked() bool {
1280 return c.static()
1281}
1282
1283func (c *Module) SetInSanitizerDir() {
1284 if c.sanitize != nil {
1285 c.sanitize.Properties.InSanitizerDir = true
1286 }
1287}
1288
1289func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1290 if c.sanitize != nil {
1291 c.sanitize.SetSanitizer(t, b)
1292 }
1293}
1294
1295func (c *Module) SetSanitizeDep(b bool) {
1296 if c.sanitize != nil {
1297 c.sanitize.Properties.SanitizeDep = b
1298 }
1299}
1300
1301var _ PlatformSanitizeable = (*Module)(nil)
1302
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001303// Create sanitized variants for modules that need them
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001304func sanitizerMutator(t SanitizerType) func(android.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -07001305 return func(mctx android.BottomUpMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001306 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
Liz Kammer187d5442021-06-25 14:50:12 -04001307 if c.Binary() && c.IsSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001308 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001309 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1310 } else if c.IsSanitizerEnabled(t) || c.SanitizeDep() {
1311 isSanitizerEnabled := c.IsSanitizerEnabled(t)
Colin Crossaf98f582021-05-12 17:27:32 -07001312 if c.StaticallyLinked() || c.Header() || t == Fuzzer {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001313 // Static and header libs are split into non-sanitized and sanitized variants.
1314 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1315 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1316 // that isn't sanitized for asan/fuzzer.
1317 //
1318 // Note for defaultVariation: since we don't split for shared libs but for static/header
1319 // libs, it is possible for the sanitized variant of a static/header lib to depend
1320 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1321 // error when the module is split. defaultVariation is the name of the variation that
1322 // will be used when such a dangling dependency occurs during the split of the current
1323 // module. By setting it to the name of the sanitized variation, the dangling dependency
1324 // is redirected to the sanitized variant of the dependent module.
1325 defaultVariation := t.variationName()
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001326 // Not all PlatformSanitizeable modules support the CFI sanitizer
1327 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001328 mctx.SetDefaultDependencyVariation(&defaultVariation)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001329
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001330 modules := mctx.CreateVariations("", t.variationName())
1331 modules[0].(PlatformSanitizeable).SetSanitizer(t, false)
1332 modules[1].(PlatformSanitizeable).SetSanitizer(t, true)
1333 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
1334 modules[1].(PlatformSanitizeable).SetSanitizeDep(false)
1335
1336 if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
Ivan Lozano4774a812020-03-10 16:23:57 -04001337 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1338 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001339 modules[1].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001340 }
1341
Jiyong Park1d1119f2019-07-29 21:27:18 +09001342 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1343 // to Make, because the sanitized version has a different suffix in name.
1344 // For other types of sanitizers, suppress the variation that is disabled.
Tri Vo6eafc362021-04-01 11:29:09 -07001345 if t != cfi && t != scs && t != Hwasan {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001346 if isSanitizerEnabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001347 modules[0].(PlatformSanitizeable).SetPreventInstall()
1348 modules[0].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001349 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001350 modules[1].(PlatformSanitizeable).SetPreventInstall()
1351 modules[1].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001352 }
1353 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001354
Jiyong Park1d1119f2019-07-29 21:27:18 +09001355 // Export the static lib name to make
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001356 if c.StaticallyLinked() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001357 if t == cfi {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001358 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
Tri Vo6eafc362021-04-01 11:29:09 -07001359 } else if t == Hwasan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001360 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001361 }
1362 }
1363 } else {
1364 // Shared libs are not split. Only the sanitized variant is created.
1365 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001366 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1367 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
Vishwath Mohane7128792017-11-17 11:08:10 -08001368
Jiyong Park1d1119f2019-07-29 21:27:18 +09001369 // locate the asan libraries under /data/asan
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001370 if mctx.Device() && t == Asan && isSanitizerEnabled {
1371 modules[0].(PlatformSanitizeable).SetInSanitizerDir()
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001372 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001373
1374 if mctx.Device() && t.incompatibleWithCfi() {
1375 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1376 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001377 modules[0].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001378 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001379 }
Colin Cross16b23492016-01-06 14:41:07 -08001380 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001381 c.SetSanitizeDep(false)
Jiyong Park82226632019-02-01 10:50:50 +09001382 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001383 // APEX modules fall here
Jooyung Han8ce8db92020-05-15 19:05:05 +09001384 sanitizeable.AddSanitizerDependencies(mctx, t.name())
Jiyong Park82226632019-02-01 10:50:50 +09001385 mctx.CreateVariations(t.variationName())
Inseob Kimc42f2f22020-07-29 20:32:10 +09001386 } else if c, ok := mctx.Module().(*Module); ok {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001387 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1388
Inseob Kimc42f2f22020-07-29 20:32:10 +09001389 // Check if it's a snapshot module supporting sanitizer
1390 if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) {
1391 // Set default variation as above.
1392 defaultVariation := t.variationName()
1393 mctx.SetDefaultDependencyVariation(&defaultVariation)
1394 modules := mctx.CreateVariations("", t.variationName())
1395 modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false)
1396 modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true)
1397
1398 // Export the static lib name to make
1399 if c.static() && c.ExportedToMake() {
1400 if t == cfi {
1401 // use BaseModuleName which is the name for Make.
1402 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1403 }
1404 }
1405 }
Colin Cross16b23492016-01-06 14:41:07 -08001406 }
1407 }
1408}
Vishwath Mohane7128792017-11-17 11:08:10 -08001409
Inseob Kim74d25562020-08-04 00:41:38 +09001410type sanitizerStaticLibsMap struct {
1411 // libsMap contains one list of modules per each image and each arch.
1412 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001413 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001414 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001415 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001416}
1417
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001418func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001419 return &sanitizerStaticLibsMap{
1420 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001421 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001422 }
1423}
1424
1425// Add the current module to sanitizer static libs maps
1426// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001427func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1428 image := GetImageVariantType(c)
1429 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001430
1431 s.libsMapLock.Lock()
1432 defer s.libsMapLock.Unlock()
1433
1434 if _, ok := s.libsMap[image]; !ok {
1435 s.libsMap[image] = make(map[string][]string)
1436 }
1437
1438 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1439}
1440
1441// Exports makefile variables in the following format:
1442// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1443// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1444// These are to be used by use_soong_sanitized_static_libraries.
1445// See build/make/core/binary.mk for more details.
1446func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1447 for _, image := range android.SortedStringKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001448 archMap := s.libsMap[ImageVariantType(image)]
Inseob Kim74d25562020-08-04 00:41:38 +09001449 for _, arch := range android.SortedStringKeys(archMap) {
1450 libs := archMap[arch]
1451 sort.Strings(libs)
1452
1453 key := fmt.Sprintf(
1454 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1455 s.sanitizerType.variationName(),
1456 image, // already upper
1457 arch)
1458
1459 ctx.Strict(key, strings.Join(libs, " "))
1460 }
1461 }
1462}
1463
Colin Cross571cccf2019-02-04 11:22:08 -08001464var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1465
Inseob Kim74d25562020-08-04 00:41:38 +09001466func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001467 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001468 return newSanitizerStaticLibsMap(cfi)
1469 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001470}
1471
Colin Cross571cccf2019-02-04 11:22:08 -08001472var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1473
Inseob Kim74d25562020-08-04 00:41:38 +09001474func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001475 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001476 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001477 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001478}
1479
Ivan Lozano30c5db22018-02-21 15:49:20 -08001480func enableMinimalRuntime(sanitize *sanitize) bool {
1481 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001482 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001483 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001484
Ivan Lozano30c5db22018-02-21 15:49:20 -08001485 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001486 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1487 Bool(sanitize.Properties.Sanitize.Undefined) ||
1488 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1489
Ivan Lozano30c5db22018-02-21 15:49:20 -08001490 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1491 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001492 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001493 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001494
Ivan Lozano30c5db22018-02-21 15:49:20 -08001495 return true
1496 }
1497 return false
1498}
1499
Ivan Lozanod7586b62021-04-01 09:49:36 -04001500func (m *Module) UbsanRuntimeNeeded() bool {
1501 return enableUbsanRuntime(m.sanitize)
1502}
1503
1504func (m *Module) MinimalRuntimeNeeded() bool {
1505 return enableMinimalRuntime(m.sanitize)
1506}
1507
Inseob Kim8471cda2019-11-15 09:59:12 +09001508func enableUbsanRuntime(sanitize *sanitize) bool {
1509 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001510 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001511 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1512}
1513
Vishwath Mohane7128792017-11-17 11:08:10 -08001514func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001515 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001516}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001517
1518func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001519 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001520}