blob: 8995a0ffe5f1b86401735a4a04c40424d370dd6f [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())
Nikita Ioffe50fb49c2025-01-24 13:49:00 +000068 AddNeverAllowRules(createNoSha1HashRule())
Neil Fullerdf5f3562018-10-21 17:19:10 +010069}
Steven Moreland65b3fd92017-12-06 14:18:35 -080070
Paul Duffin730f2a52019-06-27 14:08:51 +010071// Add a NeverAllow rule to the set of rules to apply.
72func AddNeverAllowRules(rules ...Rule) {
73 neverallows = append(neverallows, rules...)
74}
75
Sam Delmerico46d08b42022-11-15 15:51:04 -050076var (
77 neverallowNotInIncludeDir = []string{
Paul Duffinc8111702019-07-22 12:13:55 +010078 "art",
Orion Hodson6341f012019-11-06 13:39:46 +000079 "art/libnativebridge",
80 "art/libnativeloader",
Paul Duffinc8111702019-07-22 12:13:55 +010081 "libcore",
82 "libnativehelper",
83 "external/apache-harmony",
84 "external/apache-xml",
85 "external/boringssl",
86 "external/bouncycastle",
87 "external/conscrypt",
88 "external/icu",
89 "external/okhttp",
90 "external/vixl",
91 "external/wycheproof",
Paul Duffinc8111702019-07-22 12:13:55 +010092 }
Sam Delmerico46d08b42022-11-15 15:51:04 -050093 neverallowNoUseIncludeDir = []string{
Steven Morelandf36a3ac2021-04-27 18:03:14 +000094 "frameworks/av/apex",
95 "frameworks/av/tools",
96 "frameworks/native/cmds",
97 "system/apex",
98 "system/bpf",
99 "system/gatekeeper",
100 "system/hwservicemanager",
101 "system/libbase",
Steven Moreland8fc8dbf2021-04-27 02:31:07 +0000102 "system/libfmq",
Steven Morelandf36a3ac2021-04-27 18:03:14 +0000103 "system/libvintf",
Steven Moreland8fc8dbf2021-04-27 02:31:07 +0000104 }
Sam Delmerico46d08b42022-11-15 15:51:04 -0500105)
Paul Duffinc8111702019-07-22 12:13:55 +0100106
Sam Delmerico46d08b42022-11-15 15:51:04 -0500107func createIncludeDirsRules() []Rule {
108 rules := make([]Rule, 0, len(neverallowNotInIncludeDir)+len(neverallowNoUseIncludeDir))
Steven Moreland8fc8dbf2021-04-27 02:31:07 +0000109
Sam Delmerico46d08b42022-11-15 15:51:04 -0500110 for _, path := range neverallowNotInIncludeDir {
Paul Duffinc8111702019-07-22 12:13:55 +0100111 rule :=
112 NeverAllow().
113 WithMatcher("include_dirs", StartsWith(path+"/")).
114 Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" +
115 " to use alternate mechanisms and so can no longer be used.")
116
117 rules = append(rules, rule)
118 }
119
Sam Delmerico46d08b42022-11-15 15:51:04 -0500120 for _, path := range neverallowNoUseIncludeDir {
Steven Moreland8fc8dbf2021-04-27 02:31:07 +0000121 rule := NeverAllow().In(path+"/").WithMatcher("include_dirs", isSetMatcherInstance).
122 Because("include_dirs is deprecated, all usages of them in '" + path + "' have been migrated" +
123 " to use alternate mechanisms and so can no longer be used.")
124 rules = append(rules, rule)
125 }
126
Paul Duffinc8111702019-07-22 12:13:55 +0100127 return rules
128}
129
Paul Duffin730f2a52019-06-27 14:08:51 +0100130func createTrebleRules() []Rule {
131 return []Rule{
132 NeverAllow().
133 In("vendor", "device").
134 With("vndk.enabled", "true").
135 Without("vendor", "true").
Justin Yun0ecf0b22020-02-28 15:07:59 +0900136 Without("product_specific", "true").
Paul Duffin730f2a52019-06-27 14:08:51 +0100137 Because("the VNDK can never contain a library that is device dependent."),
138 NeverAllow().
139 With("vndk.enabled", "true").
140 Without("vendor", "true").
141 Without("owner", "").
142 Because("a VNDK module can never have an owner."),
Steven Moreland65b3fd92017-12-06 14:18:35 -0800143
Neil Fullerdf5f3562018-10-21 17:19:10 +0100144 // TODO(b/67974785): always enforce the manifest
Paul Duffin730f2a52019-06-27 14:08:51 +0100145 NeverAllow().
Steven Moreland51ce4f62020-02-10 17:21:32 -0800146 Without("name", "libhidlbase-combined-impl").
147 Without("name", "libhidlbase").
Paul Duffin730f2a52019-06-27 14:08:51 +0100148 With("product_variables.enforce_vintf_manifest.cflags", "*").
149 Because("manifest enforcement should be independent of ."),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100150
151 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
Paul Duffin730f2a52019-06-27 14:08:51 +0100152 NeverAllow().
153 Without("name", "libc_bionic_ndk").
154 With("product_variables.treble_linker_namespaces.cflags", "*").
155 Because("nothing should care if linker namespaces are enabled or not"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100156
157 // Example:
Paul Duffin730f2a52019-06-27 14:08:51 +0100158 // *NeverAllow().with("Srcs", "main.cpp"))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100159 }
160}
161
Paul Duffin730f2a52019-06-27 14:08:51 +0100162func createJavaDeviceForHostRules() []Rule {
Colin Cross440e0d02020-06-11 11:32:11 -0700163 javaDeviceForHostProjectsAllowedList := []string{
Dan Willemsen9fe14102021-07-13 21:52:04 -0700164 "development/build",
Colin Crossb5191a52019-04-11 14:07:38 -0700165 "external/guava",
Steve Elliott8053f822022-10-18 17:09:28 -0400166 "external/kotlinx.coroutines",
Colin Crossfd4f7432019-03-05 15:06:16 -0800167 "external/robolectric-shadows",
Rex Hoffman54641d22022-08-25 17:29:50 +0000168 "external/robolectric",
Makoto Onukib66bba32023-11-11 00:06:09 +0000169 "frameworks/base/ravenwood",
170 "frameworks/base/tools/hoststubgen",
Jerome Gaillard655ee022021-09-23 11:38:08 +0000171 "frameworks/layoutlib",
Colin Crossfd4f7432019-03-05 15:06:16 -0800172 }
173
Paul Duffin730f2a52019-06-27 14:08:51 +0100174 return []Rule{
175 NeverAllow().
Colin Cross440e0d02020-06-11 11:32:11 -0700176 NotIn(javaDeviceForHostProjectsAllowedList...).
Paul Duffin730f2a52019-06-27 14:08:51 +0100177 ModuleType("java_device_for_host", "java_host_for_device").
Colin Cross440e0d02020-06-11 11:32:11 -0700178 Because("java_device_for_host can only be used in allowed projects"),
Colin Crossfd4f7432019-03-05 15:06:16 -0800179 }
180}
181
Colin Crossc511bc52020-04-07 16:50:32 +0000182func createCcSdkVariantRules() []Rule {
Colin Cross440e0d02020-06-11 11:32:11 -0700183 sdkVersionOnlyAllowedList := []string{
Colin Crossc511bc52020-04-07 16:50:32 +0000184 // derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk.
185 // This sometimes works because the APEX modules that contain derive_sdk and
186 // derive_sdk_prefer32 suppress the platform installation rules, but fails when
187 // the APEX modules contain the SDK variant and the platform variant still exists.
Anton Hansson4b8e64b2020-05-27 18:25:23 +0100188 "packages/modules/SdkExtensions/derive_sdk",
Dan Alberte2054a92020-04-20 14:46:47 -0700189 // These are for apps and shouldn't be used by non-SDK variant modules.
190 "prebuilts/ndk",
Jiyong Parka574d532024-08-28 18:06:43 +0900191 "frameworks/native/libs/binder/ndk",
Dan Alberte2054a92020-04-20 14:46:47 -0700192 "tools/test/graphicsbenchmark/apps/sample_app",
193 "tools/test/graphicsbenchmark/functional_tests/java",
Dan Albert55576052020-04-20 14:46:47 -0700194 "vendor/xts/gts-tests/hostsidetests/gamedevicecert/apps/javatests",
Chang Li66d3cb72021-06-18 14:04:50 +0000195 "external/libtextclassifier/native",
Colin Crossc511bc52020-04-07 16:50:32 +0000196 }
197
Colin Cross440e0d02020-06-11 11:32:11 -0700198 platformVariantPropertiesAllowedList := []string{
Colin Crossc511bc52020-04-07 16:50:32 +0000199 // android_native_app_glue and libRSSupport use native_window.h but target old
200 // sdk versions (minimum and 9 respectively) where libnativewindow didn't exist,
201 // so they can't add libnativewindow to shared_libs to get the header directory
202 // for the platform variant. Allow them to use the platform variant
203 // property to set shared_libs.
204 "prebuilts/ndk",
205 "frameworks/rs",
206 }
207
208 return []Rule{
209 NeverAllow().
Colin Cross440e0d02020-06-11 11:32:11 -0700210 NotIn(sdkVersionOnlyAllowedList...).
Colin Crossc511bc52020-04-07 16:50:32 +0000211 WithMatcher("sdk_variant_only", isSetMatcherInstance).
Colin Cross440e0d02020-06-11 11:32:11 -0700212 Because("sdk_variant_only can only be used in allowed projects"),
Colin Crossc511bc52020-04-07 16:50:32 +0000213 NeverAllow().
Colin Cross440e0d02020-06-11 11:32:11 -0700214 NotIn(platformVariantPropertiesAllowedList...).
Colin Crossc511bc52020-04-07 16:50:32 +0000215 WithMatcher("platform.shared_libs", isSetMatcherInstance).
Colin Cross440e0d02020-06-11 11:32:11 -0700216 Because("platform variant properties can only be used in allowed projects"),
Colin Crossc511bc52020-04-07 16:50:32 +0000217 }
218}
219
Alan Stokes73feba32022-11-14 12:21:24 +0000220func createCcStubsRule() Rule {
221 ccStubsImplementationInstallableProjectsAllowedList := []string{
Jiyong Parkf6736c72024-07-22 11:22:35 +0900222 "packages/modules/Virtualization/libs/libvm_payload",
Alan Stokes73feba32022-11-14 12:21:24 +0000223 }
224
225 return NeverAllow().
226 NotIn(ccStubsImplementationInstallableProjectsAllowedList...).
227 WithMatcher("stubs.implementation_installable", isSetMatcherInstance).
228 Because("implementation_installable can only be used in allowed projects.")
229}
230
David Srbeckye033cba2020-05-20 22:20:28 +0100231func createUncompressDexRules() []Rule {
232 return []Rule{
233 NeverAllow().
Jiakai Zhang8e7b98c2025-01-08 14:16:52 +0000234 NotIn("art", "cts/hostsidetests/compilation").
David Srbeckye033cba2020-05-20 22:20:28 +0100235 WithMatcher("uncompress_dex", isSetMatcherInstance).
236 Because("uncompress_dex is only allowed for certain jars for test in art."),
237 }
238}
239
Nelson Li43808202024-11-20 09:05:53 +0000240func createInstallInRootAllowingRules() []Rule {
Inseob Kim800d1142021-06-14 12:03:51 +0900241 return []Rule{
242 NeverAllow().
Nikita Ioffe11a9c2c2023-06-21 16:51:09 +0100243 Without("name", "init_first_stage_defaults").
Inseob Kim800d1142021-06-14 12:03:51 +0900244 Without("name", "init_first_stage").
Nikita Ioffe11a9c2c2023-06-21 16:51:09 +0100245 Without("name", "init_first_stage.microdroid").
Nelson Li43808202024-11-20 09:05:53 +0000246 Without("name", "librecovery_ui_ext").
Inseob Kim800d1142021-06-14 12:03:51 +0900247 With("install_in_root", "true").
Inseob Kimbe6a66d2024-07-11 15:43:44 +0900248 NotModuleType("prebuilt_root").
Jihoon Kang320ca7c2024-12-03 18:14:50 +0000249 NotModuleType("prebuilt_vendor").
250 NotModuleType("prebuilt_sbin").
251 NotModuleType("prebuilt_system").
252 NotModuleType("prebuilt_first_stage_ramdisk").
Jihoon Kang96f38d22024-12-03 23:17:55 +0000253 NotModuleType("prebuilt_res").
Nelson Li43808202024-11-20 09:05:53 +0000254 Because("install_in_root is only for init_first_stage or librecovery_ui_ext."),
Inseob Kim800d1142021-06-14 12:03:51 +0900255 }
256}
257
Jiyong Park3c306f32022-04-05 15:29:53 +0900258func createProhibitFrameworkAccessRules() []Rule {
259 return []Rule{
260 NeverAllow().
261 With("libs", "framework").
262 WithoutMatcher("sdk_version", Regexp("(core_.*|^$)")).
263 Because("framework can't be used when building against SDK"),
264 }
265}
266
Mark Whitea15790a2023-08-22 21:28:11 +0000267func createProhibitHeaderOnlyRule() Rule {
268 return NeverAllow().
269 Without("name", "framework-minus-apex-headers").
270 With("headers_only", "true").
271 Because("headers_only can only be used for generating framework-minus-apex headers for non-updatable modules")
272}
273
Steven Moreland0db999c2024-09-03 22:06:24 +0000274func createLimitNdkExportRule() []Rule {
275 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."
276 // DO NOT ADD HERE - please consult danalbert@
277 // b/357711733
278 return []Rule{
279 NeverAllow().
280 NotIn("frameworks/native/libs/binder/ndk").
281 ModuleType("ndk_library").
282 WithMatcher("export_header_libs", isSetMatcherInstance).Because(reason),
283 NeverAllow().ModuleType("ndk_library").WithMatcher("export_generated_headers", isSetMatcherInstance).Because(reason),
284 NeverAllow().ModuleType("ndk_library").WithMatcher("export_include_dirs", isSetMatcherInstance).Because(reason),
285 NeverAllow().ModuleType("ndk_library").WithMatcher("export_shared_lib_headers", isSetMatcherInstance).Because(reason),
286 NeverAllow().ModuleType("ndk_library").WithMatcher("export_static_lib_headers", isSetMatcherInstance).Because(reason),
287 }
288}
289
Inseob Kim76e19852024-10-10 17:57:22 +0900290func createLimitDirgroupRule() []Rule {
Inseob Kim7195b062024-11-29 15:40:49 +0900291 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 +0900292 return []Rule{
293 NeverAllow().
294 ModuleType("dirgroup").
Orlando Arbildo47afbdd2025-01-08 00:27:37 +0000295 WithMatcher("visibility", NotInList([]string{"//trusty/vendor/google/aosp/scripts", "//trusty/vendor/google/proprietary/scripts"})).Because(reason),
Inseob Kim76e19852024-10-10 17:57:22 +0900296 NeverAllow().
297 ModuleType("dirgroup").
Orlando Arbildo47afbdd2025-01-08 00:27:37 +0000298 WithoutMatcher("visibility", InAllowedList([]string{"//trusty/vendor/google/aosp/scripts", "//trusty/vendor/google/proprietary/scripts"})).Because(reason),
Inseob Kim76e19852024-10-10 17:57:22 +0900299 NeverAllow().
300 ModuleType("genrule").
Armelle Laine89dab882025-02-23 07:35:47 +0000301 // Trusty TEE target names
302 Without("name", "trusty_tee_package_goog").
303 Without("name", "trusty_tee_package").
304 // Trusty vm target names
305 Without("name", "trusty_test_vm_arm64.bin").
306 Without("name", "trusty_test_vm_x86_64.elf").
Armelle Laine789e5592025-03-01 17:17:51 +0000307 Without("name", "trusty_test_vm_os_arm64.bin").
308 Without("name", "trusty_test_vm_os_x86_64.elf").
Armelle Laine89dab882025-02-23 07:35:47 +0000309 Without("name", "trusty_security_vm_arm64.bin").
310 Without("name", "trusty_security_vm_x86_64.elf").
311 Without("name", "trusty_widevine_vm_arm64.bin").
312 Without("name", "trusty_widevine_vm_x86_64.elf").
Inseob Kim76e19852024-10-10 17:57:22 +0900313 WithMatcher("dir_srcs", isSetMatcherInstance).Because(reason),
Inseob Kim7195b062024-11-29 15:40:49 +0900314 NeverAllow().
315 ModuleType("genrule").
Armelle Laine89dab882025-02-23 07:35:47 +0000316 // Trusty TEE target names
317 Without("name", "trusty_tee_package_goog").
318 Without("name", "trusty_tee_package").
319 // Trusty vm target names
320 Without("name", "trusty_test_vm_arm64.bin").
321 Without("name", "trusty_test_vm_x86_64.elf").
Armelle Laine789e5592025-03-01 17:17:51 +0000322 Without("name", "trusty_test_vm_os_arm64.bin").
323 Without("name", "trusty_test_vm_os_x86_64.elf").
Armelle Laine89dab882025-02-23 07:35:47 +0000324 Without("name", "trusty_security_vm_arm64.bin").
325 Without("name", "trusty_security_vm_x86_64.elf").
326 Without("name", "trusty_widevine_vm_arm64.bin").
327 Without("name", "trusty_widevine_vm_x86_64.elf").
Inseob Kim7195b062024-11-29 15:40:49 +0900328 With("keep_gendir", "true").Because(reason),
Inseob Kim76e19852024-10-10 17:57:22 +0900329 }
330}
331
Jihoon Kang0d545b82024-10-11 00:21:57 +0000332func createFilesystemIsAutoGeneratedRule() Rule {
333 return NeverAllow().
334 NotIn("build/soong/fsgen").
335 ModuleType("filesystem", "android_system_image").
336 WithMatcher("is_auto_generated", isSetMatcherInstance).
337 Because("is_auto_generated property is only allowed for filesystem modules in build/soong/fsgen directory")
338}
339
Nikita Ioffe50fb49c2025-01-24 13:49:00 +0000340func createNoSha1HashRule() Rule {
341 return NeverAllow().
342 ModuleType("filesystem", "android_filesystem").
343 ModuleType("filesystem", "android_system_image").
344 With("avb_hash_algorithm", "sha1").
345 Because("sha1 is discouraged")
346}
347
Luca Stefani50098f72024-10-12 17:55:31 +0200348func createKotlinPluginRule() []Rule {
349 kotlinPluginProjectsAllowedList := []string{
Luca Stefani50098f72024-10-12 17:55:31 +0200350 "external/kotlinc",
351 }
352
353 return []Rule{
354 NeverAllow().
355 NotIn(kotlinPluginProjectsAllowedList...).
356 ModuleType("kotlin_plugin").
357 Because("kotlin_plugin can only be used in allowed projects"),
358 }
359}
360
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000361// These module types are introduced to convert PRODUCT_COPY_FILES to Soong,
362// and is only intended to be used by filesystem_creator.
363func createPrebuiltEtcBpDefineRule() Rule {
364 return NeverAllow().
365 ModuleType(
366 "prebuilt_usr_srec",
367 "prebuilt_priv_app",
368 "prebuilt_rfs",
369 "prebuilt_framework",
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000370 "prebuilt_wlc_upt",
371 "prebuilt_odm",
Jihoon Kang2e2b7442024-11-05 00:26:20 +0000372 "prebuilt_vendor_dlkm",
373 "prebuilt_bt_firmware",
Jihoon Kangdca2f2b2024-11-06 18:43:19 +0000374 "prebuilt_tvservice",
375 "prebuilt_optee",
Jihoon Kangecf76dd2024-11-12 05:24:46 +0000376 "prebuilt_tvconfig",
Jihoon Kang3ca07a12024-12-02 19:14:30 +0000377 "prebuilt_vendor",
Jihoon Kang320ca7c2024-12-03 18:14:50 +0000378 "prebuilt_sbin",
379 "prebuilt_system",
380 "prebuilt_first_stage_ramdisk",
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000381 ).
382 DefinedInBpFile().
383 Because("module type not allowed to be defined in bp file")
384}
385
Spandan Dase2f98da2024-11-18 19:22:39 +0000386func createAutogenRroBpDefineRule() Rule {
387 return NeverAllow().
388 ModuleType(
389 "autogen_runtime_resource_overlay",
390 ).
391 DefinedInBpFile().
392 Because("Module type will be autogenerated by soong. Use runtime_resource_overlay instead")
393}
394
Steven Moreland65b3fd92017-12-06 14:18:35 -0800395func neverallowMutator(ctx BottomUpMutatorContext) {
396 m, ok := ctx.Module().(Module)
397 if !ok {
398 return
399 }
400
401 dir := ctx.ModuleDir() + "/"
402 properties := m.GetProperties()
403
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100404 osClass := ctx.Module().Target().Os.Class
405
Paul Duffin115445b2019-08-07 15:31:07 +0100406 for _, r := range neverallowRules(ctx.Config()) {
Paul Duffin730f2a52019-06-27 14:08:51 +0100407 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800408 if !n.appliesToPath(dir) {
409 continue
410 }
411
Colin Crossfd4f7432019-03-05 15:06:16 -0800412 if !n.appliesToModuleType(ctx.ModuleType()) {
413 continue
414 }
415
Cole Faust5b35cb92024-08-27 15:47:06 -0700416 if !n.appliesToProperties(ctx, properties) {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800417 continue
418 }
419
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100420 if !n.appliesToOsClass(osClass) {
421 continue
422 }
423
Paul Duffin35781882019-07-25 15:41:09 +0100424 if !n.appliesToDirectDeps(ctx) {
425 continue
426 }
427
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000428 if !n.appliesToBpDefinedModule(ctx) {
429 continue
430 }
431
Steven Moreland65b3fd92017-12-06 14:18:35 -0800432 ctx.ModuleErrorf("violates " + n.String())
433 }
434}
435
Paul Duffin73bf0542019-07-12 14:12:49 +0100436type ValueMatcher interface {
Anton Hanssone1b18362021-12-23 15:05:38 +0000437 Test(string) bool
Paul Duffin73bf0542019-07-12 14:12:49 +0100438 String() string
439}
440
441type equalMatcher struct {
442 expected string
443}
444
Anton Hanssone1b18362021-12-23 15:05:38 +0000445func (m *equalMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100446 return m.expected == value
447}
448
449func (m *equalMatcher) String() string {
450 return "=" + m.expected
451}
452
453type anyMatcher struct {
454}
455
Anton Hanssone1b18362021-12-23 15:05:38 +0000456func (m *anyMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100457 return true
458}
459
460func (m *anyMatcher) String() string {
461 return "=*"
462}
463
464var anyMatcherInstance = &anyMatcher{}
465
Paul Duffinc8111702019-07-22 12:13:55 +0100466type startsWithMatcher struct {
467 prefix string
468}
469
Anton Hanssone1b18362021-12-23 15:05:38 +0000470func (m *startsWithMatcher) Test(value string) bool {
Paul Duffinc8111702019-07-22 12:13:55 +0100471 return strings.HasPrefix(value, m.prefix)
472}
473
474func (m *startsWithMatcher) String() string {
475 return ".starts-with(" + m.prefix + ")"
476}
477
Anton Hansson45376402020-04-09 14:18:21 +0100478type regexMatcher struct {
479 re *regexp.Regexp
480}
481
Anton Hanssone1b18362021-12-23 15:05:38 +0000482func (m *regexMatcher) Test(value string) bool {
Anton Hansson45376402020-04-09 14:18:21 +0100483 return m.re.MatchString(value)
484}
485
486func (m *regexMatcher) String() string {
487 return ".regexp(" + m.re.String() + ")"
488}
489
Andrei Onea115e7e72020-06-05 21:14:03 +0100490type notInListMatcher struct {
491 allowed []string
492}
493
Anton Hanssone1b18362021-12-23 15:05:38 +0000494func (m *notInListMatcher) Test(value string) bool {
Andrei Onea115e7e72020-06-05 21:14:03 +0100495 return !InList(value, m.allowed)
496}
497
498func (m *notInListMatcher) String() string {
499 return ".not-in-list(" + strings.Join(m.allowed, ",") + ")"
500}
501
Orlando Arbildo47afbdd2025-01-08 00:27:37 +0000502type InListMatcher struct {
503 allowed []string
504}
505
506func (m *InListMatcher) Test(value string) bool {
507 return InList(value, m.allowed)
508}
509
510func (m *InListMatcher) String() string {
511 return ".in-list(" + strings.Join(m.allowed, ",") + ")"
512}
513
Colin Crossc511bc52020-04-07 16:50:32 +0000514type isSetMatcher struct{}
515
Anton Hanssone1b18362021-12-23 15:05:38 +0000516func (m *isSetMatcher) Test(value string) bool {
Colin Crossc511bc52020-04-07 16:50:32 +0000517 return value != ""
518}
519
520func (m *isSetMatcher) String() string {
521 return ".is-set"
522}
523
524var isSetMatcherInstance = &isSetMatcher{}
525
Steven Moreland65b3fd92017-12-06 14:18:35 -0800526type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100527 fields []string // e.x.: Vndk.Enabled
528 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800529}
530
Liz Kammera3d79152021-10-28 18:14:04 -0400531func (r *ruleProperty) String() string {
532 return fmt.Sprintf("%q matches: %s", strings.Join(r.fields, "."), r.matcher)
533}
534
535type ruleProperties []ruleProperty
536
537func (r ruleProperties) String() string {
538 var s []string
539 for _, r := range r {
540 s = append(s, r.String())
541 }
542 return strings.Join(s, " ")
543}
544
Paul Duffin730f2a52019-06-27 14:08:51 +0100545// A NeverAllow rule.
546type Rule interface {
547 In(path ...string) Rule
548
549 NotIn(path ...string) Rule
550
Paul Duffin35781882019-07-25 15:41:09 +0100551 InDirectDeps(deps ...string) Rule
552
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100553 WithOsClass(osClasses ...OsClass) Rule
554
Paul Duffin730f2a52019-06-27 14:08:51 +0100555 ModuleType(types ...string) Rule
556
557 NotModuleType(types ...string) Rule
558
559 With(properties, value string) Rule
560
Paul Duffinc8111702019-07-22 12:13:55 +0100561 WithMatcher(properties string, matcher ValueMatcher) Rule
562
Paul Duffin730f2a52019-06-27 14:08:51 +0100563 Without(properties, value string) Rule
564
Paul Duffinc8111702019-07-22 12:13:55 +0100565 WithoutMatcher(properties string, matcher ValueMatcher) Rule
566
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000567 DefinedInBpFile() Rule
568
Paul Duffin730f2a52019-06-27 14:08:51 +0100569 Because(reason string) Rule
570}
571
Steven Moreland65b3fd92017-12-06 14:18:35 -0800572type rule struct {
573 // User string for why this is a thing.
574 reason string
575
576 paths []string
577 unlessPaths []string
578
Paul Duffin35781882019-07-25 15:41:09 +0100579 directDeps map[string]bool
580
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100581 osClasses []OsClass
582
Colin Crossfd4f7432019-03-05 15:06:16 -0800583 moduleTypes []string
584 unlessModuleTypes []string
585
Liz Kammera3d79152021-10-28 18:14:04 -0400586 props ruleProperties
587 unlessProps ruleProperties
Andrei Onea115e7e72020-06-05 21:14:03 +0100588
589 onlyBootclasspathJar bool
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000590
591 definedInBp bool
Steven Moreland65b3fd92017-12-06 14:18:35 -0800592}
593
Paul Duffin730f2a52019-06-27 14:08:51 +0100594// Create a new NeverAllow rule.
595func NeverAllow() Rule {
Paul Duffin35781882019-07-25 15:41:09 +0100596 return &rule{directDeps: make(map[string]bool)}
Steven Moreland65b3fd92017-12-06 14:18:35 -0800597}
Colin Crossfd4f7432019-03-05 15:06:16 -0800598
Liz Kammera3d79152021-10-28 18:14:04 -0400599// In adds path(s) where this rule applies.
Paul Duffin730f2a52019-06-27 14:08:51 +0100600func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800601 r.paths = append(r.paths, cleanPaths(path)...)
602 return r
603}
Colin Crossfd4f7432019-03-05 15:06:16 -0800604
Liz Kammera3d79152021-10-28 18:14:04 -0400605// NotIn adds path(s) to that this rule does not apply to.
Paul Duffin730f2a52019-06-27 14:08:51 +0100606func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800607 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
608 return r
609}
Colin Crossfd4f7432019-03-05 15:06:16 -0800610
Liz Kammera3d79152021-10-28 18:14:04 -0400611// InDirectDeps adds dep(s) that are not allowed with this rule.
Paul Duffin35781882019-07-25 15:41:09 +0100612func (r *rule) InDirectDeps(deps ...string) Rule {
613 for _, d := range deps {
614 r.directDeps[d] = true
615 }
616 return r
617}
618
Liz Kammera3d79152021-10-28 18:14:04 -0400619// WithOsClass adds osClass(es) that this rule applies to.
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100620func (r *rule) WithOsClass(osClasses ...OsClass) Rule {
621 r.osClasses = append(r.osClasses, osClasses...)
622 return r
623}
624
Liz Kammera3d79152021-10-28 18:14:04 -0400625// ModuleType adds type(s) that this rule applies to.
Paul Duffin730f2a52019-06-27 14:08:51 +0100626func (r *rule) ModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800627 r.moduleTypes = append(r.moduleTypes, types...)
628 return r
629}
630
Liz Kammera3d79152021-10-28 18:14:04 -0400631// NotModuleType adds type(s) that this rule does not apply to..
Paul Duffin730f2a52019-06-27 14:08:51 +0100632func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800633 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
634 return r
635}
636
Liz Kammera3d79152021-10-28 18:14:04 -0400637// With specifies property/value combinations that are restricted for this rule.
Paul Duffin730f2a52019-06-27 14:08:51 +0100638func (r *rule) With(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100639 return r.WithMatcher(properties, selectMatcher(value))
640}
641
Liz Kammera3d79152021-10-28 18:14:04 -0400642// WithMatcher specifies property/matcher combinations that are restricted for this rule.
Paul Duffinc8111702019-07-22 12:13:55 +0100643func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800644 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100645 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100646 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800647 })
648 return r
649}
Colin Crossfd4f7432019-03-05 15:06:16 -0800650
Liz Kammera3d79152021-10-28 18:14:04 -0400651// Without specifies property/value combinations that this rule does not apply to.
Paul Duffin730f2a52019-06-27 14:08:51 +0100652func (r *rule) Without(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100653 return r.WithoutMatcher(properties, selectMatcher(value))
654}
655
Liz Kammera3d79152021-10-28 18:14:04 -0400656// Without specifies property/matcher combinations that this rule does not apply to.
Paul Duffinc8111702019-07-22 12:13:55 +0100657func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800658 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100659 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100660 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800661 })
662 return r
663}
Colin Crossfd4f7432019-03-05 15:06:16 -0800664
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000665// DefinedInBpFile specifies that this rule applies to modules that are defined
666// in bp files, and does not apply to modules that are auto generated by other modules.
667func (r *rule) DefinedInBpFile() Rule {
668 r.definedInBp = true
669 return r
670}
671
Paul Duffin73bf0542019-07-12 14:12:49 +0100672func selectMatcher(expected string) ValueMatcher {
673 if expected == "*" {
674 return anyMatcherInstance
675 }
676 return &equalMatcher{expected: expected}
677}
678
Liz Kammera3d79152021-10-28 18:14:04 -0400679// Because specifies a reason for this rule.
Paul Duffin730f2a52019-06-27 14:08:51 +0100680func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800681 r.reason = reason
682 return r
683}
684
685func (r *rule) String() string {
Liz Kammera3d79152021-10-28 18:14:04 -0400686 s := []string{"neverallow requirements. Not allowed:"}
687 if len(r.paths) > 0 {
688 s = append(s, fmt.Sprintf("in dirs: %q", r.paths))
Steven Moreland65b3fd92017-12-06 14:18:35 -0800689 }
Liz Kammera3d79152021-10-28 18:14:04 -0400690 if len(r.moduleTypes) > 0 {
691 s = append(s, fmt.Sprintf("module types: %q", r.moduleTypes))
Steven Moreland65b3fd92017-12-06 14:18:35 -0800692 }
Liz Kammera3d79152021-10-28 18:14:04 -0400693 if len(r.props) > 0 {
694 s = append(s, fmt.Sprintf("properties matching: %s", r.props))
Colin Crossfd4f7432019-03-05 15:06:16 -0800695 }
Liz Kammera3d79152021-10-28 18:14:04 -0400696 if len(r.directDeps) > 0 {
Cole Faust18994c72023-02-28 16:02:16 -0800697 s = append(s, fmt.Sprintf("dep(s): %q", SortedKeys(r.directDeps)))
Colin Crossfd4f7432019-03-05 15:06:16 -0800698 }
Liz Kammera3d79152021-10-28 18:14:04 -0400699 if len(r.osClasses) > 0 {
700 s = append(s, fmt.Sprintf("os class(es): %q", r.osClasses))
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100701 }
Liz Kammera3d79152021-10-28 18:14:04 -0400702 if len(r.unlessPaths) > 0 {
703 s = append(s, fmt.Sprintf("EXCEPT in dirs: %q", r.unlessPaths))
704 }
705 if len(r.unlessModuleTypes) > 0 {
706 s = append(s, fmt.Sprintf("EXCEPT module types: %q", r.unlessModuleTypes))
707 }
708 if len(r.unlessProps) > 0 {
709 s = append(s, fmt.Sprintf("EXCEPT properties matching: %q", r.unlessProps))
Andrei Onea115e7e72020-06-05 21:14:03 +0100710 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800711 if len(r.reason) != 0 {
Liz Kammera3d79152021-10-28 18:14:04 -0400712 s = append(s, " which is restricted because "+r.reason)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800713 }
Liz Kammera3d79152021-10-28 18:14:04 -0400714 if len(s) == 1 {
715 s[0] = "neverallow requirements (empty)"
716 }
717 return strings.Join(s, "\n\t")
Steven Moreland65b3fd92017-12-06 14:18:35 -0800718}
719
720func (r *rule) appliesToPath(dir string) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800721 includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths)
722 excludePath := HasAnyPrefix(dir, r.unlessPaths)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800723 return includePath && !excludePath
724}
725
Paul Duffin35781882019-07-25 15:41:09 +0100726func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool {
727 if len(r.directDeps) == 0 {
728 return true
729 }
730
731 matches := false
732 ctx.VisitDirectDeps(func(m Module) {
733 if !matches {
734 name := ctx.OtherModuleName(m)
735 matches = r.directDeps[name]
736 }
737 })
738
739 return matches
740}
741
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100742func (r *rule) appliesToOsClass(osClass OsClass) bool {
743 if len(r.osClasses) == 0 {
744 return true
745 }
746
747 for _, c := range r.osClasses {
748 if c == osClass {
749 return true
750 }
751 }
752
753 return false
754}
755
Colin Crossfd4f7432019-03-05 15:06:16 -0800756func (r *rule) appliesToModuleType(moduleType string) bool {
Jihoon Kangd5465072024-12-03 00:53:05 +0000757 // Remove prefix for auto-generated modules
758 moduleType = strings.TrimSuffix(moduleType, "__loadHookModule")
759 moduleType = strings.TrimSuffix(moduleType, "__bottomUpMutatorModule")
Colin Crossfd4f7432019-03-05 15:06:16 -0800760 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
761}
762
Cole Faust5b35cb92024-08-27 15:47:06 -0700763func (r *rule) appliesToProperties(ctx BottomUpMutatorContext, properties []interface{}) bool {
764 includeProps := hasAllProperties(ctx, properties, r.props)
765 excludeProps := hasAnyProperty(ctx, properties, r.unlessProps)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800766 return includeProps && !excludeProps
767}
768
Jihoon Kang2a7bf752024-11-01 21:21:25 +0000769func (r *rule) appliesToBpDefinedModule(ctx BottomUpMutatorContext) bool {
770 if !r.definedInBp {
771 return true
772 }
773 return !ctx.OtherModuleIsAutoGenerated(ctx.Module()) == r.definedInBp
774}
775
Paul Duffinc8111702019-07-22 12:13:55 +0100776func StartsWith(prefix string) ValueMatcher {
777 return &startsWithMatcher{prefix}
778}
779
Anton Hansson45376402020-04-09 14:18:21 +0100780func Regexp(re string) ValueMatcher {
781 r, err := regexp.Compile(re)
782 if err != nil {
783 panic(err)
784 }
785 return &regexMatcher{r}
786}
787
Andrei Onea115e7e72020-06-05 21:14:03 +0100788func NotInList(allowed []string) ValueMatcher {
789 return &notInListMatcher{allowed}
790}
791
Orlando Arbildo47afbdd2025-01-08 00:27:37 +0000792func InAllowedList(allowed []string) ValueMatcher {
793 return &InListMatcher{allowed}
794}
795
Steven Moreland65b3fd92017-12-06 14:18:35 -0800796// assorted utils
797
798func cleanPaths(paths []string) []string {
799 res := make([]string, len(paths))
800 for i, v := range paths {
801 res[i] = filepath.Clean(v) + "/"
802 }
803 return res
804}
805
806func fieldNamesForProperties(propertyNames string) []string {
807 names := strings.Split(propertyNames, ".")
808 for i, v := range names {
809 names[i] = proptools.FieldNameForProperty(v)
810 }
811 return names
812}
813
Cole Faust5b35cb92024-08-27 15:47:06 -0700814func hasAnyProperty(ctx BottomUpMutatorContext, properties []interface{}, props []ruleProperty) bool {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800815 for _, v := range props {
Cole Faust5b35cb92024-08-27 15:47:06 -0700816 if hasProperty(ctx, properties, v) {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800817 return true
818 }
819 }
820 return false
821}
822
Cole Faust5b35cb92024-08-27 15:47:06 -0700823func hasAllProperties(ctx BottomUpMutatorContext, properties []interface{}, props []ruleProperty) bool {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800824 for _, v := range props {
Cole Faust5b35cb92024-08-27 15:47:06 -0700825 if !hasProperty(ctx, properties, v) {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800826 return false
827 }
828 }
829 return true
830}
831
Cole Faust5b35cb92024-08-27 15:47:06 -0700832func hasProperty(ctx BottomUpMutatorContext, properties []interface{}, prop ruleProperty) bool {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800833 for _, propertyStruct := range properties {
834 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
835 for _, v := range prop.fields {
836 if !propertiesValue.IsValid() {
837 break
838 }
839 propertiesValue = propertiesValue.FieldByName(v)
840 }
841 if !propertiesValue.IsValid() {
842 continue
843 }
844
Paul Duffin73bf0542019-07-12 14:12:49 +0100845 check := func(value string) bool {
Anton Hanssone1b18362021-12-23 15:05:38 +0000846 return prop.matcher.Test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800847 }
848
Cole Faust5b35cb92024-08-27 15:47:06 -0700849 if matchValue(ctx, propertiesValue, check) {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800850 return true
851 }
852 }
853 return false
854}
855
Cole Faust5b35cb92024-08-27 15:47:06 -0700856func matchValue(ctx BottomUpMutatorContext, value reflect.Value, check func(string) bool) bool {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800857 if !value.IsValid() {
858 return false
859 }
860
861 if value.Kind() == reflect.Ptr {
862 if value.IsNil() {
863 return check("")
864 }
865 value = value.Elem()
866 }
867
Cole Faust5b35cb92024-08-27 15:47:06 -0700868 switch v := value.Interface().(type) {
869 case string:
870 return check(v)
871 case bool:
872 return check(strconv.FormatBool(v))
873 case int:
874 return check(strconv.FormatInt((int64)(v), 10))
875 case []string:
876 for _, v := range v {
877 if check(v) {
878 return true
879 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800880 }
Cole Faust5b35cb92024-08-27 15:47:06 -0700881 return false
882 case proptools.Configurable[string]:
883 return check(v.GetOrDefault(ctx, ""))
884 case proptools.Configurable[bool]:
885 return check(strconv.FormatBool(v.GetOrDefault(ctx, false)))
886 case proptools.Configurable[[]string]:
887 for _, v := range v.GetOrDefault(ctx, nil) {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800888 if check(v) {
889 return true
890 }
891 }
892 return false
893 }
894
895 panic("Can't handle type: " + value.Kind().String())
896}
Paul Duffin115445b2019-08-07 15:31:07 +0100897
898var neverallowRulesKey = NewOnceKey("neverallowRules")
899
900func neverallowRules(config Config) []Rule {
901 return config.Once(neverallowRulesKey, func() interface{} {
902 // No test rules were set by setTestNeverallowRules, use the global rules
903 return neverallows
904 }).([]Rule)
905}
906
907// Overrides the default neverallow rules for the supplied config.
908//
909// For testing only.
Paul Duffin45338f02021-03-30 23:07:52 +0100910func setTestNeverallowRules(config Config, testRules []Rule) {
Paul Duffin115445b2019-08-07 15:31:07 +0100911 config.Once(neverallowRulesKey, func() interface{} { return testRules })
912}
Paul Duffin45338f02021-03-30 23:07:52 +0100913
914// Prepares for a test by setting neverallow rules and enabling the mutator.
915//
916// If the supplied rules are nil then the default rules are used.
917func PrepareForTestWithNeverallowRules(testRules []Rule) FixturePreparer {
918 return GroupFixturePreparers(
919 FixtureModifyConfig(func(config Config) {
920 if testRules != nil {
921 setTestNeverallowRules(config, testRules)
922 }
923 }),
924 FixtureRegisterWithContext(func(ctx RegistrationContext) {
925 ctx.PostDepsMutators(registerNeverallowMutator)
926 }),
927 )
928}