forked from holiman/goevmlab
/
main.go
217 lines (202 loc) · 5.21 KB
/
main.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
// Copyright 2019 Martin Holst Swende
// This file is part of the go-evmlab library.
//
// The library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the goevmlab library. If not, see <http://www.gnu.org/licenses/>.
package main
import (
json "github.com/json-iterator/go"
"errors"
"fmt"
"gopkg.in/urfave/cli.v1"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"sort"
"github.com/JekaMas/goevmlab/common"
"github.com/JekaMas/goevmlab/fuzzing"
"github.com/JekaMas/goevmlab/ops"
)
func initApp() *cli.App {
app := cli.NewApp()
app.Name = filepath.Base(os.Args[0])
app.Author = "Martin Holst Swende"
app.Usage = "Test-case minimizer"
app.Flags = append(app.Flags, common.VmFlags...)
app.Action = startFuzzer
return app
}
var app = initApp()
func main() {
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
type codeMutator struct {
current []byte
lastGood []byte
}
// proceed tells the mutator to continue one mutation
// returns 'true' is the mutator is exhausted
func (m *codeMutator) proceed() bool {
m.lastGood = m.current
// Now mutate current
var next []byte
max := ops.InstructionCount(m.lastGood)
removed := 0
for removed == 0 {
cutPoint := rand.Intn(max)
next = make([]byte, 0)
it := ops.NewInstructionIterator(m.lastGood)
for it.Next() {
if removed == 0 && it.PC() > uint64(cutPoint) {
// Remove until the stack balances out
delta := 0
for {
removed += 1
delta += it.Op().Stackdelta()
if delta == 0 {
break
}
// Skip next one too
if !it.Next() {
break
}
}
}
next = append(next, byte(it.Op()))
if arg := it.Arg(); arg != nil {
next = append(next, arg...)
}
}
}
fmt.Printf("code length: %d (was %d)\n", len(next), len(m.lastGood))
m.current = next
return len(next) == len(m.lastGood)
}
// undo tells the mutator to revert the last change
func (m *codeMutator) undo() {
m.current = m.lastGood
}
func startFuzzer(c *cli.Context) error {
if c.NArg() != 1 {
return fmt.Errorf("input state test file needed")
}
testPath := c.Args().First()
if consensus, err := common.RootsEqual(testPath, c); err != nil {
return err
} else if consensus {
return errors.New("No consensus failure -- " +
"the input statetest needs to be a test which produces differing stateroot")
}
var (
gst fuzzing.GeneralStateTest
testname string
good = fmt.Sprintf("%v.min", testPath)
out = fmt.Sprintf("%v.%v", testPath, "tmp")
)
if gstPtr, err := fuzzing.FromGeneralStateTest(testPath); err != nil {
return err
} else {
gst = (*gstPtr)
for t := range gst {
testname = t
break
}
}
var identicalRoots = func() bool {
data, _ := json.MarshalIndent(gst, "", " ")
if err := ioutil.WriteFile(out, data, 0777); err != nil {
panic(err)
}
inConsensus, err := common.RootsEqual(out, c)
if err != nil {
panic(err)
}
if !inConsensus {
fmt.Printf("Still ok! (failing testcase) ...\n")
if err := ioutil.WriteFile(good, data, 0777); err != nil {
panic(err)
}
} else {
fmt.Printf("Not ok! (clients in consensus)...\n")
}
return inConsensus
}
// Try decreasing gas
gas := sort.Search(int(gst[testname].Tx.GasLimit[0]), func(i int) bool {
gst[testname].Tx.GasLimit[0] = uint64(i)
fmt.Printf("Testing gas %d\n", i)
return !identicalRoots()
})
// And restore the gas again
gst[testname].Tx.GasLimit[0] = uint64(gas)
// Try removing accounts
for target, acc := range gst[testname].Pre {
delete(gst[testname].Pre, target)
fmt.Printf("Testing dropping %x\n", target)
if !identicalRoots() {
continue
}
fmt.Printf("oops, broke it, restoring %x\n", target)
gst[testname].Pre[target] = acc
}
// Try reducing code
for target, acc := range gst[testname].Pre {
if len(acc.Code) == 0 {
continue
}
fmt.Printf("Target: %x\n", target)
code := acc.Code
m := codeMutator{current: code, lastGood: code}
// Alright, we're in business
fails := 0
for {
if exhausted := m.proceed(); exhausted {
break
}
acc := gst[testname].Pre[target]
acc.Code = m.current
gst[testname].Pre[target] = acc
if !identicalRoots() {
fails = 0
continue
} else {
fmt.Printf("oops, broke it, restoring\n")
fails++
m.undo()
// restore it
acc.Code = m.current
gst[testname].Pre[target] = acc
if fails > 5 {
break
}
}
}
}
fmt.Printf("Reducing slots...\n")
// Try removing storage
for target, acc := range gst[testname].Pre {
for k, v := range acc.Storage {
delete(gst[testname].Pre[target].Storage, k)
fmt.Printf("Testing removing slot %x from %x\n", k, target)
if !identicalRoots() {
continue
}
gst[testname].Pre[target].Storage[k] = v
}
}
return nil
}