forked from VeraBE/VeriMan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstrumentator.py
359 lines (263 loc) · 15 KB
/
instrumentator.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from slither.slither import Slither
from parser import Parser
import parser
import collections
SLITHER_UNDECLARED_CONSTRUCTOR_NAME = 'slitherConstructorVariables'
# This whole class is a prototype, a proper parser should be used
class Instrumentator:
def instrument(self, contract_path, contract_name, predicates, instrument_for_echidna):
self.contract_path = contract_path
self.contract_name = contract_name
slither = Slither(self.contract_path)
self.contract_info = slither.get_contract_from_name(self.contract_name)
if self.contract_info is None:
raise Exception('Check config file for contract name')
with open(self.contract_path) as contract_file:
contract = contract_file.read()
contract = contract.replace('}', '\n}')
self.contract_lines = contract.split('\n')
self.__pre_process_contract()
parser = Parser()
echidna_function = ''
for index, predicate_string in enumerate(predicates):
predicate = parser.parse(predicate_string)
functions_to_instrument = self.__get_functions_to_instrument(predicate)
self.__instrument_new_variables(predicate, functions_to_instrument, instrument_for_echidna)
if instrument_for_echidna:
echidna_function += '(' + predicate.solidity_repr + ')\n&& '
else:
assert_string = f'assert({predicate.solidity_repr}); // VERIMAN ASSERT FOR PREDICATE NO. {index + 1}'
self.__insert_in_functions(functions_to_instrument, assert_string, self.__insert_at_end_of_functions)
if instrument_for_echidna:
echidna_function = 'function echidna_invariant() public returns(bool) {\nreturn ' \
+ echidna_function.rsplit('\n&& ', 1)[0]\
+ ';\n}'
self.__insert_in_contract(echidna_function)
contract = '\n'.join(self.contract_lines)
with open(self.contract_path, 'w') as contract_file:
contract_file.write(contract)
def __pre_process_contract(self):
pragma = ''
pragma_found = False
inside_contract = False
constructor_found = False
self.first_line_of_contract = 0
self.last_line_of_contract = len(self.contract_lines)
for index, line in enumerate(self.contract_lines):
line_no_spaces = line.replace(' ', '')
if not pragma_found and 'pragmasolidity' in line_no_spaces:
pragma_found = True
pragma = line_no_spaces.split('pragmasolidity')[1].split(';')[0]
if line.lstrip().startswith('contract '):
was_inside_contract = inside_contract
inside_contract = line_no_spaces.startswith('contract' + self.contract_name)
if inside_contract:
for i in range(index, len(self.contract_lines) - 1):
contract_line = self.contract_lines[i]
if '{' in contract_line:
self.first_line_of_contract = i # TODO test
break
elif was_inside_contract:
self.last_line_of_contract = index # TODO test
break
if inside_contract and (line_no_spaces.startswith('constructor(') or
line_no_spaces.startswith('function' + self.contract_name + '(')):
constructor_found = True
break
if not constructor_found:
if pragma.startswith('^0.4'):
constructor_string = f'function {self.contract_name}() public {{\n}}\n'
elif pragma.startswith('^0.5'):
constructor_string = f'constructor() public {{\n}}\n'
else:
# TODO handle cases like "pragma solidity >=0.4.0 <0.6.0;"
raise Exception("Unknown pragma in contract")
self.__insert_in_contract(constructor_string)
self.__add_inherited_functions()
def __should_be_instrumented(self, func):
return func.visibility == 'public' and not func.is_shadowed
def __add_inherited_functions(self):
for func in self.contract_info.functions_inherited:
if self.__should_be_instrumented(func):
function_declaration_start = 'function ' + func.name
new_function = ''
in_desired_contract = False
in_desired_function = False
for index, line in enumerate(self.contract_lines):
in_desired_contract = in_desired_contract or 'contract ' + func.contract_declarer.name in line
in_desired_function = in_desired_contract and (in_desired_function or function_declaration_start in line)
if in_desired_contract and in_desired_function:
new_function += line
if '{' in line:
break
new_function = function_declaration_start + new_function.split(function_declaration_start, 1)[1]
new_function = new_function.split('{', 1)[0] + '{\n'
if func.return_type is not None:
new_function += 'return '
new_function += f'super.{func.name}('
for param in func.parameters:
new_function += param.name + ','
new_function = new_function.rsplit(',', 1)[0] + ');\n}\n'
self.__insert_in_contract(new_function)
def __instrument_new_variables(self, predicate, functions_to_instrument, instrument_for_echidna):
if predicate.operator == parser.PREVIOUSLY:
if instrument_for_echidna:
initialization_code = f'bool {predicate.solidity_vars[0]};'
update_code = f'{predicate.solidity_vars[0]}={predicate.values[0].solidity_repr};'
self.__insert_in_contract(initialization_code)
self.__insert_in_functions(functions_to_instrument, update_code, self.__insert_at_beginning_of_functions)
else:
initialization_code = f'bool {predicate.solidity_vars[0]}={predicate.values[0].solidity_repr};'
self.__insert_in_functions(functions_to_instrument, initialization_code, self.__insert_at_beginning_of_functions)
elif predicate.operator == parser.SINCE:
q = predicate.solidity_vars[0]
p_since_q = predicate.solidity_vars[1]
q_repr = predicate.values[1].solidity_repr
p_repr = predicate.values[0].solidity_repr
initialization_code = f'bool {q}=false;\nbool {p_since_q}=true;'
update_code = '''if({q}){{\n\
{p_since_q}={p_repr}&&{p_since_q};\n\
}}\n
{q}={q_repr}||{q};\n'''.format(q=q, p_since_q=p_since_q, q_repr=q_repr, p_repr=p_repr)
self.__insert_in_contract(initialization_code)
self.__insert_in_functions(functions_to_instrument, update_code, self.__insert_at_end_of_functions)
for term in predicate.values:
self.__instrument_new_variables(term, functions_to_instrument, instrument_for_echidna)
def __get_functions_to_instrument(self, predicate):
functions_to_instrument = set()
for variable_name in predicate.related_vars:
if self.__is_solidity_property(variable_name):
functions_to_instrument = set(self.contract_info.functions_entry_points)
else:
variable = self.contract_info.get_state_variable_from_name(variable_name)
functions_writing_variable = self.contract_info.get_functions_writing_to_variable(variable)
for func in functions_writing_variable:
if self.__should_be_instrumented(func):
functions_to_instrument.add(func)
functions_to_instrument = functions_to_instrument.union(self.__get_public_callers(functions_writing_variable))
if len(functions_to_instrument) == len(self.contract_info.functions_entry_points):
break
# The initial state also needs to be checked:
is_constructor_considered = False
for func in functions_to_instrument:
if func.name == 'constructor': # FIXME temporal, issue with Slither
is_constructor_considered = True
break
if not is_constructor_considered:
if self.contract_info.constructor is not None:
functions_to_instrument.add(self.contract_info.constructor)
else:
for func in self.contract_info.functions:
if func.name == SLITHER_UNDECLARED_CONSTRUCTOR_NAME:
functions_to_instrument.add(func)
break
# We can thought of all no-state-changing functions as equivalent:
for func in self.contract_info.functions:
if not func in functions_to_instrument and self.__should_be_instrumented(func) and func.name != 'constructor': # FIXME temporal, issue with Slither
functions_to_instrument.add(func)
break
return functions_to_instrument
def __is_solidity_property(self, variable_name):
parts = variable_name.split('.')
return parts[0] in ['block', 'msg', 'tx', 'this', 'now']
def __get_public_callers(self, functions):
result = set()
for func in functions:
callers, queue = set(), collections.deque([func])
while queue:
func_to_check = queue.popleft()
for neighbour in func_to_check.reachable_from_functions:
if neighbour not in callers:
queue.append(neighbour)
callers.add(neighbour)
if self.__should_be_instrumented(neighbour):
result.add(neighbour)
return result
def __insert_in_contract(self, code_to_insert):
self.contract_lines.insert(self.first_line_of_contract + 1, code_to_insert)
self.last_line_of_contract += 1
def __insert_in_functions(self, functions, code_string, insert_in_function):
remaining_functions = list(functions)
open_blocks = 0
in_function = False
current_function = None
constructors_in_list = list(filter(lambda func: func.name == 'constructor' or
func.name == SLITHER_UNDECLARED_CONSTRUCTOR_NAME, remaining_functions))
fallbacks_in_list = list(filter(lambda func: func.name == 'fallback', remaining_functions))
if len(constructors_in_list) > 1 or len(fallbacks_in_list) > 1:
raise Exception('Invalid set of functions to instrument')
for index in range(self.first_line_of_contract, self.last_line_of_contract):
line = self.contract_lines[index]
open_blocks = open_blocks + line.count('{') - line.count('}')
if open_blocks <= 2:
line_stripped = line.lstrip()
line_no_spaces = line.replace(' ', '')
if line_stripped.startswith('function ') \
or line_no_spaces.startswith('function()') \
or line_no_spaces.startswith('constructor('):
func_found = None
if (line_no_spaces.startswith('constructor(') or line_no_spaces.startswith('function' + self.contract_name)) \
and len(constructors_in_list) > 0:
func_found = constructors_in_list[0]
constructors_in_list = []
elif line_no_spaces.startswith('function()') and len(fallbacks_in_list) > 0:
func_found = fallbacks_in_list[0]
fallbacks_in_list = []
else:
for func in remaining_functions:
if line_no_spaces.startswith('function' + func.name + '('):
func_found = func
break
found = func_found is not None
if found:
remaining_functions.remove(func_found)
current_function = func_found
in_function = found
if in_function:
function_done = insert_in_function(code_string, index, open_blocks, current_function)
if function_done and len(remaining_functions) == 0:
break
else:
in_function = not function_done
if len(remaining_functions) > 0:
raise Exception('One or more functions couldn\'t be instrumented')
def __insert_at_beginning_of_functions(self, code_string, index, open_blocks, current_function):
function_done = False
line = self.contract_lines[index]
if open_blocks <= 2 and '{' in line:
self.contract_lines[index] = line.replace('{', '{\n' + code_string, 1)
function_done = True
return function_done
def __insert_at_end_of_functions(self, code_string, index, open_blocks, current_function):
function_done = False
line = self.contract_lines[index]
if 'return ' in line:
if not 'return VERIMAN_' in line:
store_return_values = ''
return_variables = ''
for type in current_function.return_type:
new_var_for_return_value = Parser.create_variable_name('return_value')
store_return_values += f'{type} {new_var_for_return_value},'
return_variables += new_var_for_return_value + ','
store_return_values = store_return_values.rsplit(',', 1)[0]
return_variables = return_variables.rsplit(',', 1)[0]
if len(current_function.return_type) > 1:
store_return_values = '(' + store_return_values + ')'
return_variables = '(' + return_variables + ')'
return_value = line.split('return ', 1)[1].split(';', 1)[0]
assignment_line = f'{store_return_values}={return_value};'
new_return_line = f'return {return_variables}'
self.contract_lines[index] = line.replace(f'return {return_value}', f'{assignment_line}\n{code_string}\n{new_return_line}')
else:
self.contract_lines[index] = line.replace('return ', f'{code_string}\nreturn ')
function_done = open_blocks <= 2
if 'return;' in line:
self.contract_lines[index] = line.replace('return;', f'{code_string}\nreturn;')
function_done = open_blocks <= 2
if not function_done and open_blocks == 1 and '}' in line:
solidity_lines = line.split(';')
finishes_with_return = 'return ' in solidity_lines[len(solidity_lines) - 1]
if not finishes_with_return:
self.contract_lines[index] = (code_string + '\n}').join(line.rsplit('}', 1))
function_done = True
return function_done