aboutsummaryrefslogtreecommitdiffstats
path: root/vm/cuttlefish/cuttlefish.go
blob: 501f6079dd34f381e1ac313a71cab026a2748094 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2022 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

// Package cuttlefish allows to use Cuttlefish Android emulators hosted on Google Compute Engine
// (GCE) virtual machines as VMs. It is assumed that syz-manager also runs on GCE as VMs are
// created in the current project/zone.
//
// See https://cloud.google.com/compute/docs for details.
// In particular, how to build GCE-compatible images:
// https://cloud.google.com/compute/docs/tutorials/building-images
package cuttlefish

import (
	"fmt"
	"time"

	"github.com/google/syzkaller/pkg/log"
	"github.com/google/syzkaller/pkg/report"
	"github.com/google/syzkaller/vm/vmimpl"
)

func init() {
	vmimpl.Register("cuttlefish", ctor, true)
}

type Pool struct {
	env     *vmimpl.Env
	gcePool vmimpl.Pool
}

type instance struct {
	debug   bool
	gceInst vmimpl.Instance
}

func ctor(env *vmimpl.Env) (vmimpl.Pool, error) {
	gcePool, err := vmimpl.Types["gce"].Ctor(env)
	if err != nil {
		return nil, fmt.Errorf("failed to create underlying GCE pool: %s", err)
	}

	pool := &Pool{
		env:     env,
		gcePool: gcePool,
	}

	return pool, nil
}

func (pool *Pool) Count() int {
	return pool.gcePool.Count()
}

func (pool *Pool) Create(workdir string, index int) (vmimpl.Instance, error) {
	gceInst, err := pool.gcePool.Create(workdir, index)
	if err != nil {
		return nil, fmt.Errorf("failed to create underlying gce instance: %s", err)
	}

	inst := &instance{
		debug:   pool.env.Debug,
		gceInst: gceInst,
	}

	// Start a Cuttlefish device on the GCE instance
	// TODO: pass it the specific kernel artifact using -kernel_path and -initramfs_path flags
	if err := inst.runOnHost(10*time.Minute, "./bin/launch_cvd -daemon"); err != nil {
		return nil, fmt.Errorf("failed to start cuttlefish: %s", err)
	}

	if err := inst.runOnHost(10*time.Minute, "adb wait-for-device"); err != nil {
		return nil, fmt.Errorf("failed while waiting for device: %s", err)
	}

	if err := inst.runOnHost(5*time.Minute, "adb root"); err != nil {
		return nil, fmt.Errorf("failed to get root access to device: %s", err)
	}

	return inst, nil
}

func (inst *instance) runOnHost(timeout time.Duration, cmd string) error {
	outc, errc, err := inst.gceInst.Run(timeout, nil, cmd)
	if err != nil {
		return fmt.Errorf("failed to run command: %s", err)
	}

	for {
		select {
		case <-vmimpl.Shutdown:
			return nil
		case err := <-errc:
			if err != nil {
				return fmt.Errorf("error while running: %s", err)
			}
			return nil
		case out, ok := <-outc:
			if ok && inst.debug {
				log.Logf(1, "%s", out)
			}
		}
	}
}

func (inst *instance) Copy(hostSrc string) (string, error) {
	return "", fmt.Errorf("not implemented")
}

func (inst *instance) Forward(port int) (string, error) {
	return "", fmt.Errorf("not implemented")
}

func (inst *instance) Close() {
	// Stop Cuttlefish before shutting down the GCE instance.
	inst.runOnHost(10*time.Minute, "./bin/stop_cvd")
	inst.gceInst.Close()
}

func (inst *instance) Run(timeout time.Duration, stop <-chan bool, command string) (
	<-chan []byte, <-chan error, error) {
	return nil, nil, fmt.Errorf("not implemented")
}

func (inst *instance) Diagnose(rep *report.Report) ([]byte, bool) {
	return nil, false
}