C-to-Rust code translation large model construction method based on intermediate representation

Through the method based on intermediate representation, training and fine-tuning of the large model, the problems of insufficient parallel corpus and inequality in semantics in C to Rust code translation are solved, efficient and accurate code translation is achieved, and software migration costs and time are reduced.

CN119166162APending Publication Date: 2024-12-20DALIAN UNIV OF TECH
View PDF 0 Cites 4 Cited by

Patent Information

Application Number
CN202411315168.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-20
Publication Date
2024-12-20

AI Technical Summary

Technical Problem

The existing C-to-Rust code translation method based on large models has limited effect in terms of translation accuracy and is difficult to deal with complex C code, mainly due to the problems of insufficient parallel corpus and inequality of semantics.

Method used

Using an intermediate representation method, by collecting a large amount of Rust code and converting it into intermediate representation, the big model is trained for translation, and in the second stage, the C and Rust codes with the same functional semantics are collected and fine-tuned, adjusting the differences in intermediate representation to ensure the translated Code semantic consistency.

Benefits of technology

It realizes efficient and accurate automatic translation of C code into Rust code, reducing labor costs and time overhead during software migration, and improving the readability and maintenance of the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119166162A_ABST
    Figure CN119166162A_ABST
Patent Text Reader

Abstract

The invention belongs to the field of software engineering, and particularly relates to a C-to-Rust code translation large model construction method based on intermediate representation, which can be used for constructing an efficient code translation large model so as to improve the efficiency of translating a C code into a Rust code. The method is mainly divided into two stages. Firstly, a large number of Rust codes are collected through software project hosting platforms such as Githaub and the like, and the Rust codes are converted into corresponding intermediate representations used for training a large model to achieve translation from the intermediate representations to the Rust codes. Then, C codes and Rust codes with the same functional semantics are collected, and the C codes are converted into corresponding intermediate representations, so that the large model is finely adjusted, and intermediate representation differences in the translation process from the C codes to the Rust codes are aligned; the method can be used for automatically translating codes written in a C language in a software project into Rust codes with the same functional semantics.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of software engineering, and specifically is a method for constructing a large model of C to Rust code translation based on intermediate representation, which can be used to construct an efficient large model of code translation, thereby improving the efficiency of translating C code into Rust code. Background Art

[0002] Rust is a memory- and type-safe programming language with performance comparable to low-level languages ​​such as C. Rust is also known as "safe C" because its type checker ensures that there is no undefined behavior in the code. Microsoft estimates that 70% of all its security vulnerabilities are due to memory safety issues, which can be largely or completely eliminated if the code is written in Rust. Due to Rust's security advantages, Rust has been used in mainstream open source projects such as Firecracker, and Linus Torvalds recently announced that he would support the use of Rust as the development language for the Linux kernel.

[0003] C is a widely used system programming language, but its poor language-level security mechanism often leads to serious errors and vulnerabilities in software systems. Therefore, given the security and performance advantages of Rust, developers are eager to automatically translate code written in C into Rust code. At present, the mainstream C to Rust code translation methods can be divided into two categories, namely rule-based methods and large model-based methods. Rule-based methods use predefined rules and algorithms to achieve the translation of C code to Rust code. Its translation rules are interpretable and can theoretically ensure the correctness of code translation. Typical works of this type of method include C2Rust and Crust, among which C2Rust supports automatic translation of C99 standard C code into C code. However, rule-based methods usually produce code with poor readability and does not comply with language usage specifications. This makes the generated code difficult to maintain and upgrade later. And limited by the translation rules, such methods are difficult to handle complex C code.

[0004] Different from the rule-based C to Rust code translation method, the large model-based method takes the C code as the input of the large model, and then uses the large model to automatically generate the equivalent Rust code. Because the large model is trained with high-quality code, the translated code is usually readable and conforms to the usage specifications of the target programming language. TransCoder is a typical work of this type of method, which uses the intermediate representation of the code to provide additional information to improve the ability of the large model to translate the code. However, the existing related works all adopt a problem-oriented approach to construct parallel predictions, that is, when two codes solve the same problem, the two codes are considered equivalent. However, this method cannot guarantee that the constructed corpus is completely parallel. Because the code with the same function does not mean that the semantics are completely equivalent. At the same time, it is also very difficult to collect a large number of C codes and Rust codes that solve the same problem. This makes the existing large model-based C to Rust code translation method very limited in terms of translation accuracy. For example, a method and device for automatic code conversion and evaluation from C to RUST (patent number: CN202410374048.2) directly extracts C code and Rust code that solve the same problem from the programming competition dataset CodeNet, thereby training a large model to complete the translation from C to Rust. However, since CodeNet does not contain real project code and the amount of code is small, the large model trained with CodeNet cannot translate C code in real software projects into Rust code very well. Summary of the invention

[0005] In order to solve the above problems and efficiently realize the translation of C to Rust code, the present invention provides a method for constructing a large model of C to Rust code translation based on intermediate representation, and trains the large model by using the intermediate representation of the code supported by both C and Rust to solve the problem of insufficient parallel corpus. The method is mainly divided into two stages. In the first stage, a large amount of Rust code is collected on platforms such as Github and converted into the corresponding intermediate representation, and then the large model is trained to realize the translation from the intermediate representation to the Rust code. Although the large model can complete the translation from the intermediate representation to the Rust code, due to the differences between the two languages ​​​​C and Rust, even if the C code and Rust code with the same functional semantics are converted into the intermediate representation, there are still some differences in their respective intermediate representations. Therefore, it is still necessary to fine-tune the large model using C code and Rust code with the same functional semantics. Therefore, in the second stage, the present invention collects C code and Rust code with the same functional semantics, and converts the C code into the corresponding intermediate representation, thereby fine-tuning the large model and aligning the intermediate representation differences in the translation process of C to Rust code. The present invention can be used to automatically translate the code written in C language in the software project into Rust code with the same functional semantics; the relevant steps of the present invention can also be adapted according to the characteristics of any two specific programming languages, thereby realizing automatic translation of the two programming languages.

[0006] The technical solution of the present invention:

[0007] A method for constructing a large model of C to Rust code translation based on intermediate representation includes the following steps:

[0008] Step 1: Rust code collection.

[0009] Get the software code set S written in Rust language from the software project hosting platform (such as GitHub, Gitee, GitLab, etc.) 1 ,…,s n}, where s i Represents each independently compilable Rust code.

[0010] Step 2: Get the Rust code intermediate representation.

[0011] Use the Rust language compiler rustc to compile the obtained Rust code set S to obtain the intermediate representation set I = {I 1 ,…,I n}, where I i For i The corresponding intermediate representation.

[0012] Step 3: Train a large model for translating intermediate code to Rust.

[0013] The software code set S and the intermediate representation set I are combined into a training set X = {I 1 :s 1 ,…,I n :s n} and used to train the large model M to translate the intermediate representation into Rust code, where I i As the input of the large model, s i As the expected output of the large model. For the present invention, the large model mainly refers to a large language model that can be used to process software code tasks, such as CodeGeeX, Pangu-Coder, StarCoder2, WizardCoder, etc.

[0014] Step 4: Collect C code and Rust code with the same functional semantics.

[0015] Collect C code and Rust code with the same functional semantics from software project hosting platforms (such as GitHub, Gitee, GitLab, etc.) to construct a parallel dataset S of code translation from C to Rust ′ ={s ′ 1 ,…,s ′ m}, where s i ′ =(C i ,R i ), C i Indicates C code, R i Represents the same as C i Functionally identical Rust code.

[0016] Step 5: Get the intermediate representation of C code.

[0017] For C code C i , compile it using the C language compiler and obtain its intermediate representation It should be noted that the intermediate representation of the C code in this step and the Rust code in step 2 need to be the same standard intermediate representation.

[0018] Step 6: Fine-tune the code translation model.

[0019] Compose C code and Rust code with the same functional semantics and intermediate representation of C code Combined to form a fine-tuning dataset The code translation model M used to fine-tune the intermediate code in step 3 to Rust is obtained. ′ .in, is the input of the large model M, R mis the expected output of the large model M.

[0020] Step 7: Code translation of semantic property constraints.

[0021] The code translation from C to Rust must ensure semantic consistency before and after translation, so as to avoid the large model from converting bubble sort into quick sort, which is functionally equivalent but semantically not equivalent. Therefore, for the relevant properties in the original C code, such as requiring the translated Rust code to use a certain standard library function to implement a certain function, the present invention adds the relevant properties in the original C code to the large model M in the form of constraints. ′ , thereby guiding the large model translation to generate Rust code that meets this property.

[0022] First, the C code to be translated is converted into an intermediate representation using the method in step 5. Then the intermediate representation is used together with the property constraints of the code as the fine-tuned large model M ′ If the Rust code output by the large model satisfies all property constraints, it is considered that the large model has output code that meets the requirements; otherwise, the unsatisfied property constraints are refined, the large model input is reconstructed, and the large model is allowed to regenerate Rust code; this process is repeated until a satisfactory output is generated or the user termination condition is reached.

[0023] The beneficial effects of the present invention are as follows: the present invention can accurately and automatically translate C code into Rust code, thereby quickly realizing the migration of software code from C language to Rust language, reducing the manpower cost and time expenditure in the software migration process. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] Figure 1 It is a schematic diagram of the workflow of a method for constructing a large model of C to Rust code translation based on intermediate representation of the present invention. DETAILED DESCRIPTION

[0025] The specific implementation of the present invention is further described below in conjunction with the accompanying drawings and technical solutions.

[0026] In this embodiment, the method of the present invention is deployed on a Linux server, and the specific configuration of the server is shown in Table 1. According to the experimental requirements, the C language compiler Clang and the Rust language compiler rustc, and the large model fine-tuning tool LLaMA-Factory are installed.

[0027] Table 1 Linux server configuration information table

[0028]

[0029] like Figure 1As shown, a method for constructing a large model for translating C to Rust code based on an intermediate representation of the present invention is performed according to the following process. First, a large amount of Rust code is collected from platforms such as GitHub, and then compiled to obtain the corresponding intermediate representation. Next, the selected basic code large model is trained so that it can complete the translation of the intermediate representation to the Rust code. Then, a parallel dataset for code translation from C to Rust is constructed, and this dataset is used to fine-tune the large model for translating the intermediate representation to the Rust code, aligning the difference between the intermediate representation of the C code and the intermediate representation of the Rust code. Finally, the semantic property constraints are integrated into the input of the large model, and a specific code translation task is performed until the obtained Rust code satisfies all property constraints.

[0030] The specific steps of this embodiment are as follows:

[0031] Step 1: Rust code collection.

[0032] Get the software code set S written in Rust language from the software project hosting platform (such as GitHub, Gitee, GitLab, etc.) 1 ,…,s n}, where s i Represents each independently compilable Rust code. Rust code can also be collected from other software repositories as needed; large-scale Rust code refers to a large amount of Rust code, at least one million lines.

[0033] Step 2: Get the Rust code intermediate representation.

[0034] Use the Rust language compiler rustc to compile the obtained Rust code set S to obtain the intermediate representation set I = {I 1 ,…,I n}, where I i For i The corresponding intermediate representation. The specific rustc command is as follows:

[0035] rustc file.rs --emit=llvm-ir-OC no-prepopulate-passes-C codegen-units=1 Where "file.rs" is the Rust code to be compiled. In the present invention patent, LLVM IR is used as the intermediate representation of C code and Rust code. LLVM IR comes from the open source compiler framework LLVM, which is released under the Apache license. In the above command, "--emit=llvm-ir" is used to tell rustc to output LLVM IR during the compilation process, and the rest are corresponding control parameters.

[0036] Step 3: Train a large model for translating intermediate code to Rust.

[0037] The software code set S and the intermediate representation set R are combined into a training set X = {I 1 :s 1 ,…,I n :s n} and used to train the large model M to translate the intermediate representation into Rust code, where I i is the input of the large model, s i is the expected output of the big model. For the present invention, the big model mainly refers to a big language model that can be used to process software code tasks, such as CodeGeeX, Pangu-Coder, StarCoder2, WizardCoder, etc. In this implementation example, the big model M is StarCoder2, and LLaMA-Factory is used to fine-tune it. StarCoder2 is an open source code big model released under the Apache license. In actual applications, users can choose a suitable base big model according to their needs. LLaMA-Factory is an open source big model fine-tuning tool released under the Apache license, which supports fine-tuning of more than 100 big models. The parameters of LLaMA-Factory are shown in Table 2. Users can choose a suitable big model M as well as training tools and parameters according to the specific usage environment.

[0038] Step 4: Collect C code and Rust code with the same functional semantics.

[0039] Collect C code and Rust code collections with the same functional semantics from software project hosting platforms (such as GitHub, Gitee, GitLab, etc.) ′ ={s ′ 1 ,…,s ′ m}, where s i ′ =(C i ,R i ), C i Indicates C code, R i Represents the same as C i Functionally identical Rust code.

[0040] Table 2 Parameters of LLaMA-Factory

[0041]

[0042] Step 5: Get the intermediate representation of C code.

[0043] For C code Ci , compile it using the C language compiler Clang, and obtain its intermediate representation Clang is an open source C language compiler developed based on LLVM. The compilation parameters of Clang are as follows:

[0044] clang-O3-c-emit-llvm-mllvm-disable-llvm-optznsfile.co file.bc

[0045] Among them, "file.c" is the C code to be compiled, "-emit-llvm" means clang outputs LLVM IR during compilation, and "-mllvm-disable-llvm-optzns" means LLVM does not need to optimize the code. It should be noted that the intermediate representation of the C code in this step and the Rust code in step 2 need to be the same standard intermediate representation.

[0046] Step 6: Fine-tune the code translation model.

[0047] Compose C code and Rust code with the same functional semantics and intermediate representation of C code Combined to form a fine-tuning dataset The code translation model M used to fine-tune the intermediate code in step 3 to Rust is obtained. ′ .in, is the input of the large model M, R m is the expected output of the large model M. In this step, LLaMA-Factory is also used to fine-tune the large model M. The parameters of LLaMA-Factory are the same as those in step 3.

[0048] Step 7: Code translation of semantic property constraints.

[0049] The code translation from C to Rust must ensure semantic consistency before and after translation, so as to avoid the large model from converting bubble sort into a code that is functionally equivalent but semantically not equivalent, such as quick sort. Therefore, for the relevant properties in the original C code, such as requiring the translated Rust code to use a certain standard library function to implement a certain function, the present invention adds it to the large model M in the form of constraints. ′ , thereby guiding the large model translation to generate Rust code that meets this property.

[0050] Table 3 Main properties and description

[0051] nature describe The function name remains unchanged The function names in the original C code need to be preserved in the translated Rust code. The variable name remains unchanged The variable names in the original C code need to be preserved in the translated Rust code. Standard library function constraints Specify the correspondence between standard library functions before and after translation Code compiles The translated Rust code compiles The code can be tested The translated Rust code can pass the test case test

[0052] First, the C code to be translated is converted into an intermediate representation using the method in step 5. Then the intermediate representation is used together with the property constraints of the code as the fine-tuned large model M ′ In this step, the five constraints shown in Table 3 are mainly considered. If the Rust code output by the large model satisfies all property constraints, it is considered that the large model has output code that meets the requirements; otherwise, the unsatisfied property constraints are refined, the large model input is reconstructed, and the large model is allowed to regenerate Rust code; this is repeated until a satisfactory output is generated or the user termination condition is reached.

Claims

1. A method for constructing a large model for C to Rust code translation based on intermediate representation, characterized in that: The steps include: Step 1: Rust code collection; Obtain a set of software codes written in Rust from the software project hosting platform S = {s1,…,s n }, where s i Represents each independently compilable Rust code; Step 2: Get the intermediate representation of Rust code; Use the Rust language compiler rustc to compile the Rust code set S to obtain the intermediate representation set I = {I1,…,I n }, where I i For i The corresponding intermediate representation; Step 3: Train the large code translation model from intermediate code to Rust; The software code set S and the intermediate representation set I are combined into a training set X = {I1:s1,…,I n :s n } and used to train the large model M to translate the intermediate representation into Rust code, where I i As the input of the large model, s i As the desired output of the large model; Step 4: Collect C code and Rust code with the same functional semantics; Collect C code and Rust code with the same functional semantics from the software project hosting platform and construct a parallel dataset S of code translation from C to Rust ′ ={s ′ 1,…,s ′ m }, where s i ′ =(C i ,R i ), C i Indicates C code, R i Represents the same as C i Functionally equivalent Rust code: Step 5: Get the intermediate representation of C code; For C code C i , compile it using the C language compiler, and obtain its intermediate representation I i c ; It should be noted that the intermediate representation of the C code in this step and the Rust code in step 2 need to be the same standard intermediate representation; Step 6: Fine-tune the code translation model; Compose C code and Rust code with the same functional semantics and intermediate representation of C code Combined to form a fine-tuning dataset The code translation model M used to fine-tune the intermediate code in step 3 to Rust is obtained. ′ ; Among them, I i c is the input of the large model M, R m is the expected output of the large model M; Step 7: Code translation of semantic property constraints; First, the C code to be translated is converted into an intermediate representation using the method in step 5. Then, the intermediate representation is used together with the property constraints of the code as the fine-tuned large model M ′ input; if the Rust code output by the large model satisfies all property constraints, it is considered that the large model has outputted code that meets the requirements; otherwise, the unsatisfied property constraints are refined, the large model input is reconstructed, and the large model is allowed to regenerate Rust code; this process is repeated until a satisfactory output is generated or the user termination condition is reached.

Citation Information

Cited By

  • Rust language code optimization method and device and computing equipment

    CN119829064A

  • A rust language code optimization method and device and computing device

    CN119829064B

  • Multi-source heterogeneous code conversion method and device for cross-language development

    CN121143797A

  • A multi-source heterogeneous code conversion method and device for cross-language development

    CN121143797B