New alignment mode of code tracking sequences with same functions and different source codes

By combining the Niedermann-Wonsch algorithm and the semantic similarity evaluation of the SBERT model, the problem of alignment uncertainty and inaccuracy in code tracking alignment is solved, efficient and accurate code alignment is achieved, and the accuracy of the alignment results is enhanced. performance and recall.

CN118689752APending Publication Date: 2024-09-24XUANCAI INTERACTIVE NETWORK SCI & TECH +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410918064.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Priority Date
2023-09-18
Filing Date
2024-07-10
Publication Date
2024-09-24

AI Technical Summary

Technical Problem

Existing code tracking alignment methods have problems with alignment uncertainty and inaccuracy when processing codes with the same function but different source code, resulting in low-quality alignment results, especially incorrect alignment and lack of correct alignment caused by over-reliance on data.

Method used

A new alignment method is adopted, including obtaining the statement type, variable value and data dependency of the code running, preliminary alignment through the Niedermann-Wonsch algorithm, combined with the SBERT model to evaluate the semantic similarity of statements, and using the worklist algorithm for breadth The expanded alignment results are traversed first to ensure the accuracy and recall of the alignment.

Benefits of technology

It improves the accuracy and efficiency of code tracking alignment, reduces the number of iterations, avoids overfitting, enhances the correctness and recall rate of alignment results, and ensures the ordering and accuracy of alignment results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118689752A
    Figure CN118689752A
Patent Text Reader

Abstract

The invention provides a new alignment mode of code tracking sequences with the same function and different source codes, and belongs to the field of dynamic software analysis, and the code tracking sequence alignment mode comprises the steps of obtaining code tracking sequences, aligning sequence pairs as many as possible, aligning standard input sequence pairs and performing expansion alignment by taking the sequence pairs as reference. The method comprises the following steps: acquiring a code tracking sequence, using an llvm instrumentation tool, writing an instrumentation program, acquiring all information in code operation, aligning as many sequence pairs as possible, obtaining a longest sequence pair by using a Neidman-Merr algorithm, aligning a standard input sequence pair, expanding and aligning by taking the sequence pair as a benchmark, trying to align through a breadth-first traversal algorithm, and obtaining a sequence pair; according to the method, an alignment sequence is generated by using a pre-training model using a twin network instead of a simple variable data change to calculate whether the alignment is performed by using a sensor-bert, hereinafter referred to as SBERT, so as to generate an alignment sequence, so that a wrong alignment result can be eliminated, and a part of sequence pairs which are originally aligned but not aligned can be aligned.
Need to check novelty before this filing date? Find Prior Art

Description

[0001] The present invention relates to the field of program analysis, including program dynamic analysis and program static analysis, and in particular to the field of program dynamic analysis. Background Art

[0002] Program analysis refers to the process of understanding, evaluating, and improving software programs by applying various techniques and tools. Program analysis aims to discover and solve problems in programs and improve the quality, performance, and security of programs. Program analysis is mainly divided into static analysis and dynamic analysis. Static analysis is the process of analyzing the static representation of program source code or binary code, which can help developers find problems before compiling and running, and provide suggestions for improving code quality. Dynamic analysis is the process of monitoring, controlling, and analyzing the behavior of a program while it is running. Dynamic analysis can help developers understand the behavior of the program, optimize performance, and debug problems.

[0003] In general, code-tracing alignment methods do not confirm the correctness of the results, assume that the initial sequence alignments must be correct, and use them as baselines for alignment.

[0004] However, due to various differences in the specific implementation of the code, it is impossible for all codes to be ideal, so this alignment method is very prone to errors. When the initial alignment sequence is wrong, chain subsequent alignment errors will occur.

[0005] When two versions of code traces have the same symbolic expression somewhere, but the two versions do not have the same function, after alignment, the general alignment method will use this as a baseline to continue sequence alignment, which is undoubtedly catastrophic. This will produce an uncertain number of incorrect alignments and the lack of correct alignments, because cross-alignment is not allowed in code traces and the alignment results must be sequential.

[0006] In addition, the main dependency of some other alignment methods is still data-driven. Data is very important in code running, but over-reliance on data is inaccurate. When variables with the same data appear multiple times in one version, alignment uncertainty also follows, which is very common in the code writing process. This alignment uncertainty is due to data-driven reasons. Only the first code trace is selected for alignment. This is an irresponsible alignment method and cannot guarantee correctness. Through experiments, it is found that even if the code is written by different people, the order of their input statements must be the same. In addition, the form of code statements with the same function is also similar. Therefore, this experiment uses the input statement as the benchmark for alignment and adds source code similarity judgment. These two methods not only increase the accuracy of the alignment results, but also increase the recall rate of correct alignment.

[0007] At present, the method for aligning code tracking sequences with the same function but different source codes is to obtain the variable data of the code tracking, take the symbolic expression of the data as the main parameter, directly compare the parameters, and align according to the comparison results. Although this method is simple, it cannot solve the uncertainty of alignment, nor can it guarantee the accuracy of alignment, resulting in low-quality alignment results. Summary of the invention

[0008] The purpose of the present invention is to provide a new alignment method for code tracking sequences with the same functions but different source codes in view of the deficiencies of the prior art.

[0009] The object of the present invention is achieved by the following technical solution: a new alignment method for tracking code sequences with the same function but different source codes, the method comprising the following steps: S1. Obtain the code tracking sequence during the source code execution process, including the statement type, variable value, and data dependency and control dependency of each line of code; S2. Preliminary alignment of code sequences: Sequence alignment is performed while ensuring a balance between precision and recall. The preliminary alignment of sequences only considers symbolic expressions. S3, code sequence optimization alignment: add some correct alignments and eliminate some wrong alignments through data dependency and control dependency of standard input; S4, code sequence determination alignment: use SBERT to eliminate incorrect alignments for the aligned sequence pairs; S5. Code sequence expansion alignment: Using the determined alignment sequence as a benchmark, the worklist algorithm is used to drive the breadth-first traversal to expand the alignment results.

[0010] Furthermore, in step S1, a pass file is manually written to perform a stub operation on the intermediate code in the code running, annotate each running instance of each sentence of code with its id and running times, and record the key variable values ​​of the code. The pass level of this step is Instruction Pass, so that it can operate and analyze the IR of the code at the instruction level, and can collect data information at the instruction level.

[0011] Furthermore, in step S2, in order to ensure the accuracy and recall rate of the initial alignment as much as possible, we adopted the idea of ​​the Needleman-Wunsch algorithm and used its variant to complete the first step of alignment. The two sequences generated by the correct version and the wrong version in step S1 are used as input, a two-dimensional score matrix is ​​constructed according to the sequence length, and the scores are assigned according to the data information of the code tracking. After the positive sequence is assigned, the score matrix is ​​traced back to obtain the initial alignment sequence.

[0012] Furthermore, in step S3, in all algorithm problems driven by test cases, the input order of the test cases must be the same, and this property is very important for our subsequent operations. Control flow dependency refers to the conditional branches and loop structures in program execution, which determine which statements in the program will be executed and the order of execution. In algorithm problems, the sameness of input data ensures that the control of the input data is equivalent. Therefore, in this step, all standard input statements are sorted according to time, and their control dependencies are aligned according to the sorting as the output of S3.

[0013] Furthermore, in step S4, in order to solve the misalignment caused by over-reliance on data, the structural similarity of codes with similar functions is taken into account. Experience tells us that different people write codes to solve the same problem in a thousand different ways; but this is a macroscopic difference. In the alignment with a granularity of a single statement, the differences in the code are deeply diluted, and single codes with the same function always have a high degree of similarity. Specifically, the code structure of the assignment statement must be variables=?, that is, variable name=variable value. The loop statement must be for(…) or while(…), and the branch statement must be if(…) or while(…). Therefore, in our task with a granularity of a single statement, the source code of the two aligned must be very similar. In order to increase the weight of source code attributes in alignment, we judge the semantic similarity of the source code of a single statement. In the first attempt, the edit distance was used as the basis for judging similarity. However, in some cases, the naming length of the variable may occupy the vast majority of the proportion in the statement, and the importance of semantic information in the edit distance is weakened. In order to focus on semantic information rather than changes in word letters, we used SBERT as a prediction model for semantic information similarity. SBERT is a pre-trained model that calculates sentence semantic similarity. In order to make it more suitable for source code semantic similarity inference, we used 3462 pairs of code sentence alignment for fine-tuning. In fact, in code language, the simplicity of language structure makes it possible to obtain good results even with very little data.

[0014] Beneficial effects of the present invention: The present invention uses a new method to align code traces with the same function but different source codes. Unlike other alignment methods, a deep learning model is used to evaluate the similarity of the source codes. Benchmarks are generated based on input statements to avoid incorrect alignment of control flows of different functions. The deep learning model reduces the number of iterations of the alignment process and increases the alignment efficiency. Alignment based on source code properties prevents overfitting caused by over-reliance on variable information. BRIEF DESCRIPTION OF THE DRAWINGS

[0015] Figure 1It is a flow chart of a new alignment method for code tracking sequences with the same functions but different source codes according to the present invention.

[0016] Figure 2 It is a scoring process of a new alignment method for code tracking sequences with the same function but different source codes according to the present invention.

[0017] Figure 3 The invention discloses a new alignment method for obtaining an optimized alignment process for code tracking sequences with the same functions but different source codes.

[0018] Figure 4 The invention relates to a final expansion alignment process based on optimized alignment of code tracking sequences with the same function but different source codes. DETAILED DESCRIPTION

[0019] The specific embodiments of the present invention are further described in detail below in conjunction with the accompanying drawings.

[0020] like Figure 1 As shown, the present invention provides a new method for aligning code traces with the same function but different source codes, and the steps are as follows: S1. Instrument the source code. Instrumentation can insert additional instructions or logic into the code without modifying the source code. In this step, write a pass file to achieve the function of monitoring the code execution process. We choose to use LLVM to complete this step. In this step, after the code is instrumented and run, the generated file storage includes: 1. The instruction type of the source code, including: STIns: assignment statement; BRIns: branch statement; SCANF: standard input statement; PRINT: output statement, 2. The value of the variable value, 3. The symbolic expression expr of the variable; 4. The data dependency ddep of the statement; 5. The control dependency cdep of the statement; 6. The number of executions t of the statement; 7. The number s of the statement; 8. The source code src of the statement.

[0021] S2. Perform preliminary alignment on the code trace. In this step, the Needleman-Wunsch algorithm driven by a scoring mechanism is used. In the scoring mechanism, there are three scoring situations, namely: 1. Different variable values. In this case, it is considered impossible to align the two, and this situation is recorded as 0 points; 2. The same variable value and different symbolic expression. In this case, it is considered that the two may be aligned, but the possibility of alignment is not high, and 1 point is recorded; 3. The same variable value and the same symbolic expression. In this case, it is considered that the possibility of alignment between the two is relatively high. In addition to the assignment statement, there is an assignment logic of the operation. When the symbolic expression is the same, the possibility of alignment between the two is very high. In this case, 2 points are recorded. The scoring process is attached. Figure 2After the score matrix is ​​calculated, the score matrix is ​​backtracked from bottom to top to calculate the initial alignment result.

[0022] S3. Filter the initial alignment to obtain the optimized alignment. In algorithm questions, the input of the questions must be the same. Because the order of the test cases is determined, the standard input statement is determined, and it is more obvious in the case of loop input. By filtering all the tracking information, all the input statements are filtered out and sorted in chronological order. The input statements that appear in the same order in the two versions have the same real input, and we align them. Secondly, in most cases, the input statements are controlled by loop statements and run multiple times. The control dependencies of the input statements are aligned with a probability of 100% when they are branch statements. If there is a conflicting alignment result in the initial alignment result and the next step, it is unconditionally considered that the initial alignment is wrong and should be replaced by this alignment result. After addition and replacement, the optimized alignment result is generated. See the attached for the optimized alignment process. Figure 3 .

[0023] S4. Calculate the optimized alignment result through the deep learning model to obtain a definite alignment.

[0024] S4-1. The optimized alignment results process the content directly related to the input sentences. Because the initial alignment cannot guarantee the correctness of the alignment, there are still many sentences that are not directly related to the input sentences. Use SBERT to calculate the similarity of the sentence source code. In addition, in order to make SBERT focus on the similarity results of the code language, 3642 pairs of code sentences are used to fine-tune SBERT, and the similarity labels are between 0 and 1. Due to the simplicity of the code language, 3642 pairs of data are enough to fine-tune SBERT to achieve a high accuracy in the downstream task of code similarity. Two codes are input into SBERT, and the output is the similarity of the two codes.

[0025] S4-2, similarity threshold control. After SBERT obtains the similarity of two codes, 0.6 is used as the threshold to classify the same and different. During the experiment, five different thresholds were defined, ranging from 0.4, 0.5, 0.6, 0.7, and 0.8. After experimental verification, the threshold of 0.6 can take into account both precision and recall, and generate the sequence result after optimization alignment. Of course, other thresholds can also be used to handle other specific types of tasks.

[0026] S5. Perform final expansion alignment based on optimized alignment.

[0027] S5-1. In the expansion alignment, a data structure queue is used to store the aligned results. In the expansion step, the head elements are taken out of the queue one by one, and the control dependency and data dependency of the head element are aligned. This step still uses the similarity between the variable value and the source code as the basis for judgment. If its dependency is aligned, the dependency is newly added to the queue. The algorithm is controlled by the change in queue length. When the queue length no longer changes, it means that there is no data that can be added to the queue, that is, it reaches a steady state. This algorithm uses a simple breadth-first traversal algorithm. The process of this step is attached. Figure 4 .

[0028] S5-2. In this step, the same SBERT model and the same parameters as the alignment filter are used to control alignment. As with the alignment filter, the threshold is still set to 0.6. Similarly, different threshold parameters can be set to achieve different task requirements.

Claims

1. A new alignment method for code tracking sequences with the same function but different source codes, characterized in that: The sequence alignment method comprises the following steps: S1. Obtain the code tracking sequence during the source code execution process, including the statement type, variable value, and data dependency and control dependency of each line of code; S2. Preliminary alignment of code sequences: Align the sequences while balancing precision and recall; S3, code sequence optimization alignment: add some correct alignments and eliminate some wrong alignments through data dependency and control dependency of standard input; S4. Code sequence determination alignment: Use SBERT to eliminate incorrect alignments for the aligned sequence pairs; S5. Code sequence expansion alignment: Using the determined alignment sequence as a benchmark, the worklist algorithm is used to drive the breadth-first traversal to expand the alignment results.

2. A new alignment method for code tracking sequences with the same function but different source codes according to claim 1, characterized in that: In the step S1, the code tracking sequence is obtained and the code is instrumented using LLVM to obtain operation information during operation. During the instrumentation process, input statements, assignment statements and branch statements are recorded emphatically, which play a key role in code operation.

3. A new alignment method for code tracking sequences with the same function but different source codes according to claim 1, characterized in that: In step S2, in order to obtain sequence pairs that take into account both precision and recall, a Needleman-Wunsch algorithm driven by a scoring mechanism is used for preliminary alignment, wherein the scoring mechanism is controlled by variable values ​​and symbolic expressions, and the scoring types are divided into three types: different numerical values, the same numerical values ​​but different symbolic expressions, and the same numerical values ​​and the same symbolic expressions.

4. A new alignment method for code tracking sequences with the same function but different source codes according to claim 1, characterized in that: In step S3, the obtained sequence pair content is ([scanfA1.cdep,scanfB1.cdep],[scanfA1.ddep,scanfB1.ddep], ···,[scanfA3.cdep,scanfB3.cdep], [scanfA3.ddep,scanfB3.ddep], ···), where scanfA1 and scanfB1 represent the first standard input in version A and version B respectively, the subscript "1" represents the execution order of the current statement, cdep represents the direct control statement of the current statement, and ddep represents the direct data statement of the current statement.

5. A new alignment method for code tracking sequences with the same function but different source codes according to claim 1, characterized in that: In step S4, the model SBERT is used to predict the alignment result of the code sequence, and the threshold is set to 0.

6. m .src, tarceB n .src) > 0.6 is considered an alignment pair, where traceA m .src is the source code of the mth trace in version A, traceB n .src is the source code of the nth trace in version B. They are input into the SBERT model as input, and the output Similarity is their similarity. Only when the similarity is greater than 0.6 is it considered aligned.

6. A new alignment method for code tracking sequences with the same function but different source codes according to claim 1, characterized in that: In step S5, a breadth-based traversal algorithm driven by a worklist is used. In each iteration, a pair of instances are popped out from the worklist and their control dependencies and data dependencies are compared. If the similarity exceeds the threshold, it is added to the worklist again. When the length of the worklist does not change in one iteration, the process is terminated and is considered to have reached a steady state. In this step, SBERT is still used to predict the results and decide whether to add the data to the worklist.

7. A new alignment method for code tracking sequences with the same function but different source codes according to claim 1, characterized in that: The alignment method uses program instrumentation, Needleman-Wunsch algorithm, alignment benchmark controlled by input statements, alignment results predicted by deep learning models, and worklist-driven expansion algorithm.