A method and system for automatic program repair based on machine learning
Through the automatic program repair method based on machine learning, the problem of inefficient repair of hollow pointer exceptions in the program is solved, efficient automatic repair is achieved, labor costs are reduced, and the repair accuracy is improved through self-learning optimization model.
Patent Information
- Application Number
- CN202411037958.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-31
- Publication Date
- 2025-04-04
- Estimated Expiration
- 2044-07-31
AI Technical Summary
The prior art is inefficient in handling hollow pointer exceptions in the program, and requires a lot of manpower and time, making it difficult to effectively solve the problem of diversity empty pointer.
The automatic repair method of machine learning is adopted to collect defect code samples, label and classify null pointer problems, extract relevant features, establish repair patches, and train neural network models through feedback training sets to realize real-time analysis and automatic repair of repair program code.
Improve program repair efficiency, reduce labor costs, and improve the accuracy and stability of repair patches through self-learning and optimization of models.
Smart Images

Figure CN118964078B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of automatic program repair, and in particular to a method and system for automatic program repair based on machine learning. Background Art
[0002] With the increasing scale of software, program bugs are an inevitable product of software development. The causes of program bugs can be traced back to various stages of software development. In the case of program errors, null pointer exceptions often occur. Null pointer exceptions are a common programming error that usually occurs where null reference objects are used.
[0003] During program running, due to the frequent occurrence of null pointer exceptions in program programming, the program cannot run normally, which may bring a bad user experience to users. The program is processing important data, which may cause data loss or damage. Since there are many reasons for null pointer exceptions, manual troubleshooting is often used. However, programmers have limited energy. When multiple null pointer phenomena occur in the system, the system is checked one by one, resulting in low program repair efficiency. Moreover, the causes of null pointer exceptions are diverse. When repairing them, it takes a lot of time and manpower costs. Therefore, a program automatic repair method and system based on machine learning are proposed. Summary of the invention
[0004] The purpose of the present invention is to provide a method and system for automatic program repair based on machine learning to solve the problems raised in the above background technology.
[0005] In order to solve the above technical problems, one of the objectives of the present invention is to provide a program automatic repair method based on machine learning, comprising the following steps:
[0006] S1. Collect defective code samples, mark all null pointer problems in the defective code samples and classify them;
[0007] S2. Extract relevant features of the null pointer problem and create corresponding repair patches based on the problem features;
[0008] S3, determining a feedback training set based on relevant features of the known null pointer problem and the corresponding repair patch, training the neural network model through the feedback training set, analyzing the program code to be repaired in real time, and retrieving the repair patch based on the analysis result;
[0009] S4, inspect the repair patches retrieved in S3, and for patches that fail the inspection, formulate new repair patches and mark the patch content, and feed the new repair patches back to the training set in S3;
[0010] S5. Use automated scripts to deploy the model and regularly collect and analyze the model's performance and operation data.
[0011] As a further improvement of the technical solution, in S1, known defective code samples are collected through the open source community.
[0012] As a further improvement of the technical solution, all the null pointer problems of the defective code sample are marked and classified in S1, including the following steps:
[0013] S1.1. Check the defective code samples, locate all null pointer issues and mark them;
[0014] S1.2. Define the null pointer problem classification standard according to the requirements, classify the null pointer problem in S1.1 based on the classification standard, and create an identifier for each category;
[0015] S1.3. Create a classification document that describes the specific characteristics and sample code of the null pointer problem in this category.
[0016] As a further improvement of the technical solution, extracting relevant features in the null pointer problem in S2 includes the following steps:
[0017] S2.1. Analyze the syntax structure and semantic information of the source code through static code analysis tools to extract the characteristics of null pointer problems;
[0018] S2.2. Extract the context information of the null pointer problem characteristics, determine the specific location of the problem, analyze the cause of the null pointer problem, and understand the logical or semantic errors in the problem;
[0019] S2.3. Based on the type and cause of the null pointer problem, formulate a corresponding repair strategy and write the corresponding repair patch code according to the repair strategy.
[0020] As a further improvement of the present technical solution, the pattern matching technology is adopted in S2.3 to automatically generate corresponding repair patch codes by analyzing the null pointer problem patterns existing in the code.
[0021] As a further improvement of the technical solution, the feedback training set is determined in S3 according to relevant features and solutions in the known null pointer problem, including the following steps:
[0022] S3.1. Extract features for the null pointer problem in each sample code snippet;
[0023] S3.2, record the features of the sample code snippets and the corresponding repair patches, and label the feature snippets and corresponding repair patches in each sample;
[0024] S3.3. Combine features and labels to construct samples of the training set, and train the neural network model through the training set samples.
[0025] As a further improvement of the present technical solution, in S4, an integration test is performed to simulate actual usage scenarios and verify the compatibility and correctness of the repair patch at the system level.
[0026] As a further improvement of the technical solution, in S4, for the patch that fails the inspection, a new repair patch is newly formulated and the patch content is marked, including the following steps:
[0027] S4.1, extracting code snippets from the repair patch generated by the neural network model as patches to be tested;
[0028] S4.2. Use static code analysis tools to check the generated fix patch, write unit tests for the generated fix patch, and verify the correctness of the fix patch;
[0029] S4.3. Re-formulate a repair patch based on the cause of the inspection failure and accurately mark the modifications and changes in the repair patch.
[0030] A second object of the present invention is to provide a program automatic repair system based on machine learning, including any one of the above-mentioned program automatic repair methods based on machine learning, including a code collection and classification unit, a feature extraction unit, an automatic repair unit, a test feedback unit, and an operation monitoring unit;
[0031] The code collection and classification unit is used to collect defective code samples, mark all null pointer problems of the defective code samples and classify them;
[0032] The feature extraction unit is used to extract relevant features in the null pointer problem and establish corresponding repair patches according to the problem features;
[0033] The automatic repair unit is used to determine a feedback training set according to relevant features of the known null pointer problem and the corresponding repair patch, train the neural network model through the feedback training set, analyze the program code to be repaired in real time to obtain the type of the null pointer problem, and retrieve the repair patch according to the analysis result;
[0034] The inspection feedback unit is used to inspect the repair patch retrieved from the automatic repair unit, and for patches that fail the inspection, a new repair patch is formulated and the patch content is marked, and the new repair patch is fed back to the training set;
[0035] The operation monitoring unit is used to deploy the model using an automated script, and to regularly collect and analyze the performance and operation data of the model.
[0036] Compared with the prior art, the present invention has the following beneficial effects:
[0037] 1. By collecting defective code samples, marking all null pointer problems of the defective code samples and classifying them, the causes of the null pointer problems are obtained and classified, and corresponding repair patches are established for the null pointer problems. The neural network model is trained by determining the feedback training set, and the type of null pointer problem is obtained through real-time analysis of the program code to be repaired, and the repair patch is called. The null pointer problems generated in the program are automatically repaired, thereby improving the efficiency of program repair.
[0038] 2. Determine the accuracy and stability of the automatically repaired patch by testing it. If the patch fails the test, formulate a new repair patch and mark the patch content. At the same time, feed the new repair patch content into the feedback training set to enable the model to achieve self-learning capabilities, continuously learn and improve from actual repairs, continuously improve and optimize the model, and improve the accuracy of the repair patch. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 It is the overall flow chart of the present invention;
[0040] Figure 2 A flow chart for classifying the null pointer problem of the present invention;
[0041] Figure 3 A flowchart of establishing a corresponding repair patch of the present invention;
[0042] Figure 4 A flowchart of a training neural network model of the present invention;
[0043] Figure 5 A flowchart of the inspection and repair patch of the present invention;
[0044] Figure 6 It is a schematic diagram of the overall module of the present invention.
[0045] The meaning of each number in the figure is:
[0046] 100. Code collection and classification unit; 200. Feature extraction unit; 300. Automatic repair unit; 400. Inspection and feedback unit; 500. Operation monitoring unit. DETAILED DESCRIPTION
[0047] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0048] Example 1
[0049] like Figure 1-6 As shown, one of the purposes of the present invention is to provide a program automatic repair method based on machine learning, comprising the following steps:
[0050] S1. Collect defective code samples, mark all null pointer problems in the defective code samples and classify them;
[0051] In S1, known defective code samples are collected through the open source community;
[0052] Considering the different causes of null pointer problems, in order to understand common null pointer problems and facilitate repairing them, all null pointer problems of the defective code samples are marked and classified in S1, including the following steps:
[0053] S1.1. Check the defective code samples, locate all null pointer issues and mark them;
[0054] S1.2. Define the null pointer problem classification standard according to the requirements, classify the null pointer problem in S1.1 based on the classification standard, and create an identifier for each category;
[0055] S1.3. Create a classification document that describes the specific characteristics and sample code of the null pointer problem in this category.
[0056] In specific use, by collecting a large number of defective code samples in the open source community, and using static code analysis tools such as PMD, FindBugs, Infer, etc., these static code analysis tools can automatically analyze the code and detect potential null pointer problems. According to the defective code samples, the null pointer problems are classified. For example, the null pointer problems can be divided into the following categories:
[0057] Uninitialized pointer: A pointer variable is not initialized after being declared, resulting in no valid assignment operation before using the pointer, which may cause the pointer to contain random or garbage values, thus causing the null pointer problem;
[0058] NULL pointer dereference: In some cases, a pointer is explicitly assigned to NULL and is dereferenced in subsequent code. Since NULL indicates that the pointer does not point to any valid memory address, the dereference operation will cause a null pointer problem.
[0059] Not set to NULL after release: After the memory pointed to by the pointer is released, the pointer is not set to NULL, which may cause the pointer to continue to be used in subsequent code and cause a null pointer problem;
[0060] Lack of pointer validity checking: Lack of proper validity checking when using pointers. No judgment is made whether the pointer is NULL. In such code, performing operations when the pointer is NULL will lead to null pointer problems.
[0061] The original pointer is lost when assigning a pointer: When assigning a pointer to another pointer, the resource release or null operation of the original pointer is not properly handled, resulting in a dangling pointer problem.
[0062] S2. Extract relevant features of the null pointer problem and create corresponding repair patches based on the problem features;
[0063] Since different repair patches need to be used to repair the null pointer problem in the automatic repair process in order to improve the repair efficiency when repairing the null pointer problem in the system, the extracting of relevant features in the null pointer problem in S2 includes the following steps:
[0064] S2.1. Analyze the syntax structure and semantic information of the source code through static code analysis tools to extract the characteristics of null pointer problems;
[0065] S2.2. Extract the context information of the null pointer problem characteristics, determine the specific location of the problem, analyze the cause of the null pointer problem, and understand the logical or semantic errors in the problem;
[0066] S2.3. Based on the type and cause of the null pointer problem, formulate a corresponding repair strategy and write the corresponding repair patch code according to the repair strategy.
[0067] S2.3 adopts pattern matching technology to automatically generate corresponding repair patch code by analyzing the null pointer problem pattern in the code. The pattern matching technology includes the following steps:
[0068] Identify null pointer problem patterns: By analyzing known null pointer problems, identify common null pointer problem patterns, such as uninitialized pointers, pointer dereferences without validity checks, pointer duplication, etc.
[0069] Apply pattern matching rules: Use pattern matching techniques to analyze the code and apply the constructed pattern matching rules to identify specific instances of null pointer issues;
[0070] Generate repair patch code: Identify the existence of null pointer problems and automatically generate corresponding repair patch code based on the problem pattern and corresponding repair strategy. This can be automatic addition of null pointer checks, pointer initialization, safe dereference operations, etc.
[0071] S3, determining a feedback training set based on relevant features of known null pointer problems and corresponding repair patches, training a neural network model through the feedback training set, analyzing the program code to be repaired in real time to obtain the type of null pointer problem, and retrieving the repair patch based on the analysis results;
[0072] Considering that the program is prone to null pointer exceptions due to various reasons, resulting in system crashes, in order to automatically repair the null pointer problem and avoid program crashes, the feedback training set is determined in S3 according to relevant features and solutions in the known null pointer problem, including the following steps:
[0073] S3.1. For each null pointer problem in the sample code snippet, feature extraction is performed. For example, specific keywords, context information, variable types, etc. in the code may be extracted as features.
[0074] S3.2. Record the features of the sample code snippets and the corresponding repair patches, and label the feature snippets and the corresponding repair patches in each sample. For example, the problem type can be labeled as uninitialized pointer, NULL pointer dereference, etc., and the solution can be labeled as adding initialization code, inserting pointer validity check, etc.;
[0075] S3.3. Combine features and labels to construct samples of the training set. Train the neural network model through the training set samples to ensure that the samples in the training set are evenly distributed with various types of null pointer problems and solutions to improve the training effect and generalization ability. Take features as input and labels as output.
[0076] During specific use, the feedback training set is determined by the relevant features and corresponding repair patches in the known null pointer problem, and the neural network model is trained with the feedback training set, so that the model can automatically check the location of the null pointer in the program and automatically replace the repair patch, so that the system can avoid system crashes, data loss and other problems caused by null pointer problems during operation.
[0077] S4: Check the repair patches retrieved in S3. For patches that fail the test, formulate new repair patches and mark the patch contents. The new repair patches are fed back to the training set in S3.
[0078] In S4, the integration test simulates the actual usage scenario to verify the compatibility and correctness of the repair patch at the system level. The integration test specifically includes the following steps:
[0079] Design test cases: Design corresponding test cases based on the objectives of the integration test. The test cases should cover common usage scenarios, boundary conditions, and abnormal conditions to ensure that the repair patch can correctly handle various situations.
[0080] Execute integration testing: execute the designed test cases to simulate actual usage scenarios, including calling different system interfaces and operating various functions, to ensure the compatibility and correctness of the repair patch in the entire system and verify the effectiveness of the repair patch;
[0081] Check test results: Check the output and logs during the test to confirm whether the fix successfully resolved the null pointer issue and ensure that the system runs normally in the integrated test environment.
[0082] Analyze and fix issues: If any issues or errors are found during integration testing, analyze and fix them promptly.
[0083] Considering that there are many cases of null pointers in the system, in order to improve the accuracy of the model, in S4, for the patches that fail the inspection, a new repair patch is formulated and the patch content is marked, including the following steps:
[0084] S4.1, extracting code snippets from the repair patch generated by the neural network model as patches to be tested;
[0085] S4.2. Use static code analysis tools to check the generated fix patch, write unit tests for the generated fix patch, and verify the correctness of the fix patch;
[0086] S4.3. Re-formulate a repair patch based on the cause of the inspection failure and accurately mark the modifications and changes in the repair patch.
[0087] During specific use, the automatically repaired patch is tested to determine the accuracy and stability of the patch. When the patch detection fails the test, a new repair patch is re-formulated and the patch content is marked. At the same time, the new repair patch content is fed back to the feedback training set, so that the model can achieve self-learning ability, continuously learn and improve from actual repairs, continuously improve and optimize the model, and improve the accuracy of the model repair patch.
[0088] S5. Use automated scripts to deploy models, and regularly collect and analyze model performance and operation data.
[0089] Automation of model deployment has the following benefits:
[0090] Improve deployment efficiency: Automated scripts can reduce manual operations and reduce the burden of the deployment process, improving deployment efficiency. It can not only reduce deployment time and workload, but also reduce possible errors in the deployment process;
[0091] Quickly discover problems: By regularly collecting and analyzing the performance and operation data of the model, potential performance problems, errors, and abnormal situations can be discovered in a timely manner, which helps to quickly locate and solve problems and reduce the duration of failures;
[0092] Real-time monitoring and early warning: By regularly collecting and analyzing the performance and operation data of the model, real-time monitoring and early warning of the model can be achieved. When performance degradation, abnormal behavior, or crossing of preset thresholds are found, appropriate measures can be taken in a timely manner to ensure the normal operation of the model.
[0093] The second object of the present invention is to provide a program automatic repair system based on machine learning, including any one of the above-mentioned program automatic repair methods based on machine learning, including a code collection and classification unit 100, a feature extraction unit 200, an automatic repair unit 300, a test feedback unit 400, and an operation monitoring unit 500;
[0094] The code collection and classification unit 100 is used to collect defective code samples, mark all null pointer problems of the defective code samples and classify them;
[0095] The feature extraction unit 200 is used to extract relevant features in the null pointer problem and establish corresponding repair patches according to the problem features;
[0096] The automatic repair unit 300 is used to determine a feedback training set according to relevant features of a known null pointer problem and a corresponding repair patch, train a neural network model through the feedback training set, analyze the program code to be repaired in real time to obtain the type of the null pointer problem, and retrieve the repair patch according to the analysis result;
[0097] The inspection feedback unit 400 is used to inspect the repair patch retrieved from the automatic repair unit 300, and for patches that fail the inspection, a new repair patch is formulated and the patch content is marked, and the new repair patch is fed back to the training set;
[0098] The operation monitoring unit 500 is used to deploy the model using an automated script, and to regularly collect and analyze the performance and operation data of the model.
[0099] The above shows and describes the basic principles, main features and advantages of the present invention. Those skilled in the art should understand that the present invention is not limited by the above embodiments. The above embodiments and descriptions are only preferred examples of the present invention and are not intended to limit the present invention. Without departing from the spirit and scope of the present invention, the present invention may have various changes and improvements, which fall within the scope of the present invention. The scope of protection of the present invention is defined by the attached claims and their equivalents.
Claims
1. A program automatic repair system based on machine learning, characterized in that: It includes a code collection and classification unit (100), a feature extraction unit (200), an automatic repair unit (300), a test feedback unit (400), and an operation monitoring unit (500); The code collection and classification unit (100) is used to collect defective code samples, mark all null pointer problems of the defective code samples and classify them, wherein the null pointer problems include: Uninitialized pointer: A pointer variable is not initialized after being declared, resulting in no valid assignment operation before using the pointer, causing the pointer to contain random or garbage values, thus causing the null pointer problem; NULL pointer dereference: In some cases, a pointer is explicitly assigned to NULL and is dereferenced in subsequent code. Since NULL indicates that the pointer does not point to any valid memory address, the dereference operation leads to a null pointer problem. Not set to NULL after release: After the memory pointed to by the pointer is released, the pointer is not set to NULL, which causes the pointer to continue to be used in subsequent code and causes a null pointer problem; Lack of pointer validity checking: Lack of proper validity checking when using pointers. No judgment is made whether the pointer is NULL. In such code, performing operations when the pointer is NULL will lead to null pointer problems. The original pointer is lost when assigning a pointer: when assigning a pointer to another pointer, the resource release or null operation of the original pointer is not properly handled, resulting in a dangling pointer problem; The feature extraction unit (200) is used to extract relevant features in the null pointer problem and establish corresponding repair patches according to the problem features; The automatic repair unit (300) is used to determine a feedback training set according to relevant features of a known null pointer problem and a corresponding repair patch, train a neural network model through the feedback training set, analyze the program code to be repaired in real time to obtain the type of the null pointer problem, and retrieve the repair patch according to the analysis result; The inspection feedback unit (400) is used to inspect the repair patch retrieved from the automatic repair unit (300), and for patches that fail the inspection, a new repair patch is formulated and the patch content is marked, and the new repair patch is fed back to the training set; The operation monitoring unit (500) is used to deploy the model using an automated script, and to regularly collect and analyze the performance and operation data of the model; The system collects known defective code samples through the open source community; The system marks all null pointer issues in the defective code sample and classifies them, including the following steps: S1.
1. Check the defective code samples, locate all null pointer issues and mark them; S1.
2. Define the null pointer problem classification standard according to the requirements, classify the null pointer problem in S1.1 based on the classification standard, and create an identifier for each category; S1.
3. Create a classification document that describes the specific characteristics and sample code of the null pointer problem in this category; The system extracts relevant features of the null pointer problem, including the following steps: S2.
1. Analyze the syntax structure and semantic information of the source code through static code analysis tools to extract the characteristics of null pointer problems; S2.
2. Extract the context information of the null pointer problem characteristics, determine the specific location of the problem, analyze the cause of the null pointer problem, and understand the logical or semantic errors in the problem; S2.
3. Based on the type and cause of the null pointer problem, formulate a corresponding repair strategy and write the corresponding repair patch code according to the repair strategy; The system uses pattern matching technology to automatically generate corresponding repair patch codes by analyzing the null pointer problem patterns in the code; The system determines the feedback training set based on the relevant features and solutions in the known null pointer problem, including the following steps: S3.
1. Extract features for the null pointer problem in each sample code snippet; S3.2, record the features of the sample code snippets and the corresponding repair patches, and label the feature snippets and corresponding repair patches in each sample; S3.3, combining features and labels to construct samples of a training set, and training the neural network model through the training set samples; The system passes integration testing to simulate actual usage scenarios and verify the compatibility and correctness of the repair patches at the system level; For patches that fail the inspection, the system will re-formulate a new repair patch and mark the patch content, including the following steps: S4.1, extracting code snippets from the repair patch generated by the neural network model as patches to be tested; S4.
2. Use static code analysis tools to check the generated fix patch, write unit tests for the generated fix patch, and verify the correctness of the fix patch; S4.
3. Based on the reasons for the inspection failure, re-formulate the repair patch, accurately mark the modifications and changes in the repair patch, and then install the repair patch.
Citation Information
Patent Citations
Code defect repair scheme recommendation method and device, equipment and storage medium
CN114647432A