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