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