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