-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
204 lines (169 loc) · 7.49 KB
/
main.py
File metadata and controls
204 lines (169 loc) · 7.49 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
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
import argparse
import os
import pickle
import shutil
import subprocess
import sys
from codebleu import calc_codebleu
from src.compiler.gcccompiler import GCCCompiler
from src.disassembler.objdumpdisassembler import ObjdumpDisassembler
from src.disassembler.r2disassembler import R2Disassembler
from src.predictor.r2decompilepredictor import R2DecompilePredictor
from src.evaluator.codebleuevaluator import CodeBleuEvaluator
from src.predictor.openaimodelpredictor import OpenAIModelPredictor
from src.pipeline import Pipeline
from src.r2runner import R2Runner
from src.util import create_folder_if_not_exists, codebleu_create_graph, codebleu_create_latex_table
default_llm_prompt="""
**Prompt:**
Below is a snippet of assembly code. Please reconstruct the original C code as accurately as possible and provide only the code block without any explanations or additional text.
**Assembly Code:**
```
{disassembly}
```
**Reconstructed C Code:**
```c
"""
def run_pipeline_prepare(pipeline, args):
for source_file in pipeline.get_sources():
compile_disassemble_reference(pipeline, source_file)
def run_pipeline_print(pipeline, args):
for source_file in pipeline.get_sources():
executable_filename = compile_disassemble_reference(pipeline, source_file)
print("Generating prediction...")
prediction = pipeline.generate_prediction(executable_filename)
print()
print("Original code:")
with open(os.path.join(pipeline.data_path, 'sources', source_file)) as file:
print(file.read())
print()
print("Prediction:")
print(prediction)
print()
input()
def run_pipeline_evaluate(pipeline, args, eval_path):
for source_file in pipeline.get_sources():
executable_filename = compile_disassemble_reference(pipeline, source_file)
print("Generating predictions...")
pipeline.generate_and_save_predictions(executable_filename, status_callback=print_prediction_status)
print()
evaluate(pipeline, args, eval_path)
def print_prediction_status(status, predictor_name):
if status == 0:
print(f"Generating for {predictor_name}...", end='', flush=True)
else:
print(" Done!")
def evaluate(pipeline, args, eval_path):
print("Evaluating...")
results = pipeline.evaluate()
print("Results:")
for key, score in results.items():
print("==")
print(key)
for score_key, score_value in score.items():
print(f"{score_key}: {score_value:.2%}")
print("==")
# Save results to a file
with open(os.path.join(eval_path, args.results_pkl), 'wb') as f:
pickle.dump(results, f)
# Define headers for the LaTeX table
headers = ["Metric"] + list(results.keys())
codebleu_create_latex_table(
os.path.join(eval_path, args.results_latex),
results,
headers)
codebleu_create_graph(
os.path.join(eval_path, args.results_pkl),
os.path.join(eval_path, args.plot_filename))
def compile_disassemble_reference(pipeline, source_file):
print("==============")
print(f"File: {source_file}")
print("==============")
executable_filename = os.path.splitext(source_file)[0]
print("Compiling...")
pipeline.compile(source_file, executable_filename)
print("Creating disassembly...")
pipeline.disassemble(executable_filename)
print("Adding to reference dataset...")
pipeline.add_source_to_dataset(source_file)
return executable_filename
def determine_dataset_size(sources_path):
if sources_path == 'sources/small_test':
return 'small'
else:
return 'large'
def determine_models_used(models):
if len(models) > 1:
return 'multiple'
else:
return models[0]
def determine_strip(strip):
return 'strip' if strip else 'nostrip'
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Run pipeline commands.')
parser.add_argument('-b', '--base-prompt', type=str, default=default_llm_prompt, help='Base prompt to send to LLMs. Use `{disassembly}` in the string where you want to place the disassembled code.')
parser.add_argument('-d', '--data-path', type=str, default='out/data', help='Output data directory')
parser.add_argument('-a', '--auto-data-path', action='store_true', help='Generate the data path automatically from flags. This will override the -d flag.')
parser.add_argument('-i', '--sources-path', type=str, default='sources/small_test', help='Source code input directory')
parser.add_argument('-r', '--results-pkl', type=str, default='results.pkl', help='Results filename')
parser.add_argument('-l', '--results-latex', type=str, default='table.tex', help='Results latex table filename')
parser.add_argument('-p', '--plot-filename', type=str, default='plot.png', help='Plot graph filename')
parser.add_argument('-m', '--models', type=str, nargs='+', default=['gpt-3.5-turbo'], help='List of model names')
parser.add_argument('-s', '--strip', action='store_true', help='Strip the binary during compilation')
parser.add_argument('-x', '--disassembler', choices=['objdump', 'r2'], default='r2', help='Disassembler to run')
parser.add_argument('command', choices=['clean', 'prepare', 'print', 'full-run', 'evaluate'], default='full-run', help='Command to execute')
args = parser.parse_args()
data_path = args.data_path
# This is pretty specific for generating output folders to be used for the thesis report
if args.auto_data_path:
dataset_size = determine_dataset_size(args.sources_path)
models_used = determine_models_used(args.models)
strip_status = determine_strip(args.strip)
data_path = f"out/data_{dataset_size}_{models_used}_{strip_status}_{args.disassembler}/"
print(f"Auto data path set to: {data_path}")
eval_path = os.path.join(data_path, 'evaluation')
create_folder_if_not_exists(eval_path)
r2_runner = R2Runner(subprocess)
predictors = [
OpenAIModelPredictor(os.environ.get("OPENAI_API_KEY"), model, 0, args.base_prompt)
for model in args.models
] + [R2DecompilePredictor(r2_runner)]
compiler = GCCCompiler(subprocess, args.strip)
disassembler = None
if args.disassembler == 'objdump':
disassembler = ObjdumpDisassembler(subprocess)
elif args.disassembler == 'r2':
disassembler = R2Disassembler(r2_runner)
else:
print(f'Error: Unknown disassembler {args.disassembler}')
sys.exit(1)
evaluator = CodeBleuEvaluator(calc_codebleu)
pipeline = Pipeline(
compiler=compiler,
disassembler=disassembler,
predictors=predictors,
evaluator=evaluator,
sources_path=args.sources_path,
data_path=data_path)
if args.command == 'clean':
pipeline.clean()
if os.path.isdir(eval_path):
shutil.rmtree(eval_path)
if os.path.isdir(data_path) and not os.listdir(data_path):
os.rmdir(data_path)
elif args.command == 'prepare':
pipeline.clean()
pipeline.init_folders()
run_pipeline_prepare(pipeline, args)
elif args.command == 'print':
pipeline.clean()
pipeline.init_folders()
run_pipeline_print(pipeline, args)
elif args.command == 'evaluate':
evaluate(pipeline, args, eval_path)
elif args.command == 'full-run':
pipeline.clean()
pipeline.init_folders()
run_pipeline_evaluate(pipeline, args, eval_path)
else:
print(f'Error: Unknown command {args.command}')