diff options
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/build/akaros.go | 37 | ||||
| -rw-r--r-- | pkg/build/build.go | 40 | ||||
| -rw-r--r-- | pkg/build/freebsd.go | 25 | ||||
| -rw-r--r-- | pkg/build/fuchsia.go | 42 | ||||
| -rw-r--r-- | pkg/build/gvisor.go | 13 | ||||
| -rw-r--r-- | pkg/build/linux.go | 44 | ||||
| -rw-r--r-- | pkg/build/netbsd.go | 22 | ||||
| -rw-r--r-- | pkg/build/openbsd.go | 19 | ||||
| -rw-r--r-- | pkg/build/test.go | 14 | ||||
| -rw-r--r-- | pkg/build/testlinux.go | 16 | ||||
| -rw-r--r-- | pkg/instance/instance.go | 25 |
11 files changed, 156 insertions, 141 deletions
diff --git a/pkg/build/akaros.go b/pkg/build/akaros.go index f4c9fe732..2a3b8c8ea 100644 --- a/pkg/build/akaros.go +++ b/pkg/build/akaros.go @@ -16,16 +16,15 @@ import ( type akaros struct{} -func (ctx akaros) build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir, - cmdlineFile, sysctlFile string, config []byte) error { - configFile := filepath.Join(kernelDir, ".config") - if err := osutil.WriteFile(configFile, config); err != nil { +func (ctx akaros) build(params *Params) error { + configFile := filepath.Join(params.KernelDir, ".config") + if err := osutil.WriteFile(configFile, params.Config); err != nil { return fmt.Errorf("failed to write config file: %v", err) } if err := osutil.SandboxChown(configFile); err != nil { return err } - sshkey := filepath.Join(kernelDir, "key") + sshkey := filepath.Join(params.KernelDir, "key") sshkeyPub := sshkey + ".pub" os.Remove(sshkey) os.Remove(sshkeyPub) @@ -36,22 +35,22 @@ func (ctx akaros) build(targetArch, vmType, kernelDir, outputDir, compiler, user if err := osutil.SandboxChown(sshkeyPub); err != nil { return err } - if err := ctx.make(kernelDir, "", "olddefconfig", "ARCH=x86"); err != nil { + if err := ctx.make(params.KernelDir, "", "olddefconfig", "ARCH=x86"); err != nil { return err } - if err := ctx.make(kernelDir, "", "xcc"); err != nil { + if err := ctx.make(params.KernelDir, "", "xcc"); err != nil { return err } - if err := ctx.make(kernelDir, "tools/dev-libs/elfutils", "install"); err != nil { + if err := ctx.make(params.KernelDir, "tools/dev-libs/elfutils", "install"); err != nil { return err } - if err := ctx.make(kernelDir, "", "apps-install"); err != nil { + if err := ctx.make(params.KernelDir, "", "apps-install"); err != nil { return err } - if err := ctx.make(kernelDir, "", "fill-kfs"); err != nil { + if err := ctx.make(params.KernelDir, "", "fill-kfs"); err != nil { return err } - targetKey := filepath.Join(kernelDir, "kern", "kfs", ".ssh", "authorized_keys") + targetKey := filepath.Join(params.KernelDir, "kern", "kfs", ".ssh", "authorized_keys") if err := osutil.Rename(sshkeyPub, targetKey); err != nil { return err } @@ -60,7 +59,7 @@ func (ctx akaros) build(targetArch, vmType, kernelDir, outputDir, compiler, user dropbear -F 2>db_out & bash ` - initFile := filepath.Join(kernelDir, "kern", "kfs", "init.sh") + initFile := filepath.Join(params.KernelDir, "kern", "kfs", "init.sh") if err := osutil.WriteFile(initFile, []byte(init)); err != nil { return fmt.Errorf("failed to write init script: %v", err) } @@ -70,19 +69,19 @@ bash if err := os.Chmod(initFile, 0770); err != nil { return err } - if err := ctx.cmd(kernelDir, "dropbear", "./CONFIGURE_AKAROS"); err != nil { + if err := ctx.cmd(params.KernelDir, "dropbear", "./CONFIGURE_AKAROS"); err != nil { return err } - if err := ctx.make(kernelDir, "dropbear/build"); err != nil { + if err := ctx.make(params.KernelDir, "dropbear/build"); err != nil { return err } - if err := ctx.make(kernelDir, "dropbear/build", "install"); err != nil { + if err := ctx.make(params.KernelDir, "dropbear/build", "install"); err != nil { return err } - if err := ctx.make(kernelDir, ""); err != nil { + if err := ctx.make(params.KernelDir, ""); err != nil { return err } - if err := osutil.WriteFile(filepath.Join(outputDir, "image"), nil); err != nil { + if err := osutil.WriteFile(filepath.Join(params.OutputDir, "image"), nil); err != nil { return fmt.Errorf("failed to write image file: %v", err) } for src, dst := range map[string]string{ @@ -91,8 +90,8 @@ bash "obj/kern/akaros-kernel": "kernel", "obj/kern/akaros-kernel-64b": "obj/akaros-kernel-64b", } { - fullSrc := filepath.Join(kernelDir, filepath.FromSlash(src)) - fullDst := filepath.Join(outputDir, filepath.FromSlash(dst)) + fullSrc := filepath.Join(params.KernelDir, filepath.FromSlash(src)) + fullDst := filepath.Join(params.OutputDir, filepath.FromSlash(dst)) if err := osutil.CopyFile(fullSrc, fullDst); err != nil { return fmt.Errorf("failed to copy %v: %v", src, err) } diff --git a/pkg/build/build.go b/pkg/build/build.go index d4126b6e6..a34d1e006 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -14,11 +14,25 @@ import ( "github.com/google/syzkaller/pkg/osutil" ) +// Params is input arguments for the Image function. +type Params struct { + TargetOS string + TargetArch string + VMType string + KernelDir string + OutputDir string + Compiler string + UserspaceDir string + CmdlineFile string + SysctlFile string + Config []byte +} + // Image creates a disk image for the specified OS/ARCH/VM. -// Kernel is taken from kernelDir, userspace system is taken from userspaceDir. -// If cmdlineFile is not empty, contents of the file are appended to the kernel command line. -// If sysctlFile is not empty, contents of the file are appended to the image /etc/sysctl.conf. -// Output is stored in outputDir and includes (everything except for image is optional): +// Kernel is taken from KernelDir, userspace system is taken from UserspaceDir. +// If CmdlineFile is not empty, contents of the file are appended to the kernel command line. +// If SysctlFile is not empty, contents of the file are appended to the image /etc/sysctl.conf. +// Output is stored in OutputDir and includes (everything except for image is optional): // - image: the image // - key: ssh key for the image // - kernel: kernel for injected boot @@ -26,22 +40,21 @@ import ( // - kernel.config: actual kernel config used during build // - obj/: directory with kernel object files (this should match KernelObject // specified in sys/targets, e.g. vmlinux for linux) -func Image(targetOS, targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir, - cmdlineFile, sysctlFile string, config []byte) error { - builder, err := getBuilder(targetOS, targetArch, vmType) +func Image(params *Params) error { + builder, err := getBuilder(params.TargetOS, params.TargetArch, params.VMType) if err != nil { return err } - if err := osutil.MkdirAll(filepath.Join(outputDir, "obj")); err != nil { + if err := osutil.MkdirAll(filepath.Join(params.OutputDir, "obj")); err != nil { return err } - if len(config) != 0 { + if len(params.Config) != 0 { // Write kernel config early, so that it's captured on build failures. - if err := osutil.WriteFile(filepath.Join(outputDir, "kernel.config"), config); err != nil { + if err := osutil.WriteFile(filepath.Join(params.OutputDir, "kernel.config"), params.Config); err != nil { return fmt.Errorf("failed to write config file: %v", err) } } - err = builder.build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir, cmdlineFile, sysctlFile, config) + err = builder.build(params) return extractRootCause(err) } @@ -58,8 +71,7 @@ type KernelBuildError struct { } type builder interface { - build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir, - cmdlineFile, sysctlFile string, config []byte) error + build(params *Params) error clean(kernelDir, targetArch string) error } @@ -79,7 +91,7 @@ func getBuilder(targetOS, targetArch, vmType string) (builder, error) { {"openbsd", "amd64", []string{"gce", "vmm"}, openbsd{}}, {"netbsd", "amd64", []string{"gce", "qemu"}, netbsd{}}, {"freebsd", "amd64", []string{"gce", "qemu"}, freebsd{}}, - {"test", "64", []string{"qemu"}, testBuilder{}}, + {"test", "64", []string{"qemu"}, test{}}, } for _, s := range supported { if targetOS == s.OS && targetArch == s.arch { diff --git a/pkg/build/freebsd.go b/pkg/build/freebsd.go index bc601af25..e67055895 100644 --- a/pkg/build/freebsd.go +++ b/pkg/build/freebsd.go @@ -16,11 +16,11 @@ import ( type freebsd struct{} -func (ctx freebsd) build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir, - cmdlineFile, sysctlFile string, config []byte) error { - confDir := fmt.Sprintf("%v/sys/%v/conf/", kernelDir, targetArch) +func (ctx freebsd) build(params *Params) error { + confDir := fmt.Sprintf("%v/sys/%v/conf/", params.KernelDir, params.TargetArch) confFile := "SYZKALLER" + config := params.Config if config == nil { config = []byte(` include "./GENERIC" @@ -37,22 +37,23 @@ options DIAGNOSTIC return err } - objPrefix := filepath.Join(kernelDir, "obj") - if err := ctx.make(kernelDir, objPrefix, "kernel-toolchain", "-DNO_CLEAN"); err != nil { + objPrefix := filepath.Join(params.KernelDir, "obj") + if err := ctx.make(params.KernelDir, objPrefix, "kernel-toolchain", "-DNO_CLEAN"); err != nil { return err } - if err := ctx.make(kernelDir, objPrefix, "buildkernel", fmt.Sprintf("KERNCONF=%v", confFile)); err != nil { + if err := ctx.make(params.KernelDir, objPrefix, "buildkernel", fmt.Sprintf("KERNCONF=%v", confFile)); err != nil { return err } - kernelObjDir := filepath.Join(objPrefix, kernelDir, fmt.Sprintf("%v.%v", targetArch, targetArch), "sys", confFile) + kernelObjDir := filepath.Join(objPrefix, params.KernelDir, + fmt.Sprintf("%v.%v", params.TargetArch, params.TargetArch), "sys", confFile) for _, s := range []struct{ dir, src, dst string }{ - {userspaceDir, "image", "image"}, - {userspaceDir, "key", "key"}, + {params.UserspaceDir, "image", "image"}, + {params.UserspaceDir, "key", "key"}, {kernelObjDir, "kernel.full", "obj/kernel.full"}, } { fullSrc := filepath.Join(s.dir, s.src) - fullDst := filepath.Join(outputDir, s.dst) + fullDst := filepath.Join(params.OutputDir, s.dst) if err := osutil.CopyFile(fullSrc, fullDst); err != nil { return fmt.Errorf("failed to copy %v -> %v: %v", fullSrc, fullDst, err) } @@ -71,9 +72,9 @@ echo 'pf_load="YES"' | sudo tee -a /boot/loader.conf sudo umount $tmpdir sudo mdconfig -d -u ${md#md} -`, objPrefix, kernelDir, confFile) +`, objPrefix, params.KernelDir, confFile) - if debugOut, err := osutil.RunCmd(10*time.Minute, outputDir, "/bin/sh", "-c", script); err != nil { + if debugOut, err := osutil.RunCmd(10*time.Minute, params.OutputDir, "/bin/sh", "-c", script); err != nil { return fmt.Errorf("error copying kernel: %v\n%v", err, debugOut) } return nil diff --git a/pkg/build/fuchsia.go b/pkg/build/fuchsia.go index a1bff9127..dc8e80b4d 100644 --- a/pkg/build/fuchsia.go +++ b/pkg/build/fuchsia.go @@ -14,36 +14,27 @@ import ( type fuchsia struct{} -func runSandboxed(timeout time.Duration, dir, command string, arg ...string) ([]byte, error) { - cmd := osutil.Command(command, arg...) - cmd.Dir = dir - if err := osutil.Sandbox(cmd, true, false); err != nil { - return nil, err - } - return osutil.Run(timeout, cmd) -} - -func (fu fuchsia) build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir, - cmdlineFile, sysctlFile string, config []byte) error { - sysTarget := targets.Get("fuchsia", targetArch) +func (fu fuchsia) build(params *Params) error { + sysTarget := targets.Get("fuchsia", params.TargetArch) if sysTarget == nil { - return fmt.Errorf("unsupported fuchsia arch %v", targetArch) + return fmt.Errorf("unsupported fuchsia arch %v", params.TargetArch) } arch := sysTarget.KernelHeaderArch product := fmt.Sprintf("%s.%s", "core", arch) - if _, err := runSandboxed(time.Hour, kernelDir, "scripts/fx", "--dir", "out/"+arch, + if _, err := runSandboxed(time.Hour, params.KernelDir, "scripts/fx", "--dir", "out/"+arch, "set", product, "--with-base", "//bundles:tools"); err != nil { return err } - if _, err := runSandboxed(time.Hour*2, kernelDir, "scripts/fx", "clean-build"); err != nil { + if _, err := runSandboxed(time.Hour*2, params.KernelDir, "scripts/fx", "clean-build"); err != nil { return err } // Fuchsia images no longer include ssh keys. Manually append the ssh public key to the zbi. - sshZBI := filepath.Join(kernelDir, "out", arch, "fuchsia-ssh.zbi") - kernelZBI := filepath.Join(kernelDir, "out", arch, "fuchsia.zbi") - authorizedKeys := fmt.Sprintf("data/ssh/authorized_keys=%s", filepath.Join(kernelDir, ".ssh", "authorized_keys")) - if _, err := runSandboxed(time.Minute, kernelDir, "out/"+arch+".zircon/tools/zbi", + sshZBI := filepath.Join(params.KernelDir, "out", arch, "fuchsia-ssh.zbi") + kernelZBI := filepath.Join(params.KernelDir, "out", arch, "fuchsia.zbi") + authorizedKeys := fmt.Sprintf("data/ssh/authorized_keys=%s", + filepath.Join(params.KernelDir, ".ssh", "authorized_keys")) + if _, err := runSandboxed(time.Minute, params.KernelDir, "out/"+arch+".zircon/tools/zbi", "-o", sshZBI, kernelZBI, "--entry", authorizedKeys); err != nil { return err } @@ -55,8 +46,8 @@ func (fu fuchsia) build(targetArch, vmType, kernelDir, outputDir, compiler, user "out/" + arch + ".zircon/multiboot.bin": "kernel", "out/" + arch + "/fuchsia-ssh.zbi": "initrd", } { - fullSrc := filepath.Join(kernelDir, filepath.FromSlash(src)) - fullDst := filepath.Join(outputDir, filepath.FromSlash(dst)) + fullSrc := filepath.Join(params.KernelDir, filepath.FromSlash(src)) + fullDst := filepath.Join(params.OutputDir, filepath.FromSlash(dst)) if err := osutil.CopyFile(fullSrc, fullDst); err != nil { return fmt.Errorf("failed to copy %v: %v", src, err) } @@ -69,3 +60,12 @@ func (fu fuchsia) clean(kernelDir, targetArch string) error { // So no need to clean separately. return nil } + +func runSandboxed(timeout time.Duration, dir, command string, arg ...string) ([]byte, error) { + cmd := osutil.Command(command, arg...) + cmd.Dir = dir + if err := osutil.Sandbox(cmd, true, false); err != nil { + return nil, err + } + return osutil.Run(timeout, cmd) +} diff --git a/pkg/build/gvisor.go b/pkg/build/gvisor.go index 6db9e6ab6..8d97be174 100644 --- a/pkg/build/gvisor.go +++ b/pkg/build/gvisor.go @@ -13,27 +13,26 @@ import ( type gvisor struct{} -func (gvisor gvisor) build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir, - cmdlineFile, sysctlFile string, config []byte) error { +func (gvisor gvisor) build(params *Params) error { // Bring down bazel daemon right away. We don't need it running and consuming memory. - defer osutil.RunCmd(10*time.Minute, kernelDir, compiler, "shutdown") + defer osutil.RunCmd(10*time.Minute, params.KernelDir, params.Compiler, "shutdown") outBinary := "" args := []string{"build", "--verbose_failures"} - if strings.Contains(" "+string(config)+" ", " -race ") { + if strings.Contains(" "+string(params.Config)+" ", " -race ") { args = append(args, "--features=race", "//runsc:runsc-race") outBinary = "bazel-bin/runsc/linux_amd64_static_race_stripped/runsc-race" } else { args = append(args, "//runsc:runsc") outBinary = "bazel-bin/runsc/linux_amd64_pure_stripped/runsc" } - outBinary = filepath.Join(kernelDir, filepath.FromSlash(outBinary)) + outBinary = filepath.Join(params.KernelDir, filepath.FromSlash(outBinary)) // The 1 hour timeout is quite high. But we've seen false positives with 20 mins // on the first build after bazel/deps update. Also other gvisor instances running // on the same machine contribute to longer build times. - if _, err := osutil.RunCmd(60*time.Minute, kernelDir, compiler, args...); err != nil { + if _, err := osutil.RunCmd(60*time.Minute, params.KernelDir, params.Compiler, args...); err != nil { return err } - if err := osutil.CopyFile(outBinary, filepath.Join(outputDir, "image")); err != nil { + if err := osutil.CopyFile(outBinary, filepath.Join(params.OutputDir, "image")); err != nil { return err } return nil diff --git a/pkg/build/linux.go b/pkg/build/linux.go index 3c1051ff9..aafb33867 100644 --- a/pkg/build/linux.go +++ b/pkg/build/linux.go @@ -22,21 +22,19 @@ import ( type linux struct{} -func (linux linux) build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir, - cmdlineFile, sysctlFile string, config []byte) error { - if err := linux.buildKernel(targetArch, kernelDir, outputDir, compiler, config); err != nil { +func (linux linux) build(params *Params) error { + if err := linux.buildKernel(params); err != nil { return err } - if err := linux.createImage(targetArch, vmType, kernelDir, outputDir, userspaceDir, cmdlineFile, - sysctlFile); err != nil { + if err := linux.createImage(params); err != nil { return err } return nil } -func (linux) buildKernel(targetArch, kernelDir, outputDir, compiler string, config []byte) error { - configFile := filepath.Join(kernelDir, ".config") - if err := osutil.WriteFile(configFile, config); err != nil { +func (linux) buildKernel(params *Params) error { + configFile := filepath.Join(params.KernelDir, ".config") + if err := osutil.WriteFile(configFile, params.Config); err != nil { return fmt.Errorf("failed to write config file: %v", err) } if err := osutil.SandboxChown(configFile); err != nil { @@ -45,34 +43,34 @@ func (linux) buildKernel(targetArch, kernelDir, outputDir, compiler string, conf // One would expect olddefconfig here, but olddefconfig is not present in v3.6 and below. // oldconfig is the same as olddefconfig if stdin is not set. // Note: passing in compiler is important since 4.17 (at the very least it's noted in the config). - if err := runMake(kernelDir, "oldconfig", "CC="+compiler); err != nil { + if err := runMake(params.KernelDir, "oldconfig", "CC="+params.Compiler); err != nil { return err } // Write updated kernel config early, so that it's captured on build failures. - outputConfig := filepath.Join(outputDir, "kernel.config") + outputConfig := filepath.Join(params.OutputDir, "kernel.config") if err := osutil.CopyFile(configFile, outputConfig); err != nil { return err } // We build only zImage/bzImage as we currently don't use modules. var target string - switch targetArch { + switch params.TargetArch { case "386", "amd64": target = "bzImage" case "ppc64le": target = "zImage" } - if err := runMake(kernelDir, target, "CC="+compiler); err != nil { + if err := runMake(params.KernelDir, target, "CC="+params.Compiler); err != nil { return err } - vmlinux := filepath.Join(kernelDir, "vmlinux") - outputVmlinux := filepath.Join(outputDir, "obj", "vmlinux") + vmlinux := filepath.Join(params.KernelDir, "vmlinux") + outputVmlinux := filepath.Join(params.OutputDir, "obj", "vmlinux") if err := osutil.Rename(vmlinux, outputVmlinux); err != nil { return fmt.Errorf("failed to rename vmlinux: %v", err) } return nil } -func (linux) createImage(targetArch, vmType, kernelDir, outputDir, userspaceDir, cmdlineFile, sysctlFile string) error { +func (linux) createImage(params *Params) error { tempDir, err := ioutil.TempDir("", "syz-build") if err != nil { return err @@ -84,30 +82,30 @@ func (linux) createImage(targetArch, vmType, kernelDir, outputDir, userspaceDir, } var kernelImage string - switch targetArch { + switch params.TargetArch { case "386", "amd64": kernelImage = "arch/x86/boot/bzImage" case "ppc64le": kernelImage = "arch/powerpc/boot/zImage.pseries" } - kernelImagePath := filepath.Join(kernelDir, filepath.FromSlash(kernelImage)) - cmd := osutil.Command(scriptFile, userspaceDir, kernelImagePath, targetArch) + kernelImagePath := filepath.Join(params.KernelDir, filepath.FromSlash(kernelImage)) + cmd := osutil.Command(scriptFile, params.UserspaceDir, kernelImagePath, params.TargetArch) cmd.Dir = tempDir cmd.Env = append([]string{}, os.Environ()...) cmd.Env = append(cmd.Env, - "SYZ_VM_TYPE="+vmType, - "SYZ_CMDLINE_FILE="+osutil.Abs(cmdlineFile), - "SYZ_SYSCTL_FILE="+osutil.Abs(sysctlFile), + "SYZ_VM_TYPE="+params.VMType, + "SYZ_CMDLINE_FILE="+osutil.Abs(params.CmdlineFile), + "SYZ_SYSCTL_FILE="+osutil.Abs(params.SysctlFile), ) if _, err = osutil.Run(time.Hour, cmd); err != nil { return fmt.Errorf("image build failed: %v", err) } // Note: we use CopyFile instead of Rename because src and dst can be on different filesystems. - imageFile := filepath.Join(outputDir, "image") + imageFile := filepath.Join(params.OutputDir, "image") if err := osutil.CopyFile(filepath.Join(tempDir, "disk.raw"), imageFile); err != nil { return err } - keyFile := filepath.Join(outputDir, "key") + keyFile := filepath.Join(params.OutputDir, "key") if err := osutil.CopyFile(filepath.Join(tempDir, "key"), keyFile); err != nil { return err } diff --git a/pkg/build/netbsd.go b/pkg/build/netbsd.go index 8f603cc5b..c34bd4481 100644 --- a/pkg/build/netbsd.go +++ b/pkg/build/netbsd.go @@ -20,38 +20,38 @@ import ( type netbsd struct{} -func (ctx netbsd) build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir, - cmdlineFile, sysctlFile string, config []byte) error { +func (ctx netbsd) build(params *Params) error { const kernelName = "GENERIC_SYZKALLER" - confDir := fmt.Sprintf("%v/sys/arch/%v/conf", kernelDir, targetArch) - compileDir := fmt.Sprintf("%v/sys/arch/%v/compile/obj/%v", kernelDir, targetArch, kernelName) + confDir := fmt.Sprintf("%v/sys/arch/%v/conf", params.KernelDir, params.TargetArch) + compileDir := fmt.Sprintf("%v/sys/arch/%v/compile/obj/%v", params.KernelDir, params.TargetArch, kernelName) - if err := osutil.WriteFile(filepath.Join(confDir, kernelName), config); err != nil { + if err := osutil.WriteFile(filepath.Join(confDir, kernelName), params.Config); err != nil { return err } // Build tools before building kernel - if _, err := osutil.RunCmd(10*time.Minute, kernelDir, "./build.sh", "-m", targetArch, + if _, err := osutil.RunCmd(10*time.Minute, params.KernelDir, "./build.sh", "-m", params.TargetArch, "-U", "-u", "-j"+strconv.Itoa(runtime.NumCPU()), "-V", "MKCTF=no", "tools"); err != nil { return err } // Build kernel - if _, err := osutil.RunCmd(10*time.Minute, kernelDir, "./build.sh", "-m", targetArch, + if _, err := osutil.RunCmd(10*time.Minute, params.KernelDir, "./build.sh", "-m", params.TargetArch, "-U", "-u", "-j"+strconv.Itoa(runtime.NumCPU()), "-V", "MKCTF=no", "kernel="+kernelName); err != nil { return err } for _, s := range []struct{ dir, src, dst string }{ {compileDir, "netbsd.gdb", "obj/netbsd.gdb"}, - {userspaceDir, "image", "image"}, - {userspaceDir, "key", "key"}, + {params.UserspaceDir, "image", "image"}, + {params.UserspaceDir, "key", "key"}, } { fullSrc := filepath.Join(s.dir, s.src) - fullDst := filepath.Join(outputDir, s.dst) + fullDst := filepath.Join(params.OutputDir, s.dst) if err := osutil.CopyFile(fullSrc, fullDst); err != nil { return fmt.Errorf("failed to copy %v -> %v: %v", fullSrc, fullDst, err) } } - return ctx.copyKernelToDisk(targetArch, vmType, outputDir, filepath.Join(compileDir, "netbsd")) + return ctx.copyKernelToDisk(params.TargetArch, params.VMType, params.OutputDir, + filepath.Join(compileDir, "netbsd")) } func (ctx netbsd) clean(kernelDir, targetArch string) error { diff --git a/pkg/build/openbsd.go b/pkg/build/openbsd.go index 49c78c6d0..87be7be26 100644 --- a/pkg/build/openbsd.go +++ b/pkg/build/openbsd.go @@ -16,13 +16,12 @@ import ( type openbsd struct{} -func (ctx openbsd) build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir, - cmdlineFile, sysctlFile string, config []byte) error { +func (ctx openbsd) build(params *Params) error { const kernelName = "SYZKALLER" - confDir := fmt.Sprintf("%v/sys/arch/%v/conf", kernelDir, targetArch) - compileDir := fmt.Sprintf("%v/sys/arch/%v/compile/%v", kernelDir, targetArch, kernelName) + confDir := fmt.Sprintf("%v/sys/arch/%v/conf", params.KernelDir, params.TargetArch) + compileDir := fmt.Sprintf("%v/sys/arch/%v/compile/%v", params.KernelDir, params.TargetArch, kernelName) - if err := osutil.WriteFile(filepath.Join(confDir, kernelName), config); err != nil { + if err := osutil.WriteFile(filepath.Join(confDir, kernelName), params.Config); err != nil { return err } @@ -41,18 +40,18 @@ func (ctx openbsd) build(targetArch, vmType, kernelDir, outputDir, compiler, use for _, s := range []struct{ dir, src, dst string }{ {compileDir, "obj/bsd", "kernel"}, {compileDir, "obj/bsd.gdb", "obj/bsd.gdb"}, - {userspaceDir, "image", "image"}, - {userspaceDir, "key", "key"}, + {params.UserspaceDir, "image", "image"}, + {params.UserspaceDir, "key", "key"}, } { fullSrc := filepath.Join(s.dir, s.src) - fullDst := filepath.Join(outputDir, s.dst) + fullDst := filepath.Join(params.OutputDir, s.dst) if err := osutil.CopyFile(fullSrc, fullDst); err != nil { return fmt.Errorf("failed to copy %v -> %v: %v", fullSrc, fullDst, err) } } - if vmType == "gce" { + if params.VMType == "gce" { return ctx.copyFilesToImage( - filepath.Join(userspaceDir, "overlay"), outputDir) + filepath.Join(params.UserspaceDir, "overlay"), params.OutputDir) } return nil } diff --git a/pkg/build/test.go b/pkg/build/test.go new file mode 100644 index 000000000..e80afcc86 --- /dev/null +++ b/pkg/build/test.go @@ -0,0 +1,14 @@ +// Copyright 2019 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package build + +type test struct{} + +func (tb test) build(params *Params) error { + return nil +} + +func (tb test) clean(string, string) error { + return nil +} diff --git a/pkg/build/testlinux.go b/pkg/build/testlinux.go deleted file mode 100644 index 482c162c5..000000000 --- a/pkg/build/testlinux.go +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2019 syzkaller project authors. All rights reserved. -// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. - -package build - -// TypeBuilder implements the builder interface. -type testBuilder struct{} - -func (tb testBuilder) build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir, - cmdlineFile, sysctlFile string, config []byte) error { - return nil -} - -func (tb testBuilder) clean(string, string) error { - return nil -} diff --git a/pkg/instance/instance.go b/pkg/instance/instance.go index b3e24aace..163ef285a 100644 --- a/pkg/instance/instance.go +++ b/pkg/instance/instance.go @@ -88,16 +88,25 @@ func (env *env) BuildSyzkaller(repo, commit string) error { return nil } -func (env *env) BuildKernel(compilerBin, userspaceDir, cmdlineFile, sysctlFile string, - kernelConfig []byte) (string, error) { - cfg := env.cfg - imageDir := filepath.Join(cfg.Workdir, "image") - if err := build.Image(cfg.TargetOS, cfg.TargetVMArch, cfg.Type, - cfg.KernelSrc, imageDir, compilerBin, userspaceDir, - cmdlineFile, sysctlFile, kernelConfig); err != nil { +func (env *env) BuildKernel(compilerBin, userspaceDir, cmdlineFile, sysctlFile string, kernelConfig []byte) ( + string, error) { + imageDir := filepath.Join(env.cfg.Workdir, "image") + params := &build.Params{ + TargetOS: env.cfg.TargetOS, + TargetArch: env.cfg.TargetVMArch, + VMType: env.cfg.Type, + KernelDir: env.cfg.KernelSrc, + OutputDir: imageDir, + Compiler: compilerBin, + UserspaceDir: userspaceDir, + CmdlineFile: cmdlineFile, + SysctlFile: sysctlFile, + Config: kernelConfig, + } + if err := build.Image(params); err != nil { return "", err } - if err := SetConfigImage(cfg, imageDir, true); err != nil { + if err := SetConfigImage(env.cfg, imageDir, true); err != nil { return "", err } kernelConfigFile := filepath.Join(imageDir, "kernel.config") |
