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