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
|
// 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"
"sort"
"sync"
"time"
"github.com/google/syzkaller/pkg/corpus"
"github.com/google/syzkaller/pkg/csource"
"github.com/google/syzkaller/pkg/flatrpc"
"github.com/google/syzkaller/pkg/fuzzer/queue"
"github.com/google/syzkaller/pkg/mgrconfig"
"github.com/google/syzkaller/pkg/signal"
"github.com/google/syzkaller/pkg/stat"
"github.com/google/syzkaller/prog"
)
type Fuzzer struct {
Stats
Config *Config
Cover *Cover
ProbCover *Cover
ctx context.Context
mu sync.Mutex
rnd *rand.Rand
target *prog.Target
hintsLimiter prog.HintsLimiter
runningJobs map[jobIntrospector]struct{}
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(target),
Config: cfg,
Cover: newCover(),
ProbCover: new(Cover),
ctx: ctx,
rnd: rnd,
target: target,
runningJobs: map[jobIntrospector]struct{}{},
// 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
}
func (fuzzer *Fuzzer) RecommendedCalls() int {
if fuzzer.Config.ModeKFuzzTest {
return prog.RecommendedCallsKFuzzTest
}
return prog.RecommendedCalls
}
type execQueues struct {
triageCandidateQueue *queue.DynamicOrderer
candidateQueue *queue.PlainQueue
triageQueue *queue.DynamicOrderer
smashQueue *queue.PlainQueue
source queue.Source
}
func newExecQueues(fuzzer *Fuzzer) execQueues {
ret := execQueues{
triageCandidateQueue: queue.DynamicOrder(),
candidateQueue: queue.Plain(),
triageQueue: queue.DynamicOrder(),
smashQueue: queue.Plain(),
}
// Alternate smash jobs with exec/fuzz to spread attention to the wider area.
skipQueue := 3
if fuzzer.Config.PatchTest {
// When we do patch fuzzing, we do not focus on finding and persisting
// new coverage that much, so it's reasonable to spend more time just
// mutating various corpus programs.
skipQueue = 2
}
// Sources are listed in the order, in which they will be polled.
ret.source = queue.Order(
ret.triageCandidateQueue,
ret.candidateQueue,
ret.triageQueue,
queue.Alternate(ret.smashQueue, skipQueue),
queue.Callback(fuzzer.genFuzz),
)
return ret
}
func (fuzzer *Fuzzer) CandidatesToTriage() int {
return fuzzer.statCandidates.Val() + fuzzer.statJobsTriageCandidate.Val()
}
func (fuzzer *Fuzzer) CandidateTriageFinished() bool {
return fuzzer.CandidatesToTriage() == 0
}
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 ProgFlags) *queue.Result {
fuzzer.enqueue(executor, req, flags, 0)
return req.Wait(fuzzer.ctx)
}
func (fuzzer *Fuzzer) prepare(req *queue.Request, flags ProgFlags, attempt int) {
req.OnDone(func(req *queue.Request, res *queue.Result) bool {
return fuzzer.processResult(req, res, flags, attempt)
})
}
func (fuzzer *Fuzzer) enqueue(executor queue.Executor, req *queue.Request, flags ProgFlags, attempt int) {
fuzzer.prepare(req, flags, attempt)
executor.Submit(req)
}
func (fuzzer *Fuzzer) processResult(req *queue.Request, res *queue.Result, flags ProgFlags, attempt int) bool {
// If we are already triaging this exact prog, this is flaky coverage.
// Hanged programs are harmful as they consume executor procs.
dontTriage := flags&progInTriage > 0 || res.Status == queue.Hanged
// Triage the program.
// We do it before unblocking the waiting threads because
// it may result it concurrent modification of req.Prog.
var triage map[int]*triageCall
if req.ExecOpts.ExecFlags&flatrpc.ExecFlagCollectSignal > 0 && res.Info != nil && !dontTriage {
for call, info := range res.Info.Calls {
fuzzer.triageProgCall(req.Prog, info, call, &triage)
}
fuzzer.triageProgCall(req.Prog, res.Info.Extra, -1, &triage)
if len(triage) != 0 {
queue, stat := fuzzer.triageQueue, fuzzer.statJobsTriage
if flags&progCandidate > 0 {
queue, stat = fuzzer.triageCandidateQueue, fuzzer.statJobsTriageCandidate
}
job := &triageJob{
p: req.Prog.Clone(),
executor: res.Executor,
flags: flags,
queue: queue.Append(),
calls: triage,
info: &JobInfo{
Name: req.Prog.String(),
Type: "triage",
},
}
for id := range triage {
job.info.Calls = append(job.info.Calls, job.p.CallName(id))
}
sort.Strings(job.info.Calls)
fuzzer.startJob(stat, job)
}
}
if res.Info != nil {
fuzzer.statExecTime.Add(int(res.Info.Elapsed / 1e6))
for call, info := range res.Info.Calls {
fuzzer.handleCallInfo(req, info, call)
}
fuzzer.handleCallInfo(req, res.Info.Extra, -1)
}
// Corpus candidates may have flaky coverage, so we give them a second chance.
maxCandidateAttempts := 3
if req.Risky() {
// In non-snapshot mode usually we are not sure which exactly input caused the crash,
// so give it one more chance. In snapshot mode we know for sure, so don't retry.
maxCandidateAttempts = 2
if fuzzer.Config.Snapshot || res.Status == queue.Hanged {
maxCandidateAttempts = 0
}
}
if len(triage) == 0 && flags&ProgFromCorpus != 0 && attempt < maxCandidateAttempts {
fuzzer.enqueue(fuzzer.candidateQueue, req, flags, attempt+1)
return false
}
if flags&progCandidate != 0 {
fuzzer.statCandidates.Add(-1)
}
return true
}
type Config struct {
Debug bool
Corpus *corpus.Corpus
Logf func(level int, msg string, args ...any)
Snapshot bool
Coverage bool
FaultInjection bool
Comparisons bool
Collide bool
EnabledCalls map[*prog.Syscall]bool
NoMutateCalls map[int]bool
FetchRawCover bool
NewInputFilter func(call string) bool
PatchTest bool
ModeKFuzzTest bool
}
func (fuzzer *Fuzzer) triageProgCall(p *prog.Prog, info *flatrpc.CallInfo, call int, triage *map[int]*triageCall) {
if info == nil {
return
}
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)
if *triage == nil {
*triage = make(map[int]*triageCall)
}
(*triage)[call] = &triageCall{
errno: info.Error,
newSignal: newMaxSignal,
signals: [deflakeNeedRuns]signal.Signal{signal.FromRaw(info.Signal, prio)},
}
}
func (fuzzer *Fuzzer) handleCallInfo(req *queue.Request, info *flatrpc.CallInfo, call int) {
if info == nil || info.Flags&flatrpc.CallFlagCoverageOverflow == 0 {
return
}
syscallIdx := len(fuzzer.Syscalls) - 1
if call != -1 {
syscallIdx = req.Prog.Calls[call].Meta.ID
}
stat := &fuzzer.Syscalls[syscallIdx]
if req.ExecOpts.ExecFlags&flatrpc.ExecFlagCollectComps != 0 {
stat.CompsOverflows.Add(1)
fuzzer.statCompsOverflows.Add(1)
} else {
stat.CoverOverflows.Add(1)
fuzzer.statCoverOverflows.Add(1)
}
}
func signalPrio(p *prog.Prog, info *flatrpc.CallInfo, call int) (prio uint8) {
if call == -1 {
return 0
}
if info.Error == 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)
}
if fuzzer.Config.Collide && rnd.Intn(3) == 0 {
req = &queue.Request{
Prog: randomCollide(req.Prog, rnd),
Stat: fuzzer.statExecCollide,
}
}
fuzzer.prepare(req, 0, 0)
return req
}
func (fuzzer *Fuzzer) startJob(stat *stat.Val, newJob job) {
fuzzer.Logf(2, "started %T", newJob)
go func() {
stat.Add(1)
defer stat.Add(-1)
fuzzer.statJobs.Add(1)
defer fuzzer.statJobs.Add(-1)
if obj, ok := newJob.(jobIntrospector); ok {
fuzzer.mu.Lock()
fuzzer.runningJobs[obj] = struct{}{}
fuzzer.mu.Unlock()
defer func() {
fuzzer.mu.Lock()
delete(fuzzer.runningJobs, obj)
fuzzer.mu.Unlock()
}()
}
newJob.run(fuzzer)
}()
}
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")
}
return req
}
func (fuzzer *Fuzzer) Logf(level int, msg string, args ...any) {
if fuzzer.Config.Logf == nil {
return
}
fuzzer.Config.Logf(level, msg, args...)
}
type ProgFlags int
const (
// The candidate was loaded from our local corpus rather than come from hub.
ProgFromCorpus ProgFlags = 1 << iota
ProgMinimized
ProgSmashed
progCandidate
progInTriage
)
type Candidate struct {
Prog *prog.Prog
Flags ProgFlags
}
func (fuzzer *Fuzzer) AddCandidates(candidates []Candidate) {
fuzzer.statCandidates.Add(len(candidates))
for _, candidate := range candidates {
req := &queue.Request{
Prog: candidate.Prog,
ExecOpts: setFlags(flatrpc.ExecFlagCollectSignal),
Stat: fuzzer.statExecCandidate,
Important: true,
}
fuzzer.enqueue(fuzzer.candidateQueue, req, candidate.Flags|progCandidate, 0)
}
}
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) RunningJobs() []*JobInfo {
fuzzer.mu.Lock()
defer fuzzer.mu.Unlock()
var ret []*JobInfo
for item := range fuzzer.runningJobs {
ret = append(ret, item.getInfo())
}
return ret
}
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 setFlags(execFlags flatrpc.ExecFlag) flatrpc.ExecOpts {
return flatrpc.ExecOpts{
ExecFlags: execFlags,
}
}
// TODO: This method belongs better to pkg/flatrpc, but we currently end up
// having a cyclic dependency error.
func DefaultExecOpts(cfg *mgrconfig.Config, features flatrpc.Feature, debug bool) flatrpc.ExecOpts {
env := csource.FeaturesToFlags(features, nil)
if debug {
env |= flatrpc.ExecEnvDebug
}
if cfg.Experimental.ResetAccState {
env |= flatrpc.ExecEnvResetState
}
if cfg.Cover {
env |= flatrpc.ExecEnvSignal
}
sandbox, err := flatrpc.SandboxToFlags(cfg.Sandbox)
if err != nil {
panic(fmt.Sprintf("failed to parse sandbox: %v", err))
}
env |= sandbox
exec := flatrpc.ExecFlagThreaded
if !cfg.RawCover {
exec |= flatrpc.ExecFlagDedupCover
}
return flatrpc.ExecOpts{
EnvFlags: env,
ExecFlags: exec,
SandboxArg: cfg.SandboxArg,
}
}
|