Large language model-based understandable evaluation index-oriented decompilation code optimization method

By designing comprehensibility evaluation indicators and large language models to optimize decompiled code, the problem of insufficient readability of decompiled code is solved, and an automated optimization process is realized, which improves the comprehensibility of decompiled code and the quality of generated code.

CN120578397APending Publication Date: 2025-09-02NANJING UNIV
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202510678796.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-26
Publication Date
2025-09-02

AI Technical Summary

Technical Problem

The existing decompilation technology lacks a systematic understandingability evaluation framework, which makes it difficult to guarantee the readability and correctness of decompiled code. Optimization depends on manual experiments and experience, and lacks guidance on automated indicators.

Method used

The comprehensibility evaluation indicators based on the large language model are designed and implemented. The code pattern is recognized through abstract syntax trees, and the prompt words are used to guide the large language model to optimize the decompilation code, combined with grammar and semantic inspection, and iterative optimization until the comprehensibility is improved.

Benefits of technology

Improves the comprehensibility and optimization efficiency of decompiled code, provides an automated comprehensibility evaluation and optimization process, ensuring the quality and readability of generated code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120578397A_ABST
    Figure CN120578397A_ABST
Patent Text Reader

Abstract

The invention discloses a large language model-based understandability evaluation index-oriented decompilation code optimization method, which mainly aims at Java decompilation codes, and comprises the following steps of: firstly, designing and realizing an understandability evaluation index to realize quantitative analysis on the understandability of the decompilation codes; on the basis, a grammar correctness and semantic consistency checking mechanism is introduced in the method, on the premise that code grammar correctness and semantic consistency before and after optimization are guaranteed, optimizable fragments in codes are automatically recognized through an abstract syntax tree and added into cue words, and a large language model is guided to optimize decompiled codes. Furthermore, quantitative comparison is performed on codes before and after optimization through evaluation indexes, and an iterative optimization process is controlled based on a comparison result. According to the method, the decompilation code can be optimized, the understandability of the decompilation code is improved, an index-oriented improvement thought can be provided for a decompiler developer, and the method can be widely applied to reverse engineering, safety analysis and other scenes.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the fields of computer software technology and reverse engineering technology, and in particular to a decompiled code optimization method guided by comprehensibility evaluation indicators based on a large language model. Background Art

[0002] Decompilation, a core technology in reverse engineering, refers to the process of converting machine code into a high-level language representation through static analysis. It holds significant value in software security analysis, vulnerability detection, and program understanding. However, decompilation faces numerous technical challenges. Due to the irreversible nature of the compilation process, a significant amount of detailed information in the source code is lost during the compilation phase. Consequently, there is a many-to-one mapping between source code and machine code during compilation, meaning that source code with multiple grammatical structures may be compiled into the same machine code. This makes the decompilation process essentially an underconstrained reverse reasoning problem. Existing decompilation frameworks generally employ heuristic rule-based approaches to recover and supplement missing information. However, the design of these rules relies primarily on manual experience, lacking a rigorous theoretical foundation and systematic construction methodology. This makes it difficult to effectively guarantee the correctness and readability of the generated code.

[0003] More importantly, current research on decompilation technology focuses primarily on verifying functional correctness, while a systematic evaluation framework and research system for the key metric of generated code comprehensibility has yet to be established. Given that the core application scenario of decompilation technology is to assist program understanding and reverse analysis when source code is unavailable, constructing a scientific and reasonable comprehensibility evaluation index system and optimizing the quality of decompiled output code based on this has become a major issue that urgently needs to be overcome in this field. Existing indicators cannot evaluate the comprehensibility of decompiled code. At the same time, improvements to the code generated by current decompilers rely on extensive manual experiments and feedback. Existing automated decompilation code optimization methods lack indicator guidance, making it difficult to ensure the comprehensibility of the optimized code. Summary of the Invention

[0004] Purpose of the invention: The purpose of the present invention is to provide a decompiled code optimization method guided by comprehensibility evaluation indicators based on a large language model. In combination with code patterns that affect the comprehensibility of decompiled code, an evaluation indicator for the comprehensibility of Java decompiled code is designed and implemented based on cognitive complexity. Then, the indicator is applied to solve the problem that existing decompiled code optimization methods are difficult to ensure the comprehensibility of the optimized code.

[0005] Technical solution: The present invention provides a decompiled code optimization method guided by comprehensibility evaluation indicators based on a large language model, comprising the following steps:

[0006] S1: Use the decompiler to generate decompiled code;

[0007] S2: Check the grammatical correctness and semantic consistency of the decompiled code;

[0008] S3: Perform static analysis on the decompiled code through the abstract syntax tree to identify six code patterns and locations to be optimized;

[0009] S4: Generate prompt words based on the recognized code pattern and location, and guide the large language model to optimize the decompiled code;

[0010] S5: Check the syntax correctness and semantic consistency of the optimized code;

[0011] S6: Calculate and compare the understandability of the code before and after optimization based on the decompilation cognitive complexity index;

[0012] S7: If the comprehensibility is improved, S3-S6 are repeatedly executed with the optimized code as input until the comprehensibility is no longer improved or there are no more optimized code fragments.

[0013] Furthermore, in step S2, the syntax correctness check is implemented by compiling the decompiled code through a Java compiler; the semantic consistency check is implemented by at least one of the following methods: using unit test cases to compare the input and output results of the code before and after optimization; using symbolic execution to verify the consistency of the end state of the code under the same initial state.

[0014] Furthermore, when there is a lack of test cases, unit test cases are automatically generated by the Randoop tool to perform semantic consistency verification.

[0015] Furthermore, the code pattern includes:

[0016] (31) Three or more nested conditional statements or loop statements;

[0017] (32) Parentheses are omitted in mixed operator expressions;

[0018] (33) Statements whose single line length exceeds the set threshold;

[0019] (34) Omit curly braces after conditional or loop statements;

[0020] (35) Inline assignment in expressions;

[0021] (36) Numerical literals replace constants;

[0022] Furthermore, in step S3, the single-line length threshold is set to 120 characters, and the numerical literals exclude the common cases of -1, 0, and 1.

[0023] Furthermore, the prompt words in step S4 include the following elements:

[0024] (41) It is required not to add comments or split methods;

[0025] (42) Specifically specify the method name, line number and corresponding pattern type of the code to be optimized;

[0026] (43) The example prompt word structure is: "Optimization method <method 1> <n1><Pattern Description 1> and <n2><Pattern Description 2>" of the line.

[0027] Furthermore, in step S6, the decompilation cognitive complexity index is calculated according to the following rules:

[0028] (61) Increase the complexity value for nested structures with three or more layers;

[0029] (62) Increase the complexity value for mixed operator expressions without parentheses;

[0030] (63) Add the quotient of the length of the overlong line divided by the threshold to the complexity;

[0031] (64) Increase the complexity value for conditional / loop statements that lack braces;

[0032] (65) Increase the complexity value for inline assignments in expressions;

[0033] (66) Increase complexity values ​​for unconventional numeric literals.

[0034] Furthermore, in step S5, if the optimized code fails the syntax or semantic check, the process returns to S4 to regenerate the optimized code when the number of retries does not reach the upper limit.

[0035] The present invention provides a decompiled code optimization system guided by comprehensibility evaluation indicators based on a large language model, characterized by comprising:

[0036] Decompilation module: used to generate decompiled code through Java decompiler;

[0037] Compilation and verification module: performs syntax checking through the Java compiler and manages dependencies through Maven;

[0038] Test generation module: Generates unit test cases through the Randoop tool to verify semantic consistency;

[0039] Pattern recognition module: Generates an abstract syntax tree through JavaParser and extracts six code patterns and locations;

[0040] Index calculation module: calculates the decompilation cognitive complexity index value based on the abstract syntax tree;

[0041] Large model optimization module: Generates prompt words based on recognition patterns to drive large language model optimization code;

[0042] Iteration control module: determines the termination or iteration of the optimization process based on the indicator comparison results and the existence of the pattern.

[0043] Beneficial Effects: Compared with existing technologies, this invention has the following significant advantages: By designing and implementing the first code comprehensibility evaluation index suitable for Java decompilation scenarios, and guided by this index, leveraging the text generation capabilities of a large language model to continuously optimize decompiled code, this effectively improves the comprehensibility of decompiled code and provides an improved solution for decompilers. Furthermore, this invention combines code patterns that affect comprehensibility to determine whether optimization has concluded, while guiding the large language model to perform targeted optimization, effectively improving optimization efficiency and enhancing optimization results. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] Figure 1 Flowchart of the present invention. DETAILED DESCRIPTION

[0045] The technical solution of the present invention will be further described below with reference to the accompanying drawings.

[0046] like Figure 1 As shown, an embodiment of the present invention provides a decompiled code optimization method guided by comprehensibility evaluation indicators based on a large language model, comprising the following steps:

[0047] S1: Use a Java decompiler to generate decompiled code; such as CFR, Fernflower, and Jadx, to generate decompiled code based on bytecode.

[0048] S2: Check the grammatical correctness and semantic consistency of the decompiled code; use the compiler javac to compile the decompiled code to complete the grammatical correctness check. The compiler will check whether the syntax of the code is correct during the compilation process. If the decompiled code can pass the compilation, its syntax is correct, otherwise there is a syntax error. Use the automated test generation tool Randoop to generate unit test cases, compile the test cases and run them, and check the semantic consistency of the decompiled code. During this period, the dependency management tool Maven is used to generate the dependency file paths required for compiling the decompiled code files and the test cases. If there is an error in the grammatical correctness or semantic consistency check, the process ends.

[0049] S3: Perform static analysis on the decompiled code to identify the code patterns and locations to be optimized in the decompiled code; use the Java source code parsing tool JavaParser to generate an abstract syntax tree for the decompiled code, and traverse the abstract syntax tree to identify whether the following patterns that affect comprehensibility exist in the code: (31) Deep nesting, that is, three or more nested conditional statements or loop statements. (32) Omitting parentheses in expressions containing mixed operators. (33) Statements with more than 120 characters. (34) Omitting braces after conditional statements or loop statements. (35) Inline assignment in expressions. (36) Using numeric literals instead of constants. Record the locations of these patterns at the same time. If there are no code patterns to be optimized, end the process.

[0050] S4: Optimize the decompiled code based on the code pattern and location to be optimized; design prompt words based on the code pattern and location to be optimized, and use the large language model to optimize the decompiled code. The specific prompt words are as follows: "Without adding comments, splitting methods, and adding new methods, optimize the following Java decompiled code to improve its understandability and readability. Try to fix the following potential problems: In method <method 1>, <n1>The line <schema description 1> exists, <n2> 、 <n3>Row exists <schema description 2>".

[0051] S5: Check the syntactic correctness and semantic consistency of the optimized code. Use the javac compiler to perform a syntax check on the optimized code. If the decompiled code compiles successfully, its syntax is correct; otherwise, there are syntax errors. Use the unit tests generated in S2 to perform a semantic check on the optimized code. If there are errors in the syntactic correctness or semantic consistency check, check whether the number of retries is too high. If so, terminate the process. Otherwise, return to S4 and try optimizing the decompiled code again.

[0052] S6: Calculate and compare the comprehensibility of the code before and after optimization; by expanding cognitive complexity, realize the comprehensibility evaluation index suitable for Java decompilation scenarios - decompilation cognitive complexity, and add the following rules on the basis of cognitive complexity: (61) When calculating the nesting increment in a deeply nested structure, if the nesting level is not less than 3, increase the index value. (62) When encountering mixed operators without parentheses, remove common mixed sequences of arithmetic operators, such as addition, subtraction, multiplication and division mixed operations, and increase the index value each time one is encountered. (63) When encountering an overly long line, add the quotient of its length divided by 120 (overly long line threshold) to the index. (64) When encountering an if, else, for, do or while statement without curly braces, increase the index value. (65) When identifying an assignment expression inlined into other expressions, increase the index value. (66) When identifying a numeric literal in an expression, except for the common -1, 0 and 1, increase the index value. Calculate the comprehensibility evaluation index value for the decompiled code before and after optimization. If the comprehensibility has not improved, end the process.

[0053] S7: Repeat S3-S6 until the code comprehensibility is no longer improved or there are no code fragments to be optimized. < / n2>

Claims

1. A decompiled code optimization method guided by comprehensibility evaluation indicators based on a large language model, characterized in that: The following steps are involved: S1: Use the decompiler to generate decompiled code; S2: Check the grammatical correctness and semantic consistency of the decompiled code; S3: Perform static analysis on the decompiled code through the abstract syntax tree to identify six code patterns and locations to be optimized; S4: Generate prompt words based on the recognized code pattern and location, and guide the large language model to optimize the decompiled code; S5: Check the syntax correctness and semantic consistency of the optimized code; S6: Calculate and compare the understandability of the code before and after optimization based on the decompilation cognitive complexity index; S7: If the comprehensibility is improved, S3-S6 are repeatedly executed with the optimized code as input until the comprehensibility is no longer improved or there are no more optimized code fragments.

2. The decompiled code optimization method based on the comprehensibility evaluation index guidance of a large language model according to claim 1, characterized in that: In step S2, the syntax correctness check is implemented by compiling the decompiled code through a Java compiler; Semantic consistency checking is achieved through at least one of the following methods: using unit test cases to compare the input and output results of the code before and after optimization; using symbolic execution to verify the consistency of the final state of the code under the same initial state.

3. The decompiled code optimization method based on the comprehensibility evaluation index guidance of a large language model according to claim 2, characterized in that: When test cases are lacking, unit test cases are automatically generated by the Randoop tool to perform semantic consistency verification.

4. The decompiled code optimization method based on the comprehensibility evaluation index guidance of a large language model according to claim 1, characterized in that: Code patterns include: (31) Three or more nested conditional statements or loop statements; (32) Parentheses are omitted in mixed operator expressions; (33) Statements whose single line length exceeds the set threshold; (34) Omit curly braces after conditional or loop statements; (35) Inline assignment in expressions; (36) Numeric literals replace constants.

5. The decompiled code optimization method based on the comprehensibility evaluation index guidance of a large language model according to claim 1, characterized in that: In step S3, the single-line length threshold is set to 120 characters, and the numerical literals exclude the common cases of -1, 0, and 1.

6. The decompiled code optimization method based on the comprehensibility evaluation index guidance of a large language model according to claim 1, characterized in that: The prompt words in step S4 include the following elements: (41) It is required not to add comments or split methods; (42) Specifically specify the method name, line number and corresponding pattern type of the code to be optimized; (43) The example prompt word structure is: "Optimization method <method 1> <n1><Pattern Description 1> and <n2> <Pattern Description 2>" of the line.

7. The decompiled code optimization method based on the comprehensibility evaluation index guidance of a large language model according to claim 1, characterized in that: In step S6, the decompilation cognitive complexity index is calculated according to the following rules: (61) Increase the complexity value for nested structures with three or more layers; (62) Increase the complexity value for mixed operator expressions without parentheses; (63) Add the quotient of the length of the overlong line divided by the threshold to the complexity; (64) Increase the complexity value for conditional / loop statements that lack braces; (65) Increase the complexity value for inline assignments in expressions; (66) Increase complexity values ​​for unconventional numeric literals.

8. The decompiled code optimization method based on the comprehensibility evaluation index guidance of a large language model according to claim 1, characterized in that: In step S5, if the optimized code fails the syntax or semantic check, the process returns to S4 to regenerate the optimized code when the number of retries does not reach the upper limit.

9. A decompiled code optimization system guided by comprehensibility evaluation indicators based on a large language model, characterized in that: include: Decompilation module: used to generate decompiled code through Java decompiler; Compilation and verification module: performs syntax checking through the Java compiler and manages dependencies through Maven; Test generation module: Generates unit test cases through the Randoop tool to verify semantic consistency; Pattern recognition module: Generates an abstract syntax tree through JavaParser and extracts six code patterns and locations; Index calculation module: calculates the decompilation cognitive complexity index value based on the abstract syntax tree; Large model optimization module: Generates prompt words based on recognition patterns to drive large language model optimization code; Iteration control module: determines the termination or iteration of the optimization process based on the indicator comparison results and the existence of the pattern.

Citation Information

Cited By

  • Code repairing method and device, electronic equipment, storage medium and product

    CN120929296A