Skip to content

Commit b84a6b1

Browse files
authored
Simplify ValueSet constructor logic (#605)
* Simplify ValueSet constructor logic * Fix lint and format errors
1 parent 029d4a9 commit b84a6b1

File tree

2 files changed

+18
-53
lines changed

2 files changed

+18
-53
lines changed

claripy/ast/bv.py

+5-38
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import logging
4-
import numbers
54
import weakref
65
from contextlib import suppress
76

@@ -300,44 +299,12 @@ def ESI(bits, **kwargs):
300299
return BVV(None, bits, **kwargs)
301300

302301

303-
def ValueSet(bits, region=None, region_base_addr=None, value=None, name=None, val=None):
304-
# Backward compatibility
305-
if value is None and val is not None:
306-
value = val
307-
if region_base_addr is None:
308-
region_base_addr = 0
309-
310-
v = region_base_addr + value
311-
if isinstance(v, claripy.ast.Base):
312-
v = claripy.simplify(v)
313-
314-
# Backward compatibility
315-
if isinstance(v, numbers.Number):
316-
min_v, max_v = v, v
317-
stride = 0
318-
elif isinstance(v, claripy.ast.Base):
319-
si_anno = v.get_annotation(claripy.annotation.StridedIntervalAnnotation)
320-
if si_anno is not None:
321-
min_v = si_anno.lower_bound
322-
max_v = si_anno.upper_bound
323-
stride = si_anno.stride
324-
elif v.op == "BVV":
325-
min_v = v.args[0]
326-
max_v = v.args[0]
327-
stride = 0
328-
else:
329-
raise ClaripyValueError(f"ValueSet() does not take `value` ast with op {v.op}")
330-
else:
331-
raise ClaripyValueError(f"ValueSet() does not take `value` of type {type(value)}")
332-
333-
if name is None:
334-
name = "ValueSet"
335-
bvs = BVS(name, bits).annotate(
336-
claripy.annotation.StridedIntervalAnnotation(stride, region_base_addr + min_v, region_base_addr + max_v)
337-
)
338-
302+
def ValueSet(bits: int, region: str, region_base_addr: int, value: BV | int):
303+
if isinstance(value, int):
304+
value = BVV(value, bits)
305+
value = value + region_base_addr
339306
# Annotate the bvs and return the new AST
340-
return bvs.annotate(claripy.annotation.RegionAnnotation(region, region_base_addr, value))
307+
return value.annotate(claripy.annotation.RegionAnnotation(region, region_base_addr, value))
341308

342309

343310
VS = ValueSet

tests/test_vsa.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,8 @@ def test_extraction_from_reversed_value(self):
626626

627627
def test_value_set_operations(self):
628628
# ValueSet operations
629-
vs_1 = VS(bits=32, value=0)
630-
vs_1 = vs_1.intersection(VS(bits=32, value=1))
629+
vs_1 = VS(32, "global", 0, 0)
630+
vs_1 = vs_1.intersection(VS(32, "global", 0, 1))
631631
assert vsa_model(vs_1).is_empty
632632

633633
# Test merging two addresses
@@ -640,8 +640,8 @@ def test_value_set_operations(self):
640640
)
641641
assert len(vsa_model(vs_1)) == 32
642642

643-
vs_1 = VS(name="boo", bits=32, value=0).intersection(VS(name="makeitempty", bits=32, value=1))
644-
vs_2 = VS(name="foo", bits=32, value=0).intersection(VS(name="makeitempty", bits=32, value=1))
643+
vs_1 = VS(32, "global", 0, 0).intersection(VS(32, "global", 0, 1))
644+
vs_2 = VS(32, "global", 0, 0).intersection(VS(32, "global", 0, 1))
645645
assert self.is_equal(vs_1, vs_1)
646646
assert self.is_equal(vs_2, vs_2)
647647
vs_1 = vs_1.union(VS(32, "global", 0, claripy.SI(bits=32, stride=0, lower_bound=10, upper_bound=10)))
@@ -654,8 +654,8 @@ def test_value_set_operations(self):
654654

655655
def test_value_set_subtraction(self):
656656
# Subtraction of two pointers yields a concrete value
657-
vs_1 = claripy.ValueSet(name="foo", region="global", bits=32, value=0x400010)
658-
vs_2 = claripy.ValueSet(name="bar", region="global", bits=32, value=0x400000)
657+
vs_1 = claripy.ValueSet(32, "global", 0, 0x400010)
658+
vs_2 = claripy.ValueSet(32, "global", 0, 0x400000)
659659
si = vs_1 - vs_2
660660
# assert isinstance(vsa_model(si), claripy.SI)
661661
assert type(vsa_model(si)) is StridedInterval
@@ -700,31 +700,29 @@ def test_if_proxy_identical(self):
700700

701701
def test_if_proxy_and_operations(self):
702702
# if_1 = And(VS_2, IfProxy(si == 0, 0, 1))
703-
vs_2 = claripy.ValueSet(region="global", bits=32, value=0xFA7B00B)
703+
vs_2 = claripy.ValueSet(32, "global", 0, 0xFA7B00B)
704704
si = claripy.SI(bits=32, stride=1, lower_bound=0, upper_bound=1)
705705
if_1 = vs_2 & claripy.If(
706706
si == 0,
707707
claripy.SI(bits=32, stride=0, lower_bound=0, upper_bound=0),
708708
claripy.SI(bits=32, stride=0, lower_bound=0xFFFFFFFF, upper_bound=0xFFFFFFFF),
709709
)
710710
assert claripy.backends.vsa.is_true(
711-
vsa_model(claripy.excavate_ite(if_1).args[1])
712-
== vsa_model(claripy.ValueSet(region="global", bits=32, value=0))
711+
vsa_model(claripy.excavate_ite(if_1).args[1]) == vsa_model(claripy.ValueSet(32, "global", 0, 0))
713712
)
714713
assert claripy.backends.vsa.is_true(vsa_model(claripy.excavate_ite(if_1).args[2]) == vsa_model(vs_2))
715714

716715
def test_if_proxy_or_operations(self):
717716
# if_2 = And(VS_3, IfProxy(si != 0, 0, 1))
718-
vs_3 = claripy.ValueSet(region="global", bits=32, value=0xDEADCA7)
717+
vs_3 = claripy.ValueSet(32, "global", 0, 0xDEADCA7)
719718
si = claripy.SI(bits=32, stride=1, lower_bound=0, upper_bound=1)
720719
if_2 = vs_3 & claripy.If(
721720
si != 0,
722721
claripy.SI(bits=32, stride=0, lower_bound=0, upper_bound=0),
723722
claripy.SI(bits=32, stride=0, lower_bound=0xFFFFFFFF, upper_bound=0xFFFFFFFF),
724723
)
725724
assert claripy.backends.vsa.is_true(
726-
vsa_model(claripy.excavate_ite(if_2).args[1])
727-
== vsa_model(claripy.ValueSet(region="global", bits=32, value=0))
725+
vsa_model(claripy.excavate_ite(if_2).args[1]) == vsa_model(claripy.ValueSet(32, "global", 0, 0))
728726
)
729727
assert claripy.backends.vsa.is_true(vsa_model(claripy.excavate_ite(if_2).args[2]) == vsa_model(vs_3))
730728

@@ -1072,7 +1070,7 @@ def test_solution_on_strided_interval(self):
10721070

10731071
def test_solution_on_valueset(self):
10741072
# Testing solution method with ValueSet (VS) objects
1075-
vs = claripy.ValueSet(region="global", bits=32, value=0xDEADCA7)
1073+
vs = claripy.ValueSet(32, "global", 0, 0xDEADCA7)
10761074
assert self.solver.solution(vs, 0xDEADCA7)
10771075
assert not self.solver.solution(vs, 0xDEADBEEF)
10781076

@@ -1081,8 +1079,8 @@ def test_solution_on_combined_valueset(self):
10811079
si = claripy.SI(bits=32, stride=0, lower_bound=3, upper_bound=3)
10821080
si2 = claripy.SI(bits=32, stride=10, lower_bound=32, upper_bound=320)
10831081

1084-
vs = claripy.ValueSet(bits=si.size(), region="foo", value=si)
1085-
vs2 = claripy.ValueSet(bits=si2.size(), region="foo", value=si2)
1082+
vs = claripy.ValueSet(si.size(), "foo", 0, si)
1083+
vs2 = claripy.ValueSet(si2.size(), "foo", 0, si2)
10861084
vs = vs.union(vs2)
10871085

10881086
assert self.solver.solution(vs, 3)

0 commit comments

Comments
 (0)