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
|
// Copyright 2022 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 main
import (
"fmt"
"io/fs"
"log"
"math/rand"
"os"
"path/filepath"
"regexp"
"sync"
"time"
"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/pkg/tool"
)
// TestbedTarget represents all behavioral differences between specific testbed targets.
type TestbedTarget interface {
NewJob(slotName string, checkouts []*Checkout) (*Checkout, Instance, error)
SaveStatView(view *StatView, dir string) error
SupportsHTMLView(key string) bool
}
type SyzManagerTarget struct {
config *TestbedConfig
nextCheckoutID int
nextInstanceID int
mu sync.Mutex
}
var targetConstructors = map[string]func(cfg *TestbedConfig) TestbedTarget{
"syz-manager": func(cfg *TestbedConfig) TestbedTarget {
return &SyzManagerTarget{
config: cfg,
}
},
"syz-repro": func(cfg *TestbedConfig) TestbedTarget {
inputFiles := []string{}
reproConfig := cfg.ReproConfig
if reproConfig.InputLogs != "" {
err := filepath.WalkDir(reproConfig.InputLogs, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
inputFiles = append(inputFiles, path)
}
return nil
})
if err != nil {
tool.Failf("failed to read logs file directory: %s", err)
}
} else if reproConfig.InputWorkdir != "" {
skipRegexps := []*regexp.Regexp{}
for _, reStr := range reproConfig.SkipBugs {
skipRegexps = append(skipRegexps, regexp.MustCompile(reStr))
}
bugs, err := collectBugs(reproConfig.InputWorkdir)
if err != nil {
tool.Failf("failed to read workdir: %s", err)
}
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for _, bug := range bugs {
skip := false
for _, re := range skipRegexps {
if re.MatchString(bug.Title) {
skip = true
break
}
}
if skip {
continue
}
logs := append([]string{}, bug.Logs...)
for i := 0; i < reproConfig.CrashesPerBug && len(logs) > 0; i++ {
randID := r.Intn(len(logs))
logs[len(logs)-1], logs[randID] = logs[randID], logs[len(logs)-1]
inputFiles = append(inputFiles, logs[len(logs)-1])
logs = logs[:len(logs)-1]
}
}
}
inputs := []*SyzReproInput{}
log.Printf("picked up crash files:")
for _, path := range inputFiles {
log.Printf("- %s", path)
inputs = append(inputs, &SyzReproInput{
Path: path,
runBy: make(map[*Checkout]int),
})
}
if len(inputs) == 0 {
tool.Failf("no inputs given")
}
// TODO: shuffle?
return &SyzReproTarget{
config: cfg,
dedupTitle: make(map[string]int),
inputs: inputs,
}
},
}
func (t *SyzManagerTarget) NewJob(slotName string, checkouts []*Checkout) (*Checkout, Instance, error) {
// Round-robin strategy should suffice.
t.mu.Lock()
checkout := checkouts[t.nextCheckoutID%len(checkouts)]
instanceID := t.nextInstanceID
t.nextCheckoutID++
t.nextInstanceID++
t.mu.Unlock()
uniqName := fmt.Sprintf("%s-%d", checkout.Name, instanceID)
instance, err := t.newSyzManagerInstance(slotName, uniqName, t.config.ManagerMode, checkout)
if err != nil {
return nil, nil, err
}
return checkout, instance, nil
}
func (t *SyzManagerTarget) SupportsHTMLView(key string) bool {
supported := map[string]bool{
HTMLBugsTable: true,
HTMLBugCountsTable: true,
HTMLStatsTable: true,
}
return supported[key]
}
func (t *SyzManagerTarget) SaveStatView(view *StatView, dir string) error {
benchDir := filepath.Join(dir, "benches")
err := osutil.MkdirAll(benchDir)
if err != nil {
return fmt.Errorf("failed to create %s: %w", benchDir, err)
}
tableStats := map[string]func(view *StatView) (*Table, error){
"bugs.csv": (*StatView).GenerateBugTable,
"checkout_stats.csv": (*StatView).StatsTable,
"instance_stats.csv": (*StatView).InstanceStatsTable,
}
for fileName, genFunc := range tableStats {
table, err := genFunc(view)
if err == nil {
table.SaveAsCsv(filepath.Join(dir, fileName))
} else {
log.Printf("stat generation error: %s", err)
}
}
_, err = view.SaveAvgBenches(benchDir)
return err
}
// TODO: consider other repro testing modes.
// E.g. group different logs by title. Then we could also set different sets of inputs
// for each checkout. It can be important if we improve executor logging.
type SyzReproTarget struct {
config *TestbedConfig
inputs []*SyzReproInput
seqID int
dedupTitle map[string]int
mu sync.Mutex
}
type SyzReproInput struct {
Path string
Title string
Skip bool
origTitle string
runBy map[*Checkout]int
}
func (inp *SyzReproInput) QueryTitle(checkout *Checkout, dupsMap map[string]int) error {
data, err := os.ReadFile(inp.Path)
if err != nil {
return fmt.Errorf("failed to read: %w", err)
}
report := checkout.GetReporter().Parse(data)
if report == nil {
return fmt.Errorf("found no crash")
}
if inp.Title == "" {
inp.origTitle = report.Title
inp.Title = report.Title
// Some bug titles may be present in multiple log files.
// Ensure they are all distict to the user.
dupsMap[inp.origTitle]++
if dupsMap[inp.Title] > 1 {
inp.Title += fmt.Sprintf(" (%d)", dupsMap[inp.origTitle])
}
}
return nil
}
func (t *SyzReproTarget) NewJob(slotName string, checkouts []*Checkout) (*Checkout, Instance, error) {
t.mu.Lock()
seqID := t.seqID
checkout := checkouts[t.seqID%len(checkouts)]
t.seqID++
// This may be not the most efficient algorithm, but it guarantees even distribution of
// resources and CPU time is negligible in comparison with the amount of time each instance runs.
var input *SyzReproInput
for _, candidate := range t.inputs {
if candidate.Skip {
continue
}
if candidate.runBy[checkout] == 0 {
// This is the first time we'll attempt to give this log to the checkout.
// Check if it can handle it.
err := candidate.QueryTitle(checkout, t.dedupTitle)
if err != nil {
log.Printf("[log %s]: %s, skipping", candidate.Path, err)
candidate.Skip = true
continue
}
}
if input == nil || input.runBy[checkout] > candidate.runBy[checkout] {
// Pick the least executed one.
input = candidate
}
}
if input == nil {
t.mu.Unlock()
return nil, nil, fmt.Errorf("no available inputs")
}
input.runBy[checkout]++
t.mu.Unlock()
uniqName := fmt.Sprintf("%s-%d", checkout.Name, seqID)
instance, err := t.newSyzReproInstance(slotName, uniqName, input, checkout)
if err != nil {
return nil, nil, err
}
return checkout, instance, nil
}
func (t *SyzReproTarget) SupportsHTMLView(key string) bool {
supported := map[string]bool{
HTMLReprosTable: true,
HTMLCReprosTable: true,
HTMLReproAttemptsTable: true,
HTMLReproDurationTable: true,
}
return supported[key]
}
func (t *SyzReproTarget) SaveStatView(view *StatView, dir string) error {
tableStats := map[string]func(view *StatView) (*Table, error){
"repro_success.csv": (*StatView).GenerateReproSuccessTable,
"crepros_success.csv": (*StatView).GenerateCReproSuccessTable,
"repro_attempts.csv": (*StatView).GenerateReproAttemptsTable,
"repro_duration.csv": (*StatView).GenerateReproDurationTable,
}
for fileName, genFunc := range tableStats {
table, err := genFunc(view)
if err == nil {
table.SaveAsCsv(filepath.Join(dir, fileName))
} else {
log.Printf("stat generation error: %s", err)
}
}
return nil
}
|