Skip to content

Commit

Permalink
update disassembler.py, create output file
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew <[email protected]>
  • Loading branch information
andrewn6 committed Nov 26, 2021
1 parent 8733077 commit 2702066
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
10 changes: 5 additions & 5 deletions evm/disassembler.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import sys
import os
#import capstone
# import capstone
from evm import EVM
# import convert_bytecode

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

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

with open("output", "w") as output:
output.write("FUNCTIONS:\n")
for addr, infpo in sorted(func_list.items()):
for addr, info in sorted(func_list.items()):
output.write(
" FUNC_{:04X}({}) -> ({})\n"
.format(
addr,
', '.join(get_list_with_prefix("arg", info[0])),
', '.join(get_list_with_prefix('arg', info[0])),
'. '.join(get_list_with_prefix("r", info[1])),
)
)
Expand Down
24 changes: 13 additions & 11 deletions evm/evm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, data, **kwargs):
self._table = {
int(k): v for k, v in json.load(opcode).items()}
self._terminal_ops = ['*STOP', '*RETURN', '*REVERT', '*BALANCE']
self._jumps_ops = ["*JUMP", "*JUMPI"]
self._jump_ops = ['*JUMP', '*JUMPI']

self._opcodes_func = {
0: self._stop,
Expand Down Expand Up @@ -179,7 +179,7 @@ def __init__(self, data, **kwargs):
255: self._selfdestruct,
}

def _insert_entry_list_dic(self, dict, k, v):
def _insert_entry_list_dict(self, dict, k, v):
if k not in dict:
dict[k] = []
if v not in dict[k]:
Expand Down Expand Up @@ -271,8 +271,10 @@ def _get_func_ret_vals(self, addr):


def _annotation_jump(self, addr, cond):
return '// Incoming jump from 0x{:04X}'.format(addr),
cond
return (
'// Incoming jump from 0x{:04X}'.format(addr),
cond
)

def _annotation_call(self):
return (
Expand All @@ -292,8 +294,9 @@ def _annotation_return(self, addr):
None
)
def disassemble(self):

self._recursive_run()
self.linear_run()
self._linear_run()
return self._visited, self._blocks, self._func_list

def _recursive_run(self):
Expand Down Expand Up @@ -329,7 +332,7 @@ def _recursive_run(self):
self._blocks,
self._pc,
# might have to concatenate not into cond
self._annotation_jump(self._pc - 1, "not" + str(cond))
self._annotation_jump(self._pc - 1, 'not ' + cond)
)

if type(jump_addr) != int:
Expand All @@ -339,7 +342,7 @@ def _recursive_run(self):
self._insert_entry_list_dict(
self._blocks,
jump_addr,
self._annotation_call(self._pc, - 1, cond)
self._annotation_call()
)

else:
Expand Down Expand Up @@ -525,8 +528,7 @@ def _mulmod(self):

self._stack.append('({} * {}) % {}'.format(operand_1, operand_2,
operand_3))



def _exp(self):
operand_1 = self._stack_pop()
operand_2 = self._stack_pop()
Expand Down Expand Up @@ -884,9 +886,9 @@ def _jumpdest(self):
return

def _push(self):
imm_width = int(self._table[self._data[self.pc - 1]][4:])
imm_width = int(self._table[self._data[self._pc - 1]][4:])
imm_val = self._data[self._pc:self._pc+imm_width].hex()
if len(self._visited[elf._pc - 1][0]) <= 6:
if len(self._visited[self._pc - 1][0]) <= 6:
self._visited[self._pc - 1][0] += '0x{}'.format(imm_val)
self._stack.append(int(imm_val, 16))
self._pc += imm_width
Expand Down
1 change: 1 addition & 0 deletions evm/output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FUNCTIONS:

0 comments on commit 2702066

Please sign in to comment.