-
Notifications
You must be signed in to change notification settings - Fork 0
/
call_graph.py
293 lines (225 loc) · 8.98 KB
/
call_graph.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
import pydot
import networkx as nx
import os
#import sys
import fnmatch
import csv
import pandas as pd
import re
####input the smart contract file name
filenameroot=sys.argv[0]
# filenameroot=f"40.sol"
dot_graph = pydot.graph_from_dot_file(f"{filenameroot}.all_contracts.call-graph.dot")[0]
contract_names = []
contract_nums=[]
functions_id = []
All_edges = {}
for subgraph in dot_graph.get_subgraphs() :
subgraph_name=subgraph.get_name()
contract_nums.append(subgraph_name.split("_")[1])
contract_names.append(subgraph_name.split("_")[-1])
contract_label=dict(zip(contract_nums,contract_names))
for edge in subgraph.get_edges():
edge_source = edge.get_source().split("_",1)[1][:-1]
edge_source_num=edge.get_source().split("_")[0][1:]
edge_source_contract=contract_label[edge_source_num]
edge_destination = edge.get_destination().split("_")
if len(edge_destination)>1 :
edge_destination = edge.get_destination().split("_",1)[-1][:-1]
edge_destination_num = edge.get_destination().split("_")[0][1:]
edge_destination_contract=contract_label[edge_destination_num]
edge = {(edge_source_contract, edge_source):(edge_destination_contract,edge_destination)}
All_edges.update(edge)
#--------------------------------------------------------------------------------------------------------------#
#----------------------------------------------------------------------------------------------------------------#
#from the original code, find all contracts, and all functions#
from slither.slither import Slither
contract_from_slither=Slither(f"{filenameroot}").contracts
all_contracts=[]
all_functions=[]
for contract in contract_from_slither:
all_contracts.append(contract)
contract_functions=[]
for function in contract.functions:
#if contract.name == "Party" or contract.name == "PartyGovernanceNFT" or contract.name == "PartyGovernance" or contract.name == "ERC721" or contract.name == "IERC2981" or contract.name == "ERC721TokenReceiver" or contract.name == "ReadOnlyDelegateCall" or contract.name == "ERC1155Receiver" or contract.name == "ProposalStorage" or contract.name == "IERC165":
# all_functions.append(("",function.name))
#else:
all_functions.append((contract.name,function.name))
all_functions=list(set(all_functions))
#----------------------------------------------------------------------------------------------------------------#
#create a call list
class UnionFind:
def __init__(self):
self.parent = {}
self.rank = {}
def find(self, x):
if x not in self.parent:
self.parent[x] = x
self.rank[x] = 0
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
rootX = self.find(x)
rootY = self.find(y)
if rootX == rootY:
return
if self.rank[rootX] > self.rank[rootY]:
self.parent[rootY] = rootX
else:
self.parent[rootX] = rootY
if self.rank[rootX] == self.rank[rootY]:
self.rank[rootY] += 1
def group_sublists(lst):
uf = UnionFind()
# Step 2: Union elements from sublists with common elements
for i in range(len(lst)):
for j in range(i+1, len(lst)):
for elem in lst[i]:
if elem in lst[j]:
uf.union(lst[i][0], lst[j][0])
break
# Step 3: Group by the representative element
groups = {}
for sublist in lst:
root = uf.find(sublist[0])
if root not in groups:
groups[root] = set()
for elem in sublist:
groups[root].add(elem)
# Step 4: Output the grouped results
result = [list(group) for group in groups.values()]
return result
def generate_call_code_list(functions, call_graph):
call_code_list_all=[]
def dfs(function):
if function not in call_code_list:
call_code_list.append(function)
if function in call_graph:
dfs(call_graph[function])
return call_code_list
for function in functions:
call_code_list = []
call_code_list_all.append(dfs(function))
return call_code_list_all
call_list=generate_call_code_list(all_functions,All_edges)
call_list=call_list
call_list=group_sublists(call_list)
#------------------------------------code list-----------------------------------
# create a code list
code=""
sol_files_content = {}
with open(f"{filenameroot}", 'r') as f:
code=f.read()
#code_files = [f for f in os.listdir("./crytic-export/codeonly") if f.endswith('.sol')]
#for sol_file in code_files:
# with open(f"./crytic-export/codeonly/{sol_file}", 'r') as f:
# sol_files_content[sol_file] = f.read()
# print(type(sol_files_content[sol_file]))
# code=code+sol_files_content[sol_file]
def remove_comments(input_string):
pattern = r"//.*"
output_string = re.sub(pattern, "", input_string, flags=re.MULTILINE)
return output_string
code=remove_comments(code)
with open('code.txt', 'w') as f:
f.write(code)
#-----confirm the starting line
def extract_contract_lines_procedure(text,title,keyword):
lines = text.split('\n')
keyword_line = None
keywordnew=title+keyword
for i, line in enumerate(lines):
if keywordnew in line:
keyword_line = i
break
if keyword_line != None:
lines = lines[keyword_line:]
extracted_text = '\n'.join(lines)
return extracted_text
return None
def extract_contract_lines(text, keyword):
contract_texts = [
extract_contract_lines_procedure(text, "contract ", keyword),
extract_contract_lines_procedure(text, "library ", keyword),
extract_contract_lines_procedure(text, "interface ", keyword),
extract_contract_lines_procedure(text, "abstract contract ", keyword)
]
for contract_text in contract_texts:
if contract_text:
return contract_text
return None
def extract_function_lines_procedure(text, title, keyword):
lines = text.split('\n')
keyword_line = None
keyword_func = title + keyword
for i, line in enumerate(lines):
if keyword_func in line:
keyword_line = i
break
if keyword_line is not None:
# If the function is a single-line definition (i.e., it ends with a semicolon)
if ";" in lines[keyword_line]:
return lines[keyword_line]
else: # Else, it's a multi-line function definition
closing_brace_found = False
for j, subsequent_line in enumerate(lines[keyword_line:]):
if "}" in subsequent_line:
closing_brace_found = True
break
if closing_brace_found:
return '\n'.join(lines[keyword_line: keyword_line + j + 1])
else:
return None
def extract_function_lines(contract_code, keyword):
for prefix in ["function ", "event ", "constructor ","modifier "]:
result = extract_function_lines_procedure(contract_code, prefix, keyword)
if result:
return result
return None
#----------confirm the ending line
def extract_function_code(code: str) -> str:
opening_brace_count = 0
closing_brace_count = 0
function_code = ""
match_found = False
for char in code:
if char == '{':
opening_brace_count += 1
if match_found:
function_code += char
elif (char != '{') and (char != '}'):
function_code+=char
elif char == '}':
closing_brace_count += 1
if match_found:
function_code += char
if opening_brace_count > 0 and opening_brace_count == closing_brace_count:
break
if opening_brace_count == 1 and not match_found:
match_found = True
function_code += char
return function_code.strip()
#--------------confirm a code
def find_code(contract_name,function_name,code):
contract_code=extract_contract_lines(code,contract_name)
if contract_code!=None:
function_start=extract_function_lines(contract_code,function_name)
if (function_start!=None) and (len(function_start)>1):
function_code=extract_function_code(function_start)
return function_code
elif (function_start==None):
return (contract_name+" does not have this function: "+ function_name)
else:
return function_start
else:
return("Does not have this contract: "+contract_name)
call_code_all=[]
for calls in call_list:
call_code=[]
for i in range(len(calls)):
if calls[i][1]!="fallback":
call_code.append(find_code(calls[i][0],calls[i][1],code))
else:
call_code.append(find_code(calls[i][0],"",code))
call_code_all.append(call_code)