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