github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/pkg/build/openbsd.go (about) 1 // Copyright 2018 syzkaller project authors. All rights reserved. 2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. 3 4 package build 5 6 import ( 7 "fmt" 8 "path/filepath" 9 "strconv" 10 "time" 11 12 "github.com/google/syzkaller/pkg/osutil" 13 ) 14 15 type openbsd struct{} 16 17 func (ctx openbsd) build(params Params) (ImageDetails, error) { 18 const kernelName = "SYZKALLER" 19 confDir := fmt.Sprintf("%v/sys/arch/%v/conf", params.KernelDir, params.TargetArch) 20 compileDir := fmt.Sprintf("%v/sys/arch/%v/compile/%v", params.KernelDir, params.TargetArch, kernelName) 21 22 if err := osutil.WriteFile(filepath.Join(confDir, kernelName), params.Config); err != nil { 23 return ImageDetails{}, err 24 } 25 26 if err := osutil.MkdirAll(compileDir); err != nil { 27 return ImageDetails{}, err 28 } 29 makefile := []byte(".include \"../Makefile.inc\"\n") 30 if err := osutil.WriteFile(filepath.Join(compileDir, "Makefile"), makefile); err != nil { 31 return ImageDetails{}, err 32 } 33 for _, tgt := range []string{"clean", "obj", "config", "all"} { 34 if err := ctx.make(compileDir, params.BuildCPUs, tgt); err != nil { 35 return ImageDetails{}, err 36 } 37 } 38 for _, s := range []struct{ dir, src, dst string }{ 39 {compileDir, "obj/bsd", "kernel"}, 40 {compileDir, "obj/bsd.gdb", "obj/bsd.gdb"}, 41 {params.UserspaceDir, "image", "image"}, 42 {params.UserspaceDir, "key", "key"}, 43 } { 44 fullSrc := filepath.Join(s.dir, s.src) 45 fullDst := filepath.Join(params.OutputDir, s.dst) 46 if err := osutil.CopyFile(fullSrc, fullDst); err != nil { 47 return ImageDetails{}, fmt.Errorf("failed to copy %v -> %v: %w", fullSrc, fullDst, err) 48 } 49 } 50 if params.VMType == "gce" { 51 return ImageDetails{}, ctx.copyFilesToImage( 52 filepath.Join(params.UserspaceDir, "overlay"), params.OutputDir) 53 } 54 return ImageDetails{}, nil 55 } 56 57 func (ctx openbsd) clean(params Params) error { 58 // Building clean is fast enough and incremental builds in face of 59 // changing config files don't work. Instead of optimizing for the 60 // case where humans have to think, let's bludgeon it with a 61 // machine. 62 return nil 63 } 64 65 func (ctx openbsd) make(kernelDir string, jobs int, args ...string) error { 66 args = append([]string{"-j", strconv.Itoa(jobs)}, args...) 67 _, err := osutil.RunCmd(10*time.Minute, kernelDir, "make", args...) 68 return err 69 } 70 71 // copyFilesToImage populates the filesystem image in outputDir with 72 // run-specific files. The kernel is copied as /bsd and if overlayDir 73 // exists, its contents are copied into corresponding files in the 74 // image. 75 // 76 // Ideally a user space tool capable of understanding FFS should 77 // interpret FFS inside the image file, but vnd(4) device would do in 78 // a pinch. 79 func (ctx openbsd) copyFilesToImage(overlayDir, outputDir string) error { 80 script := fmt.Sprintf(`set -eux 81 OVERLAY="%s" 82 # Cleanup in case something failed before. 83 sync 84 doas umount /altroot || true 85 doas vnconfig -u vnd0 || true 86 87 doas /sbin/vnconfig vnd0 image 88 doas mount /dev/vnd0a /altroot 89 doas cp kernel /altroot/bsd 90 test -d "$OVERLAY" && doas cp -Rf "$OVERLAY"/. /altroot 91 sync 92 doas umount /altroot 93 doas vnconfig -u vnd0 94 `, overlayDir) 95 _, err := osutil.RunCmd(10*time.Minute, outputDir, "/bin/sh", "-c", script) 96 return err 97 }