blob: e5f1f04ee1aeae67f3886d07ace70e03ed8a0de6 [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"
Markf736b922023-05-08 22:11:44 +000033 deviceString = "device"
Muhammad Haseeb Ahmadeb14ff22022-09-28 17:53:11 +000034)
35
Markf736b922023-05-08 22:11:44 +000036// Any shared libs for these deps will also be packaged
37var artDeps = []string{"libdl_android"}
38
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000039func init() {
40 RegisterJavaFuzzBuildComponents(android.InitRegistrationContext)
41}
42
43func RegisterJavaFuzzBuildComponents(ctx android.RegistrationContext) {
Muhammad Haseeb Ahmadeb14ff22022-09-28 17:53:11 +000044 ctx.RegisterModuleType("java_fuzz", JavaFuzzFactory)
LaMont Jones0c10e4d2023-05-16 00:58:37 +000045 ctx.RegisterParallelSingletonType("java_fuzz_packaging", javaFuzzPackagingFactory)
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000046}
47
Cory Barkereaf7f5e2023-02-03 00:20:52 +000048type JavaFuzzTest struct {
49 Test
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000050 fuzzPackagedModule fuzz.FuzzPackagedModule
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000051 jniFilePaths android.Paths
52}
53
Cory Barkereaf7f5e2023-02-03 00:20:52 +000054// 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)
57func 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 Braunsteincdc66f42024-04-12 11:23:19 -070067 module.Module.sourceProperties.Test_only = proptools.BoolPtr(true)
68 module.Module.sourceProperties.Top_level_test_target = true
Cory Barkereaf7f5e2023-02-03 00:20:52 +000069
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 Ahmad7e744052022-03-25 22:50:53 +000084}
85
Cory Barkereaf7f5e2023-02-03 00:20:52 +000086func (j *JavaFuzzTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Markf736b922023-05-08 22:11:44 +000087 if j.Os().Class.String() == deviceString {
Jihoon Kang6d39c702024-09-30 20:50:38 +000088 j.testProperties.Jni_libs.AppendSimpleValue(artDeps)
Markf736b922023-05-08 22:11:44 +000089 }
90
Jihoon Kang6d39c702024-09-30 20:50:38 +000091 jniLibs := j.testProperties.Jni_libs.GetOrDefault(ctx, nil)
92 if len(jniLibs) > 0 {
Markf736b922023-05-08 22:11:44 +000093 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 Barkereaf7f5e2023-02-03 00:20:52 +0000100 for _, target := range ctx.MultiTargets() {
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000101 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
Jihoon Kang6d39c702024-09-30 20:50:38 +0000102 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, jniLibs...)
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000103 }
104 }
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000105
106 j.deps(ctx)
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +0000107}
108
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000109func (j *JavaFuzzTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +0000110 if j.fuzzPackagedModule.FuzzProperties.Corpus != nil {
111 j.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, j.fuzzPackagedModule.FuzzProperties.Corpus)
112 }
113 if j.fuzzPackagedModule.FuzzProperties.Data != nil {
114 j.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, j.fuzzPackagedModule.FuzzProperties.Data)
115 }
116 if j.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
117 j.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *j.fuzzPackagedModule.FuzzProperties.Dictionary)
118 }
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +0000119 if j.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
120 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
121 android.WriteFileRule(ctx, configPath, j.fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
122 j.fuzzPackagedModule.Config = configPath
123 }
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000124
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000125 _, sharedDeps := cc.CollectAllSharedDependencies(ctx)
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000126 for _, dep := range sharedDeps {
Colin Cross313aa542023-12-13 13:47:44 -0800127 sharedLibInfo, _ := android.OtherModuleProvider(ctx, dep, cc.SharedLibraryInfoProvider)
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000128 if sharedLibInfo.SharedLibrary != nil {
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000129 arch := "lib"
Muhammad Haseeb Ahmadeb14ff22022-09-28 17:53:11 +0000130 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000131 arch = "lib64"
Muhammad Haseeb Ahmadeb14ff22022-09-28 17:53:11 +0000132 }
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000133
134 libPath := android.PathForModuleOut(ctx, filepath.Join(arch, sharedLibInfo.SharedLibrary.Base()))
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000135 ctx.Build(pctx, android.BuildParams{
136 Rule: android.Cp,
137 Input: sharedLibInfo.SharedLibrary,
138 Output: libPath,
139 })
140 j.jniFilePaths = append(j.jniFilePaths, libPath)
141 } else {
142 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
143 }
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000144
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000145 }
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000146
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000147 j.Test.GenerateAndroidBuildActions(ctx)
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +0000148}
149
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000150type javaFuzzPackager struct {
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000151 fuzz.FuzzPackager
152}
153
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000154func javaFuzzPackagingFactory() android.Singleton {
155 return &javaFuzzPackager{}
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000156}
157
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000158func (s *javaFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000159 // Map between each architecture + host/device combination.
160 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
161
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000162 s.FuzzTargets = make(map[string]bool)
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000163 ctx.VisitAllModules(func(module android.Module) {
164 // Discard non-fuzz targets.
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000165 javaFuzzModule, ok := module.(*JavaFuzzTest)
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000166 if !ok {
167 return
168 }
169
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000170 hostOrTargetString := "target"
Colin Cross64a4a5f2023-05-16 17:54:27 -0700171 if javaFuzzModule.Target().HostCross {
172 hostOrTargetString = "host_cross"
173 } else if javaFuzzModule.Host() {
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000174 hostOrTargetString = "host"
Colin Cross39a18142022-06-24 18:43:40 -0700175 }
176
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000177 fuzzModuleValidator := fuzz.FuzzModule{
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000178 javaFuzzModule.ModuleBase,
179 javaFuzzModule.DefaultableModuleBase,
180 javaFuzzModule.ApexModuleBase,
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000181 }
182
Cole Fausta963b942024-04-11 17:43:00 -0700183 if ok := fuzz.IsValid(ctx, fuzzModuleValidator); !ok {
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000184 return
185 }
186
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000187 archString := javaFuzzModule.Arch().ArchType.String()
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000188 archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
189 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
190
191 var files []fuzz.FileToZip
192 builder := android.NewRuleBuilder(pctx, ctx)
193
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000194 // Package the artifacts (data, corpus, config and dictionary) into a zipfile.
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000195 files = s.PackageArtifacts(ctx, module, javaFuzzModule.fuzzPackagedModule, archDir, builder)
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000196
197 // Add .jar
Cory Barkerc29c37a2023-05-24 22:53:41 +0000198 if !javaFuzzModule.Host() {
199 files = append(files, fuzz.FileToZip{SourceFilePath: javaFuzzModule.implementationJarFile, DestinationPathPrefix: "classes"})
200 }
201
202 files = append(files, fuzz.FileToZip{SourceFilePath: javaFuzzModule.outputFile})
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000203
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000204 // Add jni .so files
205 for _, fPath := range javaFuzzModule.jniFilePaths {
Colin Cross80462dc2023-05-08 15:09:31 -0700206 files = append(files, fuzz.FileToZip{SourceFilePath: fPath})
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +0000207 }
208
209 archDirs[archOs], ok = s.BuildZipFile(ctx, module, javaFuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000210 if !ok {
211 return
212 }
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000213 })
214 s.CreateFuzzPackage(ctx, archDirs, fuzz.Java, pctx)
215}
216
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000217func (s *javaFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000218 packages := s.Packages.Strings()
219 sort.Strings(packages)
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000220 ctx.Strict("SOONG_JAVA_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000221 // Preallocate the slice of fuzz targets to minimize memory allocations.
Cory Barkereaf7f5e2023-02-03 00:20:52 +0000222 s.PreallocateSlice(ctx, "ALL_JAVA_FUZZ_TARGETS")
Muhammad Haseeb Ahmade3803102022-01-10 21:37:07 +0000223}