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