<commit_before>// Copyright 2018 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 contains helper functions for building kernels/images.
package build

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

	"github.com/google/syzkaller/pkg/osutil"
)

// Image creates a disk image for the specified OS/ARCH/VM.
// Kernel is taken from kernelDir, userspace system is taken from userspaceDir.
// If cmdlineFile is not empty, contents of the file are appended to the kernel command line.
// If sysctlFile is not empty, contents of the file are appended to the image /etc/sysctl.conf.
// Output is stored in outputDir and includes (everything except for image is optional):
//  - image: the image
//  - key: ssh key for the image
//  - kernel: kernel for injected boot
//  - initrd: initrd for injected boot
//  - kernel.config: actual kernel config used during build
//  - obj/: directory with kernel object files (this should match KernelObject
//    specified in sys/targets, e.g. vmlinux for linux)
func Image(targetOS, targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir,
	cmdlineFile, sysctlFile string, config []byte) error {
	builder, err := getBuilder(targetOS, targetArch, vmType)
	if err != nil {
		return err
	}
	if err := osutil.MkdirAll(filepath.Join(outputDir, "obj")); err != nil {
		return err
	}
	if len(config) != 0 {
		// Write kernel config early, so that it's captured on build failures.
		if err := osutil.WriteFile(filepath.Join(outputDir, "kernel.config"), config); err != nil {
			return fmt.Errorf("failed to write config file: %v", err)
		}
	}
	return builder.build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir, cmdlineFile, sysctlFile, config)
}

func Clean(targetOS, targetArch, vmType, kernelDir string) error {
	builder, err := getBuilder(targetOS, targetArch, vmType)
	if err != nil {
		return err
	}
	return builder.clean(kernelDir, targetArch)
}

type KernelBuildError struct {
	*osutil.VerboseError
}

type builder interface {
	build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir,
		cmdlineFile, sysctlFile string, config []byte) error
	clean(kernelDir, targetArch string) error
}

func getBuilder(targetOS, targetArch, vmType string) (builder, error) {
	var supported = []struct {
		OS   string
		arch string
		vms  []string
		b    builder
	}{
		{"linux", "amd64", []string{"gvisor"}, gvisor{}},
		{"linux", "amd64", []string{"gce", "qemu"}, linux{}},
		{"fuchsia", "amd64", []string{"gce"}, fuchsia{}},
		{"fuchsia", "arm64", []string{"gce"}, fuchsia{}},
		{"akaros", "amd64", []string{"qemu"}, akaros{}},
		{"openbsd", "amd64", []string{"gce", "vmm"}, openbsd{}},
		{"netbsd", "amd64", []string{"gce", "qemu"}, netbsd{}},
		{"freebsd", "amd64", []string{"gce", "qemu"}, freebsd{}},
	}
	for _, s := range supported {
		if targetOS == s.OS && targetArch == s.arch {
			for _, vm := range s.vms {
				if vmType == vm {
					return s.b, nil
				}
			}
		}
	}
	return nil, fmt.Errorf("unsupported image type %v/%v/%v", targetOS, targetArch, vmType)
}

func CompilerIdentity(compiler string) (string, error) {<commit_msg>pkg/build: fix getBuilder for fuchsia.<commit_after>// Copyright 2018 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 contains helper functions for building kernels/images.
package build

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

	"github.com/google/syzkaller/pkg/osutil"
)

// Image creates a disk image for the specified OS/ARCH/VM.
// Kernel is taken from kernelDir, userspace system is taken from userspaceDir.
// If cmdlineFile is not empty, contents of the file are appended to the kernel command line.
// If sysctlFile is not empty, contents of the file are appended to the image /etc/sysctl.conf.
// Output is stored in outputDir and includes (everything except for image is optional):
//  - image: the image
//  - key: ssh key for the image
//  - kernel: kernel for injected boot
//  - initrd: initrd for injected boot
//  - kernel.config: actual kernel config used during build
//  - obj/: directory with kernel object files (this should match KernelObject
//    specified in sys/targets, e.g. vmlinux for linux)
func Image(targetOS, targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir,
	cmdlineFile, sysctlFile string, config []byte) error {
	builder, err := getBuilder(targetOS, targetArch, vmType)
	if err != nil {
		return err
	}
	if err := osutil.MkdirAll(filepath.Join(outputDir, "obj")); err != nil {
		return err
	}
	if len(config) != 0 {
		// Write kernel config early, so that it's captured on build failures.
		if err := osutil.WriteFile(filepath.Join(outputDir, "kernel.config"), config); err != nil {
			return fmt.Errorf("failed to write config file: %v", err)
		}
	}
	return builder.build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir, cmdlineFile, sysctlFile, config)
}

func Clean(targetOS, targetArch, vmType, kernelDir string) error {
	builder, err := getBuilder(targetOS, targetArch, vmType)
	if err != nil {
		return err
	}
	return builder.clean(kernelDir, targetArch)
}

type KernelBuildError struct {
	*osutil.VerboseError
}

type builder interface {
	build(targetArch, vmType, kernelDir, outputDir, compiler, userspaceDir,
		cmdlineFile, sysctlFile string, config []byte) error
	clean(kernelDir, targetArch string) error
}

func getBuilder(targetOS, targetArch, vmType string) (builder, error) {
	var supported = []struct {
		OS   string
		arch string
		vms  []string
		b    builder
	}{
		{"linux", "amd64", []string{"gvisor"}, gvisor{}},
		{"linux", "amd64", []string{"gce", "qemu"}, linux{}},
		{"fuchsia", "amd64", []string{"qemu"}, fuchsia{}},
		{"fuchsia", "arm64", []string{"qemu"}, fuchsia{}},
		{"akaros", "amd64", []string{"qemu"}, akaros{}},
		{"openbsd", "amd64", []string{"gce", "vmm"}, openbsd{}},
		{"netbsd", "amd64", []string{"gce", "qemu"}, netbsd{}},
		{"freebsd", "amd64", []string{"gce", "qemu"}, freebsd{}},
	}
	for _, s := range supported {
		if targetOS == s.OS && targetArch == s.arch {
			for _, vm := range s.vms {
				if vmType == vm {
					return s.b, nil
				}
			}
		}
	}
	return nil, fmt.Errorf("unsupported image type %v/%v/%v", targetOS, targetArch, vmType)
}

func CompilerIdentity(compiler string) (string, error) {
{'fraction_non_alphanumeric': 0.13849004168596574, 'fraction_numerical': 0.006793268488497761, 'mean_word_length': 4.186549239391513, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1830476', 'n_tokens_mistral': 2222, 'n_tokens_neox': 2076, 'n_words': 710}