A code similarity detection method, device and medium based on graph neural network

Through the code similarity detection method based on graph neural network, the control flow graph and graph editing distance calculation model is used to solve the problems of high computational complexity and lack of interpretability in the existing technology, and the efficient detection of code similarity is achieved and interpretability analysis.

CN118916714BActive Publication Date: 2024-12-10CENT SOUTH UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411396576.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-10-09
Publication Date
2024-12-10
Estimated Expiration
2044-10-09

AI Technical Summary

Technical Problem

The existing code similarity detection method has high computational complexity and lacks interpretability, making it difficult to generalize to a large number of code snippet application scenarios, and the 'black box' model of deep learning methods cannot explain the specific source of similarity, limiting its practical application. Availability.

Method used

Using the code similarity detection method based on graph neural network, the model and model loss function are calculated by controlling the flow graph conversion module, the graph editing distance is calculated and the graph editing distance between the control flow graphs is generated to ensure that the model is accessible. Explanatory.

Benefits of technology

It reduces the computational complexity, improves the interpretability of similarity calculation results, can better meet the needs in practical applications, and provides specific graph editing paths to explain the similarity source.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118916714B_ABST
    Figure CN118916714B_ABST
Patent Text Reader

Abstract

The present invention relates to the technical field of code similarity detection, and in particular to a code similarity detection method, device and medium based on graph neural network, the method comprising the following steps: calling a control flow graph conversion module, converting a code to be detected into a control flow graph, encoding the control flow graph for topological structure, and obtaining a node feature vector; constructing a graph edit distance calculation model; using a model loss function to train the graph edit distance calculation model, calling the trained graph edit distance calculation model to simultaneously predict the graph edit distance and node matching confidence matrix between two control flow graphs, wherein the graph edit distance is used as a quantitative indicator of code similarity, and the node matching confidence matrix can generate a graph edit path. The present invention uses a control flow graph to capture the logic and semantics of the code, and uses a deep learning model based on a graph neural network to analyze the control flow graph, thereby achieving the purpose of detecting code similarity.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code similarity detection, and in particular to a code similarity detection method, device and medium based on graph neural network. Background Art

[0002] Code similarity detection is a basic activity in the field of software engineering. It refers to the process of evaluating the similarity between different code snippets by analyzing the characteristics of the source code. It has wide applications in scenarios such as software plagiarism detection, locating duplicate code snippets, code search, and code refactoring.

[0003] The text-based code similarity detection method is simple to implement and has a fast calculation speed. However, it lacks understanding of the code structure and semantics and is easily affected by details such as variable names and formats. In the prior art, the research on code similarity detection is mainly based on lexical and grammatical analysis. Although the existing methods capture the structure and semantic information of the code, they still have the following limitations:

[0004] (1) High computational complexity: The time complexity of using traditional algorithms (such as graph matching algorithms) to analyze abstract syntax trees, control flow graphs, etc. is extremely high, and in practice it is difficult to extend to application scenarios with a large number of code snippets.

[0005] (2) Lack of explainability: Current methods combined with deep learning usually treat the calculation of code similarity as a pure regression task, only outputting similarity scores without explaining the specific source of similarity, especially at the structural level of control flow graphs or abstract syntax trees. This "black box" model lacks a clear explanation of the internal structure matching of the code, making the similarity calculation results difficult to understand, limiting its usability in practical applications.

[0006] In summary, there is an urgent need for a code similarity detection method, device and medium based on graph neural network, which can simultaneously reduce the computational complexity and improve the interpretability of the similarity calculation results, so as to better meet the needs of practical applications. Summary of the invention

[0007] The purpose of the present invention is to provide a code similarity detection method, device and medium based on graph neural network. The specific technical solution is as follows:

[0008] A code similarity detection method based on graph neural network includes the following steps:

[0009] S1: calling a control flow graph conversion module, converting the code to be detected into a control flow graph, performing topological structure encoding on the control flow graph, and obtaining a node feature vector;

[0010] S2: construct a graph edit distance calculation model, wherein the graph edit distance calculation model includes a cross-graph feature aggregation module, a node matching module, an attention pooling module, a neural tensor network, and a graph edit distance prediction module.

[0011] The cross-graph feature aggregation module is used to iteratively update the node feature vector to obtain the node embedding.

[0012] The node matching module is used to process node embedding and output matching information of nodes between graph pairs.

[0013] The attention pooling is used to process node embeddings and output graph embeddings.

[0014] The neural tensor network is used to process graph embedding and output graph embedding interaction features.

[0015] The graph edit distance prediction module is used to calculate node matching information and graph embedding interaction features to obtain the graph edit distance;

[0016] S3: Use the model loss function to train the graph edit distance calculation model, and call the trained graph edit distance calculation model to simultaneously predict the graph edit distance and node matching confidence matrix between two control flow graphs. The graph edit distance is used to judge the similarity between the codes to be detected, and the node matching confidence matrix can be used to generate the graph edit path.

[0017] Preferably, the code to be detected is converted into a control flow graph, and the control flow graph is topologically encoded to obtain a node feature vector, specifically:

[0018] Lexical and syntactic analysis is performed on the two sections of code to be tested respectively, the code to be tested is converted into a control flow graph with structural and semantic information, the basic blocks in the control flow graph are encoded to obtain node feature information and edge feature information, and the node feature information and edge feature information are converted into feature vectors in the deep learning model.

[0019] Preferably, the cross-graph feature aggregation module includes The cross-graph feature aggregation layer utilizes the message passing mechanism of the graph neural network to aggregate the features of neighbor nodes and corresponding edges, and captures the information on another control flow graph through the attention mechanism, thereby generating node embeddings with fine-grained cross-graph interaction features.

[0020] Preferably, the node matching module includes two parallel node matching workflows and a multi-layer perceptron.

[0021] Two parallel node matching workflows predict matching results based on the obtained node embeddings. The node matching workflows include Nodes match submodules;

[0022] The multi-layer perceptron is used to summarize the matching results at different scales and obtain the final node matching information.

[0023] Preferably, the node matching information includes node matching confidence and node matching cost.

[0024] Preferably, in the graph edit distance prediction module, the graph edit distance is predicted using a weighted sum of node matching costs and a bias value, and the bias value is calculated by a neural tensor network of graph embeddings between connection graph pairs.

[0025] Preferably, the model loss function includes matching loss, graph edit distance value loss and loss hyperparameter, and the loss hyperparameter is used to balance the matching loss and graph edit distance value loss.

[0026] Preferably, during model training,

[0027] The matching loss is calculated by taking the minimum log likelihood of the node matching confidence matrix and the matching of multiple optimal nodes as the matching loss;

[0028] The graph edit distance value loss is calculated by converting the graph edit distance value into a similarity score, and using the mean square error of the similarity score to represent the graph edit distance value loss.

[0029] In addition, the present invention also includes a computer device, including a memory and a processor;

[0030] The memory is used to store a computer program executable on the processor;

[0031] The processor is used to implement the steps of the code similarity detection method as described above when executing the computer program.

[0032] In addition, the present invention also includes 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 code similarity detection method described above are implemented.

[0033] The application of the technical solution of the present invention has the following beneficial effects:

[0034] The present invention uses a control flow graph to capture the logic and semantics of the code, and uses a deep learning model based on a graph neural network to analyze the control flow graph, thereby achieving the purpose of detecting code similarity. Compared with the existing deep learning code similarity detection method, the model in the present invention can not only calculate the graph edit distance between control flow graphs as a code similarity metric, but also generate a specific graph edit path, thereby ensuring the interpretability of the model.

[0035] In addition to the above-described objects, features and advantages, the present invention has other objects, features and advantages. The present invention will be further described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0036] In order to more clearly illustrate the embodiments of the present invention or the technical solutions of the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0037] Figure 1 is a flowchart of the steps of the code similarity detection method in a preferred embodiment of the present invention;

[0038] Figure 2 It is a model framework diagram of a map edit distance calculation model in a preferred embodiment of the present invention;

[0039] Figure 3 It is a schematic diagram of the process of generating a graph editing path based on node matching in a preferred embodiment of the present invention. DETAILED DESCRIPTION

[0040] The content of the present invention is to provide a code similarity detection method, device and medium based on graph neural network. The method calculates the graph editing distance between control flow graphs as a quantitative indicator of code similarity, and can generate graph editing paths between control flow graphs, thereby solving the problem of lack of interpretability of existing code similarity detection methods based on deep learning.

[0041] In order to enable those skilled in the art to better understand the scheme of the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings and specific implementation methods. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0042] Glossary:

[0043] Graph Editing Path: Given two graphs and ,Will Convert to a Atomic operation sequences of isomorphic graphs called and Specifically, there are six atomic editing operations on the graph: inserting or deleting a label Insert or delete the label between two nodes ; Change the label of a vertex or edge from Replace with and .

[0044] Graph Edit Distance: Two Graphs and The graph edit distance between Defined as and The minimum cost of all graph editing paths between is as follows:

[0045] ;

[0046] in, represents the cost function, Measuring Editing Actions The cost of Indicates that Convert to A collection of edit paths.

[0047] Node Matching: Two Graphs and The node matching between , which satisfies and Between single injection and . (Right now The node Mapping to Node ) if and only if .

[0048] like Figure 1 As shown, this embodiment discloses a code similarity detection method based on graph neural network, comprising the following steps:

[0049] S1: calling a control flow graph conversion module, converting the code to be detected into a control flow graph, performing topological structure encoding on the control flow graph, and obtaining a node feature vector.

[0050] S2: construct a graph edit distance calculation model, which includes a cross-graph feature aggregation module, a node matching module, an attention pooling module, a neural tensor network (NTN), and a graph edit distance prediction module.

[0051] The cross-graph feature aggregation module is used to iteratively update the node feature vector to obtain the node embedding.

[0052] The node matching module is used to process node embedding and output matching information of nodes between graph pairs.

[0053] The attention pooling is used to process node embeddings and output graph embeddings.

[0054] The neural tensor network is used to process graph embedding and output graph embedding interaction features.

[0055] The graph edit distance prediction module is used to calculate node matching information and graph embedding interaction features to obtain the graph edit distance.

[0056] S3: Use the model loss function to train the graph edit distance calculation model, and call the trained graph edit distance calculation model to simultaneously predict the graph edit distance and node matching confidence matrix between two control flow graphs. The graph edit distance is used to judge the similarity between the codes to be detected, and the node matching confidence matrix can be used to generate the graph edit path.

[0057] This embodiment performs lexical and syntactic analysis on the code to be detected, converts it into a control flow graph containing structural and semantic information, and then performs initial encoding on the obtained control flow graph, stores the feature information of the nodes / edges and the topological structure of the graph in the form of tensors that can be understood and processed by the deep learning model, to facilitate subsequent model training.

[0058] In this embodiment, preferably, the code to be detected is converted into a control flow graph, and the control flow graph is topologically encoded to obtain a node feature vector, specifically:

[0059] Lexical and syntactic analysis is performed on the two sections of code to be tested respectively, the code to be tested is converted into a control flow graph with structural and semantic information, the basic blocks in the control flow graph are encoded to obtain node feature information and edge feature information, and the node feature information and edge feature information are converted into feature vectors in the deep learning model.

[0060] It should be noted that the control flow graph is a directed graph containing node labels or edge labels. Formally, such a graph can be represented by a 6-tuple To indicate that and denote the node set, edge set, node label set and edge label set respectively. and There are two label functions, one from the label set and The labels of each node or edge in the control flow graph can be initialized as a vector, and the topological structure of the control flow graph can be represented by an adjacency matrix. The above preprocessing provides a basis for the subsequent use of the graph neural network model to analyze code similarity. In this embodiment, the control flow graph obtained after the syntax and lexical analysis of the two code segments to be tested is represented as and .

[0061] Preferably, the overall architecture of the graph edit distance calculation model (CFGSim model, full name Control Flow Graph Similarity) in this embodiment is as follows: Figure 2 As shown in the figure, the model calculates the graph edit distance between control flow graphs and outputs a node matching confidence matrix to generate the graph edit path to ensure the interpretability of the model. The model includes a cross-graph feature aggregation module, a node matching module, an attention pooling module, a neural tensor network, and a graph edit distance prediction module.

[0062] Specifically, the cross-graph feature aggregation includes The cross-graph feature aggregation layer (CGFA) is designed and constructed based on the graph neural network. CGFA not only aggregates the features of neighboring nodes and corresponding edges, but also captures the information on another control flow graph through the attention mechanism, thereby generating node embeddings with fine-grained cross-graph interaction features.

[0063] In a more specific embodiment, the cross-graph feature aggregation layer (CGFA) utilizes the topological structure of the control flow graph to aggregate the feature information of nodes, and adopts a two-stage design of intra-graph aggregation and cross-graph aggregation. Given the control flow graphs of two pieces of code CGFA first obtains and The node’s in-graph feature vector is then used to capture the node features on another control flow graph through the attention mechanism. The CGFA layer iterates Second-rate, is a controllable hyperparameter that generates node embeddings with fine-grained cross-graph interaction features and high-level information.

[0064] It should be noted that the in-graph aggregation works on a single control flow graph, which conforms to the design specifications of the graph neural network message passing. Its main purpose is to obtain the neighborhood features of the nodes in the control flow graph. When the aggregation operation of the graph neural network is performed, the feature vector of the neighbor node and the feature vector of the edge connecting the neighbor are concatenated as the final result of the node receiving the message passing. or , the aggregation layer in the graph updates the nodes by the following formula The eigenvector of:

[0065] ;

[0066] in, Representation Node In the The in-graph feature vector of the layer; Representation Node In the The cross-graph feature vector of the layer; the initial cross-graph feature vector of the node is set to one-hot encoding; Is a node The set of neighbors of ; Is a connection node and nodes The eigenvector of the edge; Respectively represent Combinations of layers and message passing functions, which can be any differentiable function that maps vectors to vectors. Finally, sum pooling is used to aggregate features from all neighbor nodes.

[0067] Furthermore, cross-graph aggregation uses an attention mechanism and weights the node features from another control flow graph according to the similarity of the features, as shown in the following formula (assuming ):

[0068] ;

[0069] in, Indicates Cross-graph aggregation functions of layers; Indicates that the graph is The similarity matrix of the feature vectors in the node graph at the CGFA layer, This matrix is Line Elements of a column. Use the following formula to get:

[0070] ;

[0071] ;

[0072] in, is the affinity matrix, which represents the similarity of node features in the graph pair; Representative Node and nodes affinity score. Contains learnable parameters for constructing the affinity matrix, is the dimension of node features, is a hyperparameter used for scaling. The affinity matrix is ​​generated by performing a Sinkhorn operation on the affinity matrix to ensure that it is a double random matrix, thereby avoiding the numerical scale problem of the attention weight when aggregating across graphs.

[0073] In this embodiment, the node matching module includes two parallel node matching workflows and a multi-layer perceptron. The two parallel node matching workflows predict matching results based on the obtained node embeddings. The node matching workflow includes node matching submodules. The multi-layer perceptron is used to summarize the matching results at different scales to obtain the final node matching information. In this embodiment, the node matching information includes node matching confidence and node matching cost.

[0074] Specifically, the node matching module uses the connected node embedding and The trainable parameter matrix is ​​used to calculate the control flow graph and The node correspondence between them is output as The CFGSim model uses two parallel node matching workflows to capture node matching confidence separately. and node matching cost , each node matches the workflow by node matching submodules, where is the number of cross-graph feature aggregation layers. In this embodiment, the node matching module uses a multi-layer perceptron (MLP) to summarize matching information at different scales to obtain the final output. Post-processing algorithms can be used to generate high-quality graph editing paths, ensuring the interpretability of the model.

[0075] In a more specific embodiment, the node matching module is used to capture the control flow graph The matching information between nodes. After the CGFA layer, the node features of each control flow graph can be obtained by stacking the cross-graph feature vectors of all nodes. and ,in, is the characteristic dimension. and is input to the node matching module and outputs a matrix , each element in the matrix Depend on and module parameters, so it can represent the node and To achieve this, we multiply the cross-graph node embeddings by a trainable parameter matrix Is the most direct method, as shown below:

[0076] ;

[0077] in, and The sizes are and ,but The size is , the result matrix The size is , that is, it depends only on the number of nodes of the input graph pair.

[0078] The matrix obtained by the node matching module at different layers Contains matching information of different scales, for example, when When the matching matrix It only depends on the 1-hop neighborhood information of the nodes in the graph. The output matrices of the node matching modules from different layers are stacked, and the last dimension is reduced to 1 through MLP. This process essentially summarizes the matching information of different scales to obtain higher quality output, as shown in the following formula:

[0079] ;

[0080] The graph edit distance calculation model uses two independent node matching workflows to obtain the node matching confidence matrix respectively. and node matching cost matrix ,in The method is supervised by multiple optimal matching matrices of graph pairs.

[0081] In the graph edit distance prediction module of this embodiment, the graph edit distance is predicted by the weighted sum of the node matching cost and the bias value, and the bias value is calculated by the neural tensor network of the graph embedding between the connection graph pairs. The model loss function includes matching loss, graph edit distance value loss and loss hyperparameter, and the loss hyperparameter is used to balance the matching loss and graph edit distance value loss.

[0082] In a more specific embodiment, the graph edit distance prediction module calculates The weighted sum of the matching costs and the bias value are used to predict the control flow graph and The graph edit distance is: ,in, represents the activation function, Represents a bias term, which is connected by and The graph embedding is calculated using the Neural Tensor Network (NTN).

[0083] Attention pooling for obtaining control flow graphs and Graph embedding. After all After the cross-graph feature aggregation layer, and The node characteristics of and First, perform an average pooling operation to obtain , the graph embedding is designed as the weighted sum of all node features, where the weights are determined by the attention mechanism, as shown in the following formula:

[0084] ;

[0085] ;

[0086] in, Represents the control flow graph and Shared learnable parameters, It is a picture The attention weight, yes No. elements, yes Middle The feature vector of a node, It is a picture Graph embedding.

[0087] Neural Tensor Network, used to model the relationship between two graph embeddings and use it as a bias term for graph edit distance prediction. For each neuron in the Neural Tensor Network, the interaction features between the graphs Calculated by the following formula:

[0088] ;

[0089] in, Indicates a connection operation; All represent learnable parameters. The multi-neuron structure adopted enables the model to focus on information in different subspaces. Finally, the model merges the interactive features of all neurons through a two-layer MLP to obtain the bias term for graph edit distance prediction. .

[0090] Graph edit distance prediction, in the CFGSim model, for the control flow graph generated by two pieces of code , a cost matrix is ​​generated through the node matching workflow , Represented by the node pair The cost of the editing operation caused by In addition, another node matching workflow generates a matching confidence matrix , which is a matrix with all values ​​in A real matrix consisting of the optimal node matching set Supervision. During training, the minimum negative log-likelihood is used as the loss to minimize and When CFGSim is well trained, Can approach an optimal node match Since the graph edit distance calculation involves all node pairs, the Softmax function is used to calculate The values ​​in are smoothed to capture the importance of each node pair, as shown in the following formula:

[0091] ;

[0092] in, Represented by the node pair The costs of editing operations incurred; Represents the importance of this node pair; the graph edit distance prediction module accumulates the costs of all node pairs by weight and uses it to predict the final graph edit distance .

[0093] Furthermore, the model can also be extended to the scenario of predicting graph similarity, using the sigmoid function To obtain a The similarity score between , the expression is as follows:

[0094] .

[0095] In this embodiment, in model training, the matching loss is calculated by taking the node matching confidence matrix Matching multiple optimal nodes The minimum likelihood value is used as the matching loss; the graph edit distance value loss is calculated by converting the graph edit distance value into a similarity score, and using the mean square error of the similarity score to represent the graph edit distance value loss.

[0096] Specifically, since the goal of the model is to simultaneously predict the graph edit distance between control flow graphs and find the node matching confidence (for generating graph edit paths), the model loss function of the CFGSim model is Should include matching loss And the graph edit distance loss Two parts, Is a hyperparameter that weighs the two, and the loss function can be expressed as:

[0097] ;

[0098] Matching loss: In the CFGSim model, the true matching is used to supervise the training of the matching matrix. and Optimal node matching ,for , , It can be expressed as a size of The 0-1 matrix is ​​calculated as follows:

[0099] ;

[0100] For the multi-view node matching module that obtains the matching confidence matrix, the last layer of the MLP is the sigmoid activation function, so Each value in In the range, considering that a graph pair may have multiple optimal node matches, The minimum value of the likelihood is used as the loss, and the matching loss is calculated as follows:

[0101] ;

[0102] The graph edit distance value loss is because the model tends to predict values ​​in a small range, and the graph edit distance ranges from , so it is a better choice to convert the graph edit distance value into a similarity score before training. The expression of the conversion function is as follows:

[0103] ;

[0104] The converted similarity score Must fall on , so we can simply use the minimum square error (MSE) to represent the graph edit distance loss, which is calculated as follows:

[0105] ;

[0106] in, Represents the true similarity score.

[0107] In addition, the method of this embodiment also includes generating a graph edit path between control flow graphs. The calculation of the graph edit path is equivalent to finding the optimal node match between the original graph and the target graph. Once the node match is determined, the graph edit path can be quickly generated by traversing the two graphs once. Next, we first explain how to generate an edit path from a node match, and then introduce how this example processes the node match confidence matrix. A post-processing algorithm for generating graph editing paths.

[0108] Given two graphs and , node matching Create a graph and The node correspondence between them can be used to generate the editing path. The specific algorithm flow is as follows:

[0109] For any matching node pair ,if , the node The label is changed to ;if ,but Existence unmatched nodes, you need to add these additional nodes to ,if , then in Existence unmatched nodes, remove them. After this step, Each node in A node in the .

[0110] Assume that the node satisfies and , if the node satisfies and , the edge The label is changed to ;if and , at the node and Add a label between On the other hand, if and ,delete and The label between edge.

[0111] Node matching can be done using a binary matrix To express, Representation diagram Nodes in With Figure Nodes in Match. For example, in Figure 3 In (a), a graph is shown. , (b) is the two optimal node matches between them. Applying the above algorithm on the basis of these two node matches, we can then obtain the graph editing path in (c) and In this embodiment, Indicates node-based matching The figure Convert to Graph The number of edit operations required. Note that and There may be multiple node matches between them that generate the same edit path length.

[0112] The post-processing algorithm for generating the graph editing path is specifically:

[0113] Generating the shortest graph editing path is equivalent to finding the optimal node matching between graph pairs. When the CFGSim model is well trained, the node matching confidence matrix it outputs is Matching matrix with an optimal node Very close, in the ideal case, their dot product Compared to and The other possible node matches between should be the largest. Based on this observation, this embodiment will Extract The problem is transformed into a weighted bipartite matching problem. Specifically, a weighted bipartite graph is first constructed by the following formula:

[0114] ;

[0115] in, represent The weights of the edges in , matching Weight It is defined as the sum of the weights of the edges it contains, as shown in the following formula:

[0116] ;

[0117] When the model converges, and Very close, you can find The match with the largest weight comes from Extract , as shown below:

[0118] ;

[0119] in, Represents the set of all node matches.

[0120] In order to increase the chance of finding the optimal node match, this embodiment The above enumerates several matches instead of just considering the one with the largest weight. Based on this, this embodiment proposes The main idea of ​​the algorithm is to calculate the graph Before the weight Large node matching (abbreviated as best match), and then by The function (that is, the algorithm for generating edit paths from node matching described above) converts them into a graph edit path and finally returns this Edit the shortest path in the route.

[0121] In addition, the present embodiment also includes a computer device, including a memory and a processor;

[0122] The memory is used to store a computer program executable on the processor;

[0123] The processor is used to implement the steps of the code similarity detection method as described above when executing the computer program.

[0124] Exemplarily, the computer program may be divided into one or more modules / units, which are stored in the memory and executed by the processor to implement the present invention. The one or more modules / units may be a series of computer program instruction segments capable of implementing specific functions, which are used to describe the execution process of the computer program in the computer device.

[0125] The computer device may be a computing device such as a mobile phone, a desktop computer, a notebook, a PDA, a cloud server, etc. The computer device may include, but is not limited to, a processor and a memory. For example, the computer device may also include an input / output device, a network access device, a bus, etc.

[0126] The processor may be a central processing unit (CPU), other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc. The processor is the control center of the computer device, and uses various interfaces and lines to connect various parts of the entire computer device.

[0127] The memory can be used to store the computer program and / or module, and the processor implements the computer program by running or executing the computer program and / or module stored in the memory, and calling the data stored in the memory. The memory can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area can store data created according to the use of the mobile phone (such as audio data, a phone book, etc.), etc. In addition, the memory can include a high-speed random access memory, and can also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (SecureDigital, SD) card, a flash card (Flash Card), at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.

[0128] Wherein, if the module / unit integrated in the computer device is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the present invention implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Wherein, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form, etc. The computer-readable medium can include: any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electric carrier signal, telecommunication signal and software distribution medium, etc.

[0129] In addition, this embodiment also discloses a computer device, including a memory and a processor;

[0130] The memory is used to store a computer program executable on the processor;

[0131] The processor is used to implement the steps of the above-mentioned code similarity detection method when executing the computer program.

[0132] The present invention proposes a CFGSim model, which is used to calculate the graph edit distance between control flow graphs, thereby providing a quantitative basis for code similarity detection. The model consists of a cross-graph feature aggregation layer, a node matching module, and a graph edit distance prediction module. While accurately calculating the graph edit distance between control flow graphs, it also outputs a matching confidence matrix. The matrix is ​​processed by a post-processing algorithm to generate high-quality graph edit paths, solving the problem of lack of interpretability of existing code similarity detection methods based on deep learning.

[0133] It should be noted that the device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0134] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A code similarity detection method based on graph neural network, characterized in that: The steps include: S1: calling a control flow graph conversion module, converting the code to be detected into a control flow graph, performing topological structure encoding on the control flow graph, and obtaining a node feature vector; S2: construct a graph edit distance calculation model, wherein the graph edit distance calculation model includes a cross-graph feature aggregation module, a node matching module, an attention pooling module, a neural tensor network, and a graph edit distance prediction module. The cross-graph feature aggregation module is used to iteratively update the node feature vector to obtain the node embedding. The node matching module is used to process node embedding and output matching information of nodes between graph pairs. The attention pooling is used to process node embeddings and output graph embeddings. The neural tensor network is used to process graph embedding and output graph embedding interaction features. The graph edit distance prediction module is used to calculate node matching information and graph embedding interaction features to obtain the graph edit distance; S3: Use the model loss function to train the graph edit distance calculation model, and call the trained graph edit distance calculation model to simultaneously predict the graph edit distance and node matching confidence matrix between two control flow graphs. The graph edit distance is used to judge the similarity between the codes to be detected, and the node matching confidence matrix can be used to generate the graph edit path.

2. The code similarity detection method according to claim 1, characterized in that: The code to be detected is converted into a control flow graph, and the control flow graph is topologically encoded to obtain a node feature vector, specifically: Lexical and syntactic analysis is performed on the two sections of code to be tested respectively, the code to be tested is converted into a control flow graph with structural and semantic information, the basic blocks in the control flow graph are encoded to obtain node feature information and edge feature information, and the node feature information and edge feature information are converted into feature vectors in the deep learning model.

3. The code similarity detection method according to claim 1, characterized in that: The cross-graph feature aggregation module includes The cross-graph feature aggregation layer utilizes the message passing mechanism of the graph neural network to aggregate the features of neighbor nodes and corresponding edges, and captures the information on another control flow graph through the attention mechanism, thereby generating node embeddings with fine-grained cross-graph interaction features.

4. The code similarity detection method according to claim 1, characterized in that: The node matching module includes two parallel node matching workflows and a multi-layer perceptron. Two parallel node matching workflows predict matching results based on the obtained node embeddings. The node matching workflows include Nodes match submodules; The multi-layer perceptron is used to summarize the matching results at different scales and obtain the final node matching information.

5. The code similarity detection method according to claim 1, characterized in that: The node matching information includes node matching confidence and node matching cost.

6. The code similarity detection method according to claim 1, characterized in that: In the graph edit distance prediction module, the graph edit distance is predicted using a weighted sum of node matching costs and a bias value, where the bias value is calculated by a neural tensor network that connects graph embeddings between graph pairs.

7. The code similarity detection method according to claim 1, characterized in that: The model loss function includes a matching loss, a graph edit distance value loss, and a loss hyperparameter, and the loss hyperparameter is used to balance the matching loss and the graph edit distance value loss.

8. The code similarity detection method according to claim 7, characterized in that: During model training, The matching loss is calculated by taking the minimum log likelihood of the node matching confidence matrix and the matching of multiple optimal nodes as the matching loss; The graph edit distance value loss is calculated by converting the graph edit distance value into a similarity score, and using the mean square error of the similarity score to represent the graph edit distance value loss.

9. A computer device, characterized in that: including memory and processor; The memory is used to store a computer program executable on the processor; The processor is configured to implement the steps of the code similarity detection method according to any one of claims 1 to 8 when executing the computer program.

10. 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 similarity detection method according to any one of claims 1 to 8 are implemented.