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