-
Notifications
You must be signed in to change notification settings - Fork 12
/
management.go
535 lines (453 loc) · 12.8 KB
/
management.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
package main
import (
"fmt"
"math/rand"
"sync"
)
type analyzerI interface {
// Called for all new test cases (with new hash).
isFit(seed *seedT, orgHash uint64) bool
// Called at the end of each round.
roundEnd()
distCalcGetter
cull([]uint64)
// Verbose and/or debug
makeRecStatus() recStatus
epilogue(progName string)
String() string
}
var _ analyzerI = new(pcaAnalyzer)
// *****************************************************************************
// ****************************** PCA Receiver *********************************
// Receiver made for the PCA-Learner as a fitness function. This is the second
// iteration of the receiver. Many things were tried with the first one, so it
// was getting too messy.
type tcPCAReceiver struct {
waitAnals *sync.WaitGroup
execHashes map[uint64]struct{}
newSeedChan chan<- *seedT
mainThreadWG *sync.WaitGroup
chFnd <-chan finding
analCom recChans
// Crash analysis
crashChan chan<- *seedT
crashAnal aflCrashAnalyzer
// Seed analysis based on PCA
analyzer analyzerI
// Reset/cull seeds channel.
pcaCullCh chan []uint64
frkSrvNb int
round int
progName string
}
func (rec tcPCAReceiver) getExecMapLen() int { return len(rec.execHashes) }
type recClearer struct {
distCalcGetter
wasReset bool
toRem []uint64
}
func (rc recClearer) info() (bool, []uint64) { return rc.wasReset, rc.toRem }
func makePCAReceiver(glbDataPt *PUT, analCom recChans, newSeedChan,
crashChan chan<- *seedT, chFnd <-chan finding, wg *sync.WaitGroup) (
rec tcPCAReceiver) {
rec.progName = glbDataPt.progName
rec.frkSrvNb = len(glbDataPt.puts)
rec.waitAnals = new(sync.WaitGroup)
rec.execHashes = make(map[uint64]struct{})
rec.newSeedChan = newSeedChan
rec.analCom = analCom
rec.chFnd = chFnd
rec.mainThreadWG = wg
rec.crashChan = crashChan
rec.crashAnal = makeAFLCrashAnalyzer()
rec.pcaCullCh = make(chan []uint64, 1)
rec.analyzer = newPCAAnalyzer(rec.analCom.getSeedList(), rec.pcaCullCh, rec.frkSrvNb)
return rec
}
func (rec tcPCAReceiver) receive(reporter reporterT) {
var roundFindings []finding
var wasReset bool
var toRemSend []uint64
for rec.chFnd != nil {
select {
case newFinding, ok := <-rec.chFnd:
if !ok {
rec.chFnd = nil
break
}
if !unicore {
rec.newFindingAnalysis(newFinding)
} else {
roundFindings = append(roundFindings, newFinding)
}
case <-rec.analCom.reqClear:
for len(rec.chFnd) > 0 { // Make sure we finished anlyzing all test cases.
newFinding := <-rec.chFnd
rec.newFindingAnalysis(newFinding)
}
if unicore {
for _, newFinding := range roundFindings {
rec.newFindingAnalysis(newFinding)
}
roundFindings = nil // Reset to zero.
}
rec.waitAnals.Wait()
if (verbose || debug) && len(rec.analCom.recInfoChan) == 0 {
rec.analCom.recInfoChan <- rec.String() + "\n"
}
rec.analyzer.roundEnd()
rec.analCom.ackClear <- recClearer{
distCalcGetter: rec.analyzer,
wasReset: wasReset,
toRem: toRemSend,
}
wasReset = false
toRemSend = nil
//
var statRec statusRecorder = rec.analyzer
if rand.Intn(csvWritingPeriod) == 0 {
reporter.logCSV(recName, statRec.makeRecStatus())
}
rec.round++
case toRem := <-rec.pcaCullCh:
rec.execHashes = make(map[uint64]struct{})
rec.analyzer.cull(toRem)
rec.analCom.seedManCullCh <- toRem
//
// Will send to main fuzzing loop.
wasReset = true
toRemSend = append(toRemSend, toRem...)
}
}
if false {
rec.analyzer.epilogue(rec.progName)
}
fmt.Println("End of receiving routine.")
rec.mainThreadWG.Done()
}
func (rec *tcPCAReceiver) newFindingAnalysis(newFinding finding) {
newSeedPt := newFinding.seedPt
if _, ok := rec.execHashes[newSeedPt.hash]; ok { // Already observed
newSeedPt.clean()
return
}
rec.execHashes[newSeedPt.hash] = struct{}{}
if newSeedPt.info.crashed {
rec.waitAnals.Add(1)
newSeedPt.pf.add(1)
go func() {
if rec.crashAnal.isCrash(newSeedPt) {
newSeedPt.setTrSize()
makeSeed(newSeedPt, newFinding.testCase)
rec.crashChan <- newSeedPt
} else {
newSeedPt.clean()
}
rec.waitAnals.Done()
}()
}
rec.waitAnals.Add(1)
go func() {
var orgHash uint64
orig := newSeedPt.info.orig
if orig != nil {
orgHash = orig.hash
}
//
if rec.analyzer.isFit(newSeedPt, orgHash) {
newSeedPt.setTrSize()
makeSeed(newSeedPt, newFinding.testCase)
rec.newSeedChan <- newSeedPt
} else {
newSeedPt.clean()
}
rec.waitAnals.Done()
}()
}
// ** Visualization **
func (rec tcPCAReceiver) String() (str string) { return rec.analyzer.String() }
// *****************************************************************************
// ***************************** Seed Manager **********************************
// *****************************************************************************
// The seed manager is in charge of:
// - Maintain the seed and crash list
// - Record information from last round
// - Chose seeds for next round
// - Since this routine is in charge of the seed list, other cannot read it.
// So sends a copy of the seed list upon request.
// - Also, if we use a model and we are training, update the model when a new
// one is sent. (Probably going to remove that.)
type seedManT struct {
seedManChans
previousSeedPts seedList
roundNb int
stoppedFuzz bool
covMap map[int]struct{}
verseQueue [][]byte // To update versifier. Emptied once processed.
}
func (seedMan seedManT) seedManager(glbDataPt *PUT, reporter reporterT,
wg *sync.WaitGroup) {
var oldSeeds seedList
seedMan.covMap = make(map[int]struct{})
seedSelector := glbDataPt.seedSelector
if useVersifier {
seedSelector = makeVersiSelector(len(glbDataPt.puts), seedSelector,
seedMan.versifier, seedMan.crosser)
}
selection := seedSelector.seedSelect(glbDataPt.seedPts)
glbInfo := new(infoRecord)
for !seedMan.stoppedFuzz {
select {
case newCrash := <-seedMan.crashChan:
glbDataPt.crashes[newCrash.hash] =
Crash{
In: newCrash.input,
traceBits: newCrash.traceBits,
HashVal: newCrash.hash,
err: newCrash.info.err,
}
go reporter.repCrash(glbDataPt.crashes[newCrash.hash])
case newSeedPt := <-seedMan.newSeedCh:
if seedMan.hasNewCov(newSeedPt.traceBits) {
go reporter.repSeed(newSeedPt)
seedMan.verseQueue = append(seedMan.verseQueue, newSeedPt.input)
}
seedMan.crosser.newSeed(newSeedPt)
if !evolutivePool {
continue
}
// Set exec time as parent one. Wrong but for initialization.
// Otherwise, have to calibrate for all new seeds.
newSeedPt.execTime = newSeedPt.info.orig.execTime
glbDataPt.seedPts = append(glbDataPt.seedPts, newSeedPt)
//checkSeedPts(glbDataPt.seedPts)
// Send a copy of the seed list to the routine in charge of computing
// distance between seeds.
case <-seedMan.reqSeedPts:
seedMan.seedPtsChan <- glbDataPt.seedPts.cpy()
case toRem := <-seedMan.cullCh:
for len(seedMan.newSeedCh) > 0 { // Empty the seed queue.
newSeedPt := <-seedMan.newSeedCh
newSeedPt.execTime = newSeedPt.info.orig.execTime
glbDataPt.seedPts = append(glbDataPt.seedPts, newSeedPt)
}
go seedMan.crosser.cullSeeds(toRem)
if culler, ok := seedSelector.(culler); ok {
culler.cull(toRem)
}
glbDataPt.seedPts, oldSeeds = cullSeedList(glbDataPt.seedPts, oldSeeds, toRem)
pruneSeedTree(glbDataPt.seedPts)
// Main fuzzing loop request seeds.
case _, ok := <-seedMan.reqSeeds:
if !ok {
seedMan.stoppedFuzz = true
break
}
seedMan.updateVerse()
seedMan.verseQueue = nil
glbInfo.endRoundRecording(seedMan.previousSeedPts, glbDataPt)
copiedSeedPts := make(seedList, len(selection))
copy(copiedSeedPts, selection)
if !unicore {
// If only access to one core, wants more synchronization
// between the (unique) fork server we have and the seed
// manager. In this case, making the fork server wait does not
// waste any ressource.
seedMan.selected <- copiedSeedPts
<-seedMan.ackSeeds
}
// Actualise seed selection. But will need the previous set to
// record info that is currently being fuzzed.
seedMan.previousSeedPts = selection
oldSeeds = cleanOldSeeds(oldSeeds)
selection = seedSelector.seedSelect(glbDataPt.seedPts)
if unicore {
seedMan.selected <- copiedSeedPts
<-seedMan.ackSeeds
}
if verbose || debug {
var extraStr string
if len(seedMan.recInfoChan) > 0 {
extraStr = <-seedMan.recInfoChan
}
if verbose {
glbInfo.printStatus(
seedMan.roundNb, glbDataPt, extraStr, selection)
seedMan.roundNb++
}
}
if rand.Intn(csvWritingPeriod) == 0 {
reporter.logCSV(smName, makeSMStatus(glbDataPt.seedPts, glbInfo.execNb))
}
}
}
wg.Done()
}
func (seedMan seedManT) hasNewCov(trace []byte) (has bool) {
for i, tr := range trace {
if tr > 0 {
if _, ok := seedMan.covMap[i]; !ok {
has = true
seedMan.covMap[i] = struct{}{}
}
}
}
return has
}
func (seedMan seedManT) updateVerse() {
if !useVersifier {
return
}
var wg sync.WaitGroup
newVerses := make([]*verse, len(seedMan.verseQueue))
wg.Add(len(seedMan.verseQueue))
for i, input := range seedMan.verseQueue {
go func(i int, input []byte) {
newVerses[i] = buildVerse(input)
wg.Done()
}(i, input)
}
wg.Wait()
for _, v := range newVerses {
if v == nil {
continue
}
//
seedMan.versifier.blocks = append(seedMan.versifier.blocks,
v.blocks...)
seedMan.versifier.allNodes = append(seedMan.versifier.allNodes,
v.allNodes...)
}
}
func cullSeedList(seedPts, os seedList, toRem []uint64) (newList, oldSeeds seedList) {
oldSeeds = os
hashmap := make(map[uint64]struct{})
for _, hash := range toRem {
hashmap[hash] = struct{}{}
}
for _, seedPt := range seedPts {
if _, ok := hashmap[seedPt.hash]; !ok {
newList = append(newList, seedPt)
} else {
oldSeeds = append(oldSeeds, seedPt)
}
}
return newList, oldSeeds
}
func cleanOldSeeds(oldSeeds seedList) seedList {
for _, seedPt := range oldSeeds {
//seedPt.input = nil
seedPt.traceBits = nil
}
return nil
}
// *************
// For debug. Check if seed list has two seeds with the same hash.
func checkSeedPts(seedPts seedList) {
if !debug {
return
}
seedCnt := make(map[uint64]uint)
for _, seedPt := range seedPts {
seedCnt[seedPt.hash] = 0
}
for _, seedPt := range seedPts {
seedCnt[seedPt.hash]++
}
doubleSeed := make(map[uint64]struct{})
for hash, cnt := range seedCnt {
if cnt > 1 {
doubleSeed[hash] = struct{}{}
}
}
if len(doubleSeed) > 0 {
panic(fmt.Sprintf("Exists double seeds: %d", len(doubleSeed)))
}
}
func checkSeedCull(seedPts seedList, toRem []uint64, beforeLen int) {
if len(seedPts)+len(toRem) != beforeLen {
fmt.Printf("len(seedPts) = %+v\n", len(seedPts))
fmt.Printf("len(toRem) = %+v\n", len(toRem))
fmt.Printf("beforeLen = %+v\n", beforeLen)
panic("wrong culling")
}
}
/******************************************************************************/
/************************** End of round recording ****************************/
type infoRecord struct {
execNb uint
roundNb uint
}
func (i *infoRecord) throughput() float64 {
return float64(i.execNb) / float64(i.roundNb)
}
func (glbInfo *infoRecord) endRoundRecording(selection seedList, glbDataPt *PUT) {
if len(selection) == 0 {
return
}
puts := glbDataPt.puts
glbDataPt.totalHangs = 0
for i, seedPt := range selection {
rndRep := puts[i].rndRep
// Recording seed features from last round.
seedPt.roundNb++
seedPt.execNb += rndRep.execs
seedPt.hangNb += rndRep.hangs
seedPt.cntLoops(glbDataPt.seedLoops, rndRep.loopNb)
seedPt.setExecTime(glbDataPt.rawExec)
//
glbInfo.execNb += rndRep.execs
glbInfo.roundNb++
// Updating 'global' fuzzing state.
glbDataPt.totalHangs += rndRep.hangs
glbDataPt.totalRoundNb++
}
}
// count loops; i.e. number of time a mutated seed trigger the exact same path
// in the programm as a seed.
func (seedPt *seedT) cntLoops(seedLoops map[uint64]uint, seedLoopNb uint) {
if _, ok := seedLoops[seedPt.hash]; ok {
seedLoops[seedPt.hash] += seedLoopNb
} else {
seedLoops[seedPt.hash] = seedLoopNb
}
}
// ***************** Util ******************
// ******* MultiThreaded Wait Group ********
// Work around the fact that cannot wait from different threads.
type multiWG struct {
wg *sync.WaitGroup
req, ack chan struct{}
newAdd chan struct{}
}
func makeMultiWG() (mwg multiWG) {
mwg.wg = new(sync.WaitGroup)
mwg.req = make(chan struct{})
mwg.ack = make(chan struct{})
mwg.newAdd = make(chan struct{})
go func() {
var needToWait bool
for {
select {
case <-mwg.req:
if needToWait {
mwg.wg.Wait()
needToWait = false
}
mwg.ack <- struct{}{}
case <-mwg.newAdd:
needToWait = true
}
}
}()
return mwg
}
func (mwg multiWG) add(delta int) { mwg.wg.Add(delta) }
func (mwg multiWG) done() {
mwg.newAdd <- struct{}{}
mwg.wg.Done()
}
func (mwg multiWG) wait() {
mwg.req <- struct{}{}
<-mwg.ack
}