Skip to content

Fix unsound extract rewrites over bvadd and bvsub - #106

Closed
rui314 wants to merge 1 commit into
rems-project:masterfrom
rui314:fix-commute-extract-carry
Closed

rui314 wants to merge 1 commit into
rems-project:masterfrom
rui314:fix-commute-extract-carry

Conversation

@rui314

@rui314 rui314 commented Sep 22, 2026

Copy link
Copy Markdown

We found this issue while using Isla to prove equivalence between two machine instructions. With isla-execute-function -s, an unsound simplification caused the equivalence check to report a false counterexample.

For example, consider 8-bit unsigned saturating subtraction implemented using a 9-bit difference:

d = zero_extend(a, 9) - zero_extend(b, 9)
result = d[8] ? 0 : d[7:0]

The simplifier moved the extraction through subtraction, turning d[8] into:

zero_extend(a, 9)[8] - zero_extend(b, 9)[8]

Both extracted bits are zero, so the borrow test always fails. For a = 0, b = 1, the simplified expression returns 255 instead of 0.

Addition has the same issue with carries from discarded lower bits. This patch restricts extract commutation over bvadd and bvsub to slices starting at bit zero, and adds regression tests. Bitwise operations still commute with any slice

We found this issue while using Isla to prove equivalence between two
machine instructions. An unsound simplification caused the check to
incorrectly conclude that they were not equivalent.

The isla-execute-function -s simplifier pushed extracts through
bvadd/bvsub even when lo > 0, losing carries or borrows from lower bits.
For 9-bit operands, extracting bit 8 of 0 - 1 yields 1, but extracting
bit 8 of each operand before subtracting yields 0.

This change restricts arithmetic rewrites to lo == 0 and adds regression
tests. Bitwise rewrites remain valid for any slice.
@bacam

bacam commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Thanks. I'm going to commit a slightly different version, including a test that uses the SMT solver for a more thorough check.

@Alasdair

Copy link
Copy Markdown
Collaborator

I've been experimenting with an E-graph based bitvector simplifier this week, so I might switch to that entirely if it works out.

@bacam bacam closed this in e9b5d94 Sep 23, 2026
@rui314
rui314 deleted the fix-commute-extract-carry branch September 23, 2026 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants