aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2024-05-02 19:35:10 +0200
committerDmitry Vyukov <dvyukov@google.com>2024-05-03 14:25:58 +0000
commit3e60354bf2a2ad7e7fa81fe8107f3ce24e098287 (patch)
tree385a30b0529d6412b546937d85791d823567bebf
parenta3ce1723b2f8f690652d181a96344ab9b1c438a4 (diff)
downloadsyzkaller-3e60354bf2a2ad7e7fa81fe8107f3ce24e098287.tar.gz
executor: make flatrpc build for C++
-rw-r--r--Makefile3
-rw-r--r--executor/common_ext_test.go7
-rw-r--r--executor/executor.cc3
-rw-r--r--pkg/cover/report_test.go10
-rw-r--r--pkg/csource/build.go32
-rw-r--r--pkg/fuzzer/fuzzer_test.go16
-rw-r--r--pkg/ipc/ipc_test.go23
-rw-r--r--pkg/runtest/run_test.go8
-rw-r--r--sys/targets/targets.go3
9 files changed, 48 insertions, 57 deletions
diff --git a/Makefile b/Makefile
index 0990b00ee..584336822 100644
--- a/Makefile
+++ b/Makefile
@@ -282,10 +282,11 @@ configs: kconf
bin/syz-kconf -config dashboard/config/linux/main.yml -sourcedir $(SOURCEDIR)
tidy: descriptions
- clang-tidy -quiet -header-filter=.* -warnings-as-errors=* \
+ clang-tidy -quiet -header-filter=executor/.* -warnings-as-errors=* \
-checks=-*,misc-definitions-in-headers,bugprone-macro-parentheses,clang-analyzer-*,-clang-analyzer-security.insecureAPI*,-clang-analyzer-optin.performance* \
-extra-arg=-DGOOS_$(TARGETOS)=1 -extra-arg=-DGOARCH_$(TARGETARCH)=1 \
-extra-arg=-DHOSTGOOS_$(HOSTOS)=1 -extra-arg=-DGIT_REVISION=\"$(REV)\" \
+ --extra-arg=-I. --extra-arg=-Ivendor \
executor/*.cc
ifdef CI
diff --git a/executor/common_ext_test.go b/executor/common_ext_test.go
index 00bca4494..d92eff00c 100644
--- a/executor/common_ext_test.go
+++ b/executor/common_ext_test.go
@@ -5,7 +5,6 @@ package executor
import (
"bytes"
- "os"
"testing"
"time"
@@ -26,11 +25,7 @@ func TestCommonExt(t *testing.T) {
if sysTarget.BrokenCompiler != "" {
t.Skipf("skipping, broken cross-compiler: %v", sysTarget.BrokenCompiler)
}
- bin, err := csource.BuildFile(target, "executor.cc", "-DSYZ_TEST_COMMON_EXT_EXAMPLE=1")
- if err != nil {
- t.Fatal(err)
- }
- defer os.Remove(bin)
+ bin := csource.BuildExecutor(t, target, "..", "-DSYZ_TEST_COMMON_EXT_EXAMPLE=1")
out, err := osutil.RunCmd(time.Minute, "", bin, "setup")
if err != nil {
t.Fatal(err)
diff --git a/executor/executor.cc b/executor/executor.cc
index f9ec7f1b5..6ac777ad6 100644
--- a/executor/executor.cc
+++ b/executor/executor.cc
@@ -3,6 +3,9 @@
// +build
+// Currently this is unused (included only to test building).
+#include "pkg/flatrpc/flatrpc.h"
+
#include <algorithm>
#include <errno.h>
#include <limits.h>
diff --git a/pkg/cover/report_test.go b/pkg/cover/report_test.go
index e93d43eda..07ae439ac 100644
--- a/pkg/cover/report_test.go
+++ b/pkg/cover/report_test.go
@@ -17,6 +17,7 @@ import (
"reflect"
"regexp"
"runtime"
+ "slices"
"strconv"
"strings"
"testing"
@@ -224,7 +225,10 @@ func buildTestBinary(t *testing.T, target *targets.Target, test *Test, dir strin
aslrExtraLibs = []string{"-ldl"}
}
- kcovFlags := append([]string{"-c", "-fpie", "-w", "-x", "c", "-o", kcovObj, kcovSrc, aslrDefine}, target.CFlags...)
+ targetCFlags := slices.DeleteFunc(slices.Clone(target.CFlags), func(flag string) bool {
+ return strings.HasPrefix(flag, "-std=c++")
+ })
+ kcovFlags := append([]string{"-c", "-fpie", "-w", "-x", "c", "-o", kcovObj, kcovSrc, aslrDefine}, targetCFlags...)
src := filepath.Join(dir, "main.c")
obj := filepath.Join(dir, "main.o")
bin := filepath.Join(dir, target.KernelObject)
@@ -240,7 +244,7 @@ func buildTestBinary(t *testing.T, target *targets.Target, test *Test, dir strin
// -fsanitize-coverage=trace-pc is provided during linking and
// ubsan runtime is missing for arm/arm64/riscv arches in the llvm packages.
// So we first compile with -fsanitize-coverage and then link w/o it.
- cflags := append(append([]string{"-w", "-c", "-o", obj, src}, target.CFlags...), test.CFlags...)
+ cflags := append(append([]string{"-w", "-c", "-o", obj, src}, targetCFlags...), test.CFlags...)
if test.DebugInfo {
// TODO: pkg/cover doesn't support DWARF5 yet, which is the default in Clang.
cflags = append([]string{"-g", "-gdwarf-4"}, cflags...)
@@ -257,7 +261,7 @@ func buildTestBinary(t *testing.T, target *targets.Target, test *Test, dir strin
}
ldflags := append(append(append([]string{"-o", bin, obj, kcovObj}, aslrExtraLibs...),
- target.CFlags...), test.LDFlags...)
+ targetCFlags...), test.LDFlags...)
staticIdx, pieIdx := -1, -1
for i, arg := range ldflags {
switch arg {
diff --git a/pkg/csource/build.go b/pkg/csource/build.go
index 35d97d023..5dd1234a7 100644
--- a/pkg/csource/build.go
+++ b/pkg/csource/build.go
@@ -7,7 +7,11 @@ import (
"bytes"
"fmt"
"os"
+ "path/filepath"
"runtime"
+ "slices"
+ "strings"
+ "testing"
"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/prog"
@@ -16,7 +20,7 @@ import (
// Build builds a C program from source src and returns name of the resulting binary.
func Build(target *prog.Target, src []byte) (string, error) {
- return build(target, src, "")
+ return build(target, src, "", "")
}
// BuildNoWarn is the same as Build, but ignores all compilation warnings.
@@ -24,15 +28,24 @@ func Build(target *prog.Target, src []byte) (string, error) {
// using an old repro with newer compiler, or a compiler that we never seen before.
// In these cases it's more important to build successfully.
func BuildNoWarn(target *prog.Target, src []byte) (string, error) {
- return build(target, src, "", "-fpermissive", "-w")
+ return build(target, src, "", "", "-fpermissive", "-w")
}
-// BuildFile builds a C/C++ program from file src and returns name of the resulting binary.
-func BuildFile(target *prog.Target, src string, cflags ...string) (string, error) {
- return build(target, nil, src, cflags...)
+// BuildExecutor builds the executor binary for tests.
+// rootDir must point to syzkaller root directory in slash notation.
+func BuildExecutor(t *testing.T, target *prog.Target, rootDir string, cflags ...string) string {
+ bin, err := build(target, nil, filepath.FromSlash(rootDir),
+ filepath.FromSlash("executor/executor.cc"), cflags...)
+ if err != nil {
+ t.Fatalf("failed to build executor: %v", err)
+ }
+ t.Cleanup(func() {
+ os.Remove(bin)
+ })
+ return bin
}
-func build(target *prog.Target, src []byte, file string, cflags ...string) (string, error) {
+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,
@@ -59,7 +72,14 @@ func build(target *prog.Target, src []byte, file string, cflags ...string) (stri
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 == "" {
cmd.Stdin = bytes.NewReader(src)
}
diff --git a/pkg/fuzzer/fuzzer_test.go b/pkg/fuzzer/fuzzer_test.go
index 5e275851a..13eb5609b 100644
--- a/pkg/fuzzer/fuzzer_test.go
+++ b/pkg/fuzzer/fuzzer_test.go
@@ -9,8 +9,6 @@ import (
"fmt"
"hash/crc32"
"math/rand"
- "os"
- "path/filepath"
"regexp"
"runtime"
"strings"
@@ -41,7 +39,7 @@ func TestFuzz(t *testing.T) {
if sysTarget.BrokenCompiler != "" {
t.Skipf("skipping, broken cross-compiler: %v", sysTarget.BrokenCompiler)
}
- executor := buildExecutor(t, target)
+ executor := csource.BuildExecutor(t, target, "../..", "-fsanitize-coverage=trace-pc", "-g")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@@ -318,18 +316,6 @@ func (proc *executorProc) execute(req *Request) (*Result, string, error) {
return &Result{Info: info}, "", nil
}
-func buildExecutor(t *testing.T, target *prog.Target) string {
- executor, err := csource.BuildFile(target,
- filepath.FromSlash("../../executor/executor.cc"),
- "-fsanitize-coverage=trace-pc", "-g",
- )
- if err != nil {
- t.Fatal(err)
- }
- t.Cleanup(func() { os.Remove(executor) })
- return executor
-}
-
func checkGoroutineLeaks() {
// Inspired by src/net/http/main_test.go.
buf := make([]byte, 2<<20)
diff --git a/pkg/ipc/ipc_test.go b/pkg/ipc/ipc_test.go
index fcc8b5dcd..a50fca9af 100644
--- a/pkg/ipc/ipc_test.go
+++ b/pkg/ipc/ipc_test.go
@@ -6,8 +6,6 @@ package ipc_test
import (
"fmt"
"math/rand"
- "os"
- "path/filepath"
"runtime"
"testing"
"time"
@@ -23,15 +21,6 @@ import (
"github.com/google/syzkaller/sys/targets"
)
-func buildExecutor(t *testing.T, target *prog.Target) string {
- src := filepath.FromSlash("../../executor/executor.cc")
- bin, err := csource.BuildFile(target, src)
- if err != nil {
- t.Fatal(err)
- }
- return bin
-}
-
func initTest(t *testing.T) (*prog.Target, rand.Source, int, bool, bool, targets.Timeouts) {
t.Parallel()
iters := 100
@@ -65,8 +54,7 @@ func TestExecutor(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- bin := buildExecutor(t, target)
- defer os.Remove(bin)
+ bin := csource.BuildExecutor(t, target, "../..")
// qemu-user may allow us to run some cross-arch binaries.
if _, err := osutil.RunCmd(time.Minute, "", bin, "test"); err != nil {
if sysTarget.Arch == runtime.GOARCH || sysTarget.VMArch == runtime.GOARCH {
@@ -89,8 +77,7 @@ func prepareTestProgram(target *prog.Target) *prog.Prog {
func TestExecute(t *testing.T) {
target, _, _, useShmem, useForkServer, timeouts := initTest(t)
- bin := buildExecutor(t, target)
- defer os.Remove(bin)
+ bin := csource.BuildExecutor(t, target, "../..")
flags := []ExecFlags{0, FlagThreaded}
for _, flag := range flags {
@@ -134,8 +121,7 @@ func TestExecute(t *testing.T) {
func TestParallel(t *testing.T) {
target, _, _, useShmem, useForkServer, timeouts := initTest(t)
- bin := buildExecutor(t, target)
- defer os.Remove(bin)
+ bin := csource.BuildExecutor(t, target, "../..")
cfg := &Config{
Executor: bin,
UseShmem: useShmem,
@@ -203,8 +189,7 @@ func TestZlib(t *testing.T) {
t.Fatal(err)
}
opts.EnvFlags |= FlagDebug
- cfg.Executor = buildExecutor(t, target)
- defer os.Remove(cfg.Executor)
+ cfg.Executor = csource.BuildExecutor(t, target, "../..")
env, err := MakeEnv(cfg, 0)
if err != nil {
t.Fatalf("failed to create env: %v", err)
diff --git a/pkg/runtest/run_test.go b/pkg/runtest/run_test.go
index 00bf343d7..6377876d6 100644
--- a/pkg/runtest/run_test.go
+++ b/pkg/runtest/run_test.go
@@ -54,13 +54,7 @@ func test(t *testing.T, sysTarget *targets.Target) {
if err != nil {
t.Fatal(err)
}
- executor, err := csource.BuildFile(target,
- filepath.FromSlash("../../executor/executor.cc"),
- "-fsanitize-coverage=trace-pc")
- if err != nil {
- t.Fatal(err)
- }
- defer os.Remove(executor)
+ executor := csource.BuildExecutor(t, target, "../../", "-fsanitize-coverage=trace-pc")
features, err := host.Check(target)
if err != nil {
t.Fatalf("failed to detect host features: %v", err)
diff --git a/sys/targets/targets.go b/sys/targets/targets.go
index 02a26028b..3635eb023 100644
--- a/sys/targets/targets.go
+++ b/sys/targets/targets.go
@@ -584,6 +584,9 @@ var oses = map[string]osCommon{
var (
commonCFlags = []string{
+ "-std=c++11",
+ "-I.",
+ "-Ivendor",
"-O2",
"-pthread",
"-Wall",