fuzz.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. package java
  15. import (
  16. "path/filepath"
  17. "sort"
  18. "strings"
  19. "android/soong/android"
  20. "android/soong/cc"
  21. "android/soong/fuzz"
  22. "github.com/google/blueprint"
  23. "github.com/google/blueprint/proptools"
  24. )
  25. const (
  26. hostString = "host"
  27. targetString = "target"
  28. deviceString = "device"
  29. )
  30. // Any shared libs for these deps will also be packaged
  31. var artDeps = []string{"libdl_android"}
  32. func init() {
  33. RegisterJavaFuzzBuildComponents(android.InitRegistrationContext)
  34. }
  35. func RegisterJavaFuzzBuildComponents(ctx android.RegistrationContext) {
  36. ctx.RegisterModuleType("java_fuzz", JavaFuzzFactory)
  37. ctx.RegisterParallelSingletonType("java_fuzz_packaging", javaFuzzPackagingFactory)
  38. }
  39. type JavaFuzzTest struct {
  40. Test
  41. fuzzPackagedModule fuzz.FuzzPackagedModule
  42. jniFilePaths android.Paths
  43. }
  44. // java_fuzz builds and links sources into a `.jar` file for the device.
  45. // This generates .class files in a jar which can then be instrumented before
  46. // fuzzing in Android Runtime (ART: Android OS on emulator or device)
  47. func JavaFuzzFactory() android.Module {
  48. module := &JavaFuzzTest{}
  49. module.addHostAndDeviceProperties()
  50. module.AddProperties(&module.testProperties)
  51. module.AddProperties(&module.fuzzPackagedModule.FuzzProperties)
  52. module.Module.properties.Installable = proptools.BoolPtr(true)
  53. module.Module.dexpreopter.isTest = true
  54. module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true)
  55. android.AddLoadHook(module, func(ctx android.LoadHookContext) {
  56. disableLinuxBionic := struct {
  57. Target struct {
  58. Linux_bionic struct {
  59. Enabled *bool
  60. }
  61. }
  62. }{}
  63. disableLinuxBionic.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
  64. ctx.AppendProperties(&disableLinuxBionic)
  65. })
  66. InitJavaModuleMultiTargets(module, android.HostAndDeviceSupported)
  67. return module
  68. }
  69. func (j *JavaFuzzTest) DepsMutator(ctx android.BottomUpMutatorContext) {
  70. if j.Os().Class.String() == deviceString {
  71. j.testProperties.Jni_libs = append(j.testProperties.Jni_libs, artDeps...)
  72. }
  73. if len(j.testProperties.Jni_libs) > 0 {
  74. if j.fuzzPackagedModule.FuzzProperties.Fuzz_config == nil {
  75. config := &fuzz.FuzzConfig{}
  76. j.fuzzPackagedModule.FuzzProperties.Fuzz_config = config
  77. }
  78. // this will be used by the ingestion pipeline to determine the version
  79. // of jazzer to add to the fuzzer package
  80. j.fuzzPackagedModule.FuzzProperties.Fuzz_config.IsJni = proptools.BoolPtr(true)
  81. for _, target := range ctx.MultiTargets() {
  82. sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
  83. ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
  84. }
  85. }
  86. j.deps(ctx)
  87. }
  88. func (j *JavaFuzzTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  89. if j.fuzzPackagedModule.FuzzProperties.Corpus != nil {
  90. j.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, j.fuzzPackagedModule.FuzzProperties.Corpus)
  91. }
  92. if j.fuzzPackagedModule.FuzzProperties.Data != nil {
  93. j.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, j.fuzzPackagedModule.FuzzProperties.Data)
  94. }
  95. if j.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
  96. j.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *j.fuzzPackagedModule.FuzzProperties.Dictionary)
  97. }
  98. if j.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
  99. configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
  100. android.WriteFileRule(ctx, configPath, j.fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
  101. j.fuzzPackagedModule.Config = configPath
  102. }
  103. _, sharedDeps := cc.CollectAllSharedDependencies(ctx)
  104. for _, dep := range sharedDeps {
  105. sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
  106. if sharedLibInfo.SharedLibrary != nil {
  107. arch := "lib"
  108. if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
  109. arch = "lib64"
  110. }
  111. libPath := android.PathForModuleOut(ctx, filepath.Join(arch, sharedLibInfo.SharedLibrary.Base()))
  112. ctx.Build(pctx, android.BuildParams{
  113. Rule: android.Cp,
  114. Input: sharedLibInfo.SharedLibrary,
  115. Output: libPath,
  116. })
  117. j.jniFilePaths = append(j.jniFilePaths, libPath)
  118. } else {
  119. ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
  120. }
  121. }
  122. j.Test.GenerateAndroidBuildActions(ctx)
  123. }
  124. type javaFuzzPackager struct {
  125. fuzz.FuzzPackager
  126. }
  127. func javaFuzzPackagingFactory() android.Singleton {
  128. return &javaFuzzPackager{}
  129. }
  130. func (s *javaFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
  131. // Map between each architecture + host/device combination.
  132. archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
  133. s.FuzzTargets = make(map[string]bool)
  134. ctx.VisitAllModules(func(module android.Module) {
  135. // Discard non-fuzz targets.
  136. javaFuzzModule, ok := module.(*JavaFuzzTest)
  137. if !ok {
  138. return
  139. }
  140. hostOrTargetString := "target"
  141. if javaFuzzModule.Target().HostCross {
  142. hostOrTargetString = "host_cross"
  143. } else if javaFuzzModule.Host() {
  144. hostOrTargetString = "host"
  145. }
  146. fuzzModuleValidator := fuzz.FuzzModule{
  147. javaFuzzModule.ModuleBase,
  148. javaFuzzModule.DefaultableModuleBase,
  149. javaFuzzModule.ApexModuleBase,
  150. }
  151. if ok := fuzz.IsValid(fuzzModuleValidator); !ok {
  152. return
  153. }
  154. archString := javaFuzzModule.Arch().ArchType.String()
  155. archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
  156. archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
  157. var files []fuzz.FileToZip
  158. builder := android.NewRuleBuilder(pctx, ctx)
  159. // Package the artifacts (data, corpus, config and dictionary) into a zipfile.
  160. files = s.PackageArtifacts(ctx, module, javaFuzzModule.fuzzPackagedModule, archDir, builder)
  161. // Add .jar
  162. if !javaFuzzModule.Host() {
  163. files = append(files, fuzz.FileToZip{SourceFilePath: javaFuzzModule.implementationJarFile, DestinationPathPrefix: "classes"})
  164. }
  165. files = append(files, fuzz.FileToZip{SourceFilePath: javaFuzzModule.outputFile})
  166. // Add jni .so files
  167. for _, fPath := range javaFuzzModule.jniFilePaths {
  168. files = append(files, fuzz.FileToZip{SourceFilePath: fPath})
  169. }
  170. archDirs[archOs], ok = s.BuildZipFile(ctx, module, javaFuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
  171. if !ok {
  172. return
  173. }
  174. })
  175. s.CreateFuzzPackage(ctx, archDirs, fuzz.Java, pctx)
  176. }
  177. func (s *javaFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
  178. packages := s.Packages.Strings()
  179. sort.Strings(packages)
  180. ctx.Strict("SOONG_JAVA_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
  181. // Preallocate the slice of fuzz targets to minimize memory allocations.
  182. s.PreallocateSlice(ctx, "ALL_JAVA_FUZZ_TARGETS")
  183. }