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

Revert load instructions with reserved bits[14:12] = 111 #109

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions rvgo/fast/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,12 @@ func (inst *InstrumentedState) riscvStep() (outErr error) {
switch opcode {
case 0x03: // 000_0011: memory loading
// LB, LH, LW, LD, LBU, LHU, LWU

// bits[14:12] set to 111 are reserved
if eq64(funct3, toU64(0x7)) != 0 {
revertWithCode(riscv.ErrInvalidSyscall, fmt.Errorf("illegal instruction %d: reserved instruction encoding", instr))
}

imm := parseImmTypeI(instr)
signed := iszero64(and64(funct3, toU64(4))) // 4 = 100 -> bitflag
size := shl64(and64(funct3, toU64(3)), toU64(1)) // 3 = 11 -> 1, 2, 4, 8 bytes size
Expand Down
6 changes: 6 additions & 0 deletions rvgo/slow/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,12 @@ func Step(calldata []byte, po PreimageOracle) (stateHash common.Hash, outErr err
switch opcode.val() {
case 0x03: // 000_0011: memory loading
// LB, LH, LW, LD, LBU, LHU, LWU

// bits[14:12] set to 111 are reserved
if eq64(funct3, toU64(0x7)) != (U64{}) {
revertWithCode(riscv.ErrInvalidSyscall, fmt.Errorf("illegal instruction %d: reserved instruction encoding", instr))
}

imm := parseImmTypeI(instr)
signed := iszero64(and64(funct3, toU64(4))) // 4 = 100 -> bitflag
size := shl64(and64(funct3, toU64(3)), toU64(1)) // 3 = 11 -> 1, 2, 4, 8 bytes size
Expand Down
4 changes: 4 additions & 0 deletions rvsol/src/RISCV.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,10 @@ contract RISCV is IBigStepper {
let pc_ := _pc
// 000_0011: memory loading
// LB, LH, LW, LD, LBU, LHU, LWU

// bits[14:12] set to 111 are reserved
if eq64(funct3, toU64(0x7)) { revertWithCode(0xf001ca11) }

let imm := parseImmTypeI(instr)
let signed := iszero64(and64(funct3, toU64(4))) // 4 = 100 -> bitflag
let size := shl64(and64(funct3, toU64(3)), toU64(1)) // 3 = 11 -> 1, 2, 4, 8 bytes size
Expand Down
12 changes: 12 additions & 0 deletions rvsol/test/RISCV.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2460,6 +2460,18 @@ contract RISCV_Test is CommonTest {
riscv.step(encodedState, proof, 0);
}

function test_reserved_load_instruction() public {
bytes32 value = hex"61fb11d66dcc9d48";
uint16 offset = 0x6bf;
uint64 addr = 0xd34d + offset;
uint32 insn = encodeIType(0x3, 21, 0x7, 4, offset); // lhu x21, funct 0x7, offset(x4)
(State memory state, bytes memory proof) = constructRISCVState(0, insn, addr, value);
state.registers[4] = 0xd34d;
bytes memory encodedState = encodeState(state);

vm.expectRevert(hex"00000000000000000000000000000000000000000000000000000000f001ca11");
riscv.step(encodedState, proof, 0);
}
/* Helper methods */

function encodeState(State memory state) internal pure returns (bytes memory) {
Expand Down
Loading