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