Update README.md
Browse files
    	
        README.md
    CHANGED
    
    | @@ -1,3 +1,89 @@ | |
| 1 | 
             
            ---
         | 
| 2 | 
             
            license: mit
         | 
|  | |
|  | |
|  | |
| 3 | 
             
            ---
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
             
            ---
         | 
| 2 | 
             
            license: mit
         | 
| 3 | 
            +
            tags:
         | 
| 4 | 
            +
            - decompile
         | 
| 5 | 
            +
            - binary
         | 
| 6 | 
             
            ---
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            ### 1. Introduction of LLM4Decompile
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            LLM4Decompile aims to decompile x86 assembly instructions into C. It is finetuned from Deepseek-Coder on 2B tokens of assembly-C pairs compiled from AnghaBench.
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            - **Github Repository:** [LLM4Compile](https://github.com/albertan017/LLM4Decompile)
         | 
| 13 | 
            +
             | 
| 14 | 
            +
             | 
| 15 | 
            +
            ### 2. Evaluation Results
         | 
| 16 | 
            +
            | Model              | Re-compilability |           |           |           |           | Re-executability |           |           |           |           |
         | 
| 17 | 
            +
            |--------------------|:----------------:|:---------:|:---------:|:---------:|:---------:|:----------------:|-----------|-----------|-----------|:---------:|
         | 
| 18 | 
            +
            | opt-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 give an example of how to use our model.
         | 
| 29 | 
            +
            First compile the C code into binary, disassemble the binary into assembly instructions:
         | 
| 30 | 
            +
            ```python
         | 
| 31 | 
            +
            import subprocess
         | 
| 32 | 
            +
            import os
         | 
| 33 | 
            +
            import re
         | 
| 34 | 
            +
             | 
| 35 | 
            +
            digit_pattern = r'\b0x[a-fA-F0-9]+\b'# hex lines
         | 
| 36 | 
            +
            zeros_pattern = r'^0+\s'#0s
         | 
| 37 | 
            +
            OPT = ["O0", "O1", "O2", "O3"]
         | 
| 38 | 
            +
            before = f"# This is the assembly code with {opt_state} optimization:\n"
         | 
| 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 -c -o {output_file}.o {input_file} -{opt_state} -lm'#compile the code with GCC on Linux
         | 
| 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 | 
            +
                asm = read_file(output_file+'.s')
         | 
| 53 | 
            +
                asm = asm.split('Disassembly of section .text:')[-1].strip()
         | 
| 54 | 
            +
                for tmp in asm.split('\n'):
         | 
| 55 | 
            +
                    tmp_asm = tmp.split('\t')[-1]#remove the binary code
         | 
| 56 | 
            +
                    tmp_asm = tmp_asm.split('#')[0].strip()#remove the comments
         | 
| 57 | 
            +
                    input_asm+=tmp_asm+'\n'
         | 
| 58 | 
            +
                input_asm = re.sub(zeros_pattern, '', input_asm)
         | 
| 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 | 
            +
            Then use LLM4Decompile to translate the assembly instructions into C:
         | 
| 66 | 
            +
            ```python
         | 
| 67 | 
            +
            from transformers import AutoTokenizer, AutoModelForCausalLM
         | 
| 68 | 
            +
            import torch
         | 
| 69 | 
            +
             | 
| 70 | 
            +
            model_path = 'arise-sustech/llm4decompile-1.3b'
         | 
| 71 | 
            +
            tokenizer = AutoTokenizer.from_pretrained(model_path)
         | 
| 72 | 
            +
            model = AutoModelForCausalLM.from_pretrained(model_path,torch_dtype=torch.bfloat16).cuda()
         | 
| 73 | 
            +
             | 
| 74 | 
            +
            with open(fileName +'_' + opt_state +'.asm','r') as f:#original file
         | 
| 75 | 
            +
                asm_func = f.read()
         | 
| 76 | 
            +
            inputs = tokenizer(asm_func, return_tensors="pt").to(model.device)
         | 
| 77 | 
            +
                with torch.no_grad():
         | 
| 78 | 
            +
                    outputs = model.generate(**inputs, max_new_tokens=200)
         | 
| 79 | 
            +
            c_func_decompile = tokenizer.decode(outputs[0][len(inputs[0]):-1])
         | 
| 80 | 
            +
            ```
         | 
| 81 | 
            +
             | 
| 82 | 
            +
            ### 4. License
         | 
| 83 | 
            +
            This code repository is licensed under the MIT License. The use of DeepSeek Coder models is subject to the Model License. DeepSeek Coder supports commercial use.
         | 
| 84 | 
            +
             | 
| 85 | 
            +
            See the [LICENSE-MODEL](https://github.com/deepseek-ai/deepseek-coder/blob/main/LICENSE-MODEL) for more details.
         | 
| 86 | 
            +
             | 
| 87 | 
            +
            ### 5. Contact
         | 
| 88 | 
            +
             | 
| 89 | 
            +
            If you have any questions, please raise an issue.
         |