-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.go
202 lines (187 loc) · 4.86 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
// Copyright 2020 Marius van der Wijden
// This file is part of the fuzzy-vm library.
//
// The fuzzy-vm 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.
//
// The fuzzy-vm 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 fuzzy-vm library. If not, see <http://www.gnu.org/licenses/>.
// Package main creates a fuzzer for Ethereum Virtual Machine (evm) implementations.
package main
import (
"crypto/sha1"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"runtime/pprof"
"time"
"gopkg.in/urfave/cli.v1"
"github.com/MariusVanDerWijden/FuzzyVM/benchmark"
"github.com/MariusVanDerWijden/FuzzyVM/executor"
"github.com/MariusVanDerWijden/FuzzyVM/fuzzer"
"github.com/ethereum/go-ethereum/common"
)
func initApp() *cli.App {
app := cli.NewApp()
app.Name = "FuzzyVM"
app.Author = "Marius van der Wijden"
app.Usage = "Generator for Ethereum Virtual Machine tests"
app.Action = mainLoop
app.Flags = []cli.Flag{
genProcFlag,
maxTestsFlag,
minTestsFlag,
buildFlag,
retestFlag,
execNoGen,
benchFlag,
corpusFlag,
}
return app
}
var (
app = initApp()
dirName = "out"
outDir = "crashes"
)
func main() {
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func mainLoop(c *cli.Context) {
go func() {
time.Sleep(2 * time.Minute)
file, _ := os.Create("heap_dump_fuzzyVM")
pprof.WriteHeapProfile(file)
}()
if c.GlobalBool(buildFlag.Name) {
if err := startBuilder(); err != nil {
panic(err)
}
} else if c.GlobalString(retestFlag.Name) != "" {
retest(c)
} else if c.GlobalBool(execNoGen.Name) {
if err := executor.ExecuteBatch(dirName, outDir); err != nil {
panic(err)
}
} else if c.GlobalInt(benchFlag.Name) != 0 {
benchmark.RunFullBench(c.GlobalInt(benchFlag.Name))
} else if c.GlobalInt(corpusFlag.Name) != 0 {
createCorpus(c.GlobalInt(corpusFlag.Name))
} else {
generatorLoop(c)
}
}
func startBuilder() error {
cmdName := "go-fuzz-build"
cmd := exec.Command(cmdName)
cmd.Dir = "fuzzer"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// We have to disable CGO
cgo := "CGO_ENABLED=0"
env := append(os.Environ(), cgo)
cmd.Env = env
return cmd.Run()
}
func retest(c *cli.Context) {
p := c.GlobalString(retestFlag.Name)
dir := path.Dir(p)
test := path.Base(p)
executor.ExecuteFullTest(dirName, dir, test, false)
}
func generatorLoop(c *cli.Context) {
var (
genProc = c.GlobalString(genProcFlag.Name)
minTests = c.GlobalInt(minTestsFlag.Name)
maxTests = c.GlobalInt(maxTestsFlag.Name)
errChan = make(chan error)
)
for {
fmt.Println("Starting generator")
cmd := startGenerator(genProc)
go func() {
for {
// Sleep a bit to ensure some tests have been generated.
time.Sleep(30 * time.Second)
fmt.Println("Starting executor")
if err := executor.ExecuteBatch(dirName, outDir); err != nil {
errChan <- err
}
errChan <- nil
}
}()
go watcher(cmd, errChan, maxTests)
err := <-errChan
cmd.Process.Signal(os.Kill)
if err != nil {
panic(err)
}
infos, err := ioutil.ReadDir(dirName)
if err != nil {
panic(err)
}
if len(infos) > minTests {
fmt.Println("Tests exceed minTests after execution")
return
}
}
}
func startGenerator(genThreads string) *exec.Cmd {
cmdName := "go-fuzz"
dir := "./fuzzer/fuzzer-fuzz.zip"
cmd := exec.Command(cmdName, "--bin", dir, "--procs", genThreads)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
panic(err)
}
return cmd
}
func watcher(cmd *exec.Cmd, errChan chan error, maxTests int) {
for {
time.Sleep(time.Second * 5)
infos, err := ioutil.ReadDir("out")
if err != nil {
fmt.Printf("Error killing process: %v\n", err)
cmd.Process.Kill()
errChan <- err
}
if len(infos) > maxTests {
fmt.Printf("Max tests exceeded, pausing\n")
cmd.Process.Signal(os.Interrupt)
return
}
}
}
func createCorpus(n int) {
dir := "corpus"
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.Mkdir(dir, 0777); err != nil {
fmt.Printf("Error while making corpus dir: %v\n", err)
}
} else if err != nil {
fmt.Printf("Error while using os.Stat: %v\n", err)
}
for i := 0; i < n; i++ {
elem, err := fuzzer.CreateNewCorpusElement()
if err != nil {
fmt.Printf("Error while creating corpus: %v\n", err)
}
filename := sha1.Sum(elem)
if err := ioutil.WriteFile(common.Bytes2Hex(filename[:]), elem, 0755); err != nil {
fmt.Printf("Error while writing corpus element: %v\n", err)
}
}
}