diff options
author | 2021-07-07 14:00:07 -0700 | |
---|---|---|
committer | 2021-07-16 17:43:33 -0700 | |
commit | 41ad881e162a3c1fe057e9e301da0b123de1557f (patch) | |
tree | ca919157b4a97be03caa5c16445b250438d9a4ff /rust/fuzz.go | |
parent | 393bffee78a8c825d6bc64bf096b6df6978f7150 (diff) |
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
Diffstat (limited to 'rust/fuzz.go')
-rw-r--r-- | rust/fuzz.go | 171 |
1 files changed, 26 insertions, 145 deletions
diff --git a/rust/fuzz.go b/rust/fuzz.go index 7e1c55a50..18b25130f 100644 --- a/rust/fuzz.go +++ b/rust/fuzz.go @@ -32,13 +32,7 @@ func init() { 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) compilerDeps(ctx DepsContext, deps Deps) Deps { 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 @@ func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep // 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,20 +117,12 @@ func (s *rustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) { return } - fuzzModule, ok := rustModule.compiler.(*fuzzDecorator) - if !ok { + if ok := cc.IsValid(rustModule.FuzzModule); !ok || rustModule.Properties.PreventInstall { 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() { + fuzzModule, ok := rustModule.compiler.(*fuzzDecorator) + if !ok { return } @@ -160,126 +133,34 @@ func (s *rustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) { 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(), ""}) - - // The dictionary. - if fuzzModule.dictionary != nil { - files = append(files, fileToZip{fuzzModule.dictionary, ""}) - } + files = append(files, cc.FileToZip{rustModule.unstrippedOutputFile.Path(), ""}) - // 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 - } + archDirs[archOs], ok = s.BuildZipFile(ctx, module, fuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs) + if !ok { + 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 @@ func (fuzz *fuzzDecorator) install(ctx ModuleContext) { "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) } } |