/
almostallpermutations.go
223 lines (168 loc) · 5.7 KB
/
almostallpermutations.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
package strategy
import (
"github.com/zimmski/container/list/linkedlist"
"github.com/zimmski/tavor/log"
"github.com/zimmski/tavor/rand"
"github.com/zimmski/tavor/token"
)
type almostAllPermutationsLevel struct {
token token.Token
permutation uint
}
// AlmostAllPermutationsStrategy implements a fuzzing strategy that generates "almost" all possible permutations of a token graph.
// Every iteration of the strategy generates a new permutation. The generation is deterministic. This strategy does not cover all repititional permutations which can be helpful when less permutations are needed but a almost complete permutation coverage is still needed. For example the definition +2(?(1)?(2)) does not result in 16 permutations but instead it results in only 7.
type AlmostAllPermutationsStrategy struct {
root token.Token
resetedLookup map[token.Token]uint
}
// NewAlmostAllPermutationsStrategy returns a new instance of the Almost All Permutations fuzzing strategy
func NewAlmostAllPermutationsStrategy(tok token.Token) *AlmostAllPermutationsStrategy {
s := &AlmostAllPermutationsStrategy{
root: tok,
resetedLookup: make(map[token.Token]uint),
}
return s
}
func init() {
Register("AlmostAllPermutations", func(tok token.Token) Strategy {
return NewAlmostAllPermutationsStrategy(tok)
})
}
func (s *AlmostAllPermutationsStrategy) getLevel(root token.Token, fromChildren bool) []almostAllPermutationsLevel {
var level []almostAllPermutationsLevel
var queue = linkedlist.New()
if fromChildren {
switch t := root.(type) {
case token.ForwardToken:
if v := t.Get(); v != nil {
queue.Push(v)
}
case token.ListToken:
l := t.Len()
for i := 0; i < l; i++ {
c, _ := t.Get(i)
queue.Push(c)
}
}
} else {
queue.Push(root)
}
for !queue.Empty() {
v, _ := queue.Shift()
tok, _ := v.(token.Token)
s.setTokenPermutation(tok, 1)
level = append(level, almostAllPermutationsLevel{
token: tok,
permutation: 1,
})
}
return level
}
// 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 *AlmostAllPermutationsStrategy) 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{})
s.resetedLookup = make(map[token.Token]uint)
go func() {
log.Debug("start almost all permutations routine")
level := s.getLevel(s.root, false)
if len(level) > 0 {
log.Debug("start fuzzing step")
if !s.fuzz(continueFuzzing, level) {
return
}
}
token.ResetScope(s.root)
token.ResetResetTokens(s.root)
log.Debug("done with fuzzing step")
// done with the last fuzzing step
continueFuzzing <- struct{}{}
log.Debug("finished fuzzing. Wait till the outside is ready to close.")
if _, ok := <-continueFuzzing; ok {
log.Debug("close fuzzing channel")
close(continueFuzzing)
}
}()
return continueFuzzing, nil
}
func (s *AlmostAllPermutationsStrategy) setTokenPermutation(tok token.Token, permutation uint) {
if per, ok := s.resetedLookup[tok]; ok && per == permutation {
// Permutation already set in this step
} else {
log.Debugf("set %#v(%p) to permutation %d", tok, tok, permutation)
if err := tok.Permutation(permutation); err != nil {
panic(err)
}
s.resetedLookup[tok] = permutation
}
}
func (s *AlmostAllPermutationsStrategy) fuzz(continueFuzzing chan struct{}, level []almostAllPermutationsLevel) bool {
log.Debugf("fuzzing level %d->%#v", len(level), level)
last := len(level) - 1
STEP:
for {
for i := range level {
if level[i].permutation > level[i].token.Permutations() {
if i <= last {
log.Debugf("max reached redo everything <= %d and increment next", i)
level[i+1].permutation++
if level[i+1].permutation <= level[i+1].token.Permutations() {
s.setTokenPermutation(level[i+1].token, level[i+1].permutation)
}
s.getLevel(level[i+1].token, true) // set all children to permutation 1
}
for k := 0; k <= i; k++ {
level[k].permutation = 1
s.setTokenPermutation(level[k].token, 1)
s.getLevel(level[k].token, true) // set all children to permutation 1
}
continue STEP
}
log.Debugf("permute %d->%#v", i, level[i])
s.setTokenPermutation(level[i].token, level[i].permutation)
if t, ok := level[i].token.(token.OptionalToken); !ok || !t.IsOptional() || level[i].permutation > 1 {
children := s.getLevel(level[i].token, true) // set all children to permutation 1
if len(children) > 0 {
if !s.fuzz(continueFuzzing, children) {
return false
}
}
}
if i == 0 {
level[i].permutation++
}
}
if level[0].permutation > level[0].token.Permutations() {
found := false
for i := 1; i < len(level); i++ {
if level[i].permutation < level[i].token.Permutations() {
found = true
break
}
}
if !found {
log.Debug("done with fuzzing this level")
break STEP
}
}
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")
s.resetedLookup = make(map[token.Token]uint)
}
return true
}