Refactoring fuzz packaging code
Rust and cc fuzz packaging have common. This change is to put this
common methods and fields into fuzz_common.
Test: make haiku and make haiku-rust ran locally and verify corpus,
dict and config are packaged along with the executable.
Change-Id: I04bf535cd2597ed8b2bd3d8e6ea8c28f38bdc18b
diff --git a/rust/fuzz.go b/rust/fuzz.go
index 7e1c55a..18b2513 100644
--- a/rust/fuzz.go
+++ b/rust/fuzz.go
@@ -32,13 +32,7 @@
type fuzzDecorator struct {
*binaryDecorator
- Properties cc.FuzzProperties
- dictionary android.Path
- corpus android.Paths
- corpusIntermediateDir android.Path
- config android.Path
- data android.Paths
- dataIntermediateDir android.Path
+ fuzzPackagedModule cc.FuzzPackagedModule
}
var _ compiler = (*binaryDecorator)(nil)
@@ -88,7 +82,7 @@
func (fuzzer *fuzzDecorator) compilerProps() []interface{} {
return append(fuzzer.binaryDecorator.compilerProps(),
- &fuzzer.Properties)
+ &fuzzer.fuzzPackagedModule.FuzzProperties)
}
func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage {
@@ -102,32 +96,19 @@
// Responsible for generating GNU Make rules that package fuzz targets into
// their architecture & target/host specific zip file.
type rustFuzzPackager struct {
- packages android.Paths
- fuzzTargets map[string]bool
+ cc.FuzzPackager
}
func rustFuzzPackagingFactory() android.Singleton {
return &rustFuzzPackager{}
}
-type fileToZip struct {
- SourceFilePath android.Path
- DestinationPathPrefix string
-}
-
-type archOs struct {
- hostOrTarget string
- arch string
- dir string
-}
-
func (s *rustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
-
// Map between each architecture + host/device combination.
- archDirs := make(map[archOs][]fileToZip)
+ archDirs := make(map[cc.ArchOs][]cc.FileToZip)
// List of individual fuzz targets.
- s.fuzzTargets = make(map[string]bool)
+ s.FuzzTargets = make(map[string]bool)
ctx.VisitAllModules(func(module android.Module) {
// Discard non-fuzz targets.
@@ -136,23 +117,15 @@
return
}
+ if ok := cc.IsValid(rustModule.FuzzModule); !ok || rustModule.Properties.PreventInstall {
+ return
+ }
+
fuzzModule, ok := rustModule.compiler.(*fuzzDecorator)
if !ok {
return
}
- // Discard ramdisk + vendor_ramdisk + recovery modules, they're duplicates of
- // fuzz targets we're going to package anyway.
- if !rustModule.Enabled() || rustModule.Properties.PreventInstall ||
- rustModule.InRamdisk() || rustModule.InVendorRamdisk() || rustModule.InRecovery() {
- return
- }
-
- // Discard modules that are in an unavailable namespace.
- if !rustModule.ExportedToMake() {
- return
- }
-
hostOrTargetString := "target"
if rustModule.Host() {
hostOrTargetString = "host"
@@ -160,126 +133,34 @@
archString := rustModule.Arch().ArchType.String()
archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
- archOs := archOs{hostOrTarget: hostOrTargetString, arch: archString, dir: archDir.String()}
+ archOs := cc.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
- var files []fileToZip
+ var files []cc.FileToZip
builder := android.NewRuleBuilder(pctx, ctx)
- // Package the corpora into a zipfile.
- if fuzzModule.corpus != nil {
- corpusZip := archDir.Join(ctx, module.Name()+"_seed_corpus.zip")
- command := builder.Command().BuiltTool("soong_zip").
- Flag("-j").
- FlagWithOutput("-o ", corpusZip)
- rspFile := corpusZip.ReplaceExtension(ctx, "rsp")
- command.FlagWithRspFileInputList("-r ", rspFile, fuzzModule.corpus)
- files = append(files, fileToZip{corpusZip, ""})
- }
-
- // Package the data into a zipfile.
- if fuzzModule.data != nil {
- dataZip := archDir.Join(ctx, module.Name()+"_data.zip")
- command := builder.Command().BuiltTool("soong_zip").
- FlagWithOutput("-o ", dataZip)
- for _, f := range fuzzModule.data {
- intermediateDir := strings.TrimSuffix(f.String(), f.Rel())
- command.FlagWithArg("-C ", intermediateDir)
- command.FlagWithInput("-f ", f)
- }
- files = append(files, fileToZip{dataZip, ""})
- }
+ // Package the artifacts (data, corpus, config and dictionary into a zipfile.
+ files = s.PackageArtifacts(ctx, module, fuzzModule.fuzzPackagedModule, archDir, builder)
// The executable.
- files = append(files, fileToZip{rustModule.unstrippedOutputFile.Path(), ""})
+ files = append(files, cc.FileToZip{rustModule.unstrippedOutputFile.Path(), ""})
- // The dictionary.
- if fuzzModule.dictionary != nil {
- files = append(files, fileToZip{fuzzModule.dictionary, ""})
+ archDirs[archOs], ok = s.BuildZipFile(ctx, module, fuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
+ if !ok {
+ return
}
- // Additional fuzz config.
- if fuzzModule.config != nil {
- files = append(files, fileToZip{fuzzModule.config, ""})
- }
-
- fuzzZip := archDir.Join(ctx, module.Name()+".zip")
-
- command := builder.Command().BuiltTool("soong_zip").
- Flag("-j").
- FlagWithOutput("-o ", fuzzZip)
-
- for _, file := range files {
- if file.DestinationPathPrefix != "" {
- command.FlagWithArg("-P ", file.DestinationPathPrefix)
- } else {
- command.Flag("-P ''")
- }
- command.FlagWithInput("-f ", file.SourceFilePath)
- }
-
- builder.Build("create-"+fuzzZip.String(),
- "Package "+module.Name()+" for "+archString+"-"+hostOrTargetString)
-
- // Don't add modules to 'make haiku-rust' that are set to not be
- // exported to the fuzzing infrastructure.
- if config := fuzzModule.Properties.Fuzz_config; config != nil {
- if rustModule.Host() && !BoolDefault(config.Fuzz_on_haiku_host, true) {
- return
- } else if !BoolDefault(config.Fuzz_on_haiku_device, true) {
- return
- }
- }
-
- s.fuzzTargets[module.Name()] = true
- archDirs[archOs] = append(archDirs[archOs], fileToZip{fuzzZip, ""})
})
-
- var archOsList []archOs
- for archOs := range archDirs {
- archOsList = append(archOsList, archOs)
- }
- sort.Slice(archOsList, func(i, j int) bool { return archOsList[i].dir < archOsList[j].dir })
-
- for _, archOs := range archOsList {
- filesToZip := archDirs[archOs]
- arch := archOs.arch
- hostOrTarget := archOs.hostOrTarget
- builder := android.NewRuleBuilder(pctx, ctx)
- outputFile := android.PathForOutput(ctx, "fuzz-rust-"+hostOrTarget+"-"+arch+".zip")
- s.packages = append(s.packages, outputFile)
-
- command := builder.Command().BuiltTool("soong_zip").
- Flag("-j").
- FlagWithOutput("-o ", outputFile).
- Flag("-L 0") // No need to try and re-compress the zipfiles.
-
- for _, fileToZip := range filesToZip {
- if fileToZip.DestinationPathPrefix != "" {
- command.FlagWithArg("-P ", fileToZip.DestinationPathPrefix)
- } else {
- command.Flag("-P ''")
- }
- command.FlagWithInput("-f ", fileToZip.SourceFilePath)
- }
- builder.Build("create-fuzz-package-"+arch+"-"+hostOrTarget,
- "Create fuzz target packages for "+arch+"-"+hostOrTarget)
- }
-
+ s.CreateFuzzPackage(ctx, archDirs, cc.Rust)
}
func (s *rustFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
- packages := s.packages.Strings()
+ packages := s.Packages.Strings()
sort.Strings(packages)
ctx.Strict("SOONG_RUST_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
- // Preallocate the slice of fuzz targets to minimise memory allocations.
- fuzzTargets := make([]string, 0, len(s.fuzzTargets))
- for target, _ := range s.fuzzTargets {
- fuzzTargets = append(fuzzTargets, target)
- }
- sort.Strings(fuzzTargets)
- ctx.Strict("ALL_RUST_FUZZ_TARGETS", strings.Join(fuzzTargets, " "))
+ // Preallocate the slice of fuzz targets to minimize memory allocations.
+ s.PreallocateSlice(ctx, "ALL_RUST_FUZZ_TARGETS")
}
func (fuzz *fuzzDecorator) install(ctx ModuleContext) {
@@ -289,13 +170,13 @@
"fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
fuzz.binaryDecorator.baseCompiler.install(ctx)
- if fuzz.Properties.Corpus != nil {
- fuzz.corpus = android.PathsForModuleSrc(ctx, fuzz.Properties.Corpus)
+ if fuzz.fuzzPackagedModule.FuzzProperties.Corpus != nil {
+ fuzz.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Corpus)
}
- if fuzz.Properties.Data != nil {
- fuzz.data = android.PathsForModuleSrc(ctx, fuzz.Properties.Data)
+ if fuzz.fuzzPackagedModule.FuzzProperties.Data != nil {
+ fuzz.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Data)
}
- if fuzz.Properties.Dictionary != nil {
- fuzz.dictionary = android.PathForModuleSrc(ctx, *fuzz.Properties.Dictionary)
+ if fuzz.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
+ fuzz.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzz.fuzzPackagedModule.FuzzProperties.Dictionary)
}
}