blob: cd979cfadf9912146fc3bc12ea74a5315d1ec60d [file] [log] [blame]
Colin Cross16b23492016-01-06 14:41:07 -08001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
18 "fmt"
Jeff Gaston72765392017-11-28 16:37:53 -080019 "sort"
Colin Cross16b23492016-01-06 14:41:07 -080020 "strings"
Vishwath Mohane7128792017-11-17 11:08:10 -080021 "sync"
Colin Cross16b23492016-01-06 14:41:07 -080022
Colin Cross6b753602018-06-21 13:03:07 -070023 "github.com/google/blueprint"
24
Colin Cross635c3b02016-05-18 15:37:25 -070025 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070026 "android/soong/cc/config"
Colin Cross16b23492016-01-06 14:41:07 -080027)
28
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070029var (
30 // Any C flags added by sanitizer which libTooling tools may not
31 // understand also need to be added to ClangLibToolingUnknownCflags in
32 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080033
Yi Kong20233a42019-08-21 01:38:40 -070034 asanCflags = []string{
35 "-fno-omit-frame-pointer",
36 "-fno-experimental-new-pass-manager",
37 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070038 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070039
Peter Collingbourne967511a2019-03-19 21:39:54 -070040 hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000041 "-fsanitize-hwaddress-abi=platform",
42 "-fno-experimental-new-pass-manager",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080043 // The following improves debug location information
44 // availability at the cost of its accuracy. It increases
45 // the likelihood of a stack variable's frame offset
46 // to be recorded in the debug info, which is important
47 // for the quality of hwasan reports. The downside is a
48 // higher number of "optimized out" stack variables.
49 // b/112437883.
50 "-mllvm", "-instcombine-lower-dbg-declare=0",
51 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070052
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000053 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Pirama Arumuga Nainareb8d4032020-07-27 11:22:35 -070054 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blocklist.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"}
Inseob Kim74d25562020-08-04 00:41:38 +090060 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070061
Pirama Arumuga Nainareb8d4032020-07-27 11:22:35 -070062 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080063
Peter Collingbournebd19db02019-03-06 10:38:48 -080064 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070065 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070066 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
67 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070068)
69
Colin Cross16b23492016-01-06 14:41:07 -080070type sanitizerType int
71
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070072func boolPtr(v bool) *bool {
73 if v {
74 return &v
75 } else {
76 return nil
77 }
78}
79
Colin Cross16b23492016-01-06 14:41:07 -080080const (
81 asan sanitizerType = iota + 1
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070082 hwasan
Colin Cross16b23492016-01-06 14:41:07 -080083 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070084 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000085 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080086 scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -070087 fuzzer
Colin Cross16b23492016-01-06 14:41:07 -080088)
89
Jiyong Park82226632019-02-01 10:50:50 +090090// Name of the sanitizer variation for this sanitizer type
91func (t sanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080092 switch t {
93 case asan:
94 return "asan"
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070095 case hwasan:
96 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080097 case tsan:
98 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070099 case intOverflow:
100 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000101 case cfi:
102 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800103 case scs:
104 return "scs"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700105 case fuzzer:
106 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800107 default:
108 panic(fmt.Errorf("unknown sanitizerType %d", t))
109 }
110}
111
Jiyong Park82226632019-02-01 10:50:50 +0900112// This is the sanitizer names in SANITIZE_[TARGET|HOST]
113func (t sanitizerType) name() string {
114 switch t {
115 case asan:
116 return "address"
117 case hwasan:
118 return "hwaddress"
119 case tsan:
120 return "thread"
121 case intOverflow:
122 return "integer_overflow"
123 case cfi:
124 return "cfi"
125 case scs:
126 return "shadow-call-stack"
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700127 case fuzzer:
128 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900129 default:
130 panic(fmt.Errorf("unknown sanitizerType %d", t))
131 }
132}
133
Jiyong Park1d1119f2019-07-29 21:27:18 +0900134func (t sanitizerType) incompatibleWithCfi() bool {
135 return t == asan || t == fuzzer || t == hwasan
136}
137
Colin Cross16b23492016-01-06 14:41:07 -0800138type SanitizeProperties struct {
139 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
140 Sanitize struct {
Nan Zhang0007d812017-11-07 10:57:05 -0800141 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800142
143 // main sanitizers
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700144 Address *bool `android:"arch_variant"`
145 Thread *bool `android:"arch_variant"`
146 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800147
148 // local sanitizers
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700149 Undefined *bool `android:"arch_variant"`
150 All_undefined *bool `android:"arch_variant"`
151 Misc_undefined []string `android:"arch_variant"`
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700152 Fuzzer *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700153 Safestack *bool `android:"arch_variant"`
154 Cfi *bool `android:"arch_variant"`
155 Integer_overflow *bool `android:"arch_variant"`
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700156 Scudo *bool `android:"arch_variant"`
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800157 Scs *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800158
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700159 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
160 // Replaces abort() on error with a human-readable error message.
161 // Address and Thread sanitizers always run in diagnostic mode.
162 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700163 Undefined *bool `android:"arch_variant"`
164 Cfi *bool `android:"arch_variant"`
165 Integer_overflow *bool `android:"arch_variant"`
166 Misc_undefined []string `android:"arch_variant"`
Ivan Lozano7929bba2018-12-12 09:36:31 -0800167 No_recover []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700168 }
169
170 // value to pass to -fsanitize-recover=
Colin Cross16b23492016-01-06 14:41:07 -0800171 Recover []string
172
173 // value to pass to -fsanitize-blacklist
174 Blacklist *string
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700175 // value to pass to -fsanitize-blacklist
176 Blocklist *string
Colin Cross16b23492016-01-06 14:41:07 -0800177 } `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"`
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500182 BuiltinsDep bool `blueprint:"mutated"`
Jiyong Park379de2f2018-12-19 02:47:14 +0900183 UbsanRuntimeDep bool `blueprint:"mutated"`
184 InSanitizerDir bool `blueprint:"mutated"`
185 Sanitizers []string `blueprint:"mutated"`
186 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800187}
188
189type sanitize struct {
190 Properties SanitizeProperties
191}
192
Vishwath Mohane7128792017-11-17 11:08:10 -0800193func init() {
194 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700195 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800196}
197
Colin Cross16b23492016-01-06 14:41:07 -0800198func (sanitize *sanitize) props() []interface{} {
199 return []interface{}{&sanitize.Properties}
200}
201
202func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700203 s := &sanitize.Properties.Sanitize
204
Colin Cross16b23492016-01-06 14:41:07 -0800205 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700206 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800207 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800208 }
209
Doug Hornc32c6b02019-01-17 14:44:05 -0800210 // Sanitizers do not work on Fuchsia yet.
211 if ctx.Fuchsia() {
212 s.Never = BoolPtr(true)
213 }
214
Colin Cross16b23492016-01-06 14:41:07 -0800215 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800216 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800217 return
218 }
219
Colin Cross16b23492016-01-06 14:41:07 -0800220 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700221 var globalSanitizersDiag []string
222
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700223 if ctx.Host() {
224 if !ctx.Windows() {
225 globalSanitizers = ctx.Config().SanitizeHost()
226 }
227 } else {
228 arches := ctx.Config().SanitizeDeviceArch()
229 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
230 globalSanitizers = ctx.Config().SanitizeDevice()
231 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800232 }
233 }
234
Colin Cross16b23492016-01-06 14:41:07 -0800235 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000236 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700237 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
238 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000239 }
Colin Cross16b23492016-01-06 14:41:07 -0800240
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700241 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
242 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000243 }
244
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700245 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
246 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000247 }
248
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700249 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
250 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000251 }
252
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700253 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
254 s.Fuzzer = boolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700255 }
256
257 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
258 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000259 }
260
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700261 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800262 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700263 s.Cfi = boolPtr(true)
264 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700265 }
266
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700267 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700268 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700269 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Ivan Lozano5f595532017-07-13 14:46:05 -0700270 s.Integer_overflow = boolPtr(true)
271 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700272 }
273
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700274 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
275 s.Scudo = boolPtr(true)
276 }
277
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700278 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
279 s.Hwaddress = boolPtr(true)
280 }
281
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000282 if len(globalSanitizers) > 0 {
283 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
284 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700285
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700286 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700287 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700288 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700289 s.Diag.Integer_overflow = boolPtr(true)
290 }
291
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700292 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
293 s.Diag.Cfi == nil && Bool(s.Cfi) {
294 s.Diag.Cfi = boolPtr(true)
295 }
296
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700297 if len(globalSanitizersDiag) > 0 {
298 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
299 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700300 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700301
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700302 // Enable CFI for all components in the include paths (for Aarch64 only)
303 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000304 s.Cfi = boolPtr(true)
305 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
306 s.Diag.Cfi = boolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700307 }
308 }
309
Elliott Hughesda3a0712020-03-06 16:55:28 -0800310 // Is CFI actually enabled?
311 if !ctx.Config().EnableCFI() {
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800312 s.Cfi = nil
313 s.Diag.Cfi = nil
314 }
315
Vishwath Mohan6d67e6e2017-02-07 20:31:41 -0800316 // Also disable CFI for arm32 until b/35157333 is fixed.
317 if ctx.Arch().ArchType == android.Arm {
318 s.Cfi = nil
319 s.Diag.Cfi = nil
320 }
321
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700322 // HWASan requires AArch64 hardware feature (top-byte-ignore).
323 if ctx.Arch().ArchType != android.Arm64 {
324 s.Hwaddress = nil
325 }
326
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800327 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800328 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800329 s.Scs = nil
330 }
331
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700332 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700333 if Bool(s.Address) || Bool(s.Hwaddress) {
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700334 s.Cfi = nil
335 s.Diag.Cfi = nil
336 }
337
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500338 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
339 if !ctx.Os().Linux() {
Vishwath Mohane7128792017-11-17 11:08:10 -0800340 s.Cfi = nil
341 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700342 s.Misc_undefined = nil
343 s.Undefined = nil
344 s.All_undefined = nil
345 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800346 }
347
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700348 // Also disable CFI for VNDK variants of components
349 if ctx.isVndk() && ctx.useVndk() {
Vishwath Mohan7589c822018-05-23 19:29:55 -0700350 s.Cfi = nil
351 s.Diag.Cfi = nil
352 }
353
Inseob Kimeec88e12020-01-22 11:11:29 +0900354 // Also disable CFI if building against snapshot.
355 vndkVersion := ctx.DeviceConfig().VndkVersion()
356 if ctx.useVndk() && vndkVersion != "current" && vndkVersion != "" {
357 s.Cfi = nil
358 }
359
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700360 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong1b3348d2020-01-21 15:53:22 -0800361 // Keep libc instrumented so that ramdisk / recovery can run hwasan-instrumented code if necessary.
362 if (ctx.inRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700363 s.Hwaddress = nil
364 }
365
Colin Cross3c344ef2016-07-18 15:44:56 -0700366 if ctx.staticBinary() {
367 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700368 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700369 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800370 }
371
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700372 if Bool(s.All_undefined) {
373 s.Undefined = nil
374 }
375
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700376 if !ctx.toolchain().Is64Bit() {
377 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700378 s.Thread = nil
379 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800380 // TODO(ccross): error for compile_multilib = "32"?
381 }
382
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800383 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 -0700384 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 -0800385 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700386 sanitize.Properties.SanitizerEnabled = true
387 }
388
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800389 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
390 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700391 s.Scudo = nil
392 }
393
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700394 if Bool(s.Hwaddress) {
395 s.Address = nil
396 s.Thread = nil
397 }
398
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700399 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
400 // mutually incompatible.
401 if Bool(s.Fuzzer) {
402 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800403 }
404}
405
406func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
407 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
408 return deps
409 }
410
Colin Cross16b23492016-01-06 14:41:07 -0800411 return deps
412}
413
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800414func toDisableImplicitIntegerChange(flags []string) bool {
415 // Returns true if any flag is fsanitize*integer, and there is
416 // no explicit flag about sanitize=implicit-integer-sign-change.
417 for _, f := range flags {
418 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
419 return false
420 }
421 }
422 for _, f := range flags {
423 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
424 return true
425 }
426 }
427 return false
428}
429
Colin Cross16b23492016-01-06 14:41:07 -0800430func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700431 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
432 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500433 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
434 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800435
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500436 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800437 flags.Local.LdFlags = append(flags.Local.LdFlags,
438 minimalRuntimePath,
439 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800440 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500441
442 if sanitize.Properties.BuiltinsDep {
443 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
444 }
445
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700446 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800447 return flags
448 }
449
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700450 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700451 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800452 // Frame pointer based unwinder in ASan requires ARM frame setup.
453 // TODO: put in flags?
454 flags.RequiredInstructionSet = "arm"
455 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800456 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
457 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800458
Colin Cross16b23492016-01-06 14:41:07 -0800459 if ctx.Host() {
460 // -nodefaultlibs (provided with libc++) prevents the driver from linking
461 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800462 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800463 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800464 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900465 if ctx.bootstrap() {
466 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
467 } else {
468 flags.DynamicLinker = "/system/bin/linker_asan"
469 }
Colin Cross16b23492016-01-06 14:41:07 -0800470 if flags.Toolchain.Is64Bit() {
471 flags.DynamicLinker += "64"
472 }
473 }
Colin Cross16b23492016-01-06 14:41:07 -0800474 }
475
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700476 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800477 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Yabin Cui6be405e2017-10-19 15:52:11 -0700478 }
479
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700480 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800481 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700482
483 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800484 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
485 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
486 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
487 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700488
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700489 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
490 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
491 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800492 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
493 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700494
Mitch Phillips74384752019-06-17 10:33:52 -0700495 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800496 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700497
498 // Disable fortify for fuzzing builds. Generally, we'll be building with
499 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800500 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800501
502 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
503 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
504 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
505 // the DT_RUNPATH from the shared library above it, and not the executable,
506 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
507 // DT_RUNPATH here means that transient shared libraries can be found
508 // colocated with their parents.
509 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800510 }
511
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700512 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800513 if ctx.Arch().ArchType == android.Arm {
514 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
515 // to do this on a function basis, so force Thumb on the entire module.
516 flags.RequiredInstructionSet = "thumb"
517 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000518
Colin Cross4af21ed2019-11-04 09:37:55 -0800519 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
520 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000521 // Only append the default visibility flag if -fvisibility has not already been set
522 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800523 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
524 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000525 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800526 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000527
528 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800529 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
530 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000531 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700532 }
533
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700534 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800535 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700536 }
537
Jiyong Park379de2f2018-12-19 02:47:14 +0900538 if len(sanitize.Properties.Sanitizers) > 0 {
539 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800540
Colin Cross4af21ed2019-11-04 09:37:55 -0800541 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
542 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800543 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800544 // Host sanitizers only link symbols in the final executable, so
545 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800546 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
547 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500548
549 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
550 if !ctx.toolchain().Bionic() {
551 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
552 }
553 }
554
555 if enableMinimalRuntime(sanitize) {
556 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
557 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
558 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
559 if !ctx.toolchain().Bionic() {
560 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800561 }
Colin Cross16b23492016-01-06 14:41:07 -0800562 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700563
564 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
565 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800566 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700567 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800568 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700569 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800570 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700571 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800572 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800573 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
574 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800575 }
Colin Cross16b23492016-01-06 14:41:07 -0800576 }
577
Jiyong Park379de2f2018-12-19 02:47:14 +0900578 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800579 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700580 }
581 // FIXME: enable RTTI if diag + (cfi or vptr)
582
Andreas Gampe97071162017-05-08 13:15:23 -0700583 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800584 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700585 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
586 }
587
Ivan Lozano7929bba2018-12-12 09:36:31 -0800588 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800589 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800590 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
591 }
592
Colin Cross635c3b02016-05-18 15:37:25 -0700593 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800594 if blacklist.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800595 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blacklist.String())
Colin Cross16b23492016-01-06 14:41:07 -0800596 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
597 }
598
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700599 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
600 if blocklist.Valid() {
601 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blocklist.String())
602 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
603 }
604
Colin Cross16b23492016-01-06 14:41:07 -0800605 return flags
606}
607
Colin Crossd80cbca2020-02-24 12:01:37 -0800608func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900609 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
610 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800611 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900612 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800613 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900614 }
615 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800616 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900617 }
618 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800619 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900620 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800621 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700622}
623
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700624func (sanitize *sanitize) inSanitizerDir() bool {
625 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700626}
627
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000628func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000629 switch t {
630 case asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000631 return sanitize.Properties.Sanitize.Address
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700632 case hwasan:
633 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000634 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000635 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000636 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000637 return sanitize.Properties.Sanitize.Integer_overflow
638 case cfi:
639 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800640 case scs:
641 return sanitize.Properties.Sanitize.Scs
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700642 case fuzzer:
643 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000644 default:
645 panic(fmt.Errorf("unknown sanitizerType %d", t))
646 }
647}
648
Dan Albert7d1eecf2018-01-19 12:30:45 -0800649func (sanitize *sanitize) isUnsanitizedVariant() bool {
650 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700651 !sanitize.isSanitizerEnabled(hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800652 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800653 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700654 !sanitize.isSanitizerEnabled(scs) &&
655 !sanitize.isSanitizerEnabled(fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800656}
657
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700658func (sanitize *sanitize) isVariantOnProductionDevice() bool {
659 return !sanitize.isSanitizerEnabled(asan) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700660 !sanitize.isSanitizerEnabled(hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700661 !sanitize.isSanitizerEnabled(tsan) &&
662 !sanitize.isSanitizerEnabled(fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700663}
664
Colin Cross16b23492016-01-06 14:41:07 -0800665func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
666 switch t {
667 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700668 sanitize.Properties.Sanitize.Address = boolPtr(b)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700669 case hwasan:
670 sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800671 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700672 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700673 case intOverflow:
674 sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000675 case cfi:
676 sanitize.Properties.Sanitize.Cfi = boolPtr(b)
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800677 case scs:
678 sanitize.Properties.Sanitize.Scs = boolPtr(b)
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700679 case fuzzer:
680 sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800681 default:
682 panic(fmt.Errorf("unknown sanitizerType %d", t))
683 }
684 if b {
685 sanitize.Properties.SanitizerEnabled = true
686 }
687}
688
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000689// Check if the sanitizer is explicitly disabled (as opposed to nil by
690// virtue of not being set).
691func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool {
692 if sanitize == nil {
693 return false
694 }
695
696 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
697 return sanitizerVal != nil && *sanitizerVal == false
698}
699
700// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
701// because enabling a sanitizer either directly (via the blueprint) or
702// indirectly (via a mutator) sets the bool ptr to true, and you can't
703// distinguish between the cases. It isn't needed though - both cases can be
704// treated identically.
705func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool {
706 if sanitize == nil {
707 return false
708 }
709
710 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
711 return sanitizerVal != nil && *sanitizerVal == true
712}
713
Colin Cross6b753602018-06-21 13:03:07 -0700714func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700715 switch t := tag.(type) {
716 case dependencyTag:
717 return t == reuseObjTag || t == objDepTag
718 case libraryDependencyTag:
719 return true
720 default:
721 return false
722 }
Colin Cross6b753602018-06-21 13:03:07 -0700723}
724
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700725// Propagate sanitizer requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700726func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
727 return func(mctx android.TopDownMutatorContext) {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000728 if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
Colin Cross6b753602018-06-21 13:03:07 -0700729 mctx.WalkDeps(func(child, parent android.Module) bool {
730 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
731 return false
732 }
733 if d, ok := child.(*Module); ok && d.sanitize != nil &&
Nan Zhang0007d812017-11-07 10:57:05 -0800734 !Bool(d.sanitize.Properties.Sanitize.Never) &&
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000735 !d.sanitize.isSanitizerExplicitlyDisabled(t) {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800736 if t == cfi || t == hwasan || t == scs {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700737 if d.static() {
738 d.sanitize.Properties.SanitizeDep = true
739 }
740 } else {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000741 d.sanitize.Properties.SanitizeDep = true
742 }
Colin Cross16b23492016-01-06 14:41:07 -0800743 }
Colin Cross6b753602018-06-21 13:03:07 -0700744 return true
Colin Cross16b23492016-01-06 14:41:07 -0800745 })
Jiyong Parkf97782b2019-02-13 20:28:58 +0900746 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
747 // If an APEX module includes a lib which is enabled for a sanitizer T, then
748 // the APEX module is also enabled for the same sanitizer type.
749 mctx.VisitDirectDeps(func(child android.Module) {
750 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
751 sanitizeable.EnableSanitizer(t.name())
752 }
753 })
Colin Cross16b23492016-01-06 14:41:07 -0800754 }
755 }
756}
757
Ivan Lozano30c5db22018-02-21 15:49:20 -0800758// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700759func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
760 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
761 mctx.WalkDeps(func(child, parent android.Module) bool {
762 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
763 return false
764 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800765
Inseob Kimeec88e12020-01-22 11:11:29 +0900766 d, ok := child.(*Module)
767 if !ok || !d.static() {
768 return false
769 }
770 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700771 if enableMinimalRuntime(d.sanitize) {
772 // If a static dependency is built with the minimal runtime,
773 // make sure we include the ubsan minimal runtime.
774 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +0900775 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -0700776 // If a static dependency runs with full ubsan diagnostics,
777 // make sure we include the ubsan runtime.
778 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800779 }
Colin Cross0b908332019-06-19 23:00:20 -0700780
781 if c.sanitize.Properties.MinimalRuntimeDep &&
782 c.sanitize.Properties.UbsanRuntimeDep {
783 // both flags that this mutator might set are true, so don't bother recursing
784 return false
785 }
786
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500787 if c.Os() == android.Linux {
788 c.sanitize.Properties.BuiltinsDep = true
789 }
790
Colin Cross0b908332019-06-19 23:00:20 -0700791 return true
Colin Cross6b753602018-06-21 13:03:07 -0700792 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900793
794 if p, ok := d.linker.(*vendorSnapshotLibraryDecorator); ok {
795 if Bool(p.properties.Sanitize_minimal_dep) {
796 c.sanitize.Properties.MinimalRuntimeDep = true
797 }
798 if Bool(p.properties.Sanitize_ubsan_dep) {
799 c.sanitize.Properties.UbsanRuntimeDep = true
800 }
801 }
802
803 return false
Colin Cross6b753602018-06-21 13:03:07 -0700804 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800805 }
806}
807
Jiyong Park379de2f2018-12-19 02:47:14 +0900808// Add the dependency to the runtime library for each of the sanitizer variants
809func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900810 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000811 if !c.Enabled() {
812 return
813 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900814 var sanitizers []string
815 var diagSanitizers []string
816
817 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
818 sanitizers = append(sanitizers, "undefined")
819 } else {
820 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
821 sanitizers = append(sanitizers,
822 "bool",
823 "integer-divide-by-zero",
824 "return",
825 "returns-nonnull-attribute",
826 "shift-exponent",
827 "unreachable",
828 "vla-bound",
829 // TODO(danalbert): The following checks currently have compiler performance issues.
830 //"alignment",
831 //"bounds",
832 //"enum",
833 //"float-cast-overflow",
834 //"float-divide-by-zero",
835 //"nonnull-attribute",
836 //"null",
837 //"shift-base",
838 //"signed-integer-overflow",
839 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
840 // https://llvm.org/PR19302
841 // http://reviews.llvm.org/D6974
842 // "object-size",
843 )
844 }
845 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
846 }
847
848 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
849 diagSanitizers = append(diagSanitizers, "undefined")
850 }
851
852 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
853
854 if Bool(c.sanitize.Properties.Sanitize.Address) {
855 sanitizers = append(sanitizers, "address")
856 diagSanitizers = append(diagSanitizers, "address")
857 }
858
859 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
860 sanitizers = append(sanitizers, "hwaddress")
861 }
862
863 if Bool(c.sanitize.Properties.Sanitize.Thread) {
864 sanitizers = append(sanitizers, "thread")
865 }
866
867 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
868 sanitizers = append(sanitizers, "safe-stack")
869 }
870
871 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
872 sanitizers = append(sanitizers, "cfi")
873
874 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
875 diagSanitizers = append(diagSanitizers, "cfi")
876 }
877 }
878
879 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
880 sanitizers = append(sanitizers, "unsigned-integer-overflow")
881 sanitizers = append(sanitizers, "signed-integer-overflow")
882 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
883 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
884 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
885 }
886 }
887
888 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
889 sanitizers = append(sanitizers, "scudo")
890 }
891
892 if Bool(c.sanitize.Properties.Sanitize.Scs) {
893 sanitizers = append(sanitizers, "shadow-call-stack")
894 }
895
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700896 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
897 sanitizers = append(sanitizers, "fuzzer-no-link")
898 }
899
Jiyong Park379de2f2018-12-19 02:47:14 +0900900 // Save the list of sanitizers. These will be used again when generating
901 // the build rules (for Cflags, etc.)
902 c.sanitize.Properties.Sanitizers = sanitizers
903 c.sanitize.Properties.DiagSanitizers = diagSanitizers
904
Ivan Lozanof3b190f2020-03-06 12:01:21 -0500905 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
906 if c.Host() {
907 diagSanitizers = sanitizers
908 }
909
Jiyong Park379de2f2018-12-19 02:47:14 +0900910 // Determine the runtime library required
911 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700912 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +0900913 toolchain := c.toolchain(mctx)
914 if Bool(c.sanitize.Properties.Sanitize.Address) {
915 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
916 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
917 if c.staticBinary() {
918 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700919 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +0900920 } else {
921 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
922 }
923 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
924 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
925 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
926 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
927 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
928 } else {
929 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
930 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700931 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500932 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
933 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
934 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900935 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
936 }
937
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500938 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
939 // UBSan is supported on non-bionic linux host builds as well
Jooyung Han0302a842019-10-30 18:43:49 +0900940 if isLlndkLibrary(runtimeLibrary, mctx.Config()) && !c.static() && c.UseVndk() {
Jiyong Park379de2f2018-12-19 02:47:14 +0900941 runtimeLibrary = runtimeLibrary + llndkLibrarySuffix
942 }
943
944 // Adding dependency to the runtime library. We are using *FarVariation*
945 // because the runtime libraries themselves are not mutated by sanitizer
946 // mutators and thus don't have sanitizer variants whereas this module
947 // has been already mutated.
948 //
949 // Note that by adding dependency with {static|shared}DepTag, the lib is
950 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
951 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +0900952 deps := append(extraStaticDeps, runtimeLibrary)
953 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
954 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
955 snapshots := vendorSnapshotStaticLibs(mctx.Config())
956 for idx, dep := range deps {
957 if lib, ok := snapshots.get(dep, mctx.Arch().ArchType); ok {
958 deps[idx] = lib
959 }
960 }
961 }
962
Jiyong Park379de2f2018-12-19 02:47:14 +0900963 // static executable gets static runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -0700964 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700965 mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{
Jiyong Park379de2f2018-12-19 02:47:14 +0900966 {Mutator: "link", Variation: "static"},
Colin Cross7228ecd2019-11-18 16:00:16 -0800967 c.ImageVariation(),
Colin Cross6e511a92020-07-27 21:26:48 -0700968 }...), depTag, deps...)
Jiyong Park1d1119f2019-07-29 21:27:18 +0900969 } else if !c.static() && !c.header() {
Inseob Kimeec88e12020-01-22 11:11:29 +0900970 // If we're using snapshots and in vendor, redirect to snapshot whenever possible
971 if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
972 snapshots := vendorSnapshotSharedLibs(mctx.Config())
973 if lib, ok := snapshots.get(runtimeLibrary, mctx.Arch().ArchType); ok {
974 runtimeLibrary = lib
975 }
976 }
977
Jiyong Park3b1746a2019-01-29 11:15:04 +0900978 // dynamic executable and shared libs get shared runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -0700979 depTag := libraryDependencyTag{Kind: sharedLibraryDependency, Order: earlyLibraryDependency}
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700980 mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{
Jiyong Park379de2f2018-12-19 02:47:14 +0900981 {Mutator: "link", Variation: "shared"},
Colin Cross7228ecd2019-11-18 16:00:16 -0800982 c.ImageVariation(),
Colin Cross6e511a92020-07-27 21:26:48 -0700983 }...), depTag, runtimeLibrary)
Jiyong Park379de2f2018-12-19 02:47:14 +0900984 }
985 // static lib does not have dependency to the runtime library. The
986 // dependency will be added to the executables or shared libs using
987 // the static lib.
988 }
989 }
990}
991
992type Sanitizeable interface {
993 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +0900994 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +0900995 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +0900996 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +0900997}
998
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000999// Create sanitized variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -07001000func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
1001 return func(mctx android.BottomUpMutatorContext) {
Vishwath Mohane6153452017-08-11 00:52:44 +00001002 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001003 if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001004 modules := mctx.CreateVariations(t.variationName())
Colin Cross30d5f512016-05-03 18:02:42 -07001005 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001006 } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001007 isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001008 if c.static() || c.header() || t == asan || t == fuzzer {
1009 // Static and header libs are split into non-sanitized and sanitized variants.
1010 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1011 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1012 // that isn't sanitized for asan/fuzzer.
1013 //
1014 // Note for defaultVariation: since we don't split for shared libs but for static/header
1015 // libs, it is possible for the sanitized variant of a static/header lib to depend
1016 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1017 // error when the module is split. defaultVariation is the name of the variation that
1018 // will be used when such a dangling dependency occurs during the split of the current
1019 // module. By setting it to the name of the sanitized variation, the dangling dependency
1020 // is redirected to the sanitized variant of the dependent module.
1021 defaultVariation := t.variationName()
1022 mctx.SetDefaultDependencyVariation(&defaultVariation)
1023 modules := mctx.CreateVariations("", t.variationName())
1024 modules[0].(*Module).sanitize.SetSanitizer(t, false)
1025 modules[1].(*Module).sanitize.SetSanitizer(t, true)
1026 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
1027 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001028
Ivan Lozano4774a812020-03-10 16:23:57 -04001029 if mctx.Device() && t.incompatibleWithCfi() {
1030 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1031 // are incompatible with cfi
1032 modules[1].(*Module).sanitize.SetSanitizer(cfi, false)
1033 }
1034
Jiyong Park1d1119f2019-07-29 21:27:18 +09001035 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1036 // to Make, because the sanitized version has a different suffix in name.
1037 // For other types of sanitizers, suppress the variation that is disabled.
1038 if t != cfi && t != scs && t != hwasan {
1039 if isSanitizerEnabled {
1040 modules[0].(*Module).Properties.PreventInstall = true
1041 modules[0].(*Module).Properties.HideFromMake = true
1042 } else {
1043 modules[1].(*Module).Properties.PreventInstall = true
1044 modules[1].(*Module).Properties.HideFromMake = true
1045 }
1046 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001047
Jiyong Park1d1119f2019-07-29 21:27:18 +09001048 // Export the static lib name to make
Dan Willemsenb5b2aba2020-05-03 21:28:32 -07001049 if c.static() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001050 if t == cfi {
Inseob Kim74d25562020-08-04 00:41:38 +09001051 cfiStaticLibs(mctx.Config()).add(c, c.Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001052 } else if t == hwasan {
Inseob Kim74d25562020-08-04 00:41:38 +09001053 hwasanStaticLibs(mctx.Config()).add(c, c.Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001054 }
1055 }
1056 } else {
1057 // Shared libs are not split. Only the sanitized variant is created.
1058 modules := mctx.CreateVariations(t.variationName())
1059 modules[0].(*Module).sanitize.SetSanitizer(t, true)
1060 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
Vishwath Mohane7128792017-11-17 11:08:10 -08001061
Jiyong Park1d1119f2019-07-29 21:27:18 +09001062 // locate the asan libraries under /data/asan
1063 if mctx.Device() && t == asan && isSanitizerEnabled {
1064 modules[0].(*Module).sanitize.Properties.InSanitizerDir = true
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001065 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001066
1067 if mctx.Device() && t.incompatibleWithCfi() {
1068 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1069 // are incompatible with cfi
1070 modules[0].(*Module).sanitize.SetSanitizer(cfi, false)
1071 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001072 }
Colin Cross16b23492016-01-06 14:41:07 -08001073 }
1074 c.sanitize.Properties.SanitizeDep = false
Jiyong Park82226632019-02-01 10:50:50 +09001075 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001076 // APEX modules fall here
Jooyung Han8ce8db92020-05-15 19:05:05 +09001077 sanitizeable.AddSanitizerDependencies(mctx, t.name())
Jiyong Park82226632019-02-01 10:50:50 +09001078 mctx.CreateVariations(t.variationName())
Colin Cross16b23492016-01-06 14:41:07 -08001079 }
1080 }
1081}
Vishwath Mohane7128792017-11-17 11:08:10 -08001082
Inseob Kim74d25562020-08-04 00:41:38 +09001083type sanitizerStaticLibsMap struct {
1084 // libsMap contains one list of modules per each image and each arch.
1085 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
1086 libsMap map[imageVariantType]map[string][]string
1087 libsMapLock sync.Mutex
1088 sanitizerType sanitizerType
1089}
1090
1091func newSanitizerStaticLibsMap(t sanitizerType) *sanitizerStaticLibsMap {
1092 return &sanitizerStaticLibsMap{
1093 sanitizerType: t,
1094 libsMap: make(map[imageVariantType]map[string][]string),
1095 }
1096}
1097
1098// Add the current module to sanitizer static libs maps
1099// Each module should pass its exported name as names of Make and Soong can differ.
1100func (s *sanitizerStaticLibsMap) add(c *Module, name string) {
1101 image := c.getImageVariantType()
1102 arch := c.Arch().ArchType.String()
1103
1104 s.libsMapLock.Lock()
1105 defer s.libsMapLock.Unlock()
1106
1107 if _, ok := s.libsMap[image]; !ok {
1108 s.libsMap[image] = make(map[string][]string)
1109 }
1110
1111 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1112}
1113
1114// Exports makefile variables in the following format:
1115// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1116// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1117// These are to be used by use_soong_sanitized_static_libraries.
1118// See build/make/core/binary.mk for more details.
1119func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1120 for _, image := range android.SortedStringKeys(s.libsMap) {
1121 archMap := s.libsMap[imageVariantType(image)]
1122 for _, arch := range android.SortedStringKeys(archMap) {
1123 libs := archMap[arch]
1124 sort.Strings(libs)
1125
1126 key := fmt.Sprintf(
1127 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1128 s.sanitizerType.variationName(),
1129 image, // already upper
1130 arch)
1131
1132 ctx.Strict(key, strings.Join(libs, " "))
1133 }
1134 }
1135}
1136
Colin Cross571cccf2019-02-04 11:22:08 -08001137var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1138
Inseob Kim74d25562020-08-04 00:41:38 +09001139func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001140 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001141 return newSanitizerStaticLibsMap(cfi)
1142 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001143}
1144
Colin Cross571cccf2019-02-04 11:22:08 -08001145var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1146
Inseob Kim74d25562020-08-04 00:41:38 +09001147func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001148 return config.Once(hwasanStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001149 return newSanitizerStaticLibsMap(hwasan)
1150 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001151}
1152
Ivan Lozano30c5db22018-02-21 15:49:20 -08001153func enableMinimalRuntime(sanitize *sanitize) bool {
1154 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001155 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001156 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001157
Ivan Lozano30c5db22018-02-21 15:49:20 -08001158 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001159 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1160 Bool(sanitize.Properties.Sanitize.Undefined) ||
1161 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1162
Ivan Lozano30c5db22018-02-21 15:49:20 -08001163 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1164 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001165 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001166 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001167
Ivan Lozano30c5db22018-02-21 15:49:20 -08001168 return true
1169 }
1170 return false
1171}
1172
Inseob Kim8471cda2019-11-15 09:59:12 +09001173func enableUbsanRuntime(sanitize *sanitize) bool {
1174 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001175 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001176 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1177}
1178
Vishwath Mohane7128792017-11-17 11:08:10 -08001179func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001180 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001181}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001182
1183func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001184 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001185}