blob: bb92a882d8bc33fddb02f19af87e610517584323 [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"
24
Colin Cross635c3b02016-05-18 15:37:25 -070025 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070026 "android/soong/cc/config"
Colin Cross16b23492016-01-06 14:41:07 -080027)
28
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070029var (
30 // Any C flags added by sanitizer which libTooling tools may not
31 // understand also need to be added to ClangLibToolingUnknownCflags in
32 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080033
Yi Kong20233a42019-08-21 01:38:40 -070034 asanCflags = []string{
35 "-fno-omit-frame-pointer",
36 "-fno-experimental-new-pass-manager",
37 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070038 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070039
Peter Collingbourne967511a2019-03-19 21:39:54 -070040 hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000041 "-fsanitize-hwaddress-abi=platform",
42 "-fno-experimental-new-pass-manager",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080043 // The following improves debug location information
44 // availability at the cost of its accuracy. It increases
45 // the likelihood of a stack variable's frame offset
46 // to be recorded in the debug info, which is important
47 // for the quality of hwasan reports. The downside is a
48 // higher number of "optimized out" stack variables.
49 // b/112437883.
50 "-mllvm", "-instcombine-lower-dbg-declare=0",
Mitch Phillipsb1c574f2020-06-22 13:28:23 -070051 // TODO(b/159343917): HWASan and GlobalISel don't play nicely, and
52 // GlobalISel is the default at -O0 on aarch64.
53 "-mllvm", "--aarch64-enable-global-isel-at-O=-1",
54 "-mllvm", "-fast-isel=false",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080055 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070056
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000057 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Pirama Arumuga Nainareb8d4032020-07-27 11:22:35 -070058 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blocklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070059 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
60 // used, but have no effect on assembly files
61 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070062 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070063 "-Wl,-plugin-opt,O1"}
Inseob Kim74d25562020-08-04 00:41:38 +090064 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070065
Pirama Arumuga Nainareb8d4032020-07-27 11:22:35 -070066 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080067
Peter Collingbournebd19db02019-03-06 10:38:48 -080068 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070069 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070070 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
71 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070072)
73
Colin Cross16b23492016-01-06 14:41:07 -080074type sanitizerType int
75
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070076func boolPtr(v bool) *bool {
77 if v {
78 return &v
79 } else {
80 return nil
81 }
82}
83
Colin Cross16b23492016-01-06 14:41:07 -080084const (
85 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070086 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080087 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070088 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000089 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080090 scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -070091 fuzzer
Colin Cross16b23492016-01-06 14:41:07 -080092)
93
Jiyong Park82226632019-02-01 10:50:50 +090094// Name of the sanitizer variation for this sanitizer type
95func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080096 switch t {
97 case asan:
98 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070099 case hwasan:
100 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -0800101 case tsan:
102 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700103 case intOverflow:
104 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000105 case cfi:
106 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800107 case scs:
108 return "scs"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700109 case fuzzer:
110 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800111 default:
112 panic(fmt.Errorf("unknown sanitizerType %d", t))
113 }
114}
115
Jiyong Park82226632019-02-01 10:50:50 +0900116// This is the sanitizer names in SANITIZE_[TARGET|HOST]
117func (t sanitizerType) name() string {
118 switch t {
119 case asan:
120 return "address"
121 case hwasan:
122 return "hwaddress"
123 case tsan:
124 return "thread"
125 case intOverflow:
126 return "integer_overflow"
127 case cfi:
128 return "cfi"
129 case scs:
130 return "shadow-call-stack"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700131 case fuzzer:
132 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900133 default:
134 panic(fmt.Errorf("unknown sanitizerType %d", t))
135 }
136}
137
Jiyong Park1d1119f2019-07-29 21:27:18 +0900138func (t sanitizerType) incompatibleWithCfi() bool {
139 return t == asan || t == fuzzer || t == hwasan
140}
141
Martin Stjernholmb0249572020-09-15 02:32:35 +0100142type SanitizeUserProps struct {
143 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800144
Martin Stjernholmb0249572020-09-15 02:32:35 +0100145 // main sanitizers
146 Address *bool `android:"arch_variant"`
147 Thread *bool `android:"arch_variant"`
148 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800149
Martin Stjernholmb0249572020-09-15 02:32:35 +0100150 // local sanitizers
151 Undefined *bool `android:"arch_variant"`
152 All_undefined *bool `android:"arch_variant"`
153 Misc_undefined []string `android:"arch_variant"`
154 Fuzzer *bool `android:"arch_variant"`
155 Safestack *bool `android:"arch_variant"`
156 Cfi *bool `android:"arch_variant"`
157 Integer_overflow *bool `android:"arch_variant"`
158 Scudo *bool `android:"arch_variant"`
159 Scs *bool `android:"arch_variant"`
160
161 // A modifier for ASAN and HWASAN for write only instrumentation
162 Writeonly *bool `android:"arch_variant"`
163
164 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
165 // Replaces abort() on error with a human-readable error message.
166 // Address and Thread sanitizers always run in diagnostic mode.
167 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700168 Undefined *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700169 Cfi *bool `android:"arch_variant"`
170 Integer_overflow *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100171 Misc_undefined []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800172 No_recover []string `android:"arch_variant"`
173 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800174
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800175 // Sanitizers to run with flag configuration specified
176 Config struct {
177 // Enables CFI support flags for assembly-heavy libraries
178 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800179 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800180
Martin Stjernholmb0249572020-09-15 02:32:35 +0100181 // value to pass to -fsanitize-recover=
182 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000183
Martin Stjernholmb0249572020-09-15 02:32:35 +0100184 // value to pass to -fsanitize-blacklist
185 Blocklist *string
186}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700187
Martin Stjernholmb0249572020-09-15 02:32:35 +0100188type SanitizeProperties struct {
189 // Enable AddressSanitizer, ThreadSanitizer, UndefinedBehaviorSanitizer, and
190 // others. Please see SanitizerUserProps in build/soong/cc/sanitize.go for
191 // details.
192 Sanitize SanitizeUserProps `android:"arch_variant"`
193 SanitizerEnabled bool `blueprint:"mutated"`
194 SanitizeDep bool `blueprint:"mutated"`
195 MinimalRuntimeDep bool `blueprint:"mutated"`
196 BuiltinsDep bool `blueprint:"mutated"`
197 UbsanRuntimeDep bool `blueprint:"mutated"`
198 InSanitizerDir bool `blueprint:"mutated"`
199 Sanitizers []string `blueprint:"mutated"`
200 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800201}
202
203type sanitize struct {
204 Properties SanitizeProperties
205}
206
Cindy Zhou18417cb2020-12-10 07:12:38 -0800207// Mark this tag with a check to see if apex dependency check should be skipped
208func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
209 return t.skipApexAllowedDependenciesCheck
210}
211
212var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
213
Vishwath Mohane7128792017-11-17 11:08:10 -0800214func init() {
215 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700216 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800217}
218
Colin Cross16b23492016-01-06 14:41:07 -0800219func (sanitize *sanitize) props() []interface{} {
220 return []interface{}{&sanitize.Properties}
221}
222
223func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700224 s := &sanitize.Properties.Sanitize
225
Colin Cross16b23492016-01-06 14:41:07 -0800226 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700227 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800228 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800229 }
230
Doug Hornc32c6b02019-01-17 14:44:05 -0800231 // Sanitizers do not work on Fuchsia yet.
232 if ctx.Fuchsia() {
233 s.Never = BoolPtr(true)
234 }
235
Colin Cross16b23492016-01-06 14:41:07 -0800236 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800237 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800238 return
239 }
240
Colin Cross16b23492016-01-06 14:41:07 -0800241 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700242 var globalSanitizersDiag []string
243
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700244 if ctx.Host() {
245 if !ctx.Windows() {
246 globalSanitizers = ctx.Config().SanitizeHost()
247 }
248 } else {
249 arches := ctx.Config().SanitizeDeviceArch()
250 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
251 globalSanitizers = ctx.Config().SanitizeDevice()
252 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800253 }
254 }
255
Colin Cross16b23492016-01-06 14:41:07 -0800256 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000257 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700258 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
259 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000260 }
Colin Cross16b23492016-01-06 14:41:07 -0800261
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700262 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
263 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000264 }
265
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700266 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
267 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000268 }
269
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700270 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
271 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000272 }
273
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700274 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
275 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700276 }
277
278 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
279 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000280 }
281
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700282 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800283 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700284 s.Cfi = boolPtr(true)
285 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700286 }
287
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700288 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700289 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700290 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700291 s.Integer_overflow = boolPtr(true)
292 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700293 }
294
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700295 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
296 s.Scudo = boolPtr(true)
297 }
298
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700299 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
300 s.Hwaddress = boolPtr(true)
301 }
302
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000303 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
304 // Hwaddress and Address are set before, so we can check them here
305 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
306 if s.Address == nil && s.Hwaddress == nil {
307 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
308 }
309 s.Writeonly = boolPtr(true)
310 }
311
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000312 if len(globalSanitizers) > 0 {
313 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
314 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700315
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700316 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700317 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700318 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700319 s.Diag.Integer_overflow = boolPtr(true)
320 }
321
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700322 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
323 s.Diag.Cfi == nil && Bool(s.Cfi) {
324 s.Diag.Cfi = boolPtr(true)
325 }
326
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700327 if len(globalSanitizersDiag) > 0 {
328 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
329 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700330 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700331
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700332 // Enable CFI for all components in the include paths (for Aarch64 only)
333 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000334 s.Cfi = boolPtr(true)
335 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
336 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700337 }
338 }
339
Elliott Hughesda3a0712020-03-06 16:55:28 -0800340 // Is CFI actually enabled?
341 if !ctx.Config().EnableCFI() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900342 s.Cfi = boolPtr(false)
343 s.Diag.Cfi = boolPtr(false)
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800344 }
345
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800346 // Also disable CFI for arm32 until b/35157333 is fixed.
347 if ctx.Arch().ArchType == android.Arm {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900348 s.Cfi = boolPtr(false)
349 s.Diag.Cfi = boolPtr(false)
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800350 }
351
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700352 // HWASan requires AArch64 hardware feature (top-byte-ignore).
353 if ctx.Arch().ArchType != android.Arm64 {
354 s.Hwaddress = nil
355 }
356
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800357 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800358 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800359 s.Scs = nil
360 }
361
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700362 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700363 if Bool(s.Address) || Bool(s.Hwaddress) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900364 s.Cfi = boolPtr(false)
365 s.Diag.Cfi = boolPtr(false)
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700366 }
367
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500368 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
369 if !ctx.Os().Linux() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900370 s.Cfi = boolPtr(false)
371 s.Diag.Cfi = boolPtr(false)
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700372 s.Misc_undefined = nil
373 s.Undefined = nil
374 s.All_undefined = nil
375 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800376 }
377
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700378 // Also disable CFI for VNDK variants of components
379 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900380 if ctx.static() {
381 // Cfi variant for static vndk should be captured as vendor snapshot,
382 // so don't strictly disable Cfi.
383 s.Cfi = nil
384 s.Diag.Cfi = nil
385 } else {
386 s.Cfi = boolPtr(false)
387 s.Diag.Cfi = boolPtr(false)
388 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900389 }
390
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700391 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700392 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
393 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700394 s.Hwaddress = nil
395 }
396
Colin Cross3c344ef2016-07-18 15:44:56 -0700397 if ctx.staticBinary() {
398 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700399 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700400 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800401 }
402
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700403 if Bool(s.All_undefined) {
404 s.Undefined = nil
405 }
406
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700407 if !ctx.toolchain().Is64Bit() {
408 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700409 s.Thread = nil
410 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800411 // TODO(ccross): error for compile_multilib = "32"?
412 }
413
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800414 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 -0700415 Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800416 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700417 sanitize.Properties.SanitizerEnabled = true
418 }
419
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800420 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
421 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700422 s.Scudo = nil
423 }
424
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700425 if Bool(s.Hwaddress) {
426 s.Address = nil
427 s.Thread = nil
428 }
429
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700430 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
431 // mutually incompatible.
432 if Bool(s.Fuzzer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900433 s.Cfi = boolPtr(false)
Colin Cross16b23492016-01-06 14:41:07 -0800434 }
435}
436
437func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
438 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
439 return deps
440 }
441
Colin Cross16b23492016-01-06 14:41:07 -0800442 return deps
443}
444
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800445func toDisableImplicitIntegerChange(flags []string) bool {
446 // Returns true if any flag is fsanitize*integer, and there is
447 // no explicit flag about sanitize=implicit-integer-sign-change.
448 for _, f := range flags {
449 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
450 return false
451 }
452 }
453 for _, f := range flags {
454 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
455 return true
456 }
457 }
458 return false
459}
460
Yabin Cuidb7dda82020-11-30 15:47:45 -0800461func toDisableUnsignedShiftBaseChange(flags []string) bool {
462 // Returns true if any flag is fsanitize*integer, and there is
463 // no explicit flag about sanitize=unsigned-shift-base.
464 for _, f := range flags {
465 if strings.Contains(f, "sanitize=unsigned-shift-base") {
466 return false
467 }
468 }
469 for _, f := range flags {
470 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
471 return true
472 }
473 }
474 return false
475}
476
Colin Cross16b23492016-01-06 14:41:07 -0800477func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700478 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
479 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500480 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
481 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800482
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500483 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800484 flags.Local.LdFlags = append(flags.Local.LdFlags,
485 minimalRuntimePath,
486 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800487 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500488
489 if sanitize.Properties.BuiltinsDep {
490 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
491 }
492
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700493 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800494 return flags
495 }
496
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700497 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700498 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800499 // Frame pointer based unwinder in ASan requires ARM frame setup.
500 // TODO: put in flags?
501 flags.RequiredInstructionSet = "arm"
502 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800503 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
504 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800505
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000506 if Bool(sanitize.Properties.Sanitize.Writeonly) {
507 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
508 }
509
Colin Cross16b23492016-01-06 14:41:07 -0800510 if ctx.Host() {
511 // -nodefaultlibs (provided with libc++) prevents the driver from linking
512 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800513 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800514 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800515 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900516 if ctx.bootstrap() {
517 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
518 } else {
519 flags.DynamicLinker = "/system/bin/linker_asan"
520 }
Colin Cross16b23492016-01-06 14:41:07 -0800521 if flags.Toolchain.Is64Bit() {
522 flags.DynamicLinker += "64"
523 }
524 }
Colin Cross16b23492016-01-06 14:41:07 -0800525 }
526
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700527 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800528 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000529 if Bool(sanitize.Properties.Sanitize.Writeonly) {
530 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
531 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700532 }
533
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700534 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800535 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700536
537 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800538 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
539 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
540 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
541 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700542
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700543 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
544 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
545 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800546 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
547 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700548
Mitch Phillips74384752019-06-17 10:33:52 -0700549 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800550 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700551
552 // Disable fortify for fuzzing builds. Generally, we'll be building with
553 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800554 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800555
556 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
557 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
558 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
559 // the DT_RUNPATH from the shared library above it, and not the executable,
560 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
561 // DT_RUNPATH here means that transient shared libraries can be found
562 // colocated with their parents.
563 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800564 }
565
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700566 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800567 if ctx.Arch().ArchType == android.Arm {
568 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
569 // to do this on a function basis, so force Thumb on the entire module.
570 flags.RequiredInstructionSet = "thumb"
571 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000572
Colin Cross4af21ed2019-11-04 09:37:55 -0800573 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
574 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800575 if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
576 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
577 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000578 // Only append the default visibility flag if -fvisibility has not already been set
579 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800580 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
581 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000582 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800583 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000584
585 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800586 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
587 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000588 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700589 }
590
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700591 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800592 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700593 }
594
Jiyong Park379de2f2018-12-19 02:47:14 +0900595 if len(sanitize.Properties.Sanitizers) > 0 {
596 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800597
Colin Cross4af21ed2019-11-04 09:37:55 -0800598 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
599 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800600 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800601 // Host sanitizers only link symbols in the final executable, so
602 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800603 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
604 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500605
606 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
607 if !ctx.toolchain().Bionic() {
608 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
609 }
610 }
611
612 if enableMinimalRuntime(sanitize) {
613 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
614 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
615 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
616 if !ctx.toolchain().Bionic() {
617 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800618 }
Colin Cross16b23492016-01-06 14:41:07 -0800619 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700620
621 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
622 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800623 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700624 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800625 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700626 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800627 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700628 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800629 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800630 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
631 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800632 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800633 // http://b/171275751, Android doesn't build with this sanitizer yet.
634 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
635 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
636 }
Colin Cross16b23492016-01-06 14:41:07 -0800637 }
638
Jiyong Park379de2f2018-12-19 02:47:14 +0900639 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800640 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700641 }
642 // FIXME: enable RTTI if diag + (cfi or vptr)
643
Andreas Gampe97071162017-05-08 13:15:23 -0700644 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800645 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700646 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
647 }
648
Ivan Lozano7929bba2018-12-12 09:36:31 -0800649 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800650 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800651 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
652 }
653
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700654 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
655 if blocklist.Valid() {
656 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blocklist.String())
657 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
658 }
659
Colin Cross16b23492016-01-06 14:41:07 -0800660 return flags
661}
662
Colin Crossd80cbca2020-02-24 12:01:37 -0800663func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900664 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
665 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800666 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900667 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800668 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900669 }
670 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800671 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900672 }
673 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800674 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900675 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800676 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700677}
678
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700679func (sanitize *sanitize) inSanitizerDir() bool {
680 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700681}
682
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000683func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000684 switch t {
685 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000686 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700687 case hwasan:
688 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000689 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000690 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000691 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000692 return sanitize.Properties.Sanitize.Integer_overflow
693 case cfi:
694 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800695 case scs:
696 return sanitize.Properties.Sanitize.Scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700697 case fuzzer:
698 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000699 default:
700 panic(fmt.Errorf("unknown sanitizerType %d", t))
701 }
702}
703
Dan Albert7d1eecf2018-01-19 12:30:45 -0800704func (sanitize *sanitize) isUnsanitizedVariant() bool {
705 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700706 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800707 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800708 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700709 !sanitize.isSanitizerEnabled(scs) &&
710 !sanitize.isSanitizerEnabled(fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800711}
712
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700713func (sanitize *sanitize) isVariantOnProductionDevice() bool {
714 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700715 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700716 !sanitize.isSanitizerEnabled(tsan) &&
717 !sanitize.isSanitizerEnabled(fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700718}
719
Colin Cross16b23492016-01-06 14:41:07 -0800720func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
721 switch t {
722 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700723 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700724 case hwasan:
725 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800726 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700727 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700728 case intOverflow:
729 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000730 case cfi:
731 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800732 case scs:
733 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700734 case fuzzer:
735 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800736 default:
737 panic(fmt.Errorf("unknown sanitizerType %d", t))
738 }
739 if b {
740 sanitize.Properties.SanitizerEnabled = true
741 }
742}
743
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000744// Check if the sanitizer is explicitly disabled (as opposed to nil by
745// virtue of not being set).
746func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
747 if sanitize == nil {
748 return false
749 }
750
751 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
752 return sanitizerVal != nil && *sanitizerVal == false
753}
754
755// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
756// because enabling a sanitizer either directly (via the blueprint) or
757// indirectly (via a mutator) sets the bool ptr to true, and you can't
758// distinguish between the cases. It isn't needed though - both cases can be
759// treated identically.
760func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
761 if sanitize == nil {
762 return false
763 }
764
765 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
766 return sanitizerVal != nil && *sanitizerVal == true
767}
768
Colin Cross6b753602018-06-21 13:03:07 -0700769func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700770 switch t := tag.(type) {
771 case dependencyTag:
772 return t == reuseObjTag || t == objDepTag
773 case libraryDependencyTag:
774 return true
775 default:
776 return false
777 }
Colin Cross6b753602018-06-21 13:03:07 -0700778}
779
Inseob Kimc42f2f22020-07-29 20:32:10 +0900780// Determines if the current module is a static library going to be captured
781// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
782// except for ones which explicitly disable cfi.
783func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
Bill Peckham945441c2020-08-31 16:07:58 -0700784 if isVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900785 return false
786 }
787
788 c := mctx.Module().(*Module)
789
790 if !c.inVendor() {
791 return false
792 }
793
794 if !c.static() {
795 return false
796 }
797
798 if c.Prebuilt() != nil {
799 return false
800 }
801
802 return c.sanitize != nil &&
803 !Bool(c.sanitize.Properties.Sanitize.Never) &&
804 !c.sanitize.isSanitizerExplicitlyDisabled(cfi)
805}
806
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700807// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700808func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
809 return func(mctx android.TopDownMutatorContext) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900810 if c, ok := mctx.Module().(*Module); ok {
811 enabled := c.sanitize.isSanitizerEnabled(t)
812 if t == cfi && needsCfiForVendorSnapshot(mctx) {
813 // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly
814 // determine defaultVariation in sanitizerMutator below.
815 // Instead, just mark SanitizeDep to forcefully create cfi variant.
816 enabled = true
817 c.sanitize.Properties.SanitizeDep = true
818 }
819 if enabled {
820 mctx.WalkDeps(func(child, parent android.Module) bool {
821 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
822 return false
823 }
824 if d, ok := child.(*Module); ok && d.sanitize != nil &&
825 !Bool(d.sanitize.Properties.Sanitize.Never) &&
826 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
827 if t == cfi || t == hwasan || t == scs {
828 if d.static() {
829 d.sanitize.Properties.SanitizeDep = true
830 }
831 } else {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700832 d.sanitize.Properties.SanitizeDep = true
833 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000834 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900835 return true
836 })
837 }
Jiyong Parkf97782b2019-02-13 20:28:58 +0900838 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
839 // If an APEX module includes a lib which is enabled for a sanitizer T, then
840 // the APEX module is also enabled for the same sanitizer type.
841 mctx.VisitDirectDeps(func(child android.Module) {
842 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
843 sanitizeable.EnableSanitizer(t.name())
844 }
845 })
Colin Cross16b23492016-01-06 14:41:07 -0800846 }
847 }
848}
849
Ivan Lozano30c5db22018-02-21 15:49:20 -0800850// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700851func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
852 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
853 mctx.WalkDeps(func(child, parent android.Module) bool {
854 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
855 return false
856 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800857
Inseob Kimeec88e12020-01-22 11:11:29 +0900858 d, ok := child.(*Module)
859 if !ok || !d.static() {
860 return false
861 }
862 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700863 if enableMinimalRuntime(d.sanitize) {
864 // If a static dependency is built with the minimal runtime,
865 // make sure we include the ubsan minimal runtime.
866 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +0900867 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -0700868 // If a static dependency runs with full ubsan diagnostics,
869 // make sure we include the ubsan runtime.
870 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800871 }
Colin Cross0b908332019-06-19 23:00:20 -0700872
873 if c.sanitize.Properties.MinimalRuntimeDep &&
874 c.sanitize.Properties.UbsanRuntimeDep {
875 // both flags that this mutator might set are true, so don't bother recursing
876 return false
877 }
878
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500879 if c.Os() == android.Linux {
880 c.sanitize.Properties.BuiltinsDep = true
881 }
882
Colin Cross0b908332019-06-19 23:00:20 -0700883 return true
Colin Cross6b753602018-06-21 13:03:07 -0700884 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900885
Jose Galmesf7294582020-11-13 12:07:36 -0800886 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +0900887 if Bool(p.properties.Sanitize_minimal_dep) {
888 c.sanitize.Properties.MinimalRuntimeDep = true
889 }
890 if Bool(p.properties.Sanitize_ubsan_dep) {
891 c.sanitize.Properties.UbsanRuntimeDep = true
892 }
893 }
894
895 return false
Colin Cross6b753602018-06-21 13:03:07 -0700896 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800897 }
898}
899
Jiyong Park379de2f2018-12-19 02:47:14 +0900900// Add the dependency to the runtime library for each of the sanitizer variants
901func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900902 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000903 if !c.Enabled() {
904 return
905 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900906 var sanitizers []string
907 var diagSanitizers []string
908
909 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
910 sanitizers = append(sanitizers, "undefined")
911 } else {
912 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
913 sanitizers = append(sanitizers,
914 "bool",
915 "integer-divide-by-zero",
916 "return",
917 "returns-nonnull-attribute",
918 "shift-exponent",
919 "unreachable",
920 "vla-bound",
921 // TODO(danalbert): The following checks currently have compiler performance issues.
922 //"alignment",
923 //"bounds",
924 //"enum",
925 //"float-cast-overflow",
926 //"float-divide-by-zero",
927 //"nonnull-attribute",
928 //"null",
929 //"shift-base",
930 //"signed-integer-overflow",
931 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
932 // https://llvm.org/PR19302
933 // http://reviews.llvm.org/D6974
934 // "object-size",
935 )
936 }
937 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
938 }
939
940 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
941 diagSanitizers = append(diagSanitizers, "undefined")
942 }
943
944 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
945
946 if Bool(c.sanitize.Properties.Sanitize.Address) {
947 sanitizers = append(sanitizers, "address")
948 diagSanitizers = append(diagSanitizers, "address")
949 }
950
951 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
952 sanitizers = append(sanitizers, "hwaddress")
953 }
954
955 if Bool(c.sanitize.Properties.Sanitize.Thread) {
956 sanitizers = append(sanitizers, "thread")
957 }
958
959 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
960 sanitizers = append(sanitizers, "safe-stack")
961 }
962
963 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
964 sanitizers = append(sanitizers, "cfi")
965
966 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
967 diagSanitizers = append(diagSanitizers, "cfi")
968 }
969 }
970
971 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
972 sanitizers = append(sanitizers, "unsigned-integer-overflow")
973 sanitizers = append(sanitizers, "signed-integer-overflow")
974 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
975 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
976 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
977 }
978 }
979
980 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
981 sanitizers = append(sanitizers, "scudo")
982 }
983
984 if Bool(c.sanitize.Properties.Sanitize.Scs) {
985 sanitizers = append(sanitizers, "shadow-call-stack")
986 }
987
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700988 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
989 sanitizers = append(sanitizers, "fuzzer-no-link")
990 }
991
Jiyong Park379de2f2018-12-19 02:47:14 +0900992 // Save the list of sanitizers. These will be used again when generating
993 // the build rules (for Cflags, etc.)
994 c.sanitize.Properties.Sanitizers = sanitizers
995 c.sanitize.Properties.DiagSanitizers = diagSanitizers
996
Ivan Lozanof3b190f2020-03-06 12:01:21 -0500997 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
998 if c.Host() {
999 diagSanitizers = sanitizers
1000 }
1001
Jiyong Park379de2f2018-12-19 02:47:14 +09001002 // Determine the runtime library required
1003 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001004 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001005 toolchain := c.toolchain(mctx)
1006 if Bool(c.sanitize.Properties.Sanitize.Address) {
1007 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1008 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1009 if c.staticBinary() {
1010 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001011 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001012 } else {
1013 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1014 }
1015 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
1016 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1017 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1018 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1019 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1020 } else {
1021 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1022 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001023 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001024 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
1025 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
1026 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001027 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
1028 }
1029
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001030 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
1031 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001032
1033 // Adding dependency to the runtime library. We are using *FarVariation*
1034 // because the runtime libraries themselves are not mutated by sanitizer
1035 // mutators and thus don't have sanitizer variants whereas this module
1036 // has been already mutated.
1037 //
1038 // Note that by adding dependency with {static|shared}DepTag, the lib is
1039 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
1040 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001041 deps := append(extraStaticDeps, runtimeLibrary)
1042 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
1043 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
1044 snapshots := vendorSnapshotStaticLibs(mctx.Config())
1045 for idx, dep := range deps {
1046 if lib, ok := snapshots.get(dep, mctx.Arch().ArchType); ok {
1047 deps[idx] = lib
1048 }
1049 }
1050 }
1051
Jiyong Park379de2f2018-12-19 02:47:14 +09001052 // static executable gets static runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -07001053 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Colin Cross42507332020-08-21 16:15:23 -07001054 variations := append(mctx.Target().Variations(),
1055 blueprint.Variation{Mutator: "link", Variation: "static"})
1056 if c.Device() {
1057 variations = append(variations, c.ImageVariation())
1058 }
1059 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001060 } else if !c.static() && !c.header() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001061 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
1062 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
1063 snapshots := vendorSnapshotSharedLibs(mctx.Config())
1064 if lib, ok := snapshots.get(runtimeLibrary, mctx.Arch().ArchType); ok {
1065 runtimeLibrary = lib
1066 }
1067 }
Cindy Zhou18417cb2020-12-10 07:12:38 -08001068 // Skip apex dependency check for sharedLibraryDependency
1069 // when sanitizer diags are enabled. Skipping the check will allow
1070 // building with diag libraries without having to list the
1071 // dependency in Apex's allowed_deps file.
1072 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001073 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001074 depTag := libraryDependencyTag{
1075 Kind: sharedLibraryDependency,
1076 Order: earlyLibraryDependency,
1077
1078 skipApexAllowedDependenciesCheck: diagEnabled,
1079 }
Colin Cross42507332020-08-21 16:15:23 -07001080 variations := append(mctx.Target().Variations(),
1081 blueprint.Variation{Mutator: "link", Variation: "shared"})
1082 if c.Device() {
1083 variations = append(variations, c.ImageVariation())
1084 }
Colin Crosse7257d22020-09-24 09:56:18 -07001085 c.addSharedLibDependenciesWithVersions(mctx, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001086 }
1087 // static lib does not have dependency to the runtime library. The
1088 // dependency will be added to the executables or shared libs using
1089 // the static lib.
1090 }
1091 }
1092}
1093
1094type Sanitizeable interface {
1095 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +09001096 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001097 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001098 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001099}
1100
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001101// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -07001102func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
1103 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +00001104 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001105 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001106 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -07001107 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001108 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001109 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001110 if c.static() || c.header() || t == asan || t == fuzzer {
1111 // Static and header libs are split into non-sanitized and sanitized variants.
1112 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1113 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1114 // that isn't sanitized for asan/fuzzer.
1115 //
1116 // Note for defaultVariation: since we don't split for shared libs but for static/header
1117 // libs, it is possible for the sanitized variant of a static/header lib to depend
1118 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1119 // error when the module is split. defaultVariation is the name of the variation that
1120 // will be used when such a dangling dependency occurs during the split of the current
1121 // module. By setting it to the name of the sanitized variation, the dangling dependency
1122 // is redirected to the sanitized variant of the dependent module.
1123 defaultVariation := t.variationName()
1124 mctx.SetDefaultDependencyVariation(&defaultVariation)
1125 modules := mctx.CreateVariations("", t.variationName())
1126 modules[0].(*Module).sanitize.SetSanitizer(t, false)
1127 modules[1].(*Module).sanitize.SetSanitizer(t, true)
1128 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
1129 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001130
Ivan Lozano4774a812020-03-10 16:23:57 -04001131 if mctx.Device() && t.incompatibleWithCfi() {
1132 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1133 // are incompatible with cfi
1134 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
1135 }
1136
Jiyong Park1d1119f2019-07-29 21:27:18 +09001137 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1138 // to Make, because the sanitized version has a different suffix in name.
1139 // For other types of sanitizers, suppress the variation that is disabled.
1140 if t != cfi && t != scs && t != hwasan {
1141 if isSanitizerEnabled {
1142 modules[0].(*Module).Properties.PreventInstall = true
1143 modules[0].(*Module).Properties.HideFromMake = true
1144 } else {
1145 modules[1].(*Module).Properties.PreventInstall = true
1146 modules[1].(*Module).Properties.HideFromMake = true
1147 }
1148 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001149
Jiyong Park1d1119f2019-07-29 21:27:18 +09001150 // Export the static lib name to make
Dan Willemsenb5b2aba2020-05-03 21:28:32 -07001151 if c.static() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001152 if t == cfi {
Inseob Kim74d25562020-08-04 00:41:38 +09001153 cfiStaticLibs(mctx.Config()).add(c, c.Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001154 } else if t == hwasan {
Inseob Kim74d25562020-08-04 00:41:38 +09001155 hwasanStaticLibs(mctx.Config()).add(c, c.Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001156 }
1157 }
1158 } else {
1159 // Shared libs are not split. Only the sanitized variant is created.
1160 modules := mctx.CreateVariations(t.variationName())
1161 modules[0].(*Module).sanitize.SetSanitizer(t, true)
1162 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -08001163
Jiyong Park1d1119f2019-07-29 21:27:18 +09001164 // locate the asan libraries under /data/asan
1165 if mctx.Device() && t == asan && isSanitizerEnabled {
1166 modules[0].(*Module).sanitize.Properties.InSanitizerDir = true
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001167 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001168
1169 if mctx.Device() && t.incompatibleWithCfi() {
1170 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1171 // are incompatible with cfi
1172 modules[0].(*Module).sanitize.SetSanitizer(cfi, false)
1173 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001174 }
Colin Cross16b23492016-01-06 14:41:07 -08001175 }
1176 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +09001177 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001178 // APEX modules fall here
Jooyung Han8ce8db92020-05-15 19:05:05 +09001179 sanitizeable.AddSanitizerDependencies(mctx, t.name())
Jiyong Park82226632019-02-01 10:50:50 +09001180 mctx.CreateVariations(t.variationName())
Inseob Kimc42f2f22020-07-29 20:32:10 +09001181 } else if c, ok := mctx.Module().(*Module); ok {
1182 // Check if it's a snapshot module supporting sanitizer
1183 if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) {
1184 // Set default variation as above.
1185 defaultVariation := t.variationName()
1186 mctx.SetDefaultDependencyVariation(&defaultVariation)
1187 modules := mctx.CreateVariations("", t.variationName())
1188 modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false)
1189 modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true)
1190
1191 // Export the static lib name to make
1192 if c.static() && c.ExportedToMake() {
1193 if t == cfi {
1194 // use BaseModuleName which is the name for Make.
1195 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1196 }
1197 }
1198 }
Colin Cross16b23492016-01-06 14:41:07 -08001199 }
1200 }
1201}
Vishwath Mohane7128792017-11-17 11:08:10 -08001202
Inseob Kim74d25562020-08-04 00:41:38 +09001203type sanitizerStaticLibsMap struct {
1204 // libsMap contains one list of modules per each image and each arch.
1205 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
1206 libsMap map[imageVariantType]map[string][]string
1207 libsMapLock sync.Mutex
1208 sanitizerType sanitizerType
1209}
1210
1211func newSanitizerStaticLibsMap(t sanitizerType) *sanitizerStaticLibsMap {
1212 return &sanitizerStaticLibsMap{
1213 sanitizerType: t,
1214 libsMap: make(map[imageVariantType]map[string][]string),
1215 }
1216}
1217
1218// Add the current module to sanitizer static libs maps
1219// Each module should pass its exported name as names of Make and Soong can differ.
1220func (s *sanitizerStaticLibsMap) add(c *Module, name string) {
1221 image := c.getImageVariantType()
1222 arch := c.Arch().ArchType.String()
1223
1224 s.libsMapLock.Lock()
1225 defer s.libsMapLock.Unlock()
1226
1227 if _, ok := s.libsMap[image]; !ok {
1228 s.libsMap[image] = make(map[string][]string)
1229 }
1230
1231 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1232}
1233
1234// Exports makefile variables in the following format:
1235// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1236// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1237// These are to be used by use_soong_sanitized_static_libraries.
1238// See build/make/core/binary.mk for more details.
1239func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1240 for _, image := range android.SortedStringKeys(s.libsMap) {
1241 archMap := s.libsMap[imageVariantType(image)]
1242 for _, arch := range android.SortedStringKeys(archMap) {
1243 libs := archMap[arch]
1244 sort.Strings(libs)
1245
1246 key := fmt.Sprintf(
1247 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1248 s.sanitizerType.variationName(),
1249 image, // already upper
1250 arch)
1251
1252 ctx.Strict(key, strings.Join(libs, " "))
1253 }
1254 }
1255}
1256
Colin Cross571cccf2019-02-04 11:22:08 -08001257var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1258
Inseob Kim74d25562020-08-04 00:41:38 +09001259func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001260 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001261 return newSanitizerStaticLibsMap(cfi)
1262 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001263}
1264
Colin Cross571cccf2019-02-04 11:22:08 -08001265var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1266
Inseob Kim74d25562020-08-04 00:41:38 +09001267func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001268 return config.Once(hwasanStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001269 return newSanitizerStaticLibsMap(hwasan)
1270 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001271}
1272
Ivan Lozano30c5db22018-02-21 15:49:20 -08001273func enableMinimalRuntime(sanitize *sanitize) bool {
1274 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001275 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001276 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001277
Ivan Lozano30c5db22018-02-21 15:49:20 -08001278 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001279 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1280 Bool(sanitize.Properties.Sanitize.Undefined) ||
1281 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1282
Ivan Lozano30c5db22018-02-21 15:49:20 -08001283 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1284 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001285 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001286 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001287
Ivan Lozano30c5db22018-02-21 15:49:20 -08001288 return true
1289 }
1290 return false
1291}
1292
Inseob Kim8471cda2019-11-15 09:59:12 +09001293func enableUbsanRuntime(sanitize *sanitize) bool {
1294 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001295 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001296 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1297}
1298
Vishwath Mohane7128792017-11-17 11:08:10 -08001299func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001300 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001301}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001302
1303func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001304 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001305}