Skip to content

Commit c0cdde5

Browse files
authored
llvm-context: fix off-by-one in SDIV overflow semantics (#398)
- Fix the wrong predicate (the negative side in 2s complement has one more bit) - Increase test coverage Signed-off-by: xermicus <[email protected]>
1 parent df1921b commit c0cdde5

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ This is a development pre-release.
66

77
Supported `polkadot-sdk` rev: `2509.0.0`
88

9-
### Chnaged
9+
### Changed
1010
- Emulated EVM heap memory accesses of zero length are never out of bounds.
1111

12+
### Fixed
13+
- An off-by-one bug cusing incorrect `SDIV` overflow semantics.
14+
1215
## v0.4.1
1316

1417
This is a development pre-release.

crates/integration/codesize.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Baseline": 914,
33
"Computation": 2295,
4-
"DivisionArithmetics": 14510,
4+
"DivisionArithmetics": 14496,
55
"ERC20": 17482,
66
"Events": 1674,
77
"FibonacciIterative": 1490,

crates/integration/src/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ fn signed_division() {
169169
(minus_five, two),
170170
(I256::MINUS_ONE, I256::MIN),
171171
(one, I256::ZERO),
172+
(I256::MIN, I256::MINUS_ONE),
173+
(I256::MIN + I256::ONE, I256::MINUS_ONE),
172174
] {
173175
actions.push(Call {
174176
origin: TestAddress::Alice,

crates/llvm-context/src/polkavm/context/function/runtime/arithmetics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl RuntimeFunction for SignedDivision {
9292
"max_uint",
9393
)?;
9494
let is_operand_1_overflow = context.builder().build_int_compare(
95-
inkwell::IntPredicate::EQ,
95+
inkwell::IntPredicate::SLT,
9696
operand_1,
9797
context.builder().build_int_neg(max_uint, "min_uint")?,
9898
"is_operand_1_overflow",

0 commit comments

Comments
 (0)