-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathllm4decompile.py
More file actions
273 lines (232 loc) · 12.5 KB
/
llm4decompile.py
File metadata and controls
273 lines (232 loc) · 12.5 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
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
"""Evaluate plain LLM4Decompile on exebench, which has not been finetuned or modified in any way.
Thus, we use prompting techniques specified in the models' documentation, not the tokens
that are used elsewhere in evaluator. Should be used with:
https://huggingface.co/LLM4Binary/llm4decompile-1.3b-v1.5
https://huggingface.co/LLM4Binary/llm4decompile-6.7b-v1.5
https://huggingface.co/LLM4Binary/llm4decompile-1.3b-v2
https://huggingface.co/LLM4Binary/llm4decompile-6.7b-v2
"""
import argparse
import re
import os
import json
import random
import subprocess
import tempfile
import shutil
import tarfile
from pathlib import Path
import torch
from datasets import DatasetDict, load_from_disk
from transformers import AutoTokenizer, AutoModelForCausalLM
from tqdm import tqdm
from idioms.dataiter import MatchedFunctionDataset
from idioms.data.dataset import MatchedFunction
from idioms.hf import causal_stringify_function_prompt
from evaluator import (
FunctionEvaluator,
ORIGINAL_EXAMPLE_ATTR,
read_predictions,
read_exebench_info,
exebench_to_matched_function,
calculate_executable_metrics,
write_output_to_files
)
CLANG_FORMAT = "clang-format" # "./format/clang-format"
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("model_type", help="The path to/huggingface ID of the model")
parser.add_argument("eval_dataset", help="The exebench dataset to use.")
parser.add_argument("--compiled", help="Location of the compiled binaries corresponding to the exebench dataset.")
parser.add_argument("--dataset-split", type=str, help="The partition of the validation set on which to evaluate.")
parser.add_argument("--outdir", default="baselines", help="Where to write the results summarizing the output.")
parser.add_argument("--max-prediction-length", type=int, default=1024, help="The maximum number of new tokens to be predicted for the original function code and UDT definitions.")
parser.add_argument("--max-decompiled-function-size", type=int, default=1024, help=f"Filter out functions with decompiled code size greater than this amount.")
parser.add_argument("--random-seed", type=int, default=80, help=f"Used to seed python's standard random module.")
parser.add_argument("--limit", type=int, help="Only predict this many examples.")
parser.add_argument("--use-existing-predictions", action="store_true", help="Instead of running prediction itself, use an existing set of predictions.")
parser.add_argument("--no-exebench-tests", action="store_true", help="Don't run exebench tests.")
return parser.parse_args()
ORIGINAL_INDEX_KEY = "original_index"
def build_assembly_prompt(meta: dict, binaries_dir: str, split_name: str) -> str | None:
"""Process an exebench example in the way the LLM4Decompile authors describe.
Based on https://huggingface.co/LLM4Binary/llm4decompile-1.3b-v1.5
:param meta: an exebench entry
:returns: the fully-formed prompt, or None if an error occurred.
"""
with tempfile.TemporaryDirectory() as tempdir:
binary_name = f"{split_name}_{meta['index']}_{meta['fname']}"
binary = os.path.join(binaries_dir, binary_name)
if not os.path.exists(binary):
return None
shutil.copyfile(binary, os.path.join(tempdir, binary_name))
# input_file = fileName+'.c'
# A -c is required here to compile an arbitrary function without a main function, though it's not in the original script
# compile_command = f'gcc -c -o {output_file}.o {input_file} -{opt_state} -lm'#compile the code with GCC on Linux
# subprocess.run(compile_command, shell=True, check=True, cwd=tempdir)
# Custom addition to see if the name of the function is reverted to <func0> when stripped
# subprocess.run(["strip", f"{output_file}.o", "--strip-debug"])
# dump_command = f'objdump -d {output_file}.o > {output_file}.s'#disassemble the binary file into assembly instructions
dump_command = f'objdump -d {binary_name} > {binary_name}.s'
subprocess.run(dump_command, shell=True, check=True, cwd=tempdir)
input_asm = ''
with open(os.path.join(tempdir, binary_name+'.s'), "r") as f:#asm file
asm= f.read()
function_name = meta['fname']
if '<'+function_name+'>:' not in asm: #IMPORTANT replace func0 with the function name
return None
asm = '<func0>:' + asm.split('<'+function_name+'>:')[-1].split('\n\n')[0] #IMPORTANT replace func0 with the function name
asm_clean = ""
asm_sp = asm.split("\n")
for tmp in asm_sp:
if len(tmp.split("\t"))<3 and '00' in tmp:
continue
idx = min(
len(tmp.split("\t")) - 1, 2
)
tmp_asm = "\t".join(tmp.split("\t")[idx:]) # remove the binary code
tmp_asm = tmp_asm.split("#")[0].strip() # remove the comments
asm_clean += tmp_asm + "\n"
input_asm = asm_clean.strip()
before = f"# This is the assembly code:\n"#prompt
after = "\n# What is the source code?\n"#prompt
input_asm_prompt = before+input_asm.strip()+after
return input_asm_prompt
def build_decompilation_prompt(meta: dict | MatchedFunction) -> str | None:
"""Make a prompt for decompilation-input (v2) LLM4Decompile models.
"""
# This is directly taken from the official documentation:
# https://huggingface.co/LLM4Binary/llm4decompile-1.3b-v2.
# Even for ghidra decompilation, the prompt says "assembly code."
before = f"# This is the assembly code:\n"#prompt
after = "\n# What is the source code?\n"#prompt
if isinstance(meta, dict) and meta['ghidra'] is None:
return None
with tempfile.TemporaryDirectory() as tempdir:
c_file = os.path.join(tempdir, "temp.c")
with open(c_file, "w") as fp:
decompiled_code = meta.canonical_decompiled_code if isinstance(meta, MatchedFunction) else meta['ghidra'].strip()
fp.write(decompiled_code)
run = subprocess.run([CLANG_FORMAT, c_file], capture_output=True)
# clang-format writes the formatted code stdout.
formatted = run.stdout.decode()
input_asm_prompt = before + formatted + after
return input_asm_prompt
def main(args: argparse.Namespace):
random.seed(args.random_seed)
model_type: str = args.model_type
eval_dataset = Path(args.eval_dataset)
split_name: str = args.dataset_split
assert eval_dataset.exists(), f"Eval dataset {eval_dataset} does not exit."
# max_context_length = args.max_context_length
max_prediction_length = args.max_prediction_length
max_decompiled_function_size = args.max_decompiled_function_size
limit = args.limit
outdir = Path(args.outdir) / model_type.split("/")[1] / eval_dataset.name
os.makedirs(outdir, exist_ok=True)
do_prediction: bool = not args.use_existing_predictions
do_exebench_tests: bool = not args.no_exebench_tests
assert "llm4decompile" in model_type.lower(), f"This script only suppots LLM4Decompile models."
if "v1.5" in model_type:
use_assembly = True
else:
assert "v2" in model_type, model_type
use_assembly = False
if use_assembly:
assert args.compiled is not None, f"Must supply location of compiled binaries with --compiled argument for v1.5 (assembly-based) models."
compiled = Path(args.compiled) / split_name
assert compiled.exists(), f"Compiled binaries directory {compiled} not found."
omatch = re.search("O[0123]", eval_dataset.name)
assert omatch is not None
optimization = omatch.group(0)
print(f"Optimization: {optimization}")
tokenizer = AutoTokenizer.from_pretrained(model_type)
def function_size_filter(fn: MatchedFunction) -> bool:
"""Return True if the decompiled code fits within the allowed context size.
"""
return len(tokenizer.encode(causal_stringify_function_prompt(fn))) <= max_decompiled_function_size
dataset_is_exebench = (eval_dataset / "dataset_info.json").exists() or (eval_dataset / "dataset_dict.json").exists()
assert not dataset_is_exebench or not use_assembly, f"Can only use assembly for exebench-style datsets"
if args.dataset_split is None:
split_name = "test_real" if dataset_is_exebench else "test"
else:
split_name = args.dataset_split
if do_prediction:
model = AutoModelForCausalLM.from_pretrained(model_type, torch_dtype=torch.bfloat16).cuda()
if dataset_is_exebench:
raw_dataset = load_from_disk(eval_dataset)
if isinstance(raw_dataset, DatasetDict):
raw_dataset = raw_dataset[split_name]
raw_dataset = raw_dataset.add_column("index", list(range(len(raw_dataset)))) # type: ignore
holdout_set = list(filter(function_size_filter, filter(None, map(exebench_to_matched_function, raw_dataset)))) # type: ignore
else:
holdout_set = list(filter(function_size_filter, MatchedFunctionDataset(eval_dataset.glob(f"{split_name}*.tar"))))
if limit is not None:
random.shuffle(holdout_set)
holdout_set = holdout_set[:limit]
# Gets rid of the following warning:
# Setting `pad_token_id` to `eos_token_id`:None for open-end generation.
model.generation_config.pad_token_id = tokenizer.pad_token_id
unable_to_generate_prompt = 0
ooms = 0
results = []
with tempfile.TemporaryDirectory() as binaries_dir:
if use_assembly:
for shard in compiled.iterdir():
assert ".tar" in shard.name
with tarfile.open(shard, "r:*") as tf:
tf.extractall(path=binaries_dir)
for fn in tqdm(holdout_set, dynamic_ncols=True):
meta = getattr(fn, ORIGINAL_EXAMPLE_ATTR) if dataset_is_exebench else fn
if use_assembly:
# could theoretically support this in the future too. However, the current process is flawed
# because it leaks function names in the assembly code. To prevent this, we'd have to add a binary-stripping
# step and match functions based on addresses, which would be much more involved.
# assert isinstance(meta, dict), f"Assembly mode only supported for exebench-style datasets."
assert False, "Assembly mode currently not supported."
# exebench assembly is slightly different than the assembly
# generated by the authors' process so we'll use the authors' process here.
prompt = build_assembly_prompt(meta, binaries_dir, split_name)
else:
prompt = build_decompilation_prompt(meta)
if prompt is None:
unable_to_generate_prompt += 1
continue
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
try:
with torch.no_grad():
outputs = model.generate(**inputs, max_new_tokens=max_prediction_length)
except torch.cuda.OutOfMemoryError:
ooms += 1
prediction = tokenizer.decode(outputs[0][len(inputs[0]):-1]) # type: ignore
results.append((fn, prediction))
# Save the predictions as soon as we finish with them.
if dataset_is_exebench:
exebench_info = [getattr(r[0], ORIGINAL_EXAMPLE_ATTR) for r in results]
else:
exebench_info = None
write_output_to_files(results, outdir / f"{split_name}_results", exebench_info)
else:
results = read_predictions(outdir / f"{split_name}_results.json")
if dataset_is_exebench:
exebench_info = read_exebench_info(outdir / f"{split_name}_results_exebench_info.json")
else:
exebench_info = None
with open(outdir / f"{split_name}_scores.json", "r") as fp:
scores = json.load(fp)
ooms = scores["out_of_memory_errors"]
unable_to_generate_prompt = scores["unable_to_generate_prompt"]
del scores
evaluator = FunctionEvaluator()
scores = evaluator(results)
if dataset_is_exebench and do_exebench_tests:
scores |= calculate_executable_metrics(results, exebench_info) # type: ignore
scores["unable_to_generate_prompt"] = unable_to_generate_prompt
scores["out_of_memory_errors"] = ooms
for score, value in scores.items():
print(score, value)
print()
with open(outdir / f"{split_name}_scores.json", "w") as fp:
json.dump(scores, fp, indent=2)
if __name__ == "__main__":
main(get_args())