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