Update README.md
Browse files
README.md
CHANGED
|
@@ -13,70 +13,79 @@ LLM4Decompile aims to decompile x86 assembly instructions into C. It is finetune
|
|
| 13 |
|
| 14 |
|
| 15 |
### 2. Evaluation Results
|
| 16 |
-
| Model | Re-compilability | | | | | Re-executability | | | | |
|
| 17 |
-
|--------------------|:----------------:|:---------:|:---------:|:---------:|:---------:|:----------------:|-----------|-----------|-----------|:---------:|
|
| 18 |
-
| Optimization-level | O0 | O1 | O2 | O3 | Avg. | O0 | O1 | O2 | O3 | Avg. |
|
| 19 |
-
| GPT4 | 0.92 | 0.94 | 0.88 | 0.84 | 0.895 | 0.1341 | 0.1890 | 0.1524 | 0.0854 | 0.1402 |
|
| 20 |
-
| DeepSeek-Coder-33B | 0.0659 | 0.0866 | 0.1500 | 0.1463 | 0.1122 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
|
| 21 |
-
| LLM4Decompile-1b | 0.8780 | 0.8732 | 0.8683 | 0.8378 | 0.8643 | 0.1573 | 0.0768 | 0.1000 | 0.0878 | 0.1055 |
|
| 22 |
-
| LLM4Decompile-6b | 0.8817 | 0.8951 | 0.8671 | 0.8476 | 0.8729 | 0.3000 | 0.1732 | 0.1988 | 0.1841 | 0.2140 |
|
| 23 |
-
| LLM4Decompile-33b | 0.8134 | 0.8195 | 0.8183 | 0.8305 | 0.8204 | 0.3049 | 0.1902 | 0.1817 | 0.1817 | 0.2146 |
|
| 24 |
-
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
### 3. How to Use
|
| 28 |
-
Here
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
```python
|
| 31 |
import subprocess
|
| 32 |
import os
|
| 33 |
-
import re
|
| 34 |
|
| 35 |
-
digit_pattern = r'\b0x[a-fA-F0-9]+\b'# binary codes in Hexadecimal
|
| 36 |
-
zeros_pattern = r'^0+\s'#0s
|
| 37 |
OPT = ["O0", "O1", "O2", "O3"]
|
| 38 |
-
|
| 39 |
-
after = "\n# What is the source code?\n"
|
| 40 |
-
fileName = 'path/to/file'
|
| 41 |
-
with open(fileName+'.c','r') as f:#original file
|
| 42 |
-
c_func = f.read()
|
| 43 |
for opt_state in OPT:
|
| 44 |
output_file = fileName +'_' + opt_state
|
| 45 |
input_file = fileName+'.c'
|
| 46 |
-
compile_command = f'gcc -
|
| 47 |
subprocess.run(compile_command, shell=True, check=True)
|
| 48 |
compile_command = f'objdump -d {output_file}.o > {output_file}.s'#disassemble the binary file into assembly instructions
|
| 49 |
subprocess.run(compile_command, shell=True, check=True)
|
| 50 |
|
| 51 |
input_asm = ''
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
input_asm_prompt = before+input_asm.strip()+after
|
| 61 |
with open(fileName +'_' + opt_state +'.asm','w',encoding='utf-8') as f:
|
| 62 |
f.write(input_asm_prompt)
|
| 63 |
```
|
| 64 |
|
| 65 |
-
|
| 66 |
```python
|
| 67 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 68 |
import torch
|
| 69 |
|
| 70 |
-
model_path = '
|
| 71 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 72 |
model = AutoModelForCausalLM.from_pretrained(model_path,torch_dtype=torch.bfloat16).cuda()
|
| 73 |
|
| 74 |
-
with open(fileName +'_' +
|
| 75 |
asm_func = f.read()
|
| 76 |
inputs = tokenizer(asm_func, return_tensors="pt").to(model.device)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
c_func_decompile = tokenizer.decode(outputs[0][len(inputs[0]):-1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
```
|
| 81 |
|
| 82 |
### 4. License
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
### 2. Evaluation Results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
| Model | HumanEval-Decompile | | | | | ExeBench | | | | |
|
| 18 |
+
|:-----------------------:|:-------------------:|:------:|:------:|:------:|:------:|:--------:|:------:|:------:|:------:|:------:|
|
| 19 |
+
| opt-level | O0 | O1 | O2 | O3 | Avg. | O0 | O1 | O2 | O3 | Avg. |
|
| 20 |
+
| GPT4 | 0.1341 | 0.1890 | 0.1524 | 0.0854 | 0.1402 | TBD | TBD | TBD | TBD | TBD |
|
| 21 |
+
| Deepseek-Coder-33B | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
|
| 22 |
+
| LLM4Decompile-6.7B-UO | 0.3720 | 0.1585 | 0.2134 | 0.2134 | 0.2393 | 0.0904 | 0.0988 | 0.0988 | 0.0950 | 0.0957 |
|
| 23 |
+
| LLM4Decompile-1.3B-V1.5 | 0.4817 | 0.2463 | 0.2329 | 0.2280 | 0.2972 | 0.2076 | 0.1774 | 0.1721 | 0.1728 | 0.1824 |
|
| 24 |
+
| LLM4Decompile-6.7B-V1.5 | 0.6927 | 0.4280 | 0.4134 | 0.3732 | 0.4768 | 0.2453 | 0.1999 | 0.1927 | 0.1938 | 0.2079 |
|
| 25 |
|
| 26 |
### 3. How to Use
|
| 27 |
+
Here is an example of how to use our model (Revised for V1.5).
|
| 28 |
+
Note: **Replace** func0 with the function name you want to decompile.
|
| 29 |
+
|
| 30 |
+
**Preprocessing:** Compile the C code into binary, and disassemble the binary into assembly instructions.
|
| 31 |
```python
|
| 32 |
import subprocess
|
| 33 |
import os
|
|
|
|
| 34 |
|
|
|
|
|
|
|
| 35 |
OPT = ["O0", "O1", "O2", "O3"]
|
| 36 |
+
fileName = 'samples/sample' #'path/to/file'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
for opt_state in OPT:
|
| 38 |
output_file = fileName +'_' + opt_state
|
| 39 |
input_file = fileName+'.c'
|
| 40 |
+
compile_command = f'gcc -o {output_file}.o {input_file} -{opt_state} -lm'#compile the code with GCC on Linux
|
| 41 |
subprocess.run(compile_command, shell=True, check=True)
|
| 42 |
compile_command = f'objdump -d {output_file}.o > {output_file}.s'#disassemble the binary file into assembly instructions
|
| 43 |
subprocess.run(compile_command, shell=True, check=True)
|
| 44 |
|
| 45 |
input_asm = ''
|
| 46 |
+
with open(output_file+'.s') as f:#asm file
|
| 47 |
+
asm= f.read()
|
| 48 |
+
if '<'+'func0'+'>:' not in asm: #IMPORTANT replace func0 with the function name
|
| 49 |
+
raise ValueError("compile fails")
|
| 50 |
+
asm = '<'+'func0'+'>:' + asm.split('<'+'func0'+'>:')[-1].split('\n\n')[0] #IMPORTANT replace func0 with the function name
|
| 51 |
+
asm_clean = ""
|
| 52 |
+
asm_sp = asm.split("\n")
|
| 53 |
+
for tmp in asm_sp:
|
| 54 |
+
idx = min(
|
| 55 |
+
len(tmp.split("\t")) - 1, 2
|
| 56 |
+
)
|
| 57 |
+
tmp_asm = "\t".join(tmp.split("\t")[idx:]) # remove the binary code
|
| 58 |
+
tmp_asm = tmp_asm.split("#")[0].strip() # remove the comments
|
| 59 |
+
asm_clean += tmp_asm + "\n"
|
| 60 |
+
input_asm = asm_clean.strip()
|
| 61 |
+
before = f"# This is the assembly code:\n"#prompt
|
| 62 |
+
after = "\n# What is the source code?\n"#prompt
|
| 63 |
input_asm_prompt = before+input_asm.strip()+after
|
| 64 |
with open(fileName +'_' + opt_state +'.asm','w',encoding='utf-8') as f:
|
| 65 |
f.write(input_asm_prompt)
|
| 66 |
```
|
| 67 |
|
| 68 |
+
**Decompilation:** Use LLM4Decompile to translate the assembly instructions into C:
|
| 69 |
```python
|
| 70 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 71 |
import torch
|
| 72 |
|
| 73 |
+
model_path = 'LLM4Binary/llm4decompile-6.7b-v1.5' # V1.5 Model
|
| 74 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 75 |
model = AutoModelForCausalLM.from_pretrained(model_path,torch_dtype=torch.bfloat16).cuda()
|
| 76 |
|
| 77 |
+
with open(fileName +'_' + OPT[0] +'.asm','r') as f:#optimization level O0
|
| 78 |
asm_func = f.read()
|
| 79 |
inputs = tokenizer(asm_func, return_tensors="pt").to(model.device)
|
| 80 |
+
with torch.no_grad():
|
| 81 |
+
outputs = model.generate(**inputs, max_new_tokens=4000)
|
| 82 |
c_func_decompile = tokenizer.decode(outputs[0][len(inputs[0]):-1])
|
| 83 |
+
|
| 84 |
+
with open(fileName +'.c','r') as f:#original file
|
| 85 |
+
func = f.read()
|
| 86 |
+
|
| 87 |
+
print(f'original function:\n{func}')# Note we only decompile one function, where the original file may contain multiple functions
|
| 88 |
+
print(f'decompiled function:\n{c_func_decompile}')
|
| 89 |
```
|
| 90 |
|
| 91 |
### 4. License
|