Skip to content

Commit

Permalink
don't hash sequence contents when indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
pschanely committed Aug 24, 2024
1 parent dc1def4 commit 2a5b109
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
7 changes: 4 additions & 3 deletions crosshair/opcode_intercept.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ def trace_op(self, frame, codeobj, codenum):
# it would be great to cache this computation somehow.
indices = {}
for idx, value in enumerate(container):
if value in indices:
indices[id(value)].append(idx)
value_id = id(value)
if value_id in indices:
indices[value_id].append(idx)
else:
indices[id(value)] = [idx]
indices[value_id] = [idx]
space = context_statespace()
in_bounds = space.smt_fork(
z3Or(-len(container) <= key.var, key.var < len(container)),
Expand Down
29 changes: 20 additions & 9 deletions crosshair/opcode_intercept_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,29 @@ def numstr(x: str) -> int:
check_states(numstr, POST_FAIL)


def test_concrete_list_with_symbolic_index_deduplicates_values():
def test_concrete_list_with_symbolic_index_deduplicates_values(space):
haystack = [False] * 13 + [True] + [False] * 11

def numstr(x: int) -> bool:
"""
post: _ == False
raises: KeyError
"""
# crosshair: max_iterations=3
return haystack[x]
idx = proxy_for_type(int, "idx")
with ResumedTracing():
space.add(0 <= idx)
space.add(idx < len(haystack))
ret = haystack[idx]
assert ret
assert not space.is_possible(idx != 13)

check_states(numstr, POST_FAIL)

def test_concrete_list_with_symbolic_index_unhashable_values(space):
o1 = dict()
options = [o1, o1, o1]
idx = proxy_for_type(int, "idx")
with ResumedTracing():
space.add(0 <= idx)
space.add(idx < 3)
ret = options[idx]
assert ret is o1
assert space.is_possible(idx == 0)
assert space.is_possible(idx == 2)


def test_dict_key_containment():
Expand Down
3 changes: 2 additions & 1 deletion doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Changelog
Next Version
------------

* Nothing yet!
* Fix error when symbolically indexing into a sequence of unhashable objects.
(a regression from v0.0.68)


Version 0.0.69
Expand Down

0 comments on commit 2a5b109

Please sign in to comment.