-
Notifications
You must be signed in to change notification settings - Fork 10
/
allpermutations.go
245 lines (184 loc) · 6.04 KB
/
allpermutations.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
package strategy
import (
"github.com/zimmski/tavor/log"
"github.com/zimmski/tavor/rand"
"github.com/zimmski/tavor/token"
)
type allPermutationsLevel struct {
token token.Token
permutation uint
children []allPermutationsLevel
}
// AllPermutationsStrategy implements a fuzzing strategy that generates all possible permutations of a token graph.
// Every iteration of the strategy generates a new permutation. The generation is deterministic. Since this strategy really produces every possible permutation of a token graph, it is advised to only use the strategy on graphs with few states since the state explosion problem manifests itself quite fast.
type AllPermutationsStrategy struct {
root token.Token
}
// NewAllPermutationsStrategy returns a new instance of the All Permutations fuzzing strategy
func NewAllPermutationsStrategy(tok token.Token) *AllPermutationsStrategy {
s := &AllPermutationsStrategy{
root: tok,
}
return s
}
func init() {
Register("AllPermutations", func(tok token.Token) Strategy {
return NewAllPermutationsStrategy(tok)
})
}
func (s *AllPermutationsStrategy) getTree(root token.Token, fromChildren bool) []allPermutationsLevel {
var tree []allPermutationsLevel
add := func(tok token.Token) {
s.setPermutation(tok, 1)
tree = append(tree, allPermutationsLevel{
token: tok,
permutation: 1,
children: s.getTree(tok, true),
})
}
if fromChildren {
switch t := root.(type) {
case token.ForwardToken:
if v := t.Get(); v != nil {
add(v)
}
case token.ListToken:
for i := 0; i < t.Len(); i++ {
c, _ := t.Get(i)
add(c)
}
}
} else {
add(root)
}
return tree
}
func (s *AllPermutationsStrategy) setPermutation(tok token.Token, permutation uint) {
log.Debugf("set %#v(%p) to permutation %d", tok, tok, permutation)
if err := tok.Permutation(permutation); err != nil {
panic(err)
}
}
// Fuzz starts the first iteration of the fuzzing strategy returning a channel which controls the iteration flow.
// The channel returns a value if the iteration is complete and waits with calculating the next iteration until a value is put in. The channel is automatically closed when there are no more iterations. The error return argument is not nil if an error occurs during the setup of the fuzzing strategy.
func (s *AllPermutationsStrategy) Fuzz(r rand.Rand) (chan struct{}, error) {
if token.LoopExists(s.root) {
return nil, &Error{
Message: "found endless loop in graph. Cannot proceed.",
Type: ErrorEndlessLoopDetected,
}
}
continueFuzzing := make(chan struct{})
go func() {
log.Debug("start all permutations routine")
tree := s.getTree(s.root, false)
log.Debug("start fuzzing step")
if contin, _ := s.fuzz(continueFuzzing, tree, false); !contin {
return
}
log.Debug("finished fuzzing.")
close(continueFuzzing)
}()
return continueFuzzing, nil
}
func (s *AllPermutationsStrategy) fuzz(continueFuzzing chan struct{}, tree []allPermutationsLevel, justastep bool) (bool, bool) {
log.Debugf("fuzzing level %d->%#v", len(tree), tree)
STEP:
for {
if justastep && len(tree[0].children) > 0 {
log.Debugf("STEP FURTHER INTO")
if contin, step := s.fuzz(continueFuzzing, tree[0].children, justastep); !contin {
return false, false
} else if step {
log.Debugf("CONTINUE after child step")
return true, true
}
log.Debugf("PERMUTATE after child step")
} else {
log.Debugf("permute %d->%#v", 0, tree[0])
if tree[0].permutation > 1 {
s.setPermutation(tree[0].token, tree[0].permutation)
tree[0].children = s.getTree(tree[0].token, true)
}
if len(tree[0].children) > 0 {
if contin, step := s.fuzz(continueFuzzing, tree[0].children, justastep); !contin {
return false, false
} else if step {
log.Debugf("CONTINUE after child step")
return true, true
}
} else {
if !justastep && (tree[0].token != s.root || tree[0].permutation <= tree[0].token.Permutations()) && !s.nextStep(continueFuzzing) {
return false, false
}
}
}
tree[0].permutation++
if tree[0].permutation > tree[0].token.Permutations() {
for i := 0; i < len(tree); i++ {
log.Debugf("check %d vs %d for %#v", tree[i].permutation, tree[i].token.Permutations(), tree[i])
}
i := 0
for {
if i == len(tree)-1 {
log.Debugf("done with fuzzing this level because %#v", tree)
break STEP
}
i++
if len(tree[i].children) > 0 {
log.Debugf("CHECK children %#v", tree[i])
if contin, step := s.fuzz(continueFuzzing, tree[i].children, true); !contin {
return false, false
} else if step {
for j := 0; j < i; j++ {
tree[j].permutation = 1
s.setPermutation(tree[j].token, tree[j].permutation)
tree[j].children = s.getTree(tree[j].token, true)
}
if justastep {
return true, true
}
log.Debugf("STEP continue")
continue STEP
}
log.Debugf("PERMUTATE continue")
}
tree[i].permutation++
if tree[i].permutation <= tree[i].token.Permutations() {
for j := 0; j < i; j++ {
tree[j].permutation = 1
s.setPermutation(tree[j].token, tree[j].permutation)
tree[j].children = s.getTree(tree[j].token, true)
}
log.Debugf("permute %d->%#v", i, tree[i])
s.setPermutation(tree[i].token, tree[i].permutation)
tree[i].children = s.getTree(tree[i].token, true)
if justastep {
return true, true
}
continue STEP
}
}
} else if justastep {
s.setPermutation(tree[0].token, tree[0].permutation)
tree[0].children = s.getTree(tree[0].token, true)
log.Debugf("CONTINUE after permutate")
return true, true
}
}
return true, false
}
func (s *AllPermutationsStrategy) nextStep(continueFuzzing chan struct{}) bool {
token.ResetScope(s.root)
token.ResetResetTokens(s.root)
log.Debug("done with fuzzing step")
// done with this fuzzing step
continueFuzzing <- struct{}{}
// wait until we are allowed to continue
if _, ok := <-continueFuzzing; !ok {
log.Debug("fuzzing channel closed from outside")
return false
}
log.Debug("start fuzzing step")
return true
}