Skip to content

Commit

Permalink
accept review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
daejunpark committed Sep 13, 2024
1 parent 62fb935 commit dfaf98d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/halmos/calldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ def get_dyn_sizes(self, name: str, typ: Type) -> tuple[list[int], BitVecRef]:
The candidates are derived from --array_lengths if provided; otherwise, default values are used.
"""

if name in self.arrlen:
sizes = self.arrlen[name]
else:
sizes = self.arrlen.get(name)

if sizes is None:
sizes = (
list(range(self.args.loop + 1))
if isinstance(typ, DynamicArrayType)
Expand All @@ -152,7 +152,7 @@ def create(self, abi: dict, fun_info: FunctionInfo) -> ByteVec:

# function selector
calldata = ByteVec()
calldata.append(int(fun_info.selector, 16).to_bytes(4, "big"))
calldata.append(bytes.fromhex(fun_info.selector))

# list of parameter types
fun_abi = abi[fun_info.sig]
Expand Down
10 changes: 5 additions & 5 deletions src/halmos/cheatcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def symbolic_storage(ex, arg, sevm, stack, step_id):

def create_calldata_contract(ex, arg, sevm, stack, step_id):
contract_name = name_of(extract_string_argument(arg, 0))
return create_calldata_generic(ex, arg, sevm, stack, step_id, contract_name)
return create_calldata_generic(ex, sevm, contract_name)


def create_calldata_contract_bool(ex, arg, sevm, stack, step_id):
Expand All @@ -236,15 +236,15 @@ def create_calldata_contract_bool(ex, arg, sevm, stack, step_id):
"symbolic boolean flag for SVM.createCalldata()",
)
return create_calldata_generic(
ex, arg, sevm, stack, step_id, contract_name, include_view=bool(include_view)
ex, sevm, contract_name, include_view=bool(include_view)
)


def create_calldata_file_contract(ex, arg, sevm, stack, step_id):
filename = name_of(extract_string_argument(arg, 0))
contract_name = name_of(extract_string_argument(arg, 1))
return create_calldata_generic(
ex, arg, sevm, stack, step_id, contract_name, filename
ex, sevm, contract_name, filename
)


Expand All @@ -256,12 +256,12 @@ def create_calldata_file_contract_bool(ex, arg, sevm, stack, step_id):
"symbolic boolean flag for SVM.createCalldata()",
)
return create_calldata_generic(
ex, arg, sevm, stack, step_id, contract_name, filename, bool(include_view)
ex, sevm, contract_name, filename, bool(include_view)
)


def create_calldata_generic(
ex, arg, sevm, stack, step_id, contract_name, filename=None, include_view=False
ex, sevm, contract_name, filename=None, include_view=False
):
"""
Generate arbitrary symbolic calldata for the given contract.
Expand Down
4 changes: 2 additions & 2 deletions src/halmos/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ def get_by_name(self, contract_name: str, filename: str = None) -> dict:

if filename is None:
if len(mapping) > 1:
err_msg = f"{contract_name} exists in multiple files: {mapping.keys()}"
err_msg = f"{contract_name} exists in multiple files: {list(mapping.keys())}"
raise HalmosException(err_msg)
filename = list(mapping.keys())[0]
[filename] = mapping.keys()

result = mapping.get(filename)

Expand Down
3 changes: 2 additions & 1 deletion src/halmos/sevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2115,10 +2115,11 @@ def call_unknown() -> None:

ret_lst = ret if isinstance(ret, list) else [ret]

last_idx = len(ret_lst) - 1
for idx, ret_ in enumerate(ret_lst):
new_ex = (
self.create_branch(ex, BoolVal(True), ex.pc)
if idx < len(ret_lst) - 1
if idx < last_idx
else ex
)

Expand Down
6 changes: 4 additions & 2 deletions tests/regression/test/HalmosCheatCode.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ contract HalmosCheatCodeTest is SymTest, Test {

function check_createCalldata_Beep_1_excluding_pure() public {
bytes memory data = svm.createCalldata("HalmosCheatCode.t.sol", "Beep");
_check_createCalldata_Beep(data); // fail // not reachable, no calldata generated above
_check_createCalldata_Beep(data); // fail because the only function in Beep is pure, which is excluded in createCalldata()
}

function check_createCalldata_Beep_1() public {
Expand All @@ -151,7 +151,7 @@ contract HalmosCheatCodeTest is SymTest, Test {

function check_createCalldata_Beep_2_excluding_pure() public {
bytes memory data = svm.createCalldata("Beep");
_check_createCalldata_Beep(data); // fail // not reachable, no calldata generated above
_check_createCalldata_Beep(data); // fail because the only function in Beep is pure, which is excluded in createCalldata()
}

function check_createCalldata_Beep_2() public {
Expand All @@ -162,6 +162,7 @@ contract HalmosCheatCodeTest is SymTest, Test {
function _check_createCalldata_Beep(bytes memory data) public {
Beep beep = new Beep();
(bool success, bytes memory retdata) = address(beep).call(data);
vm.assume(success);
uint ret = abi.decode(retdata, (uint256));
assertEq(ret, 42);
}
Expand Down Expand Up @@ -209,6 +210,7 @@ contract HalmosCheatCodeTest is SymTest, Test {
function _check_createCalldata_Mock(bytes memory data) public {
Mock mock = new Mock();
(bool success, bytes memory retdata) = address(mock).call(data);
vm.assume(success);

bytes4 ret = abi.decode(retdata, (bytes4));
bytes4 expected = bytes4(bytes.concat(data[0], data[1], data[2], data[3]));
Expand Down

0 comments on commit dfaf98d

Please sign in to comment.