2019-07-16 00:34:09 +08:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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 cc
|
|
|
|
|
|
|
|
import (
|
2019-10-23 01:52:01 +08:00
|
|
|
"encoding/json"
|
2019-08-29 07:04:36 +08:00
|
|
|
"path/filepath"
|
2019-10-18 10:20:41 +08:00
|
|
|
"sort"
|
2019-09-28 05:00:06 +08:00
|
|
|
"strings"
|
2019-08-29 07:04:36 +08:00
|
|
|
|
2021-02-09 20:30:33 +08:00
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
|
2019-07-16 00:34:09 +08:00
|
|
|
"android/soong/android"
|
|
|
|
"android/soong/cc/config"
|
|
|
|
)
|
|
|
|
|
2019-10-23 01:52:01 +08:00
|
|
|
type FuzzConfig struct {
|
|
|
|
// Email address of people to CC on bugs or contact about this fuzz target.
|
|
|
|
Cc []string `json:"cc,omitempty"`
|
2019-12-17 08:25:50 +08:00
|
|
|
// Specify whether to enable continuous fuzzing on devices. Defaults to true.
|
|
|
|
Fuzz_on_haiku_device *bool `json:"fuzz_on_haiku_device,omitempty"`
|
|
|
|
// Specify whether to enable continuous fuzzing on host. Defaults to true.
|
|
|
|
Fuzz_on_haiku_host *bool `json:"fuzz_on_haiku_host,omitempty"`
|
2019-10-23 01:52:01 +08:00
|
|
|
// Component in Google's bug tracking system that bugs should be filed to.
|
|
|
|
Componentid *int64 `json:"componentid,omitempty"`
|
|
|
|
// Hotlists in Google's bug tracking system that bugs should be marked with.
|
|
|
|
Hotlists []string `json:"hotlists,omitempty"`
|
2020-04-29 02:32:23 +08:00
|
|
|
// Specify whether this fuzz target was submitted by a researcher. Defaults
|
|
|
|
// to false.
|
|
|
|
Researcher_submitted *bool `json:"researcher_submitted,omitempty"`
|
2020-09-30 06:09:36 +08:00
|
|
|
// Specify who should be acknowledged for CVEs in the Android Security
|
|
|
|
// Bulletin.
|
|
|
|
Acknowledgement []string `json:"acknowledgement,omitempty"`
|
2021-01-08 04:57:23 +08:00
|
|
|
// Additional options to be passed to libfuzzer when run in Haiku.
|
|
|
|
Libfuzzer_options []string `json:"libfuzzer_options,omitempty"`
|
|
|
|
// Additional options to be passed to HWASAN when running on-device in Haiku.
|
|
|
|
Hwasan_options []string `json:"hwasan_options,omitempty"`
|
|
|
|
// Additional options to be passed to HWASAN when running on host in Haiku.
|
|
|
|
Asan_options []string `json:"asan_options,omitempty"`
|
2019-10-23 01:52:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FuzzConfig) String() string {
|
|
|
|
b, err := json.Marshal(f)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2019-09-14 08:32:50 +08:00
|
|
|
type FuzzProperties struct {
|
|
|
|
// Optional list of seed files to be installed to the fuzz target's output
|
|
|
|
// directory.
|
|
|
|
Corpus []string `android:"path"`
|
2019-11-28 05:45:45 +08:00
|
|
|
// Optional list of data files to be installed to the fuzz target's output
|
|
|
|
// directory. Directory structure relative to the module is preserved.
|
|
|
|
Data []string `android:"path"`
|
2019-09-14 08:32:50 +08:00
|
|
|
// Optional dictionary to be installed to the fuzz target's output directory.
|
|
|
|
Dictionary *string `android:"path"`
|
2019-10-23 01:52:01 +08:00
|
|
|
// Config for running the target on fuzzing infrastructure.
|
|
|
|
Fuzz_config *FuzzConfig
|
2019-09-14 08:32:50 +08:00
|
|
|
}
|
|
|
|
|
2019-07-16 00:34:09 +08:00
|
|
|
func init() {
|
|
|
|
android.RegisterModuleType("cc_fuzz", FuzzFactory)
|
2019-09-25 04:03:28 +08:00
|
|
|
android.RegisterSingletonType("cc_fuzz_packaging", fuzzPackagingFactory)
|
2019-07-16 00:34:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at
|
|
|
|
// $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on
|
|
|
|
// your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree.
|
|
|
|
func FuzzFactory() android.Module {
|
|
|
|
module := NewFuzz(android.HostAndDeviceSupported)
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFuzzInstaller() *baseInstaller {
|
|
|
|
return NewBaseInstaller("fuzz", "fuzz", InstallInData)
|
|
|
|
}
|
|
|
|
|
|
|
|
type fuzzBinary struct {
|
|
|
|
*binaryDecorator
|
|
|
|
*baseCompiler
|
2019-09-14 08:32:50 +08:00
|
|
|
|
2019-10-18 06:04:01 +08:00
|
|
|
Properties FuzzProperties
|
|
|
|
dictionary android.Path
|
|
|
|
corpus android.Paths
|
|
|
|
corpusIntermediateDir android.Path
|
2019-10-23 01:52:01 +08:00
|
|
|
config android.Path
|
2019-11-28 05:45:45 +08:00
|
|
|
data android.Paths
|
|
|
|
dataIntermediateDir android.Path
|
2019-10-18 10:20:41 +08:00
|
|
|
installedSharedDeps []string
|
2019-07-16 00:34:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzz *fuzzBinary) linkerProps() []interface{} {
|
|
|
|
props := fuzz.binaryDecorator.linkerProps()
|
2019-09-14 08:32:50 +08:00
|
|
|
props = append(props, &fuzz.Properties)
|
2019-07-16 00:34:09 +08:00
|
|
|
return props
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
|
|
|
|
fuzz.binaryDecorator.linkerInit(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzz *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
|
|
|
|
deps.StaticLibs = append(deps.StaticLibs,
|
|
|
|
config.LibFuzzerRuntimeLibrary(ctx.toolchain()))
|
|
|
|
deps = fuzz.binaryDecorator.linkerDeps(ctx, deps)
|
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
|
|
|
|
flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
|
2019-11-15 06:50:47 +08:00
|
|
|
// RunPaths on devices isn't instantiated by the base linker. `../lib` for
|
|
|
|
// installed fuzz targets (both host and device), and `./lib` for fuzz
|
|
|
|
// target packages.
|
2019-10-18 10:20:41 +08:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
|
2019-11-15 06:50:47 +08:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
|
2019-07-16 00:34:09 +08:00
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2019-10-18 10:20:41 +08:00
|
|
|
// This function performs a breadth-first search over the provided module's
|
|
|
|
// dependencies using `visitDirectDeps` to enumerate all shared library
|
|
|
|
// dependencies. We require breadth-first expansion, as otherwise we may
|
|
|
|
// incorrectly use the core libraries (sanitizer runtimes, libc, libdl, etc.)
|
|
|
|
// from a dependency. This may cause issues when dependencies have explicit
|
|
|
|
// sanitizer tags, as we may get a dependency on an unsanitized libc, etc.
|
2019-11-21 07:58:32 +08:00
|
|
|
func collectAllSharedDependencies(ctx android.SingletonContext, module android.Module) android.Paths {
|
2019-10-18 10:20:41 +08:00
|
|
|
var fringe []android.Module
|
|
|
|
|
2020-04-28 07:44:58 +08:00
|
|
|
seen := make(map[string]bool)
|
2019-11-21 07:58:32 +08:00
|
|
|
|
2019-10-18 10:20:41 +08:00
|
|
|
// Enumerate the first level of dependencies, as we discard all non-library
|
|
|
|
// modules in the BFS loop below.
|
|
|
|
ctx.VisitDirectDeps(module, func(dep android.Module) {
|
2019-11-21 07:58:32 +08:00
|
|
|
if isValidSharedDependency(dep) {
|
2019-11-13 06:03:31 +08:00
|
|
|
fringe = append(fringe, dep)
|
|
|
|
}
|
2019-10-18 10:20:41 +08:00
|
|
|
})
|
|
|
|
|
2019-11-21 07:58:32 +08:00
|
|
|
var sharedLibraries android.Paths
|
|
|
|
|
2019-10-18 10:20:41 +08:00
|
|
|
for i := 0; i < len(fringe); i++ {
|
|
|
|
module := fringe[i]
|
2020-04-28 07:44:58 +08:00
|
|
|
if seen[module.Name()] {
|
2019-10-18 10:20:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-04-28 07:44:58 +08:00
|
|
|
seen[module.Name()] = true
|
2019-10-18 10:20:41 +08:00
|
|
|
|
|
|
|
ccModule := module.(*Module)
|
2019-11-21 07:58:32 +08:00
|
|
|
sharedLibraries = append(sharedLibraries, ccModule.UnstrippedOutputFile())
|
2019-10-18 10:20:41 +08:00
|
|
|
ctx.VisitDirectDeps(module, func(dep android.Module) {
|
2020-04-28 07:44:58 +08:00
|
|
|
if isValidSharedDependency(dep) && !seen[dep.Name()] {
|
2019-11-13 06:03:31 +08:00
|
|
|
fringe = append(fringe, dep)
|
|
|
|
}
|
2019-10-18 10:20:41 +08:00
|
|
|
})
|
|
|
|
}
|
2019-11-21 07:58:32 +08:00
|
|
|
|
|
|
|
return sharedLibraries
|
2019-10-18 10:20:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function takes a module and determines if it is a unique shared library
|
|
|
|
// that should be installed in the fuzz target output directories. This function
|
|
|
|
// returns true, unless:
|
2021-02-09 20:30:33 +08:00
|
|
|
// - The module is not an installable shared library, or
|
2020-10-06 09:36:43 +08:00
|
|
|
// - The module is a header, stub, or vendor-linked library, or
|
|
|
|
// - The module is a prebuilt and its source is available, or
|
|
|
|
// - The module is a versioned member of an SDK snapshot.
|
2019-11-21 07:58:32 +08:00
|
|
|
func isValidSharedDependency(dependency android.Module) bool {
|
2019-10-18 10:20:41 +08:00
|
|
|
// TODO(b/144090547): We should be parsing these modules using
|
|
|
|
// ModuleDependencyTag instead of the current brute-force checking.
|
|
|
|
|
2020-10-24 08:22:06 +08:00
|
|
|
linkable, ok := dependency.(LinkableInterface)
|
|
|
|
if !ok || !linkable.CcLibraryInterface() {
|
|
|
|
// Discard non-linkables.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !linkable.Shared() {
|
|
|
|
// Discard static libs.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if linkable.UseVndk() {
|
|
|
|
// Discard vendor linked libraries.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
|
2019-11-13 06:03:31 +08:00
|
|
|
// Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not
|
|
|
|
// be excluded on the basis of they're not CCLibrary()'s.
|
2019-10-18 10:20:41 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-13 06:03:31 +08:00
|
|
|
// We discarded module stubs libraries above, but the LLNDK prebuilts stubs
|
|
|
|
// libraries must be handled differently - by looking for the stubDecorator.
|
|
|
|
// Discard LLNDK prebuilts stubs as well.
|
|
|
|
if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary {
|
|
|
|
if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary {
|
|
|
|
return false
|
|
|
|
}
|
2021-02-09 20:30:33 +08:00
|
|
|
// Discard installable:false libraries because they are expected to be absent
|
|
|
|
// in runtime.
|
|
|
|
if !proptools.BoolDefault(ccLibrary.Properties.Installable, true) {
|
|
|
|
return false
|
|
|
|
}
|
2019-11-13 06:03:31 +08:00
|
|
|
}
|
|
|
|
|
2020-10-06 09:36:43 +08:00
|
|
|
// If the same library is present both as source and a prebuilt we must pick
|
|
|
|
// only one to avoid a conflict. Always prefer the source since the prebuilt
|
|
|
|
// probably won't be built with sanitizers enabled.
|
2021-04-28 17:41:21 +08:00
|
|
|
if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
|
2020-10-06 09:36:43 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard versioned members of SDK snapshots, because they will conflict with
|
|
|
|
// unversioned ones.
|
|
|
|
if sdkMember, ok := dependency.(android.SdkAware); ok && !sdkMember.ContainingSdk().Unversioned() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-10-18 10:20:41 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func sharedLibraryInstallLocation(
|
|
|
|
libraryPath android.Path, isHost bool, archString string) string {
|
|
|
|
installLocation := "$(PRODUCT_OUT)/data"
|
|
|
|
if isHost {
|
|
|
|
installLocation = "$(HOST_OUT)"
|
|
|
|
}
|
|
|
|
installLocation = filepath.Join(
|
|
|
|
installLocation, "fuzz", archString, "lib", libraryPath.Base())
|
|
|
|
return installLocation
|
|
|
|
}
|
|
|
|
|
2020-03-07 01:38:12 +08:00
|
|
|
// Get the device-only shared library symbols install directory.
|
|
|
|
func sharedLibrarySymbolsInstallLocation(libraryPath android.Path, archString string) string {
|
|
|
|
return filepath.Join("$(PRODUCT_OUT)/symbols/data/fuzz/", archString, "/lib/", libraryPath.Base())
|
|
|
|
}
|
|
|
|
|
2019-07-16 00:34:09 +08:00
|
|
|
func (fuzz *fuzzBinary) install(ctx ModuleContext, file android.Path) {
|
2019-09-14 08:32:50 +08:00
|
|
|
fuzz.binaryDecorator.baseInstaller.dir = filepath.Join(
|
|
|
|
"fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
|
|
|
|
fuzz.binaryDecorator.baseInstaller.dir64 = filepath.Join(
|
|
|
|
"fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
|
2019-07-16 00:34:09 +08:00
|
|
|
fuzz.binaryDecorator.baseInstaller.install(ctx, file)
|
2019-09-14 08:32:50 +08:00
|
|
|
|
|
|
|
fuzz.corpus = android.PathsForModuleSrc(ctx, fuzz.Properties.Corpus)
|
2020-11-17 09:32:30 +08:00
|
|
|
builder := android.NewRuleBuilder(pctx, ctx)
|
2019-10-18 06:04:01 +08:00
|
|
|
intermediateDir := android.PathForModuleOut(ctx, "corpus")
|
|
|
|
for _, entry := range fuzz.corpus {
|
|
|
|
builder.Command().Text("cp").
|
|
|
|
Input(entry).
|
|
|
|
Output(intermediateDir.Join(ctx, entry.Base()))
|
|
|
|
}
|
2020-11-17 09:32:30 +08:00
|
|
|
builder.Build("copy_corpus", "copy corpus")
|
2019-10-18 06:04:01 +08:00
|
|
|
fuzz.corpusIntermediateDir = intermediateDir
|
|
|
|
|
2019-11-28 05:45:45 +08:00
|
|
|
fuzz.data = android.PathsForModuleSrc(ctx, fuzz.Properties.Data)
|
2020-11-17 09:32:30 +08:00
|
|
|
builder = android.NewRuleBuilder(pctx, ctx)
|
2019-11-28 05:45:45 +08:00
|
|
|
intermediateDir = android.PathForModuleOut(ctx, "data")
|
|
|
|
for _, entry := range fuzz.data {
|
|
|
|
builder.Command().Text("cp").
|
|
|
|
Input(entry).
|
|
|
|
Output(intermediateDir.Join(ctx, entry.Rel()))
|
|
|
|
}
|
2020-11-17 09:32:30 +08:00
|
|
|
builder.Build("copy_data", "copy data")
|
2019-11-28 05:45:45 +08:00
|
|
|
fuzz.dataIntermediateDir = intermediateDir
|
|
|
|
|
2019-09-14 08:32:50 +08:00
|
|
|
if fuzz.Properties.Dictionary != nil {
|
|
|
|
fuzz.dictionary = android.PathForModuleSrc(ctx, *fuzz.Properties.Dictionary)
|
|
|
|
if fuzz.dictionary.Ext() != ".dict" {
|
|
|
|
ctx.PropertyErrorf("dictionary",
|
|
|
|
"Fuzzer dictionary %q does not have '.dict' extension",
|
|
|
|
fuzz.dictionary.String())
|
|
|
|
}
|
|
|
|
}
|
2019-10-23 01:52:01 +08:00
|
|
|
|
|
|
|
if fuzz.Properties.Fuzz_config != nil {
|
2019-10-31 01:17:04 +08:00
|
|
|
configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
|
2020-11-14 03:48:42 +08:00
|
|
|
android.WriteFileRule(ctx, configPath, fuzz.Properties.Fuzz_config.String())
|
2019-10-23 01:52:01 +08:00
|
|
|
fuzz.config = configPath
|
|
|
|
}
|
2019-10-18 10:20:41 +08:00
|
|
|
|
|
|
|
// Grab the list of required shared libraries.
|
2020-04-28 07:44:58 +08:00
|
|
|
seen := make(map[string]bool)
|
2019-11-21 07:58:32 +08:00
|
|
|
var sharedLibraries android.Paths
|
2019-10-18 10:20:41 +08:00
|
|
|
ctx.WalkDeps(func(child, parent android.Module) bool {
|
2020-04-28 07:44:58 +08:00
|
|
|
if seen[child.Name()] {
|
2019-11-21 07:58:32 +08:00
|
|
|
return false
|
|
|
|
}
|
2020-04-28 07:44:58 +08:00
|
|
|
seen[child.Name()] = true
|
2019-11-21 07:58:32 +08:00
|
|
|
|
|
|
|
if isValidSharedDependency(child) {
|
|
|
|
sharedLibraries = append(sharedLibraries, child.(*Module).UnstrippedOutputFile())
|
2019-10-18 10:20:41 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, lib := range sharedLibraries {
|
|
|
|
fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
|
|
|
|
sharedLibraryInstallLocation(
|
|
|
|
lib, ctx.Host(), ctx.Arch().ArchType.String()))
|
2020-03-07 01:38:12 +08:00
|
|
|
|
|
|
|
// Also add the dependency on the shared library symbols dir.
|
|
|
|
if !ctx.Host() {
|
|
|
|
fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
|
|
|
|
sharedLibrarySymbolsInstallLocation(lib, ctx.Arch().ArchType.String()))
|
|
|
|
}
|
2019-10-18 10:20:41 +08:00
|
|
|
}
|
2019-07-16 00:34:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewFuzz(hod android.HostOrDeviceSupported) *Module {
|
|
|
|
module, binary := NewBinary(hod)
|
|
|
|
|
|
|
|
binary.baseInstaller = NewFuzzInstaller()
|
2020-12-15 00:27:52 +08:00
|
|
|
module.sanitize.SetSanitizer(Fuzzer, true)
|
2019-07-16 00:34:09 +08:00
|
|
|
|
|
|
|
fuzz := &fuzzBinary{
|
|
|
|
binaryDecorator: binary,
|
|
|
|
baseCompiler: NewBaseCompiler(),
|
|
|
|
}
|
|
|
|
module.compiler = fuzz
|
|
|
|
module.linker = fuzz
|
|
|
|
module.installer = fuzz
|
2019-07-19 07:20:52 +08:00
|
|
|
|
|
|
|
// The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
|
|
|
|
android.AddLoadHook(module, func(ctx android.LoadHookContext) {
|
2019-07-25 04:34:19 +08:00
|
|
|
disableDarwinAndLinuxBionic := struct {
|
2019-07-19 07:20:52 +08:00
|
|
|
Target struct {
|
|
|
|
Darwin struct {
|
|
|
|
Enabled *bool
|
|
|
|
}
|
2019-07-25 04:34:19 +08:00
|
|
|
Linux_bionic struct {
|
|
|
|
Enabled *bool
|
|
|
|
}
|
2019-07-19 07:20:52 +08:00
|
|
|
}
|
|
|
|
}{}
|
2019-07-25 04:34:19 +08:00
|
|
|
disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false)
|
|
|
|
disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false)
|
|
|
|
ctx.AppendProperties(&disableDarwinAndLinuxBionic)
|
2019-07-19 07:20:52 +08:00
|
|
|
})
|
|
|
|
|
2019-07-16 00:34:09 +08:00
|
|
|
return module
|
|
|
|
}
|
2019-09-25 04:03:28 +08:00
|
|
|
|
|
|
|
// Responsible for generating GNU Make rules that package fuzz targets into
|
|
|
|
// their architecture & target/host specific zip file.
|
|
|
|
type fuzzPackager struct {
|
2019-10-18 10:20:41 +08:00
|
|
|
packages android.Paths
|
|
|
|
sharedLibInstallStrings []string
|
|
|
|
fuzzTargets map[string]bool
|
2019-09-25 04:03:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func fuzzPackagingFactory() android.Singleton {
|
|
|
|
return &fuzzPackager{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileToZip struct {
|
|
|
|
SourceFilePath android.Path
|
|
|
|
DestinationPathPrefix string
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:58:32 +08:00
|
|
|
type archOs struct {
|
|
|
|
hostOrTarget string
|
|
|
|
arch string
|
|
|
|
dir string
|
2019-10-18 10:20:41 +08:00
|
|
|
}
|
|
|
|
|
2019-09-25 04:03:28 +08:00
|
|
|
func (s *fuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
|
|
|
|
// Map between each architecture + host/device combination, and the files that
|
|
|
|
// need to be packaged (in the tuple of {source file, destination folder in
|
|
|
|
// archive}).
|
2019-11-21 07:58:32 +08:00
|
|
|
archDirs := make(map[archOs][]fileToZip)
|
2019-09-25 04:03:28 +08:00
|
|
|
|
2019-11-21 07:58:32 +08:00
|
|
|
// Map tracking whether each shared library has an install rule to avoid duplicate install rules from
|
|
|
|
// multiple fuzzers that depend on the same shared library.
|
|
|
|
sharedLibraryInstalled := make(map[string]bool)
|
2019-10-18 10:20:41 +08:00
|
|
|
|
|
|
|
// List of individual fuzz targets, so that 'make fuzz' also installs the targets
|
|
|
|
// to the correct output directories as well.
|
|
|
|
s.fuzzTargets = make(map[string]bool)
|
|
|
|
|
2019-09-25 04:03:28 +08:00
|
|
|
ctx.VisitAllModules(func(module android.Module) {
|
|
|
|
// Discard non-fuzz targets.
|
|
|
|
ccModule, ok := module.(*Module)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-10-18 10:20:41 +08:00
|
|
|
|
2019-09-25 04:03:28 +08:00
|
|
|
fuzzModule, ok := ccModule.compiler.(*fuzzBinary)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-22 06:17:56 +08:00
|
|
|
// Discard ramdisk + vendor_ramdisk + recovery modules, they're duplicates of
|
2019-11-14 00:36:07 +08:00
|
|
|
// fuzz targets we're going to package anyway.
|
|
|
|
if !ccModule.Enabled() || ccModule.Properties.PreventInstall ||
|
2020-10-22 06:17:56 +08:00
|
|
|
ccModule.InRamdisk() || ccModule.InVendorRamdisk() || ccModule.InRecovery() {
|
2019-09-25 04:03:28 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-05 23:36:11 +08:00
|
|
|
// Discard modules that are in an unavailable namespace.
|
|
|
|
if !ccModule.ExportedToMake() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-25 04:03:28 +08:00
|
|
|
hostOrTargetString := "target"
|
|
|
|
if ccModule.Host() {
|
|
|
|
hostOrTargetString = "host"
|
|
|
|
}
|
|
|
|
|
|
|
|
archString := ccModule.Arch().ArchType.String()
|
|
|
|
archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
|
2019-11-21 07:58:32 +08:00
|
|
|
archOs := archOs{hostOrTarget: hostOrTargetString, arch: archString, dir: archDir.String()}
|
2019-09-25 04:03:28 +08:00
|
|
|
|
2019-10-18 10:20:41 +08:00
|
|
|
// Grab the list of required shared libraries.
|
2019-11-21 07:58:32 +08:00
|
|
|
sharedLibraries := collectAllSharedDependencies(ctx, module)
|
2019-10-18 10:20:41 +08:00
|
|
|
|
2019-11-14 00:36:07 +08:00
|
|
|
var files []fileToZip
|
2020-11-17 09:32:30 +08:00
|
|
|
builder := android.NewRuleBuilder(pctx, ctx)
|
2019-11-14 00:36:07 +08:00
|
|
|
|
|
|
|
// Package the corpora into a zipfile.
|
|
|
|
if fuzzModule.corpus != nil {
|
|
|
|
corpusZip := archDir.Join(ctx, module.Name()+"_seed_corpus.zip")
|
2020-11-17 09:32:30 +08:00
|
|
|
command := builder.Command().BuiltTool("soong_zip").
|
2019-11-14 00:36:07 +08:00
|
|
|
Flag("-j").
|
|
|
|
FlagWithOutput("-o ", corpusZip)
|
2021-03-13 09:48:14 +08:00
|
|
|
rspFile := corpusZip.ReplaceExtension(ctx, "rsp")
|
|
|
|
command.FlagWithRspFileInputList("-r ", rspFile, fuzzModule.corpus)
|
2019-11-14 00:36:07 +08:00
|
|
|
files = append(files, fileToZip{corpusZip, ""})
|
|
|
|
}
|
|
|
|
|
2019-11-28 05:45:45 +08:00
|
|
|
// Package the data into a zipfile.
|
|
|
|
if fuzzModule.data != nil {
|
|
|
|
dataZip := archDir.Join(ctx, module.Name()+"_data.zip")
|
2020-11-17 09:32:30 +08:00
|
|
|
command := builder.Command().BuiltTool("soong_zip").
|
2019-11-28 05:45:45 +08:00
|
|
|
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, ""})
|
|
|
|
}
|
|
|
|
|
2019-11-14 00:36:07 +08:00
|
|
|
// Find and mark all the transiently-dependent shared libraries for
|
|
|
|
// packaging.
|
2019-10-18 10:20:41 +08:00
|
|
|
for _, library := range sharedLibraries {
|
2019-11-14 00:36:07 +08:00
|
|
|
files = append(files, fileToZip{library, "lib"})
|
2019-11-13 03:12:10 +08:00
|
|
|
|
2019-10-18 10:20:41 +08:00
|
|
|
// For each architecture-specific shared library dependency, we need to
|
|
|
|
// install it to the output directory. Setup the install destination here,
|
|
|
|
// which will be used by $(copy-many-files) in the Make backend.
|
|
|
|
installDestination := sharedLibraryInstallLocation(
|
|
|
|
library, ccModule.Host(), archString)
|
2019-11-21 07:58:32 +08:00
|
|
|
if sharedLibraryInstalled[installDestination] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sharedLibraryInstalled[installDestination] = true
|
2020-03-07 01:38:12 +08:00
|
|
|
|
2019-10-18 10:20:41 +08:00
|
|
|
// Escape all the variables, as the install destination here will be called
|
|
|
|
// via. $(eval) in Make.
|
|
|
|
installDestination = strings.ReplaceAll(
|
|
|
|
installDestination, "$", "$$")
|
|
|
|
s.sharedLibInstallStrings = append(s.sharedLibInstallStrings,
|
|
|
|
library.String()+":"+installDestination)
|
2020-03-07 01:38:12 +08:00
|
|
|
|
|
|
|
// Ensure that on device, the library is also reinstalled to the /symbols/
|
|
|
|
// dir. Symbolized DSO's are always installed to the device when fuzzing, but
|
|
|
|
// we want symbolization tools (like `stack`) to be able to find the symbols
|
|
|
|
// in $ANDROID_PRODUCT_OUT/symbols automagically.
|
|
|
|
if !ccModule.Host() {
|
|
|
|
symbolsInstallDestination := sharedLibrarySymbolsInstallLocation(library, archString)
|
|
|
|
symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
|
|
|
|
s.sharedLibInstallStrings = append(s.sharedLibInstallStrings,
|
|
|
|
library.String()+":"+symbolsInstallDestination)
|
|
|
|
}
|
2019-10-18 10:20:41 +08:00
|
|
|
}
|
|
|
|
|
2019-09-25 04:03:28 +08:00
|
|
|
// The executable.
|
2019-11-14 00:36:07 +08:00
|
|
|
files = append(files, fileToZip{ccModule.UnstrippedOutputFile(), ""})
|
2019-09-25 04:03:28 +08:00
|
|
|
|
|
|
|
// The dictionary.
|
|
|
|
if fuzzModule.dictionary != nil {
|
2019-11-14 00:36:07 +08:00
|
|
|
files = append(files, fileToZip{fuzzModule.dictionary, ""})
|
2019-09-25 04:03:28 +08:00
|
|
|
}
|
2019-10-23 01:52:01 +08:00
|
|
|
|
|
|
|
// Additional fuzz config.
|
|
|
|
if fuzzModule.config != nil {
|
2019-11-14 00:36:07 +08:00
|
|
|
files = append(files, fileToZip{fuzzModule.config, ""})
|
|
|
|
}
|
|
|
|
|
|
|
|
fuzzZip := archDir.Join(ctx, module.Name()+".zip")
|
2020-11-17 09:32:30 +08:00
|
|
|
command := builder.Command().BuiltTool("soong_zip").
|
2019-11-14 00:36:07 +08:00
|
|
|
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)
|
2019-10-23 01:52:01 +08:00
|
|
|
}
|
2019-11-14 00:36:07 +08:00
|
|
|
|
2020-11-17 09:32:30 +08:00
|
|
|
builder.Build("create-"+fuzzZip.String(),
|
2019-11-14 00:36:07 +08:00
|
|
|
"Package "+module.Name()+" for "+archString+"-"+hostOrTargetString)
|
|
|
|
|
2020-02-25 00:26:20 +08:00
|
|
|
// Don't add modules to 'make haiku' that are set to not be exported to the
|
|
|
|
// fuzzing infrastructure.
|
|
|
|
if config := fuzzModule.Properties.Fuzz_config; config != nil {
|
|
|
|
if ccModule.Host() && !BoolDefault(config.Fuzz_on_haiku_host, true) {
|
|
|
|
return
|
|
|
|
} else if !BoolDefault(config.Fuzz_on_haiku_device, true) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.fuzzTargets[module.Name()] = true
|
2019-11-21 07:58:32 +08:00
|
|
|
archDirs[archOs] = append(archDirs[archOs], fileToZip{fuzzZip, ""})
|
2019-09-25 04:03:28 +08:00
|
|
|
})
|
|
|
|
|
2019-11-21 07:58:32 +08:00
|
|
|
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
|
2020-11-17 09:32:30 +08:00
|
|
|
builder := android.NewRuleBuilder(pctx, ctx)
|
2019-09-25 04:03:28 +08:00
|
|
|
outputFile := android.PathForOutput(ctx, "fuzz-"+hostOrTarget+"-"+arch+".zip")
|
2019-09-28 05:00:06 +08:00
|
|
|
s.packages = append(s.packages, outputFile)
|
2019-09-25 04:03:28 +08:00
|
|
|
|
2020-11-17 09:32:30 +08:00
|
|
|
command := builder.Command().BuiltTool("soong_zip").
|
2019-09-25 04:03:28 +08:00
|
|
|
Flag("-j").
|
2019-11-14 00:36:07 +08:00
|
|
|
FlagWithOutput("-o ", outputFile).
|
|
|
|
Flag("-L 0") // No need to try and re-compress the zipfiles.
|
2019-09-25 04:03:28 +08:00
|
|
|
|
|
|
|
for _, fileToZip := range filesToZip {
|
2019-11-14 00:36:07 +08:00
|
|
|
if fileToZip.DestinationPathPrefix != "" {
|
|
|
|
command.FlagWithArg("-P ", fileToZip.DestinationPathPrefix)
|
|
|
|
} else {
|
|
|
|
command.Flag("-P ''")
|
|
|
|
}
|
|
|
|
command.FlagWithInput("-f ", fileToZip.SourceFilePath)
|
2019-09-25 04:03:28 +08:00
|
|
|
}
|
|
|
|
|
2020-11-17 09:32:30 +08:00
|
|
|
builder.Build("create-fuzz-package-"+arch+"-"+hostOrTarget,
|
2019-09-25 04:03:28 +08:00
|
|
|
"Create fuzz target packages for "+arch+"-"+hostOrTarget)
|
|
|
|
}
|
2019-09-28 05:00:06 +08:00
|
|
|
}
|
2019-09-25 04:03:28 +08:00
|
|
|
|
2019-09-28 05:00:06 +08:00
|
|
|
func (s *fuzzPackager) MakeVars(ctx android.MakeVarsContext) {
|
2019-10-18 10:20:41 +08:00
|
|
|
packages := s.packages.Strings()
|
|
|
|
sort.Strings(packages)
|
|
|
|
sort.Strings(s.sharedLibInstallStrings)
|
2019-09-28 05:00:06 +08:00
|
|
|
// TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
|
|
|
|
// ready to handle phony targets created in Soong. In the meantime, this
|
|
|
|
// exports the phony 'fuzz' target and dependencies on packages to
|
|
|
|
// core/main.mk so that we can use dist-for-goals.
|
2019-10-18 10:20:41 +08:00
|
|
|
ctx.Strict("SOONG_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
|
|
|
|
ctx.Strict("FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
|
|
|
|
strings.Join(s.sharedLibInstallStrings, " "))
|
|
|
|
|
|
|
|
// 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_FUZZ_TARGETS", strings.Join(fuzzTargets, " "))
|
2019-09-25 04:03:28 +08:00
|
|
|
}
|