blob: be396fc18334aa121c99ff6a3429d48c049381d9 [file] [log] [blame]
Steven Moreland65b3fd92017-12-06 14:18:35 -08001// 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
15package android
16
17import (
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:
Paul Duffinaebc02a2019-06-27 14:08:51 +010034// - 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
Steven Moreland65b3fd92017-12-06 14:18:35 -080037// - - 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
Paul Duffinaebc02a2019-06-27 14:08:51 +010042// - it has none of the "Without" properties matched (same rules as above)
Steven Moreland65b3fd92017-12-06 14:18:35 -080043
44func registerNeverallowMutator(ctx RegisterMutatorsContext) {
45 ctx.BottomUp("neverallow", neverallowMutator).Parallel()
46}
47
Paul Duffinaebc02a2019-06-27 14:08:51 +010048var neverallows = []Rule{}
Steven Moreland65b3fd92017-12-06 14:18:35 -080049
Paul Duffinaebc02a2019-06-27 14:08:51 +010050func init() {
Paul Duffin2ac2bef2019-07-16 14:18:22 +010051 AddNeverAllowRules(createIncludeDirsRules()...)
Paul Duffinaebc02a2019-06-27 14:08:51 +010052 AddNeverAllowRules(createTrebleRules()...)
53 AddNeverAllowRules(createLibcoreRules()...)
54 AddNeverAllowRules(createJavaDeviceForHostRules()...)
Neil Fullerdf5f3562018-10-21 17:19:10 +010055}
Steven Moreland65b3fd92017-12-06 14:18:35 -080056
Paul Duffinaebc02a2019-06-27 14:08:51 +010057// Add a NeverAllow rule to the set of rules to apply.
58func AddNeverAllowRules(rules ...Rule) {
59 neverallows = append(neverallows, rules...)
60}
61
Paul Duffin2ac2bef2019-07-16 14:18:22 +010062func createIncludeDirsRules() []Rule {
63 // The list of paths that cannot be referenced using include_dirs
64 paths := []string{
65 "art",
66 "libcore",
67 "libnativehelper",
68 "external/apache-harmony",
69 "external/apache-xml",
70 "external/boringssl",
71 "external/bouncycastle",
72 "external/conscrypt",
73 "external/icu",
74 "external/okhttp",
75 "external/vixl",
76 "external/wycheproof",
77 "system/core/libnativebridge",
78 "system/core/libnativehelper",
79 }
80
81 // Create a composite matcher that will match if the value starts with any of the restricted
82 // paths. A / is appended to the prefix to ensure that restricting path X does not affect paths
83 // XY.
84 rules := make([]Rule, 0, len(paths))
85 for _, path := range paths {
86 rule :=
87 NeverAllow().
88 WithMatcher("include_dirs", StartsWith(path+"/")).
89 Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" +
90 " to use alternate mechanisms and so can no longer be used.")
91
92 rules = append(rules, rule)
93 }
94
95 return rules
96}
97
Paul Duffinaebc02a2019-06-27 14:08:51 +010098func createTrebleRules() []Rule {
99 return []Rule{
100 NeverAllow().
101 In("vendor", "device").
102 With("vndk.enabled", "true").
103 Without("vendor", "true").
104 Because("the VNDK can never contain a library that is device dependent."),
105 NeverAllow().
106 With("vndk.enabled", "true").
107 Without("vendor", "true").
108 Without("owner", "").
109 Because("a VNDK module can never have an owner."),
Steven Moreland65b3fd92017-12-06 14:18:35 -0800110
Neil Fullerdf5f3562018-10-21 17:19:10 +0100111 // TODO(b/67974785): always enforce the manifest
Paul Duffinaebc02a2019-06-27 14:08:51 +0100112 NeverAllow().
113 Without("name", "libhidltransport").
114 With("product_variables.enforce_vintf_manifest.cflags", "*").
115 Because("manifest enforcement should be independent of ."),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100116
117 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
Paul Duffinaebc02a2019-06-27 14:08:51 +0100118 NeverAllow().
119 Without("name", "libc_bionic_ndk").
120 With("product_variables.treble_linker_namespaces.cflags", "*").
121 Because("nothing should care if linker namespaces are enabled or not"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100122
123 // Example:
Paul Duffinaebc02a2019-06-27 14:08:51 +0100124 // *NeverAllow().with("Srcs", "main.cpp"))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100125 }
126}
127
Paul Duffinaebc02a2019-06-27 14:08:51 +0100128func createLibcoreRules() []Rule {
Neil Fullerdf5f3562018-10-21 17:19:10 +0100129 var coreLibraryProjects = []string{
130 "libcore",
131 "external/apache-harmony",
132 "external/apache-xml",
133 "external/bouncycastle",
134 "external/conscrypt",
135 "external/icu",
136 "external/okhttp",
137 "external/wycheproof",
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100138
139 // Not really a core library but still needs access to same capabilities.
140 "development",
Neil Fullerdf5f3562018-10-21 17:19:10 +0100141 }
142
Paul Duffina3d09862019-06-11 13:40:47 +0100143 // Core library constraints. The sdk_version: "none" can only be used in core library projects.
144 // Access to core library targets is restricted using visibility rules.
Paul Duffinaebc02a2019-06-27 14:08:51 +0100145 rules := []Rule{
146 NeverAllow().
147 NotIn(coreLibraryProjects...).
148 With("sdk_version", "none"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100149 }
150
Neil Fullerdf5f3562018-10-21 17:19:10 +0100151 return rules
Steven Moreland65b3fd92017-12-06 14:18:35 -0800152}
153
Paul Duffinaebc02a2019-06-27 14:08:51 +0100154func createJavaDeviceForHostRules() []Rule {
Colin Crossc35c5f92019-03-05 15:06:16 -0800155 javaDeviceForHostProjectsWhitelist := []string{
Colin Cross97add502019-04-11 14:07:38 -0700156 "external/guava",
Colin Crossc35c5f92019-03-05 15:06:16 -0800157 "external/robolectric-shadows",
158 "framework/layoutlib",
159 }
160
Paul Duffinaebc02a2019-06-27 14:08:51 +0100161 return []Rule{
162 NeverAllow().
163 NotIn(javaDeviceForHostProjectsWhitelist...).
164 ModuleType("java_device_for_host", "java_host_for_device").
165 Because("java_device_for_host can only be used in whitelisted projects"),
Colin Crossc35c5f92019-03-05 15:06:16 -0800166 }
167}
168
Steven Moreland65b3fd92017-12-06 14:18:35 -0800169func neverallowMutator(ctx BottomUpMutatorContext) {
170 m, ok := ctx.Module().(Module)
171 if !ok {
172 return
173 }
174
175 dir := ctx.ModuleDir() + "/"
176 properties := m.GetProperties()
177
Paul Duffinaebc02a2019-06-27 14:08:51 +0100178 for _, r := range neverallows {
179 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800180 if !n.appliesToPath(dir) {
181 continue
182 }
183
Colin Crossc35c5f92019-03-05 15:06:16 -0800184 if !n.appliesToModuleType(ctx.ModuleType()) {
185 continue
186 }
187
Steven Moreland65b3fd92017-12-06 14:18:35 -0800188 if !n.appliesToProperties(properties) {
189 continue
190 }
191
Paul Duffin35781882019-07-25 15:41:09 +0100192 if !n.appliesToDirectDeps(ctx) {
193 continue
194 }
195
Steven Moreland65b3fd92017-12-06 14:18:35 -0800196 ctx.ModuleErrorf("violates " + n.String())
197 }
198}
199
Paul Duffin73bf0542019-07-12 14:12:49 +0100200type ValueMatcher interface {
201 test(string) bool
202 String() string
203}
204
205type equalMatcher struct {
206 expected string
207}
208
209func (m *equalMatcher) test(value string) bool {
210 return m.expected == value
211}
212
213func (m *equalMatcher) String() string {
214 return "=" + m.expected
215}
216
217type anyMatcher struct {
218}
219
220func (m *anyMatcher) test(value string) bool {
221 return true
222}
223
224func (m *anyMatcher) String() string {
225 return "=*"
226}
227
228var anyMatcherInstance = &anyMatcher{}
229
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100230type startsWithMatcher struct {
231 prefix string
232}
233
234func (m *startsWithMatcher) test(value string) bool {
235 return strings.HasPrefix(value, m.prefix)
236}
237
238func (m *startsWithMatcher) String() string {
239 return ".starts-with(" + m.prefix + ")"
240}
241
Steven Moreland65b3fd92017-12-06 14:18:35 -0800242type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100243 fields []string // e.x.: Vndk.Enabled
244 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800245}
246
Paul Duffinaebc02a2019-06-27 14:08:51 +0100247// A NeverAllow rule.
248type Rule interface {
249 In(path ...string) Rule
250
251 NotIn(path ...string) Rule
252
Paul Duffin35781882019-07-25 15:41:09 +0100253 InDirectDeps(deps ...string) Rule
254
Paul Duffinaebc02a2019-06-27 14:08:51 +0100255 ModuleType(types ...string) Rule
256
257 NotModuleType(types ...string) Rule
258
259 With(properties, value string) Rule
260
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100261 WithMatcher(properties string, matcher ValueMatcher) Rule
262
Paul Duffinaebc02a2019-06-27 14:08:51 +0100263 Without(properties, value string) Rule
264
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100265 WithoutMatcher(properties string, matcher ValueMatcher) Rule
266
Paul Duffinaebc02a2019-06-27 14:08:51 +0100267 Because(reason string) Rule
268}
269
Steven Moreland65b3fd92017-12-06 14:18:35 -0800270type rule struct {
271 // User string for why this is a thing.
272 reason string
273
274 paths []string
275 unlessPaths []string
276
Paul Duffin35781882019-07-25 15:41:09 +0100277 directDeps map[string]bool
278
Colin Crossc35c5f92019-03-05 15:06:16 -0800279 moduleTypes []string
280 unlessModuleTypes []string
281
Steven Moreland65b3fd92017-12-06 14:18:35 -0800282 props []ruleProperty
283 unlessProps []ruleProperty
284}
285
Paul Duffinaebc02a2019-06-27 14:08:51 +0100286// Create a new NeverAllow rule.
287func NeverAllow() Rule {
Paul Duffin35781882019-07-25 15:41:09 +0100288 return &rule{directDeps: make(map[string]bool)}
Steven Moreland65b3fd92017-12-06 14:18:35 -0800289}
Colin Crossc35c5f92019-03-05 15:06:16 -0800290
Paul Duffinaebc02a2019-06-27 14:08:51 +0100291func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800292 r.paths = append(r.paths, cleanPaths(path)...)
293 return r
294}
Colin Crossc35c5f92019-03-05 15:06:16 -0800295
Paul Duffinaebc02a2019-06-27 14:08:51 +0100296func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800297 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
298 return r
299}
Colin Crossc35c5f92019-03-05 15:06:16 -0800300
Paul Duffin35781882019-07-25 15:41:09 +0100301func (r *rule) InDirectDeps(deps ...string) Rule {
302 for _, d := range deps {
303 r.directDeps[d] = true
304 }
305 return r
306}
307
Paul Duffinaebc02a2019-06-27 14:08:51 +0100308func (r *rule) ModuleType(types ...string) Rule {
Colin Crossc35c5f92019-03-05 15:06:16 -0800309 r.moduleTypes = append(r.moduleTypes, types...)
310 return r
311}
312
Paul Duffinaebc02a2019-06-27 14:08:51 +0100313func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossc35c5f92019-03-05 15:06:16 -0800314 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
315 return r
316}
317
Paul Duffinaebc02a2019-06-27 14:08:51 +0100318func (r *rule) With(properties, value string) Rule {
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100319 return r.WithMatcher(properties, selectMatcher(value))
320}
321
322func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800323 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100324 fields: fieldNamesForProperties(properties),
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100325 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800326 })
327 return r
328}
Colin Crossc35c5f92019-03-05 15:06:16 -0800329
Paul Duffinaebc02a2019-06-27 14:08:51 +0100330func (r *rule) Without(properties, value string) Rule {
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100331 return r.WithoutMatcher(properties, selectMatcher(value))
332}
333
334func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800335 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100336 fields: fieldNamesForProperties(properties),
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100337 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800338 })
339 return r
340}
Colin Crossc35c5f92019-03-05 15:06:16 -0800341
Paul Duffin73bf0542019-07-12 14:12:49 +0100342func selectMatcher(expected string) ValueMatcher {
343 if expected == "*" {
344 return anyMatcherInstance
345 }
346 return &equalMatcher{expected: expected}
347}
348
Paul Duffinaebc02a2019-06-27 14:08:51 +0100349func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800350 r.reason = reason
351 return r
352}
353
354func (r *rule) String() string {
355 s := "neverallow"
356 for _, v := range r.paths {
357 s += " dir:" + v + "*"
358 }
359 for _, v := range r.unlessPaths {
360 s += " -dir:" + v + "*"
361 }
Colin Crossc35c5f92019-03-05 15:06:16 -0800362 for _, v := range r.moduleTypes {
363 s += " type:" + v
364 }
365 for _, v := range r.unlessModuleTypes {
366 s += " -type:" + v
367 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800368 for _, v := range r.props {
Paul Duffin73bf0542019-07-12 14:12:49 +0100369 s += " " + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800370 }
371 for _, v := range r.unlessProps {
Paul Duffin73bf0542019-07-12 14:12:49 +0100372 s += " -" + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800373 }
Paul Duffin35781882019-07-25 15:41:09 +0100374 for k := range r.directDeps {
375 s += " deps:" + k
376 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800377 if len(r.reason) != 0 {
378 s += " which is restricted because " + r.reason
379 }
380 return s
381}
382
383func (r *rule) appliesToPath(dir string) bool {
384 includePath := len(r.paths) == 0 || hasAnyPrefix(dir, r.paths)
385 excludePath := hasAnyPrefix(dir, r.unlessPaths)
386 return includePath && !excludePath
387}
388
Paul Duffin35781882019-07-25 15:41:09 +0100389func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool {
390 if len(r.directDeps) == 0 {
391 return true
392 }
393
394 matches := false
395 ctx.VisitDirectDeps(func(m Module) {
396 if !matches {
397 name := ctx.OtherModuleName(m)
398 matches = r.directDeps[name]
399 }
400 })
401
402 return matches
403}
404
Colin Crossc35c5f92019-03-05 15:06:16 -0800405func (r *rule) appliesToModuleType(moduleType string) bool {
406 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
407}
408
Steven Moreland65b3fd92017-12-06 14:18:35 -0800409func (r *rule) appliesToProperties(properties []interface{}) bool {
410 includeProps := hasAllProperties(properties, r.props)
411 excludeProps := hasAnyProperty(properties, r.unlessProps)
412 return includeProps && !excludeProps
413}
414
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100415func StartsWith(prefix string) ValueMatcher {
416 return &startsWithMatcher{prefix}
417}
418
Steven Moreland65b3fd92017-12-06 14:18:35 -0800419// assorted utils
420
421func cleanPaths(paths []string) []string {
422 res := make([]string, len(paths))
423 for i, v := range paths {
424 res[i] = filepath.Clean(v) + "/"
425 }
426 return res
427}
428
429func fieldNamesForProperties(propertyNames string) []string {
430 names := strings.Split(propertyNames, ".")
431 for i, v := range names {
432 names[i] = proptools.FieldNameForProperty(v)
433 }
434 return names
435}
436
437func hasAnyPrefix(s string, prefixes []string) bool {
438 for _, prefix := range prefixes {
439 if strings.HasPrefix(s, prefix) {
440 return true
441 }
442 }
443 return false
444}
445
446func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
447 for _, v := range props {
448 if hasProperty(properties, v) {
449 return true
450 }
451 }
452 return false
453}
454
455func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
456 for _, v := range props {
457 if !hasProperty(properties, v) {
458 return false
459 }
460 }
461 return true
462}
463
464func hasProperty(properties []interface{}, prop ruleProperty) bool {
465 for _, propertyStruct := range properties {
466 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
467 for _, v := range prop.fields {
468 if !propertiesValue.IsValid() {
469 break
470 }
471 propertiesValue = propertiesValue.FieldByName(v)
472 }
473 if !propertiesValue.IsValid() {
474 continue
475 }
476
Paul Duffin73bf0542019-07-12 14:12:49 +0100477 check := func(value string) bool {
478 return prop.matcher.test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800479 }
480
481 if matchValue(propertiesValue, check) {
482 return true
483 }
484 }
485 return false
486}
487
488func matchValue(value reflect.Value, check func(string) bool) bool {
489 if !value.IsValid() {
490 return false
491 }
492
493 if value.Kind() == reflect.Ptr {
494 if value.IsNil() {
495 return check("")
496 }
497 value = value.Elem()
498 }
499
500 switch value.Kind() {
501 case reflect.String:
502 return check(value.String())
503 case reflect.Bool:
504 return check(strconv.FormatBool(value.Bool()))
505 case reflect.Int:
506 return check(strconv.FormatInt(value.Int(), 10))
507 case reflect.Slice:
508 slice, ok := value.Interface().([]string)
509 if !ok {
510 panic("Can only handle slice of string")
511 }
512 for _, v := range slice {
513 if check(v) {
514 return true
515 }
516 }
517 return false
518 }
519
520 panic("Can't handle type: " + value.Kind().String())
521}