blob: d19e54a3401ee2277f4c628e919c1aa265226829 [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
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070034 asanCflags = []string{"-fno-omit-frame-pointer"}
35 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
36 asanLibs = []string{"libasan"}
37
Evgenii Stepanov0a87b662018-12-07 15:33:24 -080038 hwasanCflags = []string{"-mllvm", "-hwasan-with-ifunc=0", "-fno-omit-frame-pointer", "-Wno-frame-larger-than=", "-mllvm", "-hwasan-create-frame-descriptions=0"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070039
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000040 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070041 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070042 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
43 // used, but have no effect on assembly files
44 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070045 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070046 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070047 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
48 cfiStaticLibsMutex sync.Mutex
49 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070050
Evgenii Stepanov98f5b062018-11-29 15:12:51 -080051 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080052
53 // Pass -Xclang before -fsanitize-minimal-runtime to work around a driver
54 // check which rejects -fsanitize-minimal-runtime together with
55 // -fsanitize=shadow-call-stack even though this combination of flags
56 // is valid.
57 // TODO(pcc): Remove the -Xclang once LLVM r346526 is rolled into the compiler.
58 minimalRuntimeFlags = []string{"-Xclang", "-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070059 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov3c5a52a2018-12-18 17:02:44 -080060 hwasanGlobalOptions = []string{"heap_history_size=1023,stack_history_size=512"}
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
Colin Cross16b23492016-01-06 14:41:07 -080080)
81
82func (t sanitizerType) String() string {
83 switch t {
84 case asan:
85 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070086 case hwasan:
87 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080088 case tsan:
89 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070090 case intOverflow:
91 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000092 case cfi:
93 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080094 case scs:
95 return "scs"
Colin Cross16b23492016-01-06 14:41:07 -080096 default:
97 panic(fmt.Errorf("unknown sanitizerType %d", t))
98 }
99}
100
101type SanitizeProperties struct {
102 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
103 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800104 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800105
106 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700107 Address *bool `android:"arch_variant"`
108 Thread *bool `android:"arch_variant"`
109 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800110
111 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700112 Undefined *bool `android:"arch_variant"`
113 All_undefined *bool `android:"arch_variant"`
114 Misc_undefined []string `android:"arch_variant"`
115 Coverage *bool `android:"arch_variant"`
116 Safestack *bool `android:"arch_variant"`
117 Cfi *bool `android:"arch_variant"`
118 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700119 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800120 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800121
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700122 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
123 // Replaces abort() on error with a human-readable error message.
124 // Address and Thread sanitizers always run in diagnostic mode.
125 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700126 Undefined *bool `android:"arch_variant"`
127 Cfi *bool `android:"arch_variant"`
128 Integer_overflow *bool `android:"arch_variant"`
129 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800130 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700131 }
132
133 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800134 Recover []string
135
136 // value to pass to -fsanitize-blacklist
137 Blacklist *string
138 } `android:"arch_variant"`
139
Jiyong Park379de2f2018-12-19 02:47:14 +0900140 SanitizerEnabled bool `blueprint:"mutated"`
141 SanitizeDep bool `blueprint:"mutated"`
142 MinimalRuntimeDep bool `blueprint:"mutated"`
143 UbsanRuntimeDep bool `blueprint:"mutated"`
144 InSanitizerDir bool `blueprint:"mutated"`
145 Sanitizers []string `blueprint:"mutated"`
146 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800147}
148
149type sanitize struct {
150 Properties SanitizeProperties
151}
152
Vishwath Mohane7128792017-11-17 11:08:10 -0800153func init() {
154 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700155 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800156}
157
Colin Cross16b23492016-01-06 14:41:07 -0800158func (sanitize *sanitize) props() []interface{} {
159 return []interface{}{&sanitize.Properties}
160}
161
162func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700163 s := &sanitize.Properties.Sanitize
164
Colin Cross16b23492016-01-06 14:41:07 -0800165 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700166 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800167 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800168 }
169
170 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800171 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800172 return
173 }
174
Colin Cross16b23492016-01-06 14:41:07 -0800175 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700176 var globalSanitizersDiag []string
177
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700178 if ctx.Host() {
179 if !ctx.Windows() {
180 globalSanitizers = ctx.Config().SanitizeHost()
181 }
182 } else {
183 arches := ctx.Config().SanitizeDeviceArch()
184 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
185 globalSanitizers = ctx.Config().SanitizeDevice()
186 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800187 }
188 }
189
Colin Cross16b23492016-01-06 14:41:07 -0800190 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000191 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700192 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
193 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000194 }
Colin Cross16b23492016-01-06 14:41:07 -0800195
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700196 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
197 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000198 }
199
Evgenii Stepanov774cb812016-12-28 15:52:54 -0800200 if found, globalSanitizers = removeFromList("address", globalSanitizers); found {
201 if s.Address == nil {
202 s.Address = boolPtr(true)
203 } else if *s.Address == false {
204 // Coverage w/o address is an error. If globalSanitizers includes both, and the module
205 // disables address, then disable coverage as well.
206 _, globalSanitizers = removeFromList("coverage", globalSanitizers)
207 }
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000208 }
209
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700210 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
211 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000212 }
213
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700214 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
215 s.Coverage = boolPtr(true)
216 }
217
218 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
219 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000220 }
221
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700222 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800223 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700224 s.Cfi = boolPtr(true)
225 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700226 }
227
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700228 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700229 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700230 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700231 s.Integer_overflow = boolPtr(true)
232 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700233 }
234
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700235 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
236 s.Scudo = boolPtr(true)
237 }
238
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700239 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
240 s.Hwaddress = boolPtr(true)
241 }
242
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000243 if len(globalSanitizers) > 0 {
244 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
245 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700246
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700247 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700248 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700249 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700250 s.Diag.Integer_overflow = boolPtr(true)
251 }
252
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700253 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
254 s.Diag.Cfi == nil && Bool(s.Cfi) {
255 s.Diag.Cfi = boolPtr(true)
256 }
257
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700258 if len(globalSanitizersDiag) > 0 {
259 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
260 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700261 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700262
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700263 // Enable CFI for all components in the include paths (for Aarch64 only)
264 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000265 s.Cfi = boolPtr(true)
266 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
267 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700268 }
269 }
270
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800271 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800272 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800273 s.Cfi = nil
274 s.Diag.Cfi = nil
275 }
276
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800277 // Also disable CFI for arm32 until b/35157333 is fixed.
278 if ctx.Arch().ArchType == android.Arm {
279 s.Cfi = nil
280 s.Diag.Cfi = nil
281 }
282
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700283 // HWASan requires AArch64 hardware feature (top-byte-ignore).
284 if ctx.Arch().ArchType != android.Arm64 {
285 s.Hwaddress = nil
286 }
287
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800288 // SCS is only implemented on AArch64.
289 // We also disable SCS if ASAN, TSAN or HWASAN are enabled because Clang considers
290 // them to be incompatible, although they are in fact compatible.
291 // TODO(pcc): Remove these checks once r347282 is rolled into the compiler.
292 if ctx.Arch().ArchType != android.Arm64 || Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) {
293 s.Scs = nil
294 }
295
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700296 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700297 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700298 s.Cfi = nil
299 s.Diag.Cfi = nil
300 }
301
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700302 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800303 if ctx.Host() {
304 s.Cfi = nil
305 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700306 s.Misc_undefined = nil
307 s.Undefined = nil
308 s.All_undefined = nil
309 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800310 }
311
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700312 // Also disable CFI for VNDK variants of components
313 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700314 s.Cfi = nil
315 s.Diag.Cfi = nil
316 }
317
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700318 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Evgenii Stepanov1e798442018-11-15 17:34:18 -0800319 // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary.
320 if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700321 s.Hwaddress = nil
322 }
323
Colin Cross3c344ef2016-07-18 15:44:56 -0700324 if ctx.staticBinary() {
325 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700326 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700327 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800328 }
329
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700330 if Bool(s.All_undefined) {
331 s.Undefined = nil
332 }
333
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700334 if !ctx.toolchain().Is64Bit() {
335 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700336 s.Thread = nil
337 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800338 // TODO(ccross): error for compile_multilib = "32"?
339 }
340
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800341 if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700342 Bool(s.Coverage) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800343 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700344 sanitize.Properties.SanitizerEnabled = true
345 }
346
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700347 // Disable Scudo if ASan or TSan is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700348 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700349 s.Scudo = nil
350 }
351
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700352 if Bool(s.Hwaddress) {
353 s.Address = nil
354 s.Thread = nil
355 }
356
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700357 if Bool(s.Coverage) {
358 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800359 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
360 }
361 }
362}
363
364func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
365 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
366 return deps
367 }
368
369 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700370 if Bool(sanitize.Properties.Sanitize.Address) {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700371 deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
Colin Cross16b23492016-01-06 14:41:07 -0800372 }
373 }
374
375 return deps
376}
377
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800378func toDisableImplicitIntegerChange(flags []string) bool {
379 // Returns true if any flag is fsanitize*integer, and there is
380 // no explicit flag about sanitize=implicit-integer-sign-change.
381 for _, f := range flags {
382 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
383 return false
384 }
385 }
386 for _, f := range flags {
387 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
388 return true
389 }
390 }
391 return false
392}
393
Colin Cross16b23492016-01-06 14:41:07 -0800394func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700395 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
396 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800397
398 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
399 flags.LdFlags = append(flags.LdFlags, minimalRuntimePath)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700400 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800401 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700402 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800403 return flags
404 }
405
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700406 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700407 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800408 // Frame pointer based unwinder in ASan requires ARM frame setup.
409 // TODO: put in flags?
410 flags.RequiredInstructionSet = "arm"
411 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700412 flags.CFlags = append(flags.CFlags, asanCflags...)
413 flags.LdFlags = append(flags.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800414
Colin Cross16b23492016-01-06 14:41:07 -0800415 if ctx.Host() {
416 // -nodefaultlibs (provided with libc++) prevents the driver from linking
417 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross16b23492016-01-06 14:41:07 -0800418 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
419 } else {
420 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
421 flags.DynamicLinker = "/system/bin/linker_asan"
422 if flags.Toolchain.Is64Bit() {
423 flags.DynamicLinker += "64"
424 }
425 }
Colin Cross16b23492016-01-06 14:41:07 -0800426 }
427
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700428 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
429 flags.CFlags = append(flags.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700430 }
431
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700432 if Bool(sanitize.Properties.Sanitize.Coverage) {
Zach Riggle06bbd892017-08-21 17:12:32 -0400433 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp")
Colin Cross16b23492016-01-06 14:41:07 -0800434 }
435
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700436 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800437 if ctx.Arch().ArchType == android.Arm {
438 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
439 // to do this on a function basis, so force Thumb on the entire module.
440 flags.RequiredInstructionSet = "thumb"
441 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000442
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700443 flags.CFlags = append(flags.CFlags, cfiCflags...)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700444 flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000445 // Only append the default visibility flag if -fvisibility has not already been set
446 // to hidden.
447 if !inList("-fvisibility=hidden", flags.CFlags) {
448 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
449 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700450 flags.LdFlags = append(flags.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000451
452 if ctx.staticBinary() {
453 _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags)
454 _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags)
455 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700456 }
457
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700458 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700459 flags.CFlags = append(flags.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700460 }
461
Jiyong Park379de2f2018-12-19 02:47:14 +0900462 if len(sanitize.Properties.Sanitizers) > 0 {
463 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800464
Colin Cross16b23492016-01-06 14:41:07 -0800465 flags.CFlags = append(flags.CFlags, sanitizeArg)
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -0700466 flags.AsFlags = append(flags.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800467 if ctx.Host() {
468 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
469 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800470 // Host sanitizers only link symbols in the final executable, so
471 // there will always be undefined symbols in intermediate libraries.
472 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800473 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700474 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800475
476 if enableMinimalRuntime(sanitize) {
477 flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " "))
478 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Ivan Lozano59fdea22018-05-10 14:17:22 -0700479 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800480 }
Colin Cross16b23492016-01-06 14:41:07 -0800481 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800482 // http://b/119329758, Android core does not boot up with this sanitizer yet.
483 if toDisableImplicitIntegerChange(flags.CFlags) {
484 flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
485 }
Colin Cross16b23492016-01-06 14:41:07 -0800486 }
487
Jiyong Park379de2f2018-12-19 02:47:14 +0900488 if len(sanitize.Properties.DiagSanitizers) > 0 {
489 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700490 }
491 // FIXME: enable RTTI if diag + (cfi or vptr)
492
Andreas Gampe97071162017-05-08 13:15:23 -0700493 if sanitize.Properties.Sanitize.Recover != nil {
494 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
495 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
496 }
497
Ivan Lozano7929bba2018-12-12 09:36:31 -0800498 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
499 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
500 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
501 }
502
Colin Cross635c3b02016-05-18 15:37:25 -0700503 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800504 if blacklist.Valid() {
505 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
506 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
507 }
508
509 return flags
510}
511
Colin Cross8ff9ef42017-05-08 13:44:11 -0700512func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Vishwath Mohane7128792017-11-17 11:08:10 -0800513 // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a
514 // name conflict.
515 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) {
516 ret.SubName += ".cfi"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000517 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700518 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) {
519 ret.SubName += ".hwasan"
520 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800521 if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) {
522 ret.SubName += ".scs"
523 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700524}
525
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700526func (sanitize *sanitize) inSanitizerDir() bool {
527 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700528}
529
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000530func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000531 switch t {
532 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000533 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700534 case hwasan:
535 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000536 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000537 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000538 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000539 return sanitize.Properties.Sanitize.Integer_overflow
540 case cfi:
541 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800542 case scs:
543 return sanitize.Properties.Sanitize.Scs
Vishwath Mohan95229302017-08-11 00:53:16 +0000544 default:
545 panic(fmt.Errorf("unknown sanitizerType %d", t))
546 }
547}
548
Dan Albert7d1eecf2018-01-19 12:30:45 -0800549func (sanitize *sanitize) isUnsanitizedVariant() bool {
550 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700551 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800552 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800553 !sanitize.isSanitizerEnabled(cfi) &&
554 !sanitize.isSanitizerEnabled(scs)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800555}
556
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700557func (sanitize *sanitize) isVariantOnProductionDevice() bool {
558 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700559 !sanitize.isSanitizerEnabled(hwasan) &&
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700560 !sanitize.isSanitizerEnabled(tsan)
561}
562
Colin Cross16b23492016-01-06 14:41:07 -0800563func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
564 switch t {
565 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700566 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700567 if !b {
568 sanitize.Properties.Sanitize.Coverage = nil
569 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700570 case hwasan:
571 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800572 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700573 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700574 case intOverflow:
575 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000576 case cfi:
577 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800578 case scs:
579 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800580 default:
581 panic(fmt.Errorf("unknown sanitizerType %d", t))
582 }
583 if b {
584 sanitize.Properties.SanitizerEnabled = true
585 }
586}
587
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000588// Check if the sanitizer is explicitly disabled (as opposed to nil by
589// virtue of not being set).
590func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
591 if sanitize == nil {
592 return false
593 }
594
595 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
596 return sanitizerVal != nil && *sanitizerVal == false
597}
598
599// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
600// because enabling a sanitizer either directly (via the blueprint) or
601// indirectly (via a mutator) sets the bool ptr to true, and you can't
602// distinguish between the cases. It isn't needed though - both cases can be
603// treated identically.
604func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
605 if sanitize == nil {
606 return false
607 }
608
609 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
610 return sanitizerVal != nil && *sanitizerVal == true
611}
612
Colin Cross6b753602018-06-21 13:03:07 -0700613func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
614 t, ok := tag.(dependencyTag)
615 return ok && t.library || t == reuseObjTag
616}
617
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700618// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700619func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
620 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000621 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700622 mctx.WalkDeps(func(child, parent android.Module) bool {
623 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
624 return false
625 }
626 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800627 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000628 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800629 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700630 if d.static() {
631 d.sanitize.Properties.SanitizeDep = true
632 }
633 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000634 d.sanitize.Properties.SanitizeDep = true
635 }
Colin Cross16b23492016-01-06 14:41:07 -0800636 }
Colin Cross6b753602018-06-21 13:03:07 -0700637 return true
Colin Cross16b23492016-01-06 14:41:07 -0800638 })
639 }
640 }
641}
642
Ivan Lozano30c5db22018-02-21 15:49:20 -0800643// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700644func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
645 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
646 mctx.WalkDeps(func(child, parent android.Module) bool {
647 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
648 return false
649 }
650 if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800651
Colin Cross6b753602018-06-21 13:03:07 -0700652 if enableMinimalRuntime(d.sanitize) {
653 // If a static dependency is built with the minimal runtime,
654 // make sure we include the ubsan minimal runtime.
655 c.sanitize.Properties.MinimalRuntimeDep = true
656 } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
657 len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 {
658 // If a static dependency runs with full ubsan diagnostics,
659 // make sure we include the ubsan runtime.
660 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800661 }
Colin Cross6b753602018-06-21 13:03:07 -0700662 }
663 return true
664 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800665 }
666}
667
Jiyong Park379de2f2018-12-19 02:47:14 +0900668// Add the dependency to the runtime library for each of the sanitizer variants
669func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
670 if mctx.Os() != android.Android {
671 return
672 }
673 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
674 var sanitizers []string
675 var diagSanitizers []string
676
677 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
678 sanitizers = append(sanitizers, "undefined")
679 } else {
680 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
681 sanitizers = append(sanitizers,
682 "bool",
683 "integer-divide-by-zero",
684 "return",
685 "returns-nonnull-attribute",
686 "shift-exponent",
687 "unreachable",
688 "vla-bound",
689 // TODO(danalbert): The following checks currently have compiler performance issues.
690 //"alignment",
691 //"bounds",
692 //"enum",
693 //"float-cast-overflow",
694 //"float-divide-by-zero",
695 //"nonnull-attribute",
696 //"null",
697 //"shift-base",
698 //"signed-integer-overflow",
699 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
700 // https://llvm.org/PR19302
701 // http://reviews.llvm.org/D6974
702 // "object-size",
703 )
704 }
705 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
706 }
707
708 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
709 diagSanitizers = append(diagSanitizers, "undefined")
710 }
711
712 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
713
714 if Bool(c.sanitize.Properties.Sanitize.Address) {
715 sanitizers = append(sanitizers, "address")
716 diagSanitizers = append(diagSanitizers, "address")
717 }
718
719 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
720 sanitizers = append(sanitizers, "hwaddress")
721 }
722
723 if Bool(c.sanitize.Properties.Sanitize.Thread) {
724 sanitizers = append(sanitizers, "thread")
725 }
726
727 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
728 sanitizers = append(sanitizers, "safe-stack")
729 }
730
731 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
732 sanitizers = append(sanitizers, "cfi")
733
734 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
735 diagSanitizers = append(diagSanitizers, "cfi")
736 }
737 }
738
739 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
740 sanitizers = append(sanitizers, "unsigned-integer-overflow")
741 sanitizers = append(sanitizers, "signed-integer-overflow")
742 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
743 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
744 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
745 }
746 }
747
748 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
749 sanitizers = append(sanitizers, "scudo")
750 }
751
752 if Bool(c.sanitize.Properties.Sanitize.Scs) {
753 sanitizers = append(sanitizers, "shadow-call-stack")
754 }
755
756 // Save the list of sanitizers. These will be used again when generating
757 // the build rules (for Cflags, etc.)
758 c.sanitize.Properties.Sanitizers = sanitizers
759 c.sanitize.Properties.DiagSanitizers = diagSanitizers
760
761 // Determine the runtime library required
762 runtimeLibrary := ""
763 toolchain := c.toolchain(mctx)
764 if Bool(c.sanitize.Properties.Sanitize.Address) {
765 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
766 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
767 if c.staticBinary() {
768 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
769 } else {
770 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
771 }
772 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
773 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
774 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
775 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
776 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
777 } else {
778 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
779 }
780 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep {
781 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
782 }
783
784 if mctx.Device() && runtimeLibrary != "" {
785 if inList(runtimeLibrary, llndkLibraries) && !c.static() {
786 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
787 }
788
789 // Adding dependency to the runtime library. We are using *FarVariation*
790 // because the runtime libraries themselves are not mutated by sanitizer
791 // mutators and thus don't have sanitizer variants whereas this module
792 // has been already mutated.
793 //
794 // Note that by adding dependency with {static|shared}DepTag, the lib is
795 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
796 if c.staticBinary() {
797 // static executable gets static runtime libs
798 mctx.AddFarVariationDependencies([]blueprint.Variation{
799 {Mutator: "link", Variation: "static"},
800 {Mutator: "arch", Variation: mctx.Target().String()},
801 }, staticDepTag, runtimeLibrary)
802 } else if !c.static() {
803 // dynamic executable andshared libs get shared runtime libs
804 mctx.AddFarVariationDependencies([]blueprint.Variation{
805 {Mutator: "link", Variation: "shared"},
806 {Mutator: "arch", Variation: mctx.Target().String()},
807 }, sharedDepTag, runtimeLibrary)
808 }
809 // static lib does not have dependency to the runtime library. The
810 // dependency will be added to the executables or shared libs using
811 // the static lib.
812 }
813 }
814}
815
816type Sanitizeable interface {
817 android.Module
818 IsSanitizerEnabled() bool
819}
820
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000821// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700822func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
823 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000824 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000825 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700826 modules := mctx.CreateVariations(t.String())
827 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000828 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
829 // Save original sanitizer status before we assign values to variant
830 // 0 as that overwrites the original.
831 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
832
Colin Crossb0f28952016-09-19 16:46:53 -0700833 modules := mctx.CreateVariations("", t.String())
834 modules[0].(*Module).sanitize.SetSanitizer(t, false)
835 modules[1].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000836
Colin Crossb0f28952016-09-19 16:46:53 -0700837 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
838 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -0800839
840 // We don't need both variants active for anything but CFI-enabled
841 // target static libraries, so suppress the appropriate variant in
842 // all other cases.
843 if t == cfi {
844 if c.static() {
845 if !mctx.Device() {
846 if isSanitizerEnabled {
847 modules[0].(*Module).Properties.PreventInstall = true
848 modules[0].(*Module).Properties.HideFromMake = true
849 } else {
850 modules[1].(*Module).Properties.PreventInstall = true
851 modules[1].(*Module).Properties.HideFromMake = true
852 }
853 } else {
Colin Cross6510f912017-11-29 00:27:14 -0800854 cfiStaticLibs := cfiStaticLibs(mctx.Config())
Vishwath Mohane7128792017-11-17 11:08:10 -0800855
856 cfiStaticLibsMutex.Lock()
857 *cfiStaticLibs = append(*cfiStaticLibs, c.Name())
858 cfiStaticLibsMutex.Unlock()
859 }
860 } else {
861 modules[0].(*Module).Properties.PreventInstall = true
862 modules[0].(*Module).Properties.HideFromMake = true
863 }
864 } else if t == asan {
865 if mctx.Device() {
866 // CFI and ASAN are currently mutually exclusive so disable
867 // CFI if this is an ASAN variant.
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000868 modules[1].(*Module).sanitize.Properties.InSanitizerDir = true
869 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
870 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700871 if isSanitizerEnabled {
872 modules[0].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800873 modules[0].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700874 } else {
875 modules[1].(*Module).Properties.PreventInstall = true
Vishwath Mohane7128792017-11-17 11:08:10 -0800876 modules[1].(*Module).Properties.HideFromMake = true
Vishwath Mohane21fe422017-11-01 19:42:45 -0700877 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800878 } else if t == scs {
879 // We don't currently link any static libraries built with make into
880 // libraries built with SCS, so we don't need logic for propagating
881 // SCSness of dependencies into make.
882 if !c.static() {
883 if isSanitizerEnabled {
884 modules[0].(*Module).Properties.PreventInstall = true
885 modules[0].(*Module).Properties.HideFromMake = true
886 } else {
887 modules[1].(*Module).Properties.PreventInstall = true
888 modules[1].(*Module).Properties.HideFromMake = true
889 }
890 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700891 } else if t == hwasan {
892 if mctx.Device() {
893 // CFI and HWASAN are currently mutually exclusive so disable
894 // CFI if this is an HWASAN variant.
895 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
896 }
897
898 if c.static() {
899 if c.useVndk() {
900 hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config())
901 hwasanStaticLibsMutex.Lock()
902 *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name())
903 hwasanStaticLibsMutex.Unlock()
904 } else {
905 hwasanStaticLibs := hwasanStaticLibs(mctx.Config())
906 hwasanStaticLibsMutex.Lock()
907 *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name())
908 hwasanStaticLibsMutex.Unlock()
909 }
910 } else {
911 if isSanitizerEnabled {
912 modules[0].(*Module).Properties.PreventInstall = true
913 modules[0].(*Module).Properties.HideFromMake = true
914 } else {
915 modules[1].(*Module).Properties.PreventInstall = true
916 modules[1].(*Module).Properties.HideFromMake = true
917 }
918 }
Vishwath Mohane21fe422017-11-01 19:42:45 -0700919 }
Colin Cross16b23492016-01-06 14:41:07 -0800920 }
921 c.sanitize.Properties.SanitizeDep = false
Jiyong Park379de2f2018-12-19 02:47:14 +0900922 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled() {
923 // APEX modules fall here
924 mctx.CreateVariations(t.String())
Colin Cross16b23492016-01-06 14:41:07 -0800925 }
926 }
927}
Vishwath Mohane7128792017-11-17 11:08:10 -0800928
929func cfiStaticLibs(config android.Config) *[]string {
930 return config.Once("cfiStaticLibs", func() interface{} {
931 return &[]string{}
932 }).(*[]string)
933}
934
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700935func hwasanStaticLibs(config android.Config) *[]string {
936 return config.Once("hwasanStaticLibs", func() interface{} {
937 return &[]string{}
938 }).(*[]string)
939}
940
941func hwasanVendorStaticLibs(config android.Config) *[]string {
942 return config.Once("hwasanVendorStaticLibs", func() interface{} {
943 return &[]string{}
944 }).(*[]string)
945}
946
Ivan Lozano30c5db22018-02-21 15:49:20 -0800947func enableMinimalRuntime(sanitize *sanitize) bool {
948 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700949 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -0800950 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
951 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
952 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
953 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
954 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
955 return true
956 }
957 return false
958}
959
Vishwath Mohane7128792017-11-17 11:08:10 -0800960func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
961 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -0800962 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -0800963 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
964}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700965
966func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
967 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
968 sort.Strings(*hwasanStaticLibs)
969 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
970
971 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
972 sort.Strings(*hwasanVendorStaticLibs)
973 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
974}