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" |
Jeff Gaston | 7276539 | 2017-11-28 16:37:53 -0800 | [diff] [blame] | 19 | "sort" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 20 | "strings" |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 21 | "sync" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 22 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint" |
| 24 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 25 | "android/soong/android" |
Evgenii Stepanov | af36db1 | 2016-08-15 14:18:24 -0700 | [diff] [blame] | 26 | "android/soong/cc/config" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 29 | var ( |
| 30 | // Any C flags added by sanitizer which libTooling tools may not |
| 31 | // understand also need to be added to ClangLibToolingUnknownCflags in |
| 32 | // cc/config/clang.go |
Vishwath Mohan | f3918d3 | 2017-02-14 07:59:33 -0800 | [diff] [blame] | 33 | |
Yi Kong | 20233a4 | 2019-08-21 01:38:40 -0700 | [diff] [blame] | 34 | asanCflags = []string{ |
| 35 | "-fno-omit-frame-pointer", |
| 36 | "-fno-experimental-new-pass-manager", |
| 37 | } |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 38 | asanLdflags = []string{"-Wl,-u,__asan_preinit"} |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 39 | |
Peter Collingbourne | 967511a | 2019-03-19 21:39:54 -0700 | [diff] [blame] | 40 | hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=", |
Evgenii Stepanov | 1c69e83 | 2019-06-14 18:37:33 -0700 | [diff] [blame] | 41 | "-fsanitize-hwaddress-abi=platform", |
| 42 | "-fno-experimental-new-pass-manager"} |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 43 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 44 | cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso", |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 45 | "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"} |
Evgenii Stepanov | dbf1d4f | 2018-08-31 12:54:33 -0700 | [diff] [blame] | 46 | // -flto and -fvisibility are required by clang when -fsanitize=cfi is |
| 47 | // used, but have no effect on assembly files |
| 48 | cfiAsflags = []string{"-flto", "-fvisibility=default"} |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 49 | cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi", |
Pirama Arumuga Nainar | bdb17f0 | 2017-08-28 21:50:17 -0700 | [diff] [blame] | 50 | "-Wl,-plugin-opt,O1"} |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 51 | cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map" |
| 52 | cfiStaticLibsMutex sync.Mutex |
| 53 | hwasanStaticLibsMutex sync.Mutex |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 54 | |
Evgenii Stepanov | 98f5b06 | 2018-11-29 15:12:51 -0800 | [diff] [blame] | 55 | intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"} |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 56 | |
Peter Collingbourne | bd19db0 | 2019-03-06 10:38:48 -0800 | [diff] [blame] | 57 | minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined", |
Ivan Lozano | ae6ae1d | 2018-10-08 09:29:39 -0700 | [diff] [blame] | 58 | "-fno-sanitize-recover=integer,undefined"} |
Evgenii Stepanov | 2c6484e | 2019-05-15 12:49:54 -0700 | [diff] [blame] | 59 | hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512", |
| 60 | "export_memory_stats=0", "max_malloc_fill_size=0"} |
Dan Willemsen | cbceaab | 2016-10-13 16:44:07 -0700 | [diff] [blame] | 61 | ) |
| 62 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 63 | type sanitizerType int |
| 64 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 65 | func boolPtr(v bool) *bool { |
| 66 | if v { |
| 67 | return &v |
| 68 | } else { |
| 69 | return nil |
| 70 | } |
| 71 | } |
| 72 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 73 | const ( |
| 74 | asan sanitizerType = iota + 1 |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 75 | hwasan |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 76 | tsan |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 77 | intOverflow |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 78 | cfi |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 79 | scs |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 80 | fuzzer |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 81 | ) |
| 82 | |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 83 | // Name of the sanitizer variation for this sanitizer type |
| 84 | func (t sanitizerType) variationName() string { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 85 | switch t { |
| 86 | case asan: |
| 87 | return "asan" |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 88 | case hwasan: |
| 89 | return "hwasan" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 90 | case tsan: |
| 91 | return "tsan" |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 92 | case intOverflow: |
| 93 | return "intOverflow" |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 94 | case cfi: |
| 95 | return "cfi" |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 96 | case scs: |
| 97 | return "scs" |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 98 | case fuzzer: |
| 99 | return "fuzzer" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 100 | default: |
| 101 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 102 | } |
| 103 | } |
| 104 | |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 105 | // This is the sanitizer names in SANITIZE_[TARGET|HOST] |
| 106 | func (t sanitizerType) name() string { |
| 107 | switch t { |
| 108 | case asan: |
| 109 | return "address" |
| 110 | case hwasan: |
| 111 | return "hwaddress" |
| 112 | case tsan: |
| 113 | return "thread" |
| 114 | case intOverflow: |
| 115 | return "integer_overflow" |
| 116 | case cfi: |
| 117 | return "cfi" |
| 118 | case scs: |
| 119 | return "shadow-call-stack" |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 120 | case fuzzer: |
| 121 | return "fuzzer" |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 122 | default: |
| 123 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 124 | } |
| 125 | } |
| 126 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 127 | func (t sanitizerType) incompatibleWithCfi() bool { |
| 128 | return t == asan || t == fuzzer || t == hwasan |
| 129 | } |
| 130 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 131 | type SanitizeProperties struct { |
| 132 | // enable AddressSanitizer, ThreadSanitizer, or UndefinedBehaviorSanitizer |
| 133 | Sanitize struct { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 134 | Never *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 135 | |
| 136 | // main sanitizers |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 137 | Address *bool `android:"arch_variant"` |
| 138 | Thread *bool `android:"arch_variant"` |
| 139 | Hwaddress *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 140 | |
| 141 | // local sanitizers |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 142 | Undefined *bool `android:"arch_variant"` |
| 143 | All_undefined *bool `android:"arch_variant"` |
| 144 | Misc_undefined []string `android:"arch_variant"` |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 145 | Fuzzer *bool `android:"arch_variant"` |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 146 | Safestack *bool `android:"arch_variant"` |
| 147 | Cfi *bool `android:"arch_variant"` |
| 148 | Integer_overflow *bool `android:"arch_variant"` |
Kostya Kortchinsky | d18ae5c | 2018-06-12 14:46:54 -0700 | [diff] [blame] | 149 | Scudo *bool `android:"arch_variant"` |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 150 | Scs *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 151 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 152 | // Sanitizers to run in the diagnostic mode (as opposed to the release mode). |
| 153 | // Replaces abort() on error with a human-readable error message. |
| 154 | // Address and Thread sanitizers always run in diagnostic mode. |
| 155 | Diag struct { |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 156 | Undefined *bool `android:"arch_variant"` |
| 157 | Cfi *bool `android:"arch_variant"` |
| 158 | Integer_overflow *bool `android:"arch_variant"` |
| 159 | Misc_undefined []string `android:"arch_variant"` |
Ivan Lozano | 7929bba | 2018-12-12 09:36:31 -0800 | [diff] [blame] | 160 | No_recover []string |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // value to pass to -fsanitize-recover= |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 164 | Recover []string |
| 165 | |
| 166 | // value to pass to -fsanitize-blacklist |
| 167 | Blacklist *string |
| 168 | } `android:"arch_variant"` |
| 169 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 170 | SanitizerEnabled bool `blueprint:"mutated"` |
| 171 | SanitizeDep bool `blueprint:"mutated"` |
| 172 | MinimalRuntimeDep bool `blueprint:"mutated"` |
| 173 | UbsanRuntimeDep bool `blueprint:"mutated"` |
| 174 | InSanitizerDir bool `blueprint:"mutated"` |
| 175 | Sanitizers []string `blueprint:"mutated"` |
| 176 | DiagSanitizers []string `blueprint:"mutated"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | type sanitize struct { |
| 180 | Properties SanitizeProperties |
| 181 | } |
| 182 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 183 | func init() { |
| 184 | android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider) |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 185 | android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 188 | func (sanitize *sanitize) props() []interface{} { |
| 189 | return []interface{}{&sanitize.Properties} |
| 190 | } |
| 191 | |
| 192 | func (sanitize *sanitize) begin(ctx BaseModuleContext) { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 193 | s := &sanitize.Properties.Sanitize |
| 194 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 195 | // Don't apply sanitizers to NDK code. |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 196 | if ctx.useSdk() { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 197 | s.Never = BoolPtr(true) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Doug Horn | c32c6b0 | 2019-01-17 14:44:05 -0800 | [diff] [blame] | 200 | // Sanitizers do not work on Fuchsia yet. |
| 201 | if ctx.Fuchsia() { |
| 202 | s.Never = BoolPtr(true) |
| 203 | } |
| 204 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 205 | // Never always wins. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 206 | if Bool(s.Never) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 207 | return |
| 208 | } |
| 209 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 210 | var globalSanitizers []string |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 211 | var globalSanitizersDiag []string |
| 212 | |
Dan Willemsen | 8536d6b | 2018-10-07 20:54:34 -0700 | [diff] [blame] | 213 | if ctx.Host() { |
| 214 | if !ctx.Windows() { |
| 215 | globalSanitizers = ctx.Config().SanitizeHost() |
| 216 | } |
| 217 | } else { |
| 218 | arches := ctx.Config().SanitizeDeviceArch() |
| 219 | if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) { |
| 220 | globalSanitizers = ctx.Config().SanitizeDevice() |
| 221 | globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag() |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 225 | if len(globalSanitizers) > 0 { |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 226 | var found bool |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 227 | if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil { |
| 228 | s.All_undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 229 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 230 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 231 | if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil { |
| 232 | s.Undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 235 | if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil { |
| 236 | s.Address = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 239 | if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil { |
| 240 | s.Thread = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 243 | if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil { |
| 244 | s.Fuzzer = boolPtr(true) |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil { |
| 248 | s.Safestack = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 251 | if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 252 | if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) { |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 253 | s.Cfi = boolPtr(true) |
| 254 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 257 | // Global integer_overflow builds do not support static libraries. |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 258 | if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil { |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 259 | if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() { |
Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 260 | s.Integer_overflow = boolPtr(true) |
| 261 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Kostya Kortchinsky | d18ae5c | 2018-06-12 14:46:54 -0700 | [diff] [blame] | 264 | if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil { |
| 265 | s.Scudo = boolPtr(true) |
| 266 | } |
| 267 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 268 | if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil { |
| 269 | s.Hwaddress = boolPtr(true) |
| 270 | } |
| 271 | |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 272 | if len(globalSanitizers) > 0 { |
| 273 | ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0]) |
| 274 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 275 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 276 | // Global integer_overflow builds do not support static library diagnostics. |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 277 | if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found && |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 278 | s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() { |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 279 | s.Diag.Integer_overflow = boolPtr(true) |
| 280 | } |
| 281 | |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 282 | if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found && |
| 283 | s.Diag.Cfi == nil && Bool(s.Cfi) { |
| 284 | s.Diag.Cfi = boolPtr(true) |
| 285 | } |
| 286 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 287 | if len(globalSanitizersDiag) > 0 { |
| 288 | ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0]) |
| 289 | } |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 290 | } |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 291 | |
Vishwath Mohan | 1c54f66 | 2018-05-24 18:36:18 -0700 | [diff] [blame] | 292 | // Enable CFI for all components in the include paths (for Aarch64 only) |
| 293 | if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 { |
Vishwath Mohan | 3af8ee0 | 2018-03-30 02:55:23 +0000 | [diff] [blame] | 294 | s.Cfi = boolPtr(true) |
| 295 | if inList("cfi", ctx.Config().SanitizeDeviceDiag()) { |
| 296 | s.Diag.Cfi = boolPtr(true) |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Evgenii Stepanov | a83fdac | 2017-01-31 18:37:30 -0800 | [diff] [blame] | 300 | // CFI needs gold linker, and mips toolchain does not have one. |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 301 | if !ctx.Config().EnableCFI() || ctx.Arch().ArchType == android.Mips || ctx.Arch().ArchType == android.Mips64 { |
Vishwath Mohan | 1b017a7 | 2017-01-19 13:54:55 -0800 | [diff] [blame] | 302 | s.Cfi = nil |
| 303 | s.Diag.Cfi = nil |
| 304 | } |
| 305 | |
Vishwath Mohan | 6d67e6e | 2017-02-07 20:31:41 -0800 | [diff] [blame] | 306 | // Also disable CFI for arm32 until b/35157333 is fixed. |
| 307 | if ctx.Arch().ArchType == android.Arm { |
| 308 | s.Cfi = nil |
| 309 | s.Diag.Cfi = nil |
| 310 | } |
| 311 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 312 | // HWASan requires AArch64 hardware feature (top-byte-ignore). |
| 313 | if ctx.Arch().ArchType != android.Arm64 { |
| 314 | s.Hwaddress = nil |
| 315 | } |
| 316 | |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 317 | // SCS is only implemented on AArch64. |
Peter Collingbourne | bd19db0 | 2019-03-06 10:38:48 -0800 | [diff] [blame] | 318 | if ctx.Arch().ArchType != android.Arm64 { |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 319 | s.Scs = nil |
| 320 | } |
| 321 | |
Vishwath Mohan | 8f4fdd8 | 2017-04-20 07:42:52 -0700 | [diff] [blame] | 322 | // Also disable CFI if ASAN is enabled. |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 323 | if Bool(s.Address) || Bool(s.Hwaddress) { |
Vishwath Mohan | 8f4fdd8 | 2017-04-20 07:42:52 -0700 | [diff] [blame] | 324 | s.Cfi = nil |
| 325 | s.Diag.Cfi = nil |
| 326 | } |
| 327 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 328 | // Disable sanitizers that depend on the UBSan runtime for host builds. |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 329 | if ctx.Host() { |
| 330 | s.Cfi = nil |
| 331 | s.Diag.Cfi = nil |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 332 | s.Misc_undefined = nil |
| 333 | s.Undefined = nil |
| 334 | s.All_undefined = nil |
| 335 | s.Integer_overflow = nil |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Vishwath Mohan | 9ccbba0 | 2018-05-28 13:54:48 -0700 | [diff] [blame] | 338 | // Also disable CFI for VNDK variants of components |
| 339 | if ctx.isVndk() && ctx.useVndk() { |
Vishwath Mohan | 7589c82 | 2018-05-23 19:29:55 -0700 | [diff] [blame] | 340 | s.Cfi = nil |
| 341 | s.Diag.Cfi = nil |
| 342 | } |
| 343 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 344 | // HWASan ramdisk (which is built from recovery) goes over some bootloader limit. |
Evgenii Stepanov | 1e79844 | 2018-11-15 17:34:18 -0800 | [diff] [blame] | 345 | // Keep libc instrumented so that recovery can run hwasan-instrumented code if necessary. |
| 346 | if ctx.inRecovery() && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 347 | s.Hwaddress = nil |
| 348 | } |
| 349 | |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 350 | if ctx.staticBinary() { |
| 351 | s.Address = nil |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 352 | s.Fuzzer = nil |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 353 | s.Thread = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 356 | if Bool(s.All_undefined) { |
| 357 | s.Undefined = nil |
| 358 | } |
| 359 | |
Evgenii Stepanov | 0a8a0d0 | 2016-05-12 13:54:53 -0700 | [diff] [blame] | 360 | if !ctx.toolchain().Is64Bit() { |
| 361 | // TSAN and SafeStack are not supported on 32-bit architectures |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 362 | s.Thread = nil |
| 363 | s.Safestack = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 364 | // TODO(ccross): error for compile_multilib = "32"? |
| 365 | } |
| 366 | |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 367 | if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) || |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 368 | Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 || |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 369 | Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) { |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 370 | sanitize.Properties.SanitizerEnabled = true |
| 371 | } |
| 372 | |
Kostya Kortchinsky | d5275c8 | 2019-02-01 08:42:56 -0800 | [diff] [blame] | 373 | // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally. |
| 374 | if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() { |
Kostya Kortchinsky | d18ae5c | 2018-06-12 14:46:54 -0700 | [diff] [blame] | 375 | s.Scudo = nil |
| 376 | } |
| 377 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 378 | if Bool(s.Hwaddress) { |
| 379 | s.Address = nil |
| 380 | s.Thread = nil |
| 381 | } |
| 382 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 383 | // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is |
| 384 | // mutually incompatible. |
| 385 | if Bool(s.Fuzzer) { |
| 386 | s.Cfi = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 391 | if !sanitize.Properties.SanitizerEnabled { // || c.static() { |
| 392 | return deps |
| 393 | } |
| 394 | |
| 395 | if ctx.Device() { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 396 | if Bool(sanitize.Properties.Sanitize.Address) { |
Christopher Ferris | 753d4a6 | 2019-05-09 13:27:02 -0700 | [diff] [blame] | 397 | // Compiling asan and having libc_scudo in the same |
| 398 | // executable will cause the executable to crash. |
| 399 | // Remove libc_scudo since it is only used to override |
| 400 | // allocation functions which asan already overrides. |
| 401 | _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | return deps |
| 406 | } |
| 407 | |
Chih-Hung Hsieh | 3567e62 | 2018-11-15 14:01:36 -0800 | [diff] [blame] | 408 | func toDisableImplicitIntegerChange(flags []string) bool { |
| 409 | // Returns true if any flag is fsanitize*integer, and there is |
| 410 | // no explicit flag about sanitize=implicit-integer-sign-change. |
| 411 | for _, f := range flags { |
| 412 | if strings.Contains(f, "sanitize=implicit-integer-sign-change") { |
| 413 | return false |
| 414 | } |
| 415 | } |
| 416 | for _, f := range flags { |
| 417 | if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") { |
| 418 | return true |
| 419 | } |
| 420 | } |
| 421 | return false |
| 422 | } |
| 423 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 424 | func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags { |
Ivan Lozano | 59fdea2 | 2018-05-10 14:17:22 -0700 | [diff] [blame] | 425 | minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a" |
| 426 | minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 427 | |
| 428 | if ctx.Device() && sanitize.Properties.MinimalRuntimeDep { |
| 429 | flags.LdFlags = append(flags.LdFlags, minimalRuntimePath) |
Ivan Lozano | 59fdea2 | 2018-05-10 14:17:22 -0700 | [diff] [blame] | 430 | flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 431 | } |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 432 | if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 433 | return flags |
| 434 | } |
| 435 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 436 | if Bool(sanitize.Properties.Sanitize.Address) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 437 | if ctx.Arch().ArchType == android.Arm { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 438 | // Frame pointer based unwinder in ASan requires ARM frame setup. |
| 439 | // TODO: put in flags? |
| 440 | flags.RequiredInstructionSet = "arm" |
| 441 | } |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 442 | flags.CFlags = append(flags.CFlags, asanCflags...) |
| 443 | flags.LdFlags = append(flags.LdFlags, asanLdflags...) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 444 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 445 | if ctx.Host() { |
| 446 | // -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 447 | // libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 448 | flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed") |
| 449 | } else { |
Yi Kong | da06908 | 2019-08-22 00:46:36 +0000 | [diff] [blame] | 450 | flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0") |
Jiyong Park | a2aca28 | 2019-02-02 13:13:38 +0900 | [diff] [blame] | 451 | if ctx.bootstrap() { |
| 452 | flags.DynamicLinker = "/system/bin/bootstrap/linker_asan" |
| 453 | } else { |
| 454 | flags.DynamicLinker = "/system/bin/linker_asan" |
| 455 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 456 | if flags.Toolchain.Is64Bit() { |
| 457 | flags.DynamicLinker += "64" |
| 458 | } |
| 459 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 462 | if Bool(sanitize.Properties.Sanitize.Hwaddress) { |
| 463 | flags.CFlags = append(flags.CFlags, hwasanCflags...) |
Yabin Cui | 6be405e | 2017-10-19 15:52:11 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 466 | if Bool(sanitize.Properties.Sanitize.Fuzzer) { |
| 467 | flags.CFlags = append(flags.CFlags, "-fsanitize=fuzzer-no-link") |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 468 | |
| 469 | // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible. |
| 470 | _, flags.LdFlags = removeFromList("-flto", flags.LdFlags) |
Mitch Phillips | 34b493f | 2019-08-02 16:57:55 -0700 | [diff] [blame] | 471 | _, flags.CFlags = removeFromList("-flto", flags.CFlags) |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 472 | flags.LdFlags = append(flags.LdFlags, "-fno-lto") |
Mitch Phillips | 34b493f | 2019-08-02 16:57:55 -0700 | [diff] [blame] | 473 | flags.CFlags = append(flags.CFlags, "-fno-lto") |
Mitch Phillips | 7438475 | 2019-06-17 10:33:52 -0700 | [diff] [blame] | 474 | |
Mitch Phillips | b8e593d | 2019-10-09 17:18:59 -0700 | [diff] [blame] | 475 | // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries |
| 476 | // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus |
| 477 | // doesn't match the linker script due to the "__emutls_v." prefix). |
| 478 | flags.LdFlags = append(flags.LdFlags, "-fno-sanitize-coverage=stack-depth") |
| 479 | flags.CFlags = append(flags.CFlags, "-fno-sanitize-coverage=stack-depth") |
| 480 | |
Mitch Phillips | 7438475 | 2019-06-17 10:33:52 -0700 | [diff] [blame] | 481 | // TODO(b/133876586): Experimental PM breaks sanitizer coverage. |
| 482 | _, flags.CFlags = removeFromList("-fexperimental-new-pass-manager", flags.CFlags) |
| 483 | flags.CFlags = append(flags.CFlags, "-fno-experimental-new-pass-manager") |
Mitch Phillips | b9b3e79 | 2019-08-28 12:41:07 -0700 | [diff] [blame] | 484 | |
| 485 | // Disable fortify for fuzzing builds. Generally, we'll be building with |
| 486 | // UBSan or ASan here and the fortify checks pollute the stack traces. |
| 487 | _, flags.CFlags = removeFromList("-D_FORTIFY_SOURCE=1", flags.CFlags) |
| 488 | _, flags.CFlags = removeFromList("-D_FORTIFY_SOURCE=2", flags.CFlags) |
| 489 | flags.CFlags = append(flags.CFlags, "-U_FORTIFY_SOURCE") |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 492 | if Bool(sanitize.Properties.Sanitize.Cfi) { |
Evgenii Stepanov | 7ebf9fa | 2017-01-20 14:13:06 -0800 | [diff] [blame] | 493 | if ctx.Arch().ArchType == android.Arm { |
| 494 | // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up |
| 495 | // to do this on a function basis, so force Thumb on the entire module. |
| 496 | flags.RequiredInstructionSet = "thumb" |
| 497 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 498 | |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 499 | flags.CFlags = append(flags.CFlags, cfiCflags...) |
Evgenii Stepanov | dbf1d4f | 2018-08-31 12:54:33 -0700 | [diff] [blame] | 500 | flags.AsFlags = append(flags.AsFlags, cfiAsflags...) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 501 | // Only append the default visibility flag if -fvisibility has not already been set |
| 502 | // to hidden. |
| 503 | if !inList("-fvisibility=hidden", flags.CFlags) { |
| 504 | flags.CFlags = append(flags.CFlags, "-fvisibility=default") |
| 505 | } |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 506 | flags.LdFlags = append(flags.LdFlags, cfiLdflags...) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 507 | |
| 508 | if ctx.staticBinary() { |
| 509 | _, flags.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.CFlags) |
| 510 | _, flags.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.LdFlags) |
| 511 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 514 | if Bool(sanitize.Properties.Sanitize.Integer_overflow) { |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 515 | flags.CFlags = append(flags.CFlags, intOverflowCflags...) |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 518 | if len(sanitize.Properties.Sanitizers) > 0 { |
| 519 | sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",") |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 520 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 521 | flags.CFlags = append(flags.CFlags, sanitizeArg) |
Evgenii Stepanov | dbf1d4f | 2018-08-31 12:54:33 -0700 | [diff] [blame] | 522 | flags.AsFlags = append(flags.AsFlags, sanitizeArg) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 523 | if ctx.Host() { |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 524 | // Host sanitizers only link symbols in the final executable, so |
| 525 | // there will always be undefined symbols in intermediate libraries. |
| 526 | _, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags) |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 527 | flags.LdFlags = append(flags.LdFlags, sanitizeArg) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 528 | } else { |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 529 | if enableMinimalRuntime(sanitize) { |
| 530 | flags.CFlags = append(flags.CFlags, strings.Join(minimalRuntimeFlags, " ")) |
| 531 | flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...) |
Ivan Lozano | 59fdea2 | 2018-05-10 14:17:22 -0700 | [diff] [blame] | 532 | flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 533 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 534 | } |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 535 | |
| 536 | if Bool(sanitize.Properties.Sanitize.Fuzzer) { |
| 537 | // When fuzzing, we wish to crash with diagnostics on any bug. |
| 538 | flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all") |
| 539 | } else if ctx.Host() { |
| 540 | flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all") |
| 541 | } else { |
| 542 | flags.CFlags = append(flags.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort") |
| 543 | } |
Chih-Hung Hsieh | 3567e62 | 2018-11-15 14:01:36 -0800 | [diff] [blame] | 544 | // http://b/119329758, Android core does not boot up with this sanitizer yet. |
| 545 | if toDisableImplicitIntegerChange(flags.CFlags) { |
| 546 | flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change") |
| 547 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 550 | if len(sanitize.Properties.DiagSanitizers) > 0 { |
| 551 | flags.CFlags = append(flags.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ",")) |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 552 | } |
| 553 | // FIXME: enable RTTI if diag + (cfi or vptr) |
| 554 | |
Andreas Gampe | 9707116 | 2017-05-08 13:15:23 -0700 | [diff] [blame] | 555 | if sanitize.Properties.Sanitize.Recover != nil { |
| 556 | flags.CFlags = append(flags.CFlags, "-fsanitize-recover="+ |
| 557 | strings.Join(sanitize.Properties.Sanitize.Recover, ",")) |
| 558 | } |
| 559 | |
Ivan Lozano | 7929bba | 2018-12-12 09:36:31 -0800 | [diff] [blame] | 560 | if sanitize.Properties.Sanitize.Diag.No_recover != nil { |
| 561 | flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+ |
| 562 | strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ",")) |
| 563 | } |
| 564 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 565 | blacklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blacklist) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 566 | if blacklist.Valid() { |
| 567 | flags.CFlags = append(flags.CFlags, "-fsanitize-blacklist="+blacklist.String()) |
| 568 | flags.CFlagsDeps = append(flags.CFlagsDeps, blacklist.Path()) |
| 569 | } |
| 570 | |
| 571 | return flags |
| 572 | } |
| 573 | |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 574 | func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 575 | // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing |
| 576 | // both the sanitized and non-sanitized variants to make without a name conflict. |
| 577 | if ret.Class == "STATIC_LIBRARIES" || ret.Class == "HEADER_LIBRARIES" { |
| 578 | if Bool(sanitize.Properties.Sanitize.Cfi) { |
| 579 | ret.SubName += ".cfi" |
| 580 | } |
| 581 | if Bool(sanitize.Properties.Sanitize.Hwaddress) { |
| 582 | ret.SubName += ".hwasan" |
| 583 | } |
| 584 | if Bool(sanitize.Properties.Sanitize.Scs) { |
| 585 | ret.SubName += ".scs" |
| 586 | } |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 587 | } |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Vishwath Mohan | 1dd8839 | 2017-03-29 22:00:18 -0700 | [diff] [blame] | 590 | func (sanitize *sanitize) inSanitizerDir() bool { |
| 591 | return sanitize.Properties.InSanitizerDir |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 594 | func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool { |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 595 | switch t { |
| 596 | case asan: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 597 | return sanitize.Properties.Sanitize.Address |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 598 | case hwasan: |
| 599 | return sanitize.Properties.Sanitize.Hwaddress |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 600 | case tsan: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 601 | return sanitize.Properties.Sanitize.Thread |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 602 | case intOverflow: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 603 | return sanitize.Properties.Sanitize.Integer_overflow |
| 604 | case cfi: |
| 605 | return sanitize.Properties.Sanitize.Cfi |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 606 | case scs: |
| 607 | return sanitize.Properties.Sanitize.Scs |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 608 | case fuzzer: |
| 609 | return sanitize.Properties.Sanitize.Fuzzer |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 610 | default: |
| 611 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 612 | } |
| 613 | } |
| 614 | |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 615 | func (sanitize *sanitize) isUnsanitizedVariant() bool { |
| 616 | return !sanitize.isSanitizerEnabled(asan) && |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 617 | !sanitize.isSanitizerEnabled(hwasan) && |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 618 | !sanitize.isSanitizerEnabled(tsan) && |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 619 | !sanitize.isSanitizerEnabled(cfi) && |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 620 | !sanitize.isSanitizerEnabled(scs) && |
| 621 | !sanitize.isSanitizerEnabled(fuzzer) |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Jayant Chowdhary | b7e08ca | 2018-05-10 15:29:24 -0700 | [diff] [blame] | 624 | func (sanitize *sanitize) isVariantOnProductionDevice() bool { |
| 625 | return !sanitize.isSanitizerEnabled(asan) && |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 626 | !sanitize.isSanitizerEnabled(hwasan) && |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 627 | !sanitize.isSanitizerEnabled(tsan) && |
| 628 | !sanitize.isSanitizerEnabled(fuzzer) |
Jayant Chowdhary | b7e08ca | 2018-05-10 15:29:24 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 631 | func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) { |
| 632 | switch t { |
| 633 | case asan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 634 | sanitize.Properties.Sanitize.Address = boolPtr(b) |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 635 | case hwasan: |
| 636 | sanitize.Properties.Sanitize.Hwaddress = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 637 | case tsan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 638 | sanitize.Properties.Sanitize.Thread = boolPtr(b) |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 639 | case intOverflow: |
| 640 | sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 641 | case cfi: |
| 642 | sanitize.Properties.Sanitize.Cfi = boolPtr(b) |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 643 | case scs: |
| 644 | sanitize.Properties.Sanitize.Scs = boolPtr(b) |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 645 | case fuzzer: |
| 646 | sanitize.Properties.Sanitize.Fuzzer = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 647 | default: |
| 648 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 649 | } |
| 650 | if b { |
| 651 | sanitize.Properties.SanitizerEnabled = true |
| 652 | } |
| 653 | } |
| 654 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 655 | // Check if the sanitizer is explicitly disabled (as opposed to nil by |
| 656 | // virtue of not being set). |
| 657 | func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool { |
| 658 | if sanitize == nil { |
| 659 | return false |
| 660 | } |
| 661 | |
| 662 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 663 | return sanitizerVal != nil && *sanitizerVal == false |
| 664 | } |
| 665 | |
| 666 | // There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled) |
| 667 | // because enabling a sanitizer either directly (via the blueprint) or |
| 668 | // indirectly (via a mutator) sets the bool ptr to true, and you can't |
| 669 | // distinguish between the cases. It isn't needed though - both cases can be |
| 670 | // treated identically. |
| 671 | func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool { |
| 672 | if sanitize == nil { |
| 673 | return false |
| 674 | } |
| 675 | |
| 676 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 677 | return sanitizerVal != nil && *sanitizerVal == true |
| 678 | } |
| 679 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 680 | func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool { |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 681 | t, ok := tag.(DependencyTag) |
| 682 | return ok && t.Library || t == reuseObjTag || t == objDepTag |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 685 | // Propagate sanitizer requirements down from binaries |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 686 | func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) { |
| 687 | return func(mctx android.TopDownMutatorContext) { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 688 | if c, ok := mctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(t) { |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 689 | mctx.WalkDeps(func(child, parent android.Module) bool { |
| 690 | if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) { |
| 691 | return false |
| 692 | } |
| 693 | if d, ok := child.(*Module); ok && d.sanitize != nil && |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 694 | !Bool(d.sanitize.Properties.Sanitize.Never) && |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 695 | !d.sanitize.isSanitizerExplicitlyDisabled(t) { |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 696 | if t == cfi || t == hwasan || t == scs { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 697 | if d.static() { |
| 698 | d.sanitize.Properties.SanitizeDep = true |
| 699 | } |
| 700 | } else { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 701 | d.sanitize.Properties.SanitizeDep = true |
| 702 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 703 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 704 | return true |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 705 | }) |
Jiyong Park | f97782b | 2019-02-13 20:28:58 +0900 | [diff] [blame] | 706 | } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok { |
| 707 | // If an APEX module includes a lib which is enabled for a sanitizer T, then |
| 708 | // the APEX module is also enabled for the same sanitizer type. |
| 709 | mctx.VisitDirectDeps(func(child android.Module) { |
| 710 | if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) { |
| 711 | sanitizeable.EnableSanitizer(t.name()) |
| 712 | } |
| 713 | }) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 718 | // Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies. |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 719 | func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) { |
| 720 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
| 721 | mctx.WalkDeps(func(child, parent android.Module) bool { |
| 722 | if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) { |
| 723 | return false |
| 724 | } |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 725 | |
Colin Cross | 0b90833 | 2019-06-19 23:00:20 -0700 | [diff] [blame] | 726 | if d, ok := child.(*Module); ok && d.static() && d.sanitize != nil { |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 727 | if enableMinimalRuntime(d.sanitize) { |
| 728 | // If a static dependency is built with the minimal runtime, |
| 729 | // make sure we include the ubsan minimal runtime. |
| 730 | c.sanitize.Properties.MinimalRuntimeDep = true |
| 731 | } else if Bool(d.sanitize.Properties.Sanitize.Diag.Integer_overflow) || |
| 732 | len(d.sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 { |
| 733 | // If a static dependency runs with full ubsan diagnostics, |
| 734 | // make sure we include the ubsan runtime. |
| 735 | c.sanitize.Properties.UbsanRuntimeDep = true |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 736 | } |
Colin Cross | 0b90833 | 2019-06-19 23:00:20 -0700 | [diff] [blame] | 737 | |
| 738 | if c.sanitize.Properties.MinimalRuntimeDep && |
| 739 | c.sanitize.Properties.UbsanRuntimeDep { |
| 740 | // both flags that this mutator might set are true, so don't bother recursing |
| 741 | return false |
| 742 | } |
| 743 | |
| 744 | return true |
| 745 | } else { |
| 746 | return false |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 747 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 748 | }) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 752 | // Add the dependency to the runtime library for each of the sanitizer variants |
| 753 | func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 754 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
Pirama Arumuga Nainar | 6aa2102 | 2019-01-25 00:20:35 +0000 | [diff] [blame] | 755 | if !c.Enabled() { |
| 756 | return |
| 757 | } |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 758 | var sanitizers []string |
| 759 | var diagSanitizers []string |
| 760 | |
| 761 | if Bool(c.sanitize.Properties.Sanitize.All_undefined) { |
| 762 | sanitizers = append(sanitizers, "undefined") |
| 763 | } else { |
| 764 | if Bool(c.sanitize.Properties.Sanitize.Undefined) { |
| 765 | sanitizers = append(sanitizers, |
| 766 | "bool", |
| 767 | "integer-divide-by-zero", |
| 768 | "return", |
| 769 | "returns-nonnull-attribute", |
| 770 | "shift-exponent", |
| 771 | "unreachable", |
| 772 | "vla-bound", |
| 773 | // TODO(danalbert): The following checks currently have compiler performance issues. |
| 774 | //"alignment", |
| 775 | //"bounds", |
| 776 | //"enum", |
| 777 | //"float-cast-overflow", |
| 778 | //"float-divide-by-zero", |
| 779 | //"nonnull-attribute", |
| 780 | //"null", |
| 781 | //"shift-base", |
| 782 | //"signed-integer-overflow", |
| 783 | // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on. |
| 784 | // https://llvm.org/PR19302 |
| 785 | // http://reviews.llvm.org/D6974 |
| 786 | // "object-size", |
| 787 | ) |
| 788 | } |
| 789 | sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...) |
| 790 | } |
| 791 | |
| 792 | if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) { |
| 793 | diagSanitizers = append(diagSanitizers, "undefined") |
| 794 | } |
| 795 | |
| 796 | diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...) |
| 797 | |
| 798 | if Bool(c.sanitize.Properties.Sanitize.Address) { |
| 799 | sanitizers = append(sanitizers, "address") |
| 800 | diagSanitizers = append(diagSanitizers, "address") |
| 801 | } |
| 802 | |
| 803 | if Bool(c.sanitize.Properties.Sanitize.Hwaddress) { |
| 804 | sanitizers = append(sanitizers, "hwaddress") |
| 805 | } |
| 806 | |
| 807 | if Bool(c.sanitize.Properties.Sanitize.Thread) { |
| 808 | sanitizers = append(sanitizers, "thread") |
| 809 | } |
| 810 | |
| 811 | if Bool(c.sanitize.Properties.Sanitize.Safestack) { |
| 812 | sanitizers = append(sanitizers, "safe-stack") |
| 813 | } |
| 814 | |
| 815 | if Bool(c.sanitize.Properties.Sanitize.Cfi) { |
| 816 | sanitizers = append(sanitizers, "cfi") |
| 817 | |
| 818 | if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) { |
| 819 | diagSanitizers = append(diagSanitizers, "cfi") |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) { |
| 824 | sanitizers = append(sanitizers, "unsigned-integer-overflow") |
| 825 | sanitizers = append(sanitizers, "signed-integer-overflow") |
| 826 | if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) { |
| 827 | diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow") |
| 828 | diagSanitizers = append(diagSanitizers, "signed-integer-overflow") |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | if Bool(c.sanitize.Properties.Sanitize.Scudo) { |
| 833 | sanitizers = append(sanitizers, "scudo") |
| 834 | } |
| 835 | |
| 836 | if Bool(c.sanitize.Properties.Sanitize.Scs) { |
| 837 | sanitizers = append(sanitizers, "shadow-call-stack") |
| 838 | } |
| 839 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 840 | if Bool(c.sanitize.Properties.Sanitize.Fuzzer) { |
| 841 | sanitizers = append(sanitizers, "fuzzer-no-link") |
| 842 | } |
| 843 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 844 | // Save the list of sanitizers. These will be used again when generating |
| 845 | // the build rules (for Cflags, etc.) |
| 846 | c.sanitize.Properties.Sanitizers = sanitizers |
| 847 | c.sanitize.Properties.DiagSanitizers = diagSanitizers |
| 848 | |
| 849 | // Determine the runtime library required |
| 850 | runtimeLibrary := "" |
Ryan Prichard | b49fe1b | 2019-10-11 15:03:34 -0700 | [diff] [blame] | 851 | var extraStaticDeps []string |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 852 | toolchain := c.toolchain(mctx) |
| 853 | if Bool(c.sanitize.Properties.Sanitize.Address) { |
| 854 | runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain) |
| 855 | } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) { |
| 856 | if c.staticBinary() { |
| 857 | runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain) |
Ryan Prichard | b49fe1b | 2019-10-11 15:03:34 -0700 | [diff] [blame] | 858 | extraStaticDeps = []string{"libdl"} |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 859 | } else { |
| 860 | runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain) |
| 861 | } |
| 862 | } else if Bool(c.sanitize.Properties.Sanitize.Thread) { |
| 863 | runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain) |
| 864 | } else if Bool(c.sanitize.Properties.Sanitize.Scudo) { |
| 865 | if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep { |
| 866 | runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain) |
| 867 | } else { |
| 868 | runtimeLibrary = config.ScudoRuntimeLibrary(toolchain) |
| 869 | } |
Mitch Phillips | b8e593d | 2019-10-09 17:18:59 -0700 | [diff] [blame] | 870 | } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep || |
| 871 | Bool(c.sanitize.Properties.Sanitize.Fuzzer) { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 872 | runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain) |
| 873 | } |
| 874 | |
| 875 | if mctx.Device() && runtimeLibrary != "" { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame^] | 876 | if inList(runtimeLibrary, *llndkLibraries(mctx.Config())) && !c.static() && c.UseVndk() { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 877 | runtimeLibrary = runtimeLibrary + llndkLibrarySuffix |
| 878 | } |
| 879 | |
| 880 | // Adding dependency to the runtime library. We are using *FarVariation* |
| 881 | // because the runtime libraries themselves are not mutated by sanitizer |
| 882 | // mutators and thus don't have sanitizer variants whereas this module |
| 883 | // has been already mutated. |
| 884 | // |
| 885 | // Note that by adding dependency with {static|shared}DepTag, the lib is |
| 886 | // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module |
| 887 | if c.staticBinary() { |
| 888 | // static executable gets static runtime libs |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 889 | mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{ |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 890 | {Mutator: "link", Variation: "static"}, |
Jiyong Park | 3b1746a | 2019-01-29 11:15:04 +0900 | [diff] [blame] | 891 | {Mutator: "image", Variation: c.imageVariation()}, |
Ivan Lozano | 183a321 | 2019-10-18 14:18:45 -0700 | [diff] [blame] | 892 | }...), StaticDepTag, append([]string{runtimeLibrary}, extraStaticDeps...)...) |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 893 | } else if !c.static() && !c.header() { |
Jiyong Park | 3b1746a | 2019-01-29 11:15:04 +0900 | [diff] [blame] | 894 | // dynamic executable and shared libs get shared runtime libs |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 895 | mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{ |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 896 | {Mutator: "link", Variation: "shared"}, |
Jiyong Park | 3b1746a | 2019-01-29 11:15:04 +0900 | [diff] [blame] | 897 | {Mutator: "image", Variation: c.imageVariation()}, |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 898 | }...), earlySharedDepTag, runtimeLibrary) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 899 | } |
| 900 | // static lib does not have dependency to the runtime library. The |
| 901 | // dependency will be added to the executables or shared libs using |
| 902 | // the static lib. |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | type Sanitizeable interface { |
| 908 | android.Module |
Jiyong Park | 388ef3f | 2019-01-28 19:47:32 +0900 | [diff] [blame] | 909 | IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool |
Jiyong Park | f97782b | 2019-02-13 20:28:58 +0900 | [diff] [blame] | 910 | EnableSanitizer(sanitizerName string) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 911 | } |
| 912 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 913 | // Create sanitized variants for modules that need them |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 914 | func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) { |
| 915 | return func(mctx android.BottomUpMutatorContext) { |
Vishwath Mohan | e615345 | 2017-08-11 00:52:44 +0000 | [diff] [blame] | 916 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 917 | if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) { |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 918 | modules := mctx.CreateVariations(t.variationName()) |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 919 | modules[0].(*Module).sanitize.SetSanitizer(t, true) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 920 | } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 921 | isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t) |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 922 | if mctx.Device() && t.incompatibleWithCfi() { |
| 923 | // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that |
| 924 | // are incompatible with cfi |
| 925 | c.sanitize.SetSanitizer(cfi, false) |
| 926 | } |
| 927 | if c.static() || c.header() || t == asan || t == fuzzer { |
| 928 | // Static and header libs are split into non-sanitized and sanitized variants. |
| 929 | // Shared libs are not split. However, for asan and fuzzer, we split even for shared |
| 930 | // libs because a library sanitized for asan/fuzzer can't be linked from a library |
| 931 | // that isn't sanitized for asan/fuzzer. |
| 932 | // |
| 933 | // Note for defaultVariation: since we don't split for shared libs but for static/header |
| 934 | // libs, it is possible for the sanitized variant of a static/header lib to depend |
| 935 | // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an |
| 936 | // error when the module is split. defaultVariation is the name of the variation that |
| 937 | // will be used when such a dangling dependency occurs during the split of the current |
| 938 | // module. By setting it to the name of the sanitized variation, the dangling dependency |
| 939 | // is redirected to the sanitized variant of the dependent module. |
| 940 | defaultVariation := t.variationName() |
| 941 | mctx.SetDefaultDependencyVariation(&defaultVariation) |
| 942 | modules := mctx.CreateVariations("", t.variationName()) |
| 943 | modules[0].(*Module).sanitize.SetSanitizer(t, false) |
| 944 | modules[1].(*Module).sanitize.SetSanitizer(t, true) |
| 945 | modules[0].(*Module).sanitize.Properties.SanitizeDep = false |
| 946 | modules[1].(*Module).sanitize.Properties.SanitizeDep = false |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 947 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 948 | // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants |
| 949 | // to Make, because the sanitized version has a different suffix in name. |
| 950 | // For other types of sanitizers, suppress the variation that is disabled. |
| 951 | if t != cfi && t != scs && t != hwasan { |
| 952 | if isSanitizerEnabled { |
| 953 | modules[0].(*Module).Properties.PreventInstall = true |
| 954 | modules[0].(*Module).Properties.HideFromMake = true |
| 955 | } else { |
| 956 | modules[1].(*Module).Properties.PreventInstall = true |
| 957 | modules[1].(*Module).Properties.HideFromMake = true |
| 958 | } |
| 959 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 960 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 961 | // Export the static lib name to make |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 962 | if c.static() { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 963 | if t == cfi { |
| 964 | appendStringSync(c.Name(), cfiStaticLibs(mctx.Config()), &cfiStaticLibsMutex) |
| 965 | } else if t == hwasan { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame^] | 966 | if c.UseVndk() { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 967 | appendStringSync(c.Name(), hwasanVendorStaticLibs(mctx.Config()), |
| 968 | &hwasanStaticLibsMutex) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 969 | } else { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 970 | appendStringSync(c.Name(), hwasanStaticLibs(mctx.Config()), |
| 971 | &hwasanStaticLibsMutex) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 972 | } |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | } else { |
| 976 | // Shared libs are not split. Only the sanitized variant is created. |
| 977 | modules := mctx.CreateVariations(t.variationName()) |
| 978 | modules[0].(*Module).sanitize.SetSanitizer(t, true) |
| 979 | modules[0].(*Module).sanitize.Properties.SanitizeDep = false |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 980 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 981 | // locate the asan libraries under /data/asan |
| 982 | if mctx.Device() && t == asan && isSanitizerEnabled { |
| 983 | modules[0].(*Module).sanitize.Properties.InSanitizerDir = true |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 984 | } |
Vishwath Mohan | e21fe42 | 2017-11-01 19:42:45 -0700 | [diff] [blame] | 985 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 986 | } |
| 987 | c.sanitize.Properties.SanitizeDep = false |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 988 | } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 989 | // APEX modules fall here |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 990 | mctx.CreateVariations(t.variationName()) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | } |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 994 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 995 | var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs") |
| 996 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 997 | func cfiStaticLibs(config android.Config) *[]string { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 998 | return config.Once(cfiStaticLibsKey, func() interface{} { |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 999 | return &[]string{} |
| 1000 | }).(*[]string) |
| 1001 | } |
| 1002 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1003 | var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs") |
| 1004 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1005 | func hwasanStaticLibs(config android.Config) *[]string { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1006 | return config.Once(hwasanStaticLibsKey, func() interface{} { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1007 | return &[]string{} |
| 1008 | }).(*[]string) |
| 1009 | } |
| 1010 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1011 | var hwasanVendorStaticLibsKey = android.NewOnceKey("hwasanVendorStaticLibs") |
| 1012 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1013 | func hwasanVendorStaticLibs(config android.Config) *[]string { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1014 | return config.Once(hwasanVendorStaticLibsKey, func() interface{} { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1015 | return &[]string{} |
| 1016 | }).(*[]string) |
| 1017 | } |
| 1018 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1019 | func appendStringSync(item string, list *[]string, mutex *sync.Mutex) { |
| 1020 | mutex.Lock() |
| 1021 | *list = append(*list, item) |
| 1022 | mutex.Unlock() |
| 1023 | } |
| 1024 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1025 | func enableMinimalRuntime(sanitize *sanitize) bool { |
| 1026 | if !Bool(sanitize.Properties.Sanitize.Address) && |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1027 | !Bool(sanitize.Properties.Sanitize.Hwaddress) && |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 1028 | !Bool(sanitize.Properties.Sanitize.Fuzzer) && |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1029 | (Bool(sanitize.Properties.Sanitize.Integer_overflow) || |
| 1030 | len(sanitize.Properties.Sanitize.Misc_undefined) > 0) && |
| 1031 | !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) || |
| 1032 | Bool(sanitize.Properties.Sanitize.Diag.Cfi) || |
| 1033 | len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) { |
| 1034 | return true |
| 1035 | } |
| 1036 | return false |
| 1037 | } |
| 1038 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1039 | func cfiMakeVarsProvider(ctx android.MakeVarsContext) { |
| 1040 | cfiStaticLibs := cfiStaticLibs(ctx.Config()) |
Jeff Gaston | 7276539 | 2017-11-28 16:37:53 -0800 | [diff] [blame] | 1041 | sort.Strings(*cfiStaticLibs) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1042 | ctx.Strict("SOONG_CFI_STATIC_LIBRARIES", strings.Join(*cfiStaticLibs, " ")) |
| 1043 | } |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1044 | |
| 1045 | func hwasanMakeVarsProvider(ctx android.MakeVarsContext) { |
| 1046 | hwasanStaticLibs := hwasanStaticLibs(ctx.Config()) |
| 1047 | sort.Strings(*hwasanStaticLibs) |
| 1048 | ctx.Strict("SOONG_HWASAN_STATIC_LIBRARIES", strings.Join(*hwasanStaticLibs, " ")) |
| 1049 | |
| 1050 | hwasanVendorStaticLibs := hwasanVendorStaticLibs(ctx.Config()) |
| 1051 | sort.Strings(*hwasanVendorStaticLibs) |
| 1052 | ctx.Strict("SOONG_HWASAN_VENDOR_STATIC_LIBRARIES", strings.Join(*hwasanVendorStaticLibs, " ")) |
| 1053 | } |