-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
290 lines (232 loc) · 10.6 KB
/
convert.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
import json
import re
import os
def getAllCode(direcoryPath):
code_map = {}
for root, dirs, files in os.walk(direcoryPath):
for file_name in files:
if file_name.endswith('.sol'):
file_path = os.path.join(root, file_name)
code_map[file_name[:file_name.rfind('.')]] = getCode(file_path)
return code_map
def getCode(filePath):
with open(filePath, 'r', encoding='utf-8') as file:
file_content = file.read()
return file_content
def getCorpusMap(filePath,test_cases_map):
with open(filePath, 'r', encoding='utf-8') as file:
data = file.read().strip()
modified_data = '[' + data.replace('\n', ',') + ']'
# test_cases_map = {}
modified_data_list = json.loads(modified_data)
for test_case in modified_data_list:
data_readable = test_case.get('data_readable')
if data_readable is None:
continue
function_name = re.match(r'[^\W_]+', data_readable).group()
test_cases_map[function_name] = json.dumps(test_case)
for key, value in test_cases_map.items():
print(f"Key: {key}")
print(f"Value: {value}")
print()
return test_cases_map
def getAllCorpusMap(directoryPath):
replayable_files = []
for root, dirs, files in os.walk(directoryPath):
for file in files:
if file.endswith("replayable"):
replayable_files.append(os.path.join(root, file))
test_cases_map = {}
for filePath in replayable_files:
test_cases_map = getCorpusMap(filePath,test_cases_map)
return test_cases_map
def getAllMap(directoryPath):
subfolders = [f.path for f in os.scandir(directoryPath) if f.is_dir()]
subfolder_names = [f.name for f in os.scandir(directoryPath) if f.is_dir()]
allMap = {}
for name, path in zip(subfolder_names, subfolders):
test_cases_map = getAllCorpusMap(path)
allMap[name] = test_cases_map
print("======allMap")
for x in allMap:
print(x)
print("============================================")
print(allMap[x])
print("============================================")
return allMap
with open(r'E:\ityfuzz\ityfuzz\backup\inicorpus\AETH\2_seed_replayable', 'r') as file:
data = json.load(file)
data_readable = data.get('data_readable', '')
function_name = re.match(r'[^\W_]+', data_readable).group()
def combineText(code,initialText):
return code+"\n"+initialText
def getInitialText():
with open(r'E:\ityfuzz\ityfuzz\test.txt', 'r', encoding='utf-8') as file:
file_content = file.read()
return file_content
def getAllInitialTextMap(codeMap,initialText):
AllInitialTextMap = {}
for name,code in codeMap.items():
AllInitialTextMap[name] = combineText(code,initialText)
return AllInitialTextMap
def getExampleQuestionText():
with open(r'E:\ityfuzz\ityfuzz\exampleQuestion.txt', 'r', encoding='utf-8') as file:
file_content = file.read()
return file_content
def getExampleAnswerText():
with open(r'E:\ityfuzz\ityfuzz\ExampleAnswer.txt', 'r', encoding='utf-8') as file:
file_content = file.read()
return file_content
def writeCprpusByResponseMapAndCorpusMap(ResponseMap,CorpusMap):
for contractName,childMap in corpusMap:
for methodName,corpusList in childMap:
for responseContractName,responseCorpus in ResponseMap:
1==1
def convertResponseToCorpus(corpus,respone):
return
def convert(corpusMap,resultTxt):
data = json.loads(resultTxt)
allList = []
for test_case in data:
childList = []
index = test_case.get("number")
for operation in test_case.get("operator"):
function_namen = operation.get("function")
param = operation.get("parameter")
if function_namen not in corpusMap:
continue
corpus = corpusMap[function_namen]
parsed_data = json.loads(corpus)
try:
data_list = parsed_data["data"]["b"]["data"]
except KeyError:
childList.append(corpus)
continue
i = 0
for item in data_list:
if i >= len(test_case):
break
print("==========================")
print(parsed_data)
print(index)
print(function_namen)
print(item)
print("==========================")
if isinstance(param[i], str):
print("if=======")
print([int(param[i][j:j + 2], 16) for j in range(2, len(param[i]), 2)])
print(item["b"]["data"])
item["b"]["data"] = [int(param[i][j:j + 2], 16) for j in range(2, len(param[i]), 2)]
else:
print("else========")
print(param[i])
print(item["b"]["data"])
item["b"]["data"] = convert_to_list(param[i])
i = i + 1
childList.append(json.dumps(parsed_data))
allList.append(childList)
return allList
def convert_to_list(num):
if not isinstance(num, int):
raise ValueError("Input must be an integer")
hex_str = hex(num)[2:]
if len(hex_str) % 2 != 0:
hex_str = '0' + hex_str
half_len = len(hex_str) // 2
result_list = [int(hex_str[i:i + 2], 16) for i in range(0, len(hex_str), 2)]
return result_list
print("corpusList size===============")
print(len(corpusMap))
print(corpusMap)
time = 0;
parsed_dataList = []
while (time < len(corpusMap)):
print(time)
values_list = list(corpusMap.values())
corpus = values_list[time]
time = time + 1
modified_data_list = json.loads(corpus)
data_readable = modified_data_list.get('data_readable')
if data_readable is None:
continue
function_name = re.match(r'[^\W_]+', data_readable).group()
data = json.loads(resultTxt)
for test_case in data:
index = test_case.get("number")
for operation in test_case.get("operator"):
function_namen = operation.get("function")
param = operation.get("parameter")
if(function_name == function_namen):
i = 0;
parsed_data = json.loads(corpus)
stack = [parsed_data]
while stack:
current_obj = stack.pop()
if isinstance(current_obj, dict):
for key, value in current_obj.items():
if key == 'type' and value == 'A256':
if isinstance(param[i], str):
print("param",param[i])
print("i",i)
current_obj['data'] = str([int(param[i][j:j + 2], 16) for j in range(2, len(param[i]), 2)])
else:
current_obj['data'] = str(param[i])
i = i+1
parsed_dataList.append(parsed_data)
time = 0
elif isinstance(value, (dict, list)):
stack.append(value)
elif isinstance(current_obj, list):
for item in current_obj:
stack.append(item)
for key, value in parsed_data.items():
if key == "data":
for key2, value2 in parsed_data.get(key):
print(key2)
print("=======")
print(value2)
if key == "type" and value == "A256":
if isinstance(param[i], str):
parsed_data["data"] = str([int(param[i][i:i+2], 16) for i in range(2, len(param[i]), 2)])
else:
parsed_data["data"] = str(param[i])
parsed_dataList.append(parsed_data)
time = 0
return parsed_dataList
def convertResponseFileToCorpus(resourseFolder,destinationFolder,corpusMap):
code_map = {}
for root, dirs, files in os.walk(resourseFolder):
for file_name in files:
if file_name.endswith('.txt'):
num = 0
file_path = os.path.join(root, file_name)
resultFile = getCode(file_path)
for contractName,corpusMapChild in corpusMap.items():
if(contractName == file_name[:file_name.rfind('.')]):
backjson = convert(corpusMapChild,resultFile)
file_folder_path = os.path.join(destinationFolder, contractName)
singlefilepath = os.path.join(file_folder_path, str(num))
print(len(backjson[2]))
for parsed_data in backjson:
singlefilepath = os.path.join(file_folder_path, str(num))
for child in parsed_data:
json_string = child
with open(singlefilepath, "a") as output_file:
output_file.write(json_string)
output_file.write("\n")
num = num + 1
codeMap = getAllCode(r'E:\ityfuzz\ityfuzz\backup\contract')
# getCorpusMap(r'E:\ityfuzz\ityfuzz\backup\inicorpus\AETH\12_replayable')
corpusMap = getAllMap(r'E:\ityfuzz\ityfuzz\backup\inicorpus')
initialText = getInitialText()
AllInitialTextMap = getAllInitialTextMap(codeMap,initialText)
convertResponseFileToCorpus(r'E:\ityfuzz\ityfuzz\backup\GPT3.5result1',r'E:\ityfuzz\ityfuzz\backup\GPT3.5result1test',corpusMap)
exampleQuestionText = getExampleQuestionText()
exampleAnswerText = getExampleAnswerText()
for i,j in resultMap.items():
print("============name==============")
print(i)
print("============name==============")
print("============result==============")
print(j)
print("============result==============")