1*e4a36f41SAndroid Build Coastguard Worker// Copyright (C) 2022 The Android Open Source Project 2*e4a36f41SAndroid Build Coastguard Worker// 3*e4a36f41SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*e4a36f41SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*e4a36f41SAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*e4a36f41SAndroid Build Coastguard Worker// 7*e4a36f41SAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*e4a36f41SAndroid Build Coastguard Worker// 9*e4a36f41SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*e4a36f41SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*e4a36f41SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*e4a36f41SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*e4a36f41SAndroid Build Coastguard Worker// limitations under the License. 14*e4a36f41SAndroid Build Coastguard Worker 15*e4a36f41SAndroid Build Coastguard Workerpackage selinux 16*e4a36f41SAndroid Build Coastguard Worker 17*e4a36f41SAndroid Build Coastguard Workerimport ( 18*e4a36f41SAndroid Build Coastguard Worker "encoding/json" 19*e4a36f41SAndroid Build Coastguard Worker "fmt" 20*e4a36f41SAndroid Build Coastguard Worker 21*e4a36f41SAndroid Build Coastguard Worker "android/soong/android" 22*e4a36f41SAndroid Build Coastguard Worker) 23*e4a36f41SAndroid Build Coastguard Worker 24*e4a36f41SAndroid Build Coastguard Workerfunc init() { 25*e4a36f41SAndroid Build Coastguard Worker android.RegisterModuleType("fuzzer_bindings_test", fuzzerBindingsTestFactory) 26*e4a36f41SAndroid Build Coastguard Worker android.PreArchMutators(registerFuzzerMutators) 27*e4a36f41SAndroid Build Coastguard Worker} 28*e4a36f41SAndroid Build Coastguard Worker 29*e4a36f41SAndroid Build Coastguard Workerfunc registerFuzzerMutators(ctx android.RegisterMutatorsContext) { 30*e4a36f41SAndroid Build Coastguard Worker ctx.BottomUp("addFuzzerConfigDeps", addFuzzerConfigDeps).Parallel() 31*e4a36f41SAndroid Build Coastguard Worker} 32*e4a36f41SAndroid Build Coastguard Worker 33*e4a36f41SAndroid Build Coastguard Workerfunc addFuzzerConfigDeps(ctx android.BottomUpMutatorContext) { 34*e4a36f41SAndroid Build Coastguard Worker if _, ok := ctx.Module().(*fuzzerBindingsTestModule); ok { 35*e4a36f41SAndroid Build Coastguard Worker for _, fuzzers := range ServiceFuzzerBindings { 36*e4a36f41SAndroid Build Coastguard Worker for _, fuzzer := range fuzzers { 37*e4a36f41SAndroid Build Coastguard Worker if !ctx.OtherModuleExists(fuzzer) && !ctx.Config().AllowMissingDependencies() { 38*e4a36f41SAndroid Build Coastguard Worker panic(fmt.Errorf("Fuzzer doesn't exist : %s", fuzzer)) 39*e4a36f41SAndroid Build Coastguard Worker } 40*e4a36f41SAndroid Build Coastguard Worker } 41*e4a36f41SAndroid Build Coastguard Worker } 42*e4a36f41SAndroid Build Coastguard Worker } 43*e4a36f41SAndroid Build Coastguard Worker} 44*e4a36f41SAndroid Build Coastguard Worker 45*e4a36f41SAndroid Build Coastguard Workertype bindingsTestProperties struct { 46*e4a36f41SAndroid Build Coastguard Worker // Contexts files to be tested. 47*e4a36f41SAndroid Build Coastguard Worker Srcs []string `android:"path"` 48*e4a36f41SAndroid Build Coastguard Worker} 49*e4a36f41SAndroid Build Coastguard Worker 50*e4a36f41SAndroid Build Coastguard Workertype fuzzerBindingsTestModule struct { 51*e4a36f41SAndroid Build Coastguard Worker android.ModuleBase 52*e4a36f41SAndroid Build Coastguard Worker tool string 53*e4a36f41SAndroid Build Coastguard Worker properties bindingsTestProperties 54*e4a36f41SAndroid Build Coastguard Worker testTimestamp android.ModuleOutPath 55*e4a36f41SAndroid Build Coastguard Worker} 56*e4a36f41SAndroid Build Coastguard Worker 57*e4a36f41SAndroid Build Coastguard Worker// fuzzer_bindings_test checks if a fuzzer is implemented for every service in service_contexts 58*e4a36f41SAndroid Build Coastguard Workerfunc fuzzerBindingsTestFactory() android.Module { 59*e4a36f41SAndroid Build Coastguard Worker m := &fuzzerBindingsTestModule{tool: "fuzzer_bindings_check"} 60*e4a36f41SAndroid Build Coastguard Worker m.AddProperties(&m.properties) 61*e4a36f41SAndroid Build Coastguard Worker android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) 62*e4a36f41SAndroid Build Coastguard Worker return m 63*e4a36f41SAndroid Build Coastguard Worker} 64*e4a36f41SAndroid Build Coastguard Worker 65*e4a36f41SAndroid Build Coastguard Workerfunc (m *fuzzerBindingsTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { 66*e4a36f41SAndroid Build Coastguard Worker tool := m.tool 67*e4a36f41SAndroid Build Coastguard Worker if tool != "fuzzer_bindings_check" { 68*e4a36f41SAndroid Build Coastguard Worker panic(fmt.Errorf("%q: unknown tool name: %q", ctx.ModuleName(), tool)) 69*e4a36f41SAndroid Build Coastguard Worker } 70*e4a36f41SAndroid Build Coastguard Worker 71*e4a36f41SAndroid Build Coastguard Worker if len(m.properties.Srcs) == 0 { 72*e4a36f41SAndroid Build Coastguard Worker ctx.PropertyErrorf("srcs", "can't be empty") 73*e4a36f41SAndroid Build Coastguard Worker return 74*e4a36f41SAndroid Build Coastguard Worker } 75*e4a36f41SAndroid Build Coastguard Worker 76*e4a36f41SAndroid Build Coastguard Worker // Generate a json file which contains existing bindings 77*e4a36f41SAndroid Build Coastguard Worker rootPath := android.PathForIntermediates(ctx, "bindings.json") 78*e4a36f41SAndroid Build Coastguard Worker jsonString, parseError := json.Marshal(ServiceFuzzerBindings) 79*e4a36f41SAndroid Build Coastguard Worker if parseError != nil { 80*e4a36f41SAndroid Build Coastguard Worker panic(fmt.Errorf("Error while marshalling ServiceFuzzerBindings dict. Check Format")) 81*e4a36f41SAndroid Build Coastguard Worker } 82*e4a36f41SAndroid Build Coastguard Worker android.WriteFileRule(ctx, rootPath, string(jsonString)) 83*e4a36f41SAndroid Build Coastguard Worker 84*e4a36f41SAndroid Build Coastguard Worker //input module json, service context and binding files here 85*e4a36f41SAndroid Build Coastguard Worker srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs) 86*e4a36f41SAndroid Build Coastguard Worker rule := android.NewRuleBuilder(pctx, ctx) 87*e4a36f41SAndroid Build Coastguard Worker 88*e4a36f41SAndroid Build Coastguard Worker rule.Command().BuiltTool(tool).Flag("-s").Inputs(srcs).Flag("-b").Input(rootPath) 89*e4a36f41SAndroid Build Coastguard Worker 90*e4a36f41SAndroid Build Coastguard Worker // Every Soong module needs to generate an output even if it doesn't require it 91*e4a36f41SAndroid Build Coastguard Worker m.testTimestamp = android.PathForModuleOut(ctx, "timestamp") 92*e4a36f41SAndroid Build Coastguard Worker rule.Command().Text("touch").Output(m.testTimestamp) 93*e4a36f41SAndroid Build Coastguard Worker rule.Build("fuzzer_bindings_test", "running service:fuzzer bindings test: "+ctx.ModuleName()) 94*e4a36f41SAndroid Build Coastguard Worker} 95*e4a36f41SAndroid Build Coastguard Worker 96*e4a36f41SAndroid Build Coastguard Workerfunc (m *fuzzerBindingsTestModule) AndroidMkEntries() []android.AndroidMkEntries { 97*e4a36f41SAndroid Build Coastguard Worker return []android.AndroidMkEntries{android.AndroidMkEntries{ 98*e4a36f41SAndroid Build Coastguard Worker Class: "FAKE", 99*e4a36f41SAndroid Build Coastguard Worker // OutputFile is needed, even though BUILD_PHONY_PACKAGE doesn't use it. 100*e4a36f41SAndroid Build Coastguard Worker // Without OutputFile this module won't be exported to Makefile. 101*e4a36f41SAndroid Build Coastguard Worker OutputFile: android.OptionalPathForPath(m.testTimestamp), 102*e4a36f41SAndroid Build Coastguard Worker Include: "$(BUILD_PHONY_PACKAGE)", 103*e4a36f41SAndroid Build Coastguard Worker ExtraEntries: []android.AndroidMkExtraEntriesFunc{ 104*e4a36f41SAndroid Build Coastguard Worker func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { 105*e4a36f41SAndroid Build Coastguard Worker entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", m.testTimestamp.String()) 106*e4a36f41SAndroid Build Coastguard Worker }, 107*e4a36f41SAndroid Build Coastguard Worker }, 108*e4a36f41SAndroid Build Coastguard Worker }} 109*e4a36f41SAndroid Build Coastguard Worker} 110