blob: 5992611671b5a852cc3e7a04fc8f26f590616747 [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
Ivan Lozano3968d8f2020-12-14 11:27:52 -050074type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080075
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 (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050085 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
Ivan Lozano3968d8f2020-12-14 11:27:52 -050091 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
Ivan Lozano3968d8f2020-12-14 11:27:52 -050095func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080096 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -050097 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -080098 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"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500109 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700110 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800111 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500112 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800113 }
114}
115
Jiyong Park82226632019-02-01 10:50:50 +0900116// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500117func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900118 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500119 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900120 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"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500131 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700132 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900133 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500134 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900135 }
136}
137
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500138func (*Module) SanitizerSupported(t SanitizerType) bool {
139 switch t {
140 case Asan:
141 return true
142 case hwasan:
143 return true
144 case tsan:
145 return true
146 case intOverflow:
147 return true
148 case cfi:
149 return true
150 case scs:
151 return true
152 case Fuzzer:
153 return true
154 default:
155 return false
156 }
157}
158
159// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
160func (t SanitizerType) incompatibleWithCfi() bool {
161 return t == Asan || t == Fuzzer || t == hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900162}
163
Martin Stjernholmb0249572020-09-15 02:32:35 +0100164type SanitizeUserProps struct {
165 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800166
Martin Stjernholmb0249572020-09-15 02:32:35 +0100167 // main sanitizers
168 Address *bool `android:"arch_variant"`
169 Thread *bool `android:"arch_variant"`
170 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800171
Martin Stjernholmb0249572020-09-15 02:32:35 +0100172 // local sanitizers
173 Undefined *bool `android:"arch_variant"`
174 All_undefined *bool `android:"arch_variant"`
175 Misc_undefined []string `android:"arch_variant"`
176 Fuzzer *bool `android:"arch_variant"`
177 Safestack *bool `android:"arch_variant"`
178 Cfi *bool `android:"arch_variant"`
179 Integer_overflow *bool `android:"arch_variant"`
180 Scudo *bool `android:"arch_variant"`
181 Scs *bool `android:"arch_variant"`
182
183 // A modifier for ASAN and HWASAN for write only instrumentation
184 Writeonly *bool `android:"arch_variant"`
185
186 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
187 // Replaces abort() on error with a human-readable error message.
188 // Address and Thread sanitizers always run in diagnostic mode.
189 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700190 Undefined *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700191 Cfi *bool `android:"arch_variant"`
192 Integer_overflow *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100193 Misc_undefined []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800194 No_recover []string `android:"arch_variant"`
195 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800196
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800197 // Sanitizers to run with flag configuration specified
198 Config struct {
199 // Enables CFI support flags for assembly-heavy libraries
200 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800201 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800202
Martin Stjernholmb0249572020-09-15 02:32:35 +0100203 // value to pass to -fsanitize-recover=
204 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000205
Martin Stjernholmb0249572020-09-15 02:32:35 +0100206 // value to pass to -fsanitize-blacklist
207 Blocklist *string
208}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700209
Martin Stjernholmb0249572020-09-15 02:32:35 +0100210type SanitizeProperties struct {
211 // Enable AddressSanitizer, ThreadSanitizer, UndefinedBehaviorSanitizer, and
212 // others. Please see SanitizerUserProps in build/soong/cc/sanitize.go for
213 // details.
214 Sanitize SanitizeUserProps `android:"arch_variant"`
215 SanitizerEnabled bool `blueprint:"mutated"`
216 SanitizeDep bool `blueprint:"mutated"`
217 MinimalRuntimeDep bool `blueprint:"mutated"`
218 BuiltinsDep bool `blueprint:"mutated"`
219 UbsanRuntimeDep bool `blueprint:"mutated"`
220 InSanitizerDir bool `blueprint:"mutated"`
221 Sanitizers []string `blueprint:"mutated"`
222 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800223}
224
225type sanitize struct {
226 Properties SanitizeProperties
227}
228
Cindy Zhou18417cb2020-12-10 07:12:38 -0800229// Mark this tag with a check to see if apex dependency check should be skipped
230func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
231 return t.skipApexAllowedDependenciesCheck
232}
233
234var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
235
Vishwath Mohane7128792017-11-17 11:08:10 -0800236func init() {
237 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700238 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800239}
240
Colin Cross16b23492016-01-06 14:41:07 -0800241func (sanitize *sanitize) props() []interface{} {
242 return []interface{}{&sanitize.Properties}
243}
244
245func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700246 s := &sanitize.Properties.Sanitize
247
Colin Cross16b23492016-01-06 14:41:07 -0800248 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700249 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800250 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800251 }
252
Doug Hornc32c6b02019-01-17 14:44:05 -0800253 // Sanitizers do not work on Fuchsia yet.
254 if ctx.Fuchsia() {
255 s.Never = BoolPtr(true)
256 }
257
Colin Cross16b23492016-01-06 14:41:07 -0800258 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800259 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800260 return
261 }
262
Colin Cross16b23492016-01-06 14:41:07 -0800263 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700264 var globalSanitizersDiag []string
265
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700266 if ctx.Host() {
267 if !ctx.Windows() {
268 globalSanitizers = ctx.Config().SanitizeHost()
269 }
270 } else {
271 arches := ctx.Config().SanitizeDeviceArch()
272 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
273 globalSanitizers = ctx.Config().SanitizeDevice()
274 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800275 }
276 }
277
Colin Cross16b23492016-01-06 14:41:07 -0800278 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000279 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700280 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
281 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000282 }
Colin Cross16b23492016-01-06 14:41:07 -0800283
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700284 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
285 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000286 }
287
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700288 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
289 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000290 }
291
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700292 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
293 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000294 }
295
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700296 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
297 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700298 }
299
300 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
301 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000302 }
303
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700304 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800305 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700306 s.Cfi = boolPtr(true)
307 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700308 }
309
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700310 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700311 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700312 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700313 s.Integer_overflow = boolPtr(true)
314 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700315 }
316
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700317 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
318 s.Scudo = boolPtr(true)
319 }
320
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700321 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
322 s.Hwaddress = boolPtr(true)
323 }
324
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000325 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
326 // Hwaddress and Address are set before, so we can check them here
327 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
328 if s.Address == nil && s.Hwaddress == nil {
329 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
330 }
331 s.Writeonly = boolPtr(true)
332 }
333
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000334 if len(globalSanitizers) > 0 {
335 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
336 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700337
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700338 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700339 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700340 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700341 s.Diag.Integer_overflow = boolPtr(true)
342 }
343
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700344 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
345 s.Diag.Cfi == nil && Bool(s.Cfi) {
346 s.Diag.Cfi = boolPtr(true)
347 }
348
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700349 if len(globalSanitizersDiag) > 0 {
350 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
351 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700352 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700353
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700354 // Enable CFI for all components in the include paths (for Aarch64 only)
355 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000356 s.Cfi = boolPtr(true)
357 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
358 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700359 }
360 }
361
Elliott Hughesda3a0712020-03-06 16:55:28 -0800362 // Is CFI actually enabled?
363 if !ctx.Config().EnableCFI() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900364 s.Cfi = boolPtr(false)
365 s.Diag.Cfi = boolPtr(false)
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800366 }
367
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800368 // Also disable CFI for arm32 until b/35157333 is fixed.
369 if ctx.Arch().ArchType == android.Arm {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900370 s.Cfi = boolPtr(false)
371 s.Diag.Cfi = boolPtr(false)
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800372 }
373
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700374 // HWASan requires AArch64 hardware feature (top-byte-ignore).
375 if ctx.Arch().ArchType != android.Arm64 {
376 s.Hwaddress = nil
377 }
378
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800379 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800380 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800381 s.Scs = nil
382 }
383
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700384 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700385 if Bool(s.Address) || Bool(s.Hwaddress) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900386 s.Cfi = boolPtr(false)
387 s.Diag.Cfi = boolPtr(false)
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700388 }
389
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500390 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
391 if !ctx.Os().Linux() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900392 s.Cfi = boolPtr(false)
393 s.Diag.Cfi = boolPtr(false)
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700394 s.Misc_undefined = nil
395 s.Undefined = nil
396 s.All_undefined = nil
397 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800398 }
399
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700400 // Also disable CFI for VNDK variants of components
401 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900402 if ctx.static() {
403 // Cfi variant for static vndk should be captured as vendor snapshot,
404 // so don't strictly disable Cfi.
405 s.Cfi = nil
406 s.Diag.Cfi = nil
407 } else {
408 s.Cfi = boolPtr(false)
409 s.Diag.Cfi = boolPtr(false)
410 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900411 }
412
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700413 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700414 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
415 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700416 s.Hwaddress = nil
417 }
418
Colin Cross3c344ef2016-07-18 15:44:56 -0700419 if ctx.staticBinary() {
420 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700421 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700422 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800423 }
424
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700425 if Bool(s.All_undefined) {
426 s.Undefined = nil
427 }
428
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700429 if !ctx.toolchain().Is64Bit() {
430 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700431 s.Thread = nil
432 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800433 // TODO(ccross): error for compile_multilib = "32"?
434 }
435
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800436 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 -0700437 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 -0800438 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700439 sanitize.Properties.SanitizerEnabled = true
440 }
441
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800442 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
443 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700444 s.Scudo = nil
445 }
446
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700447 if Bool(s.Hwaddress) {
448 s.Address = nil
449 s.Thread = nil
450 }
451
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700452 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
453 // mutually incompatible.
454 if Bool(s.Fuzzer) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900455 s.Cfi = boolPtr(false)
Colin Cross16b23492016-01-06 14:41:07 -0800456 }
457}
458
459func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
460 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
461 return deps
462 }
463
Colin Cross16b23492016-01-06 14:41:07 -0800464 return deps
465}
466
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800467func toDisableImplicitIntegerChange(flags []string) bool {
468 // Returns true if any flag is fsanitize*integer, and there is
469 // no explicit flag about sanitize=implicit-integer-sign-change.
470 for _, f := range flags {
471 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
472 return false
473 }
474 }
475 for _, f := range flags {
476 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
477 return true
478 }
479 }
480 return false
481}
482
Yabin Cuidb7dda82020-11-30 15:47:45 -0800483func toDisableUnsignedShiftBaseChange(flags []string) bool {
484 // Returns true if any flag is fsanitize*integer, and there is
485 // no explicit flag about sanitize=unsigned-shift-base.
486 for _, f := range flags {
487 if strings.Contains(f, "sanitize=unsigned-shift-base") {
488 return false
489 }
490 }
491 for _, f := range flags {
492 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
493 return true
494 }
495 }
496 return false
497}
498
Colin Cross16b23492016-01-06 14:41:07 -0800499func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700500 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
501 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500502 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
503 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800504
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500505 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800506 flags.Local.LdFlags = append(flags.Local.LdFlags,
507 minimalRuntimePath,
508 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800509 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500510
511 if sanitize.Properties.BuiltinsDep {
512 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
513 }
514
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700515 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800516 return flags
517 }
518
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700519 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700520 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800521 // Frame pointer based unwinder in ASan requires ARM frame setup.
522 // TODO: put in flags?
523 flags.RequiredInstructionSet = "arm"
524 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800525 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
526 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800527
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000528 if Bool(sanitize.Properties.Sanitize.Writeonly) {
529 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
530 }
531
Colin Cross16b23492016-01-06 14:41:07 -0800532 if ctx.Host() {
533 // -nodefaultlibs (provided with libc++) prevents the driver from linking
534 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800535 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800536 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800537 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900538 if ctx.bootstrap() {
539 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
540 } else {
541 flags.DynamicLinker = "/system/bin/linker_asan"
542 }
Colin Cross16b23492016-01-06 14:41:07 -0800543 if flags.Toolchain.Is64Bit() {
544 flags.DynamicLinker += "64"
545 }
546 }
Colin Cross16b23492016-01-06 14:41:07 -0800547 }
548
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700549 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800550 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000551 if Bool(sanitize.Properties.Sanitize.Writeonly) {
552 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
553 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700554 }
555
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700556 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800557 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700558
559 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800560 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
561 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
562 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
563 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700564
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700565 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
566 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
567 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800568 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
569 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700570
Mitch Phillips74384752019-06-17 10:33:52 -0700571 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800572 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700573
574 // Disable fortify for fuzzing builds. Generally, we'll be building with
575 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800576 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800577
578 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
579 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
580 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
581 // the DT_RUNPATH from the shared library above it, and not the executable,
582 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
583 // DT_RUNPATH here means that transient shared libraries can be found
584 // colocated with their parents.
585 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800586 }
587
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700588 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800589 if ctx.Arch().ArchType == android.Arm {
590 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
591 // to do this on a function basis, so force Thumb on the entire module.
592 flags.RequiredInstructionSet = "thumb"
593 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000594
Colin Cross4af21ed2019-11-04 09:37:55 -0800595 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
596 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800597 if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
598 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
599 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000600 // Only append the default visibility flag if -fvisibility has not already been set
601 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800602 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
603 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000604 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800605 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000606
607 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800608 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
609 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000610 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700611 }
612
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700613 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800614 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700615 }
616
Jiyong Park379de2f2018-12-19 02:47:14 +0900617 if len(sanitize.Properties.Sanitizers) > 0 {
618 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800619
Colin Cross4af21ed2019-11-04 09:37:55 -0800620 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
621 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800622 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800623 // Host sanitizers only link symbols in the final executable, so
624 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800625 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
626 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500627
628 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
629 if !ctx.toolchain().Bionic() {
630 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
631 }
632 }
633
634 if enableMinimalRuntime(sanitize) {
635 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
636 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
637 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
638 if !ctx.toolchain().Bionic() {
639 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800640 }
Colin Cross16b23492016-01-06 14:41:07 -0800641 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700642
643 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
644 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800645 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700646 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800647 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700648 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800649 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700650 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800651 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800652 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
653 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800654 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800655 // http://b/171275751, Android doesn't build with this sanitizer yet.
656 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
657 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
658 }
Colin Cross16b23492016-01-06 14:41:07 -0800659 }
660
Jiyong Park379de2f2018-12-19 02:47:14 +0900661 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800662 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700663 }
664 // FIXME: enable RTTI if diag + (cfi or vptr)
665
Andreas Gampe97071162017-05-08 13:15:23 -0700666 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800667 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700668 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
669 }
670
Ivan Lozano7929bba2018-12-12 09:36:31 -0800671 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800672 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800673 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
674 }
675
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700676 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
677 if blocklist.Valid() {
678 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blocklist.String())
679 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
680 }
681
Colin Cross16b23492016-01-06 14:41:07 -0800682 return flags
683}
684
Colin Crossd80cbca2020-02-24 12:01:37 -0800685func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900686 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
687 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800688 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900689 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800690 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900691 }
692 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800693 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900694 }
695 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800696 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900697 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800698 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700699}
700
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700701func (sanitize *sanitize) inSanitizerDir() bool {
702 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700703}
704
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500705// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
706func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000707 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500708 case Asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000709 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700710 case hwasan:
711 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000712 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000713 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000714 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000715 return sanitize.Properties.Sanitize.Integer_overflow
716 case cfi:
717 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800718 case scs:
719 return sanitize.Properties.Sanitize.Scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500720 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700721 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000722 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500723 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000724 }
725}
726
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500727// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800728func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500729 return !sanitize.isSanitizerEnabled(Asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700730 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800731 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800732 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700733 !sanitize.isSanitizerEnabled(scs) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500734 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800735}
736
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500737// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700738func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500739 return !sanitize.isSanitizerEnabled(Asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700740 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700741 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500742 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700743}
744
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500745func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Colin Cross16b23492016-01-06 14:41:07 -0800746 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500747 case Asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700748 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700749 case hwasan:
750 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800751 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700752 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700753 case intOverflow:
754 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000755 case cfi:
756 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800757 case scs:
758 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500759 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700760 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800761 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500762 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800763 }
764 if b {
765 sanitize.Properties.SanitizerEnabled = true
766 }
767}
768
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000769// Check if the sanitizer is explicitly disabled (as opposed to nil by
770// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500771func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000772 if sanitize == nil {
773 return false
774 }
775
776 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
777 return sanitizerVal != nil && *sanitizerVal == false
778}
779
780// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
781// because enabling a sanitizer either directly (via the blueprint) or
782// indirectly (via a mutator) sets the bool ptr to true, and you can't
783// distinguish between the cases. It isn't needed though - both cases can be
784// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500785func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000786 if sanitize == nil {
787 return false
788 }
789
790 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
791 return sanitizerVal != nil && *sanitizerVal == true
792}
793
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500794// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
795func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700796 switch t := tag.(type) {
797 case dependencyTag:
798 return t == reuseObjTag || t == objDepTag
799 case libraryDependencyTag:
800 return true
801 default:
802 return false
803 }
Colin Cross6b753602018-06-21 13:03:07 -0700804}
805
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500806func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
807 return IsSanitizableDependencyTag
808}
809
Inseob Kimc42f2f22020-07-29 20:32:10 +0900810// Determines if the current module is a static library going to be captured
811// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
812// except for ones which explicitly disable cfi.
813func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
Bill Peckham945441c2020-08-31 16:07:58 -0700814 if isVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900815 return false
816 }
817
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500818 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900819
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500820 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900821 return false
822 }
823
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500824 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900825 return false
826 }
827
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500828 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900829 return false
830 }
831
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500832 if !c.SanitizerSupported(cfi) {
833 return false
834 }
835
836 return c.SanitizePropDefined() &&
837 !c.SanitizeNever() &&
838 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900839}
840
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700841// Propagate sanitizer requirements down from binaries
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500842func sanitizerDepsMutator(t SanitizerType) func(android.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700843 return func(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500844 if c, ok := mctx.Module().(PlatformSanitizeable); ok {
845 enabled := c.IsSanitizerEnabled(t)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900846 if t == cfi && needsCfiForVendorSnapshot(mctx) {
847 // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly
848 // determine defaultVariation in sanitizerMutator below.
849 // Instead, just mark SanitizeDep to forcefully create cfi variant.
850 enabled = true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500851 c.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900852 }
853 if enabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500854 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Inseob Kimc42f2f22020-07-29 20:32:10 +0900855 mctx.WalkDeps(func(child, parent android.Module) bool {
856 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
857 return false
858 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500859 if d, ok := child.(PlatformSanitizeable); ok && d.SanitizePropDefined() &&
860 !d.SanitizeNever() &&
861 !d.IsSanitizerExplicitlyDisabled(t) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900862 if t == cfi || t == hwasan || t == scs {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500863 if d.StaticallyLinked() && d.SanitizerSupported(t) {
864 // Rust does not support some of these sanitizers, so we need to check if it's
865 // supported before setting this true.
866 d.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900867 }
868 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500869 d.SetSanitizeDep(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700870 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000871 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900872 return true
873 })
874 }
Jiyong Parkf97782b2019-02-13 20:28:58 +0900875 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
876 // If an APEX module includes a lib which is enabled for a sanitizer T, then
877 // the APEX module is also enabled for the same sanitizer type.
878 mctx.VisitDirectDeps(func(child android.Module) {
879 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
880 sanitizeable.EnableSanitizer(t.name())
881 }
882 })
Colin Cross16b23492016-01-06 14:41:07 -0800883 }
884 }
885}
886
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500887func (c *Module) SanitizeNever() bool {
888 return Bool(c.sanitize.Properties.Sanitize.Never)
889}
890
891func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
892 return c.sanitize.isSanitizerExplicitlyDisabled(t)
893}
894
Ivan Lozano30c5db22018-02-21 15:49:20 -0800895// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700896func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500897 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -0700898 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500899 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -0700900 mctx.WalkDeps(func(child, parent android.Module) bool {
901 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
902 return false
903 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800904
Inseob Kimeec88e12020-01-22 11:11:29 +0900905 d, ok := child.(*Module)
906 if !ok || !d.static() {
907 return false
908 }
909 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700910 if enableMinimalRuntime(d.sanitize) {
911 // If a static dependency is built with the minimal runtime,
912 // make sure we include the ubsan minimal runtime.
913 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +0900914 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -0700915 // If a static dependency runs with full ubsan diagnostics,
916 // make sure we include the ubsan runtime.
917 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800918 }
Colin Cross0b908332019-06-19 23:00:20 -0700919
920 if c.sanitize.Properties.MinimalRuntimeDep &&
921 c.sanitize.Properties.UbsanRuntimeDep {
922 // both flags that this mutator might set are true, so don't bother recursing
923 return false
924 }
925
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500926 if c.Os() == android.Linux {
927 c.sanitize.Properties.BuiltinsDep = true
928 }
929
Colin Cross0b908332019-06-19 23:00:20 -0700930 return true
Colin Cross6b753602018-06-21 13:03:07 -0700931 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900932
Jose Galmesf7294582020-11-13 12:07:36 -0800933 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +0900934 if Bool(p.properties.Sanitize_minimal_dep) {
935 c.sanitize.Properties.MinimalRuntimeDep = true
936 }
937 if Bool(p.properties.Sanitize_ubsan_dep) {
938 c.sanitize.Properties.UbsanRuntimeDep = true
939 }
940 }
941
942 return false
Colin Cross6b753602018-06-21 13:03:07 -0700943 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800944 }
945}
946
Jiyong Park379de2f2018-12-19 02:47:14 +0900947// Add the dependency to the runtime library for each of the sanitizer variants
948func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900949 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000950 if !c.Enabled() {
951 return
952 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900953 var sanitizers []string
954 var diagSanitizers []string
955
956 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
957 sanitizers = append(sanitizers, "undefined")
958 } else {
959 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
960 sanitizers = append(sanitizers,
961 "bool",
962 "integer-divide-by-zero",
963 "return",
964 "returns-nonnull-attribute",
965 "shift-exponent",
966 "unreachable",
967 "vla-bound",
968 // TODO(danalbert): The following checks currently have compiler performance issues.
969 //"alignment",
970 //"bounds",
971 //"enum",
972 //"float-cast-overflow",
973 //"float-divide-by-zero",
974 //"nonnull-attribute",
975 //"null",
976 //"shift-base",
977 //"signed-integer-overflow",
978 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
979 // https://llvm.org/PR19302
980 // http://reviews.llvm.org/D6974
981 // "object-size",
982 )
983 }
984 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
985 }
986
987 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
988 diagSanitizers = append(diagSanitizers, "undefined")
989 }
990
991 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
992
993 if Bool(c.sanitize.Properties.Sanitize.Address) {
994 sanitizers = append(sanitizers, "address")
995 diagSanitizers = append(diagSanitizers, "address")
996 }
997
998 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
999 sanitizers = append(sanitizers, "hwaddress")
1000 }
1001
1002 if Bool(c.sanitize.Properties.Sanitize.Thread) {
1003 sanitizers = append(sanitizers, "thread")
1004 }
1005
1006 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
1007 sanitizers = append(sanitizers, "safe-stack")
1008 }
1009
1010 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
1011 sanitizers = append(sanitizers, "cfi")
1012
1013 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
1014 diagSanitizers = append(diagSanitizers, "cfi")
1015 }
1016 }
1017
1018 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
1019 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1020 sanitizers = append(sanitizers, "signed-integer-overflow")
1021 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
1022 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1023 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1024 }
1025 }
1026
1027 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1028 sanitizers = append(sanitizers, "scudo")
1029 }
1030
1031 if Bool(c.sanitize.Properties.Sanitize.Scs) {
1032 sanitizers = append(sanitizers, "shadow-call-stack")
1033 }
1034
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001035 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
1036 sanitizers = append(sanitizers, "fuzzer-no-link")
1037 }
1038
Jiyong Park379de2f2018-12-19 02:47:14 +09001039 // Save the list of sanitizers. These will be used again when generating
1040 // the build rules (for Cflags, etc.)
1041 c.sanitize.Properties.Sanitizers = sanitizers
1042 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1043
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001044 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1045 if c.Host() {
1046 diagSanitizers = sanitizers
1047 }
1048
Jiyong Park379de2f2018-12-19 02:47:14 +09001049 // Determine the runtime library required
1050 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001051 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001052 toolchain := c.toolchain(mctx)
1053 if Bool(c.sanitize.Properties.Sanitize.Address) {
1054 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1055 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1056 if c.staticBinary() {
1057 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001058 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001059 } else {
1060 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1061 }
1062 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
1063 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1064 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1065 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1066 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1067 } else {
1068 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1069 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001070 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001071 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
1072 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
1073 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001074 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
1075 }
1076
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001077 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
1078 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001079
1080 // Adding dependency to the runtime library. We are using *FarVariation*
1081 // because the runtime libraries themselves are not mutated by sanitizer
1082 // mutators and thus don't have sanitizer variants whereas this module
1083 // has been already mutated.
1084 //
1085 // Note that by adding dependency with {static|shared}DepTag, the lib is
1086 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
1087 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001088 deps := append(extraStaticDeps, runtimeLibrary)
1089 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
1090 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
1091 snapshots := vendorSnapshotStaticLibs(mctx.Config())
1092 for idx, dep := range deps {
1093 if lib, ok := snapshots.get(dep, mctx.Arch().ArchType); ok {
1094 deps[idx] = lib
1095 }
1096 }
1097 }
1098
Jiyong Park379de2f2018-12-19 02:47:14 +09001099 // static executable gets static runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -07001100 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Colin Cross42507332020-08-21 16:15:23 -07001101 variations := append(mctx.Target().Variations(),
1102 blueprint.Variation{Mutator: "link", Variation: "static"})
1103 if c.Device() {
1104 variations = append(variations, c.ImageVariation())
1105 }
1106 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001107 } else if !c.static() && !c.Header() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001108 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
1109 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
1110 snapshots := vendorSnapshotSharedLibs(mctx.Config())
1111 if lib, ok := snapshots.get(runtimeLibrary, mctx.Arch().ArchType); ok {
1112 runtimeLibrary = lib
1113 }
1114 }
Cindy Zhou18417cb2020-12-10 07:12:38 -08001115 // Skip apex dependency check for sharedLibraryDependency
1116 // when sanitizer diags are enabled. Skipping the check will allow
1117 // building with diag libraries without having to list the
1118 // dependency in Apex's allowed_deps file.
1119 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001120 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001121 depTag := libraryDependencyTag{
1122 Kind: sharedLibraryDependency,
1123 Order: earlyLibraryDependency,
1124
1125 skipApexAllowedDependenciesCheck: diagEnabled,
1126 }
Colin Cross42507332020-08-21 16:15:23 -07001127 variations := append(mctx.Target().Variations(),
1128 blueprint.Variation{Mutator: "link", Variation: "shared"})
1129 if c.Device() {
1130 variations = append(variations, c.ImageVariation())
1131 }
Colin Crosse7257d22020-09-24 09:56:18 -07001132 c.addSharedLibDependenciesWithVersions(mctx, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001133 }
1134 // static lib does not have dependency to the runtime library. The
1135 // dependency will be added to the executables or shared libs using
1136 // the static lib.
1137 }
1138 }
1139}
1140
1141type Sanitizeable interface {
1142 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +09001143 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001144 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001145 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001146}
1147
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001148func (c *Module) SanitizePropDefined() bool {
1149 return c.sanitize != nil
1150}
1151
1152func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1153 return c.sanitize.isSanitizerEnabled(t)
1154}
1155
1156func (c *Module) SanitizeDep() bool {
1157 return c.sanitize.Properties.SanitizeDep
1158}
1159
1160func (c *Module) StaticallyLinked() bool {
1161 return c.static()
1162}
1163
1164func (c *Module) SetInSanitizerDir() {
1165 if c.sanitize != nil {
1166 c.sanitize.Properties.InSanitizerDir = true
1167 }
1168}
1169
1170func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1171 if c.sanitize != nil {
1172 c.sanitize.SetSanitizer(t, b)
1173 }
1174}
1175
1176func (c *Module) SetSanitizeDep(b bool) {
1177 if c.sanitize != nil {
1178 c.sanitize.Properties.SanitizeDep = b
1179 }
1180}
1181
1182var _ PlatformSanitizeable = (*Module)(nil)
1183
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001184// Create sanitized variants for modules that need them
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001185func sanitizerMutator(t SanitizerType) func(android.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -07001186 return func(mctx android.BottomUpMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001187 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1188 if c.IsDependencyRoot() && c.IsSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001189 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001190 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1191 } else if c.IsSanitizerEnabled(t) || c.SanitizeDep() {
1192 isSanitizerEnabled := c.IsSanitizerEnabled(t)
1193 if c.StaticallyLinked() || c.Header() || t == Asan || t == Fuzzer {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001194 // Static and header libs are split into non-sanitized and sanitized variants.
1195 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1196 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1197 // that isn't sanitized for asan/fuzzer.
1198 //
1199 // Note for defaultVariation: since we don't split for shared libs but for static/header
1200 // libs, it is possible for the sanitized variant of a static/header lib to depend
1201 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1202 // error when the module is split. defaultVariation is the name of the variation that
1203 // will be used when such a dangling dependency occurs during the split of the current
1204 // module. By setting it to the name of the sanitized variation, the dangling dependency
1205 // is redirected to the sanitized variant of the dependent module.
1206 defaultVariation := t.variationName()
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001207 // Not all PlatformSanitizeable modules support the CFI sanitizer
1208 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001209 mctx.SetDefaultDependencyVariation(&defaultVariation)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001210
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001211 modules := mctx.CreateVariations("", t.variationName())
1212 modules[0].(PlatformSanitizeable).SetSanitizer(t, false)
1213 modules[1].(PlatformSanitizeable).SetSanitizer(t, true)
1214 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
1215 modules[1].(PlatformSanitizeable).SetSanitizeDep(false)
1216
1217 if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
Ivan Lozano4774a812020-03-10 16:23:57 -04001218 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1219 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001220 modules[1].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001221 }
1222
Jiyong Park1d1119f2019-07-29 21:27:18 +09001223 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1224 // to Make, because the sanitized version has a different suffix in name.
1225 // For other types of sanitizers, suppress the variation that is disabled.
1226 if t != cfi && t != scs && t != hwasan {
1227 if isSanitizerEnabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001228 modules[0].(PlatformSanitizeable).SetPreventInstall()
1229 modules[0].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001230 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001231 modules[1].(PlatformSanitizeable).SetPreventInstall()
1232 modules[1].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001233 }
1234 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001235
Jiyong Park1d1119f2019-07-29 21:27:18 +09001236 // Export the static lib name to make
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001237 if c.StaticallyLinked() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001238 if t == cfi {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001239 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001240 } else if t == hwasan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001241 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001242 }
1243 }
1244 } else {
1245 // Shared libs are not split. Only the sanitized variant is created.
1246 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001247 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1248 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
Vishwath Mohane7128792017-11-17 11:08:10 -08001249
Jiyong Park1d1119f2019-07-29 21:27:18 +09001250 // locate the asan libraries under /data/asan
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001251 if mctx.Device() && t == Asan && isSanitizerEnabled {
1252 modules[0].(PlatformSanitizeable).SetInSanitizerDir()
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001253 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001254
1255 if mctx.Device() && t.incompatibleWithCfi() {
1256 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1257 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001258 modules[0].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001259 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001260 }
Colin Cross16b23492016-01-06 14:41:07 -08001261 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001262 c.SetSanitizeDep(false)
Jiyong Park82226632019-02-01 10:50:50 +09001263 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001264 // APEX modules fall here
Jooyung Han8ce8db92020-05-15 19:05:05 +09001265 sanitizeable.AddSanitizerDependencies(mctx, t.name())
Jiyong Park82226632019-02-01 10:50:50 +09001266 mctx.CreateVariations(t.variationName())
Inseob Kimc42f2f22020-07-29 20:32:10 +09001267 } else if c, ok := mctx.Module().(*Module); ok {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001268 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1269
Inseob Kimc42f2f22020-07-29 20:32:10 +09001270 // Check if it's a snapshot module supporting sanitizer
1271 if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) {
1272 // Set default variation as above.
1273 defaultVariation := t.variationName()
1274 mctx.SetDefaultDependencyVariation(&defaultVariation)
1275 modules := mctx.CreateVariations("", t.variationName())
1276 modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false)
1277 modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true)
1278
1279 // Export the static lib name to make
1280 if c.static() && c.ExportedToMake() {
1281 if t == cfi {
1282 // use BaseModuleName which is the name for Make.
1283 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1284 }
1285 }
1286 }
Colin Cross16b23492016-01-06 14:41:07 -08001287 }
1288 }
1289}
Vishwath Mohane7128792017-11-17 11:08:10 -08001290
Inseob Kim74d25562020-08-04 00:41:38 +09001291type sanitizerStaticLibsMap struct {
1292 // libsMap contains one list of modules per each image and each arch.
1293 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001294 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001295 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001296 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001297}
1298
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001299func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001300 return &sanitizerStaticLibsMap{
1301 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001302 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001303 }
1304}
1305
1306// Add the current module to sanitizer static libs maps
1307// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001308func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1309 image := GetImageVariantType(c)
1310 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001311
1312 s.libsMapLock.Lock()
1313 defer s.libsMapLock.Unlock()
1314
1315 if _, ok := s.libsMap[image]; !ok {
1316 s.libsMap[image] = make(map[string][]string)
1317 }
1318
1319 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1320}
1321
1322// Exports makefile variables in the following format:
1323// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1324// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1325// These are to be used by use_soong_sanitized_static_libraries.
1326// See build/make/core/binary.mk for more details.
1327func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1328 for _, image := range android.SortedStringKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001329 archMap := s.libsMap[ImageVariantType(image)]
Inseob Kim74d25562020-08-04 00:41:38 +09001330 for _, arch := range android.SortedStringKeys(archMap) {
1331 libs := archMap[arch]
1332 sort.Strings(libs)
1333
1334 key := fmt.Sprintf(
1335 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1336 s.sanitizerType.variationName(),
1337 image, // already upper
1338 arch)
1339
1340 ctx.Strict(key, strings.Join(libs, " "))
1341 }
1342 }
1343}
1344
Colin Cross571cccf2019-02-04 11:22:08 -08001345var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1346
Inseob Kim74d25562020-08-04 00:41:38 +09001347func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001348 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001349 return newSanitizerStaticLibsMap(cfi)
1350 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001351}
1352
Colin Cross571cccf2019-02-04 11:22:08 -08001353var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1354
Inseob Kim74d25562020-08-04 00:41:38 +09001355func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001356 return config.Once(hwasanStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001357 return newSanitizerStaticLibsMap(hwasan)
1358 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001359}
1360
Ivan Lozano30c5db22018-02-21 15:49:20 -08001361func enableMinimalRuntime(sanitize *sanitize) bool {
1362 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001363 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001364 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001365
Ivan Lozano30c5db22018-02-21 15:49:20 -08001366 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001367 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1368 Bool(sanitize.Properties.Sanitize.Undefined) ||
1369 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1370
Ivan Lozano30c5db22018-02-21 15:49:20 -08001371 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1372 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001373 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001374 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001375
Ivan Lozano30c5db22018-02-21 15:49:20 -08001376 return true
1377 }
1378 return false
1379}
1380
Inseob Kim8471cda2019-11-15 09:59:12 +09001381func enableUbsanRuntime(sanitize *sanitize) bool {
1382 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001383 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001384 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1385}
1386
Vishwath Mohane7128792017-11-17 11:08:10 -08001387func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001388 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001389}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001390
1391func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001392 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001393}