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" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 20 | "strings" |
| 21 | |
| 22 | "github.com/google/blueprint" |
| 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 | |
| 37 | cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fvisibility=default", |
| 38 | "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"} |
Vishwath Mohan | f3918d3 | 2017-02-14 07:59:33 -0800 | [diff] [blame] | 39 | // FIXME: revert the __cfi_check flag when clang is updated to r280031. |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 40 | cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi", |
| 41 | "-Wl,-plugin-opt,O1 -Wl,-export-dynamic-symbol=__cfi_check"} |
| 42 | cfiArflags = []string{"--plugin ${config.ClangBin}/../lib64/LLVMgold.so"} |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 43 | |
| 44 | intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"} |
Dan Willemsen | cbceaab | 2016-10-13 16:44:07 -0700 | [diff] [blame] | 45 | ) |
| 46 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 47 | type sanitizerType int |
| 48 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 49 | func boolPtr(v bool) *bool { |
| 50 | if v { |
| 51 | return &v |
| 52 | } else { |
| 53 | return nil |
| 54 | } |
| 55 | } |
| 56 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 57 | const ( |
| 58 | asan sanitizerType = iota + 1 |
| 59 | tsan |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 60 | intOverflow |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 61 | ) |
| 62 | |
| 63 | func (t sanitizerType) String() string { |
| 64 | switch t { |
| 65 | case asan: |
| 66 | return "asan" |
| 67 | case tsan: |
| 68 | return "tsan" |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 69 | case intOverflow: |
| 70 | return "intOverflow" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 71 | default: |
| 72 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | type SanitizeProperties struct { |
| 77 | // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer |
| 78 | Sanitize struct { |
| 79 | Never bool `android:"arch_variant"` |
| 80 | |
| 81 | // main sanitizers |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 82 | Address *bool `android:"arch_variant"` |
| 83 | Thread *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 84 | |
| 85 | // local sanitizers |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 86 | Undefined *bool `android:"arch_variant"` |
| 87 | All_undefined *bool `android:"arch_variant"` |
| 88 | Misc_undefined []string `android:"arch_variant"` |
| 89 | Coverage *bool `android:"arch_variant"` |
| 90 | Safestack *bool `android:"arch_variant"` |
| 91 | Cfi *bool `android:"arch_variant"` |
| 92 | Integer_overflow *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 93 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 94 | // Sanitizers to run in the diagnostic mode (as opposed to the release mode). |
| 95 | // Replaces abort() on error with a human-readable error message. |
| 96 | // Address and Thread sanitizers always run in diagnostic mode. |
| 97 | Diag struct { |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 98 | Undefined *bool `android:"arch_variant"` |
| 99 | Cfi *bool `android:"arch_variant"` |
| 100 | Integer_overflow *bool `android:"arch_variant"` |
| 101 | Misc_undefined []string `android:"arch_variant"` |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // value to pass to -fsanitize-recover= |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 105 | Recover []string |
| 106 | |
| 107 | // value to pass to -fsanitize-blacklist |
| 108 | Blacklist *string |
| 109 | } `android:"arch_variant"` |
| 110 | |
| 111 | SanitizerEnabled bool `blueprint:"mutated"` |
| 112 | SanitizeDep bool `blueprint:"mutated"` |
Vishwath Mohan | 1dd8839 | 2017-03-29 22:00:18 -0700 | [diff] [blame] | 113 | InSanitizerDir bool `blueprint:"mutated"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | type sanitize struct { |
| 117 | Properties SanitizeProperties |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 118 | |
Jiyong Park | 27b188b | 2017-07-18 13:23:39 +0900 | [diff] [blame] | 119 | runtimeLibrary string |
| 120 | androidMkRuntimeLibrary string |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | func (sanitize *sanitize) props() []interface{} { |
| 124 | return []interface{}{&sanitize.Properties} |
| 125 | } |
| 126 | |
| 127 | func (sanitize *sanitize) begin(ctx BaseModuleContext) { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 128 | s := &sanitize.Properties.Sanitize |
| 129 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 130 | // Don't apply sanitizers to NDK code. |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 131 | if ctx.useSdk() { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 132 | s.Never = true |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Never always wins. |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 136 | if s.Never { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 137 | return |
| 138 | } |
| 139 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 140 | var globalSanitizers []string |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 141 | var globalSanitizersDiag []string |
| 142 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 143 | if ctx.clang() { |
| 144 | if ctx.Host() { |
| 145 | globalSanitizers = ctx.AConfig().SanitizeHost() |
| 146 | } else { |
Colin Cross | 23ae82a | 2016-11-02 14:34:39 -0700 | [diff] [blame] | 147 | arches := ctx.AConfig().SanitizeDeviceArch() |
| 148 | if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) { |
| 149 | globalSanitizers = ctx.AConfig().SanitizeDevice() |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 150 | globalSanitizersDiag = ctx.AConfig().SanitizeDeviceDiag() |
Colin Cross | 23ae82a | 2016-11-02 14:34:39 -0700 | [diff] [blame] | 151 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 155 | if len(globalSanitizers) > 0 { |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 156 | var found bool |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 157 | if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil { |
| 158 | s.All_undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 159 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 160 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 161 | if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil { |
| 162 | s.Undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Evgenii Stepanov | 774cb81 | 2016-12-28 15:52:54 -0800 | [diff] [blame] | 165 | if found, globalSanitizers = removeFromList("address", globalSanitizers); found { |
| 166 | if s.Address == nil { |
| 167 | s.Address = boolPtr(true) |
| 168 | } else if *s.Address == false { |
| 169 | // Coverage w/o address is an error. If globalSanitizers includes both, and the module |
| 170 | // disables address, then disable coverage as well. |
| 171 | _, globalSanitizers = removeFromList("coverage", globalSanitizers) |
| 172 | } |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 175 | if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil { |
| 176 | s.Thread = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 179 | if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil { |
| 180 | s.Coverage = boolPtr(true) |
| 181 | } |
| 182 | |
| 183 | if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil { |
| 184 | s.Safestack = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 187 | if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil { |
| 188 | s.Cfi = boolPtr(true) |
| 189 | } |
| 190 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 191 | if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil { |
Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 192 | if !ctx.AConfig().IntegerOverflowDisabledForPath(ctx.ModuleDir()) { |
| 193 | s.Integer_overflow = boolPtr(true) |
| 194 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 197 | if len(globalSanitizers) > 0 { |
| 198 | ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0]) |
| 199 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 200 | |
| 201 | if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found && |
| 202 | s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) { |
| 203 | s.Diag.Integer_overflow = boolPtr(true) |
| 204 | } |
| 205 | |
| 206 | if len(globalSanitizersDiag) > 0 { |
| 207 | ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0]) |
| 208 | } |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 209 | } |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 210 | |
Evgenii Stepanov | a83fdac | 2017-01-31 18:37:30 -0800 | [diff] [blame] | 211 | // CFI needs gold linker, and mips toolchain does not have one. |
| 212 | if !ctx.AConfig().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 { |
Vishwath Mohan | 1b017a7 | 2017-01-19 13:54:55 -0800 | [diff] [blame] | 213 | s.Cfi = nil |
| 214 | s.Diag.Cfi = nil |
| 215 | } |
| 216 | |
Vishwath Mohan | 6d67e6e | 2017-02-07 20:31:41 -0800 | [diff] [blame] | 217 | // Also disable CFI for arm32 until b/35157333 is fixed. |
| 218 | if ctx.Arch().ArchType == android.Arm { |
| 219 | s.Cfi = nil |
| 220 | s.Diag.Cfi = nil |
| 221 | } |
| 222 | |
Vishwath Mohan | 8f4fdd8 | 2017-04-20 07:42:52 -0700 | [diff] [blame] | 223 | // Also disable CFI if ASAN is enabled. |
| 224 | if Bool(s.Address) { |
| 225 | s.Cfi = nil |
| 226 | s.Diag.Cfi = nil |
| 227 | } |
| 228 | |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 229 | if ctx.staticBinary() { |
| 230 | s.Address = nil |
Colin Cross | 91169fe | 2016-08-11 15:54:20 -0700 | [diff] [blame] | 231 | s.Coverage = nil |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 232 | s.Thread = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 235 | if Bool(s.All_undefined) { |
| 236 | s.Undefined = nil |
| 237 | } |
| 238 | |
Evgenii Stepanov | 0a8a0d0 | 2016-05-12 13:54:53 -0700 | [diff] [blame] | 239 | if !ctx.toolchain().Is64Bit() { |
| 240 | // TSAN and SafeStack are not supported on 32-bit architectures |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 241 | s.Thread = nil |
| 242 | s.Safestack = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 243 | // TODO(ccross): error for compile_multilib = "32"? |
| 244 | } |
| 245 | |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 246 | 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] | 247 | 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] | 248 | sanitize.Properties.SanitizerEnabled = true |
| 249 | } |
| 250 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 251 | if Bool(s.Coverage) { |
| 252 | if !Bool(s.Address) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 253 | ctx.ModuleErrorf(`Use of "coverage" also requires "address"`) |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 259 | if !sanitize.Properties.SanitizerEnabled { // || c.static() { |
| 260 | return deps |
| 261 | } |
| 262 | |
| 263 | if ctx.Device() { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 264 | if Bool(sanitize.Properties.Sanitize.Address) { |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 265 | deps.StaticLibs = append(deps.StaticLibs, asanLibs...) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | return deps |
| 270 | } |
| 271 | |
| 272 | func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags { |
| 273 | if !sanitize.Properties.SanitizerEnabled { |
| 274 | return flags |
| 275 | } |
| 276 | |
| 277 | if !ctx.clang() { |
| 278 | ctx.ModuleErrorf("Use of sanitizers requires clang") |
| 279 | } |
| 280 | |
| 281 | var sanitizers []string |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 282 | var diagSanitizers []string |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 283 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 284 | if Bool(sanitize.Properties.Sanitize.All_undefined) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 285 | sanitizers = append(sanitizers, "undefined") |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 286 | } else { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 287 | if Bool(sanitize.Properties.Sanitize.Undefined) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 288 | sanitizers = append(sanitizers, |
| 289 | "bool", |
| 290 | "integer-divide-by-zero", |
| 291 | "return", |
| 292 | "returns-nonnull-attribute", |
| 293 | "shift-exponent", |
| 294 | "unreachable", |
| 295 | "vla-bound", |
| 296 | // TODO(danalbert): The following checks currently have compiler performance issues. |
| 297 | //"alignment", |
| 298 | //"bounds", |
| 299 | //"enum", |
| 300 | //"float-cast-overflow", |
| 301 | //"float-divide-by-zero", |
| 302 | //"nonnull-attribute", |
| 303 | //"null", |
| 304 | //"shift-base", |
| 305 | //"signed-integer-overflow", |
| 306 | // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on. |
| 307 | // https://llvm.org/PR19302 |
| 308 | // http://reviews.llvm.org/D6974 |
| 309 | // "object-size", |
| 310 | ) |
| 311 | } |
| 312 | sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...) |
| 313 | } |
| 314 | |
Ivan Lozano | 651275b | 2017-06-13 10:24:34 -0700 | [diff] [blame] | 315 | if Bool(sanitize.Properties.Sanitize.Diag.Undefined) { |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 316 | diagSanitizers = append(diagSanitizers, "undefined") |
| 317 | } |
| 318 | |
Ivan Lozano | 651275b | 2017-06-13 10:24:34 -0700 | [diff] [blame] | 319 | diagSanitizers = append(diagSanitizers, sanitize.Properties.Sanitize.Diag.Misc_undefined...) |
| 320 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 321 | if Bool(sanitize.Properties.Sanitize.Address) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 322 | if ctx.Arch().ArchType == android.Arm { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 323 | // Frame pointer based unwinder in ASan requires ARM frame setup. |
| 324 | // TODO: put in flags? |
| 325 | flags.RequiredInstructionSet = "arm" |
| 326 | } |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 327 | flags.CFlags = append(flags.CFlags, asanCflags...) |
| 328 | flags.LdFlags = append(flags.LdFlags, asanLdflags...) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 329 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 330 | if ctx.Host() { |
| 331 | // -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 332 | // libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 333 | flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed") |
| 334 | } else { |
| 335 | flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0") |
| 336 | flags.DynamicLinker = "/system/bin/linker_asan" |
| 337 | if flags.Toolchain.Is64Bit() { |
| 338 | flags.DynamicLinker += "64" |
| 339 | } |
| 340 | } |
| 341 | sanitizers = append(sanitizers, "address") |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 342 | diagSanitizers = append(diagSanitizers, "address") |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Yabin Cui | 6be405e | 2017-10-19 15:52:11 -0700 | [diff] [blame^] | 345 | if Bool(sanitize.Properties.Sanitize.Thread) { |
| 346 | sanitizers = append(sanitizers, "thread") |
| 347 | } |
| 348 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 349 | if Bool(sanitize.Properties.Sanitize.Coverage) { |
Zach Riggle | 06bbd89 | 2017-08-21 17:12:32 -0400 | [diff] [blame] | 350 | 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] | 351 | } |
| 352 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 353 | if Bool(sanitize.Properties.Sanitize.Safestack) { |
Evgenii Stepanov | 0a8a0d0 | 2016-05-12 13:54:53 -0700 | [diff] [blame] | 354 | sanitizers = append(sanitizers, "safe-stack") |
| 355 | } |
| 356 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 357 | if Bool(sanitize.Properties.Sanitize.Cfi) { |
Evgenii Stepanov | 7ebf9fa | 2017-01-20 14:13:06 -0800 | [diff] [blame] | 358 | if ctx.Arch().ArchType == android.Arm { |
| 359 | // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up |
| 360 | // to do this on a function basis, so force Thumb on the entire module. |
| 361 | flags.RequiredInstructionSet = "thumb" |
Evgenii Stepanov | a83fdac | 2017-01-31 18:37:30 -0800 | [diff] [blame] | 362 | // Workaround for b/33678192. CFI jumptables need Thumb2 codegen. Revert when |
| 363 | // Clang is updated past r290384. |
| 364 | flags.LdFlags = append(flags.LdFlags, "-march=armv7-a") |
Evgenii Stepanov | 7ebf9fa | 2017-01-20 14:13:06 -0800 | [diff] [blame] | 365 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 366 | sanitizers = append(sanitizers, "cfi") |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 367 | flags.CFlags = append(flags.CFlags, cfiCflags...) |
| 368 | flags.LdFlags = append(flags.LdFlags, cfiLdflags...) |
| 369 | flags.ArFlags = append(flags.ArFlags, cfiArflags...) |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 370 | if Bool(sanitize.Properties.Sanitize.Diag.Cfi) { |
| 371 | diagSanitizers = append(diagSanitizers, "cfi") |
| 372 | } |
| 373 | } |
| 374 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 375 | if Bool(sanitize.Properties.Sanitize.Integer_overflow) { |
| 376 | if !ctx.static() { |
| 377 | sanitizers = append(sanitizers, "unsigned-integer-overflow") |
| 378 | sanitizers = append(sanitizers, "signed-integer-overflow") |
| 379 | flags.CFlags = append(flags.CFlags, intOverflowCflags...) |
| 380 | if Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) { |
| 381 | diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow") |
| 382 | diagSanitizers = append(diagSanitizers, "signed-integer-overflow") |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 387 | if len(sanitizers) > 0 { |
| 388 | sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",") |
| 389 | flags.CFlags = append(flags.CFlags, sanitizeArg) |
| 390 | if ctx.Host() { |
| 391 | flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all") |
| 392 | flags.LdFlags = append(flags.LdFlags, sanitizeArg) |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 393 | // Host sanitizers only link symbols in the final executable, so |
| 394 | // there will always be undefined symbols in intermediate libraries. |
| 395 | _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 396 | } else { |
Colin Cross | 263abbd | 2016-07-15 13:10:48 -0700 | [diff] [blame] | 397 | flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort") |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 401 | if len(diagSanitizers) > 0 { |
| 402 | flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ",")) |
| 403 | } |
| 404 | // FIXME: enable RTTI if diag + (cfi or vptr) |
| 405 | |
Andreas Gampe | 9707116 | 2017-05-08 13:15:23 -0700 | [diff] [blame] | 406 | if sanitize.Properties.Sanitize.Recover != nil { |
| 407 | flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+ |
| 408 | strings.Join(sanitize.Properties.Sanitize.Recover, ",")) |
| 409 | } |
| 410 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 411 | // Link a runtime library if needed. |
| 412 | runtimeLibrary := "" |
| 413 | if Bool(sanitize.Properties.Sanitize.Address) { |
| 414 | runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain()) |
Yabin Cui | 6be405e | 2017-10-19 15:52:11 -0700 | [diff] [blame^] | 415 | } else if Bool(sanitize.Properties.Sanitize.Thread) { |
| 416 | runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(ctx.toolchain()) |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 417 | } else if len(diagSanitizers) > 0 { |
| 418 | runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain()) |
| 419 | } |
| 420 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 421 | if runtimeLibrary != "" { |
Jiyong Park | 27b188b | 2017-07-18 13:23:39 +0900 | [diff] [blame] | 422 | // ASan runtime library must be the first in the link order. |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 423 | flags.libFlags = append([]string{ |
| 424 | "${config.ClangAsanLibDir}/" + runtimeLibrary + ctx.toolchain().ShlibSuffix(), |
| 425 | }, flags.libFlags...) |
| 426 | sanitize.runtimeLibrary = runtimeLibrary |
Jiyong Park | 27b188b | 2017-07-18 13:23:39 +0900 | [diff] [blame] | 427 | |
| 428 | // When linking against VNDK, use the vendor variant of the runtime lib |
| 429 | sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 430 | if ctx.useVndk() { |
Jiyong Park | 27b188b | 2017-07-18 13:23:39 +0900 | [diff] [blame] | 431 | sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix |
| 432 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 435 | blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 436 | if blacklist.Valid() { |
| 437 | flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String()) |
| 438 | flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path()) |
| 439 | } |
| 440 | |
| 441 | return flags |
| 442 | } |
| 443 | |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 444 | func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 445 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) { |
Jiyong Park | 27b188b | 2017-07-18 13:23:39 +0900 | [diff] [blame] | 446 | if sanitize.androidMkRuntimeLibrary != "" { |
| 447 | fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES += "+sanitize.androidMkRuntimeLibrary) |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 448 | } |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 449 | }) |
| 450 | } |
| 451 | |
Vishwath Mohan | 1dd8839 | 2017-03-29 22:00:18 -0700 | [diff] [blame] | 452 | func (sanitize *sanitize) inSanitizerDir() bool { |
| 453 | return sanitize.Properties.InSanitizerDir |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 456 | func (sanitize *sanitize) Sanitizer(t sanitizerType) bool { |
| 457 | if sanitize == nil { |
| 458 | return false |
| 459 | } |
| 460 | |
| 461 | switch t { |
| 462 | case asan: |
| 463 | return Bool(sanitize.Properties.Sanitize.Address) |
| 464 | case tsan: |
| 465 | return Bool(sanitize.Properties.Sanitize.Thread) |
| 466 | case intOverflow: |
| 467 | return Bool(sanitize.Properties.Sanitize.Integer_overflow) |
| 468 | default: |
| 469 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 470 | } |
| 471 | } |
| 472 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 473 | func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) { |
| 474 | switch t { |
| 475 | case asan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 476 | sanitize.Properties.Sanitize.Address = boolPtr(b) |
Colin Cross | 91169fe | 2016-08-11 15:54:20 -0700 | [diff] [blame] | 477 | if !b { |
| 478 | sanitize.Properties.Sanitize.Coverage = nil |
| 479 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 480 | case tsan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 481 | sanitize.Properties.Sanitize.Thread = boolPtr(b) |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 482 | case intOverflow: |
| 483 | sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 484 | default: |
| 485 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 486 | } |
| 487 | if b { |
| 488 | sanitize.Properties.SanitizerEnabled = true |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // Propagate asan requirements down from binaries |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 493 | func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) { |
| 494 | return func(mctx android.TopDownMutatorContext) { |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 495 | if c, ok := mctx.Module().(*Module); ok && c.sanitize.Sanitizer(t) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 496 | mctx.VisitDepsDepthFirst(func(module blueprint.Module) { |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 497 | if d, ok := mctx.Module().(*Module); ok && c.sanitize != nil && |
| 498 | !c.sanitize.Properties.Sanitize.Never { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 499 | d.sanitize.Properties.SanitizeDep = true |
| 500 | } |
| 501 | }) |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // Create asan variants for modules that need them |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 507 | func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) { |
| 508 | return func(mctx android.BottomUpMutatorContext) { |
Vishwath Mohan | e615345 | 2017-08-11 00:52:44 +0000 | [diff] [blame] | 509 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 510 | if c.isDependencyRoot() && c.sanitize.Sanitizer(t) { |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 511 | modules := mctx.CreateVariations(t.String()) |
| 512 | modules[0].(*Module).sanitize.SetSanitizer(t, true) |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 513 | } else if c.sanitize.Properties.SanitizeDep { |
Colin Cross | b0f2895 | 2016-09-19 16:46:53 -0700 | [diff] [blame] | 514 | modules := mctx.CreateVariations("", t.String()) |
| 515 | modules[0].(*Module).sanitize.SetSanitizer(t, false) |
| 516 | modules[1].(*Module).sanitize.SetSanitizer(t, true) |
| 517 | modules[0].(*Module).sanitize.Properties.SanitizeDep = false |
| 518 | modules[1].(*Module).sanitize.Properties.SanitizeDep = false |
Vishwath Mohan | e615345 | 2017-08-11 00:52:44 +0000 | [diff] [blame] | 519 | if mctx.Device() { |
| 520 | modules[1].(*Module).sanitize.Properties.InSanitizerDir = true |
| 521 | } else { |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 522 | modules[0].(*Module).Properties.PreventInstall = true |
Vishwath Mohan | e615345 | 2017-08-11 00:52:44 +0000 | [diff] [blame] | 523 | } |
Colin Cross | b0f2895 | 2016-09-19 16:46:53 -0700 | [diff] [blame] | 524 | if mctx.AConfig().EmbeddedInMake() { |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 525 | modules[0].(*Module).Properties.HideFromMake = true |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 526 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 527 | } |
| 528 | c.sanitize.Properties.SanitizeDep = false |
| 529 | } |
| 530 | } |
| 531 | } |