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

macaw-x86-symbolic: Fix idiv/div semantics #394

Merged
merged 1 commit into from
Jul 12, 2024

Conversation

RyanGlScott
Copy link
Contributor

When converting a Macaw value with the Macaw type TupleType [x_1, ..., x_n] to Crucible, the resulting Crucible value will have the Crucible type StructType (EmptyCtx ::> ToCrucibleType x_n ::> ... ::> ToCrucibleType x_1). (See macawListToCrucible(M) in Data.Macaw.Symbolic.PersistentState for where this is implemented.) Note that the order of the tuple's fields is reversed in the process of converting it to a Crucible struct. This is a convention that one must keep in mind when dealing with Macaw tuples at the Crucible level.

As it turns out, the part of macaw-x86-symbolic reponsible for interpreting the semantics of the idiv instruction (for signed quotient/remainder) and the div instruction (for unsigned quotient/remainder) were not respecting this convention. This is because the macaw-x86-symbolic semantics were returning a Crucible struct consisting of Empty :> quotient :> remainder), but at the Macaw level, this was interpreted as the tuple (remainder, quotient), which is the opposite of the intended order. This led to subtle bugs such as those observed in #393.

The solution is straightforward: have the macaw-x86-symbolic semantics compute Empty :> remainder :> quotient instead. Somewhat counterintuitive, but it does work.

Fixes #393.

@RyanGlScott RyanGlScott added bug symbolic-execution Issues relating to macaw-symbolic and symbolic execution arch:x86 x86 issues labels Jul 12, 2024
@RyanGlScott RyanGlScott changed the title macaw-x86-symbolic: Fix idiv/div semantics macaw-x86-symbolic: Fix idiv/div semantics Jul 12, 2024
When converting a Macaw value with the Macaw type `TupleType [x_1, ..., x_n]`
to Crucible, the resulting Crucible value will have the Crucible type
`StructType (EmptyCtx ::> ToCrucibleType x_n ::> ... ::> ToCrucibleType x_1)`.
(See `macawListToCrucible(M)` in `Data.Macaw.Symbolic.PersistentState` for
where this is implemented.) Note that the order of the tuple's fields is
reversed in the process of converting it to a Crucible struct. This is a
convention that one must keep in mind when dealing with Macaw tuples at the
Crucible level.

As it turns out, the part of `macaw-x86-symbolic` reponsible for interpreting
the semantics of the `idiv` instruction (for signed quotient/remainder) and the
`div` instruction (for unsigned quotient/remainder) were _not_ respecting this
convention. This is because the `macaw-x86-symbolic` semantics were returning a
Crucible struct consisting of `Empty :> quotient :> remainder)`, but at the
Macaw level, this was interpreted as the tuple `(remainder, quotient)`, which
is the opposite of the intended order. This led to subtle bugs such as those
observed in #393.

The solution is straightforward: have the `macaw-x86-symbolic` semantics
compute `Empty :> remainder :> quotient` instead. Somewhat counterintuitive,
but it does work.

Fixes #393.
@RyanGlScott RyanGlScott force-pushed the T393-fix-div-interpretation branch from 3a944b1 to 79e1a39 Compare July 12, 2024 20:42
@RyanGlScott RyanGlScott merged commit a6ff58f into master Jul 12, 2024
3 checks passed
@RyanGlScott RyanGlScott deleted the T393-fix-div-interpretation branch July 12, 2024 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch:x86 x86 issues bug symbolic-execution Issues relating to macaw-symbolic and symbolic execution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

macaw-x86-symbolic simulates idiv instructions incorrectly
2 participants