Rust language system security enhancement method and device based on large language model
By using a large language model-based approach, combined with Miri testing and abstract syntax tree (AST), and adopting a two-stage repair process, the undefined behavior problem in the unsafe Rust part of the Rust language was solved, improving the security and development efficiency of the Rust system.
Patent Information
- Application Number
- CN202411954495.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-27
- Publication Date
- 2025-10-21
- Estimated Expiration
- 2044-12-27
AI Technical Summary
Existing technologies have undefined behaviors in the unsafe Rust part of the Rust language, which leads to system security and stability issues, and existing tools find it difficult to effectively identify and repair complex semantic relationships and context dependencies.
A large language model-based approach, combined with Miri testing and abstract syntax trees (ASTs), is used to implement a two-stage repair process: first, assertions are added and code is modified; then, an adaptive rollback mechanism and knowledge base are used to optimize prompt words to ensure code security and semantic correctness.
It significantly improves the security and reliability of the Rust system, reduces the uncertainty of unsafe Rust, improves development efficiency, and reduces the risk of human error.
Smart Images

Figure CN120068077B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer operating systems, and in particular to a method and device for enhancing the security of a Rust language system based on a large language model. Background Art
[0002] Operating system security is crucial in computing systems. The Rust language offers a new paradigm for building secure, comprehensive operating systems. Code written solely in Safe Rust is immune to memory safety issues. However, despite Rust's significant security advantages, potential risk areas remain: the Unsafe Rust portion. These operations are unavoidable in system development, reducing the stability of Rust-based operating systems. Therefore, improving their security remains a significant challenge.
[0003] First, the Rust language's unique design philosophy, including ownership mechanisms, borrowing rules, and lifetimes, enables developers to explicitly manage memory, reducing the memory leaks and data races common in traditional programming languages. However, this design also increases the language learning curve and can lead to misuse of unsafe Rust. While the types of unsafe Rust operations are limited (five unsafe operations), minor semantic changes can significantly impact how code is modified, resulting in similar undefined behavior potentially resulting in incomparable solutions. In such cases, developers must deeply understand the semantics of the code and design reliable verification mechanisms. Therefore, understanding semantics and possessing specialized knowledge are crucial to solving Rust safety issues.
[0004] Secondly, current approaches to improving Rust language security primarily rely on code analysis tools, static checkers, and runtime verifiers to discover and fix potential issues with unsafe code. These tools typically incorporate rule templates or automated models to detect common undefined behavior patterns and generate remediation suggestions. However, these approaches often fail to fully capture the complex semantic relationships and contextual dependencies implicit in the code. For example, for code that relies on underlying hardware interactions, these tools may struggle to provide accurate and efficient solutions, requiring engineers to apply their expertise through a rigorous process of logical reasoning, judgment, and verification, where the efficiency of the solution is closely related to experience and expertise. Therefore, there is a need to explore more efficient, flexible, and intelligent technical means to reduce reliance on manual intervention by engineers and improve the accuracy and speed of problem discovery and repair.
[0005] Finally, the powerful self-reasoning and semantic understanding capabilities of large language models (LLMs) offer new opportunities for enhancing the security of Rust programs. LLMs can learn complex syntactic and semantic patterns from massive amounts of code data, helping developers more efficiently identify potential risk points, generate remediation suggestions, and optimize code structure. However, due to high training costs and a lack of high-quality data in specific domains, traditional LLMs struggle to deeply understand complex relational reasoning when working with specialized languages like Rust. Furthermore, LLMs can exhibit "hallucination" when generating suggestions, where the output is inconsistent with the actual semantics or context, further limiting their effectiveness in direct application. Therefore, optimizing LLM architecture and training methods for the Rust domain, combining domain knowledge with semantic constraint mechanisms, has become a new direction for improving Rust security.
[0006] In general, due to the limitations of existing technologies in semantic understanding, complex scenario reasoning, and dynamic problem repair, the comprehensive improvement of Rust language security still faces huge challenges. Summary of the Invention
[0007] Technical problem to be solved by the present invention: In response to the above-mentioned problems in the prior art, a method and device for enhancing the security of the Rust language system based on a large language model are provided. The present invention aims to solve the system security problems introduced by the undefined behavior generated by the interaction between safe Rust and unsafe Rust in the Rust language from a system perspective, reduce the uncertainty of unsafe Rust, and thus improve the overall security, reliability and development efficiency of the Rust system.
[0008] In order to solve the above technical problems, the technical solution adopted by the present invention is:
[0009] A method for enhancing the security of the Rust language system based on a large language model includes the following steps:
[0010] S1, obtain the input Rust system code snippet;
[0011] S2, perform Miri test on the Rust system code snippet. If the test passes, jump to step S8; otherwise, jump to step S3;
[0012] S3, combines a large language model and prompt words to add assertions and modifications to Rust system code snippets;
[0013] S4, perform Miri testing on the Rust system code snippet with added assertions and modifications. If the test passes, jump to step S8; otherwise, jump to step S5;
[0014] S5, determine whether the iteration maximum value has been reached. If not, jump to step S3 to continue iterating; otherwise, jump to step S6;
[0015] S6, select the Rust system code snippet with the least number of errors among the original Rust system code snippet, the Rust system code snippet with added assertions, and the modified Rust system code snippet as the best code, and roll back the Rust system code snippet to the best code;
[0016] S7, based on the abstract syntax tree AST extracted by Miri test and the knowledge base enhanced prompt words, jump to step S3;
[0017] S8, testing semantic acceptability of Rust system code snippets.
[0018] Optionally, step S2 includes:
[0019] S2.1, perform Miri testing on the Rust system code snippet to determine whether the Rust system code snippet contains an undefined behavior error message. If no undefined behavior error message exists, the test is considered passed and the process jumps to step S8; otherwise, the process jumps to step S2.2;
[0020] S2.2, determine the unsafe Rust type that causes undefined behavior. If the unsafe Rust type that causes undefined behavior is the target unsafe Rust type, then determine that the test fails and jump to step S3. The target unsafe Rust type includes five types: dereferencing raw pointers, calling unsafe functions or methods, accessing or modifying mutable static variables, implementing unsafe features, and accessing union fields; otherwise, determine that the test passes and jump to step S8.
[0021] Optionally, step S3 includes:
[0022] S3.1, combining a large language model and prompt words, automatically repairs Rust code in Rust system code snippets that cause undefined behavior of target unsafe Rust types by adding assertions;
[0023] S3.2, in combination with the large language model and hint words, modify the Rust code in the Rust system code fragment that causes undefined behavior of the target unsafe Rust type to provide an alternative safe implementation or modify it to reduce the undefined behavior in the Rust code without compromising the semantics of the code.
[0024] Optionally, the prompt words used in step S3.1 include:
[0025] ...The following is the error message... {log}, the idea of adding assertions is as follows:
[0026] 1. Add assertions before it becomes possible to cause undefined behavior to prevent it from happening;
[0027] 2. It is important and necessary to do so;
[0028] Only assertions were added, no other code modules were adjusted or removed;"
[0029] Among them, “…” indicates omitted content, {log} is the log of Miri test, ” indicates emphasis;
[0030] Optionally, the prompt words used in step S3.2 include:
[0031] Running the Miri test according to the code resulted in undefined behavior. The fix is as follows:
[0032] 1. Identify undefined behavior;
[0033] 2. The code that adds assertions cannot be modified, and there are problems with the logic itself;
[0034] 3. To maintain the functionality and semantics of the source code, avoid drastically changing the logical structure;
[0035] 4. Design safe alternatives. Refactor the code based on safe alternatives to ensure that the modified code not only avoids undefined behavior but also maintains the original functional logic and performance standards;
[0036] …Here is the code…{code}”
[0037] Among them, "..." indicates omitted content, {code} is the modified Rust system code snippet, ” for emphasis.
[0038] Optionally, the abstract syntax tree AST and knowledge base enhancement prompt words extracted based on the Miri test in step S7 include:
[0039] S7.1. Generate an abstract syntax tree (AST) from the Rust system code snippet that is rolled back to the optimal code using the large language model. The AST is a tree-like data structure that represents the grammatical structure of the program, where each node represents a grammatical element, and edges represent parent-child relationships between nodes, connecting the hierarchical structure of the grammatical elements.
[0040] S7.2, use the pruning function to prune and simplify the abstract syntax tree AST;
[0041] S7.3, calculate the similarity between the pruned and simplified AST and the correct AST stored in the knowledge base. If there is no correct AST with a similarity exceeding a preset threshold, then directly end and jump to step S3; otherwise, the correct AST with the best similarity is used as the target correct AST.
[0042] S7.4, extracting knowledge from the target correct abstract syntax tree AST, including: inputting the pruned and simplified abstract syntax tree AST and the target correct abstract syntax tree AST into a pre-trained large language model, obtaining repair suggestions for unsafe Rust types with undefined behavior and their repair paths in the pruned and simplified abstract syntax tree AST, wherein the repair suggestions include indicating which nodes need to be adjusted, which edges need to be replaced, and which structures need to add specific assertions to ensure code safety, and the large language model establishes a mapping relationship between the abstract syntax tree AST extracted by the Miri test, the correct abstract syntax tree AST stored in the knowledge base, and the repair suggestions for unsafe Rust types with undefined behavior and their repair paths in the abstract syntax tree AST extracted by the Miri test for pre-training;
[0043] S7.5, enhances the hint words with the knowledge extracted from the target correct abstract syntax tree AST, including adding repair suggestions for unsafe Rust types with undefined behavior and their repair paths to the hint words to achieve the enhancement of the hint words.
[0044] Optionally, before step S7.4, it also includes: collecting the abstract syntax tree AST extracted by the Miri test, the correct abstract syntax tree AST stored in the knowledge base, and the repair method for the unsafe Rust type with undefined behavior and its repair path in the abstract syntax tree AST extracted by the Miri test and constructing a training dataset; using the training dataset to train the large language model to establish a mapping relationship between the abstract syntax tree AST extracted by the Miri test, the correct abstract syntax tree AST stored in the knowledge base, and the repair suggestions for the unsafe Rust type with undefined behavior and its repair path in the abstract syntax tree AST extracted by the Miri test.
[0045] In addition, the present invention also provides a Rust language system security enhancement device based on a large language model, comprising a microprocessor and a memory connected to each other, wherein the microprocessor is programmed or configured to execute the Rust language system security enhancement method based on a large language model.
[0046] In addition, the present invention also provides a computer-readable storage medium, which stores a computer program or instruction, and the computer program or instruction is programmed or configured to execute the Rust language system security enhancement method based on a large language model through a processor.
[0047] In addition, the present invention also provides a computer program product, including a computer program or instructions, which are programmed or configured to execute the Rust language system security enhancement method based on a large language model through a processor.
[0048] Compared with the existing technology, the present invention has the following main advantages: In response to the system security issues introduced by the undefined behavior generated by the interaction between safe Rust and unsafe Rust in the Rust language, the present invention constructs a Rust language system security enhancement method driven by large language model (LLM) reasoning. Through security enhancement based on domain knowledge, semantic reasoning and automated verification, combined with adaptive rollback mechanism, knowledge base support and large language model (LLM) optimized repair, it can effectively solve the system security issues introduced by the undefined behavior generated by the interaction between safe Rust and unsafe Rust in the Rust language, reduce the uncertainty of unsafe Rust, and improve the overall security, reliability and development efficiency of the Rust system. BRIEF DESCRIPTION OF THE DRAWINGS
[0049] Figure 1 Schematic diagram of the basic process of the method of the embodiment of the present invention.
[0050] Figure 2 Schematic diagram of the two-stage repair process in an embodiment of the present invention.
[0051] Figure 3 Schematic diagram comparing the adaptive rollback mechanism (b) and the presence or absence of the rollback mechanism (a) in an embodiment of the present invention.
[0052] Figure 4 Schematic diagram of the process of enhancing prompt words based on AST and knowledge base in an embodiment of the present invention.
[0053] Figure 5 It is the percentage of different types of errors that pass the Miri test after repair in the embodiment of the present invention.
[0054] Figure 6 It is the percentage of semantic acceptability after different types of errors are repaired in the embodiment of the present invention.
[0055] Figure 7 It is the difference ratio of Miri test and semantic acceptability after different categories of errors are repaired in the embodiment of the present invention.
[0056] DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENTS In order to enable those skilled in the art to better understand the present invention, the following will provide a clear and complete description of the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0057] like Figure 1 As shown, the Rust language system security enhancement method based on the large language model in this embodiment includes the following steps:
[0058] S1, obtain the input Rust system code snippet;
[0059] S2, perform Miri test on the Rust system code snippet. If the test passes, jump to step S8; otherwise, jump to step S3;
[0060] S3, combines a large language model and prompt words to add assertions and modifications to Rust system code snippets;
[0061] S4, perform Miri testing on the Rust system code snippet with added assertions and modifications. If the test passes, jump to step S8; otherwise, jump to step S5;
[0062] S5, determine whether the iteration maximum value has been reached. If not, jump to step S3 to continue iterating; otherwise, jump to step S6;
[0063] S6, select the Rust system code snippet with the least number of errors among the original Rust system code snippet, the Rust system code snippet with added assertions, and the modified Rust system code snippet as the best code, and roll back the Rust system code snippet to the best code;
[0064] S7, based on the abstract syntax tree AST extracted by Miri test and the knowledge base enhanced prompt words, jump to step S3;
[0065] S8, testing semantic acceptability of Rust system code snippets.
[0066] The Miri compiler is a tool specifically designed to interpret Rust's Mid-level Intermediate Representation (MIR). By interpreting this intermediate representation, it can execute Rust programs for static and dynamic analysis to detect undefined behavior (UB) errors in Rust programs. These error messages include potential problems triggered during code execution, memory access errors, ownership or lifecycle-related errors, etc. Based on these logs, the system can identify code paths and locations that may lead to UB. In this embodiment, step S2 includes:
[0067] S2.1, perform Miri testing on the Rust system code snippet to determine whether the Rust system code snippet contains an undefined behavior error message. If no undefined behavior error message exists, the test is considered passed and the process jumps to step S8; otherwise, the process jumps to step S2.2;
[0068] S2.2, determine the unsafe Rust type that causes undefined behavior. If the unsafe Rust type that causes undefined behavior is the target unsafe Rust type, then determine that the test fails and jump to step S3. The target unsafe Rust type includes five types: dereferencing raw pointers, calling unsafe functions or methods, accessing or modifying mutable static variables, implementing unsafe features, and accessing union fields; otherwise, determine that the test passes and jump to step S8.
[0069] For the Rust language, unsafe Rust is limited to only five categories: dereferencing raw pointers; calling unsafe functions or methods; accessing or modifying mutable static variables; implementing unsafe features, and accessing union fields. The limited types of undefined behavior caused by unsafe Rust can be further divided into types that can be replaced by safe Rust APIs and types that require semantic modification. A key to this embodiment lies in the two-stage repair design driven by the large language model (LLM) in step S3. Specifically, Figure 2 As shown, step S3 in this embodiment includes:
[0070] S3.1, combining a large language model and prompt words, automatically repairs Rust code in Rust system code snippets that cause undefined behavior of target unsafe Rust types by adding assertions;
[0071] S3.2, combining the large language model and hints, modify Rust code in Rust system code snippets that cause undefined behavior for target unsafe Rust types to provide alternative safe implementations or modifications to reduce undefined behavior in Rust code without compromising the code's semantics. For the Rust language, unsafe Rust is limited to only five categories: dereferencing raw pointers; calling unsafe functions or methods; accessing or modifying mutable static variables; implementing unsafe traits; and accessing union fields. The limited types of undefined behavior caused by unsafe Rust can be further divided into those that can be replaced by safe Rust APIs and those that require semantic modification.
[0072] In this embodiment, the prompt words used in step S3.1 include:
[0073] ...The following is the error message... {log}, the idea of adding assertions is as follows:
[0074] 1. Add assertions before it becomes possible to cause undefined behavior to prevent it from happening;
[0075] 2. It is important and necessary to do so;
[0076] Only assertions were added, no other code modules were adjusted or removed;"
[0077] Among them, “…” indicates omitted content, {log} is the log of Miri test, " indicates emphasis. Each time step S3.1 is executed iteratively, the large language model will first try to add assertions in appropriate places to prevent undefined behavior, ensuring that any potential undefined behavior can be caught at runtime before propagation. The benefit of this approach is that it allows for timely detection and identification of potential undefined behaviors during code execution, preventing them from causing more serious system errors or security vulnerabilities. The corresponding prompt word engineering is shown in the above section. However, in some complex scenarios, undefined behavior involves deeper logical errors or dependencies on external states, which cannot be directly solved by assertions alone. To address this issue, a modification phase is introduced, in which it adjusts the error semantics in the Rust code to ensure that the functionality of the code remains unchanged while effectively reducing the risk of undefined behavior. These suggestions provide alternative safe implementations or modifications, enabling developers to maintain the correctness of the code without compromising safety.
[0078] In this embodiment, the prompt words used in step S3.2 include:
[0079] Running the Miri test according to the code resulted in undefined behavior. The fix is as follows:
[0080] 1. Identify undefined behavior;
[0081] 2. The code that adds assertions cannot be modified, and there are problems with the logic itself;
[0082] 3. To maintain the functionality and semantics of the source code, avoid drastically changing the logical structure;
[0083] 4. Design safe alternatives. Refactor the code based on safe alternatives to ensure that the modified code not only avoids undefined behavior but also maintains the original functional logic and performance standards;
[0084] …Here is the code…{code}”
[0085] Among them, "..." indicates omitted content, {code} is the modified Rust system code snippet, ” for emphasis.
[0086] When using a large language model framework to fix undefined behavior in Rust code, most of its errors are significantly reduced. However, some errors are increased. This phenomenon is called "large model illusion" and it is caused by the fact that the fixes generated by the model inadvertently introduce new problems, and the errors continue to accumulate over time. Figure 3 As shown in (a) in the figure, if there is no rollback mechanism, the process will enter the next stage from the final state of the assertion stage, exacerbating the effect of the illusion. Current solutions usually involve a direct rollback mechanism (rolling back to the initial state, resetting the code to its initial state to eliminate accumulated errors, thereby mitigating the spread of incorrect repairs. However, this approach sometimes leads to the loss of valuable partial corrections during the iteration process. For example, the number of errors in the iteration increases during the repair process, but the overall trend may show a fluctuating downward behavior. Analogy: humans often make frequent and random errors in the reasoning process, just like large language models, but are ultimately able to solve challenging problems. Therefore, this embodiment believes that large language models have a certain self-correction ability, allowing them to gradually converge to the correct answer through multiple iterations.
[0087] In order to avoid the illusion of large models and utilize the adaptive problem-solving capabilities of large language models, this embodiment introduces an adaptive rollback mechanism. Specifically, in step S6 of this embodiment, the Rust system code snippet with the least number of errors is selected as the best code among the original Rust system code snippet, the Rust system code snippet with added assertions, and the modified Rust system code snippet, and the Rust system code snippet is rolled back to the best code, which is to introduce an adaptive rollback mechanism for the embodiment. Figure 3As shown in (b), this adaptive rollback mechanism does not simply roll back to the initial state, but intelligently selects intermediate states based on the observed error reduction pattern, ensuring a more efficient and refined correction process. After each stage, the system rolls back to the best code state before entering the next stage, where the best code is the state with the least number of detected errors. For example, in the assertion stage, two ideas are generated. and , resulting in the sequence The system then rolls back from this sequence to the optimal code state with the fewest errors before moving on to the next revision phase. This ensures that subsequent iterations are based on the most refined and stable versions of the code, minimizing error propagation and improving the accuracy of the overall fix.
[0088] To enhance the correction process and support more professional Rust code repair, this embodiment integrates a knowledge base centered on Abstract Syntax Trees (AST) and semantic analysis.
[0089] See also Figure 4To enhance the correction process and support more professional Rust code repair, this embodiment integrates a knowledge base centered around Abstract Syntax Trees (ASTs) and semantic analysis. The specific implementation process for this component includes: first, generating an AST tree from a Rust system code snippet and pruning and optimizing it. Then, the snippet is compared with a database for similarity. If similarity exists, the knowledge base content is extracted to modify the prompt words. Finally, the code is modified for more accurate accuracy. First, the Rust system code snippet is parsed to generate the corresponding Abstract Syntax Tree (AST). The AST represents each syntactic element (such as variables, functions, operators, etc.) in the source code and their hierarchical structure in a tree structure, clearly reflecting the grammatical and structural characteristics of the code. This embodiment builds a prompting project. However, to better extract useful information, this article prunes the output AST text to retain portions relevant to code safety and functional repairs. In the Rust language, all unsafe Rust operations are marked with the "unsafe" keyword. Therefore, the AST is pruned by locating the keyword, retaining code blocks related to "unsafe." Although this method cannot accurately find the exact problem statement, it greatly narrows the scope of code that needs to be analyzed and reduces the interference of irrelevant code on the large language model LLM, thereby improving the efficiency and accuracy of the repair process. Secondly, the erroneous abstract syntax tree AST is compared with the existing AST in the database for similarity. This embodiment constructs a database containing a large number of historical code snippets and their repair records. The system identifies code snippets with similar error types and structures by calculating the similarity between the abstract syntax tree AST of the current code and the existing abstract syntax tree AST in the database. If the system detects a match with high similarity, it will automatically extract the relevant repair knowledge base content. These knowledge base contents include common error types, repair strategies, and repair suggestions for specific problems. Finally, the erroneous abstract syntax tree AST structure and the correct abstract syntax tree AST structure extracted from the knowledge base are combined with the prompt words in the modification stage as training data for small sample learning to guide the large language model to generate more accurate repair strategies. Specifically, the abstract syntax tree AST and knowledge base enhanced prompt words extracted based on the Miri test in step S7 of this embodiment include:
[0090] S7.1. Generate an abstract syntax tree (AST) from the Rust system code snippet that is rolled back to the optimal code using the large language model. The AST is a tree-like data structure that represents the grammatical structure of the program, where each node represents a grammatical element, and edges represent parent-child relationships between nodes, connecting the hierarchical structure of the grammatical elements.
[0091] S7.2, use the pruning function to prune and simplify the abstract syntax tree AST;
[0092] S7.3, calculate the similarity between the pruned and simplified AST and the correct AST stored in the knowledge base (stored in SQL in this embodiment). If there is no correct AST with a similarity exceeding a preset threshold, the process ends and jumps to step S3. Otherwise, the correct AST with the best similarity is used as the target correct AST.
[0093] S7.4, extracting knowledge from the target correct abstract syntax tree AST, including: inputting the pruned and simplified abstract syntax tree AST and the target correct abstract syntax tree AST into a pre-trained large language model, obtaining repair suggestions for unsafe Rust types with undefined behavior and their repair paths in the pruned and simplified abstract syntax tree AST, wherein the repair suggestions include indicating which nodes need to be adjusted, which edges need to be replaced, and which structures need to add specific assertions to ensure code safety, and the large language model establishes a mapping relationship between the abstract syntax tree AST extracted by the Miri test, the correct abstract syntax tree AST stored in the knowledge base, and the repair suggestions for unsafe Rust types with undefined behavior and their repair paths in the abstract syntax tree AST extracted by the Miri test for pre-training;
[0094] S7.5 enhances the hint words with the knowledge extracted from the target correct abstract syntax tree AST, including adding repair suggestions for unsafe Rust types with undefined behavior and their repair paths to the hint words to enhance the hint words, thereby dynamically adjusting the hint word content as needed by combining code context information and the structural characteristics of the abstract syntax tree AST, ensuring that the generated hint words can fully express the logical and semantic requirements of the code repair process.
[0095] In this embodiment, the Rust system code snippet that is rolled back to the optimal code in step S7.1 uses the large language model to generate the abstract syntax tree AST. The required prompt words can be used as needed. For example, the prompt words used in this embodiment are:
[0096] You are an AST extractor, you will extract the abstract syntax tree AST from the request code I sent;
[0097] Your answer should only include the abstract syntax tree (AST), without any additional commentary or explanation.
[0098] 2. Ensure that the abstract syntax tree (AST) output maintains the correct hierarchical structure and uses indentation or brackets appropriately;
[0099] ...Here is the code... {code}
[0100] Let’s think about it step by step;”
[0101] Among them, "..." indicates omitted content, {code} is the Rust system code snippet that rolls back to the best code, ” for emphasis.
[0102] In this embodiment, before step S7.4, the following steps are also included: collecting the abstract syntax tree AST extracted by the Miri test, the correct abstract syntax tree AST stored in the knowledge base, and the repair method for the unsafe Rust type with undefined behavior and its repair path in the abstract syntax tree AST extracted by the Miri test, and constructing a training data set; using the training data set to train the large language model to establish a mapping relationship between the abstract syntax tree AST extracted by the Miri test, the correct abstract syntax tree AST stored in the knowledge base, and the repair suggestions for the unsafe Rust type with undefined behavior and its repair path in the abstract syntax tree AST extracted by the Miri test.
[0103] Rust is a systems programming language that focuses on safety, speed, and concurrency. Testing is a crucial part of Rust development, as one of the Rust language's design philosophies is to ensure code safety and correctness through compile-time checking. Testing semantics for Rust system code snippets refers to the rules and conventions followed when writing test code to ensure the acceptability and effectiveness of the tests. Step S8, which tests the semantic acceptability of Rust system code snippets, is an existing method. The desired testing method can be adopted as needed, so it will not be detailed here.
[0104] To validate the Rust language system security enhancement method based on a large language model in this example, we selected the Miri official test set for code with undefined behavior as an example for repair. This test set includes different types of errors, such as memory errors, concurrency errors, dangling pointers, borrow errors, and data race errors. This example compares the repair of different types of undefined behavior using GPT4 alone and GPT4 combined with this example. This example compares the passing of Miri tests (pass) and maintaining semantic requirements (exec).
[0105] Figure 5 The percentage of different types of errors in the embodiment that pass the Miri test after repair. Figure 5It can be seen that compared with using only GPT4 for repair, the repair accuracy of the Rust language system security enhancement method based on the large language model of this embodiment has increased by an average of about 30%-40%. For example, for the memory allocation series alloc error, GPT4 can only repair 53% of undefined behavior, while the repair rate of this project reached 93% pass rate, an increase of 40%. It can be seen that compared with using only GPT4, the Rust language system security enhancement method based on the large language model of this embodiment can significantly reduce the possibility of undefined behavior in the code, and has good adaptability to multiple types of errors.
[0106] In order to better ensure the equivalence of the semantic functions of the modified code, this embodiment further performs a judgment on its semantic equivalence. The results are as follows: Figure 6 and Figure 7 shown. Figure 6 is the percentage of semantic acceptability after different types of errors are repaired in this embodiment, Figure 7 is the difference ratio of the Miri test and semantic acceptability after the different categories of errors are repaired in this embodiment. Figure 6 and Figure 7 It can be seen that the pass rate is lower than that of the Miri test. This is because the large model generates randomness and hallucinations, and lacks precise description of the context for the code snippets, which results in a semantic acceptability pass rate of about 50%. However, the semantic acceptability success rate of the code repaired with GPT4 has been significantly improved, about 25%-35%. In general, after using the method of this embodiment to repair the undefined behavior code, the memory security and runtime stability of the code are significantly improved, and potential undefined behaviors are effectively eliminated. At the same time, the automated repair mechanism of this embodiment can also reduce the manual intervention of developers, improve repair efficiency and reduce the risk of human error, providing reliable protection for building a highly secure Rust system.
[0107] In summary, the Rust language system security enhancement method based on the large language model in this embodiment includes Miri detection for input Rust system code snippets. The code snippets that pass the Miri detection directly enter the semantic acceptability test. Otherwise, first add assertions or modify the code. Miri detects whether the code repaired by LLM is wrong again. If there is an error, whether the maximum number of iterations has been reached. If the maximum number of iterations has not been reached, the process of LLM repairing the code is repeated. Otherwise, it is directly rolled back to the code in the optimal state, and the prompt words of the large model are updated based on the knowledge base of the abstract syntax tree AST. If the optimal code cannot pass the Miri test, it exits directly, indicating that the repair has failed. Otherwise, it enters the semantic acceptability test stage and exits directly after completion. This embodiment utilizes the self-reasoning and semantic understanding capabilities of the large language model, combined with static analysis tools, to input code with undefined behavior and its causes into the large model. The repair process is divided into two stages: assertion and modification. First, try to add assertions in appropriate places to prevent undefined behavior, ensuring that any potential undefined behavior can be captured at runtime before propagation. For complex scenarios involving deeper logical errors or dependencies on external state, a modification phase is introduced. During this phase, the error semantics in the Rust code are adjusted to ensure that the code's functionality remains unchanged while effectively reducing the risk of undefined behavior. To avoid the large model illusion problem that can occur during the repair process and lead to reduced repair accuracy, this embodiment designs an adaptive rollback mechanism to prevent error accumulation and more efficiently roll back to the optimal state. Furthermore, this embodiment provides knowledge base support based on an abstract syntax tree, allowing for more generalized analysis based on code structure, thus enabling generalized solutions and improving repair accuracy. Finally, code correctness and semantic acceptability are tested. Based on these methods, this embodiment efficiently and intelligently reduces undefined behavior in unsafe Rust, ensuring the security of the Rust system. This method can accurately identify potential risks in unsafe Rust code and provide intelligent automatic repairs, which helps reduce unsafe operations and undefined behavior in Rust operating systems, improve the security of Rust systems, and enhance development efficiency, providing technical support for building highly reliable and secure operating systems.
[0108] In addition, this embodiment also provides a Rust language system security enhancement device based on a large language model, including a microprocessor and a memory connected to each other, and the microprocessor is programmed or configured to execute the Rust language system security enhancement method based on a large language model.
[0109] In addition, this embodiment also provides a computer-readable storage medium, which stores a computer program or instructions, and the computer program or instructions are programmed or configured to execute the Rust language system security enhancement method based on the large language model through a processor.
[0110] In addition, this embodiment also provides a computer program product, including a computer program or instructions, which are programmed or configured to execute the Rust language system security enhancement method based on a large language model through a processor.
[0111] Those skilled in the art should understand that the technical solutions provided by the embodiments of the present application may be in the form of methods, systems, or computer program products. Therefore, the present application may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application may take the form of a computer program product implemented on one or more computer-readable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code. The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of processes and / or boxes in the flowchart and / or block diagram, may be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the steps in the process. Figure 1 a process or multiple processes and / or boxes Figure 1 These computer program instructions can also be stored in a computer-readable memory that can guide a computer or other programmable data processing device to work in a specific way, so that the instructions stored in the computer-readable memory produce a product including the instruction device, which implements the function specified in the process. Figure 1 a process or multiple processes and / or boxes Figure 1 These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process in the process. Figure 1 a process or multiple processes and / or boxes Figure 1 The steps for the function specified in one or more boxes.
[0112] The above description is merely a preferred embodiment of the present invention. The scope of protection of the present invention is not limited to the above embodiment. All technical solutions based on the concept of the present invention are within the scope of protection of the present invention. It should be noted that for those skilled in the art, various improvements and modifications that do not depart from the principles of the present invention should also be considered within the scope of protection of the present invention.
Claims
1. A Rust language system security enhancement method based on a large language model, characterized in that: The steps include: S1, obtain the input Rust system code snippet; S2, perform Miri test on the Rust system code snippet, if the test passes, jump to step S8; Otherwise, jump to step S3; S3, combines a large language model and prompt words to add assertions and modifications to Rust system code snippets; S4, perform Miri testing on the Rust system code snippet with added assertions and modifications. If the test passes, jump to step S8; Otherwise, jump to step S5; S5, determine whether the iteration maximum value has been reached. If not, jump to step S3 to continue iterating; otherwise, jump to step S6; S6, select the Rust system code snippet with the least number of errors among the original Rust system code snippet, the Rust system code snippet with added assertions, and the modified Rust system code snippet as the best code, and roll back the Rust system code snippet to the best code; S7, based on the abstract syntax tree AST extracted by Miri test and the knowledge base enhanced prompt words, jump to step S3; S8, testing semantic acceptability of Rust system code snippets; The abstract syntax tree AST and knowledge base enhanced prompt words extracted based on the Miri test in step S7 include: S7.
1. Generate an abstract syntax tree (AST) from the Rust system code snippet that is rolled back to the optimal code using the large language model. The AST is a tree-like data structure that represents the grammatical structure of the program, where each node represents a grammatical element, and edges represent parent-child relationships between nodes, connecting the hierarchical structure of the grammatical elements. S7.2, use the pruning function to prune and simplify the abstract syntax tree AST; S7.3, calculate the similarity between the pruned and simplified AST and the correct AST stored in the knowledge base. If there is no correct AST with a similarity exceeding a preset threshold, then directly end and jump to step S3; otherwise, the correct AST with the best similarity is used as the target correct AST. S7.4, extracting knowledge from the target correct abstract syntax tree AST, including: inputting the pruned and simplified abstract syntax tree AST and the target correct abstract syntax tree AST into a pre-trained large language model, obtaining repair suggestions for unsafe Rust types with undefined behavior and their repair paths in the pruned and simplified abstract syntax tree AST, wherein the repair suggestions include indicating which nodes need to be adjusted, which edges need to be replaced, and which structures need to add specific assertions to ensure code safety, and the large language model establishes a mapping relationship between the abstract syntax tree AST extracted by the Miri test, the correct abstract syntax tree AST stored in the knowledge base, and the repair suggestions for unsafe Rust types with undefined behavior and their repair paths in the abstract syntax tree AST extracted by the Miri test for pre-training; S7.5, enhances the hint words with the knowledge extracted from the target correct abstract syntax tree AST, including adding repair suggestions for unsafe Rust types with undefined behavior and their repair paths to the hint words to achieve the enhancement of the hint words.
2. The Rust language system security enhancement method based on a large language model according to claim 1 is characterized in that: Step S2 includes: S2.1, perform Miri testing on the Rust system code snippet to determine whether the Rust system code snippet contains an undefined behavior error message. If no undefined behavior error message exists, the test is considered passed and the process jumps to step S8; otherwise, the process jumps to step S2.2; S2.2, determine the unsafe Rust type that causes undefined behavior. If the unsafe Rust type that causes undefined behavior is the target unsafe Rust type, then determine that the test fails and jump to step S3. The target unsafe Rust type includes five types: dereferencing raw pointers, calling unsafe functions or methods, accessing or modifying mutable static variables, implementing unsafe features, and accessing union fields; otherwise, determine that the test passes and jump to step S8.
3. The Rust language system security enhancement method based on a large language model according to claim 1 is characterized in that: Step S3 includes: S3.1, combining a large language model and prompt words, automatically repairs Rust code in Rust system code snippets that cause undefined behavior of target unsafe Rust types by adding assertions; S3.2, in combination with the large language model and hint words, modify the Rust code in the Rust system code fragment that causes undefined behavior of the target unsafe Rust type to provide an alternative safe implementation or modify it to reduce the undefined behavior in the Rust code without compromising the semantics of the code.
4. The Rust language system security enhancement method based on a large language model according to claim 3 is characterized in that: The prompt words used in step S3.1 include: "...The following is the error message...{log}". The ideas for adding assertions are as follows:
1. Add assertions before undefined behavior becomes possible to prevent it from happening; 2. Doing so is important and necessary; Only add assertions, do not adjust or delete other code modules; "..." indicates omitted content, {log} is the log of Miri test, " ” for emphasis.
5. The Rust language system security enhancement method based on a large language model according to claim 3 is characterized in that: The prompt words used in step S3.2 include: "Undefined behavior occurs when running Miri test according to the code. The repair ideas are as follows:
1. Identify the undefined behavior; 2. The code that adds assertions cannot be modified because the logic itself has problems; 3. To maintain the functionality and semantics of the source code, avoid drastically changing the logical structure; 4. Design safe alternatives; refactor the code based on the safe alternatives to ensure that the modified code not only avoids undefined behavior but also maintains the original functional logic and performance standards; ...Here is the code...{code}", where "..." indicates omitted content, and {code} is the modified Rust system code snippet, ” for emphasis.
6. The Rust language system security enhancement method based on a large language model according to claim 1 is characterized in that: Before step S7.4, it also includes: collecting the abstract syntax tree AST extracted by the Miri test, the correct abstract syntax tree AST stored in the knowledge base, and the repair method for the unsafe Rust type with undefined behavior and its repair path in the abstract syntax tree AST extracted by the Miri test and constructing a training data set; using the training data set to train the large language model to establish a mapping relationship between the abstract syntax tree AST extracted by the Miri test, the correct abstract syntax tree AST stored in the knowledge base, and the repair suggestions for the unsafe Rust type with undefined behavior and its repair path in the abstract syntax tree AST extracted by the Miri test.
7. A Rust language system security enhancement device based on a large language model, comprising a microprocessor and a memory connected to each other, characterized in that: The microprocessor is programmed or configured to execute the Rust language system security enhancement method based on a large language model as described in any one of claims 1 to 6.
8. A computer-readable storage medium having a computer program or instruction stored therein, characterized in that: The computer program or instruction is programmed or configured to execute the Rust language system security enhancement method based on a large language model as described in any one of claims 1 to 6 through a processor.
9. A computer program product comprising a computer program or instructions, characterized in that The computer program or instruction is programmed or configured to execute the Rust language system security enhancement method based on a large language model as described in any one of claims 1 to 6 through a processor.
Citation Information
Patent Citations
Security enhancement model development method and system based on Rust language
CN116484439A
Rust language document test automatic generation method and device based on large code model
CN117951038A