blob: 4aa6dbffddf59598043c0dad4d8affb9a8070cd1 [file] [log] [blame]
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +00001// 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
15package java
16
17import (
Muhammad Haseeb Ahmadeb14ff22022-09-28 17:53:11 +000018 "path/filepath"
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +000019 "sort"
20 "strings"
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000021
22 "android/soong/android"
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000023 "android/soong/cc"
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000024 "android/soong/fuzz"
Cory Barkereaf7f5e2023-02-03 00:20:52 +000025
26 "github.com/google/blueprint"
27 "github.com/google/blueprint/proptools"
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000028)
29
Muhammad Haseeb Ahmadeb14ff22022-09-28 17:53:11 +000030const (
31 hostString = "host"
32 targetString = "target"
33)
34
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000035func init() {
36 RegisterJavaFuzzBuildComponents(android.InitRegistrationContext)
37}
38
39func RegisterJavaFuzzBuildComponents(ctx android.RegistrationContext) {
Muhammad Haseeb Ahmadeb14ff22022-09-28 17:53:11 +000040 ctx.RegisterModuleType("java_fuzz", JavaFuzzFactory)
Cory Barkereaf7f5e2023-02-03 00:20:52 +000041 ctx.RegisterSingletonType("java_fuzz_packaging", javaFuzzPackagingFactory)
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000042}
43
Cory Barkereaf7f5e2023-02-03 00:20:52 +000044type JavaFuzzTest struct {
45 Test
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000046 fuzzPackagedModule fuzz.FuzzPackagedModule
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000047 jniFilePaths android.Paths
48}
49
Cory Barkereaf7f5e2023-02-03 00:20:52 +000050// java_fuzz builds and links sources into a `.jar` file for the device.
51// This generates .class files in a jar which can then be instrumented before
52// fuzzing in Android Runtime (ART: Android OS on emulator or device)
53func JavaFuzzFactory() android.Module {
54 module := &JavaFuzzTest{}
55
56 module.addHostAndDeviceProperties()
57 module.AddProperties(&module.testProperties)
58 module.AddProperties(&module.fuzzPackagedModule.FuzzProperties)
59
60 module.Module.properties.Installable = proptools.BoolPtr(true)
61 module.Module.dexpreopter.isTest = true
62 module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true)
63
64 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
65 disableLinuxBionic := struct {
66 Target struct {
67 Linux_bionic struct {
68 Enabled *bool
69 }
70 }
71 }{}
72 disableLinuxBionic.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
73 ctx.AppendProperties(&disableLinuxBionic)
74 })
75
76 InitJavaModuleMultiTargets(module, android.HostAndDeviceSupported)
77 return module
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000078}
79
Cory Barkereaf7f5e2023-02-03 00:20:52 +000080func (j *JavaFuzzTest) DepsMutator(ctx android.BottomUpMutatorContext) {
81 if len(j.testProperties.Jni_libs) > 0 {
82 for _, target := range ctx.MultiTargets() {
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000083 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
Cory Barkereaf7f5e2023-02-03 00:20:52 +000084 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000085 }
86 }
Cory Barkereaf7f5e2023-02-03 00:20:52 +000087
88 j.deps(ctx)
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000089}
90
Cory Barkereaf7f5e2023-02-03 00:20:52 +000091func (j *JavaFuzzTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000092 if j.fuzzPackagedModule.FuzzProperties.Corpus != nil {
93 j.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, j.fuzzPackagedModule.FuzzProperties.Corpus)
94 }
95 if j.fuzzPackagedModule.FuzzProperties.Data != nil {
96 j.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, j.fuzzPackagedModule.FuzzProperties.Data)
97 }
98 if j.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
99 j.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *j.fuzzPackagedModule.FuzzProperties.Dictionary)
100 }
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +0000101 if j.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
102 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
103 android.WriteFileRule(ctx, configPath, j.fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
104 j.fuzzPackagedModule.Config = configPath
105 }
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000106
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000107 _, sharedDeps := cc.CollectAllSharedDependencies(ctx)
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000108 for _, dep := range sharedDeps {
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000109 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
110 if sharedLibInfo.SharedLibrary != nil {
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000111 arch := "lib"
Muhammad Haseeb Ahmadeb14ff22022-09-28 17:53:11 +0000112 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000113 arch = "lib64"
Muhammad Haseeb Ahmadeb14ff22022-09-28 17:53:11 +0000114 }
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000115
116 libPath := android.PathForModuleOut(ctx, filepath.Join(arch, sharedLibInfo.SharedLibrary.Base()))
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000117 ctx.Build(pctx, android.BuildParams{
118 Rule: android.Cp,
119 Input: sharedLibInfo.SharedLibrary,
120 Output: libPath,
121 })
122 j.jniFilePaths = append(j.jniFilePaths, libPath)
123 } else {
124 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
125 }
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000126
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000127 }
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000128
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000129 j.Test.GenerateAndroidBuildActions(ctx)
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +0000130}
131
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000132type javaFuzzPackager struct {
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000133 fuzz.FuzzPackager
134}
135
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000136func javaFuzzPackagingFactory() android.Singleton {
137 return &javaFuzzPackager{}
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000138}
139
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000140func (s *javaFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000141 // Map between each architecture + host/device combination.
142 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
143
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000144 s.FuzzTargets = make(map[string]bool)
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000145 ctx.VisitAllModules(func(module android.Module) {
146 // Discard non-fuzz targets.
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000147 javaFuzzModule, ok := module.(*JavaFuzzTest)
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000148 if !ok {
149 return
150 }
151
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000152 hostOrTargetString := "target"
153 if javaFuzzModule.Host() {
154 hostOrTargetString = "host"
Colin Cross39a18142022-06-24 18:43:40 -0700155 }
156
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000157 fuzzModuleValidator := fuzz.FuzzModule{
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000158 javaFuzzModule.ModuleBase,
159 javaFuzzModule.DefaultableModuleBase,
160 javaFuzzModule.ApexModuleBase,
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000161 }
162
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000163 if ok := fuzz.IsValid(fuzzModuleValidator); !ok {
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000164 return
165 }
166
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000167 archString := javaFuzzModule.Arch().ArchType.String()
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000168 archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
169 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
170
171 var files []fuzz.FileToZip
172 builder := android.NewRuleBuilder(pctx, ctx)
173
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000174 // Package the artifacts (data, corpus, config and dictionary) into a zipfile.
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000175 files = s.PackageArtifacts(ctx, module, javaFuzzModule.fuzzPackagedModule, archDir, builder)
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000176
177 // Add .jar
Colin Cross80462dc2023-05-08 15:09:31 -0700178 files = append(files, fuzz.FileToZip{SourceFilePath: javaFuzzModule.implementationJarFile})
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000179
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000180 // Add jni .so files
181 for _, fPath := range javaFuzzModule.jniFilePaths {
Colin Cross80462dc2023-05-08 15:09:31 -0700182 files = append(files, fuzz.FileToZip{SourceFilePath: fPath})
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000183 }
184
185 archDirs[archOs], ok = s.BuildZipFile(ctx, module, javaFuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000186 if !ok {
187 return
188 }
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000189 })
190 s.CreateFuzzPackage(ctx, archDirs, fuzz.Java, pctx)
191}
192
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000193func (s *javaFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000194 packages := s.Packages.Strings()
195 sort.Strings(packages)
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000196 ctx.Strict("SOONG_JAVA_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000197 // Preallocate the slice of fuzz targets to minimize memory allocations.
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000198 s.PreallocateSlice(ctx, "ALL_JAVA_FUZZ_TARGETS")
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000199}