• 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_module_type = 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	j.fuzzPackagedModule = cc.PackageFuzzModule(ctx, j.fuzzPackagedModule)
111
112	_, sharedDeps := cc.CollectAllSharedDependencies(ctx)
113	for _, dep := range sharedDeps {
114		sharedLibInfo, _ := android.OtherModuleProvider(ctx, dep, cc.SharedLibraryInfoProvider)
115		if sharedLibInfo.SharedLibrary != nil {
116			arch := "lib"
117			if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
118				arch = "lib64"
119			}
120
121			libPath := android.PathForModuleOut(ctx, filepath.Join(arch, sharedLibInfo.SharedLibrary.Base()))
122			ctx.Build(pctx, android.BuildParams{
123				Rule:   android.Cp,
124				Input:  sharedLibInfo.SharedLibrary,
125				Output: libPath,
126			})
127			j.jniFilePaths = append(j.jniFilePaths, libPath)
128		} else {
129			ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
130		}
131
132	}
133
134	j.Test.GenerateAndroidBuildActions(ctx)
135
136	fuzz.SetFuzzPackagedModuleInfo(ctx, &j.fuzzPackagedModule)
137}
138
139type javaFuzzPackager struct {
140	fuzz.FuzzPackager
141}
142
143func javaFuzzPackagingFactory() android.Singleton {
144	return &javaFuzzPackager{}
145}
146
147func (s *javaFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
148	// Map between each architecture + host/device combination.
149	archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
150
151	s.FuzzTargets = make(map[string]bool)
152	ctx.VisitAllModules(func(module android.Module) {
153		// Discard non-fuzz targets.
154		javaFuzzModule, ok := module.(*JavaFuzzTest)
155		if !ok {
156			return
157		}
158		fuzzInfo, ok := android.OtherModuleProvider(ctx, module, fuzz.FuzzPackagedModuleInfoProvider)
159		if !ok {
160			return
161		}
162
163		hostOrTargetString := "target"
164		if javaFuzzModule.Target().HostCross {
165			hostOrTargetString = "host_cross"
166		} else if javaFuzzModule.Host() {
167			hostOrTargetString = "host"
168		}
169
170		fuzzModuleValidator := fuzz.FuzzModule{
171			javaFuzzModule.ModuleBase,
172			javaFuzzModule.DefaultableModuleBase,
173			javaFuzzModule.ApexModuleBase,
174		}
175
176		if ok := fuzz.IsValid(ctx, fuzzModuleValidator); !ok {
177			return
178		}
179
180		archString := javaFuzzModule.Arch().ArchType.String()
181		archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
182		archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
183
184		var files []fuzz.FileToZip
185		builder := android.NewRuleBuilder(pctx, ctx)
186
187		// Package the artifacts (data, corpus, config and dictionary) into a zipfile.
188		files = s.PackageArtifacts(ctx, module, &fuzzInfo, archDir, builder)
189
190		// Add .jar
191		if !javaFuzzModule.Host() {
192			files = append(files, fuzz.FileToZip{SourceFilePath: javaFuzzModule.implementationJarFile, DestinationPathPrefix: "classes"})
193		}
194
195		files = append(files, fuzz.FileToZip{SourceFilePath: javaFuzzModule.outputFile})
196
197		// Add jni .so files
198		for _, fPath := range javaFuzzModule.jniFilePaths {
199			files = append(files, fuzz.FileToZip{SourceFilePath: fPath})
200		}
201
202		archDirs[archOs], ok = s.BuildZipFile(ctx, module, &fuzzInfo, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
203		if !ok {
204			return
205		}
206	})
207	s.CreateFuzzPackage(ctx, archDirs, fuzz.Java, pctx)
208}
209
210func (s *javaFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
211	packages := s.Packages.Strings()
212	sort.Strings(packages)
213	ctx.Strict("SOONG_JAVA_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
214	// Preallocate the slice of fuzz targets to minimize memory allocations.
215	s.PreallocateSlice(ctx, "ALL_JAVA_FUZZ_TARGETS")
216}
217