A Method and System for Inline Assembly Code Analysis Assisted by a Large Language Model

By using a large language model to assist in the analysis of inline assembly code, constructing an assembly call graph and generating a code summary, and combining multiple verification mechanisms, the problem of handling inline assembly code in static program analysis is solved, improving the accuracy and efficiency of the analysis, and is applicable to various program analysis frameworks.

CN119847591BActive Publication Date: 2025-10-28SHANGHAI JIAOTONG UNIV
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202510035909.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-01-09
Publication Date
2025-10-28
Estimated Expiration
2045-01-09

AI Technical Summary

Technical Problem

Existing static program analysis techniques are difficult to effectively process C language inline assembly code, resulting in inaccurate analysis results and low efficiency. In particular, the processing of inline assembly in complex software systems lacks reliability.

Method used

A large language model is used to assist in the analysis of inline assembly code. By constructing an assembly call graph, a code summary is generated. The accuracy and reliability of the summary are ensured by combining self-verification, compile-time checks, and manual checks.

Benefits of technology

It improves the accuracy and efficiency of inline assembly code analysis, reduces the time overhead of manual instruction set modeling, enhances the reliability and scalability of program analysis, and is applicable to various program analysis frameworks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119847591B_ABST
    Figure CN119847591B_ABST
Patent Text Reader

Abstract

This invention provides a method and system for inline assembly code analysis based on a large language model (LLM), comprising: locating and measuring the positions of inline assembly code that may affect the static program analysis effect in a C source code project; selecting target code summary points by constructing a call graph based on the identified inline assembly code and selecting suitable positions for code summarization within the graph; generating automated code summaries with LLM assistance by guiding the LLM to analyze the functionality of inline assembly through prompt word engineering and generating code summaries in a specific format; verifying the correctness of the code summaries by checking the correctness of the generated code summaries through methods such as self-verification by the LLM; and deploying and applying the verified code summaries in the program analysis framework to be optimized. This invention overcomes the shortcomings of existing source code-level static program analysis frameworks in handling the difficult cross-language analysis between source code and inline assembly code.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the technical field of computer program analysis, specifically to a method and system for analyzing inline assembly code based on a large language model. Background Technology

[0002] Currently, with the widespread application of complex software systems such as operating systems, databases, and browsers in various fields, research on their security characteristics and potential risks is particularly important. Static program analysis, as one research method, provides strong support for code analysis tasks due to its ability to automatically acquire behavioral information of the target program without actually running it. This technique primarily uses source code as input, revealing potential problems and optimization potential by analyzing the code's structure, syntax, and semantics.

[0003] However, despite its significant contributions to improving software security and robustness, static program analysis still faces numerous limitations and challenges. On one hand, while pursuing reliability, static analysis often struggles to ensure completeness, potentially leading to false positives in the analysis results and increasing the complexity and cost of subsequent processing. On the other hand, for complex software systems, static analysis requires processing large amounts of input code, incurring substantial computational and time overhead. Therefore, existing program analysis frameworks, to some extent, sacrifice analytical accuracy to improve scalability and efficiency, but this can also negatively impact the analysis results.

[0004] In particular, static analysis techniques face even greater challenges when encountering difficult-to-handle programming language features like C inline assembly. C inline assembly allows developers to directly embed assembly language within C source code to optimize program performance or access low-level hardware. However, the instructions in inline assembly blocks are not modified during compilation and are directly embedded into the corresponding locations in the target binary program. This compromises C's cross-platform compatibility and requires developers to be familiar with assembly syntax. More importantly, there are significant syntactic and semantic differences between inline assembly and C code—a cross-language analysis challenge—that conventional static analysis tools struggle to effectively handle. Furthermore, assembly code is often architecturally specific, lacks portability, and its diverse instruction sets further complicate the analysis process.

[0005] In complex software systems like the Linux kernel, which require interaction with low-level hardware and demand high performance, inline assembly is widely used. However, most static program analysis frameworks targeting the Linux kernel either ignore inline assembly or only perform limited processing through manual intervention, which significantly compromises the reliability of the analysis. Therefore, effectively handling inline assembly code is crucial for improving the accuracy and reliability of static program analysis techniques.

[0006] A source code static analysis method is disclosed in patent document CN111240982A, comprising: performing lexical analysis and syntactic analysis on the source code to generate an abstract syntax tree; performing semantic analysis on the abstract syntax tree to obtain a directed control flow graph and a data flow graph; traversing the directed control flow graph to obtain slice data; and finding and outputting variables that may have defects in the source code based on the data flow graph and the slice data.

[0007] With the rapid development of Large Language Models (LLMs), they are gradually demonstrating enormous potential in software engineering and computer security research. During the pre-training phase, LLMs learn from the source code, documentation, and use cases of numerous open-source projects, thus possessing rich knowledge of the software and systems domain, enabling them to better assist researchers in completing specific tasks. Large language models offer new opportunities to address classic challenges in static program analysis. By combining the analytical capabilities of large language models, it is hoped that the automated processing of inline assembly code can be achieved, thereby further improving the accuracy and reliability of static program analysis. Summary of the Invention

[0008] To address the shortcomings of existing technologies, the purpose of this invention is to provide a method and system for analyzing inline assembly code based on a large language model.

[0009] According to the present invention, an inline assembly code analysis method based on a large language model is provided, the method comprising the following steps:

[0010] Step 1: Locate the inline assembly code in the C source code project and record the mapping relationship between the source code and the inline assembly code compiled into intermediate language;

[0011] Step 2: Construct an assembly call graph based on the inline assembly code, and select locations in the graph where code summarization can be performed;

[0012] Step 3: Guide the large language model to analyze the functionality of inline assemblies and generate code summaries in a preset format through prompt word engineering;

[0013] Step 4: Verify the correctness of the generated code summary through self-verification using a large language model, compile-time checks, and manual checks;

[0014] Step 5: Deploy and apply the validated code digest within the program analysis framework to be optimized.

[0015] Preferably, step 1 includes: preprocessing and optimizing the macro functions and inline functions in the source code to locate the inline assembly code, and recording the mapping relationship between the source code and the intermediate language caused by macro function expansion and inline function expansion.

[0016] Preferably, in step 2, the constructed assembly call graph is a variant of the function call graph, including parent nodes, child nodes, and edges representing function call relationships or macro expansion relationships, wherein the leaf nodes correspond to the definition and implementation of inline assembly code.

[0017] Preferably, in step 3, the prompt word engineering includes defining the format and requirements of the code summary, as well as the detailed scope of the assembly code behavior to be described. The designed prompt word engineering drives the large language model to generate a summary that can describe the data flow and pointer-related behavior of the target assembly code.

[0018] Preferably, in step 4, the large language model self-verification guides the large language model to re-examine the previously given answer through additional prompt words, and corrects it if it is wrong;

[0019] The compile-time check adjusts the generated code summary based on the error messages output during the compilation process of the project under test;

[0020] Manual assistance involves directly comparing the content of the original function with the generated summary and making adjustments accordingly.

[0021] Preferably, the program analysis framework to be optimized in step 5 includes the SVF program analysis framework and the KLEE program analysis framework.

[0022] This invention also provides an inline assembly code analysis system based on a large language model, the system comprising the following modules:

[0023] Module 1: Locate the inline assembly code in the C source code project and record the mapping relationship between the source code and the inline assembly code compiled into intermediate language;

[0024] Module 2: Construct an assembly call graph based on the inline assembly code, and select locations in the graph where code summarization can be performed;

[0025] Module 3: Guides the large language model to analyze the functionality of inline assemblies through prompt word engineering and generates code summaries in a preset format;

[0026] Module 4: Verify the correctness of the generated code summary through self-verification by a large language model, compile-time checks, and manual checks;

[0027] Module 5: Deploy and apply validated code summaries within the program analysis framework to be optimized.

[0028] Preferably, module 1 includes: preprocessing and optimizing macro functions and inline functions in the source code to locate inline assembly code, and recording the mapping relationship between the source code and the intermediate language caused by macro function expansion and inline function expansion.

[0029] Preferably, in module 2, the constructed assembly call graph is a variant of the function call graph, including parent nodes, child nodes, and edges representing function call relationships or macro expansion relationships, wherein the leaf nodes correspond to the definition and implementation of inline assembly code.

[0030] Preferably, in module 3, the prompt word engineering includes defining the format and requirements of the code summary, as well as the detailed scope of the assembly code behavior to be described. The designed prompt word engineering drives the large language model to generate a summary that can describe the target assembly code data flow and pointer-related behavior.

[0031] In module 4, the large language model self-verification guides the large language model to re-examine the previously given answer through additional prompt words, and corrects it if it is wrong.

[0032] The compile-time check adjusts the generated code summary based on the error messages output during the compilation process of the project under test;

[0033] Manual assistance involves directly comparing the content of the original function with the generated summary and making adjustments accordingly.

[0034] The program analysis frameworks to be optimized in module 5 include the SVF program analysis framework and the KLEE program analysis framework.

[0035] Compared with the prior art, the present invention has the following beneficial effects:

[0036] 1. This invention introduces a large language model-assisted inline assembly code analysis method, which can more accurately analyze C language inline assembly code, solving the problem of cross-language analysis difficulties between source code and inline assembly code in static program analysis frameworks; the large language model has powerful code understanding and generation capabilities, and can deeply understand the semantics and behavior of assembly code, thereby generating more accurate code summaries and improving the accuracy of analysis;

[0037] 2. Traditional methods often require manual instruction set modeling when processing inline assembly code, which is not only time-consuming and labor-intensive but also prone to errors. This invention fully utilizes the domain knowledge of instruction sets of various architectures acquired by the large language model during pre-training, which can support the analysis of inline assembly code of multiple architectures and greatly reduce the time cost of manual instruction set modeling.

[0038] 3. The automated analysis process provided by this invention includes inline assembly code location, summary point selection, automated summary generation, summary verification, and deployment application, realizing a pipeline operation for inline assembly code analysis; improving the efficiency of program analysis; and through multiple verification mechanisms, ensuring the correctness and comprehensiveness of the generated code summary, thereby improving the reliability of program analysis.

[0039] 4. This invention is not only applicable to common program analysis frameworks such as SVF and KLEE, but also has good scalability to other general program analysis frameworks; it can be widely applied in various static program analysis scenarios, providing strong support for improving software security and robustness. Attached Figure Description

[0040] Other features, objects, and advantages of the present invention will become more apparent from the following detailed description of non-limiting embodiments with reference to the accompanying drawings:

[0041] Figure 1 An overall block diagram of the automated analysis process of inline assembly code based on a large language model-assisted method, provided in a preferred embodiment of the present invention;

[0042] Figure 2 Detailed operational principle diagrams of steps 3 and 4 provided for a preferred embodiment of the present invention. Detailed Implementation

[0043] The present invention will now be described in detail with reference to specific embodiments. These embodiments will help those skilled in the art to further understand the present invention, but do not limit the invention in any way. It should be noted that those skilled in the art can make several changes and improvements without departing from the concept of the present invention. These all fall within the scope of protection of the present invention.

[0044] Example 1:

[0045] Reference Figure 1 and Figure 2 According to the present invention, an inline assembly code analysis method based on a large language model is provided, the method comprising the following steps:

[0046] Step 1: Locate the inline assembly code in the C source code project and record the mapping relationship between the source code and the inline assembly code compiled into the intermediate language; perform preprocessing and code optimization on the macro functions and inline functions in the source code to locate the inline assembly code, and record the mapping relationship between the source code and the intermediate language caused by macro function expansion and inline function expansion.

[0047] Step 2: Construct an assembly call graph based on the inline assembly code, and select locations in the graph where code summarization can be performed; the constructed assembly call graph is a variant of the function call graph, including parent nodes, child nodes, and edges representing function call relationships or macro expansion relationships, wherein the leaf nodes correspond to the definition and implementation of the inline assembly code.

[0048] Step 3: Guide the large language model to analyze the functionality of inline assembly and generate a code summary in a preset format through prompt word engineering. Prompt word engineering defines the format and requirements of the code summary, as well as the detailed scope of the assembly code behavior to be described. It drives the large language model to generate a summary that describes the data flow and pointer-related behavior of the target assembly code. First, based on the code summary information obtained in the previous step, collect related source code content, including inline assembly statements and their corresponding function declarations, macro definitions, function call chains, macro expansion chains, and detailed information such as code comments. Second, use this source code information to instantiate a prompt word template, which includes an analysis task description and a code summary format, to request the large language model to analyze the code and provide preliminary analysis results. Then, guide the large language model to further explore whether there are any previously unconsidered program side effects based on the preliminary analysis results, and supplement and improve the preliminary analysis results based on these side effects. Finally, extract the generated code summary in the specified format from the final response provided by the large language model.

[0049] Step 4: Verify the correctness of the generated code summary through large language model self-verification, compile-time checking, and manual assisted checking; large language model self-verification guides the large language model to re-examine the previously given answer through additional prompt words, and corrects it if it is wrong; compile-time checking adjusts the generated code summary based on the error messages output during the compilation of the project under test; manual assisted checking makes adjustments by directly comparing the content of the original function with the generated summary.

[0050] Step 5: Deploy and apply validated code digests within the analysis framework to be optimized; the analysis framework to be optimized includes SVF analysis framework and KLEE analysis framework.

[0051] The present invention also provides an inline assembly code analysis system based on a large language model. The inline assembly code analysis system based on a large language model can be implemented by executing the process steps of the inline assembly code analysis method based on a large language model. That is, those skilled in the art can understand the inline assembly code analysis method based on a large language model as a preferred embodiment of the inline assembly code analysis system based on a large language model.

[0052] Example 2:

[0053] This invention also provides an inline assembly code analysis system based on a large language model, the system comprising the following modules:

[0054] Module 1: Locate inline assembly code in the C source code project and record the mapping relationship between the source code and the inline assembly code compiled into the intermediate language; perform preprocessing and code optimization on macro functions and inline functions in the source code to locate inline assembly code, and record the mapping relationship between the source code and the intermediate language caused by macro function expansion and inline function expansion.

[0055] Module 2: Construct an assembly call graph based on the inline assembly code, and select positions in the graph where code summarization can be performed; the constructed assembly call graph is a variant of the function call graph, including parent nodes, child nodes and edges representing function call relationships or macro expansion relationships, wherein the leaf nodes correspond to the definition and implementation of the inline assembly code.

[0056] Module 3: Guides the large language model to analyze the functionality of inline assembly and generate code summaries in a preset format through prompt word engineering. Prompt word engineering includes defining the format and requirements of the code summaries, as well as the detailed scope of the assembly code behavior to be described. Through the designed prompt word engineering, the large language model is driven to generate summaries that can describe the data flow and pointer-related behaviors of the target assembly code.

[0057] Module 4: Verify the correctness of the generated code summary through large language model self-verification, compile-time checks, and manual checks; large language model self-verification guides the large language model to re-examine the previously given answers through additional prompts, and corrects them if they are incorrect; compile-time checks adjust the generated code summary based on the error messages output during the compilation of the test project; manual checks make adjustments by directly comparing the content of the original function with the generated summary.

[0058] Module 5: Deploying and applying validated code digests within the analysis frameworks to be optimized; the analysis frameworks to be optimized include the SVF analysis framework and the KLEE analysis framework.

[0059] Example 3:

[0060] The platform implemented in this embodiment is Ubuntu 22.04 operating system, using LLVM-14 compilation environment. The large language model can meet the requirements of this invention using mainstream pre-trained models on the market. The target program analysis framework to be optimized is SVF (Static Value-Flow Analysis Framework) version 2.8.

[0061] Figure 1This is an overall block diagram of the automated analysis process of inline assembly code based on a large language model, provided in a preferred embodiment of the present invention.

[0062] like Figure 1 As shown, the automated analysis process of inline assembly code based on a large language model in this embodiment includes the following steps:

[0063] Step 1: Locating C Language Inline Assembly. Since inline assembly is frequently used in macro functions and inline functions within C source code, this code is expanded to multiple usage locations during compiler preprocessing and code optimization. Most static analysis frameworks perform analysis at the intermediate language level; therefore, it's necessary to establish a mapping relationship between inline assembly statements between the C source code and the compiled intermediate language code to provide sufficient support for subsequent analysis.

[0064] Step 2, Target Code Summary Point Selection. First, an assembly call graph (AsmCallGraph) is constructed from bottom to top using inline assembly. This is a variant of the function call graph, where parent nodes represent callers, child nodes represent callees, edges in the graph represent function call relationships or macro expansion relationships, and all leaf nodes correspond to the definition and implementation of inline assembly code.

[0065] Building upon this, since the analysis results differ between direct call locations in inline assembly and higher-level call locations, this step prioritizes generating summaries for utility functions with relatively simple functions that can be reused by other code, as these possess clear semantic behavior. On the assembly call graph, heuristic rules, combined with the auxiliary judgment of a large language model, are used to select utility functions that are as high-level as possible while still having clear functional semantics as target code summary points, and these are then passed as input to the next step.

[0066] Step 3: Automated code summarization assisted by a large language model. For the code summarization points selected by heuristic rules, an automated process assisted by a large language model was designed based on prompt word engineering. This process can analyze the assembly code behavior within the given source code content and ultimately generate a code summary that meets the requirements of static analysis. By introducing few-shot examples and chain of thought, the large language model can be guided to perform a deeper analysis of code snippets containing inline assembly, while generating more accurate and comprehensive code summaries.

[0067] The generated digest format can also be viewed as a function shim. This code digest method appropriately trims and simplifies the target function, generating a function written in C source code. However, the simplified function omits much of the code implementation, including inline assembly, and only includes the code behaviors that are of interest to program analysis, such as function parameters, return values, and the read / write status of global variables. This simplifies the function logic or provides an easily analyzable equivalent implementation, thereby improving the accuracy and efficiency of program analysis.

[0068] Step 4: Code Summarization Correctness Verification. Since large language models commonly suffer from hallucination problems when generating answers, it is necessary to verify the correctness of their automatically generated code summaries to improve the reliability of program analysis. First, by adding an extra self-verification process to the prompt word project, the large model is guided to check its previous answers; if problems are found, appropriate adjustments need to be made. Second, since code summaries need to be compiled before application, the compiler's error output can be used to check the code summaries. Finally, if the analysis scenario has high reliability requirements, a manual verification sub-step can be introduced to further filter out improperly implemented code summaries.

[0069] Step 5: Deployment and application of the code digest. This embodiment targets the SVF program analysis framework. When deploying the code digest generated by this invention, simply insert the generated function pad-style code digest into the C source file responsible for external API processing within the SVF-2.8 framework, and recompile the SVF framework and related tools. Applying the program analysis framework enhanced by this invention achieves more accurate and reliable static analysis results. It should be noted that although this embodiment uses the SVF framework, the method proposed in this invention supports most common program analysis frameworks and has excellent scalability.

[0070] Further, Figure 2 This is a detailed operational principle diagram of steps 3 and 4 provided in a preferred embodiment of the present invention.

[0071] like Figure 2 As shown, both the automated code summarization assisted by the large language model in step 3 and the correctness verification of the code summarization in step 4 require the analytical capabilities of the large language model.

[0072] During code digest generation, by defining the code digest format and specifying the detailed categories of code behaviors of interest in the current analysis scenario within the prompt word template, the large language model can be guided to analyze input containing inline assembly code and generate an equivalent and easily analyzable code digest. In the correctness verification process, a combination of three methods can be used to determine whether the generated code digest meets the requirements and has application value. Specifically, the large language model's self-verification can be implemented as a multi-round result-based voting model, driving the large language model to independently and in multiple rounds evaluate the current code digest, combining multiple results to determine whether the current object is correct.

[0073] Those skilled in the art can understand this embodiment as a more specific description of Embodiment 1 and Embodiment 2.

[0074] Those skilled in the art will understand that, besides implementing the system and its various devices, modules, and units provided by this invention in the form of purely computer-readable program code, the same functions can be achieved entirely through logical programming of the method steps, making the system and its various devices, modules, and units of this invention function in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers, and embedded microcontrollers. Therefore, the system and its various devices, modules, and units provided by this invention can be considered as a hardware component, and the devices, modules, and units included therein for implementing various functions can also be considered as structures within the hardware component; alternatively, the devices, modules, and units for implementing various functions can be considered as both software modules implementing the method and structures within the hardware component.

[0075] Specific embodiments of the present invention have been described above. It should be understood that the present invention is not limited to the specific embodiments described above, and those skilled in the art can make various changes or modifications within the scope of the claims, which do not affect the essence of the present invention. Unless otherwise specified, the embodiments and features described in this application can be arbitrarily combined with each other.

Claims

1. A method for analyzing inline assembly code based on a large language model, characterized in that, The method includes the following steps: Step 1: Locate the inline assembly code in the C source code project and record the mapping relationship between the source code and the inline assembly code compiled into intermediate language; Step 2: Construct an assembly call graph based on the inline assembly code, and select locations in the graph where code summarization can be performed; Step 3: Guide the large language model to analyze the functionality of inline assemblies and generate code summaries in a preset format through prompt word engineering; Step 4: Verify the correctness of the generated code summary through self-verification using a large language model, compile-time checks, and manual checks; Step 5: Deploy and apply the validated code digest within the analysis framework of the program to be optimized; Step 1 includes: preprocessing and optimizing the macro functions and inline functions in the source code to locate the inline assembly code, and recording the mapping relationship between the source code and the intermediate language caused by macro function expansion and inline function expansion; In step 2, the constructed assembly call graph is a variant of the function call graph, including parent nodes, child nodes, and edges representing function call relationships or macro expansion relationships, wherein the leaf nodes correspond to the definition and implementation of inline assembly code; In step 3, the prompt word engineering includes defining the format and requirements of the code summary, as well as the detailed scope of the assembly code behavior to be described. The designed prompt word engineering drives the large language model to generate a summary that can describe the data flow and pointer-related behavior of the target assembly code.

2. The inline assembly code analysis method based on a large language model as described in claim 1, characterized in that, In step 4, the large language model self-verification guides the large language model to re-examine the previously given answer through additional prompt words, and corrects it if it is wrong. The compile-time check adjusts the generated code summary based on the error messages output during the compilation process of the project under test; Manual assistance involves directly comparing the content of the original function with the generated summary and making adjustments accordingly.

3. The inline assembly code analysis method based on a large language model as described in claim 1, characterized in that, The program analysis framework to be optimized in step 5 includes the SVF program analysis framework and the KLEE program analysis framework.

4. A large language model-assisted inline assembly code analysis system, characterized in that, The system includes the following modules: Module 1: Locate the inline assembly code in the C source code project and record the mapping relationship between the source code and the inline assembly code compiled into intermediate language; Module 2: Construct an assembly call graph based on the inline assembly code, and select locations in the graph where code summarization can be performed; Module 3: Guides the large language model to analyze the functionality of inline assemblies through prompt word engineering and generates code summaries in a preset format; Module 4: Verify the correctness of the generated code summary through self-verification by a large language model, compile-time checks, and manual checks; Module 5: Deploying and applying validated code summaries within the analysis framework of the program to be optimized; The module 1 includes: preprocessing and optimizing macro functions and inline functions in the source code to locate inline assembly code, and recording the mapping relationship between the source code and the intermediate language caused by macro function expansion and inline function expansion; In module 2, the constructed assembly call graph is a variant of the function call graph, including parent nodes, child nodes, and edges representing function call relationships or macro expansion relationships, wherein the leaf nodes correspond to the definition and implementation of inline assembly code; In module 3, the prompt word engineering includes defining the format and requirements of the code summary, as well as the detailed scope of the assembly code behavior to be described. The designed prompt word engineering drives the large language model to generate a summary that can describe the data flow and pointer-related behavior of the target assembly code.

5. The inline assembly code analysis system based on a large language model as described in claim 4, characterized in that, In module 4, the large language model self-verification guides the large language model to re-examine the previously given answer through additional prompt words, and corrects it if it is wrong. The compile-time check adjusts the generated code summary based on the error messages output during the compilation process of the project under test; Manual assistance involves directly comparing the content of the original function with the generated summary and making adjustments accordingly. The program analysis frameworks to be optimized in module 5 include the SVF program analysis framework and the KLEE program analysis framework.

Citation Information

Patent Citations

  • Source code static analysis method

    CN111240982A

  • Enclave program framework dynamic TCB generation system and method based on LibOS

    CN115469924A

  • Code abstract generation method based on large language model and static analysis tool

    CN117873559A