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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
// 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 proxyapp package implements the experimental plugins support.
// We promise interface part will not be stable until documented.
package proxyapp
import (
"context"
"fmt"
"io"
"net/rpc"
"net/rpc/jsonrpc"
"os"
"sync"
"time"
"github.com/google/syzkaller/pkg/log"
"github.com/google/syzkaller/pkg/report"
"github.com/google/syzkaller/vm/proxyapp/proxyrpc"
"github.com/google/syzkaller/vm/vmimpl"
)
func ctor(params *proxyAppParams, env *vmimpl.Env) (vmimpl.Pool, error) {
subConfig, err := parseConfig(env.Config)
if err != nil {
return nil, fmt.Errorf("config parse error: %w", err)
}
p := &pool{
env: env,
close: make(chan bool, 1),
onClosed: make(chan error, 1),
}
err = p.init(params, subConfig)
if err != nil {
return nil, fmt.Errorf("can't initialize pool: %w", err)
}
go func() {
for {
select {
case <-p.close:
p.mu.Lock()
if p.proxy != nil {
p.proxy.terminate()
<-p.proxy.onTerminated
}
p.proxy = nil
p.onClosed <- nil
p.mu.Unlock()
return
case <-p.proxy.onTerminated:
}
p.mu.Lock()
time.Sleep(params.InitRetryDelay)
p.init(params, subConfig)
p.mu.Unlock()
}
}()
return p, nil
}
type pool struct {
mu sync.Mutex
env *vmimpl.Env
proxy *ProxyApp
count int
close chan bool
onClosed chan error
}
func (p *pool) init(params *proxyAppParams, cfg *Config) error {
var err error
p.proxy, err = runProxyApp(params, cfg.Command)
if err != nil {
return fmt.Errorf("failed to run ProxyApp: %w", err)
}
count, err := p.proxy.CreatePool(string(cfg.ProxyAppConfig), p.env.Debug)
if err != nil || count == 0 || (p.count != 0 && p.count != count) {
if err == nil {
err = fmt.Errorf("wrong pool size %v, prev was %v", count, p.count)
}
p.proxy.terminate()
<-p.proxy.onTerminated
p.proxy = nil
return fmt.Errorf("failed to construct pool: %w", err)
}
if p.count == 0 {
p.count = count
}
return nil
}
func (p *pool) Count() int {
return p.count
}
func (p *pool) Create(workdir string, index int) (vmimpl.Instance, error) {
p.mu.Lock()
proxy := p.proxy
p.mu.Unlock()
if proxy == nil {
return nil, fmt.Errorf("can't create instance using nil pool")
}
return proxy.CreateInstance(workdir, index)
}
// Close is not used now. Its support require wide code changes.
// TODO: support the pool cleanup on syz-manager level.
func (p *pool) Close() error {
close(p.close)
return <-p.onClosed
}
type ProxyApp struct {
*rpc.Client
terminate context.CancelFunc
onTerminated chan bool
}
func runProxyApp(params *proxyAppParams, cmd string) (*ProxyApp, error) {
ctx, cancelContext := context.WithCancel(context.Background())
subProcess := params.CommandRunner(ctx, cmd)
var toClose []io.Closer
freeAll := func() {
for _, closer := range toClose {
closer.Close()
}
cancelContext()
}
subStdout, err := subProcess.StdoutPipe()
if err != nil {
freeAll()
return nil, fmt.Errorf("failed to get stdoutpipe: %w", err)
}
toClose = append(toClose, subStdout)
subStdin, err := subProcess.StdinPipe()
if err != nil {
freeAll()
return nil, fmt.Errorf("failed to get stdinpipe: %w", err)
}
toClose = append(toClose, subStdin)
subprocessLogs, err := subProcess.StderrPipe()
if err != nil {
freeAll()
return nil, fmt.Errorf("failed to get stderrpipe: %w", err)
}
toClose = append(toClose, subprocessLogs)
codec := jsonrpc.NewClientCodec(stdInOutCloser{
io.NopCloser(subStdout),
subStdin,
})
if err := subProcess.Start(); err != nil {
freeAll()
return nil, fmt.Errorf("failed to start command %v: %w", cmd, err)
}
onTerminated := make(chan bool, 1)
go func() {
io.Copy(os.Stdout, subprocessLogs)
if err := subProcess.Wait(); err != nil {
log.Logf(0, "failed to Wait() subprocess: %v", err)
}
onTerminated <- true
}()
return &ProxyApp{
Client: rpc.NewClientWithCodec(codec),
terminate: cancelContext,
onTerminated: onTerminated,
}, nil
}
func (proxy *ProxyApp) CreatePool(config string, debug bool) (int, error) {
var reply proxyrpc.CreatePoolResult
err := proxy.Call(
"ProxyVM.CreatePool",
proxyrpc.CreatePoolParams{
Debug: debug,
Param: config,
},
&reply)
if err != nil {
return 0, err
}
return reply.Count, nil
}
func (proxy *ProxyApp) CreateInstance(workdir string, index int) (vmimpl.Instance, error) {
var reply proxyrpc.CreateInstanceResult
err := proxy.Call(
"ProxyVM.CreateInstance",
proxyrpc.CreateInstanceParams{
Workdir: workdir,
Index: index},
&reply)
if err != nil {
return nil, fmt.Errorf("failed to proxy.Call(\"ProxyVM.CreateInstance\"): %w", err)
}
return &instance{
ProxyApp: proxy,
ID: reply.ID,
}, nil
}
type instance struct {
*ProxyApp
ID string
}
// Copy copies a hostSrc file into VM and returns file name in VM.
// nolint: dupl
func (inst *instance) Copy(hostSrc string) (string, error) {
var reply proxyrpc.CopyResult
err := inst.ProxyApp.Call(
"ProxyVM.Copy",
proxyrpc.CopyParams{
ID: inst.ID,
HostSrc: hostSrc,
},
&reply)
if err != nil {
return "", err
}
return reply.VMFileName, nil
}
// Forward sets up forwarding from within VM to the given tcp
// port on the host and returns the address to use in VM.
// nolint: dupl
func (inst *instance) Forward(port int) (string, error) {
var reply proxyrpc.ForwardResult
err := inst.ProxyApp.Call(
"ProxyVM.Forward",
proxyrpc.ForwardParams{
ID: inst.ID,
Port: port,
},
&reply)
if err != nil {
return "", err
}
return reply.ManagerAddress, nil
}
func buildMerger(names ...string) (*vmimpl.OutputMerger, []io.Writer) {
var wPipes []io.Writer
merger := vmimpl.NewOutputMerger(nil)
for _, name := range names {
rpipe, wpipe := io.Pipe()
wPipes = append(wPipes, wpipe)
merger.Add(name, rpipe)
}
return merger, wPipes
}
func (inst *instance) Run(
timeout time.Duration,
stop <-chan bool,
command string,
) (<-chan []byte, <-chan error, error) {
merger, wPipes := buildMerger("stdout", "stderr", "console")
receivedStdoutChunks := wPipes[0]
receivedStderrChunks := wPipes[1]
receivedConsoleChunks := wPipes[2]
outc := merger.Output
var reply proxyrpc.RunStartReply
err := inst.ProxyApp.Call(
"ProxyVM.RunStart",
proxyrpc.RunStartParams{
ID: inst.ID,
Command: command},
&reply)
if err != nil {
return nil, nil, fmt.Errorf("error calling ProxyVM.Run with command %v: %w", command, err)
}
runID := reply.RunID
terminationError := make(chan error, 1)
timeoutSignal := time.After(timeout)
signalClientErrorf := clientErrorf(receivedStderrChunks)
go func() {
for {
var progress proxyrpc.RunReadProgressReply
readProgressCall := inst.ProxyApp.Go(
"ProxyVM.RunReadProgress",
proxyrpc.RunReadProgressParams{
ID: inst.ID,
RunID: runID,
},
&progress,
nil)
select {
case <-readProgressCall.Done:
receivedStdoutChunks.Write([]byte(progress.StdoutChunk))
receivedStderrChunks.Write([]byte(progress.StderrChunk))
receivedConsoleChunks.Write([]byte(progress.ConsoleOutChunk))
if readProgressCall.Error != nil {
signalClientErrorf("error reading progress from %v:%v: %v",
inst.ID, runID, readProgressCall.Error)
} else if progress.Error != "" {
signalClientErrorf("%v", progress.Error)
} else if progress.Finished {
terminationError <- nil
} else {
continue
}
case <-timeoutSignal:
// It is the happy path.
inst.runStop(runID)
terminationError <- vmimpl.ErrTimeout
case <-stop:
inst.runStop(runID)
terminationError <- vmimpl.ErrTimeout
}
break
}
}()
return outc, terminationError, nil
}
func (inst *instance) runStop(runID string) {
err := inst.ProxyApp.Call(
"ProxyVM.RunStop",
proxyrpc.RunStopParams{
ID: inst.ID,
RunID: runID,
},
&proxyrpc.RunStopParams{})
if err != nil {
log.Logf(0, "error calling runStop(%v) on %v: %v", runID, inst.ID, err)
}
}
func (inst *instance) Diagnose(r *report.Report) (diagnosis []byte, wait bool) {
var title string
if r != nil {
title = r.Title
}
var reply proxyrpc.DiagnoseReply
err := inst.ProxyApp.Call(
"ProxyVM.Diagnose",
proxyrpc.DiagnoseParams{
ID: inst.ID,
ReasonTitle: title,
},
&reply)
if err != nil {
return nil, false
}
return []byte(reply.Diagnosis), false
}
func (inst *instance) Close() {
var reply proxyrpc.CloseReply
err := inst.ProxyApp.Call(
"ProxyVM.Close",
proxyrpc.CloseParams{
ID: inst.ID,
},
&reply)
if err != nil {
log.Logf(0, "error closing instance %v: %v", inst.ID, err)
}
}
type stdInOutCloser struct {
io.ReadCloser
io.Writer
}
func clientErrorf(writer io.Writer) func(fmt string, s ...interface{}) {
return func(f string, s ...interface{}) {
writer.Write([]byte(fmt.Sprintf(f, s...)))
writer.Write([]byte("\nSYZFAIL: proxy app plugin error\n"))
}
}
|