fuzz.go 6.4 KB

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