A code defect detection method

By preset slicing criteria and program dependency graph traversal, combined with representation learning of statement coding network and program coding network, the shortcomings in accuracy and efficiency of existing code defect detection methods are solved, and higher code defect detection accuracy is achieved. accuracy and coverage, reducing the rate of false positives and false negatives.

CN114064487BActive Publication Date: 2024-09-24BEIJING JINGHANG COMPUTING & COMM RES INST
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111369107.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-11-18
Publication Date
2024-09-24
Estimated Expiration
2041-11-18

AI Technical Summary

Technical Problem

It is difficult for existing code defect detection methods to take into account high detection capabilities and high efficiency, and cannot meet the needs of shortening the software maintenance cycle and improving software quality. Methods based on lexical and syntax rules rely on manual rules and have high false positive and false negative rates, while machine-based methods The learning method relies on feature extraction and database quality, and there is still room for improvement in detection accuracy and false positives and false negatives.

Method used

The code is sliced ​​using the preset slicing criterion. Through segmented learning of statement semantic vectors and statement type vectors, a preset code defect detection model is constructed, and the sliced ​​code segments are generated by combining forward and backward traversal of the program dependency graph. It also uses the sentence coding network and the program coding network for representation learning, and integrates the dual attention mechanism of tokens and sentences to improve the accuracy of code defect detection.

Benefits of technology

It improves the accuracy of code defect detection, reduces the rate of false positives and false negatives, enhances the coverage of code defects, reduces the complexity of model training, improves detection efficiency, and can be located at the function level and slice level. Assist developers to quickly fix defects.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114064487B
    Figure CN114064487B
Patent Text Reader

Abstract

The present invention discloses a code defect detection method, which belongs to the field of software testing technology and can improve the accuracy of code defect detection. The method comprises: slicing the code to be identified according to a preset slicing criterion to obtain a slice code segment to be detected; inputting the slice code segment to be detected into a preset code defect detection model, and using the output result of the preset code defect detection model as the code defect detection result; wherein the preset code defect detection model is a network model obtained by segmentally learning the statement semantic vector and the statement type vector in each slice code segment in the training phase.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of software testing, and in particular to a code defect detection method. Background Art

[0002] Code analysis and software testing are the main means to check and discover security defects and exploits in software code, and have always been a hot topic in the field of information security and software security. However, with the increasing complexity and scale of security-critical and sensitive systems, as well as the frequent occurrence of software defects and the continuous increase in hacker attack methods, the difficulty of software defect detection is also increasing. Traditional code defect detection methods are difficult to balance high detection capabilities and detection efficiency, and cannot meet the needs of shortening software maintenance cycles and improving software quality and reliability.

[0003] Existing code defect detection methods mainly include those based on lexical and grammatical rules and those based on machine learning. In rule-based defect detection methods, experts write various defect rules according to the types of defects, and perform data flow analysis, taint analysis, etc. on the basis of lexical and grammatical parsing and source code modeling. Common rule-based software defect detection tools include open source tools such as ITS4, RATS, Flawfinder, and some commercial tools. Machine learning-based defect detection methods learn the characteristics of different defect types based on the defect type database defined by experts, form a deep learning network, and then detect and distinguish defects in the code.

[0004] The generation of defect rules in defect detection methods based on lexical and grammatical rules is highly dependent on experts and is greatly influenced by subjective factors. This type of method has a high accuracy in defect detection based on established rules, but due to the limited scope of manually defined defect rules, it is difficult to fully cover all defect types, and the imperfection of the rules leads to a high false alarm rate and missed alarm rate.

[0005] Defect detection methods based on machine learning are highly dependent on the quality of defect databases, but with the improvement of code detection systems, the quantity and quality of databases have been improved, and it is a method with development potential. In addition, the detection level based on machine learning methods depends on the feature extraction method and the network structure of machine learning. Existing methods are generally based on networks such as CNN or LSTM, which are more effective than rule-based methods, but there is still room for improvement in the accuracy, false positives and false negatives of defect detection. Summary of the invention

[0006] In view of the above analysis, an embodiment of the present invention aims to provide a code defect detection method, which can improve the accuracy of code defect detection.

[0007] The present invention discloses a code defect detection method, comprising:

[0008] Slice the code to be identified according to the preset slicing criteria to obtain the slice code segment to be detected;

[0009] Input the slice code segment to be detected into a preset code defect detection model, and use the output result of the preset code defect detection model as the code defect detection result; wherein, the preset code defect detection model is a network model obtained by segmented learning of the statement semantic vectors and statement type vectors in each slice code segment in the training phase.

[0010] Furthermore, before the step of slicing the code to be identified according to the preset slicing criteria to obtain the sliced ​​code segment to be detected, the code defect detection method further includes:

[0011] Determine target defect candidate key nodes based on source code; the target defect candidate key nodes include defect candidate key nodes related to function parameters and defect candidate key nodes related to function return values;

[0012] Taking each target defect candidate key node as a starting point, the program dependency graph is traversed forward and backward respectively according to the dependency relationship in the program dependency graph to obtain the sliced ​​code segment.

[0013] Furthermore, taking each target defect candidate key node as a starting point, traversing the program dependency graph from forward and backward respectively according to the dependency relationship in the program dependency graph to obtain the slice code segment includes:

[0014] Merging a forward slicing statement node set obtained by forward traversing the program dependency graph and a backward slicing statement node set obtained by backward traversing the program dependency graph to obtain a slicing statement node set;

[0015] All slice statement nodes in the slice statement node set are sorted according to the dependency relationship and the order of statements in the program slice to obtain the slice code segment.

[0016] Furthermore, the step of traversing the program dependency graph forward to obtain a forward slicing statement node set includes:

[0017] Traverse the program dependency graph forward according to the dependency relationship to obtain a set of forward slicing statement nodes between nodes;

[0018] Obtain a function called by a function call statement in a slice, and take a node of a type corresponding to a function parameter in the function as a starting point, traverse the program dependency graph forward, and obtain a set of inter-procedural forward slice statement nodes;

[0019] The inter-node forward slicing statement node set and the inter-process forward slicing statement node set are merged to obtain the forward slicing statement node set.

[0020] Furthermore, the step of traversing the program dependency graph backward to obtain a set of backward slicing statement nodes includes:

[0021] Traverse the program dependency graph backward according to the dependency relationship to obtain a set of backward slicing statement nodes between nodes;

[0022] Obtain a function called by a function call statement in a slice, and take a node of a type corresponding to a return value of the function in the function as a starting point, traverse the program dependency graph backward, and obtain a set of inter-procedural backward slice statement nodes;

[0023] The inter-node backward slicing statement node set and the inter-process backward slicing statement node set are merged to obtain the backward slicing statement node set.

[0024] Furthermore, the code defect detection method further includes:

[0025] After the sentence semantic vector and the sentence type vector have completed segmented learning, the sentence semantic vector and the sentence type vector at this time are concatenated and mapped to the same feature space to obtain the sentence vector representation of each slice code segment.

[0026] Furthermore, the segmented learning of the statement semantic vectors and statement type vectors in each slice code segment in the training phase includes:

[0027] Split the statements in each slice code segment into tokens, use the pre-trained word2vec model to get the vector representation of each token, and get the token vector matrix;

[0028] Input the token vector matrix into self-attention to obtain the initialized sentence semantic vector matrix;

[0029] Performing representation learning on the initialized sentence semantic vector matrix through a sentence encoding network to obtain the sentence semantic vector;

[0030] The type information of the statements in each slice code segment is one-hot encoded to obtain the statement type vector.

[0031] Furthermore, after the step of obtaining the sentence vector representation of each slice code segment, the code defect detection method further includes:

[0032] The sentence vector representation is input into a program coding network, and representation learning is performed on each slice code segment through the program coding network to obtain vector representations of all slice code segments.

[0033] Furthermore, the representation learning of each slice code segment through the program coding network to obtain the vector representation of all slice code segments includes:

[0034] Padding the sentence vector representation to obtain an initializer vector matrix including the sentence vector;

[0035] Performing representation learning on the initialized program vector matrix through the program encoding network to obtain a hidden vector of the program statement;

[0036] Randomly initialize a vector with the same dimension as the hidden vector of the program statement, and transform the hidden vector of the program statement into a learnable query vector through a linear layer;

[0037] The query vector is used to calculate the attention score of each statement in each slice code segment, and the attention scores of the statements in the same slice code segment are normalized using softmax to obtain the final attention value of the statement;

[0038] For each slice code segment, the final attention value of the statement is used to perform weighted summation on the hidden vectors of the program statement of the statement to obtain the vector representation of all the slice code segments.

[0039] Furthermore, the preset slicing criteria are rules for slicing the code to be identified according to any one or more defect key nodes; wherein the defect key nodes include:

[0040] Defect key nodes related to API / library function calls, defect key nodes related to pointer usage, defect key nodes related to array usage, defect key nodes related to arithmetic expressions, defect key nodes related to function parameters, and defect key nodes related to function return values.

[0041] Compared with the prior art, the present invention can achieve at least one of the following beneficial effects:

[0042] The code defect detection method provided in the embodiment of the present invention slices the code to be identified according to the preset slicing criteria to obtain the sliced ​​code segment to be detected; the sliced ​​code segment to be detected is input into the preset code defect detection model, and the output result of the preset code defect detection model is used as the code defect detection result. The accuracy of code defect detection can be improved.

[0043] Two new slicing criteria related to data transfer between functions are provided. Compared with the original four types of slicing criteria, the introduction of new slicing criteria and inter-procedural analysis can generate richer defect candidate key points, further improve the coverage of code defect statements by slicing code segments, and help reduce the underreporting of cross-function defects by the detection model.

[0044] Compared with training the code defect detection model with source code, training the code defect detection model by slicing code segments can avoid the adverse impact of a large number of defect-irrelevant statements in the source code on model training, which helps to improve the accuracy of the code defect detection model. At the same time, it also reduces the complexity of the learning model and helps to improve the efficiency of model training.

[0045] The representation learning of sliced ​​code segments is carried out by using segmented representation learning combined with a dual attention mechanism based on tokens and statements. The sliced ​​code segments are modeled using a statement encoding network and a program encoding network. Compared with traditional deep neural networks, the segmented encoding network with a dual attention mechanism based on tokens and statements can fuse local and global feature information, effectively learn the importance of different tokens and different statements to defects, and more accurately learn the vector representation of sliced ​​code segments, which helps to improve the accuracy of the code defect detection model.

[0046] It is possible to obtain function-level code defect identification results by fusing the defect identification results of slice code segments, thereby locating defects at both the function level and the slice level, which helps assist developers to understand and quickly fix defects.

[0047] In the present invention, the above-mentioned technical solutions can also be combined with each other to achieve more preferred combination solutions. Other features and advantages of the present invention will be described in the subsequent description, and some advantages can become obvious from the description, or can be understood by practicing the present invention. The purpose and other advantages of the present invention can be realized and obtained through the contents particularly pointed out in the description and the drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] The drawings are only for the purpose of illustrating particular embodiments and are not to be considered limiting of the present invention. Like reference symbols denote like components throughout the drawings.

[0049] Figure 1 is a flow chart of a code defect detection method in an embodiment of the present invention;

[0050] Figure 2 is a flow chart of a code defect detection method in another embodiment of the present invention;

[0051] Figure 3 This is a schematic diagram of the process of generating a slice code segment in an embodiment of the present invention;

[0052] Figure 4 The figure is a schematic diagram of the flow of generating sentence vectors in an embodiment of the present invention. DETAILED DESCRIPTION

[0053] The preferred embodiments of the present invention will be described in detail below in conjunction with the accompanying drawings, wherein the accompanying drawings constitute a part of this application and are used together with the embodiments of the present invention to illustrate the principles of the present invention, but are not used to limit the scope of the present invention.

[0054] A specific embodiment of the present invention discloses a code defect detection method, the flow chart is as follows: Figure 1 As shown, the following steps are included:

[0055] Step S1: Slice the code to be identified according to a preset slicing criterion to obtain a slice code segment to be detected.

[0056] Step S2: Input the slice code segment to be detected into a preset code defect detection model, and use the output result of the preset code defect detection model as the code defect detection result; wherein, the preset code defect detection model is a network model obtained by segmented learning of the statement semantic vectors and statement type vectors in each slice code segment in the training phase.

[0057] It should be noted that the “defects” in the embodiments of the present invention correspond to the “holes” in the accompanying drawings.

[0058] In step S1, further, the preset slicing criterion is a rule for slicing the code to be identified according to any one or more defective key nodes.

[0059] The key nodes of the above defects include:

[0060] Defect key nodes related to API / library function calls, defect key nodes related to pointer usage, defect key nodes related to array usage, defect key nodes related to arithmetic expressions, defect key nodes related to function parameters (Function Parameter, FP) and defect key nodes related to function return values ​​(Function Return statement, FR); the latter two defect key nodes are newly added defect key nodes in the embodiments of the present invention.

[0061] For the code to be identified, all possible code segments to be sliced ​​can be generated according to the six slicing criteria.

[0062] Furthermore, before step S1, the code defect detection method further includes:

[0063] Determine target defect candidate key nodes based on the source code; the target defect candidate key nodes include defect candidate key nodes related to function parameters and defect candidate key nodes related to function return values; it should be noted that the defect candidate key nodes related to function parameters and the defect candidate key nodes related to function return values ​​in this step correspond to the preset code defect detection model in the training stage.

[0064] The above-mentioned defect key nodes related to function parameters and the defect key nodes related to function return values ​​correspond to the preset code defect detection model in the detection stage.

[0065] like Figure 2 As shown, before the step of determining the candidate key nodes of the target defect based on the source code, the following two steps are also included:

[0066] Step 1: Use static analysis tools to parse the source code and generate code structure information represented by an abstract syntax tree and a program dependency graph; a code attribute graph that represents node attribute information by code statement content, node type, and node degree.

[0067] Among them, the abstract syntax tree is as follows Figure 3 The AST and program dependency graph in Figure 3 In the PDG, the node attribute information is as follows Figure 3 Medium property.

[0068] Step 2: Extract data dependencies, control dependencies, and function dependencies between statement nodes according to the edge types in the code attribute graph, and generate a program dependency graph.

[0069] Determine the defect candidate key nodes related to function parameters, including:

[0070] Traverse the code attribute graph generated in step 1, find the node of type "Function", continue to traverse the edge starting from this node, and obtain the terminal node of this edge. The node of type "Parameter" among these terminal nodes is the defect candidate key node related to the function parameter.

[0071] Identify the key nodes of defect candidates related to function return values, including:

[0072] Traverse the code attribute graph generated in step 1 and search for nodes of type "returnstatement", which are candidate defect key nodes related to the function return value.

[0073] Taking each target defect candidate key node as a starting point, the program dependency graph is traversed forward and backward respectively according to the dependency relationship in the program dependency graph to obtain the sliced ​​code segment.

[0074] Furthermore, taking each target defect candidate key node as a starting point, traversing the program dependency graph from forward and backward respectively according to the dependency relationship in the program dependency graph to obtain the slice code segment includes:

[0075] Merging a forward slicing statement node set obtained by forward traversing the program dependency graph and a backward slicing statement node set obtained by backward traversing the program dependency graph to obtain a slicing statement node set;

[0076] All the slice statement nodes in the slice statement node set are sorted according to the dependency relationship and the order of statements in the program slice to obtain the slice code segment. The dependency relationship may include control dependency, data dependency, function call dependency, etc. Figure 3 As shown, the statement order within a program slice can be obtained by extracting property information.

[0077] Furthermore, the step of traversing the program dependency graph forward to obtain a forward slicing statement node set includes:

[0078] The program dependency graph is traversed forward according to the dependency relationship to obtain a forward slicing statement node set between nodes; taking each defect candidate key node as a starting point, the program dependency graph is traversed forward according to the dependency relationship to obtain a forward slicing statement node set, where the defect candidate key node includes each defect candidate key node among the defect candidate key nodes related to the function parameters.

[0079] Get the function called by the function call statement in the slice, take the node of the type corresponding to the function parameter in the function as the starting point, traverse the program dependency graph forward, and obtain the inter-procedural forward slicing statement node set; take the node of type "Parameter" in the function as the starting point, traverse the program dependency graph forward, and obtain the inter-procedural forward slicing statement node set.

[0080] The inter-node forward slicing statement node set and the inter-process forward slicing statement node set are merged to obtain the forward slicing statement node set.

[0081] Furthermore, the step of traversing the program dependency graph backward to obtain a set of backward slicing statement nodes includes:

[0082] The program dependency graph is traversed backward according to the dependency relationship to obtain a set of backward slicing statement nodes between nodes; taking each defect candidate key node as a starting point, the program dependency graph is traversed backward according to the dependency relationship to obtain a set of backward slicing statement nodes, where the defect candidate key nodes include each defect candidate key node among the defect candidate key nodes related to the function return value.

[0083] Get the function called by the function call statement in the slice, take the node of the type corresponding to the return value of the function in the function as the starting point, traverse the program dependency graph backward, and obtain the set of backward slicing statement nodes between procedures; take the node of the type "returnstatement" in the function as the starting point, traverse the program dependency graph backward, and obtain the set of backward slicing statement nodes between procedures.

[0084] The inter-node backward slicing statement node set and the inter-process backward slicing statement node set are merged to obtain the backward slicing statement node set.

[0085] In step S2, if Figure 2 As shown, the preset code defect detection model corresponds to the trained defect detection model. Before the detection phase, the sentence encoding network (SENet), program encoding network (PENet) and detector network (DNet) in the model need to continuously adjust the network parameters until convergence.

[0086] The sentence semantic vector and sentence type vector can be obtained by processing the sentence, such as Figure 2 As shown, the step of segmentally learning the sentence semantic vector and sentence type vector is performed in the sentence encoding network.

[0087] like Figure 4 As shown in the figure, the sentence semantic vector is learned in segments through self-attention, and the sentence type vector is learned in segments through One-Hot encoding.

[0088] Further, the step includes:

[0089] Split the statements in each slice code segment into tokens, use the pre-trained word2vec model to get the vector representation of each token, and get the token vector matrix; in lexical analysis, token means label.

[0090] Word2vec is a group of related models used to generate word vectors. These models are two-layer neural networks that are trained to reconstruct linguistic word texts.

[0091] Input the token vector matrix into self-attention to obtain the initialization sentence semantic vector matrix; refer to Figure 2 , self-attention is located in the sentence encoding network during the training phase.

[0092] The sentence semantic vector matrix of the initialized sentence is represented and learned by a sentence encoding network to obtain the sentence semantic vector; referring to Figure 2 , the sentence encoding network can be selected as CNN and RNN, etc. Figure 4 As shown, x i se is the sentence semantic vector.

[0093] The type information of the statements in each slice code segment is one-hot encoded to obtain the statement type vector. One-hot encoding is also called one-bit effective encoding. Figure 4 As shown, x i type is a statement type vector.

[0094] Furthermore, the code defect detection method further includes:

[0095] After the sentence semantic vector and the sentence type vector have completed segment learning, the sentence semantic vector and the sentence type vector are concatenated and mapped to the same feature space to obtain the sentence vector representation of each slice code segment. Figure 4 As shown, x i se and x i type Perform concatenation, input the linear layer, and output the sentence feature vector through the linear layer.

[0096] The embodiment of the present invention splices the statement semantic vector and the statement type vector, so that the preset code defect detection model can perform code defect detection based on the statement semantics and the statement type, and the statement semantics and the statement type are learned separately, so that the output result of the preset code defect detection model is more accurate. The method of splicing vectors is a mature technology in the field and will not be repeated here.

[0097] The MLP can be used to map them to the same feature space to obtain the sentence vector representation of each slice code segment, such as Figure 4 The sentence feature vector in . A typical multi-layer perceptron MLP consists of three layers: input layer, hidden layer and output layer. The different layers of the MLP neural network are fully connected. The method of mapping to the feature space is also a mature technology in this field and will not be repeated here.

[0098] Furthermore, after the step of obtaining the sentence vector representation of each slice code segment, the code defect detection method further includes:

[0099] The sentence vector representation is input to the program coding network, and the program coding network is used to perform representation learning on each slice code segment to obtain the vector representation of all slice code segments. The program coding network can be selected as BiGRU, which can be divided into three parts: text vectorization input layer, hidden layer and output layer. Among them, the hidden layer consists of three layers: BiGRU layer, attention layer and Dense layer (fully connected layer).

[0100] Furthermore, the representation learning of each slice code segment through the program coding network to obtain the vector representation of all slice code segments includes:

[0101] Padding is performed on the sentence vector representation to obtain an initializer vector matrix including the sentence vector; padding is to increase the number of pixels on each side in order to keep the feature map not too small, but it is not necessary to exceed the size of the original image.

[0102] The program encoding network is used to perform representation learning on the initialized program vector matrix to obtain a hidden vector of the program statement.

[0103] A vector with the same dimension as the hidden vector of the program statement is randomly initialized, and the hidden vector of the program statement is converted into a learnable query vector through a linear layer; the number of linear layers can be one.

[0104] The query vector is used to calculate the attention score of each statement in each slice code segment, and the attention scores of the statements in the same slice code segment are normalized using softmax to obtain the final attention value of the statement; Figure 2 As shown, according to the text attention mechanism (corresponding to Figure 2 The query vector is used to calculate the attention score of each statement in each slice code segment.

[0105] For each slice code segment, the final attention value of the statement is used to perform weighted summation on the hidden vectors of the program statement of the statement to obtain the vector representation of all the slice code segments.

[0106] Further, refer to Figure 2 After this step, the vector representation of all the slice code segments can be sent to the fully connected layer and softmax layer in the detector network to obtain the forward calculation result, and the cross entropy loss function is calculated using the category label information of the slice code segment. According to the error between the forward calculation result and the cross entropy loss function calculation result, the network parameters are adjusted by back propagation until the sentence encoding network, program encoding network and detector network converge. This step is an existing mature technology.

[0107] Compared with the prior art, the code defect detection method provided in the embodiment of the present invention slices the code to be identified according to the preset slicing criteria to obtain the sliced ​​code segment to be detected; inputs the sliced ​​code segment to be detected into the preset code defect detection model, and uses the output result of the preset code defect detection model as the code defect detection result, which can improve the accuracy of code defect detection.

[0108] The embodiment of the present invention introduces a new slicing criterion and generates slicing code segments to more comprehensively cover defect candidate key points and more accurately extract slicing statement information, attribute information and code context information that are dependent on the defect candidate key points.

[0109] In addition, the sentence encoding network and the program encoding network are used, and the dual attention mechanism based on tokens and sentences is combined to perform segmented representation learning on the sliced ​​code segments to learn more comprehensive and accurate defect patterns.

[0110] Those skilled in the art will appreciate that all or part of the processes of the above-mentioned embodiments can be implemented by instructing related hardware through a computer program, and the program can be stored in a computer-readable storage medium, wherein the computer-readable storage medium is a disk, an optical disk, a read-only storage memory, or a random access memory, etc.

[0111] The above description is only a preferred specific implementation manner of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily conceived by any technician familiar with the technical field within the technical scope disclosed by the present invention should be covered within the protection scope of the present invention.

Claims

1. A code defect detection method, characterized in that: include: Slice the code to be identified according to the preset slicing criteria to obtain the slice code segment to be detected; Constructing a preset code defect detection model, wherein the preset code defect detection model includes: a statement encoding network, a program encoding network, and a detector network; Inputting the slice code segment to be detected into the preset code defect detection model, and using the output result of the preset code defect detection model as the code defect detection result, includes: S1: Split the statements in the code segment to be detected into tokens, obtain a token vector matrix based on the word2vec model, input the token vector matrix into the sentence encoding network to obtain a sentence semantic vector, and the sentence encoding network includes a self-attention mechanism; S2: One-hot encode the type information of the statements in the code segment to be detected to obtain a statement type vector; S3: concatenate the sentence semantic vector and the sentence type vector to obtain a first vector representation; S4: input the first vector representation to a program encoding network BiGRU to obtain a second vector representation, wherein a hidden layer of the program encoding network BiGRU is composed of a BiGRU layer, an attention layer, and a Dense layer; S5: Input the second vector representation into the detector network to obtain the code defect detection result corresponding to the slice code segment to be detected.

2. The code defect detection method according to claim 1, characterized in that: Before the step of slicing the code to be identified according to the preset slicing criteria to obtain the sliced ​​code segment to be detected, the code defect detection method further includes: Determine target defect candidate key nodes based on source code; the target defect candidate key nodes include defect candidate key nodes related to function parameters and defect candidate key nodes related to function return values; Taking each target defect candidate key node as a starting point, the program dependency graph is traversed forward and backward respectively according to the dependency relationship in the program dependency graph to obtain the sliced ​​code segment.

3. The code defect detection method according to claim 2, characterized in that: The method of taking each target defect candidate key node as a starting point and traversing the program dependency graph from forward and backward respectively according to the dependency relationship in the program dependency graph to obtain the slice code segment includes: Merging a forward slicing statement node set obtained by forward traversing the program dependency graph and a backward slicing statement node set obtained by backward traversing the program dependency graph to obtain a slicing statement node set; All slice statement nodes in the slice statement node set are sorted according to the dependency relationship and the order of statements in the program slice to obtain the slice code segment.

4. The code defect detection method according to claim 3, characterized in that: The step of traversing the program dependency graph forward to obtain a forward slicing statement node set includes: Traverse the program dependency graph forward according to the dependency relationship to obtain a set of forward slicing statement nodes between nodes; Obtain a function called by a function call statement in a slice, and take a node of a type corresponding to a function parameter in the function as a starting point, traverse the program dependency graph forward, and obtain a set of inter-procedural forward slice statement nodes; The inter-node forward slicing statement node set and the inter-process forward slicing statement node set are merged to obtain the forward slicing statement node set.

5. The code defect detection method according to claim 3, characterized in that: The step of traversing the program dependency graph backward to obtain a set of backward slicing statement nodes includes: Traverse the program dependency graph backward according to the dependency relationship to obtain a set of backward slicing statement nodes between nodes; Obtain a function called by a function call statement in a slice, and take a node of a type corresponding to a return value of the function in the function as a starting point, traverse the program dependency graph backward, and obtain a set of inter-procedural backward slice statement nodes; The inter-node backward slicing statement node set and the inter-process backward slicing statement node set are merged to obtain the backward slicing statement node set.

6. The code defect detection method according to claim 5, characterized in that: The step of inputting the token vector matrix into the sentence encoding network to obtain a sentence semantic vector includes: Input the token vector matrix into self-attention to obtain the initialized sentence semantic vector matrix; The initialized sentence semantic vector matrix is ​​represented and learned through a sentence encoding network to obtain the sentence semantic vector.

7. The code defect detection method according to claim 6, characterized in that: The inputting the first vector representation to the program encoding network BiGRU to obtain a second vector representation includes: Padding the first vector representation to obtain an initializer vector matrix including the first vector; Performing representation learning on the initialized program vector matrix through the program encoding network BiGRU to obtain a hidden vector of the program statement; Randomly initialize a vector with the same dimension as the hidden vector of the program statement, and transform the hidden vector of the program statement into a learnable query vector through a linear layer; The query vector is used to calculate the attention score of each statement in the slice code segment, and the attention scores of the statements in the same slice code segment are normalized using softmax to obtain the final attention value of the statement; For the sliced ​​code segment, the final attention value of the statement is used to perform weighted summation on the hidden vectors of the program statement of the statement to obtain a second vector representation of the sliced ​​code segment.

8. The code defect detection method according to claim 1, characterized in that: The preset slicing criteria are rules for slicing the code to be identified according to any one or more defect key nodes; wherein the defect key nodes include: Defect key nodes related to API / library function calls, defect key nodes related to pointer usage, defect key nodes related to array usage, defect key nodes related to arithmetic expressions, defect key nodes related to function parameters, and defect key nodes related to function return values.