| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 1 | // Copyright 2021 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 java | 
|  | 16 |  | 
|  | 17 | import ( | 
| Muhammad Haseeb Ahmad | eb14ff2 | 2022-09-28 17:53:11 +0000 | [diff] [blame] | 18 | "path/filepath" | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 19 | "sort" | 
|  | 20 | "strings" | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 21 |  | 
|  | 22 | "android/soong/android" | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 23 | "android/soong/cc" | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 24 | "android/soong/fuzz" | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 25 |  | 
|  | 26 | "github.com/google/blueprint" | 
|  | 27 | "github.com/google/blueprint/proptools" | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 28 | ) | 
|  | 29 |  | 
| Muhammad Haseeb Ahmad | eb14ff2 | 2022-09-28 17:53:11 +0000 | [diff] [blame] | 30 | const ( | 
|  | 31 | hostString   = "host" | 
|  | 32 | targetString = "target" | 
| Mark | f736b92 | 2023-05-08 22:11:44 +0000 | [diff] [blame] | 33 | deviceString = "device" | 
| Muhammad Haseeb Ahmad | eb14ff2 | 2022-09-28 17:53:11 +0000 | [diff] [blame] | 34 | ) | 
|  | 35 |  | 
| Mark | f736b92 | 2023-05-08 22:11:44 +0000 | [diff] [blame] | 36 | // Any shared libs for these deps will also be packaged | 
|  | 37 | var artDeps = []string{"libdl_android"} | 
|  | 38 |  | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 39 | func init() { | 
|  | 40 | RegisterJavaFuzzBuildComponents(android.InitRegistrationContext) | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | func RegisterJavaFuzzBuildComponents(ctx android.RegistrationContext) { | 
| Muhammad Haseeb Ahmad | eb14ff2 | 2022-09-28 17:53:11 +0000 | [diff] [blame] | 44 | ctx.RegisterModuleType("java_fuzz", JavaFuzzFactory) | 
| LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 45 | ctx.RegisterParallelSingletonType("java_fuzz_packaging", javaFuzzPackagingFactory) | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 46 | } | 
|  | 47 |  | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 48 | type JavaFuzzTest struct { | 
|  | 49 | Test | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 50 | fuzzPackagedModule fuzz.FuzzPackagedModule | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 51 | jniFilePaths       android.Paths | 
|  | 52 | } | 
|  | 53 |  | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 54 | // java_fuzz builds and links sources into a `.jar` file for the device. | 
|  | 55 | // This generates .class files in a jar which can then be instrumented before | 
|  | 56 | // fuzzing in Android Runtime (ART: Android OS on emulator or device) | 
|  | 57 | func JavaFuzzFactory() android.Module { | 
|  | 58 | module := &JavaFuzzTest{} | 
|  | 59 |  | 
|  | 60 | module.addHostAndDeviceProperties() | 
|  | 61 | module.AddProperties(&module.testProperties) | 
|  | 62 | module.AddProperties(&module.fuzzPackagedModule.FuzzProperties) | 
|  | 63 |  | 
|  | 64 | module.Module.properties.Installable = proptools.BoolPtr(true) | 
|  | 65 | module.Module.dexpreopter.isTest = true | 
|  | 66 | module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true) | 
| Ronald Braunstein | cdc66f4 | 2024-04-12 11:23:19 -0700 | [diff] [blame] | 67 | module.Module.sourceProperties.Test_only = proptools.BoolPtr(true) | 
|  | 68 | module.Module.sourceProperties.Top_level_test_target = true | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 69 |  | 
|  | 70 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { | 
|  | 71 | disableLinuxBionic := struct { | 
|  | 72 | Target struct { | 
|  | 73 | Linux_bionic struct { | 
|  | 74 | Enabled *bool | 
|  | 75 | } | 
|  | 76 | } | 
|  | 77 | }{} | 
|  | 78 | disableLinuxBionic.Target.Linux_bionic.Enabled = proptools.BoolPtr(false) | 
|  | 79 | ctx.AppendProperties(&disableLinuxBionic) | 
|  | 80 | }) | 
|  | 81 |  | 
|  | 82 | InitJavaModuleMultiTargets(module, android.HostAndDeviceSupported) | 
|  | 83 | return module | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 86 | func (j *JavaFuzzTest) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Mark | f736b92 | 2023-05-08 22:11:44 +0000 | [diff] [blame] | 87 | if j.Os().Class.String() == deviceString { | 
| Jihoon Kang | 6d39c70 | 2024-09-30 20:50:38 +0000 | [diff] [blame] | 88 | j.testProperties.Jni_libs.AppendSimpleValue(artDeps) | 
| Mark | f736b92 | 2023-05-08 22:11:44 +0000 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
| Jihoon Kang | 6d39c70 | 2024-09-30 20:50:38 +0000 | [diff] [blame] | 91 | jniLibs := j.testProperties.Jni_libs.GetOrDefault(ctx, nil) | 
|  | 92 | if len(jniLibs) > 0 { | 
| Mark | f736b92 | 2023-05-08 22:11:44 +0000 | [diff] [blame] | 93 | if j.fuzzPackagedModule.FuzzProperties.Fuzz_config == nil { | 
|  | 94 | config := &fuzz.FuzzConfig{} | 
|  | 95 | j.fuzzPackagedModule.FuzzProperties.Fuzz_config = config | 
|  | 96 | } | 
|  | 97 | // this will be used by the ingestion pipeline to determine the version | 
|  | 98 | // of jazzer to add to the fuzzer package | 
|  | 99 | j.fuzzPackagedModule.FuzzProperties.Fuzz_config.IsJni = proptools.BoolPtr(true) | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 100 | for _, target := range ctx.MultiTargets() { | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 101 | sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"}) | 
| Jihoon Kang | 6d39c70 | 2024-09-30 20:50:38 +0000 | [diff] [blame] | 102 | ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, jniLibs...) | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 103 | } | 
|  | 104 | } | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 105 |  | 
|  | 106 | j.deps(ctx) | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 109 | func (j *JavaFuzzTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 110 | if j.fuzzPackagedModule.FuzzProperties.Corpus != nil { | 
|  | 111 | j.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, j.fuzzPackagedModule.FuzzProperties.Corpus) | 
|  | 112 | } | 
| Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 113 | if j.fuzzPackagedModule.FuzzProperties.Device_common_corpus != nil { | 
|  | 114 | j.fuzzPackagedModule.Corpus = append(j.fuzzPackagedModule.Corpus, android.PathsForModuleSrc(ctx, j.fuzzPackagedModule.FuzzProperties.Device_common_corpus)...) | 
|  | 115 | } | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 116 | if j.fuzzPackagedModule.FuzzProperties.Data != nil { | 
|  | 117 | j.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, j.fuzzPackagedModule.FuzzProperties.Data) | 
|  | 118 | } | 
|  | 119 | if j.fuzzPackagedModule.FuzzProperties.Dictionary != nil { | 
|  | 120 | j.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *j.fuzzPackagedModule.FuzzProperties.Dictionary) | 
|  | 121 | } | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 122 | if j.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil { | 
|  | 123 | configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json") | 
|  | 124 | android.WriteFileRule(ctx, configPath, j.fuzzPackagedModule.FuzzProperties.Fuzz_config.String()) | 
|  | 125 | j.fuzzPackagedModule.Config = configPath | 
|  | 126 | } | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 127 |  | 
| Muhammad Haseeb Ahmad | 431ddf9 | 2022-10-20 00:55:58 +0000 | [diff] [blame] | 128 | _, sharedDeps := cc.CollectAllSharedDependencies(ctx) | 
| Muhammad Haseeb Ahmad | 431ddf9 | 2022-10-20 00:55:58 +0000 | [diff] [blame] | 129 | for _, dep := range sharedDeps { | 
| Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 130 | sharedLibInfo, _ := android.OtherModuleProvider(ctx, dep, cc.SharedLibraryInfoProvider) | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 131 | if sharedLibInfo.SharedLibrary != nil { | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 132 | arch := "lib" | 
| Muhammad Haseeb Ahmad | eb14ff2 | 2022-09-28 17:53:11 +0000 | [diff] [blame] | 133 | if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" { | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 134 | arch = "lib64" | 
| Muhammad Haseeb Ahmad | eb14ff2 | 2022-09-28 17:53:11 +0000 | [diff] [blame] | 135 | } | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 136 |  | 
|  | 137 | libPath := android.PathForModuleOut(ctx, filepath.Join(arch, sharedLibInfo.SharedLibrary.Base())) | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 138 | ctx.Build(pctx, android.BuildParams{ | 
|  | 139 | Rule:   android.Cp, | 
|  | 140 | Input:  sharedLibInfo.SharedLibrary, | 
|  | 141 | Output: libPath, | 
|  | 142 | }) | 
|  | 143 | j.jniFilePaths = append(j.jniFilePaths, libPath) | 
|  | 144 | } else { | 
|  | 145 | ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep)) | 
|  | 146 | } | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 147 |  | 
| Muhammad Haseeb Ahmad | 431ddf9 | 2022-10-20 00:55:58 +0000 | [diff] [blame] | 148 | } | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 149 |  | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 150 | j.Test.GenerateAndroidBuildActions(ctx) | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 153 | type javaFuzzPackager struct { | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 154 | fuzz.FuzzPackager | 
|  | 155 | } | 
|  | 156 |  | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 157 | func javaFuzzPackagingFactory() android.Singleton { | 
|  | 158 | return &javaFuzzPackager{} | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 161 | func (s *javaFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) { | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 162 | // Map between each architecture + host/device combination. | 
|  | 163 | archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip) | 
|  | 164 |  | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 165 | s.FuzzTargets = make(map[string]bool) | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 166 | ctx.VisitAllModules(func(module android.Module) { | 
|  | 167 | // Discard non-fuzz targets. | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 168 | javaFuzzModule, ok := module.(*JavaFuzzTest) | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 169 | if !ok { | 
|  | 170 | return | 
|  | 171 | } | 
|  | 172 |  | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 173 | hostOrTargetString := "target" | 
| Colin Cross | 64a4a5f | 2023-05-16 17:54:27 -0700 | [diff] [blame] | 174 | if javaFuzzModule.Target().HostCross { | 
|  | 175 | hostOrTargetString = "host_cross" | 
|  | 176 | } else if javaFuzzModule.Host() { | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 177 | hostOrTargetString = "host" | 
| Colin Cross | 39a1814 | 2022-06-24 18:43:40 -0700 | [diff] [blame] | 178 | } | 
|  | 179 |  | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 180 | fuzzModuleValidator := fuzz.FuzzModule{ | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 181 | javaFuzzModule.ModuleBase, | 
|  | 182 | javaFuzzModule.DefaultableModuleBase, | 
|  | 183 | javaFuzzModule.ApexModuleBase, | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 184 | } | 
|  | 185 |  | 
| Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 186 | if ok := fuzz.IsValid(ctx, fuzzModuleValidator); !ok { | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 187 | return | 
|  | 188 | } | 
|  | 189 |  | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 190 | archString := javaFuzzModule.Arch().ArchType.String() | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 191 | archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString) | 
|  | 192 | archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()} | 
|  | 193 |  | 
|  | 194 | var files []fuzz.FileToZip | 
|  | 195 | builder := android.NewRuleBuilder(pctx, ctx) | 
|  | 196 |  | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 197 | // Package the artifacts (data, corpus, config and dictionary) into a zipfile. | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 198 | files = s.PackageArtifacts(ctx, module, javaFuzzModule.fuzzPackagedModule, archDir, builder) | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 199 |  | 
|  | 200 | // Add .jar | 
| Cory Barker | c29c37a | 2023-05-24 22:53:41 +0000 | [diff] [blame] | 201 | if !javaFuzzModule.Host() { | 
|  | 202 | files = append(files, fuzz.FileToZip{SourceFilePath: javaFuzzModule.implementationJarFile, DestinationPathPrefix: "classes"}) | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | files = append(files, fuzz.FileToZip{SourceFilePath: javaFuzzModule.outputFile}) | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 206 |  | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 207 | // Add jni .so files | 
|  | 208 | for _, fPath := range javaFuzzModule.jniFilePaths { | 
| Colin Cross | 80462dc | 2023-05-08 15:09:31 -0700 | [diff] [blame] | 209 | files = append(files, fuzz.FileToZip{SourceFilePath: fPath}) | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 210 | } | 
|  | 211 |  | 
|  | 212 | archDirs[archOs], ok = s.BuildZipFile(ctx, module, javaFuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs) | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 213 | if !ok { | 
|  | 214 | return | 
|  | 215 | } | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 216 | }) | 
|  | 217 | s.CreateFuzzPackage(ctx, archDirs, fuzz.Java, pctx) | 
|  | 218 | } | 
|  | 219 |  | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 220 | func (s *javaFuzzPackager) MakeVars(ctx android.MakeVarsContext) { | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 221 | packages := s.Packages.Strings() | 
|  | 222 | sort.Strings(packages) | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 223 | ctx.Strict("SOONG_JAVA_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " ")) | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 224 | // Preallocate the slice of fuzz targets to minimize memory allocations. | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 225 | s.PreallocateSlice(ctx, "ALL_JAVA_FUZZ_TARGETS") | 
| Muhammad Haseeb Ahmad | e380310 | 2022-01-10 21:37:07 +0000 | [diff] [blame] | 226 | } |