blob: 031b3f411cc88426d5c52be6fbcac7d7f70fbb35 [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"
Anton Hansson45376402020-04-09 14:18:21 +010020 "regexp"
Steven Moreland65b3fd92017-12-06 14:18:35 -080021 "strconv"
22 "strings"
23
24 "github.com/google/blueprint/proptools"
25)
26
27// "neverallow" rules for the build system.
28//
29// This allows things which aren't related to the build system and are enforced
30// for sanity, in progress code refactors, or policy to be expressed in a
31// straightforward away disjoint from implementations and tests which should
32// work regardless of these restrictions.
33//
34// A module is disallowed if all of the following are true:
Paul Duffin730f2a52019-06-27 14:08:51 +010035// - it is in one of the "In" paths
36// - it is not in one of the "NotIn" paths
37// - it has all "With" properties matched
Steven Moreland65b3fd92017-12-06 14:18:35 -080038// - - values are matched in their entirety
39// - - nil is interpreted as an empty string
40// - - nested properties are separated with a '.'
41// - - if the property is a list, any of the values in the list being matches
42// counts as a match
Paul Duffin730f2a52019-06-27 14:08:51 +010043// - it has none of the "Without" properties matched (same rules as above)
Steven Moreland65b3fd92017-12-06 14:18:35 -080044
Artur Satayevc5570ac2020-04-09 16:06:36 +010045func RegisterNeverallowMutator(ctx RegisterMutatorsContext) {
Steven Moreland65b3fd92017-12-06 14:18:35 -080046 ctx.BottomUp("neverallow", neverallowMutator).Parallel()
47}
48
Paul Duffin730f2a52019-06-27 14:08:51 +010049var neverallows = []Rule{}
Steven Moreland65b3fd92017-12-06 14:18:35 -080050
Paul Duffin730f2a52019-06-27 14:08:51 +010051func init() {
Paul Duffinc8111702019-07-22 12:13:55 +010052 AddNeverAllowRules(createIncludeDirsRules()...)
Paul Duffin730f2a52019-06-27 14:08:51 +010053 AddNeverAllowRules(createTrebleRules()...)
Paul Duffin730f2a52019-06-27 14:08:51 +010054 AddNeverAllowRules(createMediaRules()...)
55 AddNeverAllowRules(createJavaDeviceForHostRules()...)
Colin Crossc511bc52020-04-07 16:50:32 +000056 AddNeverAllowRules(createCcSdkVariantRules()...)
David Srbeckye033cba2020-05-20 22:20:28 +010057 AddNeverAllowRules(createUncompressDexRules()...)
Yifan Hong696ed4d2020-07-27 12:59:58 -070058 AddNeverAllowRules(createMakefileGoalRules()...)
Neil Fullerdf5f3562018-10-21 17:19:10 +010059}
Steven Moreland65b3fd92017-12-06 14:18:35 -080060
Paul Duffin730f2a52019-06-27 14:08:51 +010061// Add a NeverAllow rule to the set of rules to apply.
62func AddNeverAllowRules(rules ...Rule) {
63 neverallows = append(neverallows, rules...)
64}
65
Paul Duffinc8111702019-07-22 12:13:55 +010066func createIncludeDirsRules() []Rule {
67 // The list of paths that cannot be referenced using include_dirs
68 paths := []string{
69 "art",
Orion Hodson6341f012019-11-06 13:39:46 +000070 "art/libnativebridge",
71 "art/libnativeloader",
Paul Duffinc8111702019-07-22 12:13:55 +010072 "libcore",
73 "libnativehelper",
74 "external/apache-harmony",
75 "external/apache-xml",
76 "external/boringssl",
77 "external/bouncycastle",
78 "external/conscrypt",
79 "external/icu",
80 "external/okhttp",
81 "external/vixl",
82 "external/wycheproof",
Paul Duffinc8111702019-07-22 12:13:55 +010083 }
84
85 // Create a composite matcher that will match if the value starts with any of the restricted
86 // paths. A / is appended to the prefix to ensure that restricting path X does not affect paths
87 // XY.
88 rules := make([]Rule, 0, len(paths))
89 for _, path := range paths {
90 rule :=
91 NeverAllow().
92 WithMatcher("include_dirs", StartsWith(path+"/")).
93 Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" +
94 " to use alternate mechanisms and so can no longer be used.")
95
96 rules = append(rules, rule)
97 }
98
99 return rules
100}
101
Paul Duffin730f2a52019-06-27 14:08:51 +0100102func createTrebleRules() []Rule {
103 return []Rule{
104 NeverAllow().
105 In("vendor", "device").
106 With("vndk.enabled", "true").
107 Without("vendor", "true").
Justin Yun0ecf0b22020-02-28 15:07:59 +0900108 Without("product_specific", "true").
Paul Duffin730f2a52019-06-27 14:08:51 +0100109 Because("the VNDK can never contain a library that is device dependent."),
110 NeverAllow().
111 With("vndk.enabled", "true").
112 Without("vendor", "true").
113 Without("owner", "").
114 Because("a VNDK module can never have an owner."),
Steven Moreland65b3fd92017-12-06 14:18:35 -0800115
Neil Fullerdf5f3562018-10-21 17:19:10 +0100116 // TODO(b/67974785): always enforce the manifest
Paul Duffin730f2a52019-06-27 14:08:51 +0100117 NeverAllow().
Steven Moreland51ce4f62020-02-10 17:21:32 -0800118 Without("name", "libhidlbase-combined-impl").
119 Without("name", "libhidlbase").
120 Without("name", "libhidlbase_pgo").
Paul Duffin730f2a52019-06-27 14:08:51 +0100121 With("product_variables.enforce_vintf_manifest.cflags", "*").
122 Because("manifest enforcement should be independent of ."),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100123
124 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
Paul Duffin730f2a52019-06-27 14:08:51 +0100125 NeverAllow().
126 Without("name", "libc_bionic_ndk").
127 With("product_variables.treble_linker_namespaces.cflags", "*").
128 Because("nothing should care if linker namespaces are enabled or not"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100129
130 // Example:
Paul Duffin730f2a52019-06-27 14:08:51 +0100131 // *NeverAllow().with("Srcs", "main.cpp"))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100132 }
133}
134
Paul Duffin730f2a52019-06-27 14:08:51 +0100135func createMediaRules() []Rule {
136 return []Rule{
137 NeverAllow().
138 With("libs", "updatable-media").
139 Because("updatable-media includes private APIs. Use updatable_media_stubs instead."),
Dongwon Kang50a299f2019-02-04 09:00:51 -0800140 }
141}
142
Paul Duffin730f2a52019-06-27 14:08:51 +0100143func createJavaDeviceForHostRules() []Rule {
Colin Cross440e0d02020-06-11 11:32:11 -0700144 javaDeviceForHostProjectsAllowedList := []string{
Colin Crossb5191a52019-04-11 14:07:38 -0700145 "external/guava",
Colin Crossfd4f7432019-03-05 15:06:16 -0800146 "external/robolectric-shadows",
147 "framework/layoutlib",
148 }
149
Paul Duffin730f2a52019-06-27 14:08:51 +0100150 return []Rule{
151 NeverAllow().
Colin Cross440e0d02020-06-11 11:32:11 -0700152 NotIn(javaDeviceForHostProjectsAllowedList...).
Paul Duffin730f2a52019-06-27 14:08:51 +0100153 ModuleType("java_device_for_host", "java_host_for_device").
Colin Cross440e0d02020-06-11 11:32:11 -0700154 Because("java_device_for_host can only be used in allowed projects"),
Colin Crossfd4f7432019-03-05 15:06:16 -0800155 }
156}
157
Colin Crossc511bc52020-04-07 16:50:32 +0000158func createCcSdkVariantRules() []Rule {
Colin Cross440e0d02020-06-11 11:32:11 -0700159 sdkVersionOnlyAllowedList := []string{
Colin Crossc511bc52020-04-07 16:50:32 +0000160 // derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk.
161 // This sometimes works because the APEX modules that contain derive_sdk and
162 // derive_sdk_prefer32 suppress the platform installation rules, but fails when
163 // the APEX modules contain the SDK variant and the platform variant still exists.
Anton Hansson4b8e64b2020-05-27 18:25:23 +0100164 "packages/modules/SdkExtensions/derive_sdk",
Dan Alberte2054a92020-04-20 14:46:47 -0700165 // These are for apps and shouldn't be used by non-SDK variant modules.
166 "prebuilts/ndk",
167 "tools/test/graphicsbenchmark/apps/sample_app",
168 "tools/test/graphicsbenchmark/functional_tests/java",
Dan Albert55576052020-04-20 14:46:47 -0700169 "vendor/xts/gts-tests/hostsidetests/gamedevicecert/apps/javatests",
Colin Crossc511bc52020-04-07 16:50:32 +0000170 }
171
Colin Cross440e0d02020-06-11 11:32:11 -0700172 platformVariantPropertiesAllowedList := []string{
Colin Crossc511bc52020-04-07 16:50:32 +0000173 // android_native_app_glue and libRSSupport use native_window.h but target old
174 // sdk versions (minimum and 9 respectively) where libnativewindow didn't exist,
175 // so they can't add libnativewindow to shared_libs to get the header directory
176 // for the platform variant. Allow them to use the platform variant
177 // property to set shared_libs.
178 "prebuilts/ndk",
179 "frameworks/rs",
180 }
181
182 return []Rule{
183 NeverAllow().
Colin Cross440e0d02020-06-11 11:32:11 -0700184 NotIn(sdkVersionOnlyAllowedList...).
Colin Crossc511bc52020-04-07 16:50:32 +0000185 WithMatcher("sdk_variant_only", isSetMatcherInstance).
Colin Cross440e0d02020-06-11 11:32:11 -0700186 Because("sdk_variant_only can only be used in allowed projects"),
Colin Crossc511bc52020-04-07 16:50:32 +0000187 NeverAllow().
Colin Cross440e0d02020-06-11 11:32:11 -0700188 NotIn(platformVariantPropertiesAllowedList...).
Colin Crossc511bc52020-04-07 16:50:32 +0000189 WithMatcher("platform.shared_libs", isSetMatcherInstance).
Colin Cross440e0d02020-06-11 11:32:11 -0700190 Because("platform variant properties can only be used in allowed projects"),
Colin Crossc511bc52020-04-07 16:50:32 +0000191 }
192}
193
David Srbeckye033cba2020-05-20 22:20:28 +0100194func createUncompressDexRules() []Rule {
195 return []Rule{
196 NeverAllow().
197 NotIn("art").
198 WithMatcher("uncompress_dex", isSetMatcherInstance).
199 Because("uncompress_dex is only allowed for certain jars for test in art."),
200 }
201}
202
Yifan Hong696ed4d2020-07-27 12:59:58 -0700203func createMakefileGoalRules() []Rule {
204 return []Rule{
205 NeverAllow().
206 ModuleType("makefile_goal").
207 WithoutMatcher("product_out_path", Regexp("^boot[0-9a-zA-Z.-]*[.]img$")).
208 Because("Only boot images may be imported as a makefile goal."),
209 }
210}
211
Steven Moreland65b3fd92017-12-06 14:18:35 -0800212func neverallowMutator(ctx BottomUpMutatorContext) {
213 m, ok := ctx.Module().(Module)
214 if !ok {
215 return
216 }
217
218 dir := ctx.ModuleDir() + "/"
219 properties := m.GetProperties()
220
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100221 osClass := ctx.Module().Target().Os.Class
222
Paul Duffin115445b2019-08-07 15:31:07 +0100223 for _, r := range neverallowRules(ctx.Config()) {
Paul Duffin730f2a52019-06-27 14:08:51 +0100224 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800225 if !n.appliesToPath(dir) {
226 continue
227 }
228
Colin Crossfd4f7432019-03-05 15:06:16 -0800229 if !n.appliesToModuleType(ctx.ModuleType()) {
230 continue
231 }
232
Steven Moreland65b3fd92017-12-06 14:18:35 -0800233 if !n.appliesToProperties(properties) {
234 continue
235 }
236
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100237 if !n.appliesToOsClass(osClass) {
238 continue
239 }
240
Paul Duffin35781882019-07-25 15:41:09 +0100241 if !n.appliesToDirectDeps(ctx) {
242 continue
243 }
244
Andrei Onea115e7e72020-06-05 21:14:03 +0100245 if !n.appliesToBootclasspathJar(ctx) {
246 continue
247 }
248
Steven Moreland65b3fd92017-12-06 14:18:35 -0800249 ctx.ModuleErrorf("violates " + n.String())
250 }
251}
252
Paul Duffin73bf0542019-07-12 14:12:49 +0100253type ValueMatcher interface {
Artur Satayevc5570ac2020-04-09 16:06:36 +0100254 Test(string) bool
Paul Duffin73bf0542019-07-12 14:12:49 +0100255 String() string
256}
257
258type equalMatcher struct {
259 expected string
260}
261
Artur Satayevc5570ac2020-04-09 16:06:36 +0100262func (m *equalMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100263 return m.expected == value
264}
265
266func (m *equalMatcher) String() string {
267 return "=" + m.expected
268}
269
270type anyMatcher struct {
271}
272
Artur Satayevc5570ac2020-04-09 16:06:36 +0100273func (m *anyMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100274 return true
275}
276
277func (m *anyMatcher) String() string {
278 return "=*"
279}
280
281var anyMatcherInstance = &anyMatcher{}
282
Paul Duffinc8111702019-07-22 12:13:55 +0100283type startsWithMatcher struct {
284 prefix string
285}
286
Artur Satayevc5570ac2020-04-09 16:06:36 +0100287func (m *startsWithMatcher) Test(value string) bool {
Paul Duffinc8111702019-07-22 12:13:55 +0100288 return strings.HasPrefix(value, m.prefix)
289}
290
291func (m *startsWithMatcher) String() string {
292 return ".starts-with(" + m.prefix + ")"
293}
294
Anton Hansson45376402020-04-09 14:18:21 +0100295type regexMatcher struct {
296 re *regexp.Regexp
297}
298
Artur Satayevc5570ac2020-04-09 16:06:36 +0100299func (m *regexMatcher) Test(value string) bool {
Anton Hansson45376402020-04-09 14:18:21 +0100300 return m.re.MatchString(value)
301}
302
303func (m *regexMatcher) String() string {
304 return ".regexp(" + m.re.String() + ")"
305}
306
Andrei Onea115e7e72020-06-05 21:14:03 +0100307type notInListMatcher struct {
308 allowed []string
309}
310
311func (m *notInListMatcher) Test(value string) bool {
312 return !InList(value, m.allowed)
313}
314
315func (m *notInListMatcher) String() string {
316 return ".not-in-list(" + strings.Join(m.allowed, ",") + ")"
317}
318
Colin Crossc511bc52020-04-07 16:50:32 +0000319type isSetMatcher struct{}
320
Artur Satayevc5570ac2020-04-09 16:06:36 +0100321func (m *isSetMatcher) Test(value string) bool {
Colin Crossc511bc52020-04-07 16:50:32 +0000322 return value != ""
323}
324
325func (m *isSetMatcher) String() string {
326 return ".is-set"
327}
328
329var isSetMatcherInstance = &isSetMatcher{}
330
Steven Moreland65b3fd92017-12-06 14:18:35 -0800331type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100332 fields []string // e.x.: Vndk.Enabled
333 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800334}
335
Paul Duffin730f2a52019-06-27 14:08:51 +0100336// A NeverAllow rule.
337type Rule interface {
338 In(path ...string) Rule
339
340 NotIn(path ...string) Rule
341
Paul Duffin35781882019-07-25 15:41:09 +0100342 InDirectDeps(deps ...string) Rule
343
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100344 WithOsClass(osClasses ...OsClass) Rule
345
Paul Duffin730f2a52019-06-27 14:08:51 +0100346 ModuleType(types ...string) Rule
347
348 NotModuleType(types ...string) Rule
349
Andrei Onea115e7e72020-06-05 21:14:03 +0100350 BootclasspathJar() Rule
351
Paul Duffin730f2a52019-06-27 14:08:51 +0100352 With(properties, value string) Rule
353
Paul Duffinc8111702019-07-22 12:13:55 +0100354 WithMatcher(properties string, matcher ValueMatcher) Rule
355
Paul Duffin730f2a52019-06-27 14:08:51 +0100356 Without(properties, value string) Rule
357
Paul Duffinc8111702019-07-22 12:13:55 +0100358 WithoutMatcher(properties string, matcher ValueMatcher) Rule
359
Paul Duffin730f2a52019-06-27 14:08:51 +0100360 Because(reason string) Rule
361}
362
Steven Moreland65b3fd92017-12-06 14:18:35 -0800363type rule struct {
364 // User string for why this is a thing.
365 reason string
366
367 paths []string
368 unlessPaths []string
369
Paul Duffin35781882019-07-25 15:41:09 +0100370 directDeps map[string]bool
371
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100372 osClasses []OsClass
373
Colin Crossfd4f7432019-03-05 15:06:16 -0800374 moduleTypes []string
375 unlessModuleTypes []string
376
Steven Moreland65b3fd92017-12-06 14:18:35 -0800377 props []ruleProperty
378 unlessProps []ruleProperty
Andrei Onea115e7e72020-06-05 21:14:03 +0100379
380 onlyBootclasspathJar bool
Steven Moreland65b3fd92017-12-06 14:18:35 -0800381}
382
Paul Duffin730f2a52019-06-27 14:08:51 +0100383// Create a new NeverAllow rule.
384func NeverAllow() Rule {
Paul Duffin35781882019-07-25 15:41:09 +0100385 return &rule{directDeps: make(map[string]bool)}
Steven Moreland65b3fd92017-12-06 14:18:35 -0800386}
Colin Crossfd4f7432019-03-05 15:06:16 -0800387
Paul Duffin730f2a52019-06-27 14:08:51 +0100388func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800389 r.paths = append(r.paths, cleanPaths(path)...)
390 return r
391}
Colin Crossfd4f7432019-03-05 15:06:16 -0800392
Paul Duffin730f2a52019-06-27 14:08:51 +0100393func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800394 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
395 return r
396}
Colin Crossfd4f7432019-03-05 15:06:16 -0800397
Paul Duffin35781882019-07-25 15:41:09 +0100398func (r *rule) InDirectDeps(deps ...string) Rule {
399 for _, d := range deps {
400 r.directDeps[d] = true
401 }
402 return r
403}
404
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100405func (r *rule) WithOsClass(osClasses ...OsClass) Rule {
406 r.osClasses = append(r.osClasses, osClasses...)
407 return r
408}
409
Paul Duffin730f2a52019-06-27 14:08:51 +0100410func (r *rule) ModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800411 r.moduleTypes = append(r.moduleTypes, types...)
412 return r
413}
414
Paul Duffin730f2a52019-06-27 14:08:51 +0100415func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800416 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
417 return r
418}
419
Paul Duffin730f2a52019-06-27 14:08:51 +0100420func (r *rule) With(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100421 return r.WithMatcher(properties, selectMatcher(value))
422}
423
424func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800425 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100426 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100427 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800428 })
429 return r
430}
Colin Crossfd4f7432019-03-05 15:06:16 -0800431
Paul Duffin730f2a52019-06-27 14:08:51 +0100432func (r *rule) Without(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100433 return r.WithoutMatcher(properties, selectMatcher(value))
434}
435
436func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800437 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100438 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100439 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800440 })
441 return r
442}
Colin Crossfd4f7432019-03-05 15:06:16 -0800443
Paul Duffin73bf0542019-07-12 14:12:49 +0100444func selectMatcher(expected string) ValueMatcher {
445 if expected == "*" {
446 return anyMatcherInstance
447 }
448 return &equalMatcher{expected: expected}
449}
450
Paul Duffin730f2a52019-06-27 14:08:51 +0100451func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800452 r.reason = reason
453 return r
454}
455
Andrei Onea115e7e72020-06-05 21:14:03 +0100456func (r *rule) BootclasspathJar() Rule {
457 r.onlyBootclasspathJar = true
458 return r
459}
460
Steven Moreland65b3fd92017-12-06 14:18:35 -0800461func (r *rule) String() string {
462 s := "neverallow"
463 for _, v := range r.paths {
464 s += " dir:" + v + "*"
465 }
466 for _, v := range r.unlessPaths {
467 s += " -dir:" + v + "*"
468 }
Colin Crossfd4f7432019-03-05 15:06:16 -0800469 for _, v := range r.moduleTypes {
470 s += " type:" + v
471 }
472 for _, v := range r.unlessModuleTypes {
473 s += " -type:" + v
474 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800475 for _, v := range r.props {
Paul Duffin73bf0542019-07-12 14:12:49 +0100476 s += " " + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800477 }
478 for _, v := range r.unlessProps {
Paul Duffin73bf0542019-07-12 14:12:49 +0100479 s += " -" + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800480 }
Paul Duffin35781882019-07-25 15:41:09 +0100481 for k := range r.directDeps {
482 s += " deps:" + k
483 }
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100484 for _, v := range r.osClasses {
485 s += " os:" + v.String()
486 }
Andrei Onea115e7e72020-06-05 21:14:03 +0100487 if r.onlyBootclasspathJar {
488 s += " inBcp"
489 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800490 if len(r.reason) != 0 {
491 s += " which is restricted because " + r.reason
492 }
493 return s
494}
495
496func (r *rule) appliesToPath(dir string) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800497 includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths)
498 excludePath := HasAnyPrefix(dir, r.unlessPaths)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800499 return includePath && !excludePath
500}
501
Paul Duffin35781882019-07-25 15:41:09 +0100502func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool {
503 if len(r.directDeps) == 0 {
504 return true
505 }
506
507 matches := false
508 ctx.VisitDirectDeps(func(m Module) {
509 if !matches {
510 name := ctx.OtherModuleName(m)
511 matches = r.directDeps[name]
512 }
513 })
514
515 return matches
516}
517
Andrei Onea115e7e72020-06-05 21:14:03 +0100518func (r *rule) appliesToBootclasspathJar(ctx BottomUpMutatorContext) bool {
519 if !r.onlyBootclasspathJar {
520 return true
521 }
522
523 return InList(ctx.ModuleName(), ctx.Config().BootJars())
524}
525
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100526func (r *rule) appliesToOsClass(osClass OsClass) bool {
527 if len(r.osClasses) == 0 {
528 return true
529 }
530
531 for _, c := range r.osClasses {
532 if c == osClass {
533 return true
534 }
535 }
536
537 return false
538}
539
Colin Crossfd4f7432019-03-05 15:06:16 -0800540func (r *rule) appliesToModuleType(moduleType string) bool {
541 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
542}
543
Steven Moreland65b3fd92017-12-06 14:18:35 -0800544func (r *rule) appliesToProperties(properties []interface{}) bool {
545 includeProps := hasAllProperties(properties, r.props)
546 excludeProps := hasAnyProperty(properties, r.unlessProps)
547 return includeProps && !excludeProps
548}
549
Paul Duffinc8111702019-07-22 12:13:55 +0100550func StartsWith(prefix string) ValueMatcher {
551 return &startsWithMatcher{prefix}
552}
553
Anton Hansson45376402020-04-09 14:18:21 +0100554func Regexp(re string) ValueMatcher {
555 r, err := regexp.Compile(re)
556 if err != nil {
557 panic(err)
558 }
559 return &regexMatcher{r}
560}
561
Andrei Onea115e7e72020-06-05 21:14:03 +0100562func NotInList(allowed []string) ValueMatcher {
563 return &notInListMatcher{allowed}
564}
565
Steven Moreland65b3fd92017-12-06 14:18:35 -0800566// assorted utils
567
568func cleanPaths(paths []string) []string {
569 res := make([]string, len(paths))
570 for i, v := range paths {
571 res[i] = filepath.Clean(v) + "/"
572 }
573 return res
574}
575
576func fieldNamesForProperties(propertyNames string) []string {
577 names := strings.Split(propertyNames, ".")
578 for i, v := range names {
579 names[i] = proptools.FieldNameForProperty(v)
580 }
581 return names
582}
583
Steven Moreland65b3fd92017-12-06 14:18:35 -0800584func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
585 for _, v := range props {
586 if hasProperty(properties, v) {
587 return true
588 }
589 }
590 return false
591}
592
593func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
594 for _, v := range props {
595 if !hasProperty(properties, v) {
596 return false
597 }
598 }
599 return true
600}
601
602func hasProperty(properties []interface{}, prop ruleProperty) bool {
603 for _, propertyStruct := range properties {
604 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
605 for _, v := range prop.fields {
606 if !propertiesValue.IsValid() {
607 break
608 }
609 propertiesValue = propertiesValue.FieldByName(v)
610 }
611 if !propertiesValue.IsValid() {
612 continue
613 }
614
Paul Duffin73bf0542019-07-12 14:12:49 +0100615 check := func(value string) bool {
Artur Satayevc5570ac2020-04-09 16:06:36 +0100616 return prop.matcher.Test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800617 }
618
619 if matchValue(propertiesValue, check) {
620 return true
621 }
622 }
623 return false
624}
625
626func matchValue(value reflect.Value, check func(string) bool) bool {
627 if !value.IsValid() {
628 return false
629 }
630
631 if value.Kind() == reflect.Ptr {
632 if value.IsNil() {
633 return check("")
634 }
635 value = value.Elem()
636 }
637
638 switch value.Kind() {
639 case reflect.String:
640 return check(value.String())
641 case reflect.Bool:
642 return check(strconv.FormatBool(value.Bool()))
643 case reflect.Int:
644 return check(strconv.FormatInt(value.Int(), 10))
645 case reflect.Slice:
646 slice, ok := value.Interface().([]string)
647 if !ok {
648 panic("Can only handle slice of string")
649 }
650 for _, v := range slice {
651 if check(v) {
652 return true
653 }
654 }
655 return false
656 }
657
658 panic("Can't handle type: " + value.Kind().String())
659}
Paul Duffin115445b2019-08-07 15:31:07 +0100660
661var neverallowRulesKey = NewOnceKey("neverallowRules")
662
663func neverallowRules(config Config) []Rule {
664 return config.Once(neverallowRulesKey, func() interface{} {
665 // No test rules were set by setTestNeverallowRules, use the global rules
666 return neverallows
667 }).([]Rule)
668}
669
670// Overrides the default neverallow rules for the supplied config.
671//
672// For testing only.
Artur Satayevc5570ac2020-04-09 16:06:36 +0100673func SetTestNeverallowRules(config Config, testRules []Rule) {
Paul Duffin115445b2019-08-07 15:31:07 +0100674 config.Once(neverallowRulesKey, func() interface{} { return testRules })
675}