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