Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 1 | // Copyright (C) 2019 The Android Open Source Project |
| 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 selinux |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io" |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 20 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
| 23 | |
| 24 | "android/soong/android" |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 25 | "android/soong/sysprop" |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 26 | ) |
| 27 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 28 | type selinuxContextsProperties struct { |
| 29 | // Filenames under sepolicy directories, which will be used to generate contexts file. |
| 30 | Srcs []string `android:"path"` |
| 31 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 32 | // Output file name. Defaults to module name |
| 33 | Stem *string |
| 34 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 35 | Product_variables struct { |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 36 | Address_sanitize struct { |
Inseob Kim | 6d3d5a6 | 2021-12-21 20:55:32 +0900 | [diff] [blame] | 37 | Srcs []string `android:"path"` |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 41 | // Whether the comments in generated contexts file will be removed or not. |
| 42 | Remove_comment *bool |
| 43 | |
| 44 | // Whether the result context file is sorted with fc_sort or not. |
| 45 | Fc_sort *bool |
| 46 | |
| 47 | // Make this module available when building for recovery |
| 48 | Recovery_available *bool |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 49 | } |
| 50 | |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 51 | type seappProperties struct { |
| 52 | // Files containing neverallow rules. |
| 53 | Neverallow_files []string `android:"path"` |
| 54 | |
| 55 | // Precompiled sepolicy binary file which will be fed to checkseapp. |
| 56 | Sepolicy *string `android:"path"` |
| 57 | } |
| 58 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 59 | type selinuxContextsModule struct { |
| 60 | android.ModuleBase |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 61 | android.DefaultableModuleBase |
| 62 | flaggableModuleBase |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 63 | |
Jooyung Han | 804e234 | 2023-06-20 15:38:50 +0900 | [diff] [blame] | 64 | properties selinuxContextsProperties |
| 65 | seappProperties seappProperties |
| 66 | build func(ctx android.ModuleContext, inputs android.Paths) android.Path |
| 67 | deps func(ctx android.BottomUpMutatorContext) |
| 68 | outputPath android.Path |
| 69 | installPath android.InstallPath |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 70 | } |
| 71 | |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 72 | var _ flaggableModule = (*selinuxContextsModule)(nil) |
| 73 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 74 | var ( |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 75 | reuseContextsDepTag = dependencyTag{name: "reuseContexts"} |
| 76 | syspropLibraryDepTag = dependencyTag{name: "sysprop_library"} |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 77 | ) |
| 78 | |
| 79 | func init() { |
| 80 | pctx.HostBinToolVariable("fc_sort", "fc_sort") |
| 81 | |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 82 | android.RegisterModuleType("contexts_defaults", contextsDefaultsFactory) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 83 | android.RegisterModuleType("file_contexts", fileFactory) |
| 84 | android.RegisterModuleType("hwservice_contexts", hwServiceFactory) |
| 85 | android.RegisterModuleType("property_contexts", propertyFactory) |
| 86 | android.RegisterModuleType("service_contexts", serviceFactory) |
Janis Danisevskis | c40681f | 2020-07-25 13:02:29 -0700 | [diff] [blame] | 87 | android.RegisterModuleType("keystore2_key_contexts", keystoreKeyFactory) |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 88 | android.RegisterModuleType("seapp_contexts", seappFactory) |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 89 | android.RegisterModuleType("vndservice_contexts", vndServiceFactory) |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 90 | |
| 91 | android.RegisterModuleType("file_contexts_test", fileContextsTestFactory) |
| 92 | android.RegisterModuleType("property_contexts_test", propertyContextsTestFactory) |
| 93 | android.RegisterModuleType("hwservice_contexts_test", hwserviceContextsTestFactory) |
| 94 | android.RegisterModuleType("service_contexts_test", serviceContextsTestFactory) |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 95 | android.RegisterModuleType("vndservice_contexts_test", vndServiceContextsTestFactory) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 98 | func (m *selinuxContextsModule) InstallInRoot() bool { |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 99 | return m.InRecovery() |
| 100 | } |
| 101 | |
| 102 | func (m *selinuxContextsModule) InstallInRecovery() bool { |
| 103 | // ModuleBase.InRecovery() checks the image variant |
| 104 | return m.InRecovery() |
| 105 | } |
| 106 | |
| 107 | func (m *selinuxContextsModule) onlyInRecovery() bool { |
| 108 | // ModuleBase.InstallInRecovery() checks commonProperties.Recovery property |
| 109 | return m.ModuleBase.InstallInRecovery() |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 112 | func (m *selinuxContextsModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 113 | if m.deps != nil { |
| 114 | m.deps(ctx) |
| 115 | } |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 116 | |
| 117 | if m.InRecovery() && !m.onlyInRecovery() { |
| 118 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 119 | {Mutator: "image", Variation: android.CoreVariation}, |
| 120 | }, reuseContextsDepTag, ctx.ModuleName()) |
| 121 | } |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | func (m *selinuxContextsModule) propertyContextsDeps(ctx android.BottomUpMutatorContext) { |
| 125 | for _, lib := range sysprop.SyspropLibraries(ctx.Config()) { |
| 126 | ctx.AddFarVariationDependencies([]blueprint.Variation{}, syspropLibraryDepTag, lib) |
| 127 | } |
| 128 | } |
| 129 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 130 | func (m *selinuxContextsModule) stem() string { |
| 131 | return proptools.StringDefault(m.properties.Stem, m.Name()) |
| 132 | } |
| 133 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 134 | func (m *selinuxContextsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 135 | if m.InRecovery() { |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 136 | // Installing context files at the root of the recovery partition |
| 137 | m.installPath = android.PathForModuleInstall(ctx) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 138 | } else { |
| 139 | m.installPath = android.PathForModuleInstall(ctx, "etc", "selinux") |
| 140 | } |
| 141 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 142 | if m.InRecovery() && !m.onlyInRecovery() { |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 143 | dep := ctx.GetDirectDepWithTag(m.Name(), reuseContextsDepTag) |
| 144 | |
| 145 | if reuseDeps, ok := dep.(*selinuxContextsModule); ok { |
| 146 | m.outputPath = reuseDeps.outputPath |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 147 | ctx.InstallFile(m.installPath, m.stem(), m.outputPath) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 148 | return |
| 149 | } |
| 150 | } |
| 151 | |
Inseob Kim | 6d3d5a6 | 2021-12-21 20:55:32 +0900 | [diff] [blame] | 152 | m.outputPath = m.build(ctx, android.PathsForModuleSrc(ctx, m.properties.Srcs)) |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 153 | ctx.InstallFile(m.installPath, m.stem(), m.outputPath) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | func newModule() *selinuxContextsModule { |
| 157 | m := &selinuxContextsModule{} |
| 158 | m.AddProperties( |
| 159 | &m.properties, |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 160 | &m.seappProperties, |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 161 | ) |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 162 | initFlaggableModule(m) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 163 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 164 | android.InitDefaultableModule(m) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 165 | android.AddLoadHook(m, func(ctx android.LoadHookContext) { |
| 166 | m.selinuxContextsHook(ctx) |
| 167 | }) |
| 168 | return m |
| 169 | } |
| 170 | |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 171 | type contextsDefaults struct { |
| 172 | android.ModuleBase |
| 173 | android.DefaultsModuleBase |
| 174 | } |
| 175 | |
| 176 | // contexts_defaults provides a set of properties that can be inherited by other contexts modules. |
| 177 | // (file_contexts, property_contexts, seapp_contexts, etc.) A module can use the properties from a |
| 178 | // contexts_defaults using `defaults: ["<:default_module_name>"]`. Properties of both modules are |
| 179 | // erged (when possible) by prepending the default module's values to the depending module's values. |
| 180 | func contextsDefaultsFactory() android.Module { |
| 181 | m := &contextsDefaults{} |
| 182 | m.AddProperties( |
| 183 | &selinuxContextsProperties{}, |
| 184 | &seappProperties{}, |
| 185 | &flagsProperties{}, |
| 186 | ) |
| 187 | android.InitDefaultsModule(m) |
| 188 | return m |
| 189 | } |
| 190 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 191 | func (m *selinuxContextsModule) selinuxContextsHook(ctx android.LoadHookContext) { |
| 192 | // TODO: clean this up to use build/soong/android/variable.go after b/79249983 |
| 193 | var srcs []string |
| 194 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 195 | for _, sanitize := range ctx.Config().SanitizeDevice() { |
| 196 | if sanitize == "address" { |
| 197 | srcs = append(srcs, m.properties.Product_variables.Address_sanitize.Srcs...) |
| 198 | break |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | m.properties.Srcs = append(m.properties.Srcs, srcs...) |
| 203 | } |
| 204 | |
| 205 | func (m *selinuxContextsModule) AndroidMk() android.AndroidMkData { |
Colin Cross | f82aed0 | 2021-11-04 17:25:55 -0700 | [diff] [blame] | 206 | nameSuffix := "" |
| 207 | if m.InRecovery() && !m.onlyInRecovery() { |
| 208 | nameSuffix = ".recovery" |
| 209 | } |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 210 | return android.AndroidMkData{ |
Colin Cross | f82aed0 | 2021-11-04 17:25:55 -0700 | [diff] [blame] | 211 | Class: "ETC", |
| 212 | OutputFile: android.OptionalPathForPath(m.outputPath), |
| 213 | SubName: nameSuffix, |
| 214 | Extra: []android.AndroidMkExtraFunc{ |
| 215 | func(w io.Writer, outputFile android.Path) { |
Colin Cross | 6c7f937 | 2022-01-11 19:35:43 -0800 | [diff] [blame] | 216 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", m.installPath.String()) |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 217 | fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.stem()) |
Colin Cross | f82aed0 | 2021-11-04 17:25:55 -0700 | [diff] [blame] | 218 | }, |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 219 | }, |
| 220 | } |
| 221 | } |
| 222 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 223 | func (m *selinuxContextsModule) ImageMutatorBegin(ctx android.BaseModuleContext) { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 224 | if proptools.Bool(m.properties.Recovery_available) && m.ModuleBase.InstallInRecovery() { |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 225 | ctx.PropertyErrorf("recovery_available", |
| 226 | "doesn't make sense at the same time as `recovery: true`") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 230 | func (m *selinuxContextsModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 231 | return !m.ModuleBase.InstallInRecovery() |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | func (m *selinuxContextsModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 235 | return false |
| 236 | } |
| 237 | |
| 238 | func (m *selinuxContextsModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 239 | return false |
| 240 | } |
| 241 | |
Inseob Kim | 6cc75f4 | 2021-04-29 13:53:20 +0000 | [diff] [blame] | 242 | func (m *selinuxContextsModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 243 | return false |
| 244 | } |
| 245 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 246 | func (m *selinuxContextsModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 247 | return m.ModuleBase.InstallInRecovery() || proptools.Bool(m.properties.Recovery_available) |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | func (m *selinuxContextsModule) ExtraImageVariations(ctx android.BaseModuleContext) []string { |
| 251 | return nil |
| 252 | } |
| 253 | |
| 254 | func (m *selinuxContextsModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { |
| 255 | } |
| 256 | |
| 257 | var _ android.ImageInterface = (*selinuxContextsModule)(nil) |
| 258 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 259 | func (m *selinuxContextsModule) buildGeneralContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 260 | builtContext := pathForModuleOut(ctx, ctx.ModuleName()+"_m4out") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 261 | |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 262 | rule := android.NewRuleBuilder(pctx, ctx) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 263 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 264 | newlineFile := pathForModuleOut(ctx, "newline") |
Inseob Kim | 35e9d41 | 2023-01-04 15:27:32 +0900 | [diff] [blame] | 265 | |
| 266 | rule.Command().Text("echo").FlagWithOutput("> ", newlineFile) |
| 267 | rule.Temporary(newlineFile) |
| 268 | |
| 269 | var inputsWithNewline android.Paths |
| 270 | for _, input := range inputs { |
| 271 | inputsWithNewline = append(inputsWithNewline, input, newlineFile) |
| 272 | } |
| 273 | |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 274 | flags := m.getBuildFlags(ctx) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 275 | rule.Command(). |
Dan Willemsen | 3c3e59b | 2019-06-19 10:52:50 -0700 | [diff] [blame] | 276 | Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")). |
| 277 | Text("--fatal-warnings -s"). |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 278 | FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()). |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 279 | Flags(flagsToM4Macros(flags)). |
Inseob Kim | 35e9d41 | 2023-01-04 15:27:32 +0900 | [diff] [blame] | 280 | Inputs(inputsWithNewline). |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 281 | FlagWithOutput("> ", builtContext) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 282 | |
| 283 | if proptools.Bool(m.properties.Remove_comment) { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 284 | rule.Temporary(builtContext) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 285 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 286 | remove_comment_output := pathForModuleOut(ctx, ctx.ModuleName()+"_remove_comment") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 287 | |
| 288 | rule.Command(). |
| 289 | Text("sed -e 's/#.*$//' -e '/^$/d'"). |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 290 | Input(builtContext). |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 291 | FlagWithOutput("> ", remove_comment_output) |
| 292 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 293 | builtContext = remove_comment_output |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | if proptools.Bool(m.properties.Fc_sort) { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 297 | rule.Temporary(builtContext) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 298 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 299 | sorted_output := pathForModuleOut(ctx, ctx.ModuleName()+"_sorted") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 300 | |
| 301 | rule.Command(). |
| 302 | Tool(ctx.Config().HostToolPath(ctx, "fc_sort")). |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 303 | FlagWithInput("-i ", builtContext). |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 304 | FlagWithOutput("-o ", sorted_output) |
| 305 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 306 | builtContext = sorted_output |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 307 | } |
| 308 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 309 | ret := pathForModuleOut(ctx, m.stem()) |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 310 | rule.Temporary(builtContext) |
| 311 | rule.Command().Text("cp").Input(builtContext).Output(ret) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 312 | |
| 313 | rule.DeleteTemporaryFiles() |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 314 | rule.Build("selinux_contexts", "building contexts: "+m.Name()) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 315 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 316 | return ret |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 317 | } |
| 318 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 319 | func (m *selinuxContextsModule) buildFileContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | dfa4a48 | 2023-11-01 17:58:18 +0900 | [diff] [blame] | 320 | if m.properties.Remove_comment == nil { |
| 321 | m.properties.Remove_comment = proptools.BoolPtr(true) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 322 | } |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 323 | return m.buildGeneralContexts(ctx, inputs) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | func fileFactory() android.Module { |
| 327 | m := newModule() |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 328 | m.build = m.buildFileContexts |
| 329 | return m |
| 330 | } |
| 331 | |
Thiébaud Weksteen | 74482f5 | 2023-04-26 13:46:59 +1000 | [diff] [blame] | 332 | func (m *selinuxContextsModule) buildServiceContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 333 | if m.properties.Remove_comment == nil { |
| 334 | m.properties.Remove_comment = proptools.BoolPtr(true) |
| 335 | } |
| 336 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 337 | return m.buildGeneralContexts(ctx, inputs) |
| 338 | } |
| 339 | |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 340 | func (m *selinuxContextsModule) checkVendorPropertyNamespace(ctx android.ModuleContext, input android.Path) android.Path { |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 341 | shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel() |
| 342 | ApiLevelR := android.ApiLevelOrPanic(ctx, "R") |
| 343 | |
| 344 | rule := android.NewRuleBuilder(pctx, ctx) |
| 345 | |
| 346 | // This list is from vts_treble_sys_prop_test. |
| 347 | allowedPropertyPrefixes := []string{ |
| 348 | "ctl.odm.", |
| 349 | "ctl.vendor.", |
| 350 | "ctl.start$odm.", |
| 351 | "ctl.start$vendor.", |
| 352 | "ctl.stop$odm.", |
| 353 | "ctl.stop$vendor.", |
| 354 | "init.svc.odm.", |
| 355 | "init.svc.vendor.", |
| 356 | "ro.boot.", |
| 357 | "ro.hardware.", |
| 358 | "ro.odm.", |
| 359 | "ro.vendor.", |
| 360 | "odm.", |
| 361 | "persist.odm.", |
| 362 | "persist.vendor.", |
| 363 | "vendor.", |
| 364 | } |
| 365 | |
| 366 | // persist.camera is also allowed for devices launching with R or eariler |
| 367 | if shippingApiLevel.LessThanOrEqualTo(ApiLevelR) { |
| 368 | allowedPropertyPrefixes = append(allowedPropertyPrefixes, "persist.camera.") |
| 369 | } |
| 370 | |
| 371 | var allowedContextPrefixes []string |
| 372 | |
| 373 | if shippingApiLevel.GreaterThanOrEqualTo(ApiLevelR) { |
| 374 | // This list is from vts_treble_sys_prop_test. |
| 375 | allowedContextPrefixes = []string{ |
| 376 | "vendor_", |
| 377 | "odm_", |
| 378 | } |
| 379 | } |
| 380 | |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 381 | cmd := rule.Command(). |
| 382 | BuiltTool("check_prop_prefix"). |
| 383 | FlagWithInput("--property-contexts ", input). |
| 384 | FlagForEachArg("--allowed-property-prefix ", proptools.ShellEscapeList(allowedPropertyPrefixes)). // contains shell special character '$' |
| 385 | FlagForEachArg("--allowed-context-prefix ", allowedContextPrefixes) |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 386 | |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 387 | if !ctx.DeviceConfig().BuildBrokenVendorPropertyNamespace() { |
| 388 | cmd.Flag("--strict") |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 389 | } |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 390 | |
| 391 | out := pathForModuleOut(ctx, "namespace_checked").Join(ctx, input.String()) |
| 392 | rule.Command().Text("cp -f").Input(input).Output(out) |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 393 | rule.Build("check_namespace", "checking namespace of "+ctx.ModuleName()) |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 394 | return out |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 395 | } |
| 396 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 397 | func (m *selinuxContextsModule) buildPropertyContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 398 | // vendor/odm properties are enforced for devices launching with Android Q or later. So, if |
| 399 | // vendor/odm, make sure that only vendor/odm properties exist. |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 400 | builtCtxFile := m.buildGeneralContexts(ctx, inputs) |
| 401 | |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 402 | shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel() |
| 403 | ApiLevelQ := android.ApiLevelOrPanic(ctx, "Q") |
| 404 | if (ctx.SocSpecific() || ctx.DeviceSpecific()) && shippingApiLevel.GreaterThanOrEqualTo(ApiLevelQ) { |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 405 | builtCtxFile = m.checkVendorPropertyNamespace(ctx, builtCtxFile) |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 406 | } |
| 407 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 408 | var apiFiles android.Paths |
| 409 | ctx.VisitDirectDepsWithTag(syspropLibraryDepTag, func(c android.Module) { |
Inseob Kim | 3a3539a | 2021-01-15 18:10:29 +0900 | [diff] [blame] | 410 | i, ok := c.(interface{ CurrentSyspropApiFile() android.OptionalPath }) |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 411 | if !ok { |
| 412 | panic(fmt.Errorf("unknown dependency %q for %q", ctx.OtherModuleName(c), ctx.ModuleName())) |
| 413 | } |
Inseob Kim | 3a3539a | 2021-01-15 18:10:29 +0900 | [diff] [blame] | 414 | if api := i.CurrentSyspropApiFile(); api.Valid() { |
| 415 | apiFiles = append(apiFiles, api.Path()) |
| 416 | } |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 417 | }) |
| 418 | |
| 419 | // check compatibility with sysprop_library |
| 420 | if len(apiFiles) > 0 { |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 421 | out := pathForModuleOut(ctx, ctx.ModuleName()+"_api_checked") |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 422 | rule := android.NewRuleBuilder(pctx, ctx) |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 423 | |
| 424 | msg := `\n******************************\n` + |
| 425 | `API of sysprop_library doesn't match with property_contexts\n` + |
| 426 | `Please fix the breakage and rebuild.\n` + |
| 427 | `******************************\n` |
| 428 | |
| 429 | rule.Command(). |
| 430 | Text("( "). |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 431 | BuiltTool("sysprop_type_checker"). |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 432 | FlagForEachInput("--api ", apiFiles). |
| 433 | FlagWithInput("--context ", builtCtxFile). |
| 434 | Text(" || ( echo").Flag("-e"). |
| 435 | Flag(`"` + msg + `"`). |
| 436 | Text("; exit 38) )") |
| 437 | |
| 438 | rule.Command().Text("cp -f").Input(builtCtxFile).Output(out) |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 439 | rule.Build("property_contexts_check_api", "checking API: "+m.Name()) |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 440 | builtCtxFile = out |
| 441 | } |
| 442 | |
| 443 | return builtCtxFile |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 444 | } |
| 445 | |
Inseob Kim | 06518b1 | 2023-08-25 21:20:08 +0900 | [diff] [blame] | 446 | func (m *selinuxContextsModule) shouldCheckCoredomain(ctx android.ModuleContext) bool { |
| 447 | if !ctx.SocSpecific() && !ctx.DeviceSpecific() { |
| 448 | return false |
| 449 | } |
| 450 | |
| 451 | return ctx.DeviceConfig().CheckVendorSeappViolations() |
| 452 | } |
| 453 | |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 454 | func (m *selinuxContextsModule) buildSeappContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 455 | neverallowFile := pathForModuleOut(ctx, "neverallow") |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 456 | ret := pathForModuleOut(ctx, "checkseapp", m.stem()) |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 457 | |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 458 | // Step 1. Generate a M4 processed neverallow file |
| 459 | flags := m.getBuildFlags(ctx) |
| 460 | m4NeverallowFile := pathForModuleOut(ctx, "neverallow.m4out") |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 461 | rule := android.NewRuleBuilder(pctx, ctx) |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 462 | rule.Command(). |
| 463 | Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")). |
| 464 | Flag("--fatal-warnings"). |
| 465 | FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()). |
| 466 | Flags(flagsToM4Macros(flags)). |
| 467 | Inputs(android.PathsForModuleSrc(ctx, m.seappProperties.Neverallow_files)). |
| 468 | FlagWithOutput("> ", m4NeverallowFile) |
| 469 | |
| 470 | rule.Temporary(m4NeverallowFile) |
| 471 | rule.Command(). |
| 472 | Text("( grep"). |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 473 | Flag("-ihe"). |
| 474 | Text("'^neverallow'"). |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 475 | Input(m4NeverallowFile). |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 476 | Text(">"). |
| 477 | Output(neverallowFile). |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 478 | Text("|| true )") // to make ninja happy even when result is empty |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 479 | |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 480 | // Step 2. Generate a M4 processed contexts file |
| 481 | builtCtx := m.buildGeneralContexts(ctx, inputs) |
| 482 | |
| 483 | // Step 3. checkseapp |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 484 | rule.Temporary(neverallowFile) |
Inseob Kim | d7d3609 | 2023-06-26 20:48:48 +0900 | [diff] [blame] | 485 | checkCmd := rule.Command().BuiltTool("checkseapp"). |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 486 | FlagWithInput("-p ", android.PathForModuleSrc(ctx, proptools.String(m.seappProperties.Sepolicy))). |
| 487 | FlagWithOutput("-o ", ret). |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame^] | 488 | Input(builtCtx). |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 489 | Input(neverallowFile) |
| 490 | |
Inseob Kim | 06518b1 | 2023-08-25 21:20:08 +0900 | [diff] [blame] | 491 | if m.shouldCheckCoredomain(ctx) { |
| 492 | checkCmd.Flag("-c") // check coredomain for vendor contexts |
Inseob Kim | d7d3609 | 2023-06-26 20:48:48 +0900 | [diff] [blame] | 493 | } |
| 494 | |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 495 | rule.Build("seapp_contexts", "Building seapp_contexts: "+m.Name()) |
| 496 | return ret |
| 497 | } |
| 498 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 499 | func hwServiceFactory() android.Module { |
| 500 | m := newModule() |
Thiébaud Weksteen | 74482f5 | 2023-04-26 13:46:59 +1000 | [diff] [blame] | 501 | m.build = m.buildServiceContexts |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 502 | return m |
| 503 | } |
| 504 | |
| 505 | func propertyFactory() android.Module { |
| 506 | m := newModule() |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 507 | m.build = m.buildPropertyContexts |
| 508 | m.deps = m.propertyContextsDeps |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 509 | return m |
| 510 | } |
| 511 | |
| 512 | func serviceFactory() android.Module { |
| 513 | m := newModule() |
Thiébaud Weksteen | 74482f5 | 2023-04-26 13:46:59 +1000 | [diff] [blame] | 514 | m.build = m.buildServiceContexts |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 515 | return m |
| 516 | } |
Janis Danisevskis | c40681f | 2020-07-25 13:02:29 -0700 | [diff] [blame] | 517 | |
| 518 | func keystoreKeyFactory() android.Module { |
| 519 | m := newModule() |
| 520 | m.build = m.buildGeneralContexts |
| 521 | return m |
| 522 | } |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 523 | |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 524 | func seappFactory() android.Module { |
| 525 | m := newModule() |
| 526 | m.build = m.buildSeappContexts |
| 527 | return m |
| 528 | } |
| 529 | |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 530 | func vndServiceFactory() android.Module { |
| 531 | m := newModule() |
| 532 | m.build = m.buildGeneralContexts |
| 533 | android.AddLoadHook(m, func(ctx android.LoadHookContext) { |
| 534 | if !ctx.SocSpecific() { |
| 535 | ctx.ModuleErrorf(m.Name(), "must set vendor: true") |
| 536 | return |
| 537 | } |
| 538 | }) |
| 539 | return m |
| 540 | } |
| 541 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 542 | var _ android.OutputFileProducer = (*selinuxContextsModule)(nil) |
| 543 | |
| 544 | // Implements android.OutputFileProducer |
| 545 | func (m *selinuxContextsModule) OutputFiles(tag string) (android.Paths, error) { |
| 546 | if tag == "" { |
| 547 | return []android.Path{m.outputPath}, nil |
| 548 | } |
| 549 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 550 | } |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 551 | |
| 552 | type contextsTestProperties struct { |
| 553 | // Contexts files to be tested. |
| 554 | Srcs []string `android:"path"` |
| 555 | |
| 556 | // Precompiled sepolicy binary to be tesed together. |
| 557 | Sepolicy *string `android:"path"` |
| 558 | } |
| 559 | |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 560 | type fileContextsTestProperties struct { |
| 561 | // Test data. File passed to `checkfc -t` to validate how contexts are resolved. |
| 562 | Test_data *string `android:"path"` |
| 563 | } |
| 564 | |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 565 | type contextsTestModule struct { |
| 566 | android.ModuleBase |
| 567 | |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 568 | // The type of context. |
| 569 | context contextType |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 570 | |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 571 | properties contextsTestProperties |
| 572 | fileProperties fileContextsTestProperties |
| 573 | testTimestamp android.OutputPath |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 576 | type contextType int |
| 577 | |
| 578 | const ( |
| 579 | FileContext contextType = iota |
| 580 | PropertyContext |
| 581 | ServiceContext |
| 582 | HwServiceContext |
| 583 | VndServiceContext |
| 584 | ) |
| 585 | |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 586 | // checkfc parses a context file and checks for syntax errors. |
| 587 | // If -s is specified, the service backend is used to verify binder services. |
| 588 | // If -l is specified, the service backend is used to verify hwbinder services. |
| 589 | // Otherwise, context_file is assumed to be a file_contexts file |
| 590 | // If -e is specified, then the context_file is allowed to be empty. |
| 591 | |
| 592 | // file_contexts_test tests given file_contexts files with checkfc. |
| 593 | func fileContextsTestFactory() android.Module { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 594 | m := &contextsTestModule{context: FileContext} |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 595 | m.AddProperties(&m.properties) |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 596 | m.AddProperties(&m.fileProperties) |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 597 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 598 | return m |
| 599 | } |
| 600 | |
| 601 | // property_contexts_test tests given property_contexts files with property_info_checker. |
| 602 | func propertyContextsTestFactory() android.Module { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 603 | m := &contextsTestModule{context: PropertyContext} |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 604 | m.AddProperties(&m.properties) |
| 605 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 606 | return m |
| 607 | } |
| 608 | |
| 609 | // hwservice_contexts_test tests given hwservice_contexts files with checkfc. |
| 610 | func hwserviceContextsTestFactory() android.Module { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 611 | m := &contextsTestModule{context: HwServiceContext} |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 612 | m.AddProperties(&m.properties) |
| 613 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 614 | return m |
| 615 | } |
| 616 | |
| 617 | // service_contexts_test tests given service_contexts files with checkfc. |
| 618 | func serviceContextsTestFactory() android.Module { |
| 619 | // checkfc -s: service_contexts test |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 620 | m := &contextsTestModule{context: ServiceContext} |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 621 | m.AddProperties(&m.properties) |
| 622 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 623 | return m |
| 624 | } |
| 625 | |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 626 | // vndservice_contexts_test tests given vndservice_contexts files with checkfc. |
| 627 | func vndServiceContextsTestFactory() android.Module { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 628 | m := &contextsTestModule{context: VndServiceContext} |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 629 | m.AddProperties(&m.properties) |
| 630 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 631 | return m |
| 632 | } |
| 633 | |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 634 | func (m *contextsTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 635 | tool := "checkfc" |
| 636 | if m.context == PropertyContext { |
| 637 | tool = "property_info_checker" |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | if len(m.properties.Srcs) == 0 { |
| 641 | ctx.PropertyErrorf("srcs", "can't be empty") |
| 642 | return |
| 643 | } |
| 644 | |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 645 | validateWithPolicy := true |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 646 | if proptools.String(m.properties.Sepolicy) == "" { |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 647 | if m.context == FileContext { |
| 648 | if proptools.String(m.fileProperties.Test_data) == "" { |
| 649 | ctx.PropertyErrorf("test_data", "Either test_data or sepolicy should be provided") |
| 650 | return |
| 651 | } |
| 652 | validateWithPolicy = false |
| 653 | } else { |
| 654 | ctx.PropertyErrorf("sepolicy", "can't be empty") |
| 655 | return |
| 656 | } |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 659 | flags := []string(nil) |
| 660 | switch m.context { |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 661 | case FileContext: |
| 662 | if !validateWithPolicy { |
| 663 | flags = []string{"-t"} |
| 664 | } |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 665 | case ServiceContext: |
| 666 | flags = []string{"-s" /* binder services */} |
| 667 | case HwServiceContext: |
| 668 | flags = []string{"-e" /* allow empty */, "-l" /* hwbinder services */} |
| 669 | case VndServiceContext: |
| 670 | flags = []string{"-e" /* allow empty */, "-v" /* vnd service */} |
| 671 | } |
| 672 | |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 673 | srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs) |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 674 | rule := android.NewRuleBuilder(pctx, ctx) |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 675 | |
| 676 | if validateWithPolicy { |
| 677 | sepolicy := android.PathForModuleSrc(ctx, proptools.String(m.properties.Sepolicy)) |
| 678 | rule.Command().BuiltTool(tool). |
| 679 | Flags(flags). |
| 680 | Input(sepolicy). |
| 681 | Inputs(srcs) |
| 682 | } else { |
| 683 | test_data := android.PathForModuleSrc(ctx, proptools.String(m.fileProperties.Test_data)) |
| 684 | rule.Command().BuiltTool(tool). |
| 685 | Flags(flags). |
| 686 | Inputs(srcs). |
| 687 | Input(test_data) |
| 688 | } |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 689 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 690 | m.testTimestamp = pathForModuleOut(ctx, "timestamp") |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 691 | rule.Command().Text("touch").Output(m.testTimestamp) |
| 692 | rule.Build("contexts_test", "running contexts test: "+ctx.ModuleName()) |
| 693 | } |
| 694 | |
| 695 | func (m *contextsTestModule) AndroidMkEntries() []android.AndroidMkEntries { |
| 696 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 697 | Class: "FAKE", |
| 698 | // OutputFile is needed, even though BUILD_PHONY_PACKAGE doesn't use it. |
| 699 | // Without OutputFile this module won't be exported to Makefile. |
| 700 | OutputFile: android.OptionalPathForPath(m.testTimestamp), |
| 701 | Include: "$(BUILD_PHONY_PACKAGE)", |
| 702 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 703 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 704 | entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", m.testTimestamp.String()) |
| 705 | }, |
| 706 | }, |
| 707 | }} |
| 708 | } |
| 709 | |
| 710 | // contextsTestModule implements ImageInterface to be able to include recovery_available contexts |
| 711 | // modules as its sources. |
| 712 | func (m *contextsTestModule) ImageMutatorBegin(ctx android.BaseModuleContext) { |
| 713 | } |
| 714 | |
| 715 | func (m *contextsTestModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
| 716 | return true |
| 717 | } |
| 718 | |
| 719 | func (m *contextsTestModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 720 | return false |
| 721 | } |
| 722 | |
| 723 | func (m *contextsTestModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 724 | return false |
| 725 | } |
| 726 | |
| 727 | func (m *contextsTestModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 728 | return false |
| 729 | } |
| 730 | |
| 731 | func (m *contextsTestModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool { |
| 732 | return false |
| 733 | } |
| 734 | |
| 735 | func (m *contextsTestModule) ExtraImageVariations(ctx android.BaseModuleContext) []string { |
| 736 | return nil |
| 737 | } |
| 738 | |
| 739 | func (m *contextsTestModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { |
| 740 | } |
| 741 | |
| 742 | var _ android.ImageInterface = (*contextsTestModule)(nil) |