Skip to content

Commit

Permalink
pkg/build: add build command for starnix
Browse files Browse the repository at this point in the history
  • Loading branch information
glpesk committed Sep 19, 2024
1 parent c673ca0 commit 362f37b
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ func getBuilder(targetOS, targetArch, vmType string) (builder, error) {
return cuttlefish{}, nil
} else if vmType == "proxyapp:android" {
return android{}, nil
} else if vmType == targets.Starnix {
return starnix{}, nil
}
}
builders := map[string]builder{
Expand Down
116 changes: 116 additions & 0 deletions pkg/build/starnix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright 2024 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package build

import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/sys/targets"
starnixVM "github.com/google/syzkaller/vm/starnix"
)

type starnix struct{}

// The name of the Fuchsia assembly override defined in `overrideRule`.
const overrideName = "syzkaller_starnix"

// A Fuchsia assembly override definition adding a package containing fuzzing dependencies.
var overrideRule = fmt.Sprintf(`import("//build/assembly/developer_overrides.gni")
assembly_developer_overrides("%s") {
testonly = true
base_packages = [
"//src/testing/fuzzing/syzkaller/starnix:syzkaller_starnix",
]
}
`, overrideName)

func (st starnix) build(params Params) (ImageDetails, error) {
sysTarget := targets.Get(targets.Linux, params.TargetArch)
arch := sysTarget.KernelArch
if arch != "x86_64" {
return ImageDetails{}, fmt.Errorf("unsupported starnix arch %v", arch)
}
arch = "x64"
product := fmt.Sprintf("%s.%s", "workbench_eng", arch)

localDir := filepath.Join(params.KernelDir, "local")
if err := os.MkdirAll(filepath.Join(params.KernelDir, "local"), 0750); err != nil {
return ImageDetails{}, err
}
if err := osutil.SandboxChown(localDir); err != nil {
return ImageDetails{}, err
}
overridePath := filepath.Join(localDir, "BUILD.gn")
if err := os.WriteFile(overridePath, []byte(overrideRule), 0660); err != nil {
return ImageDetails{}, err
}
if err := osutil.SandboxChown(overridePath); err != nil {
return ImageDetails{}, err
}
if _, err := runSandboxed(
time.Hour,
params.KernelDir,
"scripts/fx", "--dir", "out/"+arch,
"set", product,
"--assembly-override", fmt.Sprintf("'//products/workbench/*=//local:%s'", overrideName),
); err != nil {
return ImageDetails{}, err
}

imageFilePath := filepath.Join(params.OutputDir, "image")
imageFile, err := os.Create(imageFilePath)
if err != nil {
return ImageDetails{}, fmt.Errorf("failed to create output file: %w", err)
}
defer imageFile.Close()

if _, err := runSandboxed(time.Hour*2, params.KernelDir, "scripts/fx", "build"); err != nil {
return ImageDetails{}, err
}
ffxBinary, err := starnixVM.GetToolPath(params.KernelDir, "ffx")
if err != nil {
return ImageDetails{}, err
}
productBundlePathRaw, err := runSandboxed(
30*time.Second,
params.KernelDir,
ffxBinary,
"config", "get", "product.path",
)
if err != nil {
return ImageDetails{}, nil
}
productBundlePath := strings.Trim(string(productBundlePathRaw), "\"")
fxfsPath, err := runSandboxed(
30*time.Second,
params.KernelDir,
ffxBinary,
"product", "get-image-path", productBundlePath,
"--slot", "a",
"--image-type", "fxfs",
)
if err != nil {
return ImageDetails{}, nil
}
if err := osutil.CopyFile(string(fxfsPath), imageFilePath); err != nil {
return ImageDetails{}, err
}
return ImageDetails{}, nil
}

func (st starnix) clean(kernelDir, targetArch string) error {
_, err := runSandboxed(
time.Hour,
kernelDir,
"scripts/fx", "--dir", "out/"+targetArch,
"clean",
)
return err
}
4 changes: 3 additions & 1 deletion pkg/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ func (env *env) BuildKernel(buildCfg *BuildKernelConfig) (
}

func SetConfigImage(cfg *mgrconfig.Config, imageDir string, reliable bool) error {
cfg.KernelObj = filepath.Join(imageDir, "obj")
if cfg.Type != targets.Starnix {
cfg.KernelObj = filepath.Join(imageDir, "obj")
}
cfg.Image = filepath.Join(imageDir, "image")
if keyFile := filepath.Join(imageDir, "key"); osutil.IsExist(keyFile) {
cfg.SSHKey = keyFile
Expand Down
4 changes: 2 additions & 2 deletions pkg/vcs/fuchsia.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func newFuchsia(dir string, opts []RepoOpt) *fuchsia {
}

func (ctx *fuchsia) Poll(repo, branch string) (*Commit, error) {
if repo != "https://fuchsia.googlesource.com" || branch != "master" {
if repo != "https://fuchsia.googlesource.com/fuchsia" || branch != "master" {
// Fuchsia ecosystem is hard-wired to the main repo.
return nil, fmt.Errorf("fuchsia: can only check out https://fuchsia.googlesource.com/master")
return nil, fmt.Errorf("fuchsia: can only check out https://fuchsia.googlesource.com/fuchsia/+/master")
}
if _, err := runSandboxed(ctx.dir, "./.jiri_root/bin/jiri", "update"); err != nil {
if err := ctx.initRepo(); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ const (
func NewRepo(os, vmType, dir string, opts ...RepoOpt) (Repo, error) {
switch os {
case targets.Linux:
if vmType == targets.Starnix {
return newFuchsia(dir, opts), nil
}
return newLinux(dir, opts, vmType), nil
case targets.Fuchsia:
return newFuchsia(dir, opts), nil
Expand Down
4 changes: 2 additions & 2 deletions vm/starnix/starnix.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (pool *Pool) Create(workdir string, index int) (vmimpl.Instance, error) {
}()

var err error
inst.ffxBinary, err = getToolPath(inst.fuchsiaDirectory, "ffx")
inst.ffxBinary, err = GetToolPath(inst.fuchsiaDirectory, "ffx")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -454,7 +454,7 @@ type toolMetadata struct {
}

// Resolve a tool by name using tool_paths.json in the build dir.
func getToolPath(fuchsiaDir, toolName string) (string, error) {
func GetToolPath(fuchsiaDir, toolName string) (string, error) {
buildDir, err := getFuchsiaBuildDir(fuchsiaDir)
if err != nil {
return "", err
Expand Down

0 comments on commit 362f37b

Please sign in to comment.