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