Skip to content

Commit

Permalink
Revert reserved immediate word shifts with imm[5]!=0
Browse files Browse the repository at this point in the history
  • Loading branch information
mininny committed Dec 15, 2024
1 parent 8ff9318 commit f25e041
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions rvgo/fast/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,16 @@ func (inst *InstrumentedState) riscvStep() (outErr error) {
case 0: // 000 = ADDIW
rdValue = mask32Signed64(add64(rs1Value, imm))
case 1: // 001 = SLLIW
// SLLIW where imm[5] != 0 is reserved
if and64(imm, toU64(0x20)) != 0 {
revertWithCode(riscv.ErrInvalidSyscall, fmt.Errorf("illegal instruction %d: reserved instruction encoding", instr))
}
rdValue = mask32Signed64(shl64(and64(imm, toU64(0x1F)), rs1Value))
case 5: // 101 = SR~
// SRLIW and SRAIW where imm[5] != 0 is reserved
if and64(imm, toU64(0x20)) != 0 {
revertWithCode(riscv.ErrInvalidSyscall, fmt.Errorf("illegal instruction %d: reserved instruction encoding", instr))
}
shamt := and64(imm, toU64(0x1F))
switch shr64(toU64(5), imm) { // top 7 bits select the shift type
case 0x00: // 0000000 = SRLIW
Expand Down
8 changes: 8 additions & 0 deletions rvgo/slow/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,16 @@ func Step(calldata []byte, po PreimageOracle) (stateHash common.Hash, outErr err
case 0: // 000 = ADDIW
rdValue = mask32Signed64(add64(rs1Value, imm))
case 1: // 001 = SLLIW
// SLLIW where imm[5] != 0 is reserved
if and64(imm, toU64(0x20)) != (U64{}) {
revertWithCode(riscv.ErrInvalidSyscall, fmt.Errorf("illegal instruction %d: reserved instruction encoding", instr))
}
rdValue = mask32Signed64(shl64(and64(imm, toU64(0x1F)), rs1Value))
case 5: // 101 = SR~
// SRLIW and SRAIW imm[5] != 0 is reserved
if and64(imm, toU64(0x20)) != (U64{}) {
revertWithCode(riscv.ErrInvalidSyscall, fmt.Errorf("illegal instruction %d: reserved instruction encoding", instr))
}
shamt := and64(imm, toU64(0x1F))
switch shr64(toU64(5), imm).val() { // top 7 bits select the shift type
case 0x00: // 0000000 = SRLIW
Expand Down
10 changes: 10 additions & 0 deletions rvsol/src/RISCV.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1271,9 +1271,19 @@ contract RISCV is IBigStepper {
}
case 1 {
// 001 = SLLIW

// SLLIW where imm[5] != 0 is reserved
if and64(imm, toU64(0x20)) {
revertWithCode(0xf001ca11)
}
rdValue := mask32Signed64(shl64(and64(imm, toU64(0x1F)), rs1Value))
}
case 5 {
// SRLIW and SRAIW where imm[5] != 0 is reserved
if and64(imm, toU64(0x20)) {
revertWithCode(0xf001ca11)
}

// 101 = SR~
let shamt := and64(imm, toU64(0x1F))
switch shr64(toU64(5), imm)
Expand Down

0 comments on commit f25e041

Please sign in to comment.