Home | History | Annotate | Download | only in build
      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 	"strings"
     10 	"time"
     11 
     12 	"github.com/google/syzkaller/pkg/osutil"
     13 )
     14 
     15 type gvisor struct{}
     16 
     17 func (gvisor gvisor) build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir,
     18 	cmdlineFile, sysctlFile string, config []byte) error {
     19 	args := []string{"build", "--verbose_failures"}
     20 	if strings.Contains(" "+string(config)+" ", " -race ") {
     21 		args = append(args, "--features=race")
     22 	}
     23 	args = append(args, "runsc")
     24 	if _, err := osutil.RunCmd(20*time.Minute, kernelDir, compiler, args...); err != nil {
     25 		return err
     26 	}
     27 	if err := gvisor.copyBinary(kernelDir, outputDir); err != nil {
     28 		return err
     29 	}
     30 	if len(config) != 0 {
     31 		if err := osutil.WriteFile(filepath.Join(outputDir, "kernel.config"), config); err != nil {
     32 			return fmt.Errorf("failed to save kernel config: %v", err)
     33 		}
     34 	}
     35 	osutil.RunCmd(10*time.Minute, kernelDir, compiler, "shutdown")
     36 	return nil
     37 }
     38 
     39 func (gvisor) copyBinary(kernelDir, outputDir string) error {
     40 	// Funny it's not possible to understand what bazel actually built...
     41 	for _, typ := range []string{
     42 		"linux_amd64_pure_stripped",
     43 		"linux_amd64_static_stripped",
     44 		"linux_amd64_static_race_stripped",
     45 	} {
     46 		runsc := filepath.Join(kernelDir, "bazel-bin", "runsc", typ, "runsc")
     47 		if !osutil.IsExist(runsc) {
     48 			continue
     49 		}
     50 		return osutil.CopyFile(runsc, filepath.Join(outputDir, "image"))
     51 	}
     52 	return fmt.Errorf("failed to locate bazel output")
     53 }
     54 
     55 func (gvisor) clean(kernelDir string) error {
     56 	// Let's assume that bazel always properly handles build without cleaning (until proven otherwise).
     57 	return nil
     58 }
     59