• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = append(j.testProperties.Jni_libs, artDeps...)
89	}
90
91	if len(j.testProperties.Jni_libs) > 0 {
92		if j.fuzzPackagedModule.FuzzProperties.Fuzz_config == nil {
93			config := &fuzz.FuzzConfig{}
94			j.fuzzPackagedModule.FuzzProperties.Fuzz_config = config
95		}
96		// this will be used by the ingestion pipeline to determine the version
97		// of jazzer to add to the fuzzer package
98		j.fuzzPackagedModule.FuzzProperties.Fuzz_config.IsJni = proptools.BoolPtr(true)
99		for _, target := range ctx.MultiTargets() {
100			sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
101			ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
102		}
103	}
104
105	j.deps(ctx)
106}
107
108func (j *JavaFuzzTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
109	if j.fuzzPackagedModule.FuzzProperties.Corpus != nil {
110		j.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, j.fuzzPackagedModule.FuzzProperties.Corpus)
111	}
112	if j.fuzzPackagedModule.FuzzProperties.Data != nil {
113		j.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, j.fuzzPackagedModule.FuzzProperties.Data)
114	}
115	if j.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
116		j.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *j.fuzzPackagedModule.FuzzProperties.Dictionary)
117	}
118	if j.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
119		configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
120		android.WriteFileRule(ctx, configPath, j.fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
121		j.fuzzPackagedModule.Config = configPath
122	}
123
124	_, sharedDeps := cc.CollectAllSharedDependencies(ctx)
125	for _, dep := range sharedDeps {
126		sharedLibInfo, _ := android.OtherModuleProvider(ctx, dep, cc.SharedLibraryInfoProvider)
127		if sharedLibInfo.SharedLibrary != nil {
128			arch := "lib"
129			if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
130				arch = "lib64"
131			}
132
133			libPath := android.PathForModuleOut(ctx, filepath.Join(arch, sharedLibInfo.SharedLibrary.Base()))
134			ctx.Build(pctx, android.BuildParams{
135				Rule:   android.Cp,
136				Input:  sharedLibInfo.SharedLibrary,
137				Output: libPath,
138			})
139			j.jniFilePaths = append(j.jniFilePaths, libPath)
140		} else {
141			ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
142		}
143
144	}
145
146	j.Test.GenerateAndroidBuildActions(ctx)
147}
148
149type javaFuzzPackager struct {
150	fuzz.FuzzPackager
151}
152
153func javaFuzzPackagingFactory() android.Singleton {
154	return &javaFuzzPackager{}
155}
156
157func (s *javaFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
158	// Map between each architecture + host/device combination.
159	archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
160
161	s.FuzzTargets = make(map[string]bool)
162	ctx.VisitAllModules(func(module android.Module) {
163		// Discard non-fuzz targets.
164		javaFuzzModule, ok := module.(*JavaFuzzTest)
165		if !ok {
166			return
167		}
168
169		hostOrTargetString := "target"
170		if javaFuzzModule.Target().HostCross {
171			hostOrTargetString = "host_cross"
172		} else if javaFuzzModule.Host() {
173			hostOrTargetString = "host"
174		}
175
176		fuzzModuleValidator := fuzz.FuzzModule{
177			javaFuzzModule.ModuleBase,
178			javaFuzzModule.DefaultableModuleBase,
179			javaFuzzModule.ApexModuleBase,
180		}
181
182		if ok := fuzz.IsValid(ctx, fuzzModuleValidator); !ok {
183			return
184		}
185
186		archString := javaFuzzModule.Arch().ArchType.String()
187		archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
188		archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
189
190		var files []fuzz.FileToZip
191		builder := android.NewRuleBuilder(pctx, ctx)
192
193		// Package the artifacts (data, corpus, config and dictionary) into a zipfile.
194		files = s.PackageArtifacts(ctx, module, javaFuzzModule.fuzzPackagedModule, archDir, builder)
195
196		// Add .jar
197		if !javaFuzzModule.Host() {
198			files = append(files, fuzz.FileToZip{SourceFilePath: javaFuzzModule.implementationJarFile, DestinationPathPrefix: "classes"})
199		}
200
201		files = append(files, fuzz.FileToZip{SourceFilePath: javaFuzzModule.outputFile})
202
203		// Add jni .so files
204		for _, fPath := range javaFuzzModule.jniFilePaths {
205			files = append(files, fuzz.FileToZip{SourceFilePath: fPath})
206		}
207
208		archDirs[archOs], ok = s.BuildZipFile(ctx, module, javaFuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
209		if !ok {
210			return
211		}
212	})
213	s.CreateFuzzPackage(ctx, archDirs, fuzz.Java, pctx)
214}
215
216func (s *javaFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
217	packages := s.Packages.Strings()
218	sort.Strings(packages)
219	ctx.Strict("SOONG_JAVA_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
220	// Preallocate the slice of fuzz targets to minimize memory allocations.
221	s.PreallocateSlice(ctx, "ALL_JAVA_FUZZ_TARGETS")
222}
223