forked from dvyukov/go-fuzz
/
coordinator.go
329 lines (279 loc) · 7.47 KB
/
coordinator.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
// Copyright 2015 go-fuzz 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 main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net"
"net/http"
_ "net/http/pprof"
"net/rpc"
"path/filepath"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/stephens2424/writerset"
)
// Coordinator manages persistent fuzzer state like input corpus and crashers.
type Coordinator struct {
mu sync.Mutex
idSeq int
workers map[int]*CoordinatorWorker
corpus *PersistentSet
suppressions *PersistentSet
crashers *PersistentSet
startTime time.Time
lastInput time.Time
statExecs uint64
statRestarts uint64
coverFullness int
statsWriters *writerset.WriterSet
}
// CoordinatorWorker represents coordinator's view of a worker.
type CoordinatorWorker struct {
id int
procs int
pending []CoordinatorInput
lastSync time.Time
}
// coordinatorMain is entry function for coordinator.
func coordinatorMain(ln net.Listener) {
m := &Coordinator{}
m.statsWriters = writerset.New()
m.startTime = time.Now()
m.lastInput = time.Now()
m.suppressions = newPersistentSet(filepath.Join(*flagWorkdir, "suppressions"))
m.crashers = newPersistentSet(filepath.Join(*flagWorkdir, "crashers"))
m.corpus = newPersistentSet(filepath.Join(*flagWorkdir, "corpus"))
if len(m.corpus.m) == 0 {
m.corpus.add(Artifact{[]byte{}, 0, false})
}
m.workers = make(map[int]*CoordinatorWorker)
coordinatorListen(m)
go coordinatorLoop(m)
s := rpc.NewServer()
s.Register(m)
s.Accept(ln)
}
func coordinatorListen(c *Coordinator) {
if *flagHTTP != "" {
http.HandleFunc("/eventsource", c.eventSource)
http.HandleFunc("/", c.index)
go func() {
fmt.Printf("Serving statistics on http://%s/\n", *flagHTTP)
panic(http.ListenAndServe(*flagHTTP, nil))
}()
} else {
runtime.MemProfileRate = 0
}
}
func coordinatorLoop(c *Coordinator) {
for range time.NewTicker(3 * time.Second).C {
if atomic.LoadUint32(&shutdown) != 0 {
return
}
c.mu.Lock()
// Nuke dead workers.
for id, s := range c.workers {
if time.Since(s.lastSync) < syncDeadline {
continue
}
log.Printf("worker %v died", s.id)
delete(c.workers, id)
}
c.mu.Unlock()
c.broadcastStats()
}
}
func (c *Coordinator) broadcastStats() {
stats := c.coordinatorStats()
// log to stdout
log.Println(stats.String())
// write to any http clients
b, err := json.Marshal(stats)
if err != nil {
panic(err)
}
fmt.Fprintf(c.statsWriters, "event: ping\ndata: %s\n\n", string(b))
c.statsWriters.Flush()
}
func (c *Coordinator) eventSource(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.WriteHeader(http.StatusOK)
<-c.statsWriters.Add(w)
}
func (c *Coordinator) index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
r.URL.Path = "/stats.html"
}
http.FileServer(assetFS()).ServeHTTP(w, r)
}
func (c *Coordinator) coordinatorStats() coordinatorStats {
c.mu.Lock()
defer c.mu.Unlock()
stats := coordinatorStats{
Corpus: uint64(len(c.corpus.m)),
Crashers: uint64(len(c.crashers.m)),
Uptime: fmtDuration(time.Since(c.startTime)),
StartTime: c.startTime,
LastNewInputTime: c.lastInput,
Execs: c.statExecs,
Cover: uint64(c.coverFullness),
}
// Print stats line.
if c.statExecs != 0 && c.statRestarts != 0 {
stats.RestartsDenom = c.statExecs / c.statRestarts
}
for _, w := range c.workers {
stats.Workers += uint64(w.procs)
}
return stats
}
type coordinatorStats struct {
Workers, Corpus, Crashers, Execs, Cover, RestartsDenom uint64
LastNewInputTime, StartTime time.Time
Uptime string
}
func (s coordinatorStats) String() string {
return fmt.Sprintf("workers: %v, corpus: %v (%v ago), crashers: %v,"+
" restarts: 1/%v, execs: %v (%.0f/sec), cover: %v, uptime: %v",
s.Workers, s.Corpus, fmtDuration(time.Since(s.LastNewInputTime)),
s.Crashers, s.RestartsDenom, s.Execs, s.ExecsPerSec(), s.Cover,
s.Uptime,
)
}
func (s coordinatorStats) ExecsPerSec() float64 {
return float64(s.Execs) * 1e9 / float64(time.Since(s.StartTime))
}
func fmtDuration(d time.Duration) string {
if d.Hours() >= 1 {
return fmt.Sprintf("%vh%vm", int(d.Hours()), int(d.Minutes())%60)
} else if d.Minutes() >= 1 {
return fmt.Sprintf("%vm%vs", int(d.Minutes()), int(d.Seconds())%60)
} else {
return fmt.Sprintf("%vs", int(d.Seconds()))
}
}
type ConnectArgs struct {
Procs int
}
type ConnectRes struct {
ID int
Corpus []CoordinatorInput
}
// CoordinatorInput is description of input that is passed between coordinator and worker.
type CoordinatorInput struct {
Data []byte
Prio uint64
Type execType
Minimized bool
Smashed bool
}
// Connect attaches new worker to coordinator.
func (c *Coordinator) Connect(a *ConnectArgs, r *ConnectRes) error {
c.mu.Lock()
defer c.mu.Unlock()
c.idSeq++
w := &CoordinatorWorker{
id: c.idSeq,
procs: a.Procs,
lastSync: time.Now(),
}
c.workers[w.id] = w
r.ID = w.id
// Give the worker initial corpus.
for _, a := range c.corpus.m {
r.Corpus = append(r.Corpus, CoordinatorInput{a.data, a.meta, execCorpus, !a.user, true})
}
return nil
}
type NewInputArgs struct {
ID int
Data []byte
Prio uint64
}
// NewInput saves new interesting input on coordinator.
func (c *Coordinator) NewInput(a *NewInputArgs, r *int) error {
c.mu.Lock()
defer c.mu.Unlock()
w := c.workers[a.ID]
if w == nil {
return errors.New("unknown worker")
}
art := Artifact{a.Data, a.Prio, false}
if !c.corpus.add(art) {
return nil
}
c.lastInput = time.Now()
// Queue the input for sending to every worker.
for _, w1 := range c.workers {
w1.pending = append(w1.pending, CoordinatorInput{a.Data, a.Prio, execCorpus, true, w1 != w})
}
return nil
}
type NewCrasherArgs struct {
Data []byte
Error []byte
Suppression []byte
Hanging bool
}
// NewCrasher saves new crasher input on coordinator.
func (c *Coordinator) NewCrasher(a *NewCrasherArgs, r *int) error {
c.mu.Lock()
defer c.mu.Unlock()
if !*flagDup && !c.suppressions.add(Artifact{a.Suppression, 0, false}) {
return nil // Already have this.
}
if !c.crashers.add(Artifact{a.Data, 0, false}) {
return nil // Already have this.
}
// Prepare quoted version of input to simplify creation of standalone reproducers.
var buf bytes.Buffer
for i := 0; i < len(a.Data); i += 20 {
e := i + 20
if e > len(a.Data) {
e = len(a.Data)
}
fmt.Fprintf(&buf, "\t%q", a.Data[i:e])
if e != len(a.Data) {
fmt.Fprintf(&buf, " +")
}
fmt.Fprintf(&buf, "\n")
}
c.crashers.addDescription(a.Data, buf.Bytes(), "quoted")
c.crashers.addDescription(a.Data, a.Error, "output")
return nil
}
type SyncArgs struct {
ID int
Execs uint64
Restarts uint64
CoverFullness int
}
type SyncRes struct {
Inputs []CoordinatorInput // new interesting inputs
}
var errUnkownWorker = errors.New("unknown worker")
// Sync is a periodic sync with a worker.
// Worker sends statistics. Coordinator returns new inputs.
func (c *Coordinator) Sync(a *SyncArgs, r *SyncRes) error {
c.mu.Lock()
defer c.mu.Unlock()
w := c.workers[a.ID]
if w == nil {
return errUnkownWorker
}
c.statExecs += a.Execs
c.statRestarts += a.Restarts
if c.coverFullness < a.CoverFullness {
c.coverFullness = a.CoverFullness
}
w.lastSync = time.Now()
r.Inputs = w.pending
w.pending = nil
return nil
}