From a38fb99b3fbff0c988e64bf4bf277071e18b18af Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 21 May 2024 07:14:39 +0200 Subject: Makefile: build executor with C++ compiler Add C++ compiler and flags to the target and build executor with the C++ compiler. This will be needed to merge syz-fuzzer in to syz-executor since it will be beefier and will most likely require linking in libc++. But also this should fix #4821 since we won't use C++ flags when building C sources (we already had work-around in pkg/csource, but not in syz-extract). Fixes #4821 --- Makefile | 4 ++-- pkg/csource/build.go | 18 +++++++----------- sys/targets/targets.go | 21 +++++++++++++++------ tools/syz-make/make.go | 2 ++ 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 273043d00..258eaf2e2 100644 --- a/Makefile +++ b/Makefile @@ -133,8 +133,8 @@ ifneq ("$(NO_CROSS_COMPILER)", "") $(info ************************************************************************************) else mkdir -p ./bin/$(TARGETOS)_$(TARGETARCH) - $(CC) -o ./bin/$(TARGETOS)_$(TARGETARCH)/syz-executor$(EXE) executor/executor.cc \ - $(ADDCFLAGS) $(CFLAGS) -DGOOS_$(TARGETOS)=1 -DGOARCH_$(TARGETARCH)=1 \ + $(CXX) -o ./bin/$(TARGETOS)_$(TARGETARCH)/syz-executor$(EXE) executor/executor.cc \ + $(ADDCXXFLAGS) $(CFLAGS) -DGOOS_$(TARGETOS)=1 -DGOARCH_$(TARGETARCH)=1 \ -DHOSTGOOS_$(HOSTOS)=1 -DGIT_REVISION=\"$(REV)\" endif endif diff --git a/pkg/csource/build.go b/pkg/csource/build.go index 5dd1234a7..0625e9a03 100644 --- a/pkg/csource/build.go +++ b/pkg/csource/build.go @@ -9,7 +9,6 @@ import ( "os" "path/filepath" "runtime" - "slices" "strings" "testing" @@ -46,8 +45,6 @@ func BuildExecutor(t *testing.T, target *prog.Target, rootDir string, cflags ... } func build(target *prog.Target, src []byte, dir, file string, cflags ...string) (string, error) { - sysTarget := targets.Get(target.OS, target.Arch) - compiler := sysTarget.CCompiler // We call the binary syz-executor because it sometimes shows in bug titles, // and we don't want 2 different bugs for when a crash is triggered during fuzzing and during repro. bin, err := osutil.TempFile("syz-executor") @@ -66,18 +63,17 @@ func build(target *prog.Target, src []byte, dir, file string, cflags ...string) } else { flags = append(flags, file) } - flags = append(flags, sysTarget.CFlags...) + sysTarget := targets.Get(target.OS, target.Arch) + compiler, targetCFlags := sysTarget.CCompiler, sysTarget.CFlags + if file != "" && !strings.HasSuffix(file, ".c") { + compiler, targetCFlags = sysTarget.CxxCompiler, sysTarget.CxxFlags + } + flags = append(flags, targetCFlags...) + flags = append(flags, cflags...) if sysTarget.PtrSize == 4 { // We do generate uint64's for syscall arguments that overflow longs on 32-bit archs. flags = append(flags, "-Wno-overflow") } - flags = append(flags, cflags...) - if file == "" || strings.HasSuffix(file, ".c") { - // Building C source, so remove C++ flags. - flags = slices.DeleteFunc(flags, func(flag string) bool { - return strings.HasPrefix(flag, "-std=c++") - }) - } cmd := osutil.Command(compiler, flags...) cmd.Dir = dir if file == "" { diff --git a/sys/targets/targets.go b/sys/targets/targets.go index 8a1b80f27..f5092b8c1 100644 --- a/sys/targets/targets.go +++ b/sys/targets/targets.go @@ -27,8 +27,10 @@ type Target struct { Int64Alignment uint64 LittleEndian bool CFlags []string + CxxFlags []string Triple string CCompiler string + CxxCompiler string Objdump string // name of objdump executable KernelCompiler string // override CC when running kernel make KernelLinker string // override LD when running kernel make @@ -584,9 +586,6 @@ var oses = map[string]osCommon{ var ( commonCFlags = []string{ - "-std=c++11", - "-I.", - "-Iexecutor/_include", "-O2", "-pthread", "-Wall", @@ -614,6 +613,13 @@ var ( fallbackCFlags = map[string]string{ "-static-pie": "-static", // if an ASLR static binary is impossible, build just a static one } + // These are used only when building executor. + // For C repros and syz-extract, we build C source files. + commonCxxFlags = []string{ + "-std=c++14", + "-I.", + "-Iexecutor/_include", + } ) func fuchsiaCFlags(arch, clangArch string) []string { @@ -646,9 +652,6 @@ func init() { } arch32, arch64 := splitArch(runtime.GOARCH) goos := runtime.GOOS - if goos == "android" { - goos = Linux - } for _, target := range List[TestOS] { if List[goos] == nil { continue @@ -662,6 +665,7 @@ func init() { continue } target.CCompiler = host.CCompiler + target.CxxCompiler = host.CxxCompiler target.CPP = host.CPP target.CFlags = append(append([]string{}, host.CFlags...), target.CFlags...) target.CFlags = processMergedFlags(target.CFlags) @@ -719,6 +723,9 @@ func initTarget(target *Target, OS, arch string) { if target.CCompiler == "" { target.setCompiler(useClang) } + if target.CxxCompiler == "" { + target.CxxCompiler = strings.TrimSuffix(strings.TrimSuffix(target.CCompiler, "cc"), "++") + "++" + } if target.CPP == "" { target.CPP = "cpp" } @@ -734,6 +741,7 @@ func initTarget(target *Target, OS, arch string) { if runtime.GOOS != target.BuildOS { // Spoil native binaries if they are not usable, so that nobody tries to use them later. target.CCompiler = fmt.Sprintf("cant-build-%v-on-%v", target.OS, runtime.GOOS) + target.CxxCompiler = target.CCompiler target.CPP = target.CCompiler } for _, flags := range [][]string{commonCFlags, target.osCommon.cflags} { @@ -945,6 +953,7 @@ func (target *Target) lazyInit() { } } target.CFlags = newCFlags + target.CxxFlags = append(target.CFlags, commonCxxFlags...) // Check that the compiler is actually functioning. It may be present, but still broken. // Common for Linux distros, over time we've seen: // Error: alignment too large: 15 assumed diff --git a/tools/syz-make/make.go b/tools/syz-make/make.go index 6a69d5dd4..728f39352 100644 --- a/tools/syz-make/make.go +++ b/tools/syz-make/make.go @@ -76,7 +76,9 @@ func impl() ([]Var, error) { {"TARGETARCH", targetArch}, {"TARGETVMARCH", targetVMArch}, {"CC", target.CCompiler}, + {"CXX", target.CxxCompiler}, {"ADDCFLAGS", strings.Join(target.CFlags, " ")}, + {"ADDCXXFLAGS", strings.Join(target.CxxFlags, " ")}, {"NCORES", strconv.Itoa(parallelism)}, {"EXE", target.ExeExtension}, {"NATIVEBUILDOS", target.BuildOS}, -- cgit mrf-deployment