Patch generation method, patch generation device, and program

By incorporating a default value specifier in SmPL, the challenges of efficiently applying edits in program rewriting tools are addressed, resulting in improved flexibility, reduced errors, and enhanced computational efficiency.

JP7670176B2Active Publication Date: 2025-04-30NIPPON TELEGRAPH & TELEPHONE CORP
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
JP2023578235
Authority / Receiving Office
JP · JP
Patent Type
Patents
Current Assignee / Owner
Filing Date
2022-02-01
Publication Date
2025-04-30
Estimated Expiration
2042-02-01

AI Technical Summary

Technical Problem

Existing program rewriting tools, such as those using SmPL, face challenges in efficiently applying edits due to limitations in specifying edited content and characterizing edited locations, particularly when variable definitions are involved, leading to increased complexity and potential errors.

Method used

The introduction of a default value specifier in SmPL allows for concise code editing that depends on the presence or absence of variable definitions, improving the ease and interpretability of editing rules, and reducing computational overhead by generating patches that adapt to variable definitions.

Benefits of technology

This approach enhances the efficiency of program rewriting by allowing more flexible and intuitive specification of editing content, reducing the likelihood of errors, and minimizing computational complexity, thereby improving overall rewriting efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure 0007670176000008
    Figure 0007670176000008
  • Figure 0007670176000009
    Figure 0007670176000009
  • Figure 0007670176000010
    Figure 0007670176000010
Patent Text Reader

Abstract

According to the present invention, a computer performs: an input procedure for inputting a program to be rewritten and a patch that contains an operator not defined in SmPL and a description in SmPL and that includes, as arguments, a variable name id for which a default value is specified, the type T of id, and the default value t of id; and a generation procedure for, if a variable of the type T is not defined within the program to be rewritten, generating a patch indicating, in SmPL, that a variable declaration statement for substituting the result of calculating t for the variable id of the type T be added to the program to be rewritten, whereas if a variable of the type T is defined within the program to be rewritten, generating a patch in which id in said description is replaced by the variable. This improves the efficiency of rewriting the program.
Need to check novelty before this filing date? Find Prior Art

Description

[Technical field]

[0001] The present invention relates to a patch generation method, a patch generation device, and a program. [Background technology]

[0002] In software development, it is common to make the same edits to multiple parts of a program. Program rewriting tools such as patch make it possible to automatically rewrite multiple parts of a program.

[0003] A program rewriting tool takes as input the program to be rewritten and a script that defines the edits and the characterization of the lines, commands, or expressions in the program to which the edits should be applied. It applies the edits defined in the script to the parts of the program to be rewritten that match the characterization defined by the script. For example, the script for patch is given in diff format. In this case, the edits are defined by specifying deletions and additions on a line-by-line basis, and the parts to which the edits should be applied are characterized by specifying a string. The edits are applied to the lines in the program to be rewritten that match the specified string.

[0004] Patch is widely used not only for programs but also for rewriting general text files, because it allows the definition of the characteristics of the parts to which edits are applied by intuitive descriptions in diff format. However, the specification of edits is limited to line units, and the characterization of the parts to which edits are applied is limited to matching specific strings. These restrictions can be particularly restrictive when used for rewriting programs. For example, the parts to which edits are applied must match the specified string exactly, and if only the variable name differs from the string, the edit will not be applied, even if the command is the same. Although it is possible to write definitions in diff format that correspond to all possible variable name patterns using a method based on complete enumeration, this is not a practical method.

[0005] For these reasons, there is a need for a program rewriting tool that allows for more flexible and powerful editing content specification and characterization of application locations than patch, while also providing intuitive descriptive capabilities.

[0006] [A program rewriting tool based on program matching using control flows] In order to enable flexible and powerful specification of edit contents and characterization of the parts to which the edits are applied in program rewriting, a method has been proposed in which matching between code fragments in the program to be rewritten and the parts to which the edits are applied is reduced to a model checking problem regarding the control flow of the program to be rewritten (Non-Patent Document 1).

[0007] Coccinelle is a well-known program rewriting tool based on this technique. To be precise, there is Coccinelle (Non-Patent Document 4) for rewriting C programs, and Coccinelle4J (Non-Patent Document 3) for rewriting Java (registered trademark) programs, but from here on, we will refer to them collectively as Coccinelle.

[0008] Coccinelle is a popular program rewriting tool that is often used by developers of Linux (registered trademark) kernel drivers and other well-known applications (e.g., Wine (https: / / wiki.winehq.org / Static_Analysis#Coccinelle) and Zephyr (https: / / docs.zephyrproject.org / latest / guides / coccinelle.html)). In the following, Coccinelle will be described as a representative program rewriting tool based on the method of Non-Patent Document 1.

[0009] Coccinelle characterizes the editing content and the applicable locations of the edit using a domain-specific language (DSL) called Semantic Patch (SmPL), which is based on the method of Non-Patent Document 1. The specification of the editing content is done by annotating the addition and deletion of code fragments with + and - at the beginning of each code fragment, similar to diff. At this time, the unit of the code fragment does not necessarily have to be a line unit and can be specified in any syntactic unit. Also, the following characteristic functions can be utilized in SmPL.

[0010] <I. Meta-variables> In Coccinelle, meta-variables representing program expressions and types can be used to specify the editing content and the applicable locations of the edit. By using meta-variables, specific variable names, expressions, and type names can be abstracted.

[0011] Figure 1 is a diagram showing an example of SmPL using the meta-variable v. Among the code fragments in the program to be rewritten, the edit is applied to the code fragments that match the code fragments annotated with -, but any code fragment that shows an instruction to assign a value obtained by new ByteRef() to a variable of type BytesRef, even if the variable name is not v, matches the SmPL in Figure 1.

[0012] <II. Regular-expression-like operators> In Coccinelle, operators in regular expressions such as selection "(a|b)" and arbitrary string "..." can be used to characterize the applicable locations of the edit.

[0013] FIG. 2 shows an example using the selection operator (a|b). If a program fragment that matches sendStickyBroadcast(i) exists in the program to be rewritten, the first half of the edit separated by "|" is applied, and the program fragment that matches sendStickyBroadcast(i) is replaced with sendBraddcast(i). Even if matching fails, if a program fragment that matches removeStickyBroadcast(i) exists, the second half of the edit separated by "|" is applied, and the matched portion is deleted. For details including other operators, please refer to https: / / coccinelle.gitlabpages.inria.fr / website / docs / index.html.

[0014] These features of SmPL enable Coccinelle to flexibly characterize edited parts and refine edit units while still allowing them to be described intuitively. [Prior art documents] [Non-patent literature]

[0015] [Non-Patent Document 1] Brunel, J., Doligez, D., Hansen, RR, Lawall, JL, and Muller, G. A foundation for flow-based program matching: Using temporal logic and model checking. In Proceedings of the 36th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages ​​(New York, NY, USA, 2009), POPL '09, Association for Computing Machinery, p. 114-126. [Non-Patent Document 2] Jones , ND , and Hansen , RR The semantics of "semantic patches" in ladybird: Program transformation for the working programmer. In Programming Languages ​​and Systems (Berlin, Heidelberg, 2007), Z. Shao, Ed., Springer Berlin Heidelberg, pp. 10-11. 303-318.

Outdoor Tools3

Outdoor Tools 4

[0016] [Difficulty in applying patches considering whether variables are defined or not] When rewriting a program, we may want to change the edit content of the program depending on whether the program being rewritten has a definition of a specific variable. As an example, consider the case where the edit defined in the SmPL in Figure 1 is applied to the program being rewritten in Figure 3.

[0017] The program shown in Figure 3 is a fictitious program created for the purpose of explanation. The local variable m is a variable that specifies the operating mode of BytesRef. In Figure 3, the value Mode.Debug, which indicates debug mode, is assigned to it. If we consider this series of codes to be a program that calls a constructor called BytesRef(), we can read the meaning of the program as "the operating mode of ByteRef r is specified as debug mode by the local variable m."

[0018] However, the program shown in FIG. 3 can be rewritten into the program shown in FIG. 4 by applying the rewrite defined in FIG.

[0019] In Figure 4, the declaration of the local variable m remains, but the call to BytesRefBuilder, which is the replacement for BytesRef, is made with the variable arg, which is assigned the operating mode Mode.Default, as an argument. This ignores the meaning of the original code, which was "debug mode is specified."

[0020] In the case of the SmPL example in Figure 1, by using a selection operator, it is possible to simulate an edit that does not ignore the meaning of the code before it is rewritten. For example, as shown in Figure 5, a description can be made that uses a selection operator to distinguish between cases where a variable specifying the operating mode is declared or not.

[0021] The first half of the branch of the selection operator is used to check whether a variable of type Mode is declared in the program to be rewritten, and if so, the newly added program fragment will not contain a declaration of a variable of type Mode. If not, the second half of the branch of the selection operator is used to apply an edit that includes a declaration of a variable of type Mode to the newly added program fragment. This type of case analysis exponentially increases the number of branches of the selection operator when the number of code fragments that need to be added or not depending on whether a variable is defined or not increases. For each such code fragment, there are two possible patterns, presence or absence, resulting in at least a power of two branches.

[0022] SmPLs with many branches increase the possibility of errors being embedded in the specification of edit contents. Another problem is that the expression of edit contents is not intuitive. Furthermore, rewriting a program using SmPLs with many branches like this one has the problem of increasing the amount of calculations because text matching backtracking occurs frequently to determine the branch destination to be applied.

[0023] The present invention has been made in consideration of the above-mentioned points, and has an object to improve efficiency in rewriting a program. [Means for solving the problem]

[0024] In order to solve the above problem, a computer executes an input procedure for inputting a patch including an operator not defined in SmPL and a description in SmPL, with arguments being a variable name id for which a default value is specified, the type of id, and the default value of id t, and a program to be rewritten; and a generation procedure for generating a patch using SmPL to indicate that a variable declaration statement that assigns the calculation result of t to a variable id of type T should be added to the program to be rewritten if a variable of type T is not defined in the program to be rewritten, and generating a patch in which id in the description is replaced by the variable if a variable of type T is defined in the program to be rewritten. Effect of the Invention

[0025] The efficiency of program rewriting can be improved. [Brief description of the drawings]

[0026] [Figure 1] FIG. 13 is a diagram showing an example of SmPL using a meta variable v. [Diagram 2] FIG. 13 is a diagram showing an example using a selection operator (a|b). [Diagram 3] FIG. 2 is a diagram showing an example of a program to be rewritten before SmPL is applied; [Figure 4] FIG. 13 is a diagram showing an example of a program to be rewritten after SmPL is applied; [Diagram 5] FIG. 1 is rewritten using a selection operator. [Figure 6] 4 is a diagram for explaining the flow of a program rewriting process in the present embodiment. FIG. [Figure 7] FIG. 2 is a diagram showing an example of rewriting the example in FIG. 1 using a default value specifier. [Figure 8] 8 is a diagram showing the conversion result of FIG. 7 when the program to be rewritten is the program shown in FIG. [Figure 9] 1 is a diagram illustrating an example of a hardware configuration of a patch generation device 10 according to an embodiment of the present invention. [Figure 10]1 is a diagram illustrating an example of a functional configuration of a patch generating device 10 according to an embodiment of the present invention. [Figure 11] 1 is a flowchart illustrating an example of a processing procedure executed by the patch generating device 10. [Figure 12] 13 is a flowchart illustrating an example of a processing procedure for matching P with SmPL+?. [Figure 13] 13 is a flowchart illustrating an example of a procedure for searching for a variable declaration of the same type as the variable of Δi. DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENTS

[0027] Hereinafter, an embodiment of the present invention will be described with reference to the drawings. Fig. 6 is a diagram for explaining the flow of a program rewriting process in this embodiment.

[0028] In this embodiment, a default value specifier for concisely describing a code edit in which whether or not a code fragment is added depends on whether a specific variable definition exists in the rewritten program, and its evaluator are disclosed. It is assumed that the default value specifier is used as a kind of operator in a code edit definition in SmPL. A code edit definition in SmPL that includes a default value specifier is described in SmPL. +? The evaluator is SmPL. +? Given a given program and a given object, it generates a patch using SmPL that is context-sensitive to the object. The generated SmPL can be applied to the object using Coccinelle to rewrite the code.

[0029] [default value specifier] A default value specifier is a ternary operator whose arguments are the name of the variable id for which a default value is to be specified, its type T, and the default value t. The syntax of a default value specifier is "+ ?T id=t;" and can be informally read as "If a variable of type T is not defined in the program to be rewritten, add a variable declaration statement to the program to be rewritten that assigns the result of the calculation of t to variable id of type T, and if it is defined, use that variable as a replacement for id."

[0030] Default value specifiers are used in code edit definitions using SmPL. For example, code edits that add a variable declaration statement for the Mode type variable arg in Figure 1 only if the variable declaration statement does not exist in the rewritten program can be written succinctly using default value specifiers as shown in Figure 7.

[0031] [Default value specifier evaluator] The evaluator for default value specifiers is SmPL +? Receive the patch and the modified program, and +? The default value specifier in the text is converted to an SmPL statement. At this time, the evaluator does not rewrite the rewritten program at all. The rewritten program is used only to check the condition "if a variable of type T is not defined in the rewritten program" in the editing rule represented by the default value specifier.

[0032] For example, if the program to be rewritten is the one shown in Figure 3, the SmPL in Figure 7 +? The result of the evaluator converting this to SmPL is shown in Figure 8.

[0033] Figure 3 includes the definition of m as a variable of type Mode, so "Mode arg=Mode.Default;" is not added. This shows that arg can be substituted with m, so the argument of BytesRefBuilder is rewritten from arg to m.

[0034] If the program to be rewritten does not include the definition of m as a variable of type Mode, the evaluator will +? Default value specifier in (+ ?Generates an SmPL in which the "?" operator (+ Mode arg=Mode.Default;) is replaced by an operator that does not contain "?" (+ Mode arg=Mode.Default;).

[0035] [Patch generator 10] The patch generation device 10 functioning as the above-mentioned evaluator will be described. Fig. 9 is a diagram showing an example of a hardware configuration of the patch generation device 10 in the embodiment of the present invention. The patch generation device 10 in Fig. 9 includes a drive device 100, an auxiliary storage device 102, a memory device 103, a processor 104, and an interface device 105, which are all connected to each other via a bus B.

[0036] A program for implementing the processing in the patch generating device 10 is provided by a recording medium 101 such as a CD-ROM. When the recording medium 101 storing the program is set in the drive device 100, the program is installed from the recording medium 101 via the drive device 100 into the auxiliary storage device 102. However, the program does not necessarily have to be installed from the recording medium 101, but may be downloaded from another computer via a network. The auxiliary storage device 102 stores the installed program as well as necessary files, data, and the like.

[0037] When an instruction to start a program is received, the memory device 103 reads out and stores the program from the auxiliary storage device 102. The processor 104 is a CPU or a GPU (Graphics Processing Unit), or a CPU and a GPU, and executes functions related to the patch generation device 10 in accordance with the program stored in the memory device 103. The interface device 105 is used as an interface for connecting to a network.

[0038] The evaluator is realized by a process executed by the processor 104 according to a program installed in the patch generating device 10 .

[0039] Fig. 10 is a diagram showing an example of a functional configuration of a patch generation device 10 according to an embodiment of the present invention. In Fig. 10, the patch generation device 10 has an input unit 11 and a generation unit 12. That is, the input unit 11 and the generation unit 12 constitute an evaluator. Each of these units is realized by a process in which one or more programs installed in the patch generation device 10 are executed by the processor 104. The input unit and the generation unit constitute the evaluator.

[0040] The input unit 11 receives a patch (SmPL +? ) and the program to be rewritten.

[0041] If the variable of type T is not defined in the rewritten program, the generation unit 12 generates a patch indicating by an SmPL that a variable declaration statement for assigning the calculation result of t to a variable id of type T is to be added to the rewritten program. Specifically, in this case, the generation unit 12 generates a patch in which a default value specifier is replaced by a description indicating by an SmPL that a variable declaration statement for assigning the calculation result of t to a variable id of type T is to be added to the rewritten program.

[0042] On the other hand, if a variable of type T is defined in the program to be rewritten, the generating unit 12 generates a patch in which the id in the description X is replaced by the variable.

[0043] Before describing the processing procedures executed by the patch generating device 10, the definition of a data structure necessary for understanding the processing procedures will be described.

[0044] [Program to be rewritten]

[0045]

number

[0046] [SmPL]

[0047]

number

[0048] [SmPL +? ] A default value specifier has been added to SmPL Elements.

[0049]

number

[0050] In step S101, the input unit 11 receives a program P to be rewritten and an SmPL +? Enter the SmPL +? means an SmPL that includes a default value specifier. An SmPL is a patch in which the rewrite content of a rewritten program P is described by an SmPL.

[0051] Next, the generation unit 12 generates the input SmPL +? is substituted into SmPL as the initial value of the output (S102).

[0052] Next, the generation unit 12 generates the SmPL +? Default value specifiers Δ1,...,Δ n is obtained (S103).

[0053] Next, the generation unit 12 generates the SmPL +? P is matched with and the result is assigned to r (S104). If the match is successful, the triplet of P's grand term σ, the mapping θ from the metavariables in SmPL to P's grand terms, and the witness Ω is obtained as the value of r. If the match is unsuccessful, Empty is obtained as the value of r.

[0054] Next, the generation unit 12 judges whether the match result is empty (S105). If the match result is empty (i.e., if the match fails) (Yes in S105), the generation unit 12 outputs an error (S106) and ends the process.

[0055] If the result of the match is not empty (No in S105), the generation unit 12 n Each Δ i For this, a loop process including steps S107 to S111 is executed.

[0056] In step S107, the generation unit 12 calculates Δ i Get the grand term in P that the previous Element matched.

[0057] Δi is the SmPL +? Since it is an Element of , the following holds: SmPL +? =E1...Δ4...ε In this case, the "Element immediately before Δi" is SmPL +? Δ iWhen scanning from to E1, the first element other than +T, -T, and Δ is defined as E^. However, if E^ does not exist, the final result of step S107 (Lookup) is Empty.

[0058] Witness Ω has a mapping θ between metavariables in SmPL and the Grand terms of P as an element. Since metavariables in SmPL always include metavariables that correspond one-to-one to the Elements of SmPL, by referring to the Grand terms of P that θ maps to for metavariables that correspond to E^, we can obtain "Δ i You can get the "grand term in P that the element immediately before matched."

[0059] Next, the generation unit 12 calculates Δ i A variable declaration of the same type as the variable is searched for (S108).

[0060] Δ i A variable declaration G' of the same type as the variable id If found, the generation unit 12 calculates Δ i (However, Δ i =+ ? var(G id ,G type , G) in SmPL (S109). id A reference to G' id (S110).

[0061] On the other hand, Δ i A variable declaration G' of the same type as the variable id If not found, the generation unit 12 selects Δ i (However, Δ i =+ ? var(G id ,G type ,G)) to +var(G id ,G type , G) (S111).

[0062] When the loop process ends, the generation unit 12 outputs the SmPL (S112).

[0063] Next, the details of step S104 will be described. +? 12 is a flowchart for explaining an example of a processing procedure for matching P with the SmPL +? and P.

[0064] In step S201, the generation unit 12 generates the SmPL +? Substitute this for SmPL. Note that SmPL here is a local variable that is valid in FIG. 12 and is a different variable from SmPL in FIG.

[0065] Next, the generation unit 12 deletes +T and Δ from the Elements of the SmPL (S202).

[0066] Next, the generation unit 12 match The result of (SmPL,P) is substituted for r (S203). match The process is performed using the Coccinelle program matching algorithm (the CTL-VW (Non-Patent Document 1) model checking algorithm). match If program matching is successful, the process returns a triplet of P's grandterm σ, a mapping θ from metavariables in SmPL to the grand terms of P, and a witness Ω. If program matching is unsuccessful, it returns Empty. Note that Witness Ω is a triplet of P's grandterm σ, a mapping θ from metavariables in SmPL to the grand terms of P, and a witness. Witness can be said to be a value with a recursive structure. Also, metavariables in SmPL always include metavariables that correspond one-to-one to Elements in SmPL.

[0067] Next, the generation unit 12 returns r (S204).

[0068] Next, step S108 in Fig. 11 will be described. i 13 is a flowchart for explaining an example of a processing procedure for searching for a variable declaration of the same type as the variable in FIG. i (However, Δi=+ ? var(G id ,G type ,G), the rewritten program P, Grand term

[0069]

number

[0070]

number

[0071]

number

[0072]

number

[0073] Next, the generation unit 12 d In the order of G0, j In other words, the rewritten program P is scanned in reverse order. In step S303, the generation unit 12 j From the subtree of G typeType variable definition (var(G' id ,G type ,G') to find the corresponding variable definition (var(G' id ,G type , G'), the generator 12 id If the corresponding variable definition is not found, the generation unit 12 continues the loop process.

[0074] When the loop is finished (i.e., all G j About G j From the subtree of G type Type variable definition (var(G' id ,G type , G') cannot be found, the generation unit 12 returns Empty (S305).

[0075] The reason for scanning the rewritten program P in reverse order is that the rewritten program P is type In finding the variable definition of a type, G type This is because in cases where multiple variable definitions of a type can be found, the one closest to the location where the default value needs to be specified is used preferentially.

[0076] As described above, according to this embodiment, the SmPL including the default value specifier +? By this, code edits that depend on the presence or absence of variable definitions in the rewritten program can be described concisely, and the ease of describing and interpreting such code edits can be improved. As a result, it is possible to obtain an SmPL that allows the above code edits to be performed for a given rewritten program with a small amount of calculation, thereby improving the efficiency of program rewriting.

[0077] Although the embodiment of the present invention has been described in detail above, the present invention is not limited to such specific embodiment, and various modifications and variations are possible within the scope of the gist of the present invention described in the claims. [Explanation of symbols]

[0078] 10 Patch Generator 11 Input section 12 Generation part 100 Drive device 101 Recording media 102 Auxiliary storage device 103 Memory device 104 processors 105 Interface device B Bus

Claims

1. an input procedure for inputting a patch including an operator not defined in SmPL and a description in SmPL, the patch having arguments of a variable name id for which a default value is specified, a type T of id, and a default value t of id, and a program to be rewritten; a generation procedure for generating a patch indicating, by SmPL, that a variable declaration statement for assigning a calculation result of t to a variable id of type T is added to the program to be rewritten if a variable of type T is not defined in the program to be rewritten, and generating a patch in which the id in the description is replaced by the variable if a variable of type T is defined in the program to be rewritten; A patch generation method comprising the steps of:

2. The generation procedure generates a patch in which the operator is replaced by a description in SmPL indicating that a variable declaration statement for assigning a calculation result of t to a variable id of type T is added to the program to be rewritten if the variable of type T is not defined in the program to be rewritten.

2. The patch generating method according to claim 1.

3. an input unit configured to input a patch including an operator not defined in SmPL and a description in SmPL, the patch having arguments of a variable name id for which a default value is specified, a type T of id, and a default value t of id, and a program to be rewritten; a generating unit configured to generate a patch indicating, by SmPL, adding a variable declaration statement to the program to be rewritten, which assigns a calculation result of t to a variable id of type T, if the variable of type T is not defined in the program to be rewritten, and to generate a patch in which the id in the description is replaced by the variable, if the variable of type T is defined in the program to be rewritten; A patch generating device comprising:

4. the generation unit is configured to generate a patch in which the operator is replaced by a description in SmPL indicating that a variable declaration statement for assigning a calculation result of t to a variable id of type T is to be added to the program to be rewritten if the variable of type T is not defined in the program to be rewritten.

4. The patch generating device according to claim 3.

5. 3. A program for causing a computer to execute the patch generating method according to claim 1 or 2.

Citation Information

Patent Citations

  • Patch source conversion system

    JP1990114328A