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