A program vulnerability automatic repair method based on a generative adversarial network

By combining generative adversarial networks with autoencoder pre-training of semantically equivalent code and alternating iterative training, the problem of limited model repair capabilities in existing technologies is solved, achieving efficient repair of complex vulnerability code and improving the accuracy and efficiency of vulnerability repair.

CN117828615BActive Publication Date: 2026-08-25INST OF SOFTWARE - CHINESE ACAD OF SCI
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410030910.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-01-09
Publication Date
2026-08-25
Estimated Expiration
2044-01-09

AI Technical Summary

Technical Problem

Existing deep learning-based program vulnerability repair technologies are mainly trained for one-to-one repair models, which limits the model's repair capabilities and makes it unable to effectively deal with complex vulnerability code.

Method used

We employ a generative adversarial network-based approach, combining semantically equivalent code for autoencoder pre-training and alternating iterative training. Through the adversarial process between the generator and the discriminator, we learn many-to-many vulnerability remediation mapping relationships and construct a vulnerability remediation model.

Benefits of technology

It improves the efficiency and accuracy of vulnerability remediation, enabling it to better handle complex vulnerability code in the real world and enhance developers' remediation efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117828615B_ABST
    Figure CN117828615B_ABST
Patent Text Reader

Abstract

The application discloses a kind of program vulnerability automatic repair method based on generative adversarial network, belong to computer technology field.This method is by constructing original data set, and is extended for the expansion data set used for pre-training;Using expansion data set, the generation model based on encoder-decoder is pre-trained by self-encoding;Obtain the data set of vulnerability repair code, construct generative adversarial network on the basis of the generation model obtained by self-encoding pre-training, and the data set of vulnerability repair code is used to alternately iteratively train the generative adversarial network, and the vulnerability repair model is obtained after training;Actual code vulnerability repair task is completed using vulnerability repair model.The application can improve the efficiency and accuracy of developer when repairing actual code vulnerability, and then accelerate vulnerability repair process.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of computer technology and relates to an automatic program vulnerability repair method based on generative adversarial networks. Background Technology

[0002] Security vulnerabilities in software programs pose a serious threat to computer systems. Malicious users can exploit these vulnerabilities to compromise programs, forcing them to behave improperly (such as crashing or exposing sensitive user information). Every year, tens of thousands of such vulnerabilities are publicly reported in Common Vulnerabilities and Exposures (CVE) databases, and many more are discovered and fixed within proprietary code. These vulnerabilities are often caused by programmer errors, and their rapid spread is due to the prevalence of open-source software and code reuse. Therefore, effectively assisting developers in fixing vulnerabilities in their code is crucial, and Automated Program Repair (APR) provides this support. It generates patches for faulty programs with minimal human intervention, thereby improving software reliability and reducing software development costs.

[0003] With the rapid development of deep learning (DL), recent learning-based app patching (APR) techniques utilize advanced DL technology to learn and generate patches from existing codebases. DL-based APR typically formulates the vulnerability patching problem as a translation from given vulnerable code to good code, employing neural machine translation (NMT) techniques. They often use an encoder-decoder architecture, where the encoder first embeds the vulnerable code, and the decoder iteratively generates a sequence of patches, which are then validated against test cases. However, in this approach, the model learns a one-to-one mapping from the comparison of generated patches and given target patches at the token level—that is, the patching method from a vulnerable code to its given standard patch. In reality, the mapping between vulnerable code and its corresponding patch is not one-to-one but rather one-to-many or even many-to-many, more akin to a mapping between two domains. This is because a single vulnerable code often has multiple different patching methods, and different vulnerabilities may share the same patching method. In this context, the patching capability of vulnerability patching models designed and trained based on a one-to-one patching pattern is limited, and their performance is often unsatisfactory when faced with various complex vulnerability codes in the real world. Summary of the Invention

[0004] The purpose of this invention is to address the problem that existing vulnerability repair technologies mainly train for one-to-one repair modes, thus limiting the model's repair capabilities. This invention provides an automatic program vulnerability repair method based on generative adversarial networks (GANs). This method combines a self-encoding pre-training mode based on semantically equivalent code with a GAN-based training mode, which can accurately repair vulnerable programs and improve the efficiency of vulnerability repair for developers.

[0005] To achieve the above objectives, the present invention adopts the following technical solution:

[0006] An automatic program vulnerability repair method based on generative adversarial networks includes the following steps:

[0007] 1) Construct the original dataset and expand it into an augmented dataset for pre-training;

[0008] 2) Use an augmented dataset to perform autoencoder pre-training on the codec-based generative model. The goal of the pre-training is to enable the generator to learn the mapping relationship between semantically equivalent codes, including feature extraction from code space to feature space and code reconstruction from feature space to code space.

[0009] 3) Obtain the vulnerability patching code dataset, construct a generative adversarial network based on the generative model obtained by self-encoding pre-training, and use the vulnerability patching code dataset to perform alternating iterative training on the generative adversarial network. After training, the vulnerability patching model is obtained.

[0010] 4) Use vulnerability remediation models to complete actual code vulnerability remediation tasks.

[0011] Further, the steps in step 1) to construct the original dataset include: downloading the open-source code corpus The Stack used to train the large language model; firstly, filtering out the code files written in the Java programming language in The Stack; then filtering out the code files with more than 2000 lexical units; and finally selecting the program developed based on Java SE 8 from the remaining code files to construct the original dataset.

[0012] Further, the step in step 1) to expand the dataset for pre-training includes: designing semantic equivalence transformation rules, applying the semantic equivalence transformation rules to each code file in the original dataset to transform the code, obtaining the transformed code, forming code pairs from each initial code and the corresponding transformed code, and constructing the expanded dataset from all the code pairs.

[0013] Furthermore, the encoder-decoder-based generative model in step 2) includes an encoder, a decoder, and a pointer network module. The encoder consists of a word embedding layer and a bidirectional LSTM layer, and the decoder consists of a word embedding layer and an LSTM layer. For each code pair in the augmented dataset... in This is the initial code. The transformed code is... As input to the generative model, As the output of the generative model, the loss function is set as follows:

[0014]

[0015] Where, θ G represents the parameters of the generative model G, N represents the number of data pairs included in the augmented dataset, and Δ represents the cross-entropy loss function.

[0016] Furthermore, in step 3), the vulnerability remediation code in the dataset contains the vulnerability code c. v and the corresponding repaired code c f The code pairs that make up the code.

[0017] Furthermore, in step 3), constructing a generative adversarial network based on the generative model obtained through autoencoder pre-training refers to using two encoder-decoder-based generative models as two generators G. v and G f Generator G v Generator G is responsible for introducing vulnerabilities into good code. f Responsible for fixing the vulnerable code; on this basis, add two discriminators D v and D f Each discriminator consists of a convolutional network layer and a fully connected layer. Discriminator D v The generator G is responsible for identifying the source of the vulnerable code. v The generated dataset still comes from the vulnerability patch code, and the discriminator D... f The generator G is responsible for identifying the source of good code. f The generated dataset still comes from the vulnerability fix code.

[0018] Furthermore, step 3) involves iteratively training the generative adversarial network on the dataset using the vulnerability patching code, which includes the following steps:

[0019] Each code pair in the vulnerability fix code pair dataset <c v ,c f The input is fed into a generative adversarial network for the following generation operations;

[0020] To generator Gv Enter the repaired code c f Obtain the synthetic vulnerability code G v (c f Set the label to 1 and give code c v Setting the label to 0 results in two new sets of labeled data. <c v ,0> and <G v (c f ),1>, to construct a dataset

[0021] To generator G f Enter the vulnerability code c v Obtain well-synthesized code G f (c v Set the label to 1 and give code c f Setting the label to 0 results in two new sets of labeled data. <c f ,0> and <G f (c v ),1>, to construct a dataset

[0022] Using datasets Training discriminator D v Using datasets Training discriminator D f After training the discriminator several times, training is stopped.

[0023] To generator G v Enter the repaired code c f To generator G f Enter the vulnerability code c v The two generators are trained using the SeqGAN update strategy, and the action reward value is calculated as the generator gradually generates words and finally obtains the complete sequence.

[0024] Based on the calculated action reward value, the generator parameters are updated while the parameters of the discriminator remain unchanged. The generator is trained repeatedly for several times and then training is stopped.

[0025] After the training of the generator and discriminator stops as described above, the first round of complete training is completed; in the second round of complete training, the updated generator G is used. v and G f The vulnerability fix code was used to regenerate the dataset for the discriminator D. v and D f Training dataset and Train the discriminator and generator using the same steps as in the first round; repeat this complete training process for multiple rounds until the discriminator D reaches its optimal value.f The entire training process is stopped when the accuracy rate does not differ from 50% by more than the set value ε or when the number of complete training rounds reaches the set value S.

[0026] Furthermore, the formula for calculating the action reward value in step 3) is as follows:

[0027]

[0028] in, Y represents the action reward value obtained when the generator is G, the discriminator is D, the current state is s, and action a is taken. 1:t-1 This represents a partial sequence consisting of t-1 tokens generated by the generator at step t, and includes the complete sequence Y. 1:T = G(x), where x represents the input to the generator, T represents the total number of steps in the generator, and y t Let y represent the word generated at step t, and let y be the word generated at step t. t ∈V, where V represents a finite-sized vocabulary and N represents the number of Monte Carlo searches. Let n be the nth result of the Monte Carlo search, and have This means using a generator G and based on the currently generated partial sequence Y. 1:t The result of performing N Monte Carlo searches, where the generator performing the Monte Carlo search has the same settings as the generator currently being trained; for generator G v In the above formula, the discriminator D is D v Input x is c f For generator G f In the above formula, the discriminator D is D f Input x is c v .

[0029] Furthermore, the formula for updating the generator parameters in step 3) is as follows:

[0030]

[0031]

[0032] in, This indicates that at step t in the sequence generation process, the generator uses the already generated partial sequence Y... 1:t-1 Generate y t (y t When ∈V), an approximate expected value is obtained through sampling calculation. This represents the gradient calculated by differentiating the parameters θ of the generator.

[0033] Furthermore, step 4) involves using the vulnerability repair model to complete the actual code vulnerability repair task, which includes the following steps: inputting the vulnerability code to be repaired into the vulnerability repair model and marking the lines of code with vulnerabilities. The vulnerability repair model automatically extracts features and outputs a repair patch, and replaces the original lines of code to obtain the repaired code. If there are multiple lines of vulnerability in the code to be repaired, iterative repair is performed line by line until all vulnerable lines of code are repaired.

[0034] Compared with the prior art, the specific advantages of the present invention are as follows:

[0035] 1. This invention designs a series of semantic equivalence transformation rules and introduces a pre-training module. Through autoencoder pre-training, the model learns the many-to-many mapping rules hidden in actual repair from the transformation of semantic equivalence codes.

[0036] 2. This invention proposes an automatic program vulnerability repair method based on generative adversarial networks (GANs) for many-to-many mapping repair patterns. It leverages continuous adversarial interaction between the generator and discriminator, as well as between two generators, to improve model performance. This invention can enhance the efficiency and accuracy of developers in actually fixing code vulnerabilities, thereby accelerating the vulnerability repair process. Attached Figure Description

[0037] Figure 1 This is a flowchart of a vulnerability remediation method based on generative adversarial networks.

[0038] Figure 2 This is a flowchart of the method for constructing an expanded dataset.

[0039] Figure 3 This is a flowchart of autoencoder pre-training of a generative model using an expanded dataset.

[0040] Figure 4 It is a flowchart of obtaining vulnerability fix code, dataset, constructing a generative adversarial network, and performing alternating iterative training.

[0041] Figure 5 It is a flowchart of using a vulnerability remediation model to remediate vulnerabilities in the actual code to be patched. Detailed Implementation

[0042] To make the various technical features, advantages, or effects of the present invention more apparent and understandable, the present invention will be further described below in conjunction with the accompanying drawings.

[0043] This embodiment is based on an automatic program vulnerability repair method using generative adversarial networks, the overall process of which is as follows: Figure 1 As shown, the main steps include:

[0044] 1) Collect good code to build the original dataset Co And expand it into an augmented dataset C for pre-training. a The expansion process follows a series of pre-designed semantic equivalence transformation rules. Specifically, the rules are applied to every code file in the dataset, and successfully transformed code files are collected together with the original code files to form code pairs. Thus, an expanded dataset C is constructed. a In this context, C stands for Code, o stands for Origin, and a stands for Augmentation. This represents the i-th code in the original dataset. Indicates the use of rule r j For the original code The code obtained after transformation. The specific process is as follows: Figure 2 As shown, the specific explanation is as follows:

[0045] 1a) Download The Stack, an open-source code corpus used to train large language models, and go to 1b).

[0046] 1b) First, select the code files written in the Java programming language, then filter out code files with more than 2000 lexical units, and finally select the code developed based on Java SE 8 to construct the original dataset. Where L represents the number of codes in the original dataset, This refers to the i-th code file in the dataset; proceed to 1c.

[0047] 1c) Design semantic equivalence transformation rules R = {r1, r2, ..., r} K}, where R is short for Rule, K represents the number of semantic equivalence transformation rules, and r i This represents the i-th rule. Table 1 shows the names and descriptions of semantically equivalent rules. (Go to 1d).

[0048] Table 1. Names and descriptions of semantic equivalence rules

[0049]

[0050]

[0051] 1d) For the original dataset C o Each code file in Apply rules one by one k Given k = {1, 2, ..., K}, obtain the code after successful transformation. Among them, with For example, it means applying rule ri1 (i1∈{1,2,…K}) in the initial code The resulting transformed code contains l rules {r i1 ,r i2 ,…r il Application successful, proceed to 1e).

[0052] 1e) Convert the code Respectively with the initial code Pairing to form code pairs Thus, an expanded dataset is constructed.

[0053] 2) Using the augmented dataset C a As the training dataset, the codec-based generative model undergoes autoencoder pre-training. The aim is to enable the generator to learn the mapping relationships between semantically equivalent codes, including feature extraction from the code space to the feature space and code reconstruction from the feature space to the code space. The training process is as follows: Figure 3 As shown, the specific explanation is as follows:

[0054] 2a) Construct a generative model G based on an encoder-decoder, which mainly consists of an encoder G e and decoder G d The code consists of two parts: an encoder (e) and a decoder (d). The encoder consists of a word embedding layer and a bidirectional LSTM layer, and the decoder consists of a word embedding layer and an LSTM layer. In addition, it includes a pointer network module (see 2b).

[0055] 2b) For the expanded dataset C a Each pair of codes in The input to the model is The output is The loss function is set as follows:

[0056]

[0057] in, It is short for loss, the subscript auto is short for autoencoder, and θ G N represents the parameters of the generative model G, and N represents the augmented dataset C. a The number of data pairs included, where Δ represents the cross-entropy loss function.

[0058] 3) Obtain the vulnerability patch code pair dataset, and construct a generative adversarial network (GAN) based on the self-encoded pre-trained model. Perform iterative training on this GAN using the vulnerability patch code pair dataset C. g This refers to code containing vulnerabilities. v and the repaired code c f The code for <c v ,c f The dataset consists of two generators: a generative adversarial network (GAN) and a generative model (GAN). G stands for Generative Adversarial Network (GAN), v stands for vulnerability, and f stands for fix. The GAN comprises two encoder-decoder-based generative models that act as generators. Their structure and initial parameters are consistent with the pre-trained generative models. v Generator G is responsible for introducing vulnerabilities into good code. f The code responsible for fixing the vulnerability is also configured with two discriminators, discriminator D. v The generator G is responsible for identifying the source of the vulnerable code. v Generative or real-world, i.e., dataset C g Discriminator D f The generator G is responsible for identifying the source of good code. f Generative or real-world, i.e., dataset C g In this context, G stands for Generator and D stands for Discriminator. During the alternating iterative training process, the generator's ability to introduce and fix vulnerabilities is enhanced both by improving the discriminator's ability to distinguish them and by competing with each other. Finally, training terminates when the number of iterations reaches the target requirement or the discriminator's accuracy approaches 50%.

[0059] Its training process is as follows Figure 4 As shown, the specific explanation is as follows.

[0060] 3a) Download the shared reproducible error dataset compiled from CURE and Recoder workbooks from GitHub. For each vulnerability in the project, there are corresponding vulnerability version codes and fix version codes. (Go to 3b).

[0061] 3b) First, filter out code files with more than 2000 tokens. Then, select code developed based on Java SE 8. Finally, mark the locations of vulnerable lines in all the filtered code. Since the main focus is on fixing single-line vulnerabilities, the marking method is to insert special tokens at the beginning and end of the vulnerable lines. <sos>"and" <eos>This allows for the construction of a dataset for training generative adversarial networks. Where M represents the number of code pairs in the training dataset. This represents the i-th vulnerable code file in the dataset. This indicates the code file of the patched version corresponding to the i-th vulnerable code in the dataset (go to 3c).

[0062] 3c) Construct a generative adversarial network and set up two generators G. v and G f Both are generative models based on encoders and decoders. Their structure and initial parameters are consistent with the structure and parameters of the pre-trained autoencoder generative model G, and two discriminators D are set. f and D v Its structure consists of a convolutional network layer and a fully connected layer (see 3d).

[0063] 3d) Set the total number of training iterations to 0, then proceed to 3e).

[0064] 3e) Determine if the set value S has been reached for the number of complete training iterations. If yes, proceed to 3n); otherwise, proceed to 3f.

[0065] 3f) Set both the discriminator and generator training iterations to 0, then proceed to 3g).

[0066] 3g) Determine whether the number of discriminator training iterations in this complete training round has reached the set value S. d If yes, go to 3j; otherwise, go to 3h.

[0067] 3h) For dataset C g Each code pair in Perform the following operation: Input data into generator G. v Enter good code c f Obtain the synthetic vulnerability code G v (c f The label is set to 1, indicating that the code was generated by the generator, and the code is assigned a value of 1. v Setting the label to 0 indicates that the code comes from the real world, resulting in two new labeled data sets. <c v ,0> and <G v (c f ),1>; Similarly, to the generator G f Enter the vulnerability code c v Obtain well-synthesized code G f (c v Set the label to 1 and give code c f Setting the label to 0 will also result in two new labeled data sets. <c f ,0> and <G f (c v ),1>;Collect newly generated labeled data and Building a dataset Data collection and Building a dataset Where the subscript s represents the current number of discriminator training iterations, go to 3i).

[0068] 3i) Utilizing datasets Training discriminator D v Using datasets Training discriminator D f The discriminator acts as a binary classifier to determine whether a piece of good / vulnerable code originates from a generator or the real world. The optimal parameters of the discriminator are obtained through the following method. and

[0069]

[0070]

[0071] Simultaneously, the discriminator training count is incremented by 1, and the process is repeated until 3g.

[0072] 3j) Determine whether the discriminator's best accuracy in the last training round is no more than 50% below the set value ε. If yes, proceed to 3n); otherwise, proceed to 3k.

[0073] 3k) Determine whether the number of times the generator has been trained in this round of complete training has reached the set value S. g If yes, then go to 3m; otherwise, go to 3l.

[0074] 3l) Generator G v and G f The inputs are respectively and For i = {1, 2, ..., M}, since the gradient of discrete data cannot be backpropagated in traditional generative adversarial networks, the update strategy of SeqGAN is adopted. During the process of the generator gradually generating words and finally obtaining the complete sequence, the action reward value is calculated in the following way:

[0075]

[0076] in, Y represents the action reward value obtained when the generator is G, the discriminator is D, the current state is s, and action a is taken. 1:t-1 This represents a partial sequence consisting of t-1 tokens generated by the generator at step t, and includes the complete sequence Y. 1:T = G(x), where x represents the input to the generator, T represents the total number of steps in the generator (i.e., the length of the complete sequence), and y t Let y represent the word generated at step t, and let y be the word generated at step t. t ∈V, where V represents a finite-sized vocabulary and N represents the number of Monte Carlo searches. Let n be the nth result of the Monte Carlo search, and have This means using a generator G and based on the currently generated partial sequence Y. 1:t The result of performing N Monte Carlo searches, where the generator performing the Monte Carlo search has the same settings as the generator currently being trained. For generator G... v In the above formula, the discriminator D is D v Input x is For generator G f In the above formula, the discriminator D is D f Input x is After obtaining the complete sequence, update the generator parameters as follows:

[0077]

[0078]

[0079] in, This indicates that at step t in the sequence generation process, the generator uses the already generated partial sequence Y... 1:t-1 Generate y t (y t When ∈V), an approximate expected value is obtained through sampling calculation. This represents calculating the gradient by taking the derivative with respect to the generator's parameters θ. Note that the discriminator's parameters need to remain constant during the generator parameter update process. After the generator parameters are updated, the generator's training iterations are incremented by 1 (up to 3k).

[0080] 3m) Complete training count plus 1, then proceed to 3e).

[0081] 3n) Alternating iterative training ends

[0082] 4) Use the trained vulnerability remediation model to complete the actual code vulnerability remediation task. The process is as follows: Figure 5 As shown, the specific explanation is as follows:

[0083] 4a) This method can be directly applied to programs with known vulnerability locations. In practical applications, if the specific number and location of vulnerabilities in the code to be repaired are unclear, it can be obtained by using a relatively mature vulnerability location tool such as DeepFL. (See 4b).

[0084] 4b) Select a line of code that contains a vulnerability that has not been fixed, and add the token "" to the beginning and end of the line. <sos>"and" <eos>"Add annotations, then go to 4c."

[0085] 4c) Select the trained generator G f As a vulnerability remediation model, it takes the labeled code to be patched as input, outputs a patch sequence, and then proceeds to 4D.

[0086] 4d) Replace the original marked vulnerability line with the generated patch sequence to obtain the patched code, then proceed to 4e).

[0087] 4e) Determine if any unpatched lines of code remain after the fix. If yes, proceed to 4b); otherwise, proceed to 4f.

[0088] 4f) indicates that all vulnerable lines of code have been patched by the model, and the final patched code is output.

[0089] Although the present invention has been disclosed above with reference to embodiments, it is not intended to limit the present invention. Appropriate modifications or equivalent substitutions made by those skilled in the art to the technical solutions of the present invention should be covered within the protection scope of the present invention, which is defined by the claims.< / eos> < / sos> < / eos> < / sos>

Claims

1. A method for automatically repairing program vulnerabilities based on generative adversarial networks, characterized in that, The steps include: 1) Construct the original dataset, design semantic equivalence transformation rules, apply the semantic equivalence transformation rules to each code file in the original dataset to transform the code, obtain the transformed code, and form code pairs by each initial code and the corresponding transformed code. Construct an augmented dataset for pre-training from all the code pairs. 2) Use an augmented dataset to perform autoencoder pre-training on the codec-based generative model. The goal of the pre-training is to enable the generator to learn the mapping relationship between semantically equivalent codes, including feature extraction from code space to feature space and code reconstruction from feature space to code space. 3) Obtain the vulnerability patch code pair dataset, which contains vulnerability code pairs. and the corresponding repaired code The code pairs are used to construct a generative adversarial network (GAN) based on the generative model obtained through autoencoder pre-training. The GAN includes generators, each based on a generative model derived from an encoder-decoder. and generator generator Generator is responsible for introducing vulnerabilities into good code. Responsible for fixing the vulnerable code; on this basis, two discriminators are added. and Each discriminator consists of a convolutional network layer and a fully connected layer. The generator is responsible for identifying the source of the vulnerable code. The generated dataset still comes from the vulnerability patch code, and the discriminator... The generator is responsible for identifying the source of good code. The generated dataset still comes from the vulnerability patch code pair dataset; the generative adversarial network is trained iteratively using the vulnerability patch code pair dataset, so that the generative adversarial network learns the one-to-many or many-to-many mapping relationship between the vulnerability code and the corresponding patch code. After training, the vulnerability patching model is obtained. 4) Use vulnerability remediation models to complete actual code vulnerability remediation tasks.

2. The automatic program vulnerability repair method as described in claim 1, characterized in that, Step 1) involves constructing the original dataset by downloading The Stack, an open-source code corpus used to train large language models. First, the code files written in Java are selected from The Stack. Then, code files with more than 2,000 lexical units are filtered out. Finally, programs developed based on Java SE 8 are selected from the remaining code files to construct the original dataset.

3. The automatic program vulnerability repair method as described in claim 1, characterized in that, Step 2) involves a codec-based generative model comprising an encoder, a decoder, and a pointer network module. The encoder consists of a word embedding layer and a bidirectional LSTM layer, while the decoder consists of a word embedding layer and an LSTM layer. For each code pair in the augmented dataset... ,in This is the initial code. The transformed code is... As input to the generative model, As the output of the generative model, the loss function is set as follows: , in, Representing generative models The parameters, This indicates the number of data pairs contained in the expanded dataset. This represents the cross-entropy loss function.

4. The automatic program vulnerability repair method as described in claim 1, characterized in that, Step 3) involves iteratively training the generative adversarial network on the dataset using the vulnerability patching code, including the following steps: Each code pair in the vulnerability fix code pair dataset The input is fed into a generative adversarial network to perform the following generation operations; To the generator Enter the repaired code Obtain synthetic vulnerability code Set the label to 1 and provide the code. Setting the label to 0 results in two new sets of labeled data. and Build into a dataset ; To the generator Obtain well-synthesized code Set the label to 1 and provide the code. Setting the label to 0 results in two new sets of labeled data. and Build into a dataset ; Using datasets Training the discriminator Using datasets Training the discriminator After training the discriminator several times, training is stopped. To the generator Enter the repaired code To the generator Enter the vulnerability code The two generators are trained using the SeqGAN update strategy, and the action reward value is calculated as the generator gradually generates words and finally obtains the complete sequence. Based on the calculated action reward value, the generator parameters are updated while the parameters of the discriminator remain unchanged. The generator is trained repeatedly for several times and then training is stopped. After the training of the generator and discriminator stops, the first round of training is complete; in the second round of training, the updated generator is used. and Regenerate the discriminator from the dataset using the vulnerability fix code. and Training dataset and Train the discriminator and generator using the same steps as in the first round; repeat this process for multiple rounds of complete training until the discriminator... The accuracy rate does not differ from 50% by more than the set value. Or the complete training rounds reach the set value. If this happens, stop the entire training process.

5. The automatic program vulnerability repair method as described in claim 4, characterized in that, The formula for calculating the action reward value in step 3) is as follows: in, Indicates when the generator is The discriminator is The current state is And take action The action reward value obtained at that time This indicates that in the generator at the th... The common steps already generated A partial sequence consisting of 100 lexical units, and a complete sequence. , This represents the input to the generator. This represents the total number of steps in the generator. Indicates the first The word units generated at each step, and have , This represents a vocabulary of finite size. This indicates the number of Monte Carlo searches. The Monte Carlo search indicates the first There are results, and there are , indicating the use of generators And based on the currently generated partial sequence The result of performing N Monte Carlo searches, where the generator performing the Monte Carlo search has the same settings as the generator currently being trained; for the generator In the above formula, the discriminator for ,enter for For generators In the above formula, the discriminator for ,enter for .

6. The automatic program vulnerability repair method as described in claim 5, characterized in that, The formula for updating the generator parameters in step 3) is as follows: in, This indicates that the generator is at the th position in the generated sequence. During the step, based on the already generated partial sequence generate At that time, an approximate expected value is obtained through sampling calculation. Represents parameters about the generator Calculate the gradient by taking the derivative.

7. The automatic program vulnerability repair method as described in claim 1, characterized in that, Step 4) involves using the vulnerability repair model to complete the actual code vulnerability repair task, which includes the following steps: inputting the vulnerability code to be repaired into the vulnerability repair model and marking the lines of code with vulnerabilities. The vulnerability repair model automatically extracts features and outputs a repair patch, and replaces the original lines of code to obtain the repaired code. If there are multiple lines of vulnerability in the code to be repaired, iterative repair is performed line by line until all vulnerable lines of code are repaired.

Citation Information

Patent Citations

  • Cross-language compiler vulnerability mining method and device based on transfer learning

    CN115758379A

  • Pre-training vulnerability repairing method based on adversarial transfer learning

    CN117113359A