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