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 | dca2f2b | 2024-11-06 18:43:19 +0000 | [diff] [blame^] | 339 | "prebuilt_tvservice", |
| 340 | "prebuilt_optee", |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 341 | ). |
| 342 | DefinedInBpFile(). |
| 343 | Because("module type not allowed to be defined in bp file") |
| 344 | } |
| 345 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 346 | func neverallowMutator(ctx BottomUpMutatorContext) { |
| 347 | m, ok := ctx.Module().(Module) |
| 348 | if !ok { |
| 349 | return |
| 350 | } |
| 351 | |
| 352 | dir := ctx.ModuleDir() + "/" |
| 353 | properties := m.GetProperties() |
| 354 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 355 | osClass := ctx.Module().Target().Os.Class |
| 356 | |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 357 | for _, r := range neverallowRules(ctx.Config()) { |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 358 | n := r.(*rule) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 359 | if !n.appliesToPath(dir) { |
| 360 | continue |
| 361 | } |
| 362 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 363 | if !n.appliesToModuleType(ctx.ModuleType()) { |
| 364 | continue |
| 365 | } |
| 366 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 367 | if !n.appliesToProperties(ctx, properties) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 368 | continue |
| 369 | } |
| 370 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 371 | if !n.appliesToOsClass(osClass) { |
| 372 | continue |
| 373 | } |
| 374 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 375 | if !n.appliesToDirectDeps(ctx) { |
| 376 | continue |
| 377 | } |
| 378 | |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 379 | if !n.appliesToBpDefinedModule(ctx) { |
| 380 | continue |
| 381 | } |
| 382 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 383 | ctx.ModuleErrorf("violates " + n.String()) |
| 384 | } |
| 385 | } |
| 386 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 387 | type ValueMatcher interface { |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 388 | Test(string) bool |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 389 | String() string |
| 390 | } |
| 391 | |
| 392 | type equalMatcher struct { |
| 393 | expected string |
| 394 | } |
| 395 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 396 | func (m *equalMatcher) Test(value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 397 | return m.expected == value |
| 398 | } |
| 399 | |
| 400 | func (m *equalMatcher) String() string { |
| 401 | return "=" + m.expected |
| 402 | } |
| 403 | |
| 404 | type anyMatcher struct { |
| 405 | } |
| 406 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 407 | func (m *anyMatcher) Test(value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 408 | return true |
| 409 | } |
| 410 | |
| 411 | func (m *anyMatcher) String() string { |
| 412 | return "=*" |
| 413 | } |
| 414 | |
| 415 | var anyMatcherInstance = &anyMatcher{} |
| 416 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 417 | type startsWithMatcher struct { |
| 418 | prefix string |
| 419 | } |
| 420 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 421 | func (m *startsWithMatcher) Test(value string) bool { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 422 | return strings.HasPrefix(value, m.prefix) |
| 423 | } |
| 424 | |
| 425 | func (m *startsWithMatcher) String() string { |
| 426 | return ".starts-with(" + m.prefix + ")" |
| 427 | } |
| 428 | |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 429 | type regexMatcher struct { |
| 430 | re *regexp.Regexp |
| 431 | } |
| 432 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 433 | func (m *regexMatcher) Test(value string) bool { |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 434 | return m.re.MatchString(value) |
| 435 | } |
| 436 | |
| 437 | func (m *regexMatcher) String() string { |
| 438 | return ".regexp(" + m.re.String() + ")" |
| 439 | } |
| 440 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 441 | type notInListMatcher struct { |
| 442 | allowed []string |
| 443 | } |
| 444 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 445 | func (m *notInListMatcher) Test(value string) bool { |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 446 | return !InList(value, m.allowed) |
| 447 | } |
| 448 | |
| 449 | func (m *notInListMatcher) String() string { |
| 450 | return ".not-in-list(" + strings.Join(m.allowed, ",") + ")" |
| 451 | } |
| 452 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 453 | type isSetMatcher struct{} |
| 454 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 455 | func (m *isSetMatcher) Test(value string) bool { |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 456 | return value != "" |
| 457 | } |
| 458 | |
| 459 | func (m *isSetMatcher) String() string { |
| 460 | return ".is-set" |
| 461 | } |
| 462 | |
| 463 | var isSetMatcherInstance = &isSetMatcher{} |
| 464 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 465 | type ruleProperty struct { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 466 | fields []string // e.x.: Vndk.Enabled |
| 467 | matcher ValueMatcher |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 470 | func (r *ruleProperty) String() string { |
| 471 | return fmt.Sprintf("%q matches: %s", strings.Join(r.fields, "."), r.matcher) |
| 472 | } |
| 473 | |
| 474 | type ruleProperties []ruleProperty |
| 475 | |
| 476 | func (r ruleProperties) String() string { |
| 477 | var s []string |
| 478 | for _, r := range r { |
| 479 | s = append(s, r.String()) |
| 480 | } |
| 481 | return strings.Join(s, " ") |
| 482 | } |
| 483 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 484 | // A NeverAllow rule. |
| 485 | type Rule interface { |
| 486 | In(path ...string) Rule |
| 487 | |
| 488 | NotIn(path ...string) Rule |
| 489 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 490 | InDirectDeps(deps ...string) Rule |
| 491 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 492 | WithOsClass(osClasses ...OsClass) Rule |
| 493 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 494 | ModuleType(types ...string) Rule |
| 495 | |
| 496 | NotModuleType(types ...string) Rule |
| 497 | |
| 498 | With(properties, value string) Rule |
| 499 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 500 | WithMatcher(properties string, matcher ValueMatcher) Rule |
| 501 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 502 | Without(properties, value string) Rule |
| 503 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 504 | WithoutMatcher(properties string, matcher ValueMatcher) Rule |
| 505 | |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 506 | DefinedInBpFile() Rule |
| 507 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 508 | Because(reason string) Rule |
| 509 | } |
| 510 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 511 | type rule struct { |
| 512 | // User string for why this is a thing. |
| 513 | reason string |
| 514 | |
| 515 | paths []string |
| 516 | unlessPaths []string |
| 517 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 518 | directDeps map[string]bool |
| 519 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 520 | osClasses []OsClass |
| 521 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 522 | moduleTypes []string |
| 523 | unlessModuleTypes []string |
| 524 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 525 | props ruleProperties |
| 526 | unlessProps ruleProperties |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 527 | |
| 528 | onlyBootclasspathJar bool |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 529 | |
| 530 | definedInBp bool |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 533 | // Create a new NeverAllow rule. |
| 534 | func NeverAllow() Rule { |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 535 | return &rule{directDeps: make(map[string]bool)} |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 536 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 537 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 538 | // In adds path(s) where this rule applies. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 539 | func (r *rule) In(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 540 | r.paths = append(r.paths, cleanPaths(path)...) |
| 541 | return r |
| 542 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 543 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 544 | // NotIn adds path(s) to that this rule does not apply to. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 545 | func (r *rule) NotIn(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 546 | r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...) |
| 547 | return r |
| 548 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 549 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 550 | // InDirectDeps adds dep(s) that are not allowed with this rule. |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 551 | func (r *rule) InDirectDeps(deps ...string) Rule { |
| 552 | for _, d := range deps { |
| 553 | r.directDeps[d] = true |
| 554 | } |
| 555 | return r |
| 556 | } |
| 557 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 558 | // WithOsClass adds osClass(es) that this rule applies to. |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 559 | func (r *rule) WithOsClass(osClasses ...OsClass) Rule { |
| 560 | r.osClasses = append(r.osClasses, osClasses...) |
| 561 | return r |
| 562 | } |
| 563 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 564 | // ModuleType adds type(s) that this rule applies to. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 565 | func (r *rule) ModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 566 | r.moduleTypes = append(r.moduleTypes, types...) |
| 567 | return r |
| 568 | } |
| 569 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 570 | // NotModuleType adds type(s) that this rule does not apply to.. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 571 | func (r *rule) NotModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 572 | r.unlessModuleTypes = append(r.unlessModuleTypes, types...) |
| 573 | return r |
| 574 | } |
| 575 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 576 | // With specifies property/value combinations that are restricted for this rule. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 577 | func (r *rule) With(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 578 | return r.WithMatcher(properties, selectMatcher(value)) |
| 579 | } |
| 580 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 581 | // WithMatcher specifies property/matcher combinations that are restricted for this rule. |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 582 | func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 583 | r.props = append(r.props, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 584 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 585 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 586 | }) |
| 587 | return r |
| 588 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 589 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 590 | // Without specifies property/value combinations that this rule does not apply to. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 591 | func (r *rule) Without(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 592 | return r.WithoutMatcher(properties, selectMatcher(value)) |
| 593 | } |
| 594 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 595 | // Without specifies property/matcher combinations that this rule does not apply to. |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 596 | func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 597 | r.unlessProps = append(r.unlessProps, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 598 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 599 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 600 | }) |
| 601 | return r |
| 602 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 603 | |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 604 | // DefinedInBpFile specifies that this rule applies to modules that are defined |
| 605 | // in bp files, and does not apply to modules that are auto generated by other modules. |
| 606 | func (r *rule) DefinedInBpFile() Rule { |
| 607 | r.definedInBp = true |
| 608 | return r |
| 609 | } |
| 610 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 611 | func selectMatcher(expected string) ValueMatcher { |
| 612 | if expected == "*" { |
| 613 | return anyMatcherInstance |
| 614 | } |
| 615 | return &equalMatcher{expected: expected} |
| 616 | } |
| 617 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 618 | // Because specifies a reason for this rule. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 619 | func (r *rule) Because(reason string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 620 | r.reason = reason |
| 621 | return r |
| 622 | } |
| 623 | |
| 624 | func (r *rule) String() string { |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 625 | s := []string{"neverallow requirements. Not allowed:"} |
| 626 | if len(r.paths) > 0 { |
| 627 | s = append(s, fmt.Sprintf("in dirs: %q", r.paths)) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 628 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 629 | if len(r.moduleTypes) > 0 { |
| 630 | s = append(s, fmt.Sprintf("module types: %q", r.moduleTypes)) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 631 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 632 | if len(r.props) > 0 { |
| 633 | s = append(s, fmt.Sprintf("properties matching: %s", r.props)) |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 634 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 635 | if len(r.directDeps) > 0 { |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 636 | s = append(s, fmt.Sprintf("dep(s): %q", SortedKeys(r.directDeps))) |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 637 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 638 | if len(r.osClasses) > 0 { |
| 639 | s = append(s, fmt.Sprintf("os class(es): %q", r.osClasses)) |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 640 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 641 | if len(r.unlessPaths) > 0 { |
| 642 | s = append(s, fmt.Sprintf("EXCEPT in dirs: %q", r.unlessPaths)) |
| 643 | } |
| 644 | if len(r.unlessModuleTypes) > 0 { |
| 645 | s = append(s, fmt.Sprintf("EXCEPT module types: %q", r.unlessModuleTypes)) |
| 646 | } |
| 647 | if len(r.unlessProps) > 0 { |
| 648 | s = append(s, fmt.Sprintf("EXCEPT properties matching: %q", r.unlessProps)) |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 649 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 650 | if len(r.reason) != 0 { |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 651 | s = append(s, " which is restricted because "+r.reason) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 652 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 653 | if len(s) == 1 { |
| 654 | s[0] = "neverallow requirements (empty)" |
| 655 | } |
| 656 | return strings.Join(s, "\n\t") |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | func (r *rule) appliesToPath(dir string) bool { |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 660 | includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths) |
| 661 | excludePath := HasAnyPrefix(dir, r.unlessPaths) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 662 | return includePath && !excludePath |
| 663 | } |
| 664 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 665 | func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool { |
| 666 | if len(r.directDeps) == 0 { |
| 667 | return true |
| 668 | } |
| 669 | |
| 670 | matches := false |
| 671 | ctx.VisitDirectDeps(func(m Module) { |
| 672 | if !matches { |
| 673 | name := ctx.OtherModuleName(m) |
| 674 | matches = r.directDeps[name] |
| 675 | } |
| 676 | }) |
| 677 | |
| 678 | return matches |
| 679 | } |
| 680 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 681 | func (r *rule) appliesToOsClass(osClass OsClass) bool { |
| 682 | if len(r.osClasses) == 0 { |
| 683 | return true |
| 684 | } |
| 685 | |
| 686 | for _, c := range r.osClasses { |
| 687 | if c == osClass { |
| 688 | return true |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | return false |
| 693 | } |
| 694 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 695 | func (r *rule) appliesToModuleType(moduleType string) bool { |
| 696 | return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes) |
| 697 | } |
| 698 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 699 | func (r *rule) appliesToProperties(ctx BottomUpMutatorContext, properties []interface{}) bool { |
| 700 | includeProps := hasAllProperties(ctx, properties, r.props) |
| 701 | excludeProps := hasAnyProperty(ctx, properties, r.unlessProps) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 702 | return includeProps && !excludeProps |
| 703 | } |
| 704 | |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 705 | func (r *rule) appliesToBpDefinedModule(ctx BottomUpMutatorContext) bool { |
| 706 | if !r.definedInBp { |
| 707 | return true |
| 708 | } |
| 709 | return !ctx.OtherModuleIsAutoGenerated(ctx.Module()) == r.definedInBp |
| 710 | } |
| 711 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 712 | func StartsWith(prefix string) ValueMatcher { |
| 713 | return &startsWithMatcher{prefix} |
| 714 | } |
| 715 | |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 716 | func Regexp(re string) ValueMatcher { |
| 717 | r, err := regexp.Compile(re) |
| 718 | if err != nil { |
| 719 | panic(err) |
| 720 | } |
| 721 | return ®exMatcher{r} |
| 722 | } |
| 723 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 724 | func NotInList(allowed []string) ValueMatcher { |
| 725 | return ¬InListMatcher{allowed} |
| 726 | } |
| 727 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 728 | // assorted utils |
| 729 | |
| 730 | func cleanPaths(paths []string) []string { |
| 731 | res := make([]string, len(paths)) |
| 732 | for i, v := range paths { |
| 733 | res[i] = filepath.Clean(v) + "/" |
| 734 | } |
| 735 | return res |
| 736 | } |
| 737 | |
| 738 | func fieldNamesForProperties(propertyNames string) []string { |
| 739 | names := strings.Split(propertyNames, ".") |
| 740 | for i, v := range names { |
| 741 | names[i] = proptools.FieldNameForProperty(v) |
| 742 | } |
| 743 | return names |
| 744 | } |
| 745 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 746 | func hasAnyProperty(ctx BottomUpMutatorContext, properties []interface{}, props []ruleProperty) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 747 | for _, v := range props { |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 748 | if hasProperty(ctx, properties, v) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 749 | return true |
| 750 | } |
| 751 | } |
| 752 | return false |
| 753 | } |
| 754 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 755 | func hasAllProperties(ctx BottomUpMutatorContext, properties []interface{}, props []ruleProperty) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 756 | for _, v := range props { |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 757 | if !hasProperty(ctx, properties, v) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 758 | return false |
| 759 | } |
| 760 | } |
| 761 | return true |
| 762 | } |
| 763 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 764 | func hasProperty(ctx BottomUpMutatorContext, properties []interface{}, prop ruleProperty) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 765 | for _, propertyStruct := range properties { |
| 766 | propertiesValue := reflect.ValueOf(propertyStruct).Elem() |
| 767 | for _, v := range prop.fields { |
| 768 | if !propertiesValue.IsValid() { |
| 769 | break |
| 770 | } |
| 771 | propertiesValue = propertiesValue.FieldByName(v) |
| 772 | } |
| 773 | if !propertiesValue.IsValid() { |
| 774 | continue |
| 775 | } |
| 776 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 777 | check := func(value string) bool { |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 778 | return prop.matcher.Test(value) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 781 | if matchValue(ctx, propertiesValue, check) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 782 | return true |
| 783 | } |
| 784 | } |
| 785 | return false |
| 786 | } |
| 787 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 788 | func matchValue(ctx BottomUpMutatorContext, value reflect.Value, check func(string) bool) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 789 | if !value.IsValid() { |
| 790 | return false |
| 791 | } |
| 792 | |
| 793 | if value.Kind() == reflect.Ptr { |
| 794 | if value.IsNil() { |
| 795 | return check("") |
| 796 | } |
| 797 | value = value.Elem() |
| 798 | } |
| 799 | |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 800 | switch v := value.Interface().(type) { |
| 801 | case string: |
| 802 | return check(v) |
| 803 | case bool: |
| 804 | return check(strconv.FormatBool(v)) |
| 805 | case int: |
| 806 | return check(strconv.FormatInt((int64)(v), 10)) |
| 807 | case []string: |
| 808 | for _, v := range v { |
| 809 | if check(v) { |
| 810 | return true |
| 811 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 812 | } |
Cole Faust | 5b35cb9 | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 813 | return false |
| 814 | case proptools.Configurable[string]: |
| 815 | return check(v.GetOrDefault(ctx, "")) |
| 816 | case proptools.Configurable[bool]: |
| 817 | return check(strconv.FormatBool(v.GetOrDefault(ctx, false))) |
| 818 | case proptools.Configurable[[]string]: |
| 819 | for _, v := range v.GetOrDefault(ctx, nil) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 820 | if check(v) { |
| 821 | return true |
| 822 | } |
| 823 | } |
| 824 | return false |
| 825 | } |
| 826 | |
| 827 | panic("Can't handle type: " + value.Kind().String()) |
| 828 | } |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 829 | |
| 830 | var neverallowRulesKey = NewOnceKey("neverallowRules") |
| 831 | |
| 832 | func neverallowRules(config Config) []Rule { |
| 833 | return config.Once(neverallowRulesKey, func() interface{} { |
| 834 | // No test rules were set by setTestNeverallowRules, use the global rules |
| 835 | return neverallows |
| 836 | }).([]Rule) |
| 837 | } |
| 838 | |
| 839 | // Overrides the default neverallow rules for the supplied config. |
| 840 | // |
| 841 | // For testing only. |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 842 | func setTestNeverallowRules(config Config, testRules []Rule) { |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 843 | config.Once(neverallowRulesKey, func() interface{} { return testRules }) |
| 844 | } |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 845 | |
| 846 | // Prepares for a test by setting neverallow rules and enabling the mutator. |
| 847 | // |
| 848 | // If the supplied rules are nil then the default rules are used. |
| 849 | func PrepareForTestWithNeverallowRules(testRules []Rule) FixturePreparer { |
| 850 | return GroupFixturePreparers( |
| 851 | FixtureModifyConfig(func(config Config) { |
| 852 | if testRules != nil { |
| 853 | setTestNeverallowRules(config, testRules) |
| 854 | } |
| 855 | }), |
| 856 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 857 | ctx.PostDepsMutators(registerNeverallowMutator) |
| 858 | }), |
| 859 | ) |
| 860 | } |