A method, device, computer equipment and storage medium for detecting code defects
By building a self-attention learning model that combines historical defect information and code segment feature information, and performs code slicing processing, the problem of difficult to take into account both the efficiency and accuracy of code defect detection in the existing technology is solved, and efficient and precise positioning of code defects is achieved.
Patent Information
- Application Number
- CN202411805289.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-10
- Publication Date
- 2025-04-22
- Estimated Expiration
- 2044-12-10
AI Technical Summary
The prior art is difficult to find a balance between efficiency and accuracy, performing code defect detection, especially when software scale and complexity increase.
By constructing a defect detection model that combines historical defect information and code fragment feature information, using the self-attention learning mechanism for defect identification, and obtaining more fine-grained code fragments through code slicing processing based on control dependencies and data dependencies.
It realizes more accurate and accurate positioning of defects in the detected code, and improves the efficiency and accuracy of code defect detection.
Smart Images

Figure CN119272278B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a code defect detection method, device, computer equipment and storage medium. Background Art
[0002] With the rapid development of computer technology, the demand for computer software in all walks of life is increasing. The security defects in the software have left huge security risks to the computer system.
[0003] As software scale and complexity gradually increase, conventional code defect detection methods are difficult to guarantee the efficiency and accuracy of code defect detection. Therefore, how to quickly locate code defects is an important issue in the field of computer security. Summary of the invention
[0004] The embodiments of the present disclosure at least provide a code defect detection method, apparatus, computer equipment and storage medium.
[0005] In a first aspect, an embodiment of the present disclosure provides a code defect detection method, comprising:
[0006] Get the code to be tested;
[0007] Inputting the code to be detected into a trained defect detection model to obtain a defect detection result of the code to be detected;
[0008] The defect detection model is trained according to the following steps:
[0009] Obtaining target code and historical defect information of the target code;
[0010] Determining the control dependency between each code statement in the target code and the data dependency between each variable;
[0011] According to the control dependency relationship between the code statements and the data dependency relationship between the variables, the target code is sliced to obtain code fragments, and feature information of each code fragment is determined;
[0012] Based on the historical defect information and the feature information of the code snippet, the defect detection model to be trained is adjusted until a trained defect detection model is obtained.
[0013] In an optional implementation, the target code is sliced according to the control dependency relationship between the code statements and the data dependency relationship between the variables to obtain code fragments, and feature information of each code fragment is determined, including:
[0014] A program dependency graph is constructed according to the control dependency relationships between the code statements and the data dependency relationships between the variables; the nodes included in the program dependency graph are code statements or variables; the edges between the nodes included in the program dependency graph are control dependency relationships between code statements or data dependency relationships between variables;
[0015] Determining attribute characteristics of each node according to the program dependency graph;
[0016] Clustering the nodes according to the attribute characteristics of the nodes to obtain a plurality of node clusters, and segmenting the target code according to the node clusters to obtain various code blocks;
[0017] For each code block, the center point of the node cluster corresponding to the code block is taken as the key node, and the code block is bidirectionally sliced to obtain a code fragment, and the attribute characteristics of the nodes contained in the code fragment are determined.
[0018] In an optional implementation, the nodes are clustered according to the attribute characteristics of the nodes to obtain multiple node clusters, including:
[0019] According to the attribute characteristics of each node, an adjacency matrix and a degree matrix corresponding to the target code are generated; the adjacency matrix is used to represent the connectivity relationship between each node; the degree matrix is used to represent the number of nodes connected to each node;
[0020] Normalizing the adjacency matrix and the degree matrix to obtain a Laplace matrix, and calculating the eigenvalues and eigenvectors of the Laplace matrix;
[0021] Generate an eigenvector matrix based on eigenvectors corresponding to a first preset number of eigenvalues of the Laplacian matrix;
[0022] The row vectors of the feature vector matrix are clustered based on an unsupervised learning algorithm to obtain a plurality of node clusters.
[0023] In an optional implementation, for each code block, a center point of a node cluster corresponding to the code block is used as a key node, and a bidirectional slicing process is performed on the code block to obtain a code fragment, including:
[0024] For each code block, taking the center point of the node cluster corresponding to the code block as the key node, starting from the key node, traversing forward along the program dependency graph, determining a first target node associated with the key node, and traversing backward along the program dependency graph, determining a second target node associated with the key node;
[0025] Generate a forward code slice based on the key node, the first target node, and the edge between the key node and the first target node; generate a backward code slice based on the key node, the second target node, and the edge between the key node and the second target node;
[0026] Based on the forward code slicing and the backward code slicing, a code snippet is obtained.
[0027] In an optional implementation, adjusting parameters of a defect detection model to be trained based on the historical defect information and the feature information of the code snippet includes:
[0028] Performing feature representation on the historical defect information to obtain a historical defect feature vector;
[0029] Obtaining a code snippet feature vector according to the attribute features of the nodes contained in the code snippet;
[0030] Performing feature fusion processing on the historical defect feature vector and the code snippet feature vector to obtain a fused feature vector;
[0031] Based on the fused feature vector, the parameters of the defect detection model to be trained are adjusted.
[0032] In an optional implementation, the historical defect information includes defect type, defect frequency and defect severity;
[0033] The historical defect information is characterized to obtain a historical defect feature vector, including:
[0034] Performing one-hot encoding on the defect type to obtain a defect type feature vector;
[0035] Normalizing the defect frequency to obtain a defect frequency feature vector;
[0036] One-hot encoding the defect severity to obtain a defect severity feature vector;
[0037] A historical defect feature vector is obtained based on the defect type feature vector, the defect frequency feature vector and the defect severity feature vector.
[0038] In an optional implementation, the defect detection model includes a multi-head self-attention module, a first residual network module, a feedforward network module, and a second residual network module;
[0039] Based on the historical defect information and the feature information of the code snippet, adjusting the parameters of the defect detection model to be trained until a trained defect detection model is obtained, including:
[0040] The fused feature vector is input into an embedding representation to obtain a query matrix, a key matrix and a value matrix; the query matrix and the key matrix contain position information in the fused feature vector; the value matrix contains numerical information in the fused feature vector;
[0041] Input the query matrix, key matrix and value matrix into the multi-head self-attention module to obtain a multi-head self-attention result;
[0042] Inputting the multi-head self-attention result and the fused feature vector into the first residual network module to obtain a first processing result;
[0043] Inputting the first processing result into the feedforward network module to obtain a second processing result;
[0044] Inputting the first processing result and the second processing result into the second residual network module to obtain a third processing result, and decoding the third processing result to obtain a defect detection result of the code fragment;
[0045] Based on the third processing result, the parameters of the defect detection model to be trained are adjusted until a trained defect detection model is obtained.
[0046] In a second aspect, the present disclosure also provides a code defect detection device, including:
[0047] An acquisition module is used to obtain the code to be detected;
[0048] An input module is used to input the code to be detected into a trained defect detection model to obtain a defect detection result of the code to be detected; the defect detection model is trained according to the following steps: obtaining a target code and historical defect information of the target code; determining a control dependency relationship between each code statement in the target code and a data dependency relationship between each variable; slicing the target code according to the control dependency relationship between each code statement and the data dependency relationship between each variable to obtain each code fragment, and determining feature information of each code fragment; adjusting parameters of the defect detection model to be trained based on the historical defect information and the feature information of the code fragment until a trained defect detection model is obtained.
[0049] In a third aspect, an embodiment of the present disclosure further provides a computer device, comprising: a processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the computer device is running, the processor and the memory communicate via the bus, and when the machine-readable instructions are executed by the processor, the steps of the above-mentioned first aspect, or any possible implementation of the first aspect are performed.
[0050] In a fourth aspect, an embodiment of the present disclosure further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the above-mentioned first aspect or any possible implementation of the first aspect are executed.
[0051] The disclosed embodiments provide a code defect detection method, in which a model for code defect detection is trained using historical defect information and feature information of code snippets as input, and self-attention learning is performed on the basis of the historical defect information and feature information of code snippets, so that the defect detection model obtained by training can more accurately identify defects in the code to be detected; in addition, during the training process, the code is sliced based on the control dependency relationship between each code statement and the data dependency relationship between each variable to obtain a finer-grained code snippet, so that the defect detection model obtained by training can more accurately locate the defects in the code to be detected.
[0052] For a description of the effects of the above-mentioned code defect detection device, computer equipment, and computer-readable storage medium, please refer to the description of the above-mentioned code defect detection method, which will not be repeated here.
[0053] In order to make the above-mentioned objectives, features and advantages of the present disclosure more obvious and easy to understand, preferred embodiments are specifically cited below and described in detail with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0054] In order to more clearly illustrate the technical solutions of the embodiments of the present disclosure, the following is a brief introduction to the drawings required for use in the embodiments. The drawings herein are incorporated into the specification and constitute a part of the specification. These drawings illustrate embodiments consistent with the present disclosure and are used together with the specification to illustrate the technical solutions of the present disclosure. It should be understood that the following drawings only illustrate certain embodiments of the present disclosure and should not be regarded as limiting the scope. For ordinary technicians in this field, other relevant drawings can also be obtained based on these drawings without creative work.
[0055] Figure 1 A flow chart of a code defect detection method provided by an embodiment of the present disclosure is shown;
[0056] Figure 2A flow chart of a model training method provided by an embodiment of the present disclosure is shown;
[0057] Figure 3 A schematic diagram of the structure of a defect detection model provided by an embodiment of the present disclosure is shown;
[0058] Figure 4 The graph shows the detection accuracy change curves corresponding to different defect types during the verification phase provided by the embodiment of the present disclosure;
[0059] Figure 5 A comparison diagram of the results of various detection indicators under different defect types during the test phase provided by the embodiment of the present disclosure is shown;
[0060] Figure 6 A schematic diagram of the structure of a code defect detection device provided by an embodiment of the present disclosure is shown;
[0061] Figure 7 A schematic diagram of a computer device provided by an embodiment of the present disclosure is shown. DETAILED DESCRIPTION
[0062] In order to make the purpose, technical scheme and advantages of the embodiments of the present disclosure clearer, the technical scheme in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the drawings in the embodiments of the present disclosure. Obviously, the described embodiments are only part of the embodiments of the present disclosure, rather than all of the embodiments. The components of the embodiments of the present disclosure generally described and shown in the drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present disclosure provided in the drawings is not intended to limit the scope of the present disclosure for protection, but merely represents the selected embodiments of the present disclosure. Based on the embodiments of the present disclosure, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present disclosure.
[0063] Research has found that with the rapid development of computer technology, the demand for computer software in various industries has continued to increase. The security defects in the software have left huge security risks to the computer system. Therefore, how to quickly locate code defects is an important issue in the field of computer security.
[0064] Conventional code defect detection methods can generally be divided into static analysis methods, dynamic analysis methods, and hybrid analysis methods. Conventional static analysis methods do not execute software or programs, but perform defect detection based on the attribute characteristics of the source code. Although they have higher execution efficiency and code coverage, they have a higher false positive rate. Dynamic analysis methods require the execution of programs and perform defect detection based on the behavioral characteristics of software or programs. Although they have higher detection accuracy, the above process requires the analysis of a large amount of code, resulting in low execution efficiency and coverage, and a high false negative rate. Hybrid analysis methods combine static analysis methods and dynamic analysis methods, but also inherit the limitations of both. Therefore, conventional code defect detection methods are difficult to guarantee the efficiency and accuracy of code defect detection.
[0065] Based on this, the present disclosure provides a code defect detection method, in which a model for code defect detection is trained using historical defect information and feature information of code snippets as input, and self-attention learning is performed by combining historical defect information and feature information of code snippets. The defect detection model obtained by training can more accurately identify defects in the code to be detected; in addition, during the training process, the code is sliced based on the control dependency relationship between each code statement and the data dependency relationship between each variable to obtain a finer-grained code snippet, so that the defect detection model obtained by training can more accurately locate the defects in the code to be detected.
[0066] The defects existing in the above schemes and the proposed solutions are the results obtained by the inventor after practice and careful research. Therefore, the discovery process of the above problems and the solutions proposed by the present disclosure for the above problems below should be the contributions made by the inventor to the present disclosure during the disclosure process.
[0067] It should be noted that similar reference numerals and letters denote similar items in the following drawings, and therefore, once an item is defined in one drawing, further definition and explanation thereof is not required in subsequent drawings.
[0068] To facilitate understanding of this embodiment, a code defect detection method disclosed in the embodiment of the present disclosure is first introduced in detail. The execution subject of the code defect detection method provided in the embodiment of the present disclosure is generally a computer device with certain computing capabilities.
[0069] See also Figure 1 FIG. 1 is a flowchart of a code defect detection method provided by an embodiment of the present disclosure, the method comprising S101 to S102, wherein:
[0070] S101: Obtain the code to be detected.
[0071] S102: Input the code to be detected into a trained defect detection model to obtain a defect detection result of the code to be detected; the defect detection model is trained according to the following steps: obtain the target code and the historical defect information of the target code; determine the control dependency relationship between each code statement in the target code and the data dependency relationship between each variable; slice the target code according to the control dependency relationship between each code statement and the data dependency relationship between each variable to obtain each code fragment, and determine the feature information of each code fragment; adjust the parameters of the defect detection model to be trained based on the historical defect information and the feature information of the code fragment until a trained defect detection model is obtained.
[0072] In the embodiments of the present disclosure, the code to be detected may be a complete source code or a part of the complete source code.
[0073] The code to be detected is input into the trained defect detection model, and the defect detection result of the code to be detected obtained may include information such as the defect location or defect type of the code to be detected.
[0074] In the process of training the defect detection model, the target code obtained can be the complete source code or part of the complete source code.
[0075] The target code may be a code with defect information. The historical defect information of the target code may be defect information that occurred in the target code within a preset historical time period. After a defect problem occurs in the target code during execution, the defect information that occurs may be recorded.
[0076] In an embodiment of the present disclosure, historical defect information may include defect information such as defect type, defect frequency, and defect severity. Among them, the defect type may refer to the type of defect that occurs in the target code, which may specifically include null pointer reference, buffer overflow, formatted string, improper error handling, etc. The defect frequency may refer to the frequency information of defects occurring in the target code. The defect severity may refer to the defect severity level information occurring in the target code. The defect severity level may be determined based on the severity of the system failure or other failure caused by the target code during execution. Exemplarily, the defect severity level may be divided into generally serious, relatively serious, very serious, etc.; or, it may be divided into level one, level two, level three, etc., the higher the level, the higher the severity. The division of defect severity levels is not specifically limited here.
[0077] In the disclosed embodiment, the control dependency relationship between the various code statements can be determined based on the branch statements (such as if statements, switch statements, etc.) in the target code and the loop structure of the target code, etc. The data dependency relationship between the various variables can be determined based on the definition of the various variables in the target code and the usage relationship between the various variables.
[0078] By analyzing the control dependencies between code statements and the data dependencies between variables, the target code can be divided to obtain more fine-grained code fragments, which can enable more accurate positioning of defects in the target code.
[0079] Code defects usually originate from target nodes that may cause security issues, such as variables or code statements. In order to accurately locate the target nodes, the program dependency graph can be used to cluster the nodes to obtain node clusters, and then the target nodes in the node clusters are used as the dividing points to divide the target code to obtain code fragments, and then the defects of each code fragment can be located.
[0080] In one embodiment, the target code is sliced according to the control dependency relationship between the code statements and the data dependency relationship between the variables to obtain the code fragments, and the characteristic information of each code fragment is determined, which may include: constructing a program dependency graph according to the control dependency relationship between the code statements and the data dependency relationship between the variables; the nodes contained in the program dependency graph are code statements or variables; the edges between the nodes contained in the program dependency graph are the control dependency relationship between code statements or the data dependency relationship between variables; determining the attribute characteristics of each node according to the program dependency graph; clustering the nodes according to the attribute characteristics of each node to obtain multiple node clusters, and slicing the target code according to the node clusters to obtain each code block; for each code block, taking the center point of the node cluster corresponding to the code block as the key node, bidirectionally slicing the code block to obtain a code fragment, and determining the attribute characteristics of the nodes contained in the code fragment.
[0081] In the above implementation, a control dependency graph can be constructed based on the branch statements in the target code and the loop structure of the target code. The control dependency graph can include the control dependency relationship between the various code statements. A data dependency graph can be constructed based on the definition of each variable in the target code and the usage relationship between each variable. The data dependency graph can include the data dependency relationship between each variable.
[0082] After obtaining the control dependency graph and data dependency graph, the two can be integrated to generate a complete program dependency graph. .in, The nodes contained in the corresponding program dependency graph can represent code statements or variables in the target code; The edges between the nodes included in the corresponding program dependency graph can represent the control dependency relationship between code statements or the data dependency relationship between variables.
[0083] According to the program dependency graph, the dependency relationship between nodes can be extracted and the attribute characteristics of each node can be determined. The attribute characteristics of the node can include the execution frequency information of the node and the dependency strength information between nodes (such as the dependency path length, the number of dependent nodes, etc.).
[0084] After obtaining the attribute features of the nodes contained in the code snippet, the nodes may be clustered based on the attribute features of the nodes to obtain multiple node clusters. In a specific implementation, the nodes may be clustered according to any feasible clustering method, which may not be specifically limited here.
[0085] The target code can be divided into code blocks with different functions or higher correlation according to node clusters.
[0086] For each code block, a finer-grained division can be performed to obtain code snippets.
[0087] The center point of a node cluster is usually the node with the strongest dependency, and may be the key node where the defect occurs. Therefore, in the implementation process, for each code block, the center point of the node cluster corresponding to the code block can be used as the key node, and the code block can be bidirectionally sliced to obtain a code fragment. Then, the attribute features of the nodes contained in the code fragment can be determined based on the attribute features of each node contained in the target code. The attribute features of the nodes contained in the code fragment are the feature information of the code fragment.
[0088] In the embodiment of the present disclosure, a spectral clustering method may be used to cluster nodes.
[0089] In one embodiment, the nodes are clustered according to the attribute characteristics of the nodes to obtain multiple node clusters, including: generating an adjacency matrix and a degree matrix corresponding to the target code according to the attribute characteristics of the nodes; the adjacency matrix is used to represent the connectivity between the nodes; the degree matrix is used to represent the number of nodes connected to each node; the adjacency matrix and the degree matrix are normalized to obtain a Laplace matrix, and the eigenvalues and eigenvectors of the Laplace matrix are calculated; an eigenvector matrix is generated based on the eigenvectors corresponding to the first preset number of eigenvalues of the Laplace matrix; and the row vectors of the eigenvector matrix are clustered based on an unsupervised learning algorithm to obtain multiple node clusters.
[0090] In the above implementation, the adjacency matrix A and the degree matrix D may be generated according to the attribute characteristics of each node.
[0091] Among them, the adjacency matrix A can be one of the program dependency graph G Matrix, used to represent the connectivity between nodes. , then it means the node With Node There is a connection relationship between , then it means the node With Node There is no connection between them.
[0092] The degree matrix D is a diagonal matrix. The degree matrix can be expressed as , which is used to represent the degree of each node, that is, the number of nodes connected to each node.
[0093] Next, the normalized Laplace matrix can be calculated based on the adjacency matrix and degree matrix In specific implementation, it can be calculated according to the following formula: .in, is the identity matrix, is the inverse square root matrix of the degree matrix D.
[0094] In the Laplace matrix Afterwards, the Laplacian matrix can be calculated The eigenvalue of and the eigenvector .
[0095] Then you can select the eigenvectors corresponding to the first preset number of eigenvalues of the Laplacian matrix to generate the eigenvector matrix .
[0096] Next, we can use the eigenvector matrix The row vectors of are clustered based on the unsupervised learning algorithm k-means to obtain multiple node clusters.
[0097] In one embodiment, for each code block, the center point of the node cluster corresponding to the code block is taken as the key node, and the code block is bidirectionally sliced to obtain a code fragment, including: for each code block, the center point of the node cluster corresponding to the code block is taken as the key node, starting from the key node, traversing forward along the program dependency graph to determine a first target node having an association relationship with the key node, and traversing backward along the program dependency graph to determine a second target node having an association relationship with the key node; based on the key node, the first target node, and the edge between the key node and the first target node, generating a forward code slice; based on the key node, the second target node, and the edge between the key node and the second target node, generating a backward code slice; based on the forward code slice and the backward code slice, obtaining a code fragment.
[0098] In the above implementation, for each code block, the center point of the node cluster corresponding to the code block is taken as the key node, starting from the key node, traversing forward along the program dependency graph, and tracking the code statements and / or variables affected by the key node, that is, the first target node associated with the key node. Here, the first target node associated with the key node may include nodes directly and indirectly connected to the key node during the process of traversing forward along the program dependency graph.
[0099] For each code block, the center point of the node cluster corresponding to the code block is taken as the key node, starting from the key node, traversing backward along the program dependency graph, tracking the code statements and / or variables affected by the key node, that is, the second target node associated with the key node. Here, the second target node associated with the key node may include nodes directly connected and indirectly connected to the key node during the backward traversal along the program dependency graph.
[0100] After obtaining the forward code slice and the backward code slice, the forward code slice and the backward code slice can be combined, and a code snippet can be obtained according to the nodes and edges included in the forward code slice and the backward code slice. The code snippet covers all nodes and edges related to the key node.
[0101] In the embodiment of the present disclosure, after obtaining the historical defect information and the feature information of the code snippet, the historical defect information and the feature information of the code snippet can be subjected to multimodal fusion processing, and then the multimodal fusion features are input into the defect detection model to be trained.
[0102] In one embodiment, based on the historical defect information and the feature information of the code snippet, the parameters of the defect detection model to be trained are adjusted, including: feature representation of the historical defect information to obtain a historical defect feature vector; obtaining a code snippet feature vector based on the attribute features of the nodes contained in the code snippet; feature fusion processing of the historical defect feature vector and the code snippet feature vector to obtain a fused feature vector; and based on the fused feature vector, the parameters of the defect detection model to be trained are adjusted.
[0103] In the disclosed embodiment, the historical defect information and the feature information of the code snippet may be embedded and represented respectively, that is, a historical defect feature vector and a code snippet feature vector may be obtained respectively.
[0104] According to the characteristic information of the code snippet, the code snippet can be segmented to obtain a decomposed word Token sequence, such as "int", "main", "()", ")", "VAR3", ";", etc.
[0105] Then, the Skip-gram model can be used to represent each token sequence as a token vector. For example, "int" can be represented as [0.1, 0.3, ... 0.5]; and "main" can be represented as [0.2, 0.3, ... 0.6], etc.
[0106] According to the Token vector, the embedding matrix can be generated .in, , n is the number of tokens, is the dimension of the embedding matrix.
[0107] For historical defect information, a historical defect feature vector can be obtained in the following manner. In one embodiment, when the historical defect information includes defect type, defect frequency, and defect severity, the historical defect information is characterized to obtain a historical defect feature vector, including: performing one-hot encoding on the defect type to obtain a defect type feature vector; normalizing the defect frequency to obtain a defect frequency feature vector; performing one-hot encoding on the defect severity to obtain a defect severity feature vector; and obtaining a historical defect feature vector based on the defect type feature vector, the defect frequency feature vector, and the defect severity feature vector.
[0108] The defect type can be encoded one-hot to obtain the defect type feature vector. For example, a null pointer reference can be represented as [1,0,0]; a buffer overflow can be represented as [0,1,0].
[0109] The defect frequency can be normalized. Specifically, the normalization algorithm can be used to calculate the frequency of defects, that is, to obtain the defect frequency feature vector. The normalized defect frequency can be viewed as a one-dimensional feature vector.
[0110] The severity of the defect can be discretized using one-hot encoding to obtain the defect severity feature vector. For example, a high severity defect can be represented as [1,0,0]; a medium severity defect can be represented as [0,1,0]; and a low severity defect can be represented as [0,0,1].
[0111] Finally, the defect type feature vector, defect frequency feature vector and defect severity feature vector can be concatenated to obtain the historical defect feature vector .
[0112] In the feature fusion process, the historical defect feature vector can be and the code snippet feature vector Concatenate into a fusion feature .in, .
[0113] In one embodiment, the above-mentioned defect detection model may include a multi-head self-attention module, a first residual network module, a feedforward network module and a second residual network module.
[0114] Based on the historical defect information and the feature information of the code snippet, the defect detection model to be trained is adjusted until a trained defect detection model is obtained: the fused feature vector is input into an embedded representation to obtain a query matrix, a key matrix and a value matrix; the query matrix and the key matrix contain the position information in the fused feature vector; the value matrix contains the numerical information in the fused feature vector; the query matrix, the key matrix and the value matrix are input into the multi-head self-attention module to obtain a multi-head self-attention result; the multi-head self-attention result and the fused feature vector are input into the first residual network module to obtain a first processing result; the first processing result is input into the feedforward network module to obtain a second processing result; the first processing result and the second processing result are input into the second residual network module to obtain a third processing result; based on the third processing result, the defect detection model to be trained is adjusted until a trained defect detection model is obtained.
[0115] In the above implementation, before the fused feature vector is input into the defect detection model to be trained, the fused feature vector may be input embedded and position encoded.
[0116] In a specific implementation, the input embedding representation can be performed according to the following formula: .in, That is, the above-mentioned fused feature vector; is the query matrix; is the key matrix; is the Value matrix. The query matrix and the key matrix contain the position information in the fused feature vector, which can be used to determine the location of concern during defect detection; the value matrix contains the numerical information in the fused feature vector.
[0117] The query matrix, key matrix and value matrix can be used for attention calculation, and can be input into the multi-head self-attention module in the specific implementation. In the specific implementation, the multi-head attention result can be obtained according to the following formula: .in, express To prevent and The dot product result is too large, so we can divide it by .
[0118] In specific implementation, the multi-head attention mechanism can , , After being mapped by the first fully connected layer, multiple self-attention heads are input into the self-attention module, and then the output results of the multiple self-attention heads are spliced to obtain the splicing result. Finally, the splicing results are integrated through the second fully connected layer to obtain the multi-head self-attention result. In the specific implementation, the head self-attention result can be obtained according to the following formula: ; .in, represents the i-th attention head; , , , is the parameter matrix. After calculating the results of each attention head separately, the output results of multiple self-attention heads are spliced and integrated, and the obtained multi-head self-attention result can be recorded as .
[0119] The multi-head self-attention results and the fused feature vector are input into the first residual network module to obtain the first processing result, which can be obtained according to the following formula: .in, Represents the function corresponding to the first residual network module.
[0120] The first processing result is input into the feedforward network module to obtain the second processing result, which can be obtained according to the following formula: .in, Represents the function corresponding to the feedforward network module.
[0121] The first processing result and the second processing result are input into the second residual network module to obtain a third processing result, which can be obtained according to the following formula: .
[0122] Finally, in each iteration, based on the third processing result, the parameters in the defect detection model to be trained are adjusted. The defect detection model after each parameter adjustment can predict code defects more accurately than the defect detection model after the last parameter adjustment.
[0123] In the embodiment of the present disclosure, a training cutoff condition may be set. The training cutoff condition may include a preset number of iterations or adjusted model parameters to make the defect detection model converge. The defect detection model is trained based on the training cutoff condition until a trained defect detection model is obtained.
[0124] The above-trained defect detection model can be used to detect the input code to be detected and obtain the defect detection result of the code to be detected.
[0125] Specifically, when the code to be detected is input into a trained defect detection model, the defect detection result of the code to be detected is obtained, which can include: determining the control dependency relationship between each code statement in the code to be detected, and the data dependency relationship between each variable; slicing the code to be detected according to the control dependency relationship between each code statement and the data dependency relationship between each variable to obtain each code fragment, and determining the feature information of each code fragment; inputting the feature information of the code fragment into the trained defect detection model to obtain the defect detection result of the code fragment.
[0126] The above process is similar to that in the training phase and will not be repeated here.
[0127] The present disclosure also provides a flow chart of a model training method. Figure 2 shown. Figure 2 The defect detection model in can be a Transformer model. The structure of the defect detection model can be found in Figure 3 .
[0128] exist Figure 2In the process, we can first obtain the source code used in the training process and the historical defect information of the source code. Then, we can build a program dependency graph based on the control dependency relationship between each code statement and the data dependency relationship between each variable, and cluster the nodes (i.e., code statements or variables) in the graph according to the program dependency graph and select the slicing points (i.e., key nodes). Then, we slice the source code according to the slicing points to obtain code snippets. Then, we perform word segmentation on the code snippets and generate the embedding matrix corresponding to the code snippets, i.e., the code snippet feature vector. And, we generate the embedding matrix corresponding to the historical defect information of the source code, i.e., the historical defect feature vector. Next, we fuse the code snippet feature vector and the historical defect feature vector, and input the obtained fused feature vector into the Transformer model to train the Transformer model.
[0129] Figure 3 The eigenvectors in It can refer to the fused feature vector obtained by fusing the code snippet feature vector and the historical defect feature vector. Feature vector After inputting into the embedding layer, It can refer to the embedding matrix that embeds the fused feature vector and is used as input to the Transformer model.
[0130] The specific implementation process of the above-mentioned training of the Transformer model can be referred to the previous description and will not be repeated here.
[0131] In the embodiment of the present disclosure, the source code set used in the training process may include codes for 74 types of code defects, such as using externally controlled format strings, stack buffer overflows, and operating system command injections. According to the defect type, it can be divided into 6 categories, and the source code set can be divided into a training set, a validation set, and a test set in a ratio of 6:2:2 by using stratified sampling.
[0132] Figure 4 The graph shows the detection accuracy change curves corresponding to different defect types during the verification phase. Figure 5 The figure shows the comparison of the results of various detection indicators under different defect types during the testing phase.
[0133] In the code defect detection method provided by the embodiment of the present disclosure, the model for code defect detection is trained by taking historical defect information and feature information of code snippets as input, and self-attention learning is performed by combining historical defect information and feature information of code snippets, so that the defect detection model obtained by training can more accurately identify the defect location in the code; in addition, during the training process, the code is sliced based on the control dependency relationship between each code statement and the data dependency relationship between each variable to obtain a finer-grained code snippet, so that the defect detection model obtained by training can more accurately locate the defects in the code to be detected.
[0134] Those skilled in the art will appreciate that, in the above method of specific implementation, the order in which the steps are written does not imply a strict execution order and does not constitute any limitation on the implementation process. The specific execution order of the steps should be determined by their functions and possible internal logic.
[0135] Based on the same inventive concept, a code defect detection device corresponding to the code defect detection method is also provided in the embodiment of the present disclosure. Since the principle of solving the problem by the device in the embodiment of the present disclosure is similar to the above-mentioned code defect detection method in the embodiment of the present disclosure, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be repeated.
[0136] Reference Figure 6 FIG. 1 is a schematic diagram of a code defect detection device provided by an embodiment of the present disclosure, wherein the device comprises:
[0137] The acquisition module 601 is used to acquire the code to be detected;
[0138] Input module 602 is used as an input module, which is used to input the code to be detected into a trained defect detection model to obtain the defect detection result of the code to be detected; the defect detection model is trained according to the following steps: obtaining the target code and the historical defect information of the target code; determining the control dependency relationship between each code statement in the target code and the data dependency relationship between each variable; slicing the target code according to the control dependency relationship between each code statement and the data dependency relationship between each variable to obtain each code fragment, and determining the feature information of each code fragment; based on the historical defect information and the feature information of the code fragment, adjusting the parameters of the defect detection model to be trained until a trained defect detection model is obtained.
[0139] In an optional implementation, the target code is sliced according to the control dependency relationship between the code statements and the data dependency relationship between the variables to obtain code fragments, and feature information of each code fragment is determined, including:
[0140] A program dependency graph is constructed according to the control dependency relationships between the code statements and the data dependency relationships between the variables; the nodes included in the program dependency graph are code statements or variables; the edges between the nodes included in the program dependency graph are control dependency relationships between code statements or data dependency relationships between variables;
[0141] Determining attribute characteristics of each node according to the program dependency graph;
[0142] Clustering the nodes according to the attribute characteristics of the nodes to obtain a plurality of node clusters, and segmenting the target code according to the node clusters to obtain various code blocks;
[0143] For each code block, the center point of the node cluster corresponding to the code block is taken as the key node, and the code block is bidirectionally sliced to obtain a code fragment, and the attribute characteristics of the nodes contained in the code fragment are determined.
[0144] In an optional implementation, the nodes are clustered according to the attribute characteristics of the nodes to obtain multiple node clusters, including:
[0145] According to the attribute characteristics of each node, an adjacency matrix and a degree matrix corresponding to the target code are generated; the adjacency matrix is used to represent the connectivity relationship between each node; the degree matrix is used to represent the number of nodes connected to each node;
[0146] Normalizing the adjacency matrix and the degree matrix to obtain a Laplace matrix, and calculating the eigenvalues and eigenvectors of the Laplace matrix;
[0147] Generate an eigenvector matrix based on eigenvectors corresponding to a first preset number of eigenvalues of the Laplacian matrix;
[0148] The row vectors of the feature vector matrix are clustered based on an unsupervised learning algorithm to obtain a plurality of node clusters.
[0149] In an optional implementation, for each code block, a center point of a node cluster corresponding to the code block is used as a key node, and a bidirectional slicing process is performed on the code block to obtain a code fragment, including:
[0150] For each code block, taking the center point of the node cluster corresponding to the code block as the key node, starting from the key node, traversing forward along the program dependency graph, determining a first target node associated with the key node, and traversing backward along the program dependency graph, determining a second target node associated with the key node;
[0151] Generate a forward code slice based on the key node, the first target node, and the edge between the key node and the first target node; generate a backward code slice based on the key node, the second target node, and the edge between the key node and the second target node;
[0152] Based on the forward code slicing and the backward code slicing, a code snippet is obtained.
[0153] In an optional implementation, adjusting parameters of a defect detection model to be trained based on the historical defect information and the feature information of the code snippet includes:
[0154] Performing feature representation on the historical defect information to obtain a historical defect feature vector;
[0155] Obtaining a code snippet feature vector according to the attribute features of the nodes contained in the code snippet;
[0156] Performing feature fusion processing on the historical defect feature vector and the code snippet feature vector to obtain a fused feature vector;
[0157] Based on the fused feature vector, the parameters of the defect detection model to be trained are adjusted.
[0158] In an optional implementation, the historical defect information includes defect type, defect frequency and defect severity;
[0159] The historical defect information is characterized to obtain a historical defect feature vector, including:
[0160] Performing one-hot encoding on the defect type to obtain a defect type feature vector;
[0161] Normalizing the defect frequency to obtain a defect frequency feature vector;
[0162] One-hot encoding the defect severity to obtain a defect severity feature vector;
[0163] A historical defect feature vector is obtained based on the defect type feature vector, the defect frequency feature vector and the defect severity feature vector.
[0164] In an optional implementation, the defect detection model includes a multi-head self-attention module, a first residual network module, a feedforward network module, and a second residual network module;
[0165] Based on the historical defect information and the feature information of the code snippet, adjusting the parameters of the defect detection model to be trained until a trained defect detection model is obtained, including:
[0166] The fused feature vector is input into an embedding representation to obtain a query matrix, a key matrix and a value matrix; the query matrix and the key matrix contain position information in the fused feature vector; the value matrix contains numerical information in the fused feature vector;
[0167] Input the query matrix, key matrix and value matrix into the multi-head self-attention module to obtain a multi-head self-attention result;
[0168] Inputting the multi-head self-attention result and the fused feature vector into the first residual network module to obtain a first processing result;
[0169] Inputting the first processing result into the feedforward network module to obtain a second processing result;
[0170] Inputting the first processing result and the second processing result into the second residual network module to obtain a third processing result;
[0171] Based on the third processing result, the parameters of the defect detection model to be trained are adjusted until a trained defect detection model is obtained.
[0172] For descriptions of the processing flow of each module in the device and the interaction flow between each module, reference may be made to the relevant descriptions in the above method embodiment, which will not be described in detail here.
[0173] Based on the same technical concept, the embodiment of the present disclosure also provides a computer device. Figure 7 As shown, it is a schematic diagram of the structure of a computer device 700 provided in an embodiment of the present disclosure, including a processor 701, a memory 702, and a bus 703. Among them, the memory 702 is used to store execution instructions, including a memory 7021 and an external memory 7022; the memory 7021 here is also called an internal memory, which is used to temporarily store the operation data in the processor 701, and the data exchanged with the external memory 7022 such as a hard disk. The processor 701 exchanges data with the external memory 7022 through the memory 7021. When the computer device 700 is running, the processor 701 communicates with the memory 702 through the bus 703, so that the processor 701 executes the following instructions:
[0174] Get the code to be tested;
[0175] Inputting the code to be detected into a trained defect detection model to obtain a defect detection result of the code to be detected;
[0176] The defect detection model is trained according to the following steps:
[0177] Obtaining target code and historical defect information of the target code;
[0178] Determining the control dependency between each code statement in the target code and the data dependency between each variable;
[0179] According to the control dependency relationship between the code statements and the data dependency relationship between the variables, the target code is sliced to obtain code fragments, and feature information of each code fragment is determined;
[0180] Based on the historical defect information and the feature information of the code snippet, the defect detection model to be trained is adjusted until a trained defect detection model is obtained.
[0181] The embodiment of the present disclosure also provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the code defect detection method described in the above method embodiment are executed. The storage medium can be a volatile or non-volatile computer-readable storage medium.
[0182] The embodiments of the present disclosure also provide a computer program product, which carries a program code. The instructions included in the program code can be used to execute the steps of the code defect detection method described in the above method embodiment. For details, please refer to the above method embodiment, which will not be repeated here.
[0183] The computer program product may be implemented in hardware, software or a combination thereof. In one optional embodiment, the computer program product is implemented as a computer storage medium. In another optional embodiment, the computer program product is implemented as a software product, such as a software development kit (SDK).
[0184] Those skilled in the art can clearly understand that, for the convenience and simplicity of description, the specific working process of the device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here. In the several embodiments provided in the present disclosure, it should be understood that the disclosed device and method can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, and the indirect coupling or communication connection of the device or unit can be electrical, mechanical or other forms.
[0185] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0186] In addition, each functional unit in each embodiment of the present disclosure may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0187] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a non-volatile computer-readable storage medium that is executable by a processor. Based on this understanding, the technical solution of the present disclosure, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present disclosure. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0188] Finally, it should be noted that the above-described embodiments are only specific implementation methods of the present disclosure, which are used to illustrate the technical solutions of the present disclosure, rather than to limit them. The protection scope of the present disclosure is not limited thereto. Although the present disclosure is described in detail with reference to the above-described embodiments, ordinary technicians in the field should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above-described embodiments within the technical scope disclosed in the present disclosure, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present disclosure, and should be included in the protection scope of the present disclosure. Therefore, the protection scope of the present disclosure shall be based on the protection scope of the claims.
Claims
1. A code defect detection method, characterized in that: include: Get the code to be tested; Inputting the code to be detected into a trained defect detection model to obtain a defect detection result of the code to be detected; The defect detection model is trained according to the following steps: Obtaining target code and historical defect information of the target code; Determining the control dependency between each code statement in the target code and the data dependency between each variable; A program dependency graph is constructed according to the control dependency relationships between the code statements and the data dependency relationships between the variables; the nodes included in the program dependency graph are code statements or variables; the edges between the nodes included in the program dependency graph are control dependency relationships between code statements or data dependency relationships between variables; Determining attribute characteristics of each node according to the program dependency graph; According to the attribute characteristics of each node, an adjacency matrix and a degree matrix corresponding to the target code are generated; the adjacency matrix is used to represent the connectivity relationship between each node; The degree matrix is used to represent the number of nodes connected to each node; Normalizing the adjacency matrix and the degree matrix to obtain a Laplace matrix, and calculating the eigenvalues and eigenvectors of the Laplace matrix; Generate an eigenvector matrix based on eigenvectors corresponding to a first preset number of eigenvalues of the Laplacian matrix; Clustering the row vectors of the feature vector matrix based on an unsupervised learning algorithm to obtain multiple node clusters; Segmenting the target code according to the node clusters to obtain individual code blocks; For each code block, taking the center point of the node cluster corresponding to the code block as the key node, bidirectional slicing is performed on the code block to obtain a code fragment, and the attribute characteristics of the nodes contained in the code fragment are determined; Based on the historical defect information and the attribute characteristics of the nodes contained in the code snippet, the defect detection model to be trained is adjusted until a trained defect detection model is obtained.
2. The method according to claim 1, characterized in that For each code block, the center point of the node cluster corresponding to the code block is taken as the key node, and the code block is bidirectionally sliced to obtain code fragments, including: For each code block, taking the center point of the node cluster corresponding to the code block as the key node, starting from the key node, traversing forward along the program dependency graph, determining a first target node associated with the key node, and traversing backward along the program dependency graph, determining a second target node associated with the key node; Generate a forward code slice based on the key node, the first target node, and the edge between the key node and the first target node; generate a backward code slice based on the key node, the second target node, and the edge between the key node and the second target node; Based on the forward code slicing and the backward code slicing, a code snippet is obtained.
3. The method according to claim 1, characterized in that Based on the historical defect information and the attribute characteristics of the nodes contained in the code snippet, the defect detection model to be trained is adjusted, including: Performing feature representation on the historical defect information to obtain a historical defect feature vector; Obtaining a code snippet feature vector according to the attribute features of the nodes contained in the code snippet; Performing feature fusion processing on the historical defect feature vector and the code snippet feature vector to obtain a fused feature vector; Based on the fused feature vector, the parameters of the defect detection model to be trained are adjusted.
4. The method according to claim 3, characterized in that The historical defect information includes defect type, defect frequency and defect severity; The historical defect information is characterized to obtain a historical defect feature vector, including: Performing one-hot encoding on the defect type to obtain a defect type feature vector; Normalizing the defect frequency to obtain a defect frequency feature vector; One-hot encoding the defect severity to obtain a defect severity feature vector; A historical defect feature vector is obtained based on the defect type feature vector, the defect frequency feature vector and the defect severity feature vector.
5. The method according to claim 3 or 4, characterized in that: The defect detection model includes a multi-head self-attention module, a first residual network module, a feedforward network module and a second residual network module; Based on the historical defect information and the feature information of the code snippet, adjusting the parameters of the defect detection model to be trained until a trained defect detection model is obtained, including: The fused feature vector is input into an embedding representation to obtain a query matrix, a key matrix and a value matrix; the query matrix and the key matrix contain position information in the fused feature vector; the value matrix contains numerical information in the fused feature vector; Input the query matrix, key matrix and value matrix into the multi-head self-attention module to obtain a multi-head self-attention result; Inputting the multi-head self-attention result and the fused feature vector into the first residual network module to obtain a first processing result; Inputting the first processing result into the feedforward network module to obtain a second processing result; Inputting the first processing result and the second processing result into the second residual network module to obtain a third processing result; Based on the third processing result, the parameters of the defect detection model to be trained are adjusted until a trained defect detection model is obtained.
6. A code defect detection device, characterized in that: include: An acquisition module is used to obtain the code to be detected; An input module, used for inputting the code to be detected into a trained defect detection model to obtain a defect detection result of the code to be detected; The defect detection model is trained according to the following steps: obtaining the target code and the historical defect information of the target code; determining the control dependency relationship between each code statement in the target code and the data dependency relationship between each variable; constructing a program dependency graph according to the control dependency relationship between each code statement and the data dependency relationship between each variable; the nodes included in the program dependency graph are code statements or variables; the edges between the nodes included in the program dependency graph are the control dependency relationship between code statements or the data dependency relationship between variables; determining the attribute characteristics of each node according to the program dependency graph; generating an adjacency matrix and a degree matrix corresponding to the target code according to the attribute characteristics of each node; the adjacency matrix is used to represent the connectivity relationship between each node; The degree matrix is used to represent the number of nodes connected to each node; Normalizing the adjacency matrix and the degree matrix to obtain a Laplace matrix, and calculating the eigenvalues and eigenvectors of the Laplace matrix; generating an eigenvector matrix based on the eigenvectors corresponding to the first preset number of eigenvalues of the Laplace matrix; Clustering the row vectors of the feature vector matrix based on an unsupervised learning algorithm to obtain a plurality of node clusters; segmenting the target code according to the node clusters to obtain individual code blocks; For each code block, taking the center point of the node cluster corresponding to the code block as the key node, bidirectional slicing is performed on the code block to obtain a code fragment, and the attribute characteristics of the nodes contained in the code fragment are determined; Based on the historical defect information and the attribute characteristics of the nodes contained in the code snippet, the defect detection model to be trained is adjusted until a trained defect detection model is obtained.
7. A computer device, characterized in that: include: A processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the computer device is running, the processor and the memory communicate via the bus, and when the machine-readable instructions are executed by the processor, the steps of the code defect detection method according to any one of claims 1 to 5 are performed.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the code defect detection method according to any one of claims 1 to 5 are executed.
Citation Information
Patent Citations
Software defect prediction method and system
CN112597038A
Code defect detection method
CN114064487A