2019-07-15 18:34:09 +02: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-08-29 01:04:36 +02:00
|
|
|
"path/filepath"
|
2019-10-18 04:20:41 +02:00
|
|
|
"sort"
|
2019-09-27 23:00:06 +02:00
|
|
|
"strings"
|
2019-08-29 01:04:36 +02:00
|
|
|
|
2021-02-09 13:30:33 +01:00
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
|
2019-07-15 18:34:09 +02:00
|
|
|
"android/soong/android"
|
|
|
|
"android/soong/cc/config"
|
2021-07-22 21:05:08 +02:00
|
|
|
"android/soong/fuzz"
|
2019-07-15 18:34:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-06-07 22:12:06 +02:00
|
|
|
android.RegisterModuleType("cc_fuzz", LibFuzzFactory)
|
2023-05-16 02:58:37 +02:00
|
|
|
android.RegisterParallelSingletonType("cc_fuzz_packaging", fuzzPackagingFactory)
|
2023-07-07 20:11:51 +02:00
|
|
|
android.RegisterParallelSingletonType("cc_fuzz_presubmit_packaging", fuzzPackagingFactoryPresubmit)
|
2022-06-07 22:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type FuzzProperties struct {
|
2022-07-22 19:22:02 +02:00
|
|
|
FuzzFramework fuzz.Framework `blueprint:"mutated"`
|
2022-06-07 22:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type fuzzer struct {
|
|
|
|
Properties FuzzProperties
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzzer *fuzzer) flags(ctx ModuleContext, flags Flags) Flags {
|
2022-07-22 19:22:02 +02:00
|
|
|
if fuzzer.Properties.FuzzFramework == fuzz.AFL {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, []string{
|
|
|
|
"-fsanitize-coverage=trace-pc-guard",
|
|
|
|
"-Wno-unused-result",
|
|
|
|
"-Wno-unused-parameter",
|
|
|
|
"-Wno-unused-function",
|
|
|
|
}...)
|
2022-06-07 22:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzzer *fuzzer) props() []interface{} {
|
|
|
|
return []interface{}{&fuzzer.Properties}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fuzzMutatorDeps(mctx android.TopDownMutatorContext) {
|
|
|
|
currentModule, ok := mctx.Module().(*Module)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-22 19:22:02 +02:00
|
|
|
if currentModule.fuzzer == nil {
|
2022-06-07 22:12:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mctx.WalkDeps(func(child android.Module, parent android.Module) bool {
|
|
|
|
c, ok := child.(*Module)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.sanitize == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
isFuzzerPointer := c.sanitize.getSanitizerBoolPtr(Fuzzer)
|
|
|
|
if isFuzzerPointer == nil || !*isFuzzerPointer {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.fuzzer == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-22 19:22:02 +02:00
|
|
|
c.fuzzer.Properties.FuzzFramework = currentModule.fuzzer.Properties.FuzzFramework
|
2022-06-07 22:12:06 +02:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-15 18:34:09 +02: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.
|
2022-06-07 22:12:06 +02:00
|
|
|
func LibFuzzFactory() android.Module {
|
2022-07-22 19:22:02 +02:00
|
|
|
module := NewFuzzer(android.HostAndDeviceSupported)
|
2022-06-07 22:12:06 +02:00
|
|
|
return module.Init()
|
2019-07-15 18:34:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type fuzzBinary struct {
|
|
|
|
*binaryDecorator
|
|
|
|
*baseCompiler
|
2022-06-07 22:12:06 +02:00
|
|
|
fuzzPackagedModule fuzz.FuzzPackagedModule
|
2021-07-07 23:00:07 +02:00
|
|
|
installedSharedDeps []string
|
2023-04-06 00:08:46 +02:00
|
|
|
sharedLibraries android.RuleBuilderInstalls
|
2019-07-15 18:34:09 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
func (fuzz *fuzzBinary) fuzzBinary() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-07-15 18:34:09 +02:00
|
|
|
func (fuzz *fuzzBinary) linkerProps() []interface{} {
|
|
|
|
props := fuzz.binaryDecorator.linkerProps()
|
2021-07-07 23:00:07 +02:00
|
|
|
props = append(props, &fuzz.fuzzPackagedModule.FuzzProperties)
|
2022-07-22 19:22:02 +02:00
|
|
|
|
2019-07-15 18:34:09 +02:00
|
|
|
return props
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
|
|
|
|
fuzz.binaryDecorator.linkerInit(ctx)
|
|
|
|
}
|
|
|
|
|
2022-06-07 22:12:06 +02:00
|
|
|
func (fuzzBin *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
|
2022-07-22 19:22:02 +02:00
|
|
|
if ctx.Config().Getenv("FUZZ_FRAMEWORK") == "AFL" {
|
2022-06-07 22:12:06 +02:00
|
|
|
deps.HeaderLibs = append(deps.HeaderLibs, "libafl_headers")
|
|
|
|
} else {
|
|
|
|
deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeLibrary(ctx.toolchain()))
|
2022-10-21 18:34:21 +02:00
|
|
|
// Fuzzers built with HWASAN should use the interceptors for better
|
|
|
|
// mutation based on signals in strcmp, memcpy, etc. This is only needed for
|
|
|
|
// fuzz targets, not generic HWASAN-ified binaries or libraries.
|
|
|
|
if module, ok := ctx.Module().(*Module); ok {
|
|
|
|
if module.IsSanitizerEnabled(Hwasan) {
|
|
|
|
deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeInterceptors(ctx.toolchain()))
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 22:12:06 +02:00
|
|
|
}
|
2022-07-22 19:22:02 +02:00
|
|
|
|
|
|
|
deps = fuzzBin.binaryDecorator.linkerDeps(ctx, deps)
|
|
|
|
return deps
|
2019-07-15 18:34:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
|
|
|
|
flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
|
2019-11-14 23:50:47 +01: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.
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
|
2022-06-07 22:12:06 +02:00
|
|
|
|
2022-10-25 19:58:59 +02:00
|
|
|
// When running on device, fuzz targets with vendor: true set will be in
|
|
|
|
// fuzzer_name/vendor/fuzzer_name (note the extra 'vendor' and thus need to
|
|
|
|
// link with libraries in ../../lib/. Non-vendor binaries only need to look
|
|
|
|
// one level up, in ../lib/.
|
|
|
|
if ctx.inVendor() {
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../../lib`)
|
|
|
|
} else {
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
|
|
|
|
}
|
|
|
|
|
2019-07-15 18:34:09 +02:00
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2021-10-14 18:22:09 +02:00
|
|
|
// IsValidSharedDependency takes a module and determines if it is a unique shared library
|
2019-10-18 04:20:41 +02:00
|
|
|
// that should be installed in the fuzz target output directories. This function
|
|
|
|
// returns true, unless:
|
2022-08-16 19:27:33 +02:00
|
|
|
// - The module is not an installable shared library, or
|
|
|
|
// - The module is a header or stub, or
|
|
|
|
// - The module is a prebuilt and its source is available, or
|
|
|
|
// - The module is a versioned member of an SDK snapshot.
|
2021-10-14 18:22:09 +02:00
|
|
|
func IsValidSharedDependency(dependency android.Module) bool {
|
2019-10-18 04:20:41 +02:00
|
|
|
// TODO(b/144090547): We should be parsing these modules using
|
|
|
|
// ModuleDependencyTag instead of the current brute-force checking.
|
|
|
|
|
2020-10-24 02:22:06 +02:00
|
|
|
linkable, ok := dependency.(LinkableInterface)
|
|
|
|
if !ok || !linkable.CcLibraryInterface() {
|
|
|
|
// Discard non-linkables.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !linkable.Shared() {
|
|
|
|
// Discard static libs.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
|
2019-11-12 23:03:31 +01: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 04:20:41 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:03:31 +01: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 13:30:33 +01:00
|
|
|
// Discard installable:false libraries because they are expected to be absent
|
|
|
|
// in runtime.
|
2021-10-28 22:25:54 +02:00
|
|
|
if !proptools.BoolDefault(ccLibrary.Installable(), true) {
|
2021-02-09 13:30:33 +01:00
|
|
|
return false
|
|
|
|
}
|
2019-11-12 23:03:31 +01:00
|
|
|
}
|
|
|
|
|
2020-10-06 03:36:43 +02: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 11:41:21 +02:00
|
|
|
if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
|
2020-10-06 03:36:43 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-10-18 04:20:41 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-02-06 19:31:02 +01:00
|
|
|
func SharedLibraryInstallLocation(
|
2023-04-06 00:08:46 +02:00
|
|
|
libraryBase string, isHost bool, fuzzDir string, archString string) string {
|
2019-10-18 04:20:41 +02:00
|
|
|
installLocation := "$(PRODUCT_OUT)/data"
|
|
|
|
if isHost {
|
|
|
|
installLocation = "$(HOST_OUT)"
|
|
|
|
}
|
|
|
|
installLocation = filepath.Join(
|
2023-04-06 00:08:46 +02:00
|
|
|
installLocation, fuzzDir, archString, "lib", libraryBase)
|
2019-10-18 04:20:41 +02:00
|
|
|
return installLocation
|
|
|
|
}
|
|
|
|
|
2020-03-06 18:38:12 +01:00
|
|
|
// Get the device-only shared library symbols install directory.
|
2023-04-06 00:08:46 +02:00
|
|
|
func SharedLibrarySymbolsInstallLocation(libraryBase string, fuzzDir string, archString string) string {
|
|
|
|
return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, "/lib/", libraryBase)
|
2020-03-06 18:38:12 +01:00
|
|
|
}
|
|
|
|
|
2022-06-07 22:12:06 +02:00
|
|
|
func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) {
|
|
|
|
installBase := "fuzz"
|
|
|
|
|
|
|
|
fuzzBin.binaryDecorator.baseInstaller.dir = filepath.Join(
|
|
|
|
installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
|
|
|
|
fuzzBin.binaryDecorator.baseInstaller.dir64 = filepath.Join(
|
|
|
|
installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
|
|
|
|
fuzzBin.binaryDecorator.baseInstaller.install(ctx, file)
|
2019-09-14 02:32:50 +02:00
|
|
|
|
2023-02-06 19:31:02 +01:00
|
|
|
fuzzBin.fuzzPackagedModule = PackageFuzzModule(ctx, fuzzBin.fuzzPackagedModule, pctx)
|
|
|
|
|
|
|
|
// Grab the list of required shared libraries.
|
|
|
|
fuzzBin.sharedLibraries, _ = CollectAllSharedDependencies(ctx)
|
|
|
|
|
2023-04-06 00:08:46 +02:00
|
|
|
for _, ruleBuilderInstall := range fuzzBin.sharedLibraries {
|
|
|
|
install := ruleBuilderInstall.To
|
2023-02-06 19:31:02 +01:00
|
|
|
fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
|
|
|
|
SharedLibraryInstallLocation(
|
2023-04-06 00:08:46 +02:00
|
|
|
install, ctx.Host(), installBase, ctx.Arch().ArchType.String()))
|
2023-02-06 19:31:02 +01:00
|
|
|
|
|
|
|
// Also add the dependency on the shared library symbols dir.
|
|
|
|
if !ctx.Host() {
|
|
|
|
fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
|
2023-04-06 00:08:46 +02:00
|
|
|
SharedLibrarySymbolsInstallLocation(install, installBase, ctx.Arch().ArchType.String()))
|
2023-02-06 19:31:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PackageFuzzModule(ctx android.ModuleContext, fuzzPackagedModule fuzz.FuzzPackagedModule, pctx android.PackageContext) fuzz.FuzzPackagedModule {
|
|
|
|
fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Corpus)
|
2019-10-18 00:04:01 +02:00
|
|
|
intermediateDir := android.PathForModuleOut(ctx, "corpus")
|
2023-07-11 06:31:36 +02:00
|
|
|
|
|
|
|
// Create one rule per file to avoid MAX_ARG_STRLEN hardlimit.
|
2023-02-06 19:31:02 +01:00
|
|
|
for _, entry := range fuzzPackagedModule.Corpus {
|
2023-07-11 06:31:36 +02:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: android.Cp,
|
|
|
|
Output: intermediateDir.Join(ctx, entry.Base()),
|
|
|
|
Input: entry,
|
|
|
|
})
|
2019-10-18 00:04:01 +02:00
|
|
|
}
|
2023-02-06 19:31:02 +01:00
|
|
|
fuzzPackagedModule.CorpusIntermediateDir = intermediateDir
|
2019-10-18 00:04:01 +02:00
|
|
|
|
2023-02-06 19:31:02 +01:00
|
|
|
fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Data)
|
2019-11-27 22:45:45 +01:00
|
|
|
intermediateDir = android.PathForModuleOut(ctx, "data")
|
2023-07-11 06:31:36 +02:00
|
|
|
|
|
|
|
// Create one rule per file to avoid MAX_ARG_STRLEN hardlimit.
|
2023-02-06 19:31:02 +01:00
|
|
|
for _, entry := range fuzzPackagedModule.Data {
|
2023-07-11 06:31:36 +02:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: android.Cp,
|
|
|
|
Output: intermediateDir.Join(ctx, entry.Rel()),
|
|
|
|
Input: entry,
|
|
|
|
})
|
2019-11-27 22:45:45 +01:00
|
|
|
}
|
2023-02-06 19:31:02 +01:00
|
|
|
fuzzPackagedModule.DataIntermediateDir = intermediateDir
|
2019-11-27 22:45:45 +01:00
|
|
|
|
2023-02-06 19:31:02 +01:00
|
|
|
if fuzzPackagedModule.FuzzProperties.Dictionary != nil {
|
|
|
|
fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzPackagedModule.FuzzProperties.Dictionary)
|
|
|
|
if fuzzPackagedModule.Dictionary.Ext() != ".dict" {
|
2019-09-14 02:32:50 +02:00
|
|
|
ctx.PropertyErrorf("dictionary",
|
|
|
|
"Fuzzer dictionary %q does not have '.dict' extension",
|
2023-02-06 19:31:02 +01:00
|
|
|
fuzzPackagedModule.Dictionary.String())
|
2019-09-14 02:32:50 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-22 19:52:01 +02:00
|
|
|
|
2023-02-06 19:31:02 +01:00
|
|
|
if fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
|
2019-10-30 18:17:04 +01:00
|
|
|
configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
|
2023-02-06 19:31:02 +01:00
|
|
|
android.WriteFileRule(ctx, configPath, fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
|
|
|
|
fuzzPackagedModule.Config = configPath
|
2019-10-18 04:20:41 +02:00
|
|
|
}
|
2023-02-06 19:31:02 +01:00
|
|
|
return fuzzPackagedModule
|
2019-07-15 18:34:09 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 19:22:02 +02:00
|
|
|
func NewFuzzer(hod android.HostOrDeviceSupported) *Module {
|
2021-11-01 20:32:43 +01:00
|
|
|
module, binary := newBinary(hod, false)
|
2022-06-07 22:12:06 +02:00
|
|
|
baseInstallerPath := "fuzz"
|
2019-07-15 18:34:09 +02:00
|
|
|
|
2022-06-07 22:12:06 +02:00
|
|
|
binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData)
|
2019-07-15 18:34:09 +02:00
|
|
|
|
2022-06-07 22:12:06 +02:00
|
|
|
fuzzBin := &fuzzBinary{
|
2019-07-15 18:34:09 +02:00
|
|
|
binaryDecorator: binary,
|
|
|
|
baseCompiler: NewBaseCompiler(),
|
|
|
|
}
|
2022-06-07 22:12:06 +02:00
|
|
|
module.compiler = fuzzBin
|
|
|
|
module.linker = fuzzBin
|
|
|
|
module.installer = fuzzBin
|
2019-07-19 01:20:52 +02:00
|
|
|
|
2022-07-22 19:22:02 +02:00
|
|
|
module.fuzzer.Properties.FuzzFramework = fuzz.LibFuzzer
|
|
|
|
|
2019-07-19 01:20:52 +02: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) {
|
2022-10-03 21:07:37 +02:00
|
|
|
|
|
|
|
extraProps := struct {
|
|
|
|
Sanitize struct {
|
|
|
|
Fuzzer *bool
|
|
|
|
}
|
2019-07-19 01:20:52 +02:00
|
|
|
Target struct {
|
|
|
|
Darwin struct {
|
|
|
|
Enabled *bool
|
|
|
|
}
|
2019-07-24 22:34:19 +02:00
|
|
|
Linux_bionic struct {
|
|
|
|
Enabled *bool
|
|
|
|
}
|
2019-07-19 01:20:52 +02:00
|
|
|
}
|
|
|
|
}{}
|
2022-10-03 21:07:37 +02:00
|
|
|
extraProps.Sanitize.Fuzzer = BoolPtr(true)
|
|
|
|
extraProps.Target.Darwin.Enabled = BoolPtr(false)
|
|
|
|
extraProps.Target.Linux_bionic.Enabled = BoolPtr(false)
|
|
|
|
ctx.AppendProperties(&extraProps)
|
2022-06-07 22:12:06 +02:00
|
|
|
|
2022-07-22 19:22:02 +02:00
|
|
|
targetFramework := fuzz.GetFramework(ctx, fuzz.Cc)
|
|
|
|
if !fuzz.IsValidFrameworkForModule(targetFramework, fuzz.Cc, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzzing_frameworks) {
|
|
|
|
ctx.Module().Disable()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetFramework == fuzz.AFL {
|
|
|
|
fuzzBin.baseCompiler.Properties.Srcs = append(fuzzBin.baseCompiler.Properties.Srcs, ":aflpp_driver", ":afl-compiler-rt")
|
|
|
|
module.fuzzer.Properties.FuzzFramework = fuzz.AFL
|
|
|
|
}
|
|
|
|
})
|
2022-08-08 17:55:12 +02:00
|
|
|
|
2019-07-15 18:34:09 +02:00
|
|
|
return module
|
|
|
|
}
|
2019-09-24 22:03:28 +02:00
|
|
|
|
|
|
|
// Responsible for generating GNU Make rules that package fuzz targets into
|
|
|
|
// their architecture & target/host specific zip file.
|
2023-02-06 19:31:02 +01:00
|
|
|
type ccRustFuzzPackager struct {
|
2021-07-22 21:05:08 +02:00
|
|
|
fuzz.FuzzPackager
|
2023-07-07 20:11:51 +02:00
|
|
|
fuzzPackagingArchModules string
|
|
|
|
fuzzTargetSharedDepsInstallPairs string
|
|
|
|
allFuzzTargetsName string
|
|
|
|
onlyIncludePresubmits bool
|
2019-09-24 22:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func fuzzPackagingFactory() android.Singleton {
|
2022-06-07 22:12:06 +02:00
|
|
|
|
2023-02-06 19:31:02 +01:00
|
|
|
fuzzPackager := &ccRustFuzzPackager{
|
2022-06-07 22:12:06 +02:00
|
|
|
fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES",
|
|
|
|
fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
|
|
|
|
allFuzzTargetsName: "ALL_FUZZ_TARGETS",
|
2023-07-07 20:11:51 +02:00
|
|
|
onlyIncludePresubmits: false,
|
|
|
|
}
|
|
|
|
return fuzzPackager
|
|
|
|
}
|
|
|
|
|
|
|
|
func fuzzPackagingFactoryPresubmit() android.Singleton {
|
|
|
|
|
|
|
|
fuzzPackager := &ccRustFuzzPackager{
|
|
|
|
fuzzPackagingArchModules: "SOONG_PRESUBMIT_FUZZ_PACKAGING_ARCH_MODULES",
|
|
|
|
fuzzTargetSharedDepsInstallPairs: "PRESUBMIT_FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
|
|
|
|
allFuzzTargetsName: "ALL_PRESUBMIT_FUZZ_TARGETS",
|
|
|
|
onlyIncludePresubmits: true,
|
2022-06-07 22:12:06 +02:00
|
|
|
}
|
|
|
|
return fuzzPackager
|
2019-10-18 04:20:41 +02:00
|
|
|
}
|
|
|
|
|
2023-02-06 19:31:02 +01:00
|
|
|
func (s *ccRustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
|
2019-09-24 22:03:28 +02:00
|
|
|
// 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}).
|
2021-07-22 21:05:08 +02:00
|
|
|
archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
|
2019-09-24 22:03:28 +02:00
|
|
|
|
2019-10-18 04:20:41 +02:00
|
|
|
// List of individual fuzz targets, so that 'make fuzz' also installs the targets
|
|
|
|
// to the correct output directories as well.
|
2021-07-07 23:00:07 +02:00
|
|
|
s.FuzzTargets = make(map[string]bool)
|
2019-10-18 04:20:41 +02:00
|
|
|
|
2021-10-14 18:22:09 +02: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-09-24 22:03:28 +02:00
|
|
|
ctx.VisitAllModules(func(module android.Module) {
|
2023-02-06 19:31:02 +01:00
|
|
|
ccModule, ok := module.(LinkableInterface)
|
|
|
|
if !ok || ccModule.PreventInstall() {
|
2019-09-24 22:03:28 +02:00
|
|
|
return
|
|
|
|
}
|
2021-07-07 23:00:07 +02:00
|
|
|
// Discard non-fuzz targets.
|
2023-02-06 19:31:02 +01:00
|
|
|
if ok := fuzz.IsValid(ccModule.FuzzModuleStruct()); !ok {
|
2019-09-24 22:03:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-07 22:12:06 +02:00
|
|
|
sharedLibsInstallDirPrefix := "lib"
|
2023-02-06 19:31:02 +01:00
|
|
|
if !ccModule.IsFuzzModule() {
|
2019-12-05 16:36:11 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-24 22:03:28 +02:00
|
|
|
hostOrTargetString := "target"
|
2023-05-17 02:54:27 +02:00
|
|
|
if ccModule.Target().HostCross {
|
|
|
|
hostOrTargetString = "host_cross"
|
|
|
|
} else if ccModule.Host() {
|
2019-09-24 22:03:28 +02:00
|
|
|
hostOrTargetString = "host"
|
|
|
|
}
|
2023-07-07 20:11:51 +02:00
|
|
|
if s.onlyIncludePresubmits == true {
|
|
|
|
hostOrTargetString = "presubmit-" + hostOrTargetString
|
|
|
|
}
|
2019-09-24 22:03:28 +02:00
|
|
|
|
2022-06-07 22:12:06 +02:00
|
|
|
fpm := fuzz.FuzzPackagedModule{}
|
|
|
|
if ok {
|
2023-02-06 19:31:02 +01:00
|
|
|
fpm = ccModule.FuzzPackagedModule()
|
2022-06-07 22:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
intermediatePath := "fuzz"
|
|
|
|
|
2023-02-06 19:31:02 +01:00
|
|
|
archString := ccModule.Target().Arch.ArchType.String()
|
2022-06-07 22:12:06 +02:00
|
|
|
archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString)
|
2021-07-22 21:05:08 +02:00
|
|
|
archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
|
2019-09-24 22:03:28 +02:00
|
|
|
|
2021-07-22 21:05:08 +02:00
|
|
|
var files []fuzz.FileToZip
|
2020-11-17 02:32:30 +01:00
|
|
|
builder := android.NewRuleBuilder(pctx, ctx)
|
2019-11-13 17:36:07 +01:00
|
|
|
|
2021-07-07 23:00:07 +02:00
|
|
|
// Package the corpus, data, dict and config into a zipfile.
|
2022-06-07 22:12:06 +02:00
|
|
|
files = s.PackageArtifacts(ctx, module, fpm, archDir, builder)
|
2019-11-27 22:45:45 +01:00
|
|
|
|
2021-10-14 18:22:09 +02:00
|
|
|
// Package shared libraries
|
2023-02-06 19:31:02 +01:00
|
|
|
files = append(files, GetSharedLibsToZip(ccModule.FuzzSharedLibraries(), ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...)
|
2019-10-18 04:20:41 +02:00
|
|
|
|
2019-09-24 22:03:28 +02:00
|
|
|
// The executable.
|
2023-05-09 00:09:31 +02:00
|
|
|
files = append(files, fuzz.FileToZip{SourceFilePath: android.OutputFileForModule(ctx, ccModule, "unstripped")})
|
2019-09-24 22:03:28 +02:00
|
|
|
|
2023-07-07 20:11:51 +02:00
|
|
|
if s.onlyIncludePresubmits == true {
|
|
|
|
if fpm.FuzzProperties.Fuzz_config == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !BoolDefault(fpm.FuzzProperties.Fuzz_config.Use_for_presubmit, false){
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 22:12:06 +02:00
|
|
|
archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
|
2021-07-07 23:00:07 +02:00
|
|
|
if !ok {
|
|
|
|
return
|
2020-02-24 17:26:20 +01:00
|
|
|
}
|
2019-09-24 22:03:28 +02:00
|
|
|
})
|
|
|
|
|
2022-07-22 19:22:02 +02:00
|
|
|
s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx)
|
2019-09-27 23:00:06 +02:00
|
|
|
}
|
2019-09-24 22:03:28 +02:00
|
|
|
|
2023-02-06 19:31:02 +01:00
|
|
|
func (s *ccRustFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
|
2021-07-07 23:00:07 +02:00
|
|
|
packages := s.Packages.Strings()
|
2019-10-18 04:20:41 +02:00
|
|
|
sort.Strings(packages)
|
2021-10-14 18:22:09 +02:00
|
|
|
sort.Strings(s.FuzzPackager.SharedLibInstallStrings)
|
2019-09-27 23:00:06 +02: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.
|
2022-06-07 22:12:06 +02:00
|
|
|
|
|
|
|
ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " "))
|
|
|
|
|
|
|
|
ctx.Strict(s.fuzzTargetSharedDepsInstallPairs,
|
2021-10-14 18:22:09 +02:00
|
|
|
strings.Join(s.FuzzPackager.SharedLibInstallStrings, " "))
|
2019-10-18 04:20:41 +02:00
|
|
|
|
|
|
|
// Preallocate the slice of fuzz targets to minimise memory allocations.
|
2022-06-07 22:12:06 +02:00
|
|
|
s.PreallocateSlice(ctx, s.allFuzzTargetsName)
|
2019-09-24 22:03:28 +02:00
|
|
|
}
|
2021-10-14 18:22:09 +02:00
|
|
|
|
|
|
|
// GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for
|
|
|
|
// packaging.
|
2023-04-06 00:08:46 +02:00
|
|
|
func GetSharedLibsToZip(sharedLibraries android.RuleBuilderInstalls, module LinkableInterface, s *fuzz.FuzzPackager, archString string, destinationPathPrefix string, sharedLibraryInstalled *map[string]bool) []fuzz.FileToZip {
|
2021-10-14 18:22:09 +02:00
|
|
|
var files []fuzz.FileToZip
|
|
|
|
|
2022-06-07 22:12:06 +02:00
|
|
|
fuzzDir := "fuzz"
|
|
|
|
|
2023-04-06 00:08:46 +02:00
|
|
|
for _, ruleBuilderInstall := range sharedLibraries {
|
|
|
|
library := ruleBuilderInstall.From
|
|
|
|
install := ruleBuilderInstall.To
|
2023-05-09 00:09:31 +02:00
|
|
|
files = append(files, fuzz.FileToZip{
|
|
|
|
SourceFilePath: library,
|
|
|
|
DestinationPathPrefix: destinationPathPrefix,
|
|
|
|
DestinationPath: install,
|
|
|
|
})
|
2021-10-14 18:22:09 +02: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.
|
2023-02-06 19:31:02 +01:00
|
|
|
installDestination := SharedLibraryInstallLocation(
|
2023-04-06 00:08:46 +02:00
|
|
|
install, module.Host(), fuzzDir, archString)
|
2021-10-14 18:22:09 +02:00
|
|
|
if (*sharedLibraryInstalled)[installDestination] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
(*sharedLibraryInstalled)[installDestination] = true
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
// 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 !module.Host() {
|
2023-04-06 00:08:46 +02:00
|
|
|
symbolsInstallDestination := SharedLibrarySymbolsInstallLocation(install, fuzzDir, archString)
|
2021-10-14 18:22:09 +02:00
|
|
|
symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
|
|
|
|
s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
|
|
|
|
library.String()+":"+symbolsInstallDestination)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
2022-10-05 01:35:39 +02:00
|
|
|
|
|
|
|
// CollectAllSharedDependencies search over the provided module's dependencies using
|
|
|
|
// VisitDirectDeps and WalkDeps to enumerate all shared library dependencies.
|
|
|
|
// VisitDirectDeps is used first to avoid incorrectly using 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.
|
2023-04-06 00:08:46 +02:00
|
|
|
func CollectAllSharedDependencies(ctx android.ModuleContext) (android.RuleBuilderInstalls, []android.Module) {
|
2022-10-05 01:35:39 +02:00
|
|
|
seen := make(map[string]bool)
|
|
|
|
recursed := make(map[string]bool)
|
2022-10-20 02:55:58 +02:00
|
|
|
deps := []android.Module{}
|
2022-10-05 01:35:39 +02:00
|
|
|
|
2023-04-06 00:08:46 +02:00
|
|
|
var sharedLibraries android.RuleBuilderInstalls
|
2022-10-05 01:35:39 +02:00
|
|
|
|
|
|
|
// Enumerate the first level of dependencies, as we discard all non-library
|
|
|
|
// modules in the BFS loop below.
|
|
|
|
ctx.VisitDirectDeps(func(dep android.Module) {
|
|
|
|
if !IsValidSharedDependency(dep) {
|
|
|
|
return
|
|
|
|
}
|
2023-04-06 00:08:46 +02:00
|
|
|
if !ctx.OtherModuleHasProvider(dep, SharedLibraryInfoProvider) {
|
|
|
|
return
|
|
|
|
}
|
2022-10-05 01:35:39 +02:00
|
|
|
if seen[ctx.OtherModuleName(dep)] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
seen[ctx.OtherModuleName(dep)] = true
|
2022-10-20 02:55:58 +02:00
|
|
|
deps = append(deps, dep)
|
2023-04-06 00:08:46 +02:00
|
|
|
|
|
|
|
sharedLibraryInfo := ctx.OtherModuleProvider(dep, SharedLibraryInfoProvider).(SharedLibraryInfo)
|
|
|
|
installDestination := sharedLibraryInfo.SharedLibrary.Base()
|
|
|
|
ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, dep, "unstripped"), installDestination}
|
|
|
|
sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
|
2022-10-05 01:35:39 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
ctx.WalkDeps(func(child, parent android.Module) bool {
|
2023-06-09 20:06:44 +02:00
|
|
|
|
|
|
|
// If this is a Rust module which is not rust_ffi_shared, we still want to bundle any transitive
|
|
|
|
// shared dependencies (even for rust_ffi_static)
|
|
|
|
if rustmod, ok := child.(LinkableInterface); ok && rustmod.RustLibraryInterface() && !rustmod.Shared() {
|
|
|
|
if recursed[ctx.OtherModuleName(child)] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
recursed[ctx.OtherModuleName(child)] = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-10-05 01:35:39 +02:00
|
|
|
if !IsValidSharedDependency(child) {
|
|
|
|
return false
|
|
|
|
}
|
2023-04-06 00:08:46 +02:00
|
|
|
if !ctx.OtherModuleHasProvider(child, SharedLibraryInfoProvider) {
|
|
|
|
return false
|
|
|
|
}
|
2022-10-05 01:35:39 +02:00
|
|
|
if !seen[ctx.OtherModuleName(child)] {
|
|
|
|
seen[ctx.OtherModuleName(child)] = true
|
2022-10-20 02:55:58 +02:00
|
|
|
deps = append(deps, child)
|
2023-04-06 00:08:46 +02:00
|
|
|
|
|
|
|
sharedLibraryInfo := ctx.OtherModuleProvider(child, SharedLibraryInfoProvider).(SharedLibraryInfo)
|
|
|
|
installDestination := sharedLibraryInfo.SharedLibrary.Base()
|
|
|
|
ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, child, "unstripped"), installDestination}
|
|
|
|
sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
|
2022-10-05 01:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if recursed[ctx.OtherModuleName(child)] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
recursed[ctx.OtherModuleName(child)] = true
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2022-10-20 02:55:58 +02:00
|
|
|
return sharedLibraries, deps
|
2022-10-05 01:35:39 +02:00
|
|
|
}
|