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