github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/pkg/build/starnix.go (about)

     1  // Copyright 2024 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  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  	"time"
    12  
    13  	"github.com/google/syzkaller/pkg/osutil"
    14  	"github.com/google/syzkaller/sys/targets"
    15  	starnixVM "github.com/google/syzkaller/vm/starnix"
    16  )
    17  
    18  type starnix struct{}
    19  
    20  // The location of the default ffx build config (a Fuchsia implementation detail),
    21  // relative to the kernel build dir. Contains default relative paths of build
    22  // artifacts.
    23  const ffxBuildConfig = "ffx-config.json"
    24  
    25  // The name of the Fuchsia assembly override defined in `overrideRule`.
    26  const overrideName = "syzkaller_starnix"
    27  
    28  // A Fuchsia assembly override definition adding a package containing fuzzing dependencies.
    29  var overrideRule = fmt.Sprintf(`import("//build/assembly/developer_overrides.gni")
    30  
    31  assembly_developer_overrides("%s") {
    32      testonly = true
    33      base_packages = [
    34          "//src/testing/fuzzing/syzkaller/starnix:syzkaller_starnix",
    35      ]
    36  }
    37  `, overrideName)
    38  
    39  func (st starnix) build(params Params) (ImageDetails, error) {
    40  	sysTarget := targets.Get(targets.Linux, params.TargetArch)
    41  	arch := sysTarget.KernelArch
    42  	if arch != "x86_64" {
    43  		return ImageDetails{}, fmt.Errorf("unsupported starnix arch %v", arch)
    44  	}
    45  	arch = "x64"
    46  	product := fmt.Sprintf("%s.%s", "workbench_eng", arch)
    47  
    48  	localDir := filepath.Join(params.KernelDir, "local")
    49  	if err := os.MkdirAll(filepath.Join(params.KernelDir, "local"), 0755); err != nil {
    50  		return ImageDetails{}, err
    51  	}
    52  	overridePath := filepath.Join(localDir, "BUILD.gn")
    53  	if err := os.WriteFile(overridePath, []byte(overrideRule), 0660); err != nil {
    54  		return ImageDetails{}, err
    55  	}
    56  	if err := osutil.SandboxChown(overridePath); err != nil {
    57  		return ImageDetails{}, err
    58  	}
    59  	if err := osutil.SandboxChown(localDir); err != nil {
    60  		return ImageDetails{}, err
    61  	}
    62  
    63  	if _, err := runSandboxed(
    64  		30*time.Second,
    65  		params.KernelDir,
    66  		"scripts/fx", "build-profile", "disable",
    67  	); err != nil {
    68  		return ImageDetails{}, err
    69  	}
    70  
    71  	buildSubdir := "out/" + arch
    72  	if _, err := runSandboxed(
    73  		time.Hour,
    74  		params.KernelDir,
    75  		"scripts/fx", "--dir", buildSubdir,
    76  		"set", product,
    77  		"--debug",
    78  		"--assembly-override", fmt.Sprintf("//products/workbench/*=//local:%s", overrideName),
    79  	); err != nil {
    80  		return ImageDetails{}, err
    81  	}
    82  
    83  	if _, err := runSandboxed(time.Hour*2, params.KernelDir, "scripts/fx", "build"); err != nil {
    84  		return ImageDetails{}, err
    85  	}
    86  	ffxBinary, err := starnixVM.GetToolPath(params.KernelDir, "ffx")
    87  	if err != nil {
    88  		return ImageDetails{}, err
    89  	}
    90  	productBundlePathRaw, err := runSandboxed(
    91  		30*time.Second,
    92  		params.KernelDir,
    93  		ffxBinary,
    94  		"--no-environment",
    95  		"-c", filepath.Join(params.KernelDir, buildSubdir, ffxBuildConfig),
    96  		"-c", "log.enabled=false,ffx.analytics.disabled=true,daemon.autostart=false",
    97  		"config", "get", "product.path",
    98  	)
    99  	if err != nil {
   100  		return ImageDetails{}, err
   101  	}
   102  	productBundlePath := strings.Trim(string(productBundlePathRaw), "\"\n")
   103  	fxfsPathRaw, err := runSandboxed(
   104  		30*time.Second,
   105  		params.KernelDir,
   106  		ffxBinary,
   107  		"--no-environment",
   108  		"-c", "log.enabled=false,ffx.analytics.disabled=true,daemon.autostart=false",
   109  		"product", "get-image-path", productBundlePath,
   110  		"--slot", "a",
   111  		"--image-type", "fxfs.fastboot",
   112  	)
   113  	if err != nil {
   114  		return ImageDetails{}, err
   115  	}
   116  	fxfsPath := strings.Trim(string(fxfsPathRaw), "\"\n")
   117  	if err := osutil.CopyFile(fxfsPath, filepath.Join(params.OutputDir, "image")); err != nil {
   118  		return ImageDetails{}, err
   119  	}
   120  	kernelObjPath := filepath.Join(params.KernelDir, "out", arch, "exe.unstripped", "starnix_kernel")
   121  	if err := osutil.CopyFile(kernelObjPath, filepath.Join(params.OutputDir, "obj", "vmlinux")); err != nil {
   122  		return ImageDetails{}, err
   123  	}
   124  	return ImageDetails{}, nil
   125  }
   126  
   127  func (st starnix) clean(params Params) error {
   128  	_, err := runSandboxed(
   129  		time.Hour,
   130  		params.KernelDir,
   131  		"scripts/fx", "--dir", "out/"+params.TargetArch,
   132  		"clean",
   133  	)
   134  	return err
   135  }