How to use Read method of vmimpl Package

Best Syzkaller code snippet using vmimpl.Read

isolated.go

Source:isolated.go Github

copy

Full Screen

...164 }165 }()166 if err := cmd.Wait(); err != nil {167 close(done)168 out, _ := ioutil.ReadAll(rpipe)169 if inst.debug {170 log.Logf(0, "ssh failed: %v\n%s", err, out)171 }172 return fmt.Errorf("ssh %+v failed: %v\n%s", args, err, out)173 }174 close(done)175 if inst.debug {176 log.Logf(0, "ssh returned")177 }178 return nil179}180func (inst *instance) waitRebootAndSSH(rebootTimeout int, sshTimeout time.Duration) error {181 if err := inst.waitForReboot(rebootTimeout); err != nil {182 log.Logf(2, "isolated: machine did not reboot")183 return err184 }185 log.Logf(2, "isolated: rebooted wait for comeback")186 if err := inst.waitForSSH(sshTimeout); err != nil {187 log.Logf(2, "isolated: machine did not comeback")188 return err189 }190 log.Logf(2, "isolated: reboot succeeded")191 return nil192}193// Escapes double quotes(and nested double quote escapes). Ignores any other escapes.194// Reference: https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html195func escapeDoubleQuotes(inp string) string {196 var ret strings.Builder197 for pos := 0; pos < len(inp); pos++ {198 // If inp[pos] is not a double quote or a backslash, just use199 // as is.200 if inp[pos] != '"' && inp[pos] != '\\' {201 ret.WriteByte(inp[pos])202 continue203 }204 // If it is a double quote, escape.205 if inp[pos] == '"' {206 ret.WriteString("\\\"")207 continue208 }209 // If we detect a backslash, reescape only if what it's already escaping210 // is a double-quotes.211 temp := ""212 j := pos213 for ; j < len(inp); j++ {214 if inp[j] == '\\' {215 temp += string(inp[j])216 continue217 }218 // If the escape corresponds to a double quotes, re-escape.219 // Else, just use as is.220 if inp[j] == '"' {221 temp = temp + temp + "\\\""222 } else {223 temp += string(inp[j])224 }225 break226 }227 ret.WriteString(temp)228 pos = j229 }230 return ret.String()231}232func (inst *instance) repair() error {233 log.Logf(2, "isolated: trying to ssh")234 if err := inst.waitForSSH(30 * time.Minute); err != nil {235 log.Logf(2, "isolated: ssh failed")236 return fmt.Errorf("SSH failed")237 }238 if inst.cfg.TargetReboot {239 if len(inst.cfg.USBDevNums) > 0 {240 log.Logf(2, "isolated: trying to reboot by USB authorization")241 usbAuth := fmt.Sprintf("%s%s%s", "/sys/bus/usb/devices/", inst.cfg.USBDevNums[inst.index], "/authorized")242 if err := ioutil.WriteFile(usbAuth, []byte("0"), 0); err != nil {243 log.Logf(2, "isolated: failed to turn off the device")244 return err245 }246 if err := ioutil.WriteFile(usbAuth, []byte("1"), 0); err != nil {247 log.Logf(2, "isolated: failed to turn on the device")248 return err249 }250 } else {251 log.Logf(2, "isolated: ssh succeeded, trying to reboot by ssh")252 inst.ssh("reboot") // reboot will return an error, ignore it253 if err := inst.waitRebootAndSSH(5*60, 30*time.Minute); err != nil {254 return fmt.Errorf("waitRebootAndSSH failed: %v", err)255 }256 }257 }258 if inst.cfg.StartupScript != "" {259 log.Logf(2, "isolated: executing startup_script")260 // Execute the contents of the StartupScript on the DUT.261 contents, err := ioutil.ReadFile(inst.cfg.StartupScript)262 if err != nil {263 return fmt.Errorf("unable to read startup_script: %v", err)264 }265 c := string(contents)266 if err := inst.ssh(fmt.Sprintf("bash -c \"%v\"", escapeDoubleQuotes(c))); err != nil {267 return fmt.Errorf("failed to execute startup_script: %v", err)268 }269 log.Logf(2, "isolated: done executing startup_script")270 }271 return nil272}273func (inst *instance) waitForSSH(timeout time.Duration) error {274 return vmimpl.WaitForSSH(inst.debug, timeout, inst.targetAddr, inst.sshKey, inst.sshUser,275 inst.os, inst.targetPort, nil)...

Full Screen

Full Screen

adb.go

Source:adb.go Github

copy

Full Screen

...160 go func() {161 <-done162 tty.Close()163 }()164 *out, _ = ioutil.ReadAll(tty)165 errors <- nil166 }(con)167 }168 if len(output) == 0 {169 return "", fmt.Errorf("no unassociated console devices left")170 }171 time.Sleep(500 * time.Millisecond)172 unique := fmt.Sprintf(">>>%v<<<", dev)173 cmd := osutil.Command(adb, "-s", dev, "shell", "echo", "\"", unique, "\"", ">", "/dev/kmsg")174 if out, err := cmd.CombinedOutput(); err != nil {175 return "", fmt.Errorf("failed to run adb shell: %v\n%s", err, out)176 }177 time.Sleep(500 * time.Millisecond)178 close(done)179 var anyErr error180 for range output {181 err := <-errors182 if anyErr == nil && err != nil {183 anyErr = err184 }185 }186 con := ""187 for con1, out := range output {188 if bytes.Contains(*out, []byte(unique)) {189 if con == "" {190 con = con1191 } else {192 anyErr = fmt.Errorf("device is associated with several consoles: %v and %v", con, con1)193 }194 }195 }196 if con == "" {197 if anyErr != nil {198 return "", anyErr199 }200 return "", fmt.Errorf("no console is associated with this device")201 }202 return con, nil203}204func (inst *instance) Forward(port int) (string, error) {205 // If 35099 turns out to be busy, try to forward random ports several times.206 devicePort := 35099207 if _, err := inst.adb("reverse", fmt.Sprintf("tcp:%v", devicePort), fmt.Sprintf("tcp:%v", port)); err != nil {208 return "", err209 }210 return fmt.Sprintf("127.0.0.1:%v", devicePort), nil211}212func (inst *instance) adb(args ...string) ([]byte, error) {213 if inst.debug {214 Logf(0, "executing adb %+v", args)215 }216 rpipe, wpipe, err := os.Pipe()217 if err != nil {218 return nil, fmt.Errorf("failed to create pipe: %v", err)219 }220 defer wpipe.Close()221 defer rpipe.Close()222 cmd := osutil.Command(inst.adbBin, append([]string{"-s", inst.device}, args...)...)223 cmd.Stdout = wpipe224 cmd.Stderr = wpipe225 if err := cmd.Start(); err != nil {226 return nil, err227 }228 wpipe.Close()229 done := make(chan bool)230 go func() {231 select {232 case <-time.After(time.Minute):233 if inst.debug {234 Logf(0, "adb hanged")235 }236 cmd.Process.Kill()237 case <-done:238 }239 }()240 if err := cmd.Wait(); err != nil {241 close(done)242 out, _ := ioutil.ReadAll(rpipe)243 if inst.debug {244 Logf(0, "adb failed: %v\n%s", err, out)245 }246 return nil, fmt.Errorf("adb %+v failed: %v\n%s", args, err, out)247 }248 close(done)249 if inst.debug {250 Logf(0, "adb returned")251 }252 out, _ := ioutil.ReadAll(rpipe)253 return out, nil254}255func (inst *instance) repair() error {256 // Assume that the device is in a bad state initially and reboot it.257 // Ignore errors, maybe we will manage to reboot it anyway.258 inst.waitForSsh()259 // History: adb reboot episodically hangs, so we used a more reliable way:260 // using syz-executor to issue reboot syscall. However, this has stopped261 // working, probably due to the introduction of seccomp. Therefore,262 // we revert this to `adb shell reboot` in the meantime, until a more263 // reliable solution can be sought out.264 if _, err := inst.adb("shell", "reboot"); err != nil {265 return err266 }267 // Now give it another 5 minutes to boot.268 if !vmimpl.SleepInterruptible(10 * time.Second) {269 return fmt.Errorf("shutdown in progress")270 }271 if err := inst.waitForSsh(); err != nil {272 return err273 }274 // Switch to root for userdebug builds.275 inst.adb("root")276 if err := inst.waitForSsh(); err != nil {277 return err278 }279 return nil280}281func (inst *instance) waitForSsh() error {282 var err error283 for i := 0; i < 300; i++ {284 if !vmimpl.SleepInterruptible(time.Second) {285 return fmt.Errorf("shutdown in progress")286 }287 if _, err = inst.adb("shell", "pwd"); err == nil {288 return nil289 }290 }291 return fmt.Errorf("instance is dead and unrepairable: %v", err)292}293func (inst *instance) checkBatteryLevel() error {294 const (295 minLevel = 20296 requiredLevel = 30297 )298 val, err := inst.getBatteryLevel(30)299 if err != nil {300 return err301 }302 if val >= minLevel {303 Logf(0, "device %v: battery level %v%%, OK", inst.device, val)304 return nil305 }306 for {307 Logf(0, "device %v: battery level %v%%, waiting for %v%%", inst.device, val, requiredLevel)308 if !vmimpl.SleepInterruptible(time.Minute) {309 return nil310 }311 val, err = inst.getBatteryLevel(0)312 if err != nil {313 return err314 }315 if val >= requiredLevel {316 break317 }318 }319 return nil320}321func (inst *instance) getBatteryLevel(numRetry int) (int, error) {322 out, err := inst.adb("shell", "dumpsys battery | grep level:")323 // allow for retrying for devices that does not boot up so fast324 for ; numRetry >= 0 && err != nil; numRetry-- {325 if numRetry > 0 {326 // sleep for 5 seconds before retrying327 time.Sleep(5 * time.Second)328 out, err = inst.adb("shell", "dumpsys battery | grep level:")329 } else {330 if err != nil {331 return 0, err332 }333 }334 }335 val := 0336 for _, c := range out {337 if c >= '0' && c <= '9' {338 val = val*10 + int(c) - '0'339 continue340 }341 if val != 0 {342 break343 }344 }345 if val == 0 {346 return 0, fmt.Errorf("failed to parse 'dumpsys battery' output: %s", out)347 }348 return val, nil349}350func (inst *instance) Close() {351 close(inst.closed)352}353func (inst *instance) Copy(hostSrc string) (string, error) {354 vmDst := filepath.Join("/data", filepath.Base(hostSrc))355 if _, err := inst.adb("push", hostSrc, vmDst); err != nil {356 return "", err357 }358 return vmDst, nil359}360func (inst *instance) Run(timeout time.Duration, stop <-chan bool, command string) (<-chan []byte, <-chan error, error) {361 var tty io.ReadCloser362 var err error363 if inst.console == "adb" {364 tty, err = vmimpl.OpenAdbConsole(inst.adbBin, inst.device)365 } else {366 tty, err = vmimpl.OpenConsole(inst.console)367 }368 if err != nil {369 return nil, nil, err370 }371 adbRpipe, adbWpipe, err := osutil.LongPipe()372 if err != nil {373 tty.Close()374 return nil, nil, err375 }...

Full Screen

Full Screen

gvisor.go

Source:gvisor.go Github

copy

Full Screen

...103 if err != nil {104 return nil, err105 }106 defer panicLogWriteFD.Close()107 panicLogReadFD, err := os.Open(panicLog)108 if err != nil {109 return nil, err110 }111 rpipe, wpipe, err := osutil.LongPipe()112 if err != nil {113 panicLogReadFD.Close()114 return nil, err115 }116 var tee io.Writer117 if pool.env.Debug {118 tee = os.Stdout119 }120 merger := vmimpl.NewOutputMerger(tee)121 merger.Add("gvisor", rpipe)122 merger.Add("gvisor-goruntime", panicLogReadFD)123 inst := &instance{124 cfg: pool.cfg,125 image: pool.env.Image,126 debug: pool.env.Debug,127 rootDir: rootDir,128 imageDir: imageDir,129 name: fmt.Sprintf("%v-%v", pool.env.Name, index),130 merger: merger,131 }132 // Kill the previous instance in case it's still running.133 osutil.Run(time.Minute, inst.runscCmd("delete", "-force", inst.name))134 time.Sleep(3 * time.Second)135 cmd := inst.runscCmd("--panic-log", panicLog, "run", "-bundle", bundleDir, inst.name)136 cmd.Stdout = wpipe...

Full Screen

Full Screen

Read

Using AI Code Generation

copy

Full Screen

1func main() {2 vmi.Read()3}4func main() {5 vmi = &vmimpl{}6 vmi.Read()7}8func main() {9 vmi = new(vmimpl)10 vmi.Read()11}12func main() {13 vmi = &vmimpl{"vm1", "running"}14 vmi.Read()15}16func main() {17 vmi = new(vmimpl)18 vmi.Read()19 vmi = &vmimpl{"vm1", "running"}20 vmi.Read()21}22func main() {23 vmi = new(vmimpl)24 vmi.Read()25 vmi = &vmimpl{"vm1", "running"}26 vmi.Read()27 vmi = &vmimpl{"vm2", "stopped"}28 vmi.Read()29}30func main() {31 vmi = new(vmimpl)32 vmi.Read()33 vmi = &vmimpl{"vm1", "running"}34 vmi.Read()35 vmi = &vmimpl{"vm2", "stopped"}36 vmi.Read()37 vmi = &vmimpl{"vm3", "running"}38 vmi.Read()39 vmi = &vmimpl{"vm4", "stopped"}40 vmi.Read()41}42func main() {43 vmi = new(vmimpl)44 vmi.Read()45 vmi = &vmimpl{"vm1", "running"}46 vmi.Read()47 vmi = &vmimpl{"vm2

Full Screen

Full Screen

Read

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v.Read()4 fmt.Println("Hello, playground")5}6import (7func main() {8 v.Read()9 fmt.Println("Hello, playground")10}11import (12func main() {13 v.Read()14 fmt.Println("Hello, playground")15}16import (17func main() {18 v.Read()19 fmt.Println("Hello, playground")20}21import (22func main() {23 v.Read()24 fmt.Println("Hello, playground")25}26import (27func main() {28 v.Read()29 fmt.Println("Hello, playground")30}31import (32func main() {33 v.Read()34 fmt.Println("Hello, playground")35}36import (

Full Screen

Full Screen

Read

Using AI Code Generation

copy

Full Screen

1func main() {2 println(vmimpl.Read("Enter a number: ", "0"))3}4func main() {5 vmimpl.Write("Hello World!")6}7func main() {

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