Skip to content

Commit

Permalink
Merge branch 'main' into structure_change
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcere committed Jan 20, 2025
2 parents 4fd3ca6 + f38ab87 commit d71f241
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/parser/cfg_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ def _build_spec_for_sequence(self, instructions: List[CFGInstruction], map_instr
# generate no instruction unless their output stack value is different
ins_spec = None
else:
map_positions_instructions[i] = ins_spec
map_positions_instructions[i] = ins_spec["id"]

if ins_spec is None:
result, new_out_idx = ins.build_spec(new_out_idx, instrs_idx, map_instructions)
Expand Down
9 changes: 7 additions & 2 deletions src/solution_generation/reconstruct_bytecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def id_to_asm_bytecode(uf_instrs: Dict[str, Dict[str, Any]], instr_id: str) -> A
return asm_from_op_info("PUSH", "0")
# Special PUSH cases that were transformed to decimal are analyzed separately
elif associated_instr['disasm'] == "PUSH" or associated_instr['disasm'] == "PUSH data" \
or associated_instr['disasm'] == "PUSHIMMUTABLE":
or associated_instr['disasm'] == "PUSHIMMUTABLE" or associated_instr['disasm'] == "ASSIGNIMMUTABLE":
value = to_hex_default(associated_instr['value'][0])
return asm_from_op_info(associated_instr['disasm'], value)
elif associated_instr["disasm"] == "PUSH [TAG]":
Expand Down Expand Up @@ -93,7 +93,12 @@ def asm_for_split_instruction(split_ins: CFGInstruction,

elif split_ins.get_op_name().startswith("verbatim"):
asm_ins = asm_from_op_info("VERBATIM",0) #WARNING: Value assigned to verbatim is 0


elif split_ins.get_op_name().startswith("assignimmutable"):
builtin_args = split_ins.get_builtin_args()
value = to_hex_default(builtin_args[0])
asm_ins = asm_from_op_info(split_ins.get_op_name().upper(), value if builtin_args is not None and len(builtin_args) > 0 else None)

else:
# Just include the corresponding instruction and the value field for builtin translations
builtin_args = split_ins.get_builtin_args()
Expand Down
51 changes: 51 additions & 0 deletions tests/test_cfg_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import glob

import pytest
from typing import Dict
from parser.parser import parse_instruction
from parser.cfg_block import CFGBlock, CFGInstruction
from parser.cfg_instruction import build_instr_spec, build_verbatim_spec, build_push_spec, CFGInstruction
from greedy.greedy import SMSgreedy, greedy_from_file
import json


class TestCFGBlock:

def test_keccak_array_position(self):
"""
Problem: the dict "map_position" in build spec introduced a dict in the memory_dependencies when a KECCAK256
was introduced more than once.
Solution: assign ["id"]
"""
instructions = [{"in": [], "out": ["c280"], "op": "push", "builtinArgs": ["0xff"]},
{"in": [], "out": ["c279"], "op": "push",
"builtinArgs": ["0xffffffffffffffffffffffffffffffffffffffff"]},
{"in": [], "out": ["c278"], "op": "push", "builtinArgs": ["0x40"]},
{"in": [], "out": ["c277"], "op": "push", "builtinArgs": ["0x97"]},
{"in": [], "out": ["c276"], "op": "push", "builtinArgs": ["0x20"]},
{"in": [], "out": ["c275"], "op": "push", "builtinArgs": ["0x00"]},
{"in": ["c275", "v84_f313_1_f482_1"], "out": [], "op": "mstore"},
{"in": ["c276", "c277"], "out": [], "op": "mstore"},
{"in": ["c275", "c278"], "out": ["v2496_f312_3_f521_0"], "op": "keccak256"},
{"in": ["v2496_f312_3_f521_0", "c275"], "out": ["v1972_f521_0"], "op": "add"},
{"in": ["v62_f428_3_f482_1", "c279"], "out": ["v994_f79_2_f253_1_f359_0_f431_4_f486_2_f521_0"],
"op": "and"},
{"in": ["v994_f79_2_f253_1_f359_0_f431_4_f486_2_f521_0", "c279"],
"out": ["v994_f79_3_f253_1_f359_0_f431_4_f486_2_f521_0"], "op": "and"},
{"in": ["c275", "v994_f79_3_f253_1_f359_0_f431_4_f486_2_f521_0"], "out": [], "op": "mstore"},
{"in": ["c276", "v1972_f521_0"], "out": [], "op": "mstore"},
{"in": ["c275", "c278"], "out": ["v2488_f486_2_f521_0"], "op": "keccak256"},
{"in": ["v2488_f486_2_f521_0"], "out": ["v2638_f379_1_f521_0"], "op": "sload"},
{"in": ["c275", "v2638_f379_1_f521_0"], "out": ["v2770_f81_2_f379_1_f521_0"], "op": "shr"},
{"in": ["v2770_f81_2_f379_1_f521_0", "c280"], "out": ["v950_f126_0_f379_1_f521_0"],
"op": "and"},
{"in": [], "out": ["v1382"], "op": "allocate_unbounded"}]
instructions_cfg = [parse_instruction(instruction) for instruction in instructions]
block = CFGBlock("validator_revert_t_address_Block2_copy_3_copy_1", instructions_cfg, "terminal", {})
input_stack = ['v62_f428_3_f482_1', 'v84_f313_1_f482_1']
output_stack = ['v1382', 'v950_f126_0_f379_1_f521_0']
sfs = block.build_spec(input_stack ,output_stack)

assert all(isinstance(term, str) for dep in sfs["memory_dependences"] for term in dep), \
"Falla test keccak_array_position"

0 comments on commit d71f241

Please sign in to comment.