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