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