aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/csource/build.go
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 /pkg/csource/build.go
parenta3ce1723b2f8f690652d181a96344ab9b1c438a4 (diff)
executor: make flatrpc build for C++
Diffstat (limited to 'pkg/csource/build.go')
-rw-r--r--pkg/csource/build.go32
1 files changed, 26 insertions, 6 deletions
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)
}