56c4f2bfd4
The bulk of this PR is authored by @lightclient , in the original EOF-work. More recently, the code has been picked up and reworked for the new EOF specification, by @MariusVanDerWijden , in https://github.com/ethereum/go-ethereum/pull/29518, and also @shemnon has contributed with fixes. This PR is an attempt to start eating the elephant one small bite at a time, by selecting only the eof-validation as a standalone piece which can be merged without interfering too much in the core stuff. In this PR: - [x] Validation of eof containers, lifted from #29518, along with test-vectors from consensus-tests and fuzzing, to ensure that the move did not lose any functionality. - [x] Definition of eof opcodes, which is a prerequisite for validation - [x] Addition of `undefined` to a jumptable entry item. I'm not super-happy with this, but for the moment it seems the least invasive way to do it. A better way might be to go back and allowing nil-items or nil execute-functions to denote "undefined". - [x] benchmarks of eof validation speed --------- Co-authored-by: lightclient <lightclient@protonmail.com> Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de> Co-authored-by: Danno Ferrin <danno.ferrin@shemnon.com>
167 lines
3.8 KiB
Go
167 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
)
|
|
|
|
func FuzzEofParsing(f *testing.F) {
|
|
// Seed with corpus from execution-spec-tests
|
|
for i := 0; ; i++ {
|
|
fname := fmt.Sprintf("testdata/eof/eof_corpus_%d.txt", i)
|
|
corpus, err := os.Open(fname)
|
|
if err != nil {
|
|
break
|
|
}
|
|
f.Logf("Reading seed data from %v", fname)
|
|
scanner := bufio.NewScanner(corpus)
|
|
scanner.Buffer(make([]byte, 1024), 10*1024*1024)
|
|
for scanner.Scan() {
|
|
s := scanner.Text()
|
|
if len(s) >= 2 && strings.HasPrefix(s, "0x") {
|
|
s = s[2:]
|
|
}
|
|
b, err := hex.DecodeString(s)
|
|
if err != nil {
|
|
panic(err) // rotten corpus
|
|
}
|
|
f.Add(b)
|
|
}
|
|
corpus.Close()
|
|
if err := scanner.Err(); err != nil {
|
|
panic(err) // rotten corpus
|
|
}
|
|
}
|
|
// And do the fuzzing
|
|
f.Fuzz(func(t *testing.T, data []byte) {
|
|
var (
|
|
jt = vm.NewPragueEOFInstructionSetForTesting()
|
|
c vm.Container
|
|
)
|
|
cpy := common.CopyBytes(data)
|
|
if err := c.UnmarshalBinary(data, true); err == nil {
|
|
c.ValidateCode(&jt, true)
|
|
if have := c.MarshalBinary(); !bytes.Equal(have, data) {
|
|
t.Fatal("Unmarshal-> Marshal failure!")
|
|
}
|
|
}
|
|
if err := c.UnmarshalBinary(data, false); err == nil {
|
|
c.ValidateCode(&jt, false)
|
|
if have := c.MarshalBinary(); !bytes.Equal(have, data) {
|
|
t.Fatal("Unmarshal-> Marshal failure!")
|
|
}
|
|
}
|
|
if !bytes.Equal(cpy, data) {
|
|
panic("data modified during unmarshalling")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestEofParseInitcode(t *testing.T) {
|
|
testEofParse(t, true, "testdata/eof/results.initcode.txt")
|
|
}
|
|
|
|
func TestEofParseRegular(t *testing.T) {
|
|
testEofParse(t, false, "testdata/eof/results.regular.txt")
|
|
}
|
|
|
|
func testEofParse(t *testing.T, isInitCode bool, wantFile string) {
|
|
var wantFn func() string
|
|
var wantLoc = 0
|
|
{ // Configure the want-reader
|
|
wants, err := os.Open(wantFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
scanner := bufio.NewScanner(wants)
|
|
scanner.Buffer(make([]byte, 1024), 10*1024*1024)
|
|
wantFn = func() string {
|
|
if scanner.Scan() {
|
|
wantLoc++
|
|
return scanner.Text()
|
|
}
|
|
return "end of file reached"
|
|
}
|
|
}
|
|
|
|
for i := 0; ; i++ {
|
|
fname := fmt.Sprintf("testdata/eof/eof_corpus_%d.txt", i)
|
|
corpus, err := os.Open(fname)
|
|
if err != nil {
|
|
break
|
|
}
|
|
t.Logf("# Reading seed data from %v", fname)
|
|
scanner := bufio.NewScanner(corpus)
|
|
scanner.Buffer(make([]byte, 1024), 10*1024*1024)
|
|
line := 1
|
|
for scanner.Scan() {
|
|
s := scanner.Text()
|
|
if len(s) >= 2 && strings.HasPrefix(s, "0x") {
|
|
s = s[2:]
|
|
}
|
|
b, err := hex.DecodeString(s)
|
|
if err != nil {
|
|
panic(err) // rotten corpus
|
|
}
|
|
have := "OK"
|
|
if _, err := parse(b, isInitCode); err != nil {
|
|
have = fmt.Sprintf("ERR: %v", err)
|
|
}
|
|
if false { // Change this to generate the want-output
|
|
fmt.Printf("%v\n", have)
|
|
} else {
|
|
want := wantFn()
|
|
if have != want {
|
|
if len(want) > 100 {
|
|
want = want[:100]
|
|
}
|
|
if len(b) > 100 {
|
|
b = b[:100]
|
|
}
|
|
t.Errorf("%v:%d\n%v\ninput %x\nisInit: %v\nhave: %q\nwant: %q\n",
|
|
fname, line, fmt.Sprintf("%v:%d", wantFile, wantLoc), b, isInitCode, have, want)
|
|
}
|
|
}
|
|
line++
|
|
}
|
|
corpus.Close()
|
|
}
|
|
}
|
|
|
|
func BenchmarkEofParse(b *testing.B) {
|
|
corpus, err := os.Open("testdata/eof/eof_benches.txt")
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
defer corpus.Close()
|
|
scanner := bufio.NewScanner(corpus)
|
|
scanner.Buffer(make([]byte, 1024), 10*1024*1024)
|
|
line := 1
|
|
for scanner.Scan() {
|
|
s := scanner.Text()
|
|
if len(s) >= 2 && strings.HasPrefix(s, "0x") {
|
|
s = s[2:]
|
|
}
|
|
data, err := hex.DecodeString(s)
|
|
if err != nil {
|
|
b.Fatal(err) // rotten corpus
|
|
}
|
|
b.Run(fmt.Sprintf("test-%d", line), func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
b.SetBytes(int64(len(data)))
|
|
for i := 0; i < b.N; i++ {
|
|
_, _ = parse(data, false)
|
|
}
|
|
})
|
|
line++
|
|
}
|
|
}
|