How to use kernelBin method of build Package

Best Syzkaller code snippet using build.kernelBin

linux.go

Source:linux.go Github

copy

Full Screen

...30 details.CompilerID, err = queryLinuxCompiler(params.KernelDir)31 if err != nil {32 return details, err33 }34 kernelPath := filepath.Join(params.KernelDir, filepath.FromSlash(kernelBin(params.TargetArch)))35 if fileInfo, err := os.Stat(params.UserspaceDir); err == nil && fileInfo.IsDir() {36 // The old way of assembling the image from userspace dir.37 // It should be removed once all syzbot instances are switched.38 if err := linux.createImage(params, kernelPath); err != nil {39 return details, err40 }41 } else if params.VMType == "qemu" {42 // If UserspaceDir is a file (image) and we use qemu, we just copy image and kernel to the output dir43 // assuming that qemu will use injected kernel boot. In this mode we also assume password/key-less ssh.44 if err := osutil.CopyFile(kernelPath, filepath.Join(params.OutputDir, "kernel")); err != nil {45 return details, err46 }47 if err := osutil.CopyFile(params.UserspaceDir, filepath.Join(params.OutputDir, "image")); err != nil {48 return details, err49 }50 } else if err := embedLinuxKernel(params, kernelPath); err != nil {51 return details, err52 }53 details.Signature, err = elfBinarySignature(filepath.Join(params.OutputDir, "obj", "vmlinux"))54 return details, err55}56func (linux linux) buildKernel(params Params) error {57 configFile := filepath.Join(params.KernelDir, ".config")58 if err := linux.writeFile(configFile, params.Config); err != nil {59 return fmt.Errorf("failed to write config file: %v", err)60 }61 // One would expect olddefconfig here, but olddefconfig is not present in v3.6 and below.62 // oldconfig is the same as olddefconfig if stdin is not set.63 if err := runMake(params, "oldconfig"); err != nil {64 return err65 }66 // Write updated kernel config early, so that it's captured on build failures.67 outputConfig := filepath.Join(params.OutputDir, "kernel.config")68 if err := osutil.CopyFile(configFile, outputConfig); err != nil {69 return err70 }71 // Ensure CONFIG_GCC_PLUGIN_RANDSTRUCT doesn't prevent ccache usage.72 // See /Documentation/kbuild/reproducible-builds.rst.73 const seed = `const char *randstruct_seed = "e9db0ca5181da2eedb76eba144df7aba4b7f9359040ee58409765f2bdc4cb3b8";`74 gccPluginsDir := filepath.Join(params.KernelDir, "scripts", "gcc-plugins")75 if osutil.IsExist(gccPluginsDir) {76 if err := linux.writeFile(filepath.Join(gccPluginsDir, "randomize_layout_seed.h"), []byte(seed)); err != nil {77 return err78 }79 }80 // Different key is generated for each build if key is not provided.81 // see Documentation/reproducible-builds.rst. This is causing problems to our signature calculation.82 certsDir := filepath.Join(params.KernelDir, "certs")83 if osutil.IsExist(certsDir) {84 if err := linux.writeFile(filepath.Join(certsDir, "signing_key.pem"), []byte(moduleSigningKey)); err != nil {85 return err86 }87 }88 target := path.Base(kernelBin(params.TargetArch))89 if err := runMake(params, target); err != nil {90 return err91 }92 vmlinux := filepath.Join(params.KernelDir, "vmlinux")93 outputVmlinux := filepath.Join(params.OutputDir, "obj", "vmlinux")94 if err := osutil.Rename(vmlinux, outputVmlinux); err != nil {95 return fmt.Errorf("failed to rename vmlinux: %v", err)96 }97 return nil98}99func (linux) createImage(params Params, kernelPath string) error {100 tempDir, err := ioutil.TempDir("", "syz-build")101 if err != nil {102 return err103 }104 defer os.RemoveAll(tempDir)105 scriptFile := filepath.Join(tempDir, "create.sh")106 if err := osutil.WriteExecFile(scriptFile, []byte(createImageScript)); err != nil {107 return fmt.Errorf("failed to write script file: %v", err)108 }109 cmd := osutil.Command(scriptFile, params.UserspaceDir, kernelPath, params.TargetArch)110 cmd.Dir = tempDir111 cmd.Env = append([]string{}, os.Environ()...)112 cmd.Env = append(cmd.Env,113 "SYZ_VM_TYPE="+params.VMType,114 "SYZ_CMDLINE_FILE="+osutil.Abs(params.CmdlineFile),115 "SYZ_SYSCTL_FILE="+osutil.Abs(params.SysctlFile),116 )117 if _, err = osutil.Run(time.Hour, cmd); err != nil {118 return fmt.Errorf("image build failed: %v", err)119 }120 // Note: we use CopyFile instead of Rename because src and dst can be on different filesystems.121 imageFile := filepath.Join(params.OutputDir, "image")122 if err := osutil.CopyFile(filepath.Join(tempDir, "disk.raw"), imageFile); err != nil {123 return err124 }125 return nil126}127func (linux) clean(kernelDir, targetArch string) error {128 return runMakeImpl(targetArch, "", "", kernelDir, "distclean")129}130func (linux) writeFile(file string, data []byte) error {131 if err := osutil.WriteFile(file, data); err != nil {132 return err133 }134 return osutil.SandboxChown(file)135}136func runMakeImpl(arch, compiler, ccache, kernelDir string, addArgs ...string) error {137 target := targets.Get(targets.Linux, arch)138 args := LinuxMakeArgs(target, compiler, ccache, "")139 args = append(args, addArgs...)140 cmd := osutil.Command("make", args...)141 if err := osutil.Sandbox(cmd, true, true); err != nil {142 return err143 }144 cmd.Dir = kernelDir145 cmd.Env = append([]string{}, os.Environ()...)146 // This makes the build [more] deterministic:147 // 2 builds from the same sources should result in the same vmlinux binary.148 // Build on a release commit and on the previous one should result in the same vmlinux too.149 // We use it for detecting no-op changes during bisection.150 cmd.Env = append(cmd.Env,151 "KBUILD_BUILD_VERSION=0",152 "KBUILD_BUILD_TIMESTAMP=now",153 "KBUILD_BUILD_USER=syzkaller",154 "KBUILD_BUILD_HOST=syzkaller",155 "KERNELVERSION=syzkaller",156 "LOCALVERSION=-syzkaller",157 )158 _, err := osutil.Run(time.Hour, cmd)159 return err160}161func runMake(params Params, addArgs ...string) error {162 return runMakeImpl(params.TargetArch, params.Compiler, params.Ccache, params.KernelDir, addArgs...)163}164func LinuxMakeArgs(target *targets.Target, compiler, ccache, buildDir string) []string {165 args := []string{166 "-j", fmt.Sprint(runtime.NumCPU()),167 "ARCH=" + target.KernelArch,168 }169 if target.Triple != "" {170 args = append(args, "CROSS_COMPILE="+target.Triple+"-")171 }172 if compiler == "" {173 compiler = target.KernelCompiler174 if target.KernelLinker != "" {175 args = append(args, "LD="+target.KernelLinker)176 }177 }178 if compiler != "" {179 if ccache != "" {180 compiler = ccache + " " + compiler181 }182 args = append(args, "CC="+compiler)183 }184 if buildDir != "" {185 args = append(args, "O="+buildDir)186 }187 return args188}189func kernelBin(arch string) string {190 // We build only zImage/bzImage as we currently don't use modules.191 switch arch {192 case targets.AMD64:193 return "arch/x86/boot/bzImage"194 case targets.I386:195 return "arch/x86/boot/bzImage"196 case targets.S390x:197 return "arch/s390/boot/bzImage"198 case targets.PPC64LE:199 return "arch/powerpc/boot/zImage.pseries"200 case targets.ARM:201 return "arch/arm/boot/zImage"202 case targets.ARM64:203 return "arch/arm64/boot/Image.gz"...

Full Screen

Full Screen

builderfile.go

Source:builderfile.go Github

copy

Full Screen

...46 QEMU_OPT = initQemuOpt()47 QEMU_DEBUG_OPT = initQemuDebugOpt()48)49var (50 kernelBin string51)52const (53 goMajorVersionSupported = 154 maxGoMinorVersionSupported = 1755)56// Kernel target build the ELF kernel file for Bhojpur Kernel, generate kernel.elf57func Kernel() error {58 utils.Deps(BhojpurKernel)59 detectGoVersion()60 return rundir("pkg/base/app", nil, kernelBin, "build", "-o", "../../../kernel.elf",61 "-gcflags", GOGCFLAGS,62 "-tags", GOTAGS,63 "./kmain")64}65func Boot64() error {66 compileCfile("boot/boot64.S", "-m64")67 compileCfile("boot/boot64main.c", "-m64")68 ldflags := "-Ttext 0x3200000 -m elf_x86_64 -o boot64.elf boot64.o boot64main.o"69 ldArgs := append([]string{}, LDFLAGS...)70 ldArgs = append(ldArgs, strings.Fields(ldflags)...)71 return sh.RunV(LD, ldArgs...)72}73// Multiboot target build Multiboot specification compatible elf format, generate multiboot.elf74func Multiboot() error {75 utils.Deps(Boot64)76 compileCfile("boot/multiboot.c", "-m32")77 compileCfile("boot/multiboot_header.S", "-m32")78 ldflags := "-Ttext 0x3300000 -m elf_i386 -o multiboot.elf multiboot.o multiboot_header.o -b binary boot64.elf"79 ldArgs := append([]string{}, LDFLAGS...)80 ldArgs = append(ldArgs, strings.Fields(ldflags)...)81 err := sh.RunV(LD, ldArgs...)82 if err != nil {83 return err84 }85 return sh.Copy(86 filepath.Join("cmd", "server", "assets", "boot", "multiboot.elf"),87 "multiboot.elf",88 )89}90func Test() error {91 utils.Deps(BhojpurKernel)92 envs := map[string]string{93 "QEMU_OPTS": quoteArgs(QEMU_OPT),94 }95 return rundir("tests", envs, kernelBin, "test")96}97func TestDebug() error {98 utils.Deps(BhojpurKernel)99 envs := map[string]string{100 "QEMU_OPTS": quoteArgs(QEMU_DEBUG_OPT),101 }102 return rundir("tests", envs, kernelBin, "test")103}104// Qemu run multiboot.elf on qemu.105// If env QEMU_ACCEL is set,QEMU acceleration will be enabled.106// If env QEMU_GRAPHIC is set QEMU will run in graphic mode.107// Use Crtl+a c to switch console, and type `quit`108func Qemu() error {109 utils.Deps(Kernel)110 detectQemu()111 return kernelrun(QEMU_OPT, "kernel.elf")112}113// QemuDebug run multiboot.elf in debug mode.114// Monitor GDB connection on port 1234115func QemuDebug() error {116 GOGCFLAGS += " -N -l"117 utils.Deps(Kernel)118 detectQemu()119 return kernelrun(QEMU_DEBUG_OPT, "kernel.elf")120}121// Iso generate bhojpur-kernel.iso, which can be used with qemu -cdrom option.122func Iso() error {123 utils.Deps(Kernel)124 return sh.RunV(kernelBin, "pack", "-o", "bhojpur-kernel.iso", "-k", "kernel.elf")125}126// Graphic run bhojpur-kernel.iso on qemu, which vbe is enabled.127func Graphic() error {128 detectQemu()129 utils.Deps(Iso)130 return kernelrun(QEMU_OPT, "bhojpur-kernel.iso")131}132// GraphicDebug run bhojpur-kernel.iso on qemu in debug mode.133func GraphicDebug() error {134 detectQemu()135 GOGCFLAGS += " -N -l"136 utils.Deps(Iso)137 return kernelrun(QEMU_DEBUG_OPT, "bhojpur-kernel.iso")138}139func BhojpurKernel() error {140 err := rundir("pkg/base/cmd", nil, "go", "build", "-o", "../../../kernel", "./pkg/base/kernel")141 if err != nil {142 return err143 }144 current, _ := os.Getwd()145 kernelBin = filepath.Join(current, "kernel")146 return nil147}148func Clean() {149 rmGlob("*.o")150 rmGlob("kernel.elf")151 rmGlob("multiboot.elf")152 rmGlob("qemu.log")153 rmGlob("qemu.pcap")154 rmGlob("bhojpur-kernel.iso")155 rmGlob("kernel")156 rmGlob("boot64.elf")157 rmGlob("bochs.log")158}159func detectToolPrefix() string {160 prefix := os.Getenv("TOOLPREFIX")161 if prefix != "" {162 return prefix163 }164 if hasOutput("elf32-i386", "x86_64-elf-objdump", "-i") {165 return "x86_64-elf-"166 }167 if hasOutput("elf32-i386", "i386-elf-objdump", "-i") {168 return "i386-elf-"169 }170 if hasOutput("elf32-i386", "objdump", "-i") {171 return ""172 }173 panic(`174 *** Error: Couldn't find an i386-*-elf or x86_64-*-elf version of GCC/binutils175 *** Is the directory with i386-elf-gcc or x86_64-elf-gcc in your PATH?176 *** If your i386/x86_64-*-elf toolchain is installed with a command177 *** prefix other than 'i386/x86_64-elf-', set your TOOLPREFIX178 *** environment variable to that prefix and run 'make' again.179 `)180}181var goVersionRegexp = regexp.MustCompile(`go(\d+)\.(\d+)\.?(\d?)`)182func goVersion() (string, int, int, error) {183 versionBytes, err := cmdOutput(gobin(), "version")184 if err != nil {185 return "", 0, 0, err186 }187 version := strings.TrimSpace(string(versionBytes))188 result := goVersionRegexp.FindStringSubmatch(version)189 if len(result) < 3 {190 return "", 0, 0, fmt.Errorf("use of unreleased Go version `%s`, may not work", version)191 }192 major, _ := strconv.Atoi(result[1])193 minor, _ := strconv.Atoi(result[2])194 return version, major, minor, nil195}196func detectGoVersion() {197 version, major, minor, err := goVersion()198 if err != nil {199 fmt.Printf("warning: %s\n", err)200 return201 }202 if !(major == goMajorVersionSupported && minor <= maxGoMinorVersionSupported) {203 fmt.Printf("warning: max supported Go version go%d.%d.x, found Go version `%s`, may not work\n",204 goMajorVersionSupported, maxGoMinorVersionSupported, version,205 )206 return207 }208}209func gobin() string {210 goroot := os.Getenv("BHOJPUR_KERNEL_GOROOT")211 if goroot != "" {212 return filepath.Join(goroot, "bin", "go")213 }214 return "go"215}216func detectQemu() {217 if !hasCommand(QEMU64) {218 panic(QEMU64 + ` command not found`)219 }220}221func accelArg() []string {222 switch runtime.GOOS {223 case "darwin":224 return []string{"-M", "accel=hvf"}225 default:226 // fmt.Printf("accel method not found")227 return nil228 }229}230func initCflags() []string {231 cflags := strings.Fields("-fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -Werror -fno-omit-frame-pointer -I. -nostdinc")232 if hasOutput("-fno-stack-protector", CC, "--help") {233 cflags = append(cflags, "-fno-stack-protector")234 }235 if hasOutput("[^f]no-pie", CC, "-dumpspecs") {236 cflags = append(cflags, "-fno-pie", "-no-pie")237 }238 if hasOutput("[^f]nopie", CC, "-dumpspecs") {239 cflags = append(cflags, "-fno-pie", "-nopie")240 }241 return cflags242}243func initLdflags() []string {244 ldflags := strings.Fields("-N -e _start")245 return ldflags246}247func initQemuOpt() []string {248 var opts []string249 if os.Getenv("QEMU_ACCEL") != "" {250 opts = append(opts, accelArg()...)251 }252 if os.Getenv("QEMU_GRAPHIC") == "" {253 opts = append(opts, "-nographic")254 }255 return opts256}257func initQemuDebugOpt() []string {258 opts := `259 -d int -D qemu.log260 -object filter-dump,id=f1,netdev=eth0,file=qemu.pcap261 -s -S262 `263 ret := append([]string{}, initQemuOpt()...)264 ret = append(ret, strings.Fields(opts)...)265 return ret266}267func compileCfile(file string, extFlags ...string) {268 args := append([]string{}, CFLAGS...)269 args = append(args, extFlags...)270 args = append(args, "-c", file)271 err := sh.RunV(CC, args...)272 if err != nil {273 panic(err)274 }275}276func rundir(dir string, envs map[string]string, cmd string, args ...string) error {277 current, _ := os.Getwd()278 os.Chdir(dir)279 defer os.Chdir(current)280 return sh.RunWithV(envs, cmd, args...)281}282func kernelrun(qemuArgs []string, flags ...string) error {283 qemuOpts := quoteArgs(qemuArgs)284 var args []string285 args = append(args, "run")286 args = append(args, "-p", "8080:80")287 args = append(args, flags...)288 envs := map[string]string{289 "QEMU_OPTS": qemuOpts,290 }291 return sh.RunWithV(envs, kernelBin, args...)292}293func cmdOutput(cmd string, args ...string) ([]byte, error) {294 return exec.Command(cmd, args...).CombinedOutput()295}296// quote string which has spaces with ""297func quoteArgs(args []string) string {298 var ret []string299 for _, s := range args {300 if strings.Index(s, " ") != -1 {301 ret = append(ret, strconv.Quote(s))302 } else {303 ret = append(ret, s)304 }305 }...

Full Screen

Full Screen

kernelBin

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 dir, err := os.Getwd()4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 cmd := exec.Command("go", "build", "-o", dir+"/kernelBin")9 err = cmd.Run()10 if err != nil {11 fmt.Println(err)12 os.Exit(1)13 }14}15import (16func main() {17 dir, err := os.Getwd()18 if err != nil {19 fmt.Println(err)20 os.Exit(1)21 }22 cmd := exec.Command("go", "build", "-o", dir+"/kernelBin")23 err = cmd.Run()24 if err != nil {25 fmt.Println(err)26 os.Exit(1)27 }28}29import (30func main() {31 dir, err := os.Getwd()32 if err != nil {33 fmt.Println(err)34 os.Exit(1)35 }36 cmd := exec.Command("go", "build", "-o", dir+"/kernelBin")37 err = cmd.Run()38 if err != nil {39 fmt.Println(err)40 os.Exit(1)41 }42}43import (44func main() {45 dir, err := os.Getwd()

Full Screen

Full Screen

kernelBin

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 vm := otto.New()4 vm.Run(`5 var build = require('build');6 var kernel = build.kernelBin();7 console.log(kernel);8}9var build = {10 kernelBin: function() {11 return "kernel.bin";12 }13}14module.exports = build;

Full Screen

Full Screen

kernelBin

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 kernelBin, err := build.KernelBin()4 if err != nil {5 fmt.Println("Error: ", err)6 }7 fmt.Println("kernelBin: ", kernelBin)8}9How to use KernelRelease() method?10func KernelRelease() (string, error)11import (12func main() {13 release, err := build.KernelRelease()14 if err != nil {15 fmt.Println("Error: ", err)16 }17 fmt.Println("release: ", release)18}19How to use KernelVersion() method?20func KernelVersion() (string, error)21import (22func main() {23 version, err := build.KernelVersion()24 if err != nil {25 fmt.Println("Error: ", err)26 }27 fmt.Println("version: ", version)28}29How to use GOARCH() method?30The GOARCH() method returns the GOARCH of the running system. The GOARCH() method is a

Full Screen

Full Screen

kernelBin

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 app := cli.NewApp()4 app.Commands = []cli.Command{5 {6 Action: func(c *cli.Context) error {7 fmt.Println("building kernel")8 },9 Subcommands: []cli.Command{10 {11 Action: func(c *cli.Context) error {12 fmt.Println("building kernel bin")13 },14 },15 {16 Action: func(c *cli.Context) error {17 fmt.Println("building kernel iso")18 },19 },20 },21 },22 }23 app.Run(os.Args)24}25import (26func main() {27 app := cli.NewApp()28 app.Commands = []cli.Command{29 {30 Action: func(c *cli.Context) error {31 fmt.Println("building kernel")32 },33 },34 }35 app.Run(os.Args)36}37import (38func main() {39 app := cli.NewApp()40 app.Commands = []cli.Command{41 {42 Action: func(c *cli.Context) error {43 fmt.Println("building kernel")44 },45 Subcommands: []cli.Command{46 {

Full Screen

Full Screen

kernelBin

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("hello")4 b := build.New()5 kernelBin := b.KernelBin()6 fmt.Println("kernelBin:", kernelBin)7}

Full Screen

Full Screen

kernelBin

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 currentDir, _ := os.Getwd()4 build := new(Build)5 build.GetKernelVersion()6 build.GetConfigFile()7 build.GetKernelBin()8 build.GetKernelImage()9 build.GetKernelInitrd()10 build.GetKernelModules()11 build.GetKernelDtb()12 build.GetKernelLog()13 build.GetKernelDefconfig()14 build.GetKernelMenuconfig()15 build.GetKernelOlddefconfig()16 build.GetKernelAllnoconfig()17 build.GetKernelAllyesconfig()18 build.GetKernelAllmodconfig()19 build.GetKernelRandconfig()20 build.GetKernelSavedefconfig()21 build.GetKernelTinyconfig()22 build.GetKernelLocalmodconfig()23 build.GetKernelLocalyesconfig()24 build.GetKernelOldconfig()25 build.GetKernelSilentoldconfig()26 build.GetKernelXconfig()27 build.GetKernelGconfig()28 build.GetKernelKvmconfig()29 build.GetKernelLocalversion()30 build.GetKernelLocalversionauto()

Full Screen

Full Screen

kernelBin

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 build := syscall.NewBuild()4 kernelBin, err := build.KernelBin()5 if err != nil {6 fmt.Println("error:", err)7 }8 fmt.Println("kernel binary:", kernelBin)9}10import (11func main() {12 build := syscall.NewBuild()13 kernelVersion, err := build.KernelVersion()14 if err != nil {15 fmt.Println("error:", err)16 }17 fmt.Println("kernel version:", kernelVersion)18}19import (20func main() {21 build := syscall.NewBuild()22 uname, err := build.Uname()23 if err != nil {24 fmt.Println("error:", err)25 }26 fmt.Println("uname:", uname)27}28uname: {Linux 5.8.0-48-generic #54~20.04.1-Ubuntu SMP Mon Jun 21 08:00:11 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux}29import (30func main() {31 build := syscall.NewBuild()

Full Screen

Full Screen

kernelBin

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(os.NewBuild().KernelBin())4}5import (6func main() {7 fmt.Println(os.NewBuild().KernelBin())8}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful