A rust language code optimization method and device and computing device
By optimizing unsafe code in Rust projects using the Rust compiler and a large language model, the problem of a high proportion of unsafe code after c2rust transpilation is solved, improving system security and reducing maintenance costs, and achieving efficient optimization of unsafe code.
Patent Information
- Application Number
- CN202411920142.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-24
- Publication Date
- 2025-11-25
- Estimated Expiration
- 2044-12-24
AI Technical Summary
Existing c2rust tools automatically translate C language projects into Rust, resulting in an excessively high proportion of unsafe code in the Rust code. This compromises memory safety and makes it difficult to address unsafe issues beyond the rules themselves, as well as the high cost of updating and maintaining the rule base.
By acquiring the Rust project to be optimized, the Rust compiler is used to optimize unsafe tags. Combined with the large language model and preset prompt word templates, unsafe code snippets are iteratively optimized, unnecessary unsafe tags are removed, and libc library function calls and mutable static constants are optimized through regular expressions and equivalent substitution rules to reduce the proportion of unsafe code.
It significantly reduces the proportion of unsafe code in Rust projects, improves system security, and reduces later maintenance costs and improves the generalization ability of methods through automatic updates and optimizations of the large language model.
Smart Images

Figure CN119829064B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular to a Rust language code optimization method and device and computing equipment. BACKGROUND
[0002] Rust is a new static programming language characterized by memory safety, unlike traditional C programming language, Rust language can detect memory errors at compile time to ensure memory safety. Due to the excellent performance of Rust language in memory safety and performance, many developers migrate project code from C language to Rust language, but due to the existing C language code library is very large, manual refactoring needs to consume a lot of manpower, and the cost is high. Therefore, someone proposes a language conversion tool system c2rust, which can automatically convert C language to Rust language. However, c2rust has a series of limitations, especially it only does code translation of syntax and semantics of a single file, does not apply some safety features (such as lifetime, ownership, etc.) of Rust language to the code generation process, and breaks the dependency relationship between files in the project, thereby causing all functions in the translated code to be labeled as unsafe, too much unsafe code, and memory safety cannot be guaranteed, and the advantage of Rust memory safety cannot be played.
[0003] However, in the actual system design process, unsafe code is inevitable in the implementation process, and eliminating or reducing the use of unsafe code is the key to improving system security. Currently, there are some optimization methods that can identify unsafe code based on preset fixed rules, but this method is difficult to deal with unsafe problems outside the rules, and there are problems such as high maintenance cost of rule library update in the later period, and old rules cannot adapt to new Rust version updates.
[0004] Therefore, after automatically converting the C language project to Rust language, how to improve the system security is a key problem to be solved at present. SUMMARY
[0005] The present application provides a Rust language code optimization method and device and computing equipment, which can reduce the proportion of unsafe code in the Rust code translated by c2rust and improve system security. The technical solution is as follows:
[0006] In a first aspect, the embodiments of the present application provide a Rust language code optimization method. The method can obtain a Rust project to be optimized, the Rust project to be optimized including a plurality of code segments with an unsafe label. The method inputs the Rust project to be optimized into a Rust compiler, optimizes the unsafe label according to output information of the Rust compiler, traverses each code segment corresponding to the optimized unsafe label, inputs the code segment corresponding to the unsafe label and a preset prompt template into a large language model, the prompt template being used to define requirements followed by the large language model when processing the Rust project, modifies the code segment corresponding to the optimized unsafe label in the Rust project to be optimized according to output information of the large language model, detects the modified Rust project to be optimized using the Rust compiler, and deletes the corresponding unsafe label in a case where the modified Rust project to be optimized passes the compilation of the Rust compiler.
[0007] The method provided by the embodiments of the present application first optimizes the unsafe label according to the output information of the Rust compiler, and preliminarily reduces the proportion of unsafe code. Then, the code segment corresponding to the unsafe label still existing is input into the large language model, the large language model is informed by the prompt template that the input Rust code segment contains unsafe behaviors or operations, and the large language model is defined by the prompt template on how to process the Rust code segment, so that the large language model processes the unsafe code segment according to the requirements, and then the code segment corresponding to the optimized unsafe label can be modified according to the output information of the large language model, so that the modified code segment is safe, thereby reducing the proportion of unsafe code. Based on the method provided by the embodiments of the present application, the Rust project to be optimized is processed multiple times, which can greatly reduce the proportion of unsafe code in the Rust project, thereby improving the system security. Moreover, the large language model can be automatically updated and optimized, and the large language model is applied to the optimization of the unsafe code segment of the Rust program, which can effectively improve the generalization of the optimization method and reduce the cost of later maintenance.
[0008] In combination with the first aspect, in some implementations of the first aspect, the method further includes: if the modified Rust project to be optimized does not pass the compilation of the Rust compiler, modifying a temperature parameter of the large language model; repeatedly inputting the code segment corresponding to the optimized unsafe label and the preset prompt template into the large language model, modifying the code segment corresponding to the unsafe label in the Rust project to be optimized according to the output information of the large language model, detecting the modified Rust project to be optimized using the Rust compiler, until the modified Rust project to be optimized passes the compilation of the Rust compiler, or the number of repetitions exceeds a preset threshold. If the number of repetitions exceeds the preset threshold and still does not pass the compilation of the Rust compiler, the unsafe label is retained.
[0009] In some implementations of the first aspect, the unsafe label is optimized by inputting the Rust project to be optimized into a Rust compiler, removing all unsafe labels corresponding to function declarations and function internal code blocks in the Rust project to be optimized according to output information of the Rust compiler, inputting the Rust project after removing all unsafe labels into the Rust compiler, and adding an unsafe label to at least one code segment reported as an error by the Rust compiler according to error information output by the Rust compiler. In this way, all unsafe labels are removed first, then unsafe code is identified, and an unsafe label is added to the unsafe code, thereby narrowing the range of unnecessary unsafe code and preliminarily reducing the proportion of unsafe code.
[0010] In some implementations of the first aspect, adding the unsafe label to the at least one code segment reported as an error by the Rust compiler includes merging code segments with overlapping parts in the code segment reported as an error by the Rust compiler, and adding the unsafe label to the merged code segment. In this way, the code segment corresponding to the unsafe label can be avoided from being repeated.
[0011] In some implementations of the first aspect, the method further includes, in the Rust project after adding the unsafe label, using a regular expression to match a predefined error and correcting the matched error. After the modification according to the compiler prompt information, some errors may occur in the Rust code, resulting in the Rust code failing to pass the Rust compilation. Therefore, a correction library is provided in the embodiment of the present application, and the correction library includes a plurality of patterns / errors that may cause the Rust code to fail to pass the Rust compilation. After the modification according to the compiler prompt information, the regular expression is used to match, identify, and correct the errors in the correction library.
[0012] In some implementations of the first aspect, the method further includes, in the code segment corresponding to the optimized unsafe label, searching for a function call statement, and modifying the function call statement to Rust code according to a preset equivalent replacement rule. In this way, the unsafe libc library function call can be reduced, the unsafe label can be reduced, and the system security can be improved.
[0013] In some implementations of the first aspect, the method further includes, in the case where the code segment corresponding to the optimized unsafe label includes a definition, access, or modification operation of a mutable static constant, deleting the corresponding unsafe label. In this way, the unsafe label can be reduced, and the system security can be improved.
[0014] In a second aspect, the embodiments of the present application provide a Rust language code optimization device, comprising: an acquisition unit and a processing unit, wherein the acquisition unit is configured to acquire a Rust project to be optimized, the Rust project to be optimized comprising a plurality of code segments with an unsafe label; the processing unit is configured to input the Rust project to be optimized into a Rust compiler, optimize the unsafe label according to output information of the Rust compiler, traverse each code segment corresponding to the optimized unsafe label, input the code segment corresponding to the unsafe label and a preset prompt word template into a large language model, the prompt word template being used to define requirements followed by the large language model when processing the Rust project, modify the code segment corresponding to the optimized unsafe label in the Rust project to be optimized according to output information of the large language model, and detect the compilation of the modified Rust project to be optimized by using the Rust compiler, and delete the corresponding unsafe label in the case where the modified Rust project to be optimized passes the compilation of the Rust compiler.
[0015] In combination with the second aspect, in some implementations of the second aspect, the processing unit is specifically configured to: remove all function declarations and unsafe labels corresponding to function internal code blocks in the Rust project to be optimized; input the Rust project to be optimized after removing all unsafe labels into the Rust compiler; and add an unsafe label for at least one code segment with an error reported by the Rust compiler according to error reporting information output by the Rust compiler.
[0016] In the process of adding the unsafe label for the at least one code segment with an error reported by the Rust compiler, the processing unit is further specifically configured to: merge code segments with overlapping parts in the code segments with an error reported by the Rust compiler; and add the unsafe label for the merged code segments.
[0017] The processing unit is further specifically configured to: use a regular expression to match a predefined error in the Rust project to be optimized after adding the unsafe label, and correct the matched error.
[0018] The processing unit is further configured to: find a function call statement in the code segment corresponding to the optimized unsafe label; and modify the function call statement into Rust code according to a preset equivalent replacement rule.
[0019] The processing unit is further configured to: delete the corresponding unsafe label in the case where the code segment corresponding to the optimized unsafe label comprises a definition, access or modification operation of a mutable static constant.
[0020] In a third aspect, an embodiment of the present application provides a computing device, comprising: a processor and a memory; the processor is coupled to the memory; the memory is configured to store computer instructions, and the computer instructions are loaded and executed by the processor to enable the computing device to implement the method according to any one of the first aspect.
[0021] In a fourth aspect, an embodiment of the present application provides an apparatus, which is included in a computing device, and the apparatus has functions to realize the behaviors of the computing device in the above aspects and possible implementation manners of the above aspects. The functions can be realized by hardware, or by hardware executing corresponding software. The hardware or software includes one or more modules or units corresponding to the above functions.
[0022] In a fifth aspect, an embodiment of the present application provides a computer readable storage medium, which stores instructions, and when the instructions are run on a computer, the computer is enabled to execute the method according to any one of the first aspect.
[0023] In a sixth aspect, an embodiment of the present application provides a computer program product including instructions, and when the instructions are run on a computer, the computer is enabled to execute the method according to any one of the first aspect.
[0024] The technical effects obtained by the above second aspect, third aspect, fourth aspect, fifth aspect and sixth aspect are similar to the technical effects obtained by the corresponding technical means in the first aspect, and will not be repeated here. BRIEF DESCRIPTION OF DRAWINGS
[0025] Figure 1 A flowchart of a method for translating C language code into Rust language code is shown;
[0026] Figure 2 A flowchart of a method for optimizing Rust language code is shown;
[0027] Figure 3 A structural diagram of an apparatus is shown;
[0028] Figure 4 A structural diagram of an apparatus is shown. DETAILED DESCRIPTION
[0029] For the purposes of the present application, the technical solutions and advantages, the embodiments of the present application will be further described in detail below with reference to the drawings. Hereinafter, the terms "first" and "second" are only used for the purpose of description, and cannot be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined with "first" and "second" can be explicitly or implicitly included one or more features. In the description of the present embodiment, unless otherwise specified, the meaning of "a plurality of" is two or more.
[0030] In order to facilitate the understanding of the technical solutions, the terms involved in the embodiments of the present application are first explained.
[0031] 1, Rust language: Rust is a new static programming language characterized by memory safety. Unlike traditional C / C++ programming languages, Rust introduces the concept of variable lifetime and ownership. With the help of ownership and lifetime characteristics, Rust can detect memory errors at compile time, greatly avoiding problems such as memory leaks, dangling pointers, buffer overflows, and data races in concurrent programming, and ensuring memory safety.
[0032] Rust language retains the unsafe mechanism to bypass the compile-time static check to achieve complex operations that cannot be achieved in the safe check mode. In Rust language, the unsafe and safe keywords are used to distinguish the safety and potential insecurity of the code. Among them, the code block marked with the safe keyword can be automatically verified by the compiler for its safety. The code block or function marked with the unsafe keyword allows developers to use some unsafe operations, such as directly operating memory, calling external C libraries, or implementing underlying system interfaces, which may bypass Rust's safety checks, so Rust compiler cannot guarantee its safety. That is, the functions and code blocks marked by unsafe are not checked by Rust compiler, and the memory safety cannot be ensured. Intuitively, the less unsafe code in a Rust project, the better the memory safety.
[0033] 2, Code translation tool c2rust: c2rust is a tool for converting C language to Rust language, which aims to help developers migrate existing C code base to Rust while maintaining code functionality and semantic consistency.
[0034] The embodiment of the application provides a Rust language code optimization method based on a large language model. The method can obtain a Rust code segment to be optimized, removes unnecessary unsafe functions, code blocks and code segments, and thus optimizes unsafe labels. Further, the method uses a preset prompt word template to input the remaining unsafe code segments into a large language model for iterative optimization. Finally, the method replaces unnecessary unsafe libc library functions, and further reduces the proportion of unsafe codes. The method provided by the embodiment of the application can reduce the proportion of unsafe codes in the Rust code translated by c2rust, improve system security, and use a large language model to automatically and iteratively optimize Rust unsafe codes, effectively improve the generalization of the optimization method, and reduce the cost of later maintenance.
[0035] The method provided by the embodiment of the application can be applied to any scene in which C language code needs to be translated into Rust language code. The method is implemented by a computer chip or an entity, or by a product with certain functions. A typical implementation device is a computer. For example, the computer can be a personal computer, a laptop computer, a personal digital assistant, a tablet computer or a combination of any of these devices.
[0036] For ease of understanding, the process of translating C language code into Rust language code is briefly described below.
[0037] Referring to Figure 1 For the source code of the C project to be translated, a Goblint static analysis tool is used to identify and remove useless codes in the C project, and code merging is performed. For example, by using an intermediate representation (IR) of a low level virtual machine (LLVM) compiler, Goblint can analyze the control flow and data flow in C code, detect possible redundancies or unreachable codes. Then, Goblint uses an abstract syntax tree (AST) model to perform fine-grained static analysis on the program, identify unused variables, invalid function calls and dead code segments. By combining these analysis results, the tool can accurately remove useless codes, reduce redundant parts, and merge similar code blocks in appropriate cases, and output a merged single-file C project.
[0038] Then, the C project source code is translated into a Rust project using c2rust to obtain a single-file Rust project. Specifically, the source code of the C project is parsed into an AST, and an intermediate representation is generated through the LLVM toolchain. At this time, the tool collects the structure, type information, and control flow information in the code; after obtaining the AST, c2rust maps each syntax structure of C (such as functions, structures, pointer operations) to the corresponding construction in Rust through a rule set and a translation library. This process involves structural adjustment of the code, such as converting C structs to Rust structs, and considering Rust's lifetime and ownership characteristics.
[0039] After obtaining the single-file Rust project, the bare pointer rewriting is performed to remove unnecessary intermediate variables, such as using the Bristol tool to solve SAT problems without relying on the compiler, or using the UCLA tool to rely on the compiler and rewrite based on rules. Then, unnecessary unsafe functions or code blocks are eliminated, a Large Language Model (LLM) is used to further optimize unsafe code segments, and finally unnecessary unsafe calls and mutable static constants are eliminated to obtain an optimized Rust project.
[0040] The method provided by the embodiments of the present application will be described in detail below. Figure 2 A flowchart of a Rust language code optimization method based on a large language model is shown, as shown in Figure 2 The method can include the following S201-S203. The method can be executed by a processor.
[0041] S201, obtaining a Rust project to be optimized.
[0042] S202, inputting the Rust project to be optimized into a Rust compiler, and optimizing an unsafe label according to output information of the Rust compiler.
[0043] After obtaining the Rust project to be optimized, the Rust project to be optimized is input into the Rust compiler, and the compilation passability of the Rust project to be optimized is checked. In one possible implementation manner, if the compilation fails, the Rust project to be optimized is repaired by using artificial or other methods, so that the Rust project to be optimized can pass the compilation. It should be noted that the implementation of the subsequent steps depends on the Rust compiler, and therefore the Rust project to be optimized must be able to pass the compilation.
[0044] For the Rust project to be optimized that can be compiled, unsafe labels in the Rust project are optimized. In one possible implementation, all unsafe labels in the Rust code are removed first, and then the optimized unsafe labels are marked on the Rust code.
[0045] Exemplarily, for the Rust project to be optimized that can be compiled, the Rust code to be optimized is read into the memory, the syn package is used to analyze the Rust code to be optimized, the Rust source code (usually source code fragments of functions, structures, macros, etc.) is parsed into an abstract syntax tree AST, all functions and internal code blocks in the syntax tree are traversed, all unsafe labels existing on the function declarations and the internal code blocks of the functions are removed, the syntax tree is exported as Rust code, and the source file is written back. In this way, all unsafe labels in the Rust project to be optimized are removed.
[0046] syn is a parsing tool package for the Rust programming language, which provides a powerful abstraction to allow developers to conveniently parse, process, modify and generate abstract syntax trees in Rust code, and the abstract syntax tree is a structured representation of the Rust code.
[0047] The Rust project to be optimized after removing all unsafe labels is input into the rustc compiler, and the rustc compiler analyzes and outputs one or more error messages.
[0048] In one possible implementation, for the error with the number E1033, the rustc compiler outputs the error message: “Unsafe code was used outside of an unsafe block”, that is, “unsafe code outside of an unsafe code block”. For the error with the number E1033, the starting byte position and the ending byte position of all code blocks of this type of error are recorded to obtain an error set P = {z1, z2, …, z n}, z i = <t i , d i >, t i < d i , where t i represents the starting byte position of the code where the error z i is located in the source file, and d i represents the ending byte position of the code where the error z i is located in the source file.
[0049] Then, the start byte positions of the repeatedly occurring codes in all unsafe codes are merged by interval merging, and the repeated parts are removed. Exemplarily, an interval array M = Φ is initialized. Each element z in the set P is added to the interval array M, and each interval is considered in order: if the left endpoint of the current interval is after the right endpoint of the last interval in the array M, the interval is added to the end of the array M; otherwise, the right endpoint of the last interval in the array M is updated to be the larger one of the two. i The t i and d i are respectively the left and right boundaries of the interval. All elements in the error set P are sorted in ascending order of t i using the quicksort method, and the sorted result is denoted as P'. Then, each element in P' is traversed, and each interval of the element is added to the interval array M. Each interval is considered in order: if the left endpoint of the current interval is after the right endpoint of the last interval in the array M, the interval is added to the end of the array M; otherwise, the right endpoint of the last interval in the array M is updated to be the larger one of the two. For example, the start byte position of the element z1 is the 2nd byte in the 1st row, and the end byte position is the 3rd byte in the 3rd row. The start byte position of the element z2 is the 2nd byte in the 2nd row, and the end byte position is the 6th byte in the 5th row. The left endpoint of the current interval is before the right endpoint of the last interval in the array M, and the two intervals can be merged. The start byte position of the merged interval is the 2nd byte in the 1st row, and the end byte position is the 6th byte in the 5th row.
[0050] The Rust code to be optimized is read again, and the code in the interval of one or more unsafe codes recorded in the array M is marked with an unsafe label. Exemplarily, the operation is to add “unsafe {” in front of the interval code and “}” behind the interval code, and write back to the original file. Exemplarily, after the unsafe label is added to the interval “xxx”, the interval becomes unsafe {xxx}.
[0051] Optionally, after the modification according to the compiler prompt information, some errors may occur in the Rust code, causing the Rust code to fail to pass the Rust compilation. Therefore, a correction library is set in the embodiment of the present application, and the correction library includes a plurality of patterns / errors that may cause the compilation to fail to pass. After the modification according to the compiler prompt information, the errors in the correction library are matched, identified, and corrected using a regular expression.
[0052] For example, the correction library includes a pattern corresponding to the "match" keyword. After modification according to the compiler prompt information, the "match" keyword of the Rust language may be placed within the unsafe label range, and actually the "match" keyword is safe, so the regular expression can be used to match and identify the error, and the error can be correctly repaired to the previous nearest unsafe label outside. The code before repair is Unsafe{match…}, and the code after repair is match Unsafe{}.
[0053] For example, the correction library includes a pattern corresponding to the assignment statement. After modification according to the compiler prompt information, an unsafe code block may appear at both ends of an assignment statement, so the regular expression can be used to match and identify the error, and the error can be correctly repaired to a complete assignment statement within an unsafe code block. The code before repair is Unsafe{..}=unsafe{..}, and the code after repair is unsafe{..=..}.
[0054] Optionally, the Rust code obtained after correction can be further input into the compiler to check the compilation pass. After the compilation passes, the Lint information output by the compiler is analyzed, the "useless unsafe label" prompt is parsed and matched, and the unnecessary unsafe label is removed according to the starting byte position of the code block identified by the prompt. The specific operation is to remove "unsafe{" at the front and "}" at the tail of the marked interval code, and write back to the original file. That is, the code before analysis is unsafe{..=..}, and the analysis finds that the unsafe label is unnecessary and can be removed. The code can be changed to "..=..".
[0055] In the related art, all functions after translation are marked as unsafe, and actually most functions and code blocks do not need to be marked as unsafe, which expands the unsafe range and creates an obstacle for further optimization of the code using the safety features of the Rust language. In the embodiment of the present application, all unsafe labels are removed first, then the unsafe code is identified, the unsafe label is added to the unsafe code, and then the correction is performed. Through a series of operations, the unnecessary unsafe code range is narrowed, the unsafe code proportion is preliminarily reduced, the problem of too high unsafe proportion in Rust code caused by c2rust translation is solved, and the safety of the Rust code is improved.
[0056] S203, using a large language model to optimize the code segment with the optimized unsafe label.
[0057] In one possible implementation, the Rust code obtained according to step S202 can be read in, parsed and a syntax tree A is generated using the syn toolkit, all nodes of A are traversed, and the starting line number and ending line number of all unsafe marked functions and code blocks are recorded to a set U, U = {l1, l2, …, l n} where l i = <s i ,e i ,f i >, l i represents an unsafe marked function or code block, s i and e i represent the starting line number and ending line number of l i , respectively, and f i represents the path of the source file where l i is located.
[0058] A function or code block to be optimized l i is selected randomly or in order from U, and the code with line numbers from max{0, s i -p} to min{len, s i +p} is read from file f i , where p is a context range parameter, and len is the number of code lines of file f i .
[0059] Input content is generated according to a preset prompt word (Prompt) template and unsafe code segment l i , and the input content is input into a large language model. The following is an exemplary Prompt template:
[0060] “###Preamble###
[0061] You are given the below Rust code snippet that contains unsafebehaviors or operations.
[0062] ---
[0063] ###Code_snippet###
[0064] Fill in the Rust code to be optimized (along with the context before and after) in this position
[0065] ---
[0066] ###Instructions for fixing the unsafe code###
[0067] ###Instructions and examples for formatting the changelogoutput######Answer:###”
[0068] In the embodiments of the present application, the large language model is informed by the Prompt template that the input Rust code snippet contains unsafe behavior or operation, and how the large language model needs to handle the Rust code snippet is defined, and then the large language model repairs the unsafe code according to the requirements. For example, in one of the instructions for repairing the unsafe code, the above-mentioned Prompt template requires the temperature parameter to be 0.5, and the processing requirements can be as follows: 1. Not every code snippet needs to be repaired or related to safety, but all the codes in the above code snippets need to be considered. 2. It is assumed that the code snippet may not be complete and may be missing the above or below lines. 3. Do not add comments or code that are not required to fix unsafe behavior. 4. Do not use unsafe or unstable features (through '#![feature(...)]'). 5. Ensure that the code compiles correctly after being repaired. 6. Return one or more change groups, each containing one or more fixes to the above-mentioned code snippets. Each group must be formatted according to the following instructions.
[0069] After the large language model repairs the unsafe code, the change log is output, for example, the instructions and examples of the change log can be as follows: 1. The small group must list one or more pairs of (original code, fixed code) code snippets. 2. Each original code snippet must list all consecutive original code lines that must be replaced (including a few lines before and after the fix), followed by a fixed code snippet that contains all consecutive fixed code lines that must replace the original code lines (including the same number of lines before and after the change). 3. In each pair, the original code and fixed code snippets must start from the same source code line number N. Each code line listed in the original code and fixed code snippets must be prefixed with [N] matching the line index N in the above-mentioned code snippet, and then prefixed with the same blank indentation as the above-mentioned original code snippet.
[0070] The device receives the change log output by the large language model; based on the change log output by the large language model, the corresponding unsafe code segment l is modified according to the line number i Then, the modified code is input into the Rust compiler to check the compilation passability.
[0071] If the code tuple modified based on the large language model passes the compilation, the successfully repaired tuple is removed from the set U. If the code tuple modified based on the large language model does not pass the compilation, the LLM temperature parameter is modified (randomly selected between 0 and 1), and the large language model is used again to repair the unsafe code segment l.i Optimization is performed until the modified code tuple based on the large language model passes the compilation, or the number of repeated attempts exceeds the preset threshold upper limit m. If the number of repeated attempts exceeds the preset threshold and the compilation is still not repaired, the failed repair tuple is skipped, removed from the set U, and added to the unsuccessfully repaired set E.
[0072] Each tuple in the set U is traversed until all tuples in the set U are repaired by the large language model. The unsuccessfully repaired set E is fed back to the user to prompt the user to perform manual repair.
[0073] The above brings the code block and its context that still have the unsafe label after step S202 into the prompt template, calls the large language model for optimization, and further eliminates the unsafe label, which can greatly reduce the proportion of unsafe code and unsafe functions. Since the rule-based method cannot handle cases not in the rule library, and with the upgrade of the Rust language version, there are maintenance problems such as rule updating and deletion, the embodiments of the present application optimize the unsafe code based on the large language model, the large language model can be automatically updated and optimized, and the problem of poor generalization ability and high maintenance cost of unsafe code optimization technology can be solved.
[0074] In some implementations, the large language model can be trained based on public data collected from the Internet.
[0075] In other implementations, the large language model can be fine-tuned for specific purposes to improve the success rate of repairing unsafe code. For example, unsafe code success repair cases are collected to form training samples, and the large language model is trained and fine-tuned based on the training samples using PromptTuning or Adapter methods, thereby enhancing the optimization capability of the large language model for Rust code to improve the success rate of repairing unsafe code.
[0076] In other implementations, the prompt for the code large model can be optimized and improved. For example, the unsafe code can be pattern type identified, then similar samples are matched from the collected training samples, and the matched similar samples are input into the large language model as part of the prompt to improve the success rate of repairing unsafe code.
[0077] The above eliminates the unsafe label in the function and code block. After optimizing the unsafe label using the large language model, the method provided by the embodiments of the present application can also find function call statements and statements involving mutable static constants, and eliminate the unsafe label corresponding to the function call and the mutable static constant.
[0078] Exemplarily, after the C language is translated into the Rust language, some operations are implemented using functions in the libc library, and the libc library function call must use the unsafe label, but there are some functions in the Rust language that can replace the libc library function. Therefore, in the embodiment of the present application, some libc library functions that do not need to use the unsafe label can be rewritten to eliminate unnecessary unsafe libc library function calls. Exemplarily, after optimization using a large language model, the syn toolkit is used for parsing and obtaining a syntax tree A, all nodes marked as unsafe in A are traversed, and it is judged whether there is a statement involving a libc library function call. If there is a statement involving a libc library function call, according to the preset equivalent replacement rule, the statement is repaired to a safe Rust base library call. Table 1 shows some examples of preset equivalent replacement rules, wherein the first column of Table 1 is the code feature after the C language is translated into the Rust language, and the libc library functions include but are not limited to the tanh function, the exp function, the fprintf function, etc. The second column of Table 1 is the code feature after the libc library function call statement is equivalently replaced.
[0079] Table 1
[0080]
[0081] After eliminating the function call, on the basis of the obtained Rust code, the syn toolkit is used for parsing and obtaining a syntax tree A, each statement in all nodes marked as unsafe in A is traversed, and it is judged whether the statement includes the definition, access or modification operation of a variable static constant. If it includes, the unsafe label in the statement is removed, then a regular expression is used to match, identify and correct the errors in the corrected library, and finally a Rust language program code that can be compiled is obtained.
[0082] The above rewrites some unnecessary unsafe libc functions, and uses the lazy_static mechanism to optimize the unsafe label corresponding to the variable static constant, further reducing the proportion of unsafe in the code.
[0083] In summary, the embodiment of the present application provides a Rust language code optimization method based on a large language model. The method first uses rustc compiler prompt information to preprocess the Rust code to be optimized, optimizes the unsafe label in the Rust code, narrows the unsafe code range, and solves the problem that the safe function expansion code segment is marked as unsafe due to the c2rust translation error label. Then, using the code capability of the large language model, the Rust language code segment marked as unsafe is repaired and optimized in a targeted manner by using an iterative optimization method, the problem that the rule-based optimization method cannot identify cases outside the rules is solved, the generalization ability of the method is improved, and there is no need to design, update and maintain the rules.
[0084] Figure 3 A structure diagram of a Rust language code optimization device based on a large language model is provided for the embodiment of the present application. The device 300 can include an acquisition unit 310 and a processing unit 320, and the processing unit 320 can implement corresponding processing functions.
[0085] The acquisition unit 310 can be used to acquire a Rust project to be optimized, and the Rust project to be optimized includes a plurality of code segments with an unsafe label.
[0086] The processing unit 320 can be used to input the Rust project to be optimized into a Rust compiler, optimize the unsafe label according to the output information of the Rust compiler, traverse each code segment corresponding to the optimized unsafe label, input the code segment corresponding to the unsafe label and a preset prompt word template into a large language model, the prompt word template is used to define the requirements followed by the large language model when processing the Rust project, modify the code segment corresponding to the optimized unsafe label in the Rust project to be optimized according to the output information of the large language model, use the Rust compiler to detect the compilation of the modified Rust project to be optimized, and delete the corresponding unsafe label in the case where the modified Rust project to be optimized passes the compilation of the Rust compiler.
[0087] Optionally, when the unsafe label is optimized according to the output information of the Rust compiler, the processing unit 320 is specifically configured to remove the unsafe label corresponding to all function declarations and function internal code blocks in the Rust project to be optimized, input the Rust project to be optimized after removing all unsafe labels into the Rust compiler, and add an unsafe label to at least one code segment with an error output by the Rust compiler according to the error information output by the Rust compiler.
[0088] Optionally, when adding the unsafe label to the at least one code segment for which the Rust compiler reports an error, the processing unit 320 is further configured to: merge code segments having overlapping parts in the code segments for which the Rust compiler reports an error; and add the unsafe label to the merged code segments.
[0089] Optionally, the processing unit 320 is further configured to: in the Rust project to be optimized after the unsafe label is added, match the predefined error using a regular expression, and correct the matched error.
[0090] Optionally, the processing unit 320 is further configured to: in the code segment corresponding to the optimized unsafe label, find a function call statement; and modify the function call statement to Rust code according to a preset equivalent replacement rule.
[0091] Optionally, the processing unit 320 is further configured to: in a case where the code segment corresponding to the optimized unsafe label includes a definition, access or modification operation of a mutable static constant, delete the corresponding unsafe label.
[0092] Optionally, the apparatus 300 further includes a storage unit 330, which can be configured to store instructions and / or data. The processing unit 320 can read the instructions and / or data in the storage unit 330, so that the apparatus 300 implements the foregoing method embodiments.
[0093] For the steps or processes performed by the units in the apparatus 300, reference can be made to the foregoing method embodiments, and thus no further details are provided herein.
[0094] It should be understood that the “units” in the apparatus 300 can be implemented by hardware, or by software, or by execution of corresponding software by hardware. For example, the “units” can refer to application specific integrated circuits (ASICs), electronic circuits, processors (for example, shared processors, dedicated processors or group processors, etc.) and memories for executing one or more software or firmware programs, integrated logic circuits and / or other suitable components supporting the described functions.
[0095] Figure 4 A structural schematic diagram of a computer device provided by an embodiment of the present application is shown in FIG. 4. As shown in FIG. 4, the device 400 includes one or more (including two) processors 401, a communication line 402, a communication interface 403 and a memory 404. Optionally, the device 400 further includes a storage 405. Figure 4
[0096] In some embodiments, the memory 405 stores the following elements: executable modules or data structures, or a subset thereof, or an expanded set thereof.
[0097] The method described in the above embodiments of the present application can be applied to the processor 401 or implemented by the processor 401. The processor 401 can be an integrated circuit chip having a processing capability. In the implementation process, each step of the above method can be completed by the integrated logic circuit of hardware in the processor 401 or the instruction in the form of software. The above processor 401 can be a general processor (for example, a microprocessor or a conventional processor), a digital signal processor, an application specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic device, a discrete gate or transistor logic device, or a discrete hardware component. The processor 401 can implement or execute the disclosed methods, steps and logic block diagrams in the embodiments of the present application.
[0098] The steps of the method disclosed in the embodiments of the present application can be directly embodied as a hardware coding processor for execution, or a combination of hardware and software modules in the coding processor for execution. Among them, the software module can be located in a mature storage medium in the art such as random access memory, read-only memory, programmable read-only memory or electrically erasable programmable read-only memory (EEPROM). The storage medium is located in the memory 405, and the processor 401 reads the information in the memory 405 and combines the hardware to complete the steps of the above method.
[0099] The processor 401, the memory 405 and the communication interface 403 can communicate through the communication line 402.
[0100] The memory 404 is used to store the temporary data for the operation of the processor, and the data exchanged with the external storage such as hard disk.
[0101] In the above embodiments, the instructions stored in the memory for the processor to execute can be implemented in the form of a computer program product. Among them, the computer program product can be written in the memory in advance, or downloaded and installed in the memory in the form of software.
[0102] The embodiments of the present application further provide a computer program product comprising one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the processes or functions according to the embodiments of the present application are wholly or partially generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network or other programmable apparatus. The computer instructions can be stored in a computer-readable storage medium or transferred from one computer-readable storage medium to another, for example, the computer instructions can be transferred from one website, computer, server or data center to another website, computer, server or data center through wired (for example, coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (for example, infrared, wireless, microwave, etc.) mode. The computer-readable storage medium can be any available medium that can be stored by the computer or a data storage device such as a server, data center, etc. integrated with one or more available media sets. For example, the available media can include magnetic media (for example, floppy disk, hard disk or magnetic tape), optical media (for example, digital versatile disc (DVD)), or semiconductor media (for example, solid state disk (SSD)) and the like.
[0103] The embodiments of the present application provide an electronic device, which comprises a processor and a memory. The memory is configured to store a computer program. The processor is configured to execute the computer program to perform the method described above.
[0104] The embodiments of the present application provide a chip. The chip comprises a processor configured to invoke a computer program in a memory to execute the technical solutions in the above embodiments. The implementation principles and technical effects are similar to those of the above related embodiments, which will not be described here.
[0105] In the embodiments of the present disclosure, it should be understood that the disclosed apparatus / equipment and method can be implemented in other manners. For example, the embodiments of the apparatus / equipment described above are merely schematic. For example, the division of the modules or units is merely logical function division, and there can be another division manner in actual implementation. For example, a plurality of units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the displayed or discussed mutual couplings or direct couplings or communication connections can be indirect couplings or communication connections through some interfaces, devices or units, and can be in electrical, mechanical or other forms.
[0106] The embodiments of the present disclosure further provide a computer readable storage medium, which stores a computer program. The computer program is executed by a processor to implement the steps in the above-mentioned various method embodiments. The computer readable storage medium stores a computer program or instructions. The computer program or instructions are executed by the processor to implement the above-mentioned method.
[0107] The method described in the above embodiments can be implemented in whole or in part by software, hardware, firmware or any combination thereof. If implemented in software, the functions can be stored as one or more instructions or codes on a computer readable medium or transmitted over a computer readable medium. The computer readable medium can include computer storage medium and communication medium, and can also include any medium that can transfer computer program from one place to another. The storage medium can be any target medium that can be accessed by a computer.
[0108] As a possible design, the computer readable medium can include a compact disc read-only memory (CD-ROM), a RAM, a ROM, an EEPROM, or other optical disk storage; the computer readable medium can include a magnetic disk storage or other magnetic disk storage device. Moreover, any connection line can also be properly referred to as a computer readable medium. For example, if software is transmitted from a website, a server, or other remote source using a coaxial cable, an optical fiber cable, a twisted pair, a DSL, or a wireless technology (such as infrared, radio, and microwave), the coaxial cable, the optical fiber cable, the twisted pair, the DSL, or the wireless technology (such as infrared, radio, and microwave) is included in the definition of the medium. As used herein, a disk and a disc include a compact disc (CD), a laser disc, an optical disc, a DVD, a floppy disk, and a Blu-ray disc, where a disk usually reproduces data magnetically, while a disc reproduces data optically with a laser. Combinations of the above should also be included in the scope of the computer readable medium.
[0109] Embodiments of the present application are described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to embodiments of the present application. It should be understood that each flow and / or block in the flowcharts and / or block diagrams, as well as combinations of flows and / or blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing apparatus to produce a machine, so that the instructions executed by the computer or other programmable data processing apparatus generate a device that implements the flow(s) and / or block(s) specified in the flowcharts and / or block diagrams. Figure 1 one or more flows and / or blocks Figure 1 an apparatus that implements the function specified in the flow(s) or block(s).
[0110] In the above description, for the purpose of explanation and not limitation, specific details are set forth, such as specific system structures, techniques, etc., in order to provide a thorough understanding of the embodiments of the present application. However, it should be apparent to those skilled in the art that the present application can be practiced in other embodiments that do not depart from the spirit of the present application. In other instances, well-known systems, devices, circuits, and methods have not been described in detail in order to avoid unnecessary detail and to not obscure the description of the present application.
[0111] It should be understood that, when used in the specification and the appended claims of the present application, the term "comprising" indicates the presence of the described features, integers, steps, operations, elements, and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and / or groups thereof.
[0112] It should also be understood that, in the description of the application, unless otherwise specified, " / " means or, such as A / B can mean A or B; "and / or" in this article is only a description of the relationship between the associated objects, refers to any combination of one or more of the associated items listed and all possible combinations, and includes these combinations, such as A and / or B, can represent: A exists alone, A and B exist together, B exists alone, three cases.
[0113] As used in the description of the application and the appended claims, the term "if" can be interpreted as "when" or "upon" or "in response to determining" or "in response to detecting" depending on the context. Similarly, the phrase "if it is determined" or "if [a described condition or event] is detected" can be interpreted to mean "upon determining" or "in response to determining" or "upon detecting [the described condition or event]" or "in response to detecting [the described condition or event]", depending on the context.
[0114] The above-described embodiments are only used to illustrate the technical solutions of the present application, but not limit them; although the present application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that: it can still modify the technical solutions recorded in the foregoing embodiments, or make equivalent replacement for part of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present application, and should be included in the protection scope of the present application.
Claims
1. A Rust language code optimization method, characterized by, The method comprises: acquiring a Rust project to be optimized, the Rust project to be optimized comprising a plurality of code segments labeled as unsafe; inputting the Rust project to be optimized into a Rust compiler, and optimizing the unsafe labels according to output information of the Rust compiler; traversing each code segment corresponding to an optimized unsafe label, and inputting the code segment corresponding to the optimized unsafe label and a preset prompt template into a large language model, the prompt template being used to define requirements followed by the large language model when processing the Rust project; modifying the code segment corresponding to the optimized unsafe label in the Rust project to be optimized according to output information of the large language model; detecting the compilation of the modified Rust project to be optimized by using the Rust compiler; in a case where the modified Rust project to be optimized passes the compilation of the Rust compiler, deleting the corresponding unsafe label.
2. The method of claim 1, wherein, The inputting of the Rust project to be optimized into the Rust compiler and the optimizing of the unsafe labels according to the output information of the Rust compiler comprise: removing the unsafe labels corresponding to all function declarations and function internal code blocks in the Rust project to be optimized; inputting the Rust project to be optimized after removing all the unsafe labels into the Rust compiler; adding the unsafe labels to at least one code segment reported as an error by the Rust compiler according to error information output by the Rust compiler.
3. The method of claim 2, wherein, The adding of the unsafe labels to the at least one code segment reported as an error by the Rust compiler comprises: merging code segments with overlapping parts in the code segment reported as an error by the Rust compiler; and adding the unsafe labels to the merged code segments.
4. The method of claim 3, wherein, The method further comprises: using a regular expression to match predefined errors in the Rust project to be optimized after the adding of the unsafe labels, and correcting the matched errors.
5. The method of claim 1, wherein, The method further comprises: finding a function call statement in the code segment corresponding to the optimized unsafe label; modifying the function call statement into Rust code according to a preset equivalent replacement rule.
6. The method of claim 1, wherein, The method further comprises: in a case where the code segment corresponding to the optimized unsafe label comprises a definition, access, or modification operation of a mutable static constant, deleting the corresponding unsafe label.
7. A Rust language code optimization apparatus, comprising: The device comprises: an acquisition unit configured to acquire a Rust project to be optimized, the Rust project to be optimized comprising a plurality of code segments labeled as unsafe; a processing unit configured to input the Rust project to be optimized into a Rust compiler, and optimize the unsafe labels according to output information of the Rust compiler; the processing unit is further configured to traverse each code segment corresponding to an optimized unsafe label, and input the code segment corresponding to the optimized unsafe label and a preset prompt template into a large language model, the prompt template being used to define requirements followed by the large language model when processing the Rust project. The processing unit is further configured to modify, according to the output information of the large language model, a code segment corresponding to the optimized unsafe label in the Rust project to be optimized; detect, using the Rust compiler, the compilability of the modified Rust project to be optimized; and delete the corresponding unsafe label in the case where the modified Rust project to be optimized passes the compilation of the Rust compiler.
8. The apparatus of claim 7, wherein, The processing unit is specifically configured to remove all unsafe labels corresponding to function declarations and function internal code blocks in the Rust project to be optimized; and input the Rust project to be optimized after removing all unsafe labels into the Rust compiler. According to the error information output by the Rust compiler, at least one code segment with an error of the Rust compiler is added with an unsafe label.
9. A computing device, comprising: The computing device includes a processor and a memory; the processor is coupled with the memory; the memory is used to store computer instructions, the computer instructions are loaded and executed by the processor to enable the computing device to implement the method in any one of claims 1 to 6.
10. A computer-readable storage medium, characterized in that, The computer readable storage medium stores instructions, when running on a computer, causes the computer to execute the method in any one of claims 1 to 6.
Citation Information
Patent Citations
Automatic code conversion and evaluation method and device from C language to RUST language
CN118170672A
C-to-Rust code translation large model construction method based on intermediate representation
CN119166162A