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