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