How to use init method of gvisor Package

Best Syzkaller code snippet using gvisor.init

runk.go

Source:runk.go Github

copy

Full Screen

...59func Run(o Opt) error {60 log.SetLevel(log.Warning)61 // Register the global syscall table.62 kernel.RegisterSyscallTable(slinux.AMD64)63 // We initialize the rand package now to make sure /dev/urandom is pre-opened64 // on kernels that do not support getrandom(2).65 if err := rand.Init(); err != nil {66 return errors.Wrap(err, "setting up rand")67 }68 if err := usage.Init(); err != nil {69 return errors.Wrap(err, "error setting up memory usage")70 }71 p, err := newPlatform(o.GVisor.Platform)72 if err != nil {73 return err74 }75 k := &kernel.Kernel{76 Platform: p,77 }78 // Create memory file.79 mf, err := createMemoryFile()80 if err != nil {81 return errors.Wrap(err, "creating memory file")82 }83 k.SetMemoryFile(mf)84 vdso, err := loader.PrepareVDSO(nil, k)85 if err != nil {86 return errors.Wrap(err, "error creating vdso")87 }88 tk, err := kernel.NewTimekeeper(k, vdso.ParamPage.FileRange())89 if err != nil {90 return errors.Wrap(err, "error creating timekeeper")91 }92 tk.SetClocks(time.NewCalibratedClocks())93 networkStack, err := netStack(k, k, o.Network)94 if err != nil {95 return err96 }97 stack, ok := networkStack.(*hostinet.Stack)98 if ok {99 if err := stack.Configure(); err != nil {100 return err101 }102 }103 creds := auth.NewUserCredentials(104 auth.KUID(0),105 auth.KGID(0),106 nil,107 nil,108 auth.NewRootUserNamespace())109 if err = k.Init(kernel.InitKernelArgs{110 FeatureSet: cpuid.HostFeatureSet(),111 Timekeeper: tk,112 RootUserNamespace: creds.UserNamespace,113 NetworkStack: networkStack,114 ApplicationCores: uint(runtime.NumCPU()),115 Vdso: vdso,116 RootUTSNamespace: kernel.NewUTSNamespace("sbox", "sbox", creds.UserNamespace),117 RootIPCNamespace: kernel.NewIPCNamespace(creds.UserNamespace),118 RootAbstractSocketNamespace: kernel.NewAbstractSocketNamespace(),119 PIDNamespace: kernel.NewRootPIDNamespace(creds.UserNamespace),120 }); err != nil {121 return errors.Wrap(err, "error initializing kernel")122 }123 ls, err := limits.NewLinuxLimitSet()124 if err != nil {125 return err126 }127 // Create the process arguments.128 procArgs := kernel.CreateProcessArgs{129 Argv: o.Process.Args,130 Envv: []string{},131 WorkingDirectory: "/", // Defaults to '/' if empty.132 Credentials: creds,133 Umask: 0022,134 Limits: ls,135 MaxSymlinkTraversals: linux.MaxSymlinkTraversals,136 UTSNamespace: k.RootUTSNamespace(),137 IPCNamespace: k.RootIPCNamespace(),138 AbstractSocketNamespace: k.RootAbstractSocketNamespace(),139 ContainerID: "sbox",140 PIDNamespace: k.RootPIDNamespace(),141 }142 ctx := procArgs.NewContext(k)143 fdt, err := createFDTable(ctx, k, ls, o.Process.TTY, []int{0, 1, 2})144 if err != nil {145 return errors.Wrap(err, "error importing fds")146 }147 // CreateProcess takes a reference on fdTable if successful. We148 // won't need ours either way.149 procArgs.FDTable = fdt150 rootProcArgs := procArgs151 rootProcArgs.WorkingDirectory = "/"152 rootProcArgs.Credentials = auth.NewRootCredentials(creds.UserNamespace)153 rootProcArgs.Umask = 0022154 rootProcArgs.MaxSymlinkTraversals = linux.MaxSymlinkTraversals155 rootCtx := rootProcArgs.NewContext(k)156 followLinks := uint(linux.MaxSymlinkTraversals)157 mns, err := createMountNamespace(ctx, rootCtx, o.Mounts, &followLinks)158 if err != nil {159 return errors.Wrap(err, "error creating mounts")160 }161 rootProcArgs.MountNamespace = mns162 _, _, err = k.CreateProcess(rootProcArgs)163 if err != nil {164 return errors.Wrap(err, "failed to create init process")165 }166 tg := k.GlobalInit()167 if o.Process.TTY {168 ttyFile, _ := procArgs.FDTable.Get(0)169 defer ttyFile.DecRef()170 ttyfop := ttyFile.FileOperations.(*host.TTYFileOperations)171 // Set the foreground process group on the TTY to the global172 // init process group, since that is what we are about to173 // start running.174 ttyfop.InitForegroundProcessGroup(tg.ProcessGroup())175 }176 if err := k.Start(); err != nil {177 return err178 }179 k.WaitExited()180 return nil181}182func addSubmountOverlay(ctx context.Context, inode *fs.Inode, submounts []string) (*fs.Inode, error) {183 // There is no real filesystem backing this ramfs tree, so we pass in184 // "nil" here.185 msrc := fs.NewNonCachingMountSource(ctx, nil, fs.MountSourceFlags{})186 mountTree, err := ramfs.MakeDirectoryTree(ctx, msrc, submounts)...

Full Screen

Full Screen

gvisor.go

Source:gvisor.go Github

copy

Full Screen

...26}27func (s *Stack) Start(device Device, handler Handler) (err error) {28 s.Device = device29 s.Handler = handler30 // init netstack by gvisor.dev31 s.Stack = stack.New(stack.Options{32 NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},33 TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol, icmp.NewProtocol4, icmp.NewProtocol6},34 })35 defer func(s *stack.Stack) {36 if err != nil {37 s.Close()38 }39 }(s.Stack)40 // set NICID to 141 const NICID = tcpip.NICID(1)42 // WithDefaultTTL sets the default TTL used by stack.43 {44 opt := tcpip.DefaultTTLOption(64)45 if tcperr := s.Stack.SetNetworkProtocolOption(ipv4.ProtocolNumber, &opt); tcperr != nil {46 err = fmt.Errorf("set ipv4 default TTL: %s", tcperr)47 return48 }49 if tcperr := s.Stack.SetNetworkProtocolOption(ipv6.ProtocolNumber, &opt); tcperr != nil {50 err = fmt.Errorf("set ipv6 default TTL: %s", tcperr)51 return52 }53 }54 // set forwarding55 if tcperr := s.Stack.SetForwarding(ipv4.ProtocolNumber, true); tcperr != nil {56 err = fmt.Errorf("set ipv4 forwarding error: %s", tcperr)57 return58 }59 if tcperr := s.Stack.SetForwarding(ipv6.ProtocolNumber, true); tcperr != nil {60 err = fmt.Errorf("set ipv6 forwarding error: %s", tcperr)61 return62 }63 // WithICMPBurst sets the number of ICMP messages that can be sent64 // in a single burst.65 s.Stack.SetICMPBurst(50)66 // WithICMPLimit sets the maximum number of ICMP messages permitted67 // by rate limiter.68 s.Stack.SetICMPLimit(rate.Limit(1000))69 // WithTCPBufferSizeRange sets the receive and send buffer size range for TCP.70 {71 rcvOpt := tcpip.TCPReceiveBufferSizeRangeOption{Min: 4 << 10, Default: 212 << 10, Max: 4 << 20}72 if tcperr := s.Stack.SetTransportProtocolOption(tcp.ProtocolNumber, &rcvOpt); tcperr != nil {73 err = fmt.Errorf("set TCP receive buffer size range: %s", tcperr)74 return75 }76 sndOpt := tcpip.TCPSendBufferSizeRangeOption{Min: 4 << 10, Default: 212 << 10, Max: 4 << 20}77 if tcperr := s.Stack.SetTransportProtocolOption(tcp.ProtocolNumber, &sndOpt); tcperr != nil {78 err = fmt.Errorf("set TCP send buffer size range: %s", tcperr)79 return80 }81 }82 // WithTCPCongestionControl sets the current congestion control algorithm.83 {84 opt := tcpip.CongestionControlOption("reno")85 if tcperr := s.Stack.SetTransportProtocolOption(tcp.ProtocolNumber, &opt); tcperr != nil {86 err = fmt.Errorf("set TCP congestion control algorithm: %s", tcperr)87 return88 }89 }90 // WithTCPModerateReceiveBuffer sets receive buffer moderation for TCP.91 {92 opt := tcpip.TCPDelayEnabled(false)93 if tcperr := s.Stack.SetTransportProtocolOption(tcp.ProtocolNumber, &opt); tcperr != nil {94 err = fmt.Errorf("set TCP delay: %s", err)95 return96 }97 }98 // WithTCPModerateReceiveBuffer sets receive buffer moderation for TCP.99 {100 opt := tcpip.TCPModerateReceiveBufferOption(true)101 if tcperr := s.Stack.SetTransportProtocolOption(tcp.ProtocolNumber, &opt); tcperr != nil {102 err = fmt.Errorf("set TCP moderate receive buffer: %s", tcperr)103 return104 }105 }106 // WithTCPSACKEnabled sets the SACK option for TCP.107 {108 opt := tcpip.TCPSACKEnabled(true)109 if tcperr := s.Stack.SetTransportProtocolOption(tcp.ProtocolNumber, &opt); tcperr != nil {110 err = fmt.Errorf("set TCP SACK: %s", tcperr)111 return112 }113 }114 mustSubnet := func(s string) tcpip.Subnet {115 _, ipnet, err := net.ParseCIDR(s)116 if err != nil {117 panic(fmt.Errorf("unable to ParseCIDR(%s): %w", s, err))118 }119 subnet, err := tcpip.NewSubnet(tcpip.Address(ipnet.IP), tcpip.AddressMask(ipnet.Mask))120 if err != nil {121 panic(fmt.Errorf("unable to NewSubnet(%s): %w", ipnet, err))122 }123 return subnet124 }125 // Add default route table for IPv4 and IPv6126 // This will handle all incoming ICMP packets.127 s.Stack.SetRouteTable([]tcpip.Route{128 {129 // Destination: header.IPv4EmptySubnet,130 Destination: mustSubnet("0.0.0.0/0"),131 NIC: NICID,132 },133 {134 // Destination: header.IPv6EmptySubnet,135 Destination: mustSubnet("::/0"),136 NIC: NICID,137 },138 })139 // Important: We must initiate transport protocol handlers140 // before creating NIC, otherwise NIC would dispatch packets141 // to stack and cause race condition.142 s.Stack.SetTransportProtocolHandler(tcp.ProtocolNumber, tcp.NewForwarder(s.Stack, 16<<10, 1<<15, s.HandleStream).HandlePacket)143 s.Stack.SetTransportProtocolHandler(udp.ProtocolNumber, s.HandlePacket)144 // WithCreatingNIC creates NIC for stack.145 if tcperr := s.Stack.CreateNIC(NICID, CreateEndpoint(device)); tcperr != nil {146 err = fmt.Errorf("fail to create NIC in stack: %s", tcperr)147 return148 }149 // WithPromiscuousMode sets promiscuous mode in the given NIC.150 // In past we did s.AddAddressRange to assign 0.0.0.0/0 onto151 // the interface. We need that to be able to terminate all the152 // incoming connections - to any ip. AddressRange API has been153 // removed and the suggested workaround is to use Promiscuous...

Full Screen

Full Screen

net.go

Source:net.go Github

copy

Full Screen

...149 }150 err = iface.nic.Init(iface.device, 0)151 return152}153// Init initializes an Ethernet over USB device, configured with the defaults154// as set by ConfigureDevice().155func Init(deviceIP string, deviceMAC, hostMAC string, id int) (iface *Interface, err error) {156 device := &usb.Device{}157 ConfigureDevice(device)158 return Add(device, deviceIP, deviceMAC, hostMAC, id)159}...

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func init() {3 fmt.Println("Init method of gvisor class")4}5func main() {6 fmt.Println("Main method of gvisor class")7}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p, err := boot.NewPlatform()4 if err != nil {5 panic(err)6 }7 i, err := boot.NewInstance(p, "test", nil)8 if err != nil {9 panic(err)10 }11 if err := i.Start(); err != nil {12 panic(err)13 }14 if err := i.Wait(); err != nil {15 panic(err)16 }17}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1func main() {2 gvisor := gvisor{}3 gvisor.init()4 fmt.Println(gvisor.name)5}6func main() {7 gvisor := gvisor{}8 gvisor.init()9 fmt.Println(gvisor.name)10}11func main() {12 gvisor := gvisor{}13 gvisor.init()14 fmt.Println(gvisor.name)15}16func main() {17 gvisor := gvisor{}18 gvisor.init()19 fmt.Println(gvisor.name)20}21func main() {22 gvisor := gvisor{}23 gvisor.init()24 fmt.Println(gvisor.name)25}26func main() {27 gvisor := gvisor{}28 gvisor.init()29 fmt.Println(gvisor.name)30}31func main() {32 gvisor := gvisor{}33 gvisor.init()34 fmt.Println(gvisor.name)35}36func main() {37 gvisor := gvisor{}38 gvisor.init()39 fmt.Println(gvisor.name)40}41func main() {42 gvisor := gvisor{}43 gvisor.init()44 fmt.Println(gvisor.name)45}46func main() {47 gvisor := gvisor{}48 gvisor.init()49 fmt.Println(gvisor.name)50}51func main() {52 gvisor := gvisor{}53 gvisor.init()54 fmt.Println(gvisor.name)55}56func main() {57 gvisor := gvisor{}58 gvisor.init()59 fmt.Println(gvisor.name)60}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4}5import (6func main() {7 fmt.Println("Hello, playground")8 platform.Init()9}10import (11func main() {12 fmt.Println("Hello, playground")13}

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