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" |
| 24 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 25 | "android/soong/android" |
Evgenii Stepanov | af36db1 | 2016-08-15 14:18:24 -0700 | [diff] [blame] | 26 | "android/soong/cc/config" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 29 | var ( |
| 30 | // Any C flags added by sanitizer which libTooling tools may not |
| 31 | // understand also need to be added to ClangLibToolingUnknownCflags in |
| 32 | // cc/config/clang.go |
Vishwath Mohan | f3918d3 | 2017-02-14 07:59:33 -0800 | [diff] [blame] | 33 | |
Yi Kong | 20233a4 | 2019-08-21 01:38:40 -0700 | [diff] [blame] | 34 | asanCflags = []string{ |
| 35 | "-fno-omit-frame-pointer", |
| 36 | "-fno-experimental-new-pass-manager", |
| 37 | } |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 38 | asanLdflags = []string{"-Wl,-u,__asan_preinit"} |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 39 | |
Peter Collingbourne | 967511a | 2019-03-19 21:39:54 -0700 | [diff] [blame] | 40 | hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=", |
Evgenii Stepanov | 96fa3dd | 2020-03-27 19:38:42 +0000 | [diff] [blame] | 41 | "-fsanitize-hwaddress-abi=platform", |
| 42 | "-fno-experimental-new-pass-manager", |
Evgenii Stepanov | 64bee4d | 2019-11-22 18:37:10 -0800 | [diff] [blame] | 43 | // The following improves debug location information |
| 44 | // availability at the cost of its accuracy. It increases |
| 45 | // the likelihood of a stack variable's frame offset |
| 46 | // to be recorded in the debug info, which is important |
| 47 | // for the quality of hwasan reports. The downside is a |
| 48 | // higher number of "optimized out" stack variables. |
| 49 | // b/112437883. |
| 50 | "-mllvm", "-instcombine-lower-dbg-declare=0", |
| 51 | } |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 52 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 53 | cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso", |
Pirama Arumuga Nainar | eb8d403 | 2020-07-27 11:22:35 -0700 | [diff] [blame] | 54 | "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blocklist.txt"} |
Evgenii Stepanov | dbf1d4f | 2018-08-31 12:54:33 -0700 | [diff] [blame] | 55 | // -flto and -fvisibility are required by clang when -fsanitize=cfi is |
| 56 | // used, but have no effect on assembly files |
| 57 | cfiAsflags = []string{"-flto", "-fvisibility=default"} |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 58 | cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi", |
Pirama Arumuga Nainar | bdb17f0 | 2017-08-28 21:50:17 -0700 | [diff] [blame] | 59 | "-Wl,-plugin-opt,O1"} |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 60 | cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map" |
| 61 | cfiStaticLibsMutex sync.Mutex |
| 62 | hwasanStaticLibsMutex sync.Mutex |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 63 | |
Pirama Arumuga Nainar | eb8d403 | 2020-07-27 11:22:35 -0700 | [diff] [blame] | 64 | intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blocklist.txt"} |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 65 | |
Peter Collingbourne | bd19db0 | 2019-03-06 10:38:48 -0800 | [diff] [blame] | 66 | minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined", |
Ivan Lozano | ae6ae1d | 2018-10-08 09:29:39 -0700 | [diff] [blame] | 67 | "-fno-sanitize-recover=integer,undefined"} |
Evgenii Stepanov | 2c6484e | 2019-05-15 12:49:54 -0700 | [diff] [blame] | 68 | hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512", |
| 69 | "export_memory_stats=0", "max_malloc_fill_size=0"} |
Dan Willemsen | cbceaab | 2016-10-13 16:44:07 -0700 | [diff] [blame] | 70 | ) |
| 71 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 72 | type sanitizerType int |
| 73 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 74 | func boolPtr(v bool) *bool { |
| 75 | if v { |
| 76 | return &v |
| 77 | } else { |
| 78 | return nil |
| 79 | } |
| 80 | } |
| 81 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 82 | const ( |
| 83 | asan sanitizerType = iota + 1 |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 84 | hwasan |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 85 | tsan |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 86 | intOverflow |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 87 | cfi |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 88 | scs |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 89 | fuzzer |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 90 | ) |
| 91 | |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 92 | // Name of the sanitizer variation for this sanitizer type |
| 93 | func (t sanitizerType) variationName() string { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 94 | switch t { |
| 95 | case asan: |
| 96 | return "asan" |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 97 | case hwasan: |
| 98 | return "hwasan" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 99 | case tsan: |
| 100 | return "tsan" |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 101 | case intOverflow: |
| 102 | return "intOverflow" |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 103 | case cfi: |
| 104 | return "cfi" |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 105 | case scs: |
| 106 | return "scs" |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 107 | case fuzzer: |
| 108 | return "fuzzer" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 109 | default: |
| 110 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 111 | } |
| 112 | } |
| 113 | |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 114 | // This is the sanitizer names in SANITIZE_[TARGET|HOST] |
| 115 | func (t sanitizerType) name() string { |
| 116 | switch t { |
| 117 | case asan: |
| 118 | return "address" |
| 119 | case hwasan: |
| 120 | return "hwaddress" |
| 121 | case tsan: |
| 122 | return "thread" |
| 123 | case intOverflow: |
| 124 | return "integer_overflow" |
| 125 | case cfi: |
| 126 | return "cfi" |
| 127 | case scs: |
| 128 | return "shadow-call-stack" |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 129 | case fuzzer: |
| 130 | return "fuzzer" |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 131 | default: |
| 132 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 133 | } |
| 134 | } |
| 135 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 136 | func (t sanitizerType) incompatibleWithCfi() bool { |
| 137 | return t == asan || t == fuzzer || t == hwasan |
| 138 | } |
| 139 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 140 | type SanitizeProperties struct { |
| 141 | // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer |
| 142 | Sanitize struct { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 143 | Never *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 144 | |
| 145 | // main sanitizers |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 146 | Address *bool `android:"arch_variant"` |
| 147 | Thread *bool `android:"arch_variant"` |
| 148 | Hwaddress *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 149 | |
| 150 | // local sanitizers |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 151 | Undefined *bool `android:"arch_variant"` |
| 152 | All_undefined *bool `android:"arch_variant"` |
| 153 | Misc_undefined []string `android:"arch_variant"` |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 154 | Fuzzer *bool `android:"arch_variant"` |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 155 | Safestack *bool `android:"arch_variant"` |
| 156 | Cfi *bool `android:"arch_variant"` |
| 157 | Integer_overflow *bool `android:"arch_variant"` |
Kostya Kortchinsky | d18ae5c | 2018-06-12 14:46:54 -0700 | [diff] [blame] | 158 | Scudo *bool `android:"arch_variant"` |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 159 | Scs *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 160 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 161 | // Sanitizers to run in the diagnostic mode (as opposed to the release mode). |
| 162 | // Replaces abort() on error with a human-readable error message. |
| 163 | // Address and Thread sanitizers always run in diagnostic mode. |
| 164 | Diag struct { |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 165 | Undefined *bool `android:"arch_variant"` |
| 166 | Cfi *bool `android:"arch_variant"` |
| 167 | Integer_overflow *bool `android:"arch_variant"` |
| 168 | Misc_undefined []string `android:"arch_variant"` |
Ivan Lozano | 7929bba | 2018-12-12 09:36:31 -0800 | [diff] [blame] | 169 | No_recover []string |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // value to pass to -fsanitize-recover= |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 173 | Recover []string |
| 174 | |
| 175 | // value to pass to -fsanitize-blacklist |
| 176 | Blacklist *string |
Pirama Arumuga Nainar | 6c4ccca | 2020-07-27 11:49:51 -0700 | [diff] [blame] | 177 | // value to pass to -fsanitize-blacklist |
| 178 | Blocklist *string |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 179 | } `android:"arch_variant"` |
| 180 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 181 | SanitizerEnabled bool `blueprint:"mutated"` |
| 182 | SanitizeDep bool `blueprint:"mutated"` |
| 183 | MinimalRuntimeDep bool `blueprint:"mutated"` |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 184 | BuiltinsDep bool `blueprint:"mutated"` |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 185 | UbsanRuntimeDep bool `blueprint:"mutated"` |
| 186 | InSanitizerDir bool `blueprint:"mutated"` |
| 187 | Sanitizers []string `blueprint:"mutated"` |
| 188 | DiagSanitizers []string `blueprint:"mutated"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | type sanitize struct { |
| 192 | Properties SanitizeProperties |
| 193 | } |
| 194 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 195 | func init() { |
| 196 | android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider) |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 197 | android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 200 | func (sanitize *sanitize) props() []interface{} { |
| 201 | return []interface{}{&sanitize.Properties} |
| 202 | } |
| 203 | |
| 204 | func (sanitize *sanitize) begin(ctx BaseModuleContext) { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 205 | s := &sanitize.Properties.Sanitize |
| 206 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 207 | // Don't apply sanitizers to NDK code. |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 208 | if ctx.useSdk() { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 209 | s.Never = BoolPtr(true) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Doug Horn | c32c6b0 | 2019-01-17 14:44:05 -0800 | [diff] [blame] | 212 | // Sanitizers do not work on Fuchsia yet. |
| 213 | if ctx.Fuchsia() { |
| 214 | s.Never = BoolPtr(true) |
| 215 | } |
| 216 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 217 | // Never always wins. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 218 | if Bool(s.Never) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 219 | return |
| 220 | } |
| 221 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 222 | var globalSanitizers []string |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 223 | var globalSanitizersDiag []string |
| 224 | |
Dan Willemsen | 8536d6b | 2018-10-07 20:54:34 -0700 | [diff] [blame] | 225 | if ctx.Host() { |
| 226 | if !ctx.Windows() { |
| 227 | globalSanitizers = ctx.Config().SanitizeHost() |
| 228 | } |
| 229 | } else { |
| 230 | arches := ctx.Config().SanitizeDeviceArch() |
| 231 | if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) { |
| 232 | globalSanitizers = ctx.Config().SanitizeDevice() |
| 233 | globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag() |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 237 | if len(globalSanitizers) > 0 { |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 238 | var found bool |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 239 | if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil { |
| 240 | s.All_undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 241 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 242 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 243 | if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil { |
| 244 | s.Undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 247 | if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil { |
| 248 | s.Address = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 251 | if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil { |
| 252 | s.Thread = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 255 | if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil { |
| 256 | s.Fuzzer = boolPtr(true) |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil { |
| 260 | s.Safestack = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 263 | if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 264 | if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) { |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 265 | s.Cfi = boolPtr(true) |
| 266 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 269 | // Global integer_overflow builds do not support static libraries. |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 270 | if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil { |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 271 | if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() { |
Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 272 | s.Integer_overflow = boolPtr(true) |
| 273 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Kostya Kortchinsky | d18ae5c | 2018-06-12 14:46:54 -0700 | [diff] [blame] | 276 | if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil { |
| 277 | s.Scudo = boolPtr(true) |
| 278 | } |
| 279 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 280 | if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil { |
| 281 | s.Hwaddress = boolPtr(true) |
| 282 | } |
| 283 | |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 284 | if len(globalSanitizers) > 0 { |
| 285 | ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0]) |
| 286 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 287 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 288 | // Global integer_overflow builds do not support static library diagnostics. |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 289 | if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found && |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 290 | s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() { |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 291 | s.Diag.Integer_overflow = boolPtr(true) |
| 292 | } |
| 293 | |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 294 | if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found && |
| 295 | s.Diag.Cfi == nil && Bool(s.Cfi) { |
| 296 | s.Diag.Cfi = boolPtr(true) |
| 297 | } |
| 298 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 299 | if len(globalSanitizersDiag) > 0 { |
| 300 | ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0]) |
| 301 | } |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 302 | } |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 303 | |
Vishwath Mohan | 1c54f66 | 2018-05-24 18:36:18 -0700 | [diff] [blame] | 304 | // Enable CFI for all components in the include paths (for Aarch64 only) |
| 305 | if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 { |
Vishwath Mohan | 3af8ee0 | 2018-03-30 02:55:23 +0000 | [diff] [blame] | 306 | s.Cfi = boolPtr(true) |
| 307 | if inList("cfi", ctx.Config().SanitizeDeviceDiag()) { |
| 308 | s.Diag.Cfi = boolPtr(true) |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Elliott Hughes | da3a071 | 2020-03-06 16:55:28 -0800 | [diff] [blame] | 312 | // Is CFI actually enabled? |
| 313 | if !ctx.Config().EnableCFI() { |
Vishwath Mohan | 1b017a7 | 2017-01-19 13:54:55 -0800 | [diff] [blame] | 314 | s.Cfi = nil |
| 315 | s.Diag.Cfi = nil |
| 316 | } |
| 317 | |
Vishwath Mohan | 6d67e6e | 2017-02-07 20:31:41 -0800 | [diff] [blame] | 318 | // Also disable CFI for arm32 until b/35157333 is fixed. |
| 319 | if ctx.Arch().ArchType == android.Arm { |
| 320 | s.Cfi = nil |
| 321 | s.Diag.Cfi = nil |
| 322 | } |
| 323 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 324 | // HWASan requires AArch64 hardware feature (top-byte-ignore). |
| 325 | if ctx.Arch().ArchType != android.Arm64 { |
| 326 | s.Hwaddress = nil |
| 327 | } |
| 328 | |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 329 | // SCS is only implemented on AArch64. |
Peter Collingbourne | bd19db0 | 2019-03-06 10:38:48 -0800 | [diff] [blame] | 330 | if ctx.Arch().ArchType != android.Arm64 { |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 331 | s.Scs = nil |
| 332 | } |
| 333 | |
Vishwath Mohan | 8f4fdd8 | 2017-04-20 07:42:52 -0700 | [diff] [blame] | 334 | // Also disable CFI if ASAN is enabled. |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 335 | if Bool(s.Address) || Bool(s.Hwaddress) { |
Vishwath Mohan | 8f4fdd8 | 2017-04-20 07:42:52 -0700 | [diff] [blame] | 336 | s.Cfi = nil |
| 337 | s.Diag.Cfi = nil |
| 338 | } |
| 339 | |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 340 | // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds. |
| 341 | if !ctx.Os().Linux() { |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 342 | s.Cfi = nil |
| 343 | s.Diag.Cfi = nil |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 344 | s.Misc_undefined = nil |
| 345 | s.Undefined = nil |
| 346 | s.All_undefined = nil |
| 347 | s.Integer_overflow = nil |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Vishwath Mohan | 9ccbba0 | 2018-05-28 13:54:48 -0700 | [diff] [blame] | 350 | // Also disable CFI for VNDK variants of components |
| 351 | if ctx.isVndk() && ctx.useVndk() { |
Vishwath Mohan | 7589c82 | 2018-05-23 19:29:55 -0700 | [diff] [blame] | 352 | s.Cfi = nil |
| 353 | s.Diag.Cfi = nil |
| 354 | } |
| 355 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 356 | // Also disable CFI if building against snapshot. |
| 357 | vndkVersion := ctx.DeviceConfig().VndkVersion() |
| 358 | if ctx.useVndk() && vndkVersion != "current" && vndkVersion != "" { |
| 359 | s.Cfi = nil |
| 360 | } |
| 361 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 362 | // HWASan ramdisk (which is built from recovery) goes over some bootloader limit. |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 363 | // Keep libc instrumented so that ramdisk / recovery can run hwasan-instrumented code if necessary. |
| 364 | if (ctx.inRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 365 | s.Hwaddress = nil |
| 366 | } |
| 367 | |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 368 | if ctx.staticBinary() { |
| 369 | s.Address = nil |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 370 | s.Fuzzer = nil |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 371 | s.Thread = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 374 | if Bool(s.All_undefined) { |
| 375 | s.Undefined = nil |
| 376 | } |
| 377 | |
Evgenii Stepanov | 0a8a0d0 | 2016-05-12 13:54:53 -0700 | [diff] [blame] | 378 | if !ctx.toolchain().Is64Bit() { |
| 379 | // TSAN and SafeStack are not supported on 32-bit architectures |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 380 | s.Thread = nil |
| 381 | s.Safestack = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 382 | // TODO(ccross): error for compile_multilib = "32"? |
| 383 | } |
| 384 | |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 385 | 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] | 386 | Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 || |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 387 | Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) { |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 388 | sanitize.Properties.SanitizerEnabled = true |
| 389 | } |
| 390 | |
Kostya Kortchinsky | d5275c8 | 2019-02-01 08:42:56 -0800 | [diff] [blame] | 391 | // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally. |
| 392 | 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] | 393 | s.Scudo = nil |
| 394 | } |
| 395 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 396 | if Bool(s.Hwaddress) { |
| 397 | s.Address = nil |
| 398 | s.Thread = nil |
| 399 | } |
| 400 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 401 | // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is |
| 402 | // mutually incompatible. |
| 403 | if Bool(s.Fuzzer) { |
| 404 | s.Cfi = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 409 | if !sanitize.Properties.SanitizerEnabled { // || c.static() { |
| 410 | return deps |
| 411 | } |
| 412 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 413 | return deps |
| 414 | } |
| 415 | |
Chih-Hung Hsieh | 3567e62 | 2018-11-15 14:01:36 -0800 | [diff] [blame] | 416 | func toDisableImplicitIntegerChange(flags []string) bool { |
| 417 | // Returns true if any flag is fsanitize*integer, and there is |
| 418 | // no explicit flag about sanitize=implicit-integer-sign-change. |
| 419 | for _, f := range flags { |
| 420 | if strings.Contains(f, "sanitize=implicit-integer-sign-change") { |
| 421 | return false |
| 422 | } |
| 423 | } |
| 424 | for _, f := range flags { |
| 425 | if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") { |
| 426 | return true |
| 427 | } |
| 428 | } |
| 429 | return false |
| 430 | } |
| 431 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 432 | func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags { |
Ivan Lozano | 59fdea2 | 2018-05-10 14:17:22 -0700 | [diff] [blame] | 433 | minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a" |
| 434 | minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 435 | builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a" |
| 436 | builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 437 | |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 438 | if sanitize.Properties.MinimalRuntimeDep { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 439 | flags.Local.LdFlags = append(flags.Local.LdFlags, |
| 440 | minimalRuntimePath, |
| 441 | "-Wl,--exclude-libs,"+minimalRuntimeLib) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 442 | } |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 443 | |
| 444 | if sanitize.Properties.BuiltinsDep { |
| 445 | flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...) |
| 446 | } |
| 447 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 448 | if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 449 | return flags |
| 450 | } |
| 451 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 452 | if Bool(sanitize.Properties.Sanitize.Address) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 453 | if ctx.Arch().ArchType == android.Arm { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 454 | // Frame pointer based unwinder in ASan requires ARM frame setup. |
| 455 | // TODO: put in flags? |
| 456 | flags.RequiredInstructionSet = "arm" |
| 457 | } |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 458 | flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...) |
| 459 | flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 460 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 461 | if ctx.Host() { |
| 462 | // -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 463 | // libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 464 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed") |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 465 | } else { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 466 | flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0") |
Jiyong Park | a2aca28 | 2019-02-02 13:13:38 +0900 | [diff] [blame] | 467 | if ctx.bootstrap() { |
| 468 | flags.DynamicLinker = "/system/bin/bootstrap/linker_asan" |
| 469 | } else { |
| 470 | flags.DynamicLinker = "/system/bin/linker_asan" |
| 471 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 472 | if flags.Toolchain.Is64Bit() { |
| 473 | flags.DynamicLinker += "64" |
| 474 | } |
| 475 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 478 | if Bool(sanitize.Properties.Sanitize.Hwaddress) { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 479 | flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...) |
Yabin Cui | 6be405e | 2017-10-19 15:52:11 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 482 | if Bool(sanitize.Properties.Sanitize.Fuzzer) { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 483 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link") |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 484 | |
| 485 | // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 486 | _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags) |
| 487 | _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags) |
| 488 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto") |
| 489 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto") |
Mitch Phillips | 7438475 | 2019-06-17 10:33:52 -0700 | [diff] [blame] | 490 | |
Mitch Phillips | b8e593d | 2019-10-09 17:18:59 -0700 | [diff] [blame] | 491 | // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries |
| 492 | // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus |
| 493 | // doesn't match the linker script due to the "__emutls_v." prefix). |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 494 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth") |
| 495 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth") |
Mitch Phillips | b8e593d | 2019-10-09 17:18:59 -0700 | [diff] [blame] | 496 | |
Mitch Phillips | 7438475 | 2019-06-17 10:33:52 -0700 | [diff] [blame] | 497 | // TODO(b/133876586): Experimental PM breaks sanitizer coverage. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 498 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager") |
Mitch Phillips | b9b3e79 | 2019-08-28 12:41:07 -0700 | [diff] [blame] | 499 | |
| 500 | // Disable fortify for fuzzing builds. Generally, we'll be building with |
| 501 | // UBSan or ASan here and the fortify checks pollute the stack traces. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 502 | flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE") |
Mitch Phillips | 734b4cb | 2019-12-10 08:44:52 -0800 | [diff] [blame] | 503 | |
| 504 | // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's |
| 505 | // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and |
| 506 | // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets |
| 507 | // the DT_RUNPATH from the shared library above it, and not the executable, |
| 508 | // meaning that the lookup falls back to the system. Adding the $ORIGIN to the |
| 509 | // DT_RUNPATH here means that transient shared libraries can be found |
| 510 | // colocated with their parents. |
| 511 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 514 | if Bool(sanitize.Properties.Sanitize.Cfi) { |
Evgenii Stepanov | 7ebf9fa | 2017-01-20 14:13:06 -0800 | [diff] [blame] | 515 | if ctx.Arch().ArchType == android.Arm { |
| 516 | // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up |
| 517 | // to do this on a function basis, so force Thumb on the entire module. |
| 518 | flags.RequiredInstructionSet = "thumb" |
| 519 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 520 | |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 521 | flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...) |
| 522 | flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 523 | // Only append the default visibility flag if -fvisibility has not already been set |
| 524 | // to hidden. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 525 | if !inList("-fvisibility=hidden", flags.Local.CFlags) { |
| 526 | flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default") |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 527 | } |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 528 | flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 529 | |
| 530 | if ctx.staticBinary() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 531 | _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags) |
| 532 | _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 533 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 536 | if Bool(sanitize.Properties.Sanitize.Integer_overflow) { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 537 | flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...) |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 540 | if len(sanitize.Properties.Sanitizers) > 0 { |
| 541 | sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",") |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 542 | |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 543 | flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg) |
| 544 | flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 545 | if ctx.Host() { |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 546 | // Host sanitizers only link symbols in the final executable, so |
| 547 | // there will always be undefined symbols in intermediate libraries. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 548 | _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags) |
| 549 | flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg) |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 550 | |
| 551 | // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers |
| 552 | if !ctx.toolchain().Bionic() { |
| 553 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function") |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if enableMinimalRuntime(sanitize) { |
| 558 | flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " ")) |
| 559 | flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...) |
| 560 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib) |
| 561 | if !ctx.toolchain().Bionic() { |
| 562 | flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 563 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 564 | } |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 565 | |
| 566 | if Bool(sanitize.Properties.Sanitize.Fuzzer) { |
| 567 | // When fuzzing, we wish to crash with diagnostics on any bug. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 568 | 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] | 569 | } else if ctx.Host() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 570 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all") |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 571 | } else { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 572 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort") |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 573 | } |
Chih-Hung Hsieh | 3567e62 | 2018-11-15 14:01:36 -0800 | [diff] [blame] | 574 | // 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] | 575 | if toDisableImplicitIntegerChange(flags.Local.CFlags) { |
| 576 | 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] | 577 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 580 | if len(sanitize.Properties.DiagSanitizers) > 0 { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 581 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ",")) |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 582 | } |
| 583 | // FIXME: enable RTTI if diag + (cfi or vptr) |
| 584 | |
Andreas Gampe | 9707116 | 2017-05-08 13:15:23 -0700 | [diff] [blame] | 585 | if sanitize.Properties.Sanitize.Recover != nil { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 586 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+ |
Andreas Gampe | 9707116 | 2017-05-08 13:15:23 -0700 | [diff] [blame] | 587 | strings.Join(sanitize.Properties.Sanitize.Recover, ",")) |
| 588 | } |
| 589 | |
Ivan Lozano | 7929bba | 2018-12-12 09:36:31 -0800 | [diff] [blame] | 590 | if sanitize.Properties.Sanitize.Diag.No_recover != nil { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 591 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+ |
Ivan Lozano | 7929bba | 2018-12-12 09:36:31 -0800 | [diff] [blame] | 592 | strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ",")) |
| 593 | } |
| 594 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 595 | blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 596 | if blacklist.Valid() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 597 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blacklist.String()) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 598 | flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path()) |
| 599 | } |
| 600 | |
Pirama Arumuga Nainar | 6c4ccca | 2020-07-27 11:49:51 -0700 | [diff] [blame] | 601 | blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist) |
| 602 | if blocklist.Valid() { |
| 603 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blocklist.String()) |
| 604 | flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path()) |
| 605 | } |
| 606 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 607 | return flags |
| 608 | } |
| 609 | |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 610 | func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 611 | // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing |
| 612 | // 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] | 613 | if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 614 | if Bool(sanitize.Properties.Sanitize.Cfi) { |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 615 | entries.SubName += ".cfi" |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 616 | } |
| 617 | if Bool(sanitize.Properties.Sanitize.Hwaddress) { |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 618 | entries.SubName += ".hwasan" |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 619 | } |
| 620 | if Bool(sanitize.Properties.Sanitize.Scs) { |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 621 | entries.SubName += ".scs" |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 622 | } |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 623 | } |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Vishwath Mohan | 1dd8839 | 2017-03-29 22:00:18 -0700 | [diff] [blame] | 626 | func (sanitize *sanitize) inSanitizerDir() bool { |
| 627 | return sanitize.Properties.InSanitizerDir |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 630 | func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool { |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 631 | switch t { |
| 632 | case asan: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 633 | return sanitize.Properties.Sanitize.Address |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 634 | case hwasan: |
| 635 | return sanitize.Properties.Sanitize.Hwaddress |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 636 | case tsan: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 637 | return sanitize.Properties.Sanitize.Thread |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 638 | case intOverflow: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 639 | return sanitize.Properties.Sanitize.Integer_overflow |
| 640 | case cfi: |
| 641 | return sanitize.Properties.Sanitize.Cfi |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 642 | case scs: |
| 643 | return sanitize.Properties.Sanitize.Scs |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 644 | case fuzzer: |
| 645 | return sanitize.Properties.Sanitize.Fuzzer |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 646 | default: |
| 647 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 648 | } |
| 649 | } |
| 650 | |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 651 | func (sanitize *sanitize) isUnsanitizedVariant() bool { |
| 652 | return !sanitize.isSanitizerEnabled(asan) && |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 653 | !sanitize.isSanitizerEnabled(hwasan) && |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 654 | !sanitize.isSanitizerEnabled(tsan) && |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 655 | !sanitize.isSanitizerEnabled(cfi) && |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 656 | !sanitize.isSanitizerEnabled(scs) && |
| 657 | !sanitize.isSanitizerEnabled(fuzzer) |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 658 | } |
| 659 | |
Jayant Chowdhary | b7e08ca | 2018-05-10 15:29:24 -0700 | [diff] [blame] | 660 | func (sanitize *sanitize) isVariantOnProductionDevice() bool { |
| 661 | return !sanitize.isSanitizerEnabled(asan) && |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 662 | !sanitize.isSanitizerEnabled(hwasan) && |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 663 | !sanitize.isSanitizerEnabled(tsan) && |
| 664 | !sanitize.isSanitizerEnabled(fuzzer) |
Jayant Chowdhary | b7e08ca | 2018-05-10 15:29:24 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 667 | func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) { |
| 668 | switch t { |
| 669 | case asan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 670 | sanitize.Properties.Sanitize.Address = boolPtr(b) |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 671 | case hwasan: |
| 672 | sanitize.Properties.Sanitize.Hwaddress = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 673 | case tsan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 674 | sanitize.Properties.Sanitize.Thread = boolPtr(b) |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 675 | case intOverflow: |
| 676 | sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 677 | case cfi: |
| 678 | sanitize.Properties.Sanitize.Cfi = boolPtr(b) |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 679 | case scs: |
| 680 | sanitize.Properties.Sanitize.Scs = boolPtr(b) |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 681 | case fuzzer: |
| 682 | sanitize.Properties.Sanitize.Fuzzer = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 683 | default: |
| 684 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 685 | } |
| 686 | if b { |
| 687 | sanitize.Properties.SanitizerEnabled = true |
| 688 | } |
| 689 | } |
| 690 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 691 | // Check if the sanitizer is explicitly disabled (as opposed to nil by |
| 692 | // virtue of not being set). |
| 693 | func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool { |
| 694 | if sanitize == nil { |
| 695 | return false |
| 696 | } |
| 697 | |
| 698 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 699 | return sanitizerVal != nil && *sanitizerVal == false |
| 700 | } |
| 701 | |
| 702 | // There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled) |
| 703 | // because enabling a sanitizer either directly (via the blueprint) or |
| 704 | // indirectly (via a mutator) sets the bool ptr to true, and you can't |
| 705 | // distinguish between the cases. It isn't needed though - both cases can be |
| 706 | // treated identically. |
| 707 | func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool { |
| 708 | if sanitize == nil { |
| 709 | return false |
| 710 | } |
| 711 | |
| 712 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 713 | return sanitizerVal != nil && *sanitizerVal == true |
| 714 | } |
| 715 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 716 | func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool { |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame^] | 717 | switch t := tag.(type) { |
| 718 | case dependencyTag: |
| 719 | return t == reuseObjTag || t == objDepTag |
| 720 | case libraryDependencyTag: |
| 721 | return true |
| 722 | default: |
| 723 | return false |
| 724 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 727 | // Propagate sanitizer requirements down from binaries |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 728 | func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) { |
| 729 | return func(mctx android.TopDownMutatorContext) { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 730 | if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) { |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 731 | mctx.WalkDeps(func(child, parent android.Module) bool { |
| 732 | if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) { |
| 733 | return false |
| 734 | } |
| 735 | if d, ok := child.(*Module); ok && d.sanitize != nil && |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 736 | !Bool(d.sanitize.Properties.Sanitize.Never) && |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 737 | !d.sanitize.isSanitizerExplicitlyDisabled(t) { |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 738 | if t == cfi || t == hwasan || t == scs { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 739 | if d.static() { |
| 740 | d.sanitize.Properties.SanitizeDep = true |
| 741 | } |
| 742 | } else { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 743 | d.sanitize.Properties.SanitizeDep = true |
| 744 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 745 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 746 | return true |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 747 | }) |
Jiyong Park | f97782b | 2019-02-13 20:28:58 +0900 | [diff] [blame] | 748 | } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok { |
| 749 | // If an APEX module includes a lib which is enabled for a sanitizer T, then |
| 750 | // the APEX module is also enabled for the same sanitizer type. |
| 751 | mctx.VisitDirectDeps(func(child android.Module) { |
| 752 | if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) { |
| 753 | sanitizeable.EnableSanitizer(t.name()) |
| 754 | } |
| 755 | }) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 760 | // 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] | 761 | func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) { |
| 762 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
| 763 | mctx.WalkDeps(func(child, parent android.Module) bool { |
| 764 | if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) { |
| 765 | return false |
| 766 | } |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 767 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 768 | d, ok := child.(*Module) |
| 769 | if !ok || !d.static() { |
| 770 | return false |
| 771 | } |
| 772 | if d.sanitize != nil { |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 773 | if enableMinimalRuntime(d.sanitize) { |
| 774 | // If a static dependency is built with the minimal runtime, |
| 775 | // make sure we include the ubsan minimal runtime. |
| 776 | c.sanitize.Properties.MinimalRuntimeDep = true |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 777 | } else if enableUbsanRuntime(d.sanitize) { |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 778 | // If a static dependency runs with full ubsan diagnostics, |
| 779 | // make sure we include the ubsan runtime. |
| 780 | c.sanitize.Properties.UbsanRuntimeDep = true |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 781 | } |
Colin Cross | 0b90833 | 2019-06-19 23:00:20 -0700 | [diff] [blame] | 782 | |
| 783 | if c.sanitize.Properties.MinimalRuntimeDep && |
| 784 | c.sanitize.Properties.UbsanRuntimeDep { |
| 785 | // both flags that this mutator might set are true, so don't bother recursing |
| 786 | return false |
| 787 | } |
| 788 | |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 789 | if c.Os() == android.Linux { |
| 790 | c.sanitize.Properties.BuiltinsDep = true |
| 791 | } |
| 792 | |
Colin Cross | 0b90833 | 2019-06-19 23:00:20 -0700 | [diff] [blame] | 793 | return true |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 794 | } |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 795 | |
| 796 | if p, ok := d.linker.(*vendorSnapshotLibraryDecorator); ok { |
| 797 | if Bool(p.properties.Sanitize_minimal_dep) { |
| 798 | c.sanitize.Properties.MinimalRuntimeDep = true |
| 799 | } |
| 800 | if Bool(p.properties.Sanitize_ubsan_dep) { |
| 801 | c.sanitize.Properties.UbsanRuntimeDep = true |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | return false |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 806 | }) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 810 | // Add the dependency to the runtime library for each of the sanitizer variants |
| 811 | func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 812 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
Pirama Arumuga Nainar | 6aa2102 | 2019-01-25 00:20:35 +0000 | [diff] [blame] | 813 | if !c.Enabled() { |
| 814 | return |
| 815 | } |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 816 | var sanitizers []string |
| 817 | var diagSanitizers []string |
| 818 | |
| 819 | if Bool(c.sanitize.Properties.Sanitize.All_undefined) { |
| 820 | sanitizers = append(sanitizers, "undefined") |
| 821 | } else { |
| 822 | if Bool(c.sanitize.Properties.Sanitize.Undefined) { |
| 823 | sanitizers = append(sanitizers, |
| 824 | "bool", |
| 825 | "integer-divide-by-zero", |
| 826 | "return", |
| 827 | "returns-nonnull-attribute", |
| 828 | "shift-exponent", |
| 829 | "unreachable", |
| 830 | "vla-bound", |
| 831 | // TODO(danalbert): The following checks currently have compiler performance issues. |
| 832 | //"alignment", |
| 833 | //"bounds", |
| 834 | //"enum", |
| 835 | //"float-cast-overflow", |
| 836 | //"float-divide-by-zero", |
| 837 | //"nonnull-attribute", |
| 838 | //"null", |
| 839 | //"shift-base", |
| 840 | //"signed-integer-overflow", |
| 841 | // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on. |
| 842 | // https://llvm.org/PR19302 |
| 843 | // http://reviews.llvm.org/D6974 |
| 844 | // "object-size", |
| 845 | ) |
| 846 | } |
| 847 | sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...) |
| 848 | } |
| 849 | |
| 850 | if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) { |
| 851 | diagSanitizers = append(diagSanitizers, "undefined") |
| 852 | } |
| 853 | |
| 854 | diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...) |
| 855 | |
| 856 | if Bool(c.sanitize.Properties.Sanitize.Address) { |
| 857 | sanitizers = append(sanitizers, "address") |
| 858 | diagSanitizers = append(diagSanitizers, "address") |
| 859 | } |
| 860 | |
| 861 | if Bool(c.sanitize.Properties.Sanitize.Hwaddress) { |
| 862 | sanitizers = append(sanitizers, "hwaddress") |
| 863 | } |
| 864 | |
| 865 | if Bool(c.sanitize.Properties.Sanitize.Thread) { |
| 866 | sanitizers = append(sanitizers, "thread") |
| 867 | } |
| 868 | |
| 869 | if Bool(c.sanitize.Properties.Sanitize.Safestack) { |
| 870 | sanitizers = append(sanitizers, "safe-stack") |
| 871 | } |
| 872 | |
| 873 | if Bool(c.sanitize.Properties.Sanitize.Cfi) { |
| 874 | sanitizers = append(sanitizers, "cfi") |
| 875 | |
| 876 | if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) { |
| 877 | diagSanitizers = append(diagSanitizers, "cfi") |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) { |
| 882 | sanitizers = append(sanitizers, "unsigned-integer-overflow") |
| 883 | sanitizers = append(sanitizers, "signed-integer-overflow") |
| 884 | if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) { |
| 885 | diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow") |
| 886 | diagSanitizers = append(diagSanitizers, "signed-integer-overflow") |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | if Bool(c.sanitize.Properties.Sanitize.Scudo) { |
| 891 | sanitizers = append(sanitizers, "scudo") |
| 892 | } |
| 893 | |
| 894 | if Bool(c.sanitize.Properties.Sanitize.Scs) { |
| 895 | sanitizers = append(sanitizers, "shadow-call-stack") |
| 896 | } |
| 897 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 898 | if Bool(c.sanitize.Properties.Sanitize.Fuzzer) { |
| 899 | sanitizers = append(sanitizers, "fuzzer-no-link") |
| 900 | } |
| 901 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 902 | // Save the list of sanitizers. These will be used again when generating |
| 903 | // the build rules (for Cflags, etc.) |
| 904 | c.sanitize.Properties.Sanitizers = sanitizers |
| 905 | c.sanitize.Properties.DiagSanitizers = diagSanitizers |
| 906 | |
Ivan Lozano | f3b190f | 2020-03-06 12:01:21 -0500 | [diff] [blame] | 907 | // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used. |
| 908 | if c.Host() { |
| 909 | diagSanitizers = sanitizers |
| 910 | } |
| 911 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 912 | // Determine the runtime library required |
| 913 | runtimeLibrary := "" |
Ryan Prichard | b49fe1b | 2019-10-11 15:03:34 -0700 | [diff] [blame] | 914 | var extraStaticDeps []string |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 915 | toolchain := c.toolchain(mctx) |
| 916 | if Bool(c.sanitize.Properties.Sanitize.Address) { |
| 917 | runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain) |
| 918 | } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) { |
| 919 | if c.staticBinary() { |
| 920 | runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain) |
Ryan Prichard | b49fe1b | 2019-10-11 15:03:34 -0700 | [diff] [blame] | 921 | extraStaticDeps = []string{"libdl"} |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 922 | } else { |
| 923 | runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain) |
| 924 | } |
| 925 | } else if Bool(c.sanitize.Properties.Sanitize.Thread) { |
| 926 | runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain) |
| 927 | } else if Bool(c.sanitize.Properties.Sanitize.Scudo) { |
| 928 | if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep { |
| 929 | runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain) |
| 930 | } else { |
| 931 | runtimeLibrary = config.ScudoRuntimeLibrary(toolchain) |
| 932 | } |
Mitch Phillips | b8e593d | 2019-10-09 17:18:59 -0700 | [diff] [blame] | 933 | } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep || |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 934 | Bool(c.sanitize.Properties.Sanitize.Fuzzer) || |
| 935 | Bool(c.sanitize.Properties.Sanitize.Undefined) || |
| 936 | Bool(c.sanitize.Properties.Sanitize.All_undefined) { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 937 | runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain) |
| 938 | } |
| 939 | |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 940 | if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) { |
| 941 | // UBSan is supported on non-bionic linux host builds as well |
Jooyung Han | 0302a84 | 2019-10-30 18:43:49 +0900 | [diff] [blame] | 942 | if isLlndkLibrary(runtimeLibrary, mctx.Config()) && !c.static() && c.UseVndk() { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 943 | runtimeLibrary = runtimeLibrary + llndkLibrarySuffix |
| 944 | } |
| 945 | |
| 946 | // Adding dependency to the runtime library. We are using *FarVariation* |
| 947 | // because the runtime libraries themselves are not mutated by sanitizer |
| 948 | // mutators and thus don't have sanitizer variants whereas this module |
| 949 | // has been already mutated. |
| 950 | // |
| 951 | // Note that by adding dependency with {static|shared}DepTag, the lib is |
| 952 | // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module |
| 953 | if c.staticBinary() { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 954 | deps := append(extraStaticDeps, runtimeLibrary) |
| 955 | // If we're using snapshots and in vendor, redirect to snapshot whenever possible |
| 956 | if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() { |
| 957 | snapshots := vendorSnapshotStaticLibs(mctx.Config()) |
| 958 | for idx, dep := range deps { |
| 959 | if lib, ok := snapshots.get(dep, mctx.Arch().ArchType); ok { |
| 960 | deps[idx] = lib |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 965 | // static executable gets static runtime libs |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame^] | 966 | depTag := libraryDependencyTag{Kind: staticLibraryDependency} |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 967 | mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{ |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 968 | {Mutator: "link", Variation: "static"}, |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 969 | c.ImageVariation(), |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame^] | 970 | }...), depTag, deps...) |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 971 | } else if !c.static() && !c.header() { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 972 | // If we're using snapshots and in vendor, redirect to snapshot whenever possible |
| 973 | if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() { |
| 974 | snapshots := vendorSnapshotSharedLibs(mctx.Config()) |
| 975 | if lib, ok := snapshots.get(runtimeLibrary, mctx.Arch().ArchType); ok { |
| 976 | runtimeLibrary = lib |
| 977 | } |
| 978 | } |
| 979 | |
Jiyong Park | 3b1746a | 2019-01-29 11:15:04 +0900 | [diff] [blame] | 980 | // dynamic executable and shared libs get shared runtime libs |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame^] | 981 | depTag := libraryDependencyTag{Kind: sharedLibraryDependency, Order: earlyLibraryDependency} |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 982 | mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{ |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 983 | {Mutator: "link", Variation: "shared"}, |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 984 | c.ImageVariation(), |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame^] | 985 | }...), depTag, runtimeLibrary) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 986 | } |
| 987 | // static lib does not have dependency to the runtime library. The |
| 988 | // dependency will be added to the executables or shared libs using |
| 989 | // the static lib. |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | type Sanitizeable interface { |
| 995 | android.Module |
Jiyong Park | 388ef3f | 2019-01-28 19:47:32 +0900 | [diff] [blame] | 996 | IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool |
Jiyong Park | f97782b | 2019-02-13 20:28:58 +0900 | [diff] [blame] | 997 | EnableSanitizer(sanitizerName string) |
Jooyung Han | 8ce8db9 | 2020-05-15 19:05:05 +0900 | [diff] [blame] | 998 | AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 999 | } |
| 1000 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1001 | // Create sanitized variants for modules that need them |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 1002 | func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) { |
| 1003 | return func(mctx android.BottomUpMutatorContext) { |
Vishwath Mohan | e615345 | 2017-08-11 00:52:44 +0000 | [diff] [blame] | 1004 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1005 | if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) { |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 1006 | modules := mctx.CreateVariations(t.variationName()) |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 1007 | modules[0].(*Module).sanitize.SetSanitizer(t, true) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1008 | } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1009 | isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t) |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1010 | if c.static() || c.header() || t == asan || t == fuzzer { |
| 1011 | // Static and header libs are split into non-sanitized and sanitized variants. |
| 1012 | // Shared libs are not split. However, for asan and fuzzer, we split even for shared |
| 1013 | // libs because a library sanitized for asan/fuzzer can't be linked from a library |
| 1014 | // that isn't sanitized for asan/fuzzer. |
| 1015 | // |
| 1016 | // Note for defaultVariation: since we don't split for shared libs but for static/header |
| 1017 | // libs, it is possible for the sanitized variant of a static/header lib to depend |
| 1018 | // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an |
| 1019 | // error when the module is split. defaultVariation is the name of the variation that |
| 1020 | // will be used when such a dangling dependency occurs during the split of the current |
| 1021 | // module. By setting it to the name of the sanitized variation, the dangling dependency |
| 1022 | // is redirected to the sanitized variant of the dependent module. |
| 1023 | defaultVariation := t.variationName() |
| 1024 | mctx.SetDefaultDependencyVariation(&defaultVariation) |
| 1025 | modules := mctx.CreateVariations("", t.variationName()) |
| 1026 | modules[0].(*Module).sanitize.SetSanitizer(t, false) |
| 1027 | modules[1].(*Module).sanitize.SetSanitizer(t, true) |
| 1028 | modules[0].(*Module).sanitize.Properties.SanitizeDep = false |
| 1029 | modules[1].(*Module).sanitize.Properties.SanitizeDep = false |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1030 | |
Ivan Lozano | 4774a81 | 2020-03-10 16:23:57 -0400 | [diff] [blame] | 1031 | if mctx.Device() && t.incompatibleWithCfi() { |
| 1032 | // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that |
| 1033 | // are incompatible with cfi |
| 1034 | modules[1].(*Module).sanitize.SetSanitizer(cfi, false) |
| 1035 | } |
| 1036 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1037 | // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants |
| 1038 | // to Make, because the sanitized version has a different suffix in name. |
| 1039 | // For other types of sanitizers, suppress the variation that is disabled. |
| 1040 | if t != cfi && t != scs && t != hwasan { |
| 1041 | if isSanitizerEnabled { |
| 1042 | modules[0].(*Module).Properties.PreventInstall = true |
| 1043 | modules[0].(*Module).Properties.HideFromMake = true |
| 1044 | } else { |
| 1045 | modules[1].(*Module).Properties.PreventInstall = true |
| 1046 | modules[1].(*Module).Properties.HideFromMake = true |
| 1047 | } |
| 1048 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1049 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1050 | // Export the static lib name to make |
Dan Willemsen | b5b2aba | 2020-05-03 21:28:32 -0700 | [diff] [blame] | 1051 | if c.static() && c.ExportedToMake() { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1052 | if t == cfi { |
| 1053 | appendStringSync(c.Name(), cfiStaticLibs(mctx.Config()), &cfiStaticLibsMutex) |
| 1054 | } else if t == hwasan { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 1055 | if c.UseVndk() { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1056 | appendStringSync(c.Name(), hwasanVendorStaticLibs(mctx.Config()), |
| 1057 | &hwasanStaticLibsMutex) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1058 | } else { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1059 | appendStringSync(c.Name(), hwasanStaticLibs(mctx.Config()), |
| 1060 | &hwasanStaticLibsMutex) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1061 | } |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | } else { |
| 1065 | // Shared libs are not split. Only the sanitized variant is created. |
| 1066 | modules := mctx.CreateVariations(t.variationName()) |
| 1067 | modules[0].(*Module).sanitize.SetSanitizer(t, true) |
| 1068 | modules[0].(*Module).sanitize.Properties.SanitizeDep = false |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1069 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1070 | // locate the asan libraries under /data/asan |
| 1071 | if mctx.Device() && t == asan && isSanitizerEnabled { |
| 1072 | modules[0].(*Module).sanitize.Properties.InSanitizerDir = true |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1073 | } |
Ivan Lozano | 4774a81 | 2020-03-10 16:23:57 -0400 | [diff] [blame] | 1074 | |
| 1075 | if mctx.Device() && t.incompatibleWithCfi() { |
| 1076 | // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that |
| 1077 | // are incompatible with cfi |
| 1078 | modules[0].(*Module).sanitize.SetSanitizer(cfi, false) |
| 1079 | } |
Vishwath Mohan | e21fe42 | 2017-11-01 19:42:45 -0700 | [diff] [blame] | 1080 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 1081 | } |
| 1082 | c.sanitize.Properties.SanitizeDep = false |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 1083 | } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1084 | // APEX modules fall here |
Jooyung Han | 8ce8db9 | 2020-05-15 19:05:05 +0900 | [diff] [blame] | 1085 | sanitizeable.AddSanitizerDependencies(mctx, t.name()) |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 1086 | mctx.CreateVariations(t.variationName()) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | } |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1090 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1091 | var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs") |
| 1092 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1093 | func cfiStaticLibs(config android.Config) *[]string { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1094 | return config.Once(cfiStaticLibsKey, func() interface{} { |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1095 | return &[]string{} |
| 1096 | }).(*[]string) |
| 1097 | } |
| 1098 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1099 | var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs") |
| 1100 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1101 | func hwasanStaticLibs(config android.Config) *[]string { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1102 | return config.Once(hwasanStaticLibsKey, func() interface{} { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1103 | return &[]string{} |
| 1104 | }).(*[]string) |
| 1105 | } |
| 1106 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1107 | var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs") |
| 1108 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1109 | func hwasanVendorStaticLibs(config android.Config) *[]string { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1110 | return config.Once(hwasanVendorStaticLibsKey, func() interface{} { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1111 | return &[]string{} |
| 1112 | }).(*[]string) |
| 1113 | } |
| 1114 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1115 | func appendStringSync(item string, list *[]string, mutex *sync.Mutex) { |
| 1116 | mutex.Lock() |
| 1117 | *list = append(*list, item) |
| 1118 | mutex.Unlock() |
| 1119 | } |
| 1120 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1121 | func enableMinimalRuntime(sanitize *sanitize) bool { |
| 1122 | if !Bool(sanitize.Properties.Sanitize.Address) && |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1123 | !Bool(sanitize.Properties.Sanitize.Hwaddress) && |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 1124 | !Bool(sanitize.Properties.Sanitize.Fuzzer) && |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1125 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1126 | (Bool(sanitize.Properties.Sanitize.Integer_overflow) || |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1127 | len(sanitize.Properties.Sanitize.Misc_undefined) > 0 || |
| 1128 | Bool(sanitize.Properties.Sanitize.Undefined) || |
| 1129 | Bool(sanitize.Properties.Sanitize.All_undefined)) && |
| 1130 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1131 | !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) || |
| 1132 | Bool(sanitize.Properties.Sanitize.Diag.Cfi) || |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1133 | Bool(sanitize.Properties.Sanitize.Diag.Undefined) || |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1134 | len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) { |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1135 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1136 | return true |
| 1137 | } |
| 1138 | return false |
| 1139 | } |
| 1140 | |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 1141 | func enableUbsanRuntime(sanitize *sanitize) bool { |
| 1142 | return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) || |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1143 | Bool(sanitize.Properties.Sanitize.Diag.Undefined) || |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 1144 | len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 |
| 1145 | } |
| 1146 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1147 | func cfiMakeVarsProvider(ctx android.MakeVarsContext) { |
| 1148 | cfiStaticLibs := cfiStaticLibs(ctx.Config()) |
Jeff Gaston | 7276539 | 2017-11-28 16:37:53 -0800 | [diff] [blame] | 1149 | sort.Strings(*cfiStaticLibs) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1150 | ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " ")) |
| 1151 | } |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1152 | |
| 1153 | func hwasanMakeVarsProvider(ctx android.MakeVarsContext) { |
| 1154 | hwasanStaticLibs := hwasanStaticLibs(ctx.Config()) |
| 1155 | sort.Strings(*hwasanStaticLibs) |
| 1156 | ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " ")) |
| 1157 | |
| 1158 | hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config()) |
| 1159 | sort.Strings(*hwasanVendorStaticLibs) |
| 1160 | ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " ")) |
| 1161 | } |