Skip to content

Commit

Permalink
cannon: Rename FPVMState registers getter to follow convention
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaxter committed Aug 16, 2024
1 parent f047b2c commit 801dd48
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 60 deletions.
4 changes: 2 additions & 2 deletions cannon/mipsevm/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type FPVMState interface {
// GetCpu returns the currently active cpu scalars, including the program counter
GetCpu() CpuScalars

// GetRegistersMutable returns a pointer to the currently active registers
GetRegistersMutable() *[32]uint32
// GetRegistersRef returns a pointer to the currently active registers
GetRegistersRef() *[32]uint32

// GetStep returns the current VM step
GetStep() uint64
Expand Down
4 changes: 2 additions & 2 deletions cannon/mipsevm/multithreaded/mips.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func (m *InstrumentedState) handleSyscall() error {
thread := m.state.GetCurrentThread()

syscallNum, a0, a1, a2, a3 := exec.GetSyscallArgs(m.state.GetRegistersMutable())
syscallNum, a0, a1, a2, a3 := exec.GetSyscallArgs(m.state.GetRegistersRef())
v0 := uint32(0)
v1 := uint32(0)

Expand Down Expand Up @@ -264,7 +264,7 @@ func (m *InstrumentedState) mipsStep() error {
}

// Exec the rest of the step logic
return exec.ExecMipsCoreStepLogic(m.state.getCpuMutable(), m.state.GetRegistersMutable(), m.state.Memory, insn, opcode, fun, m.memoryTracker, m.stackTracker)
return exec.ExecMipsCoreStepLogic(m.state.getCpuMutable(), m.state.GetRegistersRef(), m.state.Memory, insn, opcode, fun, m.memoryTracker, m.stackTracker)
}

func (m *InstrumentedState) onWaitComplete(thread *ThreadState, isTimedOut bool) {
Expand Down
2 changes: 1 addition & 1 deletion cannon/mipsevm/multithreaded/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (s *State) getCpuMutable() *mipsevm.CpuScalars {
return &s.GetCurrentThread().Cpu
}

func (s *State) GetRegistersMutable() *[32]uint32 {
func (s *State) GetRegistersRef() *[32]uint32 {
activeThread := s.GetCurrentThread()
return &activeThread.Registers
}
Expand Down
2 changes: 1 addition & 1 deletion cannon/mipsevm/program/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func PatchStack(st mipsevm.FPVMState) error {
if err := st.GetMemory().SetMemoryRange(sp-4*memory.PageSize, bytes.NewReader(make([]byte, 5*memory.PageSize))); err != nil {
return fmt.Errorf("failed to allocate page for stack content")
}
st.GetRegistersMutable()[29] = sp
st.GetRegistersRef()[29] = sp

storeMem := func(addr uint32, v uint32) {
var dat [4]byte
Expand Down
2 changes: 1 addition & 1 deletion cannon/mipsevm/singlethreaded/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (s *State) GetPC() uint32 { return s.Cpu.PC }

func (s *State) GetCpu() mipsevm.CpuScalars { return s.Cpu }

func (s *State) GetRegistersMutable() *[32]uint32 { return &s.Registers }
func (s *State) GetRegistersRef() *[32]uint32 { return &s.Registers }

func (s *State) GetExitCode() uint8 { return s.ExitCode }

Expand Down
22 changes: 11 additions & 11 deletions cannon/mipsevm/tests/evm_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestEVM(t *testing.T) {
require.NoError(t, err, "load program into state")

// set the return address ($ra) to jump into when test completes
state.GetRegistersMutable()[31] = testutil.EndAddr
state.GetRegistersRef()[31] = testutil.EndAddr

// Catch panics and check if they are expected
defer func() {
Expand Down Expand Up @@ -192,10 +192,10 @@ func TestEVM_MMap(t *testing.T) {
state := goVm.GetState()

state.GetMemory().SetMemory(state.GetPC(), syscallInsn)
*state.GetRegistersMutable() = testutil.RandomRegisters(77)
state.GetRegistersMutable()[2] = exec.SysMmap
state.GetRegistersMutable()[4] = c.address
state.GetRegistersMutable()[5] = c.size
*state.GetRegistersRef() = testutil.RandomRegisters(77)
state.GetRegistersRef()[2] = exec.SysMmap
state.GetRegistersRef()[4] = c.address
state.GetRegistersRef()[5] = c.size
step := state.GetStep()

expectedRegisters := testutil.CopyRegisters(state)
Expand All @@ -221,7 +221,7 @@ func TestEVM_MMap(t *testing.T) {
// Check expectations
require.Equal(t, step+1, state.GetStep())
require.Equal(t, expectedHeap, state.GetHeap())
require.Equal(t, expectedRegisters, state.GetRegistersMutable())
require.Equal(t, expectedRegisters, state.GetRegistersRef())
require.Equal(t, expectedMemoryRoot, state.GetMemory().MerkleRoot())
require.Equal(t, common.Hash{}, state.GetPreimageKey())
require.Equal(t, uint32(0), state.GetPreimageOffset())
Expand Down Expand Up @@ -401,10 +401,10 @@ func TestEVMSysWriteHint(t *testing.T) {
oracle := hintTrackingOracle{}
goVm := v.VMFactory(&oracle, os.Stdout, os.Stderr, testutil.CreateLogger(), WithLastHint(tt.lastHint))
state := goVm.GetState()
state.GetRegistersMutable()[2] = exec.SysWrite
state.GetRegistersMutable()[4] = exec.FdHintWrite
state.GetRegistersMutable()[5] = uint32(tt.memOffset)
state.GetRegistersMutable()[6] = uint32(tt.bytesToWrite)
state.GetRegistersRef()[2] = exec.SysWrite
state.GetRegistersRef()[4] = exec.FdHintWrite
state.GetRegistersRef()[5] = uint32(tt.memOffset)
state.GetRegistersRef()[6] = uint32(tt.bytesToWrite)

err := state.GetMemory().SetMemoryRange(uint32(tt.memOffset), bytes.NewReader(tt.hintData))
require.NoError(t, err)
Expand Down Expand Up @@ -454,7 +454,7 @@ func TestEVMFault(t *testing.T) {
state := goVm.GetState()
state.GetMemory().SetMemory(0, tt.insn)
// set the return address ($ra) to jump into when test completes
state.GetRegistersMutable()[31] = testutil.EndAddr
state.GetRegistersRef()[31] = testutil.EndAddr

require.Panics(t, func() { _, _ = goVm.Step(true) })

Expand Down
4 changes: 2 additions & 2 deletions cannon/mipsevm/tests/evm_multithreaded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestEVM_CloneFlags(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
state := multithreaded.CreateEmptyState()
state.Memory.SetMemory(state.GetPC(), insn)
state.GetRegistersMutable()[2] = exec.SysClone // Set syscall number
state.GetRegistersMutable()[4] = tt.flags // Set first argument
state.GetRegistersRef()[2] = exec.SysClone // Set syscall number
state.GetRegistersRef()[4] = tt.flags // Set first argument
curStep := state.Step

var err error
Expand Down
68 changes: 34 additions & 34 deletions cannon/mipsevm/tests/fuzz_evm_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func FuzzStateSyscallBrk(f *testing.F) {
goVm := v.VMFactory(nil, os.Stdout, os.Stderr, testutil.CreateLogger(),
WithPC(pc), WithNextPC(nextPC), WithStep(step), WithPreimageOffset(preimageOffset))
state := goVm.GetState()
state.GetRegistersMutable()[2] = exec.SysBrk
state.GetRegistersRef()[2] = exec.SysBrk
state.GetMemory().SetMemory(pc, syscallInsn)

preStateRoot := state.GetMemory().MerkleRoot()
Expand All @@ -50,7 +50,7 @@ func FuzzStateSyscallBrk(f *testing.F) {
require.Equal(t, uint8(0), state.GetExitCode())
require.Equal(t, false, state.GetExited())
require.Equal(t, preStateRoot, state.GetMemory().MerkleRoot())
require.Equal(t, expectedRegisters, state.GetRegistersMutable())
require.Equal(t, expectedRegisters, state.GetRegistersRef())
require.Equal(t, step+1, state.GetStep())
require.Equal(t, common.Hash{}, state.GetPreimageKey())
require.Equal(t, preimageOffset, state.GetPreimageOffset())
Expand Down Expand Up @@ -80,10 +80,10 @@ func FuzzStateSyscallMmap(f *testing.F) {
goVm := v.VMFactory(nil, os.Stdout, os.Stderr, testutil.CreateLogger(),
WithStep(step), WithHeap(heap))
state := goVm.GetState()
*state.GetRegistersMutable() = testutil.RandomRegisters(seed)
state.GetRegistersMutable()[2] = exec.SysMmap
state.GetRegistersMutable()[4] = addr
state.GetRegistersMutable()[5] = siz
*state.GetRegistersRef() = testutil.RandomRegisters(seed)
state.GetRegistersRef()[2] = exec.SysMmap
state.GetRegistersRef()[4] = addr
state.GetRegistersRef()[5] = siz
state.GetMemory().SetMemory(0, syscallInsn)

preStateRoot := state.GetMemory().MerkleRoot()
Expand Down Expand Up @@ -127,7 +127,7 @@ func FuzzStateSyscallMmap(f *testing.F) {
require.Equal(t, expectedHeap, state.GetHeap())
require.Equal(t, uint8(0), state.GetExitCode())
require.Equal(t, false, state.GetExited())
require.Equal(t, expectedRegisters, state.GetRegistersMutable())
require.Equal(t, expectedRegisters, state.GetRegistersRef())

evm := testutil.NewMIPSEVM(v.Contracts)
evmPost := evm.Step(t, stepWitness, step, v.StateHashFn)
Expand All @@ -149,8 +149,8 @@ func FuzzStateSyscallExitGroup(f *testing.F) {
goVm := v.VMFactory(nil, os.Stdout, os.Stderr, testutil.CreateLogger(),
WithPC(pc), WithNextPC(nextPC), WithStep(step))
state := goVm.GetState()
state.GetRegistersMutable()[2] = exec.SysExitGroup
state.GetRegistersMutable()[4] = uint32(exitCode)
state.GetRegistersRef()[2] = exec.SysExitGroup
state.GetRegistersRef()[4] = uint32(exitCode)
state.GetMemory().SetMemory(pc, syscallInsn)

preStateRoot := state.GetMemory().MerkleRoot()
Expand All @@ -168,7 +168,7 @@ func FuzzStateSyscallExitGroup(f *testing.F) {
require.Equal(t, uint8(exitCode), state.GetExitCode())
require.Equal(t, true, state.GetExited())
require.Equal(t, preStateRoot, state.GetMemory().MerkleRoot())
require.Equal(t, preStateRegisters, state.GetRegistersMutable())
require.Equal(t, preStateRegisters, state.GetRegistersRef())
require.Equal(t, step+1, state.GetStep())
require.Equal(t, common.Hash{}, state.GetPreimageKey())
require.Equal(t, uint32(0), state.GetPreimageOffset())
Expand All @@ -192,9 +192,9 @@ func FuzzStateSyscallFcntl(f *testing.F) {
goVm := v.VMFactory(nil, os.Stdout, os.Stderr, testutil.CreateLogger(),
WithStep(step))
state := goVm.GetState()
state.GetRegistersMutable()[2] = exec.SysFcntl
state.GetRegistersMutable()[4] = fd
state.GetRegistersMutable()[5] = cmd
state.GetRegistersRef()[2] = exec.SysFcntl
state.GetRegistersRef()[4] = fd
state.GetRegistersRef()[5] = cmd
state.GetMemory().SetMemory(0, syscallInsn)

preStateRoot := state.GetMemory().MerkleRoot()
Expand Down Expand Up @@ -226,12 +226,12 @@ func FuzzStateSyscallFcntl(f *testing.F) {
expectedRegisters[2] = 0xFF_FF_FF_FF
expectedRegisters[7] = exec.MipsEBADF
}
require.Equal(t, expectedRegisters, state.GetRegistersMutable())
require.Equal(t, expectedRegisters, state.GetRegistersRef())
} else {
expectedRegisters := preStateRegisters
expectedRegisters[2] = 0xFF_FF_FF_FF
expectedRegisters[7] = exec.MipsEINVAL
require.Equal(t, expectedRegisters, state.GetRegistersMutable())
require.Equal(t, expectedRegisters, state.GetRegistersRef())
}

evm := testutil.NewMIPSEVM(v.Contracts)
Expand All @@ -257,10 +257,10 @@ func FuzzStateHintRead(f *testing.F) {
goVm := v.VMFactory(oracle, os.Stdout, os.Stderr, testutil.CreateLogger(),
WithStep(step), WithPreimageKey(preimageKey))
state := goVm.GetState()
state.GetRegistersMutable()[2] = exec.SysRead
state.GetRegistersMutable()[4] = exec.FdHintRead
state.GetRegistersMutable()[5] = addr
state.GetRegistersMutable()[6] = count
state.GetRegistersRef()[2] = exec.SysRead
state.GetRegistersRef()[4] = exec.FdHintRead
state.GetRegistersRef()[5] = addr
state.GetRegistersRef()[6] = count
state.GetMemory().SetMemory(0, syscallInsn)

preStatePreimageKey := state.GetPreimageKey()
Expand All @@ -282,7 +282,7 @@ func FuzzStateHintRead(f *testing.F) {
require.Equal(t, preStateRoot, state.GetMemory().MerkleRoot())
require.Equal(t, uint64(1), state.GetStep())
require.Equal(t, preStatePreimageKey, state.GetPreimageKey())
require.Equal(t, expectedRegisters, state.GetRegistersMutable())
require.Equal(t, expectedRegisters, state.GetRegistersRef())

evm := testutil.NewMIPSEVM(v.Contracts)
evmPost := evm.Step(t, stepWitness, step, v.StateHashFn)
Expand Down Expand Up @@ -310,10 +310,10 @@ func FuzzStatePreimageRead(f *testing.F) {
goVm := v.VMFactory(oracle, os.Stdout, os.Stderr, testutil.CreateLogger(),
WithStep(step), WithPreimageKey(preimageKey), WithPreimageOffset(preimageOffset))
state := goVm.GetState()
state.GetRegistersMutable()[2] = exec.SysRead
state.GetRegistersMutable()[4] = exec.FdPreimageRead
state.GetRegistersMutable()[5] = addr
state.GetRegistersMutable()[6] = count
state.GetRegistersRef()[2] = exec.SysRead
state.GetRegistersRef()[4] = exec.FdPreimageRead
state.GetRegistersRef()[5] = addr
state.GetRegistersRef()[6] = count
state.GetMemory().SetMemory(0, syscallInsn)

preStatePreimageKey := state.GetPreimageKey()
Expand Down Expand Up @@ -371,10 +371,10 @@ func FuzzStateHintWrite(f *testing.F) {
goVm := v.VMFactory(oracle, os.Stdout, os.Stderr, testutil.CreateLogger(),
WithStep(step), WithPreimageKey(preimageKey))
state := goVm.GetState()
state.GetRegistersMutable()[2] = exec.SysWrite
state.GetRegistersMutable()[4] = exec.FdHintWrite
state.GetRegistersMutable()[5] = addr
state.GetRegistersMutable()[6] = count
state.GetRegistersRef()[2] = exec.SysWrite
state.GetRegistersRef()[4] = exec.FdHintWrite
state.GetRegistersRef()[5] = addr
state.GetRegistersRef()[6] = count

// Set random data at the target memory range
randBytes, err := randomBytes(randSeed, count)
Expand Down Expand Up @@ -403,7 +403,7 @@ func FuzzStateHintWrite(f *testing.F) {
require.Equal(t, preStateRoot, state.GetMemory().MerkleRoot())
require.Equal(t, uint64(1), state.GetStep())
require.Equal(t, preStatePreimageKey, state.GetPreimageKey())
require.Equal(t, expectedRegisters, state.GetRegistersMutable())
require.Equal(t, expectedRegisters, state.GetRegistersRef())

evm := testutil.NewMIPSEVM(v.Contracts)
evmPost := evm.Step(t, stepWitness, step, v.StateHashFn)
Expand All @@ -428,10 +428,10 @@ func FuzzStatePreimageWrite(f *testing.F) {
goVm := v.VMFactory(oracle, os.Stdout, os.Stderr, testutil.CreateLogger(),
WithStep(step), WithPreimageKey(preimageKey), WithPreimageOffset(128))
state := goVm.GetState()
state.GetRegistersMutable()[2] = exec.SysWrite
state.GetRegistersMutable()[4] = exec.FdPreimageWrite
state.GetRegistersMutable()[5] = addr
state.GetRegistersMutable()[6] = count
state.GetRegistersRef()[2] = exec.SysWrite
state.GetRegistersRef()[4] = exec.FdPreimageWrite
state.GetRegistersRef()[5] = addr
state.GetRegistersRef()[6] = count
state.GetMemory().SetMemory(0, syscallInsn)

preStateRoot := state.GetMemory().MerkleRoot()
Expand All @@ -456,7 +456,7 @@ func FuzzStatePreimageWrite(f *testing.F) {
require.Equal(t, preStateRoot, state.GetMemory().MerkleRoot())
require.Equal(t, uint64(1), state.GetStep())
require.Equal(t, uint32(0), state.GetPreimageOffset())
require.Equal(t, expectedRegisters, state.GetRegistersMutable())
require.Equal(t, expectedRegisters, state.GetRegistersRef())

evm := testutil.NewMIPSEVM(v.Contracts)
evmPost := evm.Step(t, stepWitness, step, v.StateHashFn)
Expand Down
4 changes: 2 additions & 2 deletions cannon/mipsevm/tests/fuzz_evm_multithreaded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func FuzzStateSyscallCloneMT(f *testing.F) {
goVm := v.VMFactory(nil, os.Stdout, os.Stderr, testutil.CreateLogger(),
WithPC(pc), WithNextPC(nextPC), WithStep(step), WithPreimageOffset(preimageOffset))
state := goVm.GetState()
state.GetRegistersMutable()[2] = exec.SysClone
state.GetRegistersRef()[2] = exec.SysClone

state.GetMemory().SetMemory(pc, syscallInsn)
preStateRoot := state.GetMemory().MerkleRoot()
Expand All @@ -44,7 +44,7 @@ func FuzzStateSyscallCloneMT(f *testing.F) {
require.Equal(t, uint8(0), state.GetExitCode())
require.Equal(t, false, state.GetExited())
require.Equal(t, preStateRoot, state.GetMemory().MerkleRoot())
require.Equal(t, expectedRegisters, state.GetRegistersMutable())
require.Equal(t, expectedRegisters, state.GetRegistersRef())
require.Equal(t, step+1, state.GetStep())
require.Equal(t, common.Hash{}, state.GetPreimageKey())
require.Equal(t, preimageOffset, state.GetPreimageOffset())
Expand Down
4 changes: 2 additions & 2 deletions cannon/mipsevm/tests/fuzz_evm_singlethreaded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func FuzzStateSyscallCloneST(f *testing.F) {
goVm := v.VMFactory(nil, os.Stdout, os.Stderr, testutil.CreateLogger(),
WithPC(pc), WithNextPC(nextPC), WithStep(step), WithPreimageOffset(preimageOffset))
state := goVm.GetState()
state.GetRegistersMutable()[2] = exec.SysClone
state.GetRegistersRef()[2] = exec.SysClone

state.GetMemory().SetMemory(pc, syscallInsn)
preStateRoot := state.GetMemory().MerkleRoot()
Expand All @@ -39,7 +39,7 @@ func FuzzStateSyscallCloneST(f *testing.F) {
require.Equal(t, uint8(0), state.GetExitCode())
require.Equal(t, false, state.GetExited())
require.Equal(t, preStateRoot, state.GetMemory().MerkleRoot())
require.Equal(t, expectedRegisters, state.GetRegistersMutable())
require.Equal(t, expectedRegisters, state.GetRegistersRef())
require.Equal(t, step+1, state.GetStep())
require.Equal(t, common.Hash{}, state.GetPreimageKey())
require.Equal(t, preimageOffset, state.GetPreimageOffset())
Expand Down
2 changes: 1 addition & 1 deletion cannon/mipsevm/testutil/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ func RandomRegisters(seed int64) [32]uint32 {

func CopyRegisters(state mipsevm.FPVMState) *[32]uint32 {
copy := new([32]uint32)
*copy = *state.GetRegistersMutable()
*copy = *state.GetRegistersRef()
return copy
}
2 changes: 1 addition & 1 deletion cannon/mipsevm/testutil/vmtests.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func RunVMTests_OpenMips[T mipsevm.FPVMState](t *testing.T, stateFactory StateFa
require.NoError(t, err, "load program into state")

// set the return address ($ra) to jump into when test completes
state.GetRegistersMutable()[31] = EndAddr
state.GetRegistersRef()[31] = EndAddr

us := vmFactory(state, oracle, os.Stdout, os.Stderr, CreateLogger())

Expand Down

0 comments on commit 801dd48

Please sign in to comment.