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