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