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