Skip to content

Commit

Permalink
fuzzer: add builtin fuzzing endpoint, fix tests (#31)
Browse files Browse the repository at this point in the history
* fuzzer: add builtin fuzzing endpoint, fix tests

* go.mod: upgrade dependencies

* .github/workflows: use latest go version

* generator: fuzzer: fix build errors
  • Loading branch information
MariusVanDerWijden authored Dec 8, 2023
1 parent 2420238 commit 97c0701
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 1,317 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.19
go-version: 1.21

- name: Build
run: go build -v ./...
Expand Down
4 changes: 2 additions & 2 deletions fuzzer/fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers/logger"
"github.com/ethereum/go-ethereum/tests"
Expand Down Expand Up @@ -128,10 +129,9 @@ func minimizeProgram(test *fuzzing.GstMaker, name string) (*fuzzing.GstMaker, er
}
newOutput := new(bytes.Buffer)
cfg := vm.Config{}
cfg.Debug = true
cfg.Tracer = logger.NewJSONLogger(&logger.Config{}, newOutput)
subtest := gethStateTest.Subtests()[0]
gethStateTest.RunNoVerify(subtest, cfg, false)
gethStateTest.RunNoVerify(subtest, cfg, false, rawdb.HashScheme)
newB := newOutput.Bytes()
newIdx := strings.LastIndex(string(newB), "{")
if newIdx <= 0 {
Expand Down
62 changes: 60 additions & 2 deletions fuzzer/fuzzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,68 @@ package fuzzer

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/MariusVanDerWijden/FuzzyVM/filler"
"github.com/MariusVanDerWijden/FuzzyVM/generator"
"github.com/ethereum/go-ethereum/common"
)

func init() {
outputDir = os.TempDir()
var directories []string
for i := 0; i < 256; i++ {
directories = append(directories, fmt.Sprintf("%v/%v", outputDir, common.Bytes2Hex([]byte{byte(i)})))
}
ensureDirs(directories...)
}

func ensureDirs(dirs ...string) {
for _, dir := range dirs {
_, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("Creating directory: %v\n", dir)
if err = os.Mkdir(dir, 0777); err != nil {
fmt.Printf("Error while making the dir %q: %v\n", dir, err)
return
}
} else {
fmt.Printf("Error while using os.Stat dir %q: %v\n", dir, err)
}
}
}
}

func readCorpus() []string {
defaultDir := "./../corpus/"
entries, err := os.ReadDir(defaultDir)
if err != nil {
fmt.Printf("Error reading corpus directory: %v\n", err)
}
res := make([]string, 0, len(entries))
for _, entry := range entries {
corpus, err := os.ReadFile(filepath.Join(defaultDir, entry.Name()))
if err != nil {
fmt.Printf("Error reading corpus entry: %v\n", err)
}
res = append(res, string(corpus))
}
return res
}

func FuzzVM(f *testing.F) {
corpus := readCorpus()
for _, elem := range corpus {
f.Add([]byte(elem))
}
f.Fuzz(func(t *testing.T, a []byte) {
Fuzz(a)
})
}

func TestFuzzer(t *testing.T) {
data := "asdfasdfasdfasdfasdfasdfasdffasdfasdfasdfasdfasd"
Fuzz([]byte(data))
Expand All @@ -40,7 +96,8 @@ func TestMinimizeProgram(t *testing.T) {
}
// Save the test
test := testMaker.ToGeneralStateTest(name)
storeTest(test, name)
hashed := hash(testMaker.ToGeneralStateTest("hashName"))
storeTest(test, hashed, name)
// minimize
minimized, err := minimizeProgram(testMaker, name)
if err != nil {
Expand All @@ -49,5 +106,6 @@ func TestMinimizeProgram(t *testing.T) {
minTest := minimized.ToGeneralStateTest(name)
_ = minTest
fmt.Printf("%v", minTest)
storeTest(minTest, name+"_min")
minHashed := hash(testMaker.ToGeneralStateTest("hashName"))
storeTest(minTest, minHashed, name+"_min")
}
3 changes: 2 additions & 1 deletion generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

var (
fork = "Shanghai"
fork = "Cancun"
sender = common.HexToAddress("a94f5374fce5edbc8e2a8697c15331677e6ebf0b")
sk = hexutil.MustDecode("0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8")
recursionLevel = 0
Expand Down Expand Up @@ -89,6 +89,7 @@ func createGstMaker(fill *filler.Filler, code []byte) *fuzzing.GstMaker {
GasPrice: big.NewInt(0x80),
To: dest.Hex(),
PrivateKey: sk,
Sender: sender,
}
gst.SetTx(tx)
return gst
Expand Down
84 changes: 71 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,77 @@
module github.com/MariusVanDerWijden/FuzzyVM

go 1.15
go 1.21

toolchain go1.21.5

require (
github.com/ethereum/go-ethereum v1.13.5
github.com/holiman/goevmlab v0.0.0-20231201084119-c73b3c97929c
github.com/korovkin/limiter v0.0.0-20230307205149-3d4b2b34c99d
github.com/urfave/cli/v2 v2.26.0
golang.org/x/crypto v0.16.0
)

require (
github.com/VictoriaMetrics/fastcache v1.12.0 // indirect
github.com/dvyukov/go-fuzz v0.0.0-20220726122315-1d375ef9f9f6 // indirect
github.com/elazarl/go-bindata-assetfs v1.0.1 // indirect
github.com/ethereum/go-ethereum v1.11.5
github.com/getsentry/sentry-go v0.19.0 // indirect
github.com/holiman/goevmlab v0.0.0-20230316064510-98c61355fce0
github.com/korovkin/limiter v0.0.0-20221015170604-22eb1ceceddc
github.com/DataDog/zstd v1.5.5 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/VictoriaMetrics/fastcache v1.12.2 // indirect
github.com/allegro/bigcache v1.2.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.11.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.11.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v0.0.0-20230928194634-aa077af62593 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/deckarep/golang-set/v2 v2.5.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/getsentry/sentry-go v0.25.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/onsi/gomega v1.27.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/stephens2424/writerset v1.0.2 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/urfave/cli/v2 v2.24.4
golang.org/x/crypto v0.6.0
golang.org/x/tools v0.7.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tklauser/go-sysconf v0.3.13 // indirect
github.com/tklauser/numcpus v0.7.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.16.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Loading

0 comments on commit 97c0701

Please sign in to comment.