blob: 08ffff4227d78999c57d7d75abebf2110b5920f1 [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 -080036func init() {
37 pctx.StaticVariable("clangAsanLibDir", "${clangPath}/lib64/clang/3.8/lib/linux")
38}
39
40const (
41 asan sanitizerType = iota + 1
42 tsan
43)
44
45func (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
56type SanitizeProperties struct {
57 // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer
58 Sanitize struct {
59 Never bool `android:"arch_variant"`
60
61 // main sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070062 Address *bool `android:"arch_variant"`
63 Thread *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080064
65 // local sanitizers
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070066 Undefined *bool `android:"arch_variant"`
67 All_undefined *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080068 Misc_undefined []string `android:"arch_variant"`
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070069 Coverage *bool `android:"arch_variant"`
70 Safestack *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -080071
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 Cross30d5f512016-05-03 18:02:42 -070081 InData bool `blueprint:"mutated"`
Colin Cross16b23492016-01-06 14:41:07 -080082}
83
84type sanitize struct {
85 Properties SanitizeProperties
86}
87
88func (sanitize *sanitize) props() []interface{} {
89 return []interface{}{&sanitize.Properties}
90}
91
92func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070093 s := &sanitize.Properties.Sanitize
94
Colin Cross16b23492016-01-06 14:41:07 -080095 // Don't apply sanitizers to NDK code.
96 if ctx.sdk() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -070097 s.Never = true
Colin Cross16b23492016-01-06 14:41:07 -080098 }
99
100 // Never always wins.
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700101 if s.Never {
Colin Cross16b23492016-01-06 14:41:07 -0800102 return
103 }
104
Colin Cross16b23492016-01-06 14:41:07 -0800105 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
Colin Cross16b23492016-01-06 14:41:07 -0800114 if len(globalSanitizers) > 0 {
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000115 var found bool
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700116 if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
117 s.All_undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000118 }
Colin Cross16b23492016-01-06 14:41:07 -0800119
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700120 if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
121 s.Undefined = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000122 }
123
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700124 if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
125 s.Address = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000126 }
127
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700128 if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
129 s.Thread = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000130 }
131
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700132 if found, globalSanitizers = removeFromList("coverage", globalSanitizers); found && s.Coverage == nil {
133 s.Coverage = boolPtr(true)
134 }
135
136 if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
137 s.Safestack = boolPtr(true)
Evgenii Stepanov05bafd32016-07-07 17:38:41 +0000138 }
139
140 if len(globalSanitizers) > 0 {
141 ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
142 }
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700143 }
Colin Cross3c344ef2016-07-18 15:44:56 -0700144
145 if ctx.staticBinary() {
146 s.Address = nil
147 s.Thread = nil
Colin Cross16b23492016-01-06 14:41:07 -0800148 }
149
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700150 if Bool(s.All_undefined) {
151 s.Undefined = nil
152 }
153
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700154 if !ctx.toolchain().Is64Bit() {
155 // TSAN and SafeStack are not supported on 32-bit architectures
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700156 s.Thread = nil
157 s.Safestack = nil
Colin Cross16b23492016-01-06 14:41:07 -0800158 // TODO(ccross): error for compile_multilib = "32"?
159 }
160
Colin Cross3c344ef2016-07-18 15:44:56 -0700161 if Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) ||
162 Bool(s.Thread) || Bool(s.Coverage) || Bool(s.Safestack) {
163 sanitize.Properties.SanitizerEnabled = true
164 }
165
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700166 if Bool(s.Coverage) {
167 if !Bool(s.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800168 ctx.ModuleErrorf(`Use of "coverage" also requires "address"`)
169 }
170 }
171}
172
173func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
174 if !sanitize.Properties.SanitizerEnabled { // || c.static() {
175 return deps
176 }
177
178 if ctx.Device() {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700179 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross16b23492016-01-06 14:41:07 -0800180 deps.StaticLibs = append(deps.StaticLibs, "libasan")
181 }
Colin Cross263abbd2016-07-15 13:10:48 -0700182 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
183 deps.SharedLibs = append(deps.SharedLibs, "libdl")
184 }
Colin Cross16b23492016-01-06 14:41:07 -0800185 }
186
187 return deps
188}
189
190func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
191 if !sanitize.Properties.SanitizerEnabled {
192 return flags
193 }
194
195 if !ctx.clang() {
196 ctx.ModuleErrorf("Use of sanitizers requires clang")
197 }
198
199 var sanitizers []string
200
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700201 if Bool(sanitize.Properties.Sanitize.All_undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800202 sanitizers = append(sanitizers, "undefined")
203 if ctx.Device() {
204 ctx.ModuleErrorf("ubsan is not yet supported on the device")
205 }
206 } else {
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700207 if Bool(sanitize.Properties.Sanitize.Undefined) {
Colin Cross16b23492016-01-06 14:41:07 -0800208 sanitizers = append(sanitizers,
209 "bool",
210 "integer-divide-by-zero",
211 "return",
212 "returns-nonnull-attribute",
213 "shift-exponent",
214 "unreachable",
215 "vla-bound",
216 // TODO(danalbert): The following checks currently have compiler performance issues.
217 //"alignment",
218 //"bounds",
219 //"enum",
220 //"float-cast-overflow",
221 //"float-divide-by-zero",
222 //"nonnull-attribute",
223 //"null",
224 //"shift-base",
225 //"signed-integer-overflow",
226 // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
227 // https://llvm.org/PR19302
228 // http://reviews.llvm.org/D6974
229 // "object-size",
230 )
231 }
232 sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
233 }
234
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700235 if Bool(sanitize.Properties.Sanitize.Address) {
Colin Cross635c3b02016-05-18 15:37:25 -0700236 if ctx.Arch().ArchType == android.Arm {
Colin Cross16b23492016-01-06 14:41:07 -0800237 // Frame pointer based unwinder in ASan requires ARM frame setup.
238 // TODO: put in flags?
239 flags.RequiredInstructionSet = "arm"
240 }
241 flags.CFlags = append(flags.CFlags, "-fno-omit-frame-pointer")
242 flags.LdFlags = append(flags.LdFlags, "-Wl,-u,__asan_preinit")
243
244 // ASan runtime library must be the first in the link order.
245 runtimeLibrary := ctx.toolchain().AddressSanitizerRuntimeLibrary()
246 if runtimeLibrary != "" {
247 flags.libFlags = append([]string{"${clangAsanLibDir}/" + runtimeLibrary}, flags.libFlags...)
248 }
249 if ctx.Host() {
250 // -nodefaultlibs (provided with libc++) prevents the driver from linking
251 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
252 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
253 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
254 } else {
255 flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
256 flags.DynamicLinker = "/system/bin/linker_asan"
257 if flags.Toolchain.Is64Bit() {
258 flags.DynamicLinker += "64"
259 }
260 }
261 sanitizers = append(sanitizers, "address")
262 }
263
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700264 if Bool(sanitize.Properties.Sanitize.Coverage) {
Colin Cross16b23492016-01-06 14:41:07 -0800265 flags.CFlags = append(flags.CFlags, "-fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp")
266 }
267
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700268 if Bool(sanitize.Properties.Sanitize.Safestack) {
Evgenii Stepanov0a8a0d02016-05-12 13:54:53 -0700269 sanitizers = append(sanitizers, "safe-stack")
270 }
271
Colin Cross16b23492016-01-06 14:41:07 -0800272 if sanitize.Properties.Sanitize.Recover != nil {
273 flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+
274 strings.Join(sanitize.Properties.Sanitize.Recover, ","))
275 }
276
277 if len(sanitizers) > 0 {
278 sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
279 flags.CFlags = append(flags.CFlags, sanitizeArg)
280 if ctx.Host() {
281 flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
282 flags.LdFlags = append(flags.LdFlags, sanitizeArg)
283 flags.LdFlags = append(flags.LdFlags, "-lrt", "-ldl")
284 } else {
Colin Cross263abbd2016-07-15 13:10:48 -0700285 flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
286 if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
287 flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap=address,thread")
Colin Cross16b23492016-01-06 14:41:07 -0800288 }
289 }
290 }
291
Colin Cross635c3b02016-05-18 15:37:25 -0700292 blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist)
Colin Cross16b23492016-01-06 14:41:07 -0800293 if blacklist.Valid() {
294 flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String())
295 flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path())
296 }
297
298 return flags
299}
300
Colin Cross30d5f512016-05-03 18:02:42 -0700301func (sanitize *sanitize) inData() bool {
302 return sanitize.Properties.InData
303}
304
Colin Cross16b23492016-01-06 14:41:07 -0800305func (sanitize *sanitize) Sanitizer(t sanitizerType) bool {
306 if sanitize == nil {
307 return false
308 }
309
310 switch t {
311 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700312 return Bool(sanitize.Properties.Sanitize.Address)
Colin Cross16b23492016-01-06 14:41:07 -0800313 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700314 return Bool(sanitize.Properties.Sanitize.Thread)
Colin Cross16b23492016-01-06 14:41:07 -0800315 default:
316 panic(fmt.Errorf("unknown sanitizerType %d", t))
317 }
318}
319
320func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) {
321 switch t {
322 case asan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700323 sanitize.Properties.Sanitize.Address = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800324 case tsan:
Evgenii Stepanovfcfe56d2016-07-07 10:54:07 -0700325 sanitize.Properties.Sanitize.Thread = boolPtr(b)
Colin Cross16b23492016-01-06 14:41:07 -0800326 default:
327 panic(fmt.Errorf("unknown sanitizerType %d", t))
328 }
329 if b {
330 sanitize.Properties.SanitizerEnabled = true
331 }
332}
333
334// Propagate asan requirements down from binaries
Colin Cross635c3b02016-05-18 15:37:25 -0700335func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) {
336 return func(mctx android.TopDownMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800337 if c, ok := mctx.Module().(*Module); ok && c.sanitize.Sanitizer(t) {
338 mctx.VisitDepsDepthFirst(func(module blueprint.Module) {
339 if d, ok := mctx.Module().(*Module); ok && c.sanitize != nil &&
340 !c.sanitize.Properties.Sanitize.Never {
341 d.sanitize.Properties.SanitizeDep = true
342 }
343 })
344 }
345 }
346}
347
348// Create asan variants for modules that need them
Colin Cross635c3b02016-05-18 15:37:25 -0700349func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) {
350 return func(mctx android.BottomUpMutatorContext) {
Colin Cross16b23492016-01-06 14:41:07 -0800351 if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
352 if d, ok := c.linker.(baseLinkerInterface); ok && d.isDependencyRoot() && c.sanitize.Sanitizer(t) {
Colin Cross30d5f512016-05-03 18:02:42 -0700353 modules := mctx.CreateVariations(t.String())
354 modules[0].(*Module).sanitize.SetSanitizer(t, true)
Colin Crossb36ab1a2016-05-25 12:35:53 -0700355 if mctx.AConfig().EmbeddedInMake() && !c.Host() {
Colin Cross30d5f512016-05-03 18:02:42 -0700356 modules[0].(*Module).sanitize.Properties.InData = true
357 }
Colin Cross16b23492016-01-06 14:41:07 -0800358 } else if c.sanitize.Properties.SanitizeDep {
Colin Crossb36ab1a2016-05-25 12:35:53 -0700359 if c.Host() {
360 modules := mctx.CreateVariations(t.String())
361 modules[0].(*Module).sanitize.SetSanitizer(t, true)
362 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
363 } else {
364 modules := mctx.CreateVariations("", t.String())
365 modules[0].(*Module).sanitize.SetSanitizer(t, false)
366 modules[1].(*Module).sanitize.SetSanitizer(t, true)
367 modules[0].(*Module).sanitize.Properties.SanitizeDep = false
368 modules[1].(*Module).sanitize.Properties.SanitizeDep = false
369 modules[1].(*Module).sanitize.Properties.InData = true
370 if mctx.AConfig().EmbeddedInMake() {
371 modules[0].(*Module).Properties.HideFromMake = true
372 }
Colin Cross30d5f512016-05-03 18:02:42 -0700373 }
Colin Cross16b23492016-01-06 14:41:07 -0800374 }
375 c.sanitize.Properties.SanitizeDep = false
376 }
377 }
378}