2020-02-11 14:24:25 +01:00
|
|
|
// Copyright 2020 The Android Open Source Project
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package rust
|
|
|
|
|
|
|
|
import (
|
2021-04-29 21:50:47 +02:00
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2020-02-11 14:24:25 +01:00
|
|
|
"android/soong/android"
|
|
|
|
"android/soong/cc"
|
|
|
|
"android/soong/rust/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
android.RegisterModuleType("rust_fuzz", RustFuzzFactory)
|
2021-04-29 21:50:47 +02:00
|
|
|
android.RegisterSingletonType("rust_fuzz_packaging", rustFuzzPackagingFactory)
|
2020-02-11 14:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type fuzzDecorator struct {
|
|
|
|
*binaryDecorator
|
|
|
|
|
2021-07-07 23:00:07 +02:00
|
|
|
fuzzPackagedModule cc.FuzzPackagedModule
|
2020-02-11 14:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ compiler = (*binaryDecorator)(nil)
|
|
|
|
|
|
|
|
// rust_binary produces a binary that is runnable on a device.
|
|
|
|
func RustFuzzFactory() android.Module {
|
|
|
|
module, _ := NewRustFuzz(android.HostAndDeviceSupported)
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRustFuzz(hod android.HostOrDeviceSupported) (*Module, *fuzzDecorator) {
|
|
|
|
module, binary := NewRustBinary(hod)
|
|
|
|
fuzz := &fuzzDecorator{
|
|
|
|
binaryDecorator: binary,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the defaults for the binaryDecorator's baseCompiler
|
|
|
|
fuzz.binaryDecorator.baseCompiler.dir = "fuzz"
|
|
|
|
fuzz.binaryDecorator.baseCompiler.dir64 = "fuzz"
|
|
|
|
fuzz.binaryDecorator.baseCompiler.location = InstallInData
|
|
|
|
module.sanitize.SetSanitizer(cc.Fuzzer, true)
|
|
|
|
module.compiler = fuzz
|
|
|
|
return module, fuzz
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzzer *fuzzDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
|
|
|
|
flags = fuzzer.binaryDecorator.compilerFlags(ctx, flags)
|
|
|
|
|
|
|
|
// `../lib` for installed fuzz targets (both host and device), and `./lib` for fuzz target packages.
|
|
|
|
flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
|
|
|
|
flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
|
|
|
|
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzzer *fuzzDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
|
2021-04-13 00:52:29 +02:00
|
|
|
if libFuzzerRuntimeLibrary := config.LibFuzzerRuntimeLibrary(ctx.toolchain()); libFuzzerRuntimeLibrary != "" {
|
|
|
|
deps.StaticLibs = append(deps.StaticLibs, libFuzzerRuntimeLibrary)
|
|
|
|
}
|
2020-02-11 14:24:25 +01:00
|
|
|
deps.SharedLibs = append(deps.SharedLibs, "libc++")
|
|
|
|
deps.Rlibs = append(deps.Rlibs, "liblibfuzzer_sys")
|
|
|
|
|
|
|
|
deps = fuzzer.binaryDecorator.compilerDeps(ctx, deps)
|
|
|
|
|
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzzer *fuzzDecorator) compilerProps() []interface{} {
|
|
|
|
return append(fuzzer.binaryDecorator.compilerProps(),
|
2021-07-07 23:00:07 +02:00
|
|
|
&fuzzer.fuzzPackagedModule.FuzzProperties)
|
2020-02-11 14:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage {
|
|
|
|
return RlibLinkage
|
|
|
|
}
|
|
|
|
|
2021-01-26 15:18:53 +01:00
|
|
|
func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
|
2020-02-11 14:24:25 +01:00
|
|
|
return rlibAutoDep
|
|
|
|
}
|
2021-04-29 21:50:47 +02:00
|
|
|
|
|
|
|
// Responsible for generating GNU Make rules that package fuzz targets into
|
|
|
|
// their architecture & target/host specific zip file.
|
|
|
|
type rustFuzzPackager struct {
|
2021-07-07 23:00:07 +02:00
|
|
|
cc.FuzzPackager
|
2021-04-29 21:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func rustFuzzPackagingFactory() android.Singleton {
|
|
|
|
return &rustFuzzPackager{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
|
|
|
|
// Map between each architecture + host/device combination.
|
2021-07-07 23:00:07 +02:00
|
|
|
archDirs := make(map[cc.ArchOs][]cc.FileToZip)
|
2021-04-29 21:50:47 +02:00
|
|
|
|
|
|
|
// List of individual fuzz targets.
|
2021-07-07 23:00:07 +02:00
|
|
|
s.FuzzTargets = make(map[string]bool)
|
2021-04-29 21:50:47 +02:00
|
|
|
|
|
|
|
ctx.VisitAllModules(func(module android.Module) {
|
|
|
|
// Discard non-fuzz targets.
|
|
|
|
rustModule, ok := module.(*Module)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-07 23:00:07 +02:00
|
|
|
if ok := cc.IsValid(rustModule.FuzzModule); !ok || rustModule.Properties.PreventInstall {
|
2021-04-29 21:50:47 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-07 23:00:07 +02:00
|
|
|
fuzzModule, ok := rustModule.compiler.(*fuzzDecorator)
|
|
|
|
if !ok {
|
2021-04-29 21:50:47 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hostOrTargetString := "target"
|
|
|
|
if rustModule.Host() {
|
|
|
|
hostOrTargetString = "host"
|
|
|
|
}
|
|
|
|
|
|
|
|
archString := rustModule.Arch().ArchType.String()
|
|
|
|
archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
|
2021-07-07 23:00:07 +02:00
|
|
|
archOs := cc.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
|
2021-04-29 21:50:47 +02:00
|
|
|
|
2021-07-07 23:00:07 +02:00
|
|
|
var files []cc.FileToZip
|
2021-04-29 21:50:47 +02:00
|
|
|
builder := android.NewRuleBuilder(pctx, ctx)
|
|
|
|
|
2021-07-07 23:00:07 +02:00
|
|
|
// Package the artifacts (data, corpus, config and dictionary into a zipfile.
|
|
|
|
files = s.PackageArtifacts(ctx, module, fuzzModule.fuzzPackagedModule, archDir, builder)
|
2021-04-29 21:50:47 +02:00
|
|
|
|
|
|
|
// The executable.
|
2021-07-07 23:00:07 +02:00
|
|
|
files = append(files, cc.FileToZip{rustModule.unstrippedOutputFile.Path(), ""})
|
2021-04-29 21:50:47 +02:00
|
|
|
|
2021-07-07 23:00:07 +02:00
|
|
|
archDirs[archOs], ok = s.BuildZipFile(ctx, module, fuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
|
|
|
|
if !ok {
|
|
|
|
return
|
2021-04-29 21:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2021-07-07 23:00:07 +02:00
|
|
|
s.CreateFuzzPackage(ctx, archDirs, cc.Rust)
|
2021-04-29 21:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rustFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
|
2021-07-07 23:00:07 +02:00
|
|
|
packages := s.Packages.Strings()
|
2021-04-29 21:50:47 +02:00
|
|
|
sort.Strings(packages)
|
|
|
|
|
|
|
|
ctx.Strict("SOONG_RUST_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
|
|
|
|
|
2021-07-07 23:00:07 +02:00
|
|
|
// Preallocate the slice of fuzz targets to minimize memory allocations.
|
|
|
|
s.PreallocateSlice(ctx, "ALL_RUST_FUZZ_TARGETS")
|
2021-04-29 21:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzz *fuzzDecorator) install(ctx ModuleContext) {
|
|
|
|
fuzz.binaryDecorator.baseCompiler.dir = filepath.Join(
|
|
|
|
"fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
|
|
|
|
fuzz.binaryDecorator.baseCompiler.dir64 = filepath.Join(
|
|
|
|
"fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
|
|
|
|
fuzz.binaryDecorator.baseCompiler.install(ctx)
|
|
|
|
|
2021-07-07 23:00:07 +02:00
|
|
|
if fuzz.fuzzPackagedModule.FuzzProperties.Corpus != nil {
|
|
|
|
fuzz.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Corpus)
|
2021-04-29 21:50:47 +02:00
|
|
|
}
|
2021-07-07 23:00:07 +02:00
|
|
|
if fuzz.fuzzPackagedModule.FuzzProperties.Data != nil {
|
|
|
|
fuzz.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Data)
|
2021-04-29 21:50:47 +02:00
|
|
|
}
|
2021-07-07 23:00:07 +02:00
|
|
|
if fuzz.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
|
|
|
|
fuzz.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzz.fuzzPackagedModule.FuzzProperties.Dictionary)
|
2021-04-29 21:50:47 +02:00
|
|
|
}
|
|
|
|
}
|