blob: 8d162e770a5ab092851d9136f4f9bb0689ac3be5 [file] [log] [blame]
Colin Cross16b23492016-01-06 14:41:07 -08001// 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
15package cc
16
17import (
18 "fmt"
19 "strings"
20
21 "github.com/google/blueprint"
22
Colin Cross635c3b02016-05-18 15:37:25 -070023 "android/soong/android"
Evgenii Stepanovaf36db12016-08-15 14:18:24 -070024 "android/soong/cc/config"
Colin Cross16b23492016-01-06 14:41:07 -080025)
26
Dan Willemsencbceaab2016-10-13 16:44:07 -070027const (
Dan Willemsen78ffeea2016-10-20 18:46:48 -070028 asanCflags = "-fno-omit-frame-pointer"
Dan Willemsencbceaab2016-10-13 16:44:07 -070029 asanLdflags = "-Wl,-u,__asan_preinit"
Dan Willemsen78ffeea2016-10-20 18:46:48 -070030 asanLibs = "libasan"
Dan Willemsencbceaab2016-10-13 16:44:07 -070031)
32
Colin Cross16b23492016-01-06 14:41:07 -080033type sanitizerType int
34
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070035func boolPtr(v bool) *bool {
36 if v {
37 return &v
38 } else {
39 return nil
40 }
41}
42
Colin Cross16b23492016-01-06 14:41:07 -080043const (
44 asan sanitizerType = iota + 1
45 tsan
46)
47
48func (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
59type SanitizeProperties struct {
60 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
61 Sanitize struct {
62 Never bool `android:"arch_variant"`
63
64 // main sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070065 Address *bool `android:"arch_variant"`
66 Thread *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080067
68 // local sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070069 Undefined *bool `android:"arch_variant"`
70 All_undefined *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080071 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070072 Coverage *bool `android:"arch_variant"`
73 Safestack *bool `android:"arch_variant"`
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070074 Cfi *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080075
Evgenii Stepanov1e405e12016-08-16 15:39:54 -070076 // 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 Cross16b23492016-01-06 14:41:07 -080085 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 Cross30d5f512016-05-03 18:02:42 -070093 InData bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -080094}
95
96type sanitize struct {
97 Properties SanitizeProperties
98}
99
100func (sanitize *sanitize) props() []interface{} {
101 return []interface{}{&sanitize.Properties}
102}
103
104func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700105 s := &sanitize.Properties.Sanitize
106
Colin Cross16b23492016-01-06 14:41:07 -0800107 // Don't apply sanitizers to NDK code.
108 if ctx.sdk() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700109 s.Never = true
Colin Cross16b23492016-01-06 14:41:07 -0800110 }
111
112 // Never always wins.
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700113 if s.Never {
Colin Cross16b23492016-01-06 14:41:07 -0800114 return
115 }
116
Colin Cross16b23492016-01-06 14:41:07 -0800117 var globalSanitizers []string
118 if ctx.clang() {
119 if ctx.Host() {
120 globalSanitizers = ctx.AConfig().SanitizeHost()
121 } else {
122 globalSanitizers = ctx.AConfig().SanitizeDevice()
123 }
124 }
125
Colin Cross16b23492016-01-06 14:41:07 -0800126 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000127 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700128 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
129 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000130 }
Colin Cross16b23492016-01-06 14:41:07 -0800131
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700132 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
133 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000134 }
135
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700136 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
137 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000138 }
139
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700140 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
141 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000142 }
143
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700144 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
145 s.Coverage = boolPtr(true)
146 }
147
148 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
149 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000150 }
151
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700152 if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
153 s.Cfi = boolPtr(true)
154 }
155
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000156 if len(globalSanitizers) > 0 {
157 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
158 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700159 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700160
161 if ctx.staticBinary() {
162 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700163 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700164 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800165 }
166
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700167 if Bool(s.All_undefined) {
168 s.Undefined = nil
169 }
170
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700171 if !ctx.toolchain().Is64Bit() {
172 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700173 s.Thread = nil
174 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800175 // TODO(ccross): error for compile_multilib = "32"?
176 }
177
Colin Cross3c344ef2016-07-18 15:44:56 -0700178 if Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) ||
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700179 Bool(s.Thread) || Bool(s.Coverage) || Bool(s.Safestack) || Bool(s.Cfi) {
Colin Cross3c344ef2016-07-18 15:44:56 -0700180 sanitize.Properties.SanitizerEnabled = true
181 }
182
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700183 if Bool(s.Coverage) {
184 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800185 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
186 }
187 }
188}
189
190func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
191 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
192 return deps
193 }
194
195 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700196 if Bool(sanitize.Properties.Sanitize.Address) {
Dan Willemsencbceaab2016-10-13 16:44:07 -0700197 deps.StaticLibs = append(deps.StaticLibs, asanLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800198 }
Colin Cross263abbd2016-07-15 13:10:48 -0700199 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
200 deps.SharedLibs = append(deps.SharedLibs, "libdl")
201 }
Colin Cross16b23492016-01-06 14:41:07 -0800202 }
203
204 return deps
205}
206
207func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
208 if !sanitize.Properties.SanitizerEnabled {
209 return flags
210 }
211
212 if !ctx.clang() {
213 ctx.ModuleErrorf("Use of sanitizers requires clang")
214 }
215
216 var sanitizers []string
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700217 var diagSanitizers []string
Colin Cross16b23492016-01-06 14:41:07 -0800218
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700219 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800220 sanitizers = append(sanitizers, "undefined")
221 if ctx.Device() {
222 ctx.ModuleErrorf("ubsan is not yet supported on the device")
223 }
224 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700225 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800226 sanitizers = append(sanitizers,
227 "bool",
228 "integer-divide-by-zero",
229 "return",
230 "returns-nonnull-attribute",
231 "shift-exponent",
232 "unreachable",
233 "vla-bound",
234 // TODO(danalbert): The following checks currently have compiler performance issues.
235 //"alignment",
236 //"bounds",
237 //"enum",
238 //"float-cast-overflow",
239 //"float-divide-by-zero",
240 //"nonnull-attribute",
241 //"null",
242 //"shift-base",
243 //"signed-integer-overflow",
244 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
245 // https://llvm.org/PR19302
246 // http://reviews.llvm.org/D6974
247 // "object-size",
248 )
249 }
250 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
251 }
252
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700253 if Bool(sanitize.Properties.Sanitize.Diag.Undefined) &&
254 (Bool(sanitize.Properties.Sanitize.All_undefined) ||
255 Bool(sanitize.Properties.Sanitize.Undefined) ||
256 len(sanitize.Properties.Sanitize.Misc_undefined) > 0) {
257 diagSanitizers = append(diagSanitizers, "undefined")
258 }
259
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700260 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700261 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800262 // Frame pointer based unwinder in ASan requires ARM frame setup.
263 // TODO: put in flags?
264 flags.RequiredInstructionSet = "arm"
265 }
Dan Willemsencbceaab2016-10-13 16:44:07 -0700266 flags.CFlags = append(flags.CFlags, asanCflags)
267 flags.LdFlags = append(flags.LdFlags, asanLdflags)
Colin Cross16b23492016-01-06 14:41:07 -0800268
269 // ASan runtime library must be the first in the link order.
Evgenii Stepanovaf36db12016-08-15 14:18:24 -0700270 runtimeLibrary := config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Colin Cross16b23492016-01-06 14:41:07 -0800271 if runtimeLibrary != "" {
Colin Crossb98c8b02016-07-29 13:44:28 -0700272 flags.libFlags = append([]string{"${config.ClangAsanLibDir}/" + runtimeLibrary}, flags.libFlags...)
Colin Cross16b23492016-01-06 14:41:07 -0800273 }
274 if ctx.Host() {
275 // -nodefaultlibs (provided with libc++) prevents the driver from linking
276 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
277 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
278 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
Colin Cross46974e22016-10-20 12:41:14 -0700279 // Host ASAN only links symbols in the final executable, so
280 // there will always be undefined symbols in intermediate libraries.
281 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800282 } else {
283 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
284 flags.DynamicLinker = "/system/bin/linker_asan"
285 if flags.Toolchain.Is64Bit() {
286 flags.DynamicLinker += "64"
287 }
288 }
289 sanitizers = append(sanitizers, "address")
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700290 diagSanitizers = append(diagSanitizers, "address")
Colin Cross16b23492016-01-06 14:41:07 -0800291 }
292
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700293 if Bool(sanitize.Properties.Sanitize.Coverage) {
Colin Cross16b23492016-01-06 14:41:07 -0800294 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp")
295 }
296
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700297 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700298 sanitizers = append(sanitizers, "safe-stack")
299 }
300
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700301 if Bool(sanitize.Properties.Sanitize.Cfi) {
302 sanitizers = append(sanitizers, "cfi")
303 cfiFlags := []string{"-flto", "-fsanitize=cfi", "-fsanitize-cfi-cross-dso"}
304 flags.CFlags = append(flags.CFlags, cfiFlags...)
305 flags.CFlags = append(flags.CFlags, "-fvisibility=default")
306 flags.LdFlags = append(flags.LdFlags, cfiFlags...)
307 // FIXME: revert the __cfi_check flag when clang is updated to r280031.
308 flags.LdFlags = append(flags.LdFlags, "-Wl,-plugin-opt,O1", "-Wl,-export-dynamic-symbol=__cfi_check")
309 if Bool(sanitize.Properties.Sanitize.Diag.Cfi) {
310 diagSanitizers = append(diagSanitizers, "cfi")
311 }
312 }
313
Colin Cross16b23492016-01-06 14:41:07 -0800314 if sanitize.Properties.Sanitize.Recover != nil {
315 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
316 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
317 }
318
319 if len(sanitizers) > 0 {
320 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
321 flags.CFlags = append(flags.CFlags, sanitizeArg)
322 if ctx.Host() {
323 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
324 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
325 flags.LdFlags = append(flags.LdFlags, "-lrt", "-ldl")
326 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700327 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
Colin Cross16b23492016-01-06 14:41:07 -0800328 }
329 }
330
Evgenii Stepanov1e405e12016-08-16 15:39:54 -0700331 if len(diagSanitizers) > 0 {
332 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(diagSanitizers, ","))
333 }
334 // FIXME: enable RTTI if diag + (cfi or vptr)
335
336 // Link a runtime library if needed.
337 runtimeLibrary := ""
338 if Bool(sanitize.Properties.Sanitize.Address) {
339 runtimeLibrary = config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
340 } else if len(diagSanitizers) > 0 {
341 runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(ctx.toolchain())
342 }
343
344 // ASan runtime library must be the first in the link order.
345 if runtimeLibrary != "" {
346 flags.libFlags = append([]string{"${config.ClangAsanLibDir}/" + runtimeLibrary}, flags.libFlags...)
347 }
348
Colin Cross635c3b02016-05-18 15:37:25 -0700349 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800350 if blacklist.Valid() {
351 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
352 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
353 }
354
355 return flags
356}
357
Colin Cross30d5f512016-05-03 18:02:42 -0700358func (sanitize *sanitize) inData() bool {
359 return sanitize.Properties.InData
360}
361
Colin Cross16b23492016-01-06 14:41:07 -0800362func (sanitize *sanitize) Sanitizer(t sanitizerType) bool {
363 if sanitize == nil {
364 return false
365 }
366
367 switch t {
368 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700369 return Bool(sanitize.Properties.Sanitize.Address)
Colin Cross16b23492016-01-06 14:41:07 -0800370 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700371 return Bool(sanitize.Properties.Sanitize.Thread)
Colin Cross16b23492016-01-06 14:41:07 -0800372 default:
373 panic(fmt.Errorf("unknown sanitizerType %d", t))
374 }
375}
376
377func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
378 switch t {
379 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700380 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700381 if !b {
382 sanitize.Properties.Sanitize.Coverage = nil
383 }
Colin Cross16b23492016-01-06 14:41:07 -0800384 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700385 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800386 default:
387 panic(fmt.Errorf("unknown sanitizerType %d", t))
388 }
389 if b {
390 sanitize.Properties.SanitizerEnabled = true
391 }
392}
393
394// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700395func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
396 return func(mctx android.TopDownMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800397 if c, ok := mctx.Module().(*Module); ok && c.sanitize.Sanitizer(t) {
398 mctx.VisitDepsDepthFirst(func(module blueprint.Module) {
399 if d, ok := mctx.Module().(*Module); ok && c.sanitize != nil &&
400 !c.sanitize.Properties.Sanitize.Never {
401 d.sanitize.Properties.SanitizeDep = true
402 }
403 })
404 }
405 }
406}
407
408// Create asan variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700409func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
410 return func(mctx android.BottomUpMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800411 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Colin Crossb916a382016-07-29 17:28:03 -0700412 if c.isDependencyRoot() && c.sanitize.Sanitizer(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700413 modules := mctx.CreateVariations(t.String())
414 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Colin Cross16b23492016-01-06 14:41:07 -0800415 } else if c.sanitize.Properties.SanitizeDep {
Colin Crossb0f28952016-09-19 16:46:53 -0700416 modules := mctx.CreateVariations("", t.String())
417 modules[0].(*Module).sanitize.SetSanitizer(t, false)
418 modules[1].(*Module).sanitize.SetSanitizer(t, true)
419 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
420 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
421 if mctx.Device() {
Colin Crossb36ab1a2016-05-25 12:35:53 -0700422 modules[1].(*Module).sanitize.Properties.InData = true
Colin Crossb0f28952016-09-19 16:46:53 -0700423 } else {
424 modules[0].(*Module).Properties.PreventInstall = true
425 }
426 if mctx.AConfig().EmbeddedInMake() {
427 modules[0].(*Module).Properties.HideFromMake = true
Colin Cross30d5f512016-05-03 18:02:42 -0700428 }
Colin Cross16b23492016-01-06 14:41:07 -0800429 }
430 c.sanitize.Properties.SanitizeDep = false
431 }
432 }
433}