diff --git a/Makefile b/Makefile index 81f41fd3..b035653a 100644 --- a/Makefile +++ b/Makefile @@ -71,7 +71,7 @@ upgrade: go build $(GOFLAGS) -o ./bin/syz-upgrade github.com/google/syzkaller/tools/syz-upgrade extract: bin/syz-extract - LINUX=$(LINUX) LINUXBLD=$(LINUXBLD) ./sys/linux/extract.sh + LINUX=$(LINUX) ./sys/linux/extract.sh bin/syz-extract: go build $(GOFLAGS) -o $@ ./sys/syz-extract diff --git a/sys/linux/extract.sh b/sys/linux/extract.sh index 2eb7dd2a..9a5ba45f 100755 --- a/sys/linux/extract.sh +++ b/sys/linux/extract.sh @@ -41,36 +41,17 @@ fi generate_arch() { echo generating arch $1... - echo "cd $LINUX; make defconfig" - OUT=`(cd $LINUX; make ARCH=$2 CROSS_COMPILE=$3 CFLAGS=$4 defconfig 2>&1)` - if [ $? -ne 0 ]; then - echo "$OUT" - exit 1 - fi - # Without CONFIG_NETFILTER kernel does not build. - (cd $LINUX; sed -i "s@# CONFIG_NETFILTER is not set@CONFIG_NETFILTER=y@g" .config) - (cd $LINUX; make ARCH=$2 CROSS_COMPILE=$3 CFLAGS=$4 olddefconfig) - echo "cd $LINUX; make" - OUT=`(cd $LINUX; make ARCH=$2 CROSS_COMPILE=$3 CFLAGS=$4 init/main.o 2>&1)` - if [ $? -ne 0 ]; then - echo "$OUT" - exit 1 - fi - (cd sys/linux; ../../bin/syz-extract -arch $1 -linux "$LINUX" -linuxbld "$LINUXBLD" $FILES) + (cd sys/linux; ../../bin/syz-extract -arch $1 -linux "$LINUX" -build $FILES) if [ $? -ne 0 ]; then exit 1 fi echo } -# $1 Go arch -# $2 kernel arch -# $3 cross-compiler prefix -# $4 CLAGS -generate_arch amd64 x86_64 x86_64-linux-gnu- "-m64" -generate_arch arm64 arm64 aarch64-linux-gnu- "" +generate_arch amd64 +generate_arch arm64 if [ "$BUILD_FOR_ANDROID" == "no" ]; then - generate_arch 386 i386 "" "-m32" - generate_arch arm arm arm-linux-gnueabihf- "-march=armv6t2" - generate_arch ppc64le powerpc powerpc64le-linux-gnu- "" + generate_arch 386 + generate_arch arm + generate_arch ppc64le fi diff --git a/sys/sys.go b/sys/sys.go index 79683732..1897a635 100644 --- a/sys/sys.go +++ b/sys/sys.go @@ -70,6 +70,7 @@ func init() { for arch, target := range archs { target.OS = OS target.Arch = arch + target.CrossCFlags = append(target.CrossCFlags, target.CFlags...) } } } diff --git a/sys/syz-extract/extract.go b/sys/syz-extract/extract.go index d7a2efb9..7a9437e4 100644 --- a/sys/syz-extract/extract.go +++ b/sys/syz-extract/extract.go @@ -13,6 +13,7 @@ import ( "runtime" "strings" "sync" + "time" "github.com/google/syzkaller/pkg/ast" "github.com/google/syzkaller/pkg/compiler" @@ -24,6 +25,7 @@ var ( flagLinux = flag.String("linux", "", "path to linux kernel source checkout") flagLinuxBld = flag.String("linuxbld", "", "path to linux kernel build directory") flagArch = flag.String("arch", "", "arch to generate") + flagBuild = flag.Bool("build", false, "generate arch-specific files in the linux dir") ) type File struct { @@ -33,11 +35,6 @@ type File struct { } func main() { - failf := func(msg string, args ...interface{}) { - fmt.Fprintf(os.Stderr, msg+"\n", args...) - os.Exit(1) - } - flag.Parse() if *flagLinux == "" { failf("provide path to linux kernel checkout via -linux flag (or make extract LINUX= flag)") @@ -56,6 +53,9 @@ func main() { if n == 0 { failf("usage: syz-extract -linux=/linux/checkout -arch=arch input_file.txt...") } + if *flagBuild { + buildKernel(target, *flagLinux) + } files := make([]File, n) inc := make(chan *File, n) @@ -120,3 +120,37 @@ func processFile(target *sys.Target, inname string) (map[string]bool, error) { } return undeclared, nil } + +func buildKernel(target *sys.Target, dir string) { + // TODO(dvyukov): use separate temp build dir. + // This will allow to do build for all archs in parallel and + // won't destroy user's build state. + makeArgs := []string{ + "ARCH=" + target.KernelArch, + "CROSS_COMPILE=" + target.CCompilerPrefix, + "CFLAGS=" + strings.Join(target.CrossCFlags, " "), + } + out, err := osutil.RunCmd(time.Hour, dir, "make", append(makeArgs, "defconfig")...) + if err != nil { + failf("make defconfig failed: %v\n%s\n", err, out) + } + // Without CONFIG_NETFILTER kernel does not build. + out, err = osutil.RunCmd(time.Minute, dir, "sed", "-i", + "s@# CONFIG_NETFILTER is not set@CONFIG_NETFILTER=y@g", ".config") + if err != nil { + failf("sed .config failed: %v\n%s\n", err, out) + } + out, err = osutil.RunCmd(time.Hour, dir, "make", append(makeArgs, "olddefconfig")...) + if err != nil { + failf("make olddefconfig failed: %v\n%s\n", err, out) + } + out, err = osutil.RunCmd(time.Hour, dir, "make", append(makeArgs, "init/main.o")...) + if err != nil { + failf("make failed: %v\n%s\n", err, out) + } +} + +func failf(msg string, args ...interface{}) { + fmt.Fprintf(os.Stderr, msg+"\n", args...) + os.Exit(1) +}