WO2024255477A1 - 修复补丁生成方法、存储介质及电子装置 - Google Patents

修复补丁生成方法、存储介质及电子装置 Download PDF

Info

Publication number
WO2024255477A1
WO2024255477A1 PCT/CN2024/090980 CN2024090980W WO2024255477A1 WO 2024255477 A1 WO2024255477 A1 WO 2024255477A1 CN 2024090980 W CN2024090980 W CN 2024090980W WO 2024255477 A1 WO2024255477 A1 WO 2024255477A1
Authority
WO
WIPO (PCT)
Prior art keywords
type
abstract syntax
syntax tree
node
flow graph
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Ceased
Application number
PCT/CN2024/090980
Other languages
English (en)
French (fr)
Inventor
彭星海
刘�东
朱琪豪
曹继承
程圣宇
熊英飞
肖元安
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Peking University
ZTE Corp
Original Assignee
Peking University
ZTE Corp
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Peking University, ZTE Corp filed Critical Peking University
Publication of WO2024255477A1 publication Critical patent/WO2024255477A1/zh
Anticipated expiration legal-status Critical
Ceased legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/60Software deployment
    • G06F8/65Updates
    • G06F8/658Incremental updates; Differential updates
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N3/00Computing arrangements based on biological models
    • G06N3/02Neural networks
    • G06N3/04Architecture, e.g. interconnection topology
    • G06N3/045Combinations of networks
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N3/00Computing arrangements based on biological models
    • G06N3/02Neural networks
    • G06N3/08Learning methods

Definitions

  • the embodiments of the present disclosure relate to the field of computer technology, and in particular to a repair patch generation method, a storage medium, and an electronic device.
  • 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.
  • 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.
  • the embodiments of the present disclosure 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.
  • a repair patch generation method 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.
  • a computer-readable storage medium 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 running.
  • 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.
  • FIG1 is a hardware structure block diagram of a computer terminal for executing a repair patch generation method according to an embodiment of the present disclosure
  • FIG2 is a flowchart of a repair patch generation method according to an embodiment of the present disclosure
  • FIG3 is a structural block diagram of a repair patch generating device according to an embodiment of the present disclosure.
  • FIG4 is a schematic diagram of a repair patch generation process according to an embodiment of the present disclosure.
  • FIG5 is a schematic diagram of the internal structure of a type flow graph encoder according to an embodiment of the present disclosure.
  • FIG6 is a flow chart of a method for automatically repairing program defects based on type constraints according to an embodiment of the present disclosure
  • FIG7 is a schematic diagram of an abstract syntax tree and defect code according to an embodiment of the present disclosure.
  • FIG. 8 is a schematic diagram of a type flow graph according to an embodiment of the present disclosure.
  • FIG1 is a hardware structure block diagram of a computer terminal running a repair patch generation method in an embodiment of the present disclosure.
  • the computer terminal may include one or more (only one is shown in FIG1 ) processors 102 (the processor 102 may include but is not limited to a processing device of a microprocessor or a programmable logic device) and a memory 104 for storing data, wherein the above-mentioned computer terminal may also include a transmission device 106 and an input and output device 108 for communication functions.
  • FIG1 is only for illustration, and it does not limit the structure of the above-mentioned computer terminal.
  • the computer terminal may also include more or fewer components than those shown in FIG1 , or have a configuration different from that shown in FIG1 .
  • 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 disclosure.
  • 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.
  • the memory 104 may further include a memory remotely arranged relative to the processor 102, and these remote memories can 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.
  • 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.
  • 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.
  • the transmission device 106 can be a radio frequency (Radio Frequency, referred to as RF) module, which is used to communicate with the Internet wirelessly.
  • RF Radio Frequency
  • FIG2 is a flow chart of the repair patch generation method according to an embodiment of the present disclosure. As shown in FIG2 , the process includes the following steps:
  • 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;
  • the abstract type system defines a set of rules that can be used to define variables, constants, and operators in the program source code.
  • the type of is abstracted, 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 an 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.
  • the rule is also used to construct the program code containing defective code into a first abstract syntax tree.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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;
  • 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.
  • 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.
  • traversing the type flow graph to obtain an attribute sequence includes: The abstract syntax tree nodes and the variable nodes are arranged in an order, and 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 the node attribute values are combined into the attribute sequence.
  • the abstract syntax tree node includes the following attributes: type attribute, replicability; the variable node includes the following attributes: type attribute, accessibility.
  • 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.
  • 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.
  • 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.
  • 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.
  • the value of the feature vector is a floating point number.
  • 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.
  • 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.
  • 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.
  • the technical solution of the present disclosure, 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 disk, or an optical disk), and includes a number of instructions for 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 disclosure.
  • 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.
  • the term "module” can be a combination of software and/or hardware that implements a predetermined function.
  • 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.
  • FIG3 is a structural block diagram of a repair patch generating device according to an embodiment of the present disclosure. As shown in FIG3 , the device includes: a conversion module 10 , a traversal module 20 and a generation module 30 .
  • the conversion module 10 is configured to construct the program code containing the 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;
  • a traversal module 20 is configured 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;
  • the generation module 30 is configured 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.
  • the above modules may be implemented by software or hardware. For the latter, it may be implemented in the following ways, but not limited thereto: the above modules are all located in the same processor; or the above modules are located in different processors in any combination.
  • FIG4 is a schematic diagram of a repair patch generation process according to an embodiment of the present disclosure. As shown in FIG4 , the process involves the following modules: an abstract type system, a type flow graph constructor, a type flow graph encoder, and a syntax-guided generator.
  • the abstract type system defines a set of rules that can abstract the types of variables, constants, and operators in program source code and convert the program source code into an abstract syntax tree with type information.
  • 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, the embodiment of the present disclosure can organically integrate the grammatical rules and type rules of the program source code.
  • 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.
  • 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.
  • the abstract type system defines the following five rules:
  • 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
  • the subtype of the empty type is the empty type.
  • 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.
  • 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.
  • 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.
  • 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:
  • 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).
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • An edge in the Anode-Vnode set is the inverse of the corresponding edge in the Vnode-Anode set.
  • the edge in the Vnode-Vnode set represents the compatibility relationship between two variable types.
  • the type flow graph constructor converts the typed abstract syntax tree into a type flow graph that satisfies the above rules. picture.
  • 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 shown in Figure 5.
  • 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.
  • 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.
  • 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.
  • Figure 6 is a flowchart of the method for automatically repairing program defects based on type constraints according to an embodiment of the disclosed scenario.
  • defective code Java example defective code snippet (hereinafter referred to as defective code) in Figure 7(a) as an example, the following describes in detail how to repair the defect in Figure 7(a) through the method for automatically repairing program defects in this embodiment.
  • defect code lines that begin with "-" are defect code lines
  • defect code lines that begin with "+” are corresponding repair code lines.
  • loc0 is of boolean type and the other three variables are of double type.
  • 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.
  • the method for automatically repairing program defects based on type constraints in this embodiment includes the following steps:
  • 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.
  • a large amount of program source code is extracted to obtain multiple typed abstract syntax trees, and each typed abstract syntax tree is traversed to obtain multiple grammar rules. After removing duplicates from these grammar rules, they are combined to obtain a typed grammar rule vocabulary, which can be used by subsequent neural networks when generating an abstract syntax tree for repairing the code.
  • 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.
  • an abstract type system is used to convert the defect code into an abstract syntax tree with types.
  • the obtained abstract syntax tree is shown in FIG. 7( b ), wherein the type information in the abstract syntax tree is determined as follows:
  • 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.
  • the type of a parent node is either the same as the type of all its child nodes, or the parent type of all its child node types.
  • 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.
  • step S602 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.
  • the type flow graph contains two types of nodes:
  • 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;
  • 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.
  • Its type attribute is a numeric type, and its replicability attribute is 0 because the node depends on the variable "loc2".
  • variable node Vnode represented by an ellipse in Figure 8
  • the value of the type attribute is consistent with the actual type of the variable, and the accessibility attribute is 0 or 1.
  • the type flow graph also contains four types of edges with 11 attributes, for example, as shown in Figure 8:
  • 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;
  • 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";
  • 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;
  • edge attributes from ellipse node "loc0" to ellipse node “loc1", ellipse node “loc2”, and ellipse node “loc3” all indicate incompatible types.
  • 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.
  • traversing the type flow graph of FIG8 can obtain three data: node sequence, attribute sequence, and relationship matrix, including:
  • the Vnode sequence does not contain the ellipse node "loc3", that is, the Vnode sequence is:
  • Vnode sequence is a sequence fragment of variable names that can be accessed at the defect code.
  • the node sequence is the concatenation of the Anode sequence and the Vnode sequence, that is:
  • 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.
  • the relationship matrix of the type flow graph is the adjacency matrix between the nodes of the type flow graph, and 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 corresponding to the edge of the two nodes, and the value range is 1 to 11, as shown in Table 1. If the two nodes are not adjacent, the value of the matrix is 0, and the node itself is considered to be non-adjacent. Since the edges of the type flow graph are directed, the values of the relationship matrix are viewed from rows to columns.
  • 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.
  • the node sequence, the attribute sequence, and the relationship matrix are passed through a type flow graph encoder to obtain a feature vector.
  • the node sequence, attribute sequence, and relationship matrix obtained above are input into the type flow graph encoder of Figure 4.
  • the node sequence, attribute sequence, and relationship matrix are first passed through an embedding layer to obtain a node embedding vector, an attribute embedding vector, and a relationship embedding vector, respectively.
  • These three embedding vectors are all floating-point matrices.
  • 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.
  • the entire type flow graph encoder is composed of N neural network blocks connected in series.
  • 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.
  • 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";
  • 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, as shown in FIG. 7(c).
  • step S609 instantiate the abstract syntax tree generated in step S608 into code to obtain a repair patch.
  • 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.
  • An embodiment of the present disclosure 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.
  • the above-mentioned computer-readable storage medium may include, but is not limited to: 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, and other media that can store computer programs.
  • An embodiment of the present disclosure 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.
  • 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.
  • modules or steps of the present disclosure 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.
  • the present disclosure is not limited to any specific combination of hardware and software.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Software Systems (AREA)
  • General Physics & Mathematics (AREA)
  • General Health & Medical Sciences (AREA)
  • Biomedical Technology (AREA)
  • Data Mining & Analysis (AREA)
  • Evolutionary Computation (AREA)
  • Biophysics (AREA)
  • Molecular Biology (AREA)
  • Computing Systems (AREA)
  • Computational Linguistics (AREA)
  • Artificial Intelligence (AREA)
  • Mathematical Physics (AREA)
  • Life Sciences & Earth Sciences (AREA)
  • Health & Medical Sciences (AREA)
  • Computer Security & Cryptography (AREA)
  • Stored Programmes (AREA)

Abstract

本公开实施例提供了一种修复补丁生成方法、存储介质及电子装置。该方法包括:将含有缺陷代码的程序代码构造成第一抽象语法树,通过类型流图构造器将所述第一抽象语法树转化为类型流图,其中,第一抽象语法树中携带类型信息;遍历类型流图获得节点序列、属性序列以及关系矩阵,将节点序列、属性序列、关系矩阵输入类型流图编码器,得到特征向量;将特征向量以及语法规则词表输入语法制导生成器,得到第二抽象语法树,并对第二抽象语法树进行代码实例化,以生成修复补丁,其中,所述第二抽象语法树中携带所述类型信息。

Description

修复补丁生成方法、存储介质及电子装置
相关申请的交叉引用
本公开基于2023年6月14日提交的发明名称为“修复补丁生成方法、存储介质及电子装置”的中国专利申请CN202310709070.3,并且要求该专利申请的优先权,通过引用将其所公开的内容全部并入本公开。
技术领域
本公开实施例涉及计算机技术领域,具体而言,涉及一种修复补丁生成方法、存储介质及电子装置。
背景技术
目前,基于深度学习的语法制导的程序缺陷修复方法,仅能保证生成的修复补丁符合语法规则,但深度学习模型中的神经网络无法感知到程序的类型规则,容易生成大量的类型不正确补丁,尽管这些补丁最终可以被编译器过滤掉,但在编译大型工程时,过滤这些类型不正确补丁需要耗费大量的时间,缺陷修复系统的效率较低;而使用搜索算法来确定概率较高的语法规则路径,神经网络还可能未类型不正确的语法规则赋予较高概率,如此,可能将正确补丁排除在候选补丁之外,导致无法修复缺陷。
如何抽象代码的类型、神经网络如何表示并学习类型规则是有待解决的技术问题。
发明内容
本公开实施例提供了一种修复补丁生成方法、存储介质及电子装置,以至少解决相关技术中已有的深度学习缺陷代码修复方法缺少学习类型信息能力的问题。
根据本公开的一个实施例,提供了一种修复补丁生成方法,包括:将含有缺陷代码的程序代码构造成第一抽象语法树,通过类型流图构造器将所述第一抽象语法树转化为类型流图,其中,所述第一抽象语法树中携带类型信息;遍历所述类型流图获得节点序列、属性序列以及关系矩阵,将所述节点序列、所述属性序列、所述关系矩阵输入类型流图编码器,得到特征向量;将所述特征向量以及语法规则词表输入语法制导生成器,得到第二抽象语法树,并对所述第二抽象语法树进行代码实例化,以生成修复补丁,其中,所述第二抽象语法树中携带所述类型信息。
根据本公开的又一个实施例,还提供了一种计算机可读存储介质,所述计算机可读存储介质中存储有计算机程序,其中,所述计算机程序被设置为运行时执行上述任一项方法实施例中的步骤。
根据本公开的又一个实施例,还提供了一种电子装置,包括存储器和处理器,所述存储器中存储有计算机程序,所述处理器被设置为运行所述计算机程序以执行上述任一项方法实施例中的步骤。
附图说明
图1是本公开实施例的运行修复补丁生成方法的计算机终端的硬件结构框图;
图2根据本公开实施例的修复补丁生成方法的流程图;
图3是根据本公开实施例的修复补丁生成装置的结构框图;
图4是根据本公开实施例的修复补丁生成流程的示意图;
图5是根据本公开实施例的类型流图编码器内部结构的示意图;
图6是根据本公开场景实施例的基于类型约束的程序缺陷自动修复方法的流程图;
图7是根据本公开实施例的抽象语法树及缺陷代码的示意图;
图8是根据本公开实施例的类型流图的示意图。
具体实施方式
下文中将参考附图并结合实施例来详细说明本公开的实施例。
本公开的说明书和权利要求书及上述附图中的术语“第一”、“第二”等是用于区别类似的对象,而不必用于描述特定的顺序或先后次序。
本申请实施例中所提供的方法实施例可以在移动终端、计算机终端或者类似的运算装置中执行。以运行在计算机终端上为例,图1是本公开实施例的运行修复补丁生成方法的计算机终端的硬件结构框图。如图1所示,计算机终端可以包括一个或多个(图1中仅示出一个)处理器102(处理器102可以包括但不限于微处理器或可编程逻辑器件的处理装置)和用于存储数据的存储器104,其中,上述计算机终端还可以包括用于通信功能的传输设备106以及输入输出设备108。本领域普通技术人员可以理解,图1所示的结构仅为示意,其并不对上述计算机终端的结构造成限定。例如,计算机终端还可包括比图1中所示更多或者更少的组件,或者具有与图1所示不同的配置。
存储器104可用于存储计算机程序,例如,应用软件的软件程序以及模块,如本公开实施例中的修复补丁生成方法对应的计算机程序,处理器102通过运行存储在存储器104内的计算机程序,从而执行各种功能应用以及数据处理,即实现上述的方法。存储器104可包括高速随机存储器,还可包括非易失性存储器,如一个或者多个磁性存储装置、闪存、或者其他非易失性固态存储器。在一些实例中,存储器104可进一步包括相对于处理器102远程设置的存储器,这些远程存储器可以通过网络连接至计算机终端。上述网络的实例包括但不限于互联网、企业内部网、局域网、移动通信网及其组合。
传输设备106用于经由一个网络接收或者发送数据。上述的网络具体实例可包括计算机终端的通信供应商提供的无线网络。在一个实例中,传输设备106包括一个网络适配器(Network Interface Controller,简称为NIC),其可通过基站与其他网络设备相连从而可与互联网进行通讯。在一个实例中,传输设备106可以为射频(Radio Frequency,简称为RF)模块,其用于通过无线方式与互联网进行通讯。
在本实施例中提供了一种运行于上述计算机终端的修复补丁生成方法,图2根据本公开实施例的修复补丁生成方法的流程图,如图2所示,该流程包括如下步骤:
步骤S202,将含有缺陷代码的程序代码构造成第一抽象语法树,通过类型流图构造器将所述第一抽象语法树转化为类型流图,其中,所述第一抽象语法树中携带类型信息;
在本实施例中,抽象类型系统定义了一套规则,可以对程序源码的变量、常量、操作符 的类型进行抽象,从而将程序源码转换为带类型信息的抽象语法树。因此,在步骤S202之前,该方法还包括:通过抽象类型系统将程序源码语料转换为多棵第三抽象语法树,其中,所述第三抽象语法树中携带所述类型信息;遍历所有所述第三抽象语法树得到所述语法规则词表,其中,所述语法规则词表中携带所述类型信息,该词表可用于后续神经网络生成修复代码抽象语法树。其中,该规则也用于将含有缺陷代码的程序代码构造成第一抽象语法树。
在一个实施例中,抽象类型系统定义的规则包括:1)将程序代码或所述程序源码语料转换为抽象语法树,程序代码或所述程序源码语料语句中的变量、常量、操作符是抽象语法树的叶子节点;2)定义五种抽象类型:通用类型、字符串类型、数值类型、布尔类型和空类型,其中,空类型是字符串类型、数值类型、布尔类型的子类型;通用类型是字符串类型、数值类型、布尔类型的父类型;通用类型的父类型是通用类型,空类型的子类型是空类型;3)根据上述定义的类型,程序源码中的变量或常量可抽象为字符串类型、数值类型、布尔类型中的一种,NULL归入空类型;4)操作符的类型由它的操作数类型决定,如果一个操作符的所有操作数类型相同,则操作符类型与操作数类型一样;如果操作数类型有两种及以上,则操作符类型是所有操作数类型的父类型;5)抽象语法树的非叶子节点类型由其子节点类型决定,如果一个非叶子节点的所有子节点类型相同,则该节点的类型与其子节点类型一样;如果子节点类型有两种及以上,则该节点类型是所有子节点类型的父类型。
通过抽象类型系统定义的规则,可对程序源码或缺陷代码中的变量、常量、操作符的类型进行抽象,并在构造抽象语法树时,将抽象的各类型信息对应地携带于抽象语法树上,从而得到一种带类型的抽象语法树。
在本实施例的步骤S202中,通过类型流图构造器将所述第一抽象语法树转化为类型流图,包括:将所述第一抽象语法树中的各节点转换为所述类型流图中抽象语法树节点;将所述程序代码中的变量抽象为所述类型流图中的变量节点;其中,所述抽象语法树节点之间、变量节点之间、对应于同一变量的所述抽象语法树节点和所述变量节点之间,由不同属性的边连接。
其中,所述抽象语法树节点之间的边的属性包括:父-子、子-父、左兄弟、右兄弟;所述变量节点之间的边的属性包括:相同类型、兼容类型、不兼容类型;对应于同一变量的连接所述抽象语法树节点和所述变量节点之间的边的属性包括:申明-变量、使用-变量、变量-申明、变量-使用。
步骤S204,遍历所述类型流图获得节点序列、属性序列以及关系矩阵,将所述节点序列、所述属性序列、所述关系矩阵输入类型流图编码器,得到特征向量;
在本实施例中,类型流图编码器为多个神经网络层的有机堆叠,其中,多个神经网络层包括:嵌入层、类型敏感的自注意神经元、门控层、线性层,除嵌入层的输入为外界输入的节点序列、属性序列以及关系矩阵外,其余神经网络层的输入为前一个神经网络层的输出。
在一个示例性实施例中,遍历所述类型流图获得节点序列,包括:遍历所述类型流图中所述第一抽象语法树的抽象语法树节点名称,将所有所述抽象语法树节点名称组成第一序列片段;按照所述程序代码上下文中各变量的出现顺序,遍历所述第一抽象语法树中的变量节点名称,将缺陷代码处可访问的变量节点名称组成第二序列片段;将所述第一序列片段和所述第二序列片段进行拼接,得到所述节点序列。
在一个示例性实施例中,遍历所述类型流图获得属性序列,包括:按照所述节点序列中 抽象语法树节点和变量节点的排列顺序,依次获取各所述抽象语法树节点和各所述变量节点对应的节点属性值,并将所有所述节点属性值组合为所述属性序列。
在本实施例中,所述抽象语法树节点包括以下属性:类型属性、可复制性;所述变量节点包括以下属性:类型属性、可访问性。
在本实施例的步骤S204中,将所述节点序列、所述属性序列、所述关系矩阵输入类型流图编码器,得到一个特征向量,包括:通过所述类型流图编码器中的嵌入层,基于输入的所述节点序列、所述属性序列、所述关系矩阵,得到节点嵌入向量、属性嵌入向量和关系嵌入向量;通过所述类型流图编码器中的类型敏感的自注意神经元,对所述节点嵌入向量和所述关系嵌入向量进行融合,得到第一融合向量;通过所述类型流图编码器中的门控层,对所述属性嵌入向量和所述第一融合向量进行融合,得到第二融合向量;通过所述类型流图编码器中的线性层,对所述第二融合向量进行线性变换得到所述特征向量。
通过类型流图及其构造规则,可记录带类型的抽象语法树节点与用户自定义的变量节点之间的邻接关系,实现了将程序源码或缺陷代码的语法规则和类型规则进行了有机融合。
步骤S206,将所述特征向量以及语法规则词表输入语法制导生成器,得到第二抽象语法树,并对所述第二抽象语法树进行代码实例化,以生成修复补丁,其中,所述第二抽象语法树中携带所述类型信息。
通过类型流图编码器及其神经网络架构,通过提取类型流图的节点序列、属性序列和关系矩阵,对程序源码的语法规则和类型规则利用神经网络进行编码,可在补丁生成过程中赋予修复补丁(代码)类型约束。
在一个示例性实施例中,所述特征向量的值为浮点数。
在本实施例的步骤S206中,将所述特征向量以及语法规则词表输入语法制导生成器,得到第二抽象语法树,包括:将所述特征向量和所述语法规则词表输入语法制导生成器,预测第一语法规则,并根据所述第一语法规则进行抽象语法树的编辑操作,得到第一部分抽象语法树,其中,所述第一语法规则携带类型信息;基于所述第一部分抽象语法树、所述特征向量以及所述语法规则词表,预测第二语法规则,并根据所述第二语法规则进行抽象语法树的编辑操作,得到第二部分抽象语法树,其中,所述第二语法规则携带类型信息;迭代进行预测语法规则步骤和编辑抽象语法树步骤,直到得到完整的所述第二抽象语法树。
通过上述步骤,将缺陷代码构造成可携带类型信息的第一抽象语法树,实现了对代码类型进行抽象;而由第一抽象语法树转化而来的类型流图中,可将程序代码中的语法规则与类型规则进行有机融合,进而,基于类型流图得到的特征向量与语法规则词表输入语法制导生成器而得到的第二抽象语法树将携带类型信息,而由第二抽象语法树得到的修复补丁代码便会被赋予类型约束。因此,可以解决相关技术中已有的深度学习缺陷代码修复方法缺少学习类型信息能力的问题,达到扩展已有的深度学习缺陷代码修复方法的能力的效果。
通过以上的实施方式的描述,本领域的技术人员可以清楚地了解到根据上述实施例的方法可借助软件加必需的通用硬件平台的方式来实现,当然也可以通过硬件,但很多情况下前者是更佳的实施方式。基于这样的理解,本公开的技术方案本质上或者说对现有技术做出贡献的部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质(如ROM/RAM、磁碟、光盘)中,包括若干指令用以使得一台终端设备(可以是手机,计算机,服务器,或者网络设备等)执行本公开各个实施例所述的方法。
在本实施例中还提供了一种修复补丁生成装置,该装置用于实现上述实施例及优选实施方式,已经进行过说明的不再赘述。如以下所使用的,术语“模块”可以实现预定功能的软件和/或硬件的组合。尽管以下实施例所描述的装置较佳地以软件来实现,但是硬件,或者软件和硬件的组合的实现也是可能并被构想的。
图3是根据本公开实施例的修复补丁生成装置的结构框图,如图3所示,该装置包括:转化模块10、遍历模块20以及生成模块30。
转化模块10,设置为将含有缺陷代码的程序代码构造成第一抽象语法树,通过类型流图构造器将所述第一抽象语法树转化为类型流图,其中,所述第一抽象语法树中携带类型信息;
遍历模块20,设置为遍历所述类型流图,获得节点序列、属性序列以及关系矩阵,将所述节点序列、所述属性序列、所述关系矩阵输入类型流图编码器,得到一个特征向量;
生成模块30,设置为将所述特征向量以及语法规则词表输入语法制导生成器,得到第二抽象语法树,并对所述第二抽象语法树进行代码实例化,以生成修复补丁,其中,所述第二抽象语法树中携带所述类型信息。
上述各个模块是可以通过软件或硬件来实现的,对于后者,可以通过以下方式实现,但不限于此:上述模块均位于同一处理器中;或者,上述各个模块以任意组合的形式分别位于不同的处理器中。
为便于对本公开所提供的技术方案的理解,下面将结合具体场景的实施例进行详细的阐述。
图4是根据本公开实施例的修复补丁生成流程的示意图,如图4所示,该流程中涉及以下模块:抽象类型系统、类型流图构造器、类型流图编码器、语法制导生成器。
在一个实施例中,抽象类型系统定义了一套规则,可以对程序源码的变量、常量、操作符的类型进行抽象,将程序源码转换为带类型信息的抽象语法树。
类型流图构造器,用于将抽象语法树转换为类型流图,用于记录带类型的抽象语法树节点、用户自定义变量节点之间的邻接关系;基于类型流图及其构造规则,本公开实施例可将程序源码的语法规则和类型规则进行有机融合。
类型流图编码器,用于基于类型流图中记录的节点序列、属性序列和关系矩阵生成特征向量,通过提取类型流图的节点序列、属性序列和关系矩阵,对程序源码的语法规则和类型规则利用神经网络进行编码,可在补丁生成过程中对修复补丁赋予类型约束。
在一个实施例中,抽象类型系统定义了以下五条规则:
1)将程序源码转换为抽象语法树,程序源码语句中的变量、常量、操作符是抽象语法树的叶子节点。
2)定义五种抽象类型:通用类型、字符串类型、数值类型、布尔类型和空类型。其中,空类型是字符串类型、数值类型、布尔类型的子类型;通用类型是字符串类型、数值类型、布尔类型的父类型;通用类型的父类型是通用类型,空类型的子类型是空类型。
3)根据上述定义的类型,程序源码中的变量或常量可抽象为字符串类型、数值类型、布尔类型中的一种,NULL归入空类型。
4)操作符的类型由它的操作数类型决定,如果一个操作符的所有操作数类型相同,则操作符类型与操作数类型一样;如果操作数类型有两种及以上,则操作符类型是所有操作数类型的父类型。
5)抽象语法树的非叶子节点类型由其子节点类型决定,如果一个非叶子节点的所有子节点类型相同,则该节点的类型与其子节点类型一样;如果子节点类型有两种及以上,则该节点类型是所有子节点类型的父类型。
在一个实施例中,类型流图是一种图的数据结构,包含节点集合和边集合。类型流图的构造规则如下:
1)节点集合包含抽象语法树节点子集(其中的元素我们称为Anode)和变量节点子集(其中的元素我们称为Vnode)。
2)每个Anode拥有两个属性:类型属性和可复制性属性,类型属性的取值是五种抽象类型的一种;可复制属性是一个布尔值,表示以该节点为根节点的抽象语法子树是否依赖于局部上下文的变量,以便该抽象语法子树可以独立复制到其他位置。
3)每个Vnode表示上下文的一个用户自定义元素(变量或参数),上下文是指抽象语法树对应的程序源码范围。每个Vnode拥有两个属性:类型属性和可访问性属性,类型属性的值是该元素定义时的实际类型;可访问性属性是一个布尔值,表示在缺陷代码位置是否可以访问该元素。
4)边集合包含Anode-Anode,Anode-Vnode,Vnode-Anode和Vnode-Vnode四个子集。边集合中的边是有向的,拥有一个属性标签,具体的边属性信息如表1所示。
表1
5)Anode-Anode集合中的边表示的是抽象语法树节点之间的语法关系,两个Anode之间的关系是父-子、子-父、左兄弟、右兄弟四种关系之一。
6)Vnode-Anode集合中的边表示的是上下文的变量和抽象语法树节点之间的关系,如果变量x的申明语句对应抽象语法子树的根节点是y,那么x有一条“变量-申明”属性的边连接到y;同理,如果抽象语法树节点m使用到变量n,那么n有一条“变量-使用”属性的边连接到m。
7)Anode-Vnode集合中的边是Vnode-Anode集合中对应边的反转。
8)Vnode-Vnode集合中的边表示的是两个变量类型之间的兼容关系。
在一个实施例中,类型流图构造器将带类型的抽象语法树转换为满足上述规则的类型流 图。
在一个实施例中,类型流图编码器是一系列神经网络层的有机堆叠,作用是通过学习将类型流图信息编码为一个特征向量,其内部结构如图5所示。
如图5所示,类型流图编码器的输入是属性序列、节点序列和关系矩阵,输出是特征向量。三个输入分别通过各自的嵌入层得到属性嵌入向量、节点嵌入向量和关系嵌入向量。节点嵌入向量和关系嵌入向量通过类型敏感的自注意神经元进行融合,然后和属性嵌入向量通过门控层,最后通过线性层做线性变换。
在本实施例中,嵌入层、类型敏感的自注意神经元、门控层和线性层均来自业界公开的神经网络单元,它们一起构成了神经网络块。
在本实施例中,类型流图编码器由N个神经网络块串联而成,其中,N为大于0的整数,除第一个神经网络块的输入是属性序列、节点序列和关系矩阵的嵌入向量,其他神经网络块的输入都是前一个神经网络块的输出。
图6是根据本公开场景实施例的基于类型约束的程序缺陷自动修复方法的流程图,以图7(a)中的Java示例缺陷代码片段(以下简称为缺陷代码)为例,下面详细描述通过本实施例的程序缺陷自动修复方法修复图7(a)中的缺陷。
在本实施例中,缺陷代码中以“-”开头的是缺陷代码行,以“+”开头的是对应的修复代码行。
如图7(a)所示,在缺陷代码中定义了四个变量:loc0、loc 1、loc2、loc3,其中,loc 0为boolean类型,其余三个变量是double类型。变量loc0、loc1和loc2在缺陷代码行之前定义,因此在缺陷代码处可以访问;变量loc3在缺陷代码行之后定义,在缺陷代码处不能访问。
在一个实施例中,如图6所示,本实施例中的基于类型约束的程序缺陷自动修复方法包括以下步骤:
S601,收集大量的程序源码语料,通过抽象类型系统转换为多棵带类型的抽象语法树,通过遍历抽象语法树,得到带类型的语法规则词表。
在本实施例中,对大量的程序源码进行提取,可以得到多棵带类型的抽象语法树,对每一棵带类型的抽象语法树进行遍历,可以得到多条语法规则。将这些语法规则去重后,组合起来得到一个带类型的语法规则词表,该词表可供后续神经网络在生成修复代码的抽象语法树时使用。
S602,缺陷代码通过抽象类型系统得到带类型的抽象语法树,然后通过类型流图构造器得到类型流图。
在本实施例中,使用抽象类型系统将缺陷代码转换为带类型的抽象语法树,得到的抽象语法树如图7(b)所示,其中,抽象语法树中的类型信息的确定如下:
例如:根据抽象类型系统的定义,操作符的类型要么和所有操作数的类型一致,要么是所有操作数类型的父类型。操作符“-”有两个操作数“loc2”和“1”,“loc2”的实际类型是double,抽象为数值类型;“1”是数值常量,也抽象为数值类型,因此,操作符“-”的类型与它的两个操作数类型保持一致,即数值类型。抽象语法树中其他操作符类型的推导类似。
又例如,根据抽象类型系统的定义,父节点的类型要么和所有子节点类型相同,要么是所有子节点类型的父类型,如图7(b)所示,父节点“BinOp_N”有三个子节点“L-Op_N”、“Operator_N”和“R-Op_N”,这三个子节点都是数值类型,因此父节点“BinOp_N”也是数值类型,其他父节点的类型推导类似。
与步骤S601相同,对步骤S602中得到的带类型的抽象语法树进行子节点遍历,可以得到一系列的语法规则,例如:“Assignment_N->L-Expr_N AssignOp_N R-Expr_N”,它表示可以从“Assignment_N”节点推出“L-Expr_N”、“AssignOp_N”和“R-Expr_N”三个子节点。
根据类型流图的定义,我们将图7(b)的带类型的抽象语法树转换为类型流图,得到的类型流图如图8所示。
如图8所示,类型流图中包含两类节点:
(1)抽象语法树节点Anode(图8中以矩形框表示),它拥有类型属性和可复制性属性。
类型属性取值为抽象类型系统定义的五种类型之一,即通用类型、字符串类型、数值类型、布尔类型和空类型;
可复制性属性取值为0或1,取值0表示该节点依赖于上下文的变量,以该节点为根节点的子树无法复制到其他地方复用。以“BinOp_N”节点为例,它的类型属性是数值类型,它的可复制性属性为0,因为该节点依赖于变量“loc2”。
(2)另一类节点是变量节点Vnode(图8中以椭圆框表示),它拥有类型属性和可访问性属性。
类型属性的取值与变量的实际类型一致,可访问性属性取值为0或1,取值0表示在缺陷代码行处无法访问该变量。以椭圆节点“loc3”为例,它的类型是double,它的可访问属性为0,因为该节点对应的变量语句“double loc3=0”定义在缺陷代码“loc1=loc2-1”之后,在缺陷代码处无法访问该变量。
类型流图还包含四类共11种属性的边,例如:如图8所示:
节点“BinOp_N”到节点“L-Op_N”的边属于Anode-Anode父子属性,对应表1的属性编号为1;
矩形节点“loc1”到椭圆节点“loc1”的边属于Anode-Vnode使用变量属性,表示矩形节点“loc1”使用了椭圆节点“loc1”,对应表1的属性编号为6;
椭圆节点“loc1”到矩形节点“loc1”的边属于Vnode-Anode变量使用属性,表示矩形节点“loc1”使用了椭圆节点“loc1”,对应表1的属性编号为8;矩形节点“loc2”到椭圆节点“loc2”之间的两个边同理;
椭圆节点“loc1”到椭圆节点“loc2”边的边属性、椭圆节点“loc1”到椭圆节点“loc3”边、椭圆节点“loc2”到椭圆节点“loc3”边的边属性均表示类型相同;
椭圆节点“loc0”到椭圆节点“loc1”、椭圆节点“loc2”、椭圆节点“loc3”之间的边属性均表示类型不兼容,例如:椭圆节点“loc1”和椭圆节点“loc2”的类型相同,椭圆节点“loc2”的类型double和椭圆节点“loc0”的类型boolean不兼容。还有另外一种边属性是类型兼容,例如在C语言中,类型int兼容类型int64。
S603,先序遍历类型流图中的节点名称,得到节点序列的第一部分,即Anode序列;再按照缺陷代码上下文中变量出现顺序遍历变量名称,基于变量名称确定节点序列的第二部分,即Anode序列,将两部分序列拼接得到完整的节点序列。
在一个实施例中,对图8的类型流图进行遍历可以得到节点序列、属性序列、关系矩阵三个数据,包括:
对图8中的带类型抽象语法树节点进行先序遍历,得到Anode序列:
[Assignment_N,L-Expr_N,loc1,AssignOp_N,=,R-Expr_N,BinOp_N,L-Op_N,loc2, Operator_N,-,R-Op_N,1];
由于椭圆节点“loc3”的可访问性属性为0,缺陷代码行无法访问变量loc3,因此,Vnode序列不包含椭圆节点“loc3”,即Vnode序列为:
[loc0,loc1,loc2]。
即Vnode序列为截取缺陷代码处可以访问的变量名称序列片段。
节点序列是Anode序列和Vnode序列的拼接,即:
[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]。
S604,按照节点序列的顺序获取各节点的节点属性值,得到属性序列。
基于上述节点序列并按照节点序列的顺序,将节点序列中各变量替换为相对应的类型,可得到一个属性序列:[(数值,0),(数值,0),(数值,0),(数值,1),(数值,1),(数值,0),(数值,0),(数值,0),(数值,0),(数值,1),(数值,1),(数值,1),(数值,1),(boolean,1),(double,1),(double,1)]。
其中,前13个为Anode节点,包含类型属性和可复制性属性,“0”代表不可复制,“1”代表可复制;后3个为Vnode节点,包含类型属性和可访问性属性,“1”代表可访问。
S605,获取类型流图节点的关系矩阵;
在一个实施例中,类型流图的关系矩阵是该类型流图的各节点之间的邻接矩阵,矩阵尺寸为16x16,部分关系矩阵如表2所示。关系矩阵的行和列都是节点序列,如果两个节点邻接,则矩阵的值是对应两个节点边的属性编号,取值范围为1到11,如表1所示。如果两个节点不邻接,矩阵的取值为0,节点自身与自身之间视为不邻接。由于类型流图的边是有向的,因此,关系矩阵的取值从行往列看。
例如:表2的第1行第2列(不算表头)的值为1,表示节点“Assignment_N”到节点“L-Expr_N”是父子节点关系。
表2
S606,将节点序列、属性序列、关系矩阵通过类型流图编码器得到特征向量。
在一个实施例中,将上述得到的节点序列、属性序列、关系矩阵输入图4的类型流图编码器,节点序列、属性序列、关系矩阵首先通过嵌入层分别得到节点嵌入向量、属性嵌入向量和关系嵌入向量,这三个嵌入向量均是浮点数矩阵。
节点嵌入向量和关系嵌入向量通过类型敏感的自注意神经元进行融合,融合后的向量与属性嵌入向量通过门控层融合,最后通过线性层做线性变换,输出第一个神经网络块的特征向量。
整个类型流图编码器由N个神经网络块串联构成,在本实施例中,N的取值为8,后一个神经网络块的三个输入通过复制前一个神经网络块的输出得到,最后一个神经网络块的输出是整个类型流图编码器的输出,即输出一个特征向量,值为浮点数。
S607,将特征向量、语法规则词表输入语法制导生成器,预测得到一条带类型的语法规则,表示一个对抽象语法树节点的编辑操作。
在一个实施例中,语法制导生成器根据输入的特征向量和带类型的语法规则词表,开始生成第一条语法规则,例如:“Assignment_N->L-Expr_N AssignOp_N R-Expr_N”;
S608,预测语法规则和编辑抽象语法树迭代地进行,直到生成一棵完整的抽象语法树,该抽象语法树为一棵不同于步骤S602中基于缺陷代码得到的抽象语法树。
在一个实施例中,在步骤S607中的第一条语法规则基础上生成第二条语法规则,例如:“L-Expr_N->loc1”,生成过程迭代地进行,直到生成一棵完整的抽象语法树,如图7(c)所示。
S609,将步骤S608生成的抽象语法树进行代码实例化,得到修复补丁。
在一个实施例中,生成的抽象语法树需要进一步还原为程序源码“loc1=1;”即为缺陷代码“loc1=loc2-1;”的修复代码(修复补丁)。即这段程序的修复是将loc1的值从“loc2-1”改为常量1。
本公开上述各实施例可应用于程序缺陷修复工具中。
通过本公开上述实施例,可根据输入的缺陷代码,输出能同时满足语法规则和类型约束的修复补丁,扩展了现有语法制导缺陷修复方法的能力,规避生成类型不正确的补丁,提高缺陷修复正确率,从而提高软件开发和维护的效率,降低人力成本。
本公开的实施例还提供了一种计算机可读存储介质,该计算机可读存储介质中存储有计算机程序,其中,该计算机程序被设置为运行时执行上述任一项方法实施例中的步骤。
在一个示例性实施例中,上述计算机可读存储介质可以包括但不限于:U盘、只读存储器(Read-Only Memory,简称为ROM)、随机存取存储器(Random Access Memory,简称为RAM)、移动硬盘、磁碟或者光盘等各种可以存储计算机程序的介质。
本公开的实施例还提供了一种电子装置,包括存储器和处理器,该存储器中存储有计算机程序,该处理器被设置为运行计算机程序以执行上述任一项方法实施例中的步骤。
在一个示例性实施例中,上述电子装置还可以包括传输设备以及输入输出设备,其中,该传输设备和上述处理器连接,该输入输出设备和上述处理器连接。
本实施例中的具体示例可以参考上述实施例及示例性实施方式中所描述的示例,本实施例在此不再赘述。
显然,本领域的技术人员应该明白,上述的本公开的各模块或各步骤可以用通用的计算装置来实现,它们可以集中在单个的计算装置上,或者分布在多个计算装置所组成的网络上,它们可以用计算装置可执行的程序代码来实现,从而,可以将它们存储在存储装置中由计算装置来执行,并且在某些情况下,可以以不同于此处的顺序执行所示出或描述的步骤,或者将它们分别制作成各个集成电路模块,或者将它们中的多个模块或步骤制作成单个集成电路模块来实现。这样,本公开不限制于任何特定的硬件和软件结合。
以上所述仅为本公开的优选实施例而已,并不用于限制本公开,对于本领域的技术人员来说,本公开可以有各种更改和变化。凡在本公开的原则之内,所作的任何修改、等同替换、改进等,均应包含在本公开的保护范围之内。

Claims (17)

  1. 一种修复补丁生成方法,包括:
    将含有缺陷代码的程序代码构造成第一抽象语法树,通过类型流图构造器将所述第一抽象语法树转化为类型流图,其中,所述第一抽象语法树中携带类型信息;
    遍历所述类型流图获得节点序列、属性序列以及关系矩阵,将所述节点序列、所述属性序列、所述关系矩阵输入类型流图编码器,得到特征向量;
    将所述特征向量以及语法规则词表输入语法制导生成器,得到第二抽象语法树,并对所述第二抽象语法树进行代码实例化,以生成修复补丁,其中,所述第二抽象语法树中携带所述类型信息。
  2. 根据权利要求1所述的方法,其中,将含有缺陷代码的程序代码构造成第一抽象语法树之前,所述方法还包括:
    通过抽象类型系统将程序源码语料转换为多棵第三抽象语法树,其中,所述第三抽象语法树中携带所述类型信息;
    遍历所有所述第三抽象语法树得到所述语法规则词表,其中,所述语法规则词表中携带所述类型信息。
  3. 根据权利要求1或2所述的方法,其中,所述第一抽象语法树中的叶子节点由抽象所述程序代码中的常量、变量、操作符而得到;
    所述第三抽象语法树中的叶子节点由抽象所述程序源码语料中的常量、变量、操作符而得到。
  4. 根据权利要求3所述的方法,其中,所述类型信息包括以下类型:通用类型、字符串类型、数值类型、布尔类型和空类型;
    所述空类型是所述字符串类型、所述数值类型、所述布尔类型的子类型;
    所述通用类型是所述字符串类型、所述数值类型、所述布尔类型的父类型;
    所述通用类型的父类型是通用类型,空类型的子类型是空类型。
  5. 根据权利要求4所述的方法,其中,
    所述程序代码或所述程序源码语料中的常量的类型包括以下之一:字符串类型、数值类型、布尔类型;
    所述程序代码或所述程序源码语料中的变量的类型包括以下之一:字符串类型、数值类型、布尔类型;
    所述程序代码或所述程序源码语料中的操作符的类型由其操作数类型决定;
    所述程序代码或所述程序源码语料中的NULL归类为所述空类型。
  6. 根据权利要求5所述的方法,其中,所述程序代码或所述程序源码语料中的操作符的类型由其操作数类型决定,包括:
    在所述操作符的所有操作数类型相同的情况下,所述操作符类型与其操作数的类型相同;
    在所述操作符的操作数的存在多种类型的情况下,所述操作符类型为其所有操作数类型的父类型。
  7. 根据权利要求4所述的方法,其中,
    所述第一抽象语法树或所述第三抽象语法中的非叶子节点类型由其子节点类型决定;
    在所述非叶子节点的所有子节点的类型相同的情况下,所述非叶子节点的类型与其子节点的类型相同;
    在所述非叶子节点的子节点存在多种类型的情况下,所述非叶子节点的类型为其所有子节点类型的父类型。
  8. 根据权利要求1所述的方法,其中,通过类型流图构造器将所述第一抽象语法树转化为类型流图,包括:
    将所述第一抽象语法树中的各节点转换为所述类型流图中抽象语法树节点;
    将所述程序代码中的变量抽象为所述类型流图中的变量节点;
    其中,所述抽象语法树节点之间、变量节点之间、对应于同一变量的所述抽象语法树节点和所述变量节点之间,由不同属性的边连接。
  9. 根据权利要求8所述的方法,其中,
    所述抽象语法树节点之间的边的属性包括:父-子、子-父、左兄弟、右兄弟;
    所述变量节点之间的边的属性包括:相同类型、兼容类型、不兼容类型;
    对应于同一变量的连接所述抽象语法树节点和所述变量节点之间的边的属性包括:申明-变量、使用-变量、变量-申明、变量-使用。
  10. 根据权利要求8所述的方法,其中,遍历所述类型流图获得节点序列,包括:
    遍历所述类型流图中所述第一抽象语法树的抽象语法树节点名称,将所有所述抽象语法树节点名称组成第一序列片段;
    按照所述程序代码上下文中各变量的出现顺序,遍历所述第一抽象语法树中的变量节点名称,将缺陷代码处可访问的变量节点名称组成第二序列片段;
    将所述第一序列片段和所述第二序列片段进行拼接,得到所述节点序列。
  11. 根据权利要求10所述的方法,其中,遍历所述类型流图获得属性序列,包括:
    按照所述节点序列中抽象语法树节点和变量节点的排列顺序,依次获取各所述抽象语法树节点和各所述变量节点对应的节点属性值,并将所有所述节点属性值组合为所述属性序列。
  12. 根据权利要求11所述的方法,其中,所述抽象语法树节点包括以下属性:类型属性、可复制性;所述变量节点包括以下属性:类型属性、可访问性。
  13. 根据权利要求1所述的方法,其中,将所述节点序列、所述属性序列、所述关系矩阵输入类型流图编码器,得到一个特征向量,包括:
    通过所述类型流图编码器中的嵌入层,并基于输入的所述节点序列、所述属性序列、所述关系矩阵,得到节点嵌入向量、属性嵌入向量和关系嵌入向量;
    通过所述类型流图编码器中的类型敏感的自注意神经元,对所述节点嵌入向量和所述关系嵌入向量进行融合,得到第一融合向量;
    通过所述类型流图编码器中的门控层,对所述属性嵌入向量和所述第一融合向量进行融合,得到第二融合向量;
    通过所述类型流图编码器中的线性层,对所述第二融合向量进行线性变换得到所述特征 向量。
  14. 根据权利要求1所述的方法,其中,所述特征向量的值为浮点数。
  15. 根据权利要求1所述的方法,其中,将所述特征向量以及语法规则词表输入语法制导生成器,得到第二抽象语法树,包括:
    将所述特征向量和所述语法规则词表输入语法制导生成器,预测第一语法规则,并根据所述第一语法规则进行抽象语法树的编辑操作,得到第一部分抽象语法树,其中,所述第一语法规则携带类型信息;
    基于所述第一部分抽象语法树、所述特征向量以及所述语法规则词表,预测第二语法规则,并根据所述第二语法规则进行抽象语法树的编辑操作,得到第二部分抽象语法树,其中,所述第二语法规则携带类型信息;
    迭代进行预测语法规则步骤和编辑抽象语法树步骤,直到得到完整的所述第二抽象语法树。
  16. 一种计算机可读存储介质,其中,所述计算机可读存储介质中存储有计算机程序,其中,所述计算机程序被处理器执行时实现所述权利要求1至15任一项中所述的方法的步骤。
  17. 一种电子装置,包括存储器、处理器以及存储在所述存储器上并可在所述处理器上运行的计算机程序,其中,所述处理器执行所述计算机程序时实现所述权利要求1至15任一项中所述的方法的步骤。
PCT/CN2024/090980 2023-06-14 2024-04-30 修复补丁生成方法、存储介质及电子装置 Ceased WO2024255477A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202310709070.3 2023-06-14
CN202310709070.3A CN119149039A (zh) 2023-06-14 2023-06-14 修复补丁生成方法、存储介质及电子装置

Publications (1)

Publication Number Publication Date
WO2024255477A1 true WO2024255477A1 (zh) 2024-12-19

Family

ID=93816115

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2024/090980 Ceased WO2024255477A1 (zh) 2023-06-14 2024-04-30 修复补丁生成方法、存储介质及电子装置

Country Status (2)

Country Link
CN (1) CN119149039A (zh)
WO (1) WO2024255477A1 (zh)

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20150135166A1 (en) * 2013-11-12 2015-05-14 Microsoft Corporation Source code generation, completion, checking, correction
CN113064586A (zh) * 2021-05-12 2021-07-02 南京大学 一种基于抽象语法树增广图模型的代码补全方法
CN114489669A (zh) * 2022-01-12 2022-05-13 南京大学 一种基于图学习的Python语言代码片段生成方法
CN115934147A (zh) * 2021-08-06 2023-04-07 中兴通讯股份有限公司 软件自动修复方法、系统、电子设备及存储介质

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20150135166A1 (en) * 2013-11-12 2015-05-14 Microsoft Corporation Source code generation, completion, checking, correction
CN113064586A (zh) * 2021-05-12 2021-07-02 南京大学 一种基于抽象语法树增广图模型的代码补全方法
CN115934147A (zh) * 2021-08-06 2023-04-07 中兴通讯股份有限公司 软件自动修复方法、系统、电子设备及存储介质
CN114489669A (zh) * 2022-01-12 2022-05-13 南京大学 一种基于图学习的Python语言代码片段生成方法

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
CAO YING KUI, SUN ZE YU, ZOU YAN ZHEN, XIE BING: "Structurally-Enhanced Approach for Automatic Code Change Transformation", JOURNAL OF SOFTWARE, SURVEY ON IN-BAND NETWORK TELEMETRY, vol. 32, no. 4, 1 January 2021 (2021-01-01), pages 1006 - 1022, XP093247891, ISSN: 1000-9825, DOI: 10.13328/j.cnki.jos.006227 *

Also Published As

Publication number Publication date
CN119149039A (zh) 2024-12-17

Similar Documents

Publication Publication Date Title
US20210374563A1 (en) Solution Automation
CN114399019B (zh) 神经网络编译方法、系统、计算机设备及存储介质
CN106874244B (zh) 一种基于工作序列的文档自动生成模型的构建方法
CN111324577B (zh) 一种Yml文件读写的方法及装置
CN110866029B (zh) sql语句构建方法、装置、服务器及可读存储介质
US11501177B2 (en) Knowledge engineering and reasoning on a knowledge graph
CN109657803B (zh) 机器学习模型的构建
WO2023010916A1 (zh) 软件自动修复方法、系统、电子设备及存储介质
CN118246033B (zh) 跨平台代码异常漏洞检测方法、系统、设备、介质及产品
CN117992033A (zh) 一种基于aigc的编程方法、装置、设备及存储介质
CN114328525A (zh) 数据处理方法及设备
CN114201397A (zh) 一种接口测试建模方法、装置、电子设备和存储介质
US10338891B2 (en) Migration between model elements of different types in a modeling environment
CN119046304B (zh) 业务引擎实例的生成方法、装置、计算机设备和可读存储介质
US20250005318A1 (en) System and method for automated data-driven domain model synthesis
CN118377605B (zh) 任务调度模型构建方法及装置
CN119669215A (zh) 一种不同数据源类型的sql语句的字段血缘确定方法及装置
CN116523052B (zh) 一种快速推理方法、装置及设备
CN114595464B (zh) 智能合约重入漏洞检测方法、装置、存储介质及相关设备
CN117609005A (zh) 一种代码相似度检测方法
CN119149039A (zh) 修复补丁生成方法、存储介质及电子装置
CN115878747A (zh) 一种基于Flink的实时文本规则处理方法
CN115048133A (zh) 规则引擎的规则更新方法、装置、设备及存储介质
US20260104902A1 (en) Method And Apparatus For Extending Capability Of Function Block
CN115879868B (zh) 一种专家系统与深度学习相融合的智能合约安全审计方法

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 24822432

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE