From edcd9e3c9ade5c8ccb7857c0add43b05e7a86c44 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Sun, 1 Dec 2019 10:59:54 +0100 Subject: sys/syz-extract: use clang if gcc is broken On my Debian gcc -m32 is hopelessly broken. Using clang fixes at least arch 386. Arch arm is still broken b/c clang does not like some of kernel arm inline assemly constraints. --- sys/syz-extract/extract.go | 4 ++++ sys/syz-extract/linux.go | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/sys/syz-extract/extract.go b/sys/syz-extract/extract.go index 64776bb96..70c203b8d 100644 --- a/sys/syz-extract/extract.go +++ b/sys/syz-extract/extract.go @@ -13,6 +13,7 @@ import ( "runtime" "sort" "strings" + "sync" "github.com/google/syzkaller/pkg/ast" "github.com/google/syzkaller/pkg/compiler" @@ -38,6 +39,9 @@ type Arch struct { files []*File err error done chan bool + // Used by OS implementations: + once sync.Once + cc string } type File struct { diff --git a/sys/syz-extract/linux.go b/sys/syz-extract/linux.go index b57375b55..4c759a19d 100644 --- a/sys/syz-extract/linux.go +++ b/sys/syz-extract/linux.go @@ -5,6 +5,7 @@ package main import ( "fmt" + "os/exec" "path/filepath" "runtime" "strings" @@ -110,6 +111,13 @@ func (*linux) prepareArch(arch *Arch) error { } func (*linux) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uint64, map[string]bool, error) { + arch.once.Do(func() { + arch.cc = "gcc" + if !checkCompiler("gcc", arch.target.CFlags) && + checkCompiler("clang", arch.target.CFlags) { + arch.cc = "clang" + } + }) headerArch := arch.target.KernelHeaderArch sourceDir := arch.sourceDir buildDir := arch.buildDir @@ -151,7 +159,7 @@ unsigned long phys_base; unsigned long __phys_addr(unsigned long addr) { return 0; } #endif ` - res, undeclared, err := extract(info, "gcc", args, addSource, true, false) + res, undeclared, err := extract(info, arch.cc, args, addSource, true, false) if err != nil { return nil, nil, err } @@ -171,3 +179,9 @@ unsigned long __phys_addr(unsigned long addr) { return 0; } } return res, undeclared, nil } + +func checkCompiler(cc string, args []string) bool { + cmd := exec.Command(cc, append(args, "-x", "c", "-", "-o", "/dev/null")...) + cmd.Stdin = strings.NewReader("int main(){}") + return cmd.Run() == nil +} -- cgit mrf-deployment