github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/tools/syz-build/build.go (about)

     1  // Copyright 2021 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  // syz-build is a wrapper around pkg/build for testing purposes.
     5  package main
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"os"
    11  	"runtime"
    12  
    13  	"github.com/google/syzkaller/pkg/build"
    14  	"github.com/google/syzkaller/pkg/debugtracer"
    15  	"github.com/google/syzkaller/pkg/tool"
    16  )
    17  
    18  var (
    19  	flagOS            = flag.String("os", runtime.GOOS, "OS to build")
    20  	flagArch          = flag.String("arch", runtime.GOARCH, "arch to build")
    21  	flagVM            = flag.String("vm", "gce", "VM type to build")
    22  	flagKernelSrc     = flag.String("kernel_src", "", "path to kernel checkout")
    23  	flagCompiler      = flag.String("compiler", "", "non-defult compiler")
    24  	flagLinker        = flag.String("linker", "", "non-default linker")
    25  	flagKernelConfig  = flag.String("config", "", "kernel config file")
    26  	flagKernelSysctl  = flag.String("sysctl", "", "kernel sysctl file")
    27  	flagKernelCmdline = flag.String("cmdline", "", "kernel cmdline file")
    28  	flagUserspace     = flag.String("userspace", "", "path to userspace for build")
    29  	flagTrace         = flag.Bool("trace", false, "trace build process and save debug artefacts")
    30  )
    31  
    32  func main() {
    33  	flag.Parse()
    34  	if os.Getuid() != 0 {
    35  		fmt.Printf("not running under root, image build may fail\n")
    36  	}
    37  	os.Setenv("SYZ_DISABLE_SANDBOXING", "yes")
    38  	var kernelConfig []byte
    39  	if *flagKernelConfig != "" {
    40  		var err error
    41  		kernelConfig, err = os.ReadFile(*flagKernelConfig)
    42  		if err != nil {
    43  			tool.Fail(err)
    44  		}
    45  	}
    46  	wd, err := os.Getwd()
    47  	if err != nil {
    48  		tool.Fail(err)
    49  	}
    50  	params := build.Params{
    51  		TargetOS:     *flagOS,
    52  		TargetArch:   *flagArch,
    53  		VMType:       *flagVM,
    54  		KernelDir:    *flagKernelSrc,
    55  		OutputDir:    wd,
    56  		Compiler:     *flagCompiler,
    57  		Linker:       *flagLinker,
    58  		Ccache:       "",
    59  		UserspaceDir: *flagUserspace,
    60  		CmdlineFile:  *flagKernelCmdline,
    61  		SysctlFile:   *flagKernelSysctl,
    62  		Config:       kernelConfig,
    63  		Tracer:       &debugtracer.NullTracer{},
    64  	}
    65  	if *flagTrace {
    66  		params.Tracer = &debugtracer.GenericTracer{
    67  			TraceWriter: os.Stdout,
    68  			OutDir:      ".",
    69  		}
    70  	}
    71  	details, err := build.Image(params)
    72  	if err != nil {
    73  		tool.Fail(err)
    74  	}
    75  	params.Tracer.Log("signature: %v", details.Signature)
    76  	params.Tracer.Log("compiler: %v", details.CompilerID)
    77  }