Code review and optimization system based on machine learning and its use method
Through a code review and optimization system based on machine learning, abstract syntax trees and convolutional neural networks are used to conduct code review, combined with random forest to detect virus codes, and optimize the codes through replacement rules, solving the problem of missed detection and optimization of virus codes in the existing technology. Inconvenient problems are solved, and efficient and accurate code review and optimization are achieved.
Patent Information
- Application Number
- CN202410629705.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-21
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2044-05-21
AI Technical Summary
Existing code review technology lacks the link to detect security vulnerabilities in the code, resulting in virus codes being missed, affecting the security of the code base, making it inconvenient for automatic optimization, and consuming technicians' time.
Adopt a code review and optimization system based on machine learning, including source code decomposition unit, code review module, virus code detection unit and code optimization unit, extract semantic and image features through abstract syntax trees, and use convolutional neural networks and random forests to conduct code analysis Audit and virus code detection, and introduce temporary variable arrays to establish replacement rules for code optimization.
It improves the efficiency and accuracy of code review, prevents missed detection of virus codes, enhances the security of the code base, and reduces code running time through optimization, improving code quality.
Smart Images

Figure CN118364475B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of code review, and in particular to a code review and optimization system based on machine learning and a method for using the same. Background Art
[0002] Code review is a systematic review of computer source code. Its purpose is to find and correct errors that were not discovered in the early stages of software development. Code review is the main measure to improve the internal quality of software. After the code is submitted, it must be reviewed before it can be merged into the remote version library.
[0003] In the prior art, however, there is a lack of a link for detecting security vulnerabilities in the code during code review, which results in virus codes being missed and incorporated into the database. The virus codes are not discovered, and the code is attacked by the virus codes during subsequent code execution, thereby causing losses. At the same time, it is not convenient to automatically optimize the code during the review process, and technical personnel are required to continue running the code for optimization after the review is completed, which is time-consuming. Summary of the invention
[0004] The purpose of the present invention is to provide a code review and optimization system based on machine learning and its use method, so as to solve the problem proposed in the above background technology that there is a lack of a link for security checking of the code, resulting in the situation that virus code is missed and affects the security of the code base.
[0005] To achieve the above-mentioned object, the present invention provides the following technical solutions: a code review and optimization system based on machine learning, comprising a source code decomposition unit, a code review module, a virus code detection unit and a code optimization unit;
[0006] The source code decomposition unit is used to check for duplicates and obtain valid source code, construct an abstract syntax structure of the source code using an optimized abstract syntax tree, extract semantic feature information in the source code, and simultaneously extract image feature information in the source code;
[0007] The code review module is used to obtain the semantic feature matrix of the source code and use a convolutional neural network to predict the code review result;
[0008] The virus code detection unit is used to extract virus code features and identify virus codes in the code;
[0009] The virus code detection unit includes a feature fusion module, a code training module and a security identification module;
[0010] The feature fusion module is used to obtain multiple features for fusion;
[0011] The code training module is used to standardize the data set and use random forest to identify the code;
[0012] The security identification module is used to determine the risk level of the virus code;
[0013] The code optimization unit is used to optimize the intermediate code for specific instruction actions that can match the rules using the established mapping formula.
[0014] Preferably, the source code decomposition unit includes a code duplication checking module, an abstract syntax tree optimization module, a semantic feature extraction module and an image feature extraction module;
[0015] The code duplication checking module is used to check the source code for duplication and remove the source code that has high duplication with the code in the library;
[0016] The abstract syntax tree optimization module is used to screen the node information characteristics of the abstract syntax tree, remove redundant node information, and obtain an optimized abstract syntax tree;
[0017] The semantic feature extraction module is used to extract semantic features using an optimized abstract syntax tree;
[0018] The image feature extraction module is used to extract LBP features, GLCM features and n-gram features in the data.
[0019] Preferably, the code duplication checking module includes a feature enhancement module and a similarity comparison module;
[0020] The feature enhancement module is used to extract features using a graph convolutional neural network and perform feature enhancement using a graph attention mechanism;
[0021] The similarity comparison module is used to obtain similarity vectors between different codes, calculate the similarity between codes, and eliminate source codes with high similarity.
[0022] Preferably, the code review module includes a source code segmentation module, an annotation information acquisition module and a review result prediction module;
[0023] The source code segmentation module is used to segment the entire source code into a number of code segments, obtain the semantic feature vector of each code segment, and construct a semantic feature matrix;
[0024] The annotation information acquisition module is used to acquire data including the code before and after modification and the annotation information of the code submitted by the developer;
[0025] The audit result prediction module is used to predict the audit result and obtain qualified codes.
[0026] Preferably, the code optimization unit includes a mapping construction module, a replacement rule establishment module and an optimization result testing module;
[0027] The mapping construction module is used to introduce a temporary variable array and map the memory virtual operation to the local operation;
[0028] The replacement rule establishment module is used to establish a replacement rule to replace the original memory virtual operation with a local operation;
[0029] The optimization result testing module is used to execute the replaced code and test its optimization effect.
[0030] The method for using the machine learning-based code review and optimization system includes the following steps:
[0031] S1. Code duplication check: Get the source code, use graph convolutional neural network to extract code features, obtain the similarity vectors between different codes, calculate the similarity between the codes, sort the calculation results, remove the codes with high similarity, and obtain the valid source code;
[0032] S2, code decomposition: use the optimized abstract syntax tree to extract the semantic feature information in the valid source code, and extract the image feature information of the valid source code;
[0033] S3. Audit result prediction: Divide the source code into several code segments, obtain the semantic feature vector of each code segment, build a semantic feature matrix, and obtain the code annotation information data at the same time. Use convolutional neural network to perform convolution operation and predict the code audit result.
[0034] S4. Identify virus codes: Obtain image features of the code, including LBP features, GLCM features, and n-gram features, fuse the features to different degrees, standardize the data set, use random forest to determine whether the code is a virus code, and determine the risk level of the virus code, which is divided into primary, intermediate, and advanced risk levels;
[0035] S5. Code optimization: Send the excellent code after review and judgment into the code library, introduce a temporary variable array, establish replacement rules, replace the virtual operation of memory with the main operation, obtain the optimized code, run the code to test its optimization effect, and store the optimized code in the library.
[0036] Preferably, in step S2, the optimized abstract syntax tree includes the following contents: distinguishing the nodes of the abstract syntax tree, selectively removing necessary nodes according to information characteristics of the nodes, and reconstructing the optimized abstract syntax tree.
[0037] Preferably, in step S3, the convolution operation using a convolutional neural network includes the following:
[0038] Let the semantic vector x corresponding to the Ith code segment after source code segmentation be i ∈Rd , a source code divided into n sections is represented as:
[0039]
[0040] A single convolution operation corresponds to a convolution kernel w∈R bd , the convolution kernel processes the window that holds h words to generate a new feature c i :
[0041] c i =f(w·x i;i+h-1 +a);
[0042] The feature map formula of the semantic feature vector is as follows:
[0043] c={c1,c2,...c n-h+1};
[0044] Take the maximum value ξ=max{c} as the feature after convolution kernel convolution. After convolution and operation of m convolution kernels, the feature map T={ξ1,...,ξ m},
[0045] The value of a neuron is calculated as follows:
[0046] y (i) =w (i) T+a (i) ;
[0047] The input Softmax function is normalized to obtain the discrete probability distribution of the source code on all predicted labels as follows:
[0048] [y (1) ,y (2) ,...,y (j) ];
[0049] Among them, a∈R is the deviation value, f(·) represents the nonlinear function, and c∈R n-h+1 .
[0050] Preferably, in step S4, the virus code identification includes the following steps:
[0051] S41, analyzing the original data, converting the file with the suffix .byte into a grayscale image, extracting the LBP features and GLCM features of the grayscale image, and extracting the n-gram features from the file with the suffix .asm;
[0052] S42, performing different combinations of LBP features, GLCM features and n-gram features to obtain feature fusion of the code, and performing standardization on the data set;
[0053] S43, dividing the processed data set into a training set and a test set, using random forest to classify the data set, determining whether the code is a virus code, searching in the virus code library whether there is a similar virus code, testing the accuracy of the virus code determination, classifying the determined virus code, and storing it in the virus code library.
[0054] Preferably, in step S5, the replacement rule includes the following contents:
[0055] Replacement rule C1: If there are adjacent specific instruction actions op_mov_im(A, Im) and op_st(reg(α), A) where A∈{T0, T1, A0}, then op_mov_im(reg[α], Im)) can be used to replace op_mon_im(A, Im) and op_st(reg(α), A);
[0056] Replacement rule C2: If there are adjacent specific instruction actions op(A), op_ld(B, reg(α)), op(B, A) and op_st(reg(α), B), where A, B∈{T0,T1,A0}, then op(reg[α],A) can be used to replace op_ld(B, reg(α)), op(B,A) and op_st(reg(α),B);
[0057] Replacement rule C3: If there are adjacent specific instruction actions op_ld(A, reg(α)), op(A) and op_st(reg(α), A), where A∈{T0,T1,A0}, then op(reg[α]) can be used to replace op_ld(A, reg(α)), op(A) and op_st(reg(α), A);
[0058] Replacement rule C4: If there are adjacent specific instruction actions op(A), op_ld(A, reg(α)) and op_s(reg(β), A), where A∈{T0,T1,A0}, then mov_reg_reg(reg[β],reg[α]) can be used to replace op(A), op_ld(A, reg(α)) and op_st(reg(β), A).
[0059] Compared with the prior art, the present invention has the following beneficial effects:
[0060] The present invention checks the obtained source code for duplicates, calculates the similarity of the codes, removes the codes with high similarity that already exist in the library, obtains valid source code, and improves the efficiency of code review. The source code is divided into several code segments for discretizing code information, thereby reducing the subsequent calculation burden on the code. A convolutional neural network performs convolution operations, predicts the code review results, and improves the accuracy of the review prediction. The LBP feature, GLCM feature and n-gram feature are fused, and random forest is used to determine whether the code is a virus code, so as to prevent the security of the code library from being affected by the omission of virus code. A temporary variable array is introduced, and a replacement rule is established. The memory virtual operation is replaced by the main body operation to obtain the optimized code, so as to achieve the purpose of optimizing the intermediate code, improve the code optimization reduction rate, reduce the running time of the code, and thus improve the quality of the code. BRIEF DESCRIPTION OF THE DRAWINGS
[0061] Figure 1 This is a system block diagram of the code review and optimization system based on machine learning of the present invention;
[0062] Figure 2 This is a block diagram of a code duplication checking module of a code review and optimization system based on machine learning of the present invention;
[0063] Figure 3 A flowchart of a method for using the machine learning-based code review and optimization system of the present invention;
[0064] Figure 4 This is a bar chart of the judgment accuracy of the code review and optimization system based on machine learning of the present invention.
[0065] In the figure: 1. Source code decomposition unit; 11. Code duplication check module; 111. Feature enhancement module; 112. Similarity comparison module; 12. Abstract syntax tree optimization module; 13. Semantic feature extraction module; 14. Image feature extraction module; 2. Code review module; 21. Source code segmentation module; 22. Annotation information acquisition module; 23. Review result prediction module; 3. Virus code detection unit; 31. Feature fusion module; 32. Code training module; 33. Security judgment module; 4. Code optimization unit; 41. Mapping construction module; 42. Replacement rule establishment module; 43. Optimization result testing module. DETAILED DESCRIPTION
[0066] 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 implementation regulations described 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.
[0067] Embodiment 1
[0068] Reference Figure 1-4 As shown: a code review and optimization system based on machine learning, including a source code decomposition unit 1, a code review module 2, a virus code detection unit 3 and a code optimization unit 4;
[0069] The source code decomposition unit 1 is used to check for duplicates and obtain valid source code, construct an abstract syntax structure of the source code using an optimized abstract syntax tree, extract semantic feature information in the source code, and simultaneously extract image feature information in the source code;
[0070] The source code decomposition unit 1 includes a code duplication checking module 11, an abstract syntax tree optimization module 12, a semantic feature extraction module 13 and an image feature extraction module 14;
[0071] The code duplication checking module 11 is used to check the source code for duplication and remove the source code that has a high duplication with the code in the library;
[0072] The code duplication checking module 11 includes a feature enhancement module 111 and a similarity comparison module 112;
[0073] The feature enhancement module 111 is used to extract features using a graph convolutional neural network and perform feature enhancement using a graph attention mechanism;
[0074] The similarity comparison module 112 is used to obtain similarity vectors between different codes, calculate the similarity between codes, and eliminate source codes with high similarity;
[0075] The abstract syntax tree optimization module 12 is used to screen the node information characteristics of the abstract syntax tree, remove redundant node information, and obtain an optimized abstract syntax tree;
[0076] The semantic feature extraction module 13 is used to extract semantic features using an optimized abstract syntax tree;
[0077] The image feature extraction module 14 is used to extract LBP features, GLCM features and n-gram features in the data.
[0078] The code review module 2 is used to obtain the semantic feature matrix of the source code and use the convolutional neural network to predict the code review results;
[0079] The code review module 2 includes a source code segmentation module 21, an annotation information acquisition module 22 and a review result prediction module 23;
[0080] The source code segmentation module 21 is used to segment the entire source code into several code segments, obtain the semantic feature vector of each code segment, and construct a semantic feature matrix. Code segmentation can discretize code information and reduce the subsequent calculation burden on the code;
[0081] The annotation information acquisition module 22 is used to obtain data including the code before and after modification and the annotation information of the code submitted by the developer, so as to provide more information support for the code prediction and improve the accuracy of the prediction;
[0082] The audit result prediction module 23 is used to predict the audit result and obtain qualified codes.
[0083] The virus code detection unit 3 is used to extract virus code features and identify virus codes in the code;
[0084] The virus code detection unit 3 includes a feature fusion module 31, a code training module 32 and a security determination module 33;
[0085] The feature fusion module 31 is used to obtain multiple features for fusion;
[0086] The code training module 32 is used to standardize the data set and use random forest to identify the code;
[0087] The security determination module 33 is used to determine the risk level of the virus code;
[0088] The code optimization unit 4 is used to optimize the intermediate code for specific instruction actions that can match the rules using the established mapping formula.
[0089] The code optimization unit 4 includes a mapping construction module 41, a replacement rule establishment module 42 and an optimization result testing module 43;
[0090] The mapping construction module 41 is used to introduce a temporary variable array and map the memory virtual operation to the local operation;
[0091] The replacement rule establishment module 42 is used to establish a replacement rule to replace the original memory virtual operation with a local operation;
[0092] The optimization result testing module 43 is used to execute the replaced code and test its optimization effect.
[0093] The method for using the machine learning-based code review and optimization system includes the following steps:
[0094] Step 1: Check for duplicate code: Get the source code, use graph convolutional neural network to extract code features, obtain similarity vectors between different codes, calculate the similarity between codes, sort the calculation results, remove codes with high similarity, and obtain valid source code;
[0095] Step 2: Code decomposition: Use the optimized abstract syntax tree to extract the semantic feature information in the valid source code, and at the same time extract the image feature information of the valid source code;
[0096] The optimized abstract syntax tree includes the following contents: distinguishing the nodes of the abstract syntax tree, selectively removing necessary nodes according to the information characteristics of the nodes, and reconstructing the optimized abstract syntax tree.
[0097] Step 3: Predict the audit results: Divide the source code into several code segments, obtain the semantic feature vector of each code segment, build a semantic feature matrix, and obtain the code annotation information data at the same time. Use convolutional neural network to perform convolution operations to predict the code audit results.
[0098] The convolution operation using convolutional neural network includes the following:
[0099] Let the semantic vector x corresponding to the Ith code segment after source code segmentation be i ∈R d , a source code divided into n sections is represented as:
[0100]
[0101] A single convolution operation corresponds to a convolution kernel w∈R bd , the convolution kernel processes the window that holds h words to generate a new feature c i :
[0102] c i =f(w·x i;i+h-1 +a);
[0103] The feature map formula of the semantic feature vector is as follows:
[0104] c={c1,c2,...c n-h+1};
[0105] Take the maximum value ξ=max{c} as the feature after convolution of the convolution kernel. After the convolution and operation of m convolution kernels, the feature map T={ξ1,...,ξ m},
[0106] The value of a neuron is calculated as follows:
[0107] y (i) =w (i) T+a (i) ;
[0108] The input Softmax function is normalized to obtain the discrete probability distribution of the source code on all predicted labels as follows:
[0109] [y (1) ,y (2) ,...,y (j) ];
[0110] Among them, a∈R is the deviation value, f(·) represents the nonlinear function, and c∈R n-h+1 .
[0111] Step 4: Identify virus codes: Obtain the image features of the code, including LBP features, GLCM features, and n-gram features, fuse the features to different degrees, standardize the data set, use random forest to determine whether the code is a virus code, and determine the risk level of the virus code, which is divided into primary, intermediate, and advanced levels;
[0112] Identifying virus codes includes the following steps:
[0113] S41, analyzing the original data, converting the file with the suffix .byte into a grayscale image, extracting the LBP features and GLCM features of the grayscale image, and extracting the n-gram features from the file with the suffix .asm;
[0114] S42, performing multi-feature fusion on LBP features, GLCM features and n-gram features according to the file name, obtaining a data set of codes, and performing standardization on the data set;
[0115] S43, dividing the processed data set into a training set and a test set, using random forest to classify the data set, determining whether the code is a virus code, searching in the virus code library whether there is a similar virus code, testing the accuracy of the virus code determination, classifying the determined virus code, and storing it in the virus code library.
[0116] Step 5. Code optimization: Send the reviewed and identified excellent code to the code library, introduce a temporary variable array, establish replacement rules, replace memory virtual operations with entity operations, obtain optimized code, run the code to test its optimization effect, and store the optimized code in the library.
[0117] The replacement rules include the following:
[0118] Replacement rule C1: If there are adjacent specific instruction actions op_mov_im(A, Im) and op_st(reg(α), A) where A∈{T0, T1, A0}, then op_mov_im(reg[α], Im)) can be used to replace op_mon_im(A, Im) and op_st(reg(α), A);
[0119] Replacement rule C2: If there are adjacent specific instruction actions op(A), op_ld(B, reg(α)), op(B, A) and op_st(reg(α), B), where A, B∈{T0,T1,A0}, then op(reg[α],A) can be used to replace op_ld(B, reg(α)), op(B,A) and op_st(reg(α),B);
[0120] Replacement rule C3: If there are adjacent specific instruction actions op_ld(A, reg(α)), op(A) and op_st(reg(α), A), where A∈{T0,T1,A0}, then op(reg[α]) can be used to replace op_ld(A, reg(α)), op(A) and op_st(reg(α), A);
[0121] Replacement rule C4: If there are adjacent specific instruction actions op(A), op_ld(A, reg(α)) and op_s(reg(β), A), where A∈{T0,T1,A0}, then mov_reg_reg(reg[β],reg[α]) can be used to replace op(A), op_ld(A, reg(α)) and op_st(reg(β), A).
[0122] By extracting the semantic features and image features of the code, the audit result prediction and virus code judgment are performed on the code respectively. After obtaining the LBP features, GLCM features and n-gram features of the code, the LBP features, GLCM features and n-gram features are fused according to the file name to obtain the fused code dataset. The random forest is used to classify the dataset to determine whether the code is virus code and to test the accuracy of virus code judgment.
[0123] Embodiment 2
[0124] Reference Figure 1-4 As shown: a code review and optimization system based on machine learning, including a source code decomposition unit 1, a code review module 2, a virus code detection unit 3 and a code optimization unit 4;
[0125] The source code decomposition unit 1 is used to check for duplicates and obtain valid source code, construct an abstract syntax structure of the source code using an optimized abstract syntax tree, extract semantic feature information in the source code, and simultaneously extract image feature information in the source code;
[0126] The source code decomposition unit 1 includes a code duplication checking module 11, an abstract syntax tree optimization module 12, a semantic feature extraction module 13 and an image feature extraction module 14;
[0127] The code duplication checking module 11 is used to check the source code for duplication and remove the source code that has a high duplication with the code in the library;
[0128] The code duplication checking module 11 includes a feature enhancement module 111 and a similarity comparison module 112;
[0129] The feature enhancement module 111 is used to extract features using a graph convolutional neural network and perform feature enhancement using a graph attention mechanism;
[0130] The similarity comparison module 112 is used to obtain similarity vectors between different codes, calculate the similarity between codes, and eliminate source codes with high similarity;
[0131] The abstract syntax tree optimization module 12 is used to screen the node information characteristics of the abstract syntax tree, remove redundant node information, and obtain an optimized abstract syntax tree;
[0132] The semantic feature extraction module 13 is used to extract semantic features using an optimized abstract syntax tree;
[0133] The image feature extraction module 14 is used to extract LBP features, GLCM features and n-gram features in the data.
[0134] The code review module 2 is used to obtain the semantic feature matrix of the source code and use the convolutional neural network to predict the code review results;
[0135] The code review module 2 includes a source code segmentation module 21, an annotation information acquisition module 22 and a review result prediction module 23;
[0136] The source code segmentation module 21 is used to segment the entire source code into several code segments, obtain the semantic feature vector of each code segment, and construct a semantic feature matrix. Code segmentation can discretize code information and reduce the subsequent calculation burden on the code;
[0137] The annotation information acquisition module 22 is used to obtain data including the code before and after modification and the annotation information of the code submitted by the developer, so as to provide more information support for the code prediction and improve the accuracy of the prediction;
[0138] The audit result prediction module 23 is used to predict the audit result and obtain qualified codes.
[0139] The virus code detection unit 3 is used to extract virus code features and identify virus codes in the code;
[0140] The virus code detection unit 3 includes a feature fusion module 31, a code training module 32 and a security determination module 33;
[0141] The feature fusion module 31 is used to obtain multiple features for fusion;
[0142] The code training module 32 is used to standardize the data set and use random forest to identify the code;
[0143] The security determination module 33 is used to determine the risk level of the virus code;
[0144] The code optimization unit 4 is used to optimize the intermediate code for specific instruction actions that can match the rules using the established mapping formula.
[0145] The code optimization unit 4 includes a mapping construction module 41, a replacement rule establishment module 42 and an optimization result testing module 43;
[0146] The mapping construction module 41 is used to introduce a temporary variable array and map the memory virtual operation to the local operation;
[0147] The replacement rule establishment module 42 is used to establish a replacement rule to replace the original memory virtual operation with a local operation;
[0148] The optimization result testing module 43 is used to execute the replaced code and test its optimization effect.
[0149] The method for using the machine learning-based code review and optimization system includes the following steps:
[0150] Step 1: Check for duplicate code: Get the source code, use graph convolutional neural network to extract code features, obtain similarity vectors between different codes, calculate the similarity between codes, sort the calculation results, remove codes with high similarity, and obtain valid source code;
[0151] Step 2: Code decomposition: Use the optimized abstract syntax tree to extract the semantic feature information in the valid source code, and at the same time extract the image feature information of the valid source code;
[0152] The optimized abstract syntax tree includes the following contents: distinguishing the nodes of the abstract syntax tree, selectively removing necessary nodes according to the information characteristics of the nodes, and reconstructing the optimized abstract syntax tree.
[0153] Step 3: Predict the audit results: Divide the source code into several code segments, obtain the semantic feature vector of each code segment, build a semantic feature matrix, and obtain the code annotation information data at the same time. Use convolutional neural network to perform convolution operations to predict the code audit results.
[0154] The convolution operation using convolutional neural network includes the following:
[0155] Let the semantic vector x corresponding to the Ith code segment after source code segmentation be i ∈R d, a source code divided into n sections is represented as:
[0156]
[0157] A single convolution operation corresponds to a convolution kernel w∈R bd , the convolution kernel processes the window that holds h words to generate a new feature c i :
[0158] c i =f(w·x i;i+h-1 +a);
[0159] The feature map formula of the semantic feature vector is as follows:
[0160] c={c1,c2,...c n-h+1};
[0161] Take the maximum value ξ=max{c} as the feature after convolution of the convolution kernel. After the convolution and operation of m convolution kernels, the feature map T={ξ1,...,ξ m},
[0162] The value of a neuron is calculated as follows:
[0163] y (i) =w (i) T+a (i) ;
[0164] The input Softmax function is normalized to obtain the discrete probability distribution of the source code on all predicted labels as follows:
[0165] [y (1) ,y (2) ,...,y (j) ];
[0166] Among them, a∈R is the deviation value, f(·) represents the nonlinear function, and c∈R n-h+1 .
[0167] Step 4: Identify virus codes: Obtain the image features of the code, including LBP features, GLCM features, and n-gram features, fuse the features to different degrees, standardize the data set, use random forest to determine whether the code is a virus code, and determine the risk level of the virus code, which is divided into primary, intermediate, and advanced levels;
[0168] Identifying virus codes includes the following steps:
[0169] S41, analyzing the original data, converting the file with the suffix .byte into a grayscale image, extracting the LBP features and GLCM features of the grayscale image, and extracting the n-gram features from the file with the suffix .asm;
[0170] S42, feature fusion is performed on the homologous LBP features and GLCM features, the n-gram feature is a single feature, a data set of codes is obtained, and the data set is standardized;
[0171] S43, dividing the processed data set into a training set and a test set, using random forest to classify the data set, determining whether the code is a virus code, searching in the virus code library whether there is a similar virus code, testing the accuracy of the virus code determination, classifying the determined virus code, and storing it in the virus code library.
[0172] Step 5. Code optimization: Send the reviewed and identified excellent code to the code library, introduce a temporary variable array, establish replacement rules, replace memory virtual operations with entity operations, obtain optimized code, run the code to test its optimization effect, and store the optimized code in the library.
[0173] The replacement rules include the following:
[0174] Replacement rule C1: If there are adjacent specific instruction actions op_mov_im(A, Im) and op_st(reg(α), A) where A∈{T0, T1, A0}, then op_mov_im(reg[α], Im)) can be used to replace op_mon_im(A, Im) and op_st(reg(α), A);
[0175] Replacement rule C2: If there are adjacent specific instruction actions op(A), op_ld(B, reg(α)), op(B, A) and op_st(reg(α), B), where A, B∈{T0,T1,A0}, then op(reg[α],A) can be used to replace op_ld(B, reg(α)), op(B,A) and op_st(reg(α),B);
[0176] Replacement rule C3: If there are adjacent specific instruction actions op_ld(A, reg(α)), op(A) and op_st(reg(α), A), where A∈{T0,T1,A0}, then op(reg[α]) can be used to replace op_ld(A, reg(α)), op(A) and op_st(reg(α), A);
[0177] Replacement rule C4: If there are adjacent specific instruction actions op(A), op_ld(A, reg(α)) and op_s(reg(β), A), where A∈{T0,T1,A0}, then mov_reg_reg(reg[β],reg[α]) can be used to replace op(A), op_ld(A, reg(α)) and op_st(reg(β), A).
[0178] By extracting the semantic features and image features of the code, the audit result prediction and virus code judgment are performed on the code respectively. After obtaining the LBP features, GLCM features and n-gram features of the code, the LBP features and GLCM features of the same file name are fused together with the n-gram features to construct a code dataset. The random forest is used to classify the dataset to determine whether the code is virus code and test the accuracy of virus code judgment.
[0179] Embodiment 3
[0180] Reference Figure 1-4 As shown: a code review and optimization system based on machine learning, including a source code decomposition unit 1, a code review module 2, a virus code detection unit 3 and a code optimization unit 4;
[0181] The source code decomposition unit 1 is used to check for duplicates and obtain valid source code, construct an abstract syntax structure of the source code using an optimized abstract syntax tree, extract semantic feature information in the source code, and simultaneously extract image feature information in the source code;
[0182] The source code decomposition unit 1 includes a code duplication checking module 11, an abstract syntax tree optimization module 12, a semantic feature extraction module 13 and an image feature extraction module 14;
[0183] The code duplication checking module 11 is used to check the source code for duplication and remove the source code that has a high duplication with the code in the library;
[0184] The code duplication checking module 11 includes a feature enhancement module 111 and a similarity comparison module 112;
[0185] The feature enhancement module 111 is used to extract features using a graph convolutional neural network and perform feature enhancement using a graph attention mechanism;
[0186] The similarity comparison module 112 is used to obtain similarity vectors between different codes, calculate the similarity between codes, and eliminate source codes with high similarity;
[0187] The abstract syntax tree optimization module 12 is used to screen the node information characteristics of the abstract syntax tree, remove redundant node information, and obtain an optimized abstract syntax tree;
[0188] The semantic feature extraction module 13 is used to extract semantic features using an optimized abstract syntax tree;
[0189] The image feature extraction module 14 is used to extract LBP features, GLCM features and n-gram features in the data.
[0190] The code review module 2 is used to obtain the semantic feature matrix of the source code and use the convolutional neural network to predict the code review results;
[0191] The code review module 2 includes a source code segmentation module 21, an annotation information acquisition module 22 and a review result prediction module 23;
[0192] The source code segmentation module 21 is used to segment the entire source code into several code segments, obtain the semantic feature vector of each code segment, and construct a semantic feature matrix. Code segmentation can discretize code information and reduce the subsequent calculation burden on the code;
[0193] The annotation information acquisition module 22 is used to obtain data including the code before and after modification and the annotation information of the code submitted by the developer, so as to provide more information support for the code prediction and improve the accuracy of the prediction;
[0194] The audit result prediction module 23 is used to predict the audit result and obtain qualified codes.
[0195] The virus code detection unit 3 is used to extract virus code features and identify virus codes in the code;
[0196] The virus code detection unit 3 includes a feature fusion module 31, a code training module 32 and a security determination module 33;
[0197] The feature fusion module 31 is used to obtain multiple features for fusion;
[0198] The code training module 32 is used to standardize the data set and use random forest to identify the code;
[0199] The security determination module 33 is used to determine the risk level of the virus code;
[0200] The code optimization unit 4 is used to optimize the intermediate code for specific instruction actions that can match the rules using the established mapping formula.
[0201] The code optimization unit 4 includes a mapping construction module 41, a replacement rule establishment module 42 and an optimization result testing module 43;
[0202] The mapping construction module 41 is used to introduce a temporary variable array and map the memory virtual operation to the local operation;
[0203] The replacement rule establishment module 42 is used to establish a replacement rule to replace the original memory virtual operation with a local operation;
[0204] The optimization result testing module 43 is used to execute the replaced code and test its optimization effect.
[0205] The method for using the machine learning-based code review and optimization system includes the following steps:
[0206] Step 1: Check for duplicate code: Get the source code, use graph convolutional neural network to extract code features, obtain similarity vectors between different codes, calculate the similarity between codes, sort the calculation results, remove codes with high similarity, and obtain valid source code;
[0207] Step 2: Code decomposition: Use the optimized abstract syntax tree to extract the semantic feature information in the valid source code, and at the same time extract the image feature information of the valid source code;
[0208] The optimized abstract syntax tree includes the following contents: distinguishing the nodes of the abstract syntax tree, selectively removing necessary nodes according to the information characteristics of the nodes, and reconstructing the optimized abstract syntax tree.
[0209] Step 3: Predict the audit results: Divide the source code into several code segments, obtain the semantic feature vector of each code segment, build a semantic feature matrix, and obtain the code annotation information data at the same time. Use convolutional neural network to perform convolution operations to predict the code audit results.
[0210] The convolution operation using convolutional neural network includes the following:
[0211] Let the semantic vector x corresponding to the Ith code segment after source code segmentation be i ∈R d , a source code divided into n sections is represented as:
[0212]
[0213] A single convolution operation corresponds to a convolution kernel w∈R bd , the convolution kernel processes the window that holds h words to generate a new feature c i :
[0214] c i =f(w·x i;i+h-1 +a);
[0215] The feature map formula of the semantic feature vector is as follows:
[0216] c={c1,c2,...c n-h+1};
[0217] Take the maximum value ξ=max{c} as the feature after convolution of the convolution kernel. After the convolution and operation of m convolution kernels, the feature map T={ξ1,...,ξ m},
[0218] The value of a neuron is calculated as follows:
[0219] y (i) =w (i) T+a (i) ;
[0220] The input Softmax function is normalized to obtain the discrete probability distribution of the source code on all predicted labels as follows:
[0221] [y (1) ,y (2) ,...,y (j) ];
[0222] Among them, a∈R is the deviation value, f(·) represents the nonlinear function, and c∈R n-h+1 .
[0223] Step 4: Identify virus codes: Obtain the image features of the code, including LBP features, GLCM features, and n-gram features, fuse the features to different degrees, standardize the data set, use random forest to determine whether the code is a virus code, and determine the risk level of the virus code, which is divided into primary, intermediate, and advanced levels;
[0224] Identifying virus codes includes the following steps:
[0225] S41, analyzing the original data, converting the file with the suffix .byte into a grayscale image, extracting the LBP features and GLCM features of the grayscale image, and extracting the n-gram features from the file with the suffix .asm;
[0226] S42, using LBP features, GLCM features and n-gram features as single features to obtain a data set of codes, and performing standardization on the data set;
[0227] S43, dividing the processed data set into a training set and a test set, using random forest to classify the data set, determining whether the code is a virus code, searching in the virus code library whether there is a similar virus code, testing the accuracy of the virus code determination, classifying the determined virus code, and storing it in the virus code library.
[0228] Step 5. Code optimization: Send the reviewed and identified excellent code to the code library, introduce a temporary variable array, establish replacement rules, replace memory virtual operations with entity operations, obtain optimized code, run the code to test its optimization effect, and store the optimized code in the library.
[0229] The replacement rules include the following:
[0230] Replacement rule C1: If there are adjacent specific instruction actions op_mov_im(A, Im) and op_st(reg(α), A) where A∈{T0, T1, A0}, then op_mov_im(reg[α], Im)) can be used to replace op_mon_im(A, Im) and op_st(reg(α), A);
[0231] Replacement rule C2: If there are adjacent specific instruction actions op(A), op_ld(B, reg(α)), op(B, A) and op_st(reg(α), B), where A, B∈{T0,T1,A0}, then op(reg[α],A) can be used to replace op_ld(B, reg(α)), op(B,A) and op_st(reg(α),B);
[0232] Replacement rule C3: If there are adjacent specific instruction actions op_ld(A, reg(α)), op(A) and op_st(reg(α), A), where A∈{T0,T1,A0}, then op(reg[α]) can be used to replace op_ld(A, reg(α)), op(A) and op_st(reg(α), A);
[0233] Replacement rule C4: If there are adjacent specific instruction actions op(A), op_ld(A, reg(α)) and op_s(reg(β), A), where A∈{T0,T1,A0}, then mov_reg_reg(reg[β],reg[α]) can be used to replace op(A), op_ld(A, reg(α)) and op_st(reg(β), A).
[0234] By extracting the semantic features and image features of the code, the audit result prediction and virus code judgment are performed on the code respectively. After obtaining the LBP features, GLCM features and n-gram features of the code, the LBP features, GLCM features and n-gram features are used as multiple single features to construct a code dataset. The random forest is used to classify the dataset to determine whether the code is virus code and test the accuracy of virus code judgment.
[0235] Comparative Example 1
[0236] Please refer to Figure 1-4 As shown, this embodiment provides a code review and optimization system based on machine learning and a method of using the same, which is roughly the same as Embodiment 1, with the main difference being that in step 2, the abstract syntax tree is not optimized.
[0237] Comparative Example 2
[0238] Please refer to Figure 1-4 As shown, this embodiment provides a code review and optimization system based on machine learning and a method of using the same, which is roughly the same as Embodiment 1, with the main difference being that in step three, when obtaining the semantic feature vector of the code, the code segments are not segmented.
[0239] Test and result analysis:
[0240] The machine learning-based code review and optimization system and its use method used in Examples 1-3 are recorded as Example Group 1-3, and the machine learning-based code review and optimization system and its use method used in Comparative Example 1-2 are recorded as Comparative Group 1-2. The virus code judgment accuracy and code optimization reduction rate of the machine learning-based code review and optimization system and its use method of Example Group 1-3 and Comparative Group 1-2 are tested, and the relevant experimental data are recorded in Table 1.
[0241] Table 1: Test data record table
[0242] Group Judgment accuracy (%) Optimization reduction rate (%) Example 1 Group 100 32.17 Example 2 Group 99 31.25 Example 3 Group 98 32.00 Comparison group 1 78 25.68 Comparison of 2 groups 88 23.99
[0243] As can be seen from Table 1, the machine learning-based code review and optimization system and its use method used by implementation groups 1-3 have a much higher accuracy rate in judging virus codes than comparison groups 1-2, and the optimization reduction rate of the machine learning-based code review and optimization system and its use method used by implementation groups 1-3 is higher than that of comparison groups 1-2. Therefore, the machine learning-based code review and optimization system and its use method used in Example 1 have a higher accuracy rate in judging virus codes. In particular, Example 1 performs multi-feature fusion of LBP features, GLCM features and n-gram features in the process of judging virus codes, which improves the accuracy rate of judging virus codes compared with Examples 2 and 3.
[0244] The present invention checks the obtained source code for duplicates, calculates the code similarity, removes the code with high similarity existing in the library, obtains the effective source code, improves the efficiency of code review, optimizes the abstract syntax tree, uses the optimized abstract syntax tree to extract the semantic feature information in the effective source code, and obtains its image feature information at the same time, divides the source code into a plurality of code segments, discretizes the code information, reduces the subsequent calculation burden on the code, uses the semantic feature vector of each code segment to build a semantic feature matrix, obtains the annotation information data of the code, assists the convolution neural network to perform convolution operation, predicts the code review result, provides more information support for the code review, and improves the accuracy of the review prediction, integrates the LBP feature, GLCM feature and n-gr am feature in the code image feature, uses the random forest to judge whether the code is a virus code, prevents the security of the code library from being affected by the omission of the virus code, judges the risk level of the virus code, and stores it in the virus code library, sends the excellent code after review and judgment to the code library, introduces a temporary variable array, establishes a replacement rule, replaces the memory virtual operation with the body operation, obtains the optimized code, and improves the quality of the code.
[0245] Although the present invention has been described in detail with reference to the aforementioned embodiments, it is still possible for those skilled in the art to modify the technical solutions described in the aforementioned embodiments, or to make equivalent substitutions for some of the technical features therein. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. Code review and optimization system based on machine learning, characterized by: It includes a source code decomposition unit (1), a code review module (2), a virus code detection unit (3) and a code optimization unit (4); The source code decomposition unit (1) is used to check for duplicates and obtain valid source code, construct an abstract syntax structure of the source code using an optimized abstract syntax tree, extract semantic feature information from the source code, and simultaneously extract image feature information from the source code; The source code decomposition unit (1) comprises a code duplication checking module (11), an abstract syntax tree optimization module (12), a semantic feature extraction module (13) and an image feature extraction module (14); The code duplication checking module (11) is used to check the source code for duplication and remove the source code that has a high duplication with the code in the library; The code duplication checking module (11) comprises a feature enhancement module (111) and a similarity comparison module (112); The feature enhancement module (111) is used to extract features using a graph convolutional neural network and perform feature enhancement using a graph attention mechanism; The similarity comparison module (112) is used to obtain similarity vectors between different codes, calculate the similarity between codes, and eliminate source codes with high similarity; The abstract syntax tree optimization module (12) is used to screen the node information characteristics of the abstract syntax tree, remove redundant node information, and obtain an optimized abstract syntax tree; The semantic feature extraction module (13) is used to extract semantic features using an optimized abstract syntax tree; The image feature extraction module (14) is used to extract LBP features, GLCM features and n-gram features in the data; The code review module (2) is used to obtain the semantic feature matrix of the source code and use a convolutional neural network to predict the code review result; The code review module (2) comprises a source code segmentation module (21), an annotation information acquisition module (22) and a review result prediction module (23); The source code segmentation module (21) is used to segment the entire source code into a plurality of code segments, obtain the semantic feature vector of each code segment, and construct a semantic feature matrix; The annotation information acquisition module (22) is used to acquire data including the code before and after modification and the annotation information of the code submitted by the developer, so as to provide more information support for code prediction; The audit result prediction module (23) is used to predict the audit result and obtain a qualified code; The virus code detection unit (3) is used to extract virus code features and identify virus codes in the code; The virus code detection unit (3) comprises a feature fusion module (31), a code training module (32) and a security determination module (33); The feature fusion module (31) is used to obtain multiple features for fusion; The code training module (32) is used to standardize the data set and use random forest to identify the code; The security determination module (33) is used to determine the risk level of the virus code; The code optimization unit (4) is used to optimize the intermediate code for the specific instruction action that can match the rule using the established mapping formula; The code optimization unit (4) comprises a mapping construction module (41), a replacement rule establishment module (42) and an optimization result testing module (43); The mapping construction module (41) is used to introduce a temporary variable array and map the memory virtual operation to the local operation; The replacement rule establishment module (42) is used to establish a replacement rule to replace the original memory virtual operation with a local operation; The optimization result testing module (43) is used to execute the replaced code and test its optimization effect.
2. A method for using a code review and optimization system based on machine learning, characterized in that: The machine learning-based code review and optimization system according to claim 1 is used, comprising the following steps: S1. Code duplication check: Get the source code, use graph convolutional neural network to extract code features, obtain the similarity vectors between different codes, calculate the similarity between the codes, sort the calculation results, remove the codes with high similarity, and obtain the valid source code; S2, code decomposition: use the optimized abstract syntax tree to extract the semantic feature information in the valid source code, and extract the image feature information of the valid source code; S3. Audit result prediction: Divide the source code into several code segments, obtain the semantic feature vector of each code segment, build a semantic feature matrix, and obtain the code annotation information data at the same time. Use convolutional neural network to perform convolution operation and predict the code audit result. S4. Identify virus codes: Obtain image features of the code, including LBP features, GLCM features, and n-gram features, fuse the features to different degrees, standardize the data set, use random forest to determine whether the code is a virus code, and determine the risk level of the virus code, which is divided into primary, intermediate, and advanced risk levels; S5. Code optimization: Send the excellent code after review and judgment into the code library, introduce a temporary variable array, establish replacement rules, replace the virtual operation of memory with the main operation, obtain the optimized code, run the code to test its optimization effect, and store the optimized code in the library.
3. The method for using the machine learning-based code review and optimization system according to claim 2, characterized in that: In step S2, the optimized abstract syntax tree includes the following contents: differentiating the nodes of the abstract syntax tree, selectively removing necessary nodes according to the information characteristics of the nodes, and reconstructing the optimized abstract syntax tree.
4. The method for using the machine learning-based code review and optimization system according to claim 2, characterized in that: In step S3, the convolution operation using the convolutional neural network includes the following: Let the semantic vector x corresponding to the Ith code segment after source code segmentation be i ∈R d , a source code divided into n sections is represented as: A single convolution operation corresponds to a convolution kernel w∈R bd , the convolution kernel processes the window that holds h words to generate a new feature c i : c i =f(w·x i;i+h-1 +a); The feature map formula of the semantic feature vector is as follows: c={c1,c2,...c n-h+1 }; Take the maximum value ξ=max{c} as the feature after convolution kernel convolution. After convolution and operation of m convolution kernels, the feature map T={ξ1,...,ξ m }, The value of a neuron is calculated as follows: y (i) =w (i) T+a (i) ; The input Softmax function is normalized to obtain the discrete probability distribution of the source code on all predicted labels as follows: [and (1) ,and (2) ,...,and (j) ]; Among them, a∈R is the deviation value, f(·) represents the nonlinear function, and c∈R n-h+1 .
5. The method for using the machine learning-based code review and optimization system according to claim 2, characterized in that: In step S4, the virus code identification includes the following steps: S41, analyzing the original data, converting the file with the suffix .byte into a grayscale image, extracting the LBP features and GLCM features of the grayscale image, and extracting the n-gram features from the file with the suffix .asm; S42, performing different combinations of LBP features, GLCM features and n-gram features to obtain feature fusion of the code, and performing standardization on the data set; S43, dividing the processed data set into a training set and a test set, using random forest to classify the data set, determining whether the code is a virus code, searching in the virus code library whether there is a similar virus code, testing the accuracy of the virus code determination, classifying the determined virus code, and storing it in the virus code library.
6. The method for using the machine learning-based code review and optimization system according to claim 2, characterized in that: In step S5, the replacement rule includes the following contents: Replacement rule C1: If there are adjacent specific instruction actions op_mov_im(A, Im) and op_st(reg(α), A) where A∈{T0, T1, A0}, then op_mov_im(reg[α], Im)) can be used to replace op_mon_im(A, Im) and op_st(reg(α), A); Replacement rule C2: If there are adjacent specific instruction actions op(A), op_ld(B, reg(α)), op(B, A) and op_st(reg(α), B), where A, B∈{T0,T1,A0}, then op(reg[α],A) can be used to replace op_ld(B, reg(α)), op(B,A) and op_st(reg(α),B); Replacement rule C3: If there are adjacent specific instruction actions op_ld(A, reg(α)), op(A) and op_st(reg(α), A), where A∈{T0,T1,A0}, then op(reg[α]) can be used to replace op_ld(A, reg(α)), op(A) and op_st(reg(α), A); Replacement rule C4: If there are adjacent specific instruction actions op(A), op_ld(A, reg(α)) and op_s(reg(β), A), where A∈{T0,T1,A0}, then mov_reg_reg(reg[β],reg[α]) can be used to replace op(A), op_ld(A, reg(α)) and op_st(reg(β), A).
