LLM4Binary commited on
Commit
da3359f
·
verified ·
1 Parent(s): f74c56c

Update README.md

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