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

Bvsmod results inconsistent with IDIV instruction #1212

Open
smpcpp opened this issue Nov 24, 2022 · 3 comments
Open

Bvsmod results inconsistent with IDIV instruction #1212

smpcpp opened this issue Nov 24, 2022 · 3 comments

Comments

@smpcpp
Copy link

smpcpp commented Nov 24, 2022

this->eval = (static_cast<triton::uint512>((((op1Signed % op2Signed) + op2Signed) % op2Signed)) & this->getBitvectorMask());

The BvsmodNode is used to produce the semantics for an x86 IDIV instruction but it doesn't always give the same results.
This was changed from the basic modulus in 52aae9e but I couldn't see why.

Example:

BA FFFFFFFF | mov edx,FFFFFFFF |  
B8 59E4ADBD | mov eax,BDADE459 |  
B9 E8030000 | mov ecx,3E8 |  
F7F9 | idiv ecx |  

Emulating in triton produces a remainder EDX = 0x2c9
Running locally produces EDX = 0xfffffee1

Changing back to

this->eval = (static_cast<triton::uint512>((op1Signed % op2Signed)) & this->getBitvectorMask());

produces the correct remainder.

@smpcpp
Copy link
Author

smpcpp commented Nov 26, 2022

Maybe the solution is replacing the bvsmod node with bvsrem in idiv_s semantics
I see now you changed from bvsrem to bvsmod in a1ea273 but there wasn't a referenced issue.

@JonathanSalwan
Copy link
Owner

Sorry for the delay, can you provide a unit test of this behaviour ?

@GhostInCell
Copy link

Encountered with same issue. Here is a short example:

import z3 #4.13.0.0
from triton import * #1.0.0rc3

ctx = TritonContext(ARCH.X86_64)
astCtx = ctx.getAstContext()

a = astCtx.bvsmod(astCtx.bv(96, 8), astCtx.bv(-77, 8))
b = z3.simplify(z3.IntVal(96) % z3.IntVal(-77))

#assert(198 == 19)
assert(a.evaluate() == b.as_long())

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

No branches or pull requests

3 participants