mirror of https://github.com/ConsenSys/mythril
Merge pull request #730 from ConsenSys/refactor_class_structure
Refactor package and module structurepull/753/head
commit
888ceff7ee
@ -1,3 +0,0 @@ |
||||
import time |
||||
|
||||
start_time = time.time() |
@ -1,65 +0,0 @@ |
||||
from ethereum import vm, messages, transactions |
||||
from ethereum.state import State |
||||
from ethereum.slogging import get_logger |
||||
from mythril.ether import util |
||||
from logging import StreamHandler |
||||
from io import StringIO |
||||
import re |
||||
|
||||
|
||||
def trace(code, calldata=""): |
||||
log_handlers = [ |
||||
"eth.vm.op", |
||||
"eth.vm.op.stack", |
||||
"eth.vm.op.memory", |
||||
"eth.vm.op.storage", |
||||
] |
||||
output = StringIO() |
||||
stream_handler = StreamHandler(output) |
||||
|
||||
for handler in log_handlers: |
||||
log_vm_op = get_logger(handler) |
||||
log_vm_op.setLevel("TRACE") |
||||
log_vm_op.addHandler(stream_handler) |
||||
|
||||
addr = bytes.fromhex("0123456789ABCDEF0123456789ABCDEF01234567") |
||||
state = State() |
||||
|
||||
ext = messages.VMExt(state, transactions.Transaction(0, 0, 21000, addr, 0, addr)) |
||||
message = vm.Message(addr, addr, 0, 21000, calldata) |
||||
vm.vm_execute(ext, message, util.safe_decode(code)) |
||||
stream_handler.flush() |
||||
ret = output.getvalue() |
||||
lines = ret.split("\n") |
||||
|
||||
state_trace = [] |
||||
for line in lines: |
||||
m = re.search(r"pc=b\'(\d+)\'.*op=([A-Z0-9]+)", line) |
||||
if m: |
||||
pc = m.group(1) |
||||
op = m.group(2) |
||||
m = re.match(r".*stack=(\[.*?\])", line) |
||||
|
||||
if m: |
||||
stackitems = re.findall(r"b\'(\d+)\'", m.group(1)) |
||||
stack = "[" |
||||
|
||||
if len(stackitems): |
||||
for i in range(0, len(stackitems) - 1): |
||||
stack += hex(int(stackitems[i])) + ", " |
||||
stack += hex(int(stackitems[-1])) |
||||
|
||||
stack += "]" |
||||
else: |
||||
stack = "[]" |
||||
|
||||
if re.match(r"^PUSH.*", op): |
||||
val = re.search(r"pushvalue=(\d+)", line).group(1) |
||||
pushvalue = hex(int(val)) |
||||
state_trace.append( |
||||
{"pc": pc, "op": op, "stack": stack, "pushvalue": pushvalue} |
||||
) |
||||
else: |
||||
state_trace.append({"pc": pc, "op": op, "stack": stack}) |
||||
|
||||
return state_trace |
@ -1,41 +1,41 @@ |
||||
import unittest |
||||
from mythril.ether.ethcontract import ETHContract |
||||
from mythril.ethereum.evmcontract import EVMContract |
||||
|
||||
|
||||
class ETHContractTestCase(unittest.TestCase): |
||||
class EVMContractTestCase(unittest.TestCase): |
||||
def setUp(self): |
||||
self.code = "0x60606040525b603c5b60006010603e565b9050593681016040523660008237602060003683856040603f5a0204f41560545760206000f35bfe5b50565b005b73c3b2ae46792547a96b9f84405e36d0e07edcd05c5b905600a165627a7a7230582062a884f947232ada573f95940cce9c8bfb7e4e14e21df5af4e884941afb55e590029" |
||||
self.creation_code = "0x60606040525b603c5b60006010603e565b9050593681016040523660008237602060003683856040603f5a0204f41560545760206000f35bfe5b50565b005b73c3b2ae46792547a96b9f84405e36d0e07edcd05c5b905600a165627a7a7230582062a884f947232ada573f95940cce9c8bfb7e4e14e21df5af4e884941afb55e590029" |
||||
|
||||
|
||||
class Getinstruction_listTestCase(ETHContractTestCase): |
||||
class Getinstruction_listTestCase(EVMContractTestCase): |
||||
def runTest(self): |
||||
contract = ETHContract(self.code, self.creation_code) |
||||
contract = EVMContract(self.code, self.creation_code) |
||||
|
||||
disassembly = contract.disassembly |
||||
|
||||
self.assertEqual( |
||||
len(disassembly.instruction_list), |
||||
53, |
||||
"Error disassembling code using ETHContract.get_instruction_list()", |
||||
"Error disassembling code using EVMContract.get_instruction_list()", |
||||
) |
||||
|
||||
|
||||
class GetEASMTestCase(ETHContractTestCase): |
||||
class GetEASMTestCase(EVMContractTestCase): |
||||
def runTest(self): |
||||
contract = ETHContract(self.code) |
||||
contract = EVMContract(self.code) |
||||
|
||||
instruction_list = contract.get_easm() |
||||
|
||||
self.assertTrue( |
||||
"PUSH1 0x60" in instruction_list, |
||||
"Error obtaining EASM code through ETHContract.get_easm()", |
||||
"Error obtaining EASM code through EVMContract.get_easm()", |
||||
) |
||||
|
||||
|
||||
class MatchesExpressionTestCase(ETHContractTestCase): |
||||
class MatchesExpressionTestCase(EVMContractTestCase): |
||||
def runTest(self): |
||||
contract = ETHContract(self.code) |
||||
contract = EVMContract(self.code) |
||||
|
||||
self.assertTrue( |
||||
contract.matches_expression("code#PUSH1# or code#PUSH1#"), |
Loading…
Reference in new issue