-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsource_map.py
195 lines (169 loc) · 6.43 KB
/
source_map.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
import re
import ast
import json
import global_params
from utils import run_command
from ast_helper import AstHelper
class Source:
def __init__(self, filename):
self.filename = filename
self.content = self._load_content()
self.line_break_positions = self._load_line_break_positions()
def _load_content(self):
with open(self.filename, 'r') as f:
content = f.read()
return content
def _load_line_break_positions(self):
return [i for i, letter in enumerate(self.content) if letter == '\n']
class SourceMap:
parent_filename = ""
position_groups = {}
sources = {}
ast_helper = None
def __init__(self, cname, parent_filename, input_type, root_path=""):
self.root_path = root_path
self.cname = cname
self.input_type = input_type
if not SourceMap.parent_filename:
SourceMap.parent_filename = parent_filename
if input_type == "solidity":
SourceMap.position_groups = SourceMap._load_position_groups()
elif input_type == "standard json":
SourceMap.position_groups = SourceMap._load_position_groups_standard_json()
else:
raise Exception("There is no such type of input")
SourceMap.ast_helper = AstHelper(SourceMap.parent_filename, input_type)
self.source = self._get_source()
self.positions = self._get_positions()
self.instr_positions = {}
self.var_names = self._get_var_names()
self.func_call_names = self._get_func_call_names()
self.callee_src_pairs = self._get_callee_src_pairs()
def get_source_code(self, pc):
try:
pos = self.instr_positions[pc]
except:
return ""
begin = pos['begin']
end = pos['end']
return self.source.content[begin:end]
def get_source_code_from_src(self, src):
src = src.split(":")
start = int(src[0])
end = start + int(src[1])
return self.source.content[start:end]
def get_buggy_line(self, pc):
try:
pos = self.instr_positions[pc]
except:
return ""
location = self.get_location(pc)
begin = self.source.line_break_positions[location['begin']['line'] - 1] + 1
end = pos['end']
return self.source.content[begin:end]
def get_buggy_line_from_src(self, src):
pos = self._convert_src_to_pos(src)
location = self.get_location_from_src(src)
begin = self.source.line_break_positions[location['begin']['line'] - 1] + 1
end = pos['end']
return self.source.content[begin:end]
def get_location(self, pc):
pos = self.instr_positions[pc]
return self._convert_offset_to_line_column(pos)
def get_location_from_src(self, src):
pos = self._convert_src_to_pos(src)
return self._convert_offset_to_line_column(pos)
def is_a_parameter_or_state_variable(self, var_name):
try:
names = [
node.id for node in ast.walk(ast.parse(var_name))
if isinstance(node, ast.Name)
]
if names[0] in self.var_names:
return True
except:
return False
return False
def _convert_src_to_pos(self, src):
pos = {}
src = src.split(":")
pos['begin'] = int(src[0])
length = int(src[1])
pos['end'] = pos['begin'] + length - 1
return pos
def _get_source(self):
fname = self.get_filename()
if fname in SourceMap.sources:
return SourceMap.sources[fname]
else:
SourceMap.sources[fname] = Source(fname)
return SourceMap.sources[fname]
def _get_callee_src_pairs(self):
return SourceMap.ast_helper.get_callee_src_pairs(self.cname)
def _get_var_names(self):
return SourceMap.ast_helper.extract_state_variable_names(self.cname)
def _get_func_call_names(self):
func_call_srcs = SourceMap.ast_helper.extract_func_call_srcs(self.cname)
func_call_names = []
for src in func_call_srcs:
src = src.split(":")
start = int(src[0])
end = start + int(src[1])
func_call_names.append(self.source.content[start:end])
return func_call_names
@classmethod
def _load_position_groups_standard_json(cls):
with open('standard_json_output', 'r') as f:
output = f.read()
output = json.loads(output)
return output["contracts"]
@classmethod
def _load_position_groups(cls):
cmd = "solc --combined-json asm %s" % cls.parent_filename
out = run_command(cmd)
out = json.loads(out)
return out['contracts']
def _get_positions(self):
if self.input_type == "solidity":
asm = SourceMap.position_groups[self.cname]['asm']['.data']['0']
else:
filename, contract_name = self.cname.split(":")
asm = SourceMap.position_groups[filename][contract_name]['evm']['legacyAssembly']['.data']['0']
positions = asm['.code']
while(True):
try:
positions.append(None)
positions += asm['.data']['0']['.code']
asm = asm['.data']['0']
except:
break
return positions
def _convert_offset_to_line_column(self, pos):
ret = {}
ret['begin'] = None
ret['end'] = None
if pos['begin'] >= 0 and (pos['end'] - pos['begin'] + 1) >= 0:
ret['begin'] = self._convert_from_char_pos(pos['begin'])
ret['end'] = self._convert_from_char_pos(pos['end'])
return ret
def _convert_from_char_pos(self, pos):
line = self._find_lower_bound(pos, self.source.line_break_positions)
if self.source.line_break_positions[line] != pos:
line += 1
begin_col = 0 if line == 0 else self.source.line_break_positions[line - 1] + 1
col = pos - begin_col
return {'line': line, 'column': col}
def _find_lower_bound(self, target, array):
start = 0
length = len(array)
while length > 0:
half = length >> 1
middle = start + half
if array[middle] <= target:
length = length - 1 - half
start = middle + 1
else:
length = half
return start - 1
def get_filename(self):
return self.cname.split(":")[0]