forked from dvyukov/go-fuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slave.go
707 lines (655 loc) · 17.3 KB
/
slave.go
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// Copyright 2015 Dmitry Vyukov. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package main
import (
"archive/zip"
"bufio"
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"os"
"strings"
"sync/atomic"
"time"
"unsafe"
. "github.com/dvyukov/go-fuzz/go-fuzz-defs"
)
const (
execBootstrap = iota
execCorpus
execMinimizeInput
execMinimizeCrasher
execTriageInput
execFuzz
execVersifier
execSmash
execSonar
execSonarHint
execTotal
execCount
)
// Slave manages one testee.
type Slave struct {
id int
hub *Hub
mutator *Mutator
coverBin *TestBinary
sonarBin *TestBinary
triageQueue []MasterInput
crasherQueue []NewCrasherArgs
lastSync time.Time
stats Stats
execs [execCount]uint64
}
type Input struct {
mine bool
data []byte
cover []byte
coverSize int
res int
depth int
typ int
execTime uint64
favored bool
score int
runningScoreSum int
}
func slaveMain() {
zipr, err := zip.OpenReader(*flagBin)
if err != nil {
log.Fatalf("failed to open bin file: %v", err)
}
var coverBin, sonarBin string
var metadata MetaData
for _, zipf := range zipr.File {
r, err := zipf.Open()
if err != nil {
log.Fatalf("failed to uzip file from input archive: %v", err)
}
if zipf.Name == "metadata" {
if err := json.NewDecoder(r).Decode(&metadata); err != nil {
log.Fatalf("failed to decode metadata: %v", err)
}
} else {
f, err := ioutil.TempFile("", "go-fuzz")
if err != nil {
log.Fatalf("failed to create temp file: %v", err)
}
f.Close()
os.Remove(f.Name())
f, err = os.OpenFile(f.Name()+".exe", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0700)
if err != nil {
log.Fatalf("failed to create temp file: %v", err)
}
if _, err := io.Copy(f, r); err != nil {
log.Fatalf("failed to uzip bin file: %v", err)
}
f.Close()
switch zipf.Name {
case "cover.exe":
coverBin = f.Name()
case "sonar.exe":
sonarBin = f.Name()
default:
log.Fatalf("unknown file '%v' in input archive", f.Name())
}
}
r.Close()
}
zipr.Close()
if coverBin == "" || sonarBin == "" || len(metadata.Blocks) == 0 {
log.Fatalf("bad input archive: missing file")
}
shutdownCleanup = append(shutdownCleanup, func() {
os.Remove(coverBin)
os.Remove(sonarBin)
})
hub := newHub(metadata)
for i := 0; i < *flagProcs; i++ {
s := &Slave{
id: i,
hub: hub,
mutator: newMutator(),
}
s.coverBin = newTestBinary(coverBin, s.periodicCheck, &s.stats)
s.sonarBin = newTestBinary(sonarBin, s.periodicCheck, &s.stats)
go s.loop()
}
}
func (s *Slave) loop() {
iter, fuzzSonarIter, versifierSonarIter := 0, 0, 0
for atomic.LoadUint32(&shutdown) == 0 {
if len(s.crasherQueue) > 0 {
n := len(s.crasherQueue) - 1
crash := s.crasherQueue[n]
s.crasherQueue[n] = NewCrasherArgs{}
s.crasherQueue = s.crasherQueue[:n]
if *flagV >= 2 {
log.Printf("slave %v processes crasher [%v]%v", s.id, len(crash.Data), hash(crash.Data))
}
s.processCrasher(crash)
continue
}
select {
case input := <-s.hub.triageC:
if *flagV >= 2 {
log.Printf("slave %v triages master input [%v]%v minimized=%v smashed=%v", s.id, len(input.Data), hash(input.Data), input.Minimized, input.Smashed)
}
s.triageInput(input)
for {
x := atomic.LoadUint32(&s.hub.initialTriage)
if x == 0 || atomic.CompareAndSwapUint32(&s.hub.initialTriage, x, x-1) {
break
}
}
continue
default:
}
if atomic.LoadUint32(&s.hub.initialTriage) != 0 {
// Other slaves are still triaging initial inputs.
// Wait until they finish, otherwise we can generate
// as if new interesting inputs that are not actually new
// and thus unnecessary inflate corpus on every run.
time.Sleep(100 * time.Millisecond)
continue
}
if len(s.triageQueue) > 0 {
n := len(s.triageQueue) - 1
input := s.triageQueue[n]
s.triageQueue[n] = MasterInput{}
s.triageQueue = s.triageQueue[:n]
if *flagV >= 2 {
log.Printf("slave %v triages local input [%v]%v minimized=%v smashed=%v", s.id, len(input.Data), hash(input.Data), input.Minimized, input.Smashed)
}
s.triageInput(input)
continue
}
ro := s.hub.ro.Load().(*ROData)
if len(ro.corpus) == 0 {
// Some other slave triages corpus inputs.
time.Sleep(100 * time.Millisecond)
continue
}
// 9 out of 10 iterations are random fuzzing.
iter++
if iter%10 != 0 || ro.verse == nil {
data, depth := s.mutator.generate(ro)
// Every 1000-th iteration goes to sonar.
fuzzSonarIter++
if *flagSonar && fuzzSonarIter%1000 == 0 {
// TODO: ensure that generated hint inputs does not actually take 99% of time.
sonar := s.testInputSonar(data, depth)
s.processSonarData(data, sonar, depth, false)
} else {
// Plain old blind fuzzing.
s.testInput(data, depth, execFuzz)
}
} else {
// 1 out of 10 iterations goes to versifier.
data := ro.verse.Rhyme()
const maxSize = MaxInputSize - 5*SonarMaxLen // need some gap for sonar replacements
if len(data) > maxSize {
data = data[:maxSize]
}
// Every 100-th versifier input goes to sonar.
versifierSonarIter++
if *flagSonar && versifierSonarIter%100 == 0 {
sonar := s.testInputSonar(data, 0)
s.processSonarData(data, sonar, 0, false)
} else {
s.testInput(data, 0, execVersifier)
}
}
}
s.shutdown()
}
// triageInput processes every new input.
// It calculates per-input metrics like execution time, coverage mask,
// and minimizes the input to the minimal input with the same coverage.
func (s *Slave) triageInput(input MasterInput) {
if len(input.Data) > MaxInputSize {
input.Data = input.Data[:MaxInputSize]
}
inp := Input{
data: input.Data,
depth: int(input.Prio),
typ: input.Type,
execTime: 1 << 60,
}
// Calculate min exec time, min coverage and max result of 3 runs.
for i := 0; i < 3; i++ {
s.execs[execTriageInput]++
res, ns, cover, _, output, crashed, hanged := s.coverBin.test(inp.data)
if crashed {
// Inputs in corpus should not crash.
s.noteCrasher(inp.data, output, hanged)
return
}
if inp.cover == nil {
inp.cover = make([]byte, CoverSize)
copy(inp.cover, cover)
} else {
for i, v := range cover {
x := inp.cover[i]
if v > x {
inp.cover[i] = v
}
}
}
if inp.res < res {
inp.res = res
}
if inp.execTime > ns {
inp.execTime = ns
}
}
if !input.Minimized {
inp.mine = true
ro := s.hub.ro.Load().(*ROData)
// When minimizing new inputs we don't pursue exactly the same coverage,
// instead we pursue just the "novelty" in coverage.
// Here we use corpusCover, because maxCover already includes the input coverage.
newCover, ok := findNewCover(ro.corpusCover, inp.cover)
if !ok {
return // covered by somebody else
}
inp.data = s.minimizeInput(inp.data, false, func(candidate, cover, output []byte, res int, crashed, hanged bool) bool {
if crashed {
s.noteCrasher(candidate, output, hanged)
return false
}
if inp.res != res || worseCover(newCover, cover) {
s.noteNewInput(candidate, cover, res, inp.depth+1, execMinimizeInput)
return false
}
return true
})
} else if !input.Smashed {
s.smash(inp.data, inp.depth)
}
inp.coverSize = 0
for _, v := range inp.cover {
if v != 0 {
inp.coverSize++
}
}
s.hub.newInputC <- inp
}
// processCrasher minimizes new crashers and sends them to the hub.
func (s *Slave) processCrasher(crash NewCrasherArgs) {
// Hanging inputs can take very long time to minimize.
if !crash.Hanging {
crash.Data = s.minimizeInput(crash.Data, true, func(candidate, cover, output []byte, res int, crashed, hanged bool) bool {
if !crashed {
return false
}
supp := extractSuppression(output)
if hanged || !bytes.Equal(crash.Suppression, supp) {
s.noteCrasher(candidate, output, hanged)
return false
}
crash.Error = output
return true
})
}
s.hub.newCrasherC <- crash
}
// minimizeInput applies series of minimizing transformations to data
// and asks pred whether the input is equivalent to the original one or not.
func (s *Slave) minimizeInput(data []byte, canonicalize bool, pred func(candidate, cover, output []byte, result int, crashed, hanged bool) bool) []byte {
res := make([]byte, len(data))
copy(res, data)
start := time.Now()
stat := &s.execs[execMinimizeInput]
if canonicalize {
stat = &s.execs[execMinimizeCrasher]
}
// First, try to cut tail.
for n := 1024; n != 0; n /= 2 {
for len(res) > n {
if time.Since(start) > *flagMinimize {
return res
}
candidate := res[:len(res)-n]
*stat++
result, _, cover, _, output, crashed, hanged := s.coverBin.test(candidate)
if !pred(candidate, cover, output, result, crashed, hanged) {
break
}
res = candidate
}
}
// Then, try to remove each individual byte.
tmp := make([]byte, len(res))
for i := 0; i < len(res); i++ {
if time.Since(start) > *flagMinimize {
return res
}
candidate := tmp[:len(res)-1]
copy(candidate[:i], res[:i])
copy(candidate[i:], res[i+1:])
*stat++
result, _, cover, _, output, crashed, hanged := s.coverBin.test(candidate)
if !pred(candidate, cover, output, result, crashed, hanged) {
continue
}
res = makeCopy(candidate)
i--
}
// Then, try to remove each possible subset of bytes.
for i := 0; i < len(res)-1; i++ {
copy(tmp, res[:i])
for j := len(res); j > i+1; j-- {
if time.Since(start) > *flagMinimize {
return res
}
candidate := tmp[:len(res)-j+i]
copy(candidate[i:], res[j:])
*stat++
result, _, cover, _, output, crashed, hanged := s.coverBin.test(candidate)
if !pred(candidate, cover, output, result, crashed, hanged) {
continue
}
res = makeCopy(candidate)
j = len(res)
}
}
// Then, try to replace each individual byte with '0'.
if canonicalize {
for i := 0; i < len(res); i++ {
if res[i] == '0' {
continue
}
if time.Since(start) > *flagMinimize {
return res
}
candidate := tmp[:len(res)]
copy(candidate, res)
candidate[i] = '0'
*stat++
result, _, cover, _, output, crashed, hanged := s.coverBin.test(candidate)
if !pred(candidate, cover, output, result, crashed, hanged) {
continue
}
res = makeCopy(candidate)
}
}
return res
}
// smash gives some minimal attention to every new input.
func (s *Slave) smash(data []byte, depth int) {
ro := s.hub.ro.Load().(*ROData)
// Pass it through sonar.
if *flagSonar {
sonar := s.testInputSonar(data, depth)
s.processSonarData(data, sonar, depth, true)
}
// Flip each bit one-by-one.
for i := 0; i < len(data)*8; i++ {
data[i/8] ^= 1 << uint(i%8)
s.testInput(data, depth, execSmash)
data[i/8] ^= 1 << uint(i%8)
}
// Two walking bits.
for i := 0; i < len(data)*8-1; i++ {
data[i/8] ^= 1 << uint(i%8)
data[(i+1)/8] ^= 1 << uint((i+1)%8)
s.testInput(data, depth, execSmash)
data[i/8] ^= 1 << uint(i%8)
data[(i+1)/8] ^= 1 << uint((i+1)%8)
}
// Four walking bits.
for i := 0; i < len(data)*8-3; i++ {
data[i/8] ^= 1 << uint(i%8)
data[(i+1)/8] ^= 1 << uint((i+1)%8)
data[(i+2)/8] ^= 1 << uint((i+2)%8)
data[(i+3)/8] ^= 1 << uint((i+3)%8)
s.testInput(data, depth, execSmash)
data[i/8] ^= 1 << uint(i%8)
data[(i+1)/8] ^= 1 << uint((i+1)%8)
data[(i+2)/8] ^= 1 << uint((i+2)%8)
data[(i+3)/8] ^= 1 << uint((i+3)%8)
}
// Byte flip.
for i := 0; i < len(data); i++ {
data[i] ^= 0xff
s.testInput(data, depth, execSmash)
data[i] ^= 0xff
}
// Two walking bytes.
for i := 0; i < len(data)-1; i++ {
data[i] ^= 0xff
data[i+1] ^= 0xff
s.testInput(data, depth, execSmash)
data[i] ^= 0xff
data[i+1] ^= 0xff
}
// Four walking bytes.
for i := 0; i < len(data)-3; i++ {
data[i] ^= 0xff
data[i+1] ^= 0xff
data[i+2] ^= 0xff
data[i+3] ^= 0xff
s.testInput(data, depth, execSmash)
data[i] ^= 0xff
data[i+1] ^= 0xff
data[i+2] ^= 0xff
data[i+3] ^= 0xff
}
// Increment/decrement every byte.
for i := 0; i < len(data); i++ {
for j := uint8(1); j <= 4; j++ {
data[i] += j
s.testInput(data, depth, execSmash)
data[i] -= j
data[i] -= j
s.testInput(data, depth, execSmash)
data[i] += j
}
}
// Set bytes to interesting values.
for i := 0; i < len(data); i++ {
v := data[i]
for _, x := range interesting8 {
data[i] = uint8(x)
s.testInput(data, depth, execSmash)
}
data[i] = v
}
// Set words to interesting values.
for i := 0; i < len(data)-1; i++ {
p := (*int16)(unsafe.Pointer(&data[i]))
v := *p
for _, x := range interesting16 {
*p = x
s.testInput(data, depth, execSmash)
if x != 0 && x != -1 {
*p = int16(swap16(uint16(x)))
s.testInput(data, depth, execSmash)
}
}
*p = v
}
// Set double-words to interesting values.
for i := 0; i < len(data)-3; i++ {
p := (*int32)(unsafe.Pointer(&data[i]))
v := *p
for _, x := range interesting32 {
*p = x
s.testInput(data, depth, execSmash)
if x != 0 && x != -1 {
*p = int32(swap32(uint32(x)))
s.testInput(data, depth, execSmash)
}
}
*p = v
}
// Trim after every byte.
for i := 1; i < len(data); i++ {
tmp := data[:i]
s.testInput(tmp, depth, execSmash)
}
// Insert a byte after every byte.
tmp := make([]byte, len(data)+1)
if len(tmp) > MaxInputSize {
tmp = tmp[:MaxInputSize]
}
for i := 0; i <= len(data) && i < MaxInputSize-1; i++ {
copy(tmp, data[:i])
copy(tmp[i+1:], data[i:])
tmp[i] = 0
s.testInput(tmp, depth, execSmash)
tmp[i] = 'a'
s.testInput(tmp, depth, execSmash)
}
// Do a bunch of random mutations so that this input catches up with the rest.
for i := 0; i < 1e4; i++ {
tmp := s.mutator.mutate(data, ro)
s.testInput(tmp, depth+1, execFuzz)
}
}
func (s *Slave) testInput(data []byte, depth, typ int) {
s.testInputImpl(s.coverBin, data, depth, typ)
}
func (s *Slave) testInputSonar(data []byte, depth int) (sonar []byte) {
return s.testInputImpl(s.sonarBin, data, depth, execSonar)
}
func (s *Slave) testInputImpl(bin *TestBinary, data []byte, depth, typ int) (sonar []byte) {
ro := s.hub.ro.Load().(*ROData)
if len(ro.badInputs) > 0 {
if _, ok := ro.badInputs[hash(data)]; ok {
return nil // no, thanks
}
}
s.execs[typ]++
res, _, cover, sonar, output, crashed, hanged := bin.test(data)
if crashed {
s.noteCrasher(data, output, hanged)
return nil
}
s.noteNewInput(data, cover, res, depth, typ)
return sonar
}
func (s *Slave) noteNewInput(data, cover []byte, res, depth, typ int) {
if res < 0 {
// User said to not add this input to corpus.
return
}
if s.hub.updateMaxCover(cover) {
s.triageQueue = append(s.triageQueue, MasterInput{makeCopy(data), uint64(depth), typ, false, false})
}
}
func (s *Slave) noteCrasher(data, output []byte, hanged bool) {
ro := s.hub.ro.Load().(*ROData)
supp := extractSuppression(output)
if _, ok := ro.suppressions[hash(supp)]; ok {
return
}
s.crasherQueue = append(s.crasherQueue, NewCrasherArgs{
Data: makeCopy(data),
Error: output,
Suppression: supp,
Hanging: hanged,
})
}
func (s *Slave) periodicCheck() {
if atomic.LoadUint32(&shutdown) != 0 {
s.shutdown()
select {}
}
if time.Since(s.lastSync) < syncPeriod {
return
}
s.execs[execTotal] += s.stats.execs
s.lastSync = time.Now()
s.hub.syncC <- s.stats
s.stats.execs = 0
s.stats.restarts = 0
if *flagV >= 2 {
log.Printf("slave %v: triageq=%v execs=%v mininp=%v mincrash=%v triage=%v fuzz=%v versifier=%v smash=%v sonar=%v hint=%v",
s.id, len(s.triageQueue),
s.execs[execTotal], s.execs[execMinimizeInput], s.execs[execMinimizeCrasher],
s.execs[execTriageInput], s.execs[execFuzz], s.execs[execVersifier], s.execs[execSmash],
s.execs[execSonar], s.execs[execSonarHint])
}
}
// shutdown cleanups after slave, it is not guaranteed to be called.
func (s *Slave) shutdown() {
s.coverBin.close()
s.sonarBin.close()
}
func extractSuppression(out []byte) []byte {
var supp []byte
seenPanic := false
collect := false
s := bufio.NewScanner(bytes.NewReader(out))
for s.Scan() {
line := s.Text()
if !seenPanic && (strings.HasPrefix(line, "panic: ") ||
strings.HasPrefix(line, "fatal error: ") ||
strings.HasPrefix(line, "SIG") && strings.Index(line, ": ") != 0) {
// Start of a crash message.
seenPanic = true
supp = append(supp, line...)
supp = append(supp, '\n')
if line == "SIGABRT: abort" || line == "signal: killed" {
return supp // timeout stacks are flaky
}
}
if collect && line == "runtime stack:" {
// Skip runtime stack.
// Unless it is a runtime bug, user stack is more descriptive.
collect = false
}
if collect && len(line) > 0 && (line[0] >= 'a' && line[0] <= 'z' ||
line[0] >= 'A' && line[0] <= 'Z') {
// Function name line.
idx := strings.LastIndex(line, "(")
if idx != -1 {
supp = append(supp, line[:idx]...)
supp = append(supp, '\n')
}
}
if collect && line == "" {
// End of first goroutine stack.
break
}
if seenPanic && !collect && line == "" {
// Start of first goroutine stack.
collect = true
}
}
if len(supp) == 0 {
supp = out
}
return supp
}
func reverse(data []byte) []byte {
tmp := make([]byte, len(data))
for i, v := range data {
tmp[len(data)-i-1] = v
}
return tmp
}
func increment(data []byte) []byte {
tmp := make([]byte, len(data))
for i, v := range data {
tmp[i] = v + 1
if v != 0xff {
break
}
}
return tmp
}
func decrement(data []byte) []byte {
tmp := make([]byte, len(data))
for i, v := range data {
tmp[i] = v - 1
if v != 0 {
break
}
}
return tmp
}