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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
// Copyright 2018 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 vm
import (
"bytes"
"fmt"
"testing"
"time"
"github.com/google/syzkaller/pkg/mgrconfig"
"github.com/google/syzkaller/pkg/report"
"github.com/google/syzkaller/sys/targets"
"github.com/google/syzkaller/vm/vmimpl"
)
type testPool struct {
}
func (pool *testPool) Count() int {
return 1
}
func (pool *testPool) Create(workdir string, index int) (vmimpl.Instance, error) {
return &testInstance{
outc: make(chan []byte, 10),
errc: make(chan error, 1),
}, nil
}
func (pool *testPool) Close() error {
return nil
}
type testInstance struct {
outc chan []byte
errc chan error
diagnoseBug bool
diagnoseNoWait bool
}
func (inst *testInstance) Copy(hostSrc string) (string, error) {
return "", nil
}
func (inst *testInstance) Forward(port int) (string, error) {
return "", nil
}
func (inst *testInstance) Run(timeout time.Duration, stop <-chan bool, command string) (
outc <-chan []byte, errc <-chan error, err error) {
return inst.outc, inst.errc, nil
}
func (inst *testInstance) Diagnose(rep *report.Report) ([]byte, bool) {
var diag []byte
if inst.diagnoseBug {
diag = []byte("BUG: DIAGNOSE\n")
} else {
diag = []byte("DIAGNOSE\n")
}
if inst.diagnoseNoWait {
return diag, false
}
inst.outc <- diag
return nil, true
}
func (inst *testInstance) Close() error {
return nil
}
func init() {
beforeContextDefault = maxErrorLength + 100
tickerPeriod = 1 * time.Second
vmimpl.WaitForOutputTimeout = 3 * time.Second
ctor := func(env *vmimpl.Env) (vmimpl.Pool, error) {
return &testPool{}, nil
}
vmimpl.Register("test", vmimpl.Type{
Ctor: ctor,
Preemptible: true,
})
}
type Test struct {
Name string
Exit ExitCondition
DiagnoseBug bool // Diagnose produces output that is detected as kernel crash.
DiagnoseNoWait bool // Diagnose returns output directly rather than to console.
Body func(outc chan []byte, errc chan error)
BodyExecuting func(outc chan []byte, errc chan error, inject chan<- bool)
Report *report.Report
}
// nolint: goconst // "DIAGNOSE\n", "BUG: bad\n" and "other output\n"
var tests = []*Test{
{
Name: "program-exits-normally",
Exit: ExitNormal,
Body: func(outc chan []byte, errc chan error) {
time.Sleep(time.Second)
errc <- nil
},
},
{
Name: "program-exits-when-it-should-not",
Body: func(outc chan []byte, errc chan error) {
time.Sleep(time.Second)
errc <- nil
},
Report: &report.Report{
Title: lostConnectionCrash,
},
},
{
Name: "#875-diagnose-bugs",
Exit: ExitNormal,
DiagnoseBug: true,
Body: func(outc chan []byte, errc chan error) {
errc <- nil
},
},
{
Name: "#875-diagnose-bugs-2",
Body: func(outc chan []byte, errc chan error) {
errc <- nil
},
Report: &report.Report{
Title: lostConnectionCrash,
Output: []byte(
"DIAGNOSE\n",
),
},
},
{
Name: "diagnose-no-wait",
Body: func(outc chan []byte, errc chan error) {
errc <- nil
},
DiagnoseNoWait: true,
Report: &report.Report{
Title: lostConnectionCrash,
Output: []byte(
"\n" +
"VM DIAGNOSIS:\n" +
"DIAGNOSE\n",
),
},
},
{
Name: "diagnose-bug-no-wait",
Body: func(outc chan []byte, errc chan error) {
outc <- []byte("BUG: bad\n")
time.Sleep(time.Second)
outc <- []byte("other output\n")
},
DiagnoseNoWait: true,
Report: &report.Report{
Title: "BUG: bad",
Report: []byte(
"BUG: bad\n" +
"other output\n",
),
Output: []byte(
"BUG: bad\n" +
"other output\n" +
"\n" +
"VM DIAGNOSIS:\n" +
"DIAGNOSE\n",
),
},
},
{
Name: "kernel-crashes",
Body: func(outc chan []byte, errc chan error) {
outc <- []byte("BUG: bad\n")
time.Sleep(time.Second)
outc <- []byte("other output\n")
},
Report: &report.Report{
Title: "BUG: bad",
Report: []byte(
"BUG: bad\n" +
"DIAGNOSE\n" +
"other output\n",
),
},
},
{
Name: "fuzzer-is-preempted",
Body: func(outc chan []byte, errc chan error) {
outc <- []byte("BUG: bad\n")
outc <- []byte(executorPreemptedStr + "\n")
},
},
{
Name: "program-exits-but-kernel-crashes-afterwards",
Exit: ExitNormal,
Body: func(outc chan []byte, errc chan error) {
errc <- nil
time.Sleep(time.Second)
outc <- []byte("BUG: bad\n")
},
Report: &report.Report{
Title: "BUG: bad",
Report: []byte(
"BUG: bad\n" +
"DIAGNOSE\n",
),
},
},
{
Name: "timeout",
Exit: ExitTimeout,
Body: func(outc chan []byte, errc chan error) {
errc <- vmimpl.ErrTimeout
},
},
{
Name: "bad-timeout",
Body: func(outc chan []byte, errc chan error) {
errc <- vmimpl.ErrTimeout
},
Report: &report.Report{
Title: timeoutCrash,
},
},
{
Name: "program-crashes",
Body: func(outc chan []byte, errc chan error) {
errc <- fmt.Errorf("error")
},
Report: &report.Report{
Title: lostConnectionCrash,
},
},
{
Name: "program-crashes-expected",
Exit: ExitError,
Body: func(outc chan []byte, errc chan error) {
errc <- fmt.Errorf("error")
},
},
{
Name: "no-output-1",
Body: func(outc chan []byte, errc chan error) {
},
Report: &report.Report{
Title: noOutputCrash,
},
},
{
Name: "no-output-2",
Body: func(outc chan []byte, errc chan error) {
for i := 0; i < 5; i++ {
time.Sleep(time.Second)
outc <- []byte("something\n")
}
},
Report: &report.Report{
Title: noOutputCrash,
},
},
{
Name: "no-no-output",
Exit: ExitNormal,
Body: func(outc chan []byte, errc chan error) {
for i := 0; i < 5; i++ {
time.Sleep(time.Second)
outc <- append(executingProgram, '\n')
}
errc <- nil
},
},
{
Name: "outc-closed",
Exit: ExitTimeout,
Body: func(outc chan []byte, errc chan error) {
close(outc)
time.Sleep(time.Second)
errc <- vmimpl.ErrTimeout
},
},
{
Name: "lots-of-output",
Exit: ExitTimeout,
Body: func(outc chan []byte, errc chan error) {
for i := 0; i < 100; i++ {
outc <- []byte("something\n")
}
time.Sleep(time.Second)
errc <- vmimpl.ErrTimeout
},
},
{
Name: "split-line",
Exit: ExitNormal,
Body: func(outc chan []byte, errc chan error) {
// "ODEBUG:" lines should be ignored, however the matchPos logic
// used to trim the lines so that we could see just "BUG:" later
// and detect it as crash.
buf := new(bytes.Buffer)
for i := 0; i < 50; i++ {
buf.WriteString("[ 2886.597572] ODEBUG: Out of memory. ODEBUG disabled\n")
buf.Write(bytes.Repeat([]byte{'-'}, i))
buf.WriteByte('\n')
}
output := buf.Bytes()
for i := range output {
outc <- output[i : i+1]
}
errc <- nil
},
},
{
Name: "inject-executing",
Exit: ExitNormal,
BodyExecuting: func(outc chan []byte, errc chan error, inject chan<- bool) {
for i := 0; i < 6; i++ {
time.Sleep(time.Second)
inject <- true
}
errc <- nil
},
},
}
func TestMonitorExecution(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.Name, func(t *testing.T) {
t.Parallel()
testMonitorExecution(t, test)
})
}
}
func testMonitorExecution(t *testing.T, test *Test) {
dir := t.TempDir()
cfg := &mgrconfig.Config{
Derived: mgrconfig.Derived{
TargetOS: targets.Linux,
TargetArch: targets.AMD64,
TargetVMArch: targets.AMD64,
Timeouts: targets.Timeouts{
Scale: 1,
Slowdown: 1,
NoOutput: 5 * time.Second,
},
SysTarget: targets.Get(targets.Linux, targets.AMD64),
},
Workdir: dir,
Type: "test",
}
pool, err := Create(cfg, false)
if err != nil {
t.Fatal(err)
}
defer pool.Close()
reporter, err := report.NewReporter(cfg)
if err != nil {
t.Fatal(err)
}
inst, err := pool.Create(0)
if err != nil {
t.Fatal(err)
}
defer inst.Close()
testInst := inst.impl.(*testInstance)
testInst.diagnoseBug = test.DiagnoseBug
testInst.diagnoseNoWait = test.DiagnoseNoWait
done := make(chan bool)
finishCalled := 0
finishCb := EarlyFinishCb(func() { finishCalled++ })
opts := []any{test.Exit, finishCb}
var inject chan bool
if test.BodyExecuting != nil {
inject = make(chan bool, 10)
opts = append(opts, InjectExecuting(inject))
} else {
test.BodyExecuting = func(outc chan []byte, errc chan error, inject chan<- bool) {
test.Body(outc, errc)
}
}
go func() {
test.BodyExecuting(testInst.outc, testInst.errc, inject)
done <- true
}()
_, rep, err := inst.Run(time.Second, reporter, "", opts...)
if err != nil {
t.Fatal(err)
}
<-done
if finishCalled != 1 {
t.Fatalf("finish callback is called %v times", finishCalled)
}
if test.Report != nil && rep == nil {
t.Fatalf("got no report")
}
if test.Report == nil && rep != nil {
t.Fatalf("got unexpected report: %v", rep.Title)
}
if test.Report == nil {
return
}
if test.Report.Title != rep.Title {
t.Fatalf("want title %q, got title %q", test.Report.Title, rep.Title)
}
if !bytes.Equal(test.Report.Report, rep.Report) {
t.Fatalf("want report:\n%s\n\ngot report:\n%s", test.Report.Report, rep.Report)
}
if test.Report.Output != nil && !bytes.Equal(test.Report.Output, rep.Output) {
t.Fatalf("want output:\n%s\n\ngot output:\n%s", test.Report.Output, rep.Output)
}
}
func TestVMType(t *testing.T) {
testCases := []struct {
in string
want string
}{
{targets.GVisor, targets.GVisor},
{"proxyapp:android", "proxyapp"},
}
for _, tc := range testCases {
if got := vmType(tc.in); got != tc.want {
t.Errorf("vmType(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
|