-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.go
207 lines (180 loc) · 4.43 KB
/
sync.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
package main
import (
"errors"
"fmt"
"math/rand"
"os"
"path"
"sync"
"time"
)
type FuzzerSync struct {
doneCh chan struct{}
config FuzzerConfig
guider Guider
mutator Mutator
logger *Logger
recordPathPrefix string
traceQueue []*Trace
stats *Stats
iteration int
completed map[int]bool
lock *sync.Mutex
rand *rand.Rand
}
func NewFuzzerSync(config FuzzerConfig, logger *Logger) *FuzzerSync {
s := &FuzzerSync{
doneCh: make(chan struct{}),
config: config,
guider: nil,
mutator: nil,
logger: logger,
traceQueue: make([]*Trace, 0),
stats: NewStats(),
iteration: 0,
completed: make(map[int]bool),
lock: new(sync.Mutex),
rand: rand.New(rand.NewSource(time.Now().UnixMilli())),
}
return s
}
func (f *FuzzerSync) Done() chan struct{} {
return f.doneCh
}
func (f *FuzzerSync) UpdateGM(recordPathPrefix string, guider Guider, mutator Mutator) {
f.recordPathPrefix = recordPathPrefix
f.guider = guider
f.mutator = mutator
}
func (f *FuzzerSync) Reset() {
f.lock.Lock()
defer f.lock.Unlock()
f.traceQueue = make([]*Trace, 0)
f.stats = NewStats()
f.iteration = 0
f.completed = make(map[int]bool)
f.doneCh = make(chan struct{})
}
func (f *FuzzerSync) GetStats() *Stats {
f.lock.Lock()
defer f.lock.Unlock()
return f.stats.Copy()
}
func (f *FuzzerSync) Update(iter int, trace *Trace, eventTrace *EventTrace, logs string, err error) {
f.logger.With(LogParams{"iter": iter, "logs": len(logs)}).Debug("Compeleted iteration")
iterS := fmt.Sprintf("%s_%d", f.recordPathPrefix, iter)
if len(logs) != 0 || err != nil {
if err != nil && !errors.Is(err, ErrRedisBug) {
f.logger.With(LogParams{"error": err, "iter": iter}).Info("Error running iteration")
}
f.recordLogs(iterS, logs)
}
if trace != nil && eventTrace != nil {
new, weight := f.guider.Check(iterS, trace, eventTrace, false)
if new {
mutatedTraces := make([]*Trace, 0)
for i := 0; i < weight*f.config.MutationsPerTrace; i++ {
newTrace, ok := f.mutator.Mutate(trace, eventTrace)
if ok {
mutatedTraces = append(mutatedTraces, newTrace.Copy())
}
}
f.lock.Lock()
f.traceQueue = append(f.traceQueue, mutatedTraces...)
f.lock.Unlock()
}
}
f.lock.Lock()
f.stats.AddCoverage(f.guider.Coverage())
f.completed[iter] = true
allDone := true
for i := 0; i < f.config.Iterations; i++ {
if _, ok := f.completed[i]; !ok {
allDone = false
break
}
}
f.lock.Unlock()
if allDone {
close(f.doneCh)
}
}
func (f *FuzzerSync) recordLogs(filePrefix string, logs string) {
filePath := path.Join(f.config.RecordPath, filePrefix+".log")
f.logger.With(LogParams{"file": filePath}).Debug("Writing logs to file")
os.WriteFile(filePath, []byte(logs), 0644)
}
func (f *FuzzerSync) GetTrace() (int, *Trace, bool) {
f.lock.Lock()
defer f.lock.Unlock()
iteration := f.iteration
if iteration == f.config.Iterations {
return 0, nil, false
}
if iteration%f.config.ReseedFrequency == 0 {
f.seed()
}
var trace *Trace = nil
if len(f.traceQueue) > 0 {
trace = f.traceQueue[0]
f.traceQueue = f.traceQueue[1:]
f.stats.MutatedTraces += 1
}
if trace == nil {
trace = f.randomTrace()
f.stats.RandomTraces += 1
}
f.iteration += 1
return iteration, trace, true
}
func (f *FuzzerSync) seed() {
traces := make([]*Trace, f.config.SeedPopulation)
for i := 0; i < f.config.SeedPopulation; i++ {
traces[i] = f.randomTrace().Copy()
}
f.traceQueue = traces
}
func (f *FuzzerSync) randomTrace() *Trace {
trace := NewTrace()
for i := 0; i < f.config.Horizon; i++ {
fromIdx := f.rand.Intn(f.config.NumNodes) + 1
toIdx := f.rand.Intn(f.config.NumNodes) + 1
trace.Add(Choice{
Type: "Node",
Step: i,
From: fromIdx,
To: toIdx,
MaxMessages: f.rand.Intn(f.config.MaxMessages),
})
}
choices := make([]int, f.config.Horizon)
for i := 0; i < f.config.Horizon; i++ {
choices[i] = i
}
for _, c := range sample(choices, f.config.NumCrashes, f.rand) {
idx := f.rand.Intn(f.config.NumNodes) + 1
trace.Add(Choice{
Type: "Crash",
Node: idx,
Step: c,
})
s := sample(intRange(c, f.config.Horizon), 1, f.rand)[0]
trace.Add(Choice{
Type: "Start",
Node: idx,
Step: s,
})
}
for _, req := range sample(choices, f.config.NumRequests, f.rand) {
op := "read"
if f.rand.Intn(2) == 1 {
op = "write"
}
trace.Add(Choice{
Type: "ClientRequest",
Op: op,
Step: req,
})
}
return trace
}