Merge "rust: Add support for host fuzzers." into main

This commit is contained in:
Ivan Lozano 2023-08-16 14:07:19 +00:00 committed by Gerrit Code Review
commit b0e1035417
5 changed files with 48 additions and 8 deletions

View file

@ -318,6 +318,15 @@ func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flag
flags.LinkFlags = append(flags.LinkFlags, cc.RpathFlags(ctx)...) flags.LinkFlags = append(flags.LinkFlags, cc.RpathFlags(ctx)...)
} }
if !ctx.toolchain().Bionic() && ctx.Os() != android.LinuxMusl && !ctx.Windows() {
// Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device
// builds. This is irrelevant for the Windows target as these are Posix specific.
flags.LinkFlags = append(flags.LinkFlags,
"-ldl",
"-lpthread",
"-lm",
)
}
return flags return flags
} }

View file

@ -25,6 +25,7 @@ import (
func init() { func init() {
android.RegisterModuleType("rust_fuzz", RustFuzzFactory) android.RegisterModuleType("rust_fuzz", RustFuzzFactory)
android.RegisterModuleType("rust_fuzz_host", RustFuzzHostFactory)
} }
type fuzzDecorator struct { type fuzzDecorator struct {
@ -43,6 +44,11 @@ func RustFuzzFactory() android.Module {
return module.Init() return module.Init()
} }
func RustFuzzHostFactory() android.Module {
module, _ := NewRustFuzz(android.HostSupported)
return module.Init()
}
func NewRustFuzz(hod android.HostOrDeviceSupported) (*Module, *fuzzDecorator) { func NewRustFuzz(hod android.HostOrDeviceSupported) (*Module, *fuzzDecorator) {
module, binary := NewRustBinary(hod) module, binary := NewRustBinary(hod)
fuzz := &fuzzDecorator{ fuzz := &fuzzDecorator{

View file

@ -34,6 +34,10 @@ func TestRustFuzz(t *testing.T) {
srcs: ["foo.rs"], srcs: ["foo.rs"],
rustlibs: ["libtest_fuzzing"], rustlibs: ["libtest_fuzzing"],
} }
rust_fuzz_host {
name: "host_fuzzer",
srcs: ["foo.rs"],
}
`) `)
// Check that appropriate dependencies are added and that the rustlib linkage is correct. // Check that appropriate dependencies are added and that the rustlib linkage is correct.
@ -50,7 +54,13 @@ func TestRustFuzz(t *testing.T) {
if !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-C passes='sancov-module'") || if !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-C passes='sancov-module'") ||
!strings.Contains(fuzz_libtest.Args["rustcFlags"], "--cfg fuzzing") { !strings.Contains(fuzz_libtest.Args["rustcFlags"], "--cfg fuzzing") {
t.Errorf("rust_fuzz module does not contain the expected flags (sancov-module, cfg fuzzing).") t.Errorf("rust_fuzz module does not contain the expected flags (sancov-module, cfg fuzzing).")
}
// Check that host modules support fuzzing.
host_fuzzer := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Rule("rustc")
if !strings.Contains(host_fuzzer.Args["rustcFlags"], "-C passes='sancov-module'") ||
!strings.Contains(host_fuzzer.Args["rustcFlags"], "--cfg fuzzing") {
t.Errorf("rust_fuzz_host module does not contain the expected flags (sancov-module, cfg fuzzing).")
} }
// Check that dependencies have 'fuzzer' variants produced for them as well. // Check that dependencies have 'fuzzer' variants produced for them as well.

View file

@ -209,8 +209,8 @@ func (sanitize *sanitize) begin(ctx BaseModuleContext) {
} }
// TODO:(b/178369775) // TODO:(b/178369775)
// For now sanitizing is only supported on devices // For now sanitizing is only supported on non-windows targets
if ctx.Os() == android.Android && (Bool(s.Hwaddress) || Bool(s.Address) || Bool(s.Memtag_heap) || Bool(s.Fuzzer)) { if ctx.Os() != android.Windows && (Bool(s.Hwaddress) || Bool(s.Address) || Bool(s.Memtag_heap) || Bool(s.Fuzzer)) {
sanitize.Properties.SanitizerEnabled = true sanitize.Properties.SanitizerEnabled = true
} }
} }
@ -234,6 +234,11 @@ func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags, deps PathDeps) (
if Bool(sanitize.Properties.Sanitize.Address) { if Bool(sanitize.Properties.Sanitize.Address) {
flags.RustFlags = append(flags.RustFlags, asanFlags...) flags.RustFlags = append(flags.RustFlags, asanFlags...)
if ctx.Host() {
// -nodefaultlibs (provided with libc++) prevents the driver from linking
// libraries needed with -fsanitize=address. http://b/18650275 (WAI)
flags.LinkFlags = append(flags.LinkFlags, []string{"-Wl,--no-as-needed"}...)
}
} }
return flags, deps return flags, deps
} }
@ -273,10 +278,17 @@ func rustSanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
var deps []string var deps []string
if mod.IsSanitizerEnabled(cc.Asan) { if mod.IsSanitizerEnabled(cc.Asan) {
variations = append(variations, if mod.Host() {
blueprint.Variation{Mutator: "link", Variation: "shared"}) variations = append(variations,
depTag = cc.SharedDepTag() blueprint.Variation{Mutator: "link", Variation: "static"})
deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan")} depTag = cc.StaticDepTag(false)
deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan.static")}
} else {
variations = append(variations,
blueprint.Variation{Mutator: "link", Variation: "shared"})
depTag = cc.SharedDepTag()
deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan")}
}
} else if mod.IsSanitizerEnabled(cc.Hwasan) { } else if mod.IsSanitizerEnabled(cc.Hwasan) {
// TODO(b/204776996): HWASan for static Rust binaries isn't supported yet. // TODO(b/204776996): HWASan for static Rust binaries isn't supported yet.
if binary, ok := mod.compiler.(binaryInterface); ok { if binary, ok := mod.compiler.(binaryInterface); ok {
@ -391,7 +403,8 @@ func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, entries *android.Andro
} }
func (mod *Module) SanitizerSupported(t cc.SanitizerType) bool { func (mod *Module) SanitizerSupported(t cc.SanitizerType) bool {
if mod.Host() { // Sanitizers are not supported on Windows targets.
if mod.Os() == android.Windows {
return false return false
} }
switch t { switch t {
@ -417,7 +430,8 @@ func (mod *Module) IsSanitizerEnabled(t cc.SanitizerType) bool {
} }
func (mod *Module) IsSanitizerExplicitlyDisabled(t cc.SanitizerType) bool { func (mod *Module) IsSanitizerExplicitlyDisabled(t cc.SanitizerType) bool {
if mod.Host() { // Sanitizers are not supported on Windows targets.
if mod.Os() == android.Windows {
return true return true
} }

View file

@ -182,6 +182,7 @@ func registerRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
ctx.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory) ctx.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
ctx.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory) ctx.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
ctx.RegisterModuleType("rust_fuzz", RustFuzzFactory) ctx.RegisterModuleType("rust_fuzz", RustFuzzFactory)
ctx.RegisterModuleType("rust_fuzz_host", RustFuzzHostFactory)
ctx.RegisterModuleType("rust_ffi", RustFFIFactory) ctx.RegisterModuleType("rust_ffi", RustFFIFactory)
ctx.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory) ctx.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
ctx.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory) ctx.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)