Fine-grained semantic code clone detection method

By generating and decomposing an abstract syntax tree, calculating the degree of freedom matrix and similarity matrix, and using the random forest algorithm to detect code clones, we solve the problems of low code clone detection efficiency and difficult positioning in the existing technology, and achieve efficient fine-grained semantics. Code clone detection.

CN118550581APending Publication Date: 2024-08-27Chinese People's Liberation Army Cyberspace Force Information Engineering University
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202410676027.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-05-27
Publication Date
2024-08-27

AI Technical Summary

Technical Problem

Existing code clone detection technology is difficult to efficiently identify and locate similar parts in semantic code, especially in large data sets and complex code structures, making it difficult to locate and repair security vulnerabilities.

Method used

By generating an abstract syntax tree (AST), decomposing it into multiple subtrees, and calculating the degree of freedom matrix and similarity matrix, the random forest algorithm is used for fine-grained semantic code clone detection to accurately identify the similarities between code blocks.

Benefits of technology

It reduces the computational complexity, improves the efficiency of code clone detection, can accurately identify and locate code clone parts, and achieves fine-grained semantic clone detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118550581A
    Figure CN118550581A
Patent Text Reader

Abstract

The invention provides a fine-grained semantic code clone detection method. The method comprises the following steps of: 1, respectively generating abstract syntax trees AST of two target codes, and respectively decomposing the AST of the two target codes to respectively obtain a plurality of sub-trees; 2, tree coding is carried out on the multiple sub-trees of the two target codes, and freedom degree matrixes of the two target codes are obtained; 3, a similarity matrix of the two target codes is calculated according to the degree-of-freedom matrixes of the two target codes, the similarity between corresponding code blocks in the two target codes is represented through values in the similarity matrix, and the smaller the values are, the larger the similarity of the two code blocks is; and 4, converting the similarity matrix into a similarity vector, and inputting the similarity vector into a code clone detection model to obtain a detection result.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a fine-grained semantic code clone detection method. Background Art

[0002] With the continuous progress and development of the computer and software fields, the scale of software codes is getting larger and larger. Therefore, in order to save time and energy, many software developers will choose to clone codes with similar functions to build and maintain software codes. Extensive code cloning has brought convenience to software development to a certain extent and produced positive benefits, but it also reduces the security of software and increases maintenance costs. For example, if the copied code contains any vulnerability, the vulnerability will spread throughout the software system, especially after the vulnerable code is reused many times, the scope of the vulnerability will expand exponentially. Therefore, it is essential to find an automatic method that can efficiently detect code clones from a large amount of source code.

[0003] In the field of code clone detection, research work mainly revolves around two major methods, namely, scalable clone detection focusing on efficiency improvement and semantic clone detection for deep semantic understanding. For scalable clone detection technology, the mainstream methods include text-based and token-based methods, which convert source code into text or token sequences and use a series of similarity calculation algorithms to achieve rapid analysis of cloned code. Although such methods are excellent in speed, they cannot effectively handle clones with semantic similarity due to the lack of consideration of code semantic information. To this end, researchers have further proposed semantic clone detection schemes based on code intermediate representations (such as control flow graphs, program dependency graphs, etc.). These methods perform clone detection by constructing and analyzing graph structures that contain program semantic features. However, since creating and parsing complex graph structures is time-consuming and computationally expensive, such methods are difficult to ensure good scalability when processing large data sets. Compared with graph-based methods, clone detection technology based on abstract syntax trees (ASTs) is more lightweight, but due to the complex hierarchical characteristics of AST itself, it still leads to high time overhead when performing similarity comparisons.

[0004] In addition, current clone detection tools cannot accurately identify similar parts between clone pairs, especially when dealing with semantic code clones. Specifically, when faced with cloned code containing a large number of lines of code, and the actual security issues are only concentrated in a few key lines of code, existing clone detection technology often has difficulty effectively locating these hidden security vulnerabilities or defects, and naturally cannot quickly carry out targeted repairs. If these security vulnerabilities are hidden in semantic clone code, it will be even more difficult to locate and repair them. Summary of the invention

[0005] In order to improve the detection efficiency of code clone detection and accurately identify the similar parts between clone pairs, the present invention provides a fine-grained semantic code clone detection method.

[0006] The present invention provides a fine-grained semantic code clone detection method, comprising:

[0007] Step 1: Generate abstract syntax trees AST of two target codes respectively, and decompose the ASTs of the two target codes respectively to obtain multiple subtrees;

[0008] Step 2: Perform tree encoding on multiple subtrees of the two target codes respectively to obtain the degree of freedom matrices of the two target codes;

[0009] Step 3: Calculate the similarity matrix of the two target codes according to the degree of freedom matrix of the two target codes, and use the value in the similarity matrix to characterize the similarity between the corresponding code blocks in the two target codes. The smaller the value, the greater the similarity between the two code blocks.

[0010] Step 4: Convert the similarity matrix into a similarity vector, input the similarity vector into a code clone detection model, and obtain a detection result.

[0011] Furthermore, in step 1, the ASTs of the two target codes are decomposed to obtain multiple subtrees respectively, including:

[0012] Step A1: Decomposing the AST according to the statement types in the AST of the target code to obtain the first subtree and the structural subtree corresponding to each statement type; the structural subtree is the remaining subtree in the AST except the first subtree corresponding to each statement type;

[0013] Step A2: Decompose the tree structure with the largest number of nodes in the first subtree corresponding to each statement type again to obtain a second subtree corresponding to each statement type.

[0014] Furthermore, step 2 specifically includes:

[0015] Step B1: Divide all nodes in the ASTs of the two target codes into m types according to the node functions, and calculate the degree of freedom of each type of node in the AST of the target code;

[0016] Step B2: For each target code, traverse its subtrees respectively to obtain the m-dimensional degree of freedom vector corresponding to each subtree, and form the first degree of freedom matrix of the target code by the m-dimensional degree of freedom vectors corresponding to all first subtrees and structural subtrees, and form the second degree of freedom matrix of the target code by the m-dimensional degree of freedom vectors corresponding to all second subtrees.

[0017] Furthermore, the statement type includes one or more of an if statement, a for statement, a while statement, a try statement and a switch statement.

[0018] Furthermore, the degree of freedom of a node is calculated according to the number of child nodes it contains.

[0019] Further, the similarity matrix of the two target codes is calculated according to the degree of freedom matrix of the two target codes, specifically including:

[0020] The similarity matrix of the two target codes is calculated according to the following formula based on the degree of freedom matrix of the two target codes;

[0021]

[0022] Among them, A and B represent the degree of freedom matrices of the two target codes; a ij represents the value of the element in row i and column j in A, b ij represents the element value of the i-th row and j-th column in the degree of freedom matrix B, and n represents the total number of rows in the degree of freedom matrix.

[0023] Furthermore, the code clone detection model adopts a random forest algorithm.

[0024] Beneficial effects of the present invention:

[0025] (1) A complex abstract syntax tree (AST) is decomposed into multiple simple subtrees. By performing tree encoding on each simple subtree, the complex tree structure is finally converted into a simple vector representation, thereby reducing the computational complexity and improving the efficiency of code clone detection.

[0026] (2) All subtrees that make up the AST are encoded into a degree of freedom matrix through tree encoding. A row vector in the degree of freedom matrix represents the characteristics of a subtree. The similarity between the vectors is used to represent the similarity between the subtrees of the target codes. Therefore, based on the similarity matrix of the two target codes, the specific code clone part can be accurately identified and located, realizing fine-grained semantic clone detection. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] Figure 1 A schematic diagram of a flow chart of a fine-grained semantic code clone detection method provided by an embodiment of the present invention;

[0028] Figure 2 The following is a schematic diagram of the calculation process of the degree of freedom matrix provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0029] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution in the embodiment of the present invention will be clearly described below in conjunction with the drawings in the embodiment of the present invention. Obviously, the described embodiment is a part of the embodiment of the present invention, not all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0030] like Figure 1 As shown, an embodiment of the present invention provides a fine-grained semantic code clone detection method, including:

[0031] S101: AST generation and decomposition;

[0032] Specifically, first, the abstract syntax trees AST of two target codes (i.e., program 1 and program 2) are generated respectively; in this embodiment, since the experimental data set is composed of codes written in the Java programming language, the javalang tool is used to perform static analysis on the target code to obtain the AST of the target code. Then, the ASTs of the two target codes are decomposed to obtain multiple subtrees respectively; in this embodiment, the AST is decomposed according to the statement type in the AST of the target code to obtain the first subtree and the structural subtree corresponding to each statement type; the structural subtree is the remaining subtree in the AST except the first subtree corresponding to each statement type; in order to further extract the structural details in the target code, the tree structure with the largest number of nodes in the first subtree corresponding to each statement type is decomposed again to obtain the second subtree corresponding to each statement type. Among them, retaining the structural subtree is to better retain the overall structural information of the target code.

[0033] In practical applications, for the target code, by screening subtrees with a large number of nodes and subtrees with high frequency of occurrence, the following five types of statements are generally selected as decomposition objects, namely Forstmt, Whilestmt, Trystmt, Switchstmt and Ifstmt.

[0034] S102: tree coding;

[0035] Specifically, tree encoding is performed on multiple subtrees of the two target codes respectively to obtain the degree of freedom matrices of the two target codes; this step specifically includes the following two substeps: first, all nodes in the ASTs of the two target codes are divided into m types according to the node functions, and the degree of freedom of each type of node in the AST of the target code is calculated; secondly, for each target code, each subtree is traversed respectively to obtain the m-dimensional degree of freedom vector corresponding to each subtree to realize tree encoding, and the first degree of freedom matrix of the target code is composed of the m-dimensional degree of freedom vectors corresponding to all the first subtrees and the structural subtrees, and the second degree of freedom matrix of the target code is composed of the m-dimensional degree of freedom vectors corresponding to all the second subtrees.

[0036] In actual applications, the inventors found through big data analysis that according to the node function, all nodes in AST can generally be divided into up to 72 node types, specifically, including 57 non-leaf nodes and 15 leaf nodes.

[0037] Furthermore, since all nodes in AST can be described by their child nodes, the degree of freedom of the node is calculated by counting the number of child nodes contained in the node in this implementation. For example, the number of child nodes contained in the node can be directly used as the degree of freedom of the node.

[0038] Furthermore, in order to facilitate subsequent calculations, in practical applications, it is necessary to normalize the degree of freedom values ​​of the nodes, for example, convert the degree of freedom values ​​into probability vector representations. The specific formula is as follows:

[0039]

[0040] Among them, D1 and D2 represent the degree of freedom vectors before and after the conversion respectively, and D2[i] represents the normalized degree of freedom value of node type i in the AST.

[0041] Figure 2 An example is shown to illustrate the calculation process of the degree of freedom matrix (the example involves 2 subtrees and 1 structural subtree corresponding to if statements and for statements).

[0042] Based on the above principles, assuming that the first subtrees of 5 statement types and 1 structural subtree are decomposed in step S101, firstly, by performing tree encoding on the 6 subtrees respectively and aggregating them in a fixed order between the subtrees, a 6*72 degree of freedom matrix can be obtained, which is recorded as the first degree of freedom matrix. Next, the tree structures with the largest number of nodes in the 5 first subtrees are decomposed again to obtain their nested subtrees, also called second subtrees. Assuming that the nested subtree of Switchstmt is determined to be SwitchstmtCase, the nested subtree of Trystmt is Forstmt, and the nested subtrees of Forstmt, Whilestmt and Ifstmt are all Blockstmt, the 5 second subtrees are also tree encoded, thereby obtaining another 5*72 nested subtree degree of freedom matrix, which is recorded as the second degree of freedom matrix.

[0043] S103: feature extraction;

[0044] Specifically, the similarity matrix of the two target codes is calculated based on the degree of freedom matrix (DOF matrix) of the two target codes;

[0045] For the first degree of freedom matrices of the two target codes, calculate the similarity of their corresponding subtrees one by one, that is: first take the first row vector of the first degree of freedom matrix corresponding to program 1 and calculate the similarity with each row vector of the first degree of freedom matrix corresponding to program 2, and use the obtained similarity score as the first row vector of the similarity matrix; similarly, calculate the similarity between the second row vector of the first degree of freedom matrix corresponding to program 1 and each row vector of the first degree of freedom matrix corresponding to program 2, and so on. Assuming that the subtree degree of freedom vector matrix has a total of 6 rows, a 6*6 similarity matrix can be obtained in the end.

[0046] Similarly, for the second degree of freedom matrices of the two target codes, according to the above calculation process, a 5*5 similarity matrix can be obtained.

[0047] As an implementable method, in this embodiment, the similarity of the corresponding subtrees is evaluated by calculating the "Euclidean distance" of the subtrees, as shown in the following formula:

[0048]

[0049] Among them, A and B represent the degree of freedom matrices of the two target codes; a ij represents the value of the element in row i and column j in A, b ij represents the element value of the i-th row and j-th column in the degree of freedom matrix B, and n represents the total number of rows in the degree of freedom matrix.

[0050] S104: Classification;

[0051] Specifically, the similarity matrix is ​​converted into a similarity vector, and the similarity vector is input into the code clone detection model to obtain the detection result, i.e., clone or non-clone. Specifically, assuming that a 6*6 similarity matrix and a 5*5 similarity matrix are obtained in the previous step, the two similarity matrices are expanded and connected row by row, and finally a 61-dimensional similarity vector can be obtained as the model input.

[0052] It should be noted that the code clone detection model in this embodiment needs to be pre-trained. Specifically, a training data set is constructed, which includes a number of code pairs that have been labeled with "clone" or "non-clone" labels, and similarity vectors of all clone pairs and non-clone pairs are extracted according to the above steps S101 to S103, and these labeled similarity vectors are input into a machine learning model (such as a random forest algorithm (RF)) for training, and the trained model is saved as the code clone detection model.

[0053] S105: fine-grained detection;

[0054] Specifically, the values ​​in the similarity matrix are used to characterize the similarity between corresponding code blocks in two target codes. The smaller the value, the greater the similarity between the two code blocks. The fine-grained detection results between each code block are obtained by analyzing the size of each value in the subtree similarity matrix of the clone code pair.

[0055] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A fine-grained semantic code clone detection method, characterized in that: include: Step 1: Generate abstract syntax trees AST of two target codes respectively, and decompose the ASTs of the two target codes respectively to obtain multiple subtrees; Step 2: Perform tree encoding on multiple subtrees of the two target codes respectively to obtain the degree of freedom matrices of the two target codes; Step 3: Calculate the similarity matrix of the two target codes according to the degree of freedom matrix of the two target codes, and use the value in the similarity matrix to characterize the similarity between the corresponding code blocks in the two target codes. The smaller the value, the greater the similarity between the two code blocks. Step 4: Convert the similarity matrix into a similarity vector, input the similarity vector into a code clone detection model, and obtain a detection result.

2. A fine-grained semantic code clone detection method according to claim 1, characterized in that: In step 1, the ASTs of the two target codes are decomposed to obtain multiple subtrees, including: Step A1: Decomposing the AST according to the statement types in the AST of the target code to obtain the first subtree and the structural subtree corresponding to each statement type; the structural subtree is the remaining subtree in the AST except the first subtree corresponding to each statement type; Step A2: Decompose the tree structure with the largest number of nodes in the first subtree corresponding to each statement type again to obtain a second subtree corresponding to each statement type.

3. A fine-grained semantic code clone detection method according to claim 2, characterized in that: Step 2 specifically includes: Step B1: Divide all nodes in the ASTs of the two target codes into m types according to the node functions, and calculate the degree of freedom of each type of node in the AST of the target code; Step B2: For each target code, traverse its subtrees respectively to obtain the m-dimensional degree of freedom vector corresponding to each subtree, and form the first degree of freedom matrix of the target code by the m-dimensional degree of freedom vectors corresponding to all first subtrees and structural subtrees, and form the second degree of freedom matrix of the target code by the m-dimensional degree of freedom vectors corresponding to all second subtrees.

4. A fine-grained semantic code clone detection method according to claim 2, characterized in that: The statement type includes one or more of an if statement, a for statement, a while statement, a try statement and a switch statement.

5. A fine-grained semantic code clone detection method according to claim 3, characterized in that: The degrees of freedom of a node are calculated based on the number of child nodes it contains.

6. A fine-grained semantic code clone detection method according to claim 3, characterized in that: The similarity matrix of the two target codes is calculated according to the degree of freedom matrix of the two target codes, specifically including: The similarity matrix of the two target codes is calculated according to the following formula based on the degree of freedom matrix of the two target codes; Among them, A and B represent the degree of freedom matrices of the two target codes; a ij represents the value of the element in row i and column j in A, b ij represents the element value of the i-th row and j-th column in the degree of freedom matrix B, and n represents the total number of rows in the degree of freedom matrix.

7. A fine-grained semantic code clone detection method according to claim 1, characterized in that: The code clone detection model adopts a random forest algorithm.

Citation Information

Cited By

  • Code clone detection method and device based on hierarchical analysis, equipment and medium

    CN118916077A