def test_stack_pop_too_many(initial_size, overflow): # Arrange machine_state = MachineState(8000000) machine_state.stack = [42] * initial_size # Act + Assert with pytest.raises(StackUnderflowException): machine_state.pop(initial_size + overflow)
def test_stack_single_pop(): # Arrange machine_state = MachineState(8000000) machine_state.stack = [1, 2, 3] # Act result = machine_state.pop() # Assert assert isinstance(result, int)
def test_stack_multiple_pop_(): # Arrange machine_state = MachineState(8000000) machine_state.stack = [1, 2, 3] # Act a, b = machine_state.pop(2) # Assert assert a == 3 assert b == 2
def test_stack_multiple_pop(initial_stack, amount, expected): # Arrange machine_state = MachineState(8000000) machine_state.stack = initial_stack[:] # Act results = machine_state.pop(amount) # Assert assert results == initial_stack[-amount:][::-1] assert results == expected assert len(machine_state.stack) == len(initial_stack) - amount
def test_memory_extension(initial_size, start, extension_size): # Arrange machine_state = MachineState(gas_limit=8000000) machine_state.memory = [0] * initial_size # Act machine_state.mem_extend(start, extension_size) # Assert assert machine_state.memory_size == len(machine_state.memory) assert machine_state.memory_size == max(initial_size, start + extension_size)
def test_memory_write(initial_size, memory_offset, data): # Arrange machine_state = MachineState(8000000) machine_state.memory = [0] * initial_size # Act machine_state.memory_write(memory_offset, data) # Assert assert len(machine_state.memory) == max(initial_size, memory_offset + len(data)) assert machine_state.memory[memory_offset:memory_offset + len(data)] == data
def test_memory_extension(initial_size, start, extension_size): # Arrange machine_state = MachineState(gas_limit=8000000) machine_state.memory = Memory() machine_state.memory.extend(initial_size) # Act machine_state.mem_extend(start, extension_size) # Assert assert machine_state.memory_size == len(machine_state.memory) assert machine_state.memory_size == max( initial_size, (utils.ceil32(start + extension_size) // 32) * 32 )
def execute_create(): global last_state global created_contract_account if not last_state and not created_contract_account: code_raw = [] for i in range(len(contract_init_code) // 2): code_raw.append(int(contract_init_code[2 * i:2 * (i + 1)], 16)) calldata = ConcreteCalldata(0, code_raw) world_state = WorldState() account = world_state.create_account(balance=1000000, address=101) account.code = Disassembly("60a760006000f000") environment = Environment(account, None, calldata, None, None, None) og_state = GlobalState(world_state, environment, None, MachineState(gas_limit=8000000)) og_state.transaction_stack.append( (MessageCallTransaction(world_state=WorldState(), gas_limit=8000000), None)) laser = LaserEVM() states = [og_state] last_state = og_state for state in states: new_states, op_code = laser.execute_state(state) last_state = state if op_code == "STOP": break states.extend(new_states) created_contract_address = last_state.mstate.stack[-1].value created_contract_account = last_state.world_state.accounts[ created_contract_address] return last_state, created_contract_account
def __init__( self, world_state: "WorldState", environment: Environment, node: Node, machine_state=None, transaction_stack=None, last_return_data=None, annotations=None, ) -> None: """Constructor for GlobalState. :param world_state: :param environment: :param node: :param machine_state: :param transaction_stack: :param last_return_data: :param annotations: """ self.node = node self.world_state = world_state self.environment = environment self.mstate = (machine_state if machine_state else MachineState( gas_limit=1000000000)) self.transaction_stack = transaction_stack if transaction_stack else [] self.op_code = "" self.last_return_data = last_return_data self._annotations = annotations or []
def get_state(): active_account = Account("0x0", code=Disassembly("60606040")) environment = Environment(active_account, None, None, None, None, None) state = GlobalState(None, environment, None, MachineState(gas_limit=8000000)) state.transaction_stack.append( (MessageCallTransaction(world_state=WorldState(), gas_limit=8000000), None) ) return state
def _get_global_state(): active_account = Account("0x0", code=Disassembly("60606040")) passive_account = Account("0x325345346564645654645", code=Disassembly("6060604061626364")) environment = Environment(active_account, None, None, None, None, None) world_state = WorldState() world_state.put_account(active_account) world_state.put_account(passive_account) return GlobalState(world_state, environment, None, MachineState(gas_limit=8000000))
def test_execute(mocker): active_account = Account("0x00") environment = Environment(active_account, None, None, None, None, None) state_1 = GlobalState(None, environment, None, MachineState(gas_limit=8000000)) state_1.mstate.stack = [1, 2] mocker.patch.object(state_1, "get_current_instruction") state_1.get_current_instruction.return_value = {"opcode": "PUSH"} state_2 = GlobalState(None, environment, None, MachineState(gas_limit=8000000)) state_2.mstate.stack = [1, 2, 3] mocker.patch.object(state_2, "get_current_instruction") state_2.get_current_instruction.return_value = {"opcode": "ADD"} node_1 = Node("Test contract") node_1.states = [state_1, state_2] state_3 = GlobalState(None, environment, None, MachineState(gas_limit=8000000)) state_3.mstate.stack = [1, 2] mocker.patch.object(state_3, "get_current_instruction") state_3.get_current_instruction.return_value = {"opcode": "ADD"} node_2 = Node("Test contract") node_2.states = [state_3] edge = Edge(node_1.uid, node_2.uid) statespace = LaserEVM(None) statespace.edges = [edge] statespace.nodes[node_1.uid] = node_1 statespace.nodes[node_2.uid] = node_2 # Act result = TaintRunner.execute(statespace, node_1, state_1, [True, True]) # Assert print(result) assert len(result.records) == 3 assert result.records[2].states == [] assert state_3 in result.records[1].states
def get_state(): world_state = WorldState() account = world_state.create_account(balance=10, address=101) account.code = Disassembly("60606040") environment = Environment(account, None, None, None, None, None) state = GlobalState(world_state, environment, None, MachineState(gas_limit=8000000)) state.transaction_stack.append( (MessageCallTransaction(world_state=WorldState(), gas_limit=8000000), None)) return state
def test_codecopy_concrete(): # Arrange active_account = Account("0x0", code=Disassembly("60606040")) environment = Environment(active_account, None, None, None, None, None) og_state = GlobalState(None, environment, None, MachineState(gas=10000000)) og_state.mstate.stack = [2, 2, 2] instruction = Instruction("codecopy", dynamic_loader=None) # Act new_state = instruction.evaluate(og_state)[0] # Assert assert new_state.mstate.memory[2] == 96 assert new_state.mstate.memory[3] == 64
def __init__( self, world_state: "WorldState", environment: Environment, node: Node, machine_state=None, transaction_stack=None, last_return_data=None, ): """ Constructor for GlobalState""" self.node = node self.world_state = world_state self.environment = environment self.mstate = machine_state if machine_state else MachineState( gas=10000000) self.transaction_stack = transaction_stack if transaction_stack else [] self.op_code = "" self.last_return_data = last_return_data
def test_codecopy_concrete(): # Arrange active_account = Account("0x0", code=Disassembly("60606040")) environment = Environment(active_account, None, None, None, None, None) og_state = GlobalState(None, environment, None, MachineState(gas_limit=8000000)) og_state.transaction_stack.append( (MessageCallTransaction(world_state=WorldState(), gas_limit=8000000), None)) og_state.mstate.stack = [2, 2, 2] instruction = Instruction("codecopy", dynamic_loader=None) # Act new_state = instruction.evaluate(og_state)[0] # Assert assert new_state.mstate.memory[2] == 96 assert new_state.mstate.memory[3] == 64
def test_extcodecopy_fail(): # Arrange new_world_state = WorldState() new_account = new_world_state.create_account(balance=10, address=101) new_account.code = Disassembly("60616240") new_environment = Environment(new_account, None, None, None, None, None) state = GlobalState( new_world_state, new_environment, None, MachineState(gas_limit=8000000) ) state.transaction_stack.append( (MessageCallTransaction(world_state=WorldState(), gas_limit=8000000), None) ) state.mstate.stack = [2, 2, 2, symbol_factory.BitVecSym("FAIL", 256)] instruction = Instruction("extcodecopy", dynamic_loader=None) # Act new_state = instruction.evaluate(state)[0] # Assert assert new_state.mstate.stack == [] assert new_state.mstate.memory._memory == state.mstate.memory._memory
def test_extcodecopy(): # Arrange new_world_state = WorldState() new_account = new_world_state.create_account(balance=10, address=101) new_account.code = Disassembly("60616240") ext_account = new_world_state.create_account(balance=1000, address=121) ext_account.code = Disassembly("6040404040") new_environment = Environment(new_account, None, None, None, None, None) state = GlobalState( new_world_state, new_environment, None, MachineState(gas_limit=8000000) ) state.transaction_stack.append( (MessageCallTransaction(world_state=WorldState(), gas_limit=8000000), None) ) state.mstate.stack = [3, 0, 0, 121] instruction = Instruction("extcodecopy", dynamic_loader=None) # Act new_state = instruction.evaluate(state)[0] # Assert assert new_state.mstate.memory[0:3] == [96, 64, 64]
import pytest import random from mythril.laser.ethereum.state.global_state import GlobalState from mythril.laser.ethereum.state.machine_state import MachineState from mythril.laser.ethereum.strategy.basic import ReturnWeightedRandomStrategy from mythril.laser.ethereum.strategy.graph import SimpleGraph test_worklist_input = [([ GlobalState(None, None, None, machine_state=MachineState(gas=100, depth=2)), GlobalState(None, None, None, machine_state=MachineState(gas=100, depth=10)), GlobalState(None, None, None, machine_state=MachineState(gas=100, depth=20)), ])] @pytest.mark.parametrize("work_list", test_worklist_input) def test_random_weighted_strategy(work_list): strategy = ReturnWeightedRandomStrategy(SimpleGraph(), max_depth=30) strategy.graph.work_list = work_list counter = {} iterations = 10000 eps = 1e-2 random.seed(1) total_sum = sum( [1 / (1 + global_state.mstate.depth) for global_state in work_list])
from mythril.laser.ethereum.state.world_state import WorldState from mythril.laser.ethereum.instructions import Instruction from mythril.laser.ethereum.transaction.transaction_models import MessageCallTransaction from mythril.support.support_utils import get_code_hash from mythril.laser.smt import symbol_factory # Arrange world_state = WorldState() account = world_state.create_account(balance=10, address=101) account.code = Disassembly("60606040") world_state.create_account(balance=10, address=1000) environment = Environment(account, None, None, None, None, None) og_state = GlobalState(world_state, environment, None, MachineState(gas_limit=8000000)) og_state.transaction_stack.append( (MessageCallTransaction(world_state=WorldState(), gas_limit=8000000), None)) instruction = Instruction("extcodehash", dynamic_loader=None) def test_extcodehash_no_account(): # If account does not exist, return 0 og_state.mstate.stack = [symbol_factory.BitVecVal(1, 256)] new_state = instruction.evaluate(og_state)[0] assert new_state.mstate.stack[-1] == 0