A similarity detection method for open-ended programming questions

By constructing a semantic coding network and feature fusion, the problem of semantic features being ignored in cross-morphological code similarity detection is solved, the detection accuracy and efficiency are improved, and more efficient similarity matching is achieved.

CN118568521BActive Publication Date: 2024-11-19XI AN JIAOTONG UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411038014.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-31
Publication Date
2024-11-19
Estimated Expiration
2044-07-31

AI Technical Summary

Technical Problem

In the cross-morphological code similarity detection, the method based on character literals ignores the potential semantic features of the code, resulting in poor similarity matching results, and feature selection and feature engineering require a lot of expert experience and are inefficient.

Method used

By building a source code semantic coding network and a binary code semantic coding network, data extraction and preprocessing of the source code and binary code respectively, representation vectors are generated, feature fusion and similarity detection are performed, and cosine similarity measurement and vector distance are used. Calculate to determine code similarity.

Benefits of technology

The accuracy of the detection model and the accuracy of similarity detection are improved, the limitations of traditional methods are overcome, and the detection efficiency is improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118568521B_ABST
    Figure CN118568521B_ABST
Patent Text Reader

Abstract

The present invention provides a similarity detection method for open-ended programming questions, and relates to the technical field of code similarity detection. The method comprises: extracting and preprocessing data from source code to obtain source code information; extracting and preprocessing data from binary code to obtain binary code information; constructing a source code semantic coding network and a binary code semantic coding network respectively, and encoding the source code information and the binary code information respectively to obtain a source code representation vector and a binary code representation vector; performing feature fusion operations on the source code representation vector and the binary code representation vector respectively to obtain a source code fusion vector and a binary code fusion vector; performing similarity detection on the source code fusion vector and the binary code fusion vector to obtain a detection result. The method improves the accuracy of the detection model, the accuracy rate of similarity detection, and the detection efficiency.
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 similarity detection method for open-ended programming questions. Background Art

[0002] At present, the similarity detection between cross-modal source code and binary code mainly extracts the literal feature information of the code, including string literals, integer literals and recursive information in the code, and uses a matching algorithm to calculate the similarity between codes. In 2019, the Institute of Information Engineering of the Chinese Academy of Sciences proposed a technology that can be used to detect the reuse of open source projects in commercial software - B2SFinder. The B2SFinder model selects seven code features in the code feature information, including character literal information, string array information, if / else branch structure information and switch / case selection structure features that are not easily affected by compilation, as the representation information of the code. These seven stable code features are further divided into three categories. For the classified code features, three matching tasks are completed, namely, string-based exact matching, search-based integer matching and semantic-based control flow matching. Finally, the weight ratio of code features in a given software or application is calculated to achieve cross-modal code similarity detection. CodeCMR uses independent neural network models to learn representation vectors from source code and binary code respectively, and cross-modal fusion of representation vectors is carried out under the guidance of triplet loss network to achieve cross-modal similarity matching at the function level.

[0003] However, source code and binary code are two completely different forms of code representation, and there are huge differences between the two. On the one hand, the traditional cross-modality detection method based on character literals has very limited information such as strings, numbers, and recursion in function codes. Using only a small amount of code literal information and ignoring the potential semantic features of the code may lead to poor similarity matching results. On the other hand, a lot of expert experience is required to complete feature selection and feature engineering, and this process takes a lot of time, which will lead to low efficiency of similarity matching. Therefore, it is very necessary to design a similarity detection method for open programming questions. Summary of the invention

[0004] The purpose of the present invention is to provide a similarity detection method for open-ended programming questions, so as to improve the accuracy of the detection model and the accuracy and detection efficiency of the similarity detection.

[0005] To achieve the above object, the present invention provides the following solutions:

[0006] A similarity detection method for open-ended programming questions comprises the following steps:

[0007] Extract and preprocess the source code to obtain source code information;

[0008] Perform data extraction and preprocessing on the binary code to obtain binary code information;

[0009] A source code semantic encoding network and a binary code semantic encoding network are constructed respectively, and source code information and binary code information are encoded respectively to obtain a source code representation vector and a binary code representation vector;

[0010] Perform feature fusion operations on the source code representation vector and the binary code representation vector respectively to obtain a source code fusion vector and a binary code fusion vector;

[0011] A similarity test is performed on the source code fusion vector and the binary code fusion vector to obtain a test result.

[0012] Optionally, both the source code information and the binary code information include: string literals, token sequence information and structure information; the structure information is represented by a control flow graph.

[0013] Optionally, the source code semantic encoding network is: CodeBERT model.

[0014] Optionally, the specific steps of constructing a binary code semantic encoding network are:

[0015] Construct a binary sample pair dataset based on token sequence information; the binary sample pair includes: positive samples and negative samples;

[0016] The feature vector is obtained by extracting and merging the features of the binary sample data set through the representation learning model;

[0017] Through the network module based on contrastive learning, the feature vector is optimized and encoded to obtain the optimized vector;

[0018] Through vector concatenation, multi-layer perceptron processing and sigmoid function, interval similarity results are obtained.

[0019] Optionally, the feature fusion includes a joint representation method and a vector direct concatenation method.

[0020] Optionally, the specific calculation formula of the joint representation is: ; Among them, f() is the fusion mechanism, x n is the modal feature vector generated by representation learning of the nth code information, x m is the fusion vector that completes feature fusion through joint representation.

[0021] Optionally, the specific calculation formula of the vector direct concatenation method is: ;in, To complete the feature vector of feature fusion through vector concatenation method, is the representation vector of string literal information, is the representation vector of token sequence information, is the representation vector of structural information.

[0022] Optionally, the specific steps of similarity detection are:

[0023] Align the source code representation vector and the binary code representation vector using the cosine similarity metric;

[0024] Calculate the distance between the source code representation vector and the binary code representation vector to obtain the code distance;

[0025] The code distance is compared with the similarity threshold. If the code distance is greater than the similarity threshold, the source code is judged to be similar to the binary code; the similarity threshold is 0.8.

[0026] Optionally, the specific calculation formula of the code distance is: ; where x i is the i-th dimension source code representation vector, y i is the i-th dimension binary code representation vector, and m is the number of dimensions of the representation vector.

[0027] According to the specific embodiment provided by the present invention, the present invention discloses the following technical effects: the similarity detection method of the open-ended programming questions provided by the present invention comprises: extracting and preprocessing data of the source code to obtain source code information; extracting and preprocessing data of the binary code to obtain binary code information; constructing a source code semantic coding network and a binary code semantic coding network respectively, and encoding the source code information and the binary code information respectively to obtain a source code representation vector and a binary code representation vector; performing feature fusion operations on the source code representation vector and the binary code representation vector respectively to obtain a source code fusion vector and a binary code fusion vector; performing similarity detection on the source code fusion vector and the binary code fusion vector to obtain a detection result. The method improves the accuracy of the detection model and the accuracy and efficiency of similarity detection. BRIEF DESCRIPTION OF THE DRAWINGS

[0028] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments 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 labor.

[0029] Figure 1A flow chart of a similarity detection method for an open-ended programming question according to an embodiment of the present invention;

[0030] Figure 2 4 is a structural diagram of the CodeBERT model according to an embodiment of the present invention. DETAILED DESCRIPTION

[0031] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0032] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and understandable, the present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.

[0033] like Figure 1 As shown, an embodiment of the present invention provides a similarity detection method for open-ended programming questions, comprising the following steps:

[0034] Step 100: extracting and preprocessing data from the source code to obtain source code information; both data extraction and preprocessing are performed at the function level.

[0035] Specifically, the source code information includes: string literals, token sequence information and structure information; the structure information is represented by a control flow graph.

[0036] Furthermore, the method for extracting string literals is: traverse the entire source function, and when a double quote is encountered, it indicates that a string literal is found, and its content is extracted and stored separately. The specific steps are as follows:

[0037] First, input the source function Funs;

[0038] Secondly, output all the strings in the source function, strings[]; this step includes:

[0039] Traverse each sourceline of the source function and call dealSourceLine(Funs);

[0040] Determine whether there is a double quote in the current line. If so, store and scan findFirstQuo(source line );

[0041] When the next double quote appears, put the current string into string[], clear the temporary string storage area, and continue scanning to findEndQuo(source line);

[0042] When the current line does not have a closing quote, store the line feed information and continue line traversal for the next line linefeedStr(sourceline).

[0043] Finally, strings[] is returned.

[0044] Furthermore, the method for preprocessing string literals is: when storing the string, the spaces connecting words inside the string are replaced with "-", and the strings are separated by spaces; if some source functions do not contain string information, this method will return a null value.

[0045] Specifically, the steps of extracting and preprocessing token sequence information are as follows: converting source code into token sequence; deleting all contents in the source code that are not related to the code semantics; performing word segmentation on the sorted source code; and standardizing the token sequence information; The specific steps of converting source code into token sequence are as follows:

[0046] First, input the source function fun;

[0047] Secondly, output the token information corresponding to the source function f ; This step includes:

[0048] Determine whether the current scanned string is a variable. If so, use var instead of rechangeVar(fun);

[0049] Determine whether the current scanned string is a number. If so, use dig to replace rechangeDig(fun);

[0050] Determine whether the current scanned string is a double symbol such as ++ or +=. If so, the two symbols are used as a token together, otherwise they are used as a single token process_symbol(fun);

[0051] Separate each token with a space and store it in saveToken().

[0052] Finally, return the token f .

[0053] Specifically, the lightweight open source tool Joern is used to extract and preprocess the control flow graph information; Joern can automatically parse and generate various code property graphs based on static source code blocks.

[0054] Step 200: extracting and preprocessing the binary code to obtain binary code information;

[0055] Specifically, the binary code information includes: string literals, token sequence information and structure information; the structure information is represented by a control flow graph.

[0056] Furthermore, the method for extracting string literals is: using IDA Pro to disassemble the binary code, and completing the work of extracting string literals by recording all string information in the "Strings" window in IDA Pro.

[0057] Specifically, the steps to extract and preprocess token sequence information are:

[0058] In the disassembly stage, use "-" to replace the original spaces to get the assembly code;

[0059] All integer values ​​are uniformly replaced with the "dig" identifier to reduce the impact of specific numerical values ​​in the assembly code on similarity analysis;

[0060] Replaced various general register names (such as "eax" and "ebx") with "reg".

[0061] Specifically, the steps to extract control flow graph information are:

[0062] Analyze the jump logic of the instruction through IDA Pro, divide the binary code into several basic blocks, and form the nodes of the control flow graph with the basic blocks;

[0063] Connect basic blocks according to the jump relationship between instructions to form the edges of the control flow graph;

[0064] By collecting the information of control flow graph nodes and edges, the control flow graph information is extracted and stored.

[0065] More specifically, the method for storing the control flow graph information is: recording the basic code block represented by each control flow graph node by storing it line by line; and using independent lines to describe the jump relationship between these nodes.

[0066] Step 300: constructing a source code semantic coding network and a binary code semantic coding network respectively, and encoding the source code information and the binary code information respectively to obtain a source code representation vector and a binary code representation vector;

[0067] like Figure 2As shown, specifically, the source code semantic encoding network is: the preset CodeBERT model. The CodeBERT model includes a word embedding module and a feature extraction module consisting of 12 encoding layers, wherein each encoding layer contains 12 self-attention units (size 64) and a fully connected layer. Among them, the grayscale circular graphics in the source code token sequence are the token sequence information of the source code, and the grayscale circular graphics in the word embedding module are vectors containing position information.

[0068] Specifically, the specific steps for encoding source code information are:

[0069] The token sequence information of the source code is input into the CodeBERT model, and the token sequence information is converted into a vector containing position information through the word embedding module;

[0070] Through the feature extraction module, the vector containing position information is converted into a representative feature vector.

[0071] Specifically, the specific steps to build a binary code semantic encoding network are:

[0072] Step 301: construct a binary sample pair data set according to the token sequence information; the specific steps of constructing the binary sample pair data set are:

[0073] Preset a pair of binary codes and , and They are and The code representation vector obtained after token embedding;

[0074] Calculate the cosine distance of the above two vectors; the calculation formula for cosine distance is:

[0075] ;

[0076] ;

[0077] Among them, Dis() is the feature distance, cosine() is the cosine distance, and Similarity() is the similarity detection value of the vector;

[0078] The threshold of positive and negative sample pairs is determined based on the cosine distance; the triplet of samples is expressed as ,in, is a similarity label, when When , the sample is a positive sample; when When 0, the sample is a negative sample; the thresholds for positive and negative sample pairs include: similarity threshold and dissimilarity threshold; the value of the similarity threshold is 0.8, and the value of the dissimilarity threshold is 0.3;

[0079] A binary sample pair dataset is constructed based on the positive and negative sample pair thresholds. The binary sample pair dataset is compiled using assembly code with the O0 and O3 optimization options of the GCC, ICC, and Clang compilers, and contains 125,692 pairs of positive and negative samples, including 62,352 pairs of positive samples and 63,340 pairs of negative samples.

[0080] Step 302: extract and merge features of the binary sample data set through the representation learning model to obtain feature vectors; the feature vectors include three modal feature vectors: string literal feature vectors, token sequence information feature vectors, and structure information feature vectors;

[0081] Step 303: optimizing the encoding of the feature vector through a network module based on contrastive learning to obtain an optimized vector; the network module based on contrastive learning includes: TextCNN, GCN and Bi-LSTM modules, which are used to reduce the distance between homologous binary code pairs and expand the distance between non-homologous pairs;

[0082] Furthermore, the method for optimizing the encoding of feature vectors is the triplet loss optimization method (Tripletloss); the specific implementation process of the triplet loss optimization method is: constructing a data triplet containing a base sample, a positive sample, and a negative sample, ensuring that the distance between the base sample and the positive sample is smaller than the distance between the base sample and the negative sample, and using the distance margin for verification; the calculation formula of the triplet loss optimization method is:

[0083] ;

[0084] Among them, B, P and N are triple samples respectively. , and The representation vector in space, margin is the interval margin used to separate positive and negative samples.

[0085] The calculation results are divided into three cases: Easy Triplets (loss is 0, no training required), Hard Triplets (the distance between the base sample and the positive sample is greater than the distance between the base sample and the negative sample, and the loss is greater than the margin), and Semi-Hard Triplets (the distance between the base sample and the positive sample is less than the distance between the base sample and the negative sample, but the margin is not met, and the loss is between 0 and the margin).

[0086] Furthermore, with source code And its corresponding two binary code versions and For example, and Similar, and Not similar; build a triple , and define the similarity detection task as follows:

[0087] ;

[0088] in, is the similarity measure, is the parameter to be optimized, is the training data set, is a general term for source code representation learning models. is a general term for binary code representation learning models, and β is a constant used to distinguish similar pairs from dissimilar pairs, which is 0.06.

[0089] Step 304: Obtain interval similarity results through vector concatenation, multi-layer perceptron processing and sigmoid function.

[0090] Step 400: performing feature fusion operations on the source code representation vector and the binary code representation vector respectively to obtain a source code fusion vector and a binary code fusion vector;

[0091] Specifically, feature fusion includes joint representation and vector direct concatenation.

[0092] Specifically, the specific calculation formula of the joint representation method is: ; Among them, f() is the fusion mechanism, x n is the modal feature vector generated by representation learning of the nth code information, x m is the fusion vector that completes feature fusion through joint representation.

[0093] Furthermore, the source code function and a target code snippet The method of obtaining the respective fusion vectors through the joint representation is the same as the source code function Take this as an example, the specific implementation process is:

[0094] Extract source code function tokens, character literals, and structural features, and through the adapted model , training to obtain the characteristic vectors of each mode, the formulas of this process are as follows:

[0095] ; ; ;

[0096] in, , and Source code The representation vectors generated by the adapted semantic encoding network of different modal information;

[0097] Fusion through joint representation , and Three vectors, the calculation formula is:

[0098] ;

[0099] in, Fusion vector for source code.

[0100] Specifically, the specific calculation formula of the vector direct splicing method is: ;in, It is the feature vector that completes feature fusion through vector concatenation method, with a dimension of 320. is the representation vector of string literal information, is the representation vector of token sequence information, is the representation vector of structural information.

[0101] Step 500: Perform similarity detection on the source code fusion vector and the binary code fusion vector to obtain a detection result.

[0102] Specifically, the specific steps of similarity detection are:

[0103] The source code representation vector and the binary code representation vector are aligned using the cosine similarity measurement method; the alignment operation is expressed as: ;

[0104] in, is the source code representation vector, is the binary code representation vector, for The projection function, for The projection function.

[0105] Calculate the source code representation vector Vector represented with binary code The specific calculation formula of code distance is: ;

[0106] Among them, x i is the i-th dimension source code representation vector, y i is the i-th dimension binary code representation vector, m is the number of dimensions of the representation vector;

[0107] The code distance is compared with the preset similarity threshold. If the code distance is greater than the similarity threshold, the source code is judged to be similar to the binary code; the similarity threshold is 0.8.

[0108] The beneficial effects of the present invention are as follows:

[0109] 1) It overcomes the limitation of traditional methods that only rely on code literals and sequence features;

[0110] 2) Improved the accuracy of the detection model;

[0111] 3) The accuracy and efficiency of similarity detection are improved.

[0112] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.

[0113] The present invention uses specific examples to illustrate the principles and implementation methods of the present invention. The above examples are only used to help understand the method and core ideas of the present invention. At the same time, for those skilled in the art, according to the ideas of the present invention, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as limiting the present invention.

Claims

1. A similarity detection method for open-ended programming questions, characterized in that: The steps include: Extract and preprocess the source code to obtain source code information; Extracting and preprocessing the binary code to obtain binary code information; the source code information and the binary code information both include: string literals, token sequence information and structure information; the structure information is represented by a control flow graph; A source code semantic coding network and a binary code semantic coding network are respectively constructed, and the source code information and the binary code information are respectively encoded to obtain a source code representation vector and a binary code representation vector; the specific steps of constructing the binary code semantic coding network are: Constructing a binary sample pair data set according to the token sequence information; the binary sample pair includes: a positive sample and a negative sample; Extracting and merging features of the binary sample data set through a representation learning model to obtain a feature vector; the feature vector includes three modal feature vectors: a string literal feature vector, a token sequence information feature vector, and a structure information feature vector; The feature vector is optimized and encoded by a network module based on contrastive learning to obtain an optimized vector; the network module based on contrastive learning includes: TextCNN, GCN and Bi-LSTM modules; Through vector concatenation, multi-layer perceptron processing and sigmoid function, the interval similarity result is obtained; Performing feature fusion operations on the source code representation vector and the binary code representation vector respectively to obtain a source code fusion vector and a binary code fusion vector; the feature fusion includes a joint representation method and a vector direct concatenation method; The specific calculation formula of the joint representation method is: m =f(x1, x2, x3, ..., x n ), where f( ) is the fusion mechanism, x n is the modal feature vector generated by representation learning of the nth code information, x m is the fusion vector for completing feature fusion through joint representation; The specific calculation formula of the vector direct splicing method is: s =[c tok ;c str ; C cfg ]; among them, Con s is the feature vector that completes feature fusion through vector concatenation method, c tok is the representation vector of string literal information, c str is the representation vector of token sequence information, c cfg is the representation vector of the control flow graph; A similarity test is performed on the source code fusion vector and the binary code fusion vector to obtain a test result.

2. The similarity detection method for open-ended programming questions according to claim 1, characterized in that: The source code semantic encoding network is: a preset CodeBERT model.

3. The similarity detection method for open-ended programming questions according to claim 1, characterized in that: The specific steps of the similarity detection are: Aligning the source code representation vector and the binary code representation vector by using a cosine similarity measurement method; Calculating the distance between the source code representation vector and the binary code representation vector to obtain a code distance; The code distance is compared with a preset similarity threshold, and if the code distance is greater than the similarity threshold, it is determined that the source code is similar to the binary code.

4. The similarity detection method for open-ended programming questions according to claim 3 is characterized in that: The specific calculation formula of the code distance is: Among them, x i is the i-th dimension source code representation vector, y i is the i-th dimension binary code representation vector, and m is the number of dimensions of the representation vector.