blob: fb7cb37167ff6a0a16a9933e020f3af106e3d6b5 [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"
Colin Cross16b23492016-01-06 14:41:07 -080024)
25
26type sanitizerType int
27
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070028func boolPtr(v bool) *bool {
29 if v {
30 return &v
31 } else {
32 return nil
33 }
34}
35
Colin Cross16b23492016-01-06 14:41:07 -080036const (
37 asan sanitizerType = iota + 1
38 tsan
39)
40
41func (t sanitizerType) String() string {
42 switch t {
43 case asan:
44 return "asan"
45 case tsan:
46 return "tsan"
47 default:
48 panic(fmt.Errorf("unknown sanitizerType %d", t))
49 }
50}
51
52type SanitizeProperties struct {
53 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
54 Sanitize struct {
55 Never bool `android:"arch_variant"`
56
57 // main sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070058 Address *bool `android:"arch_variant"`
59 Thread *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080060
61 // local sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070062 Undefined *bool `android:"arch_variant"`
63 All_undefined *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080064 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070065 Coverage *bool `android:"arch_variant"`
66 Safestack *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080067
68 // value to pass to -fsantitize-recover=
69 Recover []string
70
71 // value to pass to -fsanitize-blacklist
72 Blacklist *string
73 } `android:"arch_variant"`
74
75 SanitizerEnabled bool `blueprint:"mutated"`
76 SanitizeDep bool `blueprint:"mutated"`
Colin Cross30d5f512016-05-03 18:02:42 -070077 InData bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -080078}
79
80type sanitize struct {
81 Properties SanitizeProperties
82}
83
84func (sanitize *sanitize) props() []interface{} {
85 return []interface{}{&sanitize.Properties}
86}
87
88func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070089 s := &sanitize.Properties.Sanitize
90
Colin Cross16b23492016-01-06 14:41:07 -080091 // Don't apply sanitizers to NDK code.
92 if ctx.sdk() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070093 s.Never = true
Colin Cross16b23492016-01-06 14:41:07 -080094 }
95
96 // Never always wins.
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070097 if s.Never {
Colin Cross16b23492016-01-06 14:41:07 -080098 return
99 }
100
Colin Cross16b23492016-01-06 14:41:07 -0800101 var globalSanitizers []string
102 if ctx.clang() {
103 if ctx.Host() {
104 globalSanitizers = ctx.AConfig().SanitizeHost()
105 } else {
106 globalSanitizers = ctx.AConfig().SanitizeDevice()
107 }
108 }
109
Colin Cross16b23492016-01-06 14:41:07 -0800110 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000111 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700112 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
113 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000114 }
Colin Cross16b23492016-01-06 14:41:07 -0800115
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700116 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
117 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000118 }
119
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700120 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
121 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000122 }
123
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700124 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
125 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000126 }
127
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700128 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
129 s.Coverage = boolPtr(true)
130 }
131
132 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
133 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000134 }
135
136 if len(globalSanitizers) > 0 {
137 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
138 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700139 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700140
141 if ctx.staticBinary() {
142 s.Address = nil
143 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800144 }
145
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700146 if Bool(s.All_undefined) {
147 s.Undefined = nil
148 }
149
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700150 if !ctx.toolchain().Is64Bit() {
151 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700152 s.Thread = nil
153 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800154 // TODO(ccross): error for compile_multilib = "32"?
155 }
156
Colin Cross3c344ef2016-07-18 15:44:56 -0700157 if Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) ||
158 Bool(s.Thread) || Bool(s.Coverage) || Bool(s.Safestack) {
159 sanitize.Properties.SanitizerEnabled = true
160 }
161
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700162 if Bool(s.Coverage) {
163 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800164 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
165 }
166 }
167}
168
169func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
170 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
171 return deps
172 }
173
174 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700175 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800176 deps.StaticLibs = append(deps.StaticLibs, "libasan")
177 }
Colin Cross263abbd2016-07-15 13:10:48 -0700178 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
179 deps.SharedLibs = append(deps.SharedLibs, "libdl")
180 }
Colin Cross16b23492016-01-06 14:41:07 -0800181 }
182
183 return deps
184}
185
186func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
187 if !sanitize.Properties.SanitizerEnabled {
188 return flags
189 }
190
191 if !ctx.clang() {
192 ctx.ModuleErrorf("Use of sanitizers requires clang")
193 }
194
195 var sanitizers []string
196
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700197 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800198 sanitizers = append(sanitizers, "undefined")
199 if ctx.Device() {
200 ctx.ModuleErrorf("ubsan is not yet supported on the device")
201 }
202 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700203 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800204 sanitizers = append(sanitizers,
205 "bool",
206 "integer-divide-by-zero",
207 "return",
208 "returns-nonnull-attribute",
209 "shift-exponent",
210 "unreachable",
211 "vla-bound",
212 // TODO(danalbert): The following checks currently have compiler performance issues.
213 //"alignment",
214 //"bounds",
215 //"enum",
216 //"float-cast-overflow",
217 //"float-divide-by-zero",
218 //"nonnull-attribute",
219 //"null",
220 //"shift-base",
221 //"signed-integer-overflow",
222 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
223 // https://llvm.org/PR19302
224 // http://reviews.llvm.org/D6974
225 // "object-size",
226 )
227 }
228 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
229 }
230
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700231 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700232 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800233 // Frame pointer based unwinder in ASan requires ARM frame setup.
234 // TODO: put in flags?
235 flags.RequiredInstructionSet = "arm"
236 }
237 flags.CFlags = append(flags.CFlags, "-fno-omit-frame-pointer")
238 flags.LdFlags = append(flags.LdFlags, "-Wl,-u,__asan_preinit")
239
240 // ASan runtime library must be the first in the link order.
241 runtimeLibrary := ctx.toolchain().AddressSanitizerRuntimeLibrary()
242 if runtimeLibrary != "" {
Colin Crossb98c8b02016-07-29 13:44:28 -0700243 flags.libFlags = append([]string{"${config.ClangAsanLibDir}/" + runtimeLibrary}, flags.libFlags...)
Colin Cross16b23492016-01-06 14:41:07 -0800244 }
245 if ctx.Host() {
246 // -nodefaultlibs (provided with libc++) prevents the driver from linking
247 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
248 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
249 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
250 } else {
251 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
252 flags.DynamicLinker = "/system/bin/linker_asan"
253 if flags.Toolchain.Is64Bit() {
254 flags.DynamicLinker += "64"
255 }
256 }
257 sanitizers = append(sanitizers, "address")
258 }
259
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700260 if Bool(sanitize.Properties.Sanitize.Coverage) {
Colin Cross16b23492016-01-06 14:41:07 -0800261 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp")
262 }
263
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700264 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700265 sanitizers = append(sanitizers, "safe-stack")
266 }
267
Colin Cross16b23492016-01-06 14:41:07 -0800268 if sanitize.Properties.Sanitize.Recover != nil {
269 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
270 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
271 }
272
273 if len(sanitizers) > 0 {
274 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
275 flags.CFlags = append(flags.CFlags, sanitizeArg)
276 if ctx.Host() {
277 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
278 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
279 flags.LdFlags = append(flags.LdFlags, "-lrt", "-ldl")
280 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700281 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
282 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
283 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap=address,thread")
Colin Cross16b23492016-01-06 14:41:07 -0800284 }
285 }
286 }
287
Colin Cross635c3b02016-05-18 15:37:25 -0700288 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800289 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 Cross30d5f512016-05-03 18:02:42 -0700297func (sanitize *sanitize) inData() bool {
298 return sanitize.Properties.InData
299}
300
Colin Cross16b23492016-01-06 14:41:07 -0800301func (sanitize *sanitize) Sanitizer(t sanitizerType) bool {
302 if sanitize == nil {
303 return false
304 }
305
306 switch t {
307 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700308 return Bool(sanitize.Properties.Sanitize.Address)
Colin Cross16b23492016-01-06 14:41:07 -0800309 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700310 return Bool(sanitize.Properties.Sanitize.Thread)
Colin Cross16b23492016-01-06 14:41:07 -0800311 default:
312 panic(fmt.Errorf("unknown sanitizerType %d", t))
313 }
314}
315
316func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
317 switch t {
318 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700319 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800320 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700321 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800322 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 Cross635c3b02016-05-18 15:37:25 -0700331func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
332 return func(mctx android.TopDownMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800333 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 Cross635c3b02016-05-18 15:37:25 -0700345func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
346 return func(mctx android.BottomUpMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800347 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
Colin Crossb916a382016-07-29 17:28:03 -0700348 if c.isDependencyRoot() && c.sanitize.Sanitizer(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700349 modules := mctx.CreateVariations(t.String())
350 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Colin Crossb36ab1a2016-05-25 12:35:53 -0700351 if mctx.AConfig().EmbeddedInMake() && !c.Host() {
Colin Cross30d5f512016-05-03 18:02:42 -0700352 modules[0].(*Module).sanitize.Properties.InData = true
353 }
Colin Cross16b23492016-01-06 14:41:07 -0800354 } else if c.sanitize.Properties.SanitizeDep {
Colin Crossb36ab1a2016-05-25 12:35:53 -0700355 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 Cross30d5f512016-05-03 18:02:42 -0700369 }
Colin Cross16b23492016-01-06 14:41:07 -0800370 }
371 c.sanitize.Properties.SanitizeDep = false
372 }
373 }
374}