forked from PKU-ASAL/WASEM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicblock.py
More file actions
executable file
·78 lines (69 loc) · 2.61 KB
/
Copy pathbasicblock.py
File metadata and controls
executable file
·78 lines (69 loc) · 2.61 KB
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
class BasicBlock(object):
"""
The basic block in the CFG, consisting of instructions
"""
def __init__(self, start_offset=0x00, start_instr=None,
name='block_default_name'):
"""
The properties of basic blocks
Properties:
start_offset: the `offset` of the first instruction
start_instr: the first instruction of the current basic block
name: the name of the basic block, whose naming style is "block_[func_index]_[start_offset]"
end_offset: the `offset_end` of the last instruction
end_instr: the last instruction
Below are properties may be deprecated in the future
states: not clear
function_name: its corresponding function's name
"""
self.start_offset = start_offset
self.start_instr = start_instr
self.name = name
self.end_offset = None
self.end_instr = None
self.instructions = list()
# may be deprecated in the future
self.states = []
self.function_name = "unknown"
@property
def size(self):
return self.end_offset - self.start_offset
def __str__(self):
out = ''
line = ''
line = str(self.start_offset) + ': ' + str(self.name) + '\n'
line += 'start_instr = ' + str(self.start_instr.name) + '\n'
line += 'size = ' + str(self.size) + '\n'
line += 'end_offset = ' + str(self.end_offset) + '\n'
line += 'end_instr = ' + str(self.end_instr.name) + '\n'
line += 'function_name = ' + str(self.function_name) + '\n'
out += line + '\n\n'
return out
def instructions_details(self, format='hex'):
out = ''
line = ''
for i in self.instructions:
line = '%x: ' % i.offset
if i.operand is not None and not i.xref:
line += '%s' % str(i)
elif isinstance(i.xref, list) and i.xref:
line += '%s %s' % (i.name, i.xref)
elif isinstance(i.xref, int) and i.xref:
line += '%s %x' % (i.name, i.xref)
elif i.operand_interpretation:
line += i.operand_interpretation
else:
line += i.name + ' '
out += line + '\n'
return out
def instructions_ssa(self, format='hex'):
out = ''
line = ''
for i in self.instructions:
line = '%x: ' % i.offset
if i.ssa:
line += '' + i.ssa.format()
else:
line += '[NO_SSA] ' + i.name
out += line + '\n'
return out