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