Views
No views yet
| Model | Re-compilability | Re-executability | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Optimization-level | O0 | O1 | O2 | O3 | Avg. | O0 | O1 | O2 | O3 | Avg. |
| GPT4 | 0.92 | 0.94 | 0.88 | 0.84 | 0.895 | 0.1341 | 0.1890 | 0.1524 | 0.0854 | 0.1402 |
| DeepSeek-Coder-33B | 0.0659 | 0.0866 | 0.1500 | 0.1463 | 0.1122 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
| LLM4Decompile-1b | 0.8780 | 0.8732 | 0.8683 | 0.8378 | 0.8643 | 0.1573 | 0.0768 | 0.1000 | 0.0878 | 0.1055 |
| LLM4Decompile-6b | 0.8817 | 0.8951 | 0.8671 | 0.8476 | 0.8729 | 0.3000 | 0.1732 | 0.1988 | 0.1841 | 0.2140 |
| LLM4Decompile-33b | 0.8134 | 0.8195 | 0.8183 | 0.8305 | 0.8204 | 0.3049 | 0.1902 | 0.1817 | 0.1817 | 0.2146 |
1import subprocess
2import os
3import re
4
5digit_pattern = r'\b0x[a-fA-F0-9]+\b'# binary codes in Hexadecimal
6zeros_pattern = r'^0+\s'#0s
7OPT = ["O0", "O1", "O2", "O3"]
8fileName = 'path/to/file'
9with open(fileName+'.c','r') as f:#original file
10 c_func = f.read()
11for opt_state in OPT:
12 output_file = fileName +'_' + opt_state
13 input_file = fileName+'.c'
14 compile_command = f'gcc -c -o {output_file}.o {input_file} -{opt_state} -lm'#compile the code with GCC on Linux
15 subprocess.run(compile_command, shell=True, check=True)
16 compile_command = f'objdump -d {output_file}.o > {output_file}.s'#disassemble the binary file into assembly instructions
17 subprocess.run(compile_command, shell=True, check=True)
18
19 input_asm = ''
20 with open(output_file+'.s') as f:#asm file
21 asm= f.read()
22 asm = asm.split('Disassembly of section .text:')[-1].strip()
23 for tmp in asm.split('\n'):
24 tmp_asm = tmp.split('\t')[-1]#remove the binary code
25 tmp_asm = tmp_asm.split('#')[0].strip()#remove the comments
26 input_asm+=tmp_asm+'\n'
27 input_asm = re.sub(zeros_pattern, '', input_asm)
28 before = f"# This is the assembly code with {opt_state} optimization:\n"#prompt
29 after = "\n# What is the source code?\n"#prompt
30 input_asm_prompt = before+input_asm.strip()+after
31 with open(fileName +'_' + opt_state +'.asm','w',encoding='utf-8') as f:
32 f.write(input_asm_prompt)1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3
4model_path = 'arise-sustech/llm4decompile-33b' #note: it may need multi-gpu support
5tokenizer = AutoTokenizer.from_pretrained(model_path)
6model = AutoModelForCausalLM.from_pretrained(model_path,torch_dtype=torch.bfloat16).cuda()
7
8with open(fileName +'_' + opt_state +'.asm','r') as f:#original file
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=512)
13c_func_decompile = tokenizer.decode(outputs[0][len(inputs[0]):-1])@misc{tan2024llm4decompile,
title={LLM4Decompile: Decompiling Binary Code with Large Language Models},
author={Hanzhuo Tan and Qi Luo and Jing Li and Yuqun Zhang},
year={2024},
eprint={2403.05286},
archivePrefix={arXiv},
primaryClass={cs.PL}
}