Fix optimization specific SREM edge case (#527)

Existing test coverage didn't catch a signed remainder overflow
semantics edge case behavior where at least one the operators is being
constant folded by LLVM instead of solc. This PR fixes the SREM
translation bug and adds missing test coverage for division ops with
constants.

Closes #524

---------

Signed-off-by: xermicus <cyrill@parity.io>
This commit is contained in:
xermicus authored and GitHub committed 2026-05-28 09:01:41 +02:00
1 parent 62a62770e3
commit 40e97d11eb
23 files changed
+1336 -21

No files matched your search

@@ -0,0 +1,32 @@
/// Both-constant MOD variants. Selector index is read from calldata[0..32]
/// and dispatched to a branch with two literal operands. Compiled directly via
/// revive's Yul-to-LLVM path (bypassing solc's Yul optimizer) so the literals
/// reach LLVM intact and exercise revive's "two literal operands" codegen path.
object "ModBothConst" {
code {
let size := datasize("ModBothConst_deployed")
codecopy(0, dataoffset("ModBothConst_deployed"), size)
return(0, size)
}
object "ModBothConst_deployed" {
code {
let which := calldataload(0)
// `tag` is XORed into the result so that any poison/undef LLVM
// produces from a UB-triggering const-fold propagates and diverges
// from EVM, instead of coincidentally returning 0 and masking it.
let tag := calldataload(32)
let r := 0
switch which
case 0 { r := mod(5, 5) }
case 1 { r := mod(5, 1) }
case 2 { r := mod(0, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) }
case 3 { r := mod(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) }
case 4 { r := mod(5, 2) }
case 5 { r := mod(2, 5) }
case 6 { r := mod(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, 0) }
default { revert(0, 0) }
mstore(0, xor(r, tag))
return(0, 32)
}
}
}