Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MTCannon: Fix AssertEVMReverts to correctly construct data #12200

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cannon/mipsevm/multithreaded/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ func (s *State) EncodeThreadProof() []byte {
out := make([]byte, 0, THREAD_WITNESS_SIZE)
out = append(out, threadBytes[:]...)
out = append(out, otherThreadsWitness[:]...)

return out
}

Expand Down
4 changes: 4 additions & 0 deletions cannon/mipsevm/multithreaded/testutil/mutators.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,7 @@ func (m *StateMutatorMultiThreaded) SetPreimageOffset(val uint32) {
func (m *StateMutatorMultiThreaded) SetStep(val uint64) {
m.state.Step = val
}

func (m *StateMutatorMultiThreaded) SetRegisters(index uint32, val uint32) {
m.state.GetRegistersRef()[index] = val
}
4 changes: 4 additions & 0 deletions cannon/mipsevm/singlethreaded/testutil/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@ func (m *StateMutatorSingleThreaded) SetPreimageOffset(val uint32) {
func (m *StateMutatorSingleThreaded) SetStep(val uint64) {
m.state.Step = val
}

func (m *StateMutatorSingleThreaded) SetRegisters(index uint32, val uint32) {
m.state.GetRegistersRef()[index] = val
}
23 changes: 15 additions & 8 deletions cannon/mipsevm/tests/evm_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,24 +499,31 @@ func TestEVMFault(t *testing.T) {
name string
nextPC uint32
insn uint32
errMsg string
}{
{"illegal instruction", 0, 0xFF_FF_FF_FF},
{"branch in delay-slot", 8, 0x11_02_00_03},
{"jump in delay-slot", 8, 0x0c_00_00_0c},
{"illegal instruction", 0, 0xFF_FF_FF_FF, "invalid instruction"},
{"branch in delay-slot", 8, 0x11_02_00_03, "branch in delay slot"},
{"jump in delay-slot", 8, 0x0c_00_00_0c, "jump in delay slot"},
}

for _, v := range versions {
for _, tt := range cases {
testName := fmt.Sprintf("%v (%v)", tt.name, v.Name)
t.Run(testName, func(t *testing.T) {
goVm := v.VMFactory(nil, os.Stdout, os.Stderr, testutil.CreateLogger(), testutil.WithNextPC(tt.nextPC))
// set the return address ($ra) to jump into when test completes
goVm := v.VMFactory(nil, os.Stdout, os.Stderr, testutil.CreateLogger(), testutil.WithNextPC(tt.nextPC), testutil.WithRegisters(31, testutil.EndAddr))
proofData := v.ThreadProofEncoder(nil, os.Stdout, os.Stderr, testutil.CreateLogger(), testutil.WithNextPC(tt.nextPC), testutil.WithRegisters(31, testutil.EndAddr))
state := goVm.GetState()
state.GetMemory().SetMemory(0, tt.insn)
// set the return address ($ra) to jump into when test completes
state.GetRegistersRef()[31] = testutil.EndAddr

statePCs := []uint32{state.GetPC()}
// TODO: Hardcode here to achieve the expected result.
// Future improvements might involve having the Go VM indicate attempted memory accesses
// and caching this info to generate proofs for the solidity VM.
if tt.errMsg == "invalid instruction" {
statePCs = append(statePCs, 0xa7ef00cc)
}
require.Panics(t, func() { _, _ = goVm.Step(true) })
testutil.AssertEVMReverts(t, state, v.Contracts, tracer)
testutil.AssertEVMReverts(t, state, v.Contracts, tracer, statePCs, proofData, &tt.errMsg)
})
}
}
Expand Down
6 changes: 5 additions & 1 deletion cannon/mipsevm/tests/evm_multithreaded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1179,10 +1179,14 @@ func TestEVM_UnsupportedSyscall(t *testing.T) {
// Setup basic getThreadId syscall instruction
state.Memory.SetMemory(state.GetPC(), syscallInsn)
state.GetRegistersRef()[2] = syscallNum
proofData := state.EncodeThreadProof()

statePCs := []uint32{state.GetPC()}
// Set up post-state expectations
require.Panics(t, func() { _, _ = goVm.Step(true) })
testutil.AssertEVMReverts(t, state, contracts, tracer)

errorMessage := "MIPS2: unimplemented syscall"
testutil.AssertEVMReverts(t, state, contracts, tracer, statePCs, proofData, &errorMessage)
})
}
}
Expand Down
49 changes: 34 additions & 15 deletions cannon/mipsevm/tests/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,50 @@ func multiThreadElfVmFactory(t require.TestingT, elfFile string, po mipsevm.Prei
return fpvm
}

type ThreadProofEncoder func(po mipsevm.PreimageOracle, stdOut, stdErr io.Writer, log log.Logger, opts ...testutil.StateOption) []byte

func singalThreadProofEncoder(po mipsevm.PreimageOracle, stdOut, stdErr io.Writer, log log.Logger, opts ...testutil.StateOption) []byte {
return nil
}

func multiThreadProofEncoder(po mipsevm.PreimageOracle, stdOut, stdErr io.Writer, log log.Logger, opts ...testutil.StateOption) []byte {
state := multithreaded.CreateEmptyState()
mutator := mttestutil.NewStateMutatorMultiThreaded(state)
for _, opt := range opts {
opt(mutator)
}
proofData := state.EncodeThreadProof()
return proofData
}

type VersionedVMTestCase struct {
Name string
Contracts *testutil.ContractMetadata
StateHashFn mipsevm.HashFn
VMFactory VMFactory
ElfVMFactory ElfVMFactory
Name string
Contracts *testutil.ContractMetadata
StateHashFn mipsevm.HashFn
VMFactory VMFactory
ElfVMFactory ElfVMFactory
ThreadProofEncoder ThreadProofEncoder
}

func GetSingleThreadedTestCase(t require.TestingT) VersionedVMTestCase {
return VersionedVMTestCase{
Name: "single-threaded",
Contracts: testutil.TestContractsSetup(t, testutil.MipsSingleThreaded),
StateHashFn: singlethreaded.GetStateHashFn(),
VMFactory: singleThreadedVmFactory,
ElfVMFactory: singleThreadElfVmFactory,
Name: "single-threaded",
Contracts: testutil.TestContractsSetup(t, testutil.MipsSingleThreaded),
StateHashFn: singlethreaded.GetStateHashFn(),
VMFactory: singleThreadedVmFactory,
ElfVMFactory: singleThreadElfVmFactory,
ThreadProofEncoder: singalThreadProofEncoder,
}
}

func GetMultiThreadedTestCase(t require.TestingT) VersionedVMTestCase {
return VersionedVMTestCase{
Name: "multi-threaded",
Contracts: testutil.TestContractsSetup(t, testutil.MipsMultithreaded),
StateHashFn: multithreaded.GetStateHashFn(),
VMFactory: multiThreadedVmFactory,
ElfVMFactory: multiThreadElfVmFactory,
Name: "multi-threaded",
Contracts: testutil.TestContractsSetup(t, testutil.MipsMultithreaded),
StateHashFn: multithreaded.GetStateHashFn(),
VMFactory: multiThreadedVmFactory,
ElfVMFactory: multiThreadElfVmFactory,
ThreadProofEncoder: multiThreadProofEncoder,
}
}

Expand Down
25 changes: 21 additions & 4 deletions cannon/mipsevm/testutil/mips.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/state"
Expand Down Expand Up @@ -181,21 +182,37 @@ func ValidateEVM(t *testing.T, stepWitness *mipsevm.StepWitness, step uint64, go
}

// AssertEVMReverts runs a single evm step from an FPVM prestate and asserts that the VM panics
func AssertEVMReverts(t *testing.T, state mipsevm.FPVMState, contracts *ContractMetadata, tracer *tracing.Hooks) {
insnProof := state.GetMemory().MerkleProof(state.GetPC())
joohhnnn marked this conversation as resolved.
Show resolved Hide resolved
func AssertEVMReverts(t *testing.T, state mipsevm.FPVMState, contracts *ContractMetadata, tracer *tracing.Hooks, statePCs []uint32, ProofData []byte, expectedReason *string) {
for _, pc := range statePCs {
proof := state.GetMemory().MerkleProof(pc)
ProofData = append(ProofData, proof[:]...)
}
encodedWitness, _ := state.EncodeWitness()
stepWitness := &mipsevm.StepWitness{
State: encodedWitness,
ProofData: insnProof[:],
ProofData: ProofData,
}
input := EncodeStepInput(t, stepWitness, mipsevm.LocalContext{}, contracts.Artifacts.MIPS)
startingGas := uint64(30_000_000)

env, evmState := NewEVMEnv(contracts)
env.Config.Tracer = tracer
sender := common.Address{0x13, 0x37}
_, _, err := env.Call(vm.AccountRef(sender), contracts.Addresses.MIPS, input, startingGas, common.U2560)
ret, _, err := env.Call(vm.AccountRef(sender), contracts.Addresses.MIPS, input, startingGas, common.U2560)
require.EqualValues(t, err, vm.ErrExecutionReverted)

if expectedReason != nil {
reason := ""
if len(ret) > 4 { // 4 bytes for the error selector
unpacked, decodeErr := abi.UnpackRevert(ret)
if decodeErr == nil {
reason = unpacked
}
}

require.Equal(t, *expectedReason, reason, "Revert reason mismatch")
}

logs := evmState.Logs()
require.Equal(t, 0, len(logs))
}
Expand Down
7 changes: 7 additions & 0 deletions cannon/mipsevm/testutil/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type StateMutator interface {
SetExited(val bool)
SetStep(val uint64)
SetLastHint(val hexutil.Bytes)
SetRegisters(index uint32, val uint32)
Randomize(randSeed int64)
}

Expand Down Expand Up @@ -103,6 +104,12 @@ func WithRandomization(seed int64) StateOption {
}
}

func WithRegisters(index uint32, val uint32) StateOption {
return func(state StateMutator) {
state.SetRegisters(index, val)
}
}

func AlignPC(pc uint32) uint32 {
// Memory-align random pc and leave room for nextPC
pc = pc & 0xFF_FF_FF_FC // Align address
Expand Down