xref: /aosp_15_r20/build/soong/java/fuzz.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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
15package java
16
17import (
18	"path/filepath"
19	"sort"
20	"strings"
21
22	"android/soong/android"
23	"android/soong/cc"
24	"android/soong/fuzz"
25
26	"github.com/google/blueprint"
27	"github.com/google/blueprint/proptools"
28)
29
30const (
31	hostString   = "host"
32	targetString = "target"
33	deviceString = "device"
34)
35
36// Any shared libs for these deps will also be packaged
37var artDeps = []string{"libdl_android"}
38
39func init() {
40	RegisterJavaFuzzBuildComponents(android.InitRegistrationContext)
41}
42
43func RegisterJavaFuzzBuildComponents(ctx android.RegistrationContext) {
44	ctx.RegisterModuleType("java_fuzz", JavaFuzzFactory)
45	ctx.RegisterParallelSingletonType("java_fuzz_packaging", javaFuzzPackagingFactory)
46}
47
48type JavaFuzzTest struct {
49	Test
50	fuzzPackagedModule fuzz.FuzzPackagedModule
51	jniFilePaths       android.Paths
52}
53
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)
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)
67	module.Module.sourceProperties.Test_only = proptools.BoolPtr(true)
68	module.Module.sourceProperties.Top_level_test_target = true
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
84}
85
86func (j *JavaFuzzTest) DepsMutator(ctx android.BottomUpMutatorContext) {
87	if j.Os().Class.String() == deviceString {
88		j.testProperties.Jni_libs.AppendSimpleValue(artDeps)
89	}
90
91	jniLibs := j.testProperties.Jni_libs.GetOrDefault(ctx, nil)
92	if len(jniLibs) > 0 {
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)
100		for _, target := range ctx.MultiTargets() {
101			sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
102			ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, jniLibs...)
103		}
104	}
105
106	j.deps(ctx)
107}
108
109func (j *JavaFuzzTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
110	if j.fuzzPackagedModule.FuzzProperties.Corpus != nil {
111		j.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, j.fuzzPackagedModule.FuzzProperties.Corpus)
112	}
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	}
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	}
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	}
127
128	_, sharedDeps := cc.CollectAllSharedDependencies(ctx)
129	for _, dep := range sharedDeps {
130		sharedLibInfo, _ := android.OtherModuleProvider(ctx, dep, cc.SharedLibraryInfoProvider)
131		if sharedLibInfo.SharedLibrary != nil {
132			arch := "lib"
133			if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
134				arch = "lib64"
135			}
136
137			libPath := android.PathForModuleOut(ctx, filepath.Join(arch, sharedLibInfo.SharedLibrary.Base()))
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		}
147
148	}
149
150	j.Test.GenerateAndroidBuildActions(ctx)
151}
152
153type javaFuzzPackager struct {
154	fuzz.FuzzPackager
155}
156
157func javaFuzzPackagingFactory() android.Singleton {
158	return &javaFuzzPackager{}
159}
160
161func (s *javaFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
162	// Map between each architecture + host/device combination.
163	archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
164
165	s.FuzzTargets = make(map[string]bool)
166	ctx.VisitAllModules(func(module android.Module) {
167		// Discard non-fuzz targets.
168		javaFuzzModule, ok := module.(*JavaFuzzTest)
169		if !ok {
170			return
171		}
172
173		hostOrTargetString := "target"
174		if javaFuzzModule.Target().HostCross {
175			hostOrTargetString = "host_cross"
176		} else if javaFuzzModule.Host() {
177			hostOrTargetString = "host"
178		}
179
180		fuzzModuleValidator := fuzz.FuzzModule{
181			javaFuzzModule.ModuleBase,
182			javaFuzzModule.DefaultableModuleBase,
183			javaFuzzModule.ApexModuleBase,
184		}
185
186		if ok := fuzz.IsValid(ctx, fuzzModuleValidator); !ok {
187			return
188		}
189
190		archString := javaFuzzModule.Arch().ArchType.String()
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
197		// Package the artifacts (data, corpus, config and dictionary) into a zipfile.
198		files = s.PackageArtifacts(ctx, module, javaFuzzModule.fuzzPackagedModule, archDir, builder)
199
200		// Add .jar
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})
206
207		// Add jni .so files
208		for _, fPath := range javaFuzzModule.jniFilePaths {
209			files = append(files, fuzz.FileToZip{SourceFilePath: fPath})
210		}
211
212		archDirs[archOs], ok = s.BuildZipFile(ctx, module, javaFuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
213		if !ok {
214			return
215		}
216	})
217	s.CreateFuzzPackage(ctx, archDirs, fuzz.Java, pctx)
218}
219
220func (s *javaFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
221	packages := s.Packages.Strings()
222	sort.Strings(packages)
223	ctx.Strict("SOONG_JAVA_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
224	// Preallocate the slice of fuzz targets to minimize memory allocations.
225	s.PreallocateSlice(ctx, "ALL_JAVA_FUZZ_TARGETS")
226}
227