Best Syzkaller code snippet using adb.repair
adb.go
Source:adb.go
...40 BatteryCheck bool `json:"battery_check"`41 // If this option is set (default), the device is rebooted after each crash.42 // Set it to false to disable reboots.43 TargetReboot bool `json:"target_reboot"`44 RepairScript string `json:"repair_script"` // script to execute before each startup45 StartupScript string `json:"startup_script"` // script to execute after each startup46}47type Pool struct {48 env *vmimpl.Env49 cfg *Config50}51type instance struct {52 cfg *Config53 adbBin string54 device string55 console string56 closed chan bool57 debug bool58}59var (60 androidSerial = "^[0-9A-Z]+$"61 ipAddress = `^(?:localhost|(?:[0-9]{1,3}\.){3}[0-9]{1,3})\:(?:[0-9]{1,5})$` // cuttlefish or remote_device_proxy62)63func loadDevice(data []byte) (*Device, error) {64 devObj := &Device{}65 var devStr string66 err1 := config.LoadData(data, devObj)67 err2 := config.LoadData(data, &devStr)68 if err1 != nil && err2 != nil {69 return nil, fmt.Errorf("failed to parse adb vm config: %v %v", err1, err2)70 }71 if err2 == nil {72 devObj.Serial = devStr73 }74 return devObj, nil75}76func ctor(env *vmimpl.Env) (vmimpl.Pool, error) {77 cfg := &Config{78 Adb: "adb",79 BatteryCheck: true,80 TargetReboot: true,81 }82 if err := config.LoadData(env.Config, cfg); err != nil {83 return nil, fmt.Errorf("failed to parse adb vm config: %v", err)84 }85 if _, err := exec.LookPath(cfg.Adb); err != nil {86 return nil, err87 }88 if len(cfg.Devices) == 0 {89 return nil, fmt.Errorf("no adb devices specified")90 }91 // Device should be either regular serial number, or a valid Cuttlefish ID.92 devRe := regexp.MustCompile(fmt.Sprintf("%s|%s", androidSerial, ipAddress))93 for _, dev := range cfg.Devices {94 device, err := loadDevice(dev)95 if err != nil {96 return nil, err97 }98 if !devRe.MatchString(device.Serial) {99 return nil, fmt.Errorf("invalid adb device id '%v'", device.Serial)100 }101 }102 if env.Debug {103 cfg.Devices = cfg.Devices[:1]104 }105 pool := &Pool{106 cfg: cfg,107 env: env,108 }109 return pool, nil110}111func (pool *Pool) Count() int {112 return len(pool.cfg.Devices)113}114func (pool *Pool) Create(workdir string, index int) (vmimpl.Instance, error) {115 device, err := loadDevice(pool.cfg.Devices[index])116 if err != nil {117 return nil, err118 }119 inst := &instance{120 cfg: pool.cfg,121 adbBin: pool.cfg.Adb,122 device: device.Serial,123 console: device.Console,124 closed: make(chan bool),125 debug: pool.env.Debug,126 }127 closeInst := inst128 defer func() {129 if closeInst != nil {130 closeInst.Close()131 }132 }()133 if err := inst.repair(); err != nil {134 return nil, err135 }136 if inst.console == "" {137 inst.console = findConsole(inst.adbBin, inst.device)138 }139 log.Logf(0, "associating adb device %v with console %v", inst.device, inst.console)140 if pool.cfg.BatteryCheck {141 if err := inst.checkBatteryLevel(); err != nil {142 return nil, err143 }144 }145 // Remove temp files from previous runs.146 // rm chokes on bad symlinks so we must remove them first147 if _, err := inst.adb("shell", "ls /data/syzkaller*"); err == nil {148 if _, err := inst.adb("shell", "find /data/syzkaller* -type l -exec unlink {} \\;"+149 " && rm -Rf /data/syzkaller*"); err != nil {150 return nil, err151 }152 }153 inst.adb("shell", "echo 0 > /proc/sys/kernel/kptr_restrict")154 closeInst = nil155 return inst, nil156}157var (158 consoleCacheMu sync.Mutex159 consoleToDev = make(map[string]string)160 devToConsole = make(map[string]string)161)162// findConsole returns console file associated with the dev device (e.g. /dev/ttyUSB0).163// This code was tested with Suzy-Q and Android Serial Cable (ASC). For Suzy-Q see:164// https://chromium.googlesource.com/chromiumos/platform/ec/+/master/docs/case_closed_debugging.md165// The difference between Suzy-Q and ASC is that ASC is a separate cable,166// so it is not possible to match USB bus/port used by adb with the serial console device;167// while Suzy-Q console uses the same USB calbe as adb.168// The overall idea is as follows. We use 'adb shell' to write a unique string onto console,169// then we read from all console devices and see on what console the unique string appears.170func findConsole(adb, dev string) string {171 consoleCacheMu.Lock()172 defer consoleCacheMu.Unlock()173 if con := devToConsole[dev]; con != "" {174 return con175 }176 con, err := findConsoleImpl(adb, dev)177 if err != nil {178 log.Logf(0, "failed to associate adb device %v with console: %v", dev, err)179 log.Logf(0, "falling back to 'adb shell dmesg -w'")180 log.Logf(0, "note: some bugs may be detected as 'lost connection to test machine' with no kernel output")181 con = "adb"182 devToConsole[dev] = con183 return con184 }185 devToConsole[dev] = con186 consoleToDev[con] = dev187 return con188}189func findConsoleImpl(adb, dev string) (string, error) {190 // Attempt to find an exact match, at /dev/ttyUSB.{SERIAL}191 // This is something that can be set up on Linux via 'udev' rules192 exactCon := "/dev/ttyUSB." + dev193 if osutil.IsExist(exactCon) {194 return exactCon, nil195 }196 // Search all consoles, as described in 'findConsole'197 consoles, err := filepath.Glob("/dev/ttyUSB*")198 if err != nil {199 return "", fmt.Errorf("failed to list /dev/ttyUSB devices: %v", err)200 }201 output := make(map[string]*[]byte)202 errors := make(chan error, len(consoles))203 done := make(chan bool)204 for _, con := range consoles {205 if consoleToDev[con] != "" {206 continue207 }208 out := new([]byte)209 output[con] = out210 go func(con string) {211 tty, err := vmimpl.OpenConsole(con)212 if err != nil {213 errors <- err214 return215 }216 defer tty.Close()217 go func() {218 <-done219 tty.Close()220 }()221 *out, _ = ioutil.ReadAll(tty)222 errors <- nil223 }(con)224 }225 if len(output) == 0 {226 return "", fmt.Errorf("no unassociated console devices left")227 }228 time.Sleep(500 * time.Millisecond)229 unique := fmt.Sprintf(">>>%v<<<", dev)230 cmd := osutil.Command(adb, "-s", dev, "shell", "echo", "\"<1>", unique, "\"", ">", "/dev/kmsg")231 if out, err := cmd.CombinedOutput(); err != nil {232 return "", fmt.Errorf("failed to run adb shell: %v\n%s", err, out)233 }234 time.Sleep(500 * time.Millisecond)235 close(done)236 var anyErr error237 for range output {238 err := <-errors239 if anyErr == nil && err != nil {240 anyErr = err241 }242 }243 con := ""244 for con1, out := range output {245 if bytes.Contains(*out, []byte(unique)) {246 if con == "" {247 con = con1248 } else {249 anyErr = fmt.Errorf("device is associated with several consoles: %v and %v", con, con1)250 }251 }252 }253 if con == "" {254 if anyErr != nil {255 return "", anyErr256 }257 return "", fmt.Errorf("no console is associated with this device")258 }259 return con, nil260}261func (inst *instance) Forward(port int) (string, error) {262 var err error263 for i := 0; i < 1000; i++ {264 devicePort := vmimpl.RandomPort()265 _, err = inst.adb("reverse", fmt.Sprintf("tcp:%v", devicePort), fmt.Sprintf("tcp:%v", port))266 if err == nil {267 return fmt.Sprintf("127.0.0.1:%v", devicePort), nil268 }269 }270 return "", err271}272func (inst *instance) adb(args ...string) ([]byte, error) {273 if inst.debug {274 log.Logf(0, "executing adb %+v", args)275 }276 args = append([]string{"-s", inst.device}, args...)277 out, err := osutil.RunCmd(time.Minute, "", inst.adbBin, args...)278 if inst.debug {279 log.Logf(0, "adb returned")280 }281 return out, err282}283func (inst *instance) repair() error {284 // Assume that the device is in a bad state initially and reboot it.285 // Ignore errors, maybe we will manage to reboot it anyway.286 if inst.cfg.RepairScript != "" {287 if err := inst.runScript(inst.cfg.RepairScript); err != nil {288 return err289 }290 }291 inst.waitForSSH()292 // History: adb reboot episodically hangs, so we used a more reliable way:293 // using syz-executor to issue reboot syscall. However, this has stopped294 // working, probably due to the introduction of seccomp. Therefore,295 // we revert this to `adb shell reboot` in the meantime, until a more296 // reliable solution can be sought out.297 if inst.cfg.TargetReboot {298 if _, err := inst.adb("shell", "reboot"); err != nil {299 return err300 }301 // Now give it another 5 minutes to boot.302 if !vmimpl.SleepInterruptible(10 * time.Second) {303 return fmt.Errorf("shutdown in progress")304 }305 if err := inst.waitForSSH(); err != nil {306 return err307 }308 }309 // Switch to root for userdebug builds.310 inst.adb("root")311 inst.waitForSSH()312 // Mount debugfs.313 if _, err := inst.adb("shell", "ls /sys/kernel/debug/kcov"); err != nil {314 log.Logf(2, "debugfs was unmounted mounting")315 // This prop only exist on Android 12+316 inst.adb("shell", "setprop persist.dbg.keep_debugfs_mounted 1")317 if _, err := inst.adb("shell", "mount -t debugfs debugfs /sys/kernel/debug "+318 "&& chmod 0755 /sys/kernel/debug"); err != nil {319 return err320 }321 }322 if inst.cfg.StartupScript != "" {323 if err := inst.runScript(inst.cfg.StartupScript); err != nil {324 return err325 }326 }327 return nil328}329func (inst *instance) runScript(script string) error {330 log.Logf(2, "adb: executing %s", script)331 // Execute the contents of the script.332 contents, err := ioutil.ReadFile(script)333 if err != nil {334 return fmt.Errorf("unable to read %s: %v", script, err)335 }336 c := string(contents)337 output, err := osutil.RunCmd(5*time.Minute, "", "sh", "-c", c)338 if err != nil {339 return fmt.Errorf("failed to execute %s: %v", script, err)340 }341 log.Logf(2, "adb: execute %s output\n%s", script, output)342 log.Logf(2, "adb: done executing %s", script)343 return nil344}345func (inst *instance) waitForSSH() error {346 var err error347 for i := 0; i < 300; i++ {348 if !vmimpl.SleepInterruptible(time.Second) {349 return fmt.Errorf("shutdown in progress")350 }351 if _, err = inst.adb("shell", "pwd"); err == nil {352 return nil353 }354 }355 return fmt.Errorf("instance is dead and unrepairable: %v", err)356}357func (inst *instance) checkBatteryLevel() error {358 const (359 minLevel = 20360 requiredLevel = 30361 )362 val, err := inst.getBatteryLevel(30)363 if err != nil {364 return err365 }366 if val >= minLevel {367 log.Logf(0, "device %v: battery level %v%%, OK", inst.device, val)368 return nil369 }...
repair
Using AI Code Generation
1import "fmt"2type mobile struct {3}4func (m mobile) repair() {5 fmt.Println("Repairing", m.brand, m.model)6}7func main() {8 m1 := mobile{"Apple", "Iphone 7", "Black", 60000}9 m1.repair()10}11import "fmt"12type mobile struct {13}14type adb struct {15}16func (m mobile) repair() {17 fmt.Println("Repairing", m.brand, m.model)18}19func main() {20 m1 := adb{mobile{"Apple", "Iphone 7", "Black", 60000}}21 m1.repair()22}23import "fmt"24type mobile interface {25 repair()26}27type adb struct {28}29func (m adb) repair() {30 fmt.Println("Repairing", m.brand, m.model)31}32func main() {33 m1 = adb{"Apple", "Iphone 7", "Black", 60000}34 m1.repair()35}
repair
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 var mobile = adb{}5 mobile.repair()6}7import (8func main() {9 fmt.Println("Hello, playground")10 var mobile = adb{}11 mobile.repair()12 var mobile2 = adb{}13 mobile2.repair()14}15import (16func main() {17 fmt.Println("Hello, playground")18 var mobile = adb{}19 mobile.repair()20 var mobile2 = adb{}21 mobile2.repair()22 var mobile3 = adb{}23 mobile3.repair()24}25import (26func main() {27 fmt.Println("Hello, playground")28 var mobile = adb{}29 mobile.repair()30 var mobile2 = adb{}31 mobile2.repair()32 var mobile3 = adb{}33 mobile3.repair()34 var mobile4 = adb{}35 mobile4.repair()36}37import (38func main() {39 fmt.Println("Hello, playground")40 var mobile = adb{}41 mobile.repair()42 var mobile2 = adb{}43 mobile2.repair()44 var mobile3 = adb{}45 mobile3.repair()46 var mobile4 = adb{}47 mobile4.repair()48 var mobile5 = adb{}49 mobile5.repair()50}51import (52func main() {53 fmt.Println("Hello, playground")54 var mobile = adb{}55 mobile.repair()56 var mobile2 = adb{}57 mobile2.repair()58 var mobile3 = adb{}59 mobile3.repair()60 var mobile4 = adb{}61 mobile4.repair()62 var mobile5 = adb{}63 mobile5.repair()64 var mobile6 = adb{}65 mobile6.repair()66}
repair
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println("Hello, World!")4 a1.Repair()5}6import "fmt"7func main() {8 fmt.Println("Hello, World!")9 a1.Repair()10}11import "fmt"12func main() {13 fmt.Println("Hello, World!")14 a1.Repair()15}16import "fmt"17func main() {18 fmt.Println("Hello, World!")19 a1.Repair()20}21import "fmt"22func main() {23 fmt.Println("Hello, World!")24 a1.Repair()25}26import "fmt"27func main() {28 fmt.Println("Hello, World!")29 a1.Repair()30}31import "fmt"32func main() {33 fmt.Println("Hello, World!")34 a1.Repair()35}36import "fmt"37func main() {38 fmt.Println("Hello, World!")39 a1.Repair()40}41import "fmt"42func main() {43 fmt.Println("Hello, World!")44 a1.Repair()45}46import "fmt"47func main() {48 fmt.Println("Hello, World!")49 a1.Repair()50}
repair
Using AI Code Generation
1import (2func main() {3 adb.Repair()4 fmt.Println("Repair done")5}6import (7func Repair() {8 fmt.Println("Repairing...")9}10I have a package called "adb" and I have a file called "adb.go" and "2.go". I want to call the Repair() method in adb.go from 2.go. I have tried to do this, but it doesn't work. How can I call the Repair() method in adb.go from 2.go?11I have a package called "adb" and I have a file called "adb.go" and "2.go". I want to call the Repair() method in adb.go from 2.go. I have tried to do this, but it doesn't work. How can I call the Repair() method in adb.go from 2.go?
repair
Using AI Code Generation
1import (2func main() {3 if devices, err = adb.GetDevices(); err != nil {4 panic(err)5 }6 if err = devices[0].Repair(); err != nil {7 panic(err)8 }9 fmt.Println("Repair successful")10}11import (12func main() {13 if devices, err = adb.GetDevices(); err != nil {14 panic(err)15 }16 if err = devices[0].Reboot(); err != nil {17 panic(err)18 }19 fmt.Println("Reboot successful")20}21import (22func main() {23 if devices, err = adb.GetDevices(); err != nil {24 panic(err)25 }26 if err = devices[0].Reverse("tcp:8080", "tcp:8080"); err != nil {27 panic(err)28 }29 fmt.Println("Reverse successful")30}31import (32func main() {33 if devices, err = adb.GetDevices(); err != nil {34 panic(err)35 }36 if err = devices[0].Root(); err != nil {37 panic(err)38 }39 fmt.Println("Root successful")40}41import (42func main() {
repair
Using AI Code Generation
1import (2func main() {3 adb := adb.New()4 adb.Repair()5}6import (7type Adb struct {8}9func New() *Adb {10 return &Adb{}11}12func (a *Adb) Repair() {13 cmd := exec.Command("adb", "kill-server")14 cmd.Run()15 cmd = exec.Command("adb", "start-server")16 cmd.Run()17}18import (19func TestRepair(t *testing.T) {20 adb := New()21 adb.Repair()22 cmd := exec.Command("adb", "devices")23 out, err := cmd.Output()24 if err != nil {25 t.Error(err)26 }27 if string(out) == "" {28 t.Error("No devices found")29 }30}31import (
repair
Using AI Code Generation
1import (2func main() {3 device := adb.NewDevice("device id")4 device.Repair()5}6import (7func main() {8 device := adb.NewDevice("device id")9 device.Reboot()10}11import (12func main() {13 device := adb.NewDevice("device id")14 device.Screenshot()15}16import (17func main() {18 device := adb.NewDevice("device id")19 device.Shell("command")20}21import (22func main() {23 device := adb.NewDevice("device id")24 device.Startapp("package name")25}26import (27func main() {28 device := adb.NewDevice("device id")29 device.Stopapp("package name")30}31import (32func main() {33 device := adb.NewDevice("device id")34 device.Swipe(0, 0, 100, 100, 1000)35}36import (37func main() {38 device := adb.NewDevice("device id")39 device.Tap(0, 0)40}
repair
Using AI Code Generation
1import (2func main() {3 a := adb.New()4 devices, err := a.Devices()5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(devices)9 err = a.Repair(devices[0])10 if err != nil {11 fmt.Println(err)12 }13}14import (15func main() {16 a := adb.New()17 devices, err := a.Devices()18 if err != nil {19 fmt.Println(err)20 }21 fmt.Println(devices)22 err = a.Fastboot(devices[0])23 if err != nil {24 fmt.Println(err)25 }26}27import (28func main() {29 a := adb.New()30 devices, err := a.Devices()31 if err != nil {32 fmt.Println(err)33 }34 fmt.Println(devices)35 err = a.Reboot(devices[0])36 if err != nil {37 fmt.Println(err)38 }39}40import (41func main() {42 a := adb.New()43 devices, err := a.Devices()44 if err != nil {45 fmt.Println(err)46 }47 fmt.Println(devices)
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!