How to use Run method of kvm Package

Best Syzkaller code snippet using kvm.Run

demo.go

Source:demo.go Github

copy

Full Screen

1package main2// #include <linux/kvm.h>3// #include <stdio.h>4// #include <err.h>5//6// static int myhandler(void* r) {7// struct kvm_run* run = (struct kvm_run*) r;8// switch (run->exit_reason) {9// case KVM_EXIT_HLT:10// puts("KVM_EXIT_HLT");11// return 0;12// case KVM_EXIT_IO:13// if (run->io.direction == KVM_EXIT_IO_OUT && run->io.size == 1 && run->io.port == 0x3f8 && run->io.count == 1)14// putchar(*(((char *)run) + run->io.data_offset));15// else16// errx(1, "unhandled KVM_EXIT_IO");17// break;18// case KVM_EXIT_FAIL_ENTRY:19// errx(1, "KVM_EXIT_FAIL_ENTRY: hardware_entry_failure_reason = 0x%llx",20// (unsigned long long)run->fail_entry.hardware_entry_failure_reason);21// case KVM_EXIT_INTERNAL_ERROR:22// errx(1, "KVM_EXIT_INTERNAL_ERROR: suberror = 0x%x", run->internal.suberror);23// default:24// errx(1, "exit_reason = 0x%x", run->exit_reason);25// }26// return 0;27// }28import "C"29import (30 "fmt"31 "log"32 "syscall"33 "unsafe"34)35/* for KVM_SET_USER_MEMORY_REGION */36type kvm_userspace_memory_region struct {37 slot uint3238 flags uint3239 guest_phys_addr uintptr40 memory_size uintptr41 userspace_addr uintptr /* start of the userspace allocated memory */42}43type kvm_segment struct {44 base uint6445 limit uint3246 selector uint1647 tpe uint848 present uint849 dpl uint850 db uint851 s uint852 l uint853 g uint854 avl uint855 unusable uint856 padding uint857}58type kvm_dtable struct {59 base uint6460 limit uint1661 padding [3]uint1662}63/* for KVM_GET_SREGS and KVM_SET_SREGS */64type kvm_sregs struct {65 /* out (KVM_GET_SREGS) / in (KVM_SET_SREGS) */66 cs kvm_segment67 ds kvm_segment68 es kvm_segment69 fs kvm_segment70 gs kvm_segment71 ss kvm_segment72 tr kvm_segment73 ldt kvm_segment74 gdt kvm_dtable75 idt kvm_dtable76 _cr0 uint6477 cr2 uint6478 cr3 uint6479 cr4 uint6480 cr8 uint6481 efer uint6482 apic_base uint6483 interrupt_bitmap [(KVM_NR_INTERRUPTS + 63) / 64]uint6484}85/* for KVM_GET_REGS and KVM_SET_REGS */86type kvm_regs struct {87 /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */88 rax uint6489 rbx uint6490 rcx uint6491 rdx uint6492 rsi uint6493 rdi uint6494 rsp uint6495 rbp uint6496 r8 uint6497 r9 uint6498 r10 uint6499 r11 uint64100 r12 uint64101 r13 uint64102 r14 uint64103 r15 uint64104 rip uint64105 rflags uint64106}107var (108 code = []uint8{109 0xba, 0xf8, 0x03, /* mov $0x3f8, %dx */110 0x00, 0xd8, /* add %bl, %al */111 0x04, '0', /* add $'0', %al */112 0xee, /* out %al, (%dx) */113 0xb0, '\n', /* mov $'\n', %al */114 0xee, /* out %al, (%dx) */115 0xf4, /* hlt */116 }117)118const (119 KVM_GET_API_VERSION = uintptr(44544)120 KVM_CREATE_VM = uintptr(44545)121 KVM_GET_VCPU_MMAP_SIZE = uintptr(44548)122 KVM_CREATE_VCPU = uintptr(44609)123 KVM_RUN = uintptr(44672)124 KVM_SET_USER_MEMORY_REGION = uintptr(1075883590)125 KVM_GET_SREGS = int(-2126991741)126 KVM_SET_SREGS = uintptr(1094233732)127 KVM_SET_REGS = uintptr(1083223682)128 // Other consts129 KVM_NR_INTERRUPTS = 256130)131func ioctl(fd, op, arg uintptr) (uintptr, uintptr, syscall.Errno) {132 return syscall.Syscall(syscall.SYS_IOCTL, fd, op, arg)133}134func mmap(addr, size, prot, flags uintptr, fd int, off uintptr) (uintptr, uintptr, syscall.Errno) {135 return syscall.Syscall6(syscall.SYS_MMAP, addr, size, prot, flags, uintptr(fd), off)136}137func memcpy(dest, src, size uintptr) {138 if dest == 0 || src == 0 {139 panic("nil argument to copy")140 }141 for i := uintptr(0); i < size; i++ {142 d := (*byte)(unsafe.Pointer(dest + i))143 s := (*byte)(unsafe.Pointer(src + i))144 *d = *s145 }146}147func trickGo(a int) uintptr {148 return uintptr(a)149}150func main() {151 kvm, err := syscall.Open("/dev/kvm", syscall.O_RDWR|syscall.O_CLOEXEC, 0)152 if err != nil {153 panic(err.Error())154 }155 r1, _, errno := ioctl(uintptr(kvm), KVM_GET_API_VERSION, uintptr(0))156 if errno != 0 {157 panic(err.Error())158 }159 if r1 != 12 {160 log.Fatalf("KVM_GET_API_VERSION %d, expected 12\n", r1)161 }162 vmfd, _, errno := ioctl(uintptr(kvm), KVM_CREATE_VM, 0)163 if errno != 0 {164 log.Fatalf("Error KVM_CREATE\n")165 }166 //fmt.Println(vmfd)167 mem, _, errno := mmap(0, 0x1000, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED|syscall.MAP_ANONYMOUS, 0, 0)168 if mem == 0 || errno != 0 {169 log.Fatalf("Oups %d - %d\n", mem, errno)170 }171 //fmt.Println(mem)172 memcpy(mem, uintptr(unsafe.Pointer(&code[0])), uintptr(len(code)))173 // Map it to the second page frame (to avoid real-mode IDT at 0)174 region := kvm_userspace_memory_region{175 slot: 0,176 guest_phys_addr: 0x1000,177 memory_size: 0x1000,178 userspace_addr: mem,179 }180 r1, _, errno = ioctl(vmfd, KVM_SET_USER_MEMORY_REGION, uintptr(unsafe.Pointer(&region)))181 if errno != 0 || r1 != 0 {182 log.Fatalf("Ioctl failed %v %v\n", errno, r1)183 }184 vcpufd, _, errno := ioctl(vmfd, KVM_CREATE_VCPU, 0)185 if errno != 0 {186 log.Fatalf("Error KVM_CREATE_VCPU\n")187 }188 //fmt.Println(vcpufd)189 //map the shared kvm_run structure and the following data190 mmap_size, _, errno := ioctl(uintptr(kvm), KVM_GET_VCPU_MMAP_SIZE, 0)191 if errno != 0 {192 log.Fatalf("KVM_GET_VCPU_MMAP_SIZE\n")193 }194 //TODO this c struct is going to be shit to translate.195 _run := [2352]uint8{}196 if mmap_size < uintptr(len(_run)) {197 log.Fatalf("KVM_GET_VCPU_MMAP_SIZE unexpectedly small.\n")198 }199 run, _, errno := mmap(0, mmap_size, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED, int(vcpufd), 0)200 if run == 0 || errno != 0 {201 log.Fatalf("mmap vpcu did not work")202 }203 sregs := kvm_sregs{}204 // Initialize CS to point at 0, via a read-modify-write of sregs205 r1, _, errno = ioctl(vcpufd, trickGo(KVM_GET_SREGS), uintptr(unsafe.Pointer(&sregs)))206 if errno != 0 {207 log.Fatalf("KVM_GET_SREGS\n")208 }209 sregs.cs.base = 0210 sregs.cs.selector = 0211 r1, _, errno = ioctl(vcpufd, KVM_SET_SREGS, uintptr(unsafe.Pointer(&sregs)))212 if errno != 0 {213 log.Fatalf("KVM_SET_SREGS\n")214 }215 regs := kvm_regs{rip: 0x1000, rax: 2, rbx: 2, rflags: 0x2}216 r1, _, errno = ioctl(vcpufd, KVM_SET_REGS, uintptr(unsafe.Pointer(&regs)))217 if errno != 0 {218 log.Fatalf("KVM_SET_REGS\n")219 }220 for {221 r1, _, errno = ioctl(vcpufd, KVM_RUN, 0)222 if errno != 0 || r1 == trickGo(-1) {223 log.Fatalf("KVM_RUN")224 }225 //TODO fix this.226 if C.myhandler(unsafe.Pointer(run)) == 0 {227 fmt.Println("We are done apparently?")228 }229 }230 }...

Full Screen

Full Screen

add_kvm.go

Source:add_kvm.go Github

copy

Full Screen

...22var AddKVMCmd = &subcommands.Command{23 UsageLine: "kvm [Options...]",24 ShortDesc: "Add a kvm to a rack",25 LongDesc: cmdhelp.AddKVMLongDesc,26 CommandRun: func() subcommands.CommandRun {27 c := &addKVM{}28 c.authFlags.Register(&c.Flags, site.DefaultAuthOptions)29 c.envFlags.Register(&c.Flags)30 c.commonFlags.Register(&c.Flags)31 c.Flags.StringVar(&c.newSpecsFile, "f", "", cmdhelp.KVMFileText)32 c.Flags.BoolVar(&c.interactive, "i", false, "enable interactive mode for input")33 c.Flags.StringVar(&c.rackName, "rack", "", "name of the rack to associate the kvm")34 c.Flags.StringVar(&c.kvmName, "name", "", "the name of the kvm to add")35 c.Flags.StringVar(&c.macAddress, "mac", "", "the mac address of the kvm to add")36 c.Flags.StringVar(&c.platform, "platform", "", "the platform of the kvm to add")37 c.Flags.Var(flag.StringSlice(&c.tags), "tag", "Name(s) of tag(s). Can be specified multiple times.")38 return c39 },40}41type addKVM struct {42 subcommands.CommandRunBase43 authFlags authcli.Flags44 envFlags site.EnvFlags45 commonFlags site.CommonFlags46 newSpecsFile string47 interactive bool48 rackName string49 kvmName string50 macAddress string51 platform string52 tags []string53}54func (c *addKVM) Run(a subcommands.Application, args []string, env subcommands.Env) int {55 if err := c.innerRun(a, args, env); err != nil {56 cmdlib.PrintError(a, err)57 return 158 }59 return 060}61func (c *addKVM) innerRun(a subcommands.Application, args []string, env subcommands.Env) error {62 if err := c.validateArgs(); err != nil {63 return err64 }65 ctx := cli.GetContext(a, c, env)66 ns, err := c.envFlags.Namespace()67 if err != nil {68 return err69 }70 ctx = utils.SetupContext(ctx, ns)71 hc, err := cmdlib.NewHTTPClient(ctx, &c.authFlags)72 if err != nil {73 return err74 }75 e := c.envFlags.Env()...

Full Screen

Full Screen

delete_kvm.go

Source:delete_kvm.go Github

copy

Full Screen

...22 LongDesc: `Delete a kvm on a rack.23Example:24shivas delete kvm {KVM Name}25Deletes the given kvm.`,26 CommandRun: func() subcommands.CommandRun {27 c := &deleteKVM{}28 c.authFlags.Register(&c.Flags, site.DefaultAuthOptions)29 c.envFlags.Register(&c.Flags)30 return c31 },32}33type deleteKVM struct {34 subcommands.CommandRunBase35 authFlags authcli.Flags36 envFlags site.EnvFlags37}38func (c *deleteKVM) Run(a subcommands.Application, args []string, env subcommands.Env) int {39 if err := c.innerRun(a, args, env); err != nil {40 cmdlib.PrintError(a, err)41 return 142 }43 return 044}45func (c *deleteKVM) innerRun(a subcommands.Application, args []string, env subcommands.Env) error {46 if err := c.validateArgs(); err != nil {47 return err48 }49 ctx := cli.GetContext(a, c, env)50 ns, err := c.envFlags.Namespace()51 if err != nil {52 return err53 }54 ctx = utils.SetupContext(ctx, ns)55 hc, err := cmdlib.NewHTTPClient(ctx, &c.authFlags)56 if err != nil {57 return err58 }59 e := c.envFlags.Env()...

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 kvm := kvm.NewKvm()4 kvm.Run()5 fmt.Println(kvm.Name)6}

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 os.Exit(1)5 }6 defer conn.Close()7 dom, err := conn.LookupDomainByName("ubuntu")8 if err != nil {9 log.Printf("Failed to get the domain object")10 os.Exit(1)11 }12 defer dom.Free()13 flags, err := dom.GetFlags()14 if err != nil {15 log.Printf("Failed to get the flags for the domain")16 os.Exit(1)17 }18 pretty.Println("Flags: ", flags)19 info, err := dom.GetInfo()20 if err != nil {21 log.Printf("Failed to get the info for the domain")22 os.Exit(1)23 }24 pretty.Println("Info: ", info)25 name, err := dom.GetName()26 if err != nil {27 log.Printf("Failed to get the name of the domain")28 os.Exit(1)29 }30 pretty.Println("Name: ", name)31 uuid, err := dom.GetUUID()32 if err != nil {33 log.Printf("Failed to get the UUID for the domain")34 os.Exit(1)35 }36 pretty.Println("UUID: ", uuid)37 uuidString, err := dom.GetUUIDString()38 if err != nil {39 log.Printf("Failed to get the UUIDString for the domain")40 os.Exit(1)41 }42 pretty.Println("UUIDString: ", uuidString)43 xmlDesc, err := dom.GetXMLDesc(0)44 if err != nil {45 log.Printf("Failed to get the XML description for the domain")46 os.Exit(1)47 }48 pretty.Println("XMLDesc: ", xmlDesc)49 autostart, err := dom.GetAutostart()

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 kvm, err := kvm.New()4 if err != nil {5 fmt.Println(err)6 }7 vm, err := kvm.CreateVM()8 if err != nil {9 fmt.Println(err)10 }11 program := []byte{0xb8, 0x2a, 0x00, 0x00, 0x00, 0xc3}12 err = vm.Run(program)13 if err != nil {14 fmt.Println(err)15 }16 result, err := vm.GetResult()17 if err != nil {18 fmt.Println(err)19 }20 fmt.Println(result)21}

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 machine := kvm.NewMachine()4 machine.AddCPU(kvm.CPU{})5 machine.AddMemory(1024)6 err := machine.Run()7 if err != nil {8 fmt.Println(err)9 }10}

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 kvm := kvm.NewKvm()4 kvm.SetImage("ubuntu.qcow2")5 kvm.SetMemory(1024)6 kvm.SetCpus(2)7 kvm.SetKernel("bzImage")8 kvm.SetInitrd("initrd.img")9 kvm.SetCmdline("console=ttyS0")10 kvm.SetTap("tap0")11 kvm.SetBridge("br0")12 kvm.SetNetdev("net0")13 kvm.SetSerial("ttyS0")14 kvm.SetVirtio("virtio0")15 kvm.SetVirtioBlock("virtio1")16 kvm.SetVirtioBlockImage("ubuntu.virtio.qcow2")17 kvm.SetVirtioNet("virtio2")18 kvm.SetVirtioNetImage("ubuntu.virtio.qcow2")19 kvm.SetVirtioBlock("virtio3")20 kvm.SetVirtioBlockImage("ubuntu.virtio.qcow2")21 kvm.SetVirtioBlock("virtio4")22 kvm.SetVirtioBlockImage("ubuntu.virtio.qcow2")23 kvm.SetVirtioBlock("virtio5")

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.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful