blob: 5663aa7aef38dc042b795df017ea4cf66e95691a [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",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080042 "-fno-experimental-new-pass-manager",
43 // The following improves debug location information
44 // availability at the cost of its accuracy. It increases
45 // the likelihood of a stack variable's frame offset
46 // to be recorded in the debug info, which is important
47 // for the quality of hwasan reports. The downside is a
48 // higher number of "optimized out" stack variables.
49 // b/112437883.
50 "-mllvm", "-instcombine-lower-dbg-declare=0",
51 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070052
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000053 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070054 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070055 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
56 // used, but have no effect on assembly files
57 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070058 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070059 "-Wl,-plugin-opt,O1"}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070060 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
61 cfiStaticLibsMutex sync.Mutex
62 hwasanStaticLibsMutex sync.Mutex
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070063
Evgenii Stepanov98f5b062018-11-29 15:12:51 -080064 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080065
Peter Collingbournebd19db02019-03-06 10:38:48 -080066 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070067 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070068 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
69 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070070)
71
Colin Cross16b23492016-01-06 14:41:07 -080072type sanitizerType int
73
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070074func boolPtr(v bool) *bool {
75 if v {
76 return &v
77 } else {
78 return nil
79 }
80}
81
Colin Cross16b23492016-01-06 14:41:07 -080082const (
83 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070084 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080085 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070086 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000087 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080088 scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -070089 fuzzer
Colin Cross16b23492016-01-06 14:41:07 -080090)
91
Jiyong Park82226632019-02-01 10:50:50 +090092// Name of the sanitizer variation for this sanitizer type
93func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080094 switch t {
95 case asan:
96 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070097 case hwasan:
98 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080099 case tsan:
100 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700101 case intOverflow:
102 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000103 case cfi:
104 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800105 case scs:
106 return "scs"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700107 case fuzzer:
108 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800109 default:
110 panic(fmt.Errorf("unknown sanitizerType %d", t))
111 }
112}
113
Jiyong Park82226632019-02-01 10:50:50 +0900114// This is the sanitizer names in SANITIZE_[TARGET|HOST]
115func (t sanitizerType) name() string {
116 switch t {
117 case asan:
118 return "address"
119 case hwasan:
120 return "hwaddress"
121 case tsan:
122 return "thread"
123 case intOverflow:
124 return "integer_overflow"
125 case cfi:
126 return "cfi"
127 case scs:
128 return "shadow-call-stack"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700129 case fuzzer:
130 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900131 default:
132 panic(fmt.Errorf("unknown sanitizerType %d", t))
133 }
134}
135
Jiyong Park1d1119f2019-07-29 21:27:18 +0900136func (t sanitizerType) incompatibleWithCfi() bool {
137 return t == asan || t == fuzzer || t == hwasan
138}
139
Colin Cross16b23492016-01-06 14:41:07 -0800140type SanitizeProperties struct {
141 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
142 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800143 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800144
145 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700146 Address *bool `android:"arch_variant"`
147 Thread *bool `android:"arch_variant"`
148 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800149
150 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700151 Undefined *bool `android:"arch_variant"`
152 All_undefined *bool `android:"arch_variant"`
153 Misc_undefined []string `android:"arch_variant"`
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700154 Fuzzer *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700155 Safestack *bool `android:"arch_variant"`
156 Cfi *bool `android:"arch_variant"`
157 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700158 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800159 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800160
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700161 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
162 // Replaces abort() on error with a human-readable error message.
163 // Address and Thread sanitizers always run in diagnostic mode.
164 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700165 Undefined *bool `android:"arch_variant"`
166 Cfi *bool `android:"arch_variant"`
167 Integer_overflow *bool `android:"arch_variant"`
168 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800169 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700170 }
171
172 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800173 Recover []string
174
175 // value to pass to -fsanitize-blacklist
176 Blacklist *string
177 } `android:"arch_variant"`
178
Jiyong Park379de2f2018-12-19 02:47:14 +0900179 SanitizerEnabled bool `blueprint:"mutated"`
180 SanitizeDep bool `blueprint:"mutated"`
181 MinimalRuntimeDep bool `blueprint:"mutated"`
182 UbsanRuntimeDep bool `blueprint:"mutated"`
183 InSanitizerDir bool `blueprint:"mutated"`
184 Sanitizers []string `blueprint:"mutated"`
185 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800186}
187
188type sanitize struct {
189 Properties SanitizeProperties
190}
191
Vishwath Mohane7128792017-11-17 11:08:10 -0800192func init() {
193 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700194 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800195}
196
Colin Cross16b23492016-01-06 14:41:07 -0800197func (sanitize *sanitize) props() []interface{} {
198 return []interface{}{&sanitize.Properties}
199}
200
201func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700202 s := &sanitize.Properties.Sanitize
203
Colin Cross16b23492016-01-06 14:41:07 -0800204 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700205 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800206 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800207 }
208
Doug Hornc32c6b02019-01-17 14:44:05 -0800209 // Sanitizers do not work on Fuchsia yet.
210 if ctx.Fuchsia() {
211 s.Never = BoolPtr(true)
212 }
213
Colin Cross16b23492016-01-06 14:41:07 -0800214 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800215 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800216 return
217 }
218
Colin Cross16b23492016-01-06 14:41:07 -0800219 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700220 var globalSanitizersDiag []string
221
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700222 if ctx.Host() {
223 if !ctx.Windows() {
224 globalSanitizers = ctx.Config().SanitizeHost()
225 }
226 } else {
227 arches := ctx.Config().SanitizeDeviceArch()
228 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
229 globalSanitizers = ctx.Config().SanitizeDevice()
230 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800231 }
232 }
233
Colin Cross16b23492016-01-06 14:41:07 -0800234 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000235 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700236 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
237 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000238 }
Colin Cross16b23492016-01-06 14:41:07 -0800239
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700240 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
241 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000242 }
243
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700244 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
245 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000246 }
247
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700248 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
249 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000250 }
251
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700252 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
253 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700254 }
255
256 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
257 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000258 }
259
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700260 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800261 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700262 s.Cfi = boolPtr(true)
263 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700264 }
265
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700266 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700267 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700268 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700269 s.Integer_overflow = boolPtr(true)
270 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700271 }
272
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700273 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
274 s.Scudo = boolPtr(true)
275 }
276
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700277 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
278 s.Hwaddress = boolPtr(true)
279 }
280
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000281 if len(globalSanitizers) > 0 {
282 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
283 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700284
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700285 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700286 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700287 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700288 s.Diag.Integer_overflow = boolPtr(true)
289 }
290
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700291 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
292 s.Diag.Cfi == nil && Bool(s.Cfi) {
293 s.Diag.Cfi = boolPtr(true)
294 }
295
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700296 if len(globalSanitizersDiag) > 0 {
297 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
298 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700299 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700300
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700301 // Enable CFI for all components in the include paths (for Aarch64 only)
302 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000303 s.Cfi = boolPtr(true)
304 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
305 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700306 }
307 }
308
Evgenii Stepanova83fdac2017-01-31 18:37:30 -0800309 // CFI needs gold linker, and mips toolchain does not have one.
Colin Cross6510f912017-11-29 00:27:14 -0800310 if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800311 s.Cfi = nil
312 s.Diag.Cfi = nil
313 }
314
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800315 // Also disable CFI for arm32 until b/35157333 is fixed.
316 if ctx.Arch().ArchType == android.Arm {
317 s.Cfi = nil
318 s.Diag.Cfi = nil
319 }
320
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700321 // HWASan requires AArch64 hardware feature (top-byte-ignore).
322 if ctx.Arch().ArchType != android.Arm64 {
323 s.Hwaddress = nil
324 }
325
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800326 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800327 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800328 s.Scs = nil
329 }
330
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700331 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700332 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700333 s.Cfi = nil
334 s.Diag.Cfi = nil
335 }
336
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700337 // Disable sanitizers that depend on the UBSan runtime for host builds.
Vishwath Mohane7128792017-11-17 11:08:10 -0800338 if ctx.Host() {
339 s.Cfi = nil
340 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700341 s.Misc_undefined = nil
342 s.Undefined = nil
343 s.All_undefined = nil
344 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800345 }
346
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700347 // Also disable CFI for VNDK variants of components
348 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700349 s.Cfi = nil
350 s.Diag.Cfi = nil
351 }
352
Inseob Kimeec88e12020-01-22 11:11:29 +0900353 // Also disable CFI if building against snapshot.
354 vndkVersion := ctx.DeviceConfig().VndkVersion()
355 if ctx.useVndk() && vndkVersion != "current" && vndkVersion != "" {
356 s.Cfi = nil
357 }
358
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700359 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong1b3348d2020-01-21 15:53:22 -0800360 // Keep libc instrumented so that ramdisk / recovery can run hwasan-instrumented code if necessary.
361 if (ctx.inRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700362 s.Hwaddress = nil
363 }
364
Colin Cross3c344ef2016-07-18 15:44:56 -0700365 if ctx.staticBinary() {
366 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700367 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700368 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800369 }
370
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700371 if Bool(s.All_undefined) {
372 s.Undefined = nil
373 }
374
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700375 if !ctx.toolchain().Is64Bit() {
376 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700377 s.Thread = nil
378 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800379 // TODO(ccross): error for compile_multilib = "32"?
380 }
381
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800382 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 -0700383 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 -0800384 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700385 sanitize.Properties.SanitizerEnabled = true
386 }
387
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800388 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
389 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700390 s.Scudo = nil
391 }
392
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700393 if Bool(s.Hwaddress) {
394 s.Address = nil
395 s.Thread = nil
396 }
397
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700398 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
399 // mutually incompatible.
400 if Bool(s.Fuzzer) {
401 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800402 }
403}
404
405func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
406 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
407 return deps
408 }
409
410 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700411 if Bool(sanitize.Properties.Sanitize.Address) {
Christopher Ferris753d4a62019-05-09 13:27:02 -0700412 // Compiling asan and having libc_scudo in the same
413 // executable will cause the executable to crash.
414 // Remove libc_scudo since it is only used to override
415 // allocation functions which asan already overrides.
416 _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800417 }
418 }
419
420 return deps
421}
422
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800423func toDisableImplicitIntegerChange(flags []string) bool {
424 // Returns true if any flag is fsanitize*integer, and there is
425 // no explicit flag about sanitize=implicit-integer-sign-change.
426 for _, f := range flags {
427 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
428 return false
429 }
430 }
431 for _, f := range flags {
432 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
433 return true
434 }
435 }
436 return false
437}
438
Colin Cross16b23492016-01-06 14:41:07 -0800439func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700440 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
441 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800442
443 if ctx.Device() && sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800444 flags.Local.LdFlags = append(flags.Local.LdFlags,
445 minimalRuntimePath,
446 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800447 }
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700448 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800449 return flags
450 }
451
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700452 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700453 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800454 // Frame pointer based unwinder in ASan requires ARM frame setup.
455 // TODO: put in flags?
456 flags.RequiredInstructionSet = "arm"
457 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800458 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
459 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800460
Colin Cross16b23492016-01-06 14:41:07 -0800461 if ctx.Host() {
462 // -nodefaultlibs (provided with libc++) prevents the driver from linking
463 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800464 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800465 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800466 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900467 if ctx.bootstrap() {
468 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
469 } else {
470 flags.DynamicLinker = "/system/bin/linker_asan"
471 }
Colin Cross16b23492016-01-06 14:41:07 -0800472 if flags.Toolchain.Is64Bit() {
473 flags.DynamicLinker += "64"
474 }
475 }
Colin Cross16b23492016-01-06 14:41:07 -0800476 }
477
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700478 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800479 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700480 }
481
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700482 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800483 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700484
485 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800486 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
487 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
488 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
489 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700490
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700491 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
492 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
493 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800494 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
495 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700496
Mitch Phillips74384752019-06-17 10:33:52 -0700497 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800498 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700499
500 // Disable fortify for fuzzing builds. Generally, we'll be building with
501 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800502 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800503
504 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
505 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
506 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
507 // the DT_RUNPATH from the shared library above it, and not the executable,
508 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
509 // DT_RUNPATH here means that transient shared libraries can be found
510 // colocated with their parents.
511 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800512 }
513
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700514 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800515 if ctx.Arch().ArchType == android.Arm {
516 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
517 // to do this on a function basis, so force Thumb on the entire module.
518 flags.RequiredInstructionSet = "thumb"
519 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000520
Colin Cross4af21ed2019-11-04 09:37:55 -0800521 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
522 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000523 // Only append the default visibility flag if -fvisibility has not already been set
524 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800525 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
526 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000527 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800528 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000529
530 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800531 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
532 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000533 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700534 }
535
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700536 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800537 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700538 }
539
Jiyong Park379de2f2018-12-19 02:47:14 +0900540 if len(sanitize.Properties.Sanitizers) > 0 {
541 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800542
Colin Cross4af21ed2019-11-04 09:37:55 -0800543 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
544 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800545 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800546 // Host sanitizers only link symbols in the final executable, so
547 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800548 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
549 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800550 } else {
Ivan Lozano30c5db22018-02-21 15:49:20 -0800551 if enableMinimalRuntime(sanitize) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800552 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
Ivan Lozano30c5db22018-02-21 15:49:20 -0800553 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
Colin Cross4af21ed2019-11-04 09:37:55 -0800554 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800555 }
Colin Cross16b23492016-01-06 14:41:07 -0800556 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700557
558 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
559 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800560 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700561 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800562 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700563 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800564 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700565 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800566 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800567 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
568 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800569 }
Colin Cross16b23492016-01-06 14:41:07 -0800570 }
571
Jiyong Park379de2f2018-12-19 02:47:14 +0900572 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800573 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700574 }
575 // FIXME: enable RTTI if diag + (cfi or vptr)
576
Andreas Gampe97071162017-05-08 13:15:23 -0700577 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800578 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700579 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
580 }
581
Ivan Lozano7929bba2018-12-12 09:36:31 -0800582 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800583 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800584 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
585 }
586
Colin Cross635c3b02016-05-18 15:37:25 -0700587 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800588 if blacklist.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800589 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blacklist.String())
Colin Cross16b23492016-01-06 14:41:07 -0800590 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
591 }
592
593 return flags
594}
595
Colin Cross8ff9ef42017-05-08 13:44:11 -0700596func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900597 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
598 // both the sanitized and non-sanitized variants to make without a name conflict.
599 if ret.Class == "STATIC_LIBRARIES" || ret.Class == "HEADER_LIBRARIES" {
600 if Bool(sanitize.Properties.Sanitize.Cfi) {
601 ret.SubName += ".cfi"
602 }
603 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
604 ret.SubName += ".hwasan"
605 }
606 if Bool(sanitize.Properties.Sanitize.Scs) {
607 ret.SubName += ".scs"
608 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800609 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700610}
611
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700612func (sanitize *sanitize) inSanitizerDir() bool {
613 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700614}
615
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000616func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000617 switch t {
618 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000619 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700620 case hwasan:
621 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000622 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000623 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000624 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000625 return sanitize.Properties.Sanitize.Integer_overflow
626 case cfi:
627 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800628 case scs:
629 return sanitize.Properties.Sanitize.Scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700630 case fuzzer:
631 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000632 default:
633 panic(fmt.Errorf("unknown sanitizerType %d", t))
634 }
635}
636
Dan Albert7d1eecf2018-01-19 12:30:45 -0800637func (sanitize *sanitize) isUnsanitizedVariant() bool {
638 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700639 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800640 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800641 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700642 !sanitize.isSanitizerEnabled(scs) &&
643 !sanitize.isSanitizerEnabled(fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800644}
645
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700646func (sanitize *sanitize) isVariantOnProductionDevice() bool {
647 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700648 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700649 !sanitize.isSanitizerEnabled(tsan) &&
650 !sanitize.isSanitizerEnabled(fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700651}
652
Colin Cross16b23492016-01-06 14:41:07 -0800653func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
654 switch t {
655 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700656 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700657 case hwasan:
658 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800659 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700660 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700661 case intOverflow:
662 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000663 case cfi:
664 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800665 case scs:
666 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700667 case fuzzer:
668 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800669 default:
670 panic(fmt.Errorf("unknown sanitizerType %d", t))
671 }
672 if b {
673 sanitize.Properties.SanitizerEnabled = true
674 }
675}
676
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000677// Check if the sanitizer is explicitly disabled (as opposed to nil by
678// virtue of not being set).
679func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
680 if sanitize == nil {
681 return false
682 }
683
684 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
685 return sanitizerVal != nil && *sanitizerVal == false
686}
687
688// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
689// because enabling a sanitizer either directly (via the blueprint) or
690// indirectly (via a mutator) sets the bool ptr to true, and you can't
691// distinguish between the cases. It isn't needed though - both cases can be
692// treated identically.
693func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
694 if sanitize == nil {
695 return false
696 }
697
698 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
699 return sanitizerVal != nil && *sanitizerVal == true
700}
701
Colin Cross6b753602018-06-21 13:03:07 -0700702func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Ivan Lozano183a3212019-10-18 14:18:45 -0700703 t, ok := tag.(DependencyTag)
704 return ok && t.Library || t == reuseObjTag || t == objDepTag
Colin Cross6b753602018-06-21 13:03:07 -0700705}
706
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700707// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700708func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
709 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000710 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700711 mctx.WalkDeps(func(child, parent android.Module) bool {
712 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
713 return false
714 }
715 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800716 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000717 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800718 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700719 if d.static() {
720 d.sanitize.Properties.SanitizeDep = true
721 }
722 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000723 d.sanitize.Properties.SanitizeDep = true
724 }
Colin Cross16b23492016-01-06 14:41:07 -0800725 }
Colin Cross6b753602018-06-21 13:03:07 -0700726 return true
Colin Cross16b23492016-01-06 14:41:07 -0800727 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900728 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
729 // If an APEX module includes a lib which is enabled for a sanitizer T, then
730 // the APEX module is also enabled for the same sanitizer type.
731 mctx.VisitDirectDeps(func(child android.Module) {
732 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
733 sanitizeable.EnableSanitizer(t.name())
734 }
735 })
Colin Cross16b23492016-01-06 14:41:07 -0800736 }
737 }
738}
739
Ivan Lozano30c5db22018-02-21 15:49:20 -0800740// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700741func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
742 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
743 mctx.WalkDeps(func(child, parent android.Module) bool {
744 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
745 return false
746 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800747
Inseob Kimeec88e12020-01-22 11:11:29 +0900748 d, ok := child.(*Module)
749 if !ok || !d.static() {
750 return false
751 }
752 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700753 if enableMinimalRuntime(d.sanitize) {
754 // If a static dependency is built with the minimal runtime,
755 // make sure we include the ubsan minimal runtime.
756 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +0900757 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -0700758 // If a static dependency runs with full ubsan diagnostics,
759 // make sure we include the ubsan runtime.
760 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800761 }
Colin Cross0b908332019-06-19 23:00:20 -0700762
763 if c.sanitize.Properties.MinimalRuntimeDep &&
764 c.sanitize.Properties.UbsanRuntimeDep {
765 // both flags that this mutator might set are true, so don't bother recursing
766 return false
767 }
768
769 return true
Colin Cross6b753602018-06-21 13:03:07 -0700770 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900771
772 if p, ok := d.linker.(*vendorSnapshotLibraryDecorator); ok {
773 if Bool(p.properties.Sanitize_minimal_dep) {
774 c.sanitize.Properties.MinimalRuntimeDep = true
775 }
776 if Bool(p.properties.Sanitize_ubsan_dep) {
777 c.sanitize.Properties.UbsanRuntimeDep = true
778 }
779 }
780
781 return false
Colin Cross6b753602018-06-21 13:03:07 -0700782 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800783 }
784}
785
Jiyong Park379de2f2018-12-19 02:47:14 +0900786// Add the dependency to the runtime library for each of the sanitizer variants
787func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900788 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000789 if !c.Enabled() {
790 return
791 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900792 var sanitizers []string
793 var diagSanitizers []string
794
795 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
796 sanitizers = append(sanitizers, "undefined")
797 } else {
798 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
799 sanitizers = append(sanitizers,
800 "bool",
801 "integer-divide-by-zero",
802 "return",
803 "returns-nonnull-attribute",
804 "shift-exponent",
805 "unreachable",
806 "vla-bound",
807 // TODO(danalbert): The following checks currently have compiler performance issues.
808 //"alignment",
809 //"bounds",
810 //"enum",
811 //"float-cast-overflow",
812 //"float-divide-by-zero",
813 //"nonnull-attribute",
814 //"null",
815 //"shift-base",
816 //"signed-integer-overflow",
817 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
818 // https://llvm.org/PR19302
819 // http://reviews.llvm.org/D6974
820 // "object-size",
821 )
822 }
823 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
824 }
825
826 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
827 diagSanitizers = append(diagSanitizers, "undefined")
828 }
829
830 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
831
832 if Bool(c.sanitize.Properties.Sanitize.Address) {
833 sanitizers = append(sanitizers, "address")
834 diagSanitizers = append(diagSanitizers, "address")
835 }
836
837 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
838 sanitizers = append(sanitizers, "hwaddress")
839 }
840
841 if Bool(c.sanitize.Properties.Sanitize.Thread) {
842 sanitizers = append(sanitizers, "thread")
843 }
844
845 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
846 sanitizers = append(sanitizers, "safe-stack")
847 }
848
849 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
850 sanitizers = append(sanitizers, "cfi")
851
852 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
853 diagSanitizers = append(diagSanitizers, "cfi")
854 }
855 }
856
857 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
858 sanitizers = append(sanitizers, "unsigned-integer-overflow")
859 sanitizers = append(sanitizers, "signed-integer-overflow")
860 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
861 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
862 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
863 }
864 }
865
866 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
867 sanitizers = append(sanitizers, "scudo")
868 }
869
870 if Bool(c.sanitize.Properties.Sanitize.Scs) {
871 sanitizers = append(sanitizers, "shadow-call-stack")
872 }
873
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700874 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
875 sanitizers = append(sanitizers, "fuzzer-no-link")
876 }
877
Jiyong Park379de2f2018-12-19 02:47:14 +0900878 // Save the list of sanitizers. These will be used again when generating
879 // the build rules (for Cflags, etc.)
880 c.sanitize.Properties.Sanitizers = sanitizers
881 c.sanitize.Properties.DiagSanitizers = diagSanitizers
882
883 // Determine the runtime library required
884 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700885 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +0900886 toolchain := c.toolchain(mctx)
887 if Bool(c.sanitize.Properties.Sanitize.Address) {
888 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
889 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
890 if c.staticBinary() {
891 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700892 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +0900893 } else {
894 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
895 }
896 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
897 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
898 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
899 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
900 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
901 } else {
902 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
903 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700904 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
905 Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900906 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
907 }
908
909 if mctx.Device() && runtimeLibrary != "" {
Jooyung Han0302a842019-10-30 18:43:49 +0900910 if isLlndkLibrary(runtimeLibrary, mctx.Config()) && !c.static() && c.UseVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900911 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
912 }
913
914 // Adding dependency to the runtime library. We are using *FarVariation*
915 // because the runtime libraries themselves are not mutated by sanitizer
916 // mutators and thus don't have sanitizer variants whereas this module
917 // has been already mutated.
918 //
919 // Note that by adding dependency with {static|shared}DepTag, the lib is
920 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
921 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +0900922 deps := append(extraStaticDeps, runtimeLibrary)
923 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
924 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
925 snapshots := vendorSnapshotStaticLibs(mctx.Config())
926 for idx, dep := range deps {
927 if lib, ok := snapshots.get(dep, mctx.Arch().ArchType); ok {
928 deps[idx] = lib
929 }
930 }
931 }
932
Jiyong Park379de2f2018-12-19 02:47:14 +0900933 // static executable gets static runtime libs
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700934 mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{
Jiyong Park379de2f2018-12-19 02:47:14 +0900935 {Mutator: "link", Variation: "static"},
Colin Cross7228ecd2019-11-18 16:00:16 -0800936 c.ImageVariation(),
Inseob Kimeec88e12020-01-22 11:11:29 +0900937 }...), StaticDepTag, deps...)
Jiyong Park1d1119f2019-07-29 21:27:18 +0900938 } else if !c.static() && !c.header() {
Inseob Kimeec88e12020-01-22 11:11:29 +0900939 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
940 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
941 snapshots := vendorSnapshotSharedLibs(mctx.Config())
942 if lib, ok := snapshots.get(runtimeLibrary, mctx.Arch().ArchType); ok {
943 runtimeLibrary = lib
944 }
945 }
946
Jiyong Park3b1746a2019-01-29 11:15:04 +0900947 // dynamic executable and shared libs get shared runtime libs
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700948 mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{
Jiyong Park379de2f2018-12-19 02:47:14 +0900949 {Mutator: "link", Variation: "shared"},
Colin Cross7228ecd2019-11-18 16:00:16 -0800950 c.ImageVariation(),
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700951 }...), earlySharedDepTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900952 }
953 // static lib does not have dependency to the runtime library. The
954 // dependency will be added to the executables or shared libs using
955 // the static lib.
956 }
957 }
958}
959
960type Sanitizeable interface {
961 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900962 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900963 EnableSanitizer(sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900964}
965
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000966// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700967func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
968 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +0000969 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000970 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +0900971 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -0700972 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000973 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000974 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
Jiyong Park1d1119f2019-07-29 21:27:18 +0900975 if mctx.Device() && t.incompatibleWithCfi() {
976 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
977 // are incompatible with cfi
978 c.sanitize.SetSanitizer(cfi, false)
979 }
980 if c.static() || c.header() || t == asan || t == fuzzer {
981 // Static and header libs are split into non-sanitized and sanitized variants.
982 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
983 // libs because a library sanitized for asan/fuzzer can't be linked from a library
984 // that isn't sanitized for asan/fuzzer.
985 //
986 // Note for defaultVariation: since we don't split for shared libs but for static/header
987 // libs, it is possible for the sanitized variant of a static/header lib to depend
988 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
989 // error when the module is split. defaultVariation is the name of the variation that
990 // will be used when such a dangling dependency occurs during the split of the current
991 // module. By setting it to the name of the sanitized variation, the dangling dependency
992 // is redirected to the sanitized variant of the dependent module.
993 defaultVariation := t.variationName()
994 mctx.SetDefaultDependencyVariation(&defaultVariation)
995 modules := mctx.CreateVariations("", t.variationName())
996 modules[0].(*Module).sanitize.SetSanitizer(t, false)
997 modules[1].(*Module).sanitize.SetSanitizer(t, true)
998 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
999 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001000
Jiyong Park1d1119f2019-07-29 21:27:18 +09001001 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1002 // to Make, because the sanitized version has a different suffix in name.
1003 // For other types of sanitizers, suppress the variation that is disabled.
1004 if t != cfi && t != scs && t != hwasan {
1005 if isSanitizerEnabled {
1006 modules[0].(*Module).Properties.PreventInstall = true
1007 modules[0].(*Module).Properties.HideFromMake = true
1008 } else {
1009 modules[1].(*Module).Properties.PreventInstall = true
1010 modules[1].(*Module).Properties.HideFromMake = true
1011 }
1012 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001013
Jiyong Park1d1119f2019-07-29 21:27:18 +09001014 // Export the static lib name to make
Vishwath Mohane7128792017-11-17 11:08:10 -08001015 if c.static() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001016 if t == cfi {
1017 appendStringSync(c.Name(), cfiStaticLibs(mctx.Config()), &cfiStaticLibsMutex)
1018 } else if t == hwasan {
Ivan Lozano52767be2019-10-18 14:49:46 -07001019 if c.UseVndk() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001020 appendStringSync(c.Name(), hwasanVendorStaticLibs(mctx.Config()),
1021 &hwasanStaticLibsMutex)
Vishwath Mohane7128792017-11-17 11:08:10 -08001022 } else {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001023 appendStringSync(c.Name(), hwasanStaticLibs(mctx.Config()),
1024 &hwasanStaticLibsMutex)
Vishwath Mohane7128792017-11-17 11:08:10 -08001025 }
Jiyong Park1d1119f2019-07-29 21:27:18 +09001026 }
1027 }
1028 } else {
1029 // Shared libs are not split. Only the sanitized variant is created.
1030 modules := mctx.CreateVariations(t.variationName())
1031 modules[0].(*Module).sanitize.SetSanitizer(t, true)
1032 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -08001033
Jiyong Park1d1119f2019-07-29 21:27:18 +09001034 // locate the asan libraries under /data/asan
1035 if mctx.Device() && t == asan && isSanitizerEnabled {
1036 modules[0].(*Module).sanitize.Properties.InSanitizerDir = true
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001037 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001038 }
Colin Cross16b23492016-01-06 14:41:07 -08001039 }
1040 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +09001041 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001042 // APEX modules fall here
Jiyong Park82226632019-02-01 10:50:50 +09001043 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -08001044 }
1045 }
1046}
Vishwath Mohane7128792017-11-17 11:08:10 -08001047
Colin Cross571cccf2019-02-04 11:22:08 -08001048var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1049
Vishwath Mohane7128792017-11-17 11:08:10 -08001050func cfiStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001051 return config.Once(cfiStaticLibsKey, func() interface{} {
Vishwath Mohane7128792017-11-17 11:08:10 -08001052 return &[]string{}
1053 }).(*[]string)
1054}
1055
Colin Cross571cccf2019-02-04 11:22:08 -08001056var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1057
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001058func hwasanStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001059 return config.Once(hwasanStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001060 return &[]string{}
1061 }).(*[]string)
1062}
1063
Colin Cross571cccf2019-02-04 11:22:08 -08001064var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs")
1065
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001066func hwasanVendorStaticLibs(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001067 return config.Once(hwasanVendorStaticLibsKey, func() interface{} {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001068 return &[]string{}
1069 }).(*[]string)
1070}
1071
Jiyong Park1d1119f2019-07-29 21:27:18 +09001072func appendStringSync(item string, list *[]string, mutex *sync.Mutex) {
1073 mutex.Lock()
1074 *list = append(*list, item)
1075 mutex.Unlock()
1076}
1077
Ivan Lozano30c5db22018-02-21 15:49:20 -08001078func enableMinimalRuntime(sanitize *sanitize) bool {
1079 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001080 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001081 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano30c5db22018-02-21 15:49:20 -08001082 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
1083 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) &&
1084 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1085 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
1086 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
1087 return true
1088 }
1089 return false
1090}
1091
Inseob Kim8471cda2019-11-15 09:59:12 +09001092func enableUbsanRuntime(sanitize *sanitize) bool {
1093 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1094 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1095}
1096
Vishwath Mohane7128792017-11-17 11:08:10 -08001097func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
1098 cfiStaticLibs := cfiStaticLibs(ctx.Config())
Jeff Gaston72765392017-11-28 16:37:53 -08001099 sort.Strings(*cfiStaticLibs)
Vishwath Mohane7128792017-11-17 11:08:10 -08001100 ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " "))
1101}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001102
1103func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
1104 hwasanStaticLibs := hwasanStaticLibs(ctx.Config())
1105 sort.Strings(*hwasanStaticLibs)
1106 ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " "))
1107
1108 hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config())
1109 sort.Strings(*hwasanVendorStaticLibs)
1110 ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " "))
1111}