blob: af072cdbb8567557926125efec441d52b21ded12 [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
Paul Duffin45338f02021-03-30 23:07:52 +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(createJavaDeviceForHostRules()...)
Colin Crossc511bc52020-04-07 16:50:32 +000055 AddNeverAllowRules(createCcSdkVariantRules()...)
David Srbeckye033cba2020-05-20 22:20:28 +010056 AddNeverAllowRules(createUncompressDexRules()...)
Yifan Hong696ed4d2020-07-27 12:59:58 -070057 AddNeverAllowRules(createMakefileGoalRules()...)
Neil Fullerdf5f3562018-10-21 17:19:10 +010058}
Steven Moreland65b3fd92017-12-06 14:18:35 -080059
Paul Duffin730f2a52019-06-27 14:08:51 +010060// Add a NeverAllow rule to the set of rules to apply.
61func AddNeverAllowRules(rules ...Rule) {
62 neverallows = append(neverallows, rules...)
63}
64
Paul Duffinc8111702019-07-22 12:13:55 +010065func createIncludeDirsRules() []Rule {
Steven Moreland8fc8dbf2021-04-27 02:31:07 +000066 notInIncludeDir := []string{
Paul Duffinc8111702019-07-22 12:13:55 +010067 "art",
Orion Hodson6341f012019-11-06 13:39:46 +000068 "art/libnativebridge",
69 "art/libnativeloader",
Paul Duffinc8111702019-07-22 12:13:55 +010070 "libcore",
71 "libnativehelper",
72 "external/apache-harmony",
73 "external/apache-xml",
74 "external/boringssl",
75 "external/bouncycastle",
76 "external/conscrypt",
77 "external/icu",
78 "external/okhttp",
79 "external/vixl",
80 "external/wycheproof",
Paul Duffinc8111702019-07-22 12:13:55 +010081 }
Steven Moreland8fc8dbf2021-04-27 02:31:07 +000082 noUseIncludeDir := []string{
Steven Morelandf36a3ac2021-04-27 18:03:14 +000083 "frameworks/av/apex",
84 "frameworks/av/tools",
85 "frameworks/native/cmds",
86 "system/apex",
87 "system/bpf",
88 "system/gatekeeper",
89 "system/hwservicemanager",
90 "system/libbase",
Steven Moreland8fc8dbf2021-04-27 02:31:07 +000091 "system/libfmq",
Steven Morelandf36a3ac2021-04-27 18:03:14 +000092 "system/libvintf",
Steven Moreland8fc8dbf2021-04-27 02:31:07 +000093 }
Paul Duffinc8111702019-07-22 12:13:55 +010094
Steven Moreland8fc8dbf2021-04-27 02:31:07 +000095 rules := make([]Rule, 0, len(notInIncludeDir)+len(noUseIncludeDir))
96
97 for _, path := range notInIncludeDir {
Paul Duffinc8111702019-07-22 12:13:55 +010098 rule :=
99 NeverAllow().
100 WithMatcher("include_dirs", StartsWith(path+"/")).
101 Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" +
102 " to use alternate mechanisms and so can no longer be used.")
103
104 rules = append(rules, rule)
105 }
106
Steven Moreland8fc8dbf2021-04-27 02:31:07 +0000107 for _, path := range noUseIncludeDir {
108 rule := NeverAllow().In(path+"/").WithMatcher("include_dirs", isSetMatcherInstance).
109 Because("include_dirs is deprecated, all usages of them in '" + path + "' have been migrated" +
110 " to use alternate mechanisms and so can no longer be used.")
111 rules = append(rules, rule)
112 }
113
Paul Duffinc8111702019-07-22 12:13:55 +0100114 return rules
115}
116
Paul Duffin730f2a52019-06-27 14:08:51 +0100117func createTrebleRules() []Rule {
118 return []Rule{
119 NeverAllow().
120 In("vendor", "device").
121 With("vndk.enabled", "true").
122 Without("vendor", "true").
Justin Yun0ecf0b22020-02-28 15:07:59 +0900123 Without("product_specific", "true").
Paul Duffin730f2a52019-06-27 14:08:51 +0100124 Because("the VNDK can never contain a library that is device dependent."),
125 NeverAllow().
126 With("vndk.enabled", "true").
127 Without("vendor", "true").
128 Without("owner", "").
129 Because("a VNDK module can never have an owner."),
Steven Moreland65b3fd92017-12-06 14:18:35 -0800130
Neil Fullerdf5f3562018-10-21 17:19:10 +0100131 // TODO(b/67974785): always enforce the manifest
Paul Duffin730f2a52019-06-27 14:08:51 +0100132 NeverAllow().
Steven Moreland51ce4f62020-02-10 17:21:32 -0800133 Without("name", "libhidlbase-combined-impl").
134 Without("name", "libhidlbase").
135 Without("name", "libhidlbase_pgo").
Paul Duffin730f2a52019-06-27 14:08:51 +0100136 With("product_variables.enforce_vintf_manifest.cflags", "*").
137 Because("manifest enforcement should be independent of ."),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100138
139 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
Paul Duffin730f2a52019-06-27 14:08:51 +0100140 NeverAllow().
141 Without("name", "libc_bionic_ndk").
142 With("product_variables.treble_linker_namespaces.cflags", "*").
143 Because("nothing should care if linker namespaces are enabled or not"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100144
145 // Example:
Paul Duffin730f2a52019-06-27 14:08:51 +0100146 // *NeverAllow().with("Srcs", "main.cpp"))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100147 }
148}
149
Paul Duffin730f2a52019-06-27 14:08:51 +0100150func createJavaDeviceForHostRules() []Rule {
Colin Cross440e0d02020-06-11 11:32:11 -0700151 javaDeviceForHostProjectsAllowedList := []string{
Colin Crossb5191a52019-04-11 14:07:38 -0700152 "external/guava",
Colin Crossfd4f7432019-03-05 15:06:16 -0800153 "external/robolectric-shadows",
154 "framework/layoutlib",
155 }
156
Paul Duffin730f2a52019-06-27 14:08:51 +0100157 return []Rule{
158 NeverAllow().
Colin Cross440e0d02020-06-11 11:32:11 -0700159 NotIn(javaDeviceForHostProjectsAllowedList...).
Paul Duffin730f2a52019-06-27 14:08:51 +0100160 ModuleType("java_device_for_host", "java_host_for_device").
Colin Cross440e0d02020-06-11 11:32:11 -0700161 Because("java_device_for_host can only be used in allowed projects"),
Colin Crossfd4f7432019-03-05 15:06:16 -0800162 }
163}
164
Colin Crossc511bc52020-04-07 16:50:32 +0000165func createCcSdkVariantRules() []Rule {
Colin Cross440e0d02020-06-11 11:32:11 -0700166 sdkVersionOnlyAllowedList := []string{
Colin Crossc511bc52020-04-07 16:50:32 +0000167 // derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk.
168 // This sometimes works because the APEX modules that contain derive_sdk and
169 // derive_sdk_prefer32 suppress the platform installation rules, but fails when
170 // the APEX modules contain the SDK variant and the platform variant still exists.
Anton Hansson4b8e64b2020-05-27 18:25:23 +0100171 "packages/modules/SdkExtensions/derive_sdk",
Dan Alberte2054a92020-04-20 14:46:47 -0700172 // These are for apps and shouldn't be used by non-SDK variant modules.
173 "prebuilts/ndk",
174 "tools/test/graphicsbenchmark/apps/sample_app",
175 "tools/test/graphicsbenchmark/functional_tests/java",
Dan Albert55576052020-04-20 14:46:47 -0700176 "vendor/xts/gts-tests/hostsidetests/gamedevicecert/apps/javatests",
Chang Li66d3cb72021-06-18 14:04:50 +0000177 "external/libtextclassifier/native",
Colin Crossc511bc52020-04-07 16:50:32 +0000178 }
179
Colin Cross440e0d02020-06-11 11:32:11 -0700180 platformVariantPropertiesAllowedList := []string{
Colin Crossc511bc52020-04-07 16:50:32 +0000181 // android_native_app_glue and libRSSupport use native_window.h but target old
182 // sdk versions (minimum and 9 respectively) where libnativewindow didn't exist,
183 // so they can't add libnativewindow to shared_libs to get the header directory
184 // for the platform variant. Allow them to use the platform variant
185 // property to set shared_libs.
186 "prebuilts/ndk",
187 "frameworks/rs",
188 }
189
190 return []Rule{
191 NeverAllow().
Colin Cross440e0d02020-06-11 11:32:11 -0700192 NotIn(sdkVersionOnlyAllowedList...).
Colin Crossc511bc52020-04-07 16:50:32 +0000193 WithMatcher("sdk_variant_only", isSetMatcherInstance).
Colin Cross440e0d02020-06-11 11:32:11 -0700194 Because("sdk_variant_only can only be used in allowed projects"),
Colin Crossc511bc52020-04-07 16:50:32 +0000195 NeverAllow().
Colin Cross440e0d02020-06-11 11:32:11 -0700196 NotIn(platformVariantPropertiesAllowedList...).
Colin Crossc511bc52020-04-07 16:50:32 +0000197 WithMatcher("platform.shared_libs", isSetMatcherInstance).
Colin Cross440e0d02020-06-11 11:32:11 -0700198 Because("platform variant properties can only be used in allowed projects"),
Colin Crossc511bc52020-04-07 16:50:32 +0000199 }
200}
201
David Srbeckye033cba2020-05-20 22:20:28 +0100202func createUncompressDexRules() []Rule {
203 return []Rule{
204 NeverAllow().
205 NotIn("art").
206 WithMatcher("uncompress_dex", isSetMatcherInstance).
207 Because("uncompress_dex is only allowed for certain jars for test in art."),
208 }
209}
210
Yifan Hong696ed4d2020-07-27 12:59:58 -0700211func createMakefileGoalRules() []Rule {
212 return []Rule{
213 NeverAllow().
214 ModuleType("makefile_goal").
215 WithoutMatcher("product_out_path", Regexp("^boot[0-9a-zA-Z.-]*[.]img$")).
216 Because("Only boot images may be imported as a makefile goal."),
217 }
218}
219
Steven Moreland65b3fd92017-12-06 14:18:35 -0800220func neverallowMutator(ctx BottomUpMutatorContext) {
221 m, ok := ctx.Module().(Module)
222 if !ok {
223 return
224 }
225
226 dir := ctx.ModuleDir() + "/"
227 properties := m.GetProperties()
228
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100229 osClass := ctx.Module().Target().Os.Class
230
Paul Duffin115445b2019-08-07 15:31:07 +0100231 for _, r := range neverallowRules(ctx.Config()) {
Paul Duffin730f2a52019-06-27 14:08:51 +0100232 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800233 if !n.appliesToPath(dir) {
234 continue
235 }
236
Colin Crossfd4f7432019-03-05 15:06:16 -0800237 if !n.appliesToModuleType(ctx.ModuleType()) {
238 continue
239 }
240
Steven Moreland65b3fd92017-12-06 14:18:35 -0800241 if !n.appliesToProperties(properties) {
242 continue
243 }
244
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100245 if !n.appliesToOsClass(osClass) {
246 continue
247 }
248
Paul Duffin35781882019-07-25 15:41:09 +0100249 if !n.appliesToDirectDeps(ctx) {
250 continue
251 }
252
Andrei Onea115e7e72020-06-05 21:14:03 +0100253 if !n.appliesToBootclasspathJar(ctx) {
254 continue
255 }
256
Steven Moreland65b3fd92017-12-06 14:18:35 -0800257 ctx.ModuleErrorf("violates " + n.String())
258 }
259}
260
Paul Duffin73bf0542019-07-12 14:12:49 +0100261type ValueMatcher interface {
Artur Satayevc5570ac2020-04-09 16:06:36 +0100262 Test(string) bool
Paul Duffin73bf0542019-07-12 14:12:49 +0100263 String() string
264}
265
266type equalMatcher struct {
267 expected string
268}
269
Artur Satayevc5570ac2020-04-09 16:06:36 +0100270func (m *equalMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100271 return m.expected == value
272}
273
274func (m *equalMatcher) String() string {
275 return "=" + m.expected
276}
277
278type anyMatcher struct {
279}
280
Artur Satayevc5570ac2020-04-09 16:06:36 +0100281func (m *anyMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100282 return true
283}
284
285func (m *anyMatcher) String() string {
286 return "=*"
287}
288
289var anyMatcherInstance = &anyMatcher{}
290
Paul Duffinc8111702019-07-22 12:13:55 +0100291type startsWithMatcher struct {
292 prefix string
293}
294
Artur Satayevc5570ac2020-04-09 16:06:36 +0100295func (m *startsWithMatcher) Test(value string) bool {
Paul Duffinc8111702019-07-22 12:13:55 +0100296 return strings.HasPrefix(value, m.prefix)
297}
298
299func (m *startsWithMatcher) String() string {
300 return ".starts-with(" + m.prefix + ")"
301}
302
Anton Hansson45376402020-04-09 14:18:21 +0100303type regexMatcher struct {
304 re *regexp.Regexp
305}
306
Artur Satayevc5570ac2020-04-09 16:06:36 +0100307func (m *regexMatcher) Test(value string) bool {
Anton Hansson45376402020-04-09 14:18:21 +0100308 return m.re.MatchString(value)
309}
310
311func (m *regexMatcher) String() string {
312 return ".regexp(" + m.re.String() + ")"
313}
314
Andrei Onea115e7e72020-06-05 21:14:03 +0100315type notInListMatcher struct {
316 allowed []string
317}
318
319func (m *notInListMatcher) Test(value string) bool {
320 return !InList(value, m.allowed)
321}
322
323func (m *notInListMatcher) String() string {
324 return ".not-in-list(" + strings.Join(m.allowed, ",") + ")"
325}
326
Colin Crossc511bc52020-04-07 16:50:32 +0000327type isSetMatcher struct{}
328
Artur Satayevc5570ac2020-04-09 16:06:36 +0100329func (m *isSetMatcher) Test(value string) bool {
Colin Crossc511bc52020-04-07 16:50:32 +0000330 return value != ""
331}
332
333func (m *isSetMatcher) String() string {
334 return ".is-set"
335}
336
337var isSetMatcherInstance = &isSetMatcher{}
338
Steven Moreland65b3fd92017-12-06 14:18:35 -0800339type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100340 fields []string // e.x.: Vndk.Enabled
341 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800342}
343
Paul Duffin730f2a52019-06-27 14:08:51 +0100344// A NeverAllow rule.
345type Rule interface {
346 In(path ...string) Rule
347
348 NotIn(path ...string) Rule
349
Paul Duffin35781882019-07-25 15:41:09 +0100350 InDirectDeps(deps ...string) Rule
351
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100352 WithOsClass(osClasses ...OsClass) Rule
353
Paul Duffin730f2a52019-06-27 14:08:51 +0100354 ModuleType(types ...string) Rule
355
356 NotModuleType(types ...string) Rule
357
Andrei Onea115e7e72020-06-05 21:14:03 +0100358 BootclasspathJar() Rule
359
Paul Duffin730f2a52019-06-27 14:08:51 +0100360 With(properties, value string) Rule
361
Paul Duffinc8111702019-07-22 12:13:55 +0100362 WithMatcher(properties string, matcher ValueMatcher) Rule
363
Paul Duffin730f2a52019-06-27 14:08:51 +0100364 Without(properties, value string) Rule
365
Paul Duffinc8111702019-07-22 12:13:55 +0100366 WithoutMatcher(properties string, matcher ValueMatcher) Rule
367
Paul Duffin730f2a52019-06-27 14:08:51 +0100368 Because(reason string) Rule
369}
370
Steven Moreland65b3fd92017-12-06 14:18:35 -0800371type rule struct {
372 // User string for why this is a thing.
373 reason string
374
375 paths []string
376 unlessPaths []string
377
Paul Duffin35781882019-07-25 15:41:09 +0100378 directDeps map[string]bool
379
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100380 osClasses []OsClass
381
Colin Crossfd4f7432019-03-05 15:06:16 -0800382 moduleTypes []string
383 unlessModuleTypes []string
384
Steven Moreland65b3fd92017-12-06 14:18:35 -0800385 props []ruleProperty
386 unlessProps []ruleProperty
Andrei Onea115e7e72020-06-05 21:14:03 +0100387
388 onlyBootclasspathJar bool
Steven Moreland65b3fd92017-12-06 14:18:35 -0800389}
390
Paul Duffin730f2a52019-06-27 14:08:51 +0100391// Create a new NeverAllow rule.
392func NeverAllow() Rule {
Paul Duffin35781882019-07-25 15:41:09 +0100393 return &rule{directDeps: make(map[string]bool)}
Steven Moreland65b3fd92017-12-06 14:18:35 -0800394}
Colin Crossfd4f7432019-03-05 15:06:16 -0800395
Paul Duffin730f2a52019-06-27 14:08:51 +0100396func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800397 r.paths = append(r.paths, cleanPaths(path)...)
398 return r
399}
Colin Crossfd4f7432019-03-05 15:06:16 -0800400
Paul Duffin730f2a52019-06-27 14:08:51 +0100401func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800402 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
403 return r
404}
Colin Crossfd4f7432019-03-05 15:06:16 -0800405
Paul Duffin35781882019-07-25 15:41:09 +0100406func (r *rule) InDirectDeps(deps ...string) Rule {
407 for _, d := range deps {
408 r.directDeps[d] = true
409 }
410 return r
411}
412
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100413func (r *rule) WithOsClass(osClasses ...OsClass) Rule {
414 r.osClasses = append(r.osClasses, osClasses...)
415 return r
416}
417
Paul Duffin730f2a52019-06-27 14:08:51 +0100418func (r *rule) ModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800419 r.moduleTypes = append(r.moduleTypes, types...)
420 return r
421}
422
Paul Duffin730f2a52019-06-27 14:08:51 +0100423func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800424 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
425 return r
426}
427
Paul Duffin730f2a52019-06-27 14:08:51 +0100428func (r *rule) With(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100429 return r.WithMatcher(properties, selectMatcher(value))
430}
431
432func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800433 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100434 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100435 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800436 })
437 return r
438}
Colin Crossfd4f7432019-03-05 15:06:16 -0800439
Paul Duffin730f2a52019-06-27 14:08:51 +0100440func (r *rule) Without(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100441 return r.WithoutMatcher(properties, selectMatcher(value))
442}
443
444func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800445 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100446 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100447 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800448 })
449 return r
450}
Colin Crossfd4f7432019-03-05 15:06:16 -0800451
Paul Duffin73bf0542019-07-12 14:12:49 +0100452func selectMatcher(expected string) ValueMatcher {
453 if expected == "*" {
454 return anyMatcherInstance
455 }
456 return &equalMatcher{expected: expected}
457}
458
Paul Duffin730f2a52019-06-27 14:08:51 +0100459func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800460 r.reason = reason
461 return r
462}
463
Andrei Onea115e7e72020-06-05 21:14:03 +0100464func (r *rule) BootclasspathJar() Rule {
465 r.onlyBootclasspathJar = true
466 return r
467}
468
Steven Moreland65b3fd92017-12-06 14:18:35 -0800469func (r *rule) String() string {
470 s := "neverallow"
471 for _, v := range r.paths {
472 s += " dir:" + v + "*"
473 }
474 for _, v := range r.unlessPaths {
475 s += " -dir:" + v + "*"
476 }
Colin Crossfd4f7432019-03-05 15:06:16 -0800477 for _, v := range r.moduleTypes {
478 s += " type:" + v
479 }
480 for _, v := range r.unlessModuleTypes {
481 s += " -type:" + v
482 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800483 for _, v := range r.props {
Paul Duffin73bf0542019-07-12 14:12:49 +0100484 s += " " + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800485 }
486 for _, v := range r.unlessProps {
Paul Duffin73bf0542019-07-12 14:12:49 +0100487 s += " -" + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800488 }
Paul Duffin35781882019-07-25 15:41:09 +0100489 for k := range r.directDeps {
490 s += " deps:" + k
491 }
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100492 for _, v := range r.osClasses {
493 s += " os:" + v.String()
494 }
Andrei Onea115e7e72020-06-05 21:14:03 +0100495 if r.onlyBootclasspathJar {
496 s += " inBcp"
497 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800498 if len(r.reason) != 0 {
499 s += " which is restricted because " + r.reason
500 }
501 return s
502}
503
504func (r *rule) appliesToPath(dir string) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800505 includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths)
506 excludePath := HasAnyPrefix(dir, r.unlessPaths)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800507 return includePath && !excludePath
508}
509
Paul Duffin35781882019-07-25 15:41:09 +0100510func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool {
511 if len(r.directDeps) == 0 {
512 return true
513 }
514
515 matches := false
516 ctx.VisitDirectDeps(func(m Module) {
517 if !matches {
518 name := ctx.OtherModuleName(m)
519 matches = r.directDeps[name]
520 }
521 })
522
523 return matches
524}
525
Andrei Onea115e7e72020-06-05 21:14:03 +0100526func (r *rule) appliesToBootclasspathJar(ctx BottomUpMutatorContext) bool {
527 if !r.onlyBootclasspathJar {
528 return true
529 }
530
531 return InList(ctx.ModuleName(), ctx.Config().BootJars())
532}
533
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100534func (r *rule) appliesToOsClass(osClass OsClass) bool {
535 if len(r.osClasses) == 0 {
536 return true
537 }
538
539 for _, c := range r.osClasses {
540 if c == osClass {
541 return true
542 }
543 }
544
545 return false
546}
547
Colin Crossfd4f7432019-03-05 15:06:16 -0800548func (r *rule) appliesToModuleType(moduleType string) bool {
549 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
550}
551
Steven Moreland65b3fd92017-12-06 14:18:35 -0800552func (r *rule) appliesToProperties(properties []interface{}) bool {
553 includeProps := hasAllProperties(properties, r.props)
554 excludeProps := hasAnyProperty(properties, r.unlessProps)
555 return includeProps && !excludeProps
556}
557
Paul Duffinc8111702019-07-22 12:13:55 +0100558func StartsWith(prefix string) ValueMatcher {
559 return &startsWithMatcher{prefix}
560}
561
Anton Hansson45376402020-04-09 14:18:21 +0100562func Regexp(re string) ValueMatcher {
563 r, err := regexp.Compile(re)
564 if err != nil {
565 panic(err)
566 }
567 return &regexMatcher{r}
568}
569
Andrei Onea115e7e72020-06-05 21:14:03 +0100570func NotInList(allowed []string) ValueMatcher {
571 return &notInListMatcher{allowed}
572}
573
Steven Moreland65b3fd92017-12-06 14:18:35 -0800574// assorted utils
575
576func cleanPaths(paths []string) []string {
577 res := make([]string, len(paths))
578 for i, v := range paths {
579 res[i] = filepath.Clean(v) + "/"
580 }
581 return res
582}
583
584func fieldNamesForProperties(propertyNames string) []string {
585 names := strings.Split(propertyNames, ".")
586 for i, v := range names {
587 names[i] = proptools.FieldNameForProperty(v)
588 }
589 return names
590}
591
Steven Moreland65b3fd92017-12-06 14:18:35 -0800592func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
593 for _, v := range props {
594 if hasProperty(properties, v) {
595 return true
596 }
597 }
598 return false
599}
600
601func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
602 for _, v := range props {
603 if !hasProperty(properties, v) {
604 return false
605 }
606 }
607 return true
608}
609
610func hasProperty(properties []interface{}, prop ruleProperty) bool {
611 for _, propertyStruct := range properties {
612 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
613 for _, v := range prop.fields {
614 if !propertiesValue.IsValid() {
615 break
616 }
617 propertiesValue = propertiesValue.FieldByName(v)
618 }
619 if !propertiesValue.IsValid() {
620 continue
621 }
622
Paul Duffin73bf0542019-07-12 14:12:49 +0100623 check := func(value string) bool {
Artur Satayevc5570ac2020-04-09 16:06:36 +0100624 return prop.matcher.Test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800625 }
626
627 if matchValue(propertiesValue, check) {
628 return true
629 }
630 }
631 return false
632}
633
634func matchValue(value reflect.Value, check func(string) bool) bool {
635 if !value.IsValid() {
636 return false
637 }
638
639 if value.Kind() == reflect.Ptr {
640 if value.IsNil() {
641 return check("")
642 }
643 value = value.Elem()
644 }
645
646 switch value.Kind() {
647 case reflect.String:
648 return check(value.String())
649 case reflect.Bool:
650 return check(strconv.FormatBool(value.Bool()))
651 case reflect.Int:
652 return check(strconv.FormatInt(value.Int(), 10))
653 case reflect.Slice:
654 slice, ok := value.Interface().([]string)
655 if !ok {
656 panic("Can only handle slice of string")
657 }
658 for _, v := range slice {
659 if check(v) {
660 return true
661 }
662 }
663 return false
664 }
665
666 panic("Can't handle type: " + value.Kind().String())
667}
Paul Duffin115445b2019-08-07 15:31:07 +0100668
669var neverallowRulesKey = NewOnceKey("neverallowRules")
670
671func neverallowRules(config Config) []Rule {
672 return config.Once(neverallowRulesKey, func() interface{} {
673 // No test rules were set by setTestNeverallowRules, use the global rules
674 return neverallows
675 }).([]Rule)
676}
677
678// Overrides the default neverallow rules for the supplied config.
679//
680// For testing only.
Paul Duffin45338f02021-03-30 23:07:52 +0100681func setTestNeverallowRules(config Config, testRules []Rule) {
Paul Duffin115445b2019-08-07 15:31:07 +0100682 config.Once(neverallowRulesKey, func() interface{} { return testRules })
683}
Paul Duffin45338f02021-03-30 23:07:52 +0100684
685// Prepares for a test by setting neverallow rules and enabling the mutator.
686//
687// If the supplied rules are nil then the default rules are used.
688func PrepareForTestWithNeverallowRules(testRules []Rule) FixturePreparer {
689 return GroupFixturePreparers(
690 FixtureModifyConfig(func(config Config) {
691 if testRules != nil {
692 setTestNeverallowRules(config, testRules)
693 }
694 }),
695 FixtureRegisterWithContext(func(ctx RegistrationContext) {
696 ctx.PostDepsMutators(registerNeverallowMutator)
697 }),
698 )
699}