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" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | type sanitizerType int |
| 27 | |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 28 | func boolPtr(v bool) *bool { |
| 29 | if v { |
| 30 | return &v |
| 31 | } else { |
| 32 | return nil |
| 33 | } |
| 34 | } |
| 35 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 36 | func init() { |
| 37 | pctx.StaticVariable("clangAsanLibDir", "${clangPath}/lib64/clang/3.8/lib/linux") |
| 38 | } |
| 39 | |
| 40 | const ( |
| 41 | asan sanitizerType = iota + 1 |
| 42 | tsan |
| 43 | ) |
| 44 | |
| 45 | func (t sanitizerType) String() string { |
| 46 | switch t { |
| 47 | case asan: |
| 48 | return "asan" |
| 49 | case tsan: |
| 50 | return "tsan" |
| 51 | default: |
| 52 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | type SanitizeProperties struct { |
| 57 | // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer |
| 58 | Sanitize struct { |
| 59 | Never bool `android:"arch_variant"` |
| 60 | |
| 61 | // main sanitizers |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 62 | Address *bool `android:"arch_variant"` |
| 63 | Thread *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 64 | |
| 65 | // local sanitizers |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 66 | Undefined *bool `android:"arch_variant"` |
| 67 | All_undefined *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 68 | Misc_undefined []string `android:"arch_variant"` |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 69 | Coverage *bool `android:"arch_variant"` |
| 70 | Safestack *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 71 | |
| 72 | // value to pass to -fsantitize-recover= |
| 73 | Recover []string |
| 74 | |
| 75 | // value to pass to -fsanitize-blacklist |
| 76 | Blacklist *string |
| 77 | } `android:"arch_variant"` |
| 78 | |
| 79 | SanitizerEnabled bool `blueprint:"mutated"` |
| 80 | SanitizeDep bool `blueprint:"mutated"` |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 81 | InData bool `blueprint:"mutated"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | type sanitize struct { |
| 85 | Properties SanitizeProperties |
| 86 | } |
| 87 | |
| 88 | func (sanitize *sanitize) props() []interface{} { |
| 89 | return []interface{}{&sanitize.Properties} |
| 90 | } |
| 91 | |
| 92 | func (sanitize *sanitize) begin(ctx BaseModuleContext) { |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 93 | s := &sanitize.Properties.Sanitize |
| 94 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 95 | // Don't apply sanitizers to NDK code. |
| 96 | if ctx.sdk() { |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 97 | s.Never = true |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Never always wins. |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 101 | if s.Never { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 102 | return |
| 103 | } |
| 104 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 105 | var globalSanitizers []string |
| 106 | if ctx.clang() { |
| 107 | if ctx.Host() { |
| 108 | globalSanitizers = ctx.AConfig().SanitizeHost() |
| 109 | } else { |
| 110 | globalSanitizers = ctx.AConfig().SanitizeDevice() |
| 111 | } |
| 112 | } |
| 113 | |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 114 | var found bool |
| 115 | if s.All_undefined == nil { |
| 116 | found, globalSanitizers = removeFromList("undefined", globalSanitizers) |
| 117 | s.All_undefined = boolPtr(found) |
| 118 | } |
| 119 | |
| 120 | if s.Undefined == nil { |
| 121 | found, globalSanitizers = removeFromList("default-ub", globalSanitizers) |
| 122 | s.Undefined = boolPtr(found) |
| 123 | } |
| 124 | |
| 125 | if s.Address == nil { |
| 126 | found, globalSanitizers = removeFromList("address", globalSanitizers) |
| 127 | s.Address = boolPtr(found) |
| 128 | } |
| 129 | |
| 130 | if s.Thread == nil { |
| 131 | found, globalSanitizers = removeFromList("thread", globalSanitizers) |
| 132 | s.Thread = boolPtr(found) |
| 133 | } |
| 134 | |
| 135 | if s.Coverage == nil { |
| 136 | found, globalSanitizers = removeFromList("coverage", globalSanitizers) |
| 137 | s.Coverage = boolPtr(found) |
| 138 | } |
| 139 | |
| 140 | if s.Safestack == nil { |
| 141 | found, globalSanitizers = removeFromList("safe-stack", globalSanitizers) |
| 142 | s.Safestack = boolPtr(found) |
| 143 | } |
| 144 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 145 | if len(globalSanitizers) > 0 { |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 146 | ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0]) |
| 147 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 148 | |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 149 | if Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || |
| 150 | Bool(s.Thread) || Bool(s.Coverage) || Bool(s.Safestack) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 151 | sanitize.Properties.SanitizerEnabled = true |
| 152 | } |
| 153 | |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 154 | if Bool(s.All_undefined) { |
| 155 | s.Undefined = nil |
| 156 | } |
| 157 | |
Evgenii Stepanov | 0a8a0d0 | 2016-05-12 13:54:53 -0700 | [diff] [blame] | 158 | if !ctx.toolchain().Is64Bit() { |
| 159 | // TSAN and SafeStack are not supported on 32-bit architectures |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 160 | s.Thread = nil |
| 161 | s.Safestack = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 162 | // TODO(ccross): error for compile_multilib = "32"? |
| 163 | } |
| 164 | |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 165 | if Bool(s.Coverage) { |
| 166 | if !Bool(s.Address) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 167 | ctx.ModuleErrorf(`Use of "coverage" also requires "address"`) |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 173 | if !sanitize.Properties.SanitizerEnabled { // || c.static() { |
| 174 | return deps |
| 175 | } |
| 176 | |
| 177 | if ctx.Device() { |
| 178 | deps.SharedLibs = append(deps.SharedLibs, "libdl") |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 179 | if Bool(sanitize.Properties.Sanitize.Address) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 180 | deps.StaticLibs = append(deps.StaticLibs, "libasan") |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return deps |
| 185 | } |
| 186 | |
| 187 | func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags { |
| 188 | if !sanitize.Properties.SanitizerEnabled { |
| 189 | return flags |
| 190 | } |
| 191 | |
| 192 | if !ctx.clang() { |
| 193 | ctx.ModuleErrorf("Use of sanitizers requires clang") |
| 194 | } |
| 195 | |
| 196 | var sanitizers []string |
| 197 | |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 198 | if Bool(sanitize.Properties.Sanitize.All_undefined) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 199 | sanitizers = append(sanitizers, "undefined") |
| 200 | if ctx.Device() { |
| 201 | ctx.ModuleErrorf("ubsan is not yet supported on the device") |
| 202 | } |
| 203 | } else { |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 204 | if Bool(sanitize.Properties.Sanitize.Undefined) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 205 | sanitizers = append(sanitizers, |
| 206 | "bool", |
| 207 | "integer-divide-by-zero", |
| 208 | "return", |
| 209 | "returns-nonnull-attribute", |
| 210 | "shift-exponent", |
| 211 | "unreachable", |
| 212 | "vla-bound", |
| 213 | // TODO(danalbert): The following checks currently have compiler performance issues. |
| 214 | //"alignment", |
| 215 | //"bounds", |
| 216 | //"enum", |
| 217 | //"float-cast-overflow", |
| 218 | //"float-divide-by-zero", |
| 219 | //"nonnull-attribute", |
| 220 | //"null", |
| 221 | //"shift-base", |
| 222 | //"signed-integer-overflow", |
| 223 | // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on. |
| 224 | // https://llvm.org/PR19302 |
| 225 | // http://reviews.llvm.org/D6974 |
| 226 | // "object-size", |
| 227 | ) |
| 228 | } |
| 229 | sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...) |
| 230 | } |
| 231 | |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 232 | if Bool(sanitize.Properties.Sanitize.Address) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 233 | if ctx.Arch().ArchType == android.Arm { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 234 | // Frame pointer based unwinder in ASan requires ARM frame setup. |
| 235 | // TODO: put in flags? |
| 236 | flags.RequiredInstructionSet = "arm" |
| 237 | } |
| 238 | flags.CFlags = append(flags.CFlags, "-fno-omit-frame-pointer") |
| 239 | flags.LdFlags = append(flags.LdFlags, "-Wl,-u,__asan_preinit") |
| 240 | |
| 241 | // ASan runtime library must be the first in the link order. |
| 242 | runtimeLibrary := ctx.toolchain().AddressSanitizerRuntimeLibrary() |
| 243 | if runtimeLibrary != "" { |
| 244 | flags.libFlags = append([]string{"${clangAsanLibDir}/" + runtimeLibrary}, flags.libFlags...) |
| 245 | } |
| 246 | if ctx.Host() { |
| 247 | // -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 248 | // libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
| 249 | flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread") |
| 250 | flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed") |
| 251 | } else { |
| 252 | flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0") |
| 253 | flags.DynamicLinker = "/system/bin/linker_asan" |
| 254 | if flags.Toolchain.Is64Bit() { |
| 255 | flags.DynamicLinker += "64" |
| 256 | } |
| 257 | } |
| 258 | sanitizers = append(sanitizers, "address") |
| 259 | } |
| 260 | |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 261 | if Bool(sanitize.Properties.Sanitize.Coverage) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 262 | flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp") |
| 263 | } |
| 264 | |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 265 | if Bool(sanitize.Properties.Sanitize.Safestack) { |
Evgenii Stepanov | 0a8a0d0 | 2016-05-12 13:54:53 -0700 | [diff] [blame] | 266 | sanitizers = append(sanitizers, "safe-stack") |
| 267 | } |
| 268 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 269 | if sanitize.Properties.Sanitize.Recover != nil { |
| 270 | flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+ |
| 271 | strings.Join(sanitize.Properties.Sanitize.Recover, ",")) |
| 272 | } |
| 273 | |
| 274 | if len(sanitizers) > 0 { |
| 275 | sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",") |
| 276 | flags.CFlags = append(flags.CFlags, sanitizeArg) |
| 277 | if ctx.Host() { |
| 278 | flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all") |
| 279 | flags.LdFlags = append(flags.LdFlags, sanitizeArg) |
| 280 | flags.LdFlags = append(flags.LdFlags, "-lrt", "-ldl") |
| 281 | } else { |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 282 | if !Bool(sanitize.Properties.Sanitize.Address) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 283 | flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort") |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 288 | blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 289 | if blacklist.Valid() { |
| 290 | flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String()) |
| 291 | flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path()) |
| 292 | } |
| 293 | |
| 294 | return flags |
| 295 | } |
| 296 | |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 297 | func (sanitize *sanitize) inData() bool { |
| 298 | return sanitize.Properties.InData |
| 299 | } |
| 300 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 301 | func (sanitize *sanitize) Sanitizer(t sanitizerType) bool { |
| 302 | if sanitize == nil { |
| 303 | return false |
| 304 | } |
| 305 | |
| 306 | switch t { |
| 307 | case asan: |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 308 | return Bool(sanitize.Properties.Sanitize.Address) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 309 | case tsan: |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 310 | return Bool(sanitize.Properties.Sanitize.Thread) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 311 | default: |
| 312 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) { |
| 317 | switch t { |
| 318 | case asan: |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 319 | sanitize.Properties.Sanitize.Address = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 320 | case tsan: |
Evgenii Stepanov | bb02886 | 2016-05-18 16:39:54 -0700 | [diff] [blame^] | 321 | sanitize.Properties.Sanitize.Thread = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 322 | default: |
| 323 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 324 | } |
| 325 | if b { |
| 326 | sanitize.Properties.SanitizerEnabled = true |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // Propagate asan requirements down from binaries |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 331 | func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) { |
| 332 | return func(mctx android.TopDownMutatorContext) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 333 | if c, ok := mctx.Module().(*Module); ok && c.sanitize.Sanitizer(t) { |
| 334 | mctx.VisitDepsDepthFirst(func(module blueprint.Module) { |
| 335 | if d, ok := mctx.Module().(*Module); ok && c.sanitize != nil && |
| 336 | !c.sanitize.Properties.Sanitize.Never { |
| 337 | d.sanitize.Properties.SanitizeDep = true |
| 338 | } |
| 339 | }) |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // Create asan variants for modules that need them |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 345 | func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) { |
| 346 | return func(mctx android.BottomUpMutatorContext) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 347 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
| 348 | if d, ok := c.linker.(baseLinkerInterface); ok && d.isDependencyRoot() && c.sanitize.Sanitizer(t) { |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 349 | modules := mctx.CreateVariations(t.String()) |
| 350 | modules[0].(*Module).sanitize.SetSanitizer(t, true) |
Colin Cross | b36ab1a | 2016-05-25 12:35:53 -0700 | [diff] [blame] | 351 | if mctx.AConfig().EmbeddedInMake() && !c.Host() { |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 352 | modules[0].(*Module).sanitize.Properties.InData = true |
| 353 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 354 | } else if c.sanitize.Properties.SanitizeDep { |
Colin Cross | b36ab1a | 2016-05-25 12:35:53 -0700 | [diff] [blame] | 355 | if c.Host() { |
| 356 | modules := mctx.CreateVariations(t.String()) |
| 357 | modules[0].(*Module).sanitize.SetSanitizer(t, true) |
| 358 | modules[0].(*Module).sanitize.Properties.SanitizeDep = false |
| 359 | } else { |
| 360 | modules := mctx.CreateVariations("", t.String()) |
| 361 | modules[0].(*Module).sanitize.SetSanitizer(t, false) |
| 362 | modules[1].(*Module).sanitize.SetSanitizer(t, true) |
| 363 | modules[0].(*Module).sanitize.Properties.SanitizeDep = false |
| 364 | modules[1].(*Module).sanitize.Properties.SanitizeDep = false |
| 365 | modules[1].(*Module).sanitize.Properties.InData = true |
| 366 | if mctx.AConfig().EmbeddedInMake() { |
| 367 | modules[0].(*Module).Properties.HideFromMake = true |
| 368 | } |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 369 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 370 | } |
| 371 | c.sanitize.Properties.SanitizeDep = false |
| 372 | } |
| 373 | } |
| 374 | } |