Skip to content

Commit

Permalink
Add offset validation and remove unnecessary code per code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mininny committed Oct 22, 2024
1 parent 4f9d2ce commit 22f0c71
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 19 deletions.
3 changes: 3 additions & 0 deletions rvgo/fast/instrumented.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func (m *InstrumentedState) readPreimage(key [32]byte, offset uint64) (dat [32]b
m.lastPreimage = preimage
}
m.lastPreimageOffset = offset
if offset >= uint64(len(preimage)) {
panic("Preimage offset out-of-bounds")
}
datLen = uint64(copy(dat[:], preimage[offset:]))
return
}
Expand Down
5 changes: 1 addition & 4 deletions rvgo/fast/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@ func (m *Memory) SetAligned(addr uint64, dat []byte) {
m.Invalidate(addr) // invalidate this branch of memory, now that the value changed
}

d := copy(p.Data[pageAddr:], dat)
if d == len(dat) {
return // if all the data fitted in the page, we're done
}
copy(p.Data[pageAddr:], dat)
}

func (m *Memory) GetUnaligned(addr uint64, dest []byte) {
Expand Down
6 changes: 1 addition & 5 deletions rvgo/fast/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,6 @@ func (inst *InstrumentedState) riscvStep() (outErr error) {
s.Memory.SetAligned(rightAddr, bytez[leftSize:size])
}

//
// CSR (control and status registers) functions
//

//
// Preimage oracle interactions
//
Expand Down Expand Up @@ -378,7 +374,7 @@ func (inst *InstrumentedState) riscvStep() (outErr error) {
// ensure MAP_ANONYMOUS is set and fd == -1
if (flags&0x20) == 0 || fd != u64Mask() {
addr = u64Mask()
errCode = toU64(0x4d) // no error
errCode = toU64(0x4d) // EBADF
} else {
// ignore: prot, flags, fd, offset
switch addr {
Expand Down
12 changes: 7 additions & 5 deletions rvgo/slow/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,13 @@ func Step(calldata []byte, po PreimageOracle) (stateHash common.Hash, outErr err
}

proofContentOffset := shortToU64(uint16(stateContentOffset) + paddedStateSize + 32)
// TODO: validate abi offset values?

if and(b32asBEWord(calldataload(shortToU64(uint16(stateContentOffset)+paddedStateSize))), shortToU256(60-1)) != (U256{}) {
// proof offset must be stateContentOffset+paddedStateSize+32
// proof size: 64-5+1=60 * 32 byte leaf,
// but multiple memProof can be used, so the proofSize must be a multiple of 60
panic("invalid proof offset input")
}

//
// State loading
Expand Down Expand Up @@ -450,10 +456,6 @@ func Step(calldata []byte, po PreimageOracle) (stateHash common.Hash, outErr err
storeMemUnaligned(addr, size, u64ToU256(value), proofIndexL, proofIndexR)
}

//
// CSR (control and status registers) functions
//

//
// Preimage oracle interactions
//
Expand Down
5 changes: 0 additions & 5 deletions rvsol/src/RISCV.sol
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ contract RISCV {
out := 548
}
if iszero(eq(proof.offset, proofContentOffset())) { revert(0, 0) }
// TODO: validate abi offset values?

//
// State loading
Expand Down Expand Up @@ -728,10 +727,6 @@ contract RISCV {
storeMemUnaligned(addr, size, u64ToU256(value), proofIndexL, proofIndexR)
}

//
// CSR (control and status registers) functions
//

//
// Preimage oracle interactions
//
Expand Down

0 comments on commit 22f0c71

Please sign in to comment.