From c3865b0a2e082fbab7a36003a9da7c6733bcf581 Mon Sep 17 00:00:00 2001 From: Dominik Muhs Date: Sun, 9 Sep 2018 13:03:45 +0200 Subject: [PATCH] Refactor instruction decorator into smaller methods --- mythril/laser/ethereum/instructions.py | 33 +++++++++++++++++--------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/mythril/laser/ethereum/instructions.py b/mythril/laser/ethereum/instructions.py index 10a3fb4b..71b380f9 100644 --- a/mythril/laser/ethereum/instructions.py +++ b/mythril/laser/ethereum/instructions.py @@ -30,17 +30,28 @@ class StopSignal(Exception): class PCMutator(object): """ Wrapper that handles copy and original return """ - def __init__(self, auto_increment=True): - self.auto_increment = auto_increment + def __init__(self, increment_pc=True): + self.increment_pc = increment_pc + + @staticmethod + def call_on_state_copy(func, func_obj, state): + global_state_copy = copy(state) + return func(func_obj, global_state_copy) + + def increment_states_pc(self, states): + if self.increment_pc: + for state in states: + state.mstate.pc += 1 + return states def __call__(self, func): - def wrapper(obj, global_state): - global_state_copy = copy(global_state) - new_global_states = func(obj, global_state_copy) - if self.auto_increment: - for state in new_global_states: - state.mstate.pc += 1 - return new_global_states + def wrapper(func_obj, global_state): + new_global_states = self.call_on_state_copy( + func, + func_obj, + global_state + ) + return self.increment_states_pc(new_global_states) return wrapper @@ -775,7 +786,7 @@ class Instruction: logging.debug("Error writing to storage: Invalid index") return [global_state] - @PCMutator(auto_increment=False) + @PCMutator(increment_pc=False) def jump_(self, global_state): state = global_state.mstate disassembly = global_state.environment.code @@ -804,7 +815,7 @@ class Instruction: return [new_state] - @PCMutator(auto_increment=False) + @PCMutator(increment_pc=False) def jumpi_(self, global_state): state = global_state.mstate disassembly = global_state.environment.code