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