-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0cc631a
commit 9397fd8
Showing
3 changed files
with
77 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from z3 import ( | ||
ULE, | ||
BitVec, | ||
BitVecSort, | ||
BitVecVal, | ||
Function, | ||
Not, | ||
simplify, | ||
) | ||
|
||
from halmos.utils import match_dynamic_array_overflow_condition | ||
|
||
|
||
def test_match_dynamic_array_overflow_condition(): | ||
# Create Z3 objects | ||
f_sha3_256 = Function("f_sha3_256", BitVecSort(256), BitVecSort(256)) | ||
slot = BitVec("slot", 256) | ||
offset = BitVecVal(1000, 256) # Less than 2**64 | ||
|
||
# Test the function | ||
cond = Not(ULE(f_sha3_256(slot), offset + f_sha3_256(slot))) | ||
assert match_dynamic_array_overflow_condition(cond) | ||
|
||
# Test with opposite order of addition | ||
opposite_order_cond = Not(ULE(f_sha3_256(slot), f_sha3_256(slot) + offset)) | ||
assert not match_dynamic_array_overflow_condition(opposite_order_cond) | ||
|
||
# Test with opposite order after simplification | ||
simplified_opposite_order_cond = simplify( | ||
Not(ULE(f_sha3_256(slot), f_sha3_256(slot) + offset)) | ||
) | ||
assert match_dynamic_array_overflow_condition(simplified_opposite_order_cond) | ||
|
||
# Test with offset = 2**64 - 1 (should match) | ||
max_valid_offset = BitVecVal(2**64 - 1, 256) | ||
max_valid_cond = Not(ULE(f_sha3_256(slot), max_valid_offset + f_sha3_256(slot))) | ||
assert match_dynamic_array_overflow_condition(max_valid_cond) | ||
|
||
# Test with offset >= 2**64 | ||
large_offset = BitVecVal(2**64, 256) | ||
large_offset_cond = Not(ULE(f_sha3_256(slot), large_offset + f_sha3_256(slot))) | ||
assert not match_dynamic_array_overflow_condition(large_offset_cond) | ||
|
||
# Test with a different function | ||
different_func = Function("different_func", BitVecSort(256), BitVecSort(256)) | ||
non_matching_cond = Not(ULE(different_func(slot), offset + different_func(slot))) | ||
assert not match_dynamic_array_overflow_condition(non_matching_cond) | ||
|
||
# Test with just ULE, not Not(ULE(...)) | ||
ule_only = ULE(f_sha3_256(slot), offset + f_sha3_256(slot)) | ||
assert not match_dynamic_array_overflow_condition(ule_only) | ||
|
||
# Test with mismatched slots | ||
slot2 = BitVec("slot2", 256) | ||
mismatched_slots = Not(ULE(f_sha3_256(slot), offset + f_sha3_256(slot2))) | ||
assert not match_dynamic_array_overflow_condition(mismatched_slots) |