Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Jeff Gaston | 7276539 | 2017-11-28 16:37:53 -0800 | [diff] [blame] | 19 | "sort" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 20 | "strings" |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 21 | "sync" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 22 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint" |
| 24 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 25 | "android/soong/android" |
Evgenii Stepanov | af36db1 | 2016-08-15 14:18:24 -0700 | [diff] [blame] | 26 | "android/soong/cc/config" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 29 | var ( |
| 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 Mohan | f3918d3 | 2017-02-14 07:59:33 -0800 | [diff] [blame] | 33 | |
Yi Kong | 20233a4 | 2019-08-21 01:38:40 -0700 | [diff] [blame] | 34 | asanCflags = []string{ |
| 35 | "-fno-omit-frame-pointer", |
| 36 | "-fno-experimental-new-pass-manager", |
| 37 | } |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 38 | asanLdflags = []string{"-Wl,-u,__asan_preinit"} |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 39 | |
Peter Collingbourne | 967511a | 2019-03-19 21:39:54 -0700 | [diff] [blame] | 40 | hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=", |
Evgenii Stepanov | 96fa3dd | 2020-03-27 19:38:42 +0000 | [diff] [blame] | 41 | "-fsanitize-hwaddress-abi=platform", |
| 42 | "-fno-experimental-new-pass-manager", |
Evgenii Stepanov | 64bee4d | 2019-11-22 18:37:10 -0800 | [diff] [blame] | 43 | // The following improves debug location information |
| 44 | // availability at the cost of its accuracy. It increases |
| 45 | // the likelihood of a stack variable's frame offset |
| 46 | // to be recorded in the debug info, which is important |
| 47 | // for the quality of hwasan reports. The downside is a |
| 48 | // higher number of "optimized out" stack variables. |
| 49 | // b/112437883. |
| 50 | "-mllvm", "-instcombine-lower-dbg-declare=0", |
Mitch Phillips | b1c574f | 2020-06-22 13:28:23 -0700 | [diff] [blame] | 51 | // TODO(b/159343917): HWASan and GlobalISel don't play nicely, and |
| 52 | // GlobalISel is the default at -O0 on aarch64. |
| 53 | "-mllvm", "--aarch64-enable-global-isel-at-O=-1", |
| 54 | "-mllvm", "-fast-isel=false", |
Evgenii Stepanov | 64bee4d | 2019-11-22 18:37:10 -0800 | [diff] [blame] | 55 | } |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 56 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 57 | cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso", |
Pirama Arumuga Nainar | eb8d403 | 2020-07-27 11:22:35 -0700 | [diff] [blame] | 58 | "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blocklist.txt"} |
Evgenii Stepanov | dbf1d4f | 2018-08-31 12:54:33 -0700 | [diff] [blame] | 59 | // -flto and -fvisibility are required by clang when -fsanitize=cfi is |
| 60 | // used, but have no effect on assembly files |
| 61 | cfiAsflags = []string{"-flto", "-fvisibility=default"} |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 62 | cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi", |
Pirama Arumuga Nainar | bdb17f0 | 2017-08-28 21:50:17 -0700 | [diff] [blame] | 63 | "-Wl,-plugin-opt,O1"} |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 64 | cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map" |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 65 | |
Pirama Arumuga Nainar | eb8d403 | 2020-07-27 11:22:35 -0700 | [diff] [blame] | 66 | intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blocklist.txt"} |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 67 | |
Peter Collingbourne | bd19db0 | 2019-03-06 10:38:48 -0800 | [diff] [blame] | 68 | minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined", |
Ivan Lozano | ae6ae1d | 2018-10-08 09:29:39 -0700 | [diff] [blame] | 69 | "-fno-sanitize-recover=integer,undefined"} |
Evgenii Stepanov | 2c6484e | 2019-05-15 12:49:54 -0700 | [diff] [blame] | 70 | hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512", |
| 71 | "export_memory_stats=0", "max_malloc_fill_size=0"} |
Dan Willemsen | cbceaab | 2016-10-13 16:44:07 -0700 | [diff] [blame] | 72 | ) |
| 73 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 74 | type SanitizerType int |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 75 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 76 | func boolPtr(v bool) *bool { |
| 77 | if v { |
| 78 | return &v |
| 79 | } else { |
| 80 | return nil |
| 81 | } |
| 82 | } |
| 83 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 84 | const ( |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 85 | Asan SanitizerType = iota + 1 |
Tri Vo | 6eafc36 | 2021-04-01 11:29:09 -0700 | [diff] [blame] | 86 | Hwasan |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 87 | tsan |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 88 | intOverflow |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 89 | cfi |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 90 | scs |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 91 | Fuzzer |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 92 | memtag_heap |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 93 | ) |
| 94 | |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 95 | // Name of the sanitizer variation for this sanitizer type |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 96 | func (t SanitizerType) variationName() string { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 97 | switch t { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 98 | case Asan: |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 99 | return "asan" |
Tri Vo | 6eafc36 | 2021-04-01 11:29:09 -0700 | [diff] [blame] | 100 | case Hwasan: |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 101 | return "hwasan" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 102 | case tsan: |
| 103 | return "tsan" |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 104 | case intOverflow: |
| 105 | return "intOverflow" |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 106 | case cfi: |
| 107 | return "cfi" |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 108 | case scs: |
| 109 | return "scs" |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 110 | case memtag_heap: |
| 111 | return "memtag_heap" |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 112 | case Fuzzer: |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 113 | return "fuzzer" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 114 | default: |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 115 | panic(fmt.Errorf("unknown SanitizerType %d", t)) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 119 | // This is the sanitizer names in SANITIZE_[TARGET|HOST] |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 120 | func (t SanitizerType) name() string { |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 121 | switch t { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 122 | case Asan: |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 123 | return "address" |
Tri Vo | 6eafc36 | 2021-04-01 11:29:09 -0700 | [diff] [blame] | 124 | case Hwasan: |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 125 | return "hwaddress" |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 126 | case memtag_heap: |
| 127 | return "memtag_heap" |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 128 | case tsan: |
| 129 | return "thread" |
| 130 | case intOverflow: |
| 131 | return "integer_overflow" |
| 132 | case cfi: |
| 133 | return "cfi" |
| 134 | case scs: |
| 135 | return "shadow-call-stack" |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 136 | case Fuzzer: |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 137 | return "fuzzer" |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 138 | default: |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 139 | panic(fmt.Errorf("unknown SanitizerType %d", t)) |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 143 | func (*Module) SanitizerSupported(t SanitizerType) bool { |
| 144 | switch t { |
| 145 | case Asan: |
| 146 | return true |
Tri Vo | 6eafc36 | 2021-04-01 11:29:09 -0700 | [diff] [blame] | 147 | case Hwasan: |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 148 | return true |
| 149 | case tsan: |
| 150 | return true |
| 151 | case intOverflow: |
| 152 | return true |
| 153 | case cfi: |
| 154 | return true |
| 155 | case scs: |
| 156 | return true |
| 157 | case Fuzzer: |
| 158 | return true |
| 159 | default: |
| 160 | return false |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // incompatibleWithCfi returns true if a sanitizer is incompatible with CFI. |
| 165 | func (t SanitizerType) incompatibleWithCfi() bool { |
Tri Vo | 6eafc36 | 2021-04-01 11:29:09 -0700 | [diff] [blame] | 166 | return t == Asan || t == Fuzzer || t == Hwasan |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 167 | } |
| 168 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 169 | type SanitizeUserProps struct { |
| 170 | Never *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 171 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 172 | // main sanitizers |
| 173 | Address *bool `android:"arch_variant"` |
| 174 | Thread *bool `android:"arch_variant"` |
| 175 | Hwaddress *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 176 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 177 | // local sanitizers |
| 178 | Undefined *bool `android:"arch_variant"` |
| 179 | All_undefined *bool `android:"arch_variant"` |
| 180 | Misc_undefined []string `android:"arch_variant"` |
| 181 | Fuzzer *bool `android:"arch_variant"` |
| 182 | Safestack *bool `android:"arch_variant"` |
| 183 | Cfi *bool `android:"arch_variant"` |
| 184 | Integer_overflow *bool `android:"arch_variant"` |
| 185 | Scudo *bool `android:"arch_variant"` |
| 186 | Scs *bool `android:"arch_variant"` |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 187 | Memtag_heap *bool `android:"arch_variant"` |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 188 | |
| 189 | // A modifier for ASAN and HWASAN for write only instrumentation |
| 190 | Writeonly *bool `android:"arch_variant"` |
| 191 | |
| 192 | // Sanitizers to run in the diagnostic mode (as opposed to the release mode). |
| 193 | // Replaces abort() on error with a human-readable error message. |
| 194 | // Address and Thread sanitizers always run in diagnostic mode. |
| 195 | Diag struct { |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 196 | Undefined *bool `android:"arch_variant"` |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 197 | Cfi *bool `android:"arch_variant"` |
| 198 | Integer_overflow *bool `android:"arch_variant"` |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 199 | Memtag_heap *bool `android:"arch_variant"` |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 200 | Misc_undefined []string `android:"arch_variant"` |
Cindy Zhou | d3fe492 | 2020-12-01 11:14:30 -0800 | [diff] [blame] | 201 | No_recover []string `android:"arch_variant"` |
| 202 | } `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 203 | |
Cindy Zhou | 8cd45de | 2020-11-16 08:41:00 -0800 | [diff] [blame] | 204 | // Sanitizers to run with flag configuration specified |
| 205 | Config struct { |
| 206 | // Enables CFI support flags for assembly-heavy libraries |
| 207 | Cfi_assembly_support *bool `android:"arch_variant"` |
Cindy Zhou | d3fe492 | 2020-12-01 11:14:30 -0800 | [diff] [blame] | 208 | } `android:"arch_variant"` |
Cindy Zhou | 8cd45de | 2020-11-16 08:41:00 -0800 | [diff] [blame] | 209 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 210 | // value to pass to -fsanitize-recover= |
| 211 | Recover []string |
Jasraj Bedi | bb4511d | 2020-07-23 22:58:17 +0000 | [diff] [blame] | 212 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 213 | // value to pass to -fsanitize-blacklist |
| 214 | Blocklist *string |
| 215 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 216 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 217 | type SanitizeProperties struct { |
| 218 | // Enable AddressSanitizer, ThreadSanitizer, UndefinedBehaviorSanitizer, and |
| 219 | // others. Please see SanitizerUserProps in build/soong/cc/sanitize.go for |
| 220 | // details. |
| 221 | Sanitize SanitizeUserProps `android:"arch_variant"` |
| 222 | SanitizerEnabled bool `blueprint:"mutated"` |
| 223 | SanitizeDep bool `blueprint:"mutated"` |
| 224 | MinimalRuntimeDep bool `blueprint:"mutated"` |
| 225 | BuiltinsDep bool `blueprint:"mutated"` |
| 226 | UbsanRuntimeDep bool `blueprint:"mutated"` |
| 227 | InSanitizerDir bool `blueprint:"mutated"` |
| 228 | Sanitizers []string `blueprint:"mutated"` |
| 229 | DiagSanitizers []string `blueprint:"mutated"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | type sanitize struct { |
| 233 | Properties SanitizeProperties |
| 234 | } |
| 235 | |
Cindy Zhou | 18417cb | 2020-12-10 07:12:38 -0800 | [diff] [blame] | 236 | // Mark this tag with a check to see if apex dependency check should be skipped |
| 237 | func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool { |
| 238 | return t.skipApexAllowedDependenciesCheck |
| 239 | } |
| 240 | |
| 241 | var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil) |
| 242 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 243 | func init() { |
| 244 | android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider) |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 245 | android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 248 | func (sanitize *sanitize) props() []interface{} { |
| 249 | return []interface{}{&sanitize.Properties} |
| 250 | } |
| 251 | |
| 252 | func (sanitize *sanitize) begin(ctx BaseModuleContext) { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 253 | s := &sanitize.Properties.Sanitize |
| 254 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 255 | // Don't apply sanitizers to NDK code. |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 256 | if ctx.useSdk() { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 257 | s.Never = BoolPtr(true) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Doug Horn | c32c6b0 | 2019-01-17 14:44:05 -0800 | [diff] [blame] | 260 | // Sanitizers do not work on Fuchsia yet. |
| 261 | if ctx.Fuchsia() { |
| 262 | s.Never = BoolPtr(true) |
| 263 | } |
| 264 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 265 | // Never always wins. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 266 | if Bool(s.Never) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 267 | return |
| 268 | } |
| 269 | |
Evgenii Stepanov | 04896ca | 2021-01-12 18:28:33 -0800 | [diff] [blame] | 270 | // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap}). |
| 271 | if ctx.testBinary() && s.Memtag_heap == nil { |
| 272 | s.Memtag_heap = boolPtr(true) |
| 273 | s.Diag.Memtag_heap = boolPtr(true) |
| 274 | } |
| 275 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 276 | var globalSanitizers []string |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 277 | var globalSanitizersDiag []string |
| 278 | |
Dan Willemsen | 8536d6b | 2018-10-07 20:54:34 -0700 | [diff] [blame] | 279 | if ctx.Host() { |
| 280 | if !ctx.Windows() { |
| 281 | globalSanitizers = ctx.Config().SanitizeHost() |
| 282 | } |
| 283 | } else { |
| 284 | arches := ctx.Config().SanitizeDeviceArch() |
| 285 | if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) { |
| 286 | globalSanitizers = ctx.Config().SanitizeDevice() |
| 287 | globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag() |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 291 | if len(globalSanitizers) > 0 { |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 292 | var found bool |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 293 | if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil { |
| 294 | s.All_undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 295 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 296 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 297 | if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil { |
| 298 | s.Undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 301 | if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil { |
| 302 | s.Address = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 305 | if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil { |
| 306 | s.Thread = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 309 | if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil { |
| 310 | s.Fuzzer = boolPtr(true) |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil { |
| 314 | s.Safestack = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 317 | if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 318 | if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) { |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 319 | s.Cfi = boolPtr(true) |
| 320 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 323 | // Global integer_overflow builds do not support static libraries. |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 324 | if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil { |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 325 | if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() { |
Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 326 | s.Integer_overflow = boolPtr(true) |
| 327 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Kostya Kortchinsky | d18ae5c | 2018-06-12 14:46:54 -0700 | [diff] [blame] | 330 | if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil { |
| 331 | s.Scudo = boolPtr(true) |
| 332 | } |
| 333 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 334 | if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil { |
| 335 | s.Hwaddress = boolPtr(true) |
| 336 | } |
| 337 | |
Jasraj Bedi | bb4511d | 2020-07-23 22:58:17 +0000 | [diff] [blame] | 338 | if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil { |
| 339 | // Hwaddress and Address are set before, so we can check them here |
| 340 | // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false |
| 341 | if s.Address == nil && s.Hwaddress == nil { |
| 342 | ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'") |
| 343 | } |
| 344 | s.Writeonly = boolPtr(true) |
| 345 | } |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 346 | if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil { |
Evgenii Stepanov | 4beaa0c | 2021-01-05 16:41:26 -0800 | [diff] [blame] | 347 | if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) { |
| 348 | s.Memtag_heap = boolPtr(true) |
| 349 | } |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 350 | } |
Jasraj Bedi | bb4511d | 2020-07-23 22:58:17 +0000 | [diff] [blame] | 351 | |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 352 | if len(globalSanitizers) > 0 { |
| 353 | ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0]) |
| 354 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 355 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 356 | // Global integer_overflow builds do not support static library diagnostics. |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 357 | if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found && |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 358 | s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() { |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 359 | s.Diag.Integer_overflow = boolPtr(true) |
| 360 | } |
| 361 | |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 362 | if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found && |
| 363 | s.Diag.Cfi == nil && Bool(s.Cfi) { |
| 364 | s.Diag.Cfi = boolPtr(true) |
| 365 | } |
| 366 | |
Evgenii Stepanov | 04896ca | 2021-01-12 18:28:33 -0800 | [diff] [blame] | 367 | if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found && |
| 368 | s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) { |
| 369 | s.Diag.Memtag_heap = boolPtr(true) |
| 370 | } |
| 371 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 372 | if len(globalSanitizersDiag) > 0 { |
| 373 | ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0]) |
| 374 | } |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 375 | } |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 376 | |
Evgenii Stepanov | 4beaa0c | 2021-01-05 16:41:26 -0800 | [diff] [blame] | 377 | // Enable Memtag for all components in the include paths (for Aarch64 only) |
Evgenii Stepanov | 04896ca | 2021-01-12 18:28:33 -0800 | [diff] [blame] | 378 | if ctx.Arch().ArchType == android.Arm64 { |
Evgenii Stepanov | 4beaa0c | 2021-01-05 16:41:26 -0800 | [diff] [blame] | 379 | if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) { |
Evgenii Stepanov | 04896ca | 2021-01-12 18:28:33 -0800 | [diff] [blame] | 380 | if s.Memtag_heap == nil { |
| 381 | s.Memtag_heap = boolPtr(true) |
| 382 | } |
| 383 | if s.Diag.Memtag_heap == nil { |
| 384 | s.Diag.Memtag_heap = boolPtr(true) |
| 385 | } |
Evgenii Stepanov | 4beaa0c | 2021-01-05 16:41:26 -0800 | [diff] [blame] | 386 | } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) { |
Evgenii Stepanov | 04896ca | 2021-01-12 18:28:33 -0800 | [diff] [blame] | 387 | if s.Memtag_heap == nil { |
| 388 | s.Memtag_heap = boolPtr(true) |
| 389 | } |
Evgenii Stepanov | 4beaa0c | 2021-01-05 16:41:26 -0800 | [diff] [blame] | 390 | } |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Vishwath Mohan | 1c54f66 | 2018-05-24 18:36:18 -0700 | [diff] [blame] | 393 | // Enable CFI for all components in the include paths (for Aarch64 only) |
| 394 | if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 { |
Vishwath Mohan | 3af8ee0 | 2018-03-30 02:55:23 +0000 | [diff] [blame] | 395 | s.Cfi = boolPtr(true) |
| 396 | if inList("cfi", ctx.Config().SanitizeDeviceDiag()) { |
| 397 | s.Diag.Cfi = boolPtr(true) |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Elliott Hughes | da3a071 | 2020-03-06 16:55:28 -0800 | [diff] [blame] | 401 | // Is CFI actually enabled? |
| 402 | if !ctx.Config().EnableCFI() { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 403 | s.Cfi = boolPtr(false) |
| 404 | s.Diag.Cfi = boolPtr(false) |
Vishwath Mohan | 1b017a7 | 2017-01-19 13:54:55 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 407 | // HWASan requires AArch64 hardware feature (top-byte-ignore). |
| 408 | if ctx.Arch().ArchType != android.Arm64 { |
| 409 | s.Hwaddress = nil |
| 410 | } |
| 411 | |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 412 | // SCS is only implemented on AArch64. |
Peter Collingbourne | bd19db0 | 2019-03-06 10:38:48 -0800 | [diff] [blame] | 413 | if ctx.Arch().ArchType != android.Arm64 { |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 414 | s.Scs = nil |
| 415 | } |
| 416 | |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 417 | // memtag_heap is only implemented on AArch64. |
| 418 | if ctx.Arch().ArchType != android.Arm64 { |
| 419 | s.Memtag_heap = nil |
| 420 | } |
| 421 | |
Vishwath Mohan | 8f4fdd8 | 2017-04-20 07:42:52 -0700 | [diff] [blame] | 422 | // Also disable CFI if ASAN is enabled. |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 423 | if Bool(s.Address) || Bool(s.Hwaddress) { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 424 | s.Cfi = boolPtr(false) |
| 425 | s.Diag.Cfi = boolPtr(false) |
Vishwath Mohan | 8f4fdd8 | 2017-04-20 07:42:52 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 428 | // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds. |
| 429 | if !ctx.Os().Linux() { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 430 | s.Cfi = boolPtr(false) |
| 431 | s.Diag.Cfi = boolPtr(false) |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 432 | s.Misc_undefined = nil |
| 433 | s.Undefined = nil |
| 434 | s.All_undefined = nil |
| 435 | s.Integer_overflow = nil |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Vishwath Mohan | 9ccbba0 | 2018-05-28 13:54:48 -0700 | [diff] [blame] | 438 | // Also disable CFI for VNDK variants of components |
| 439 | if ctx.isVndk() && ctx.useVndk() { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 440 | if ctx.static() { |
| 441 | // Cfi variant for static vndk should be captured as vendor snapshot, |
| 442 | // so don't strictly disable Cfi. |
| 443 | s.Cfi = nil |
| 444 | s.Diag.Cfi = nil |
| 445 | } else { |
| 446 | s.Cfi = boolPtr(false) |
| 447 | s.Diag.Cfi = boolPtr(false) |
| 448 | } |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 449 | } |
| 450 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 451 | // HWASan ramdisk (which is built from recovery) goes over some bootloader limit. |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 452 | // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary. |
| 453 | if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 454 | s.Hwaddress = nil |
| 455 | } |
| 456 | |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 457 | if ctx.staticBinary() { |
| 458 | s.Address = nil |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 459 | s.Fuzzer = nil |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 460 | s.Thread = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 463 | if Bool(s.All_undefined) { |
| 464 | s.Undefined = nil |
| 465 | } |
| 466 | |
Evgenii Stepanov | 0a8a0d0 | 2016-05-12 13:54:53 -0700 | [diff] [blame] | 467 | if !ctx.toolchain().Is64Bit() { |
| 468 | // TSAN and SafeStack are not supported on 32-bit architectures |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 469 | s.Thread = nil |
| 470 | s.Safestack = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 471 | // TODO(ccross): error for compile_multilib = "32"? |
| 472 | } |
| 473 | |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 474 | if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) || |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 475 | Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 || |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 476 | Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap)) { |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 477 | sanitize.Properties.SanitizerEnabled = true |
| 478 | } |
| 479 | |
Kostya Kortchinsky | d5275c8 | 2019-02-01 08:42:56 -0800 | [diff] [blame] | 480 | // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally. |
| 481 | if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() { |
Kostya Kortchinsky | d18ae5c | 2018-06-12 14:46:54 -0700 | [diff] [blame] | 482 | s.Scudo = nil |
| 483 | } |
| 484 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 485 | if Bool(s.Hwaddress) { |
| 486 | s.Address = nil |
| 487 | s.Thread = nil |
| 488 | } |
| 489 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 490 | // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is |
| 491 | // mutually incompatible. |
| 492 | if Bool(s.Fuzzer) { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 493 | s.Cfi = boolPtr(false) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| 497 | func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 498 | if !sanitize.Properties.SanitizerEnabled { // || c.static() { |
| 499 | return deps |
| 500 | } |
| 501 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 502 | return deps |
| 503 | } |
| 504 | |
Chih-Hung Hsieh | 3567e62 | 2018-11-15 14:01:36 -0800 | [diff] [blame] | 505 | func toDisableImplicitIntegerChange(flags []string) bool { |
| 506 | // Returns true if any flag is fsanitize*integer, and there is |
| 507 | // no explicit flag about sanitize=implicit-integer-sign-change. |
| 508 | for _, f := range flags { |
| 509 | if strings.Contains(f, "sanitize=implicit-integer-sign-change") { |
| 510 | return false |
| 511 | } |
| 512 | } |
| 513 | for _, f := range flags { |
| 514 | if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") { |
| 515 | return true |
| 516 | } |
| 517 | } |
| 518 | return false |
| 519 | } |
| 520 | |
Yabin Cui | db7dda8 | 2020-11-30 15:47:45 -0800 | [diff] [blame] | 521 | func toDisableUnsignedShiftBaseChange(flags []string) bool { |
| 522 | // Returns true if any flag is fsanitize*integer, and there is |
| 523 | // no explicit flag about sanitize=unsigned-shift-base. |
| 524 | for _, f := range flags { |
| 525 | if strings.Contains(f, "sanitize=unsigned-shift-base") { |
| 526 | return false |
| 527 | } |
| 528 | } |
| 529 | for _, f := range flags { |
| 530 | if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") { |
| 531 | return true |
| 532 | } |
| 533 | } |
| 534 | return false |
| 535 | } |
| 536 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 537 | func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags { |
Ivan Lozano | 59fdea2 | 2018-05-10 14:17:22 -0700 | [diff] [blame] | 538 | minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a" |
| 539 | minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 540 | builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a" |
| 541 | builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 542 | |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 543 | if sanitize.Properties.MinimalRuntimeDep { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 544 | flags.Local.LdFlags = append(flags.Local.LdFlags, |
| 545 | minimalRuntimePath, |
| 546 | "-Wl,--exclude-libs,"+minimalRuntimeLib) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 547 | } |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 548 | |
| 549 | if sanitize.Properties.BuiltinsDep { |
| 550 | flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...) |
| 551 | } |
| 552 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 553 | if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 554 | return flags |
| 555 | } |
| 556 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 557 | if Bool(sanitize.Properties.Sanitize.Address) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 558 | if ctx.Arch().ArchType == android.Arm { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 559 | // Frame pointer based unwinder in ASan requires ARM frame setup. |
| 560 | // TODO: put in flags? |
| 561 | flags.RequiredInstructionSet = "arm" |
| 562 | } |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 563 | flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...) |
| 564 | flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 565 | |
Jasraj Bedi | bb4511d | 2020-07-23 22:58:17 +0000 | [diff] [blame] | 566 | if Bool(sanitize.Properties.Sanitize.Writeonly) { |
| 567 | flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0") |
| 568 | } |
| 569 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 570 | if ctx.Host() { |
| 571 | // -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 572 | // libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 573 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed") |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 574 | } else { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 575 | flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0") |
Jiyong Park | a2aca28 | 2019-02-02 13:13:38 +0900 | [diff] [blame] | 576 | if ctx.bootstrap() { |
| 577 | flags.DynamicLinker = "/system/bin/bootstrap/linker_asan" |
| 578 | } else { |
| 579 | flags.DynamicLinker = "/system/bin/linker_asan" |
| 580 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 581 | if flags.Toolchain.Is64Bit() { |
| 582 | flags.DynamicLinker += "64" |
| 583 | } |
| 584 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 587 | if Bool(sanitize.Properties.Sanitize.Hwaddress) { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 588 | flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...) |
Jasraj Bedi | bb4511d | 2020-07-23 22:58:17 +0000 | [diff] [blame] | 589 | if Bool(sanitize.Properties.Sanitize.Writeonly) { |
| 590 | flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0") |
| 591 | } |
Yabin Cui | 6be405e | 2017-10-19 15:52:11 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 594 | if Bool(sanitize.Properties.Sanitize.Fuzzer) { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 595 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link") |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 596 | |
| 597 | // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 598 | _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags) |
| 599 | _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags) |
| 600 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto") |
| 601 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto") |
Mitch Phillips | 7438475 | 2019-06-17 10:33:52 -0700 | [diff] [blame] | 602 | |
Mitch Phillips | b8e593d | 2019-10-09 17:18:59 -0700 | [diff] [blame] | 603 | // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries |
| 604 | // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus |
| 605 | // doesn't match the linker script due to the "__emutls_v." prefix). |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 606 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth") |
| 607 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth") |
Mitch Phillips | b8e593d | 2019-10-09 17:18:59 -0700 | [diff] [blame] | 608 | |
Mitch Phillips | 7438475 | 2019-06-17 10:33:52 -0700 | [diff] [blame] | 609 | // TODO(b/133876586): Experimental PM breaks sanitizer coverage. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 610 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager") |
Mitch Phillips | b9b3e79 | 2019-08-28 12:41:07 -0700 | [diff] [blame] | 611 | |
| 612 | // Disable fortify for fuzzing builds. Generally, we'll be building with |
| 613 | // UBSan or ASan here and the fortify checks pollute the stack traces. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 614 | flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE") |
Mitch Phillips | 734b4cb | 2019-12-10 08:44:52 -0800 | [diff] [blame] | 615 | |
| 616 | // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's |
| 617 | // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and |
| 618 | // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets |
| 619 | // the DT_RUNPATH from the shared library above it, and not the executable, |
| 620 | // meaning that the lookup falls back to the system. Adding the $ORIGIN to the |
| 621 | // DT_RUNPATH here means that transient shared libraries can be found |
| 622 | // colocated with their parents. |
| 623 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 626 | if Bool(sanitize.Properties.Sanitize.Cfi) { |
Evgenii Stepanov | 7ebf9fa | 2017-01-20 14:13:06 -0800 | [diff] [blame] | 627 | if ctx.Arch().ArchType == android.Arm { |
| 628 | // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up |
| 629 | // to do this on a function basis, so force Thumb on the entire module. |
| 630 | flags.RequiredInstructionSet = "thumb" |
| 631 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 632 | |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 633 | flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...) |
| 634 | flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...) |
Cindy Zhou | 8cd45de | 2020-11-16 08:41:00 -0800 | [diff] [blame] | 635 | if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) { |
| 636 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables") |
| 637 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 638 | // Only append the default visibility flag if -fvisibility has not already been set |
| 639 | // to hidden. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 640 | if !inList("-fvisibility=hidden", flags.Local.CFlags) { |
| 641 | flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default") |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 642 | } |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 643 | flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 644 | |
| 645 | if ctx.staticBinary() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 646 | _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags) |
| 647 | _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 648 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 651 | if Bool(sanitize.Properties.Sanitize.Integer_overflow) { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 652 | flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...) |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 655 | if len(sanitize.Properties.Sanitizers) > 0 { |
| 656 | sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",") |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 657 | |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 658 | flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg) |
| 659 | flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 660 | if ctx.Host() { |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 661 | // Host sanitizers only link symbols in the final executable, so |
| 662 | // there will always be undefined symbols in intermediate libraries. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 663 | _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags) |
| 664 | flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg) |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 665 | |
| 666 | // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers |
| 667 | if !ctx.toolchain().Bionic() { |
| 668 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function") |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | if enableMinimalRuntime(sanitize) { |
| 673 | flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " ")) |
| 674 | flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...) |
| 675 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib) |
| 676 | if !ctx.toolchain().Bionic() { |
| 677 | flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 678 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 679 | } |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 680 | |
| 681 | if Bool(sanitize.Properties.Sanitize.Fuzzer) { |
| 682 | // When fuzzing, we wish to crash with diagnostics on any bug. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 683 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all") |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 684 | } else if ctx.Host() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 685 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all") |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 686 | } else { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 687 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort") |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 688 | } |
Chih-Hung Hsieh | 3567e62 | 2018-11-15 14:01:36 -0800 | [diff] [blame] | 689 | // http://b/119329758, Android core does not boot up with this sanitizer yet. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 690 | if toDisableImplicitIntegerChange(flags.Local.CFlags) { |
| 691 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change") |
Chih-Hung Hsieh | 3567e62 | 2018-11-15 14:01:36 -0800 | [diff] [blame] | 692 | } |
Yabin Cui | db7dda8 | 2020-11-30 15:47:45 -0800 | [diff] [blame] | 693 | // http://b/171275751, Android doesn't build with this sanitizer yet. |
| 694 | if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) { |
| 695 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base") |
| 696 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 697 | } |
| 698 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 699 | if len(sanitize.Properties.DiagSanitizers) > 0 { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 700 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ",")) |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 701 | } |
| 702 | // FIXME: enable RTTI if diag + (cfi or vptr) |
| 703 | |
Andreas Gampe | 9707116 | 2017-05-08 13:15:23 -0700 | [diff] [blame] | 704 | if sanitize.Properties.Sanitize.Recover != nil { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 705 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+ |
Andreas Gampe | 9707116 | 2017-05-08 13:15:23 -0700 | [diff] [blame] | 706 | strings.Join(sanitize.Properties.Sanitize.Recover, ",")) |
| 707 | } |
| 708 | |
Ivan Lozano | 7929bba | 2018-12-12 09:36:31 -0800 | [diff] [blame] | 709 | if sanitize.Properties.Sanitize.Diag.No_recover != nil { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 710 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+ |
Ivan Lozano | 7929bba | 2018-12-12 09:36:31 -0800 | [diff] [blame] | 711 | strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ",")) |
| 712 | } |
| 713 | |
Pirama Arumuga Nainar | 6c4ccca | 2020-07-27 11:49:51 -0700 | [diff] [blame] | 714 | blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist) |
| 715 | if blocklist.Valid() { |
| 716 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blocklist.String()) |
| 717 | flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path()) |
| 718 | } |
| 719 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 720 | return flags |
| 721 | } |
| 722 | |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 723 | func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 724 | // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing |
| 725 | // both the sanitized and non-sanitized variants to make without a name conflict. |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 726 | if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 727 | if Bool(sanitize.Properties.Sanitize.Cfi) { |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 728 | entries.SubName += ".cfi" |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 729 | } |
| 730 | if Bool(sanitize.Properties.Sanitize.Hwaddress) { |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 731 | entries.SubName += ".hwasan" |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 732 | } |
| 733 | if Bool(sanitize.Properties.Sanitize.Scs) { |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 734 | entries.SubName += ".scs" |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 735 | } |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 736 | } |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Vishwath Mohan | 1dd8839 | 2017-03-29 22:00:18 -0700 | [diff] [blame] | 739 | func (sanitize *sanitize) inSanitizerDir() bool { |
| 740 | return sanitize.Properties.InSanitizerDir |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 743 | // getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties. |
| 744 | func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool { |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 745 | switch t { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 746 | case Asan: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 747 | return sanitize.Properties.Sanitize.Address |
Tri Vo | 6eafc36 | 2021-04-01 11:29:09 -0700 | [diff] [blame] | 748 | case Hwasan: |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 749 | return sanitize.Properties.Sanitize.Hwaddress |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 750 | case tsan: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 751 | return sanitize.Properties.Sanitize.Thread |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 752 | case intOverflow: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 753 | return sanitize.Properties.Sanitize.Integer_overflow |
| 754 | case cfi: |
| 755 | return sanitize.Properties.Sanitize.Cfi |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 756 | case scs: |
| 757 | return sanitize.Properties.Sanitize.Scs |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 758 | case memtag_heap: |
| 759 | return sanitize.Properties.Sanitize.Memtag_heap |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 760 | case Fuzzer: |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 761 | return sanitize.Properties.Sanitize.Fuzzer |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 762 | default: |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 763 | panic(fmt.Errorf("unknown SanitizerType %d", t)) |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 767 | // isUnsanitizedVariant returns true if no sanitizers are enabled. |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 768 | func (sanitize *sanitize) isUnsanitizedVariant() bool { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 769 | return !sanitize.isSanitizerEnabled(Asan) && |
Tri Vo | 6eafc36 | 2021-04-01 11:29:09 -0700 | [diff] [blame] | 770 | !sanitize.isSanitizerEnabled(Hwasan) && |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 771 | !sanitize.isSanitizerEnabled(tsan) && |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 772 | !sanitize.isSanitizerEnabled(cfi) && |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 773 | !sanitize.isSanitizerEnabled(scs) && |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 774 | !sanitize.isSanitizerEnabled(memtag_heap) && |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 775 | !sanitize.isSanitizerEnabled(Fuzzer) |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 776 | } |
| 777 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 778 | // isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled). |
Jayant Chowdhary | b7e08ca | 2018-05-10 15:29:24 -0700 | [diff] [blame] | 779 | func (sanitize *sanitize) isVariantOnProductionDevice() bool { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 780 | return !sanitize.isSanitizerEnabled(Asan) && |
Tri Vo | 6eafc36 | 2021-04-01 11:29:09 -0700 | [diff] [blame] | 781 | !sanitize.isSanitizerEnabled(Hwasan) && |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 782 | !sanitize.isSanitizerEnabled(tsan) && |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 783 | !sanitize.isSanitizerEnabled(Fuzzer) |
Jayant Chowdhary | b7e08ca | 2018-05-10 15:29:24 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 786 | func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 787 | switch t { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 788 | case Asan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 789 | sanitize.Properties.Sanitize.Address = boolPtr(b) |
Tri Vo | 6eafc36 | 2021-04-01 11:29:09 -0700 | [diff] [blame] | 790 | case Hwasan: |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 791 | sanitize.Properties.Sanitize.Hwaddress = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 792 | case tsan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 793 | sanitize.Properties.Sanitize.Thread = boolPtr(b) |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 794 | case intOverflow: |
| 795 | sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 796 | case cfi: |
| 797 | sanitize.Properties.Sanitize.Cfi = boolPtr(b) |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 798 | case scs: |
| 799 | sanitize.Properties.Sanitize.Scs = boolPtr(b) |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 800 | case memtag_heap: |
| 801 | sanitize.Properties.Sanitize.Memtag_heap = boolPtr(b) |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 802 | case Fuzzer: |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 803 | sanitize.Properties.Sanitize.Fuzzer = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 804 | default: |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 805 | panic(fmt.Errorf("unknown SanitizerType %d", t)) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 806 | } |
| 807 | if b { |
| 808 | sanitize.Properties.SanitizerEnabled = true |
| 809 | } |
| 810 | } |
| 811 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 812 | // Check if the sanitizer is explicitly disabled (as opposed to nil by |
| 813 | // virtue of not being set). |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 814 | func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 815 | if sanitize == nil { |
| 816 | return false |
| 817 | } |
| 818 | |
| 819 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 820 | return sanitizerVal != nil && *sanitizerVal == false |
| 821 | } |
| 822 | |
| 823 | // There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled) |
| 824 | // because enabling a sanitizer either directly (via the blueprint) or |
| 825 | // indirectly (via a mutator) sets the bool ptr to true, and you can't |
| 826 | // distinguish between the cases. It isn't needed though - both cases can be |
| 827 | // treated identically. |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 828 | func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 829 | if sanitize == nil { |
| 830 | return false |
| 831 | } |
| 832 | |
| 833 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 834 | return sanitizerVal != nil && *sanitizerVal == true |
| 835 | } |
| 836 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 837 | // IsSanitizableDependencyTag returns true if the dependency tag is sanitizable. |
| 838 | func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool { |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 839 | switch t := tag.(type) { |
| 840 | case dependencyTag: |
| 841 | return t == reuseObjTag || t == objDepTag |
| 842 | case libraryDependencyTag: |
| 843 | return true |
| 844 | default: |
| 845 | return false |
| 846 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 849 | func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker { |
| 850 | return IsSanitizableDependencyTag |
| 851 | } |
| 852 | |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 853 | // Determines if the current module is a static library going to be captured |
| 854 | // as vendor snapshot. Such modules must create both cfi and non-cfi variants, |
| 855 | // except for ones which explicitly disable cfi. |
| 856 | func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool { |
Bill Peckham | 945441c | 2020-08-31 16:07:58 -0700 | [diff] [blame] | 857 | if isVendorProprietaryModule(mctx) { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 858 | return false |
| 859 | } |
| 860 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 861 | c := mctx.Module().(PlatformSanitizeable) |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 862 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 863 | if !c.InVendor() { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 864 | return false |
| 865 | } |
| 866 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 867 | if !c.StaticallyLinked() { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 868 | return false |
| 869 | } |
| 870 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 871 | if c.IsPrebuilt() { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 872 | return false |
| 873 | } |
| 874 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 875 | if !c.SanitizerSupported(cfi) { |
| 876 | return false |
| 877 | } |
| 878 | |
| 879 | return c.SanitizePropDefined() && |
| 880 | !c.SanitizeNever() && |
| 881 | !c.IsSanitizerExplicitlyDisabled(cfi) |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 882 | } |
| 883 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 884 | // Propagate sanitizer requirements down from binaries |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 885 | func sanitizerDepsMutator(t SanitizerType) func(android.TopDownMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 886 | return func(mctx android.TopDownMutatorContext) { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 887 | if c, ok := mctx.Module().(PlatformSanitizeable); ok { |
| 888 | enabled := c.IsSanitizerEnabled(t) |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 889 | if t == cfi && needsCfiForVendorSnapshot(mctx) { |
| 890 | // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly |
| 891 | // determine defaultVariation in sanitizerMutator below. |
| 892 | // Instead, just mark SanitizeDep to forcefully create cfi variant. |
| 893 | enabled = true |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 894 | c.SetSanitizeDep(true) |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 895 | } |
| 896 | if enabled { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 897 | isSanitizableDependencyTag := c.SanitizableDepTagChecker() |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 898 | mctx.WalkDeps(func(child, parent android.Module) bool { |
| 899 | if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) { |
| 900 | return false |
| 901 | } |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 902 | if d, ok := child.(PlatformSanitizeable); ok && d.SanitizePropDefined() && |
| 903 | !d.SanitizeNever() && |
| 904 | !d.IsSanitizerExplicitlyDisabled(t) { |
Colin Cross | af98f58 | 2021-05-12 17:27:32 -0700 | [diff] [blame^] | 905 | if t == cfi || t == Hwasan || t == scs || t == Asan { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 906 | if d.StaticallyLinked() && d.SanitizerSupported(t) { |
| 907 | // Rust does not support some of these sanitizers, so we need to check if it's |
| 908 | // supported before setting this true. |
| 909 | d.SetSanitizeDep(true) |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 910 | } |
| 911 | } else { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 912 | d.SetSanitizeDep(true) |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 913 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 914 | } |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 915 | return true |
| 916 | }) |
| 917 | } |
Jiyong Park | f97782b | 2019-02-13 20:28:58 +0900 | [diff] [blame] | 918 | } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok { |
| 919 | // If an APEX module includes a lib which is enabled for a sanitizer T, then |
| 920 | // the APEX module is also enabled for the same sanitizer type. |
| 921 | mctx.VisitDirectDeps(func(child android.Module) { |
| 922 | if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) { |
| 923 | sanitizeable.EnableSanitizer(t.name()) |
| 924 | } |
| 925 | }) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 930 | func (c *Module) SanitizeNever() bool { |
| 931 | return Bool(c.sanitize.Properties.Sanitize.Never) |
| 932 | } |
| 933 | |
| 934 | func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool { |
| 935 | return c.sanitize.isSanitizerExplicitlyDisabled(t) |
| 936 | } |
| 937 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 938 | // Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies. |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 939 | func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 940 | // Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers. |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 941 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 942 | isSanitizableDependencyTag := c.SanitizableDepTagChecker() |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 943 | mctx.WalkDeps(func(child, parent android.Module) bool { |
| 944 | if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) { |
| 945 | return false |
| 946 | } |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 947 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 948 | d, ok := child.(*Module) |
| 949 | if !ok || !d.static() { |
| 950 | return false |
| 951 | } |
| 952 | if d.sanitize != nil { |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 953 | if enableMinimalRuntime(d.sanitize) { |
| 954 | // If a static dependency is built with the minimal runtime, |
| 955 | // make sure we include the ubsan minimal runtime. |
| 956 | c.sanitize.Properties.MinimalRuntimeDep = true |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 957 | } else if enableUbsanRuntime(d.sanitize) { |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 958 | // If a static dependency runs with full ubsan diagnostics, |
| 959 | // make sure we include the ubsan runtime. |
| 960 | c.sanitize.Properties.UbsanRuntimeDep = true |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 961 | } |
Colin Cross | 0b90833 | 2019-06-19 23:00:20 -0700 | [diff] [blame] | 962 | |
| 963 | if c.sanitize.Properties.MinimalRuntimeDep && |
| 964 | c.sanitize.Properties.UbsanRuntimeDep { |
| 965 | // both flags that this mutator might set are true, so don't bother recursing |
| 966 | return false |
| 967 | } |
| 968 | |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 969 | if c.Os() == android.Linux { |
| 970 | c.sanitize.Properties.BuiltinsDep = true |
| 971 | } |
| 972 | |
Colin Cross | 0b90833 | 2019-06-19 23:00:20 -0700 | [diff] [blame] | 973 | return true |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 974 | } |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 975 | |
Jose Galmes | f729458 | 2020-11-13 12:07:36 -0800 | [diff] [blame] | 976 | if p, ok := d.linker.(*snapshotLibraryDecorator); ok { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 977 | if Bool(p.properties.Sanitize_minimal_dep) { |
| 978 | c.sanitize.Properties.MinimalRuntimeDep = true |
| 979 | } |
| 980 | if Bool(p.properties.Sanitize_ubsan_dep) { |
| 981 | c.sanitize.Properties.UbsanRuntimeDep = true |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | return false |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 986 | }) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 990 | // Add the dependency to the runtime library for each of the sanitizer variants |
| 991 | func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 992 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
Pirama Arumuga Nainar | 6aa2102 | 2019-01-25 00:20:35 +0000 | [diff] [blame] | 993 | if !c.Enabled() { |
| 994 | return |
| 995 | } |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 996 | var sanitizers []string |
| 997 | var diagSanitizers []string |
| 998 | |
| 999 | if Bool(c.sanitize.Properties.Sanitize.All_undefined) { |
| 1000 | sanitizers = append(sanitizers, "undefined") |
| 1001 | } else { |
| 1002 | if Bool(c.sanitize.Properties.Sanitize.Undefined) { |
| 1003 | sanitizers = append(sanitizers, |
| 1004 | "bool", |
| 1005 | "integer-divide-by-zero", |
| 1006 | "return", |
| 1007 | "returns-nonnull-attribute", |
| 1008 | "shift-exponent", |
| 1009 | "unreachable", |
| 1010 | "vla-bound", |
| 1011 | // TODO(danalbert): The following checks currently have compiler performance issues. |
| 1012 | //"alignment", |
| 1013 | //"bounds", |
| 1014 | //"enum", |
| 1015 | //"float-cast-overflow", |
| 1016 | //"float-divide-by-zero", |
| 1017 | //"nonnull-attribute", |
| 1018 | //"null", |
| 1019 | //"shift-base", |
| 1020 | //"signed-integer-overflow", |
| 1021 | // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on. |
| 1022 | // https://llvm.org/PR19302 |
| 1023 | // http://reviews.llvm.org/D6974 |
| 1024 | // "object-size", |
| 1025 | ) |
| 1026 | } |
| 1027 | sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...) |
| 1028 | } |
| 1029 | |
| 1030 | if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) { |
| 1031 | diagSanitizers = append(diagSanitizers, "undefined") |
| 1032 | } |
| 1033 | |
| 1034 | diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...) |
| 1035 | |
| 1036 | if Bool(c.sanitize.Properties.Sanitize.Address) { |
| 1037 | sanitizers = append(sanitizers, "address") |
| 1038 | diagSanitizers = append(diagSanitizers, "address") |
| 1039 | } |
| 1040 | |
| 1041 | if Bool(c.sanitize.Properties.Sanitize.Hwaddress) { |
| 1042 | sanitizers = append(sanitizers, "hwaddress") |
| 1043 | } |
| 1044 | |
| 1045 | if Bool(c.sanitize.Properties.Sanitize.Thread) { |
| 1046 | sanitizers = append(sanitizers, "thread") |
| 1047 | } |
| 1048 | |
| 1049 | if Bool(c.sanitize.Properties.Sanitize.Safestack) { |
| 1050 | sanitizers = append(sanitizers, "safe-stack") |
| 1051 | } |
| 1052 | |
| 1053 | if Bool(c.sanitize.Properties.Sanitize.Cfi) { |
| 1054 | sanitizers = append(sanitizers, "cfi") |
| 1055 | |
| 1056 | if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) { |
| 1057 | diagSanitizers = append(diagSanitizers, "cfi") |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) { |
| 1062 | sanitizers = append(sanitizers, "unsigned-integer-overflow") |
| 1063 | sanitizers = append(sanitizers, "signed-integer-overflow") |
| 1064 | if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) { |
| 1065 | diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow") |
| 1066 | diagSanitizers = append(diagSanitizers, "signed-integer-overflow") |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | if Bool(c.sanitize.Properties.Sanitize.Scudo) { |
| 1071 | sanitizers = append(sanitizers, "scudo") |
| 1072 | } |
| 1073 | |
| 1074 | if Bool(c.sanitize.Properties.Sanitize.Scs) { |
| 1075 | sanitizers = append(sanitizers, "shadow-call-stack") |
| 1076 | } |
| 1077 | |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 1078 | if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.binary() { |
| 1079 | noteDep := "note_memtag_heap_async" |
| 1080 | if Bool(c.sanitize.Properties.Sanitize.Diag.Memtag_heap) { |
| 1081 | noteDep = "note_memtag_heap_sync" |
| 1082 | } |
Inseob Kim | 253f521 | 2021-04-08 17:10:31 +0900 | [diff] [blame] | 1083 | // If we're using snapshots, redirect to snapshot whenever possible |
| 1084 | // TODO(b/178470649): clean manual snapshot redirections |
| 1085 | snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo) |
| 1086 | if lib, ok := snapshot.StaticLibs[noteDep]; ok { |
| 1087 | noteDep = lib |
| 1088 | } |
Evgenii Stepanov | 193ac2e | 2020-04-28 15:09:12 -0700 | [diff] [blame] | 1089 | depTag := libraryDependencyTag{Kind: staticLibraryDependency, wholeStatic: true} |
| 1090 | variations := append(mctx.Target().Variations(), |
| 1091 | blueprint.Variation{Mutator: "link", Variation: "static"}) |
| 1092 | if c.Device() { |
| 1093 | variations = append(variations, c.ImageVariation()) |
| 1094 | } |
| 1095 | mctx.AddFarVariationDependencies(variations, depTag, noteDep) |
| 1096 | } |
| 1097 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 1098 | if Bool(c.sanitize.Properties.Sanitize.Fuzzer) { |
| 1099 | sanitizers = append(sanitizers, "fuzzer-no-link") |
| 1100 | } |
| 1101 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1102 | // Save the list of sanitizers. These will be used again when generating |
| 1103 | // the build rules (for Cflags, etc.) |
| 1104 | c.sanitize.Properties.Sanitizers = sanitizers |
| 1105 | c.sanitize.Properties.DiagSanitizers = diagSanitizers |
| 1106 | |
Ivan Lozano | f3b190f | 2020-03-06 12:01:21 -0500 | [diff] [blame] | 1107 | // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used. |
| 1108 | if c.Host() { |
| 1109 | diagSanitizers = sanitizers |
| 1110 | } |
| 1111 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1112 | // Determine the runtime library required |
| 1113 | runtimeLibrary := "" |
Ryan Prichard | b49fe1b | 2019-10-11 15:03:34 -0700 | [diff] [blame] | 1114 | var extraStaticDeps []string |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1115 | toolchain := c.toolchain(mctx) |
| 1116 | if Bool(c.sanitize.Properties.Sanitize.Address) { |
| 1117 | runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain) |
| 1118 | } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) { |
| 1119 | if c.staticBinary() { |
| 1120 | runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain) |
Ryan Prichard | b49fe1b | 2019-10-11 15:03:34 -0700 | [diff] [blame] | 1121 | extraStaticDeps = []string{"libdl"} |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1122 | } else { |
| 1123 | runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain) |
| 1124 | } |
| 1125 | } else if Bool(c.sanitize.Properties.Sanitize.Thread) { |
| 1126 | runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain) |
| 1127 | } else if Bool(c.sanitize.Properties.Sanitize.Scudo) { |
| 1128 | if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep { |
| 1129 | runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain) |
| 1130 | } else { |
| 1131 | runtimeLibrary = config.ScudoRuntimeLibrary(toolchain) |
| 1132 | } |
Mitch Phillips | b8e593d | 2019-10-09 17:18:59 -0700 | [diff] [blame] | 1133 | } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep || |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1134 | Bool(c.sanitize.Properties.Sanitize.Fuzzer) || |
| 1135 | Bool(c.sanitize.Properties.Sanitize.Undefined) || |
| 1136 | Bool(c.sanitize.Properties.Sanitize.All_undefined) { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1137 | runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain) |
Colin Cross | 32f1de3 | 2021-03-29 13:41:37 -0700 | [diff] [blame] | 1138 | if c.staticBinary() { |
| 1139 | runtimeLibrary += ".static" |
| 1140 | } |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1141 | } |
| 1142 | |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1143 | if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) { |
| 1144 | // UBSan is supported on non-bionic linux host builds as well |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1145 | |
| 1146 | // Adding dependency to the runtime library. We are using *FarVariation* |
| 1147 | // because the runtime libraries themselves are not mutated by sanitizer |
| 1148 | // mutators and thus don't have sanitizer variants whereas this module |
| 1149 | // has been already mutated. |
| 1150 | // |
| 1151 | // Note that by adding dependency with {static|shared}DepTag, the lib is |
| 1152 | // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module |
| 1153 | if c.staticBinary() { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 1154 | deps := append(extraStaticDeps, runtimeLibrary) |
Colin Cross | e0edaf9 | 2021-01-11 17:31:17 -0800 | [diff] [blame] | 1155 | // If we're using snapshots, redirect to snapshot whenever possible |
| 1156 | snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo) |
| 1157 | for idx, dep := range deps { |
| 1158 | if lib, ok := snapshot.StaticLibs[dep]; ok { |
| 1159 | deps[idx] = lib |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1163 | // static executable gets static runtime libs |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 1164 | depTag := libraryDependencyTag{Kind: staticLibraryDependency} |
Colin Cross | 4250733 | 2020-08-21 16:15:23 -0700 | [diff] [blame] | 1165 | variations := append(mctx.Target().Variations(), |
| 1166 | blueprint.Variation{Mutator: "link", Variation: "static"}) |
| 1167 | if c.Device() { |
| 1168 | variations = append(variations, c.ImageVariation()) |
| 1169 | } |
| 1170 | mctx.AddFarVariationDependencies(variations, depTag, deps...) |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1171 | } else if !c.static() && !c.Header() { |
Colin Cross | e0edaf9 | 2021-01-11 17:31:17 -0800 | [diff] [blame] | 1172 | // If we're using snapshots, redirect to snapshot whenever possible |
| 1173 | snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo) |
| 1174 | if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok { |
| 1175 | runtimeLibrary = lib |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 1176 | } |
Colin Cross | e0edaf9 | 2021-01-11 17:31:17 -0800 | [diff] [blame] | 1177 | |
Cindy Zhou | 18417cb | 2020-12-10 07:12:38 -0800 | [diff] [blame] | 1178 | // Skip apex dependency check for sharedLibraryDependency |
| 1179 | // when sanitizer diags are enabled. Skipping the check will allow |
| 1180 | // building with diag libraries without having to list the |
| 1181 | // dependency in Apex's allowed_deps file. |
| 1182 | diagEnabled := len(diagSanitizers) > 0 |
Jiyong Park | 3b1746a | 2019-01-29 11:15:04 +0900 | [diff] [blame] | 1183 | // dynamic executable and shared libs get shared runtime libs |
Cindy Zhou | 18417cb | 2020-12-10 07:12:38 -0800 | [diff] [blame] | 1184 | depTag := libraryDependencyTag{ |
| 1185 | Kind: sharedLibraryDependency, |
| 1186 | Order: earlyLibraryDependency, |
| 1187 | |
| 1188 | skipApexAllowedDependenciesCheck: diagEnabled, |
| 1189 | } |
Colin Cross | 4250733 | 2020-08-21 16:15:23 -0700 | [diff] [blame] | 1190 | variations := append(mctx.Target().Variations(), |
| 1191 | blueprint.Variation{Mutator: "link", Variation: "shared"}) |
| 1192 | if c.Device() { |
| 1193 | variations = append(variations, c.ImageVariation()) |
| 1194 | } |
Colin Cross | e7257d2 | 2020-09-24 09:56:18 -0700 | [diff] [blame] | 1195 | c.addSharedLibDependenciesWithVersions(mctx, variations, depTag, runtimeLibrary, "", true) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1196 | } |
| 1197 | // static lib does not have dependency to the runtime library. The |
| 1198 | // dependency will be added to the executables or shared libs using |
| 1199 | // the static lib. |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | type Sanitizeable interface { |
| 1205 | android.Module |
Jiyong Park | 388ef3f | 2019-01-28 19:47:32 +0900 | [diff] [blame] | 1206 | IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool |
Jiyong Park | f97782b | 2019-02-13 20:28:58 +0900 | [diff] [blame] | 1207 | EnableSanitizer(sanitizerName string) |
Jooyung Han | 8ce8db9 | 2020-05-15 19:05:05 +0900 | [diff] [blame] | 1208 | AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1209 | } |
| 1210 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1211 | func (c *Module) SanitizePropDefined() bool { |
| 1212 | return c.sanitize != nil |
| 1213 | } |
| 1214 | |
| 1215 | func (c *Module) IsSanitizerEnabled(t SanitizerType) bool { |
| 1216 | return c.sanitize.isSanitizerEnabled(t) |
| 1217 | } |
| 1218 | |
| 1219 | func (c *Module) SanitizeDep() bool { |
| 1220 | return c.sanitize.Properties.SanitizeDep |
| 1221 | } |
| 1222 | |
| 1223 | func (c *Module) StaticallyLinked() bool { |
| 1224 | return c.static() |
| 1225 | } |
| 1226 | |
| 1227 | func (c *Module) SetInSanitizerDir() { |
| 1228 | if c.sanitize != nil { |
| 1229 | c.sanitize.Properties.InSanitizerDir = true |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | func (c *Module) SetSanitizer(t SanitizerType, b bool) { |
| 1234 | if c.sanitize != nil { |
| 1235 | c.sanitize.SetSanitizer(t, b) |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | func (c *Module) SetSanitizeDep(b bool) { |
| 1240 | if c.sanitize != nil { |
| 1241 | c.sanitize.Properties.SanitizeDep = b |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | var _ PlatformSanitizeable = (*Module)(nil) |
| 1246 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1247 | // Create sanitized variants for modules that need them |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1248 | func sanitizerMutator(t SanitizerType) func(android.BottomUpMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 1249 | return func(mctx android.BottomUpMutatorContext) { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1250 | if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() { |
| 1251 | if c.IsDependencyRoot() && c.IsSanitizerEnabled(t) { |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 1252 | modules := mctx.CreateVariations(t.variationName()) |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1253 | modules[0].(PlatformSanitizeable).SetSanitizer(t, true) |
| 1254 | } else if c.IsSanitizerEnabled(t) || c.SanitizeDep() { |
| 1255 | isSanitizerEnabled := c.IsSanitizerEnabled(t) |
Colin Cross | af98f58 | 2021-05-12 17:27:32 -0700 | [diff] [blame^] | 1256 | if c.StaticallyLinked() || c.Header() || t == Fuzzer { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1257 | // Static and header libs are split into non-sanitized and sanitized variants. |
| 1258 | // Shared libs are not split. However, for asan and fuzzer, we split even for shared |
| 1259 | // libs because a library sanitized for asan/fuzzer can't be linked from a library |
| 1260 | // that isn't sanitized for asan/fuzzer. |
| 1261 | // |
| 1262 | // Note for defaultVariation: since we don't split for shared libs but for static/header |
| 1263 | // libs, it is possible for the sanitized variant of a static/header lib to depend |
| 1264 | // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an |
| 1265 | // error when the module is split. defaultVariation is the name of the variation that |
| 1266 | // will be used when such a dangling dependency occurs during the split of the current |
| 1267 | // module. By setting it to the name of the sanitized variation, the dangling dependency |
| 1268 | // is redirected to the sanitized variant of the dependent module. |
| 1269 | defaultVariation := t.variationName() |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1270 | // Not all PlatformSanitizeable modules support the CFI sanitizer |
| 1271 | cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi) |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1272 | mctx.SetDefaultDependencyVariation(&defaultVariation) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1273 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1274 | modules := mctx.CreateVariations("", t.variationName()) |
| 1275 | modules[0].(PlatformSanitizeable).SetSanitizer(t, false) |
| 1276 | modules[1].(PlatformSanitizeable).SetSanitizer(t, true) |
| 1277 | modules[0].(PlatformSanitizeable).SetSanitizeDep(false) |
| 1278 | modules[1].(PlatformSanitizeable).SetSanitizeDep(false) |
| 1279 | |
| 1280 | if mctx.Device() && t.incompatibleWithCfi() && cfiSupported { |
Ivan Lozano | 4774a81 | 2020-03-10 16:23:57 -0400 | [diff] [blame] | 1281 | // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that |
| 1282 | // are incompatible with cfi |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1283 | modules[1].(PlatformSanitizeable).SetSanitizer(cfi, false) |
Ivan Lozano | 4774a81 | 2020-03-10 16:23:57 -0400 | [diff] [blame] | 1284 | } |
| 1285 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1286 | // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants |
| 1287 | // to Make, because the sanitized version has a different suffix in name. |
| 1288 | // For other types of sanitizers, suppress the variation that is disabled. |
Tri Vo | 6eafc36 | 2021-04-01 11:29:09 -0700 | [diff] [blame] | 1289 | if t != cfi && t != scs && t != Hwasan { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1290 | if isSanitizerEnabled { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1291 | modules[0].(PlatformSanitizeable).SetPreventInstall() |
| 1292 | modules[0].(PlatformSanitizeable).SetHideFromMake() |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1293 | } else { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1294 | modules[1].(PlatformSanitizeable).SetPreventInstall() |
| 1295 | modules[1].(PlatformSanitizeable).SetHideFromMake() |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1296 | } |
| 1297 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1298 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1299 | // Export the static lib name to make |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1300 | if c.StaticallyLinked() && c.ExportedToMake() { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1301 | if t == cfi { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1302 | cfiStaticLibs(mctx.Config()).add(c, c.Module().Name()) |
Tri Vo | 6eafc36 | 2021-04-01 11:29:09 -0700 | [diff] [blame] | 1303 | } else if t == Hwasan { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1304 | hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name()) |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | } else { |
| 1308 | // Shared libs are not split. Only the sanitized variant is created. |
| 1309 | modules := mctx.CreateVariations(t.variationName()) |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1310 | modules[0].(PlatformSanitizeable).SetSanitizer(t, true) |
| 1311 | modules[0].(PlatformSanitizeable).SetSanitizeDep(false) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1312 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1313 | // locate the asan libraries under /data/asan |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1314 | if mctx.Device() && t == Asan && isSanitizerEnabled { |
| 1315 | modules[0].(PlatformSanitizeable).SetInSanitizerDir() |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1316 | } |
Ivan Lozano | 4774a81 | 2020-03-10 16:23:57 -0400 | [diff] [blame] | 1317 | |
| 1318 | if mctx.Device() && t.incompatibleWithCfi() { |
| 1319 | // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that |
| 1320 | // are incompatible with cfi |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1321 | modules[0].(PlatformSanitizeable).SetSanitizer(cfi, false) |
Ivan Lozano | 4774a81 | 2020-03-10 16:23:57 -0400 | [diff] [blame] | 1322 | } |
Vishwath Mohan | e21fe42 | 2017-11-01 19:42:45 -0700 | [diff] [blame] | 1323 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 1324 | } |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1325 | c.SetSanitizeDep(false) |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 1326 | } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1327 | // APEX modules fall here |
Jooyung Han | 8ce8db9 | 2020-05-15 19:05:05 +0900 | [diff] [blame] | 1328 | sanitizeable.AddSanitizerDependencies(mctx, t.name()) |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 1329 | mctx.CreateVariations(t.variationName()) |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 1330 | } else if c, ok := mctx.Module().(*Module); ok { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1331 | //TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable |
| 1332 | |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 1333 | // Check if it's a snapshot module supporting sanitizer |
| 1334 | if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) { |
| 1335 | // Set default variation as above. |
| 1336 | defaultVariation := t.variationName() |
| 1337 | mctx.SetDefaultDependencyVariation(&defaultVariation) |
| 1338 | modules := mctx.CreateVariations("", t.variationName()) |
| 1339 | modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false) |
| 1340 | modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true) |
| 1341 | |
| 1342 | // Export the static lib name to make |
| 1343 | if c.static() && c.ExportedToMake() { |
| 1344 | if t == cfi { |
| 1345 | // use BaseModuleName which is the name for Make. |
| 1346 | cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName()) |
| 1347 | } |
| 1348 | } |
| 1349 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | } |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1353 | |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1354 | type sanitizerStaticLibsMap struct { |
| 1355 | // libsMap contains one list of modules per each image and each arch. |
| 1356 | // e.g. libs[vendor]["arm"] contains arm modules installed to vendor |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1357 | libsMap map[ImageVariantType]map[string][]string |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1358 | libsMapLock sync.Mutex |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1359 | sanitizerType SanitizerType |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1360 | } |
| 1361 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1362 | func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap { |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1363 | return &sanitizerStaticLibsMap{ |
| 1364 | sanitizerType: t, |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1365 | libsMap: make(map[ImageVariantType]map[string][]string), |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | // Add the current module to sanitizer static libs maps |
| 1370 | // Each module should pass its exported name as names of Make and Soong can differ. |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1371 | func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) { |
| 1372 | image := GetImageVariantType(c) |
| 1373 | arch := c.Module().Target().Arch.ArchType.String() |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1374 | |
| 1375 | s.libsMapLock.Lock() |
| 1376 | defer s.libsMapLock.Unlock() |
| 1377 | |
| 1378 | if _, ok := s.libsMap[image]; !ok { |
| 1379 | s.libsMap[image] = make(map[string][]string) |
| 1380 | } |
| 1381 | |
| 1382 | s.libsMap[image][arch] = append(s.libsMap[image][arch], name) |
| 1383 | } |
| 1384 | |
| 1385 | // Exports makefile variables in the following format: |
| 1386 | // SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES |
| 1387 | // e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES |
| 1388 | // These are to be used by use_soong_sanitized_static_libraries. |
| 1389 | // See build/make/core/binary.mk for more details. |
| 1390 | func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) { |
| 1391 | for _, image := range android.SortedStringKeys(s.libsMap) { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 1392 | archMap := s.libsMap[ImageVariantType(image)] |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1393 | for _, arch := range android.SortedStringKeys(archMap) { |
| 1394 | libs := archMap[arch] |
| 1395 | sort.Strings(libs) |
| 1396 | |
| 1397 | key := fmt.Sprintf( |
| 1398 | "SOONG_%s_%s_%s_STATIC_LIBRARIES", |
| 1399 | s.sanitizerType.variationName(), |
| 1400 | image, // already upper |
| 1401 | arch) |
| 1402 | |
| 1403 | ctx.Strict(key, strings.Join(libs, " ")) |
| 1404 | } |
| 1405 | } |
| 1406 | } |
| 1407 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1408 | var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs") |
| 1409 | |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1410 | func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1411 | return config.Once(cfiStaticLibsKey, func() interface{} { |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1412 | return newSanitizerStaticLibsMap(cfi) |
| 1413 | }).(*sanitizerStaticLibsMap) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1414 | } |
| 1415 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1416 | var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs") |
| 1417 | |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1418 | func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1419 | return config.Once(hwasanStaticLibsKey, func() interface{} { |
Tri Vo | 6eafc36 | 2021-04-01 11:29:09 -0700 | [diff] [blame] | 1420 | return newSanitizerStaticLibsMap(Hwasan) |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1421 | }).(*sanitizerStaticLibsMap) |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1422 | } |
| 1423 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1424 | func enableMinimalRuntime(sanitize *sanitize) bool { |
| 1425 | if !Bool(sanitize.Properties.Sanitize.Address) && |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1426 | !Bool(sanitize.Properties.Sanitize.Hwaddress) && |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 1427 | !Bool(sanitize.Properties.Sanitize.Fuzzer) && |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1428 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1429 | (Bool(sanitize.Properties.Sanitize.Integer_overflow) || |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1430 | len(sanitize.Properties.Sanitize.Misc_undefined) > 0 || |
| 1431 | Bool(sanitize.Properties.Sanitize.Undefined) || |
| 1432 | Bool(sanitize.Properties.Sanitize.All_undefined)) && |
| 1433 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1434 | !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) || |
| 1435 | Bool(sanitize.Properties.Sanitize.Diag.Cfi) || |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1436 | Bool(sanitize.Properties.Sanitize.Diag.Undefined) || |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1437 | len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) { |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1438 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1439 | return true |
| 1440 | } |
| 1441 | return false |
| 1442 | } |
| 1443 | |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 1444 | func enableUbsanRuntime(sanitize *sanitize) bool { |
| 1445 | return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) || |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1446 | Bool(sanitize.Properties.Sanitize.Diag.Undefined) || |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 1447 | len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 |
| 1448 | } |
| 1449 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1450 | func cfiMakeVarsProvider(ctx android.MakeVarsContext) { |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1451 | cfiStaticLibs(ctx.Config()).exportToMake(ctx) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1452 | } |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1453 | |
| 1454 | func hwasanMakeVarsProvider(ctx android.MakeVarsContext) { |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1455 | hwasanStaticLibs(ctx.Config()).exportToMake(ctx) |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1456 | } |