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

Fix BitVecExtract simplification for constant folding #2524

Merged
merged 5 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion manticore/core/smtlib/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def visit_BitVecExtract(self, expression, *operands):
begining = expression.begining
end = expression.end
value = value >> begining
mask = (1 << (end - begining)) - 1
mask = (1 << (end - begining + 1)) - 1
value = value & mask
return BitVecConstant(size=expression.size, value=value, taint=expression.taint)

Expand Down
14 changes: 11 additions & 3 deletions tests/other/test_smtlibv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
constant_folder,
replace,
BitVecConstant,
BitVecExtract,
)
from manticore.core.smtlib.solver import (
Z3Solver,
Expand Down Expand Up @@ -280,7 +281,7 @@ def test_constant_bitvec(self):
self.assertTrue(x.value == 0)

def testBasicAST_001(self):
""" Can't build abstract classes """
"""Can't build abstract classes"""
a = BitVecConstant(size=32, value=100)

self.assertRaises(TypeError, Expression, ())
Expand All @@ -289,7 +290,7 @@ def testBasicAST_001(self):
self.assertRaises(TypeError, Operation, a)

def testBasicOperation(self):
""" Add """
"""Add"""
a = BitVecConstant(size=32, value=100)
b = BitVecVariable(size=32, name="VAR")
c = a + b
Expand Down Expand Up @@ -820,6 +821,13 @@ def test_arithmetic_simplify_extract(self):
)
self.assertEqual(translate_to_smtlib(simplify(c)), "((_ extract 23 8) VARA)")

def test_constant_folding_extract(self):
cs = ConstraintSet()
x = BitVecConstant(size=32, value=0xAB123456, taint=("important",))
z = constant_folder(BitVecExtract(operand=x, offset=8, size=16))
self.assertItemsEqual(z.taint, ("important",))
self.assertEqual(z.value, 0x1234)

def test_arithmetic_simplify_udiv(self):
cs = ConstraintSet()
a = cs.new_bitvec(32, name="VARA")
Expand Down Expand Up @@ -856,7 +864,7 @@ def test_simplify_SUB(self):
self.assertTrue(self.solver.check(cs))

def testBasicReplace(self):
""" Add """
"""Add"""
a = BitVecConstant(size=32, value=100)
b1 = BitVecVariable(size=32, name="VAR1")
b2 = BitVecVariable(size=32, name="VAR2")
Expand Down