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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
// Copyright 2020 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 qemu
import (
"encoding/json"
"errors"
"fmt"
"net"
"strings"
"github.com/google/syzkaller/pkg/log"
)
type qmpVersion struct {
Package string
QEMU struct {
Major int
Micro int
Minor int
}
}
type qmpBanner struct {
QMP struct {
Version qmpVersion
}
}
type qmpCommand struct {
Execute string `json:"execute"`
Arguments any `json:"arguments,omitempty"`
}
type hmpCommand struct {
Command string `json:"command-line"`
CPU int `json:"cpu-index"`
}
type qmpResponse struct {
Error struct {
Class string
Desc string
}
Return any
Event string
Data map[string]any
Timestamp struct {
Seconds int64
Microseconds int64
}
}
func (inst *instance) qmpConnCheck() error {
if inst.mon != nil {
return nil
}
addr := fmt.Sprintf("127.0.0.1:%v", inst.monport)
conn, err := net.Dial("tcp", addr)
if err != nil {
return err
}
monDec := json.NewDecoder(conn)
monEnc := json.NewEncoder(conn)
var banner qmpBanner
if err := monDec.Decode(&banner); err != nil {
return err
}
inst.monEnc = monEnc
inst.monDec = monDec
if _, err := inst.doQmp(&qmpCommand{Execute: "qmp_capabilities"}); err != nil {
inst.monEnc = nil
inst.monDec = nil
return err
}
inst.mon = conn
return nil
}
func (inst *instance) qmpRecv() (*qmpResponse, error) {
for {
qmp := new(qmpResponse)
err := inst.monDec.Decode(qmp)
if err != nil || qmp.Event == "" {
return qmp, err
}
log.Logf(3, "event: %v", qmp)
}
}
func (inst *instance) doQmp(cmd *qmpCommand) (*qmpResponse, error) {
if err := inst.monEnc.Encode(cmd); err != nil {
return nil, err
}
return inst.qmpRecv()
}
func (inst *instance) qmp(cmd *qmpCommand) (any, error) {
if err := inst.qmpConnCheck(); err != nil {
return nil, err
}
resp, err := inst.doQmp(cmd)
if err != nil {
return nil, err
}
if resp.Error.Desc != "" {
return nil, fmt.Errorf("%v", resp.Error)
}
if resp.Return == nil {
return nil, fmt.Errorf(`no "return" nor "error" in [%v]`, resp)
}
if output, _ := resp.Return.(string); strings.HasPrefix(output, "Error:") ||
strings.HasPrefix(output, "unknown command:") {
return nil, errors.New(output)
}
return resp.Return, nil
}
func (inst *instance) hmp(cmd string, cpu int) (string, error) {
if inst.debug {
log.Logf(0, "qemu: running hmp command: %v", cmd)
}
req := &qmpCommand{
Execute: "human-monitor-command",
Arguments: &hmpCommand{
Command: cmd,
CPU: cpu,
},
}
resp, err := inst.qmp(req)
if inst.debug {
log.Logf(0, "qemu: reply: %v\n%v", err, resp)
}
if err != nil {
return "", fmt.Errorf("qemu hmp command '%s': %w", cmd, err)
}
return resp.(string), nil
}
|