Skip to content

Commit 2702066

Browse files
committed
update disassembler.py, create output file
Signed-off-by: Andrew <andrewnijmeh1@gmail.com>
1 parent 8733077 commit 2702066

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

evm/disassembler.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import sys
22
import os
3-
#import capstone
3+
# import capstone
44
from evm import EVM
55
# import convert_bytecode
66

77
def get_list_with_prefix(prefix, len):
8-
return ["{}{}".format(prefix, i) for i in range(len)]
8+
return ['{}{}'.format(prefix, i) for i in range(len)]
99

1010
if __name__ == "__main__":
11-
evm = EVM(bytes.fromhex(input('>> Enter your hex. ')))
11+
evm = EVM(bytes.fromhex(input('>> Enter your hex: ')))
1212
insts, func_list, blocks = evm.disassemble()
1313

1414
with open("output", "w") as output:
1515
output.write("FUNCTIONS:\n")
16-
for addr, infpo in sorted(func_list.items()):
16+
for addr, info in sorted(func_list.items()):
1717
output.write(
1818
" FUNC_{:04X}({}) -> ({})\n"
1919
.format(
2020
addr,
21-
', '.join(get_list_with_prefix("arg", info[0])),
21+
', '.join(get_list_with_prefix('arg', info[0])),
2222
'. '.join(get_list_with_prefix("r", info[1])),
2323
)
2424
)

evm/evm.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(self, data, **kwargs):
3030
self._table = {
3131
int(k): v for k, v in json.load(opcode).items()}
3232
self._terminal_ops = ['*STOP', '*RETURN', '*REVERT', '*BALANCE']
33-
self._jumps_ops = ["*JUMP", "*JUMPI"]
33+
self._jump_ops = ['*JUMP', '*JUMPI']
3434

3535
self._opcodes_func = {
3636
0: self._stop,
@@ -179,7 +179,7 @@ def __init__(self, data, **kwargs):
179179
255: self._selfdestruct,
180180
}
181181

182-
def _insert_entry_list_dic(self, dict, k, v):
182+
def _insert_entry_list_dict(self, dict, k, v):
183183
if k not in dict:
184184
dict[k] = []
185185
if v not in dict[k]:
@@ -271,8 +271,10 @@ def _get_func_ret_vals(self, addr):
271271

272272

273273
def _annotation_jump(self, addr, cond):
274-
return '// Incoming jump from 0x{:04X}'.format(addr),
275-
cond
274+
return (
275+
'// Incoming jump from 0x{:04X}'.format(addr),
276+
cond
277+
)
276278

277279
def _annotation_call(self):
278280
return (
@@ -292,8 +294,9 @@ def _annotation_return(self, addr):
292294
None
293295
)
294296
def disassemble(self):
297+
295298
self._recursive_run()
296-
self.linear_run()
299+
self._linear_run()
297300
return self._visited, self._blocks, self._func_list
298301

299302
def _recursive_run(self):
@@ -329,7 +332,7 @@ def _recursive_run(self):
329332
self._blocks,
330333
self._pc,
331334
# might have to concatenate not into cond
332-
self._annotation_jump(self._pc - 1, "not" + str(cond))
335+
self._annotation_jump(self._pc - 1, 'not ' + cond)
333336
)
334337

335338
if type(jump_addr) != int:
@@ -339,7 +342,7 @@ def _recursive_run(self):
339342
self._insert_entry_list_dict(
340343
self._blocks,
341344
jump_addr,
342-
self._annotation_call(self._pc, - 1, cond)
345+
self._annotation_call()
343346
)
344347

345348
else:
@@ -525,8 +528,7 @@ def _mulmod(self):
525528

526529
self._stack.append('({} * {}) % {}'.format(operand_1, operand_2,
527530
operand_3))
528-
529-
531+
530532
def _exp(self):
531533
operand_1 = self._stack_pop()
532534
operand_2 = self._stack_pop()
@@ -884,9 +886,9 @@ def _jumpdest(self):
884886
return
885887

886888
def _push(self):
887-
imm_width = int(self._table[self._data[self.pc - 1]][4:])
889+
imm_width = int(self._table[self._data[self._pc - 1]][4:])
888890
imm_val = self._data[self._pc:self._pc+imm_width].hex()
889-
if len(self._visited[elf._pc - 1][0]) <= 6:
891+
if len(self._visited[self._pc - 1][0]) <= 6:
890892
self._visited[self._pc - 1][0] += '0x{}'.format(imm_val)
891893
self._stack.append(int(imm_val, 16))
892894
self._pc += imm_width

evm/output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FUNCTIONS:

0 commit comments

Comments
 (0)