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