Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
625054c3
Unverified
Commit
625054c3
authored
May 13, 2024
by
Inphi
Committed by
GitHub
May 13, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cannon: Handle div by zero in MIPS.sol (#10468)
* cannon: Handle div by zero in MIPS.sol * fix revert stmt style
parent
49010623
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
4 deletions
+30
-4
semver-lock.json
packages/contracts-bedrock/semver-lock.json
+2
-2
MIPS.sol
packages/contracts-bedrock/src/cannon/MIPS.sol
+8
-2
MIPS.t.sol
packages/contracts-bedrock/test/cannon/MIPS.t.sol
+20
-0
No files found.
packages/contracts-bedrock/semver-lock.json
View file @
625054c3
...
@@ -116,8 +116,8 @@
...
@@ -116,8 +116,8 @@
"sourceCodeHash"
:
"0x73aa5934e56ba2a45f368806c5db1d442bf5713d51b2184749f4638eaceb832e"
"sourceCodeHash"
:
"0x73aa5934e56ba2a45f368806c5db1d442bf5713d51b2184749f4638eaceb832e"
},
},
"src/cannon/MIPS.sol"
:
{
"src/cannon/MIPS.sol"
:
{
"initCodeHash"
:
"0x
d447743c7a3c1babe141050603dd643467ada00bd90465a52ef3093445ee1b02
"
,
"initCodeHash"
:
"0x
a5d36fc67170ad87322f358f612695f642757bbf5280800d5d878da21402579a
"
,
"sourceCodeHash"
:
"0x
e37efbb893e0a7499fcd565f8495f6ba911611ba36795ac131738474f116fce5
"
"sourceCodeHash"
:
"0x
75701f3efb7a9c16079ba0a4ed2867999aab7d95bfa0fe5ebb131cfc278593aa
"
},
},
"src/cannon/PreimageOracle.sol"
:
{
"src/cannon/PreimageOracle.sol"
:
{
"initCodeHash"
:
"0xe5db668fe41436f53995e910488c7c140766ba8745e19743773ebab508efd090"
,
"initCodeHash"
:
"0xe5db668fe41436f53995e910488c7c140766ba8745e19743773ebab508efd090"
,
...
...
packages/contracts-bedrock/src/cannon/MIPS.sol
View file @
625054c3
...
@@ -43,8 +43,8 @@ contract MIPS is ISemver {
...
@@ -43,8 +43,8 @@ contract MIPS is ISemver {
uint32 public constant BRK_START = 0x40000000;
uint32 public constant BRK_START = 0x40000000;
/// @notice The semantic version of the MIPS contract.
/// @notice The semantic version of the MIPS contract.
/// @custom:semver 1.0.
0
/// @custom:semver 1.0.
1
string public constant version = "1.0.
0
";
string public constant version = "1.0.
1
";
uint32 internal constant FD_STDIN = 0;
uint32 internal constant FD_STDIN = 0;
uint32 internal constant FD_STDOUT = 1;
uint32 internal constant FD_STDOUT = 1;
...
@@ -422,6 +422,9 @@ contract MIPS is ISemver {
...
@@ -422,6 +422,9 @@ contract MIPS is ISemver {
// Stores the quotient in LO
// Stores the quotient in LO
// And the remainder in HI
// And the remainder in HI
else if (_func == 0x1a) {
else if (_func == 0x1a) {
if (int32(_rt) == 0) {
revert("MIPS: division by zero");
}
state.hi = uint32(int32(_rs) % int32(_rt));
state.hi = uint32(int32(_rs) % int32(_rt));
state.lo = uint32(int32(_rs) / int32(_rt));
state.lo = uint32(int32(_rs) / int32(_rt));
}
}
...
@@ -429,6 +432,9 @@ contract MIPS is ISemver {
...
@@ -429,6 +432,9 @@ contract MIPS is ISemver {
// Stores the quotient in LO
// Stores the quotient in LO
// And the remainder in HI
// And the remainder in HI
else if (_func == 0x1b) {
else if (_func == 0x1b) {
if (_rt == 0) {
revert("MIPS: division by zero");
}
state.hi = _rs % _rt;
state.hi = _rs % _rt;
state.lo = _rs / _rt;
state.lo = _rs / _rt;
}
}
...
...
packages/contracts-bedrock/test/cannon/MIPS.t.sol
View file @
625054c3
...
@@ -892,6 +892,26 @@ contract MIPS_Test is CommonTest {
...
@@ -892,6 +892,26 @@ contract MIPS_Test is CommonTest {
assertEq(postState, outputState(expect), "unexpected post state");
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 {
function test_beq_succeeds() external {
uint16 boff = 0x10;
uint16 boff = 0x10;
uint32 insn = encodeitype(0x4, 0x9, 0x8, boff); // beq $t0, $t1, 16
uint32 insn = encodeitype(0x4, 0x9, 0x8, boff); // beq $t0, $t1, 16
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment