Software automatic repair method and system, electronic device and storage medium
By acquiring the syntactic features of software defect code and a trained patch template generation model, a language-compliant patch template is generated, solving the problem of insufficient generalization ability of patch templates in existing technologies and achieving efficient repair adaptable to different languages.
Patent Information
- Application Number
- CN202110904041.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-08-06
- Publication Date
- 2025-10-21
- Estimated Expiration
- 2041-08-06
Smart Images

Figure CN115934147B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of computers, and in particular to a software automatic repair method, system, electronic device, and storage medium. Background Art
[0002] Automatic Bug Fixing (ABF), strictly speaking, refers to a program that fixes bugs in target software by automatically generating correct repair packages without the need for human intervention.
[0003] Traditional software automatic repair techniques primarily rely on mining pre-existing patch templates from code repositories, performing greedy and random searches based on defective code, and generating software patches that pass test cases. Due to the limited number of pre-existing patch templates and the diverse nature of programming languages, these templates have limited generalization capabilities and cannot fully adapt to all defective code. This makes it particularly difficult to find an appropriate patch based on pre-existing patch templates when addressing new software defects, making it difficult to automatically repair the defective code. Summary of the Invention
[0004] The main purpose of the embodiments of the present application is to propose a software automatic repair method, system, electronic device and storage medium, which can generate highly adaptable patches for defective codes in different program development languages and improve the repair capability of software automatic repair.
[0005] To achieve the above-mentioned purpose, an embodiment of the present application provides a method for automatic software repair, including: obtaining software defect code; generating a patch template that conforms to the grammar of the language used by the software defect code based on the grammatical features of the software defect code and a trained patch template generation model; filling in the patch template to generate a patch for the software defect code; and repairing the software defect code with the patch.
[0006] To achieve the above-mentioned objectives, an embodiment of the present application provides a software automatic repair system, including: an acquisition module for acquiring software defect code; a template generation module for generating a patch template that conforms to the grammar of the language used by the software defect code based on the grammatical features of the software defect code and a trained patch template generation model; a patch generation module for filling in the patch template and generating a patch for the software defect code; and a repair module for repairing the software defect code with the patch.
[0007] To achieve the above-mentioned purpose, an embodiment of the present application also provides an electronic device, comprising: at least one processor; a memory communicatively connected to the at least one processor; the memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the above-mentioned software automatic repair method.
[0008] To achieve the above-mentioned purpose, an embodiment of the present application further provides a computer-readable storage medium storing a computer program, which implements the above-mentioned software automatic repair method when executed by a processor.
[0009] By generating a model based on the grammatical features of the software defect code and the trained patch template, a patch template that conforms to the grammar of the language used by the software defect code is generated, the patch template is filled in, and a patch for the software defect code is generated. This can make the generated patch consistent with the grammar of the software defect code. Regardless of the language the software defect code is written in, an adapted patch can be generated for repair, thereby improving the repair capability of the software automatic repair. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] One or more embodiments are exemplarily illustrated by pictures in the corresponding drawings. These exemplifications do not constitute limitations on the embodiments. Elements with the same reference numerals in the drawings are represented as similar elements. Unless otherwise stated, the figures in the drawings do not constitute proportional limitations.
[0011] Figure 1 is a flow chart of a software automatic repair method provided according to an embodiment of the present invention;
[0012] Figure 2 is an extended grammar rule for automatic software repair according to an embodiment of the present invention;
[0013] Figure 3 is a schematic diagram of a patch template generation model provided according to an embodiment of the present invention;
[0014] Figure 4 is a schematic diagram of a proposer / decision maker structure provided according to an embodiment of the present invention;
[0015] Figure 5 This is a schematic diagram of an implementation of a software automatic repair method provided by an embodiment of the present invention;
[0016] Figure 6 2. It is a schematic diagram of a software automatic repair system provided according to an embodiment of the present invention;
[0017] Figure 7 FIG. 1 is a structural diagram of an electronic device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0018] In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, each embodiment of the present application will be described in detail below with reference to the accompanying drawings. However, it will be understood by those skilled in the art that in each embodiment of the present application, many technical details are proposed to enable the reader to better understand the present application. However, even without these technical details and various changes and modifications based on the following embodiments, the technical solutions claimed in the present application can be implemented. The division of the following embodiments is for convenience of description and should not constitute any limitation on the specific implementation of the present application. The various embodiments can be combined and referenced with each other under the premise of no contradiction.
[0019] The embodiment of the present invention relates to a software automatic repair method, such as Figure 1 As shown, the method specifically includes:
[0020] Step 101, obtaining a software defect code;
[0021] Step 102: Generate a patch template that conforms to the grammar of the language used by the software defect code based on the grammatical features of the software defect code and the trained patch template generation model;
[0022] Step 103: Fill in the patch template to generate a patch for the software defect code;
[0023] Step 104: Repair the software defect code with a patch.
[0024] The automatic software repair method of this embodiment is applied to electronic devices, such as computers, mobile phones, and tablets, by developing an automatic software repair system to achieve automatic software repair. This method generates a patch template that conforms to the syntax of the language used by the software defect code based on the grammatical features of the software defect code and a trained patch template generation model. The patch template is then filled in to generate a patch for the software defect code. This ensures that the generated patch conforms to the syntax of the software defect code. Regardless of the language in which the software defect code is written, an adapted patch can be generated to repair the defect, thereby improving the repair capabilities of the automatic software repair system.
[0025] The following is a detailed description of the implementation details of the software automatic repair method of this embodiment. The following content is only provided for ease of understanding and is not necessary for implementing this solution.
[0026] In step 101, the electronic device obtains a software defect code, that is, a fault source code.
[0027] Specifically, the software defect code can be obtained by the electronic device based on the code test result, or can be obtained by the electronic device reading the input content of the technician.
[0028] In step 102, the electronic device generates a patch template that conforms to the grammar of the language used by the software defect code based on the grammatical features of the software defect code and the trained patch template generation model.
[0029] Specifically, the electronic device can use basic deep learning technology to self-learn the syntax of the software defect code from the acquired software defect code, call the trained patch template generation model, and generate a patch template that conforms to the syntax of the software defect code.
[0030] In one example, before step 102, the electronic device also determines the defect function in the software defect code based on the defect location technology; parses the defect function to generate a first abstract syntax tree (Abstract Syntax Tree, referred to as "AST"); obtains the pre-order traversal sequence of the first abstract syntax tree, the label vector of each node and the first abstract syntax graph according to the feature vector, label and adjacency matrix of each node in the first abstract syntax tree; obtains the extended program syntax according to the preset syntax rules and syntax features; calls the word embedding method according to the rule sequence of the extended program syntax to generate the embedding vector of each rule sequence and the program patch; generates the encoding vector of each rule sequence according to the encoding sequence number of the rule sequence; generates a second abstract syntax tree according to the program patch; obtains the second abstract syntax graph and the second abstract syntax tree path according to the second abstract syntax tree.
[0031] The patch template generation model includes: a code encoder, a patch encoder, an abstract syntax tree path encoder, and an extended syntax decoder. Generating a patch template for software defect code specifically includes: inputting a pre-order traversal sequence, each node label vector, and a first abstract syntax graph into the code encoder to obtain a code encoding result; inputting the code encoding result, each rule sequence embedding vector, each rule sequence encoding vector, and a second abstract syntax graph into the patch encoder to obtain a patch encoding result; inputting the code encoding result, the code encoding result, and the second abstract syntax tree path into the abstract syntax tree path encoder to obtain an abstract syntax tree path encoding result; inputting the abstract syntax tree path encoding result into the extended syntax decoder to select the optimal rule sequence; and generating a patch template based on the optimal rule sequence.
[0032] The optimal rule sequence is not a complete patch sequence, but rather a segment within the complete patch sequence. The patch template generation model iteratively extends this segment through the code encoder, patch encoder, abstract syntax tree path encoder, and extended syntax decoder until the segment can no longer be extended or reaches a preset length. The iterations are then terminated, resulting in a complete patch sequence. The patch template generation model generates patch templates based on the generated complete patch sequences.
[0033] Specifically, in each process of generating a complete patch sequence, the initial input of the patch encoder can be a preset string, which is used to mark the beginning of the patch sequence. After the extended syntax decoder generates the first sequence segment, this first sequence segment is input into the patch encoder during the iterative operation. After the iterative operation, this first sequence segment is extended and expanded, and a new sequence segment is connected to the first sequence segment to obtain an extended sequence segment. The extended sequence segment is further iterated to obtain a complete patch sequence.
[0034] Among them, each node in the first abstract syntax tree represents a string, and its feature vector is obtained by processing the string through word embedding technology and vectorizing the string. The pre-order traversal sequence is obtained by pre-order traversal of the first abstract syntax tree, and each element in the sequence is a feature vector. The present application also marks a label for each first abstract syntax tree node, and the label is used to characterize the positional relationship between the string represented by the node and the defective code line where the defect is located, and has four types: 1. The node is located inside the defective code line; 2. The node is located in the previous line of the defective code line; 3. The node is located in the next line of the defective code line; 4. The node is located in other lines. The present application converts the label of each first abstract syntax tree node into a node label vector through word embedding technology, and the sequence of node label vectors and the pre-order traversal sequence have the same node order. Since the pre-order traversal sequence and the node label vectors do not contain the structural information between the nodes on the first abstract syntax tree, the present application also processes the first abstract syntax tree to obtain the first abstract syntax graph, connecting the node only to its closest left neighbor, and does not add other additional edges. The storage form of the first abstract syntax graph is the adjacency matrix of each node.
[0035] The present application also uses stored preset grammar rules, such as grammar rules for modification operations and grammar (also called native grammar) information of software defect codes, to obtain extended program grammar. And the rule sequence obtained by parsing the grammar is called to generate each rule sequence embedding vector and program patch. The rule sequence embedding vector is the rule information encoded using the rule ID. These rules are represented as real number vectors by the embedding method. In some software automatic repair methods, the grammar rules (i.e., rule sequences) are regarded as atomic tags using rule definition encoding. In fact, this will result in the loss of information about some rule contents. In order to alleviate this problem, we use the rule definition encoding to enhance the representation of the rules and encode the rule content into a vector. Generate a rule sequence encoding vector based on the encoding sequence number in the rule sequence. Construct an abstract syntax tree (i.e., a second abstract syntax tree) of a partial patch template by repairing the patch, and obtain a second abstract syntax graph and a partial abstract syntax tree path (i.e., a second abstract syntax tree path) using the partial abstract syntax tree.
[0036] Figure 2 The specific composition of the extended grammar of the present invention is given. The present invention is not designed for a specific programming language, so the native programming language is called HL (host language). NTS represents the original non-terminal symbol in the HL grammar, <hlstatement>represents the non-terminal symbol representing an expression in the HL grammar, <hlidentifier>Represents the terminal symbol in the HL grammar. The extended grammar of the present invention includes the following 6 rules: Rule 1 stipulates that a patch contains one or more modification operations. Rule 2 stipulates that modification operations have two types, add and / or change. Rule 3 declares the syntax of the add type modification operation. The add type modification operation will insert a newly generated expression before the defective line code, that is, <hlidentifier>It can be expanded into an expression by HL syntax or copied from the original defective function. Rule 4 declares the syntax of the change type modification operation. The change type modification operation replaces a part of the subtree of the defective code. The modification operation contains two parameters. The first parameter is the position of the replaced subtree, which is represented by its position in the sequence of the pre-order traversal of the abstract syntax tree. The second parameter represents the newly generated subtree, which is used to replace the original defective subtree. The new subtree and the old subtree must have the same root node to ensure the grammatical correctness of the replaced program. In both types of modification operations, the model needs to generate a new abstract syntax tree. In actual situations, although there will be different degrees of modification in the program before and after the replacement, a large part of the program has the same parts. Taking advantage of this property, the present invention proposes a copy operation that can copy an expression of the same type from the defective function when generating a new abstract syntax tree. Rule 5 declares the syntax of this operation, which can be used to expand any non-terminal symbol in the HL syntax. The copy operation has a parameter that indicates the position of the abstract syntax tree to be copied. Its position in the pre-order traversal sequence is also used to indicate the position of the syntax tree. The root node of the copied subtree also needs to have the same node type as the non-terminal symbol to ensure the grammatical correctness after copying. Rule 6 states that the terminal symbol can be converted into a special placeholder in the patch template. When the model determines that a terminal symbol should be expanded to a project-specific identifier, the placeholder can be used to replace its position in the patch. At the same time, the terminal symbol can also be replaced by a common identifier in the vocabulary. In the implementation of the present invention, identifiers that appear more than 100 times in the training set are added to the vocabulary.
[0037] Figure 3 Figure 2 shows a schematic diagram of the patch template generation model constructed using the neural network algorithm employed in the present invention. The patch template generation model comprises four components: a code encoder, which processes the input abstract syntax tree of the defect function; an AST encoder, which processes the abstract syntax tree of a partially generated patch; a tree path encoder, which processes the abstract syntax tree path from the root node to the expanded node; and an expanded syntax decoder, which outputs the probability of each grammatical rule being selected based on the input of the hidden layer.
[0038] In one example, a code encoder includes: a first self-attention layer, a first gating layer and a first graph convolution layer; a pre-order traversal sequence, each node label vector and a first abstract syntax graph are input into the code encoder to obtain a code encoding result, including: obtaining a position feature vector of each node according to the pre-order traversal sequence; obtaining a first query vector, a first key value vector and a first weight vector according to the pre-order traversal sequence and the position feature vector; inputting the first query vector, the first key value vector and the first weight vector into the first self-attention layer to obtain a first self-attention result; inputting the label vectors of each node of the first self-attention result into the first gating layer to obtain a first gating result; inputting the first gating result and the first abstract syntax graph into the first graph convolution layer to obtain a first graph convolution result; assigning the first graph convolution result to the first query vector, the first key value vector and the first weight vector, and iteratively calculating the first self-attention layer, the first gating layer and the first graph convolution layer to obtain a code encoding result.
[0039] Among them, Figure 3 As shown, the first self-attention layer can be composed of self-attention neurons, which first need to use a position feature vector to represent the position information of each node. The calculation formula of the position feature vector is as follows:
[0040]
[0041]
[0042] Where pos = i + step, step is the dimension of the preset word embedding vector, the word embedding vector is the vector obtained after the feature vector is processed by word embedding, i represents that the word is the i-th member of its sequence, and j represents the value of the j-th dimension of the word embedding vector. The code encoder fuses the position feature vector with the three input vectors respectively to obtain the first query vector (Q), the first key value vector (K), and the first weight vector (V). In this embodiment, the position feature vector is fused with the same input vector to obtain Q, K, and V with the same values.
[0043] The self-attention neuron calculates the input Q, K, and V based on the multi-head attention mechanism. The calculation process of a single head is as follows:
[0044]
[0045] Among them, d k = d / H, where d is the dimension of the word embedding vector, H is the number of self-attention neurons, and T is the transpose operation. The result calculated by the self-attention layer is the first self-attention result.
[0046] The code encoder inputs the node label vectors of the first self-attention result into the first gating layer, where the first gating layer can be composed of gated neurons. The gated neurons have three input parameters: the query vector q and two vectors c1 and c2, where q and c1 are assigned by the first self-attention result and c2 is assigned by the node label vector. The calculation process of the gated neuron is as follows:
[0047]
[0048]
[0049]
[0050] Where i represents that the word is the i-th member of the sequence, and α is the weight calculated for the corresponding vector. Represents the feature vector of c1 and c2 after calculation by the first fully connected layer, Represents the feature vector of c1 and c2 after calculation by the second fully connected layer.
[0051] The code encoder inputs the first gating result and the first abstract syntax graph into the first graph convolution layer to obtain the first graph convolution result, wherein the first graph convolution layer can be composed of graph convolution neurons. The calculation process of the neuron can be expressed as:
[0052]
[0053] Where A is the regularized adjacency matrix of the first abstract syntax graph G, r s With r p Represents any node in graph G, u p Represents the eigenvector of the corresponding node. The initial value of the eigenvector corresponding to the node is the output of the previous neuron, that is, the h of the corresponding node i Vector. W g It is the weight matrix used in the graph convolutional network that can be learned by the neural network, and its initial value is arbitrary.
[0054] The code encoder assigns the first graph convolution result to Q, K, and V, and iteratively calculates the first self-attention layer, the first gating layer, and the first graph convolution layer to obtain the code encoding result. The code encoder can form a group with one first self-attention layer, one first gating layer, and one first graph convolution layer, and perform N1 iterative calculations by setting N1 groups.
[0055] In one example, a patch encoder includes: a second self-attention layer, a second gating layer, a natural language attention layer, and a second graph convolution layer; the code encoding result and each rule sequence embedding vector, each rule sequence encoding vector, and the second abstract syntax graph are input into the patch encoder to obtain a patch encoding result, including: obtaining the position feature vector of each node according to the pre-order traversal sequence; obtaining the second query vector, the second key value vector, and the second weight vector according to the rule sequence embedding vector and the position feature vector; inputting the second query vector, the second key value vector, and the second weight vector into the second self-attention layer to obtain the first Second self-attention result; input the second self-attention result and each rule sequence encoding vector into the second gating layer to obtain the second gating result; input the code encoding result and the second gating result into the natural language attention layer to obtain the natural language attention result; input the first natural language attention result and the second abstract syntax graph into the second graph convolution layer to obtain the second graph convolution result; assign the second graph convolution result to the second query vector, the second key value vector and the second weight vector, and iteratively calculate the second self-attention layer, the second gating layer, the natural language attention layer and the second graph convolution layer to obtain the patch encoding result.
[0056] Among them, the position feature vector calculated by the patch encoder is the same as the position feature vector in the code encoder, and the calculation process of the second query vector, the second key vector and the second weight vector are the same as the first query vector, the first key vector and the first weight vector respectively. It is only necessary to change the pre-order traversal sequence in the calculation process to a regular sequence embedding vector. The second self-attention layer can be composed of the same self-attention neurons as the code encoder. The second gating layer can be composed of the same gating neurons as the code encoder. The natural language attention layer can be composed of the same self-attention neurons as the code encoder, and the second graph convolution layer can be composed of the same first graph convolution neurons as the code encoder.
[0057] In one example, between the second gating layer and the natural language attention layer, there is a grammatical rule sequence r1, r2, ..., r P Used to generate a partial AST in the decoding step, where P represents the length of the sequence. These grammar rules can also be represented as real number vectors r1, r2, ..., r by the embedding method. P , for grammar rule i: a-->b1...b K , where a is the parent node, and b1...b K are the predecessor nodes. They can be terminals or non-terminals. Index i is the ID of the rule. We use a fully connected approach by encoding the rule content into a vector r (c) . Among them, the input is vector a b1b K In particular, the sequence is also padded to the maximum length. Then, the rule defines the feature y1 (rule) ,...,y P (rule) Computed by another fully connected layer.
[0058]
[0059] where r i It is a rule i Table query embedding, r i (c) is the content encoding rule representation, and we again encode the predecessor node information a. After the step, layer normalization is also performed.
[0060] Among them, the patch encoder can be a group of a second self-attention layer, a second gating layer, a natural language attention layer and a second graph convolution layer, and N2 iterative calculations can be achieved by setting N2 groups.
[0061] In one example, an abstract syntax tree path encoder includes: a patch attention layer, a code attention layer, and a fully connected layer; the code encoding result, the code encoding result, and the second abstract syntax tree path are input into the abstract syntax tree path encoder to obtain the abstract syntax tree path encoding result, including: inputting the patch encoding result and the second abstract syntax tree path into the patch attention layer to obtain the patch attention result; inputting the code encoding result and the patch attention result into the code attention layer to obtain the code attention result; inputting the code attention result into the fully connected layer, assigning the output result of the fully connected layer to the second abstract syntax tree path, iteratively calculating the patch attention layer, the code attention layer, and the fully connected layer to obtain the abstract syntax tree path encoding result.
[0062] The patch attention layer can be composed of patch attention neurons, which are the same as the self-attention neurons of the code encoder. The code attention layer can be composed of code attention neurons, which are the same as the self-attention neurons of the code encoder. The abstract syntax tree path encoder can be composed of a patch attention neuron, a code attention neuron, and a fully connected neuron as a group. By setting N3 groups, N3 iterative calculations can be achieved.
[0063] The abstract syntax tree path encoder combines the generated patch information with the defect code description and combines it with the corresponding abstract syntax tree path information. The abstract syntax tree path refers to a sequence of deep traversals from the root node to the syntax tree node to be expanded. Similar to the abstract syntax tree reader, in the abstract syntax tree path encoder we use multiple modules with the same structure (each module contains multiple sub-layers). Residual connections and layer normalization are used between each sub-layer. The abstract syntax tree path encoder takes the non-terminal node to be expanded as the query input. The query node is represented as a path from the root node to the node to be expanded. We represent the nodes in the path as real numbers, and then apply a fully connected layer to these vectors, the output of which is q i (path) We then apply two attention sub-layers with the same structure as the code encoder to combine the outputs of the code encoder and the patch encoder.
[0064] Apply the abstract syntax tree attention sub-layer on the output of the patch encoder and extract features. In this layer, Q is obtained based on the query q i (path) K and V are calculated based on the code features output by the code encoder. The abstract syntax tree path encoder further incorporates features from the input description into the decoder. This integration is also achieved through an attention sublayer, where Q is calculated from the output features of the abstract syntax tree attention sublayer; and K and V are calculated from the output of the code encoder. Finally, two fully connected layers are used, the first of which uses the GELU activation function to extract features for prediction.
[0065] In one example, an extended syntax decoder includes: a native rule proposer, a copy rule proposer, a defect subtree proposer and a decision maker; the abstract syntax tree path encoding result is input into the extended syntax decoder, the optimal rule sequence is selected, and a patch template is generated, including: inputting the abstract syntax tree path encoding result into the native rule proposer, the copy rule proposer and the defect subtree proposer respectively to obtain the extended rule selection probability; wherein, the native rule proposer is used to generate the selection probability of a predefined extended rule, the copy rule proposer is used to select a subtree, and the defect subtree proposer selects a subtree position with a defect; the abstract syntax tree path encoding result, the extended rule selection probability, the subtree of the first abstract syntax tree and the subtree position with a defect are input into the decision maker to obtain the probability of the optimal rule; according to the probability of the optimal rule, the optimal rule sequence is obtained.
[0066] like Figure 4 The figure shows the structure of the proposer / decision maker proposed in this invention. When expanding the abstract syntax tree, each proposer will provide multiple optional grammar rules and give the estimated probability p of each grammar rule. For example, for proposer 1, there may be options 1-1, 1-2, and 1-m, corresponding to p 1-1 、p 1-2 to p 1-m , and so on, the proposer N can choose Nt, choose N-2 to choose Nm, corresponding to p N-N 、p N-2 to p N-t Based on the node type of the expanded syntax tree, the decision maker needs to give a probability q for each proposer. For example, proposer 1 can have a corresponding probability q1, and proposer N can have a corresponding probability q N The probability of each grammatical rule is finally calculated by p*q.
[0067] Each proposer has a logic component that resets the probability of the corresponding grammar rule to 0 for rules that are included in the proposer but cannot be used to expand the current grammar tree node (for example, the left node of the grammar rule is of a different type than the current node).
[0068] There is also a similar logical component in the decider. For those proposers that cannot be used for the corresponding node, the logical component will also reset the corresponding probability to 0, so that the final probability of the grammatical rule proposed by the proposer is 0, which also ensures the grammatical correctness of the patch generated by the present invention.
[0069] The implementation of the present invention includes three proposers and a decision maker. The first proposer is the native rule proposer (RulePredictor), which is used to estimate the selection probability of a predefined extension rule. The second proposer is the copy rule proposer, which is used to select a suitable subtree in the subtree copy operation. The third proposer is the subtree proposer, which is used to select the subtree position with defects when expanding the change node. Finally, the decision maker outputs the selection probabilities of the three proposers respectively, combines them with the probabilities generated by their respective proposers, and outputs the probability of the optimal grammatical rule. The present invention iteratively generates a complete rule sequence starting from a special starting rule.
[0070] Compared with code generation, it is difficult for the decoder to directly and simply migrate to the generation of modification operation sequences. First, there are some special non-terminal symbols in the extended grammar with different extension rules. At the same time, the modification operation needs to meet some grammatical restrictions. These existing decoders have no way to achieve. Therefore, the present invention proposes a proposer / decider structure to estimate the probability of each step of the extension rule. The function of the proposer is to provide a set of different available rules, and each rule has its corresponding probability of being selected. The function of the decision maker is to provide the selection probability of different proposers. For the options provided by the illegal proposer, the decision maker will modify its probability to 0. The probability of the final grammatical rule is obtained by multiplying the probability provided by the decision maker and the probability of the proposer.
[0071] Code generation is regarded as the expansion process of the code abstract syntax tree. The probability of the next grammar rule selection is estimated based on the generated partial abstract syntax tree. The use of this decoder can ensure that the generated patch can satisfy the grammar of the corresponding language.
[0072] In one example, the copy rule proposer is further used to generate a copy operation code according to the position of the subtree corresponding to the defect function after selecting a subtree of the first abstract syntax tree; the defect subtree proposer is further used to generate a defect subtree code according to the position of the subtree with a defect after selecting the position of the subtree with a defect; the abstract syntax tree path encoding result, the extended rule selection probability, the subtree of the first abstract syntax tree and the subtree position with a defect are input into the decision maker, including: inputting the abstract syntax tree path encoding result, the extended rule selection probability, the copy operation code, and the defect subtree code into the decision maker.
[0073] In step 103, the electronic device fills a patch template with the identifier of the software defect code to generate a patch for the software defect code.
[0074] Specifically, this application addresses the deficiency of some software automatic repair technologies that cannot generate project-specific identifiers, and proposes a technology that uses placeholders in patch templates and then fills in patch templates. The direct way for some software automatic repair technologies to solve the problem of being unable to generate project-specific identifiers is to let the neural network select appropriate identifiers from the input context, but this requires the context of the entire software defect code to be used as the input of the model, and currently no neural network can handle such a large input. The present invention proposes to generate some specific placeholders in the patch to replace these project-specific identifiers. When the patch is applied to the defective program, these placeholders will be instantiated as corresponding identifiers. By considering the type constraints in the program, etc., the number of available identifiers for a position will not be too many, so the placeholders will not have much impact on the grammatical content of the patch template.
[0075] In step 104, the electronic device repairs the software defect code using a patch.
[0076] Specifically, the present invention proposes to use an extended syntax-guided decoder to generate modification operations rather than complete repaired code. The present invention draws on the syntax-guided decoder in the field of automatic code generation. At the same time, in order to address the problem of repeatedly generating complex expressions, the present invention converts patches into a sequence of predefined modification operations, which can more concisely represent modifications to small parts of the program. In order to enable the decoder to generate corresponding modification operations according to the grammar, it is noted that the sequence of modification operations can also be described by a set of extended grammars that include the original language grammar. Therefore, the present invention provides an extended grammar suitable for modification operations based on the native grammar, so that software defective code can be repaired with patches.
[0077] like Figure 5 As shown, the present invention provides a software automatic repair method based on extended grammar rules. This method converts the software method to be repaired into an abstract syntax tree expression, simultaneously generates a patch template using a method for generating a sequence of grammar rules, and finally fills the patch template accordingly, thereby obtaining a repair solution for the software to be repaired, helping developers repair software defects that arise during the development process. To address the shortcomings of existing deep learning-based software automatic repair technologies, which may generate incorrect grammar and a less concise patch expression, the present invention proposes using an extended grammar-guided decoder to generate modification operations rather than the complete repaired code. Drawing on grammar-guided decoders in the field of automatic code generation, the present invention views code generation as an extension of the code abstract syntax tree. Based on the generated partial abstract syntax tree, it estimates the probability of the next grammar rule selection. Using this decoder, the generated patch can ensure that the generated patch satisfies the grammar of the corresponding language. Furthermore, to address the problem of repeatedly generating complex expressions, the present invention converts the patch into a sequence of predefined modification operations, which can more concisely represent modifications to small parts of the program. To enable the decoder to generate corresponding modification operations based on the grammar, it is noted that the sequence of modification operations can also be described by a set of extended grammars that contain the original language grammar. Therefore, the present invention provides an extended grammar suitable for modification operations based on the original grammar.
[0078] In an example, the first abstract syntax tree may be traversed in in-order or post-order, and each vector sequence may be traversed in a corresponding order.
[0079] In one example, the software automatic repair method of the present invention is used to perform a defect repair experiment, and specifically has a high repair rate.
[0080] (1) First, we need to obtain the training set required for training the model. We crawled the Java syntax commit records created between March 2011 and March 2018 from the Github code repository and used keyword filtering to filter out the code commit records related to the fix and the commit records that only modified a single code snippet. The final training dataset contains a total of 103,585 training data points, of which 80% are used as the training set and 20% as the validation set.
[0081] (2) The experimental verification of this invention uses the commonly used defect dataset Defects4J v1.2, which contains 395 defects, and Defects4J v2.0, which contains an additional 420 defects. The defect localization method used in the experiment is the Ochiai algorithm based on test case coverage, which is commonly used in software automatic repair research. Each defect is given 5 hours of patch verification time.
[0082] (3) The following table lists the repair results of the present invention on experimental data.
[0083] Defects4J v1.2 Defects4J v2.0 TBar 42 8 SimFix 34 2 The present invention 53 19
[0084] TBar and Simfix are the two top-performing automated software repair techniques in Defects4J v1.2. The table lists the total number of defects repaired by each of these techniques on two test datasets. As can be seen, our method repaired 11 more defects than TBar on Defects4J v1.2 and doubled the number on Defects4J v2.0. These results demonstrate that our method offers stronger repair capabilities and better generalizability than existing techniques.
[0085] The embodiment of the present invention also relates to a software automatic repair system, such as Figure 6 Shown, including:
[0086] Acquisition module 601, used to acquire software defect code;
[0087] The template generation module 602 is used to generate a patch template that conforms to the syntax of the language used by the software defect code based on the grammatical features of the software defect code and the trained patch template generation model;
[0088] The patch generation module 603 fills in the patch template and generates a patch for the software defect code;
[0089] The repair module 604 is used to repair software defect codes using patches.
[0090] In one example, before generating a patch template that conforms to the syntax of the language used by the software defect code based on the grammatical features of the software defect code and a trained patch template generation model, the method also includes: determining a defect function in the software defect code based on defect location technology; parsing the defect function to generate a first abstract syntax tree; obtaining a pre-order traversal sequence of the first abstract syntax tree, a label vector of each node, and a first abstract syntax graph based on the feature vector, label, and adjacency matrix of each node in the first abstract syntax tree; obtaining an extended program syntax based on preset grammatical rules and grammatical features; calling a word embedding method based on the rule sequence of the extended program syntax to generate an embedding vector of each rule sequence and a program patch; generating an encoding vector of each rule sequence based on the encoding sequence number of the rule sequence; generating a second abstract syntax tree based on the program patch; and generating a second abstract syntax tree based on the second abstract syntax tree. , obtaining a second abstract syntax graph and a second abstract syntax tree path; a patch template generation model, including: a code encoder, a patch encoder, an abstract syntax tree path encoder and an extended syntax decoder; generating a patch template that conforms to the syntax of the language used by the software defect code, including: inputting the pre-order traversal sequence, each node label vector and the first abstract syntax graph into the code encoder to obtain a code encoding result; inputting the code encoding result and each rule sequence embedding vector, each rule sequence encoding vector, and the second abstract syntax graph into the patch encoder to obtain a patch encoding result; inputting the code encoding result, the code encoding result and the second abstract syntax tree path into the abstract syntax tree path encoder to obtain an abstract syntax tree path encoding result; inputting the abstract syntax tree path encoding result into the extended syntax decoder to select the optimal rule sequence; generating a patch template based on the optimal rule sequence.
[0091] In one example, a code encoder includes: a first self-attention layer, a first gating layer and a first graph convolution layer; a pre-order traversal sequence, each node label vector and a first abstract syntax graph are input into the code encoder to obtain a code encoding result, including: obtaining a position feature vector of each node according to the pre-order traversal sequence; obtaining a first query vector, a first key value vector and a first weight vector according to the pre-order traversal sequence and the position feature vector; inputting the first query vector, the first key value vector and the first weight vector into the first self-attention layer to obtain a first self-attention result; inputting the label vectors of each node of the first self-attention result into the first gating layer to obtain a first gating result; inputting the first gating result and the first abstract syntax graph into the first graph convolution layer to obtain a first graph convolution result; assigning the first graph convolution result to the first query vector, the first key value vector and the first weight vector, and iteratively calculating the first self-attention layer, the first gating layer and the first graph convolution layer to obtain a code encoding result.
[0092] In one example, a patch encoder includes: a second self-attention layer, a second gating layer, a natural language attention layer, and a second graph convolution layer; the code encoding result and each rule sequence embedding vector, each rule sequence encoding vector, and the second abstract syntax graph are input into the patch encoder to obtain a patch encoding result, including: obtaining the position feature vector of each node according to the pre-order traversal sequence; obtaining a second query vector, a second key value vector, and a second weight vector according to the rule sequence embedding vector and the position feature vector; inputting the second query vector, the second key value vector, and the second weight vector into the second self-attention layer to obtain The second self-attention result; the second self-attention result and each rule sequence encoding vector are input into the second gating layer to obtain the second gating result; the code encoding result and the second gating result are input into the natural language attention layer to obtain the natural language attention result; the natural language attention result and the second abstract syntax graph are input into the second graph convolution layer to obtain the second graph convolution result; the second graph convolution result is assigned to the second query vector, the second key value vector and the second weight vector, and the second self-attention layer, the second gating layer, the natural language attention layer and the second graph convolution layer are iteratively calculated to obtain the patch encoding result.
[0093] In one example, an abstract syntax tree path encoder includes: a patch attention layer, a code attention layer, and a fully connected layer; the code encoding result, the code encoding result, and the second abstract syntax tree path are input into the abstract syntax tree path encoder to obtain the abstract syntax tree path encoding result, including: inputting the patch encoding result and the second abstract syntax tree path into the patch attention layer to obtain the patch attention result; inputting the code encoding result and the patch attention result into the code attention layer to obtain the code attention result; inputting the code attention result into the fully connected layer, assigning the output result of the fully connected layer to the second abstract syntax tree path, iteratively calculating the patch attention layer, the code attention layer, and the fully connected layer to obtain the abstract syntax tree path encoding result.
[0094] In one example, an extended syntax decoder includes: a native rule proposer, a copy rule proposer, a defect subtree proposer and a decision maker; the abstract syntax tree path encoding result is input into the extended syntax decoder, the optimal rule sequence is selected, and a patch template is generated, including: inputting the abstract syntax tree path encoding result into the native rule proposer, the copy rule proposer and the defect subtree proposer respectively to obtain the extended rule selection probability; wherein, the native rule proposer is used to generate the selection probability of a predefined extended rule, the copy rule proposer is used to select a subtree, and the defect subtree proposer selects a subtree position with a defect; the abstract syntax tree path encoding result, the extended rule selection probability, the subtree of the first abstract syntax tree and the subtree position with a defect are input into the decision maker to obtain the probability of the optimal rule; according to the probability of the optimal rule, the optimal rule sequence is obtained.
[0095] In one example, the copy rule proposer is further used to generate a copy operation code according to the position of the subtree corresponding to the defect function after selecting a subtree of the first abstract syntax tree; the defect subtree proposer is further used to generate a defect subtree code according to the position of the subtree with a defect after selecting the position of the subtree with a defect; the abstract syntax tree path encoding result, the extended rule selection probability, the subtree of the first abstract syntax tree and the subtree position with a defect are input into the decision maker, including: inputting the abstract syntax tree path encoding result, the extended rule selection probability, the copy operation code, and the defect subtree code into the decision maker.
[0096] An embodiment of the present invention also relates to an electronic device, such as Figure 7 As shown, it includes: at least one processor 701; a memory 702 communicatively connected to the at least one processor; wherein the memory 702 stores instructions that can be executed by the at least one processor 701, and the instructions are executed by the at least one processor 701 in any of the above method embodiments.
[0097] The memory 702 and processor 701 are connected using a bus. The bus may include any number of interconnected buses and bridges, connecting various circuits of one or more processors 701 and memory 702. The bus may also connect various other circuits such as peripheral devices, voltage regulators, and power management circuits. These are all well known in the art and are therefore not described further herein. The bus interface provides an interface between the bus and the transceiver. The transceiver may be a single component or multiple components, such as multiple receivers and transmitters, providing a unit for communicating with various other devices over a transmission medium. Information processed by the processor 701 is transmitted over a wireless medium via an antenna. Furthermore, the antenna receives information and transmits it to the processor 701.
[0098] The processor 701 is responsible for managing the bus and general processing, and can also provide various functions, including timing, peripheral interfaces, voltage regulation, power management, and other control functions. The memory 702 can be used to store information used by the processor when performing operations.
[0099] An embodiment of the present invention relates to a computer-readable storage medium storing a computer program, which implements the above method embodiment when executed by a processor.
[0100] That is, those skilled in the art will understand that all or part of the steps in the above-mentioned embodiment methods can be implemented by instructing the relevant hardware through a program, which is stored in a storage medium and includes a number of instructions for causing a device (which can be a single-chip microcomputer, chip, etc.) or a processor to execute all or part of the steps in the various embodiments of the present application. The aforementioned storage medium includes: a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk, etc., various media that can store program code.
[0101] Those skilled in the art will appreciate that the above-mentioned embodiments are specific examples for implementing the present invention, and that in actual applications, various changes may be made thereto in form and detail without departing from the spirit and scope of the present invention.< / hlidentifier> < / hlidentifier> < / hlstatement>
Claims
1. A software automatic repair method, characterized in that: include: Obtain software defect codes; Determining a defect function in the software defect code based on defect localization technology; Parsing the defective function to generate a first abstract syntax tree; Obtaining a pre-order traversal sequence of the first abstract syntax tree, label vectors of each node, and a first abstract syntax graph according to the feature vector, label, and adjacency matrix of each node in the first abstract syntax tree; Obtaining an extended program grammar according to preset grammatical rules and grammatical features of the software defect code; According to the rule sequence of the extended program grammar, a word embedding method is called to generate an embedding vector of each rule sequence and a program patch; Generate a coding vector for each rule sequence according to the coding sequence number of the rule sequence; generating a second abstract syntax tree according to the program patch; Obtaining a second abstract syntax graph and a second abstract syntax tree path according to the second abstract syntax tree; Inputting the pre-order traversal sequence, each of the node label vectors, and the first abstract syntax graph into a code encoder in a trained patch template generation model to obtain a code encoding result, wherein the patch template generation model further includes a patch encoder, an abstract syntax tree path encoder, and an extended syntax decoder; Inputting the code encoding result, each of the regular sequence embedding vectors, each of the regular sequence encoding vectors, and the second abstract syntax graph into the patch encoder to obtain a patch encoding result; Inputting the code encoding result, the code encoding result and the second abstract syntax tree path into the abstract syntax tree path encoder to obtain an abstract syntax tree path encoding result; Inputting the abstract syntax tree path encoding result into the extended syntax decoder to select the best rule sequence; generating a patch template that complies with the grammar of the language used by the software defect code according to the optimal rule sequence; Filling the patch template to generate a patch for the software defect code; The software defect code is repaired using the patch.
2. The software automatic repair method according to claim 1, characterized in that: The code encoder comprises: a first self-attention layer, a first gating layer and a first graph convolution layer; The step of inputting the pre-order traversal sequence, each of the node label vectors, and the first abstract syntax graph into the code encoder to obtain a code encoding result includes: Obtaining a position feature vector of each node according to the pre-order traversal sequence; Obtaining a first query vector, a first key vector, and a first weight vector according to the pre-order traversal sequence and the position feature vector; Inputting the first query vector, the first key vector, and the first weight vector into the first self-attention layer to obtain a first self-attention result; Inputting each node label vector of the first self-attention result into the first gating layer to obtain a first gating result; Inputting the first gating result and the first abstract syntax graph into the first graph convolution layer to obtain a first graph convolution result; Assigning the first graph convolution result to the first query vector, the first key value vector and the first weight vector, iteratively calculating the first self-attention layer, the first gating layer and the first graph convolution layer to obtain the code encoding result.
3. The software automatic repair method according to claim 1, characterized in that: The patch encoder comprises: a second self-attention layer, a second gating layer, a natural language attention layer, and a second graph convolution layer; The step of inputting the code encoding result, each of the regular sequence embedding vectors, each of the regular sequence encoding vectors, and the second abstract syntax graph into the patch encoder to obtain a patch encoding result includes: Obtaining a position feature vector of each node according to the pre-order traversal sequence; Obtaining a second query vector, a second key value vector, and a second weight vector according to the regular sequence embedding vector and the position feature vector; Inputting the second query vector, the second key vector, and the second weight vector into the second self-attention layer to obtain a second self-attention result; Inputting the second self-attention result and each of the regular sequence encoding vectors into the second gating layer to obtain a second gating result; Inputting the code encoding result and the second gating result into the natural language attention layer to obtain a natural language attention result; Inputting the natural language attention result and the second abstract syntax graph into the second graph convolution layer to obtain a second graph convolution result; Assign the second graph convolution result to the second query vector, the second key vector and the second weight vector, iteratively calculate the second self-attention layer, the second gating layer, the natural language attention layer and the second graph convolution layer to obtain the patch encoding result.
4. The software automatic repair method according to claim 1, characterized in that: The abstract syntax tree path encoder includes: a patch attention layer, a code attention layer and a fully connected layer; The step of inputting the code encoding result, the code encoding result, and the second abstract syntax tree path into the abstract syntax tree path encoder to obtain an abstract syntax tree path encoding result includes: Inputting the patch encoding result and the second abstract syntax tree path into the patch attention layer to obtain a patch attention result; Inputting the code encoding result and the patch attention result into the code attention layer to obtain a code attention result; The code attention result is input into the fully connected layer, the output result of the fully connected layer is assigned to the second abstract syntax tree path, the patch attention layer, the code attention layer and the fully connected layer are iteratively calculated to obtain the abstract syntax tree path encoding result.
5. The software automatic repair method according to claim 1, characterized in that: The extended grammar decoder includes: a native rule proposer, a copy rule proposer, a defect subtree proposer and a decision maker; The step of inputting the abstract syntax tree path encoding result into the extended syntax decoder and selecting the best rule sequence comprises: Inputting the abstract syntax tree path encoding result into the native rule proposer, the copied rule proposer, and the defective subtree proposer, respectively, to obtain an extended rule selection probability; wherein the native rule proposer is used to generate a predefined extended rule selection probability, the copied rule proposer is used to select a subtree of the first abstract syntax tree, and the defective subtree proposer selects a subtree position with a defect; Inputting the abstract syntax tree path encoding result, the extended rule selection probability, the subtree of the first abstract syntax tree and the position of the subtree with defects into a decision maker to obtain the probability of the optimal rule; The optimal rule sequence is obtained according to the probability of the optimal rule.
6. The software automatic repair method according to claim 5, characterized in that: The copy rule proposer is further configured to, after selecting a subtree of the first abstract syntax tree, generate a copy operation code according to a position of the subtree corresponding to the defective function; The defective subtree proposer is further configured to generate a defective subtree code according to the defective subtree position after selecting the defective subtree position; Inputting the abstract syntax tree path encoding result, the extended rule selection probability, the subtree of the first abstract syntax tree, and the position of the subtree with defects into a decision maker includes: The abstract syntax tree path encoding result, the extended rule selection probability, the copy operation encoding, and the defective subtree encoding are input into the decision maker.
7. A software automatic repair system, characterized in that: include: An acquisition module, used to obtain software defect codes; The automatic software repair system is further configured to determine a defect function in the software defect code based on defect location technology; parse the defect function to generate a first abstract syntax tree; obtain a pre-order traversal sequence of the first abstract syntax tree, label vectors of each node, and a first abstract syntax graph based on the feature vector, label, and adjacency matrix of each node in the first abstract syntax tree; obtain an extended program syntax based on preset syntax rules and syntax features of the software defect code; and invoke a word embedding method based on the rule sequence of the extended program syntax to generate embedding vectors for each rule sequence and a program patch. Generate a coding vector for each rule sequence according to the coding sequence number of the rule sequence; generating a second abstract syntax tree according to the program patch; Obtaining a second abstract syntax graph and a second abstract syntax tree path according to the second abstract syntax tree; A template generation module is configured to input the pre-order traversal sequence, each node label vector, and the first abstract syntax graph into a code encoder in a trained patch template generation model to obtain a code encoding result, wherein the patch template generation model further includes a patch encoder, an abstract syntax tree path encoder, and an extended syntax decoder; input the code encoding result, each rule sequence embedding vector, each rule sequence encoding vector, and the second abstract syntax graph into the patch encoder to obtain a patch encoding result; input the code encoding result, the code encoding result, and the second abstract syntax tree path into the abstract syntax tree path encoder to obtain an abstract syntax tree path encoding result; input the abstract syntax tree path encoding result into the extended syntax decoder to select an optimal rule sequence; and generate a patch template that conforms to the syntax of the language used by the software defect code based on the optimal rule sequence; A patch generation module, configured to fill in the patch template and generate a patch for the software defect code; A repair module is used to repair the software defect code using the patch.
8. An electronic device, characterized in that: include: at least one processor; And, a memory communicatively connected to the at least one processor; wherein the memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the software automatic repair method as described in any one of claims 1 to 6.
9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the software automatic repair method according to any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Method for automatically completing code on basis of LSTM (Long Short Term Memory)
CN108388425A
Open source software defect data classification method and system based on abstract syntax tree
CN112181428A