Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 18 | "fmt" |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 19 | "path/filepath" |
| 20 | "reflect" |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 21 | "regexp" |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 22 | "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 Onorato | b4638c1 | 2021-10-27 15:47:06 -0700 | [diff] [blame] | 31 | // against assumptions, in progress code refactors, or policy to be expressed in a |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 32 | // 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 Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 36 | // - 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 Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 39 | // - - 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 Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 44 | // - it has none of the "Without" properties matched (same rules as above) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 45 | |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 46 | func registerNeverallowMutator(ctx RegisterMutatorsContext) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 47 | ctx.BottomUp("neverallow", neverallowMutator).Parallel() |
| 48 | } |
| 49 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 50 | var neverallows = []Rule{} |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 51 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 52 | func init() { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 53 | AddNeverAllowRules(createIncludeDirsRules()...) |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 54 | AddNeverAllowRules(createTrebleRules()...) |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 55 | AddNeverAllowRules(createJavaDeviceForHostRules()...) |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 56 | AddNeverAllowRules(createCcSdkVariantRules()...) |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 57 | AddNeverAllowRules(createUncompressDexRules()...) |
Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 58 | AddNeverAllowRules(createInitFirstStageRules()...) |
Jiyong Park | 3c306f3 | 2022-04-05 15:29:53 +0900 | [diff] [blame] | 59 | AddNeverAllowRules(createProhibitFrameworkAccessRules()...) |
Alan Stokes | 73feba3 | 2022-11-14 12:21:24 +0000 | [diff] [blame] | 60 | AddNeverAllowRules(createCcStubsRule()) |
Jihoon Kang | 381c2fa | 2023-06-01 22:17:32 +0000 | [diff] [blame] | 61 | AddNeverAllowRules(createJavaExcludeStaticLibsRule()) |
Mark White | a15790a | 2023-08-22 21:28:11 +0000 | [diff] [blame] | 62 | AddNeverAllowRules(createProhibitHeaderOnlyRule()) |
Steven Moreland | 0db999c | 2024-09-03 22:06:24 +0000 | [diff] [blame] | 63 | AddNeverAllowRules(createLimitNdkExportRule()...) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 64 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 65 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 66 | // Add a NeverAllow rule to the set of rules to apply. |
| 67 | func AddNeverAllowRules(rules ...Rule) { |
| 68 | neverallows = append(neverallows, rules...) |
| 69 | } |
| 70 | |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 71 | var ( |
| 72 | neverallowNotInIncludeDir = []string{ |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 73 | "art", |
Orion Hodson | 6341f01 | 2019-11-06 13:39:46 +0000 | [diff] [blame] | 74 | "art/libnativebridge", |
| 75 | "art/libnativeloader", |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 76 | "libcore", |
| 77 | "libnativehelper", |
| 78 | "external/apache-harmony", |
| 79 | "external/apache-xml", |
| 80 | "external/boringssl", |
| 81 | "external/bouncycastle", |
| 82 | "external/conscrypt", |
| 83 | "external/icu", |
| 84 | "external/okhttp", |
| 85 | "external/vixl", |
| 86 | "external/wycheproof", |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 87 | } |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 88 | neverallowNoUseIncludeDir = []string{ |
Steven Moreland | f36a3ac | 2021-04-27 18:03:14 +0000 | [diff] [blame] | 89 | "frameworks/av/apex", |
| 90 | "frameworks/av/tools", |
| 91 | "frameworks/native/cmds", |
| 92 | "system/apex", |
| 93 | "system/bpf", |
| 94 | "system/gatekeeper", |
| 95 | "system/hwservicemanager", |
| 96 | "system/libbase", |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 97 | "system/libfmq", |
Steven Moreland | f36a3ac | 2021-04-27 18:03:14 +0000 | [diff] [blame] | 98 | "system/libvintf", |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 99 | } |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 100 | ) |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 101 | |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 102 | func createIncludeDirsRules() []Rule { |
| 103 | rules := make([]Rule, 0, len(neverallowNotInIncludeDir)+len(neverallowNoUseIncludeDir)) |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 104 | |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 105 | for _, path := range neverallowNotInIncludeDir { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 106 | rule := |
| 107 | NeverAllow(). |
| 108 | WithMatcher("include_dirs", StartsWith(path+"/")). |
| 109 | Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" + |
| 110 | " to use alternate mechanisms and so can no longer be used.") |
| 111 | |
| 112 | rules = append(rules, rule) |
| 113 | } |
| 114 | |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 115 | for _, path := range neverallowNoUseIncludeDir { |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 116 | rule := NeverAllow().In(path+"/").WithMatcher("include_dirs", isSetMatcherInstance). |
| 117 | Because("include_dirs is deprecated, all usages of them in '" + path + "' have been migrated" + |
| 118 | " to use alternate mechanisms and so can no longer be used.") |
| 119 | rules = append(rules, rule) |
| 120 | } |
| 121 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 122 | return rules |
| 123 | } |
| 124 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 125 | func createTrebleRules() []Rule { |
| 126 | return []Rule{ |
| 127 | NeverAllow(). |
| 128 | In("vendor", "device"). |
| 129 | With("vndk.enabled", "true"). |
| 130 | Without("vendor", "true"). |
Justin Yun | 0ecf0b2 | 2020-02-28 15:07:59 +0900 | [diff] [blame] | 131 | Without("product_specific", "true"). |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 132 | Because("the VNDK can never contain a library that is device dependent."), |
| 133 | NeverAllow(). |
| 134 | With("vndk.enabled", "true"). |
| 135 | Without("vendor", "true"). |
| 136 | Without("owner", ""). |
| 137 | Because("a VNDK module can never have an owner."), |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 138 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 139 | // TODO(b/67974785): always enforce the manifest |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 140 | NeverAllow(). |
Steven Moreland | 51ce4f6 | 2020-02-10 17:21:32 -0800 | [diff] [blame] | 141 | Without("name", "libhidlbase-combined-impl"). |
| 142 | Without("name", "libhidlbase"). |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 143 | With("product_variables.enforce_vintf_manifest.cflags", "*"). |
| 144 | Because("manifest enforcement should be independent of ."), |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 145 | |
| 146 | // TODO(b/67975799): vendor code should always use /vendor/bin/sh |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 147 | NeverAllow(). |
| 148 | Without("name", "libc_bionic_ndk"). |
| 149 | With("product_variables.treble_linker_namespaces.cflags", "*"). |
| 150 | Because("nothing should care if linker namespaces are enabled or not"), |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 151 | |
| 152 | // Example: |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 153 | // *NeverAllow().with("Srcs", "main.cpp")) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 157 | func createJavaDeviceForHostRules() []Rule { |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 158 | javaDeviceForHostProjectsAllowedList := []string{ |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 159 | "development/build", |
Colin Cross | b5191a5 | 2019-04-11 14:07:38 -0700 | [diff] [blame] | 160 | "external/guava", |
Steve Elliott | 8053f82 | 2022-10-18 17:09:28 -0400 | [diff] [blame] | 161 | "external/kotlinx.coroutines", |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 162 | "external/robolectric-shadows", |
Rex Hoffman | 54641d2 | 2022-08-25 17:29:50 +0000 | [diff] [blame] | 163 | "external/robolectric", |
Makoto Onuki | b66bba3 | 2023-11-11 00:06:09 +0000 | [diff] [blame] | 164 | "frameworks/base/ravenwood", |
| 165 | "frameworks/base/tools/hoststubgen", |
Jerome Gaillard | 655ee02 | 2021-09-23 11:38:08 +0000 | [diff] [blame] | 166 | "frameworks/layoutlib", |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 169 | return []Rule{ |
| 170 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 171 | NotIn(javaDeviceForHostProjectsAllowedList...). |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 172 | ModuleType("java_device_for_host", "java_host_for_device"). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 173 | Because("java_device_for_host can only be used in allowed projects"), |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 177 | func createCcSdkVariantRules() []Rule { |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 178 | sdkVersionOnlyAllowedList := []string{ |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 179 | // derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk. |
| 180 | // This sometimes works because the APEX modules that contain derive_sdk and |
| 181 | // derive_sdk_prefer32 suppress the platform installation rules, but fails when |
| 182 | // the APEX modules contain the SDK variant and the platform variant still exists. |
Anton Hansson | 4b8e64b | 2020-05-27 18:25:23 +0100 | [diff] [blame] | 183 | "packages/modules/SdkExtensions/derive_sdk", |
Dan Albert | e2054a9 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 184 | // These are for apps and shouldn't be used by non-SDK variant modules. |
| 185 | "prebuilts/ndk", |
Jiyong Park | a574d53 | 2024-08-28 18:06:43 +0900 | [diff] [blame] | 186 | "frameworks/native/libs/binder/ndk", |
Dan Albert | e2054a9 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 187 | "tools/test/graphicsbenchmark/apps/sample_app", |
| 188 | "tools/test/graphicsbenchmark/functional_tests/java", |
Dan Albert | 5557605 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 189 | "vendor/xts/gts-tests/hostsidetests/gamedevicecert/apps/javatests", |
Chang Li | 66d3cb7 | 2021-06-18 14:04:50 +0000 | [diff] [blame] | 190 | "external/libtextclassifier/native", |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 193 | platformVariantPropertiesAllowedList := []string{ |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 194 | // android_native_app_glue and libRSSupport use native_window.h but target old |
| 195 | // sdk versions (minimum and 9 respectively) where libnativewindow didn't exist, |
| 196 | // so they can't add libnativewindow to shared_libs to get the header directory |
| 197 | // for the platform variant. Allow them to use the platform variant |
| 198 | // property to set shared_libs. |
| 199 | "prebuilts/ndk", |
| 200 | "frameworks/rs", |
| 201 | } |
| 202 | |
| 203 | return []Rule{ |
| 204 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 205 | NotIn(sdkVersionOnlyAllowedList...). |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 206 | WithMatcher("sdk_variant_only", isSetMatcherInstance). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 207 | Because("sdk_variant_only can only be used in allowed projects"), |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 208 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 209 | NotIn(platformVariantPropertiesAllowedList...). |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 210 | WithMatcher("platform.shared_libs", isSetMatcherInstance). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 211 | Because("platform variant properties can only be used in allowed projects"), |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Alan Stokes | 73feba3 | 2022-11-14 12:21:24 +0000 | [diff] [blame] | 215 | func createCcStubsRule() Rule { |
| 216 | ccStubsImplementationInstallableProjectsAllowedList := []string{ |
Jiyong Park | f6736c7 | 2024-07-22 11:22:35 +0900 | [diff] [blame] | 217 | "packages/modules/Virtualization/libs/libvm_payload", |
Alan Stokes | 73feba3 | 2022-11-14 12:21:24 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | return NeverAllow(). |
| 221 | NotIn(ccStubsImplementationInstallableProjectsAllowedList...). |
| 222 | WithMatcher("stubs.implementation_installable", isSetMatcherInstance). |
| 223 | Because("implementation_installable can only be used in allowed projects.") |
| 224 | } |
| 225 | |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 226 | func createUncompressDexRules() []Rule { |
| 227 | return []Rule{ |
| 228 | NeverAllow(). |
| 229 | NotIn("art"). |
| 230 | WithMatcher("uncompress_dex", isSetMatcherInstance). |
| 231 | Because("uncompress_dex is only allowed for certain jars for test in art."), |
| 232 | } |
| 233 | } |
| 234 | |
Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 235 | func createInitFirstStageRules() []Rule { |
| 236 | return []Rule{ |
| 237 | NeverAllow(). |
Nikita Ioffe | 11a9c2c | 2023-06-21 16:51:09 +0100 | [diff] [blame] | 238 | Without("name", "init_first_stage_defaults"). |
Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 239 | Without("name", "init_first_stage"). |
Nikita Ioffe | 11a9c2c | 2023-06-21 16:51:09 +0100 | [diff] [blame] | 240 | Without("name", "init_first_stage.microdroid"). |
Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 241 | With("install_in_root", "true"). |
Inseob Kim | be6a66d | 2024-07-11 15:43:44 +0900 | [diff] [blame] | 242 | NotModuleType("prebuilt_root"). |
Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 243 | Because("install_in_root is only for init_first_stage."), |
| 244 | } |
| 245 | } |
| 246 | |
Jiyong Park | 3c306f3 | 2022-04-05 15:29:53 +0900 | [diff] [blame] | 247 | func createProhibitFrameworkAccessRules() []Rule { |
| 248 | return []Rule{ |
| 249 | NeverAllow(). |
| 250 | With("libs", "framework"). |
| 251 | WithoutMatcher("sdk_version", Regexp("(core_.*|^$)")). |
| 252 | Because("framework can't be used when building against SDK"), |
| 253 | } |
| 254 | } |
| 255 | |
Jihoon Kang | 381c2fa | 2023-06-01 22:17:32 +0000 | [diff] [blame] | 256 | func createJavaExcludeStaticLibsRule() Rule { |
| 257 | return NeverAllow(). |
Jihoon Kang | 3d4d88d | 2023-06-14 23:14:42 +0000 | [diff] [blame] | 258 | NotIn("build/soong", "libcore", "frameworks/base/api"). |
Jihoon Kang | 381c2fa | 2023-06-01 22:17:32 +0000 | [diff] [blame] | 259 | ModuleType("java_library"). |
| 260 | WithMatcher("exclude_static_libs", isSetMatcherInstance). |
Jihoon Kang | 3d4d88d | 2023-06-14 23:14:42 +0000 | [diff] [blame] | 261 | Because("exclude_static_libs property is only allowed for java modules defined in build/soong, libcore, and frameworks/base/api") |
Jihoon Kang | 381c2fa | 2023-06-01 22:17:32 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Mark White | a15790a | 2023-08-22 21:28:11 +0000 | [diff] [blame] | 264 | func createProhibitHeaderOnlyRule() Rule { |
| 265 | return NeverAllow(). |
| 266 | Without("name", "framework-minus-apex-headers"). |
| 267 | With("headers_only", "true"). |
| 268 | Because("headers_only can only be used for generating framework-minus-apex headers for non-updatable modules") |
| 269 | } |
| 270 | |
Steven Moreland | 0db999c | 2024-09-03 22:06:24 +0000 | [diff] [blame] | 271 | func createLimitNdkExportRule() []Rule { |
| 272 | 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." |
| 273 | // DO NOT ADD HERE - please consult danalbert@ |
| 274 | // b/357711733 |
| 275 | return []Rule{ |
| 276 | NeverAllow(). |
| 277 | NotIn("frameworks/native/libs/binder/ndk"). |
| 278 | ModuleType("ndk_library"). |
| 279 | WithMatcher("export_header_libs", isSetMatcherInstance).Because(reason), |
| 280 | NeverAllow().ModuleType("ndk_library").WithMatcher("export_generated_headers", isSetMatcherInstance).Because(reason), |
| 281 | NeverAllow().ModuleType("ndk_library").WithMatcher("export_include_dirs", isSetMatcherInstance).Because(reason), |
| 282 | NeverAllow().ModuleType("ndk_library").WithMatcher("export_shared_lib_headers", isSetMatcherInstance).Because(reason), |
| 283 | NeverAllow().ModuleType("ndk_library").WithMatcher("export_static_lib_headers", isSetMatcherInstance).Because(reason), |
| 284 | } |
| 285 | } |
| 286 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 287 | func neverallowMutator(ctx BottomUpMutatorContext) { |
| 288 | m, ok := ctx.Module().(Module) |
| 289 | if !ok { |
| 290 | return |
| 291 | } |
| 292 | |
| 293 | dir := ctx.ModuleDir() + "/" |
| 294 | properties := m.GetProperties() |
| 295 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 296 | osClass := ctx.Module().Target().Os.Class |
| 297 | |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 298 | for _, r := range neverallowRules(ctx.Config()) { |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 299 | n := r.(*rule) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 300 | if !n.appliesToPath(dir) { |
| 301 | continue |
| 302 | } |
| 303 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 304 | if !n.appliesToModuleType(ctx.ModuleType()) { |
| 305 | continue |
| 306 | } |
| 307 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 308 | if !n.appliesToProperties(ctx, properties) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 309 | continue |
| 310 | } |
| 311 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 312 | if !n.appliesToOsClass(osClass) { |
| 313 | continue |
| 314 | } |
| 315 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 316 | if !n.appliesToDirectDeps(ctx) { |
| 317 | continue |
| 318 | } |
| 319 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 320 | ctx.ModuleErrorf("violates " + n.String()) |
| 321 | } |
| 322 | } |
| 323 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 324 | type ValueMatcher interface { |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 325 | Test(string) bool |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 326 | String() string |
| 327 | } |
| 328 | |
| 329 | type equalMatcher struct { |
| 330 | expected string |
| 331 | } |
| 332 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 333 | func (m *equalMatcher) Test(value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 334 | return m.expected == value |
| 335 | } |
| 336 | |
| 337 | func (m *equalMatcher) String() string { |
| 338 | return "=" + m.expected |
| 339 | } |
| 340 | |
| 341 | type anyMatcher struct { |
| 342 | } |
| 343 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 344 | func (m *anyMatcher) Test(value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 345 | return true |
| 346 | } |
| 347 | |
| 348 | func (m *anyMatcher) String() string { |
| 349 | return "=*" |
| 350 | } |
| 351 | |
| 352 | var anyMatcherInstance = &anyMatcher{} |
| 353 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 354 | type startsWithMatcher struct { |
| 355 | prefix string |
| 356 | } |
| 357 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 358 | func (m *startsWithMatcher) Test(value string) bool { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 359 | return strings.HasPrefix(value, m.prefix) |
| 360 | } |
| 361 | |
| 362 | func (m *startsWithMatcher) String() string { |
| 363 | return ".starts-with(" + m.prefix + ")" |
| 364 | } |
| 365 | |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 366 | type regexMatcher struct { |
| 367 | re *regexp.Regexp |
| 368 | } |
| 369 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 370 | func (m *regexMatcher) Test(value string) bool { |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 371 | return m.re.MatchString(value) |
| 372 | } |
| 373 | |
| 374 | func (m *regexMatcher) String() string { |
| 375 | return ".regexp(" + m.re.String() + ")" |
| 376 | } |
| 377 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 378 | type notInListMatcher struct { |
| 379 | allowed []string |
| 380 | } |
| 381 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 382 | func (m *notInListMatcher) Test(value string) bool { |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 383 | return !InList(value, m.allowed) |
| 384 | } |
| 385 | |
| 386 | func (m *notInListMatcher) String() string { |
| 387 | return ".not-in-list(" + strings.Join(m.allowed, ",") + ")" |
| 388 | } |
| 389 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 390 | type isSetMatcher struct{} |
| 391 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 392 | func (m *isSetMatcher) Test(value string) bool { |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 393 | return value != "" |
| 394 | } |
| 395 | |
| 396 | func (m *isSetMatcher) String() string { |
| 397 | return ".is-set" |
| 398 | } |
| 399 | |
| 400 | var isSetMatcherInstance = &isSetMatcher{} |
| 401 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 402 | type ruleProperty struct { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 403 | fields []string // e.x.: Vndk.Enabled |
| 404 | matcher ValueMatcher |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 407 | func (r *ruleProperty) String() string { |
| 408 | return fmt.Sprintf("%q matches: %s", strings.Join(r.fields, "."), r.matcher) |
| 409 | } |
| 410 | |
| 411 | type ruleProperties []ruleProperty |
| 412 | |
| 413 | func (r ruleProperties) String() string { |
| 414 | var s []string |
| 415 | for _, r := range r { |
| 416 | s = append(s, r.String()) |
| 417 | } |
| 418 | return strings.Join(s, " ") |
| 419 | } |
| 420 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 421 | // A NeverAllow rule. |
| 422 | type Rule interface { |
| 423 | In(path ...string) Rule |
| 424 | |
| 425 | NotIn(path ...string) Rule |
| 426 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 427 | InDirectDeps(deps ...string) Rule |
| 428 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 429 | WithOsClass(osClasses ...OsClass) Rule |
| 430 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 431 | ModuleType(types ...string) Rule |
| 432 | |
| 433 | NotModuleType(types ...string) Rule |
| 434 | |
| 435 | With(properties, value string) Rule |
| 436 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 437 | WithMatcher(properties string, matcher ValueMatcher) Rule |
| 438 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 439 | Without(properties, value string) Rule |
| 440 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 441 | WithoutMatcher(properties string, matcher ValueMatcher) Rule |
| 442 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 443 | Because(reason string) Rule |
| 444 | } |
| 445 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 446 | type rule struct { |
| 447 | // User string for why this is a thing. |
| 448 | reason string |
| 449 | |
| 450 | paths []string |
| 451 | unlessPaths []string |
| 452 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 453 | directDeps map[string]bool |
| 454 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 455 | osClasses []OsClass |
| 456 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 457 | moduleTypes []string |
| 458 | unlessModuleTypes []string |
| 459 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 460 | props ruleProperties |
| 461 | unlessProps ruleProperties |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 462 | |
| 463 | onlyBootclasspathJar bool |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 466 | // Create a new NeverAllow rule. |
| 467 | func NeverAllow() Rule { |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 468 | return &rule{directDeps: make(map[string]bool)} |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 469 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 470 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 471 | // In adds path(s) where this rule applies. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 472 | func (r *rule) In(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 473 | r.paths = append(r.paths, cleanPaths(path)...) |
| 474 | return r |
| 475 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 476 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 477 | // NotIn adds path(s) to that this rule does not apply to. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 478 | func (r *rule) NotIn(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 479 | r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...) |
| 480 | return r |
| 481 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 482 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 483 | // InDirectDeps adds dep(s) that are not allowed with this rule. |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 484 | func (r *rule) InDirectDeps(deps ...string) Rule { |
| 485 | for _, d := range deps { |
| 486 | r.directDeps[d] = true |
| 487 | } |
| 488 | return r |
| 489 | } |
| 490 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 491 | // WithOsClass adds osClass(es) that this rule applies to. |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 492 | func (r *rule) WithOsClass(osClasses ...OsClass) Rule { |
| 493 | r.osClasses = append(r.osClasses, osClasses...) |
| 494 | return r |
| 495 | } |
| 496 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 497 | // ModuleType adds type(s) that this rule applies to. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 498 | func (r *rule) ModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 499 | r.moduleTypes = append(r.moduleTypes, types...) |
| 500 | return r |
| 501 | } |
| 502 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 503 | // NotModuleType adds type(s) that this rule does not apply to.. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 504 | func (r *rule) NotModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 505 | r.unlessModuleTypes = append(r.unlessModuleTypes, types...) |
| 506 | return r |
| 507 | } |
| 508 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 509 | // With specifies property/value combinations that are restricted for this rule. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 510 | func (r *rule) With(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 511 | return r.WithMatcher(properties, selectMatcher(value)) |
| 512 | } |
| 513 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 514 | // WithMatcher specifies property/matcher combinations that are restricted for this rule. |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 515 | func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 516 | r.props = append(r.props, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 517 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 518 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 519 | }) |
| 520 | return r |
| 521 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 522 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 523 | // Without specifies property/value combinations that this rule does not apply to. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 524 | func (r *rule) Without(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 525 | return r.WithoutMatcher(properties, selectMatcher(value)) |
| 526 | } |
| 527 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 528 | // Without specifies property/matcher combinations that this rule does not apply to. |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 529 | func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 530 | r.unlessProps = append(r.unlessProps, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 531 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 532 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 533 | }) |
| 534 | return r |
| 535 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 536 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 537 | func selectMatcher(expected string) ValueMatcher { |
| 538 | if expected == "*" { |
| 539 | return anyMatcherInstance |
| 540 | } |
| 541 | return &equalMatcher{expected: expected} |
| 542 | } |
| 543 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 544 | // Because specifies a reason for this rule. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 545 | func (r *rule) Because(reason string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 546 | r.reason = reason |
| 547 | return r |
| 548 | } |
| 549 | |
| 550 | func (r *rule) String() string { |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 551 | s := []string{"neverallow requirements. Not allowed:"} |
| 552 | if len(r.paths) > 0 { |
| 553 | s = append(s, fmt.Sprintf("in dirs: %q", r.paths)) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 554 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 555 | if len(r.moduleTypes) > 0 { |
| 556 | s = append(s, fmt.Sprintf("module types: %q", r.moduleTypes)) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 557 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 558 | if len(r.props) > 0 { |
| 559 | s = append(s, fmt.Sprintf("properties matching: %s", r.props)) |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 560 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 561 | if len(r.directDeps) > 0 { |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 562 | s = append(s, fmt.Sprintf("dep(s): %q", SortedKeys(r.directDeps))) |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 563 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 564 | if len(r.osClasses) > 0 { |
| 565 | s = append(s, fmt.Sprintf("os class(es): %q", r.osClasses)) |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 566 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 567 | if len(r.unlessPaths) > 0 { |
| 568 | s = append(s, fmt.Sprintf("EXCEPT in dirs: %q", r.unlessPaths)) |
| 569 | } |
| 570 | if len(r.unlessModuleTypes) > 0 { |
| 571 | s = append(s, fmt.Sprintf("EXCEPT module types: %q", r.unlessModuleTypes)) |
| 572 | } |
| 573 | if len(r.unlessProps) > 0 { |
| 574 | s = append(s, fmt.Sprintf("EXCEPT properties matching: %q", r.unlessProps)) |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 575 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 576 | if len(r.reason) != 0 { |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 577 | s = append(s, " which is restricted because "+r.reason) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 578 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 579 | if len(s) == 1 { |
| 580 | s[0] = "neverallow requirements (empty)" |
| 581 | } |
| 582 | return strings.Join(s, "\n\t") |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | func (r *rule) appliesToPath(dir string) bool { |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 586 | includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths) |
| 587 | excludePath := HasAnyPrefix(dir, r.unlessPaths) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 588 | return includePath && !excludePath |
| 589 | } |
| 590 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 591 | func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool { |
| 592 | if len(r.directDeps) == 0 { |
| 593 | return true |
| 594 | } |
| 595 | |
| 596 | matches := false |
| 597 | ctx.VisitDirectDeps(func(m Module) { |
| 598 | if !matches { |
| 599 | name := ctx.OtherModuleName(m) |
| 600 | matches = r.directDeps[name] |
| 601 | } |
| 602 | }) |
| 603 | |
| 604 | return matches |
| 605 | } |
| 606 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 607 | func (r *rule) appliesToOsClass(osClass OsClass) bool { |
| 608 | if len(r.osClasses) == 0 { |
| 609 | return true |
| 610 | } |
| 611 | |
| 612 | for _, c := range r.osClasses { |
| 613 | if c == osClass { |
| 614 | return true |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | return false |
| 619 | } |
| 620 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 621 | func (r *rule) appliesToModuleType(moduleType string) bool { |
| 622 | return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes) |
| 623 | } |
| 624 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 625 | func (r *rule) appliesToProperties(ctx BottomUpMutatorContext, properties []interface{}) bool { |
| 626 | includeProps := hasAllProperties(ctx, properties, r.props) |
| 627 | excludeProps := hasAnyProperty(ctx, properties, r.unlessProps) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 628 | return includeProps && !excludeProps |
| 629 | } |
| 630 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 631 | func StartsWith(prefix string) ValueMatcher { |
| 632 | return &startsWithMatcher{prefix} |
| 633 | } |
| 634 | |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 635 | func Regexp(re string) ValueMatcher { |
| 636 | r, err := regexp.Compile(re) |
| 637 | if err != nil { |
| 638 | panic(err) |
| 639 | } |
| 640 | return ®exMatcher{r} |
| 641 | } |
| 642 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 643 | func NotInList(allowed []string) ValueMatcher { |
| 644 | return ¬InListMatcher{allowed} |
| 645 | } |
| 646 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 647 | // assorted utils |
| 648 | |
| 649 | func cleanPaths(paths []string) []string { |
| 650 | res := make([]string, len(paths)) |
| 651 | for i, v := range paths { |
| 652 | res[i] = filepath.Clean(v) + "/" |
| 653 | } |
| 654 | return res |
| 655 | } |
| 656 | |
| 657 | func fieldNamesForProperties(propertyNames string) []string { |
| 658 | names := strings.Split(propertyNames, ".") |
| 659 | for i, v := range names { |
| 660 | names[i] = proptools.FieldNameForProperty(v) |
| 661 | } |
| 662 | return names |
| 663 | } |
| 664 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 665 | func hasAnyProperty(ctx BottomUpMutatorContext, properties []interface{}, props []ruleProperty) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 666 | for _, v := range props { |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 667 | if hasProperty(ctx, properties, v) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 668 | return true |
| 669 | } |
| 670 | } |
| 671 | return false |
| 672 | } |
| 673 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 674 | func hasAllProperties(ctx BottomUpMutatorContext, properties []interface{}, props []ruleProperty) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 675 | for _, v := range props { |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 676 | if !hasProperty(ctx, properties, v) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 677 | return false |
| 678 | } |
| 679 | } |
| 680 | return true |
| 681 | } |
| 682 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 683 | func hasProperty(ctx BottomUpMutatorContext, properties []interface{}, prop ruleProperty) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 684 | for _, propertyStruct := range properties { |
| 685 | propertiesValue := reflect.ValueOf(propertyStruct).Elem() |
| 686 | for _, v := range prop.fields { |
| 687 | if !propertiesValue.IsValid() { |
| 688 | break |
| 689 | } |
| 690 | propertiesValue = propertiesValue.FieldByName(v) |
| 691 | } |
| 692 | if !propertiesValue.IsValid() { |
| 693 | continue |
| 694 | } |
| 695 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 696 | check := func(value string) bool { |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 697 | return prop.matcher.Test(value) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 698 | } |
| 699 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 700 | if matchValue(ctx, propertiesValue, check) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 701 | return true |
| 702 | } |
| 703 | } |
| 704 | return false |
| 705 | } |
| 706 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 707 | func matchValue(ctx BottomUpMutatorContext, value reflect.Value, check func(string) bool) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 708 | if !value.IsValid() { |
| 709 | return false |
| 710 | } |
| 711 | |
| 712 | if value.Kind() == reflect.Ptr { |
| 713 | if value.IsNil() { |
| 714 | return check("") |
| 715 | } |
| 716 | value = value.Elem() |
| 717 | } |
| 718 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 719 | switch v := value.Interface().(type) { |
| 720 | case string: |
| 721 | return check(v) |
| 722 | case bool: |
| 723 | return check(strconv.FormatBool(v)) |
| 724 | case int: |
| 725 | return check(strconv.FormatInt((int64)(v), 10)) |
| 726 | case []string: |
| 727 | for _, v := range v { |
| 728 | if check(v) { |
| 729 | return true |
| 730 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 731 | } |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 732 | return false |
| 733 | case proptools.Configurable[string]: |
| 734 | return check(v.GetOrDefault(ctx, "")) |
| 735 | case proptools.Configurable[bool]: |
| 736 | return check(strconv.FormatBool(v.GetOrDefault(ctx, false))) |
| 737 | case proptools.Configurable[[]string]: |
| 738 | for _, v := range v.GetOrDefault(ctx, nil) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 739 | if check(v) { |
| 740 | return true |
| 741 | } |
| 742 | } |
| 743 | return false |
| 744 | } |
| 745 | |
| 746 | panic("Can't handle type: " + value.Kind().String()) |
| 747 | } |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 748 | |
| 749 | var neverallowRulesKey = NewOnceKey("neverallowRules") |
| 750 | |
| 751 | func neverallowRules(config Config) []Rule { |
| 752 | return config.Once(neverallowRulesKey, func() interface{} { |
| 753 | // No test rules were set by setTestNeverallowRules, use the global rules |
| 754 | return neverallows |
| 755 | }).([]Rule) |
| 756 | } |
| 757 | |
| 758 | // Overrides the default neverallow rules for the supplied config. |
| 759 | // |
| 760 | // For testing only. |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 761 | func setTestNeverallowRules(config Config, testRules []Rule) { |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 762 | config.Once(neverallowRulesKey, func() interface{} { return testRules }) |
| 763 | } |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 764 | |
| 765 | // Prepares for a test by setting neverallow rules and enabling the mutator. |
| 766 | // |
| 767 | // If the supplied rules are nil then the default rules are used. |
| 768 | func PrepareForTestWithNeverallowRules(testRules []Rule) FixturePreparer { |
| 769 | return GroupFixturePreparers( |
| 770 | FixtureModifyConfig(func(config Config) { |
| 771 | if testRules != nil { |
| 772 | setTestNeverallowRules(config, testRules) |
| 773 | } |
| 774 | }), |
| 775 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 776 | ctx.PostDepsMutators(registerNeverallowMutator) |
| 777 | }), |
| 778 | ) |
| 779 | } |