Views
No views yet
| Model/Benchmark | HumanEval-Decompile | ExeBench | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Optimization Level | O0 | O1 | O2 | O3 | AVG | O0 | O1 | O2 | O3 | AVG |
| DeepSeek-Coder-6.7B | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0.0000 |
| GPT-4o | 0.3049 | 0.1159 | 0.1037 | 0.1159 | 0.1601 | 0.0443 | 0.0328 | 0.0397 | 0.0343 | 0.0378 |
| LLM4Decompile-End-1.3B | 0.4720 | 0.2061 | 0.2122 | 0.2024 | 0.2732 | 0.1786 | 0.1362 | 0.1320 | 0.1328 | 0.1449 |
| LLM4Decompile-End-6.7B | 0.6805 | 0.3951 | 0.3671 | 0.3720 | 0.4537 | 0.2289 | 0.1660 | 0.1618 | 0.1625 | 0.1798 |
| LLM4Decompile-End-33B | 0.5168 | 0.2956 | 0.2815 | 0.2675 | 0.3404 | 0.1886 | 0.1465 | 0.1396 | 0.1411 | 0.1540 |
1import subprocess
2import os
3
4OPT = ["O0", "O1", "O2", "O3"]
5fileName = 'samples/sample' #'path/to/file'
6for opt_state in OPT:
7 output_file = fileName +'_' + opt_state
8 input_file = fileName+'.c'
9 compile_command = f'gcc -o {output_file}.o {input_file} -{opt_state} -lm'#compile the code with GCC on Linux
10 subprocess.run(compile_command, shell=True, check=True)
11 compile_command = f'objdump -d {output_file}.o > {output_file}.s'#disassemble the binary file into assembly instructions
12 subprocess.run(compile_command, shell=True, check=True)
13
14 input_asm = ''
15 with open(output_file+'.s') as f:#asm file
16 asm= f.read()
17 if '<'+'func0'+'>:' not in asm: #IMPORTANT replace func0 with the function name
18 raise ValueError("compile fails")
19 asm = '<'+'func0'+'>:' + asm.split('<'+'func0'+'>:')[-1].split('\n\n')[0] #IMPORTANT replace func0 with the function name
20 asm_clean = ""
21 asm_sp = asm.split("\n")
22 for tmp in asm_sp:
23 if len(tmp.split("\t"))<3 and '00' in tmp:
24 continue
25 idx = min(
26 len(tmp.split("\t")) - 1, 2
27 )
28 tmp_asm = "\t".join(tmp.split("\t")[idx:]) # remove the binary code
29 tmp_asm = tmp_asm.split("#")[0].strip() # remove the comments
30 asm_clean += tmp_asm + "\n"
31 input_asm = asm_clean.strip()
32 before = f"# This is the assembly code:\n"#prompt
33 after = "\n# What is the source code?\n"#prompt
34 input_asm_prompt = before+input_asm.strip()+after
35 with open(fileName +'_' + opt_state +'.asm','w',encoding='utf-8') as f:
36 f.write(input_asm_prompt)1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3
4model_path = 'LLM4Binary/llm4decompile-1.3b-v1.5' # V1.5 Model
5tokenizer = AutoTokenizer.from_pretrained(model_path)
6model = AutoModelForCausalLM.from_pretrained(model_path,torch_dtype=torch.bfloat16).cuda()
7
8with open(fileName +'_' + OPT[0] +'.asm','r') as f:#optimization level O0
9 asm_func = f.read()
10inputs = tokenizer(asm_func, return_tensors="pt").to(model.device)
11with torch.no_grad():
12 outputs = model.generate(**inputs, max_new_tokens=4000)
13c_func_decompile = tokenizer.decode(outputs[0][len(inputs[0]):-1])
14
15with open(fileName +'.c','r') as f:#original file
16 func = f.read()
17
18print(f'original function:\n{func}')# Note we only decompile one function, where the original file may contain multiple functions
19print(f'decompiled function:\n{c_func_decompile}')