コード例 #1
0
 def __init__(
     self,
     world_state: WorldState,
     caller: ExprRef = None,
     call_data=None,
     identifier: Optional[str] = None,
     gas_price=None,
     gas_limit=None,
     origin=None,
     code=None,
     call_value=None,
     contract_name=None,
 ) -> None:
     self.prev_world_state = deepcopy(world_state)
     callee_account = world_state.create_account(0,
                                                 concrete_storage=True,
                                                 creator=caller.value)
     callee_account.contract_name = contract_name
     # TODO: set correct balance for new account
     super().__init__(
         world_state=world_state,
         callee_account=callee_account,
         caller=caller,
         call_data=call_data,
         identifier=identifier,
         gas_price=gas_price,
         gas_limit=gas_limit,
         origin=origin,
         code=code,
         call_value=call_value,
         init_call_data=False,
     )
コード例 #2
0
ファイル: create_test.py プロジェクト: xahiru/mythril
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
コード例 #3
0
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
コード例 #4
0
ファイル: transaction_models.py プロジェクト: norhh/mythril
    def __init__(
        self,
        world_state: WorldState,
        caller: ExprRef,
        identifier=None,
        callee_account=None,
        code=None,
        call_data=None,
        gas_price=None,
        call_value=None,
        origin=None,
        call_data_type=None,
    ):
        assert isinstance(world_state, WorldState)
        self.id = identifier or get_next_transaction_id()
        self.world_state = world_state
        # TODO: set correct balance for new account
        self.callee_account = (
            callee_account
            if callee_account
            else world_state.create_account(0, concrete_storage=True)
        )

        self.caller = caller

        self.gas_price = (
            BitVec("gasprice{}".format(identifier), 256)
            if gas_price is None
            else gas_price
        )
        self.call_value = (
            BitVec("callvalue{}".format(identifier), 256)
            if call_value is None
            else call_value
        )
        self.origin = (
            BitVec("origin{}".format(identifier), 256) if origin is None else origin
        )
        self.call_data_type = (
            BitVec("call_data_type{}".format(identifier), 256)
            if call_data_type is None
            else call_data_type
        )

        self.call_data = (
            Calldata(self.id, call_data)
            if not isinstance(call_data, Calldata)
            else call_data
        )
        self.origin = origin
        self.code = code
        self.return_data = None
コード例 #5
0
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]
コード例 #6
0
def test_codecopy_concrete():
    # Arrange
    world_state = WorldState()
    account = world_state.create_account(balance=10, address=101)
    account.code = Disassembly("60606040")
    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))

    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
コード例 #7
0
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
コード例 #8
0
ファイル: transaction_models.py プロジェクト: xahiru/mythril
 def __init__(
     self,
     world_state: WorldState,
     caller: ExprRef = None,
     call_data=None,
     identifier: Optional[str] = None,
     gas_price=None,
     gas_limit=None,
     origin=None,
     code=None,
     call_value=None,
     contract_name=None,
     contract_address=None,
 ) -> None:
     self.prev_world_state = deepcopy(world_state)
     contract_address = (contract_address if isinstance(
         contract_address, int) else None)
     callee_account = world_state.create_account(0,
                                                 concrete_storage=True,
                                                 creator=caller.value,
                                                 address=contract_address)
     callee_account.contract_name = contract_name or callee_account.contract_name
     # init_call_data "should" be false, but it is easier to model the calldata symbolically
     # and add logic in codecopy/codesize/calldatacopy/calldatasize than to model code "correctly"
     super().__init__(
         world_state=world_state,
         callee_account=callee_account,
         caller=caller,
         call_data=call_data,
         identifier=identifier,
         gas_price=gas_price,
         gas_limit=gas_limit,
         origin=origin,
         code=code,
         call_value=call_value,
         init_call_data=True,
     )
コード例 #9
0
from mythril.disassembler.disassembly import Disassembly
from mythril.laser.ethereum.state.environment import Environment
from mythril.laser.ethereum.state.account import Account
from mythril.laser.ethereum.state.machine_state import MachineState
from mythril.laser.ethereum.state.global_state import GlobalState
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