Repair patch generation method, storage medium and electronic device
By converting defect code into type flow graphs and generating feature vectors, combined with syntax rule vocabulary, the problem of missing type information in deep learning defect code repair methods is solved, and the generation of type constraints is realized, which improves the repair accuracy and efficiency.
Patent Information
- Application Number
- CN202310709070.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-14
- Publication Date
- 2024-12-17
AI Technical Summary
Existing deep learning defective code repair methods cannot effectively learn and perceive code type rules, resulting in incorrect repair patch types generated, inefficient compilation process, and possible exclusion of correct patches.
By constructing the defect code into the first abstract syntax tree, converting it into a type flow graph, obtaining the node sequence, attribute sequence and relationship matrix, input the type flow graph encoder to generate feature vectors, and combining the syntax rule vocabulary, use grammar guidance to generate The machine generates a fix patch carrying type information.
It realizes the organic fusion of abstraction of code types and syntax rules and type rules. The generated repair patches have type constraints, expands the ability of deep learning defective code repair methods, and improves the repair accuracy and efficiency.
Smart Images

Figure CN119149039A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to the field of computer technology, and in particular, to a repair patch generation method, a storage medium, and an electronic device. Background Art
[0002] At present, the syntax-guided program defect repair method based on deep learning can only ensure that the generated repair patches comply with the grammatical rules, but the neural network in the deep learning model cannot perceive the type rules of the program and is prone to generate a large number of incorrect type patches. Although these patches can eventually be filtered out by the compiler, it takes a lot of time to filter out these incorrect type patches when compiling large projects, and the efficiency of the defect repair system is low. When using a search algorithm to determine the grammatical rule path with a higher probability, the neural network may also assign a higher probability to the incorrect type grammatical rules. In this way, the correct patch may be excluded from the candidate patches, resulting in the inability to repair the defect.
[0003] How to abstract the type of code and how neural networks represent and learn type rules are technical problems that need to be solved. Summary of the invention
[0004] Embodiments of the present invention provide a repair patch generation method, a storage medium, and an electronic device to at least solve the problem that the existing deep learning defect code repair method in the related art lacks the ability to learn type information.
[0005] According to one embodiment of the present invention, a repair patch generation method is provided, comprising: constructing a program code containing a defective code into a first abstract syntax tree, converting the first abstract syntax tree into a type flow graph through a type flow graph constructor, wherein the first abstract syntax tree carries type information; traversing the type flow graph to obtain a node sequence, an attribute sequence, and a relationship matrix, inputting the node sequence, the attribute sequence, and the relationship matrix into a type flow graph encoder to obtain a feature vector; inputting the feature vector and the grammar rule vocabulary into a grammar-guided generator to obtain a second abstract syntax tree, and code-instantiating the second abstract syntax tree to generate a repair patch, wherein the second abstract syntax tree carries the type information.
[0006] According to yet another embodiment of the present invention, a computer-readable storage medium is provided, in which a computer program is stored, wherein the computer program is configured to execute the steps of any one of the above method embodiments when run.
[0007] According to yet another embodiment of the present invention, there is provided an electronic device, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.
[0008] Through the above-mentioned embodiment of the present invention, the defective code is constructed into a first abstract syntax tree that can carry type information, thereby realizing the abstraction of the code type; and in the type flow graph converted from the first abstract syntax tree, the grammatical rules in the program code and the type rules can be organically integrated, and then, the second abstract syntax tree obtained by inputting the feature vector and grammatical rule vocabulary obtained based on the type flow graph into the syntax-guided generator will carry type information, and the repair patch code obtained from the second abstract syntax tree will be given type constraints. Therefore, the problem that the existing deep learning defective code repair method in the related art lacks the ability to learn type information can be solved, and the effect of expanding the ability of the existing deep learning defective code repair method can be achieved. BRIEF DESCRIPTION OF THE DRAWINGS
[0009] Figure 1 It is a hardware structure block diagram of a computer terminal for running a repair patch generation method according to an embodiment of the present invention;
[0010] Figure 2 A flowchart of a repair patch generation method according to an embodiment of the present invention;
[0011] Figure 3 is a structural block diagram of a repair patch generating device according to an embodiment of the present invention;
[0012] Figure 4 is a schematic diagram of a repair patch generation process according to an embodiment of the present invention;
[0013] Figure 5 is a schematic diagram of the internal structure of a type stream graph encoder according to an embodiment of the present invention;
[0014] Figure 6 is a flow chart of a method for automatically repairing program defects based on type constraints according to a scenario embodiment of the present invention;
[0015] Figure 7 is a schematic diagram of an abstract syntax tree and defect codes according to an embodiment of the present invention;
[0016] Figure 8 is a schematic diagram of a type flow graph according to an embodiment of the present invention. DETAILED DESCRIPTION
[0017] Hereinafter, embodiments of the present invention will be described in detail with reference to the accompanying drawings and in combination with the embodiments.
[0018] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence.
[0019] The method embodiments provided in the embodiments of the present application can be executed in a mobile terminal, a computer terminal or a similar computing device. Taking running on a computer terminal as an example, Figure 1 1 is a hardware structure block diagram of a computer terminal for executing a repair patch generation method according to an embodiment of the present invention. Figure 1 As shown, the computer terminal may include one or more ( Figure 1 Only one is shown in the figure) a processor 102 (the processor 102 may include but is not limited to a microprocessor or a processing device of a programmable logic device) and a memory 104 for storing data, wherein the above-mentioned computer terminal may also include a transmission device 106 for communication functions and an input and output device 108. It can be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the above-mentioned computer terminal. Figure 1 More or fewer components as shown, or with Figure 1 Different configurations are shown.
[0020] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the repair patch generation method in the embodiment of the present invention. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, that is, to implement the above method. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include a memory remotely arranged relative to the processor 102, and these remote memories may be connected to the computer terminal via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0021] The transmission device 106 is used to receive or send data via a network. The specific example of the above network may include a wireless network provided by a communication provider of a computer terminal. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, referred to as NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 can be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0022] In this embodiment, a repair patch generation method running on the above-mentioned computer terminal is provided. Figure 2 A flowchart of a repair patch generation method according to an embodiment of the present invention is shown in FIG. Figure 2 As shown, the process includes the following steps:
[0023] Step S202, constructing the program code containing the defective code into a first abstract syntax tree, and converting the first abstract syntax tree into a type flow graph by a type flow graph constructor, wherein the first abstract syntax tree carries type information;
[0024] In this embodiment, the abstract type system defines a set of rules that can abstract the types of variables, constants, and operators of the program source code, thereby converting the program source code into an abstract syntax tree with type information. Therefore, before step S202, the method also includes: converting the program source code corpus into multiple third abstract syntax trees through the abstract type system, wherein the third abstract syntax tree carries the type information; traversing all the third abstract syntax trees to obtain the grammar rule vocabulary, wherein the grammar rule vocabulary carries the type information, and the vocabulary can be used for subsequent neural network generation of a repair code abstract syntax tree. Among them, this rule is also used to construct the program code containing defective code into a first abstract syntax tree.
[0025] In one embodiment, the rules defined by the abstract type system include: 1) converting the program code or the program source code corpus into an abstract syntax tree, where the variables, constants, and operators in the program code or the program source code corpus statements are leaf nodes of the abstract syntax tree; 2) defining five abstract types: general type, string type, numerical type, Boolean type, and empty type, where the empty type is a subtype of the string type, numerical type, and Boolean type; the general type is a parent type of the string type, numerical type, and Boolean type; the parent type of the general type is the general type, and the subtype of the empty type is the empty type; 3) according to the types defined above, the variables in the program source code are Quantities or constants can be abstracted as one of the string type, numeric type, and Boolean type, and NULL is classified as the empty type; 4) The type of an operator is determined by the type of its operands. If all operand types of an operator are the same, the operator type is the same as the operand type; if there are two or more operand types, the operator type is the parent type of all operand types; 5) The type of a non-leaf node in the abstract syntax tree is determined by the type of its child nodes. If all child nodes of a non-leaf node are of the same type, the type of the node is the same as its child node type; if there are two or more child node types, the node type is the parent type of all child node types.
[0026] Through the rules defined by the abstract type system, the types of variables, constants, and operators in the program source code or defective code can be abstracted, and when constructing the abstract syntax tree, the abstract type information is carried accordingly on the abstract syntax tree, thereby obtaining a typed abstract syntax tree.
[0027] In step S202 of the present embodiment, the first abstract syntax tree is converted into a type flow graph through a type flow graph constructor, including: converting each node in the first abstract syntax tree into an abstract syntax tree node in the type flow graph; abstracting the variables in the program code into variable nodes in the type flow graph; wherein the abstract syntax tree nodes, the variable nodes, and the abstract syntax tree nodes and the variable nodes corresponding to the same variable are connected by edges of different attributes.
[0028] Among them, the attributes of the edges between the abstract syntax tree nodes include: parent-child, child-parent, left brother, right brother; the attributes of the edges between the variable nodes include: same type, compatible type, incompatible type; the attributes of the edges connecting the abstract syntax tree node and the variable node corresponding to the same variable include: declaration-variable, use-variable, variable-declaration, variable-use.
[0029] Step S204, traversing the type flow graph to obtain a node sequence, an attribute sequence, and a relationship matrix, and inputting the node sequence, the attribute sequence, and the relationship matrix into a type flow graph encoder to obtain a feature vector;
[0030] In this embodiment, the type flow graph encoder is an organic stack of multiple neural network layers, wherein the multiple neural network layers include: an embedding layer, type-sensitive self-attention neurons, a gating layer, and a linear layer. Except for the embedding layer whose input is a node sequence, an attribute sequence, and a relationship matrix input from the outside, the input of the remaining neural network layers is the output of the previous neural network layer.
[0031] In an exemplary embodiment, traversing the type flow graph to obtain a node sequence includes: traversing the abstract syntax tree node names of the first abstract syntax tree in the type flow graph, and forming all the abstract syntax tree node names into a first sequence segment; traversing the variable node names in the first abstract syntax tree according to the order of appearance of each variable in the program code context, and forming the variable node names accessible at the defective code into a second sequence segment; splicing the first sequence segment and the second sequence segment to obtain the node sequence.
[0032] In an exemplary embodiment, traversing the type flow graph to obtain the attribute sequence includes: obtaining the node attribute values corresponding to each abstract syntax tree node and each variable node in turn according to the arrangement order of the abstract syntax tree nodes and the variable nodes in the node sequence, and combining all the node attribute values into the attribute sequence.
[0033] In this embodiment, the abstract syntax tree node includes the following attributes: type attribute, replicability; the variable node includes the following attributes: type attribute, accessibility.
[0034] In step S204 of the present embodiment, the node sequence, the attribute sequence, and the relationship matrix are input into a type flow graph encoder to obtain a feature vector, including: obtaining a node embedding vector, an attribute embedding vector, and a relationship embedding vector based on the input node sequence, the attribute sequence, and the relationship matrix through an embedding layer in the type flow graph encoder; fusing the node embedding vector and the relationship embedding vector through type-sensitive self-attention neurons in the type flow graph encoder to obtain a first fused vector; fusing the attribute embedding vector and the first fused vector through a gating layer in the type flow graph encoder to obtain a second fused vector; and performing a linear transformation on the second fused vector through a linear layer in the type flow graph encoder to obtain the feature vector.
[0035] Through the type flow graph and its construction rules, the adjacency relationship between the typed abstract syntax tree nodes and the user-defined variable nodes can be recorded, realizing the organic integration of the syntax rules and type rules of the program source code or defective code.
[0036] Step S206: input the feature vector and the grammar rule vocabulary into a grammar-guided generator to obtain a second abstract syntax tree, and instantiate the second abstract syntax tree into code to generate a repair patch, wherein the second abstract syntax tree carries the type information.
[0037] Through the type flow graph encoder and its neural network architecture, by extracting the node sequence, attribute sequence and relationship matrix of the type flow graph, the grammatical rules and type rules of the program source code are encoded using a neural network, and type constraints can be given to the repair patch (code) during the patch generation process.
[0038] In an exemplary embodiment, the value of the feature vector is a floating point number.
[0039] In step S206 of the present embodiment, the feature vector and the grammar rule vocabulary are input into a grammar-guided generator to obtain a second abstract syntax tree, including: inputting the feature vector and the grammar rule vocabulary into the grammar-guided generator, predicting a first grammar rule, and performing an editing operation on the abstract syntax tree according to the first grammar rule to obtain a first partial abstract syntax tree, wherein the first grammar rule carries type information; predicting a second grammar rule based on the first partial abstract syntax tree, the feature vector and the grammar rule vocabulary, and performing an editing operation on the abstract syntax tree according to the second grammar rule to obtain a second partial abstract syntax tree, wherein the second grammar rule carries type information; iteratively performing the grammar rule prediction step and the abstract syntax tree editing step until a complete second abstract syntax tree is obtained.
[0040] Through the above steps, the defective code is constructed into a first abstract syntax tree that can carry type information, thereby realizing the abstraction of the code type; and in the type flow graph converted from the first abstract syntax tree, the grammatical rules in the program code and the type rules can be organically integrated, and then, the second abstract syntax tree obtained by inputting the feature vector and grammatical rule vocabulary obtained based on the type flow graph into the syntax-guided generator will carry type information, and the repair patch code obtained from the second abstract syntax tree will be given type constraints. Therefore, the problem that the existing deep learning defective code repair method in the related art lacks the ability to learn type information can be solved, and the effect of expanding the ability of the existing deep learning defective code repair method can be achieved.
[0041] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course can also be implemented by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present invention, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for enabling a terminal device (which can be a mobile phone, a computer, a server, or a network device, etc.) to execute the methods described in each embodiment of the present invention.
[0042] In this embodiment, a repair patch generation device is also provided, which is used to implement the above-mentioned embodiments and preferred implementation modes, and the descriptions that have been made will not be repeated. As used below, the term "module" can be a combination of software and / or hardware that implements a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.
[0043] Figure 3is a structural block diagram of a repair patch generating device according to an embodiment of the present invention. Figure 3 As shown, the device includes: a conversion module 10, a traversal module 20 and a generation module 30.
[0044] A conversion module 10, configured to construct a program code containing a defective code into a first abstract syntax tree, and convert the first abstract syntax tree into a type flow graph through a type flow graph constructor, wherein the first abstract syntax tree carries type information;
[0045] A traversal module 20 is used to traverse the type flow graph to obtain a node sequence, an attribute sequence and a relationship matrix, and input the node sequence, the attribute sequence and the relationship matrix into a type flow graph encoder to obtain a feature vector;
[0046] The generation module 30 is used to input the feature vector and the grammar rule vocabulary into a grammar-guided generator to obtain a second abstract syntax tree, and instantiate the second abstract syntax tree in code to generate a repair patch, wherein the second abstract syntax tree carries the type information.
[0047] It should be noted that the above modules can be implemented by software or hardware. For the latter, it can be implemented in the following ways, but not limited to: the above modules are all located in the same processor; or the above modules are located in different processors in any combination.
[0048] To facilitate the understanding of the technical solution provided by the present invention, a detailed description will be given below in conjunction with embodiments of specific scenarios.
[0049] Figure 4 is a schematic diagram of a repair patch generation process according to an embodiment of the present invention. Figure 4 As shown, the process involves the following modules: abstract type system, type flow graph constructor, type flow graph encoder, and syntax-directed generator.
[0050] Specifically, the abstract type system defines a set of rules that can abstract the types of variables, constants, and operators in the program source code and convert the program source code into an abstract syntax tree with type information.
[0051] A type flow graph constructor is used to convert an abstract syntax tree into a type flow graph, which is used to record the adjacency relationship between typed abstract syntax tree nodes and user-defined variable nodes; based on the type flow graph and its construction rules, an embodiment of the present invention can organically integrate the grammatical rules and type rules of the program source code.
[0052] The type flow graph encoder is used to generate feature vectors based on the node sequence, attribute sequence and relationship matrix recorded in the type flow graph. By extracting the node sequence, attribute sequence and relationship matrix of the type flow graph, the grammatical rules and type rules of the program source code are encoded using a neural network, and type constraints can be given to the repair patch during the patch generation process.
[0053] In one embodiment, the abstract type system defines the following five rules:
[0054] 1) Convert the program source code into an abstract syntax tree. The variables, constants, and operators in the program source code statements are the leaf nodes of the abstract syntax tree.
[0055] 2) Define five abstract types: generic type, string type, numeric type, Boolean type, and empty type. Among them, the empty type is the subtype of the string type, numeric type, and Boolean type; the generic type is the parent type of the string type, numeric type, and Boolean type; the parent type of the generic type is the generic type, and the subtype of the empty type is the empty type.
[0056] 3) According to the types defined above, variables or constants in the program source code can be abstracted into one of the string type, numeric type, and Boolean type, and NULL is classified as the empty type.
[0057] 4) The type of an operator is determined by the type of its operands. If all operand types of an operator are the same, the operator type is the same as the operand type; if there are two or more operand types, the operator type is the supertype of all operand types.
[0058] 5) The type of a non-leaf node in the abstract syntax tree is determined by the type of its child node. If all child nodes of a non-leaf node are of the same type, the type of the node is the same as its child node type. If there are two or more child node types, the node type is the parent type of all child node types.
[0059] In one embodiment, the type flow graph is a graph data structure, including a node set and an edge set. The construction rules of the type flow graph are as follows:
[0060] 1) The node set includes an abstract syntax tree node subset (the elements of which we call Anode) and a variable node subset (the elements of which we call Vnode).
[0061] 2) Each Anode has two attributes: a type attribute and a replicability attribute. The value of the type attribute is one of five abstract types; the replicability attribute is a Boolean value that indicates whether the abstract syntax subtree with this node as the root node depends on the variables of the local context so that the abstract syntax subtree can be independently copied to other locations.
[0062] 3) Each Vnode represents a user-defined element (variable or parameter) of the context, and the context refers to the program source code range corresponding to the abstract syntax tree. Each Vnode has two attributes: type attribute and accessibility attribute. The value of the type attribute is the actual type of the element when it is defined; the accessibility attribute is a Boolean value that indicates whether the element is accessible at the defective code location.
[0063] 4) The edge set contains four subsets: Anode-Anode, Anode-Vnode, Vnode-Anode and Vnode-Vnode. The edges in the edge set are directed and have an attribute label. The specific edge attribute information is shown in Table 1.
[0064] Table 1
[0065]
[0066] 5) The edges in the Anode-Anode set represent the grammatical relationship between the abstract syntax tree nodes. The relationship between two Anodes is one of the four relationships: parent-child, child-parent, left brother, and right brother.
[0067] 6) The edges in the Vnode-Anode set represent the relationship between the context variables and the abstract syntax tree nodes. If the declaration statement of variable x corresponds to the root node of the abstract syntax subtree is y, then x has an edge with the "variable-declaration" attribute connected to y; similarly, if the abstract syntax tree node m uses the variable n, then n has an edge with the "variable-use" attribute connected to m.
[0068] 7) An edge in the Anode-Vnode set is the inverse of the corresponding edge in the Vnode-Anode set.
[0069] 8) The edge in the Vnode-Vnode set represents the compatibility relationship between two variable types.
[0070] In one embodiment, the type flow graph constructor converts the typed abstract syntax tree into a type flow graph that satisfies the above rules.
[0071] In one embodiment, the type flow graph encoder is an organic stack of a series of neural network layers, which is used to encode the type flow graph information into a feature vector through learning. Its internal structure is as follows: Figure 5 shown.
[0072] like Figure 5As shown in the figure, the input of the type flow graph encoder is the attribute sequence, node sequence and relationship matrix, and the output is the feature vector. The three inputs are respectively passed through their respective embedding layers to obtain the attribute embedding vector, node embedding vector and relationship embedding vector. The node embedding vector and the relationship embedding vector are fused through the type-sensitive self-attention neuron, and then passed through the gating layer with the attribute embedding vector, and finally linearly transformed through the linear layer.
[0073] In this embodiment, the embedding layer, type-sensitive self-attention neurons, gating layer and linear layer are all from neural network units disclosed in the industry, which together constitute a neural network block.
[0074] In this embodiment, the type flow graph encoder is composed of N neural network blocks connected in series, where N is an integer greater than 0. Except for the first neural network block whose input is the embedding vector of the attribute sequence, node sequence and relationship matrix, the input of other neural network blocks are the output of the previous neural network block.
[0075] Figure 6 is a flow chart of a method for automatically repairing program defects based on type constraints according to a scenario embodiment of the present invention. Figure 7 As an example, the Java example defective code snippet (hereinafter referred to as defective code) in (a) is used as an example. The following describes in detail how to repair the program defect automatically using the program defect repair method of this embodiment. Figure 7 The defect in (a).
[0076] In this embodiment, the defect code lines that begin with "-" are defect code lines, and the defect code lines that begin with "+" are corresponding repair code lines.
[0077] like Figure 7 As shown in (a), four variables are defined in the defective code: loc0, loc1, loc2, and loc3, where loc0 is of boolean type and the other three variables are of double type. Variables loc0, loc1, and loc2 are defined before the defective code line, so they can be accessed at the defective code; variable loc3 is defined after the defective code line and cannot be accessed at the defective code.
[0078] Specifically, Figure 6 As shown, the method for automatically repairing program defects based on type constraints in this embodiment includes the following steps:
[0079] S601, collect a large amount of program source code corpus, convert it into multiple typed abstract syntax trees through an abstract type system, and obtain a typed grammar rule vocabulary by traversing the abstract syntax trees.
[0080] Specifically, by extracting a large amount of program source code, we can obtain typed abstract syntax trees, and by traversing each typed abstract syntax tree, we can obtain grammar rules. After removing duplicates from these grammar rules, we combine them to obtain a typed grammar rule vocabulary, which can be used by subsequent neural networks to generate abstract syntax trees for repairing code.
[0081] S602, the defective code obtains a typed abstract syntax tree through an abstract type system, and then obtains a type flow graph through a type flow graph constructor.
[0082] Specifically, the defective code is converted into a typed abstract syntax tree using an abstract type system. The resulting abstract syntax tree is as follows: Figure 7 (b), wherein the type information in the abstract syntax tree is determined as follows:
[0083] For example: According to the definition of the abstract type system, the type of an operator is either consistent with the type of all operands or the supertype of all operand types. The operator "-" has two operands "loc2" and "1". The actual type of "loc2" is double, which is abstracted as a numeric type; "1" is a numeric constant, which is also abstracted as a numeric type. Therefore, the type of the operator "-" is consistent with the types of its two operands, that is, numeric types. The deduction of other operator types in the abstract syntax tree is similar.
[0084] For another example, according to the definition of the abstract type system, the type of the parent node is either the same as the type of all child nodes, or the parent type of all child node types, such as Figure 7 As shown in (b), the parent node "BinOp_N" has three child nodes "L-Op_N", "Operator_N" and "R-Op_N". These three child nodes are all numeric types, so the parent node "BinOp_N" is also a numeric type. The type deduction of other parent nodes is similar.
[0085] Similar to step S601, by traversing the child nodes of the typed abstract syntax tree obtained in step S602, a series of grammar rules can be obtained, for example: "Assignment_N->L-Expr_N AssignOp_N R-Expr_N", which means that the three child nodes "L-Expr_N", "AssignOp_N" and "R-Expr_N" can be derived from the "Assignment_N" node.
[0086] According to the definition of type flow graph, we will Figure 7 The typed abstract syntax tree of (b) is converted into a type flow graph, and the resulting type flow graph is as follows Figure 8 shown.
[0087] like Figure 8 As shown, the type flow graph contains two types of nodes:
[0088] (1) Abstract syntax tree node Anode ( Figure 8 It has type attributes and replicability attributes.
[0089] The type attribute takes one of the five types defined by the abstract type system, namely, generic type, string type, numeric type, Boolean type, and null type;
[0090] The replicability attribute takes a value of 0 or 1. A value of 0 means that the node depends on the variables of the context, and the subtree with the node as the root node cannot be copied to other places for reuse. Take the "BinOp_N" node as an example. Its type attribute is a numeric type, and its replicability attribute is 0 because the node depends on the variable "loc2".
[0091] (2) Another type of node is the variable node Vnode ( Figure 8 It has type attributes and accessibility attributes.
[0092] The value of the type attribute is consistent with the actual type of the variable, and the accessibility attribute is 0 or 1. A value of 0 means that the variable cannot be accessed at the defective code line. Take the ellipse node "loc3" as an example. Its type is double, and its accessibility attribute is 0, because the variable statement "double loc3=0" corresponding to the node is defined after the defective code "loc1=loc2-1", and the variable cannot be accessed at the defective code.
[0093] The type flow graph also contains four types of edges with 11 attributes, for example: Figure 8 As shown:
[0094] The edge from node "BinOp_N" to node "L-Op_N" belongs to the Anode-Anode parent-child attribute, and the corresponding attribute number in Table 1 is 1;
[0095] The edge from the rectangular node "loc1" to the elliptical node "loc1" belongs to the Anode-Vnode using variable attribute, which means that the rectangular node "loc1" uses the elliptical node "loc1", and the corresponding attribute number in Table 1 is 6;
[0096] The edge from the ellipse node "loc1" to the rectangle node "loc1" belongs to the Vnode-Anode variable usage attribute, which means that the rectangle node "loc1" uses the ellipse node "loc1", and the corresponding attribute number in Table 1 is 8; the same applies to the two edges from the rectangle node "loc2" to the ellipse node "loc2";
[0097] The edge attributes of the edge from the ellipse node "loc1" to the ellipse node "loc2", the edge from the ellipse node "loc1" to the ellipse node "loc3", and the edge attributes of the edge from the ellipse node "loc2" to the ellipse node "loc3" all indicate the same type;
[0098] The edge attributes from ellipse node "loc0" to ellipse node "loc1", ellipse node "loc2", and ellipse node "loc3" all indicate incompatible types. For example, ellipse node "loc1" and ellipse node "loc2" are of the same type, but the type double of ellipse node "loc2" and the type boolean of ellipse node "loc0" are incompatible. There is another type of edge attribute that is type compatible. For example, in C language, type int is compatible with type int64.
[0099] S603, pre-order traversal of the node names in the type flow graph to obtain the first part of the node sequence, i.e., the Anode sequence; then traversal of the variable names according to the order in which the variables appear in the defect code context, and determination of the second part of the node sequence, i.e., the Anode sequence, based on the variable names, and concatenation of the two parts of the sequence to obtain a complete node sequence.
[0100] Specifically, Figure 8 By traversing the type flow graph, we can obtain three data: node sequence, attribute sequence, and relationship matrix, including:
[0101] right Figure 8 Perform a pre-order traversal of the typed abstract syntax tree nodes in to obtain the Anode sequence:
[0102] [Assignment_N, L-Expr_N, loc1, AssignOp_N, =, R-Expr_N, BinOp_N, L-Op_N, loc2, Operator_N, -, R-Op_N, 1];
[0103] Since the accessibility attribute of the ellipse node "loc3" is 0, the defective code line cannot access the variable loc3. Therefore, the Vnode sequence does not contain the ellipse node "loc3", that is, the Vnode sequence is:
[0104] [loc0, loc1, loc2].
[0105] That is, the Vnode sequence is a sequence fragment of variable names that can be accessed at the defect code.
[0106] The node sequence is the concatenation of the Anode sequence and the Vnode sequence, that is:
[0107] [Assignment_N,L-Expr_N,loc1,AssignOp_N,=,R-Expr_N,BinOp_N,L-Op_N,loc2,Operator_N,-,R-Op_N,1,loc0,loc1,loc2].
[0108] S604, acquiring the node attribute value of each node in the order of the node sequence to obtain an attribute sequence.
[0109] Based on the above node sequence and in the order of the node sequence, replacing each variable in the node sequence with the corresponding type, we can get an attribute sequence: [(numeric value, 0), (numeric value, 0), (numeric value, 1), (numeric value, 1), (numeric value, 0), (numeric value, 0), (numeric value, 0), (numeric value, 1), (numeric value, 1), (numeric value, 1), (numeric value, 1), (boolean, 1), (double, 1), (double, 1)].
[0110] Among them, the first 13 are Anode nodes, including type attributes and reproducibility attributes, "0" represents non-reproducible, and "1" represents reproducible; the last 3 are Vnode nodes, including type attributes and accessibility attributes, and "1" represents accessible.
[0111] S605, obtaining a relationship matrix of type flow graph nodes;
[0112] Specifically, the relationship matrix of the type flow graph is the adjacency matrix between the nodes of the type flow graph. The matrix size is 16x16. Part of the relationship matrix is shown in Table 2. The rows and columns of the relationship matrix are node sequences. If two nodes are adjacent, the value of the matrix is the attribute number of the edge of the two nodes, ranging from 1 to 11, as shown in Table 1. If the two nodes are not adjacent, the value of the matrix is 0, and the nodes themselves are considered not adjacent to each other. Since the edges of the type flow graph are directed, the values of the relationship matrix are read from rows to columns.
[0113] For example, the value of the 2nd column of the 1st row (excluding the header) of Table 2 is 1, indicating that the node "Assignment_N" to the node "L-Expr_N" is a parent-child node relationship.
[0114] Table 2
[0115] Assignment_N L-Expr_N loc1 …… loc2 Assignment_N 0 1 0 …… 0 L-Expr_N 2 0 1 …… 0 loc1 0 2 0 …… 0 …… …… …… …… …… …… loc2 0 0 0 …… 0
[0116] S606, the node sequence, the attribute sequence, and the relationship matrix are passed through a type flow graph encoder to obtain a feature vector.
[0117] Specifically, the node sequence, attribute sequence, and relationship matrix obtained above are input into Figure 4 Type flow graph encoder, node sequence, attribute sequence, and relationship matrix are first passed through the embedding layer to obtain node embedding vector, attribute embedding vector, and relationship embedding vector respectively. These three embedding vectors are all floating-point matrices.
[0118] The node embedding vector and the relationship embedding vector are fused through type-sensitive self-attention neurons. The fused vector is fused with the attribute embedding vector through a gating layer, and finally a linear transformation is performed through a linear layer to output the feature vector of the first neural network block.
[0119] The entire type flow graph encoder is composed of N neural network blocks connected in series. In this embodiment, the value of N is 8. The three inputs of the latter neural network block are obtained by copying the output of the previous neural network block. The output of the last neural network block is the output of the entire type flow graph encoder, that is, a feature vector is output, and the value is a floating point number.
[0120] S607, input the feature vector and the grammar rule vocabulary into the grammar-directed generator, and predict a grammar rule with a type, which represents an editing operation on an abstract syntax tree node.
[0121] Specifically, the grammar-directed generator starts to generate the first grammar rule according to the input feature vector and the typed grammar rule vocabulary, for example: "Assignment_N->L-Expr_N AssignOp_N R-Expr_N";
[0122] S608, predicting grammar rules and editing the abstract syntax tree are iteratively performed until a complete abstract syntax tree is generated. The abstract syntax tree is different from the abstract syntax tree obtained based on the defective code in step S602.
[0123] Specifically, a second grammar rule is generated based on the first grammar rule in step S607, for example: "L-Expr_N->loc1", and the generation process is performed iteratively until a complete abstract syntax tree is generated, such as Figure 7 (c) as shown.
[0124] S609, instantiate the abstract syntax tree generated in step S608 into code to obtain a repair patch.
[0125] Specifically, the generated abstract syntax tree needs to be further restored to the program source code "loc1=1;", which is the repair code (repair patch) of the defective code "loc1=loc2-1;". That is, the repair of this program is to change the value of loc1 from "loc2-1" to the constant 1.
[0126] The above-mentioned embodiments of the present invention can be applied to a program defect repair tool.
[0127] Through the above-mentioned embodiments of the present invention, a repair patch that can simultaneously meet the grammatical rules and type constraints can be output according to the input defect code, thereby expanding the capabilities of the existing syntax-guided defect repair method, avoiding the generation of incorrect type patches, and improving the accuracy of defect repair, thereby improving the efficiency of software development and maintenance and reducing labor costs.
[0128] An embodiment of the present invention further provides a computer-readable storage medium, in which a computer program is stored, wherein the computer program is configured to execute the steps of any of the above method embodiments when running.
[0129] In an exemplary embodiment, the computer-readable storage medium may include, but is not limited to, various media that can store computer programs, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk.
[0130] An embodiment of the present invention further provides an electronic device, including a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.
[0131] In an exemplary embodiment, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor, and the input / output device is connected to the processor.
[0132] For specific examples in this embodiment, reference may be made to the examples described in the above embodiments and exemplary implementation modes, and this embodiment will not be described in detail herein.
[0133] Obviously, those skilled in the art should understand that the above modules or steps of the present invention can be implemented by a general computing device, they can be concentrated on a single computing device, or distributed on a network composed of multiple computing devices, they can be implemented by a program code executable by a computing device, so that they can be stored in a storage device and executed by the computing device, and in some cases, the steps shown or described can be executed in a different order than here, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. Thus, the present invention is not limited to any specific combination of hardware and software.
[0134] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A repair patch generation method, characterized in that: include: Constructing a program code containing a defective code into a first abstract syntax tree, and converting the first abstract syntax tree into a type flow graph by a type flow graph constructor, wherein the first abstract syntax tree carries type information; Traversing the type flow graph to obtain a node sequence, an attribute sequence, and a relationship matrix, and inputting the node sequence, the attribute sequence, and the relationship matrix into a type flow graph encoder to obtain a feature vector; The feature vector and the grammar rule vocabulary are input into a grammar-directed generator to obtain a second abstract syntax tree, and the second abstract syntax tree is instantiated into code to generate a repair patch, wherein the second abstract syntax tree carries the type information.
2. The method according to claim 1, characterized in that Before constructing the program code containing the defective code into a first abstract syntax tree, the method further includes: Converting the program source code corpus into a plurality of third abstract syntax trees through an abstract type system, wherein the third abstract syntax trees carry the type information; All the third abstract syntax trees are traversed to obtain the grammar rule vocabulary, wherein the grammar rule vocabulary carries the type information.
3. The method according to claim 2, characterized in that in, The leaf nodes in the first abstract syntax tree are obtained by abstracting constants, variables, and operators in the program code; The leaf nodes in the third abstract syntax tree are obtained by abstracting constants, variables, and operators in the program source code corpus.
4. The method according to claim 3, characterized in that in, The type information includes the following types: general type, string type, numerical type, Boolean type and empty type; The empty type is a subtype of the string type, the numeric type, and the Boolean type; The generic type is a parent type of the string type, the numeric type, and the Boolean type; The parent type of the generic type is the generic type, and the child type of the empty type is the empty type.
5. The method according to claim 4, characterized in that in, The type of the constant in the program code or the program source code corpus includes one of the following: string type, numerical type, Boolean type; The type of the variable in the program code or the program source code corpus includes one of the following: string type, numerical type, Boolean type; The type of an operator in the program code or the program source code corpus is determined by the type of its operand; NULL in the program code or the program source code corpus is classified as the empty type.
6. The method according to claim 5, characterized in that The type of an operator in the program code or the program source code corpus is determined by the type of its operand, including: If all operands of the operator are of the same type, the operator type is the same as the type of its operands; If there are multiple types of operands of the operator, the operator type is the supertype of all its operand types.
7. The method according to claim 4, characterized in that in, The type of a non-leaf node in the first abstract syntax tree or the third abstract syntax is determined by the type of its child node; In the case where all child nodes of the non-leaf node are of the same type, the type of the non-leaf node is the same as the type of its child nodes; In the case that the non-leaf node has multiple types of child nodes, the type of the non-leaf node is the parent type of all its child node types.
8. The method according to claim 1, characterized in that The first abstract syntax tree is converted into a type flow graph by a type flow graph constructor, including: Convert each node in the first abstract syntax tree into an abstract syntax tree node in the type flow graph; Abstracting the variables in the program code into variable nodes in the type flow graph; The abstract syntax tree nodes, the variable nodes, and the abstract syntax tree nodes and the variable nodes corresponding to the same variable are connected by edges with different attributes.
9. The method according to claim 8, characterized in that in, The attributes of the edges between the nodes of the abstract syntax tree include: parent-child, child-parent, left brother, right brother; The attributes of the edges between the variable nodes include: same type, compatible type, incompatible type; The attributes of the edge connecting the abstract syntax tree node and the variable node corresponding to the same variable include: declaration-variable, use-variable, variable-declaration, variable-use.
10. The method according to claim 8, characterized in that Traversing the type flow graph to obtain a node sequence includes: Traversing the abstract syntax tree node names of the first abstract syntax tree in the type flow graph, and composing all the abstract syntax tree node names into a first sequence segment; According to the order of appearance of each variable in the program code context, traverse the variable node names in the first abstract syntax tree, and form the variable node names accessible at the defective code into a second sequence segment; The first sequence fragment and the second sequence fragment are spliced to obtain the node sequence.
11. The method according to claim 10, characterized in that Traversing the type flow graph to obtain a sequence of attributes includes: According to the arrangement order of the abstract syntax tree nodes and the variable nodes in the node sequence, the node attribute values corresponding to each of the abstract syntax tree nodes and each of the variable nodes are obtained in sequence, and all of the node attribute values are combined into the attribute sequence.
12. The method according to claim 11, characterized in that The abstract syntax tree node includes the following attributes: type attribute and replicability; the variable node includes the following attributes: type attribute and accessibility.
13. The method according to claim 1, characterized in that The node sequence, the attribute sequence, and the relationship matrix are input into a type flow graph encoder to obtain a feature vector, including: Obtaining a node embedding vector, an attribute embedding vector, and a relationship embedding vector through an embedding layer in the type flow graph encoder based on the input node sequence, the attribute sequence, and the relationship matrix; fusing the node embedding vector and the relationship embedding vector through a type-sensitive self-attention neuron in the type flow graph encoder to obtain a first fused vector; fusing the attribute embedding vector and the first fused vector through a gating layer in the type flow graph encoder to obtain a second fused vector; The second fused vector is linearly transformed through a linear layer in the type flow graph encoder to obtain the feature vector.
14. The method according to claim 1, characterized in that in, The value of the feature vector is a floating point number.
15. The method according to claim 1, characterized in that Inputting the feature vector and the grammar rule vocabulary into a grammar-directed generator to obtain a second abstract syntax tree, including: Inputting the feature vector and the grammar rule vocabulary into a grammar-directed generator, predicting a first grammar rule, and performing an editing operation on an abstract syntax tree according to the first grammar rule to obtain a first partial abstract syntax tree, wherein the first grammar rule carries type information; Predicting a second grammar rule based on the first partial abstract syntax tree, the feature vector, and the grammar rule vocabulary, and performing an editing operation on the abstract syntax tree according to the second grammar rule to obtain a second partial abstract syntax tree, wherein the second grammar rule carries type information; The grammar rule prediction step and the abstract syntax tree editing step are iterated until the second abstract syntax tree is completely obtained.
16. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program implements the steps of the method described in any one of claims 1 to 15 when executed by a processor.
17. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method described in any one of claims 1 to 15 are implemented.
