blob: 4cc70e0e9dfe6308ce1a2968bb16cf0740a79e3e [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"
Liz Kammerb2fc4702021-06-25 14:53:40 -040024 "github.com/google/blueprint/proptools"
Colin Cross6b753602018-06-21 13:03:07 -070025
Colin Cross635c3b02016-05-18 15:37:25 -070026 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070027 "android/soong/cc/config"
Colin Cross16b23492016-01-06 14:41:07 -080028)
29
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070030var (
31 // Any C flags added by sanitizer which libTooling tools may not
32 // understand also need to be added to ClangLibToolingUnknownCflags in
33 // cc/config/clang.go
Vishwath Mohanf3918d32017-02-14 07:59:33 -080034
Yi Kong20233a42019-08-21 01:38:40 -070035 asanCflags = []string{
36 "-fno-omit-frame-pointer",
37 "-fno-experimental-new-pass-manager",
38 }
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070039 asanLdflags = []string{"-Wl,-u,__asan_preinit"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070040
Peter Collingbourne967511a2019-03-19 21:39:54 -070041 hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
Evgenii Stepanov96fa3dd2020-03-27 19:38:42 +000042 "-fsanitize-hwaddress-abi=platform",
43 "-fno-experimental-new-pass-manager",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080044 // The following improves debug location information
45 // availability at the cost of its accuracy. It increases
46 // the likelihood of a stack variable's frame offset
47 // to be recorded in the debug info, which is important
48 // for the quality of hwasan reports. The downside is a
49 // higher number of "optimized out" stack variables.
50 // b/112437883.
51 "-mllvm", "-instcombine-lower-dbg-declare=0",
Mitch Phillipsb1c574f2020-06-22 13:28:23 -070052 // TODO(b/159343917): HWASan and GlobalISel don't play nicely, and
53 // GlobalISel is the default at -O0 on aarch64.
54 "-mllvm", "--aarch64-enable-global-isel-at-O=-1",
55 "-mllvm", "-fast-isel=false",
Evgenii Stepanov64bee4d2019-11-22 18:37:10 -080056 }
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070057
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000058 cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
Pirama Arumuga Nainareb8d4032020-07-27 11:22:35 -070059 "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blocklist.txt"}
Evgenii Stepanovdbf1d4f2018-08-31 12:54:33 -070060 // -flto and -fvisibility are required by clang when -fsanitize=cfi is
61 // used, but have no effect on assembly files
62 cfiAsflags = []string{"-flto", "-fvisibility=default"}
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070063 cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
Pirama Arumuga Nainarbdb17f02017-08-28 21:50:17 -070064 "-Wl,-plugin-opt,O1"}
Inseob Kim74d25562020-08-04 00:41:38 +090065 cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070066
Pirama Arumuga Nainareb8d4032020-07-27 11:22:35 -070067 intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blocklist.txt"}
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080068
Peter Collingbournebd19db02019-03-06 10:38:48 -080069 minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
Ivan Lozanoae6ae1d2018-10-08 09:29:39 -070070 "-fno-sanitize-recover=integer,undefined"}
Evgenii Stepanov2c6484e2019-05-15 12:49:54 -070071 hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
72 "export_memory_stats=0", "max_malloc_fill_size=0"}
Dan Willemsencbceaab2016-10-13 16:44:07 -070073)
74
Ivan Lozano3968d8f2020-12-14 11:27:52 -050075type SanitizerType int
Colin Cross16b23492016-01-06 14:41:07 -080076
Colin Cross16b23492016-01-06 14:41:07 -080077const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050078 Asan SanitizerType = iota + 1
Tri Vo6eafc362021-04-01 11:29:09 -070079 Hwasan
Colin Cross16b23492016-01-06 14:41:07 -080080 tsan
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070081 intOverflow
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000082 cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -080083 scs
Ivan Lozano3968d8f2020-12-14 11:27:52 -050084 Fuzzer
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -070085 memtag_heap
Colin Cross16b23492016-01-06 14:41:07 -080086)
87
Jiyong Park82226632019-02-01 10:50:50 +090088// Name of the sanitizer variation for this sanitizer type
Ivan Lozano3968d8f2020-12-14 11:27:52 -050089func (t SanitizerType) variationName() string {
Colin Cross16b23492016-01-06 14:41:07 -080090 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -050091 case Asan:
Colin Cross16b23492016-01-06 14:41:07 -080092 return "asan"
Tri Vo6eafc362021-04-01 11:29:09 -070093 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070094 return "hwasan"
Colin Cross16b23492016-01-06 14:41:07 -080095 case tsan:
96 return "tsan"
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070097 case intOverflow:
98 return "intOverflow"
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000099 case cfi:
100 return "cfi"
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800101 case scs:
102 return "scs"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700103 case memtag_heap:
104 return "memtag_heap"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500105 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700106 return "fuzzer"
Colin Cross16b23492016-01-06 14:41:07 -0800107 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500108 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800109 }
110}
111
Jiyong Park82226632019-02-01 10:50:50 +0900112// This is the sanitizer names in SANITIZE_[TARGET|HOST]
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500113func (t SanitizerType) name() string {
Jiyong Park82226632019-02-01 10:50:50 +0900114 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500115 case Asan:
Jiyong Park82226632019-02-01 10:50:50 +0900116 return "address"
Tri Vo6eafc362021-04-01 11:29:09 -0700117 case Hwasan:
Jiyong Park82226632019-02-01 10:50:50 +0900118 return "hwaddress"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700119 case memtag_heap:
120 return "memtag_heap"
Jiyong Park82226632019-02-01 10:50:50 +0900121 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"
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500129 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700130 return "fuzzer"
Jiyong Park82226632019-02-01 10:50:50 +0900131 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500132 panic(fmt.Errorf("unknown SanitizerType %d", t))
Jiyong Park82226632019-02-01 10:50:50 +0900133 }
134}
135
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500136func (*Module) SanitizerSupported(t SanitizerType) bool {
137 switch t {
138 case Asan:
139 return true
Tri Vo6eafc362021-04-01 11:29:09 -0700140 case Hwasan:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500141 return true
142 case tsan:
143 return true
144 case intOverflow:
145 return true
146 case cfi:
147 return true
148 case scs:
149 return true
150 case Fuzzer:
151 return true
152 default:
153 return false
154 }
155}
156
157// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
158func (t SanitizerType) incompatibleWithCfi() bool {
Tri Vo6eafc362021-04-01 11:29:09 -0700159 return t == Asan || t == Fuzzer || t == Hwasan
Jiyong Park1d1119f2019-07-29 21:27:18 +0900160}
161
Martin Stjernholmb0249572020-09-15 02:32:35 +0100162type SanitizeUserProps struct {
163 Never *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800164
Martin Stjernholmb0249572020-09-15 02:32:35 +0100165 // main sanitizers
166 Address *bool `android:"arch_variant"`
167 Thread *bool `android:"arch_variant"`
168 Hwaddress *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800169
Martin Stjernholmb0249572020-09-15 02:32:35 +0100170 // local sanitizers
171 Undefined *bool `android:"arch_variant"`
172 All_undefined *bool `android:"arch_variant"`
173 Misc_undefined []string `android:"arch_variant"`
174 Fuzzer *bool `android:"arch_variant"`
175 Safestack *bool `android:"arch_variant"`
176 Cfi *bool `android:"arch_variant"`
177 Integer_overflow *bool `android:"arch_variant"`
178 Scudo *bool `android:"arch_variant"`
179 Scs *bool `android:"arch_variant"`
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700180 Memtag_heap *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100181
182 // A modifier for ASAN and HWASAN for write only instrumentation
183 Writeonly *bool `android:"arch_variant"`
184
185 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
186 // Replaces abort() on error with a human-readable error message.
187 // Address and Thread sanitizers always run in diagnostic mode.
188 Diag struct {
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700189 Undefined *bool `android:"arch_variant"`
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700190 Cfi *bool `android:"arch_variant"`
191 Integer_overflow *bool `android:"arch_variant"`
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700192 Memtag_heap *bool `android:"arch_variant"`
Martin Stjernholmb0249572020-09-15 02:32:35 +0100193 Misc_undefined []string `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800194 No_recover []string `android:"arch_variant"`
195 } `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800196
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800197 // Sanitizers to run with flag configuration specified
198 Config struct {
199 // Enables CFI support flags for assembly-heavy libraries
200 Cfi_assembly_support *bool `android:"arch_variant"`
Cindy Zhoud3fe4922020-12-01 11:14:30 -0800201 } `android:"arch_variant"`
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800202
Martin Stjernholmb0249572020-09-15 02:32:35 +0100203 // value to pass to -fsanitize-recover=
204 Recover []string
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000205
Martin Stjernholmb0249572020-09-15 02:32:35 +0100206 // value to pass to -fsanitize-blacklist
207 Blocklist *string
208}
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700209
Martin Stjernholmb0249572020-09-15 02:32:35 +0100210type SanitizeProperties struct {
211 // Enable AddressSanitizer, ThreadSanitizer, UndefinedBehaviorSanitizer, and
212 // others. Please see SanitizerUserProps in build/soong/cc/sanitize.go for
213 // details.
214 Sanitize SanitizeUserProps `android:"arch_variant"`
215 SanitizerEnabled bool `blueprint:"mutated"`
216 SanitizeDep bool `blueprint:"mutated"`
217 MinimalRuntimeDep bool `blueprint:"mutated"`
218 BuiltinsDep bool `blueprint:"mutated"`
219 UbsanRuntimeDep bool `blueprint:"mutated"`
220 InSanitizerDir bool `blueprint:"mutated"`
221 Sanitizers []string `blueprint:"mutated"`
222 DiagSanitizers []string `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -0800223}
224
225type sanitize struct {
226 Properties SanitizeProperties
227}
228
Cindy Zhou18417cb2020-12-10 07:12:38 -0800229// Mark this tag with a check to see if apex dependency check should be skipped
230func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
231 return t.skipApexAllowedDependenciesCheck
232}
233
234var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
235
Vishwath Mohane7128792017-11-17 11:08:10 -0800236func init() {
237 android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700238 android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
Vishwath Mohane7128792017-11-17 11:08:10 -0800239}
240
Colin Cross16b23492016-01-06 14:41:07 -0800241func (sanitize *sanitize) props() []interface{} {
242 return []interface{}{&sanitize.Properties}
243}
244
245func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700246 s := &sanitize.Properties.Sanitize
247
Colin Cross16b23492016-01-06 14:41:07 -0800248 // Don't apply sanitizers to NDK code.
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700249 if ctx.useSdk() {
Nan Zhang0007d812017-11-07 10:57:05 -0800250 s.Never = BoolPtr(true)
Colin Cross16b23492016-01-06 14:41:07 -0800251 }
252
Doug Hornc32c6b02019-01-17 14:44:05 -0800253 // Sanitizers do not work on Fuchsia yet.
254 if ctx.Fuchsia() {
255 s.Never = BoolPtr(true)
256 }
257
Colin Cross16b23492016-01-06 14:41:07 -0800258 // Never always wins.
Nan Zhang0007d812017-11-07 10:57:05 -0800259 if Bool(s.Never) {
Colin Cross16b23492016-01-06 14:41:07 -0800260 return
261 }
262
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800263 // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap}).
264 if ctx.testBinary() && s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400265 s.Memtag_heap = proptools.BoolPtr(true)
266 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800267 }
268
Colin Cross16b23492016-01-06 14:41:07 -0800269 var globalSanitizers []string
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700270 var globalSanitizersDiag []string
271
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700272 if ctx.Host() {
273 if !ctx.Windows() {
274 globalSanitizers = ctx.Config().SanitizeHost()
275 }
276 } else {
277 arches := ctx.Config().SanitizeDeviceArch()
278 if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
279 globalSanitizers = ctx.Config().SanitizeDevice()
280 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Colin Cross16b23492016-01-06 14:41:07 -0800281 }
282 }
283
Colin Cross16b23492016-01-06 14:41:07 -0800284 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000285 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700286 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400287 s.All_undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000288 }
Colin Cross16b23492016-01-06 14:41:07 -0800289
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700290 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400291 s.Undefined = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000292 }
293
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700294 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400295 s.Address = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000296 }
297
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700298 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400299 s.Thread = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000300 }
301
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700302 if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400303 s.Fuzzer = proptools.BoolPtr(true)
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700304 }
305
306 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400307 s.Safestack = proptools.BoolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000308 }
309
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700310 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
Colin Cross6510f912017-11-29 00:27:14 -0800311 if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400312 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700313 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700314 }
315
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700316 // Global integer_overflow builds do not support static libraries.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700317 if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700318 if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400319 s.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano5f595532017-07-13 14:46:05 -0700320 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700321 }
322
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700323 if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400324 s.Scudo = proptools.BoolPtr(true)
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700325 }
326
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700327 if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400328 s.Hwaddress = proptools.BoolPtr(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700329 }
330
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000331 if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
332 // Hwaddress and Address are set before, so we can check them here
333 // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
334 if s.Address == nil && s.Hwaddress == nil {
335 ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
336 }
Liz Kammerb2fc4702021-06-25 14:53:40 -0400337 s.Writeonly = proptools.BoolPtr(true)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000338 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700339 if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800340 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400341 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800342 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700343 }
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000344
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000345 if len(globalSanitizers) > 0 {
346 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
347 }
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700348
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700349 // Global integer_overflow builds do not support static library diagnostics.
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700350 if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700351 s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400352 s.Diag.Integer_overflow = proptools.BoolPtr(true)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700353 }
354
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700355 if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
356 s.Diag.Cfi == nil && Bool(s.Cfi) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400357 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700358 }
359
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800360 if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
361 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400362 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800363 }
364
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700365 if len(globalSanitizersDiag) > 0 {
366 ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
367 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700368 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700369
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800370 // Enable Memtag for all components in the include paths (for Aarch64 only)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800371 if ctx.Arch().ArchType == android.Arm64 {
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800372 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800373 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400374 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800375 }
376 if s.Diag.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400377 s.Diag.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800378 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800379 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800380 if s.Memtag_heap == nil {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400381 s.Memtag_heap = proptools.BoolPtr(true)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -0800382 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -0800383 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700384 }
385
Vishwath Mohan1c54f662018-05-24 18:36:18 -0700386 // Enable CFI for all components in the include paths (for Aarch64 only)
387 if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400388 s.Cfi = proptools.BoolPtr(true)
Vishwath Mohan3af8ee02018-03-30 02:55:23 +0000389 if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400390 s.Diag.Cfi = proptools.BoolPtr(true)
Vishwath Mohan1fa3ac52017-10-31 02:26:14 -0700391 }
392 }
393
Elliott Hughesda3a0712020-03-06 16:55:28 -0800394 // Is CFI actually enabled?
395 if !ctx.Config().EnableCFI() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400396 s.Cfi = nil
397 s.Diag.Cfi = nil
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800398 }
399
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700400 // HWASan requires AArch64 hardware feature (top-byte-ignore).
401 if ctx.Arch().ArchType != android.Arm64 {
402 s.Hwaddress = nil
403 }
404
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800405 // SCS is only implemented on AArch64.
Peter Collingbournebd19db02019-03-06 10:38:48 -0800406 if ctx.Arch().ArchType != android.Arm64 {
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800407 s.Scs = nil
408 }
409
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700410 // memtag_heap is only implemented on AArch64.
411 if ctx.Arch().ArchType != android.Arm64 {
412 s.Memtag_heap = nil
413 }
414
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700415 // Also disable CFI if ASAN is enabled.
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700416 if Bool(s.Address) || Bool(s.Hwaddress) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400417 s.Cfi = nil
418 s.Diag.Cfi = nil
Vishwath Mohan8f4fdd82017-04-20 07:42:52 -0700419 }
420
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500421 // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
422 if !ctx.Os().Linux() {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400423 s.Cfi = nil
424 s.Diag.Cfi = nil
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700425 s.Misc_undefined = nil
426 s.Undefined = nil
427 s.All_undefined = nil
428 s.Integer_overflow = nil
Vishwath Mohane7128792017-11-17 11:08:10 -0800429 }
430
Vishwath Mohan9ccbba02018-05-28 13:54:48 -0700431 // Also disable CFI for VNDK variants of components
432 if ctx.isVndk() && ctx.useVndk() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900433 if ctx.static() {
434 // Cfi variant for static vndk should be captured as vendor snapshot,
435 // so don't strictly disable Cfi.
436 s.Cfi = nil
437 s.Diag.Cfi = nil
438 } else {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400439 s.Cfi = nil
440 s.Diag.Cfi = nil
Inseob Kimc42f2f22020-07-29 20:32:10 +0900441 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900442 }
443
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700444 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700445 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
446 if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700447 s.Hwaddress = nil
448 }
449
Colin Cross3c344ef2016-07-18 15:44:56 -0700450 if ctx.staticBinary() {
451 s.Address = nil
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700452 s.Fuzzer = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700453 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800454 }
455
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700456 if Bool(s.All_undefined) {
457 s.Undefined = nil
458 }
459
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700460 if !ctx.toolchain().Is64Bit() {
461 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700462 s.Thread = nil
463 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800464 // TODO(ccross): error for compile_multilib = "32"?
465 }
466
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800467 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 -0700468 Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700469 Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap)) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700470 sanitize.Properties.SanitizerEnabled = true
471 }
472
Kostya Kortchinskyd5275c82019-02-01 08:42:56 -0800473 // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
474 if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
Kostya Kortchinskyd18ae5c2018-06-12 14:46:54 -0700475 s.Scudo = nil
476 }
477
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700478 if Bool(s.Hwaddress) {
479 s.Address = nil
480 s.Thread = nil
Evgenii Stepanovb15a5642021-06-23 12:30:55 -0700481 // Disable ubsan diagnosic as a workaround for a compiler bug.
482 // TODO(b/191808836): re-enable.
483 s.Diag.Undefined = nil
484 s.Diag.Integer_overflow = nil
485 s.Diag.Misc_undefined = nil
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700486 }
487
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700488 // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
489 // mutually incompatible.
490 if Bool(s.Fuzzer) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400491 s.Cfi = nil
Colin Cross16b23492016-01-06 14:41:07 -0800492 }
493}
494
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800495func toDisableImplicitIntegerChange(flags []string) bool {
496 // Returns true if any flag is fsanitize*integer, and there is
497 // no explicit flag about sanitize=implicit-integer-sign-change.
498 for _, f := range flags {
499 if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
500 return false
501 }
502 }
503 for _, f := range flags {
504 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
505 return true
506 }
507 }
508 return false
509}
510
Yabin Cuidb7dda82020-11-30 15:47:45 -0800511func toDisableUnsignedShiftBaseChange(flags []string) bool {
512 // Returns true if any flag is fsanitize*integer, and there is
513 // no explicit flag about sanitize=unsigned-shift-base.
514 for _, f := range flags {
515 if strings.Contains(f, "sanitize=unsigned-shift-base") {
516 return false
517 }
518 }
519 for _, f := range flags {
520 if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
521 return true
522 }
523 }
524 return false
525}
526
Colin Cross16b23492016-01-06 14:41:07 -0800527func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozano59fdea22018-05-10 14:17:22 -0700528 minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
529 minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500530 builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
531 builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
Ivan Lozano30c5db22018-02-21 15:49:20 -0800532
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500533 if sanitize.Properties.MinimalRuntimeDep {
Colin Cross4af21ed2019-11-04 09:37:55 -0800534 flags.Local.LdFlags = append(flags.Local.LdFlags,
535 minimalRuntimePath,
536 "-Wl,--exclude-libs,"+minimalRuntimeLib)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800537 }
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500538
539 if sanitize.Properties.BuiltinsDep {
540 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
541 }
542
Ivan Lozanoa9255a82018-03-13 10:41:07 -0700543 if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
Colin Cross16b23492016-01-06 14:41:07 -0800544 return flags
545 }
546
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700547 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700548 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800549 // Frame pointer based unwinder in ASan requires ARM frame setup.
550 // TODO: put in flags?
551 flags.RequiredInstructionSet = "arm"
552 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800553 flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
554 flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
Colin Cross16b23492016-01-06 14:41:07 -0800555
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000556 if Bool(sanitize.Properties.Sanitize.Writeonly) {
557 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
558 }
559
Colin Cross16b23492016-01-06 14:41:07 -0800560 if ctx.Host() {
561 // -nodefaultlibs (provided with libc++) prevents the driver from linking
562 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Colin Cross4af21ed2019-11-04 09:37:55 -0800563 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
Colin Cross16b23492016-01-06 14:41:07 -0800564 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800565 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
Jiyong Parka2aca282019-02-02 13:13:38 +0900566 if ctx.bootstrap() {
567 flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
568 } else {
569 flags.DynamicLinker = "/system/bin/linker_asan"
570 }
Colin Cross16b23492016-01-06 14:41:07 -0800571 if flags.Toolchain.Is64Bit() {
572 flags.DynamicLinker += "64"
573 }
574 }
Colin Cross16b23492016-01-06 14:41:07 -0800575 }
576
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700577 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800578 flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
Jasraj Bedibb4511d2020-07-23 22:58:17 +0000579 if Bool(sanitize.Properties.Sanitize.Writeonly) {
580 flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
581 }
Yabin Cui6be405e2017-10-19 15:52:11 -0700582 }
583
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700584 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800585 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700586
587 // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
Colin Cross4af21ed2019-11-04 09:37:55 -0800588 _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
589 _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
590 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
591 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
Mitch Phillips74384752019-06-17 10:33:52 -0700592
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700593 // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
594 // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
595 // doesn't match the linker script due to the "__emutls_v." prefix).
Colin Cross4af21ed2019-11-04 09:37:55 -0800596 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
597 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
Mitch Phillipsb8e593d2019-10-09 17:18:59 -0700598
Mitch Phillips74384752019-06-17 10:33:52 -0700599 // TODO(b/133876586): Experimental PM breaks sanitizer coverage.
Colin Cross4af21ed2019-11-04 09:37:55 -0800600 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
Mitch Phillipsb9b3e792019-08-28 12:41:07 -0700601
602 // Disable fortify for fuzzing builds. Generally, we'll be building with
603 // UBSan or ASan here and the fortify checks pollute the stack traces.
Colin Cross4af21ed2019-11-04 09:37:55 -0800604 flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
Mitch Phillips734b4cb2019-12-10 08:44:52 -0800605
606 // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
607 // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
608 // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
609 // the DT_RUNPATH from the shared library above it, and not the executable,
610 // meaning that the lookup falls back to the system. Adding the $ORIGIN to the
611 // DT_RUNPATH here means that transient shared libraries can be found
612 // colocated with their parents.
613 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
Colin Cross16b23492016-01-06 14:41:07 -0800614 }
615
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700616 if Bool(sanitize.Properties.Sanitize.Cfi) {
Evgenii Stepanov7ebf9fa2017-01-20 14:13:06 -0800617 if ctx.Arch().ArchType == android.Arm {
618 // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
619 // to do this on a function basis, so force Thumb on the entire module.
620 flags.RequiredInstructionSet = "thumb"
621 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000622
Colin Cross4af21ed2019-11-04 09:37:55 -0800623 flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
624 flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
Cindy Zhou8cd45de2020-11-16 08:41:00 -0800625 if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
626 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
627 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000628 // Only append the default visibility flag if -fvisibility has not already been set
629 // to hidden.
Colin Cross4af21ed2019-11-04 09:37:55 -0800630 if !inList("-fvisibility=hidden", flags.Local.CFlags) {
631 flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000632 }
Colin Cross4af21ed2019-11-04 09:37:55 -0800633 flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000634
635 if ctx.staticBinary() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800636 _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
637 _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000638 }
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700639 }
640
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700641 if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
Colin Cross4af21ed2019-11-04 09:37:55 -0800642 flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700643 }
644
Jiyong Park379de2f2018-12-19 02:47:14 +0900645 if len(sanitize.Properties.Sanitizers) > 0 {
646 sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
Ivan Lozano30c5db22018-02-21 15:49:20 -0800647
Colin Cross4af21ed2019-11-04 09:37:55 -0800648 flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
649 flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
Colin Cross16b23492016-01-06 14:41:07 -0800650 if ctx.Host() {
Evgenii Stepanov76cee232017-01-27 15:44:44 -0800651 // Host sanitizers only link symbols in the final executable, so
652 // there will always be undefined symbols in intermediate libraries.
Colin Cross4af21ed2019-11-04 09:37:55 -0800653 _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
654 flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500655
656 // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
657 if !ctx.toolchain().Bionic() {
658 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
659 }
660 }
661
662 if enableMinimalRuntime(sanitize) {
663 flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
664 flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
665 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
666 if !ctx.toolchain().Bionic() {
667 flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
Ivan Lozano30c5db22018-02-21 15:49:20 -0800668 }
Colin Cross16b23492016-01-06 14:41:07 -0800669 }
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700670
671 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
672 // When fuzzing, we wish to crash with diagnostics on any bug.
Colin Cross4af21ed2019-11-04 09:37:55 -0800673 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700674 } else if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800675 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700676 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800677 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700678 }
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800679 // http://b/119329758, Android core does not boot up with this sanitizer yet.
Colin Cross4af21ed2019-11-04 09:37:55 -0800680 if toDisableImplicitIntegerChange(flags.Local.CFlags) {
681 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -0800682 }
Yabin Cuidb7dda82020-11-30 15:47:45 -0800683 // http://b/171275751, Android doesn't build with this sanitizer yet.
684 if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
685 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
686 }
Colin Cross16b23492016-01-06 14:41:07 -0800687 }
688
Jiyong Park379de2f2018-12-19 02:47:14 +0900689 if len(sanitize.Properties.DiagSanitizers) > 0 {
Colin Cross4af21ed2019-11-04 09:37:55 -0800690 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700691 }
692 // FIXME: enable RTTI if diag + (cfi or vptr)
693
Andreas Gampe97071162017-05-08 13:15:23 -0700694 if sanitize.Properties.Sanitize.Recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800695 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
Andreas Gampe97071162017-05-08 13:15:23 -0700696 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
697 }
698
Ivan Lozano7929bba2018-12-12 09:36:31 -0800699 if sanitize.Properties.Sanitize.Diag.No_recover != nil {
Colin Cross4af21ed2019-11-04 09:37:55 -0800700 flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
Ivan Lozano7929bba2018-12-12 09:36:31 -0800701 strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
702 }
703
Pirama Arumuga Nainar6c4ccca2020-07-27 11:49:51 -0700704 blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
705 if blocklist.Valid() {
706 flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blocklist.String())
707 flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
708 }
709
Colin Cross16b23492016-01-06 14:41:07 -0800710 return flags
711}
712
Colin Crossd80cbca2020-02-24 12:01:37 -0800713func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900714 // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
715 // both the sanitized and non-sanitized variants to make without a name conflict.
Colin Crossd80cbca2020-02-24 12:01:37 -0800716 if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
Jiyong Park1d1119f2019-07-29 21:27:18 +0900717 if Bool(sanitize.Properties.Sanitize.Cfi) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800718 entries.SubName += ".cfi"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900719 }
720 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800721 entries.SubName += ".hwasan"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900722 }
723 if Bool(sanitize.Properties.Sanitize.Scs) {
Colin Crossd80cbca2020-02-24 12:01:37 -0800724 entries.SubName += ".scs"
Jiyong Park1d1119f2019-07-29 21:27:18 +0900725 }
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800726 }
Colin Cross8ff9ef42017-05-08 13:44:11 -0700727}
728
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700729func (sanitize *sanitize) inSanitizerDir() bool {
730 return sanitize.Properties.InSanitizerDir
Colin Cross30d5f512016-05-03 18:02:42 -0700731}
732
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500733// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
734func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
Vishwath Mohan95229302017-08-11 00:53:16 +0000735 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500736 case Asan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000737 return sanitize.Properties.Sanitize.Address
Tri Vo6eafc362021-04-01 11:29:09 -0700738 case Hwasan:
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700739 return sanitize.Properties.Sanitize.Hwaddress
Vishwath Mohan95229302017-08-11 00:53:16 +0000740 case tsan:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000741 return sanitize.Properties.Sanitize.Thread
Vishwath Mohan95229302017-08-11 00:53:16 +0000742 case intOverflow:
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000743 return sanitize.Properties.Sanitize.Integer_overflow
744 case cfi:
745 return sanitize.Properties.Sanitize.Cfi
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800746 case scs:
747 return sanitize.Properties.Sanitize.Scs
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700748 case memtag_heap:
749 return sanitize.Properties.Sanitize.Memtag_heap
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500750 case Fuzzer:
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700751 return sanitize.Properties.Sanitize.Fuzzer
Vishwath Mohan95229302017-08-11 00:53:16 +0000752 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500753 panic(fmt.Errorf("unknown SanitizerType %d", t))
Vishwath Mohan95229302017-08-11 00:53:16 +0000754 }
755}
756
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500757// isUnsanitizedVariant returns true if no sanitizers are enabled.
Dan Albert7d1eecf2018-01-19 12:30:45 -0800758func (sanitize *sanitize) isUnsanitizedVariant() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500759 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700760 !sanitize.isSanitizerEnabled(Hwasan) &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800761 !sanitize.isSanitizerEnabled(tsan) &&
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800762 !sanitize.isSanitizerEnabled(cfi) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700763 !sanitize.isSanitizerEnabled(scs) &&
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700764 !sanitize.isSanitizerEnabled(memtag_heap) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500765 !sanitize.isSanitizerEnabled(Fuzzer)
Dan Albert7d1eecf2018-01-19 12:30:45 -0800766}
767
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500768// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700769func (sanitize *sanitize) isVariantOnProductionDevice() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500770 return !sanitize.isSanitizerEnabled(Asan) &&
Tri Vo6eafc362021-04-01 11:29:09 -0700771 !sanitize.isSanitizerEnabled(Hwasan) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -0700772 !sanitize.isSanitizerEnabled(tsan) &&
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500773 !sanitize.isSanitizerEnabled(Fuzzer)
Jayant Chowdharyb7e08ca2018-05-10 15:29:24 -0700774}
775
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500776func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400777 bPtr := proptools.BoolPtr(b)
778 if !b {
779 bPtr = nil
780 }
Colin Cross16b23492016-01-06 14:41:07 -0800781 switch t {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500782 case Asan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400783 sanitize.Properties.Sanitize.Address = bPtr
Tri Vo6eafc362021-04-01 11:29:09 -0700784 case Hwasan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400785 sanitize.Properties.Sanitize.Hwaddress = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800786 case tsan:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400787 sanitize.Properties.Sanitize.Thread = bPtr
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -0700788 case intOverflow:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400789 sanitize.Properties.Sanitize.Integer_overflow = bPtr
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000790 case cfi:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400791 sanitize.Properties.Sanitize.Cfi = bPtr
Peter Collingbourne8c7e6e22018-11-19 16:03:58 -0800792 case scs:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400793 sanitize.Properties.Sanitize.Scs = bPtr
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700794 case memtag_heap:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400795 sanitize.Properties.Sanitize.Memtag_heap = bPtr
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500796 case Fuzzer:
Liz Kammerb2fc4702021-06-25 14:53:40 -0400797 sanitize.Properties.Sanitize.Fuzzer = bPtr
Colin Cross16b23492016-01-06 14:41:07 -0800798 default:
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500799 panic(fmt.Errorf("unknown SanitizerType %d", t))
Colin Cross16b23492016-01-06 14:41:07 -0800800 }
801 if b {
802 sanitize.Properties.SanitizerEnabled = true
803 }
804}
805
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000806// Check if the sanitizer is explicitly disabled (as opposed to nil by
807// virtue of not being set).
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500808func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000809 if sanitize == nil {
810 return false
811 }
812
813 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
814 return sanitizerVal != nil && *sanitizerVal == false
815}
816
817// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
818// because enabling a sanitizer either directly (via the blueprint) or
819// indirectly (via a mutator) sets the bool ptr to true, and you can't
820// distinguish between the cases. It isn't needed though - both cases can be
821// treated identically.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500822func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000823 if sanitize == nil {
824 return false
825 }
826
827 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
828 return sanitizerVal != nil && *sanitizerVal == true
829}
830
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500831// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
832func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
Colin Cross6e511a92020-07-27 21:26:48 -0700833 switch t := tag.(type) {
834 case dependencyTag:
835 return t == reuseObjTag || t == objDepTag
836 case libraryDependencyTag:
837 return true
838 default:
839 return false
840 }
Colin Cross6b753602018-06-21 13:03:07 -0700841}
842
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500843func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
844 return IsSanitizableDependencyTag
845}
846
Inseob Kimc42f2f22020-07-29 20:32:10 +0900847// Determines if the current module is a static library going to be captured
848// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
849// except for ones which explicitly disable cfi.
850func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400851 if IsVendorProprietaryModule(mctx) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900852 return false
853 }
854
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500855 c := mctx.Module().(PlatformSanitizeable)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900856
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500857 if !c.InVendor() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900858 return false
859 }
860
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500861 if !c.StaticallyLinked() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900862 return false
863 }
864
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500865 if c.IsPrebuilt() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900866 return false
867 }
868
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500869 if !c.SanitizerSupported(cfi) {
870 return false
871 }
872
873 return c.SanitizePropDefined() &&
874 !c.SanitizeNever() &&
875 !c.IsSanitizerExplicitlyDisabled(cfi)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900876}
877
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700878// Propagate sanitizer requirements down from binaries
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500879func sanitizerDepsMutator(t SanitizerType) func(android.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700880 return func(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500881 if c, ok := mctx.Module().(PlatformSanitizeable); ok {
882 enabled := c.IsSanitizerEnabled(t)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900883 if t == cfi && needsCfiForVendorSnapshot(mctx) {
884 // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly
885 // determine defaultVariation in sanitizerMutator below.
886 // Instead, just mark SanitizeDep to forcefully create cfi variant.
887 enabled = true
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500888 c.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900889 }
890 if enabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500891 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Inseob Kimc42f2f22020-07-29 20:32:10 +0900892 mctx.WalkDeps(func(child, parent android.Module) bool {
893 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
894 return false
895 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500896 if d, ok := child.(PlatformSanitizeable); ok && d.SanitizePropDefined() &&
897 !d.SanitizeNever() &&
898 !d.IsSanitizerExplicitlyDisabled(t) {
Colin Crossaf98f582021-05-12 17:27:32 -0700899 if t == cfi || t == Hwasan || t == scs || t == Asan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500900 if d.StaticallyLinked() && d.SanitizerSupported(t) {
901 // Rust does not support some of these sanitizers, so we need to check if it's
902 // supported before setting this true.
903 d.SetSanitizeDep(true)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900904 }
905 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500906 d.SetSanitizeDep(true)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -0700907 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000908 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900909 return true
910 })
911 }
Jiyong Parkf97782b2019-02-13 20:28:58 +0900912 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
913 // If an APEX module includes a lib which is enabled for a sanitizer T, then
914 // the APEX module is also enabled for the same sanitizer type.
915 mctx.VisitDirectDeps(func(child android.Module) {
916 if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
917 sanitizeable.EnableSanitizer(t.name())
918 }
919 })
Colin Cross16b23492016-01-06 14:41:07 -0800920 }
921 }
922}
923
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500924func (c *Module) SanitizeNever() bool {
925 return Bool(c.sanitize.Properties.Sanitize.Never)
926}
927
928func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
929 return c.sanitize.isSanitizerExplicitlyDisabled(t)
930}
931
Ivan Lozano30c5db22018-02-21 15:49:20 -0800932// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
Colin Cross6b753602018-06-21 13:03:07 -0700933func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500934 // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
Colin Cross6b753602018-06-21 13:03:07 -0700935 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500936 isSanitizableDependencyTag := c.SanitizableDepTagChecker()
Colin Cross6b753602018-06-21 13:03:07 -0700937 mctx.WalkDeps(func(child, parent android.Module) bool {
938 if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
939 return false
940 }
Ivan Lozano30c5db22018-02-21 15:49:20 -0800941
Inseob Kimeec88e12020-01-22 11:11:29 +0900942 d, ok := child.(*Module)
943 if !ok || !d.static() {
944 return false
945 }
946 if d.sanitize != nil {
Colin Cross6b753602018-06-21 13:03:07 -0700947 if enableMinimalRuntime(d.sanitize) {
948 // If a static dependency is built with the minimal runtime,
949 // make sure we include the ubsan minimal runtime.
950 c.sanitize.Properties.MinimalRuntimeDep = true
Inseob Kim8471cda2019-11-15 09:59:12 +0900951 } else if enableUbsanRuntime(d.sanitize) {
Colin Cross6b753602018-06-21 13:03:07 -0700952 // If a static dependency runs with full ubsan diagnostics,
953 // make sure we include the ubsan runtime.
954 c.sanitize.Properties.UbsanRuntimeDep = true
Ivan Lozano30c5db22018-02-21 15:49:20 -0800955 }
Colin Cross0b908332019-06-19 23:00:20 -0700956
957 if c.sanitize.Properties.MinimalRuntimeDep &&
958 c.sanitize.Properties.UbsanRuntimeDep {
959 // both flags that this mutator might set are true, so don't bother recursing
960 return false
961 }
962
Ivan Lozano9ac32c72020-02-19 15:24:02 -0500963 if c.Os() == android.Linux {
964 c.sanitize.Properties.BuiltinsDep = true
965 }
966
Colin Cross0b908332019-06-19 23:00:20 -0700967 return true
Colin Cross6b753602018-06-21 13:03:07 -0700968 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900969
Jose Galmesf7294582020-11-13 12:07:36 -0800970 if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
Inseob Kimeec88e12020-01-22 11:11:29 +0900971 if Bool(p.properties.Sanitize_minimal_dep) {
972 c.sanitize.Properties.MinimalRuntimeDep = true
973 }
974 if Bool(p.properties.Sanitize_ubsan_dep) {
975 c.sanitize.Properties.UbsanRuntimeDep = true
976 }
977 }
978
979 return false
Colin Cross6b753602018-06-21 13:03:07 -0700980 })
Ivan Lozano30c5db22018-02-21 15:49:20 -0800981 }
982}
983
Jiyong Park379de2f2018-12-19 02:47:14 +0900984// Add the dependency to the runtime library for each of the sanitizer variants
985func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park379de2f2018-12-19 02:47:14 +0900986 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Pirama Arumuga Nainar6aa21022019-01-25 00:20:35 +0000987 if !c.Enabled() {
988 return
989 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900990 var sanitizers []string
991 var diagSanitizers []string
992
993 if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
994 sanitizers = append(sanitizers, "undefined")
995 } else {
996 if Bool(c.sanitize.Properties.Sanitize.Undefined) {
997 sanitizers = append(sanitizers,
998 "bool",
999 "integer-divide-by-zero",
1000 "return",
1001 "returns-nonnull-attribute",
1002 "shift-exponent",
1003 "unreachable",
1004 "vla-bound",
1005 // TODO(danalbert): The following checks currently have compiler performance issues.
1006 //"alignment",
1007 //"bounds",
1008 //"enum",
1009 //"float-cast-overflow",
1010 //"float-divide-by-zero",
1011 //"nonnull-attribute",
1012 //"null",
1013 //"shift-base",
1014 //"signed-integer-overflow",
1015 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
1016 // https://llvm.org/PR19302
1017 // http://reviews.llvm.org/D6974
1018 // "object-size",
1019 )
1020 }
1021 sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
1022 }
1023
1024 if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
1025 diagSanitizers = append(diagSanitizers, "undefined")
1026 }
1027
1028 diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
1029
1030 if Bool(c.sanitize.Properties.Sanitize.Address) {
1031 sanitizers = append(sanitizers, "address")
1032 diagSanitizers = append(diagSanitizers, "address")
1033 }
1034
1035 if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1036 sanitizers = append(sanitizers, "hwaddress")
1037 }
1038
1039 if Bool(c.sanitize.Properties.Sanitize.Thread) {
1040 sanitizers = append(sanitizers, "thread")
1041 }
1042
1043 if Bool(c.sanitize.Properties.Sanitize.Safestack) {
1044 sanitizers = append(sanitizers, "safe-stack")
1045 }
1046
1047 if Bool(c.sanitize.Properties.Sanitize.Cfi) {
1048 sanitizers = append(sanitizers, "cfi")
1049
1050 if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
1051 diagSanitizers = append(diagSanitizers, "cfi")
1052 }
1053 }
1054
1055 if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
1056 sanitizers = append(sanitizers, "unsigned-integer-overflow")
1057 sanitizers = append(sanitizers, "signed-integer-overflow")
1058 if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
1059 diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
1060 diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
1061 }
1062 }
1063
1064 if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1065 sanitizers = append(sanitizers, "scudo")
1066 }
1067
1068 if Bool(c.sanitize.Properties.Sanitize.Scs) {
1069 sanitizers = append(sanitizers, "shadow-call-stack")
1070 }
1071
Ivan Lozanod7586b62021-04-01 09:49:36 -04001072 if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.Binary() {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001073 noteDep := "note_memtag_heap_async"
1074 if Bool(c.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
1075 noteDep = "note_memtag_heap_sync"
1076 }
Inseob Kim253f5212021-04-08 17:10:31 +09001077 // If we're using snapshots, redirect to snapshot whenever possible
1078 // TODO(b/178470649): clean manual snapshot redirections
1079 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1080 if lib, ok := snapshot.StaticLibs[noteDep]; ok {
1081 noteDep = lib
1082 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07001083 depTag := libraryDependencyTag{Kind: staticLibraryDependency, wholeStatic: true}
1084 variations := append(mctx.Target().Variations(),
1085 blueprint.Variation{Mutator: "link", Variation: "static"})
1086 if c.Device() {
1087 variations = append(variations, c.ImageVariation())
1088 }
1089 mctx.AddFarVariationDependencies(variations, depTag, noteDep)
1090 }
1091
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001092 if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
1093 sanitizers = append(sanitizers, "fuzzer-no-link")
1094 }
1095
Jiyong Park379de2f2018-12-19 02:47:14 +09001096 // Save the list of sanitizers. These will be used again when generating
1097 // the build rules (for Cflags, etc.)
1098 c.sanitize.Properties.Sanitizers = sanitizers
1099 c.sanitize.Properties.DiagSanitizers = diagSanitizers
1100
Ivan Lozanof3b190f2020-03-06 12:01:21 -05001101 // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
1102 if c.Host() {
1103 diagSanitizers = sanitizers
1104 }
1105
Jiyong Park379de2f2018-12-19 02:47:14 +09001106 // Determine the runtime library required
1107 runtimeLibrary := ""
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001108 var extraStaticDeps []string
Jiyong Park379de2f2018-12-19 02:47:14 +09001109 toolchain := c.toolchain(mctx)
1110 if Bool(c.sanitize.Properties.Sanitize.Address) {
1111 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
1112 } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
1113 if c.staticBinary() {
1114 runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07001115 extraStaticDeps = []string{"libdl"}
Jiyong Park379de2f2018-12-19 02:47:14 +09001116 } else {
1117 runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
1118 }
1119 } else if Bool(c.sanitize.Properties.Sanitize.Thread) {
1120 runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
1121 } else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
1122 if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
1123 runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
1124 } else {
1125 runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
1126 }
Mitch Phillipsb8e593d2019-10-09 17:18:59 -07001127 } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001128 Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
1129 Bool(c.sanitize.Properties.Sanitize.Undefined) ||
1130 Bool(c.sanitize.Properties.Sanitize.All_undefined) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001131 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
Colin Cross32f1de32021-03-29 13:41:37 -07001132 if c.staticBinary() {
1133 runtimeLibrary += ".static"
1134 }
Jiyong Park379de2f2018-12-19 02:47:14 +09001135 }
1136
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001137 if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
1138 // UBSan is supported on non-bionic linux host builds as well
Jiyong Park379de2f2018-12-19 02:47:14 +09001139
1140 // Adding dependency to the runtime library. We are using *FarVariation*
1141 // because the runtime libraries themselves are not mutated by sanitizer
1142 // mutators and thus don't have sanitizer variants whereas this module
1143 // has been already mutated.
1144 //
1145 // Note that by adding dependency with {static|shared}DepTag, the lib is
1146 // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
1147 if c.staticBinary() {
Inseob Kimeec88e12020-01-22 11:11:29 +09001148 deps := append(extraStaticDeps, runtimeLibrary)
Colin Crosse0edaf92021-01-11 17:31:17 -08001149 // If we're using snapshots, redirect to snapshot whenever possible
1150 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1151 for idx, dep := range deps {
1152 if lib, ok := snapshot.StaticLibs[dep]; ok {
1153 deps[idx] = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001154 }
1155 }
1156
Jiyong Park379de2f2018-12-19 02:47:14 +09001157 // static executable gets static runtime libs
Colin Cross6e511a92020-07-27 21:26:48 -07001158 depTag := libraryDependencyTag{Kind: staticLibraryDependency}
Colin Cross42507332020-08-21 16:15:23 -07001159 variations := append(mctx.Target().Variations(),
1160 blueprint.Variation{Mutator: "link", Variation: "static"})
1161 if c.Device() {
1162 variations = append(variations, c.ImageVariation())
1163 }
1164 mctx.AddFarVariationDependencies(variations, depTag, deps...)
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001165 } else if !c.static() && !c.Header() {
Colin Crosse0edaf92021-01-11 17:31:17 -08001166 // If we're using snapshots, redirect to snapshot whenever possible
1167 snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
1168 if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
1169 runtimeLibrary = lib
Inseob Kimeec88e12020-01-22 11:11:29 +09001170 }
Colin Crosse0edaf92021-01-11 17:31:17 -08001171
Cindy Zhou18417cb2020-12-10 07:12:38 -08001172 // Skip apex dependency check for sharedLibraryDependency
1173 // when sanitizer diags are enabled. Skipping the check will allow
1174 // building with diag libraries without having to list the
1175 // dependency in Apex's allowed_deps file.
1176 diagEnabled := len(diagSanitizers) > 0
Jiyong Park3b1746a2019-01-29 11:15:04 +09001177 // dynamic executable and shared libs get shared runtime libs
Cindy Zhou18417cb2020-12-10 07:12:38 -08001178 depTag := libraryDependencyTag{
1179 Kind: sharedLibraryDependency,
1180 Order: earlyLibraryDependency,
1181
1182 skipApexAllowedDependenciesCheck: diagEnabled,
1183 }
Colin Cross42507332020-08-21 16:15:23 -07001184 variations := append(mctx.Target().Variations(),
1185 blueprint.Variation{Mutator: "link", Variation: "shared"})
1186 if c.Device() {
1187 variations = append(variations, c.ImageVariation())
1188 }
Ivan Lozanod67a6b02021-05-20 13:01:32 -04001189 AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeLibrary, "", true)
Jiyong Park379de2f2018-12-19 02:47:14 +09001190 }
1191 // static lib does not have dependency to the runtime library. The
1192 // dependency will be added to the executables or shared libs using
1193 // the static lib.
1194 }
1195 }
1196}
1197
1198type Sanitizeable interface {
1199 android.Module
Jiyong Park388ef3f2019-01-28 19:47:32 +09001200 IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
Jiyong Parkf97782b2019-02-13 20:28:58 +09001201 EnableSanitizer(sanitizerName string)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001202 AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
Jiyong Park379de2f2018-12-19 02:47:14 +09001203}
1204
Ivan Lozanod7586b62021-04-01 09:49:36 -04001205func (c *Module) MinimalRuntimeDep() bool {
1206 return c.sanitize.Properties.MinimalRuntimeDep
1207}
1208
1209func (c *Module) UbsanRuntimeDep() bool {
1210 return c.sanitize.Properties.UbsanRuntimeDep
1211}
1212
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001213func (c *Module) SanitizePropDefined() bool {
1214 return c.sanitize != nil
1215}
1216
1217func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
1218 return c.sanitize.isSanitizerEnabled(t)
1219}
1220
1221func (c *Module) SanitizeDep() bool {
1222 return c.sanitize.Properties.SanitizeDep
1223}
1224
1225func (c *Module) StaticallyLinked() bool {
1226 return c.static()
1227}
1228
1229func (c *Module) SetInSanitizerDir() {
1230 if c.sanitize != nil {
1231 c.sanitize.Properties.InSanitizerDir = true
1232 }
1233}
1234
1235func (c *Module) SetSanitizer(t SanitizerType, b bool) {
1236 if c.sanitize != nil {
1237 c.sanitize.SetSanitizer(t, b)
1238 }
1239}
1240
1241func (c *Module) SetSanitizeDep(b bool) {
1242 if c.sanitize != nil {
1243 c.sanitize.Properties.SanitizeDep = b
1244 }
1245}
1246
1247var _ PlatformSanitizeable = (*Module)(nil)
1248
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001249// Create sanitized variants for modules that need them
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001250func sanitizerMutator(t SanitizerType) func(android.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -07001251 return func(mctx android.BottomUpMutatorContext) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001252 if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
1253 if c.IsDependencyRoot() && c.IsSanitizerEnabled(t) {
Jiyong Park82226632019-02-01 10:50:50 +09001254 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001255 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1256 } else if c.IsSanitizerEnabled(t) || c.SanitizeDep() {
1257 isSanitizerEnabled := c.IsSanitizerEnabled(t)
Colin Crossaf98f582021-05-12 17:27:32 -07001258 if c.StaticallyLinked() || c.Header() || t == Fuzzer {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001259 // Static and header libs are split into non-sanitized and sanitized variants.
1260 // Shared libs are not split. However, for asan and fuzzer, we split even for shared
1261 // libs because a library sanitized for asan/fuzzer can't be linked from a library
1262 // that isn't sanitized for asan/fuzzer.
1263 //
1264 // Note for defaultVariation: since we don't split for shared libs but for static/header
1265 // libs, it is possible for the sanitized variant of a static/header lib to depend
1266 // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
1267 // error when the module is split. defaultVariation is the name of the variation that
1268 // will be used when such a dangling dependency occurs during the split of the current
1269 // module. By setting it to the name of the sanitized variation, the dangling dependency
1270 // is redirected to the sanitized variant of the dependent module.
1271 defaultVariation := t.variationName()
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001272 // Not all PlatformSanitizeable modules support the CFI sanitizer
1273 cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001274 mctx.SetDefaultDependencyVariation(&defaultVariation)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001275
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001276 modules := mctx.CreateVariations("", t.variationName())
1277 modules[0].(PlatformSanitizeable).SetSanitizer(t, false)
1278 modules[1].(PlatformSanitizeable).SetSanitizer(t, true)
1279 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
1280 modules[1].(PlatformSanitizeable).SetSanitizeDep(false)
1281
1282 if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
Ivan Lozano4774a812020-03-10 16:23:57 -04001283 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1284 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001285 modules[1].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001286 }
1287
Jiyong Park1d1119f2019-07-29 21:27:18 +09001288 // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
1289 // to Make, because the sanitized version has a different suffix in name.
1290 // For other types of sanitizers, suppress the variation that is disabled.
Tri Vo6eafc362021-04-01 11:29:09 -07001291 if t != cfi && t != scs && t != Hwasan {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001292 if isSanitizerEnabled {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001293 modules[0].(PlatformSanitizeable).SetPreventInstall()
1294 modules[0].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001295 } else {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001296 modules[1].(PlatformSanitizeable).SetPreventInstall()
1297 modules[1].(PlatformSanitizeable).SetHideFromMake()
Jiyong Park1d1119f2019-07-29 21:27:18 +09001298 }
1299 }
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001300
Jiyong Park1d1119f2019-07-29 21:27:18 +09001301 // Export the static lib name to make
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001302 if c.StaticallyLinked() && c.ExportedToMake() {
Jiyong Park1d1119f2019-07-29 21:27:18 +09001303 if t == cfi {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001304 cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
Tri Vo6eafc362021-04-01 11:29:09 -07001305 } else if t == Hwasan {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001306 hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
Jiyong Park1d1119f2019-07-29 21:27:18 +09001307 }
1308 }
1309 } else {
1310 // Shared libs are not split. Only the sanitized variant is created.
1311 modules := mctx.CreateVariations(t.variationName())
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001312 modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
1313 modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
Vishwath Mohane7128792017-11-17 11:08:10 -08001314
Jiyong Park1d1119f2019-07-29 21:27:18 +09001315 // locate the asan libraries under /data/asan
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001316 if mctx.Device() && t == Asan && isSanitizerEnabled {
1317 modules[0].(PlatformSanitizeable).SetInSanitizerDir()
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001318 }
Ivan Lozano4774a812020-03-10 16:23:57 -04001319
1320 if mctx.Device() && t.incompatibleWithCfi() {
1321 // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
1322 // are incompatible with cfi
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001323 modules[0].(PlatformSanitizeable).SetSanitizer(cfi, false)
Ivan Lozano4774a812020-03-10 16:23:57 -04001324 }
Vishwath Mohane21fe422017-11-01 19:42:45 -07001325 }
Colin Cross16b23492016-01-06 14:41:07 -08001326 }
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001327 c.SetSanitizeDep(false)
Jiyong Park82226632019-02-01 10:50:50 +09001328 } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
Jiyong Park379de2f2018-12-19 02:47:14 +09001329 // APEX modules fall here
Jooyung Han8ce8db92020-05-15 19:05:05 +09001330 sanitizeable.AddSanitizerDependencies(mctx, t.name())
Jiyong Park82226632019-02-01 10:50:50 +09001331 mctx.CreateVariations(t.variationName())
Inseob Kimc42f2f22020-07-29 20:32:10 +09001332 } else if c, ok := mctx.Module().(*Module); ok {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001333 //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
1334
Inseob Kimc42f2f22020-07-29 20:32:10 +09001335 // Check if it's a snapshot module supporting sanitizer
1336 if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) {
1337 // Set default variation as above.
1338 defaultVariation := t.variationName()
1339 mctx.SetDefaultDependencyVariation(&defaultVariation)
1340 modules := mctx.CreateVariations("", t.variationName())
1341 modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false)
1342 modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true)
1343
1344 // Export the static lib name to make
1345 if c.static() && c.ExportedToMake() {
1346 if t == cfi {
1347 // use BaseModuleName which is the name for Make.
1348 cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
1349 }
1350 }
1351 }
Colin Cross16b23492016-01-06 14:41:07 -08001352 }
1353 }
1354}
Vishwath Mohane7128792017-11-17 11:08:10 -08001355
Inseob Kim74d25562020-08-04 00:41:38 +09001356type sanitizerStaticLibsMap struct {
1357 // libsMap contains one list of modules per each image and each arch.
1358 // e.g. libs[vendor]["arm"] contains arm modules installed to vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001359 libsMap map[ImageVariantType]map[string][]string
Inseob Kim74d25562020-08-04 00:41:38 +09001360 libsMapLock sync.Mutex
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001361 sanitizerType SanitizerType
Inseob Kim74d25562020-08-04 00:41:38 +09001362}
1363
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001364func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
Inseob Kim74d25562020-08-04 00:41:38 +09001365 return &sanitizerStaticLibsMap{
1366 sanitizerType: t,
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001367 libsMap: make(map[ImageVariantType]map[string][]string),
Inseob Kim74d25562020-08-04 00:41:38 +09001368 }
1369}
1370
1371// Add the current module to sanitizer static libs maps
1372// Each module should pass its exported name as names of Make and Soong can differ.
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001373func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
1374 image := GetImageVariantType(c)
1375 arch := c.Module().Target().Arch.ArchType.String()
Inseob Kim74d25562020-08-04 00:41:38 +09001376
1377 s.libsMapLock.Lock()
1378 defer s.libsMapLock.Unlock()
1379
1380 if _, ok := s.libsMap[image]; !ok {
1381 s.libsMap[image] = make(map[string][]string)
1382 }
1383
1384 s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
1385}
1386
1387// Exports makefile variables in the following format:
1388// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
1389// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
1390// These are to be used by use_soong_sanitized_static_libraries.
1391// See build/make/core/binary.mk for more details.
1392func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
1393 for _, image := range android.SortedStringKeys(s.libsMap) {
Ivan Lozano3968d8f2020-12-14 11:27:52 -05001394 archMap := s.libsMap[ImageVariantType(image)]
Inseob Kim74d25562020-08-04 00:41:38 +09001395 for _, arch := range android.SortedStringKeys(archMap) {
1396 libs := archMap[arch]
1397 sort.Strings(libs)
1398
1399 key := fmt.Sprintf(
1400 "SOONG_%s_%s_%s_STATIC_LIBRARIES",
1401 s.sanitizerType.variationName(),
1402 image, // already upper
1403 arch)
1404
1405 ctx.Strict(key, strings.Join(libs, " "))
1406 }
1407 }
1408}
1409
Colin Cross571cccf2019-02-04 11:22:08 -08001410var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
1411
Inseob Kim74d25562020-08-04 00:41:38 +09001412func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001413 return config.Once(cfiStaticLibsKey, func() interface{} {
Inseob Kim74d25562020-08-04 00:41:38 +09001414 return newSanitizerStaticLibsMap(cfi)
1415 }).(*sanitizerStaticLibsMap)
Vishwath Mohane7128792017-11-17 11:08:10 -08001416}
1417
Colin Cross571cccf2019-02-04 11:22:08 -08001418var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
1419
Inseob Kim74d25562020-08-04 00:41:38 +09001420func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
Colin Cross571cccf2019-02-04 11:22:08 -08001421 return config.Once(hwasanStaticLibsKey, func() interface{} {
Tri Vo6eafc362021-04-01 11:29:09 -07001422 return newSanitizerStaticLibsMap(Hwasan)
Inseob Kim74d25562020-08-04 00:41:38 +09001423 }).(*sanitizerStaticLibsMap)
Jiyong Park1d1119f2019-07-29 21:27:18 +09001424}
1425
Ivan Lozano30c5db22018-02-21 15:49:20 -08001426func enableMinimalRuntime(sanitize *sanitize) bool {
1427 if !Bool(sanitize.Properties.Sanitize.Address) &&
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001428 !Bool(sanitize.Properties.Sanitize.Hwaddress) &&
Mitch Phillips5a6ea6c2019-05-01 14:42:05 -07001429 !Bool(sanitize.Properties.Sanitize.Fuzzer) &&
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001430
Ivan Lozano30c5db22018-02-21 15:49:20 -08001431 (Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001432 len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
1433 Bool(sanitize.Properties.Sanitize.Undefined) ||
1434 Bool(sanitize.Properties.Sanitize.All_undefined)) &&
1435
Ivan Lozano30c5db22018-02-21 15:49:20 -08001436 !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
1437 Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001438 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Ivan Lozano30c5db22018-02-21 15:49:20 -08001439 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001440
Ivan Lozano30c5db22018-02-21 15:49:20 -08001441 return true
1442 }
1443 return false
1444}
1445
Ivan Lozanod7586b62021-04-01 09:49:36 -04001446func (m *Module) UbsanRuntimeNeeded() bool {
1447 return enableUbsanRuntime(m.sanitize)
1448}
1449
1450func (m *Module) MinimalRuntimeNeeded() bool {
1451 return enableMinimalRuntime(m.sanitize)
1452}
1453
Inseob Kim8471cda2019-11-15 09:59:12 +09001454func enableUbsanRuntime(sanitize *sanitize) bool {
1455 return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
Ivan Lozano9ac32c72020-02-19 15:24:02 -05001456 Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
Inseob Kim8471cda2019-11-15 09:59:12 +09001457 len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
1458}
1459
Vishwath Mohane7128792017-11-17 11:08:10 -08001460func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001461 cfiStaticLibs(ctx.Config()).exportToMake(ctx)
Vishwath Mohane7128792017-11-17 11:08:10 -08001462}
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001463
1464func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
Inseob Kim74d25562020-08-04 00:41:38 +09001465 hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -07001466}