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 | |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 34 | asanCflags = []string{"-fno-omit-frame-pointer"} |
| 35 | asanLdflags = []string{"-Wl,-u,__asan_preinit"} |
| 36 | asanLibs = []string{"libasan"} |
| 37 | |
Peter Collingbourne | 967511a | 2019-03-19 21:39:54 -0700 | [diff] [blame] | 38 | // TODO(pcc): Stop passing -hwasan-allow-ifunc here once it has been made |
| 39 | // the default. |
| 40 | hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=", |
| 41 | "-mllvm", "-hwasan-create-frame-descriptions=0", |
Peter Collingbourne | e726ba5 | 2019-03-21 16:21:44 -0700 | [diff] [blame] | 42 | "-mllvm", "-hwasan-allow-ifunc", |
| 43 | "-fsanitize-hwaddress-abi=platform"} |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 44 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 45 | cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso", |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 46 | "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"} |
Evgenii Stepanov | dbf1d4f | 2018-08-31 12:54:33 -0700 | [diff] [blame] | 47 | // -flto and -fvisibility are required by clang when -fsanitize=cfi is |
| 48 | // used, but have no effect on assembly files |
| 49 | cfiAsflags = []string{"-flto", "-fvisibility=default"} |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 50 | cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi", |
Pirama Arumuga Nainar | bdb17f0 | 2017-08-28 21:50:17 -0700 | [diff] [blame] | 51 | "-Wl,-plugin-opt,O1"} |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 52 | cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map" |
| 53 | cfiStaticLibsMutex sync.Mutex |
| 54 | hwasanStaticLibsMutex sync.Mutex |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 55 | |
Evgenii Stepanov | 98f5b06 | 2018-11-29 15:12:51 -0800 | [diff] [blame] | 56 | intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"} |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 57 | |
Peter Collingbourne | bd19db0 | 2019-03-06 10:38:48 -0800 | [diff] [blame] | 58 | minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined", |
Ivan Lozano | ae6ae1d | 2018-10-08 09:29:39 -0700 | [diff] [blame] | 59 | "-fno-sanitize-recover=integer,undefined"} |
Evgenii Stepanov | 3c5a52a | 2018-12-18 17:02:44 -0800 | [diff] [blame] | 60 | hwasanGlobalOptions = []string{"heap_history_size=1023,stack_history_size=512"} |
Dan Willemsen | cbceaab | 2016-10-13 16:44:07 -0700 | [diff] [blame] | 61 | ) |
| 62 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 63 | type sanitizerType int |
| 64 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 65 | func boolPtr(v bool) *bool { |
| 66 | if v { |
| 67 | return &v |
| 68 | } else { |
| 69 | return nil |
| 70 | } |
| 71 | } |
| 72 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 73 | const ( |
| 74 | asan sanitizerType = iota + 1 |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 75 | hwasan |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 76 | tsan |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 77 | intOverflow |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 78 | cfi |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 79 | scs |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 80 | ) |
| 81 | |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 82 | // Name of the sanitizer variation for this sanitizer type |
| 83 | func (t sanitizerType) variationName() string { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 84 | switch t { |
| 85 | case asan: |
| 86 | return "asan" |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 87 | case hwasan: |
| 88 | return "hwasan" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 89 | case tsan: |
| 90 | return "tsan" |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 91 | case intOverflow: |
| 92 | return "intOverflow" |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 93 | case cfi: |
| 94 | return "cfi" |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 95 | case scs: |
| 96 | return "scs" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 97 | default: |
| 98 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 99 | } |
| 100 | } |
| 101 | |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 102 | // This is the sanitizer names in SANITIZE_[TARGET|HOST] |
| 103 | func (t sanitizerType) name() string { |
| 104 | switch t { |
| 105 | case asan: |
| 106 | return "address" |
| 107 | case hwasan: |
| 108 | return "hwaddress" |
| 109 | case tsan: |
| 110 | return "thread" |
| 111 | case intOverflow: |
| 112 | return "integer_overflow" |
| 113 | case cfi: |
| 114 | return "cfi" |
| 115 | case scs: |
| 116 | return "shadow-call-stack" |
| 117 | default: |
| 118 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 119 | } |
| 120 | } |
| 121 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 122 | type SanitizeProperties struct { |
| 123 | // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer |
| 124 | Sanitize struct { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 125 | Never *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 126 | |
| 127 | // main sanitizers |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 128 | Address *bool `android:"arch_variant"` |
| 129 | Thread *bool `android:"arch_variant"` |
| 130 | Hwaddress *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 131 | |
| 132 | // local sanitizers |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 133 | Undefined *bool `android:"arch_variant"` |
| 134 | All_undefined *bool `android:"arch_variant"` |
| 135 | Misc_undefined []string `android:"arch_variant"` |
| 136 | Coverage *bool `android:"arch_variant"` |
| 137 | Safestack *bool `android:"arch_variant"` |
| 138 | Cfi *bool `android:"arch_variant"` |
| 139 | Integer_overflow *bool `android:"arch_variant"` |
Kostya Kortchinsky | d18ae5c | 2018-06-12 14:46:54 -0700 | [diff] [blame] | 140 | Scudo *bool `android:"arch_variant"` |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 141 | Scs *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 142 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 143 | // Sanitizers to run in the diagnostic mode (as opposed to the release mode). |
| 144 | // Replaces abort() on error with a human-readable error message. |
| 145 | // Address and Thread sanitizers always run in diagnostic mode. |
| 146 | Diag struct { |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 147 | Undefined *bool `android:"arch_variant"` |
| 148 | Cfi *bool `android:"arch_variant"` |
| 149 | Integer_overflow *bool `android:"arch_variant"` |
| 150 | Misc_undefined []string `android:"arch_variant"` |
Ivan Lozano | 7929bba | 2018-12-12 09:36:31 -0800 | [diff] [blame] | 151 | No_recover []string |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // value to pass to -fsanitize-recover= |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 155 | Recover []string |
| 156 | |
| 157 | // value to pass to -fsanitize-blacklist |
| 158 | Blacklist *string |
| 159 | } `android:"arch_variant"` |
| 160 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 161 | SanitizerEnabled bool `blueprint:"mutated"` |
| 162 | SanitizeDep bool `blueprint:"mutated"` |
| 163 | MinimalRuntimeDep bool `blueprint:"mutated"` |
| 164 | UbsanRuntimeDep bool `blueprint:"mutated"` |
| 165 | InSanitizerDir bool `blueprint:"mutated"` |
| 166 | Sanitizers []string `blueprint:"mutated"` |
| 167 | DiagSanitizers []string `blueprint:"mutated"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | type sanitize struct { |
| 171 | Properties SanitizeProperties |
| 172 | } |
| 173 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 174 | func init() { |
| 175 | android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider) |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 176 | android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 179 | func (sanitize *sanitize) props() []interface{} { |
| 180 | return []interface{}{&sanitize.Properties} |
| 181 | } |
| 182 | |
| 183 | func (sanitize *sanitize) begin(ctx BaseModuleContext) { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 184 | s := &sanitize.Properties.Sanitize |
| 185 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 186 | // Don't apply sanitizers to NDK code. |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 187 | if ctx.useSdk() { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 188 | s.Never = BoolPtr(true) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Doug Horn | c32c6b0 | 2019-01-17 14:44:05 -0800 | [diff] [blame] | 191 | // Sanitizers do not work on Fuchsia yet. |
| 192 | if ctx.Fuchsia() { |
| 193 | s.Never = BoolPtr(true) |
| 194 | } |
| 195 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 196 | // Never always wins. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 197 | if Bool(s.Never) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 198 | return |
| 199 | } |
| 200 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 201 | var globalSanitizers []string |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 202 | var globalSanitizersDiag []string |
| 203 | |
Dan Willemsen | 8536d6b | 2018-10-07 20:54:34 -0700 | [diff] [blame] | 204 | if ctx.Host() { |
| 205 | if !ctx.Windows() { |
| 206 | globalSanitizers = ctx.Config().SanitizeHost() |
| 207 | } |
| 208 | } else { |
| 209 | arches := ctx.Config().SanitizeDeviceArch() |
| 210 | if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) { |
| 211 | globalSanitizers = ctx.Config().SanitizeDevice() |
| 212 | globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag() |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 216 | if len(globalSanitizers) > 0 { |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 217 | var found bool |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 218 | if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil { |
| 219 | s.All_undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 220 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 221 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 222 | if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil { |
| 223 | s.Undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Evgenii Stepanov | 774cb81 | 2016-12-28 15:52:54 -0800 | [diff] [blame] | 226 | if found, globalSanitizers = removeFromList("address", globalSanitizers); found { |
| 227 | if s.Address == nil { |
| 228 | s.Address = boolPtr(true) |
| 229 | } else if *s.Address == false { |
| 230 | // Coverage w/o address is an error. If globalSanitizers includes both, and the module |
| 231 | // disables address, then disable coverage as well. |
| 232 | _, globalSanitizers = removeFromList("coverage", globalSanitizers) |
| 233 | } |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 236 | if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil { |
| 237 | s.Thread = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 240 | if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil { |
| 241 | s.Coverage = boolPtr(true) |
| 242 | } |
| 243 | |
| 244 | if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil { |
| 245 | s.Safestack = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 248 | if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 249 | if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) { |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 250 | s.Cfi = boolPtr(true) |
| 251 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 254 | // Global integer_overflow builds do not support static libraries. |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 255 | if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil { |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 256 | if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() { |
Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 257 | s.Integer_overflow = boolPtr(true) |
| 258 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Kostya Kortchinsky | d18ae5c | 2018-06-12 14:46:54 -0700 | [diff] [blame] | 261 | if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil { |
| 262 | s.Scudo = boolPtr(true) |
| 263 | } |
| 264 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 265 | if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil { |
| 266 | s.Hwaddress = boolPtr(true) |
| 267 | } |
| 268 | |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 269 | if len(globalSanitizers) > 0 { |
| 270 | ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0]) |
| 271 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 272 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 273 | // Global integer_overflow builds do not support static library diagnostics. |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 274 | if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found && |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 275 | s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() { |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 276 | s.Diag.Integer_overflow = boolPtr(true) |
| 277 | } |
| 278 | |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 279 | if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found && |
| 280 | s.Diag.Cfi == nil && Bool(s.Cfi) { |
| 281 | s.Diag.Cfi = boolPtr(true) |
| 282 | } |
| 283 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 284 | if len(globalSanitizersDiag) > 0 { |
| 285 | ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0]) |
| 286 | } |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 287 | } |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 288 | |
Vishwath Mohan | 1c54f66 | 2018-05-24 18:36:18 -0700 | [diff] [blame] | 289 | // Enable CFI for all components in the include paths (for Aarch64 only) |
| 290 | 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] | 291 | s.Cfi = boolPtr(true) |
| 292 | if inList("cfi", ctx.Config().SanitizeDeviceDiag()) { |
| 293 | s.Diag.Cfi = boolPtr(true) |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Evgenii Stepanov | a83fdac | 2017-01-31 18:37:30 -0800 | [diff] [blame] | 297 | // CFI needs gold linker, and mips toolchain does not have one. |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 298 | if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 { |
Vishwath Mohan | 1b017a7 | 2017-01-19 13:54:55 -0800 | [diff] [blame] | 299 | s.Cfi = nil |
| 300 | s.Diag.Cfi = nil |
| 301 | } |
| 302 | |
Vishwath Mohan | 6d67e6e | 2017-02-07 20:31:41 -0800 | [diff] [blame] | 303 | // Also disable CFI for arm32 until b/35157333 is fixed. |
| 304 | if ctx.Arch().ArchType == android.Arm { |
| 305 | s.Cfi = nil |
| 306 | s.Diag.Cfi = nil |
| 307 | } |
| 308 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 309 | // HWASan requires AArch64 hardware feature (top-byte-ignore). |
| 310 | if ctx.Arch().ArchType != android.Arm64 { |
| 311 | s.Hwaddress = nil |
| 312 | } |
| 313 | |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 314 | // SCS is only implemented on AArch64. |
Peter Collingbourne | bd19db0 | 2019-03-06 10:38:48 -0800 | [diff] [blame] | 315 | if ctx.Arch().ArchType != android.Arm64 { |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 316 | s.Scs = nil |
| 317 | } |
| 318 | |
Vishwath Mohan | 8f4fdd8 | 2017-04-20 07:42:52 -0700 | [diff] [blame] | 319 | // Also disable CFI if ASAN is enabled. |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 320 | if Bool(s.Address) || Bool(s.Hwaddress) { |
Vishwath Mohan | 8f4fdd8 | 2017-04-20 07:42:52 -0700 | [diff] [blame] | 321 | s.Cfi = nil |
| 322 | s.Diag.Cfi = nil |
| 323 | } |
| 324 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 325 | // Disable sanitizers that depend on the UBSan runtime for host builds. |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 326 | if ctx.Host() { |
| 327 | s.Cfi = nil |
| 328 | s.Diag.Cfi = nil |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 329 | s.Misc_undefined = nil |
| 330 | s.Undefined = nil |
| 331 | s.All_undefined = nil |
| 332 | s.Integer_overflow = nil |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Vishwath Mohan | 9ccbba0 | 2018-05-28 13:54:48 -0700 | [diff] [blame] | 335 | // Also disable CFI for VNDK variants of components |
| 336 | if ctx.isVndk() && ctx.useVndk() { |
Vishwath Mohan | 7589c82 | 2018-05-23 19:29:55 -0700 | [diff] [blame] | 337 | s.Cfi = nil |
| 338 | s.Diag.Cfi = nil |
| 339 | } |
| 340 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 341 | // HWASan ramdisk (which is built from recovery) goes over some bootloader limit. |
Evgenii Stepanov | 1e79844 | 2018-11-15 17:34:18 -0800 | [diff] [blame] | 342 | // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary. |
| 343 | if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 344 | s.Hwaddress = nil |
| 345 | } |
| 346 | |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 347 | if ctx.staticBinary() { |
| 348 | s.Address = nil |
Colin Cross | 91169fe | 2016-08-11 15:54:20 -0700 | [diff] [blame] | 349 | s.Coverage = nil |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 350 | s.Thread = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 353 | if Bool(s.All_undefined) { |
| 354 | s.Undefined = nil |
| 355 | } |
| 356 | |
Evgenii Stepanov | 0a8a0d0 | 2016-05-12 13:54:53 -0700 | [diff] [blame] | 357 | if !ctx.toolchain().Is64Bit() { |
| 358 | // TSAN and SafeStack are not supported on 32-bit architectures |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 359 | s.Thread = nil |
| 360 | s.Safestack = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 361 | // TODO(ccross): error for compile_multilib = "32"? |
| 362 | } |
| 363 | |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 364 | if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) || |
Kostya Kortchinsky | d18ae5c | 2018-06-12 14:46:54 -0700 | [diff] [blame] | 365 | Bool(s.Coverage) || 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] | 366 | Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) { |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 367 | sanitize.Properties.SanitizerEnabled = true |
| 368 | } |
| 369 | |
Kostya Kortchinsky | d5275c8 | 2019-02-01 08:42:56 -0800 | [diff] [blame] | 370 | // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally. |
| 371 | 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] | 372 | s.Scudo = nil |
| 373 | } |
| 374 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 375 | if Bool(s.Hwaddress) { |
| 376 | s.Address = nil |
| 377 | s.Thread = nil |
| 378 | } |
| 379 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 380 | if Bool(s.Coverage) { |
| 381 | if !Bool(s.Address) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 382 | ctx.ModuleErrorf(`Use of "coverage" also requires "address"`) |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 388 | if !sanitize.Properties.SanitizerEnabled { // || c.static() { |
| 389 | return deps |
| 390 | } |
| 391 | |
| 392 | if ctx.Device() { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 393 | if Bool(sanitize.Properties.Sanitize.Address) { |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 394 | deps.StaticLibs = append(deps.StaticLibs, asanLibs...) |
Christopher Ferris | 753d4a6 | 2019-05-09 13:27:02 -0700 | [diff] [blame^] | 395 | // Compiling asan and having libc_scudo in the same |
| 396 | // executable will cause the executable to crash. |
| 397 | // Remove libc_scudo since it is only used to override |
| 398 | // allocation functions which asan already overrides. |
| 399 | _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| 403 | return deps |
| 404 | } |
| 405 | |
Chih-Hung Hsieh | 3567e62 | 2018-11-15 14:01:36 -0800 | [diff] [blame] | 406 | func toDisableImplicitIntegerChange(flags []string) bool { |
| 407 | // Returns true if any flag is fsanitize*integer, and there is |
| 408 | // no explicit flag about sanitize=implicit-integer-sign-change. |
| 409 | for _, f := range flags { |
| 410 | if strings.Contains(f, "sanitize=implicit-integer-sign-change") { |
| 411 | return false |
| 412 | } |
| 413 | } |
| 414 | for _, f := range flags { |
| 415 | if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") { |
| 416 | return true |
| 417 | } |
| 418 | } |
| 419 | return false |
| 420 | } |
| 421 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 422 | func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags { |
Ivan Lozano | 59fdea2 | 2018-05-10 14:17:22 -0700 | [diff] [blame] | 423 | minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a" |
| 424 | minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 425 | |
| 426 | if ctx.Device() && sanitize.Properties.MinimalRuntimeDep { |
| 427 | flags.LdFlags = append(flags.LdFlags, minimalRuntimePath) |
Ivan Lozano | 59fdea2 | 2018-05-10 14:17:22 -0700 | [diff] [blame] | 428 | flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 429 | } |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 430 | if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 431 | return flags |
| 432 | } |
| 433 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 434 | if Bool(sanitize.Properties.Sanitize.Address) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 435 | if ctx.Arch().ArchType == android.Arm { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 436 | // Frame pointer based unwinder in ASan requires ARM frame setup. |
| 437 | // TODO: put in flags? |
| 438 | flags.RequiredInstructionSet = "arm" |
| 439 | } |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 440 | flags.CFlags = append(flags.CFlags, asanCflags...) |
| 441 | flags.LdFlags = append(flags.LdFlags, asanLdflags...) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 442 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 443 | if ctx.Host() { |
| 444 | // -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 445 | // libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 446 | flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed") |
| 447 | } else { |
| 448 | flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0") |
Jiyong Park | a2aca28 | 2019-02-02 13:13:38 +0900 | [diff] [blame] | 449 | if ctx.bootstrap() { |
| 450 | flags.DynamicLinker = "/system/bin/bootstrap/linker_asan" |
| 451 | } else { |
| 452 | flags.DynamicLinker = "/system/bin/linker_asan" |
| 453 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 454 | if flags.Toolchain.Is64Bit() { |
| 455 | flags.DynamicLinker += "64" |
| 456 | } |
| 457 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 460 | if Bool(sanitize.Properties.Sanitize.Hwaddress) { |
| 461 | flags.CFlags = append(flags.CFlags, hwasanCflags...) |
Yabin Cui | 6be405e | 2017-10-19 15:52:11 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 464 | if Bool(sanitize.Properties.Sanitize.Coverage) { |
Zach Riggle | 06bbd89 | 2017-08-21 17:12:32 -0400 | [diff] [blame] | 465 | flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp") |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 468 | if Bool(sanitize.Properties.Sanitize.Cfi) { |
Evgenii Stepanov | 7ebf9fa | 2017-01-20 14:13:06 -0800 | [diff] [blame] | 469 | if ctx.Arch().ArchType == android.Arm { |
| 470 | // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up |
| 471 | // to do this on a function basis, so force Thumb on the entire module. |
| 472 | flags.RequiredInstructionSet = "thumb" |
| 473 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 474 | |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 475 | flags.CFlags = append(flags.CFlags, cfiCflags...) |
Evgenii Stepanov | dbf1d4f | 2018-08-31 12:54:33 -0700 | [diff] [blame] | 476 | flags.AsFlags = append(flags.AsFlags, cfiAsflags...) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 477 | // Only append the default visibility flag if -fvisibility has not already been set |
| 478 | // to hidden. |
| 479 | if !inList("-fvisibility=hidden", flags.CFlags) { |
| 480 | flags.CFlags = append(flags.CFlags, "-fvisibility=default") |
| 481 | } |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 482 | flags.LdFlags = append(flags.LdFlags, cfiLdflags...) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 483 | |
| 484 | if ctx.staticBinary() { |
| 485 | _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags) |
| 486 | _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags) |
| 487 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 490 | if Bool(sanitize.Properties.Sanitize.Integer_overflow) { |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 491 | flags.CFlags = append(flags.CFlags, intOverflowCflags...) |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 494 | if len(sanitize.Properties.Sanitizers) > 0 { |
| 495 | sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",") |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 496 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 497 | flags.CFlags = append(flags.CFlags, sanitizeArg) |
Evgenii Stepanov | dbf1d4f | 2018-08-31 12:54:33 -0700 | [diff] [blame] | 498 | flags.AsFlags = append(flags.AsFlags, sanitizeArg) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 499 | if ctx.Host() { |
| 500 | flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all") |
| 501 | flags.LdFlags = append(flags.LdFlags, sanitizeArg) |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 502 | // Host sanitizers only link symbols in the final executable, so |
| 503 | // there will always be undefined symbols in intermediate libraries. |
| 504 | _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 505 | } else { |
Colin Cross | 263abbd | 2016-07-15 13:10:48 -0700 | [diff] [blame] | 506 | flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort") |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 507 | |
| 508 | if enableMinimalRuntime(sanitize) { |
| 509 | flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " ")) |
| 510 | flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...) |
Ivan Lozano | 59fdea2 | 2018-05-10 14:17:22 -0700 | [diff] [blame] | 511 | flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 512 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 513 | } |
Chih-Hung Hsieh | 3567e62 | 2018-11-15 14:01:36 -0800 | [diff] [blame] | 514 | // http://b/119329758, Android core does not boot up with this sanitizer yet. |
| 515 | if toDisableImplicitIntegerChange(flags.CFlags) { |
| 516 | flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change") |
| 517 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 520 | if len(sanitize.Properties.DiagSanitizers) > 0 { |
| 521 | flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ",")) |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 522 | } |
| 523 | // FIXME: enable RTTI if diag + (cfi or vptr) |
| 524 | |
Andreas Gampe | 9707116 | 2017-05-08 13:15:23 -0700 | [diff] [blame] | 525 | if sanitize.Properties.Sanitize.Recover != nil { |
| 526 | flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+ |
| 527 | strings.Join(sanitize.Properties.Sanitize.Recover, ",")) |
| 528 | } |
| 529 | |
Ivan Lozano | 7929bba | 2018-12-12 09:36:31 -0800 | [diff] [blame] | 530 | if sanitize.Properties.Sanitize.Diag.No_recover != nil { |
| 531 | flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+ |
| 532 | strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ",")) |
| 533 | } |
| 534 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 535 | blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 536 | if blacklist.Valid() { |
| 537 | flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String()) |
| 538 | flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path()) |
| 539 | } |
| 540 | |
| 541 | return flags |
| 542 | } |
| 543 | |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 544 | func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 545 | // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a |
| 546 | // name conflict. |
| 547 | if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) { |
| 548 | ret.SubName += ".cfi" |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 549 | } |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 550 | if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Hwaddress) { |
| 551 | ret.SubName += ".hwasan" |
| 552 | } |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 553 | if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Scs) { |
| 554 | ret.SubName += ".scs" |
| 555 | } |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Vishwath Mohan | 1dd8839 | 2017-03-29 22:00:18 -0700 | [diff] [blame] | 558 | func (sanitize *sanitize) inSanitizerDir() bool { |
| 559 | return sanitize.Properties.InSanitizerDir |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 562 | func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool { |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 563 | switch t { |
| 564 | case asan: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 565 | return sanitize.Properties.Sanitize.Address |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 566 | case hwasan: |
| 567 | return sanitize.Properties.Sanitize.Hwaddress |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 568 | case tsan: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 569 | return sanitize.Properties.Sanitize.Thread |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 570 | case intOverflow: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 571 | return sanitize.Properties.Sanitize.Integer_overflow |
| 572 | case cfi: |
| 573 | return sanitize.Properties.Sanitize.Cfi |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 574 | case scs: |
| 575 | return sanitize.Properties.Sanitize.Scs |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 576 | default: |
| 577 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 578 | } |
| 579 | } |
| 580 | |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 581 | func (sanitize *sanitize) isUnsanitizedVariant() bool { |
| 582 | return !sanitize.isSanitizerEnabled(asan) && |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 583 | !sanitize.isSanitizerEnabled(hwasan) && |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 584 | !sanitize.isSanitizerEnabled(tsan) && |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 585 | !sanitize.isSanitizerEnabled(cfi) && |
| 586 | !sanitize.isSanitizerEnabled(scs) |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Jayant Chowdhary | b7e08ca | 2018-05-10 15:29:24 -0700 | [diff] [blame] | 589 | func (sanitize *sanitize) isVariantOnProductionDevice() bool { |
| 590 | return !sanitize.isSanitizerEnabled(asan) && |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 591 | !sanitize.isSanitizerEnabled(hwasan) && |
Jayant Chowdhary | b7e08ca | 2018-05-10 15:29:24 -0700 | [diff] [blame] | 592 | !sanitize.isSanitizerEnabled(tsan) |
| 593 | } |
| 594 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 595 | func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) { |
| 596 | switch t { |
| 597 | case asan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 598 | sanitize.Properties.Sanitize.Address = boolPtr(b) |
Colin Cross | 91169fe | 2016-08-11 15:54:20 -0700 | [diff] [blame] | 599 | if !b { |
| 600 | sanitize.Properties.Sanitize.Coverage = nil |
| 601 | } |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 602 | case hwasan: |
| 603 | sanitize.Properties.Sanitize.Hwaddress = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 604 | case tsan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 605 | sanitize.Properties.Sanitize.Thread = boolPtr(b) |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 606 | case intOverflow: |
| 607 | sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 608 | case cfi: |
| 609 | sanitize.Properties.Sanitize.Cfi = boolPtr(b) |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 610 | case scs: |
| 611 | sanitize.Properties.Sanitize.Scs = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 612 | default: |
| 613 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 614 | } |
| 615 | if b { |
| 616 | sanitize.Properties.SanitizerEnabled = true |
| 617 | } |
| 618 | } |
| 619 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 620 | // Check if the sanitizer is explicitly disabled (as opposed to nil by |
| 621 | // virtue of not being set). |
| 622 | func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool { |
| 623 | if sanitize == nil { |
| 624 | return false |
| 625 | } |
| 626 | |
| 627 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 628 | return sanitizerVal != nil && *sanitizerVal == false |
| 629 | } |
| 630 | |
| 631 | // There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled) |
| 632 | // because enabling a sanitizer either directly (via the blueprint) or |
| 633 | // indirectly (via a mutator) sets the bool ptr to true, and you can't |
| 634 | // distinguish between the cases. It isn't needed though - both cases can be |
| 635 | // treated identically. |
| 636 | func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool { |
| 637 | if sanitize == nil { |
| 638 | return false |
| 639 | } |
| 640 | |
| 641 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 642 | return sanitizerVal != nil && *sanitizerVal == true |
| 643 | } |
| 644 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 645 | func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool { |
| 646 | t, ok := tag.(dependencyTag) |
| 647 | return ok && t.library || t == reuseObjTag |
| 648 | } |
| 649 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 650 | // Propagate sanitizer requirements down from binaries |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 651 | func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) { |
| 652 | return func(mctx android.TopDownMutatorContext) { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 653 | if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) { |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 654 | mctx.WalkDeps(func(child, parent android.Module) bool { |
| 655 | if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) { |
| 656 | return false |
| 657 | } |
| 658 | if d, ok := child.(*Module); ok && d.sanitize != nil && |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 659 | !Bool(d.sanitize.Properties.Sanitize.Never) && |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 660 | !d.sanitize.isSanitizerExplicitlyDisabled(t) { |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 661 | if t == cfi || t == hwasan || t == scs { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 662 | if d.static() { |
| 663 | d.sanitize.Properties.SanitizeDep = true |
| 664 | } |
| 665 | } else { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 666 | d.sanitize.Properties.SanitizeDep = true |
| 667 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 668 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 669 | return true |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 670 | }) |
Jiyong Park | f97782b | 2019-02-13 20:28:58 +0900 | [diff] [blame] | 671 | } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok { |
| 672 | // If an APEX module includes a lib which is enabled for a sanitizer T, then |
| 673 | // the APEX module is also enabled for the same sanitizer type. |
| 674 | mctx.VisitDirectDeps(func(child android.Module) { |
| 675 | if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) { |
| 676 | sanitizeable.EnableSanitizer(t.name()) |
| 677 | } |
| 678 | }) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 683 | // 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] | 684 | func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) { |
| 685 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
| 686 | mctx.WalkDeps(func(child, parent android.Module) bool { |
| 687 | if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) { |
| 688 | return false |
| 689 | } |
| 690 | if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil { |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 691 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 692 | if enableMinimalRuntime(d.sanitize) { |
| 693 | // If a static dependency is built with the minimal runtime, |
| 694 | // make sure we include the ubsan minimal runtime. |
| 695 | c.sanitize.Properties.MinimalRuntimeDep = true |
| 696 | } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) || |
| 697 | len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 { |
| 698 | // If a static dependency runs with full ubsan diagnostics, |
| 699 | // make sure we include the ubsan runtime. |
| 700 | c.sanitize.Properties.UbsanRuntimeDep = true |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 701 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 702 | } |
| 703 | return true |
| 704 | }) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 708 | // Add the dependency to the runtime library for each of the sanitizer variants |
| 709 | func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 710 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
Pirama Arumuga Nainar | 6aa2102 | 2019-01-25 00:20:35 +0000 | [diff] [blame] | 711 | if !c.Enabled() { |
| 712 | return |
| 713 | } |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 714 | var sanitizers []string |
| 715 | var diagSanitizers []string |
| 716 | |
| 717 | if Bool(c.sanitize.Properties.Sanitize.All_undefined) { |
| 718 | sanitizers = append(sanitizers, "undefined") |
| 719 | } else { |
| 720 | if Bool(c.sanitize.Properties.Sanitize.Undefined) { |
| 721 | sanitizers = append(sanitizers, |
| 722 | "bool", |
| 723 | "integer-divide-by-zero", |
| 724 | "return", |
| 725 | "returns-nonnull-attribute", |
| 726 | "shift-exponent", |
| 727 | "unreachable", |
| 728 | "vla-bound", |
| 729 | // TODO(danalbert): The following checks currently have compiler performance issues. |
| 730 | //"alignment", |
| 731 | //"bounds", |
| 732 | //"enum", |
| 733 | //"float-cast-overflow", |
| 734 | //"float-divide-by-zero", |
| 735 | //"nonnull-attribute", |
| 736 | //"null", |
| 737 | //"shift-base", |
| 738 | //"signed-integer-overflow", |
| 739 | // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on. |
| 740 | // https://llvm.org/PR19302 |
| 741 | // http://reviews.llvm.org/D6974 |
| 742 | // "object-size", |
| 743 | ) |
| 744 | } |
| 745 | sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...) |
| 746 | } |
| 747 | |
| 748 | if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) { |
| 749 | diagSanitizers = append(diagSanitizers, "undefined") |
| 750 | } |
| 751 | |
| 752 | diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...) |
| 753 | |
| 754 | if Bool(c.sanitize.Properties.Sanitize.Address) { |
| 755 | sanitizers = append(sanitizers, "address") |
| 756 | diagSanitizers = append(diagSanitizers, "address") |
| 757 | } |
| 758 | |
| 759 | if Bool(c.sanitize.Properties.Sanitize.Hwaddress) { |
| 760 | sanitizers = append(sanitizers, "hwaddress") |
| 761 | } |
| 762 | |
| 763 | if Bool(c.sanitize.Properties.Sanitize.Thread) { |
| 764 | sanitizers = append(sanitizers, "thread") |
| 765 | } |
| 766 | |
| 767 | if Bool(c.sanitize.Properties.Sanitize.Safestack) { |
| 768 | sanitizers = append(sanitizers, "safe-stack") |
| 769 | } |
| 770 | |
| 771 | if Bool(c.sanitize.Properties.Sanitize.Cfi) { |
| 772 | sanitizers = append(sanitizers, "cfi") |
| 773 | |
| 774 | if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) { |
| 775 | diagSanitizers = append(diagSanitizers, "cfi") |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) { |
| 780 | sanitizers = append(sanitizers, "unsigned-integer-overflow") |
| 781 | sanitizers = append(sanitizers, "signed-integer-overflow") |
| 782 | if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) { |
| 783 | diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow") |
| 784 | diagSanitizers = append(diagSanitizers, "signed-integer-overflow") |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | if Bool(c.sanitize.Properties.Sanitize.Scudo) { |
| 789 | sanitizers = append(sanitizers, "scudo") |
| 790 | } |
| 791 | |
| 792 | if Bool(c.sanitize.Properties.Sanitize.Scs) { |
| 793 | sanitizers = append(sanitizers, "shadow-call-stack") |
| 794 | } |
| 795 | |
| 796 | // Save the list of sanitizers. These will be used again when generating |
| 797 | // the build rules (for Cflags, etc.) |
| 798 | c.sanitize.Properties.Sanitizers = sanitizers |
| 799 | c.sanitize.Properties.DiagSanitizers = diagSanitizers |
| 800 | |
| 801 | // Determine the runtime library required |
| 802 | runtimeLibrary := "" |
| 803 | toolchain := c.toolchain(mctx) |
| 804 | if Bool(c.sanitize.Properties.Sanitize.Address) { |
| 805 | runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain) |
| 806 | } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) { |
| 807 | if c.staticBinary() { |
| 808 | runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain) |
| 809 | } else { |
| 810 | runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain) |
| 811 | } |
| 812 | } else if Bool(c.sanitize.Properties.Sanitize.Thread) { |
| 813 | runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain) |
| 814 | } else if Bool(c.sanitize.Properties.Sanitize.Scudo) { |
| 815 | if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep { |
| 816 | runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain) |
| 817 | } else { |
| 818 | runtimeLibrary = config.ScudoRuntimeLibrary(toolchain) |
| 819 | } |
| 820 | } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep { |
| 821 | runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain) |
| 822 | } |
| 823 | |
| 824 | if mctx.Device() && runtimeLibrary != "" { |
Jiyong Park | 3b1746a | 2019-01-29 11:15:04 +0900 | [diff] [blame] | 825 | if inList(runtimeLibrary, llndkLibraries) && !c.static() && c.useVndk() { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 826 | runtimeLibrary = runtimeLibrary + llndkLibrarySuffix |
| 827 | } |
| 828 | |
| 829 | // Adding dependency to the runtime library. We are using *FarVariation* |
| 830 | // because the runtime libraries themselves are not mutated by sanitizer |
| 831 | // mutators and thus don't have sanitizer variants whereas this module |
| 832 | // has been already mutated. |
| 833 | // |
| 834 | // Note that by adding dependency with {static|shared}DepTag, the lib is |
| 835 | // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module |
| 836 | if c.staticBinary() { |
| 837 | // static executable gets static runtime libs |
| 838 | mctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 839 | {Mutator: "link", Variation: "static"}, |
Jiyong Park | 3b1746a | 2019-01-29 11:15:04 +0900 | [diff] [blame] | 840 | {Mutator: "image", Variation: c.imageVariation()}, |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 841 | {Mutator: "arch", Variation: mctx.Target().String()}, |
| 842 | }, staticDepTag, runtimeLibrary) |
| 843 | } else if !c.static() { |
Jiyong Park | 3b1746a | 2019-01-29 11:15:04 +0900 | [diff] [blame] | 844 | // dynamic executable and shared libs get shared runtime libs |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 845 | mctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 846 | {Mutator: "link", Variation: "shared"}, |
Jiyong Park | 3b1746a | 2019-01-29 11:15:04 +0900 | [diff] [blame] | 847 | {Mutator: "image", Variation: c.imageVariation()}, |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 848 | {Mutator: "arch", Variation: mctx.Target().String()}, |
Jiyong Park | 64a44f2 | 2019-01-18 14:37:08 +0900 | [diff] [blame] | 849 | }, earlySharedDepTag, runtimeLibrary) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 850 | } |
| 851 | // static lib does not have dependency to the runtime library. The |
| 852 | // dependency will be added to the executables or shared libs using |
| 853 | // the static lib. |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | type Sanitizeable interface { |
| 859 | android.Module |
Jiyong Park | 388ef3f | 2019-01-28 19:47:32 +0900 | [diff] [blame] | 860 | IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool |
Jiyong Park | f97782b | 2019-02-13 20:28:58 +0900 | [diff] [blame] | 861 | EnableSanitizer(sanitizerName string) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 862 | } |
| 863 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 864 | // Create sanitized variants for modules that need them |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 865 | func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) { |
| 866 | return func(mctx android.BottomUpMutatorContext) { |
Vishwath Mohan | e615345 | 2017-08-11 00:52:44 +0000 | [diff] [blame] | 867 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 868 | if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) { |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 869 | modules := mctx.CreateVariations(t.variationName()) |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 870 | modules[0].(*Module).sanitize.SetSanitizer(t, true) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 871 | } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep { |
| 872 | // Save original sanitizer status before we assign values to variant |
| 873 | // 0 as that overwrites the original. |
| 874 | isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t) |
| 875 | |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 876 | modules := mctx.CreateVariations("", t.variationName()) |
Colin Cross | b0f2895 | 2016-09-19 16:46:53 -0700 | [diff] [blame] | 877 | modules[0].(*Module).sanitize.SetSanitizer(t, false) |
| 878 | modules[1].(*Module).sanitize.SetSanitizer(t, true) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 879 | |
Colin Cross | b0f2895 | 2016-09-19 16:46:53 -0700 | [diff] [blame] | 880 | modules[0].(*Module).sanitize.Properties.SanitizeDep = false |
| 881 | modules[1].(*Module).sanitize.Properties.SanitizeDep = false |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 882 | |
| 883 | // We don't need both variants active for anything but CFI-enabled |
| 884 | // target static libraries, so suppress the appropriate variant in |
| 885 | // all other cases. |
| 886 | if t == cfi { |
| 887 | if c.static() { |
| 888 | if !mctx.Device() { |
| 889 | if isSanitizerEnabled { |
| 890 | modules[0].(*Module).Properties.PreventInstall = true |
| 891 | modules[0].(*Module).Properties.HideFromMake = true |
| 892 | } else { |
| 893 | modules[1].(*Module).Properties.PreventInstall = true |
| 894 | modules[1].(*Module).Properties.HideFromMake = true |
| 895 | } |
| 896 | } else { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 897 | cfiStaticLibs := cfiStaticLibs(mctx.Config()) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 898 | |
| 899 | cfiStaticLibsMutex.Lock() |
| 900 | *cfiStaticLibs = append(*cfiStaticLibs, c.Name()) |
| 901 | cfiStaticLibsMutex.Unlock() |
| 902 | } |
| 903 | } else { |
| 904 | modules[0].(*Module).Properties.PreventInstall = true |
| 905 | modules[0].(*Module).Properties.HideFromMake = true |
| 906 | } |
| 907 | } else if t == asan { |
| 908 | if mctx.Device() { |
| 909 | // CFI and ASAN are currently mutually exclusive so disable |
| 910 | // CFI if this is an ASAN variant. |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 911 | modules[1].(*Module).sanitize.Properties.InSanitizerDir = true |
| 912 | modules[1].(*Module).sanitize.SetSanitizer(cfi, false) |
| 913 | } |
Vishwath Mohan | e21fe42 | 2017-11-01 19:42:45 -0700 | [diff] [blame] | 914 | if isSanitizerEnabled { |
| 915 | modules[0].(*Module).Properties.PreventInstall = true |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 916 | modules[0].(*Module).Properties.HideFromMake = true |
Vishwath Mohan | e21fe42 | 2017-11-01 19:42:45 -0700 | [diff] [blame] | 917 | } else { |
| 918 | modules[1].(*Module).Properties.PreventInstall = true |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 919 | modules[1].(*Module).Properties.HideFromMake = true |
Vishwath Mohan | e21fe42 | 2017-11-01 19:42:45 -0700 | [diff] [blame] | 920 | } |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 921 | } else if t == scs { |
| 922 | // We don't currently link any static libraries built with make into |
| 923 | // libraries built with SCS, so we don't need logic for propagating |
| 924 | // SCSness of dependencies into make. |
| 925 | if !c.static() { |
| 926 | if isSanitizerEnabled { |
| 927 | modules[0].(*Module).Properties.PreventInstall = true |
| 928 | modules[0].(*Module).Properties.HideFromMake = true |
| 929 | } else { |
| 930 | modules[1].(*Module).Properties.PreventInstall = true |
| 931 | modules[1].(*Module).Properties.HideFromMake = true |
| 932 | } |
| 933 | } |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 934 | } else if t == hwasan { |
| 935 | if mctx.Device() { |
| 936 | // CFI and HWASAN are currently mutually exclusive so disable |
| 937 | // CFI if this is an HWASAN variant. |
| 938 | modules[1].(*Module).sanitize.SetSanitizer(cfi, false) |
| 939 | } |
| 940 | |
| 941 | if c.static() { |
| 942 | if c.useVndk() { |
| 943 | hwasanVendorStaticLibs := hwasanVendorStaticLibs(mctx.Config()) |
| 944 | hwasanStaticLibsMutex.Lock() |
| 945 | *hwasanVendorStaticLibs = append(*hwasanVendorStaticLibs, c.Name()) |
| 946 | hwasanStaticLibsMutex.Unlock() |
| 947 | } else { |
| 948 | hwasanStaticLibs := hwasanStaticLibs(mctx.Config()) |
| 949 | hwasanStaticLibsMutex.Lock() |
| 950 | *hwasanStaticLibs = append(*hwasanStaticLibs, c.Name()) |
| 951 | hwasanStaticLibsMutex.Unlock() |
| 952 | } |
| 953 | } else { |
| 954 | if isSanitizerEnabled { |
| 955 | modules[0].(*Module).Properties.PreventInstall = true |
| 956 | modules[0].(*Module).Properties.HideFromMake = true |
| 957 | } else { |
| 958 | modules[1].(*Module).Properties.PreventInstall = true |
| 959 | modules[1].(*Module).Properties.HideFromMake = true |
| 960 | } |
| 961 | } |
Vishwath Mohan | e21fe42 | 2017-11-01 19:42:45 -0700 | [diff] [blame] | 962 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 963 | } |
| 964 | c.sanitize.Properties.SanitizeDep = false |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 965 | } 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] | 966 | // APEX modules fall here |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 967 | mctx.CreateVariations(t.variationName()) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | } |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 971 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 972 | var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs") |
| 973 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 974 | func cfiStaticLibs(config android.Config) *[]string { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 975 | return config.Once(cfiStaticLibsKey, func() interface{} { |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 976 | return &[]string{} |
| 977 | }).(*[]string) |
| 978 | } |
| 979 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 980 | var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs") |
| 981 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 982 | func hwasanStaticLibs(config android.Config) *[]string { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 983 | return config.Once(hwasanStaticLibsKey, func() interface{} { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 984 | return &[]string{} |
| 985 | }).(*[]string) |
| 986 | } |
| 987 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 988 | var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs") |
| 989 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 990 | func hwasanVendorStaticLibs(config android.Config) *[]string { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 991 | return config.Once(hwasanVendorStaticLibsKey, func() interface{} { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 992 | return &[]string{} |
| 993 | }).(*[]string) |
| 994 | } |
| 995 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 996 | func enableMinimalRuntime(sanitize *sanitize) bool { |
| 997 | if !Bool(sanitize.Properties.Sanitize.Address) && |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 998 | !Bool(sanitize.Properties.Sanitize.Hwaddress) && |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 999 | (Bool(sanitize.Properties.Sanitize.Integer_overflow) || |
| 1000 | len(sanitize.Properties.Sanitize.Misc_undefined) > 0) && |
| 1001 | !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) || |
| 1002 | Bool(sanitize.Properties.Sanitize.Diag.Cfi) || |
| 1003 | len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) { |
| 1004 | return true |
| 1005 | } |
| 1006 | return false |
| 1007 | } |
| 1008 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1009 | func cfiMakeVarsProvider(ctx android.MakeVarsContext) { |
| 1010 | cfiStaticLibs := cfiStaticLibs(ctx.Config()) |
Jeff Gaston | 7276539 | 2017-11-28 16:37:53 -0800 | [diff] [blame] | 1011 | sort.Strings(*cfiStaticLibs) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1012 | ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " ")) |
| 1013 | } |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1014 | |
| 1015 | func hwasanMakeVarsProvider(ctx android.MakeVarsContext) { |
| 1016 | hwasanStaticLibs := hwasanStaticLibs(ctx.Config()) |
| 1017 | sort.Strings(*hwasanStaticLibs) |
| 1018 | ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " ")) |
| 1019 | |
| 1020 | hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config()) |
| 1021 | sort.Strings(*hwasanVendorStaticLibs) |
| 1022 | ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " ")) |
| 1023 | } |