Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 android |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | "reflect" |
| 20 | "strconv" |
| 21 | "strings" |
| 22 | |
| 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
| 26 | // "neverallow" rules for the build system. |
| 27 | // |
| 28 | // This allows things which aren't related to the build system and are enforced |
| 29 | // for sanity, in progress code refactors, or policy to be expressed in a |
| 30 | // straightforward away disjoint from implementations and tests which should |
| 31 | // work regardless of these restrictions. |
| 32 | // |
| 33 | // A module is disallowed if all of the following are true: |
| 34 | // - it is in one of the "in" paths |
| 35 | // - it is not in one of the "notIn" paths |
| 36 | // - it has all "with" properties matched |
| 37 | // - - values are matched in their entirety |
| 38 | // - - nil is interpreted as an empty string |
| 39 | // - - nested properties are separated with a '.' |
| 40 | // - - if the property is a list, any of the values in the list being matches |
| 41 | // counts as a match |
| 42 | // - it has none of the "without" properties matched (same rules as above) |
| 43 | |
| 44 | func registerNeverallowMutator(ctx RegisterMutatorsContext) { |
| 45 | ctx.BottomUp("neverallow", neverallowMutator).Parallel() |
| 46 | } |
| 47 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 48 | var neverallows = createNeverAllows() |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 49 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 50 | func createNeverAllows() []*rule { |
| 51 | rules := []*rule{} |
| 52 | rules = append(rules, createTrebleRules()...) |
| 53 | rules = append(rules, createLibcoreRules()...) |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 54 | rules = append(rules, createJavaDeviceForHostRules()...) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 55 | return rules |
| 56 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 57 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 58 | func createTrebleRules() []*rule { |
| 59 | return []*rule{ |
| 60 | neverallow(). |
| 61 | in("vendor", "device"). |
| 62 | with("vndk.enabled", "true"). |
| 63 | without("vendor", "true"). |
| 64 | because("the VNDK can never contain a library that is device dependent."), |
| 65 | neverallow(). |
| 66 | with("vndk.enabled", "true"). |
| 67 | without("vendor", "true"). |
| 68 | without("owner", ""). |
| 69 | because("a VNDK module can never have an owner."), |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 70 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 71 | // TODO(b/67974785): always enforce the manifest |
| 72 | neverallow(). |
| 73 | without("name", "libhidltransport"). |
| 74 | with("product_variables.enforce_vintf_manifest.cflags", "*"). |
| 75 | because("manifest enforcement should be independent of ."), |
| 76 | |
| 77 | // TODO(b/67975799): vendor code should always use /vendor/bin/sh |
| 78 | neverallow(). |
| 79 | without("name", "libc_bionic_ndk"). |
| 80 | with("product_variables.treble_linker_namespaces.cflags", "*"). |
| 81 | because("nothing should care if linker namespaces are enabled or not"), |
| 82 | |
| 83 | // Example: |
| 84 | // *neverallow().with("Srcs", "main.cpp")) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func createLibcoreRules() []*rule { |
| 89 | var coreLibraryProjects = []string{ |
| 90 | "libcore", |
| 91 | "external/apache-harmony", |
| 92 | "external/apache-xml", |
| 93 | "external/bouncycastle", |
| 94 | "external/conscrypt", |
| 95 | "external/icu", |
| 96 | "external/okhttp", |
| 97 | "external/wycheproof", |
| 98 | } |
| 99 | |
| 100 | var coreModules = []string{ |
| 101 | "core-all", |
| 102 | "core-oj", |
| 103 | "core-libart", |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 104 | "okhttp", |
| 105 | "bouncycastle", |
| 106 | "conscrypt", |
| 107 | "apache-xml", |
| 108 | } |
| 109 | |
| 110 | // Core library constraints. Prevent targets adding dependencies on core |
| 111 | // library internals, which could lead to compatibility issues with the ART |
| 112 | // mainline module. They should use core.platform.api.stubs instead. |
| 113 | rules := []*rule{ |
| 114 | neverallow(). |
| 115 | notIn(append(coreLibraryProjects, "development")...). |
| 116 | with("no_standard_libs", "true"), |
| 117 | } |
| 118 | |
| 119 | for _, m := range coreModules { |
| 120 | r := neverallow(). |
| 121 | notIn(coreLibraryProjects...). |
| 122 | with("libs", m). |
| 123 | because("Only core libraries projects can depend on " + m) |
| 124 | rules = append(rules, r) |
| 125 | } |
| 126 | return rules |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 129 | func createJavaDeviceForHostRules() []*rule { |
| 130 | javaDeviceForHostProjectsWhitelist := []string{ |
| 131 | "external/robolectric-shadows", |
| 132 | "framework/layoutlib", |
| 133 | } |
| 134 | |
| 135 | return []*rule{ |
| 136 | neverallow(). |
| 137 | notIn(javaDeviceForHostProjectsWhitelist...). |
| 138 | moduleType("java_device_for_host", "java_host_for_device"). |
| 139 | because("java_device_for_host can only be used in whitelisted projects"), |
| 140 | } |
| 141 | } |
| 142 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 143 | func neverallowMutator(ctx BottomUpMutatorContext) { |
| 144 | m, ok := ctx.Module().(Module) |
| 145 | if !ok { |
| 146 | return |
| 147 | } |
| 148 | |
| 149 | dir := ctx.ModuleDir() + "/" |
| 150 | properties := m.GetProperties() |
| 151 | |
| 152 | for _, n := range neverallows { |
| 153 | if !n.appliesToPath(dir) { |
| 154 | continue |
| 155 | } |
| 156 | |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 157 | if !n.appliesToModuleType(ctx.ModuleType()) { |
| 158 | continue |
| 159 | } |
| 160 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 161 | if !n.appliesToProperties(properties) { |
| 162 | continue |
| 163 | } |
| 164 | |
| 165 | ctx.ModuleErrorf("violates " + n.String()) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | type ruleProperty struct { |
| 170 | fields []string // e.x.: Vndk.Enabled |
| 171 | value string // e.x.: true |
| 172 | } |
| 173 | |
| 174 | type rule struct { |
| 175 | // User string for why this is a thing. |
| 176 | reason string |
| 177 | |
| 178 | paths []string |
| 179 | unlessPaths []string |
| 180 | |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 181 | moduleTypes []string |
| 182 | unlessModuleTypes []string |
| 183 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 184 | props []ruleProperty |
| 185 | unlessProps []ruleProperty |
| 186 | } |
| 187 | |
| 188 | func neverallow() *rule { |
| 189 | return &rule{} |
| 190 | } |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 191 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 192 | func (r *rule) in(path ...string) *rule { |
| 193 | r.paths = append(r.paths, cleanPaths(path)...) |
| 194 | return r |
| 195 | } |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 196 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 197 | func (r *rule) notIn(path ...string) *rule { |
| 198 | r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...) |
| 199 | return r |
| 200 | } |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 201 | |
| 202 | func (r *rule) moduleType(types ...string) *rule { |
| 203 | r.moduleTypes = append(r.moduleTypes, types...) |
| 204 | return r |
| 205 | } |
| 206 | |
| 207 | func (r *rule) notModuleType(types ...string) *rule { |
| 208 | r.unlessModuleTypes = append(r.unlessModuleTypes, types...) |
| 209 | return r |
| 210 | } |
| 211 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 212 | func (r *rule) with(properties, value string) *rule { |
| 213 | r.props = append(r.props, ruleProperty{ |
| 214 | fields: fieldNamesForProperties(properties), |
| 215 | value: value, |
| 216 | }) |
| 217 | return r |
| 218 | } |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 219 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 220 | func (r *rule) without(properties, value string) *rule { |
| 221 | r.unlessProps = append(r.unlessProps, ruleProperty{ |
| 222 | fields: fieldNamesForProperties(properties), |
| 223 | value: value, |
| 224 | }) |
| 225 | return r |
| 226 | } |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 227 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 228 | func (r *rule) because(reason string) *rule { |
| 229 | r.reason = reason |
| 230 | return r |
| 231 | } |
| 232 | |
| 233 | func (r *rule) String() string { |
| 234 | s := "neverallow" |
| 235 | for _, v := range r.paths { |
| 236 | s += " dir:" + v + "*" |
| 237 | } |
| 238 | for _, v := range r.unlessPaths { |
| 239 | s += " -dir:" + v + "*" |
| 240 | } |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 241 | for _, v := range r.moduleTypes { |
| 242 | s += " type:" + v |
| 243 | } |
| 244 | for _, v := range r.unlessModuleTypes { |
| 245 | s += " -type:" + v |
| 246 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 247 | for _, v := range r.props { |
| 248 | s += " " + strings.Join(v.fields, ".") + "=" + v.value |
| 249 | } |
| 250 | for _, v := range r.unlessProps { |
| 251 | s += " -" + strings.Join(v.fields, ".") + "=" + v.value |
| 252 | } |
| 253 | if len(r.reason) != 0 { |
| 254 | s += " which is restricted because " + r.reason |
| 255 | } |
| 256 | return s |
| 257 | } |
| 258 | |
| 259 | func (r *rule) appliesToPath(dir string) bool { |
| 260 | includePath := len(r.paths) == 0 || hasAnyPrefix(dir, r.paths) |
| 261 | excludePath := hasAnyPrefix(dir, r.unlessPaths) |
| 262 | return includePath && !excludePath |
| 263 | } |
| 264 | |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 265 | func (r *rule) appliesToModuleType(moduleType string) bool { |
| 266 | return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes) |
| 267 | } |
| 268 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 269 | func (r *rule) appliesToProperties(properties []interface{}) bool { |
| 270 | includeProps := hasAllProperties(properties, r.props) |
| 271 | excludeProps := hasAnyProperty(properties, r.unlessProps) |
| 272 | return includeProps && !excludeProps |
| 273 | } |
| 274 | |
| 275 | // assorted utils |
| 276 | |
| 277 | func cleanPaths(paths []string) []string { |
| 278 | res := make([]string, len(paths)) |
| 279 | for i, v := range paths { |
| 280 | res[i] = filepath.Clean(v) + "/" |
| 281 | } |
| 282 | return res |
| 283 | } |
| 284 | |
| 285 | func fieldNamesForProperties(propertyNames string) []string { |
| 286 | names := strings.Split(propertyNames, ".") |
| 287 | for i, v := range names { |
| 288 | names[i] = proptools.FieldNameForProperty(v) |
| 289 | } |
| 290 | return names |
| 291 | } |
| 292 | |
| 293 | func hasAnyPrefix(s string, prefixes []string) bool { |
| 294 | for _, prefix := range prefixes { |
| 295 | if strings.HasPrefix(s, prefix) { |
| 296 | return true |
| 297 | } |
| 298 | } |
| 299 | return false |
| 300 | } |
| 301 | |
| 302 | func hasAnyProperty(properties []interface{}, props []ruleProperty) bool { |
| 303 | for _, v := range props { |
| 304 | if hasProperty(properties, v) { |
| 305 | return true |
| 306 | } |
| 307 | } |
| 308 | return false |
| 309 | } |
| 310 | |
| 311 | func hasAllProperties(properties []interface{}, props []ruleProperty) bool { |
| 312 | for _, v := range props { |
| 313 | if !hasProperty(properties, v) { |
| 314 | return false |
| 315 | } |
| 316 | } |
| 317 | return true |
| 318 | } |
| 319 | |
| 320 | func hasProperty(properties []interface{}, prop ruleProperty) bool { |
| 321 | for _, propertyStruct := range properties { |
| 322 | propertiesValue := reflect.ValueOf(propertyStruct).Elem() |
| 323 | for _, v := range prop.fields { |
| 324 | if !propertiesValue.IsValid() { |
| 325 | break |
| 326 | } |
| 327 | propertiesValue = propertiesValue.FieldByName(v) |
| 328 | } |
| 329 | if !propertiesValue.IsValid() { |
| 330 | continue |
| 331 | } |
| 332 | |
| 333 | check := func(v string) bool { |
| 334 | return prop.value == "*" || prop.value == v |
| 335 | } |
| 336 | |
| 337 | if matchValue(propertiesValue, check) { |
| 338 | return true |
| 339 | } |
| 340 | } |
| 341 | return false |
| 342 | } |
| 343 | |
| 344 | func matchValue(value reflect.Value, check func(string) bool) bool { |
| 345 | if !value.IsValid() { |
| 346 | return false |
| 347 | } |
| 348 | |
| 349 | if value.Kind() == reflect.Ptr { |
| 350 | if value.IsNil() { |
| 351 | return check("") |
| 352 | } |
| 353 | value = value.Elem() |
| 354 | } |
| 355 | |
| 356 | switch value.Kind() { |
| 357 | case reflect.String: |
| 358 | return check(value.String()) |
| 359 | case reflect.Bool: |
| 360 | return check(strconv.FormatBool(value.Bool())) |
| 361 | case reflect.Int: |
| 362 | return check(strconv.FormatInt(value.Int(), 10)) |
| 363 | case reflect.Slice: |
| 364 | slice, ok := value.Interface().([]string) |
| 365 | if !ok { |
| 366 | panic("Can only handle slice of string") |
| 367 | } |
| 368 | for _, v := range slice { |
| 369 | if check(v) { |
| 370 | return true |
| 371 | } |
| 372 | } |
| 373 | return false |
| 374 | } |
| 375 | |
| 376 | panic("Can't handle type: " + value.Kind().String()) |
| 377 | } |