-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdisassembler.py
75 lines (60 loc) · 2.39 KB
/
disassembler.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
import opcodes
import instruction
class Disassembler:
def __init__(self, bytecode: bytes):
self.bytecode = bytecode
self.instructions: dict[int, instruction.Instruction] = {}
self.instructions_list: list[instruction.Instruction] = []
self.jumpdests: set[int] | None = None
self.invalid_jumpdests: set[int] | None = None
self.opcodes: set[int] = set()
def disassemble(self):
offset, pc, bytecode = 0, 0, self.bytecode
end = len(bytecode)
dead = False
while offset < end:
opcode = bytecode[offset]
if opcode == opcodes.JUMPDEST:
dead = False
if opcode in opcodes.opcodes:
push_data_size = opcodes.opcodes[opcode][1]
else:
push_data_size = 0
push_data = self.get_push_data(offset + 1, push_data_size, end)
inst = instruction.Instruction(offset, pc, opcode, push_data)
self.add_instruction(inst, dead=dead)
if inst.is_halt_or_unconditional_jump_op():
dead = True
offset += 1 + push_data_size
pc += 1
if not dead:
# append STOP instruction
inst = instruction.Instruction(offset, pc, opcodes.STOP, None)
self.add_instruction(inst, dead=dead)
self.jumpdests = {
offset
for offset, inst in self.instructions.items()
if inst.opcode == opcodes.JUMPDEST
}
self.invalid_jumpdests = {0, 2, 7} - self.jumpdests
def add_instruction(self, inst: instruction.Instruction, dead: bool):
self.instructions[inst.offset] = inst
self.instructions_list.append(inst)
if not dead:
self.opcodes.add(inst.opcode)
def at(self, pc=None, offset=None) -> instruction.Instruction:
if pc is not None:
return self.instructions_list[pc]
else:
return self.instructions[offset]
def get_push_data(self, offset: int, push_data_size, end):
if not push_data_size:
return None
data_end = offset + push_data_size
if data_end <= end:
data_bytes = self.bytecode[offset:data_end]
else:
# append 0s
data_bytes = self.bytecode[offset:end] + bytes(data_end - end)
push_data = int.from_bytes(data_bytes, "big")
return push_data