blob: cf0b2977953aef08df5ff701e98a1a80fc80c375 [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 (
Liz Kammera3d79152021-10-28 18:14:04 -040018 "fmt"
Steven Moreland65b3fd92017-12-06 14:18:35 -080019 "path/filepath"
20 "reflect"
Anton Hansson45376402020-04-09 14:18:21 +010021 "regexp"
Steven Moreland65b3fd92017-12-06 14:18:35 -080022 "strconv"
23 "strings"
24
25 "github.com/google/blueprint/proptools"
26)
27
28// "neverallow" rules for the build system.
29//
30// This allows things which aren't related to the build system and are enforced
Joe Onoratob4638c12021-10-27 15:47:06 -070031// against assumptions, in progress code refactors, or policy to be expressed in a
Steven Moreland65b3fd92017-12-06 14:18:35 -080032// straightforward away disjoint from implementations and tests which should
33// work regardless of these restrictions.
34//
35// A module is disallowed if all of the following are true:
Paul Duffin730f2a52019-06-27 14:08:51 +010036// - it is in one of the "In" paths
37// - it is not in one of the "NotIn" paths
38// - it has all "With" properties matched
Steven Moreland65b3fd92017-12-06 14:18:35 -080039// - - values are matched in their entirety
40// - - nil is interpreted as an empty string
41// - - nested properties are separated with a '.'
42// - - if the property is a list, any of the values in the list being matches
43// counts as a match
Paul Duffin730f2a52019-06-27 14:08:51 +010044// - it has none of the "Without" properties matched (same rules as above)
Steven Moreland65b3fd92017-12-06 14:18:35 -080045
Paul Duffin45338f02021-03-30 23:07:52 +010046func registerNeverallowMutator(ctx RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -070047 ctx.BottomUp("neverallow", neverallowMutator)
Steven Moreland65b3fd92017-12-06 14:18:35 -080048}
49
Paul Duffin730f2a52019-06-27 14:08:51 +010050var neverallows = []Rule{}
Steven Moreland65b3fd92017-12-06 14:18:35 -080051
Paul Duffin730f2a52019-06-27 14:08:51 +010052func init() {
Paul Duffinc8111702019-07-22 12:13:55 +010053 AddNeverAllowRules(createIncludeDirsRules()...)
Paul Duffin730f2a52019-06-27 14:08:51 +010054 AddNeverAllowRules(createTrebleRules()...)
Paul Duffin730f2a52019-06-27 14:08:51 +010055 AddNeverAllowRules(createJavaDeviceForHostRules()...)
Colin Crossc511bc52020-04-07 16:50:32 +000056 AddNeverAllowRules(createCcSdkVariantRules()...)
David Srbeckye033cba2020-05-20 22:20:28 +010057 AddNeverAllowRules(createUncompressDexRules()...)
Nelson Li43808202024-11-20 09:05:53 +000058 AddNeverAllowRules(createInstallInRootAllowingRules()...)
Jiyong Park3c306f32022-04-05 15:29:53 +090059 AddNeverAllowRules(createProhibitFrameworkAccessRules()...)
Alan Stokes73feba32022-11-14 12:21:24 +000060 AddNeverAllowRules(createCcStubsRule())
Mark Whitea15790a2023-08-22 21:28:11 +000061 AddNeverAllowRules(createProhibitHeaderOnlyRule())
Steven Moreland0db999c2024-09-03 22:06:24 +000062 AddNeverAllowRules(createLimitNdkExportRule()...)
Inseob Kim76e19852024-10-10 17:57:22 +090063 AddNeverAllowRules(createLimitDirgroupRule()...)
Jihoon Kang0d545b82024-10-11 00:21:57 +000064 AddNeverAllowRules(createFilesystemIsAutoGeneratedRule())
Luca Stefani50098f72024-10-12 17:55:31 +020065 AddNeverAllowRules(createKotlinPluginRule()...)
Jihoon Kang2a7bf752024-11-01 21:21:25 +000066 AddNeverAllowRules(createPrebuiltEtcBpDefineRule())
Spandan Dase2f98da2024-11-18 19:22:39 +000067 AddNeverAllowRules(createAutogenRroBpDefineRule())
Neil Fullerdf5f3562018-10-21 17:19:10 +010068}
Steven Moreland65b3fd92017-12-06 14:18:35 -080069
Paul Duffin730f2a52019-06-27 14:08:51 +010070// Add a NeverAllow rule to the set of rules to apply.
71func AddNeverAllowRules(rules ...Rule) {
72 neverallows = append(neverallows, rules...)
73}
74
Sam Delmerico46d08b42022-11-15 15:51:04 -050075var (
76 neverallowNotInIncludeDir = []string{
Paul Duffinc8111702019-07-22 12:13:55 +010077 "art",
Orion Hodson6341f012019-11-06 13:39:46 +000078 "art/libnativebridge",
79 "art/libnativeloader",
Paul Duffinc8111702019-07-22 12:13:55 +010080 "libcore",
81 "libnativehelper",
82 "external/apache-harmony",
83 "external/apache-xml",
84 "external/boringssl",
85 "external/bouncycastle",
86 "external/conscrypt",
87 "external/icu",
88 "external/okhttp",
89 "external/vixl",
90 "external/wycheproof",
Paul Duffinc8111702019-07-22 12:13:55 +010091 }
Sam Delmerico46d08b42022-11-15 15:51:04 -050092 neverallowNoUseIncludeDir = []string{
Steven Morelandf36a3ac2021-04-27 18:03:14 +000093 "frameworks/av/apex",
94 "frameworks/av/tools",
95 "frameworks/native/cmds",
96 "system/apex",
97 "system/bpf",
98 "system/gatekeeper",
99 "system/hwservicemanager",
100 "system/libbase",
Steven Moreland8fc8dbf2021-04-27 02:31:07 +0000101 "system/libfmq",
Steven Morelandf36a3ac2021-04-27 18:03:14 +0000102 "system/libvintf",
Steven Moreland8fc8dbf2021-04-27 02:31:07 +0000103 }
Sam Delmerico46d08b42022-11-15 15:51:04 -0500104)
Paul Duffinc8111702019-07-22 12:13:55 +0100105
Sam Delmerico46d08b42022-11-15 15:51:04 -0500106func createIncludeDirsRules() []Rule {
107 rules := make([]Rule, 0, len(neverallowNotInIncludeDir)+len(neverallowNoUseIncludeDir))
Steven Moreland8fc8dbf2021-04-27 02:31:07 +0000108
Sam Delmerico46d08b42022-11-15 15:51:04 -0500109 for _, path := range neverallowNotInIncludeDir {
Paul Duffinc8111702019-07-22 12:13:55 +0100110 rule :=
111 NeverAllow().
112 WithMatcher("include_dirs", StartsWith(path+"/")).
113 Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" +
114 " to use alternate mechanisms and so can no longer be used.")
115
116 rules = append(rules, rule)
117 }
118
Sam Delmerico46d08b42022-11-15 15:51:04 -0500119 for _, path := range neverallowNoUseIncludeDir {
Steven Moreland8fc8dbf2021-04-27 02:31:07 +0000120 rule := NeverAllow().In(path+"/").WithMatcher("include_dirs", isSetMatcherInstance).
121 Because("include_dirs is deprecated, all usages of them in '" + path + "' have been migrated" +
122 " to use alternate mechanisms and so can no longer be used.")
123 rules = append(rules, rule)
124 }
125
Paul Duffinc8111702019-07-22 12:13:55 +0100126 return rules
127}
128
Paul Duffin730f2a52019-06-27 14:08:51 +0100129func createTrebleRules() []Rule {
130 return []Rule{
131 NeverAllow().
132 In("vendor", "device").
133 With("vndk.enabled", "true").
134 Without("vendor", "true").
Justin Yun0ecf0b22020-02-28 15:07:59 +0900135 Without("product_specific", "true").
Paul Duffin730f2a52019-06-27 14:08:51 +0100136 Because("the VNDK can never contain a library that is device dependent."),
137 NeverAllow().
138 With("vndk.enabled", "true").
139 Without("vendor", "true").
140 Without("owner", "").
141 Because("a VNDK module can never have an owner."),
Steven Moreland65b3fd92017-12-06 14:18:35 -0800142
Neil Fullerdf5f3562018-10-21 17:19:10 +0100143 // TODO(b/67974785): always enforce the manifest
Paul Duffin730f2a52019-06-27 14:08:51 +0100144 NeverAllow().
Steven Moreland51ce4f62020-02-10 17:21:32 -0800145 Without("name", "libhidlbase-combined-impl").
146 Without("name", "libhidlbase").
Paul Duffin730f2a52019-06-27 14:08:51 +0100147 With("product_variables.enforce_vintf_manifest.cflags", "*").
148 Because("manifest enforcement should be independent of ."),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100149
150 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
Paul Duffin730f2a52019-06-27 14:08:51 +0100151 NeverAllow().
152 Without("name", "libc_bionic_ndk").
153 With("product_variables.treble_linker_namespaces.cflags", "*").
154 Because("nothing should care if linker namespaces are enabled or not"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100155
156 // Example:
Paul Duffin730f2a52019-06-27 14:08:51 +0100157 // *NeverAllow().with("Srcs", "main.cpp"))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100158 }
159}
160
Paul Duffin730f2a52019-06-27 14:08:51 +0100161func createJavaDeviceForHostRules() []Rule {
Colin Cross440e0d02020-06-11 11:32:11 -0700162 javaDeviceForHostProjectsAllowedList := []string{
Dan Willemsen9fe14102021-07-13 21:52:04 -0700163 "development/build",
Colin Crossb5191a52019-04-11 14:07:38 -0700164 "external/guava",
Steve Elliott8053f822022-10-18 17:09:28 -0400165 "external/kotlinx.coroutines",
Colin Crossfd4f7432019-03-05 15:06:16 -0800166 "external/robolectric-shadows",
Rex Hoffman54641d22022-08-25 17:29:50 +0000167 "external/robolectric",
Makoto Onukib66bba32023-11-11 00:06:09 +0000168 "frameworks/base/ravenwood",
169 "frameworks/base/tools/hoststubgen",
Jerome Gaillard655ee022021-09-23 11:38:08 +0000170 "frameworks/layoutlib",
Colin Crossfd4f7432019-03-05 15:06:16 -0800171 }
172
Paul Duffin730f2a52019-06-27 14:08:51 +0100173 return []Rule{
174 NeverAllow().
Colin Cross440e0d02020-06-11 11:32:11 -0700175 NotIn(javaDeviceForHostProjectsAllowedList...).
Paul Duffin730f2a52019-06-27 14:08:51 +0100176 ModuleType("java_device_for_host", "java_host_for_device").
Colin Cross440e0d02020-06-11 11:32:11 -0700177 Because("java_device_for_host can only be used in allowed projects"),
Colin Crossfd4f7432019-03-05 15:06:16 -0800178 }
179}
180
Colin Crossc511bc52020-04-07 16:50:32 +0000181func createCcSdkVariantRules() []Rule {
Colin Cross440e0d02020-06-11 11:32:11 -0700182 sdkVersionOnlyAllowedList := []string{
Colin Crossc511bc52020-04-07 16:50:32 +0000183 // derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk.
184 // This sometimes works because the APEX modules that contain derive_sdk and
185 // derive_sdk_prefer32 suppress the platform installation rules, but fails when
186 // the APEX modules contain the SDK variant and the platform variant still exists.
Anton Hansson4b8e64b2020-05-27 18:25:23 +0100187 "packages/modules/SdkExtensions/derive_sdk",
Dan Alberte2054a92020-04-20 14:46:47 -0700188 // These are for apps and shouldn't be used by non-SDK variant modules.
189 "prebuilts/ndk",
Jiyong Parka574d532024-08-28 18:06:43 +0900190 "frameworks/native/libs/binder/ndk",
Dan Alberte2054a92020-04-20 14:46:47 -0700191 "tools/test/graphicsbenchmark/apps/sample_app",
192 "tools/test/graphicsbenchmark/functional_tests/java",
Dan Albert55576052020-04-20 14:46:47 -0700193 "vendor/xts/gts-tests/hostsidetests/gamedevicecert/apps/javatests",
Chang Li66d3cb72021-06-18 14:04:50 +0000194 "external/libtextclassifier/native",
Colin Crossc511bc52020-04-07 16:50:32 +0000195 }
196
Colin Cross440e0d02020-06-11 11:32:11 -0700197 platformVariantPropertiesAllowedList := []string{
Colin Crossc511bc52020-04-07 16:50:32 +0000198 // android_native_app_glue and libRSSupport use native_window.h but target old
199 // sdk versions (minimum and 9 respectively) where libnativewindow didn't exist,
200 // so they can't add libnativewindow to shared_libs to get the header directory
201 // for the platform variant. Allow them to use the platform variant
202 // property to set shared_libs.
203 "prebuilts/ndk",
204 "frameworks/rs",
205 }
206
207 return []Rule{
208 NeverAllow().
Colin Cross440e0d02020-06-11 11:32:11 -0700209 NotIn(sdkVersionOnlyAllowedList...).
Colin Crossc511bc52020-04-07 16:50:32 +0000210 WithMatcher("sdk_variant_only", isSetMatcherInstance).
Colin Cross440e0d02020-06-11 11:32:11 -0700211 Because("sdk_variant_only can only be used in allowed projects"),
Colin Crossc511bc52020-04-07 16:50:32 +0000212 NeverAllow().
Colin Cross440e0d02020-06-11 11:32:11 -0700213 NotIn(platformVariantPropertiesAllowedList...).
Colin Crossc511bc52020-04-07 16:50:32 +0000214 WithMatcher("platform.shared_libs", isSetMatcherInstance).
Colin Cross440e0d02020-06-11 11:32:11 -0700215 Because("platform variant properties can only be used in allowed projects"),
Colin Crossc511bc52020-04-07 16:50:32 +0000216 }
217}
218
Alan Stokes73feba32022-11-14 12:21:24 +0000219func createCcStubsRule() Rule {
220 ccStubsImplementationInstallableProjectsAllowedList := []string{
Jiyong Parkf6736c72024-07-22 11:22:35 +0900221 "packages/modules/Virtualization/libs/libvm_payload",
Alan Stokes73feba32022-11-14 12:21:24 +0000222 }
223
224 return NeverAllow().
225 NotIn(ccStubsImplementationInstallableProjectsAllowedList...).
226 WithMatcher("stubs.implementation_installable", isSetMatcherInstance).
227 Because("implementation_installable can only be used in allowed projects.")
228}
229
David Srbeckye033cba2020-05-20 22:20:28 +0100230func createUncompressDexRules() []Rule {
231 return []Rule{
232 NeverAllow().
Jiakai Zhang8e7b98c2025-01-08 14:16:52 +0000233 NotIn("art", "cts/hostsidetests/compilation").
David Srbeckye033cba2020-05-20 22:20:28 +0100234 WithMatcher("uncompress_dex", isSetMatcherInstance).
235 Because("uncompress_dex is only allowed for certain jars for test in art."),
236 }
237}
238
Nelson Li43808202024-11-20 09:05:53 +0000239func createInstallInRootAllowingRules() []Rule {
Inseob Kim800d1142021-06-14 12:03:51 +0900240 return []Rule{
241 NeverAllow().
Nikita Ioffe11a9c2c2023-06-21 16:51:09 +0100242 Without("name", "init_first_stage_defaults").
Inseob Kim800d1142021-06-14 12:03:51 +0900243 Without("name", "init_first_stage").
Nikita Ioffe11a9c2c2023-06-21 16:51:09 +0100244 Without("name", "init_first_stage.microdroid").
Nelson Li43808202024-11-20 09:05:53 +0000245 Without("name", "librecovery_ui_ext").
Inseob Kim800d1142021-06-14 12:03:51 +0900246 With("install_in_root", "true").
Inseob Kimbe6a66d2024-07-11 15:43:44 +0900247 NotModuleType("prebuilt_root").
Jihoon Kang320ca7c2024-12-03 18:14:50 +0000248 NotModuleType("prebuilt_vendor").
249 NotModuleType("prebuilt_sbin").
250 NotModuleType("prebuilt_system").
251 NotModuleType("prebuilt_first_stage_ramdisk").
Jihoon Kang96f38d22024-12-03 23:17:55 +0000252 NotModuleType("prebuilt_res").
Nelson Li43808202024-11-20 09:05:53 +0000253 Because("install_in_root is only for init_first_stage or librecovery_ui_ext."),
Inseob Kim800d1142021-06-14 12:03:51 +0900254 }
255}
256
Jiyong Park3c306f32022-04-05 15:29:53 +0900257func createProhibitFrameworkAccessRules() []Rule {
258 return []Rule{
259 NeverAllow().
260 With("libs", "framework").
261 WithoutMatcher("sdk_version", Regexp("(core_.*|^$)")).
262 Because("framework can't be used when building against SDK"),
263 }
264}
265
Mark Whitea15790a2023-08-22 21:28:11 +0000266func createProhibitHeaderOnlyRule() Rule {
267 return NeverAllow().
268 Without("name", "framework-minus-apex-headers").
269 With("headers_only", "true").
270 Because("headers_only can only be used for generating framework-minus-apex headers for non-updatable modules")
271}
272
Steven Moreland0db999c2024-09-03 22:06:24 +0000273func createLimitNdkExportRule() []Rule {
274 reason := "If the headers you're trying to export are meant to be a part of the NDK, they should be exposed by an ndk_headers module. If the headers shouldn't be a part of the NDK, the headers should instead be exposed from a separate `cc_library_headers` which consumers depend on."
275 // DO NOT ADD HERE - please consult danalbert@
276 // b/357711733
277 return []Rule{
278 NeverAllow().
279 NotIn("frameworks/native/libs/binder/ndk").
280 ModuleType("ndk_library").
281 WithMatcher("export_header_libs", isSetMatcherInstance).Because(reason),
282 NeverAllow().ModuleType("ndk_library").WithMatcher("export_generated_headers", isSetMatcherInstance).Because(reason),
283 NeverAllow().ModuleType("ndk_library").WithMatcher("export_include_dirs", isSetMatcherInstance).Because(reason),
284 NeverAllow().ModuleType("ndk_library").WithMatcher("export_shared_lib_headers", isSetMatcherInstance).Because(reason),
285 NeverAllow().ModuleType("ndk_library").WithMatcher("export_static_lib_headers", isSetMatcherInstance).Because(reason),
286 }
287}
288
Inseob Kim76e19852024-10-10 17:57:22 +0900289func createLimitDirgroupRule() []Rule {
Inseob Kim7195b062024-11-29 15:40:49 +0900290 reason := "dirgroup module and dir_srcs / keep_gendir property of genrule is allowed only to Trusty build rule."
Inseob Kim76e19852024-10-10 17:57:22 +0900291 return []Rule{
292 NeverAllow().
293 ModuleType("dirgroup").
Orlando Arbildo47afbdd2025-01-08 00:27:37 +0000294 WithMatcher("visibility", NotInList([]string{"//trusty/vendor/google/aosp/scripts", "//trusty/vendor/google/proprietary/scripts"})).Because(reason),
Inseob Kim76e19852024-10-10 17:57:22 +0900295 NeverAllow().
296 ModuleType("dirgroup").
Orlando Arbildo47afbdd2025-01-08 00:27:37 +0000297 WithoutMatcher("visibility", InAllowedList([]string{"//trusty/vendor/google/aosp/scripts", "//trusty/vendor/google/proprietary/scripts"})).Because(reason),
Inseob Kim76e19852024-10-10 17:57:22 +0900298 NeverAllow().
299 ModuleType("genrule").
Inseob Kime8b1ac42024-10-24 09:27:44 +0000300 Without("name", "trusty-arm64.lk.elf.gen").
Alice Wang38aed8d2024-11-27 08:18:59 +0000301 Without("name", "trusty-arm64-virt-test-debug.lk.elf.gen").
Inseob Kime8b1ac42024-10-24 09:27:44 +0000302 Without("name", "trusty-x86_64.lk.elf.gen").
303 Without("name", "trusty-x86_64-test.lk.elf.gen").
Orlando Arbildo47afbdd2025-01-08 00:27:37 +0000304 Without("name", "trusty-arm64.wv.lk.elf.gen").
305 Without("name", "trusty-arm64-virt-test-debug.wv.lk.elf.gen").
306 Without("name", "trusty-x86_64.wv.lk.elf.gen").
307 Without("name", "trusty-x86_64-test.wv.lk.elf.gen").
Inseob Kim76e19852024-10-10 17:57:22 +0900308 WithMatcher("dir_srcs", isSetMatcherInstance).Because(reason),
Inseob Kim7195b062024-11-29 15:40:49 +0900309 NeverAllow().
310 ModuleType("genrule").
311 Without("name", "trusty-arm64.lk.elf.gen").
312 Without("name", "trusty-arm64-virt-test-debug.lk.elf.gen").
313 Without("name", "trusty-x86_64.lk.elf.gen").
314 Without("name", "trusty-x86_64-test.lk.elf.gen").
Orlando Arbildo47afbdd2025-01-08 00:27:37 +0000315 Without("name", "trusty-arm64.wv.lk.elf.gen").
316 Without("name", "trusty-arm64-virt-test-debug.wv.lk.elf.gen").
317 Without("name", "trusty-x86_64.wv.lk.elf.gen").
318 Without("name", "trusty-x86_64-test.wv.lk.elf.gen").
Inseob Kim7195b062024-11-29 15:40:49 +0900319 With("keep_gendir", "true").Because(reason),
Inseob Kim76e19852024-10-10 17:57:22 +0900320 }
321}
322
Jihoon Kang0d545b82024-10-11 00:21:57 +0000323func createFilesystemIsAutoGeneratedRule() Rule {
324 return NeverAllow().
325 NotIn("build/soong/fsgen").
326 ModuleType("filesystem", "android_system_image").
327 WithMatcher("is_auto_generated", isSetMatcherInstance).
328 Because("is_auto_generated property is only allowed for filesystem modules in build/soong/fsgen directory")
329}
330
Luca Stefani50098f72024-10-12 17:55:31 +0200331func createKotlinPluginRule() []Rule {
332 kotlinPluginProjectsAllowedList := []string{
Luca Stefani50098f72024-10-12 17:55:31 +0200333 "external/kotlinc",
334 }
335
336 return []Rule{
337 NeverAllow().
338 NotIn(kotlinPluginProjectsAllowedList...).
339 ModuleType("kotlin_plugin").
340 Because("kotlin_plugin can only be used in allowed projects"),
341 }
342}
343
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000344// These module types are introduced to convert PRODUCT_COPY_FILES to Soong,
345// and is only intended to be used by filesystem_creator.
346func createPrebuiltEtcBpDefineRule() Rule {
347 return NeverAllow().
348 ModuleType(
349 "prebuilt_usr_srec",
350 "prebuilt_priv_app",
351 "prebuilt_rfs",
352 "prebuilt_framework",
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000353 "prebuilt_wlc_upt",
354 "prebuilt_odm",
Jihoon Kang2e2b7442024-11-05 00:26:20 +0000355 "prebuilt_vendor_dlkm",
356 "prebuilt_bt_firmware",
Jihoon Kangdca2f2b2024-11-06 18:43:19 +0000357 "prebuilt_tvservice",
358 "prebuilt_optee",
Jihoon Kangecf76dd2024-11-12 05:24:46 +0000359 "prebuilt_tvconfig",
Jihoon Kang3ca07a12024-12-02 19:14:30 +0000360 "prebuilt_vendor",
Jihoon Kang320ca7c2024-12-03 18:14:50 +0000361 "prebuilt_sbin",
362 "prebuilt_system",
363 "prebuilt_first_stage_ramdisk",
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000364 ).
365 DefinedInBpFile().
366 Because("module type not allowed to be defined in bp file")
367}
368
Spandan Dase2f98da2024-11-18 19:22:39 +0000369func createAutogenRroBpDefineRule() Rule {
370 return NeverAllow().
371 ModuleType(
372 "autogen_runtime_resource_overlay",
373 ).
374 DefinedInBpFile().
375 Because("Module type will be autogenerated by soong. Use runtime_resource_overlay instead")
376}
377
Steven Moreland65b3fd92017-12-06 14:18:35 -0800378func neverallowMutator(ctx BottomUpMutatorContext) {
379 m, ok := ctx.Module().(Module)
380 if !ok {
381 return
382 }
383
384 dir := ctx.ModuleDir() + "/"
385 properties := m.GetProperties()
386
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100387 osClass := ctx.Module().Target().Os.Class
388
Paul Duffin115445b2019-08-07 15:31:07 +0100389 for _, r := range neverallowRules(ctx.Config()) {
Paul Duffin730f2a52019-06-27 14:08:51 +0100390 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800391 if !n.appliesToPath(dir) {
392 continue
393 }
394
Colin Crossfd4f7432019-03-05 15:06:16 -0800395 if !n.appliesToModuleType(ctx.ModuleType()) {
396 continue
397 }
398
Cole Faust5b35cb92024-08-27 15:47:06 -0700399 if !n.appliesToProperties(ctx, properties) {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800400 continue
401 }
402
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100403 if !n.appliesToOsClass(osClass) {
404 continue
405 }
406
Paul Duffin35781882019-07-25 15:41:09 +0100407 if !n.appliesToDirectDeps(ctx) {
408 continue
409 }
410
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000411 if !n.appliesToBpDefinedModule(ctx) {
412 continue
413 }
414
Steven Moreland65b3fd92017-12-06 14:18:35 -0800415 ctx.ModuleErrorf("violates " + n.String())
416 }
417}
418
Paul Duffin73bf0542019-07-12 14:12:49 +0100419type ValueMatcher interface {
Anton Hanssone1b18362021-12-23 15:05:38 +0000420 Test(string) bool
Paul Duffin73bf0542019-07-12 14:12:49 +0100421 String() string
422}
423
424type equalMatcher struct {
425 expected string
426}
427
Anton Hanssone1b18362021-12-23 15:05:38 +0000428func (m *equalMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100429 return m.expected == value
430}
431
432func (m *equalMatcher) String() string {
433 return "=" + m.expected
434}
435
436type anyMatcher struct {
437}
438
Anton Hanssone1b18362021-12-23 15:05:38 +0000439func (m *anyMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100440 return true
441}
442
443func (m *anyMatcher) String() string {
444 return "=*"
445}
446
447var anyMatcherInstance = &anyMatcher{}
448
Paul Duffinc8111702019-07-22 12:13:55 +0100449type startsWithMatcher struct {
450 prefix string
451}
452
Anton Hanssone1b18362021-12-23 15:05:38 +0000453func (m *startsWithMatcher) Test(value string) bool {
Paul Duffinc8111702019-07-22 12:13:55 +0100454 return strings.HasPrefix(value, m.prefix)
455}
456
457func (m *startsWithMatcher) String() string {
458 return ".starts-with(" + m.prefix + ")"
459}
460
Anton Hansson45376402020-04-09 14:18:21 +0100461type regexMatcher struct {
462 re *regexp.Regexp
463}
464
Anton Hanssone1b18362021-12-23 15:05:38 +0000465func (m *regexMatcher) Test(value string) bool {
Anton Hansson45376402020-04-09 14:18:21 +0100466 return m.re.MatchString(value)
467}
468
469func (m *regexMatcher) String() string {
470 return ".regexp(" + m.re.String() + ")"
471}
472
Andrei Onea115e7e72020-06-05 21:14:03 +0100473type notInListMatcher struct {
474 allowed []string
475}
476
Anton Hanssone1b18362021-12-23 15:05:38 +0000477func (m *notInListMatcher) Test(value string) bool {
Andrei Onea115e7e72020-06-05 21:14:03 +0100478 return !InList(value, m.allowed)
479}
480
481func (m *notInListMatcher) String() string {
482 return ".not-in-list(" + strings.Join(m.allowed, ",") + ")"
483}
484
Orlando Arbildo47afbdd2025-01-08 00:27:37 +0000485type InListMatcher struct {
486 allowed []string
487}
488
489func (m *InListMatcher) Test(value string) bool {
490 return InList(value, m.allowed)
491}
492
493func (m *InListMatcher) String() string {
494 return ".in-list(" + strings.Join(m.allowed, ",") + ")"
495}
496
Colin Crossc511bc52020-04-07 16:50:32 +0000497type isSetMatcher struct{}
498
Anton Hanssone1b18362021-12-23 15:05:38 +0000499func (m *isSetMatcher) Test(value string) bool {
Colin Crossc511bc52020-04-07 16:50:32 +0000500 return value != ""
501}
502
503func (m *isSetMatcher) String() string {
504 return ".is-set"
505}
506
507var isSetMatcherInstance = &isSetMatcher{}
508
Steven Moreland65b3fd92017-12-06 14:18:35 -0800509type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100510 fields []string // e.x.: Vndk.Enabled
511 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800512}
513
Liz Kammera3d79152021-10-28 18:14:04 -0400514func (r *ruleProperty) String() string {
515 return fmt.Sprintf("%q matches: %s", strings.Join(r.fields, "."), r.matcher)
516}
517
518type ruleProperties []ruleProperty
519
520func (r ruleProperties) String() string {
521 var s []string
522 for _, r := range r {
523 s = append(s, r.String())
524 }
525 return strings.Join(s, " ")
526}
527
Paul Duffin730f2a52019-06-27 14:08:51 +0100528// A NeverAllow rule.
529type Rule interface {
530 In(path ...string) Rule
531
532 NotIn(path ...string) Rule
533
Paul Duffin35781882019-07-25 15:41:09 +0100534 InDirectDeps(deps ...string) Rule
535
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100536 WithOsClass(osClasses ...OsClass) Rule
537
Paul Duffin730f2a52019-06-27 14:08:51 +0100538 ModuleType(types ...string) Rule
539
540 NotModuleType(types ...string) Rule
541
542 With(properties, value string) Rule
543
Paul Duffinc8111702019-07-22 12:13:55 +0100544 WithMatcher(properties string, matcher ValueMatcher) Rule
545
Paul Duffin730f2a52019-06-27 14:08:51 +0100546 Without(properties, value string) Rule
547
Paul Duffinc8111702019-07-22 12:13:55 +0100548 WithoutMatcher(properties string, matcher ValueMatcher) Rule
549
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000550 DefinedInBpFile() Rule
551
Paul Duffin730f2a52019-06-27 14:08:51 +0100552 Because(reason string) Rule
553}
554
Steven Moreland65b3fd92017-12-06 14:18:35 -0800555type rule struct {
556 // User string for why this is a thing.
557 reason string
558
559 paths []string
560 unlessPaths []string
561
Paul Duffin35781882019-07-25 15:41:09 +0100562 directDeps map[string]bool
563
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100564 osClasses []OsClass
565
Colin Crossfd4f7432019-03-05 15:06:16 -0800566 moduleTypes []string
567 unlessModuleTypes []string
568
Liz Kammera3d79152021-10-28 18:14:04 -0400569 props ruleProperties
570 unlessProps ruleProperties
Andrei Onea115e7e72020-06-05 21:14:03 +0100571
572 onlyBootclasspathJar bool
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000573
574 definedInBp bool
Steven Moreland65b3fd92017-12-06 14:18:35 -0800575}
576
Paul Duffin730f2a52019-06-27 14:08:51 +0100577// Create a new NeverAllow rule.
578func NeverAllow() Rule {
Paul Duffin35781882019-07-25 15:41:09 +0100579 return &rule{directDeps: make(map[string]bool)}
Steven Moreland65b3fd92017-12-06 14:18:35 -0800580}
Colin Crossfd4f7432019-03-05 15:06:16 -0800581
Liz Kammera3d79152021-10-28 18:14:04 -0400582// In adds path(s) where this rule applies.
Paul Duffin730f2a52019-06-27 14:08:51 +0100583func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800584 r.paths = append(r.paths, cleanPaths(path)...)
585 return r
586}
Colin Crossfd4f7432019-03-05 15:06:16 -0800587
Liz Kammera3d79152021-10-28 18:14:04 -0400588// NotIn adds path(s) to that this rule does not apply to.
Paul Duffin730f2a52019-06-27 14:08:51 +0100589func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800590 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
591 return r
592}
Colin Crossfd4f7432019-03-05 15:06:16 -0800593
Liz Kammera3d79152021-10-28 18:14:04 -0400594// InDirectDeps adds dep(s) that are not allowed with this rule.
Paul Duffin35781882019-07-25 15:41:09 +0100595func (r *rule) InDirectDeps(deps ...string) Rule {
596 for _, d := range deps {
597 r.directDeps[d] = true
598 }
599 return r
600}
601
Liz Kammera3d79152021-10-28 18:14:04 -0400602// WithOsClass adds osClass(es) that this rule applies to.
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100603func (r *rule) WithOsClass(osClasses ...OsClass) Rule {
604 r.osClasses = append(r.osClasses, osClasses...)
605 return r
606}
607
Liz Kammera3d79152021-10-28 18:14:04 -0400608// ModuleType adds type(s) that this rule applies to.
Paul Duffin730f2a52019-06-27 14:08:51 +0100609func (r *rule) ModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800610 r.moduleTypes = append(r.moduleTypes, types...)
611 return r
612}
613
Liz Kammera3d79152021-10-28 18:14:04 -0400614// NotModuleType adds type(s) that this rule does not apply to..
Paul Duffin730f2a52019-06-27 14:08:51 +0100615func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800616 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
617 return r
618}
619
Liz Kammera3d79152021-10-28 18:14:04 -0400620// With specifies property/value combinations that are restricted for this rule.
Paul Duffin730f2a52019-06-27 14:08:51 +0100621func (r *rule) With(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100622 return r.WithMatcher(properties, selectMatcher(value))
623}
624
Liz Kammera3d79152021-10-28 18:14:04 -0400625// WithMatcher specifies property/matcher combinations that are restricted for this rule.
Paul Duffinc8111702019-07-22 12:13:55 +0100626func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800627 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100628 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100629 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800630 })
631 return r
632}
Colin Crossfd4f7432019-03-05 15:06:16 -0800633
Liz Kammera3d79152021-10-28 18:14:04 -0400634// Without specifies property/value combinations that this rule does not apply to.
Paul Duffin730f2a52019-06-27 14:08:51 +0100635func (r *rule) Without(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100636 return r.WithoutMatcher(properties, selectMatcher(value))
637}
638
Liz Kammera3d79152021-10-28 18:14:04 -0400639// Without specifies property/matcher combinations that this rule does not apply to.
Paul Duffinc8111702019-07-22 12:13:55 +0100640func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800641 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100642 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100643 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800644 })
645 return r
646}
Colin Crossfd4f7432019-03-05 15:06:16 -0800647
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000648// DefinedInBpFile specifies that this rule applies to modules that are defined
649// in bp files, and does not apply to modules that are auto generated by other modules.
650func (r *rule) DefinedInBpFile() Rule {
651 r.definedInBp = true
652 return r
653}
654
Paul Duffin73bf0542019-07-12 14:12:49 +0100655func selectMatcher(expected string) ValueMatcher {
656 if expected == "*" {
657 return anyMatcherInstance
658 }
659 return &equalMatcher{expected: expected}
660}
661
Liz Kammera3d79152021-10-28 18:14:04 -0400662// Because specifies a reason for this rule.
Paul Duffin730f2a52019-06-27 14:08:51 +0100663func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800664 r.reason = reason
665 return r
666}
667
668func (r *rule) String() string {
Liz Kammera3d79152021-10-28 18:14:04 -0400669 s := []string{"neverallow requirements. Not allowed:"}
670 if len(r.paths) > 0 {
671 s = append(s, fmt.Sprintf("in dirs: %q", r.paths))
Steven Moreland65b3fd92017-12-06 14:18:35 -0800672 }
Liz Kammera3d79152021-10-28 18:14:04 -0400673 if len(r.moduleTypes) > 0 {
674 s = append(s, fmt.Sprintf("module types: %q", r.moduleTypes))
Steven Moreland65b3fd92017-12-06 14:18:35 -0800675 }
Liz Kammera3d79152021-10-28 18:14:04 -0400676 if len(r.props) > 0 {
677 s = append(s, fmt.Sprintf("properties matching: %s", r.props))
Colin Crossfd4f7432019-03-05 15:06:16 -0800678 }
Liz Kammera3d79152021-10-28 18:14:04 -0400679 if len(r.directDeps) > 0 {
Cole Faust18994c72023-02-28 16:02:16 -0800680 s = append(s, fmt.Sprintf("dep(s): %q", SortedKeys(r.directDeps)))
Colin Crossfd4f7432019-03-05 15:06:16 -0800681 }
Liz Kammera3d79152021-10-28 18:14:04 -0400682 if len(r.osClasses) > 0 {
683 s = append(s, fmt.Sprintf("os class(es): %q", r.osClasses))
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100684 }
Liz Kammera3d79152021-10-28 18:14:04 -0400685 if len(r.unlessPaths) > 0 {
686 s = append(s, fmt.Sprintf("EXCEPT in dirs: %q", r.unlessPaths))
687 }
688 if len(r.unlessModuleTypes) > 0 {
689 s = append(s, fmt.Sprintf("EXCEPT module types: %q", r.unlessModuleTypes))
690 }
691 if len(r.unlessProps) > 0 {
692 s = append(s, fmt.Sprintf("EXCEPT properties matching: %q", r.unlessProps))
Andrei Onea115e7e72020-06-05 21:14:03 +0100693 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800694 if len(r.reason) != 0 {
Liz Kammera3d79152021-10-28 18:14:04 -0400695 s = append(s, " which is restricted because "+r.reason)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800696 }
Liz Kammera3d79152021-10-28 18:14:04 -0400697 if len(s) == 1 {
698 s[0] = "neverallow requirements (empty)"
699 }
700 return strings.Join(s, "\n\t")
Steven Moreland65b3fd92017-12-06 14:18:35 -0800701}
702
703func (r *rule) appliesToPath(dir string) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800704 includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths)
705 excludePath := HasAnyPrefix(dir, r.unlessPaths)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800706 return includePath && !excludePath
707}
708
Paul Duffin35781882019-07-25 15:41:09 +0100709func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool {
710 if len(r.directDeps) == 0 {
711 return true
712 }
713
714 matches := false
715 ctx.VisitDirectDeps(func(m Module) {
716 if !matches {
717 name := ctx.OtherModuleName(m)
718 matches = r.directDeps[name]
719 }
720 })
721
722 return matches
723}
724
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100725func (r *rule) appliesToOsClass(osClass OsClass) bool {
726 if len(r.osClasses) == 0 {
727 return true
728 }
729
730 for _, c := range r.osClasses {
731 if c == osClass {
732 return true
733 }
734 }
735
736 return false
737}
738
Colin Crossfd4f7432019-03-05 15:06:16 -0800739func (r *rule) appliesToModuleType(moduleType string) bool {
Jihoon Kangd5465072024-12-03 00:53:05 +0000740 // Remove prefix for auto-generated modules
741 moduleType = strings.TrimSuffix(moduleType, "__loadHookModule")
742 moduleType = strings.TrimSuffix(moduleType, "__bottomUpMutatorModule")
Colin Crossfd4f7432019-03-05 15:06:16 -0800743 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
744}
745
Cole Faust5b35cb92024-08-27 15:47:06 -0700746func (r *rule) appliesToProperties(ctx BottomUpMutatorContext, properties []interface{}) bool {
747 includeProps := hasAllProperties(ctx, properties, r.props)
748 excludeProps := hasAnyProperty(ctx, properties, r.unlessProps)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800749 return includeProps && !excludeProps
750}
751
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000752func (r *rule) appliesToBpDefinedModule(ctx BottomUpMutatorContext) bool {
753 if !r.definedInBp {
754 return true
755 }
756 return !ctx.OtherModuleIsAutoGenerated(ctx.Module()) == r.definedInBp
757}
758
Paul Duffinc8111702019-07-22 12:13:55 +0100759func StartsWith(prefix string) ValueMatcher {
760 return &startsWithMatcher{prefix}
761}
762
Anton Hansson45376402020-04-09 14:18:21 +0100763func Regexp(re string) ValueMatcher {
764 r, err := regexp.Compile(re)
765 if err != nil {
766 panic(err)
767 }
768 return &regexMatcher{r}
769}
770
Andrei Onea115e7e72020-06-05 21:14:03 +0100771func NotInList(allowed []string) ValueMatcher {
772 return &notInListMatcher{allowed}
773}
774
Orlando Arbildo47afbdd2025-01-08 00:27:37 +0000775func InAllowedList(allowed []string) ValueMatcher {
776 return &InListMatcher{allowed}
777}
778
Steven Moreland65b3fd92017-12-06 14:18:35 -0800779// assorted utils
780
781func cleanPaths(paths []string) []string {
782 res := make([]string, len(paths))
783 for i, v := range paths {
784 res[i] = filepath.Clean(v) + "/"
785 }
786 return res
787}
788
789func fieldNamesForProperties(propertyNames string) []string {
790 names := strings.Split(propertyNames, ".")
791 for i, v := range names {
792 names[i] = proptools.FieldNameForProperty(v)
793 }
794 return names
795}
796
Cole Faust5b35cb92024-08-27 15:47:06 -0700797func hasAnyProperty(ctx BottomUpMutatorContext, properties []interface{}, props []ruleProperty) bool {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800798 for _, v := range props {
Cole Faust5b35cb92024-08-27 15:47:06 -0700799 if hasProperty(ctx, properties, v) {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800800 return true
801 }
802 }
803 return false
804}
805
Cole Faust5b35cb92024-08-27 15:47:06 -0700806func hasAllProperties(ctx BottomUpMutatorContext, properties []interface{}, props []ruleProperty) bool {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800807 for _, v := range props {
Cole Faust5b35cb92024-08-27 15:47:06 -0700808 if !hasProperty(ctx, properties, v) {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800809 return false
810 }
811 }
812 return true
813}
814
Cole Faust5b35cb92024-08-27 15:47:06 -0700815func hasProperty(ctx BottomUpMutatorContext, properties []interface{}, prop ruleProperty) bool {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800816 for _, propertyStruct := range properties {
817 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
818 for _, v := range prop.fields {
819 if !propertiesValue.IsValid() {
820 break
821 }
822 propertiesValue = propertiesValue.FieldByName(v)
823 }
824 if !propertiesValue.IsValid() {
825 continue
826 }
827
Paul Duffin73bf0542019-07-12 14:12:49 +0100828 check := func(value string) bool {
Anton Hanssone1b18362021-12-23 15:05:38 +0000829 return prop.matcher.Test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800830 }
831
Cole Faust5b35cb92024-08-27 15:47:06 -0700832 if matchValue(ctx, propertiesValue, check) {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800833 return true
834 }
835 }
836 return false
837}
838
Cole Faust5b35cb92024-08-27 15:47:06 -0700839func matchValue(ctx BottomUpMutatorContext, value reflect.Value, check func(string) bool) bool {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800840 if !value.IsValid() {
841 return false
842 }
843
844 if value.Kind() == reflect.Ptr {
845 if value.IsNil() {
846 return check("")
847 }
848 value = value.Elem()
849 }
850
Cole Faust5b35cb92024-08-27 15:47:06 -0700851 switch v := value.Interface().(type) {
852 case string:
853 return check(v)
854 case bool:
855 return check(strconv.FormatBool(v))
856 case int:
857 return check(strconv.FormatInt((int64)(v), 10))
858 case []string:
859 for _, v := range v {
860 if check(v) {
861 return true
862 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800863 }
Cole Faust5b35cb92024-08-27 15:47:06 -0700864 return false
865 case proptools.Configurable[string]:
866 return check(v.GetOrDefault(ctx, ""))
867 case proptools.Configurable[bool]:
868 return check(strconv.FormatBool(v.GetOrDefault(ctx, false)))
869 case proptools.Configurable[[]string]:
870 for _, v := range v.GetOrDefault(ctx, nil) {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800871 if check(v) {
872 return true
873 }
874 }
875 return false
876 }
877
878 panic("Can't handle type: " + value.Kind().String())
879}
Paul Duffin115445b2019-08-07 15:31:07 +0100880
881var neverallowRulesKey = NewOnceKey("neverallowRules")
882
883func neverallowRules(config Config) []Rule {
884 return config.Once(neverallowRulesKey, func() interface{} {
885 // No test rules were set by setTestNeverallowRules, use the global rules
886 return neverallows
887 }).([]Rule)
888}
889
890// Overrides the default neverallow rules for the supplied config.
891//
892// For testing only.
Paul Duffin45338f02021-03-30 23:07:52 +0100893func setTestNeverallowRules(config Config, testRules []Rule) {
Paul Duffin115445b2019-08-07 15:31:07 +0100894 config.Once(neverallowRulesKey, func() interface{} { return testRules })
895}
Paul Duffin45338f02021-03-30 23:07:52 +0100896
897// Prepares for a test by setting neverallow rules and enabling the mutator.
898//
899// If the supplied rules are nil then the default rules are used.
900func PrepareForTestWithNeverallowRules(testRules []Rule) FixturePreparer {
901 return GroupFixturePreparers(
902 FixtureModifyConfig(func(config Config) {
903 if testRules != nil {
904 setTestNeverallowRules(config, testRules)
905 }
906 }),
907 FixtureRegisterWithContext(func(ctx RegistrationContext) {
908 ctx.PostDepsMutators(registerNeverallowMutator)
909 }),
910 )
911}