Refactor the keccakmanager and use concrete storage

storage/refactor
Nikhil Parasaram 6 years ago
parent 5f24d23438
commit 7bce67d7ae
  1. 24
      mythril/laser/ethereum/instructions.py
  2. 14
      mythril/laser/ethereum/keccak.py
  3. 2
      mythril/laser/ethereum/transaction/symbolic.py
  4. 2
      mythril/laser/ethereum/transaction/transaction_models.py

@ -56,8 +56,6 @@ log = logging.getLogger(__name__)
TT256 = 2 ** 256
TT256M1 = 2 ** 256 - 1
keccak_function_manager = KeccakFunctionManager()
class StateTransition(object):
"""Decorator that handles global state copy and original return.
@ -901,7 +899,6 @@ class Instruction:
:param global_state:
:return:
"""
global keccak_function_manager
state = global_state.mstate
op0, op1 = state.stack.pop(), state.stack.pop()
@ -1360,25 +1357,24 @@ class Instruction:
:param global_state:
:return:
"""
global keccak_function_manager
state = global_state.mstate
index = state.stack.pop()
log.debug("Storage access at index " + str(index))
index = simplify(index)
if not keccak_function_manager.is_keccak(index):
if not KeccakFunctionManager.is_keccak(index):
return self._sload_helper(global_state, index)
storage_keys = global_state.environment.active_account.storage.keys()
keccak_keys = list(filter(keccak_function_manager.is_keccak, storage_keys))
keccak_keys = list(filter(KeccakFunctionManager.is_keccak, storage_keys))
results = [] # type: List[GlobalState]
constraints = []
for keccak_key in keccak_keys:
key_argument = keccak_function_manager.get_argument(keccak_key)
index_argument = keccak_function_manager.get_argument(index)
key_argument = KeccakFunctionManager.get_argument(keccak_key)
index_argument = KeccakFunctionManager.get_argument(index)
if key_argument.size() != index_argument.size():
continue
constraints.append((keccak_key, key_argument == index_argument))
@ -1430,11 +1426,10 @@ class Instruction:
:param this_key:
:param argument:
"""
global keccak_function_manager
for keccak_key in keccak_keys:
if keccak_key == this_key:
continue
keccak_argument = keccak_function_manager.get_argument(keccak_key)
keccak_argument = KeccakFunctionManager.get_argument(keccak_key)
yield keccak_argument != argument
@StateTransition()
@ -1444,27 +1439,26 @@ class Instruction:
:param global_state:
:return:
"""
global keccak_function_manager
state = global_state.mstate
index, value = state.stack.pop(), state.stack.pop()
log.debug("Write to storage[" + str(index) + "]")
index = simplify(index)
is_keccak = keccak_function_manager.is_keccak(index)
is_keccak = KeccakFunctionManager.is_keccak(index)
if not is_keccak:
return self._sstore_helper(global_state, index, value)
storage_keys = global_state.environment.active_account.storage.keys()
keccak_keys = filter(keccak_function_manager.is_keccak, storage_keys)
keccak_keys = filter(KeccakFunctionManager.is_keccak, storage_keys)
results = [] # type: List[GlobalState]
new = symbol_factory.Bool(False)
for keccak_key in keccak_keys:
key_argument = keccak_function_manager.get_argument(
key_argument = KeccakFunctionManager.get_argument(
keccak_key
) # type: Expression
index_argument = keccak_function_manager.get_argument(
index_argument = KeccakFunctionManager.get_argument(
index
) # type: Expression
if key_argument.size() != index_argument.size():

@ -7,11 +7,8 @@ from typing import cast, Union
class KeccakFunctionManager:
"""A keccak function manager for symbolic expressions."""
def __init__(self):
""""""
self.keccak_expression_mapping = {}
def is_keccak(self, expression: Expression) -> bool:
@staticmethod
def is_keccak(expression: Expression) -> bool:
"""
:param expression:
@ -21,12 +18,13 @@ class KeccakFunctionManager:
return False
return expression.func_name == "keccak256"
def get_argument(self, expression: Expression) -> Expression:
@staticmethod
def get_argument(expression: Expression) -> Expression:
"""
:param expression:
:return:
"""
if not self.is_keccak(expression):
raise ValueError("Expression is not a recognized keccac result")
if not KeccakFunctionManager.is_keccak(expression):
raise ValueError("Expression is not a recognized keccak result")
return cast(BitVecFunc, expression).input_

@ -73,7 +73,7 @@ def execute_contract_creation(
del laser_evm.open_states[:]
new_account = laser_evm.world_state.create_account(
0, concrete_storage=False, dynamic_loader=None, creator=CREATOR_ADDRESS
0, concrete_storage=True, dynamic_loader=None, creator=CREATOR_ADDRESS
)
if contract_name:
new_account.contract_name = contract_name

@ -154,7 +154,7 @@ class ContractCreationTransaction(BaseTransaction):
super().__init__(*args, **kwargs, init_call_data=False) # type: ignore
# TODO: set correct balance for new account
self.callee_account = self.callee_account or self.world_state.create_account(
0, concrete_storage=False
0, concrete_storage=True
)
def initial_global_state(self) -> GlobalState:

Loading…
Cancel
Save