forked from dvyukov/go-fuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
438 lines (395 loc) · 11 KB
/
hub.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
// 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 (
"fmt"
"log"
"net/rpc"
"path/filepath"
"sync"
"sync/atomic"
"time"
. "github.com/dvyukov/go-fuzz/go-fuzz-defs"
"github.com/dvyukov/go-fuzz/go-fuzz/versifier"
)
const (
syncPeriod = 3 * time.Second
syncDeadline = 100 * syncPeriod
minScore = 1.0
maxScore = 1000.0
defScore = 10.0
)
// Hub contains data shared between all slaves in the process (e.g. corpus).
// This reduces memory consumption for highly parallel slaves.
// Hub also handles communication with the master.
type Hub struct {
id int
master *rpc.Client
ro atomic.Value // *ROData
maxCoverMu sync.Mutex
maxCover atomic.Value // []byte
initialTriage uint32
corpusCoverSize int
corpusSigs map[Sig]struct{}
corpusStale bool
triageQueue []MasterInput
triageC chan MasterInput
newInputC chan Input
newCrasherC chan NewCrasherArgs
syncC chan Stats
stats Stats
corpusOrigins [execCount]uint64
}
type ROData struct {
corpus []Input
corpusCover []byte
badInputs map[Sig]struct{}
suppressions map[Sig]struct{}
strLits [][]byte // string literals in testee
intLits [][]byte // int literals in testee
coverBlocks map[int][]CoverBlock
sonarSites []SonarSite
verse *versifier.Verse
}
type Stats struct {
execs uint64
restarts uint64
}
func newHub(metadata MetaData) *Hub {
procs := *flagProcs
hub := &Hub{
corpusSigs: make(map[Sig]struct{}),
triageC: make(chan MasterInput, procs),
newInputC: make(chan Input, procs),
newCrasherC: make(chan NewCrasherArgs, procs),
syncC: make(chan Stats, procs),
}
if err := hub.connect(); err != nil {
log.Fatalf("failed to connect to master: %v", err)
}
coverBlocks := make(map[int][]CoverBlock)
for _, b := range metadata.Blocks {
coverBlocks[b.ID] = append(coverBlocks[b.ID], b)
}
sonarSites := make([]SonarSite, len(metadata.Sonar))
for i, b := range metadata.Sonar {
if i != b.ID {
log.Fatalf("corrupted sonar metadata")
}
sonarSites[i].id = b.ID
sonarSites[i].loc = fmt.Sprintf("%v:%v.%v,%v.%v", b.File, b.StartLine, b.StartCol, b.EndLine, b.EndCol)
}
hub.maxCover.Store(make([]byte, CoverSize))
ro := &ROData{
corpusCover: make([]byte, CoverSize),
badInputs: make(map[Sig]struct{}),
suppressions: make(map[Sig]struct{}),
coverBlocks: coverBlocks,
sonarSites: sonarSites,
}
// Prepare list of string and integer literals.
for _, lit := range metadata.Literals {
if lit.IsStr {
ro.strLits = append(ro.strLits, []byte(lit.Val))
} else {
ro.intLits = append(ro.intLits, []byte(lit.Val))
}
}
hub.ro.Store(ro)
go hub.loop()
return hub
}
func (hub *Hub) connect() error {
c, err := rpc.Dial("tcp", *flagSlave)
if err != nil {
return err
}
var res ConnectRes
if err := c.Call("Master.Connect", &ConnectArgs{Procs: *flagProcs}, &res); err != nil {
return err
}
hub.master = c
hub.id = res.ID
hub.initialTriage = uint32(len(res.Corpus))
hub.triageQueue = res.Corpus
return nil
}
func (hub *Hub) loop() {
// Local buffer helps to avoid deadlocks on chan overflows.
var triageC chan MasterInput
var triageInput MasterInput
syncTicker := time.NewTicker(syncPeriod).C
for {
if len(hub.triageQueue) > 0 && triageC == nil {
n := len(hub.triageQueue) - 1
triageInput = hub.triageQueue[n]
hub.triageQueue[n] = MasterInput{}
hub.triageQueue = hub.triageQueue[:n]
triageC = hub.triageC
}
select {
case <-syncTicker:
// Sync with the master.
if *flagV >= 1 {
ro := hub.ro.Load().(*ROData)
log.Printf("hub: corpus=%v bootstrap=%v fuzz=%v minimize=%v versifier=%v smash=%v sonar=%v",
len(ro.corpus), hub.corpusOrigins[execBootstrap]+hub.corpusOrigins[execCorpus],
hub.corpusOrigins[execFuzz]+hub.corpusOrigins[execSonar],
hub.corpusOrigins[execMinimizeInput]+hub.corpusOrigins[execMinimizeCrasher],
hub.corpusOrigins[execVersifier], hub.corpusOrigins[execSmash],
hub.corpusOrigins[execSonarHint])
}
args := &SyncArgs{
ID: hub.id,
Execs: hub.stats.execs,
Restarts: hub.stats.restarts,
CoverFullness: hub.corpusCoverSize,
}
hub.stats.execs = 0
hub.stats.restarts = 0
var res SyncRes
if err := hub.master.Call("Master.Sync", args, &res); err != nil {
log.Printf("sync call failed: %v, reconnection to master", err)
if err := hub.connect(); err != nil {
log.Printf("failed to connect to master: %v, killing slave", err)
return
}
}
if len(res.Inputs) > 0 {
hub.triageQueue = append(hub.triageQueue, res.Inputs...)
}
if hub.corpusStale {
hub.updateScores()
hub.corpusStale = false
}
case triageC <- triageInput:
// Send new input to slaves for triage.
if len(hub.triageQueue) > 0 {
n := len(hub.triageQueue) - 1
triageInput = hub.triageQueue[n]
hub.triageQueue[n] = MasterInput{}
hub.triageQueue = hub.triageQueue[:n]
} else {
triageC = nil
triageInput = MasterInput{}
}
case s := <-hub.syncC:
// Sync from a slave.
hub.stats.execs += s.execs
hub.stats.restarts += s.restarts
case input := <-hub.newInputC:
// New interesting input from slaves.
ro := hub.ro.Load().(*ROData)
if !compareCover(ro.corpusCover, input.cover) {
break
}
sig := hash(input.data)
if _, ok := hub.corpusSigs[sig]; ok {
break
}
// Passed deduplication, taking it.
if *flagV >= 2 {
log.Printf("hub received new input [%v]%v mine=%v", len(input.data), hash(input.data), input.mine)
}
hub.corpusSigs[sig] = struct{}{}
ro1 := new(ROData)
*ro1 = *ro
// Assign it the default score, but mark corpus for score recalculation.
hub.corpusStale = true
scoreSum := 0
if len(ro1.corpus) > 0 {
scoreSum = ro1.corpus[len(ro1.corpus)-1].runningScoreSum
}
input.score = defScore
input.runningScoreSum = scoreSum + defScore
ro1.corpus = append(ro1.corpus, input)
hub.updateMaxCover(input.cover)
ro1.corpusCover = makeCopy(ro.corpusCover)
hub.corpusCoverSize = updateMaxCover(ro1.corpusCover, input.cover)
if input.res > 0 || input.typ == execBootstrap {
ro1.verse = versifier.BuildVerse(ro.verse, input.data)
}
hub.ro.Store(ro1)
hub.corpusOrigins[input.typ]++
if input.mine {
if err := hub.master.Call("Master.NewInput", NewInputArgs{hub.id, input.data, uint64(input.depth)}, nil); err != nil {
log.Printf("new input call failed: %v, reconnecting to master", err)
if err := hub.connect(); err != nil {
log.Printf("failed to connect to master: %v, killing slave", err)
return
}
}
}
if *flagDumpCover {
dumpCover(filepath.Join(*flagWorkdir, "coverprofile"), ro.coverBlocks, ro.corpusCover)
}
case crash := <-hub.newCrasherC:
// New crasher from slaves. Woohoo!
if crash.Hanging || !*flagDup {
ro := hub.ro.Load().(*ROData)
ro1 := new(ROData)
*ro1 = *ro
if crash.Hanging {
ro1.badInputs = make(map[Sig]struct{})
for k, v := range ro.badInputs {
ro1.badInputs[k] = v
}
ro1.badInputs[hash(crash.Data)] = struct{}{}
}
if !*flagDup {
ro1.suppressions = make(map[Sig]struct{})
for k, v := range ro.suppressions {
ro1.suppressions[k] = v
}
ro1.suppressions[hash(crash.Suppression)] = struct{}{}
}
hub.ro.Store(ro1)
}
if err := hub.master.Call("Master.NewCrasher", crash, nil); err != nil {
log.Printf("new crasher call failed: %v", err)
}
}
}
}
// Preliminary cover update to prevent new input thundering herd.
// This function is synchronous to reduce latency.
func (hub *Hub) updateMaxCover(cover []byte) bool {
oldMaxCover := hub.maxCover.Load().([]byte)
if !compareCover(oldMaxCover, cover) {
return false
}
hub.maxCoverMu.Lock()
defer hub.maxCoverMu.Unlock()
oldMaxCover = hub.maxCover.Load().([]byte)
if !compareCover(oldMaxCover, cover) {
return false
}
maxCover := makeCopy(oldMaxCover)
updateMaxCover(maxCover, cover)
hub.maxCover.Store(maxCover)
return true
}
func (hub *Hub) updateScores() {
ro := hub.ro.Load().(*ROData)
ro1 := new(ROData)
*ro1 = *ro
corpus := make([]Input, len(ro.corpus))
copy(corpus, ro.corpus)
ro1.corpus = corpus
var sumExecTime, sumCoverSize uint64
for _, inp := range corpus {
sumExecTime += inp.execTime
sumCoverSize += uint64(inp.coverSize)
}
n := uint64(len(corpus))
avgExecTime := sumExecTime / n
avgCoverSize := sumCoverSize / n
// Phase 1: calculate score for each input independently.
for i, inp := range corpus {
score := defScore
// Execution time multiplier 0.1-3x.
// Fuzzing faster inputs increases efficiency.
execTime := float64(inp.execTime) / float64(avgExecTime)
if execTime > 10 {
score /= 10
} else if execTime > 4 {
score /= 4
} else if execTime > 2 {
score /= 2
} else if execTime < 0.25 {
score *= 3
} else if execTime < 0.33 {
score *= 2
} else if execTime < 0.5 {
score *= 1.5
}
// Coverage size multiplier 0.25-3x.
// Inputs with larger coverage are more interesting.
coverSize := float64(inp.coverSize) / float64(avgCoverSize)
if coverSize > 3 {
score *= 3
} else if coverSize > 2 {
score *= 2
} else if coverSize > 1.5 {
score *= 1.5
} else if coverSize < 0.3 {
score /= 4
} else if coverSize < 0.5 {
score /= 2
} else if coverSize < 0.75 {
score /= 1.5
}
// Input depth multiplier 1-5x.
// Deeper inputs have higher chances of digging deeper into code.
if inp.depth < 10 {
// no boost for you
} else if inp.depth < 20 {
score *= 2
} else if inp.depth < 40 {
score *= 3
} else if inp.depth < 80 {
score *= 4
} else {
score *= 5
}
// User boost (Fuzz function return value) multiplier 1-2x.
// We don't know what it is, but user said so.
if inp.res > 0 {
// Assuming this is a correct input (e.g. deserialized successfully).
score *= 2
}
if score < minScore {
score = minScore
} else if score > maxScore {
score = maxScore
}
corpus[i].score = int(score)
}
// Phase 2: Choose a minimal set of (favored) inputs that give full coverage.
// Non-favored inputs receive minimal score.
type Candidate struct {
index int
score int
chosen bool
}
candidates := make([]Candidate, CoverSize)
for idx, inp := range corpus {
corpus[idx].favored = false
for i, c := range inp.cover {
c = roundUpCover(c)
if c == 0 || c != ro.corpusCover[i] {
continue
}
if c > ro.corpusCover[i] {
log.Fatalf("bad")
}
if candidates[i].score < inp.score {
candidates[i].index = idx
candidates[i].score = inp.score
}
}
}
for ci, cand := range candidates {
if cand.score == 0 {
continue
}
inp := &corpus[cand.index]
inp.favored = true
for i := ci + 1; i < CoverSize; i++ {
c := roundUpCover(inp.cover[i])
if c == 0 || c != ro.corpusCover[i] {
continue
}
candidates[i].score = 0
}
}
scoreSum := 0
for i, inp := range corpus {
if !inp.favored {
inp.score = minScore
}
scoreSum += inp.score
corpus[i].runningScoreSum = scoreSum
}
hub.ro.Store(ro1)
}