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.
This commit is contained in:
Dmitry Vyukov 2019-12-01 10:59:54 +01:00
parent a76bf83ffa
commit edcd9e3c9a
2 changed files with 19 additions and 1 deletions

View File

@ -13,6 +13,7 @@ import (
"runtime" "runtime"
"sort" "sort"
"strings" "strings"
"sync"
"github.com/google/syzkaller/pkg/ast" "github.com/google/syzkaller/pkg/ast"
"github.com/google/syzkaller/pkg/compiler" "github.com/google/syzkaller/pkg/compiler"
@ -38,6 +39,9 @@ type Arch struct {
files []*File files []*File
err error err error
done chan bool done chan bool
// Used by OS implementations:
once sync.Once
cc string
} }
type File struct { type File struct {

View File

@ -5,6 +5,7 @@ package main
import ( import (
"fmt" "fmt"
"os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "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) { 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 headerArch := arch.target.KernelHeaderArch
sourceDir := arch.sourceDir sourceDir := arch.sourceDir
buildDir := arch.buildDir buildDir := arch.buildDir
@ -151,7 +159,7 @@ unsigned long phys_base;
unsigned long __phys_addr(unsigned long addr) { return 0; } unsigned long __phys_addr(unsigned long addr) { return 0; }
#endif #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 { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -171,3 +179,9 @@ unsigned long __phys_addr(unsigned long addr) { return 0; }
} }
return res, undeclared, nil 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
}