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" |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 19 | "io" |
Jeff Gaston | 7276539 | 2017-11-28 16:37:53 -0800 | [diff] [blame] | 20 | "sort" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 21 | "strings" |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 22 | "sync" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 23 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 24 | "android/soong/android" |
Evgenii Stepanov | af36db1 | 2016-08-15 14:18:24 -0700 | [diff] [blame] | 25 | "android/soong/cc/config" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 26 | ) |
| 27 | |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 28 | var ( |
| 29 | // Any C flags added by sanitizer which libTooling tools may not |
| 30 | // understand also need to be added to ClangLibToolingUnknownCflags in |
| 31 | // cc/config/clang.go |
Vishwath Mohan | f3918d3 | 2017-02-14 07:59:33 -0800 | [diff] [blame] | 32 | |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 33 | asanCflags = []string{"-fno-omit-frame-pointer"} |
| 34 | asanLdflags = []string{"-Wl,-u,__asan_preinit"} |
| 35 | asanLibs = []string{"libasan"} |
| 36 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 37 | cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso", |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 38 | "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"} |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 39 | cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi", |
Pirama Arumuga Nainar | bdb17f0 | 2017-08-28 21:50:17 -0700 | [diff] [blame] | 40 | "-Wl,-plugin-opt,O1"} |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 41 | cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map" |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 42 | cfiStaticLibsMutex sync.Mutex |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 43 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 44 | intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"} |
| 45 | minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer", "-fno-sanitize-recover=integer"} |
Dan Willemsen | cbceaab | 2016-10-13 16:44:07 -0700 | [diff] [blame] | 46 | ) |
| 47 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 48 | type sanitizerType int |
| 49 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 50 | func boolPtr(v bool) *bool { |
| 51 | if v { |
| 52 | return &v |
| 53 | } else { |
| 54 | return nil |
| 55 | } |
| 56 | } |
| 57 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 58 | const ( |
| 59 | asan sanitizerType = iota + 1 |
| 60 | tsan |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 61 | intOverflow |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 62 | cfi |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 63 | ) |
| 64 | |
| 65 | func (t sanitizerType) String() string { |
| 66 | switch t { |
| 67 | case asan: |
| 68 | return "asan" |
| 69 | case tsan: |
| 70 | return "tsan" |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 71 | case intOverflow: |
| 72 | return "intOverflow" |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 73 | case cfi: |
| 74 | return "cfi" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 75 | default: |
| 76 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | type SanitizeProperties struct { |
| 81 | // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer |
| 82 | Sanitize struct { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 83 | Never *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 84 | |
| 85 | // main sanitizers |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 86 | Address *bool `android:"arch_variant"` |
| 87 | Thread *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 88 | |
| 89 | // local sanitizers |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 90 | Undefined *bool `android:"arch_variant"` |
| 91 | All_undefined *bool `android:"arch_variant"` |
| 92 | Misc_undefined []string `android:"arch_variant"` |
| 93 | Coverage *bool `android:"arch_variant"` |
| 94 | Safestack *bool `android:"arch_variant"` |
| 95 | Cfi *bool `android:"arch_variant"` |
| 96 | Integer_overflow *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 97 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 98 | // Sanitizers to run in the diagnostic mode (as opposed to the release mode). |
| 99 | // Replaces abort() on error with a human-readable error message. |
| 100 | // Address and Thread sanitizers always run in diagnostic mode. |
| 101 | Diag struct { |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 102 | Undefined *bool `android:"arch_variant"` |
| 103 | Cfi *bool `android:"arch_variant"` |
| 104 | Integer_overflow *bool `android:"arch_variant"` |
| 105 | Misc_undefined []string `android:"arch_variant"` |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // value to pass to -fsanitize-recover= |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 109 | Recover []string |
| 110 | |
| 111 | // value to pass to -fsanitize-blacklist |
| 112 | Blacklist *string |
| 113 | } `android:"arch_variant"` |
| 114 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 115 | SanitizerEnabled bool `blueprint:"mutated"` |
| 116 | SanitizeDep bool `blueprint:"mutated"` |
| 117 | MinimalRuntimeDep bool `blueprint:"mutated"` |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 118 | UbsanRuntimeDep bool `blueprint:"mutated"` |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 119 | InSanitizerDir bool `blueprint:"mutated"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | type sanitize struct { |
| 123 | Properties SanitizeProperties |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 124 | |
Jiyong Park | 27b188b | 2017-07-18 13:23:39 +0900 | [diff] [blame] | 125 | runtimeLibrary string |
| 126 | androidMkRuntimeLibrary string |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 129 | func init() { |
| 130 | android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider) |
| 131 | } |
| 132 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 133 | func (sanitize *sanitize) props() []interface{} { |
| 134 | return []interface{}{&sanitize.Properties} |
| 135 | } |
| 136 | |
| 137 | func (sanitize *sanitize) begin(ctx BaseModuleContext) { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 138 | s := &sanitize.Properties.Sanitize |
| 139 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 140 | // Don't apply sanitizers to NDK code. |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 141 | if ctx.useSdk() { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 142 | s.Never = BoolPtr(true) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Never always wins. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 146 | if Bool(s.Never) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 147 | return |
| 148 | } |
| 149 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 150 | var globalSanitizers []string |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 151 | var globalSanitizersDiag []string |
| 152 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 153 | if ctx.clang() { |
| 154 | if ctx.Host() { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 155 | globalSanitizers = ctx.Config().SanitizeHost() |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 156 | } else { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 157 | arches := ctx.Config().SanitizeDeviceArch() |
Colin Cross | 23ae82a | 2016-11-02 14:34:39 -0700 | [diff] [blame] | 158 | if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 159 | globalSanitizers = ctx.Config().SanitizeDevice() |
| 160 | globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag() |
Colin Cross | 23ae82a | 2016-11-02 14:34:39 -0700 | [diff] [blame] | 161 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 165 | if len(globalSanitizers) > 0 { |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 166 | var found bool |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 167 | if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil { |
| 168 | s.All_undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 169 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 170 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 171 | if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil { |
| 172 | s.Undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Evgenii Stepanov | 774cb81 | 2016-12-28 15:52:54 -0800 | [diff] [blame] | 175 | if found, globalSanitizers = removeFromList("address", globalSanitizers); found { |
| 176 | if s.Address == nil { |
| 177 | s.Address = boolPtr(true) |
| 178 | } else if *s.Address == false { |
| 179 | // Coverage w/o address is an error. If globalSanitizers includes both, and the module |
| 180 | // disables address, then disable coverage as well. |
| 181 | _, globalSanitizers = removeFromList("coverage", globalSanitizers) |
| 182 | } |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 185 | if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil { |
| 186 | s.Thread = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 189 | if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil { |
| 190 | s.Coverage = boolPtr(true) |
| 191 | } |
| 192 | |
| 193 | if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil { |
| 194 | s.Safestack = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 197 | if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 198 | if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) { |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 199 | s.Cfi = boolPtr(true) |
| 200 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 203 | // Global integer_overflow builds do not support static libraries. |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 204 | if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil { |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 205 | if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() { |
Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 206 | s.Integer_overflow = boolPtr(true) |
| 207 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 210 | if len(globalSanitizers) > 0 { |
| 211 | ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0]) |
| 212 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 213 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 214 | // Global integer_overflow builds do not support static library diagnostics. |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 215 | if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found && |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 216 | s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() { |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 217 | s.Diag.Integer_overflow = boolPtr(true) |
| 218 | } |
| 219 | |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 220 | if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found && |
| 221 | s.Diag.Cfi == nil && Bool(s.Cfi) { |
| 222 | s.Diag.Cfi = boolPtr(true) |
| 223 | } |
| 224 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 225 | if len(globalSanitizersDiag) > 0 { |
| 226 | ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0]) |
| 227 | } |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 228 | } |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 229 | |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 230 | // Enable CFI for all components in the include paths |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 231 | if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) { |
Vishwath Mohan | 3af8ee0 | 2018-03-30 02:55:23 +0000 | [diff] [blame] | 232 | s.Cfi = boolPtr(true) |
| 233 | if inList("cfi", ctx.Config().SanitizeDeviceDiag()) { |
| 234 | s.Diag.Cfi = boolPtr(true) |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Evgenii Stepanov | a83fdac | 2017-01-31 18:37:30 -0800 | [diff] [blame] | 238 | // CFI needs gold linker, and mips toolchain does not have one. |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 239 | 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] | 240 | s.Cfi = nil |
| 241 | s.Diag.Cfi = nil |
| 242 | } |
| 243 | |
Vishwath Mohan | 6d67e6e | 2017-02-07 20:31:41 -0800 | [diff] [blame] | 244 | // Also disable CFI for arm32 until b/35157333 is fixed. |
| 245 | if ctx.Arch().ArchType == android.Arm { |
| 246 | s.Cfi = nil |
| 247 | s.Diag.Cfi = nil |
| 248 | } |
| 249 | |
Vishwath Mohan | 8f4fdd8 | 2017-04-20 07:42:52 -0700 | [diff] [blame] | 250 | // Also disable CFI if ASAN is enabled. |
| 251 | if Bool(s.Address) { |
| 252 | s.Cfi = nil |
| 253 | s.Diag.Cfi = nil |
| 254 | } |
| 255 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 256 | // Disable sanitizers that depend on the UBSan runtime for host builds. |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 257 | if ctx.Host() { |
| 258 | s.Cfi = nil |
| 259 | s.Diag.Cfi = nil |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 260 | s.Misc_undefined = nil |
| 261 | s.Undefined = nil |
| 262 | s.All_undefined = nil |
| 263 | s.Integer_overflow = nil |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 266 | if ctx.staticBinary() { |
| 267 | s.Address = nil |
Colin Cross | 91169fe | 2016-08-11 15:54:20 -0700 | [diff] [blame] | 268 | s.Coverage = nil |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 269 | s.Thread = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 272 | if Bool(s.All_undefined) { |
| 273 | s.Undefined = nil |
| 274 | } |
| 275 | |
Evgenii Stepanov | 0a8a0d0 | 2016-05-12 13:54:53 -0700 | [diff] [blame] | 276 | if !ctx.toolchain().Is64Bit() { |
| 277 | // TSAN and SafeStack are not supported on 32-bit architectures |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 278 | s.Thread = nil |
| 279 | s.Safestack = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 280 | // TODO(ccross): error for compile_multilib = "32"? |
| 281 | } |
| 282 | |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 283 | if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) || |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 284 | Bool(s.Coverage) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0) { |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 285 | sanitize.Properties.SanitizerEnabled = true |
| 286 | } |
| 287 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 288 | if Bool(s.Coverage) { |
| 289 | if !Bool(s.Address) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 290 | ctx.ModuleErrorf(`Use of "coverage" also requires "address"`) |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 296 | if !sanitize.Properties.SanitizerEnabled { // || c.static() { |
| 297 | return deps |
| 298 | } |
| 299 | |
| 300 | if ctx.Device() { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 301 | if Bool(sanitize.Properties.Sanitize.Address) { |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 302 | deps.StaticLibs = append(deps.StaticLibs, asanLibs...) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | return deps |
| 307 | } |
| 308 | |
| 309 | func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags { |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 310 | minimalRuntimePath := "${config.ClangAsanLibDir}/" + config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a" |
| 311 | |
| 312 | if ctx.Device() && sanitize.Properties.MinimalRuntimeDep { |
| 313 | flags.LdFlags = append(flags.LdFlags, minimalRuntimePath) |
| 314 | } |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 315 | if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 316 | return flags |
| 317 | } |
| 318 | |
| 319 | if !ctx.clang() { |
| 320 | ctx.ModuleErrorf("Use of sanitizers requires clang") |
| 321 | } |
| 322 | |
| 323 | var sanitizers []string |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 324 | var diagSanitizers []string |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 325 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 326 | if Bool(sanitize.Properties.Sanitize.All_undefined) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 327 | sanitizers = append(sanitizers, "undefined") |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 328 | } else { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 329 | if Bool(sanitize.Properties.Sanitize.Undefined) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 330 | sanitizers = append(sanitizers, |
| 331 | "bool", |
| 332 | "integer-divide-by-zero", |
| 333 | "return", |
| 334 | "returns-nonnull-attribute", |
| 335 | "shift-exponent", |
| 336 | "unreachable", |
| 337 | "vla-bound", |
| 338 | // TODO(danalbert): The following checks currently have compiler performance issues. |
| 339 | //"alignment", |
| 340 | //"bounds", |
| 341 | //"enum", |
| 342 | //"float-cast-overflow", |
| 343 | //"float-divide-by-zero", |
| 344 | //"nonnull-attribute", |
| 345 | //"null", |
| 346 | //"shift-base", |
| 347 | //"signed-integer-overflow", |
| 348 | // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on. |
| 349 | // https://llvm.org/PR19302 |
| 350 | // http://reviews.llvm.org/D6974 |
| 351 | // "object-size", |
| 352 | ) |
| 353 | } |
| 354 | sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...) |
| 355 | } |
| 356 | |
Ivan Lozano | 651275b | 2017-06-13 10:24:34 -0700 | [diff] [blame] | 357 | if Bool(sanitize.Properties.Sanitize.Diag.Undefined) { |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 358 | diagSanitizers = append(diagSanitizers, "undefined") |
| 359 | } |
| 360 | |
Ivan Lozano | 651275b | 2017-06-13 10:24:34 -0700 | [diff] [blame] | 361 | diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...) |
| 362 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 363 | if Bool(sanitize.Properties.Sanitize.Address) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 364 | if ctx.Arch().ArchType == android.Arm { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 365 | // Frame pointer based unwinder in ASan requires ARM frame setup. |
| 366 | // TODO: put in flags? |
| 367 | flags.RequiredInstructionSet = "arm" |
| 368 | } |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 369 | flags.CFlags = append(flags.CFlags, asanCflags...) |
| 370 | flags.LdFlags = append(flags.LdFlags, asanLdflags...) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 371 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 372 | if ctx.Host() { |
| 373 | // -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 374 | // libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 375 | flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed") |
| 376 | } else { |
| 377 | flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0") |
| 378 | flags.DynamicLinker = "/system/bin/linker_asan" |
| 379 | if flags.Toolchain.Is64Bit() { |
| 380 | flags.DynamicLinker += "64" |
| 381 | } |
| 382 | } |
| 383 | sanitizers = append(sanitizers, "address") |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 384 | diagSanitizers = append(diagSanitizers, "address") |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Yabin Cui | 6be405e | 2017-10-19 15:52:11 -0700 | [diff] [blame] | 387 | if Bool(sanitize.Properties.Sanitize.Thread) { |
| 388 | sanitizers = append(sanitizers, "thread") |
| 389 | } |
| 390 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 391 | if Bool(sanitize.Properties.Sanitize.Coverage) { |
Zach Riggle | 06bbd89 | 2017-08-21 17:12:32 -0400 | [diff] [blame] | 392 | flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp") |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 395 | if Bool(sanitize.Properties.Sanitize.Safestack) { |
Evgenii Stepanov | 0a8a0d0 | 2016-05-12 13:54:53 -0700 | [diff] [blame] | 396 | sanitizers = append(sanitizers, "safe-stack") |
| 397 | } |
| 398 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 399 | if Bool(sanitize.Properties.Sanitize.Cfi) { |
Evgenii Stepanov | 7ebf9fa | 2017-01-20 14:13:06 -0800 | [diff] [blame] | 400 | if ctx.Arch().ArchType == android.Arm { |
| 401 | // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up |
| 402 | // to do this on a function basis, so force Thumb on the entire module. |
| 403 | flags.RequiredInstructionSet = "thumb" |
| 404 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 405 | sanitizers = append(sanitizers, "cfi") |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 406 | |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 407 | flags.CFlags = append(flags.CFlags, cfiCflags...) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 408 | // Only append the default visibility flag if -fvisibility has not already been set |
| 409 | // to hidden. |
| 410 | if !inList("-fvisibility=hidden", flags.CFlags) { |
| 411 | flags.CFlags = append(flags.CFlags, "-fvisibility=default") |
| 412 | } |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 413 | flags.LdFlags = append(flags.LdFlags, cfiLdflags...) |
Zhizhou Yang | 51be632 | 2018-02-08 18:32:11 -0800 | [diff] [blame] | 414 | flags.ArGoldPlugin = true |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 415 | if Bool(sanitize.Properties.Sanitize.Diag.Cfi) { |
| 416 | diagSanitizers = append(diagSanitizers, "cfi") |
| 417 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 418 | |
| 419 | if ctx.staticBinary() { |
| 420 | _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags) |
| 421 | _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags) |
| 422 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 425 | if Bool(sanitize.Properties.Sanitize.Integer_overflow) { |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 426 | sanitizers = append(sanitizers, "unsigned-integer-overflow") |
| 427 | sanitizers = append(sanitizers, "signed-integer-overflow") |
| 428 | flags.CFlags = append(flags.CFlags, intOverflowCflags...) |
| 429 | if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) { |
| 430 | diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow") |
| 431 | diagSanitizers = append(diagSanitizers, "signed-integer-overflow") |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 435 | if len(sanitizers) > 0 { |
| 436 | sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",") |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 437 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 438 | flags.CFlags = append(flags.CFlags, sanitizeArg) |
| 439 | if ctx.Host() { |
| 440 | flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all") |
| 441 | flags.LdFlags = append(flags.LdFlags, sanitizeArg) |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 442 | // Host sanitizers only link symbols in the final executable, so |
| 443 | // there will always be undefined symbols in intermediate libraries. |
| 444 | _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 445 | } else { |
Colin Cross | 263abbd | 2016-07-15 13:10:48 -0700 | [diff] [blame] | 446 | flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort") |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 447 | |
| 448 | if enableMinimalRuntime(sanitize) { |
| 449 | flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " ")) |
| 450 | flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...) |
| 451 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 455 | if len(diagSanitizers) > 0 { |
Ivan Lozano | b7d0f52 | 2018-01-20 01:44:38 +0000 | [diff] [blame] | 456 | flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ",")) |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 457 | } |
| 458 | // FIXME: enable RTTI if diag + (cfi or vptr) |
| 459 | |
Andreas Gampe | 9707116 | 2017-05-08 13:15:23 -0700 | [diff] [blame] | 460 | if sanitize.Properties.Sanitize.Recover != nil { |
| 461 | flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+ |
| 462 | strings.Join(sanitize.Properties.Sanitize.Recover, ",")) |
| 463 | } |
| 464 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 465 | // Link a runtime library if needed. |
| 466 | runtimeLibrary := "" |
| 467 | if Bool(sanitize.Properties.Sanitize.Address) { |
| 468 | runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain()) |
Yabin Cui | 6be405e | 2017-10-19 15:52:11 -0700 | [diff] [blame] | 469 | } else if Bool(sanitize.Properties.Sanitize.Thread) { |
| 470 | runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain()) |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 471 | } else if len(diagSanitizers) > 0 || sanitize.Properties.UbsanRuntimeDep { |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 472 | runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain()) |
| 473 | } |
| 474 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 475 | if runtimeLibrary != "" { |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 476 | runtimeLibraryPath := "${config.ClangAsanLibDir}/" + runtimeLibrary |
| 477 | if !ctx.static() { |
| 478 | runtimeLibraryPath = runtimeLibraryPath + ctx.toolchain().ShlibSuffix() |
| 479 | } else { |
| 480 | runtimeLibraryPath = runtimeLibraryPath + ".a" |
| 481 | } |
| 482 | |
Jiyong Park | 27b188b | 2017-07-18 13:23:39 +0900 | [diff] [blame] | 483 | // ASan runtime library must be the first in the link order. |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 484 | flags.libFlags = append([]string{runtimeLibraryPath}, flags.libFlags...) |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 485 | sanitize.runtimeLibrary = runtimeLibrary |
Jiyong Park | 27b188b | 2017-07-18 13:23:39 +0900 | [diff] [blame] | 486 | |
| 487 | // When linking against VNDK, use the vendor variant of the runtime lib |
| 488 | sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 489 | if ctx.useVndk() { |
Jiyong Park | 27b188b | 2017-07-18 13:23:39 +0900 | [diff] [blame] | 490 | sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix |
| 491 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 494 | blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 495 | if blacklist.Valid() { |
| 496 | flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String()) |
| 497 | flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path()) |
| 498 | } |
| 499 | |
| 500 | return flags |
| 501 | } |
| 502 | |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 503 | func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 504 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) { |
Jiyong Park | 27b188b | 2017-07-18 13:23:39 +0900 | [diff] [blame] | 505 | if sanitize.androidMkRuntimeLibrary != "" { |
| 506 | fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary) |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 507 | } |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 508 | }) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 509 | |
| 510 | // Add a suffix for CFI-enabled static libraries to allow surfacing both to make without a |
| 511 | // name conflict. |
| 512 | if ret.Class == "STATIC_LIBRARIES" && Bool(sanitize.Properties.Sanitize.Cfi) { |
| 513 | ret.SubName += ".cfi" |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 514 | } |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Vishwath Mohan | 1dd8839 | 2017-03-29 22:00:18 -0700 | [diff] [blame] | 517 | func (sanitize *sanitize) inSanitizerDir() bool { |
| 518 | return sanitize.Properties.InSanitizerDir |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 521 | func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool { |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 522 | switch t { |
| 523 | case asan: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 524 | return sanitize.Properties.Sanitize.Address |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 525 | case tsan: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 526 | return sanitize.Properties.Sanitize.Thread |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 527 | case intOverflow: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 528 | return sanitize.Properties.Sanitize.Integer_overflow |
| 529 | case cfi: |
| 530 | return sanitize.Properties.Sanitize.Cfi |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 531 | default: |
| 532 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 533 | } |
| 534 | } |
| 535 | |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 536 | func (sanitize *sanitize) isUnsanitizedVariant() bool { |
| 537 | return !sanitize.isSanitizerEnabled(asan) && |
| 538 | !sanitize.isSanitizerEnabled(tsan) && |
| 539 | !sanitize.isSanitizerEnabled(cfi) |
| 540 | } |
| 541 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 542 | func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) { |
| 543 | switch t { |
| 544 | case asan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 545 | sanitize.Properties.Sanitize.Address = boolPtr(b) |
Colin Cross | 91169fe | 2016-08-11 15:54:20 -0700 | [diff] [blame] | 546 | if !b { |
| 547 | sanitize.Properties.Sanitize.Coverage = nil |
| 548 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 549 | case tsan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 550 | sanitize.Properties.Sanitize.Thread = boolPtr(b) |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 551 | case intOverflow: |
| 552 | sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 553 | case cfi: |
| 554 | sanitize.Properties.Sanitize.Cfi = boolPtr(b) |
| 555 | sanitize.Properties.Sanitize.Diag.Cfi = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 556 | default: |
| 557 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 558 | } |
| 559 | if b { |
| 560 | sanitize.Properties.SanitizerEnabled = true |
| 561 | } |
| 562 | } |
| 563 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 564 | // Check if the sanitizer is explicitly disabled (as opposed to nil by |
| 565 | // virtue of not being set). |
| 566 | func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool { |
| 567 | if sanitize == nil { |
| 568 | return false |
| 569 | } |
| 570 | |
| 571 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 572 | return sanitizerVal != nil && *sanitizerVal == false |
| 573 | } |
| 574 | |
| 575 | // There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled) |
| 576 | // because enabling a sanitizer either directly (via the blueprint) or |
| 577 | // indirectly (via a mutator) sets the bool ptr to true, and you can't |
| 578 | // distinguish between the cases. It isn't needed though - both cases can be |
| 579 | // treated identically. |
| 580 | func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool { |
| 581 | if sanitize == nil { |
| 582 | return false |
| 583 | } |
| 584 | |
| 585 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 586 | return sanitizerVal != nil && *sanitizerVal == true |
| 587 | } |
| 588 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 589 | // Propagate asan requirements down from binaries |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 590 | func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) { |
| 591 | return func(mctx android.TopDownMutatorContext) { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 592 | if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) { |
Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 593 | mctx.VisitDepsDepthFirst(func(module android.Module) { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 594 | if d, ok := module.(*Module); ok && d.sanitize != nil && |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 595 | !Bool(d.sanitize.Properties.Sanitize.Never) && |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 596 | !d.sanitize.isSanitizerExplicitlyDisabled(t) { |
| 597 | if (t == cfi && d.static()) || t != cfi { |
| 598 | d.sanitize.Properties.SanitizeDep = true |
| 599 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 600 | } |
| 601 | }) |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 606 | // Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies. |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 607 | func sanitizerRuntimeDepsMutator() func(android.TopDownMutatorContext) { |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 608 | return func(mctx android.TopDownMutatorContext) { |
| 609 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
| 610 | mctx.VisitDepsDepthFirst(func(module android.Module) { |
| 611 | if d, ok := module.(*Module); ok && d.static() && d.sanitize != nil { |
| 612 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 613 | if enableMinimalRuntime(d.sanitize) { |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 614 | // If a static dependency is built with the minimal runtime, |
| 615 | // make sure we include the ubsan minimal runtime. |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 616 | c.sanitize.Properties.MinimalRuntimeDep = true |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame^] | 617 | } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) || |
| 618 | len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 { |
| 619 | // If a static dependency runs with full ubsan diagnostics, |
| 620 | // make sure we include the ubsan runtime. |
| 621 | c.sanitize.Properties.UbsanRuntimeDep = true |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | }) |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 629 | // Create sanitized variants for modules that need them |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 630 | func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) { |
| 631 | return func(mctx android.BottomUpMutatorContext) { |
Vishwath Mohan | e615345 | 2017-08-11 00:52:44 +0000 | [diff] [blame] | 632 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 633 | if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) { |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 634 | modules := mctx.CreateVariations(t.String()) |
| 635 | modules[0].(*Module).sanitize.SetSanitizer(t, true) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 636 | } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep { |
| 637 | // Save original sanitizer status before we assign values to variant |
| 638 | // 0 as that overwrites the original. |
| 639 | isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t) |
| 640 | |
Colin Cross | b0f2895 | 2016-09-19 16:46:53 -0700 | [diff] [blame] | 641 | modules := mctx.CreateVariations("", t.String()) |
| 642 | modules[0].(*Module).sanitize.SetSanitizer(t, false) |
| 643 | modules[1].(*Module).sanitize.SetSanitizer(t, true) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 644 | |
Colin Cross | b0f2895 | 2016-09-19 16:46:53 -0700 | [diff] [blame] | 645 | modules[0].(*Module).sanitize.Properties.SanitizeDep = false |
| 646 | modules[1].(*Module).sanitize.Properties.SanitizeDep = false |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 647 | |
| 648 | // We don't need both variants active for anything but CFI-enabled |
| 649 | // target static libraries, so suppress the appropriate variant in |
| 650 | // all other cases. |
| 651 | if t == cfi { |
| 652 | if c.static() { |
| 653 | if !mctx.Device() { |
| 654 | if isSanitizerEnabled { |
| 655 | modules[0].(*Module).Properties.PreventInstall = true |
| 656 | modules[0].(*Module).Properties.HideFromMake = true |
| 657 | } else { |
| 658 | modules[1].(*Module).Properties.PreventInstall = true |
| 659 | modules[1].(*Module).Properties.HideFromMake = true |
| 660 | } |
| 661 | } else { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 662 | cfiStaticLibs := cfiStaticLibs(mctx.Config()) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 663 | |
| 664 | cfiStaticLibsMutex.Lock() |
| 665 | *cfiStaticLibs = append(*cfiStaticLibs, c.Name()) |
| 666 | cfiStaticLibsMutex.Unlock() |
| 667 | } |
| 668 | } else { |
| 669 | modules[0].(*Module).Properties.PreventInstall = true |
| 670 | modules[0].(*Module).Properties.HideFromMake = true |
| 671 | } |
| 672 | } else if t == asan { |
| 673 | if mctx.Device() { |
| 674 | // CFI and ASAN are currently mutually exclusive so disable |
| 675 | // CFI if this is an ASAN variant. |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 676 | modules[1].(*Module).sanitize.Properties.InSanitizerDir = true |
| 677 | modules[1].(*Module).sanitize.SetSanitizer(cfi, false) |
| 678 | } |
Vishwath Mohan | e21fe42 | 2017-11-01 19:42:45 -0700 | [diff] [blame] | 679 | if isSanitizerEnabled { |
| 680 | modules[0].(*Module).Properties.PreventInstall = true |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 681 | modules[0].(*Module).Properties.HideFromMake = true |
Vishwath Mohan | e21fe42 | 2017-11-01 19:42:45 -0700 | [diff] [blame] | 682 | } else { |
| 683 | modules[1].(*Module).Properties.PreventInstall = true |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 684 | modules[1].(*Module).Properties.HideFromMake = true |
Vishwath Mohan | e21fe42 | 2017-11-01 19:42:45 -0700 | [diff] [blame] | 685 | } |
| 686 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 687 | } |
| 688 | c.sanitize.Properties.SanitizeDep = false |
| 689 | } |
| 690 | } |
| 691 | } |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 692 | |
| 693 | func cfiStaticLibs(config android.Config) *[]string { |
| 694 | return config.Once("cfiStaticLibs", func() interface{} { |
| 695 | return &[]string{} |
| 696 | }).(*[]string) |
| 697 | } |
| 698 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 699 | func enableMinimalRuntime(sanitize *sanitize) bool { |
| 700 | if !Bool(sanitize.Properties.Sanitize.Address) && |
| 701 | (Bool(sanitize.Properties.Sanitize.Integer_overflow) || |
| 702 | len(sanitize.Properties.Sanitize.Misc_undefined) > 0) && |
| 703 | !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) || |
| 704 | Bool(sanitize.Properties.Sanitize.Diag.Cfi) || |
| 705 | len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) { |
| 706 | return true |
| 707 | } |
| 708 | return false |
| 709 | } |
| 710 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 711 | func cfiMakeVarsProvider(ctx android.MakeVarsContext) { |
| 712 | cfiStaticLibs := cfiStaticLibs(ctx.Config()) |
Jeff Gaston | 7276539 | 2017-11-28 16:37:53 -0800 | [diff] [blame] | 713 | sort.Strings(*cfiStaticLibs) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 714 | ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " ")) |
| 715 | } |