-
Notifications
You must be signed in to change notification settings - Fork 0
/
evm_stack.py
243 lines (209 loc) · 7.93 KB
/
evm_stack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from collections import namedtuple, defaultdict
from web3_connections import get_trace, get_transactionData
from dataclasses import dataclass
import web3
import json
from macaron_utils import print_err
import os.path
import pickle
import sys
memory_writes = {
"CALLDATACOPY": (-1, -3),
"CODECOPY": (-1, -3),
"EXTCODECOPY": (-2, -4),
"MSTORE": (-1, 32),
"MSTORE8": (-1, 1),
"CALL": (-6, -7),
"CALLCODE": (-6, -7),
"DELEGATECALL": (-5, -6),
}
memory_reads = {
"SHA3": (-1, -2),
"MLOAD": (-1, 32),
"CREATE": (-2, -3),
"CREATE2": (-2, -3),
"CALL": (-4, -5),
"STATICCALL": (-3, -4),
"CALLCODE": (-4, -5),
"RETURN": (-1, -2),
"DELEGATECALL": (-3, -4),
"LOG1": (-1, -2),
"LOG2": (-1, -2),
"LOG3": (-1, -2),
"LOG4": (-1, -2),
}
def get_mem_args(op, stack, **kwargs):
"""Returns start and end"""
if op in memory_writes:
start_index, length_index = memory_writes[op]
if op in memory_reads:
start_index, length_index = memory_reads[op]
if start_index < 0:
start_index = int(stack[start_index], 16)
if length_index < 0:
length_index = int(stack[length_index], 16)
return start_index, length_index
value_arg = {"CALL": -3, "CALLCODE": -3, "CREATE": -1, "CREATE2": -1}
calls = {"CALL", "CALLCODE", "STATICCALL", "DELEGATECALL", "CREATE", "CREATE2"}
MAX_MEMORY = 0x10000
class EVMExecuctionStack:
InstructionEntry = namedtuple("InstructionEntry", ["pc", "data"])
DataEntry = namedtuple("DataEntry", ["stack", "storage", "memory"])
@dataclass(init=True, repr=True, frozen=True)
class TraceEntry:
address: str
reason: str
detail: str = ""
value: int = 0
depth: int = 0
def __init__(self, rpc_endpoint):
self.api = web3.Web3(
web3.providers.HTTPProvider(rpc_endpoint, request_kwargs={"timeout": 60})
)
self.stack = []
self.trace = []
self.memory = []
self.calldatas = []
self.code = {}
self.instructions = defaultdict(list)
self.function_db = {}
def entry(self, transaction_data):
address, starting_calldata, value = (
transaction_data["to"][2:],
bytes.fromhex(transaction_data["input"][2:]),
int(transaction_data["value"], 16),
)
self.calldatas.append(starting_calldata)
self.stack.append(self.TraceEntry(address, "ENTRY", starting_calldata, value))
self.memory.append(bytearray(b"\0" * MAX_MEMORY))
self.do_trace("0x0:ENTRY", starting_calldata, value)
def do_trace(self, reason, detail="", value=0):
last_stack = self.stack[-1]
self.trace.append(
self.TraceEntry(
last_stack.address, reason, detail, value, depth=len(self.stack)
)
)
def mstore(self, index, entry):
index = int(index, 16)
self.memory[-1][index : index + 32] = bytearray.fromhex(entry)
def mstore8(self, index, entry):
index = int(index, 16)
self.memory[-1][index : index + 1] = bytearray.fromhex(entry[-2:])
def calldatacopy(self, memstart, cdstart, length):
memstart, cdstart, length = int(memstart, 16), int(cdstart, 16), int(length, 16)
self.memory[-1][memstart : memstart + length] = self.calldatas[-1][
cdstart : length + cdstart
]
def get_calldata_and_value(self, t):
op = t["op"]
stack = t["stack"]
start, length = memory_reads[op]
start, length = int(stack[start], 16), int(stack[length], 16)
calldata = bytes(self.memory[-1][start : start + length])
if op in value_arg:
value = int(t["stack"][value_arg[op]], 16)
else:
value = 0
return calldata, value
def call(self, address, t):
calldata, value = self.get_calldata_and_value(t)
self.calldatas.append(calldata)
self.memory.append(bytearray(b"\0" * MAX_MEMORY))
if t["op"] in ["CREATE", "CREATE2"]:
calldata = "New Contract Creation"
self.stack.append(self.TraceEntry(address, t["op"], calldata, value))
self.do_trace(reason=f'{hex(t["pc"])}:{t["op"]}', detail=calldata, value=value)
def codecopy(self, memstart, codestart, length):
memstart, codestart, length = (
int(memstart, 16),
int(codestart, 16),
int(length, 16),
)
current_address = self.stack[-1].address
if current_address not in self.code:
# get code
self.code[current_address] = bytes(
self.api.eth.getCode(self.api.toChecksumAddress(current_address))
)
copied = self.code[current_address][codestart : length + codestart]
copied += b"\0" * (length - len(copied)) # pad with 0 chars
self.memory[-1][memstart : memstart + length] = copied
def ret(self, t):
last_mem = self.memory[-1]
detail, value = self.stack[-1].detail, self.stack[-1].value
self.stack.pop()
self.memory.pop()
if t["op"] == "REVERT":
# get revert reason
start, length = int(t["stack"][-1], 16), int(t["stack"][-2], 16)
revert_reason = last_mem[start + 4 : start + length].decode("cp437")
self.do_trace(
reason=f'{hex(t["pc"])}:revert("{revert_reason}")',
detail=detail,
value=value,
)
else:
self.do_trace(
reason=f'{hex(t["pc"])}:{t["op"]}', detail=detail, value=value
)
def head(self):
return self.trace[-1]
def import_transaction(self, transaction, rpc_endpoint):
# Trace cache
if not os.path.exists("traces"):
os.makedirs("traces")
transaction_path = f"traces/{transaction}.pkl"
transaction_trace = None
try:
if os.path.isfile(transaction_path):
with open(transaction_path, "rb") as f:
transaction_trace = pickle.load(f)
else:
transaction_trace = get_trace(transaction, rpc_endpoint)["result"][
"structLogs"
]
with open(transaction_path, "wb") as f:
pickle.dump(transaction_trace, f)
except Exception as e:
print(e)
exit(1)
try:
transaction_data = get_transactionData(transaction, rpc_endpoint)
except Exception:
print_err(
f"Could not achieve a connection to rpc_endpoint '{rpc_endpoint}'"
)
exit(1)
if transaction_data["result"]["to"] == None:
print_err("Contract creation transactions cannot be traced")
exit(1)
# Begin replaying the trace
self.entry(transaction_data["result"])
prev_t = {"depth": 0, "op": None}
for i, t in enumerate(transaction_trace):
op = t["op"]
stack = t["stack"]
depth = t["depth"]
if depth < prev_t["depth"]:
self.ret(prev_t)
current_stack_entry = self.head()
pc = t["pc"]
self.instructions[current_stack_entry].append(
self.InstructionEntry(
pc, self.DataEntry(t["stack"], t["storage"], t["memory"])
)
)
if op == "MSTORE":
self.mstore(stack[-1], stack[-2])
if op == "MSTORE8":
self.mstore8(stack[-1], stack[-2])
if op == "CALLDATACOPY":
self.calldatacopy(stack[-1], stack[-2], stack[-3])
if op == "CODECOPY":
self.codecopy(stack[-1], stack[-2], stack[-3])
if op in calls and transaction_trace[i + 1]["depth"] != prev_t["depth"]:
# take next address from the stack, cast to 160-bits
call_address = stack[-2][-40:]
self.call(call_address, t)
prev_t = t