-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutator.go
193 lines (160 loc) · 4.01 KB
/
mutator.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
package main
import (
"fmt"
"math/rand"
"time"
)
type Mutator interface {
Mutate(*Trace, *EventTrace) (*Trace, bool)
}
type randomMutator struct{}
func (r *randomMutator) Mutate(_ *Trace, _ *EventTrace) (*Trace, bool) {
return nil, false
}
func RandomMutator() Mutator {
return &randomMutator{}
}
type SwapCrashNodeMutator struct {
NumSwaps int
r *rand.Rand
}
var _ Mutator = &SwapCrashNodeMutator{}
func NewSwapCrashNodeMutator(swaps int) *SwapCrashNodeMutator {
return &SwapCrashNodeMutator{
NumSwaps: swaps,
r: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
func (s *SwapCrashNodeMutator) Mutate(trace *Trace, eventTrace *EventTrace) (*Trace, bool) {
swaps := make(map[int]int)
nodeChoices := make([]int, 0)
for i, ch := range trace.Choices {
if ch.Type == "Crash" {
nodeChoices = append(nodeChoices, i)
}
}
if len(nodeChoices) < s.NumSwaps*2 {
return nil, false
}
for len(swaps) < s.NumSwaps {
sp := sample(nodeChoices, 2, s.r)
swaps[sp[0]] = sp[1]
}
newTrace := trace.Copy()
for i, j := range swaps {
iCh := newTrace.Choices[i]
jCh := newTrace.Choices[j]
iChNew := iCh.Copy()
iChNew.Node = jCh.Node
jChNew := jCh.Copy()
jChNew.Node = iCh.Node
newTrace.Choices[i] = iChNew
newTrace.Choices[j] = jChNew
}
return newTrace, true
}
type SwapNodeMutator struct {
NumSwaps int
rand *rand.Rand
}
var _ Mutator = &SwapNodeMutator{}
func NewSwapNodeMutator(swaps int) *SwapNodeMutator {
return &SwapNodeMutator{
NumSwaps: swaps,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
func (s *SwapNodeMutator) Mutate(trace *Trace, _ *EventTrace) (*Trace, bool) {
nodeChoiceIndices := make([]int, 0)
for i, choice := range trace.Choices {
if choice.Type == "Node" {
nodeChoiceIndices = append(nodeChoiceIndices, i)
}
}
numNodeChoiceIndices := len(nodeChoiceIndices)
if numNodeChoiceIndices == 0 {
return nil, false
}
choices := numNodeChoiceIndices
if s.NumSwaps < choices {
choices = s.NumSwaps
}
toSwap := make(map[string]map[int]int)
for len(toSwap) < choices {
i := nodeChoiceIndices[s.rand.Intn(numNodeChoiceIndices)]
j := nodeChoiceIndices[s.rand.Intn(numNodeChoiceIndices)]
key := fmt.Sprintf("%d_%d", i, j)
if _, ok := toSwap[key]; !ok {
toSwap[key] = map[int]int{i: j}
}
}
newTrace := trace.Copy()
for _, v := range toSwap {
for i, j := range v {
first := newTrace.Choices[i]
second := newTrace.Choices[j]
newTrace.Choices[i] = second.Copy()
newTrace.Choices[j] = first.Copy()
}
}
return newTrace, true
}
type SwapMaxMessagesMutator struct {
NumSwaps int
r *rand.Rand
}
var _ Mutator = &SwapMaxMessagesMutator{}
func NewSwapMaxMessagesMutator(swaps int) *SwapMaxMessagesMutator {
return &SwapMaxMessagesMutator{
NumSwaps: swaps,
r: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
func (s *SwapMaxMessagesMutator) Mutate(trace *Trace, eventTrace *EventTrace) (*Trace, bool) {
swaps := make(map[int]int)
nodeChoices := make([]int, 0)
for i, ch := range trace.Choices {
if ch.Type == "Node" {
nodeChoices = append(nodeChoices, i)
}
}
if len(nodeChoices) < s.NumSwaps {
return nil, false
}
for len(swaps) < s.NumSwaps {
sp := sample(nodeChoices, 2, s.r)
swaps[sp[0]] = sp[1]
}
newTrace := trace.Copy()
for i, j := range swaps {
iCh := newTrace.Choices[i]
jCh := newTrace.Choices[j]
iChNew := iCh.Copy()
iChNew.MaxMessages = jCh.MaxMessages
jChNew := jCh.Copy()
jChNew.MaxMessages = iCh.MaxMessages
newTrace.Choices[i] = iChNew
newTrace.Choices[j] = jChNew
}
return newTrace, true
}
type combinedMutator struct {
mutators []Mutator
}
var _ Mutator = &combinedMutator{}
func (c *combinedMutator) Mutate(trace *Trace, eventTrace *EventTrace) (*Trace, bool) {
curTrace := trace.Copy()
for _, m := range c.mutators {
nextTrace, ok := m.Mutate(curTrace, eventTrace)
if !ok {
return nil, false
}
curTrace = nextTrace
}
return curTrace, true
}
func CombineMutators(mutators ...Mutator) Mutator {
return &combinedMutator{
mutators: mutators,
}
}