Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Jeff Gaston | 7276539 | 2017-11-28 16:37:53 -0800 | [diff] [blame] | 19 | "sort" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 20 | "strings" |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 21 | "sync" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 22 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint" |
| 24 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 25 | "android/soong/android" |
Evgenii Stepanov | af36db1 | 2016-08-15 14:18:24 -0700 | [diff] [blame] | 26 | "android/soong/cc/config" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 29 | var ( |
| 30 | // Any C flags added by sanitizer which libTooling tools may not |
| 31 | // understand also need to be added to ClangLibToolingUnknownCflags in |
| 32 | // cc/config/clang.go |
Vishwath Mohan | f3918d3 | 2017-02-14 07:59:33 -0800 | [diff] [blame] | 33 | |
Yi Kong | 20233a4 | 2019-08-21 01:38:40 -0700 | [diff] [blame] | 34 | asanCflags = []string{ |
| 35 | "-fno-omit-frame-pointer", |
| 36 | "-fno-experimental-new-pass-manager", |
| 37 | } |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 38 | asanLdflags = []string{"-Wl,-u,__asan_preinit"} |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 39 | |
Peter Collingbourne | 967511a | 2019-03-19 21:39:54 -0700 | [diff] [blame] | 40 | hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=", |
Evgenii Stepanov | 96fa3dd | 2020-03-27 19:38:42 +0000 | [diff] [blame] | 41 | "-fsanitize-hwaddress-abi=platform", |
| 42 | "-fno-experimental-new-pass-manager", |
Evgenii Stepanov | 64bee4d | 2019-11-22 18:37:10 -0800 | [diff] [blame] | 43 | // The following improves debug location information |
| 44 | // availability at the cost of its accuracy. It increases |
| 45 | // the likelihood of a stack variable's frame offset |
| 46 | // to be recorded in the debug info, which is important |
| 47 | // for the quality of hwasan reports. The downside is a |
| 48 | // higher number of "optimized out" stack variables. |
| 49 | // b/112437883. |
| 50 | "-mllvm", "-instcombine-lower-dbg-declare=0", |
Mitch Phillips | b1c574f | 2020-06-22 13:28:23 -0700 | [diff] [blame] | 51 | // TODO(b/159343917): HWASan and GlobalISel don't play nicely, and |
| 52 | // GlobalISel is the default at -O0 on aarch64. |
| 53 | "-mllvm", "--aarch64-enable-global-isel-at-O=-1", |
| 54 | "-mllvm", "-fast-isel=false", |
Evgenii Stepanov | 64bee4d | 2019-11-22 18:37:10 -0800 | [diff] [blame] | 55 | } |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 56 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 57 | cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso", |
Pirama Arumuga Nainar | eb8d403 | 2020-07-27 11:22:35 -0700 | [diff] [blame] | 58 | "-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blocklist.txt"} |
Evgenii Stepanov | dbf1d4f | 2018-08-31 12:54:33 -0700 | [diff] [blame] | 59 | // -flto and -fvisibility are required by clang when -fsanitize=cfi is |
| 60 | // used, but have no effect on assembly files |
| 61 | cfiAsflags = []string{"-flto", "-fvisibility=default"} |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 62 | cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi", |
Pirama Arumuga Nainar | bdb17f0 | 2017-08-28 21:50:17 -0700 | [diff] [blame] | 63 | "-Wl,-plugin-opt,O1"} |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 64 | cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map" |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 65 | |
Pirama Arumuga Nainar | eb8d403 | 2020-07-27 11:22:35 -0700 | [diff] [blame] | 66 | intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blocklist.txt"} |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 67 | |
Peter Collingbourne | bd19db0 | 2019-03-06 10:38:48 -0800 | [diff] [blame] | 68 | minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined", |
Ivan Lozano | ae6ae1d | 2018-10-08 09:29:39 -0700 | [diff] [blame] | 69 | "-fno-sanitize-recover=integer,undefined"} |
Evgenii Stepanov | 2c6484e | 2019-05-15 12:49:54 -0700 | [diff] [blame] | 70 | hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512", |
| 71 | "export_memory_stats=0", "max_malloc_fill_size=0"} |
Dan Willemsen | cbceaab | 2016-10-13 16:44:07 -0700 | [diff] [blame] | 72 | ) |
| 73 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 74 | type sanitizerType int |
| 75 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 76 | func boolPtr(v bool) *bool { |
| 77 | if v { |
| 78 | return &v |
| 79 | } else { |
| 80 | return nil |
| 81 | } |
| 82 | } |
| 83 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 84 | const ( |
| 85 | asan sanitizerType = iota + 1 |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 86 | hwasan |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 87 | tsan |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 88 | intOverflow |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 89 | cfi |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 90 | scs |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 91 | fuzzer |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 92 | ) |
| 93 | |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 94 | // Name of the sanitizer variation for this sanitizer type |
| 95 | func (t sanitizerType) variationName() string { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 96 | switch t { |
| 97 | case asan: |
| 98 | return "asan" |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 99 | case hwasan: |
| 100 | return "hwasan" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 101 | case tsan: |
| 102 | return "tsan" |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 103 | case intOverflow: |
| 104 | return "intOverflow" |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 105 | case cfi: |
| 106 | return "cfi" |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 107 | case scs: |
| 108 | return "scs" |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 109 | case fuzzer: |
| 110 | return "fuzzer" |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 111 | default: |
| 112 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 113 | } |
| 114 | } |
| 115 | |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 116 | // This is the sanitizer names in SANITIZE_[TARGET|HOST] |
| 117 | func (t sanitizerType) name() string { |
| 118 | switch t { |
| 119 | case asan: |
| 120 | return "address" |
| 121 | case hwasan: |
| 122 | return "hwaddress" |
| 123 | case tsan: |
| 124 | return "thread" |
| 125 | case intOverflow: |
| 126 | return "integer_overflow" |
| 127 | case cfi: |
| 128 | return "cfi" |
| 129 | case scs: |
| 130 | return "shadow-call-stack" |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 131 | case fuzzer: |
| 132 | return "fuzzer" |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 133 | default: |
| 134 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 135 | } |
| 136 | } |
| 137 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 138 | func (t sanitizerType) incompatibleWithCfi() bool { |
| 139 | return t == asan || t == fuzzer || t == hwasan |
| 140 | } |
| 141 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 142 | type SanitizeUserProps struct { |
| 143 | Never *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 144 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 145 | // main sanitizers |
| 146 | Address *bool `android:"arch_variant"` |
| 147 | Thread *bool `android:"arch_variant"` |
| 148 | Hwaddress *bool `android:"arch_variant"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 149 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 150 | // local sanitizers |
| 151 | Undefined *bool `android:"arch_variant"` |
| 152 | All_undefined *bool `android:"arch_variant"` |
| 153 | Misc_undefined []string `android:"arch_variant"` |
| 154 | Fuzzer *bool `android:"arch_variant"` |
| 155 | Safestack *bool `android:"arch_variant"` |
| 156 | Cfi *bool `android:"arch_variant"` |
| 157 | Integer_overflow *bool `android:"arch_variant"` |
| 158 | Scudo *bool `android:"arch_variant"` |
| 159 | Scs *bool `android:"arch_variant"` |
| 160 | |
| 161 | // A modifier for ASAN and HWASAN for write only instrumentation |
| 162 | Writeonly *bool `android:"arch_variant"` |
| 163 | |
| 164 | // Sanitizers to run in the diagnostic mode (as opposed to the release mode). |
| 165 | // Replaces abort() on error with a human-readable error message. |
| 166 | // Address and Thread sanitizers always run in diagnostic mode. |
| 167 | Diag struct { |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 168 | Undefined *bool `android:"arch_variant"` |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 169 | Cfi *bool `android:"arch_variant"` |
| 170 | Integer_overflow *bool `android:"arch_variant"` |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 171 | Misc_undefined []string `android:"arch_variant"` |
| 172 | No_recover []string |
| 173 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 174 | |
Cindy Zhou | 8cd45de | 2020-11-16 08:41:00 -0800 | [diff] [blame^] | 175 | // Sanitizers to run with flag configuration specified |
| 176 | Config struct { |
| 177 | // Enables CFI support flags for assembly-heavy libraries |
| 178 | Cfi_assembly_support *bool `android:"arch_variant"` |
| 179 | } |
| 180 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 181 | // value to pass to -fsanitize-recover= |
| 182 | Recover []string |
Jasraj Bedi | bb4511d | 2020-07-23 22:58:17 +0000 | [diff] [blame] | 183 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 184 | // value to pass to -fsanitize-blacklist |
| 185 | Blocklist *string |
| 186 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 187 | |
Martin Stjernholm | b024957 | 2020-09-15 02:32:35 +0100 | [diff] [blame] | 188 | type SanitizeProperties struct { |
| 189 | // Enable AddressSanitizer, ThreadSanitizer, UndefinedBehaviorSanitizer, and |
| 190 | // others. Please see SanitizerUserProps in build/soong/cc/sanitize.go for |
| 191 | // details. |
| 192 | Sanitize SanitizeUserProps `android:"arch_variant"` |
| 193 | SanitizerEnabled bool `blueprint:"mutated"` |
| 194 | SanitizeDep bool `blueprint:"mutated"` |
| 195 | MinimalRuntimeDep bool `blueprint:"mutated"` |
| 196 | BuiltinsDep bool `blueprint:"mutated"` |
| 197 | UbsanRuntimeDep bool `blueprint:"mutated"` |
| 198 | InSanitizerDir bool `blueprint:"mutated"` |
| 199 | Sanitizers []string `blueprint:"mutated"` |
| 200 | DiagSanitizers []string `blueprint:"mutated"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | type sanitize struct { |
| 204 | Properties SanitizeProperties |
| 205 | } |
| 206 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 207 | func init() { |
| 208 | android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider) |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 209 | android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 212 | func (sanitize *sanitize) props() []interface{} { |
| 213 | return []interface{}{&sanitize.Properties} |
| 214 | } |
| 215 | |
| 216 | func (sanitize *sanitize) begin(ctx BaseModuleContext) { |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 217 | s := &sanitize.Properties.Sanitize |
| 218 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 219 | // Don't apply sanitizers to NDK code. |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 220 | if ctx.useSdk() { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 221 | s.Never = BoolPtr(true) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Doug Horn | c32c6b0 | 2019-01-17 14:44:05 -0800 | [diff] [blame] | 224 | // Sanitizers do not work on Fuchsia yet. |
| 225 | if ctx.Fuchsia() { |
| 226 | s.Never = BoolPtr(true) |
| 227 | } |
| 228 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 229 | // Never always wins. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 230 | if Bool(s.Never) { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 231 | return |
| 232 | } |
| 233 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 234 | var globalSanitizers []string |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 235 | var globalSanitizersDiag []string |
| 236 | |
Dan Willemsen | 8536d6b | 2018-10-07 20:54:34 -0700 | [diff] [blame] | 237 | if ctx.Host() { |
| 238 | if !ctx.Windows() { |
| 239 | globalSanitizers = ctx.Config().SanitizeHost() |
| 240 | } |
| 241 | } else { |
| 242 | arches := ctx.Config().SanitizeDeviceArch() |
| 243 | if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) { |
| 244 | globalSanitizers = ctx.Config().SanitizeDevice() |
| 245 | globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag() |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 249 | if len(globalSanitizers) > 0 { |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 250 | var found bool |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 251 | if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil { |
| 252 | s.All_undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 253 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 254 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 255 | if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil { |
| 256 | s.Undefined = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 259 | if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil { |
| 260 | s.Address = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 263 | if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil { |
| 264 | s.Thread = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 267 | if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil { |
| 268 | s.Fuzzer = boolPtr(true) |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil { |
| 272 | s.Safestack = boolPtr(true) |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 275 | if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 276 | if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) { |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 277 | s.Cfi = boolPtr(true) |
| 278 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 281 | // Global integer_overflow builds do not support static libraries. |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 282 | if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil { |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 283 | if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() { |
Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 284 | s.Integer_overflow = boolPtr(true) |
| 285 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Kostya Kortchinsky | d18ae5c | 2018-06-12 14:46:54 -0700 | [diff] [blame] | 288 | if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil { |
| 289 | s.Scudo = boolPtr(true) |
| 290 | } |
| 291 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 292 | if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil { |
| 293 | s.Hwaddress = boolPtr(true) |
| 294 | } |
| 295 | |
Jasraj Bedi | bb4511d | 2020-07-23 22:58:17 +0000 | [diff] [blame] | 296 | if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil { |
| 297 | // Hwaddress and Address are set before, so we can check them here |
| 298 | // If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false |
| 299 | if s.Address == nil && s.Hwaddress == nil { |
| 300 | ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'") |
| 301 | } |
| 302 | s.Writeonly = boolPtr(true) |
| 303 | } |
| 304 | |
Evgenii Stepanov | 05bafd3 | 2016-07-07 17:38:41 +0000 | [diff] [blame] | 305 | if len(globalSanitizers) > 0 { |
| 306 | ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0]) |
| 307 | } |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 308 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 309 | // Global integer_overflow builds do not support static library diagnostics. |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 310 | if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found && |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 311 | s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() { |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 312 | s.Diag.Integer_overflow = boolPtr(true) |
| 313 | } |
| 314 | |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 315 | if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found && |
| 316 | s.Diag.Cfi == nil && Bool(s.Cfi) { |
| 317 | s.Diag.Cfi = boolPtr(true) |
| 318 | } |
| 319 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 320 | if len(globalSanitizersDiag) > 0 { |
| 321 | ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0]) |
| 322 | } |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 323 | } |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 324 | |
Vishwath Mohan | 1c54f66 | 2018-05-24 18:36:18 -0700 | [diff] [blame] | 325 | // Enable CFI for all components in the include paths (for Aarch64 only) |
| 326 | 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] | 327 | s.Cfi = boolPtr(true) |
| 328 | if inList("cfi", ctx.Config().SanitizeDeviceDiag()) { |
| 329 | s.Diag.Cfi = boolPtr(true) |
Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Elliott Hughes | da3a071 | 2020-03-06 16:55:28 -0800 | [diff] [blame] | 333 | // Is CFI actually enabled? |
| 334 | if !ctx.Config().EnableCFI() { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 335 | s.Cfi = boolPtr(false) |
| 336 | s.Diag.Cfi = boolPtr(false) |
Vishwath Mohan | 1b017a7 | 2017-01-19 13:54:55 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Vishwath Mohan | 6d67e6e | 2017-02-07 20:31:41 -0800 | [diff] [blame] | 339 | // Also disable CFI for arm32 until b/35157333 is fixed. |
| 340 | if ctx.Arch().ArchType == android.Arm { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 341 | s.Cfi = boolPtr(false) |
| 342 | s.Diag.Cfi = boolPtr(false) |
Vishwath Mohan | 6d67e6e | 2017-02-07 20:31:41 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 345 | // HWASan requires AArch64 hardware feature (top-byte-ignore). |
| 346 | if ctx.Arch().ArchType != android.Arm64 { |
| 347 | s.Hwaddress = nil |
| 348 | } |
| 349 | |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 350 | // SCS is only implemented on AArch64. |
Peter Collingbourne | bd19db0 | 2019-03-06 10:38:48 -0800 | [diff] [blame] | 351 | if ctx.Arch().ArchType != android.Arm64 { |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 352 | s.Scs = nil |
| 353 | } |
| 354 | |
Vishwath Mohan | 8f4fdd8 | 2017-04-20 07:42:52 -0700 | [diff] [blame] | 355 | // Also disable CFI if ASAN is enabled. |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 356 | if Bool(s.Address) || Bool(s.Hwaddress) { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 357 | s.Cfi = boolPtr(false) |
| 358 | s.Diag.Cfi = boolPtr(false) |
Vishwath Mohan | 8f4fdd8 | 2017-04-20 07:42:52 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 361 | // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds. |
| 362 | if !ctx.Os().Linux() { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 363 | s.Cfi = boolPtr(false) |
| 364 | s.Diag.Cfi = boolPtr(false) |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 365 | s.Misc_undefined = nil |
| 366 | s.Undefined = nil |
| 367 | s.All_undefined = nil |
| 368 | s.Integer_overflow = nil |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Vishwath Mohan | 9ccbba0 | 2018-05-28 13:54:48 -0700 | [diff] [blame] | 371 | // Also disable CFI for VNDK variants of components |
| 372 | if ctx.isVndk() && ctx.useVndk() { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 373 | if ctx.static() { |
| 374 | // Cfi variant for static vndk should be captured as vendor snapshot, |
| 375 | // so don't strictly disable Cfi. |
| 376 | s.Cfi = nil |
| 377 | s.Diag.Cfi = nil |
| 378 | } else { |
| 379 | s.Cfi = boolPtr(false) |
| 380 | s.Diag.Cfi = boolPtr(false) |
| 381 | } |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 382 | } |
| 383 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 384 | // HWASan ramdisk (which is built from recovery) goes over some bootloader limit. |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 385 | // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary. |
| 386 | if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 387 | s.Hwaddress = nil |
| 388 | } |
| 389 | |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 390 | if ctx.staticBinary() { |
| 391 | s.Address = nil |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 392 | s.Fuzzer = nil |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 393 | s.Thread = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 396 | if Bool(s.All_undefined) { |
| 397 | s.Undefined = nil |
| 398 | } |
| 399 | |
Evgenii Stepanov | 0a8a0d0 | 2016-05-12 13:54:53 -0700 | [diff] [blame] | 400 | if !ctx.toolchain().Is64Bit() { |
| 401 | // TSAN and SafeStack are not supported on 32-bit architectures |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 402 | s.Thread = nil |
| 403 | s.Safestack = nil |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 404 | // TODO(ccross): error for compile_multilib = "32"? |
| 405 | } |
| 406 | |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 407 | if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) || |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 408 | Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 || |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 409 | Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs)) { |
Colin Cross | 3c344ef | 2016-07-18 15:44:56 -0700 | [diff] [blame] | 410 | sanitize.Properties.SanitizerEnabled = true |
| 411 | } |
| 412 | |
Kostya Kortchinsky | d5275c8 | 2019-02-01 08:42:56 -0800 | [diff] [blame] | 413 | // Disable Scudo if ASan or TSan is enabled, or if it's disabled globally. |
| 414 | if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() { |
Kostya Kortchinsky | d18ae5c | 2018-06-12 14:46:54 -0700 | [diff] [blame] | 415 | s.Scudo = nil |
| 416 | } |
| 417 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 418 | if Bool(s.Hwaddress) { |
| 419 | s.Address = nil |
| 420 | s.Thread = nil |
| 421 | } |
| 422 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 423 | // TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is |
| 424 | // mutually incompatible. |
| 425 | if Bool(s.Fuzzer) { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 426 | s.Cfi = boolPtr(false) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
| 430 | func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 431 | if !sanitize.Properties.SanitizerEnabled { // || c.static() { |
| 432 | return deps |
| 433 | } |
| 434 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 435 | return deps |
| 436 | } |
| 437 | |
Chih-Hung Hsieh | 3567e62 | 2018-11-15 14:01:36 -0800 | [diff] [blame] | 438 | func toDisableImplicitIntegerChange(flags []string) bool { |
| 439 | // Returns true if any flag is fsanitize*integer, and there is |
| 440 | // no explicit flag about sanitize=implicit-integer-sign-change. |
| 441 | for _, f := range flags { |
| 442 | if strings.Contains(f, "sanitize=implicit-integer-sign-change") { |
| 443 | return false |
| 444 | } |
| 445 | } |
| 446 | for _, f := range flags { |
| 447 | if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") { |
| 448 | return true |
| 449 | } |
| 450 | } |
| 451 | return false |
| 452 | } |
| 453 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 454 | func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags { |
Ivan Lozano | 59fdea2 | 2018-05-10 14:17:22 -0700 | [diff] [blame] | 455 | minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a" |
| 456 | minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 457 | builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a" |
| 458 | builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 459 | |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 460 | if sanitize.Properties.MinimalRuntimeDep { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 461 | flags.Local.LdFlags = append(flags.Local.LdFlags, |
| 462 | minimalRuntimePath, |
| 463 | "-Wl,--exclude-libs,"+minimalRuntimeLib) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 464 | } |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 465 | |
| 466 | if sanitize.Properties.BuiltinsDep { |
| 467 | flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...) |
| 468 | } |
| 469 | |
Ivan Lozano | a9255a8 | 2018-03-13 10:41:07 -0700 | [diff] [blame] | 470 | if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 471 | return flags |
| 472 | } |
| 473 | |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 474 | if Bool(sanitize.Properties.Sanitize.Address) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 475 | if ctx.Arch().ArchType == android.Arm { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 476 | // Frame pointer based unwinder in ASan requires ARM frame setup. |
| 477 | // TODO: put in flags? |
| 478 | flags.RequiredInstructionSet = "arm" |
| 479 | } |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 480 | flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...) |
| 481 | flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 482 | |
Jasraj Bedi | bb4511d | 2020-07-23 22:58:17 +0000 | [diff] [blame] | 483 | if Bool(sanitize.Properties.Sanitize.Writeonly) { |
| 484 | flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0") |
| 485 | } |
| 486 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 487 | if ctx.Host() { |
| 488 | // -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 489 | // libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 490 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed") |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 491 | } else { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 492 | flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0") |
Jiyong Park | a2aca28 | 2019-02-02 13:13:38 +0900 | [diff] [blame] | 493 | if ctx.bootstrap() { |
| 494 | flags.DynamicLinker = "/system/bin/bootstrap/linker_asan" |
| 495 | } else { |
| 496 | flags.DynamicLinker = "/system/bin/linker_asan" |
| 497 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 498 | if flags.Toolchain.Is64Bit() { |
| 499 | flags.DynamicLinker += "64" |
| 500 | } |
| 501 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 504 | if Bool(sanitize.Properties.Sanitize.Hwaddress) { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 505 | flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...) |
Jasraj Bedi | bb4511d | 2020-07-23 22:58:17 +0000 | [diff] [blame] | 506 | if Bool(sanitize.Properties.Sanitize.Writeonly) { |
| 507 | flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0") |
| 508 | } |
Yabin Cui | 6be405e | 2017-10-19 15:52:11 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 511 | if Bool(sanitize.Properties.Sanitize.Fuzzer) { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 512 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link") |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 513 | |
| 514 | // TODO(b/131771163): LTO and Fuzzer support is mutually incompatible. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 515 | _, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags) |
| 516 | _, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags) |
| 517 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto") |
| 518 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto") |
Mitch Phillips | 7438475 | 2019-06-17 10:33:52 -0700 | [diff] [blame] | 519 | |
Mitch Phillips | b8e593d | 2019-10-09 17:18:59 -0700 | [diff] [blame] | 520 | // TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries |
| 521 | // discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus |
| 522 | // doesn't match the linker script due to the "__emutls_v." prefix). |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 523 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth") |
| 524 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth") |
Mitch Phillips | b8e593d | 2019-10-09 17:18:59 -0700 | [diff] [blame] | 525 | |
Mitch Phillips | 7438475 | 2019-06-17 10:33:52 -0700 | [diff] [blame] | 526 | // TODO(b/133876586): Experimental PM breaks sanitizer coverage. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 527 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager") |
Mitch Phillips | b9b3e79 | 2019-08-28 12:41:07 -0700 | [diff] [blame] | 528 | |
| 529 | // Disable fortify for fuzzing builds. Generally, we'll be building with |
| 530 | // UBSan or ASan here and the fortify checks pollute the stack traces. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 531 | flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE") |
Mitch Phillips | 734b4cb | 2019-12-10 08:44:52 -0800 | [diff] [blame] | 532 | |
| 533 | // Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's |
| 534 | // linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and |
| 535 | // their libraries to /data/fuzz/<arch>/lib, any transient shared library gets |
| 536 | // the DT_RUNPATH from the shared library above it, and not the executable, |
| 537 | // meaning that the lookup falls back to the system. Adding the $ORIGIN to the |
| 538 | // DT_RUNPATH here means that transient shared libraries can be found |
| 539 | // colocated with their parents. |
| 540 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 543 | if Bool(sanitize.Properties.Sanitize.Cfi) { |
Evgenii Stepanov | 7ebf9fa | 2017-01-20 14:13:06 -0800 | [diff] [blame] | 544 | if ctx.Arch().ArchType == android.Arm { |
| 545 | // __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up |
| 546 | // to do this on a function basis, so force Thumb on the entire module. |
| 547 | flags.RequiredInstructionSet = "thumb" |
| 548 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 549 | |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 550 | flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...) |
| 551 | flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...) |
Cindy Zhou | 8cd45de | 2020-11-16 08:41:00 -0800 | [diff] [blame^] | 552 | if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) { |
| 553 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables") |
| 554 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 555 | // Only append the default visibility flag if -fvisibility has not already been set |
| 556 | // to hidden. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 557 | if !inList("-fvisibility=hidden", flags.Local.CFlags) { |
| 558 | flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default") |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 559 | } |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 560 | flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 561 | |
| 562 | if ctx.staticBinary() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 563 | _, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags) |
| 564 | _, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 565 | } |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 568 | if Bool(sanitize.Properties.Sanitize.Integer_overflow) { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 569 | flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...) |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 572 | if len(sanitize.Properties.Sanitizers) > 0 { |
| 573 | sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",") |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 574 | |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 575 | flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg) |
| 576 | flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 577 | if ctx.Host() { |
Evgenii Stepanov | 76cee23 | 2017-01-27 15:44:44 -0800 | [diff] [blame] | 578 | // Host sanitizers only link symbols in the final executable, so |
| 579 | // there will always be undefined symbols in intermediate libraries. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 580 | _, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags) |
| 581 | flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg) |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 582 | |
| 583 | // non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers |
| 584 | if !ctx.toolchain().Bionic() { |
| 585 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function") |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if enableMinimalRuntime(sanitize) { |
| 590 | flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " ")) |
| 591 | flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...) |
| 592 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib) |
| 593 | if !ctx.toolchain().Bionic() { |
| 594 | flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 595 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 596 | } |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 597 | |
| 598 | if Bool(sanitize.Properties.Sanitize.Fuzzer) { |
| 599 | // When fuzzing, we wish to crash with diagnostics on any bug. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 600 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all") |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 601 | } else if ctx.Host() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 602 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all") |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 603 | } else { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 604 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort") |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 605 | } |
Chih-Hung Hsieh | 3567e62 | 2018-11-15 14:01:36 -0800 | [diff] [blame] | 606 | // http://b/119329758, Android core does not boot up with this sanitizer yet. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 607 | if toDisableImplicitIntegerChange(flags.Local.CFlags) { |
| 608 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change") |
Chih-Hung Hsieh | 3567e62 | 2018-11-15 14:01:36 -0800 | [diff] [blame] | 609 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 610 | } |
| 611 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 612 | if len(sanitize.Properties.DiagSanitizers) > 0 { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 613 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ",")) |
Evgenii Stepanov | 1e405e1 | 2016-08-16 15:39:54 -0700 | [diff] [blame] | 614 | } |
| 615 | // FIXME: enable RTTI if diag + (cfi or vptr) |
| 616 | |
Andreas Gampe | 9707116 | 2017-05-08 13:15:23 -0700 | [diff] [blame] | 617 | if sanitize.Properties.Sanitize.Recover != nil { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 618 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+ |
Andreas Gampe | 9707116 | 2017-05-08 13:15:23 -0700 | [diff] [blame] | 619 | strings.Join(sanitize.Properties.Sanitize.Recover, ",")) |
| 620 | } |
| 621 | |
Ivan Lozano | 7929bba | 2018-12-12 09:36:31 -0800 | [diff] [blame] | 622 | if sanitize.Properties.Sanitize.Diag.No_recover != nil { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 623 | flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+ |
Ivan Lozano | 7929bba | 2018-12-12 09:36:31 -0800 | [diff] [blame] | 624 | strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ",")) |
| 625 | } |
| 626 | |
Pirama Arumuga Nainar | 6c4ccca | 2020-07-27 11:49:51 -0700 | [diff] [blame] | 627 | blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist) |
| 628 | if blocklist.Valid() { |
| 629 | flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blocklist.String()) |
| 630 | flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path()) |
| 631 | } |
| 632 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 633 | return flags |
| 634 | } |
| 635 | |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 636 | func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 637 | // Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing |
| 638 | // both the sanitized and non-sanitized variants to make without a name conflict. |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 639 | if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 640 | if Bool(sanitize.Properties.Sanitize.Cfi) { |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 641 | entries.SubName += ".cfi" |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 642 | } |
| 643 | if Bool(sanitize.Properties.Sanitize.Hwaddress) { |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 644 | entries.SubName += ".hwasan" |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 645 | } |
| 646 | if Bool(sanitize.Properties.Sanitize.Scs) { |
Colin Cross | d80cbca | 2020-02-24 12:01:37 -0800 | [diff] [blame] | 647 | entries.SubName += ".scs" |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 648 | } |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 649 | } |
Colin Cross | 8ff9ef4 | 2017-05-08 13:44:11 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Vishwath Mohan | 1dd8839 | 2017-03-29 22:00:18 -0700 | [diff] [blame] | 652 | func (sanitize *sanitize) inSanitizerDir() bool { |
| 653 | return sanitize.Properties.InSanitizerDir |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 656 | func (sanitize *sanitize) getSanitizerBoolPtr(t sanitizerType) *bool { |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 657 | switch t { |
| 658 | case asan: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 659 | return sanitize.Properties.Sanitize.Address |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 660 | case hwasan: |
| 661 | return sanitize.Properties.Sanitize.Hwaddress |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 662 | case tsan: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 663 | return sanitize.Properties.Sanitize.Thread |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 664 | case intOverflow: |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 665 | return sanitize.Properties.Sanitize.Integer_overflow |
| 666 | case cfi: |
| 667 | return sanitize.Properties.Sanitize.Cfi |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 668 | case scs: |
| 669 | return sanitize.Properties.Sanitize.Scs |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 670 | case fuzzer: |
| 671 | return sanitize.Properties.Sanitize.Fuzzer |
Vishwath Mohan | 9522930 | 2017-08-11 00:53:16 +0000 | [diff] [blame] | 672 | default: |
| 673 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 674 | } |
| 675 | } |
| 676 | |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 677 | func (sanitize *sanitize) isUnsanitizedVariant() bool { |
| 678 | return !sanitize.isSanitizerEnabled(asan) && |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 679 | !sanitize.isSanitizerEnabled(hwasan) && |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 680 | !sanitize.isSanitizerEnabled(tsan) && |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 681 | !sanitize.isSanitizerEnabled(cfi) && |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 682 | !sanitize.isSanitizerEnabled(scs) && |
| 683 | !sanitize.isSanitizerEnabled(fuzzer) |
Dan Albert | 7d1eecf | 2018-01-19 12:30:45 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Jayant Chowdhary | b7e08ca | 2018-05-10 15:29:24 -0700 | [diff] [blame] | 686 | func (sanitize *sanitize) isVariantOnProductionDevice() bool { |
| 687 | return !sanitize.isSanitizerEnabled(asan) && |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 688 | !sanitize.isSanitizerEnabled(hwasan) && |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 689 | !sanitize.isSanitizerEnabled(tsan) && |
| 690 | !sanitize.isSanitizerEnabled(fuzzer) |
Jayant Chowdhary | b7e08ca | 2018-05-10 15:29:24 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 693 | func (sanitize *sanitize) SetSanitizer(t sanitizerType, b bool) { |
| 694 | switch t { |
| 695 | case asan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 696 | sanitize.Properties.Sanitize.Address = boolPtr(b) |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 697 | case hwasan: |
| 698 | sanitize.Properties.Sanitize.Hwaddress = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 699 | case tsan: |
Evgenii Stepanov | fcfe56d | 2016-07-07 10:54:07 -0700 | [diff] [blame] | 700 | sanitize.Properties.Sanitize.Thread = boolPtr(b) |
Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 701 | case intOverflow: |
| 702 | sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 703 | case cfi: |
| 704 | sanitize.Properties.Sanitize.Cfi = boolPtr(b) |
Peter Collingbourne | 8c7e6e2 | 2018-11-19 16:03:58 -0800 | [diff] [blame] | 705 | case scs: |
| 706 | sanitize.Properties.Sanitize.Scs = boolPtr(b) |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 707 | case fuzzer: |
| 708 | sanitize.Properties.Sanitize.Fuzzer = boolPtr(b) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 709 | default: |
| 710 | panic(fmt.Errorf("unknown sanitizerType %d", t)) |
| 711 | } |
| 712 | if b { |
| 713 | sanitize.Properties.SanitizerEnabled = true |
| 714 | } |
| 715 | } |
| 716 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 717 | // Check if the sanitizer is explicitly disabled (as opposed to nil by |
| 718 | // virtue of not being set). |
| 719 | func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t sanitizerType) bool { |
| 720 | if sanitize == nil { |
| 721 | return false |
| 722 | } |
| 723 | |
| 724 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 725 | return sanitizerVal != nil && *sanitizerVal == false |
| 726 | } |
| 727 | |
| 728 | // There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled) |
| 729 | // because enabling a sanitizer either directly (via the blueprint) or |
| 730 | // indirectly (via a mutator) sets the bool ptr to true, and you can't |
| 731 | // distinguish between the cases. It isn't needed though - both cases can be |
| 732 | // treated identically. |
| 733 | func (sanitize *sanitize) isSanitizerEnabled(t sanitizerType) bool { |
| 734 | if sanitize == nil { |
| 735 | return false |
| 736 | } |
| 737 | |
| 738 | sanitizerVal := sanitize.getSanitizerBoolPtr(t) |
| 739 | return sanitizerVal != nil && *sanitizerVal == true |
| 740 | } |
| 741 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 742 | func isSanitizableDependencyTag(tag blueprint.DependencyTag) bool { |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 743 | switch t := tag.(type) { |
| 744 | case dependencyTag: |
| 745 | return t == reuseObjTag || t == objDepTag |
| 746 | case libraryDependencyTag: |
| 747 | return true |
| 748 | default: |
| 749 | return false |
| 750 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 753 | // Determines if the current module is a static library going to be captured |
| 754 | // as vendor snapshot. Such modules must create both cfi and non-cfi variants, |
| 755 | // except for ones which explicitly disable cfi. |
| 756 | func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool { |
Bill Peckham | 945441c | 2020-08-31 16:07:58 -0700 | [diff] [blame] | 757 | if isVendorProprietaryModule(mctx) { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 758 | return false |
| 759 | } |
| 760 | |
| 761 | c := mctx.Module().(*Module) |
| 762 | |
| 763 | if !c.inVendor() { |
| 764 | return false |
| 765 | } |
| 766 | |
| 767 | if !c.static() { |
| 768 | return false |
| 769 | } |
| 770 | |
| 771 | if c.Prebuilt() != nil { |
| 772 | return false |
| 773 | } |
| 774 | |
| 775 | return c.sanitize != nil && |
| 776 | !Bool(c.sanitize.Properties.Sanitize.Never) && |
| 777 | !c.sanitize.isSanitizerExplicitlyDisabled(cfi) |
| 778 | } |
| 779 | |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 780 | // Propagate sanitizer requirements down from binaries |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 781 | func sanitizerDepsMutator(t sanitizerType) func(android.TopDownMutatorContext) { |
| 782 | return func(mctx android.TopDownMutatorContext) { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 783 | if c, ok := mctx.Module().(*Module); ok { |
| 784 | enabled := c.sanitize.isSanitizerEnabled(t) |
| 785 | if t == cfi && needsCfiForVendorSnapshot(mctx) { |
| 786 | // We shouldn't change the result of isSanitizerEnabled(cfi) to correctly |
| 787 | // determine defaultVariation in sanitizerMutator below. |
| 788 | // Instead, just mark SanitizeDep to forcefully create cfi variant. |
| 789 | enabled = true |
| 790 | c.sanitize.Properties.SanitizeDep = true |
| 791 | } |
| 792 | if enabled { |
| 793 | mctx.WalkDeps(func(child, parent android.Module) bool { |
| 794 | if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) { |
| 795 | return false |
| 796 | } |
| 797 | if d, ok := child.(*Module); ok && d.sanitize != nil && |
| 798 | !Bool(d.sanitize.Properties.Sanitize.Never) && |
| 799 | !d.sanitize.isSanitizerExplicitlyDisabled(t) { |
| 800 | if t == cfi || t == hwasan || t == scs { |
| 801 | if d.static() { |
| 802 | d.sanitize.Properties.SanitizeDep = true |
| 803 | } |
| 804 | } else { |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 805 | d.sanitize.Properties.SanitizeDep = true |
| 806 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 807 | } |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 808 | return true |
| 809 | }) |
| 810 | } |
Jiyong Park | f97782b | 2019-02-13 20:28:58 +0900 | [diff] [blame] | 811 | } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok { |
| 812 | // If an APEX module includes a lib which is enabled for a sanitizer T, then |
| 813 | // the APEX module is also enabled for the same sanitizer type. |
| 814 | mctx.VisitDirectDeps(func(child android.Module) { |
| 815 | if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) { |
| 816 | sanitizeable.EnableSanitizer(t.name()) |
| 817 | } |
| 818 | }) |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | } |
| 822 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 823 | // 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] | 824 | func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) { |
| 825 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
| 826 | mctx.WalkDeps(func(child, parent android.Module) bool { |
| 827 | if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) { |
| 828 | return false |
| 829 | } |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 830 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 831 | d, ok := child.(*Module) |
| 832 | if !ok || !d.static() { |
| 833 | return false |
| 834 | } |
| 835 | if d.sanitize != nil { |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 836 | if enableMinimalRuntime(d.sanitize) { |
| 837 | // If a static dependency is built with the minimal runtime, |
| 838 | // make sure we include the ubsan minimal runtime. |
| 839 | c.sanitize.Properties.MinimalRuntimeDep = true |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 840 | } else if enableUbsanRuntime(d.sanitize) { |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 841 | // If a static dependency runs with full ubsan diagnostics, |
| 842 | // make sure we include the ubsan runtime. |
| 843 | c.sanitize.Properties.UbsanRuntimeDep = true |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 844 | } |
Colin Cross | 0b90833 | 2019-06-19 23:00:20 -0700 | [diff] [blame] | 845 | |
| 846 | if c.sanitize.Properties.MinimalRuntimeDep && |
| 847 | c.sanitize.Properties.UbsanRuntimeDep { |
| 848 | // both flags that this mutator might set are true, so don't bother recursing |
| 849 | return false |
| 850 | } |
| 851 | |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 852 | if c.Os() == android.Linux { |
| 853 | c.sanitize.Properties.BuiltinsDep = true |
| 854 | } |
| 855 | |
Colin Cross | 0b90833 | 2019-06-19 23:00:20 -0700 | [diff] [blame] | 856 | return true |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 857 | } |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 858 | |
Jose Galmes | f729458 | 2020-11-13 12:07:36 -0800 | [diff] [blame] | 859 | if p, ok := d.linker.(*snapshotLibraryDecorator); ok { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 860 | if Bool(p.properties.Sanitize_minimal_dep) { |
| 861 | c.sanitize.Properties.MinimalRuntimeDep = true |
| 862 | } |
| 863 | if Bool(p.properties.Sanitize_ubsan_dep) { |
| 864 | c.sanitize.Properties.UbsanRuntimeDep = true |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | return false |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 869 | }) |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 873 | // Add the dependency to the runtime library for each of the sanitizer variants |
| 874 | func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 875 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
Pirama Arumuga Nainar | 6aa2102 | 2019-01-25 00:20:35 +0000 | [diff] [blame] | 876 | if !c.Enabled() { |
| 877 | return |
| 878 | } |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 879 | var sanitizers []string |
| 880 | var diagSanitizers []string |
| 881 | |
| 882 | if Bool(c.sanitize.Properties.Sanitize.All_undefined) { |
| 883 | sanitizers = append(sanitizers, "undefined") |
| 884 | } else { |
| 885 | if Bool(c.sanitize.Properties.Sanitize.Undefined) { |
| 886 | sanitizers = append(sanitizers, |
| 887 | "bool", |
| 888 | "integer-divide-by-zero", |
| 889 | "return", |
| 890 | "returns-nonnull-attribute", |
| 891 | "shift-exponent", |
| 892 | "unreachable", |
| 893 | "vla-bound", |
| 894 | // TODO(danalbert): The following checks currently have compiler performance issues. |
| 895 | //"alignment", |
| 896 | //"bounds", |
| 897 | //"enum", |
| 898 | //"float-cast-overflow", |
| 899 | //"float-divide-by-zero", |
| 900 | //"nonnull-attribute", |
| 901 | //"null", |
| 902 | //"shift-base", |
| 903 | //"signed-integer-overflow", |
| 904 | // TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on. |
| 905 | // https://llvm.org/PR19302 |
| 906 | // http://reviews.llvm.org/D6974 |
| 907 | // "object-size", |
| 908 | ) |
| 909 | } |
| 910 | sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...) |
| 911 | } |
| 912 | |
| 913 | if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) { |
| 914 | diagSanitizers = append(diagSanitizers, "undefined") |
| 915 | } |
| 916 | |
| 917 | diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...) |
| 918 | |
| 919 | if Bool(c.sanitize.Properties.Sanitize.Address) { |
| 920 | sanitizers = append(sanitizers, "address") |
| 921 | diagSanitizers = append(diagSanitizers, "address") |
| 922 | } |
| 923 | |
| 924 | if Bool(c.sanitize.Properties.Sanitize.Hwaddress) { |
| 925 | sanitizers = append(sanitizers, "hwaddress") |
| 926 | } |
| 927 | |
| 928 | if Bool(c.sanitize.Properties.Sanitize.Thread) { |
| 929 | sanitizers = append(sanitizers, "thread") |
| 930 | } |
| 931 | |
| 932 | if Bool(c.sanitize.Properties.Sanitize.Safestack) { |
| 933 | sanitizers = append(sanitizers, "safe-stack") |
| 934 | } |
| 935 | |
| 936 | if Bool(c.sanitize.Properties.Sanitize.Cfi) { |
| 937 | sanitizers = append(sanitizers, "cfi") |
| 938 | |
| 939 | if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) { |
| 940 | diagSanitizers = append(diagSanitizers, "cfi") |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) { |
| 945 | sanitizers = append(sanitizers, "unsigned-integer-overflow") |
| 946 | sanitizers = append(sanitizers, "signed-integer-overflow") |
| 947 | if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) { |
| 948 | diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow") |
| 949 | diagSanitizers = append(diagSanitizers, "signed-integer-overflow") |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | if Bool(c.sanitize.Properties.Sanitize.Scudo) { |
| 954 | sanitizers = append(sanitizers, "scudo") |
| 955 | } |
| 956 | |
| 957 | if Bool(c.sanitize.Properties.Sanitize.Scs) { |
| 958 | sanitizers = append(sanitizers, "shadow-call-stack") |
| 959 | } |
| 960 | |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 961 | if Bool(c.sanitize.Properties.Sanitize.Fuzzer) { |
| 962 | sanitizers = append(sanitizers, "fuzzer-no-link") |
| 963 | } |
| 964 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 965 | // Save the list of sanitizers. These will be used again when generating |
| 966 | // the build rules (for Cflags, etc.) |
| 967 | c.sanitize.Properties.Sanitizers = sanitizers |
| 968 | c.sanitize.Properties.DiagSanitizers = diagSanitizers |
| 969 | |
Ivan Lozano | f3b190f | 2020-03-06 12:01:21 -0500 | [diff] [blame] | 970 | // TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used. |
| 971 | if c.Host() { |
| 972 | diagSanitizers = sanitizers |
| 973 | } |
| 974 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 975 | // Determine the runtime library required |
| 976 | runtimeLibrary := "" |
Ryan Prichard | b49fe1b | 2019-10-11 15:03:34 -0700 | [diff] [blame] | 977 | var extraStaticDeps []string |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 978 | toolchain := c.toolchain(mctx) |
| 979 | if Bool(c.sanitize.Properties.Sanitize.Address) { |
| 980 | runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain) |
| 981 | } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) { |
| 982 | if c.staticBinary() { |
| 983 | runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain) |
Ryan Prichard | b49fe1b | 2019-10-11 15:03:34 -0700 | [diff] [blame] | 984 | extraStaticDeps = []string{"libdl"} |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 985 | } else { |
| 986 | runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain) |
| 987 | } |
| 988 | } else if Bool(c.sanitize.Properties.Sanitize.Thread) { |
| 989 | runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain) |
| 990 | } else if Bool(c.sanitize.Properties.Sanitize.Scudo) { |
| 991 | if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep { |
| 992 | runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain) |
| 993 | } else { |
| 994 | runtimeLibrary = config.ScudoRuntimeLibrary(toolchain) |
| 995 | } |
Mitch Phillips | b8e593d | 2019-10-09 17:18:59 -0700 | [diff] [blame] | 996 | } else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep || |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 997 | Bool(c.sanitize.Properties.Sanitize.Fuzzer) || |
| 998 | Bool(c.sanitize.Properties.Sanitize.Undefined) || |
| 999 | Bool(c.sanitize.Properties.Sanitize.All_undefined) { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1000 | runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain) |
| 1001 | } |
| 1002 | |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1003 | if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) { |
| 1004 | // UBSan is supported on non-bionic linux host builds as well |
Jooyung Han | 0302a84 | 2019-10-30 18:43:49 +0900 | [diff] [blame] | 1005 | if isLlndkLibrary(runtimeLibrary, mctx.Config()) && !c.static() && c.UseVndk() { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1006 | runtimeLibrary = runtimeLibrary + llndkLibrarySuffix |
| 1007 | } |
| 1008 | |
| 1009 | // Adding dependency to the runtime library. We are using *FarVariation* |
| 1010 | // because the runtime libraries themselves are not mutated by sanitizer |
| 1011 | // mutators and thus don't have sanitizer variants whereas this module |
| 1012 | // has been already mutated. |
| 1013 | // |
| 1014 | // Note that by adding dependency with {static|shared}DepTag, the lib is |
| 1015 | // added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module |
| 1016 | if c.staticBinary() { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 1017 | deps := append(extraStaticDeps, runtimeLibrary) |
| 1018 | // If we're using snapshots and in vendor, redirect to snapshot whenever possible |
| 1019 | if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() { |
| 1020 | snapshots := vendorSnapshotStaticLibs(mctx.Config()) |
| 1021 | for idx, dep := range deps { |
| 1022 | if lib, ok := snapshots.get(dep, mctx.Arch().ArchType); ok { |
| 1023 | deps[idx] = lib |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1028 | // static executable gets static runtime libs |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 1029 | depTag := libraryDependencyTag{Kind: staticLibraryDependency} |
Colin Cross | 4250733 | 2020-08-21 16:15:23 -0700 | [diff] [blame] | 1030 | variations := append(mctx.Target().Variations(), |
| 1031 | blueprint.Variation{Mutator: "link", Variation: "static"}) |
| 1032 | if c.Device() { |
| 1033 | variations = append(variations, c.ImageVariation()) |
| 1034 | } |
| 1035 | mctx.AddFarVariationDependencies(variations, depTag, deps...) |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1036 | } else if !c.static() && !c.header() { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 1037 | // If we're using snapshots and in vendor, redirect to snapshot whenever possible |
| 1038 | if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() { |
| 1039 | snapshots := vendorSnapshotSharedLibs(mctx.Config()) |
| 1040 | if lib, ok := snapshots.get(runtimeLibrary, mctx.Arch().ArchType); ok { |
| 1041 | runtimeLibrary = lib |
| 1042 | } |
| 1043 | } |
| 1044 | |
Jiyong Park | 3b1746a | 2019-01-29 11:15:04 +0900 | [diff] [blame] | 1045 | // dynamic executable and shared libs get shared runtime libs |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 1046 | depTag := libraryDependencyTag{Kind: sharedLibraryDependency, Order: earlyLibraryDependency} |
Colin Cross | 4250733 | 2020-08-21 16:15:23 -0700 | [diff] [blame] | 1047 | variations := append(mctx.Target().Variations(), |
| 1048 | blueprint.Variation{Mutator: "link", Variation: "shared"}) |
| 1049 | if c.Device() { |
| 1050 | variations = append(variations, c.ImageVariation()) |
| 1051 | } |
Colin Cross | e7257d2 | 2020-09-24 09:56:18 -0700 | [diff] [blame] | 1052 | c.addSharedLibDependenciesWithVersions(mctx, variations, depTag, runtimeLibrary, "", true) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1053 | } |
| 1054 | // static lib does not have dependency to the runtime library. The |
| 1055 | // dependency will be added to the executables or shared libs using |
| 1056 | // the static lib. |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | type Sanitizeable interface { |
| 1062 | android.Module |
Jiyong Park | 388ef3f | 2019-01-28 19:47:32 +0900 | [diff] [blame] | 1063 | IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool |
Jiyong Park | f97782b | 2019-02-13 20:28:58 +0900 | [diff] [blame] | 1064 | EnableSanitizer(sanitizerName string) |
Jooyung Han | 8ce8db9 | 2020-05-15 19:05:05 +0900 | [diff] [blame] | 1065 | AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1066 | } |
| 1067 | |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1068 | // Create sanitized variants for modules that need them |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 1069 | func sanitizerMutator(t sanitizerType) func(android.BottomUpMutatorContext) { |
| 1070 | return func(mctx android.BottomUpMutatorContext) { |
Vishwath Mohan | e615345 | 2017-08-11 00:52:44 +0000 | [diff] [blame] | 1071 | if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1072 | if c.isDependencyRoot() && c.sanitize.isSanitizerEnabled(t) { |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 1073 | modules := mctx.CreateVariations(t.variationName()) |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 1074 | modules[0].(*Module).sanitize.SetSanitizer(t, true) |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1075 | } else if c.sanitize.isSanitizerEnabled(t) || c.sanitize.Properties.SanitizeDep { |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1076 | isSanitizerEnabled := c.sanitize.isSanitizerEnabled(t) |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1077 | if c.static() || c.header() || t == asan || t == fuzzer { |
| 1078 | // Static and header libs are split into non-sanitized and sanitized variants. |
| 1079 | // Shared libs are not split. However, for asan and fuzzer, we split even for shared |
| 1080 | // libs because a library sanitized for asan/fuzzer can't be linked from a library |
| 1081 | // that isn't sanitized for asan/fuzzer. |
| 1082 | // |
| 1083 | // Note for defaultVariation: since we don't split for shared libs but for static/header |
| 1084 | // libs, it is possible for the sanitized variant of a static/header lib to depend |
| 1085 | // on non-sanitized variant of a shared lib. Such unfulfilled variation causes an |
| 1086 | // error when the module is split. defaultVariation is the name of the variation that |
| 1087 | // will be used when such a dangling dependency occurs during the split of the current |
| 1088 | // module. By setting it to the name of the sanitized variation, the dangling dependency |
| 1089 | // is redirected to the sanitized variant of the dependent module. |
| 1090 | defaultVariation := t.variationName() |
| 1091 | mctx.SetDefaultDependencyVariation(&defaultVariation) |
| 1092 | modules := mctx.CreateVariations("", t.variationName()) |
| 1093 | modules[0].(*Module).sanitize.SetSanitizer(t, false) |
| 1094 | modules[1].(*Module).sanitize.SetSanitizer(t, true) |
| 1095 | modules[0].(*Module).sanitize.Properties.SanitizeDep = false |
| 1096 | modules[1].(*Module).sanitize.Properties.SanitizeDep = false |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1097 | |
Ivan Lozano | 4774a81 | 2020-03-10 16:23:57 -0400 | [diff] [blame] | 1098 | if mctx.Device() && t.incompatibleWithCfi() { |
| 1099 | // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that |
| 1100 | // are incompatible with cfi |
| 1101 | modules[1].(*Module).sanitize.SetSanitizer(cfi, false) |
| 1102 | } |
| 1103 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1104 | // For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants |
| 1105 | // to Make, because the sanitized version has a different suffix in name. |
| 1106 | // For other types of sanitizers, suppress the variation that is disabled. |
| 1107 | if t != cfi && t != scs && t != hwasan { |
| 1108 | if isSanitizerEnabled { |
| 1109 | modules[0].(*Module).Properties.PreventInstall = true |
| 1110 | modules[0].(*Module).Properties.HideFromMake = true |
| 1111 | } else { |
| 1112 | modules[1].(*Module).Properties.PreventInstall = true |
| 1113 | modules[1].(*Module).Properties.HideFromMake = true |
| 1114 | } |
| 1115 | } |
Vishwath Mohan | b743e9c | 2017-11-01 09:20:21 +0000 | [diff] [blame] | 1116 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1117 | // Export the static lib name to make |
Dan Willemsen | b5b2aba | 2020-05-03 21:28:32 -0700 | [diff] [blame] | 1118 | if c.static() && c.ExportedToMake() { |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1119 | if t == cfi { |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1120 | cfiStaticLibs(mctx.Config()).add(c, c.Name()) |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1121 | } else if t == hwasan { |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1122 | hwasanStaticLibs(mctx.Config()).add(c, c.Name()) |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | } else { |
| 1126 | // Shared libs are not split. Only the sanitized variant is created. |
| 1127 | modules := mctx.CreateVariations(t.variationName()) |
| 1128 | modules[0].(*Module).sanitize.SetSanitizer(t, true) |
| 1129 | modules[0].(*Module).sanitize.Properties.SanitizeDep = false |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1130 | |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1131 | // locate the asan libraries under /data/asan |
| 1132 | if mctx.Device() && t == asan && isSanitizerEnabled { |
| 1133 | modules[0].(*Module).sanitize.Properties.InSanitizerDir = true |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1134 | } |
Ivan Lozano | 4774a81 | 2020-03-10 16:23:57 -0400 | [diff] [blame] | 1135 | |
| 1136 | if mctx.Device() && t.incompatibleWithCfi() { |
| 1137 | // TODO: Make sure that cfi mutator runs "after" any of the sanitizers that |
| 1138 | // are incompatible with cfi |
| 1139 | modules[0].(*Module).sanitize.SetSanitizer(cfi, false) |
| 1140 | } |
Vishwath Mohan | e21fe42 | 2017-11-01 19:42:45 -0700 | [diff] [blame] | 1141 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 1142 | } |
| 1143 | c.sanitize.Properties.SanitizeDep = false |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 1144 | } else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) { |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 1145 | // APEX modules fall here |
Jooyung Han | 8ce8db9 | 2020-05-15 19:05:05 +0900 | [diff] [blame] | 1146 | sanitizeable.AddSanitizerDependencies(mctx, t.name()) |
Jiyong Park | 8222663 | 2019-02-01 10:50:50 +0900 | [diff] [blame] | 1147 | mctx.CreateVariations(t.variationName()) |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 1148 | } else if c, ok := mctx.Module().(*Module); ok { |
| 1149 | // Check if it's a snapshot module supporting sanitizer |
| 1150 | if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) { |
| 1151 | // Set default variation as above. |
| 1152 | defaultVariation := t.variationName() |
| 1153 | mctx.SetDefaultDependencyVariation(&defaultVariation) |
| 1154 | modules := mctx.CreateVariations("", t.variationName()) |
| 1155 | modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false) |
| 1156 | modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true) |
| 1157 | |
| 1158 | // Export the static lib name to make |
| 1159 | if c.static() && c.ExportedToMake() { |
| 1160 | if t == cfi { |
| 1161 | // use BaseModuleName which is the name for Make. |
| 1162 | cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName()) |
| 1163 | } |
| 1164 | } |
| 1165 | } |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 1166 | } |
| 1167 | } |
| 1168 | } |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1169 | |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1170 | type sanitizerStaticLibsMap struct { |
| 1171 | // libsMap contains one list of modules per each image and each arch. |
| 1172 | // e.g. libs[vendor]["arm"] contains arm modules installed to vendor |
| 1173 | libsMap map[imageVariantType]map[string][]string |
| 1174 | libsMapLock sync.Mutex |
| 1175 | sanitizerType sanitizerType |
| 1176 | } |
| 1177 | |
| 1178 | func newSanitizerStaticLibsMap(t sanitizerType) *sanitizerStaticLibsMap { |
| 1179 | return &sanitizerStaticLibsMap{ |
| 1180 | sanitizerType: t, |
| 1181 | libsMap: make(map[imageVariantType]map[string][]string), |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | // Add the current module to sanitizer static libs maps |
| 1186 | // Each module should pass its exported name as names of Make and Soong can differ. |
| 1187 | func (s *sanitizerStaticLibsMap) add(c *Module, name string) { |
| 1188 | image := c.getImageVariantType() |
| 1189 | arch := c.Arch().ArchType.String() |
| 1190 | |
| 1191 | s.libsMapLock.Lock() |
| 1192 | defer s.libsMapLock.Unlock() |
| 1193 | |
| 1194 | if _, ok := s.libsMap[image]; !ok { |
| 1195 | s.libsMap[image] = make(map[string][]string) |
| 1196 | } |
| 1197 | |
| 1198 | s.libsMap[image][arch] = append(s.libsMap[image][arch], name) |
| 1199 | } |
| 1200 | |
| 1201 | // Exports makefile variables in the following format: |
| 1202 | // SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES |
| 1203 | // e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES |
| 1204 | // These are to be used by use_soong_sanitized_static_libraries. |
| 1205 | // See build/make/core/binary.mk for more details. |
| 1206 | func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) { |
| 1207 | for _, image := range android.SortedStringKeys(s.libsMap) { |
| 1208 | archMap := s.libsMap[imageVariantType(image)] |
| 1209 | for _, arch := range android.SortedStringKeys(archMap) { |
| 1210 | libs := archMap[arch] |
| 1211 | sort.Strings(libs) |
| 1212 | |
| 1213 | key := fmt.Sprintf( |
| 1214 | "SOONG_%s_%s_%s_STATIC_LIBRARIES", |
| 1215 | s.sanitizerType.variationName(), |
| 1216 | image, // already upper |
| 1217 | arch) |
| 1218 | |
| 1219 | ctx.Strict(key, strings.Join(libs, " ")) |
| 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1224 | var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs") |
| 1225 | |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1226 | func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1227 | return config.Once(cfiStaticLibsKey, func() interface{} { |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1228 | return newSanitizerStaticLibsMap(cfi) |
| 1229 | }).(*sanitizerStaticLibsMap) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1230 | } |
| 1231 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1232 | var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs") |
| 1233 | |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1234 | func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 1235 | return config.Once(hwasanStaticLibsKey, func() interface{} { |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1236 | return newSanitizerStaticLibsMap(hwasan) |
| 1237 | }).(*sanitizerStaticLibsMap) |
Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 1238 | } |
| 1239 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1240 | func enableMinimalRuntime(sanitize *sanitize) bool { |
| 1241 | if !Bool(sanitize.Properties.Sanitize.Address) && |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1242 | !Bool(sanitize.Properties.Sanitize.Hwaddress) && |
Mitch Phillips | 5a6ea6c | 2019-05-01 14:42:05 -0700 | [diff] [blame] | 1243 | !Bool(sanitize.Properties.Sanitize.Fuzzer) && |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1244 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1245 | (Bool(sanitize.Properties.Sanitize.Integer_overflow) || |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1246 | len(sanitize.Properties.Sanitize.Misc_undefined) > 0 || |
| 1247 | Bool(sanitize.Properties.Sanitize.Undefined) || |
| 1248 | Bool(sanitize.Properties.Sanitize.All_undefined)) && |
| 1249 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1250 | !(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) || |
| 1251 | Bool(sanitize.Properties.Sanitize.Diag.Cfi) || |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1252 | Bool(sanitize.Properties.Sanitize.Diag.Undefined) || |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1253 | len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) { |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1254 | |
Ivan Lozano | 30c5db2 | 2018-02-21 15:49:20 -0800 | [diff] [blame] | 1255 | return true |
| 1256 | } |
| 1257 | return false |
| 1258 | } |
| 1259 | |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 1260 | func enableUbsanRuntime(sanitize *sanitize) bool { |
| 1261 | return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) || |
Ivan Lozano | 9ac32c7 | 2020-02-19 15:24:02 -0500 | [diff] [blame] | 1262 | Bool(sanitize.Properties.Sanitize.Diag.Undefined) || |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 1263 | len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0 |
| 1264 | } |
| 1265 | |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1266 | func cfiMakeVarsProvider(ctx android.MakeVarsContext) { |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1267 | cfiStaticLibs(ctx.Config()).exportToMake(ctx) |
Vishwath Mohan | e712879 | 2017-11-17 11:08:10 -0800 | [diff] [blame] | 1268 | } |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1269 | |
| 1270 | func hwasanMakeVarsProvider(ctx android.MakeVarsContext) { |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 1271 | hwasanStaticLibs(ctx.Config()).exportToMake(ctx) |
Evgenii Stepanov | d97a6e9 | 2018-08-02 16:19:13 -0700 | [diff] [blame] | 1272 | } |