blob: f568d5a681c96c68bc6382161e7ef7c4cf6fb299 [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 Stepanov1c69e832019-06-14 18:37:33 -070041 "-fsanitize-hwaddress-abi=platform",
42 "-fno-experimental-new-pass-manager"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070043
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000044 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070045 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070046 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
47 // used, but have no effect on assembly files
48 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070049 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070050 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070051 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
52 cfiStaticLibsMutex sync.Mutex
53 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070054
Evgenii Stepanov98f5b062018-11-29 15:12:51 -080055 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080056
Peter Collingbournebd19db02019-03-06 10:38:48 -080057 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070058 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070059 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
60 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070061)
62
Colin Cross16b23492016-01-06 14:41:07 -080063type sanitizerType int
64
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070065func boolPtr(v bool) *bool {
66 if v {
67 return &v
68 } else {
69 return nil
70 }
71}
72
Colin Cross16b23492016-01-06 14:41:07 -080073const (
74 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070075 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080076 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070077 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000078 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080079 scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -070080 fuzzer
Colin Cross16b23492016-01-06 14:41:07 -080081)
82
Jiyong Park82226632019-02-01 10:50:50 +090083// Name of the sanitizer variation for this sanitizer type
84func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080085 switch t {
86 case asan:
87 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070088 case hwasan:
89 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080090 case tsan:
91 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070092 case intOverflow:
93 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000094 case cfi:
95 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080096 case scs:
97 return "scs"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -070098 case fuzzer:
99 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800100 default:
101 panic(fmt.Errorf("unknown sanitizerType %d", t))
102 }
103}
104
Jiyong Park82226632019-02-01 10:50:50 +0900105// This is the sanitizer names in SANITIZE_[TARGET|HOST]
106func (t sanitizerType) name() string {
107 switch t {
108 case asan:
109 return "address"
110 case hwasan:
111 return "hwaddress"
112 case tsan:
113 return "thread"
114 case intOverflow:
115 return "integer_overflow"
116 case cfi:
117 return "cfi"
118 case scs:
119 return "shadow-call-stack"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700120 case fuzzer:
121 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900122 default:
123 panic(fmt.Errorf("unknown sanitizerType %d", t))
124 }
125}
126
Jiyong Park1d1119f2019-07-29 21:27:18 +0900127func (t sanitizerType) incompatibleWithCfi() bool {
128 return t == asan || t == fuzzer || t == hwasan
129}
130
Colin Cross16b23492016-01-06 14:41:07 -0800131type SanitizeProperties struct {
132 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
133 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800134 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800135
136 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700137 Address *bool `android:"arch_variant"`
138 Thread *bool `android:"arch_variant"`
139 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800140
141 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700142 Undefined *bool `android:"arch_variant"`
143 All_undefined *bool `android:"arch_variant"`
144 Misc_undefined []string `android:"arch_variant"`
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700145 Fuzzer *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700146 Safestack *bool `android:"arch_variant"`
147 Cfi *bool `android:"arch_variant"`
148 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700149 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800150 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800151
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700152 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
153 // Replaces abort() on error with a human-readable error message.
154 // Address and Thread sanitizers always run in diagnostic mode.
155 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700156 Undefined *bool `android:"arch_variant"`
157 Cfi *bool `android:"arch_variant"`
158 Integer_overflow *bool `android:"arch_variant"`
159 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800160 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700161 }
162
163 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800164 Recover []string
165
166 // value to pass to -fsanitize-blacklist
167 Blacklist *string
168 } `android:"arch_variant"`
169
Jiyong Park379de2f2018-12-19 02:47:14 +0900170 SanitizerEnabled bool `blueprint:"mutated"`
171 SanitizeDep bool `blueprint:"mutated"`
172 MinimalRuntimeDep bool `blueprint:"mutated"`
173 UbsanRuntimeDep bool `blueprint:"mutated"`
174 InSanitizerDir bool `blueprint:"mutated"`
175 Sanitizers []string `blueprint:"mutated"`
176 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800177}
178
179type sanitize struct {
180 Properties SanitizeProperties
181}
182
Vishwath Mohane7128792017-11-17 11:08:10 -0800183func init() {
184 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700185 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800186}
187
Colin Cross16b23492016-01-06 14:41:07 -0800188func (sanitize *sanitize) props() []interface{} {
189 return []interface{}{&sanitize.Properties}
190}
191
192func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700193 s := &sanitize.Properties.Sanitize
194
Colin Cross16b23492016-01-06 14:41:07 -0800195 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700196 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800197 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800198 }
199
Doug Hornc32c6b02019-01-17 14:44:05 -0800200 // Sanitizers do not work on Fuchsia yet.
201 if ctx.Fuchsia() {
202 s.Never = BoolPtr(true)
203 }
204
Colin Cross16b23492016-01-06 14:41:07 -0800205 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800206 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800207 return
208 }
209
Colin Cross16b23492016-01-06 14:41:07 -0800210 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700211 var globalSanitizersDiag []string
212
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700213 if ctx.Host() {
214 if !ctx.Windows() {
215 globalSanitizers = ctx.Config().SanitizeHost()
216 }
217 } else {
218 arches := ctx.Config().SanitizeDeviceArch()
219 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
220 globalSanitizers = ctx.Config().SanitizeDevice()
221 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800222 }
223 }
224
Colin Cross16b23492016-01-06 14:41:07 -0800225 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000226 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700227 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
228 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000229 }
Colin Cross16b23492016-01-06 14:41:07 -0800230
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700231 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
232 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000233 }
234
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700235 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
236 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000237 }
238
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700239 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
240 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000241 }
242
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700243 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
244 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700245 }
246
247 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
248 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000249 }
250
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700251 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800252 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700253 s.Cfi = boolPtr(true)
254 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700255 }
256
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700257 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700258 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700259 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700260 s.Integer_overflow = boolPtr(true)
261 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700262 }
263
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700264 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
265 s.Scudo = boolPtr(true)
266 }
267
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700268 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
269 s.Hwaddress = boolPtr(true)
270 }
271
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000272 if len(globalSanitizers) > 0 {
273 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
274 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700275
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700276 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700277 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700278 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700279 s.Diag.Integer_overflow = boolPtr(true)
280 }
281
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700282 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
283 s.Diag.Cfi == nil && Bool(s.Cfi) {
284 s.Diag.Cfi = boolPtr(true)
285 }
286
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700287 if len(globalSanitizersDiag) > 0 {
288 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
289 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700290 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700291
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700292 // Enable CFI for all components in the include paths (for Aarch64 only)
293 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000294 s.Cfi = boolPtr(true)
295 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
296 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700297 }
298 }
299
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800300 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800301 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800302 s.Cfi = nil
303 s.Diag.Cfi = nil
304 }
305
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800306 // Also disable CFI for arm32 until b/35157333 is fixed.
307 if ctx.Arch().ArchType == android.Arm {
308 s.Cfi = nil
309 s.Diag.Cfi = nil
310 }
311
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700312 // HWASan requires AArch64 hardware feature (top-byte-ignore).
313 if ctx.Arch().ArchType != android.Arm64 {
314 s.Hwaddress = nil
315 }
316
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800317 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800318 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800319 s.Scs = nil
320 }
321
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700322 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700323 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700324 s.Cfi = nil
325 s.Diag.Cfi = nil
326 }
327
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700328 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800329 if ctx.Host() {
330 s.Cfi = nil
331 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700332 s.Misc_undefined = nil
333 s.Undefined = nil
334 s.All_undefined = nil
335 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800336 }
337
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700338 // Also disable CFI for VNDK variants of components
339 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700340 s.Cfi = nil
341 s.Diag.Cfi = nil
342 }
343
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700344 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800345 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
346 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700347 s.Hwaddress = nil
348 }
349
Colin Cross3c344ef2016-07-18 15:44:56 -0700350 if ctx.staticBinary() {
351 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700352 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700353 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800354 }
355
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700356 if Bool(s.All_undefined) {
357 s.Undefined = nil
358 }
359
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700360 if !ctx.toolchain().Is64Bit() {
361 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700362 s.Thread = nil
363 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800364 // TODO(ccross): error for compile_multilib = "32"?
365 }
366
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800367 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 -0700368 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 -0800369 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700370 sanitize.Properties.SanitizerEnabled = true
371 }
372
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800373 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
374 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700375 s.Scudo = nil
376 }
377
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700378 if Bool(s.Hwaddress) {
379 s.Address = nil
380 s.Thread = nil
381 }
382
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700383 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
384 // mutually incompatible.
385 if Bool(s.Fuzzer) {
386 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800387 }
388}
389
390func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
391 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
392 return deps
393 }
394
395 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700396 if Bool(sanitize.Properties.Sanitize.Address) {
Christopher Ferris753d4a62019-05-09 13:27:02 -0700397 // Compiling asan and having libc_scudo in the same
398 // executable will cause the executable to crash.
399 // Remove libc_scudo since it is only used to override
400 // allocation functions which asan already overrides.
401 _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800402 }
403 }
404
405 return deps
406}
407
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800408func toDisableImplicitIntegerChange(flags []string) bool {
409 // Returns true if any flag is fsanitize*integer, and there is
410 // no explicit flag about sanitize=implicit-integer-sign-change.
411 for _, f := range flags {
412 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
413 return false
414 }
415 }
416 for _, f := range flags {
417 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
418 return true
419 }
420 }
421 return false
422}
423
Colin Cross16b23492016-01-06 14:41:07 -0800424func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700425 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
426 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800427
428 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
429 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700430 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800431 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700432 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800433 return flags
434 }
435
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700436 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700437 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800438 // Frame pointer based unwinder in ASan requires ARM frame setup.
439 // TODO: put in flags?
440 flags.RequiredInstructionSet = "arm"
441 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700442 flags.CFlags = append(flags.CFlags, asanCflags...)
443 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800444
Colin Cross16b23492016-01-06 14:41:07 -0800445 if ctx.Host() {
446 // -nodefaultlibs (provided with libc++) prevents the driver from linking
447 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800448 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
449 } else {
Yi Kongda069082019-08-22 00:46:36 +0000450 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900451 if ctx.bootstrap() {
452 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
453 } else {
454 flags.DynamicLinker = "/system/bin/linker_asan"
455 }
Colin Cross16b23492016-01-06 14:41:07 -0800456 if flags.Toolchain.Is64Bit() {
457 flags.DynamicLinker += "64"
458 }
459 }
Colin Cross16b23492016-01-06 14:41:07 -0800460 }
461
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700462 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
463 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700464 }
465
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700466 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
467 flags.CFlags = append(flags.CFlags, "-fsanitize=fuzzer-no-link")
468 flags.LdFlags = append(flags.LdFlags, "-fsanitize=fuzzer-no-link")
469
470 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
471 _, flags.LdFlags = removeFromList("-flto", flags.LdFlags)
Mitch Phillips34b493f2019-08-02 16:57:55 -0700472 _, flags.CFlags = removeFromList("-flto", flags.CFlags)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700473 flags.LdFlags = append(flags.LdFlags, "-fno-lto")
Mitch Phillips34b493f2019-08-02 16:57:55 -0700474 flags.CFlags = append(flags.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700475
476 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
477 _, flags.CFlags = removeFromList("-fexperimental-new-pass-manager", flags.CFlags)
478 flags.CFlags = append(flags.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700479
480 // Disable fortify for fuzzing builds. Generally, we'll be building with
481 // UBSan or ASan here and the fortify checks pollute the stack traces.
482 _, flags.CFlags = removeFromList("-D_FORTIFY_SOURCE=1", flags.CFlags)
483 _, flags.CFlags = removeFromList("-D_FORTIFY_SOURCE=2", flags.CFlags)
484 flags.CFlags = append(flags.CFlags, "-U_FORTIFY_SOURCE")
Colin Cross16b23492016-01-06 14:41:07 -0800485 }
486
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700487 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800488 if ctx.Arch().ArchType == android.Arm {
489 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
490 // to do this on a function basis, so force Thumb on the entire module.
491 flags.RequiredInstructionSet = "thumb"
492 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000493
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700494 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700495 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000496 // Only append the default visibility flag if -fvisibility has not already been set
497 // to hidden.
498 if !inList("-fvisibility=hidden", flags.CFlags) {
499 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
500 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700501 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000502
503 if ctx.staticBinary() {
504 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
505 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
506 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700507 }
508
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700509 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700510 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700511 }
512
Jiyong Park379de2f2018-12-19 02:47:14 +0900513 if len(sanitize.Properties.Sanitizers) > 0 {
514 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800515
Colin Cross16b23492016-01-06 14:41:07 -0800516 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700517 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800518 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800519 // Host sanitizers only link symbols in the final executable, so
520 // there will always be undefined symbols in intermediate libraries.
521 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700522 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800523 } else {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800524 if enableMinimalRuntime(sanitize) {
525 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
526 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700527 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800528 }
Colin Cross16b23492016-01-06 14:41:07 -0800529 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700530
531 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
532 // When fuzzing, we wish to crash with diagnostics on any bug.
533 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
534 } else if ctx.Host() {
535 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
536 } else {
537 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
538 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800539 // http://b/119329758, Android core does not boot up with this sanitizer yet.
540 if toDisableImplicitIntegerChange(flags.CFlags) {
541 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
542 }
Colin Cross16b23492016-01-06 14:41:07 -0800543 }
544
Jiyong Park379de2f2018-12-19 02:47:14 +0900545 if len(sanitize.Properties.DiagSanitizers) > 0 {
546 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700547 }
548 // FIXME: enable RTTI if diag + (cfi or vptr)
549
Andreas Gampe97071162017-05-08 13:15:23 -0700550 if sanitize.Properties.Sanitize.Recover != nil {
551 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
552 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
553 }
554
Ivan Lozano7929bba2018-12-12 09:36:31 -0800555 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
556 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
557 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
558 }
559
Colin Cross635c3b02016-05-18 15:37:25 -0700560 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800561 if blacklist.Valid() {
562 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
563 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
564 }
565
566 return flags
567}
568
Colin Cross8ff9ef42017-05-08 13:44:11 -0700569func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900570 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
571 // both the sanitized and non-sanitized variants to make without a name conflict.
572 if ret.Class == "STATIC_LIBRARIES" || ret.Class == "HEADER_LIBRARIES" {
573 if Bool(sanitize.Properties.Sanitize.Cfi) {
574 ret.SubName += ".cfi"
575 }
576 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
577 ret.SubName += ".hwasan"
578 }
579 if Bool(sanitize.Properties.Sanitize.Scs) {
580 ret.SubName += ".scs"
581 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800582 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700583}
584
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700585func (sanitize *sanitize) inSanitizerDir() bool {
586 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700587}
588
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000589func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000590 switch t {
591 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000592 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700593 case hwasan:
594 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000595 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000596 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000597 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000598 return sanitize.Properties.Sanitize.Integer_overflow
599 case cfi:
600 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800601 case scs:
602 return sanitize.Properties.Sanitize.Scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700603 case fuzzer:
604 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000605 default:
606 panic(fmt.Errorf("unknown sanitizerType %d", t))
607 }
608}
609
Dan Albert7d1eecf2018-01-19 12:30:45 -0800610func (sanitize *sanitize) isUnsanitizedVariant() bool {
611 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700612 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800613 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800614 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700615 !sanitize.isSanitizerEnabled(scs) &&
616 !sanitize.isSanitizerEnabled(fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800617}
618
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700619func (sanitize *sanitize) isVariantOnProductionDevice() bool {
620 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700621 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700622 !sanitize.isSanitizerEnabled(tsan) &&
623 !sanitize.isSanitizerEnabled(fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700624}
625
Colin Cross16b23492016-01-06 14:41:07 -0800626func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
627 switch t {
628 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700629 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700630 case hwasan:
631 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800632 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700633 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700634 case intOverflow:
635 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000636 case cfi:
637 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800638 case scs:
639 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700640 case fuzzer:
641 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800642 default:
643 panic(fmt.Errorf("unknown sanitizerType %d", t))
644 }
645 if b {
646 sanitize.Properties.SanitizerEnabled = true
647 }
648}
649
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000650// Check if the sanitizer is explicitly disabled (as opposed to nil by
651// virtue of not being set).
652func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
653 if sanitize == nil {
654 return false
655 }
656
657 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
658 return sanitizerVal != nil && *sanitizerVal == false
659}
660
661// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
662// because enabling a sanitizer either directly (via the blueprint) or
663// indirectly (via a mutator) sets the bool ptr to true, and you can't
664// distinguish between the cases. It isn't needed though - both cases can be
665// treated identically.
666func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
667 if sanitize == nil {
668 return false
669 }
670
671 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
672 return sanitizerVal != nil && *sanitizerVal == true
673}
674
Colin Cross6b753602018-06-21 13:03:07 -0700675func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
676 t, ok := tag.(dependencyTag)
Peter Collingbourne1c648b82019-09-26 12:24:45 -0700677 return ok && t.library || t == reuseObjTag || t == objDepTag
Colin Cross6b753602018-06-21 13:03:07 -0700678}
679
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700680// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700681func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
682 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000683 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700684 mctx.WalkDeps(func(child, parent android.Module) bool {
685 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
686 return false
687 }
688 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800689 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000690 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800691 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700692 if d.static() {
693 d.sanitize.Properties.SanitizeDep = true
694 }
695 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000696 d.sanitize.Properties.SanitizeDep = true
697 }
Colin Cross16b23492016-01-06 14:41:07 -0800698 }
Colin Cross6b753602018-06-21 13:03:07 -0700699 return true
Colin Cross16b23492016-01-06 14:41:07 -0800700 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900701 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
702 // If an APEX module includes a lib which is enabled for a sanitizer T, then
703 // the APEX module is also enabled for the same sanitizer type.
704 mctx.VisitDirectDeps(func(child android.Module) {
705 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
706 sanitizeable.EnableSanitizer(t.name())
707 }
708 })
Colin Cross16b23492016-01-06 14:41:07 -0800709 }
710 }
711}
712
Ivan Lozano30c5db22018-02-21 15:49:20 -0800713// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700714func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
715 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
716 mctx.WalkDeps(func(child, parent android.Module) bool {
717 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
718 return false
719 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800720
Colin Cross0b908332019-06-19 23:00:20 -0700721 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700722 if enableMinimalRuntime(d.sanitize) {
723 // If a static dependency is built with the minimal runtime,
724 // make sure we include the ubsan minimal runtime.
725 c.sanitize.Properties.MinimalRuntimeDep = true
726 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
727 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
728 // If a static dependency runs with full ubsan diagnostics,
729 // make sure we include the ubsan runtime.
730 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800731 }
Colin Cross0b908332019-06-19 23:00:20 -0700732
733 if c.sanitize.Properties.MinimalRuntimeDep &&
734 c.sanitize.Properties.UbsanRuntimeDep {
735 // both flags that this mutator might set are true, so don't bother recursing
736 return false
737 }
738
739 return true
740 } else {
741 return false
Colin Cross6b753602018-06-21 13:03:07 -0700742 }
Colin Cross6b753602018-06-21 13:03:07 -0700743 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800744 }
745}
746
Jiyong Park379de2f2018-12-19 02:47:14 +0900747// Add the dependency to the runtime library for each of the sanitizer variants
748func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900749 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000750 if !c.Enabled() {
751 return
752 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900753 var sanitizers []string
754 var diagSanitizers []string
755
756 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
757 sanitizers = append(sanitizers, "undefined")
758 } else {
759 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
760 sanitizers = append(sanitizers,
761 "bool",
762 "integer-divide-by-zero",
763 "return",
764 "returns-nonnull-attribute",
765 "shift-exponent",
766 "unreachable",
767 "vla-bound",
768 // TODO(danalbert): The following checks currently have compiler performance issues.
769 //"alignment",
770 //"bounds",
771 //"enum",
772 //"float-cast-overflow",
773 //"float-divide-by-zero",
774 //"nonnull-attribute",
775 //"null",
776 //"shift-base",
777 //"signed-integer-overflow",
778 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
779 // https://llvm.org/PR19302
780 // http://reviews.llvm.org/D6974
781 // "object-size",
782 )
783 }
784 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
785 }
786
787 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
788 diagSanitizers = append(diagSanitizers, "undefined")
789 }
790
791 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
792
793 if Bool(c.sanitize.Properties.Sanitize.Address) {
794 sanitizers = append(sanitizers, "address")
795 diagSanitizers = append(diagSanitizers, "address")
796 }
797
798 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
799 sanitizers = append(sanitizers, "hwaddress")
800 }
801
802 if Bool(c.sanitize.Properties.Sanitize.Thread) {
803 sanitizers = append(sanitizers, "thread")
804 }
805
806 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
807 sanitizers = append(sanitizers, "safe-stack")
808 }
809
810 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
811 sanitizers = append(sanitizers, "cfi")
812
813 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
814 diagSanitizers = append(diagSanitizers, "cfi")
815 }
816 }
817
818 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
819 sanitizers = append(sanitizers, "unsigned-integer-overflow")
820 sanitizers = append(sanitizers, "signed-integer-overflow")
821 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
822 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
823 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
824 }
825 }
826
827 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
828 sanitizers = append(sanitizers, "scudo")
829 }
830
831 if Bool(c.sanitize.Properties.Sanitize.Scs) {
832 sanitizers = append(sanitizers, "shadow-call-stack")
833 }
834
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700835 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
836 sanitizers = append(sanitizers, "fuzzer-no-link")
837 }
838
Jiyong Park379de2f2018-12-19 02:47:14 +0900839 // Save the list of sanitizers. These will be used again when generating
840 // the build rules (for Cflags, etc.)
841 c.sanitize.Properties.Sanitizers = sanitizers
842 c.sanitize.Properties.DiagSanitizers = diagSanitizers
843
844 // Determine the runtime library required
845 runtimeLibrary := ""
846 toolchain := c.toolchain(mctx)
847 if Bool(c.sanitize.Properties.Sanitize.Address) {
848 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
849 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
850 if c.staticBinary() {
851 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
852 } else {
853 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
854 }
855 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
856 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
857 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
858 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
859 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
860 } else {
861 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
862 }
863 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
864 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
865 }
866
867 if mctx.Device() && runtimeLibrary != "" {
Inseob Kim9516ee92019-05-09 10:56:13 +0900868 if inList(runtimeLibrary, *llndkLibraries(mctx.Config())) && !c.static() && c.useVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900869 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
870 }
871
872 // Adding dependency to the runtime library. We are using *FarVariation*
873 // because the runtime libraries themselves are not mutated by sanitizer
874 // mutators and thus don't have sanitizer variants whereas this module
875 // has been already mutated.
876 //
877 // Note that by adding dependency with {static|shared}DepTag, the lib is
878 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
879 if c.staticBinary() {
880 // static executable gets static runtime libs
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700881 mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{
Jiyong Park379de2f2018-12-19 02:47:14 +0900882 {Mutator: "link", Variation: "static"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900883 {Mutator: "image", Variation: c.imageVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700884 }...), staticDepTag, runtimeLibrary)
Jiyong Park1d1119f2019-07-29 21:27:18 +0900885 } else if !c.static() && !c.header() {
Jiyong Park3b1746a2019-01-29 11:15:04 +0900886 // dynamic executable and shared libs get shared runtime libs
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700887 mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{
Jiyong Park379de2f2018-12-19 02:47:14 +0900888 {Mutator: "link", Variation: "shared"},
Jiyong Park3b1746a2019-01-29 11:15:04 +0900889 {Mutator: "image", Variation: c.imageVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700890 }...), earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900891 }
892 // static lib does not have dependency to the runtime library. The
893 // dependency will be added to the executables or shared libs using
894 // the static lib.
895 }
896 }
897}
898
899type Sanitizeable interface {
900 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900901 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900902 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900903}
904
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000905// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700906func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
907 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000908 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000909 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900910 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700911 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000912 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000913 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
Jiyong Park1d1119f2019-07-29 21:27:18 +0900914 if mctx.Device() && t.incompatibleWithCfi() {
915 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
916 // are incompatible with cfi
917 c.sanitize.SetSanitizer(cfi, false)
918 }
919 if c.static() || c.header() || t == asan || t == fuzzer {
920 // Static and header libs are split into non-sanitized and sanitized variants.
921 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
922 // libs because a library sanitized for asan/fuzzer can't be linked from a library
923 // that isn't sanitized for asan/fuzzer.
924 //
925 // Note for defaultVariation: since we don't split for shared libs but for static/header
926 // libs, it is possible for the sanitized variant of a static/header lib to depend
927 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
928 // error when the module is split. defaultVariation is the name of the variation that
929 // will be used when such a dangling dependency occurs during the split of the current
930 // module. By setting it to the name of the sanitized variation, the dangling dependency
931 // is redirected to the sanitized variant of the dependent module.
932 defaultVariation := t.variationName()
933 mctx.SetDefaultDependencyVariation(&defaultVariation)
934 modules := mctx.CreateVariations("", t.variationName())
935 modules[0].(*Module).sanitize.SetSanitizer(t, false)
936 modules[1].(*Module).sanitize.SetSanitizer(t, true)
937 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
938 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000939
Jiyong Park1d1119f2019-07-29 21:27:18 +0900940 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
941 // to Make, because the sanitized version has a different suffix in name.
942 // For other types of sanitizers, suppress the variation that is disabled.
943 if t != cfi && t != scs && t != hwasan {
944 if isSanitizerEnabled {
945 modules[0].(*Module).Properties.PreventInstall = true
946 modules[0].(*Module).Properties.HideFromMake = true
947 } else {
948 modules[1].(*Module).Properties.PreventInstall = true
949 modules[1].(*Module).Properties.HideFromMake = true
950 }
951 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000952
Jiyong Park1d1119f2019-07-29 21:27:18 +0900953 // Export the static lib name to make
Vishwath Mohane7128792017-11-17 11:08:10 -0800954 if c.static() {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900955 if t == cfi {
956 appendStringSync(c.Name(), cfiStaticLibs(mctx.Config()), &cfiStaticLibsMutex)
957 } else if t == hwasan {
958 if c.useVndk() {
959 appendStringSync(c.Name(), hwasanVendorStaticLibs(mctx.Config()),
960 &hwasanStaticLibsMutex)
Vishwath Mohane7128792017-11-17 11:08:10 -0800961 } else {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900962 appendStringSync(c.Name(), hwasanStaticLibs(mctx.Config()),
963 &hwasanStaticLibsMutex)
Vishwath Mohane7128792017-11-17 11:08:10 -0800964 }
Jiyong Park1d1119f2019-07-29 21:27:18 +0900965 }
966 }
967 } else {
968 // Shared libs are not split. Only the sanitized variant is created.
969 modules := mctx.CreateVariations(t.variationName())
970 modules[0].(*Module).sanitize.SetSanitizer(t, true)
971 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800972
Jiyong Park1d1119f2019-07-29 21:27:18 +0900973 // locate the asan libraries under /data/asan
974 if mctx.Device() && t == asan && isSanitizerEnabled {
975 modules[0].(*Module).sanitize.Properties.InSanitizerDir = true
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700976 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700977 }
Colin Cross16b23492016-01-06 14:41:07 -0800978 }
979 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +0900980 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900981 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +0900982 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -0800983 }
984 }
985}
Vishwath Mohane7128792017-11-17 11:08:10 -0800986
Colin Cross571cccf2019-02-04 11:22:08 -0800987var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
988
Vishwath Mohane7128792017-11-17 11:08:10 -0800989func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800990 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -0800991 return &[]string{}
992 }).(*[]string)
993}
994
Colin Cross571cccf2019-02-04 11:22:08 -0800995var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
996
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700997func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800998 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700999 return &[]string{}
1000 }).(*[]string)
1001}
1002
Colin Cross571cccf2019-02-04 11:22:08 -08001003var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
1004
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001005func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001006 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001007 return &[]string{}
1008 }).(*[]string)
1009}
1010
Jiyong Park1d1119f2019-07-29 21:27:18 +09001011func appendStringSync(item string, list *[]string, mutex *sync.Mutex) {
1012 mutex.Lock()
1013 *list = append(*list, item)
1014 mutex.Unlock()
1015}
1016
Ivan Lozano30c5db22018-02-21 15:49:20 -08001017func enableMinimalRuntime(sanitize *sanitize) bool {
1018 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001019 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001020 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -08001021 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
1022 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
1023 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1024 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
1025 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
1026 return true
1027 }
1028 return false
1029}
1030
Vishwath Mohane7128792017-11-17 11:08:10 -08001031func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1032 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001033 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001034 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1035}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001036
1037func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1038 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1039 sort.Strings(*hwasanStaticLibs)
1040 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1041
1042 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1043 sort.Strings(*hwasanVendorStaticLibs)
1044 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1045}