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

cannon: Handle div by zero in MIPS.sol #10468

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/contracts-bedrock/semver-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@
"sourceCodeHash": "0x73aa5934e56ba2a45f368806c5db1d442bf5713d51b2184749f4638eaceb832e"
},
"src/cannon/MIPS.sol": {
"initCodeHash": "0xd447743c7a3c1babe141050603dd643467ada00bd90465a52ef3093445ee1b02",
"sourceCodeHash": "0xe37efbb893e0a7499fcd565f8495f6ba911611ba36795ac131738474f116fce5"
"initCodeHash": "0xa5d36fc67170ad87322f358f612695f642757bbf5280800d5d878da21402579a",
"sourceCodeHash": "0x75701f3efb7a9c16079ba0a4ed2867999aab7d95bfa0fe5ebb131cfc278593aa"
},
"src/cannon/PreimageOracle.sol": {
"initCodeHash": "0xe5db668fe41436f53995e910488c7c140766ba8745e19743773ebab508efd090",
Expand Down
10 changes: 8 additions & 2 deletions packages/contracts-bedrock/src/cannon/MIPS.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ contract MIPS is ISemver {
uint32 public constant BRK_START = 0x40000000;

/// @notice The semantic version of the MIPS contract.
/// @custom:semver 1.0.0
string public constant version = "1.0.0";
/// @custom:semver 1.0.1
string public constant version = "1.0.1";

uint32 internal constant FD_STDIN = 0;
uint32 internal constant FD_STDOUT = 1;
Expand Down Expand Up @@ -422,13 +422,19 @@ contract MIPS is ISemver {
// Stores the quotient in LO
// And the remainder in HI
else if (_func == 0x1a) {
if (int32(_rt) == 0) {
revert("MIPS: division by zero");
}
state.hi = uint32(int32(_rs) % int32(_rt));
state.lo = uint32(int32(_rs) / int32(_rt));
}
// divu: Unsigned divides `rs` by `rt`.
// Stores the quotient in LO
// And the remainder in HI
else if (_func == 0x1b) {
if (_rt == 0) {
revert("MIPS: division by zero");
}
state.hi = _rs % _rt;
state.lo = _rs / _rt;
}
Expand Down
20 changes: 20 additions & 0 deletions packages/contracts-bedrock/test/cannon/MIPS.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,26 @@ contract MIPS_Test is CommonTest {
assertEq(postState, outputState(expect), "unexpected post state");
}

function test_div_byZero_fails() external {
uint32 insn = encodespec(0x9, 0xa, 0x0, 0x1a); // div t1, t2
(MIPS.State memory state, bytes memory proof) = constructMIPSState(0, insn, 0x4, 0);
state.registers[9] = 5; // t1
state.registers[10] = 0; // t2

vm.expectRevert("MIPS: division by zero");
mips.step(encodeState(state), proof, 0);
}

function test_divu_byZero_fails() external {
uint32 insn = encodespec(0x9, 0xa, 0x0, 0x1b); // divu t1, t2
(MIPS.State memory state, bytes memory proof) = constructMIPSState(0, insn, 0x4, 0);
state.registers[9] = 5; // t1
state.registers[10] = 0; // t2

vm.expectRevert("MIPS: division by zero");
mips.step(encodeState(state), proof, 0);
}

function test_beq_succeeds() external {
uint16 boff = 0x10;
uint32 insn = encodeitype(0x4, 0x9, 0x8, boff); // beq $t0, $t1, 16
Expand Down