Rust language code optimization method and device and computing equipment

By combining the Rust compiler and large language model code optimization methods, the unsafe tags in the Rust code translated by c2rust solved the problem that existing tools cannot effectively apply Rust memory security features, and achieved the effect of significantly reducing the proportion of unsafe code and improving system security.

CN119829064AActive Publication Date: 2025-04-15XFUSION DIGITAL TECH CO LTD

Patent Information

Application Number
CN202411920142.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-24
Publication Date
2025-04-15
Estimated Expiration
2044-12-24

AI Technical Summary

Technical Problem

The existing automatic conversion tool c2rust from C to Rust cannot effectively apply Rust's memory security features, resulting in a large number of functions in the translated code being marked as unsafe and unable to fully utilize Rust's memory security advantages.

Method used

Through a code optimization method based on Rust compiler and large language model, unsafe tags are optimized and unsafe code ratio is reduced. This method includes inputting the Rust project to be optimized into the Rust compiler, optimizing the unsafe tag based on the output information, and inputting the optimized code snippet into the large language model for further optimization, and finally detecting and deleting unnecessary unsafe tags through the Rust compiler.

Benefits of technology

It effectively reduces the proportion of unsafe code in the Rust project, improves the system's memory security, and reduces the later maintenance cost and generalization of optimization methods through automatic update and optimization of large language models.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119829064A_ABST
    Figure CN119829064A_ABST
Patent Text Reader

Abstract

The invention discloses a Rust language code optimization method and device and computing equipment, and relates to the technical field of computers.The method comprises the steps that a to-be-optimized Rust project can be obtained; inputting the to-be-optimized Rust item into the Rust compiler, and optimizing the unsafe label according to the output information of the Rust compiler; inputting the code snippets corresponding to the optimized unsafe labels and a preset cue word template into a large language model; according to output information of the large language model, modifying a corresponding code snippet in the to-be-optimized Rust project; detecting the compilation of the modified to-be-optimized Rust item by using a Rust compiler; and if the compilation is passed, deleting the corresponding unsafe label. In this way, the proportion of unsafe codes in the Rust project can be greatly reduced, and the system safety is improved. Moreover, the generalization of the optimization method can be effectively improved by utilizing the large language model, and the cost of later maintenance is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a Rust language code optimization method, device and computing equipment. Background Art

[0002] Rust is an emerging static programming language with memory safety as its feature. Unlike the traditional C programming language, Rust can detect memory errors at compile time and ensure memory safety. Due to the outstanding performance of Rust in memory safety and performance, many developers migrate project codes from C to Rust. However, since the existing C code base is very large, manual reconstruction requires a lot of manpower and is costly. To this end, someone proposed a language conversion tool system c2rust, which can automatically convert C to Rust. However, c2rust has a series of limitations, especially that it only translates the syntax and semantics of a single file, does not apply some of the safety features of Rust (such as life cycle, ownership, etc.) to the code generation process, and breaks the dependencies between files in the project, which results in all functions in the translated code being labeled as unsafe. There are too many unsafe codes, memory safety cannot be guaranteed, and the advantages of Rust memory safety cannot be brought into play.

[0003] However, in the actual system design process, unsafe code is inevitable in the implementation process. Eliminating or reducing the use of unsafe code is the key to improving system security. There are currently some optimization methods that can identify unsafe code based on preset fixed rules, but this method is difficult to deal with unsafe issues outside the rules. At the same time, there are problems such as high cost of updating and maintaining the rule base in the later stage and the inability of old rules to adapt to the new Rust version update.

[0004] Therefore, how to improve system security after automatically translating C language projects into Rust language is a key issue that needs to be solved at present. Summary of the invention

[0005] This application provides a Rust language code optimization method, device and computing device, 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, an embodiment of the present application provides a Rust language code optimization method, which can obtain a Rust project to be optimized, wherein the Rust project to be optimized includes multiple code snippets labeled as unsafe; input the Rust project to be optimized into a Rust compiler, and optimize the unsafe labels according to the output information of the Rust compiler; traverse the code snippets corresponding to each optimized unsafe label, and input the code snippets corresponding to the unsafe labels and a preset prompt word template into a large language model, wherein the prompt word template is used to define the requirements followed by the large language model when processing the Rust project; according to the output information of the large language model, modify the code snippets corresponding to the optimized unsafe labels in the Rust project to be optimized; use the Rust compiler to detect the compilability of the modified Rust project to be optimized; and when the modified Rust project to be optimized passes the compilation of the Rust compiler, delete the corresponding unsafe labels.

[0007] The method provided in the embodiment of the present application first optimizes the unsafe labels according to the output information of the Rust compiler, and preliminarily reduces the proportion of unsafe code. Then, the code snippets corresponding to the unsafe labels that still exist are input into the large language model, and the large language model is informed through the prompt word template that the input Rust code snippets contain unsafe behaviors or operations, and the prompt word template defines how the large language model needs to handle the Rust code snippets, so that the large language model handles the unsafe code snippets as required, and then the optimized unsafe labels can be modified according to the output information of the large language model. The code snippets corresponding to the modified code snippets are safe, thereby reducing the proportion of unsafe code. Based on the method provided in the embodiment of the present application, the Rust project to be optimized is optimized multiple times, which can greatly reduce the proportion of unsafe code in the Rust project, thereby improving system security. In addition, since the large language model can automatically update the optimization, applying the large language model to the optimization of unsafe code snippets of the Rust program can effectively improve the generalization of the optimization method and reduce the cost of later maintenance.

[0008] In combination with the first aspect, in certain implementations of the first aspect, the method further includes: if the modified Rust project to be optimized fails to pass the compilation of the Rust compiler, modifying the temperature parameter of the large language model; repeatedly inputting the code snippet corresponding to the optimized unsafe label and the preset prompt word template into the large language model, modifying the code snippet corresponding to the unsafe label in the Rust project to be optimized according to the output information of the large language model, and using the Rust compiler to detect the compilability of the modified Rust project to be optimized, 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 fails to pass the compilation of the Rust compiler, retain the unsafe label.

[0009] In combination with the first aspect, in some implementations of the first aspect, the Rust project to be optimized is input into the Rust compiler, and unsafe labels are optimized according to the output information of the Rust compiler, including: removing all function declarations in the Rust project to be optimized and unsafe labels corresponding to the code blocks inside the function; inputting the Rust project to be optimized after all unsafe labels are removed into the Rust compiler; and adding an unsafe label to at least one code snippet where the Rust compiler reports an error according to the error information output by the Rust compiler. In this way, all unsafe labels are removed first, and then unsafe code is identified, and unsafe labels are added to the unsafe code, thereby reducing the scope of unnecessary unsafe code and preliminarily reducing the proportion of unsafe code.

[0010] In conjunction with the first aspect, in some implementations of the first aspect, adding an unsafe label to at least one code snippet reported by the Rust compiler includes: merging the code snippets with overlapping parts in the code snippets reported by the Rust compiler; and adding an unsafe label to the merged code snippet. In this way, duplication of code snippets corresponding to the unsafe label can be avoided.

[0011] In combination with the first aspect, in some implementations of the first aspect, the method further includes: in the Rust project to be optimized after adding the unsafe label, using regular expressions to match predefined errors, and correcting the matched errors. After modification according to the compiler prompt information, some errors may occur in the Rust code, resulting in failure to compile Rust. For this reason, a correction library is set in the embodiment of the present application, and the correction library includes many patterns / errors that may cause compilation failure. After modification according to the compiler prompt information, regular expressions are used to match, identify and correct the errors in the correction library.

[0012] In conjunction with the first aspect, in some implementations of the first aspect, the method further includes: searching for function call statements in the optimized code snippet corresponding to the unsafe label; and modifying the function call statements into Rust code according to a preset equivalent replacement rule. This can reduce unsafe libc library function calls, thereby reducing unsafe labels and improving system security.

[0013] In combination with the first aspect, in certain implementations of the first aspect, the method further includes: when the code snippet corresponding to the optimized unsafe label includes a definition, access or modification operation of a variable static constant, deleting the corresponding unsafe label, thereby reducing unsafe labels and improving system security.

[0014] In a second aspect, an embodiment of the present application provides a Rust language code optimization device, comprising: an acquisition unit and a processing unit, wherein the acquisition unit is used to acquire a Rust project to be optimized, wherein the Rust project to be optimized includes multiple code snippets labeled as unsafe. The processing unit is used to input the Rust project to be optimized into the Rust compiler, and optimize the unsafe labels according to the output information of the Rust compiler; traverse the code snippets corresponding to each optimized unsafe label, and input the code snippets corresponding to the unsafe labels and the preset prompt word template into the large language model, and the prompt word template is used to define the requirements followed by the large language model when processing the Rust project; according to the output information of the large language model, modify the code snippets corresponding to the optimized unsafe labels in the Rust project to be optimized; use the Rust compiler to detect the compilability of the modified Rust project to be optimized; when the modified Rust project to be optimized passes the compilation of the Rust compiler, delete the corresponding unsafe labels.

[0015] In combination with the second aspect, in certain implementations of the second aspect, the above-mentioned processing unit is specifically used to: remove all function declarations in the Rust project to be optimized and unsafe labels corresponding to the code blocks inside the functions; input the Rust project to be optimized after all unsafe labels are removed into the Rust compiler; and according to the error information output by the Rust compiler, add an unsafe label to at least one code snippet in which the Rust compiler reports an error.

[0016] When used to add an unsafe label to at least one code snippet in which the Rust compiler reports an error, the above-mentioned processing unit is also specifically used to: merge snippets with overlapping parts in the code snippets in which the Rust compiler reports an error; and add an unsafe label to the merged code snippet.

[0017] The above processing unit is also specifically used to: in the Rust project to be optimized after adding the unsafe label, use regular expressions to match predefined errors, and correct the matched errors.

[0018] The above-mentioned processing unit is also used to: search for function call statements in the code fragment corresponding to the optimized unsafe label; and modify the function call statements into Rust codes according to preset equivalent replacement rules.

[0019] The processing unit is further configured to: when the optimized code snippet corresponding to the unsafe label includes a definition, access or modification operation of a variable static constant, delete the corresponding unsafe label.

[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 used to store computer instructions, and the computer instructions are loaded and executed by the processor to enable the computing device to implement a method as described in any one of the above-mentioned first aspects.

[0021] In a fourth aspect, an embodiment of the present application provides a device, which is included in a computing device, and has the function of implementing the above aspects and the computing device behavior in the possible implementation methods of the above aspects. The function can be implemented 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, wherein instructions are stored in the computer-readable storage medium, and when the computer-readable storage medium is executed on a computer, the computer executes any one of the methods in the first aspect.

[0023] In a sixth aspect, an embodiment of the present application provides a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute any of the methods described in the first aspect above.

[0024] The technical effects obtained by the above-mentioned second, third, fourth, fifth and sixth aspects are similar to the technical effects obtained by the corresponding technical means in the above-mentioned first aspect, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0025] Figure 1 A schematic diagram of a process of translating a C language code into a Rust language code provided in an embodiment of the present application is shown;

[0026] Figure 2 A schematic diagram of a Rust language code optimization method provided in an embodiment of the present application is shown;

[0027] Figure 3 A schematic diagram of the structure of a device provided in an embodiment of the present application is shown;

[0028] Figure 4 A schematic diagram of the structure of a device provided in an embodiment of the present application is shown. DETAILED DESCRIPTION

[0029] In order to make the purpose, technical scheme and advantages of the present application clearer, the implementation mode of the present application will be further described in detail below with reference to the accompanying drawings. Hereinafter, the terms "first" and "second" are used for descriptive purposes only and cannot be understood as indicating or implying relative importance or implicitly indicating the number of indicated technical features. Thus, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features. In the description of the present embodiment, unless otherwise specified, the meaning of "multiple" is two or more.

[0030] In order to facilitate the understanding of the technical solution, the terms involved in the embodiments of the present application are first explained.

[0031] 1. Rust language: Rust is an emerging static programming language with memory safety as its feature. Different from the traditional C / C++ programming language, Rust language introduces the concepts of variable life cycle and ownership. With the help of ownership and life cycle features, Rust language can detect memory errors at compile time, thus avoiding problems such as memory leaks, dangling pointers, buffer overflows and data competition in concurrent programming to a large extent, ensuring memory safety.

[0032] The Rust language retains the unsafe mechanism to bypass static checks at compile time to implement complex operations that cannot be implemented in the safety check mode. The unsafe and safe keywords are used in the Rust language to distinguish between the safety and potential unsafety of the code. Among them, the compiler can automatically verify the safety of the code block marked with the safe keyword. Code blocks or functions marked with the unsafe keyword allow developers to use some unsafe operations, such as directly operating memory, calling external C libraries, or implementing underlying system interfaces. These operations may bypass Rust's safety checks, so the Rust compiler cannot guarantee their safety. In other words, functions and code blocks marked with unsafe are not checked by the Rust compiler and memory safety cannot be ensured. Intuitively, the less unsafe code a Rust project has, the better its memory safety.

[0033] 2. Code translation tool c2rust: c2rust is a tool that converts C language to Rust language, designed to help developers migrate existing C code bases to Rust while maintaining the functional and semantic consistency of the code.

[0034] The embodiment of the present application proposes a Rust language code optimization method based on a large language model, which can obtain Rust code snippets to be optimized, remove unnecessary unsafe functions, code blocks and code snippets, thereby optimizing unsafe labels; further use the preset prompt word template to input the remaining unsafe code snippets into the large language model for iterative optimization; finally, replace unnecessary unsafe libc library functions to further reduce the proportion of unsafe code. The method provided by the embodiment of the present application can reduce the proportion of unsafe code in the Rust code translated by c2rust, improve system security, and use the large language model to automatically iteratively optimize Rust unsafe code, effectively improving the generalization of the optimization method and reducing the cost of later maintenance.

[0035] The method provided in the embodiment of the present application can be applied to any scenario where C language code needs to be translated into Rust language code. The method is implemented by a computer chip or entity, or by a product with a certain function. A typical implementation device is a computer. Exemplarily, the computer can be, for example, 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 following is a brief description of the process of translating C language code into Rust language code.

[0037] Reference Figure 1 For the source code of the C project to be translated, the Goblint static analysis tool is used to identify and remove useless code in the C project and merge the code. For example, through the intermediate representation (IR) of the Low Level Virtual Machine (LLVM) compiler, Goblint can analyze the control flow and data flow in the C code and detect possible redundant or unreachable code. Then, Goblint uses its Abstract Syntax Tree (AST) model to perform fine-grained static analysis of the program to identify unused variables, invalid function calls, and dead code segments. By combining these analysis results, the tool can accurately remove useless code, reduce redundant parts, merge similar code blocks when appropriate, and output a merged single-file C project.

[0038] Then, use c2rust to translate the C project source code into the Rust project to obtain a single-file Rust project. Specifically, the source code of the C project is parsed into AST, and an intermediate representation is generated through the LLVM tool chain. At this time, the tool will collect the structure, type information, and control flow information in the code; after obtaining the AST, c2rust will map the various grammatical structures of C (such as functions, structures, pointer operations) to the corresponding structures in Rust through the rule set and translation library. This process involves structural adjustments to the code, such as converting a C struct to a Rust struct, and taking into account Rust's life cycle and ownership characteristics.

[0039] After obtaining a single-file Rust project, perform naked pointer rewriting to remove useless 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. After that, eliminate unnecessary unsafe functions or code blocks, use the Large Language Model (LLM) to further optimize unsafe code snippets, and finally eliminate unnecessary unsafe calls and mutable static constants to obtain an optimized Rust project.

[0040] The method provided in the embodiments of the present application is described in detail below. Figure 2 A schematic diagram of a Rust language code optimization method based on a large language model provided in an embodiment of the present application is shown, as Figure 2 As shown, the method may include the following S201-S203. The method may be executed by a processor.

[0041] S201, obtain the Rust project to be optimized.

[0042] S202, input the Rust project to be optimized into the Rust compiler, and optimize the unsafe label according to the 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 to check whether the Rust project to be optimized can be compiled. In a possible implementation, if the Rust project cannot be compiled, the Rust project to be optimized is repaired manually or by other means so that the Rust project to be optimized can be compiled. It should be noted that the implementation of the subsequent steps depends on the Rust compiler, so the Rust project to be optimized must be able to be compiled.

[0044] For the Rust project to be optimized that can be compiled, the unsafe labels in the Rust project are optimized. In a possible implementation, all unsafe labels in the Rust code can be removed first, and then the optimized unsafe labels are marked on the Rust code.

[0045] For example, for a Rust project to be optimized that can be compiled, the Rust code to be optimized is read into memory, the Rust code to be optimized is analyzed using the syn package, the Rust source code (usually source code fragments of functions, structures, macros, etc.) is parsed into an abstract syntax tree AST, all functions and their internal code blocks in the syntax tree are traversed, all unsafe tags existing on function declarations and function internal code blocks are removed, the syntax tree is exported as Rust code, and written back to the source file. In this way, all unsafe tags in the Rust project to be optimized are removed.

[0046] Among them, syn is a parsing toolkit for the Rust programming language, which provides a powerful abstraction that allows developers to easily parse, process, modify and generate abstract syntax trees in Rust code. The abstract syntax tree is a structured representation of Rust code.

[0047] After removing all unsafe tags, the Rust project to be optimized is input into the rustc compiler, which analyzes it and outputs one or more error messages.

[0048] In one possible implementation, for the target error E1033, the rustc compiler outputs the error message: "Unsafe code was used outside of an unsafe block", that is, "unsafe code is outside the unsafe code block". For the error E1033, the starting byte position and the ending byte position of all code blocks of this type of error are recorded to obtain the error set P = {z1, z2, ..., z n}, z i = <t i ,d i >, t i <d i , where t i Indicates error i The starting byte position of the code in the source file, d i Indicates error i The ending byte position of the code in the source file.

[0049] Then, the starting byte positions of the codes that appear repeatedly in all the unsafe codes are merged by interval merging, and the repeated parts are removed. For example, the interval array M=Φ is initialized. Each element z in the set P is i Considered as an interval, t i and d i Use the quick sort method to sort the left and right boundaries of the interval. i Sort all elements in the error set P in ascending order, recorded as P'. Then traverse each element in P', add the interval of each element to the interval array M, and consider each subsequent interval in order: if the left endpoint of the current interval is after the right endpoint of the last interval in array M, add this interval to the end of array M; otherwise, use the right endpoint of the current interval to update the right endpoint of the last interval in array M and set it to the larger value of the two. For example, the starting byte position of element z1 is the second byte of the first row and the ending byte position is the third byte of the third row. The starting byte position of element z2 is the second byte of the second row and the ending byte position is the sixth byte of the fifth row. Then the left endpoint of the current interval is before the right endpoint of the last interval in array M. The two can be merged. The starting byte position of the merged interval is the second byte of the first row and the ending byte position is the sixth byte of the fifth row.

[0050] Read the Rust code to be optimized again, and use the unsafe tag to mark the code in the interval according to the interval of one or more unsafe codes recorded in the array M. Exemplarily, the operation is to add "unsafe{" in front of the interval code and "}" at the end, and write it back to the original file. Exemplarily, after adding the unsafe tag to a certain interval "xxx", it becomes unsafe{xxx}.

[0051] Optionally, after making modifications according to the compiler prompt information, some errors may occur in the Rust code, resulting in failure to compile through Rust. For this reason, a correction library is set in an embodiment of the present application. The correction library includes many patterns / errors that may cause compilation failure. After making modifications according to the compiler prompt information, regular expressions are used to match, identify and correct the errors in the correction library.

[0052] For example, the library includes the pattern Unsafe{match corresponding to the "match" keyword. After making modifications according to the compiler prompts, the "match" keyword of the Rust language may be placed within the scope of the unsafe tag, while the "match" keyword is actually safe. Therefore, regular expressions can be used to match and identify the error and correctly fix it to the previous nearest unsafe tag. The code before the fix is ​​Unsafe{match...}, and the code after the fix is ​​match Unsafe{}.

[0053] For another example, the correction library includes patterns corresponding to assignment statements. After making modifications according to the compiler prompt information, it may cause an assignment statement to have unsafe code blocks appearing on both the left and right sides. Therefore, regular expressions can be used to match and identify the error and correctly fix it to a complete assignment statement in an unsafe code block. The code before the repair is Unsafe{..}=unsafe{..}, and the code after the repair is unsafe{..=..}.

[0054] Optionally, the corrected Rust code can be further input into the compiler to check whether it passes the compilation. 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 of the marked interval code, remove "}" at the end, and write it back to the original file. In other words, the code before analysis is unsafe{..=..}. Through analysis, it is found that the unsafe label is unnecessary and can be removed. The code can be changed to "..=..".

[0055] In the related art, all functions are marked as unsafe after translation. In fact, most functions and code blocks do not need to be marked as unsafe, which expands the scope of unsafety and creates obstacles for further using the safety features of the Rust language to optimize the code. In the embodiment of the present application, all unsafe labels are first removed, and then the unsafe code is identified, and the unsafe label is added to the unsafe code, and then the code is corrected. Through a series of operations, the scope of unnecessary unsafe code is reduced, the proportion of unsafe code is initially reduced, and the problem of excessively high proportion of unsafe code in Rust code caused by c2rust translation is solved, thereby improving the security of Rust code.

[0056] S203, using a large language model to optimize the optimized unsafe tag code segment.

[0057] In a possible implementation, the Rust code obtained according to step S202 may be read in, parsed using the syn toolkit to generate a syntax tree A, all nodes of A are traversed, and the start and end line numbers of all functions and code blocks marked unsafe are recorded in a set U, where U = {l1,l2,…,l n}, where l i = i ,e i ,f i >, l i Represents a function or code block marked as unsafe. i and e i Respectively represent l i The starting and ending line numbers of f i Indicates l i The path to the source file.

[0058] Randomly or sequentially select a function or code block l to be optimized from U i , from the file f i Read the number of lines from max{0,s i -p} to min{len,s i +p}, where p is the context range parameter and len is the file f i The number of lines of code.

[0059] According to the preset prompt template and unsafe code segment l i Generate input content and input it into the 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 the Rust code to be optimized (along with the surrounding context) to this location

[0065] ---

[0066] ###Instructions for fixing the unsafe code### ​

[0067] ###Instructions and examples for formatting the changelogoutput######Answer:###”

[0068] In an embodiment of the present application, the large language model is informed through the Prompt template that the input Rust code snippet contains unsafe behavior or operation, and it is defined how the large language model needs to handle the Rust code snippet, and then the large language model repairs the unsafe code as required. Exemplarily, in an instruction for repairing unsafe code, the above Prompt template requires the temperature parameter to be 0.5, and the processing requirements may be as follows: 1. Not every code snippet needs to be repaired or is related to unsafety, but the code in all the above code snippets needs to be considered. 2. Assume that the code snippet may be incomplete and may be missing lines above or below. 3. Do not add comments or code that are not needed to repair unsafe behavior. 4. Do not use unsafe or unstable features (through '#![feature(...)]'). 5. Make sure the code compiles correctly after repair. 6. Return one or more change groups, each of which contains one or more fixes to the above code snippet. Each group must be formatted according to the instructions below.

[0069] After the large language model fixes the unsafe code, it outputs a change log. Exemplarily, the description and example of the change log can be as follows: 1. The team 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 the few lines before and after the fix), followed by a fixed code snippet, which contains all consecutive fixed code lines that must replace the original code lines (including the same few lines before and after the change). 3. In each pair, the original code and fixed code snippets must start at the same source code line number N. Each code line listed in the original code and fixed code snippets must be prefixed with [N] that matches the line index N in the above code snippet, and then prefixed with exactly the same blank indentation as the above 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 device modifies the corresponding unsafe code segment l according to the line number. i Then, the modified code is fed into the Rust compiler to check if it compiles successfully.

[0071] If the code tuple modified based on the large language model passes compilation, the successfully repaired tuple is removed from the set U. If the code tuple modified based on the large language model fails compilation, modify the LLM temperature parameter (randomly selected between 0 and 1) and use the large language model again to modify the unsafe code segment li Optimize until the modified code tuple based on the large language model passes 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 code tuple still fails to pass compilation, the tuple that failed to be repaired is skipped, removed from the set U, and added to the unsuccessful repair 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, and the unsuccessfully repaired set E is fed back to the user to prompt and guide the user to perform manual repair.

[0073] In the above, the code block and its context that have not eliminated the unsafe tag after step S202 are brought into the prompt word template, and the large language model is called for optimization to further eliminate the unsafe tag, which can greatly reduce the proportion of unsafe code and unsafe functions. Since the rule-based method cannot handle cases that are not in the rule base, and with the upgrade of the Rust language version, there are maintenance issues such as rule updates and deletions. The embodiment of the present application optimizes unsafe code based on a large language model. The large language model can be automatically updated and optimized, which can solve the problems of poor generalization ability and high maintenance cost of unsafe code optimization technology.

[0074] In some implementations, the large language model can be trained based on public data captured from the Internet.

[0075] In other implementations, the large language model can be fine-tuned in a targeted manner to improve the successful repair rate of unsafe code. For example, successful repair cases of unsafe code are collected to form training samples, and the large language model is trained and fine-tuned based on the training samples using methods such as PromptTuning or Adapter, thereby enhancing the large language model's optimization capability for Rust code and improving the successful repair rate of unsafe code.

[0076] In other implementations, the prompt words of the query code large model can be optimized and improved. For example, the pattern type of the unsafe code can be recognized, and then similar samples can be matched from the collected training samples, and the matched similar samples can be input into the large language model as part of the prompt words to improve the successful repair rate of the unsafe code.

[0077] The above eliminates the unsafe labels in functions and code blocks. After using a large language model to optimize the unsafe labels, the method provided in the embodiment of the present application can also search for function call statements and statements involving mutable static constants, and eliminate the unsafe labels corresponding to function calls and mutable static constants.

[0078] Exemplarily, after C language is translated into Rust language, some operations are implemented using functions in libc library, and libc library function calls must use unsafe labels, but there are some functions in Rust language that can replace libc library functions. For this reason, in the embodiment of the present application, some libc library functions that do not need to use unsafe labels can be rewritten to eliminate unnecessary unsafe libc library function calls. Exemplarily, after using a large language model for optimization, the syn toolkit is used to parse and obtain a syntax tree A, and all nodes marked as unsafe in A are traversed to determine whether there are statements involving libc library function calls. If there are statements involving libc library function calls, according to the preset equivalent replacement rules, the statement is repaired to a safe Rust basic library call. Table 1 shows some examples of preset equivalent replacement rules, wherein the first column of Table 1 is the code features after C language is translated into Rust language, libc library functions include but are not limited to tanh function, exp function, fprintf function, etc., and the second column of Table 1 is the code features after the libc library function call statement is equivalently replaced.

[0079] Table 1

[0080]

[0081] After eliminating the function call, based on the obtained Rust code, continue to use the syn toolkit to parse and obtain the syntax tree A, traverse all statements marked as unsafe nodes in A, and determine whether the statement includes the definition, access or modification of mutable static constants. If included, remove the unsafe label in the statement, and then use regular expressions to match, identify and correct the errors in the correction library, and finally obtain the Rust language program code that can be compiled.

[0082] The above rewrites some unnecessary unsafe libc functions and uses the lazy_static mechanism to optimize the unsafe labels corresponding to mutable static constants, further reducing the unsafe ratio 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 the prompt information of the rustc compiler to preprocess the Rust code to be optimized, optimize the unsafe labels in the Rust code, reduce the scope of the unsafe code, and solve the problem that the safe function extension code fragment is marked as unsafe due to the incorrect annotation of the c2rust translation. Then, using the code capabilities of the large language model, an iterative optimization method is used to specifically repair and optimize the Rust language code fragments marked as unsafe, solve the problem that the rule-based optimization method cannot identify cases outside the rules, and improve the generalization ability of the method, without the need for rule design, update and maintenance.

[0084] Figure 3 A schematic diagram of the structure of a Rust language code optimization device for a large language model provided in an embodiment of the present application. The device 300 may include an acquisition unit 310 and a processing unit 320, and the processing unit 320 may implement corresponding processing functions.

[0085] Among them, the acquisition unit 310 can be used to obtain a Rust project to be optimized, and the Rust project to be optimized includes multiple code snippets labeled as unsafe.

[0086] The processing unit 320 can be used to input the Rust project to be optimized into the Rust compiler, optimize the unsafe label according to the output information of the Rust compiler; traverse the code snippet corresponding to each optimized unsafe label, and input the code snippet corresponding to the unsafe label and a preset prompt word template into the large language model, where the prompt word template is used to define the requirements followed by the large language model when processing the Rust project; modify the code snippet 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 compilability of the modified Rust project to be optimized; and delete the corresponding unsafe label when the modified Rust project to be optimized passes the compilation of the Rust compiler.

[0087] Optionally, when optimizing unsafe labels according to the output information of the Rust compiler, the above-mentioned processing unit 320 is specifically used to: remove all function declarations in the Rust project to be optimized and the unsafe labels corresponding to the internal code blocks of the functions; input the Rust project to be optimized after removing all unsafe labels into the Rust compiler; and according to the error information output by the Rust compiler, add an unsafe label to at least one code snippet in which the Rust compiler reports an error.

[0088] Optionally, when used to add an unsafe label to at least one code snippet in which the Rust compiler reports an error, the above-mentioned processing unit 320 is also specifically used to: merge the snippets with overlapping parts in the code snippets in which the Rust compiler reports an error; and add an unsafe label to the merged code snippet.

[0089] Optionally, the processing unit 320 is further specifically used to: in the Rust project to be optimized after adding the unsafe label, use a regular expression to match predefined errors, and correct the matched errors.

[0090] Optionally, the processing unit 320 is further used to: search for a function call statement in the optimized code snippet corresponding to the unsafe label; and modify the function call statement into Rust code according to a preset equivalent replacement rule.

[0091] Optionally, the processing unit 320 is further configured to: when the optimized code snippet corresponding to the unsafe label includes a definition, access or modification operation of a variable static constant, delete the corresponding unsafe label.

[0092] Optionally, the device 300 may further include a storage unit 330, which may be used to store instructions and / or data. The processing unit 320 may read the instructions and / or data in the storage unit 330, so that the device 300 implements the aforementioned method embodiment.

[0093] Regarding the steps or processes executed by each unit in the device 300, please refer to the above method embodiment for details, which will not be described in detail here.

[0094] It should be understood that the "unit" in the device 300 can be implemented by hardware, can be implemented by software, or can be implemented by hardware executing the corresponding software implementation. For example, the "unit" can refer to an application specific integrated circuit (ASIC), an electronic circuit, a processor (such as a shared processor, a dedicated processor or a group processor, etc.) and a memory for executing one or more software or firmware programs, a combined logic circuit and / or other suitable components that support the described functions.

[0095] Figure 4 This is a schematic diagram of the structure of a computer device provided in an embodiment of the present application. Figure 4 As shown, 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 also includes a memory 405 .

[0096] In some implementations, the memory 405 stores the following elements: executable modules or data structures, or a subset thereof, or an extended set thereof.

[0097] The method described in the above embodiment of the present application can be applied to the processor 401, or implemented by the processor 401. The processor 401 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by an integrated logic circuit of hardware or software instructions in the processor 401. 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 devices, discrete gates, transistor logic devices or discrete hardware components. 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 embodiment of the present application can be directly embodied as being executed by a hardware decoding processor, or being executed by a combination of hardware and software modules in the decoding processor. Among them, the software module can be located in a mature storage medium in the field such as a random access memory, a read-only memory, a programmable read-only memory, or an 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 completes the steps of the above method in combination with its hardware.

[0099] The processor 401 , the memory 405 , and the communication interface 403 may communicate with each other via the communication line 402 .

[0100] The memory 404 is used to store temporarily the operation data in the processor and the data exchanged with the external memory such as the hard disk.

[0101] In the above embodiments, the instructions stored in the memory for execution by the processor may be implemented in the form of a computer program product, wherein the computer program product may be pre-written in the memory, or may be downloaded and installed in the memory in the form of software.

[0102] The present application embodiment also provides a computer program product, which includes one or more computer instructions. When the computer program instructions are loaded and executed on the computer, the process or function according to the embodiment of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network or other programmable device. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website site, a computer, a server or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (digital subscriber line, DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium can be any available medium that a computer can store or a data storage device such as a server, a data center, etc. that includes one or more available media integrated. For example, the available medium can include a magnetic medium (e.g., a floppy disk, a hard disk or a tape), an optical medium (e.g., a digital versatile disc (digital versatile disc, DVD)), or a semiconductor medium (e.g., a solid state drive (solid state disk, SSD)), etc.

[0103] An embodiment of the present application provides an electronic device, which includes a processor and a memory, wherein the memory is used to store a computer program, and the processor is used to execute the computer program to perform the above method.

[0104] The embodiment of the present application provides a chip. The chip includes a processor, and the processor is used to call a computer program in a memory to execute the technical solution in the above embodiment. Its implementation principle and technical effect are similar to those of the above related embodiments, and will not be repeated here.

[0105] In the embodiments provided in the present application, it should be understood that the disclosed device / electronic device and method can be implemented in other ways. For example, the device / electronic device embodiments described above are only schematic, for example, the division of the modules or units is only a logical function division, and there may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point, the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms. In addition, each functional unit in each embodiment of the present application can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units.

[0106] The present application also provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps in the above-mentioned method embodiments can be implemented. The computer-readable storage medium stores a computer program or instructions. When the computer program or instructions are executed by a processor, the above-mentioned method is implemented.

[0107] The methods described in the above embodiments may be implemented in whole or in part by software, hardware, firmware, or any combination thereof. If implemented in software, the functions may be stored as one or more instructions or codes on a computer-readable medium or transmitted on a computer-readable medium. Computer-readable media may include computer storage media and communication media, and may also include any medium that can transfer a computer program from one place to another. The storage medium may be any target medium that can be accessed by a computer.

[0108] As a possible design, the computer-readable medium may include a compact disc read-only memory (CD-ROM), RAM, ROM, EEPROM or other optical disc storage; the computer-readable medium may include a magnetic disk storage or other magnetic disk storage device. Moreover, any connecting line may also be appropriately referred to as a computer-readable medium. For example, if the software is transmitted from a website, server or other remote source using a coaxial cable, fiber optic cable, twisted pair, DSL or wireless technology (such as infrared, radio and microwave), the coaxial cable, fiber optic cable, twisted pair, DSL or wireless technology such as infrared, radio and microwave are included in the definition of the medium. Disks and optical discs as used herein include compact discs (CDs), laser discs, optical discs, DVDs, floppy disks and Blu-ray discs, where disks generally reproduce data magnetically, while optical discs reproduce data optically using lasers. Combinations of the above should also be included in the scope of computer-readable media.

[0109] The 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 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 the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processing unit of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processing unit of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0110] In the above description, specific details such as specific system structures, technologies, etc. are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present application. However, it should be clear to those skilled in the art that the present application may also be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present application.

[0111] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, wholes, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or combinations thereof.

[0112] It should also be understood that, in the description of the present application, unless otherwise specified, “ / ” means or, for example, A / B can mean A or B; “and / or” in this article is merely a way to describe the association relationship of associated objects, and refers to any combination of one or more of the associated listed items and all possible combinations, including these combinations, for example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone.

[0113] As used in the specification and appended claims of this application, the term "if" can be interpreted as "when" or "uponce" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrase "if it is determined" or "if [described condition or event] is detected" can be interpreted as meaning "uponce it is determined" or "in response to determining" or "uponce [described condition or event] is detected" or "in response to detecting [described condition or event]", depending on the context.

[0114] The embodiments described above are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, a person skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.

Claims

1. A Rust language code optimization method, characterized in that: The method comprises: Obtain a Rust project to be optimized, wherein the Rust project to be optimized includes multiple code snippets labeled as unsafe; Inputting the Rust project to be optimized into a Rust compiler, and optimizing unsafe labels according to output information of the Rust compiler; Traversing the code snippet corresponding to each optimized unsafe label, inputting the code snippet corresponding to the optimized unsafe label and a preset prompt word template into the large language model, wherein the prompt word template is used to define the requirements followed by the large language model when processing the Rust project; According to the output information of the large language model, modify the code snippet corresponding to the optimized unsafe label in the Rust project to be optimized; Using the Rust compiler to detect the compilability of the modified Rust project to be optimized; When the modified Rust project to be optimized is compiled by the Rust compiler, the corresponding unsafe label is deleted.

2. The method according to claim 1, characterized in that The step of inputting the Rust project to be optimized into a Rust compiler and optimizing unsafe labels according to output information of the Rust compiler includes: Remove all function declarations and unsafe labels corresponding to the code blocks inside the function in the Rust project to be optimized; 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, an unsafe label is added to at least one code snippet for which the Rust compiler reports an error.

3. The method according to claim 2, characterized in that The step of adding an unsafe label to at least one code snippet in which the Rust compiler reports an error includes: Merge the code snippets with overlapping parts in the Rust compiler error code snippets; Add unsafe label to merged code snippets.

4. The method according to claim 3, characterized in that The method further comprises: In the Rust project to be optimized after adding the unsafe tag, use regular expressions to match predefined errors and correct the matched errors.

5. The method according to claim 1, characterized in that The method further comprises: Searching for a function call statement in the code snippet corresponding to the optimized unsafe label; According to the preset equivalent replacement rule, the function call statement is modified into Rust code.

6. The method according to claim 1, characterized in that The method further comprises: In the case where the code snippet corresponding to the optimized unsafe label includes a definition, access or modification operation of a mutable static constant, the corresponding unsafe label is deleted.

7. A Rust language code optimization device, characterized in that: The device comprises: An acquisition unit, used for acquiring a Rust project to be optimized, wherein the Rust project to be optimized includes a plurality of code snippets labeled as unsafe; A processing unit, configured to input the Rust project to be optimized into a Rust compiler, and optimize unsafe labels according to output information of the Rust compiler; The processing unit is further used to traverse the code snippet corresponding to each optimized unsafe label, and input the code snippet corresponding to the unsafe label and a preset prompt word template into the large language model, wherein the prompt word template is used to define the requirements followed by the large language model when processing the Rust project; The processing unit is further used to modify the code snippet 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 compilability of the modified Rust project to be optimized; and delete the corresponding unsafe label when the modified Rust project to be optimized passes the compilation of the Rust compiler.

8. The device according to claim 7, characterized in that The processing unit is specifically used to: remove all function declarations in the Rust project to be optimized and unsafe labels corresponding to the code blocks inside the function; input the Rust project to be optimized after all unsafe labels are removed into the Rust compiler; According to the error information output by the Rust compiler, an unsafe label is added to at least one code snippet for which the Rust compiler reports an error.

9. A computing device, characterized in that The computing device comprises a processor and a memory; the processor is coupled to the memory; the memory is used 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 claims 1 to 6.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores instructions, which, when executed on a computer, enable the computer to execute the method according to 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

Cited By

  • C-to-Rust code translation method and system and medium

    CN121209888A