A method for generating program patches based on expressions
By performing program repair at the expression level, using type filtering and context-aware generation of expression patches, the problem of repairing too coarse granularity and too large search space in the prior art is solved, and fine-grained repair and efficient repair are achieved.
Patent Information
- Application Number
- CN202211390998.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-11-08
- Publication Date
- 2024-12-20
- Estimated Expiration
- 2042-11-08
AI Technical Summary
The existing search-based automatic repair technology is repaired at the statement level. The granularity is too coarse, resulting in the inability to obtain the correct repair components, and the patch search space is too large, which affects the repair effect.
Using expression-based program patch generation method, the program is decomposed first and second levels, and the repair components are extracted and allocated using type filtering and context-awareness, expression patch statements are generated, and third-party data source guidance is introduced. Repair operation to form the final patch.
It realizes fine-grained repair, improves the probability of correct repair of components, improves repair efficiency, avoids the problem of errors that cannot be repaired due to omission of defect locations, and improves the repair through priority rating and precise allocation of repair materials. accuracy.
Smart Images

Figure CN115951892B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of defect repair, and in particular to an expression-based program patch generation method. Background Art
[0002] Although the existing search-based automatic repair technology has achieved certain results, the repair effect is still limited. The main reason is that the existing repair methods are all studied on the basis of statements, and the code statements in the program are directly reused for repair. The repair granularity of this type of method is too coarse, and the correct repair component may not be obtained. Therefore, coarse-grained repair often fails to complete the repair because the correct repair component is not in the search space. In order to increase the probability of containing the correct patch in the search space, researchers have made a series of improvements. For example, by designing more mutation operators to increase the patch search space; introducing third-party data sources, historical repair templates or relying on manually defined repair templates to increase the number of patches. However, a large patch search space will also affect the overall repair effect, resulting in a large number of irrelevant repair components appearing in the search space, increasing the time and resource consumption of patch verification. However, none of the above methods fundamentally solves the problem of coarse-grained repair. Studies have shown that the correct repair component often exists at a finer granularity than statements. This prompted researchers to begin exploring how to use finer-grained repair components (for example: expressions) for repair. Therefore, the present invention aims to provide a program patch generation method that can repair defective programs at the expression level. Summary of the invention
[0003] The purpose of the present invention is to overcome the shortcomings of the prior art and provide a program patch generation method based on expression.
[0004] The objective of the present invention is achieved through the following technical solutions: an expression-based program patch generation method, the method is used to repair defective programs at the expression level to complete fine-grained repair, the program patch generation method can be executed by one or more processors, including: S1, after the one or more processors perform a first-level decomposition of the program to obtain expressions and statements, they continue to perform a second-level decomposition on statements of a specific type to retain some statements as repair components; S2, the one or more processors extract the repair components based on type filtering and context awareness to assign expression repair components to defective locations; S3, after the one or more processors modify the expression nodes on the syntax tree, they generate expression patch statements and introduce a third-party data source to guide the repair operation; S4, the one or more processors combine the defective location, the operator and the expression patch statement to form a final patch.
[0005] Preferably, the one or more processors are configured with a storage unit that stores a function library and can convert the format of the source code, and the one or more processors can select expression nodes related to class instantiation and statement decomposition, and / or program statement nodes as data sources for repair components.
[0006] Preferably, after constructing the abstract syntax tree of the source program, the one or more processors can access the nodes of the syntax tree and extract their expressions, wherein the expressions include but are not limited to variable types, variable names, method names, method types, method parameters, class instantiations, method calls, conditional expressions and strings.
[0007] Preferably, the one or more processors are capable of performing a first-level decomposition of the program based on statement types to obtain the expression, wherein when the one or more processors extract the repair components, they are capable of making the expression repair components have a high similarity with the defect location in terms of context based on the type filtering and the context awareness.
[0008] Preferably, the one or more processors can divide the expression into global expressions and local expressions according to the scope, determine the use context of the repair component expression node Nt by checking the ancestor node and the sibling node of the repair component expression node Nt, thereby obtaining the context information of Nt, and when the repair component expression node Nt with the context information is applied to the defective expression node Ns, the formula Calculate the similarity between two node contexts To further determine the feasibility, K represents all different types of expression nodes, and f represents the probability of occurrence of the corresponding expression node.
[0009] Preferably, the one or more processors can analyze the variable name and type information of each expression in the statement St where the repair component expression node Nt is located, and analyze the variable name and type information of each expression in the defective statement Ss, for a given repair component expression node Nt, to determine the similarity of the statements. The similarity of the statements can be determined by the formula It is represented by, where K is all different types of expression nodes, N is the expression variable name, T is the expression type, and Same is the similarity of the expression variable name or type.
[0010] Preferably, the one or more processors form the final patch according to the following steps: perform syntax tree analysis on the defect location and construct a defect location space; assign repair components to each defect location based on type filtering and context awareness to construct a repair material space; and synthesize the patch directly on the syntax tree.
[0011] Preferably, the one or more processors can allocate expression repair components based on the formula The similarity between the statement where the expression is located and the error statement is confirmed. When the similarity is greater than a set threshold, the repair patch in the defective program is directly used as a component of the statement as a patch statement.
[0012] Preferably, the defect location space constructed by the one or more processors is used to determine the node type of the erroneous statement, and identify the node position to assign the operator, wherein the operations corresponding to the operator include no modification, deletion and replacement.
[0013] Preferably, when the one or more processors allocate the operators, they can select the modification operator based on the expression type of the repair location and the type of the repair component expression, and randomly generate operators for expressions not covered in the operation list.
[0014] The present invention has the following advantages: This application improves the existing defect location technology, uses Ochiai location technology to score the defect location, and repairs it in batches according to priority. This method changes the previous partial repair to a full repair, avoiding the error that cannot be repaired due to the omission of the defect location; at the same time, the higher the priority, the higher the priority, which improves the repair efficiency. This application directly repairs the defect location in the program syntax tree, and disassembles the defective program into the form of an expression. It can not only complete the modification of the expression node to achieve fine-grained repair, but also replace the entire syntax tree to complete the coarse-grained repair at the statement level. In order to increase the probability of correct repair, this application proposes an expression-based patch generation method TinyRepair, and designs and implements a prototype tool. This method improves the existing location technology, designs 16 data structures and two filtering rules, uses the abstract syntax tree (AST) to decompose the program and synthesize patches, and introduces a third-party data source to guide the repair operation. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] Figure 1 A schematic diagram of a method for constructing a defect position space in the present invention;
[0016] Figure 2 A schematic diagram of the way to construct a space for repair materials;
[0017] Figure 3 A schematic diagram of a method for dynamically synthesizing patches;
[0018] Figure 4 This is a flowchart of the patch repair process. DETAILED DESCRIPTION
[0019] The present invention is further described below in conjunction with the accompanying drawings, and the protection scope of the present invention is not limited to the following:
[0020] The present invention provides a method for generating program patches based on expressions, which is implemented by the following technical solutions:
[0021] The program is decomposed into expression and statement forms by using an abstract syntax tree. The statements of a specific type are further decomposed, and a part of the statements are retained as repair components; then, expression repair components are assigned to the defect position by using type filtering and context-aware strategies; statements are filtered by using context-aware strategies, and statement repair components are retained; then, expression nodes are modified on the syntax tree to generate expression patch statements, and a third-party data source is introduced to guide the repair operation. The third-party data source can be the probability distribution of repair operations derived from actual repair patches of a large number of open source projects. For example, the prior art Xuan Bach D Le, David Lo, and Claire Le Goues. 2016. History Driven Program Repair. In SANER'2016, Vol. 1. IEEE, 213–224 can be referred to. Therefore, this application will not repeat it. The specific process of guiding the repair operation is: when allocating operators, firstly, by calculating the expression type of the repair position and the expression type of the repair component, the modification operator is selected according to the probability distribution of the repair operation of the open source project; secondly, for expressions not covered in the repair operation list, a modification operator will be randomly generated. Finally, the defect location, operator and patch statement are combined to form the final patch. Specifically:
[0022] Abstract syntax tree analysis: In programming codes, symbol granularity is mainly divided into character level, word level, and AST node, etc. Only AST nodes can decompose the program into the repair granularity (expression) required by this application, and retain the semantic and structural information of the program to the greatest extent. This application uses the Java function library of Eclipse JDT to convert the source code into AST form. At the same time, two categories of syntax tree nodes are selected as the data source of repair materials: the first is expression nodes related to class instantiation and statement decomposition, such as array calls (ArrayAccess), variable calls (FieldAccess), etc.; the second is program statement nodes, such as for statements, if statements, etc. Table 1 shows the AST node types selected by this application.
[0023] Table 1 AST node types
[0024]
[0025] Extract expression components: After constructing the source program abstract syntax tree, iterate and visit the nodes of the syntax tree through the ASTVisitor class. In order to improve the accuracy and efficiency of collecting and repairing components, this application ignores the information of the upper tree nodes of the package and only extracts expression components such as variable type, variable name, method name, method type, method parameter, class instantiation, method call, conditional expression and string under the same category. The information extracted from AST is as follows:
[0026] (I) Expression form
[0027] Form 1: Array Access
[0028] 1) Abstract syntax tree structure grammar:
[0029] ArrayAccess:
[0030] Expression[Expression]
[0031] 2) The data structure is as follows: (mainly extracting array type, variable name and index information)
[0032] Form 2: Array Creation
[0033] 1) Abstract syntax tree structure grammar:
[0034]
[0035] 2) The data structure is as follows: (array type, variable name, index, dimension, initial value information)
[0036] Form 3: Array Initializer
[0037] 1) Abstract syntax tree structure grammar:
[0038] ArrayInitializer:
[0039] {[Expression{,Expression}[,]]}
[0040] 2) The data structure is as follows: (array type, variable name, index, dimension, initial value information)
[0041] Form 4: Assignment
[0042] 1) Abstract syntax tree structure grammar:
[0043] Assignment:
[0044] ExpressionAssignmentOperatorExpression
[0045] 2) The data structure is as follows: (variable type, variable name, initial value information)
[0046] Form 5: Literal
[0047] 1) Abstract syntax tree structure grammar:
[0048]
[0049] 2) The data structure is as follows: (Only get the text value information for Boolean literals (BooleanLiteral), type literals (TypeLiteral), characters (CharacterLiteral) and strings (StringLiteral))
[0050] Form 6: ClassInstanceCreation
[0051] 1) Abstract syntax tree structure grammar:
[0052]
[0053] 2) The data structure is as follows: (object type, variable name information)
[0054] Expression——Type——Name
[0055] Form 7: Object Reference (FieldAccess)
[0056] 1) Abstract syntax tree structure grammar:
[0057] FieldAccess:
[0058] Expression.Identifier
[0059] 2) The data structure is as follows: (object type, variable name information)
[0060] Form 8: Prefix, Infix, Postfix
[0061] 1) Abstract syntax tree structure grammar:
[0062]
[0063] 2) The data structure is as follows: (expression return type, operator)
[0064] Form 9: Method Invocation
[0065] 1) Abstract syntax tree structure grammar:
[0066]
[0067] 2) The data structure is as follows: (function return type, method name, parameters)
[0068] Form 10: Variable name (Name)
[0069] 1) Abstract syntax tree structure grammar:
[0070] Name:
[0071] SimpleName
[0072] QualifiedName
[0073] 2) The data structure is as follows: (type, variable name)
[0074] (II) Sentence Form
[0075] When statements are used directly as repair patches, it not only violates the expression-based repair method of this application, but more importantly, the addition of a large number of statements will further increase the search space and reduce the repair efficiency. To this end, this application has made two adjustments to the source program statements: first, the assertion statement (AssertStatement), do-while statement (DoStat-ement), while statement (WhileStatement), if statement (IfStatement), foreach statement (Enhanced-ForStatement), for statement (ForStatement), return statement (ReturnStatement) and variable declaration statement (VariableDeclarationStatement) are decomposed into expressions according to the defined rules; second, for the repair patch directly in the form of program statements, this application uses context-aware strategies to retain the associated statement components. Combining statement and expression components helps the search algorithm find more repair components.
[0076] Form 1: AssertStatement
[0077] 1) Abstract syntax tree structure grammar:
[0078] AssertStatement:
[0079] assertExpression[:Expression];
[0080] 2) The data structure is as follows: (variable type, variable name, operator)
[0081] Form 2: do-while statement (DoStatement), while statement (WhileStatement), if statement (IfStatement)
[0082] 1) Abstract syntax tree structure grammar:
[0083]
[0084] 2) The data structure is as follows: (variable type, variable name, operator)
[0085] Form 3: foreach statement (EnhancedForStatement), for statement (ForStatement)
[0086] 1) Abstract syntax tree structure grammar:
[0087]
[0088] 2) The data structure is as follows: (variable type, variable name, operator)
[0089] Form 4: Return Statement
[0090] 1) Abstract syntax tree structure grammar:
[0091] ReturnStatement:
[0092] return [Expression];
[0093] 2) The data structure is as follows: (variable type, variable name, operator)
[0094] Form 5: Variable Declaration Statement
[0095] 1) Abstract syntax tree structure grammar:
[0096] VariableDeclarationStatement:
[0097] {ExtendedModifier}Type VariableDeclarationFragment
[0098] {,VariableDeclarationFragment};
[0099] 2) The data structure is as follows: (variable type, variable name, allocation)
[0100] Form 6: Other
[0101] 1) Abstract syntax tree structure grammar:
[0102]
[0103] 2) The data structure is as follows: (expression)
[0104] Statement
[0105] Type filtering: To avoid irrelevant repair components from producing specious patches and reduce the probability of compilation failure, this section defines six filtering rules for common error statement types, as shown in Table 2.
[0106] Table 2 Six filtering rules
[0107] Table Six filtering rules
[0108]
[0109] Context-aware strategy: Existing studies have shown that more than 90% of repair components appear in the same source file as the defective program. Therefore, this application limits the source of the repair material to the same file. At the same time, most current repair methods obtain patch statements by scope (such as Arja, GenProg), which contain many irrelevant repair components. For this reason, when extracting repair components, on the basis of type filtering, filtering rules based on contextual information are designed, so that the filtered repair components and the error location have a high degree of similarity in context.
[0110] This application will use AST to obtain context information from two aspects: 1. Expression node context genealogy: First, the expressions are divided into two categories according to the scope: global expressions (GE) and local expressions (LE). By checking the ancestor nodes and sibling nodes of the repair component expression node (Nt), it is determined in what context Nt is used, so as to obtain the context information of Nt. For global expressions, all its ancestor nodes and sibling nodes will be traversed; for local expressions, they will only be traversed within methods or statement blocks. When the repair expression node Nt with context information is applied to the defective expression node Ns, the feasibility is determined by calculating the similarity of the contexts of the two nodes. The calculation formula is shown as follows: Where K represents all different types of expression nodes, f is the frequency of occurrence of the corresponding expression node, and f srepresents the probability of occurrence of defect expression node Ns, f T represents the probability of the repair component expression node Nt appearing, f s (t) represents the probability of the t expression node appearing in the usage context of the defect expression node Ns, f T (t) represents the probability of the t expression node appearing in the context of the repair component expression node Nt. 2. Context sentence similarity: For a given expression node Nt, analyze the variable name and type information of each expression in the sentence St where the node Nt is located, and analyze the variable name and type information of each expression in the defect sentence Ss to determine the sentence similarity. The calculation formula is as follows: Where K is all different types of expression nodes, N is the expression variable name, T is the expression type, Same is the similarity of the expression variable name or type, N s (t) refers to the variable name of the t expression in the defective statement Ss, N T (t) refers to the variable name of the t expression in the statement St where the repair component expression node Nt is located. s (t) refers to the t expression type in the defective statement Ss, T T (t) refers to the type of the t expression in the statement St where the repair component expression node Nt is located.
[0111] The patch statement synthesis is completed as follows: For expression statements, the repair materials of the patch statements at this stage are mainly expressions. The basic process is: first, use the abstract syntax tree (AST) to perform syntax tree analysis on the defect location, and use the AST node to build the defect location space; then use type filtering and context-aware strategies to assign repair components to each defect location and build the repair material space; finally, synthesize the patch directly on the abstract syntax tree. The specific steps include the following:
[0112] (1) Figure 1 As shown in the figure, construct the defect location space: This stage mainly determines what type of nodes the error statement consists of, identifies the node location and assigns operators. The operators include: 0 - do not modify; 1 - delete: DEL(e i , e j ), that is, the expression e j From its parent node p(e i )'s child node list; 2-Replace: REP(e i , e j ), that is, using the expression e j Update expression e i .
[0113] If the modification operations in the position space are generated randomly, this will reduce the accuracy of the repair to a certain extent. The research conducted by LeGoues, Ming and others on more than 3,000 actual repair patches in more than 700 open source projects shows that the repair operation has certain rules. For example, when the error position is a variable name (SimpleName) and a string (StringLiteral), the probability of successful repair by replacing the variable name is significantly higher than the probability of successful repair by replacing the string; in addition, under the method call (MethodInvocation), it is more recommended to delete the variable name (SimpleName) rather than the Boolean expression (BooleanLiteral). In order to reduce the randomness of operator allocation, this application summarizes the existing repair probability distribution and determines several repair operation frequencies based on the defect and repair component type. The higher the frequency, the higher the probability of successful repair. When allocating operators, first select the modification operator by calculating the expression type of the repair position and the type of the repair component expression; secondly, for expressions not covered in the operation list, operators will be randomly generated.
[0114] (2) Constructing a repair material space. In practical applications, there may be multiple legal matches for a syntax tree node at an error location. This application uses type filtering and context-aware strategies to assign repair components to the error location according to the expression context genealogy value and construct a repair material space. The larger the genealogy value, the higher the priority. The repair material space is as follows: Figure 2 shown.
[0115] (3) Dynamic patch synthesis. In this stage, the position array points to the syntax tree node position, selects the repair material and modifies it directly on the syntax tree to generate the patch statement. The specific process is as follows: Figure 3 shown.
[0116] This application retains the repair patch in the defective program as a direct statement component. When allocating expression repair materials, the context similarity calculation formula in the context-aware strategy is directly used to calculate the similarity between the statement where the expression is located and the error statement. If the similarity is greater than a certain threshold (for example, the threshold is set to 0.5), it is used as a patch statement. A candidate patch refers to a combination of different operations and code segments, which modifies the error code in the program and generates a new program entity. The operation here is called a patch operator. In different repair methods, the predefined operators are different. For example: Jkail only uses the delete operation to achieve the purpose of repair by deleting certain statements in the program; Arja defines some rules to limit the use of insert and delete operators. In order to expand the operation space and increase the probability of successful repair, this application selects three patch operations, namely: delete (Delete), insert (Insert) and replace (Replace).
[0117] Finally, it should be noted that the above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art can still modify the technical solutions described in the aforementioned embodiments or replace some of the technical features therein by equivalents. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for generating program patches based on expressions, characterized in that: The method is used to repair a defective program at an expression level to complete fine-grained repair. The program patch generation method can be executed by one or more processors, and includes: S1, after performing a first-level decomposition on the program to obtain expressions and statements, the one or more processors continue to perform a second-level decomposition on statements of a specific type to retain some statements as repair components; S2, the one or more processors extract the repair components based on type filtering and context awareness to assign expression repair components to the defect location; S3, after the one or more processors modify the expression nodes on the syntax tree, they generate expression patch statements and introduce a third-party data source to guide the repair operation; S4, the one or more processors combine the defect location, the operator and the expression patch statement to form a final patch; The one or more processors are configured with a storage unit storing a function library and capable of converting the format of the source code, and the one or more processors are capable of selecting expression nodes related to class instantiation and statement decomposition, and / or program statement nodes as data sources of repair components; The defect location space constructed by the one or more processors is used to determine the node type of the error statement and identify the node location to assign the operator, wherein the operation corresponding to the operator includes no modification, deletion and replacement; When the one or more processors allocate the operators, the modification operators can be selected based on the expression type of the repair position and the type of the repair component expression, and for expressions not covered in the operation list, operators will be randomly generated; The one or more processors form the final patch according to the following steps: Perform syntax tree analysis on the defect location and construct the defect location space; Based on type filtering and context awareness, repair components are assigned to each defect location to build a repair material space; Synthesize patches directly on the syntax tree.
2. The program patch generation method according to claim 1, characterized in that: The one or more processors can access the nodes of the syntax tree and extract expressions thereof after constructing the abstract syntax tree of the source program, wherein the expressions include but are not limited to variable types, variable names, method names, method types, method parameters, class instantiations, method calls, conditional expressions, and strings.
3. The program patch generation method according to claim 1, characterized in that: The one or more processors are capable of performing a first-level decomposition of the program based on statement types to obtain the expression, wherein when the one or more processors extract the repair components, they are capable of making the expression repair components have a high similarity with the defect location in terms of context based on the type filtering and the context awareness.
4. The program patch generation method according to claim 1, characterized in that: The one or more processors can divide the expression into global expressions and local expressions according to the scope, determine the use context of the repair component expression node Nt by checking the ancestor node and the sibling node of the repair component expression node Nt, thereby obtaining the context information of Nt, and when the repair component expression node Nt with the context information is applied to the defective expression node Ns, the formula Calculate the similarity φ(f S , f T ) to further determine the feasibility, where K represents all different types of expression nodes, and f represents the probability of the corresponding expression node occurring.
5. The program patch generation method according to claim 1, characterized in that: The one or more processors can analyze the variable names and type information of each expression in the statement St where the repair component expression node Nt is located, and analyze the variable names and type information of each expression in the defective statement Ss, for a given repair component expression node Nt, to determine the similarity of the statements. The similarity of the statements can be determined by the formula It is represented by, where K is all different types of expression nodes, N is the expression variable name, T is the expression type, and Same is the similarity of the expression variable name or type.
6. The program patch generation method according to claim 1, characterized in that: The one or more processors can allocate expression repair components based on the formula The similarity between the statement where the expression is located and the error statement is confirmed. When the similarity is greater than a set threshold, the repair patch in the defective program is directly used as a component of the statement as a patch statement.
