blob: d9e28ccb5d5cf93d1799dd2ae54cc43bf3950b01 [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"`
Colin Cross16b23492016-01-06 14:41:07 -080074
75 // value to pass to -fsantitize-recover=
76 Recover []string
77
78 // value to pass to -fsanitize-blacklist
79 Blacklist *string
80 } `android:"arch_variant"`
81
82 SanitizerEnabled bool `blueprint:"mutated"`
83 SanitizeDep bool `blueprint:"mutated"`
Colin Cross30d5f512016-05-03 18:02:42 -070084 InData bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -080085}
86
87type sanitize struct {
88 Properties SanitizeProperties
89}
90
91func (sanitize *sanitize) props() []interface{} {
92 return []interface{}{&sanitize.Properties}
93}
94
95func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070096 s := &sanitize.Properties.Sanitize
97
Colin Cross16b23492016-01-06 14:41:07 -080098 // Don't apply sanitizers to NDK code.
99 if ctx.sdk() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700100 s.Never = true
Colin Cross16b23492016-01-06 14:41:07 -0800101 }
102
103 // Never always wins.
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700104 if s.Never {
Colin Cross16b23492016-01-06 14:41:07 -0800105 return
106 }
107
Colin Cross16b23492016-01-06 14:41:07 -0800108 var globalSanitizers []string
109 if ctx.clang() {
110 if ctx.Host() {
111 globalSanitizers = ctx.AConfig().SanitizeHost()
112 } else {
113 globalSanitizers = ctx.AConfig().SanitizeDevice()
114 }
115 }
116
Colin Cross16b23492016-01-06 14:41:07 -0800117 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000118 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700119 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
120 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000121 }
Colin Cross16b23492016-01-06 14:41:07 -0800122
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700123 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
124 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000125 }
126
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700127 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
128 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000129 }
130
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700131 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
132 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000133 }
134
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700135 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
136 s.Coverage = boolPtr(true)
137 }
138
139 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
140 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000141 }
142
143 if len(globalSanitizers) > 0 {
144 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
145 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700146 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700147
148 if ctx.staticBinary() {
149 s.Address = nil
Colin Cross91169fe2016-08-11 15:54:20 -0700150 s.Coverage = nil
Colin Cross3c344ef2016-07-18 15:44:56 -0700151 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800152 }
153
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700154 if Bool(s.All_undefined) {
155 s.Undefined = nil
156 }
157
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700158 if !ctx.toolchain().Is64Bit() {
159 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700160 s.Thread = nil
161 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800162 // TODO(ccross): error for compile_multilib = "32"?
163 }
164
Colin Cross3c344ef2016-07-18 15:44:56 -0700165 if Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) ||
166 Bool(s.Thread) || Bool(s.Coverage) || Bool(s.Safestack) {
167 sanitize.Properties.SanitizerEnabled = true
168 }
169
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700170 if Bool(s.Coverage) {
171 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800172 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
173 }
174 }
175}
176
177func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
178 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
179 return deps
180 }
181
182 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700183 if Bool(sanitize.Properties.Sanitize.Address) {
Dan Willemsencbceaab2016-10-13 16:44:07 -0700184 deps.StaticLibs = append(deps.StaticLibs, asanLibs)
Colin Cross16b23492016-01-06 14:41:07 -0800185 }
Colin Cross263abbd2016-07-15 13:10:48 -0700186 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
187 deps.SharedLibs = append(deps.SharedLibs, "libdl")
188 }
Colin Cross16b23492016-01-06 14:41:07 -0800189 }
190
191 return deps
192}
193
194func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
195 if !sanitize.Properties.SanitizerEnabled {
196 return flags
197 }
198
199 if !ctx.clang() {
200 ctx.ModuleErrorf("Use of sanitizers requires clang")
201 }
202
203 var sanitizers []string
204
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700205 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800206 sanitizers = append(sanitizers, "undefined")
207 if ctx.Device() {
208 ctx.ModuleErrorf("ubsan is not yet supported on the device")
209 }
210 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700211 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800212 sanitizers = append(sanitizers,
213 "bool",
214 "integer-divide-by-zero",
215 "return",
216 "returns-nonnull-attribute",
217 "shift-exponent",
218 "unreachable",
219 "vla-bound",
220 // TODO(danalbert): The following checks currently have compiler performance issues.
221 //"alignment",
222 //"bounds",
223 //"enum",
224 //"float-cast-overflow",
225 //"float-divide-by-zero",
226 //"nonnull-attribute",
227 //"null",
228 //"shift-base",
229 //"signed-integer-overflow",
230 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
231 // https://llvm.org/PR19302
232 // http://reviews.llvm.org/D6974
233 // "object-size",
234 )
235 }
236 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
237 }
238
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700239 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700240 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800241 // Frame pointer based unwinder in ASan requires ARM frame setup.
242 // TODO: put in flags?
243 flags.RequiredInstructionSet = "arm"
244 }
Dan Willemsencbceaab2016-10-13 16:44:07 -0700245 flags.CFlags = append(flags.CFlags, asanCflags)
246 flags.LdFlags = append(flags.LdFlags, asanLdflags)
Colin Cross16b23492016-01-06 14:41:07 -0800247
248 // ASan runtime library must be the first in the link order.
Evgenii Stepanovaf36db12016-08-15 14:18:24 -0700249 runtimeLibrary := config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
Colin Cross16b23492016-01-06 14:41:07 -0800250 if runtimeLibrary != "" {
Colin Crossb98c8b02016-07-29 13:44:28 -0700251 flags.libFlags = append([]string{"${config.ClangAsanLibDir}/" + runtimeLibrary}, flags.libFlags...)
Colin Cross16b23492016-01-06 14:41:07 -0800252 }
253 if ctx.Host() {
254 // -nodefaultlibs (provided with libc++) prevents the driver from linking
255 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
256 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
257 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
Colin Cross46974e22016-10-20 12:41:14 -0700258 // Host ASAN only links symbols in the final executable, so
259 // there will always be undefined symbols in intermediate libraries.
260 _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
Colin Cross16b23492016-01-06 14:41:07 -0800261 } else {
262 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
263 flags.DynamicLinker = "/system/bin/linker_asan"
264 if flags.Toolchain.Is64Bit() {
265 flags.DynamicLinker += "64"
266 }
267 }
268 sanitizers = append(sanitizers, "address")
269 }
270
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700271 if Bool(sanitize.Properties.Sanitize.Coverage) {
Colin Cross16b23492016-01-06 14:41:07 -0800272 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp")
273 }
274
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700275 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700276 sanitizers = append(sanitizers, "safe-stack")
277 }
278
Colin Cross16b23492016-01-06 14:41:07 -0800279 if sanitize.Properties.Sanitize.Recover != nil {
280 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
281 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
282 }
283
284 if len(sanitizers) > 0 {
285 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
286 flags.CFlags = append(flags.CFlags, sanitizeArg)
287 if ctx.Host() {
288 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
289 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
290 flags.LdFlags = append(flags.LdFlags, "-lrt", "-ldl")
291 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700292 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
293 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
294 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap=address,thread")
Colin Cross16b23492016-01-06 14:41:07 -0800295 }
296 }
297 }
298
Colin Cross635c3b02016-05-18 15:37:25 -0700299 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800300 if blacklist.Valid() {
301 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
302 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
303 }
304
305 return flags
306}
307
Colin Cross30d5f512016-05-03 18:02:42 -0700308func (sanitize *sanitize) inData() bool {
309 return sanitize.Properties.InData
310}
311
Colin Cross16b23492016-01-06 14:41:07 -0800312func (sanitize *sanitize) Sanitizer(t sanitizerType) bool {
313 if sanitize == nil {
314 return false
315 }
316
317 switch t {
318 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700319 return Bool(sanitize.Properties.Sanitize.Address)
Colin Cross16b23492016-01-06 14:41:07 -0800320 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700321 return Bool(sanitize.Properties.Sanitize.Thread)
Colin Cross16b23492016-01-06 14:41:07 -0800322 default:
323 panic(fmt.Errorf("unknown sanitizerType %d", t))
324 }
325}
326
327func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
328 switch t {
329 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700330 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross91169fe2016-08-11 15:54:20 -0700331 if !b {
332 sanitize.Properties.Sanitize.Coverage = nil
333 }
Colin Cross16b23492016-01-06 14:41:07 -0800334 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700335 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800336 default:
337 panic(fmt.Errorf("unknown sanitizerType %d", t))
338 }
339 if b {
340 sanitize.Properties.SanitizerEnabled = true
341 }
342}
343
344// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700345func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
346 return func(mctx android.TopDownMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800347 if c, ok := mctx.Module().(*Module); ok && c.sanitize.Sanitizer(t) {
348 mctx.VisitDepsDepthFirst(func(module blueprint.Module) {
349 if d, ok := mctx.Module().(*Module); ok && c.sanitize != nil &&
350 !c.sanitize.Properties.Sanitize.Never {
351 d.sanitize.Properties.SanitizeDep = true
352 }
353 })
354 }
355 }
356}
357
358// Create asan variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700359func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
360 return func(mctx android.BottomUpMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800361 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Colin Crossb916a382016-07-29 17:28:03 -0700362 if c.isDependencyRoot() && c.sanitize.Sanitizer(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700363 modules := mctx.CreateVariations(t.String())
364 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Colin Cross16b23492016-01-06 14:41:07 -0800365 } else if c.sanitize.Properties.SanitizeDep {
Colin Crossb0f28952016-09-19 16:46:53 -0700366 modules := mctx.CreateVariations("", t.String())
367 modules[0].(*Module).sanitize.SetSanitizer(t, false)
368 modules[1].(*Module).sanitize.SetSanitizer(t, true)
369 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
370 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
371 if mctx.Device() {
Colin Crossb36ab1a2016-05-25 12:35:53 -0700372 modules[1].(*Module).sanitize.Properties.InData = true
Colin Crossb0f28952016-09-19 16:46:53 -0700373 } else {
374 modules[0].(*Module).Properties.PreventInstall = true
375 }
376 if mctx.AConfig().EmbeddedInMake() {
377 modules[0].(*Module).Properties.HideFromMake = true
Colin Cross30d5f512016-05-03 18:02:42 -0700378 }
Colin Cross16b23492016-01-06 14:41:07 -0800379 }
380 c.sanitize.Properties.SanitizeDep = false
381 }
382 }
383}