-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
fuzzer.go
333 lines (293 loc) · 8.22 KB
/
fuzzer.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
// Copyright 2024 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 fuzzer
import (
"context"
"fmt"
"math/rand"
"runtime"
"sync"
"time"
"github.com/google/syzkaller/pkg/corpus"
"github.com/google/syzkaller/pkg/flatrpc"
"github.com/google/syzkaller/pkg/fuzzer/queue"
"github.com/google/syzkaller/pkg/ipc"
"github.com/google/syzkaller/pkg/stats"
"github.com/google/syzkaller/prog"
)
type Fuzzer struct {
Stats
Config *Config
Cover *Cover
ctx context.Context
mu sync.Mutex
rnd *rand.Rand
target *prog.Target
ct *prog.ChoiceTable
ctProgs int
ctMu sync.Mutex // TODO: use RWLock.
ctRegenerate chan struct{}
execQueues
}
func NewFuzzer(ctx context.Context, cfg *Config, rnd *rand.Rand,
target *prog.Target) *Fuzzer {
if cfg.NewInputFilter == nil {
cfg.NewInputFilter = func(call string) bool {
return true
}
}
f := &Fuzzer{
Stats: newStats(),
Config: cfg,
Cover: newCover(),
ctx: ctx,
rnd: rnd,
target: target,
// We're okay to lose some of the messages -- if we are already
// regenerating the table, we don't want to repeat it right away.
ctRegenerate: make(chan struct{}),
}
f.execQueues = newExecQueues(f)
f.updateChoiceTable(nil)
go f.choiceTableUpdater()
if cfg.Debug {
go f.logCurrentStats()
}
return f
}
type execQueues struct {
smashQueue *queue.PlainQueue
triageQueue *queue.DynamicOrderer
candidateQueue *queue.PlainQueue
triageCandidateQueue *queue.DynamicOrderer
source queue.Source
}
func newExecQueues(fuzzer *Fuzzer) execQueues {
ret := execQueues{
triageCandidateQueue: queue.DynamicOrder(),
candidateQueue: queue.PlainWithStat(fuzzer.StatCandidates),
triageQueue: queue.DynamicOrder(),
smashQueue: queue.Plain(),
}
// Sources are listed in the order, in which they will be polled.
ret.source = queue.Order(
ret.triageCandidateQueue,
ret.candidateQueue,
ret.triageQueue,
// Alternate smash jobs with exec/fuzz once in 3 times.
queue.Alternate(ret.smashQueue, 3),
queue.Callback(fuzzer.genFuzz),
)
return ret
}
func (fuzzer *Fuzzer) execute(executor queue.Executor, req *queue.Request) *queue.Result {
return fuzzer.executeWithFlags(executor, req, 0)
}
func (fuzzer *Fuzzer) executeWithFlags(executor queue.Executor, req *queue.Request, flags ProgTypes) *queue.Result {
executor.Submit(req)
res := req.Wait(fuzzer.ctx)
fuzzer.processResult(req, res, flags)
return res
}
func (fuzzer *Fuzzer) prepare(req *queue.Request, flags ProgTypes) {
req.OnDone(func(req *queue.Request, res *queue.Result) bool {
fuzzer.processResult(req, res, flags)
return true
})
}
func (fuzzer *Fuzzer) enqueue(executor queue.Executor, req *queue.Request, flags ProgTypes) {
fuzzer.prepare(req, flags)
executor.Submit(req)
}
func (fuzzer *Fuzzer) processResult(req *queue.Request, res *queue.Result, flags ProgTypes) {
inTriage := flags&progInTriage > 0
// Triage individual calls.
// We do it before unblocking the waiting threads because
// it may result it concurrent modification of req.Prog.
// If we are already triaging this exact prog, this is flaky coverage.
if req.ExecOpts.ExecFlags&flatrpc.ExecFlagCollectSignal > 0 && res.Info != nil && !inTriage {
for call, info := range res.Info.Calls {
fuzzer.triageProgCall(req.Prog, &info, call, flags)
}
fuzzer.triageProgCall(req.Prog, &res.Info.Extra, -1, flags)
}
if res.Info != nil {
fuzzer.statExecTime.Add(int(res.Info.Elapsed.Milliseconds()))
}
}
type Config struct {
Debug bool
Corpus *corpus.Corpus
BaseOpts ipc.ExecOpts // Fuzzer will use BaseOpts as a base for all requests.
Logf func(level int, msg string, args ...interface{})
Coverage bool
FaultInjection bool
Comparisons bool
Collide bool
EnabledCalls map[*prog.Syscall]bool
NoMutateCalls map[int]bool
FetchRawCover bool
NewInputFilter func(call string) bool
}
func (fuzzer *Fuzzer) triageProgCall(p *prog.Prog, info *ipc.CallInfo, call int, flags ProgTypes) {
prio := signalPrio(p, info, call)
newMaxSignal := fuzzer.Cover.addRawMaxSignal(info.Signal, prio)
if newMaxSignal.Empty() {
return
}
if !fuzzer.Config.NewInputFilter(p.CallName(call)) {
return
}
fuzzer.Logf(2, "found new signal in call %d in %s", call, p)
queue := fuzzer.triageQueue
if flags&progCandidate > 0 {
queue = fuzzer.triageCandidateQueue
}
fuzzer.startJob(fuzzer.statJobsTriage, &triageJob{
p: p.Clone(),
call: call,
info: *info,
newSignal: newMaxSignal,
flags: flags,
queue: queue.Append(),
})
}
func signalPrio(p *prog.Prog, info *ipc.CallInfo, call int) (prio uint8) {
if call == -1 {
return 0
}
if info.Errno == 0 {
prio |= 1 << 1
}
if !p.Target.CallContainsAny(p.Calls[call]) {
prio |= 1 << 0
}
return
}
func (fuzzer *Fuzzer) genFuzz() *queue.Request {
// Either generate a new input or mutate an existing one.
mutateRate := 0.95
if !fuzzer.Config.Coverage {
// If we don't have real coverage signal, generate programs
// more frequently because fallback signal is weak.
mutateRate = 0.5
}
var req *queue.Request
rnd := fuzzer.rand()
if rnd.Float64() < mutateRate {
req = mutateProgRequest(fuzzer, rnd)
}
if req == nil {
req = genProgRequest(fuzzer, rnd)
}
fuzzer.prepare(req, 0)
return req
}
func (fuzzer *Fuzzer) startJob(stat *stats.Val, newJob job) {
fuzzer.Logf(2, "started %T", newJob)
go func() {
stat.Add(1)
fuzzer.statJobs.Add(1)
newJob.run(fuzzer)
fuzzer.statJobs.Add(-1)
stat.Add(-1)
}()
}
func (fuzzer *Fuzzer) Next() *queue.Request {
req := fuzzer.source.Next()
if req == nil {
// The fuzzer is not supposed to issue nil requests.
panic("nil request from the fuzzer")
}
req.ExecOpts = fuzzer.Config.BaseOpts.MergeFlags(req.ExecOpts)
return req
}
func (fuzzer *Fuzzer) Logf(level int, msg string, args ...interface{}) {
if fuzzer.Config.Logf == nil {
return
}
fuzzer.Config.Logf(level, msg, args...)
}
type Candidate struct {
Prog *prog.Prog
Smashed bool
Minimized bool
}
func (fuzzer *Fuzzer) AddCandidates(candidates []Candidate) {
for _, candidate := range candidates {
req, flags := candidateRequest(fuzzer, candidate)
fuzzer.enqueue(fuzzer.candidateQueue, req, flags)
}
}
func (fuzzer *Fuzzer) rand() *rand.Rand {
fuzzer.mu.Lock()
defer fuzzer.mu.Unlock()
return rand.New(rand.NewSource(fuzzer.rnd.Int63()))
}
func (fuzzer *Fuzzer) updateChoiceTable(programs []*prog.Prog) {
newCt := fuzzer.target.BuildChoiceTable(programs, fuzzer.Config.EnabledCalls)
fuzzer.ctMu.Lock()
defer fuzzer.ctMu.Unlock()
if len(programs) >= fuzzer.ctProgs {
fuzzer.ctProgs = len(programs)
fuzzer.ct = newCt
}
}
func (fuzzer *Fuzzer) choiceTableUpdater() {
for {
select {
case <-fuzzer.ctx.Done():
return
case <-fuzzer.ctRegenerate:
}
fuzzer.updateChoiceTable(fuzzer.Config.Corpus.Programs())
}
}
func (fuzzer *Fuzzer) ChoiceTable() *prog.ChoiceTable {
progs := fuzzer.Config.Corpus.Programs()
fuzzer.ctMu.Lock()
defer fuzzer.ctMu.Unlock()
// There were no deep ideas nor any calculations behind these numbers.
regenerateEveryProgs := 333
if len(progs) < 100 {
regenerateEveryProgs = 33
}
if fuzzer.ctProgs+regenerateEveryProgs < len(progs) {
select {
case fuzzer.ctRegenerate <- struct{}{}:
default:
// We're okay to lose the message.
// It means that we're already regenerating the table.
}
}
return fuzzer.ct
}
func (fuzzer *Fuzzer) logCurrentStats() {
for {
select {
case <-time.After(time.Minute):
case <-fuzzer.ctx.Done():
return
}
var m runtime.MemStats
runtime.ReadMemStats(&m)
str := fmt.Sprintf("running jobs: %d, heap (MB): %d",
fuzzer.statJobs.Val(), m.Alloc/1000/1000)
fuzzer.Logf(0, "%s", str)
}
}
func (fuzzer *Fuzzer) RotateMaxSignal(items int) {
corpusSignal := fuzzer.Config.Corpus.Signal()
pureMaxSignal := fuzzer.Cover.pureMaxSignal(corpusSignal)
if pureMaxSignal.Len() < items {
items = pureMaxSignal.Len()
}
fuzzer.Logf(1, "rotate %d max signal elements", items)
delta := pureMaxSignal.RandomSubset(fuzzer.rand(), items)
fuzzer.Cover.subtract(delta)
}
func setFlags(execFlags flatrpc.ExecFlag) ipc.ExecOpts {
return ipc.ExecOpts{
ExecFlags: execFlags,
}
}