An API misuse defect fixing method based on prompt learning
By using a prompt-based learning approach, combined with multiple repair modes and API misuse defect program to generate defect prompts, the pre-trained model is guided to generate repair solutions. This solves the problems of low efficiency and limited types of API misuse defect repair in existing technologies, and achieves efficient multi-type defect repair.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- NANJING UNIV
- Filing Date
- 2022-04-27
- Publication Date
- 2026-07-03
AI Technical Summary
Existing automatic defect repair methods are inefficient and limited in type when repairing API misuse defects, and cannot effectively solve multiple types of API misuse defects.
A prompt-based learning approach is adopted, which uses a large-scale pre-trained model combined with multiple repair modes and API misuse defect procedures to generate defect prompts, guiding the pre-trained model to generate repair solutions, avoiding the overhead of fine-tuning the model, and completing defect repair by providing task examples or prompt text.
It enables efficient repair of various types of API misuse defects, improves repair efficiency, and can generate semantically correct repair solutions.
Smart Images

Figure CN114706789B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a method for fixing API misuse defects based on prompting learning, specifically belonging to the field of software defect fixing technology. Background Technology
[0002] API (Application Programming Interface) is also known as an application programming interface, which is the agreement between different components of a software system. Due to the increasing scale of software in recent years, complex systems often need to be divided into smaller components, making the design of programming interfaces particularly important. In programming practice, the first step in designing a programming interface is to ensure a reasonable division of responsibilities within the software system. Good interface design can reduce the interdependencies between system parts, increase the cohesion of component units, and reduce the coupling between component units, thereby improving the maintainability and scalability of the system.
[0003] Due to the widespread use of APIs in current software development, developers can directly reuse implemented functions by calling APIs without needing to know the specific implementation details, thus improving software development efficiency. However, due to the complexity of APIs themselves, lack of documentation, or user oversight, developers sometimes misuse APIs, leading to API misuse defects. These API misuse defects can cause program errors under certain conditions, producing abnormal results or behaviors.
[0004] API misuse refers to actions that violate API usage constraints, such as violating call conditions or call order restrictions, which may ultimately lead to software crashes. There are various types of API misuse, including missing API calls, missing condition checks, or incorrect API call order. For example, when using Java to open a file for writing, failing to close the file after writing will cause the file writing to fail; this is an example of API misuse.
[0005] For fixing API misuse defects, automatic defect repair methods are generally used. This involves automatically generating patches to fix software defects, thereby improving the efficiency of defect repair. Search-based defect repair methods use predefined mutation operators to mutate the code and generate patches, then use project-specific test cases to verify the patches' correctness. Pattern-based defect repair methods use predefined repair patterns and modify the code at locations matching those patterns to generate patches. Semantic-based defect repair methods use constraint solving and program synthesis techniques to directly generate patches that satisfy constraints for specific types of defects.
[0006] However, current automatic defect repair methods still have certain shortcomings in repairing API misuse defects. Search-based defect repair methods are highly general, but their efficiency is low due to the lack of specificity in the design of mutation operators. Defect repair methods based on repair patterns and semantics can only repair relatively single types of defects and cannot be directly applied to repairing API misuse defects. Therefore, there is an urgent need for a method that can be applied to repairing multiple types of API misuse defects and has high repair efficiency. Summary of the Invention
[0007] The summary section of this application is intended to provide a brief overview of the concepts, which will be described in detail in the detailed description section below. This summary section is not intended to identify key or essential features of the claimed technical solutions, nor is it intended to limit the scope of the claimed technical solutions.
[0008] To address the problems and shortcomings of existing technologies, this invention aims to provide an API misuse defect repair method based on cue learning. Cue learning is a method that uses cue information to guide a large-scale pre-trained model to complete a specific task. This method does not require labeled data to fine-tune the pre-trained model for application to downstream tasks; instead, it learns from the pre-trained model by providing task examples or cue text, thus avoiding the significant overhead of the pre-trained model. Therefore, by combining multiple repair modes and API misuse defect procedural generation, cue learning provides repair examples to the pre-trained model to guide it in completing defect repair tasks. This method can repair various types of API misuse defects and effectively improve repair efficiency, thereby solving the problems mentioned in the background art.
[0009] To achieve the above objectives, the present invention provides the following technical solution:
[0010] This invention discloses a method for correcting API misuse defects based on prompting learning, which mainly includes the following steps:
[0011] Step 1: Obtain API misuse defects in the software and extract the method bodies containing the defects as API misuse defect programs;
[0012] Step 2: Based on the characteristics of the API misuse defect and the automatic defect repair method based on repair mode, design multiple repair modes for the API misuse defect;
[0013] Step 3: Combine the multi-repair mode with the API misuse defect program to generate defect prompts, providing defect repair examples for the pre-trained model;
[0014] Step 4: Input the generated defect prompts into the pre-trained model to guide it to generate a repair solution for the API misuse defect based on the defect prompts.
[0015] Furthermore, the multiple repair modes for API misuse defects described in step 2 provide knowledge for repairing specific types of defects and are designed according to the characteristics of the API misuse defects. The multiple repair modes include adding null pointer checks, adding value or state checks, adding exception handling, mutating method call patterns, mutating condition patterns, and mutating variable patterns, each of which involves different operations.
[0016] The specific operations involved for each repair mode are as follows:
[0017] Add null pointer checking mode: Add null pointer checking for method calls;
[0018] Add value or status check patterns: Add value checks to method calls, add status checks to method calls;
[0019] Add exception handling mode: Add exception handling for method calls and add types of exceptions to be caught;
[0020] Method call patterns can be mutated as follows: adding method calls, deleting method calls, replacing method calls, adding parameters to method calls, deleting parameters from method calls, and replacing parameters in method calls.
[0021] Variant conditional patterns: Replace operators in conditional expressions;
[0022] Mutant variable pattern: Replace variables in method calls, replace variables in assignment expressions.
[0023] Furthermore, in the added null pointer check mode and the added value or status check mode, there will be different handling strategies when the variable is null or does not meet certain conditions, such as returning the default value, throwing an exception, and reassigning the value.
[0024] Furthermore, in step 3, the multi-repair mode is combined with the API misuse defect procedure to generate defect prompts for the pre-trained model. The specific steps are as follows:
[0025] Step 3.1: First, convert the designed multi-repair mode into specific code snippets, including the method body containing the defect and the corresponding repaired method body;
[0026] Step 3.2: Add suggestive text before both the defective method body and the corresponding repaired method body;
[0027] Step 3.3: Add suggestive text to the method body to be repaired in the API misuse defect program to indicate that the method body to be repaired contains a defect;
[0028] Step 3.4: Combine the defective method body and the corresponding repaired method body with the method body to be repaired to generate the defect prompt.
[0029] Furthermore, the specific steps for generating and outputting the API misuse defect repair solution in step 4 are as follows:
[0030] Step 4.1: Combine the API misuse defect program with all the multi-repair modes in sequence to generate defect prompts;
[0031] Step 4.2: Input all the generated defect prompts into the pre-trained model in sequence, and output the repair solution;
[0032] Step 4.3: Combine all the obtained remediation solutions into a set, review them, and output the final remediation solution for the API misuse defect.
[0033] Compared with existing technologies, the beneficial effects of this invention are as follows: This invention provides an API misuse defect repair method based on cue learning. Cue learning eliminates the need for labeled data to fine-tune a pre-trained model for downstream tasks; instead, it guides the pre-trained model by providing task examples or cue text, thus avoiding the overhead of fine-tuning the pre-trained model. The method mainly consists of two stages: cue generation and repair generation. In the cue generation stage, defect cue is generated by combining designed multiple repair modes and API misuse defect procedures, providing examples for defect repair to the pre-trained model. In the repair generation stage, the defect cue is used as input to the pre-trained model, guiding it to generate repair solutions for API misuse defects based on the cue. The multiple repair modes for API misuse defects provide knowledge for repairing specific types of defects. Combined with the strong code generation capabilities of a large-scale pre-trained model, specific defect repair tasks can be completed simply by providing task examples, further improving the efficiency of repairing multiple types of API misuse defects simultaneously. Attached Figure Description
[0034] The accompanying drawings, which form part of this application, are used to provide a further understanding of the application and to make other features, objects, and advantages of the application more apparent. The illustrative embodiments and descriptions of this application are used to explain the application and do not constitute an undue limitation of the application.
[0035] In the attached diagram:
[0036] Figure 1 : This is a schematic diagram of the structure of the present invention implemented in the API misuse defect program;
[0037] Figure 2 : This is a schematic diagram of the overall structure of the present invention;
[0038] Figure 3 This is a schematic diagram of the overall process flow of the present invention;
[0039] Figure 4 This is a schematic diagram of the steps involved in generating defect notifications in this invention.
[0040] Figure 5 This is a schematic diagram illustrating the steps involved in generating the API misuse defect repair scheme in this invention. Detailed Implementation
[0041] Embodiments of this disclosure will now be described in more detail with reference to the accompanying drawings. While some embodiments of this disclosure are shown in the drawings, it should be understood that this disclosure can be implemented in various forms and should not be construed as limited to the embodiments set forth herein. Rather, these embodiments are provided to provide a more thorough and complete understanding of this disclosure. It should be understood that the accompanying drawings and embodiments of this disclosure are for illustrative purposes only and are not intended to limit the scope of protection of this disclosure.
[0042] It should also be noted that, for ease of description, only the parts relevant to the invention are shown in the accompanying drawings. Unless otherwise specified, the embodiments and features described in this disclosure can be combined with each other.
[0043] This invention discloses a method for fixing API misuse defects based on prompting learning. The following will describe this disclosure in detail with reference to the accompanying drawings and embodiments.
[0044] In software development, API misuse defects can occur due to factors such as a lack of API documentation and developers' lack of experience using APIs. This reduces software reliability and increases maintenance costs. However, search-based defect remediation methods are inefficient, and pattern- and semantic-based methods only address a limited range of defect types, making them less effective at fixing API misuse defects.
[0045] This invention presents a novel method for fixing API misuse defects based on cue learning. The main idea is to leverage the strong code generation capabilities of a large-scale pre-trained model, combined with a designed multi-fix mode and API misuse defect program to generate appropriate defect cueing. This cueing guides the pre-trained model to generate the correct fix for the API misuse defect. Cue learning is a method that uses cueing information to guide a large-scale pre-trained model to complete a specific task. This method does not require labeled data to fine-tune the pre-trained model for downstream tasks; instead, it learns from the pre-trained model by providing task examples or cueing text, thus avoiding the significant overhead of pre-training.
[0046] The following reference Figures 1 to 3 As shown, this method mainly includes the following steps:
[0047] Step 1: Obtain API misuse defects in the software and extract the method bodies containing the defects as API misuse defect programs;
[0048] Step 2: Based on the characteristics of API misuse defects and the automatic defect repair method based on repair patterns, design multiple repair patterns for API misuse defects.
[0049] Step 3: Combine the multiple repair modes with the API misuse defect program to generate defect prompts, providing defect repair examples for the pre-trained model;
[0050] Step 4: Input the generated defect prompts into the pre-trained model to guide it in generating a repair solution for the API misuse defect based on the defect prompts.
[0051] This invention mainly consists of two stages: prompt generation and repair generation. In the prompt generation stage, defect prompts are generated by combining multiple repair modes and API misuse defect procedures, providing examples of defect repair for the pre-trained model. In the repair generation stage, the defect prompts are used as input to the pre-trained model, guiding it to generate repair solutions for API misuse defects based on the prompts. On one hand, the automatic defect repair method based on repair modes provides knowledge for repairing specific types of defects, and by combining this with the characteristics of API misuse defects, multiple repair modes are designed for API misuse defects. On the other hand, the pre-trained model has strong code generation capabilities. By combining the designed multiple repair modes and defect prompts generated by the defect procedure, it can provide examples of defect repair for the pre-trained model and use the prompts to guide it in completing the defect repair task.
[0052] Specifically, in the prompt generation phase, the characteristics of API misuse defects are first summarized, and multiple repair modes are designed for API misuse defects in conjunction with the automatic defect repair method of the repair mode. This invention mainly designs six types of multiple repair modes, and each repair mode type involves different operations, as shown below:
[0053] Add null pointer checking mode: Add null pointer checking for method calls;
[0054] Add value or status check patterns: Add value checks to method calls, add status checks to method calls;
[0055] Add exception handling mode: Add exception handling for method calls and add types of exceptions to be caught;
[0056] Method call patterns can be mutated as follows: adding method calls, deleting method calls, replacing method calls, adding parameters to method calls, deleting parameters from method calls, and replacing parameters in method calls.
[0057] Variant conditional patterns: Replace operators in conditional expressions;
[0058] Mutant variable pattern: Replace variables in method calls, replace variables in assignment expressions.
[0059] Let F denote the set of all multi-repair modes, f i If a multi-repair mode is to be represented in the set, then it can be expressed as f. i ∈F. Among them, for the null pointer check mode and the value or status check mode, there are different handling strategies when the variable is null or does not meet certain conditions, such as returning the default value, throwing an exception, and reassigning the value.
[0060] Then, by combining multiple repair modes and API misuse defect procedures, appropriate defect hints are generated for the pre-trained model. An API misuse defect procedure refers to a method body containing a defect. After discovering API misuse defects in the software through techniques such as defect localization, the method body containing the defect is extracted as the API misuse defect procedure. The generated defect hint is represented as follows:
[0061] p i =Gen(f i B),
[0062] Where B represents the API misuse defective program, Gen represents the defect message generation method, and p i This indicates the generated defect message. The generated defect message p i It includes examples of fixing specific types of defects and API misuse defects, and provides prompts to guide the pre-trained model to complete the defect fixing task.
[0063] Refer to Figure 4 As shown, the combination of multiple repair modes and API misuse defect procedures generates defect hints for the pre-trained model. The specific steps are as follows:
[0064] Step 3.1: First, convert the designed multi-fix pattern into specific code snippets, including the method body containing the defect and the corresponding fixed method body;
[0065] Step 3.2: Add the suggestive text before the defective method body and the corresponding fixed method body, respectively;
[0066] Step 3.3: Add suggestive text to the method body to be fixed in the API misuse defect program to indicate that the method body to be fixed contains a defect;
[0067] Step 3.4: Combine the defective method body and the corresponding repaired method body with the method body to be repaired to generate a defect message.
[0068] Specifically, the designed multi-repair mode is first converted into specific code snippets, including the defective method body and the corresponding repaired method body. The type, method name, variable name, etc. in the method body are represented by common symbols. Then, suggestive text is added before these two method bodies respectively, indicating that one is the defective method body and the other is the repaired method body. Finally, text is added to the API misuse defect program, i.e., the method body to be repaired, to indicate that the method body is defective, and it is concatenated with the method body converted from the multi-repair mode to form the generated defect message.
[0069] Reference Figure 5 As shown, the specific steps for generating a remediation solution output for API misuse defects are as follows:
[0070] Step 4.1: Combine the API misuse defect program with all the multi-repair modes in sequence to generate defect prompts;
[0071] Step 4.2: Input all generated defect suggestions into the pre-trained model in sequence, and output the repair solution;
[0072] Step 4.3: Combine all the obtained remediation solutions into a set, review them, and output the final remediation solution for the API misuse defect.
[0073] Specifically, during the repair generation phase, the generated defect hints p i This serves as input to a pre-trained model, guiding it to generate remediation solutions for API misuse defects. The generated remediation solutions are expressed as follows:
[0074] s i =Pre(p i ),
[0075] Where Pre represents the pre-trained model used, p i This indicates the generated defect message, s i This indicates the generated remediation plan. When generating the remediation plan, the API misuse defective program B is sequentially combined with all multi-remediation modes f. i Combine to generate defect hints p i Then, all the prompts are sequentially input into the pre-trained model to obtain multiple repair solutions. i Finally, all the repair solutions i After the set is reviewed, the final defect repair method is obtained, which is the repair solution generated for API misuse defect program B.
[0076] Example of API misuse defect repair
[0077] 1. Description of input and output data
[0078] The method of this invention is applied to the real API misuse defect dataset APIREPBENCH, and taking defect LOG4J2-1061 in the dataset as an example, the corresponding method body containing the defect is shown below:
[0079] public boolean remove(final Marker marker){
[0080] return this.marker.remove(MarkerManager.getMarker(marker.getName()));
[0081] }
[0082] This code snippet shows that the `return` statement on line 2 calls a method on the variable `marker`, but no null pointer check is performed on `marker` before the call. Therefore, if `marker` is null, the program will throw an exception. Based on the designed multi-repair mode for API misuse defects, and combined with the defect prompts generated by the API misuse defect program, several repair solutions are obtained by inputting the generated defect prompts into a pre-trained model. Subsequently, all generated repair solutions are manually reviewed, and finally, the correct repair solution for the API misuse defect is output.
[0083] 2. Defect Repair
[0084] After obtaining the method body containing the defect, it is combined with a pre-designed multi-repair mode for API misuse defects to generate a defect message. Here, we take the null pointer check mode from the multi-repair mode as an example; the defect message generated by combining it with the defect LOG4J2-1061 is shown below:
[0085]
[0086] As can be seen from the code above, lines 1 to 13 contain two method bodies obtained by adding null pointer checks: the method body containing the defect and the corresponding repaired method body. Lines 2 to 5 contain the defective method body lacking null pointer checks, while lines 7 to 13 contain the repaired method body with null pointer checks added. The types, method names, and variable names in the method bodies are represented using common symbols, such as Type1 and method1. Each method body is preceded by suggestive text, as shown in lines 1, 6, 14, and 18, which guides the pre-trained model in defect repair. Lines 15 to 17 show the method bodies containing defects in the real defect LOG4J2-1061 described in the input / output data explanation. Other types of multi-repair modes are converted using the same method and combined with the API misuse defect program to generate defect prompts.
[0087] After generating prompts by combining the API misuse defect program and multiple repair modes, all defect prompts are sequentially input into the pre-trained model. Taking the pre-trained model Codex as an example, the method of this invention is used to sequentially input all generated defect prompts into the Codex model. Then, the corresponding generated repair solutions are obtained from the model's output. All generated repair solutions are manually reviewed, and finally, the semantically correct repair solution is selected as the API misuse defect repair solution.
[0088] 3. Output Results
[0089] The repair solution for defect LOG4J2-1061, as ultimately output by the method according to the present invention, is as follows:
[0090]
[0091] As can be seen from this code, a null pointer check for the parameter `marker` was added before the `return` statement on line 5, as shown in lines 2 to 4. When the program executes the `return` statement on line 5, it can return the default value if `marker` is null, avoiding the exception thrown by the program when `marker` is null, and successfully fixing the misuse defect. Therefore, this embodiment shows that the API misuse defect fixing method based on prompt learning of the present invention can be reasonably applied in reality.
[0092] The above description is merely a selection of preferred embodiments of this disclosure and an explanation of the technical principles employed. Those skilled in the art should understand that the scope of the invention involved in the embodiments of this disclosure is not limited to technical solutions formed by specific combinations of the above-described technical features, but should also cover other technical solutions formed by arbitrary combinations of the above-described technical features or their equivalents without departing from the above-described inventive concept. For example, technical solutions formed by substituting the above-described features with (but not limited to) technical features with similar functions disclosed in the embodiments of this disclosure.
Claims
1. A method for repairing API misuse defects based on prompt learning, characterized in that, Includes the following steps: Step 1: Obtain API misuse defects in the software and extract the method bodies containing the defects as API misuse defect programs; Step 2: Based on the characteristics of the API misuse defect and the automatic defect repair method based on repair mode, design multiple repair modes for the API misuse defect; Step 3: Combine the multi-repair mode with the API misuse defect program to generate defect prompts, providing defect repair examples for the pre-trained model; Step 4: Input the generated defect prompts into the pre-trained model to guide it to generate a repair solution for the API misuse defect based on the defect prompts; The multiple repair modes for API misuse defects described in step 2 provide knowledge for repairing specific types of defects and are designed according to the characteristics of the API misuse defects. The multiple repair modes include adding null pointer checks, adding value or state checks, adding exception handling, mutating method call patterns, mutating condition patterns, and mutating variable patterns. Each repair mode involves different operations. In step 3, the multi-repair mode is combined with the API misuse defect program to generate defect prompts for the pre-trained model. The specific steps are as follows: Step 3.1: First, convert the designed multi-repair mode into specific code snippets, including the method body containing the defect and the corresponding repaired method body; Step 3.2: Add suggestive text before both the defective method body and the corresponding repaired method body; Step 3.3: Add suggestive text to the method body to be repaired in the API misuse defect program to indicate that the method body to be repaired contains a defect; Step 3.4: Combine the defective method body and the corresponding repaired method body with the method body to be repaired to generate the defect prompt.
2. The API misuse bug fixing method based on hint learning according to claim 1, characterized in that: In the added null pointer check mode and the added value or status check mode, there are different handling strategies when the variable is null or does not meet certain conditions, such as returning the default value, throwing an exception, and reassigning the value.
3. The method for fixing API misuse defects based on prompting learning according to claim 1, characterized in that: The specific steps for generating and outputting the API misuse defect repair solution in step 4 are as follows: Step 4.1: Combine the API misuse defect program with all the multi-repair modes in sequence to generate defect prompts; Step 4.2: Input all the generated defect prompts into the pre-trained model in sequence, and output the repair solution; Step 4.3: Combine all the obtained remediation solutions into a set, review them, and output the final remediation solution for the API misuse defect.