Method, device, storage medium and electronic device for extracting code loop counter

By generating a control flow graph and extracting potential monotonic scalar data using depth-first search and constraint solving methods, the problem of inaccurate extraction of loop counters in existing technologies is solved, thereby improving the accuracy and efficiency of software security detection.

CN115438341BActive Publication Date: 2026-07-21BEIHANG UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
BEIHANG UNIV
Filing Date
2022-09-06
Publication Date
2026-07-21

AI Technical Summary

Technical Problem

Existing technologies for extracting loop counters from loop code have low accuracy, resulting in low correctness and efficiency of static analysis of software code, which affects the security and stability of the software.

Method used

By parsing the code, a control flow graph is generated, and the constant data of the loop code structure is obtained. The potential monotonic scalar data is extracted using depth-first search and constraint solving methods, and the loop counter is obtained by combining the loop exit information.

Benefits of technology

It improves the accuracy of loop counter identification, enhances the accuracy and completeness of software security testing, and improves the accuracy and efficiency of static analysis of software code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115438341B_ABST
    Figure CN115438341B_ABST
Patent Text Reader

Abstract

The present application relates to a kind of extracting code loop counter method, device, storage medium and electronic equipment, belong to safety detection technical field.The method of the present application, steps are as follows: analyzing code is analyzed;Generation control flow graph and obtain loop code structure;Obtain the constant data set in loop code structure;Based on constant data set, obtain scalar constant data set;Obtain potential monotonic scalar constant data set;Obtain monotonic scalar constant data set;Obtain loop definition exit;Obtain loop jump statement exit;Based on loop exit set and monotonic scalar constant data set, obtain loop counter.Through the above steps, the extraction of loop counter can be completed.The present application can accurately and completely obtain loop counter set, provide support for carrying out loop code structure itself related defect check, also make complete path traversal with the code with loop structure possible, improve the efficiency and accuracy of detection.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of security detection technology and relates to a method, apparatus, storage medium, and electronic device for extracting a code loop counter. Background Technology

[0002] With the continuous development of artificial intelligence in modern society, the complexity of software is increasing, and the amount of code is also growing. Therefore, complex software presents new challenges to the security, reliability, and stability of software systems. Static code analysis is one of the best practices for solving software security problems because it can detect defects in software earlier at a lower cost, and is therefore widely used in the field of software testing. In code defect databases, the confirmation of many typical code defects, such as buffer overflows, out-of-bounds access, SQL injection, and memory leaks, requires the application of path traversal techniques during static analysis. Based on whether the data traversed by the path is "computed," path traversal is usually divided into two categories: data-independent path traversal and data-dependent path traversal. Data-dependent path traversal, in addition to analyzing the path's inherent attributes (such as source and destination), also processes and analyzes the data traversed by the path, and the path's inherent attributes may be affected by the data traversed. In comparison, data-dependent path traversal can abstractly simulate code execution in a static state, thus enabling a deeper level of code analysis.

[0003] Complex software typically contains loop code structures. These structures can lead to excessively long paths, a massive number of paths, abstract paths, and unknown paths, making the full path traversal detection and analysis methods used in current software security testing extremely difficult. This negatively impacts the accuracy and completeness of software code security analysis, resulting in low analysis efficiency and poor software reliability and stability. Therefore, correctly extracting loop counters from software code is crucial for improving the security of complex software.

[0004] Existing methods for extracting loop counters typically rely solely on variable analysis at the loop entry point to determine the loop counter. This results in inaccurate loop counters, ultimately leading to failure in static analysis of the software code and reduced software security. Summary of the Invention

[0005] In view of the above analysis, the present invention provides a method, apparatus, storage medium and electronic device for extracting a loop counter from a loop code, in order to solve the problem of low accuracy in extracting a loop counter from a loop code in the prior art.

[0006] On one hand, the present invention provides a method for extracting a code loop counter, specifically including the following steps:

[0007] S1. Parsing the code being analyzed:

[0008] The code is analyzed to obtain the functions, variable data, and keyword metadata within those functions; a set of code metadata is obtained from the functions, variable application data, and keyword metadata.

[0009] S2. Generate the control flow graph and obtain the loop code structure:

[0010] Based on the code metadata set, code block analysis is performed on keyword metadata of functions to build a code block tree; depth-first traversal is performed on the code block tree to construct the control flow graph of the function; the control flow graph is traversed to obtain the loop code structure in the function;

[0011] S3. Obtain the set of constant data for the loop code structure:

[0012] Based on the code metadata set, analyze the loop definition data and variable application data in the variable data of the loop code structure to obtain the fixed value data in the loop code structure, and obtain the fixed value data set from the fixed value data;

[0013] S4. Obtaining a scalar data set based on a fixed-value data set:

[0014] Iterate through the set of fixed-value data to obtain the data type of the fixed-value data; if the data type is numeric, the corresponding fixed-value data is scalar data; obtain the set of scalar data from the scalar data;

[0015] S5. Obtain the potential monotonic scalar data set:

[0016] Retrieve scalar data from the scalar data set; execute a loop based on the control flow graph and code metadata set. The loop process is as follows: starting from the loop entry point of the loop code structure, find the corresponding node in the control flow graph, and traverse the control flow graph using a depth-first search method for one loop; during each loop execution, record the initial value of the scalar data before the loop (data(x,n,0)) and the final value after the loop (data(x,n,1)). The difference between the scalar data after the nth loop and before the loop is gap(x,n) = data(x,n,1) - da. ta(x,n,0), where x is scalar data and n is the nth iteration of the scalar data x; obtain the scalar data difference set set(x) = {gap(x,1),gap(x,2),…,gap(x,n)} from the iteration difference gap(x,n); determine whether the positive and negative attributes of each gap(x,n) in the data difference set set(x) are the same through the constraint solving method. If they are the same, the corresponding scalar data is the latent monotonic scalar data. Traverse the scalar data in the scalar data set to obtain the latent monotonic scalar data set;

[0017] S6. Obtain the set of monotonic scalar constant values.

[0018] Retrieve latent monotone scalar data from the latent monotone scalar data set; abstract the latent monotone scalar data into built-in identifier values; use the built-in identifier values ​​as the initial data abs_data(x,1,0) for the loop, execute the first loop based on the code metadata set and control flow graph, and obtain the final value abs_data(x,1,1) after the loop. The difference between the latent monotone scalar data before and after the first loop is abs_gap(x,1) = abs_data(x,1,1) - abs_data(x,1,0). Use the constraint solving method to determine whether the positive and negative attributes of abs_gap(x,1) and gap(x,1) are the same. If they are the same, execute the second loop; the initial data of the latent monotone scalar data in the second loop... Given the value abs_data(x,2,0) = abs_data(x,1,1), the second loop is executed based on the code metadata set and control flow graph to obtain the final value abs_data(x,2,1) after the loop. The difference between the loop before and after the second loop in the monotonically scalar data is obtained as abs_gap(x,2) = abs_data(x,2,1) - abs_data(x,2,0). It is then determined whether the positive and negative attributes of abs_gap(x,2) and gap(k,1) are the same. If they have the same positive and negative attributes, the potential monotonically scalar fixed-value data is monotonically scalar data. All potential monotonically scalar data in the potential monotonically scalar data set are traversed to obtain the monotonically scalar data set.

[0019] S7. Obtain the loop definition exit based on the loop definition data;

[0020] S8. Obtain the loop exit set

[0021] The exit statements of the loop code structure are obtained from the code metadata set. The expression corresponding to the exit statement is the exit condition of the loop code structure. The statement exit set is obtained from the exit statement. The loop exit set is obtained from the loop definition exit and the statement exit set.

[0022] S9. Obtain the set of loop counters based on the set of loop exits and the set of monotonic scalar constant values.

[0023] Traverse the loop exit set to obtain the exit data used by the exit statement and exit expression. The exit data constitutes the exit data set. Take the intersection of the exit dataset and the monotonic scalar set of the contract monotonic scalar set. The data in the intersection is the loop counter. Thus, the loop counter set is obtained.

[0024] Optionally, the specific process of building the code block tree in step S2 is as follows: code blocks are formed by combining the code in the keyword metadata of the function; first-level code blocks in the function are obtained; second-level code blocks in the first-level code blocks are obtained; child code blocks are obtained in sequence according to the hierarchy and order of the code blocks until completion; and a code block tree is built from the obtained child code blocks.

[0025] Optionally, the loop definition data includes the starting line number, ending line number, loop entry point, entry condition, entry line number, and loop code structure type information of the loop code structure.

[0026] Optionally, the specific process of step S3 is as follows: obtain the starting line number of the loop code structure from the loop definition data; find the ending line number, loop entry, entry condition, entry line number and loop code structure type information of the loop code structure based on the starting line number of the loop code structure; determine the range of code lines contained in the loop code structure based on the information; obtain variable application data from the range of code lines; filter out fixed value data from the variable application data; obtain a set of fixed value data from the fixed value data.

[0027] Optionally, the set value data is the data to be assigned a value.

[0028] Optionally, step S4 consists of the following steps:

[0029] S41. Obtain the set of constant value data;

[0030] S42. Determine if there are any untraversed fixed-value data in the fixed-value data set; if not, end the acquisition of scalar data and obtain the scalar data set; if so, take out one untraversed fixed-value data in sequence and proceed to the next step.

[0031] S43. Convert the string of the untraversed fixed value data into a binary tree expression; analyze the binary tree expression to obtain the data type of the untraversed fixed value data;

[0032] S44. Determine if the data type is numeric; if not, return to S42; if yes, determine the fixed value data corresponding to the data type as scalar data and proceed to the next step.

[0033] S45. Place the scalar data into the scalar data set; return to S42.

[0034] Optionally, step S7 is as follows:

[0035] Loop definition data is obtained based on the code metadata set; the loop definition entry condition is extracted from the loop definition data, and the loop entry condition expression is in_exp(0); the loop definition exit is obtained by performing a NOT operation on the loop entry condition, and the loop definition exit statement expression out(0) = ! in_exp(0) is obtained by adding a NOT operator before the loop entry condition expression.

[0036] On the other hand, the present invention also provides an apparatus for extracting loop counters from loop code, the apparatus comprising:

[0037] The code parsing module is used to analyze the code being analyzed, and to obtain the functions, variable data, and keyword metadata within those functions.

[0038] The control flow graph generation module is used to generate the control flow graph of the functions of the analyzed code and to obtain the loop code structure in the functions;

[0039] The loop counter extraction module is used to extract loop counters from the control flow graph of the function being analyzed, generated by the control flow graph generation module, and to obtain the loop code structure within the function.

[0040] Thirdly, the present invention also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the aforementioned method.

[0041] Finally, the present invention also provides an electronic device, comprising: a memory having a computer program stored thereon; and a processor for executing the computer program in the memory to implement the steps of the aforementioned method.

[0042] Compared with the prior art, the present invention can achieve the following beneficial effects:

[0043] (1) The present invention can improve the accuracy of identifying the loop counter in the loop body, thereby improving the accuracy and completeness of the software security detection method.

[0044] (2) When acquiring the loop counter, the present invention combines the loop exit information and the value sequence characteristics corresponding to each loop counter. The acquired loop counter contains type feature information, which makes full path traversal possible, thereby improving the accuracy and completeness of static analysis of software code. Attached Figure Description

[0045] The accompanying drawings are for illustrative purposes only and are not intended to limit the scope of the invention.

[0046] Figure 1 This is an overall flowchart of the present invention;

[0047] Figure 2 This is the control flow graph of the analyzed code in Embodiment 1 of the present invention;

[0048] Figure 3 This is a flowchart illustrating the process of obtaining a scalar set of values ​​in Embodiment 1 of the present invention.

[0049] Figure 4 This is a flowchart illustrating the process of obtaining a set of monotonic scalar constants in Embodiment 1 of the present invention.

[0050] Figure 5 This is a flowchart of obtaining the loop exit set in Embodiment 1 of the present invention. Detailed Implementation

[0051] The present invention will now be described in detail with reference to the accompanying drawings and specific embodiments.

[0052] A specific embodiment of the present invention, such as Figure 1-5 A method for extracting code loop counters is disclosed, which specifically includes the following steps:

[0053] S1. Parsing the code being analyzed:

[0054] Lexical analysis, syntactic analysis, and / or semantic analysis are performed on the analyzed code to obtain the functions, variable data, and keyword metadata within those functions. A set of code metadata is then obtained from the functions, their variable application data, and the keyword metadata. The variable data includes the definition data of the code structure and the variable application data.

[0055] S2. Generate the control flow graph and obtain the loop code structure:

[0056] Based on the code metadata set, code block analysis is performed on a function-by-function basis. Code blocks are formed by combining code from keyword metadata, and a code block tree is built. A depth-first traversal is performed on the code block tree to construct the control flow graph of the function. The control flow graph is then traversed to obtain the loop code structure in the function.

[0057] The specific process of building a code block tree is as follows: code blocks are formed by combining the code in the keyword metadata of the function; the first-level code block in the function is obtained; the second-level code block in the first-level code block is obtained; the child code blocks are obtained in sequence according to the hierarchy and order of the code blocks until the process is completed; and the code block tree is built from the obtained child code blocks.

[0058] S3. Obtain the set of constant data for the loop code structure:

[0059] Based on the code metadata set, the loop definition data and variable application data of the loop code structure are analyzed to obtain the fixed value data in the loop code structure, and the fixed value data set is obtained from the fixed value data. Among them, the loop definition data includes the starting line number, ending line number, loop entry, entry condition, entry line number and loop code structure type information of the loop code structure.

[0060] The specific process is as follows: Obtain the starting line number of the loop code structure from the loop definition data; based on the starting line number, find the ending line number, loop entry point, entry condition, entry line number, and loop code structure type information; determine the range of code lines contained in the loop code structure based on this information; obtain variable application data from the code line range; filter out fixed-value data from the variable application data; and obtain a set of fixed-value data from the fixed-value data. The fixed-value data consists of the data to be assigned values.

[0061] S4. Obtaining a scalar data set based on a fixed-value data set:

[0062] Traverse the set of fixed-value data and convert the fixed-value data in the set into a binary tree expression; analyze the binary tree expression to obtain the data type of the fixed-value data; if the data type is numeric, the corresponding fixed-value data is scalar data; obtain the scalar data set from the scalar data.

[0063] Preferably, if the data type is a non-numeric type, no processing is performed on the non-numeric type.

[0064] It is understandable that int, float, and double are numeric types.

[0065] The specific steps are as follows:

[0066] S41. Obtain the set of constant value data;

[0067] S42. Determine if there are any untraversed fixed-value data in the fixed-value data set; if not, end the acquisition of scalar data and obtain the scalar data set; if so, take out one untraversed fixed-value data in sequence and proceed to the next step.

[0068] S43. Convert the string of the untraversed fixed value data into a binary tree expression; analyze the binary tree expression to obtain the data type of the untraversed fixed value data;

[0069] S44. Determine if the data type is numeric; if not, return to S42; if yes, determine the fixed value data corresponding to the data type as scalar data and proceed to the next step.

[0070] S45. Place the scalar data into the scalar data set; return to S42.

[0071] S5. Obtain the potential monotonic scalar data set:

[0072] Retrieve scalar data from the scalar data set; execute a loop based on the control flow graph and code metadata set. The loop process is as follows: starting from the loop entry point of the loop code structure, find the corresponding node in the control flow graph, and traverse the control flow graph using a depth-first search method for one loop; during each loop execution, record the initial value of the scalar data before the loop (data(x,n,0)) and the final value after the loop (data(x,n,1)). The difference between the scalar data after the nth loop and before the loop is gap(x,n) = data(x,n,1). -data(x,n,0), where x is scalar data and n is the nth iteration of the scalar data x; obtain the scalar data difference set set(x) = {gap(x,1),gap(x,2),…,gap(x,n)} from the iteration difference gap(x,n); determine whether the positive and negative attributes of each gap(x,n) in the data difference set set(x) are the same through the constraint solving method. If they are the same, the corresponding scalar data is the latent monotonic scalar data. Traverse the scalar data in the scalar data set to obtain the latent monotonic scalar data set.

[0073] S6. Obtain the set of monotonic scalar constant values.

[0074] Retrieve latent monotone scalar data from the latent monotone scalar data set; abstract the latent monotone scalar data in the latent monotone scalar data set into built-in identifier values; use the abstracted latent monotone scalar data built-in identifier values ​​as the initial data abs_data(x,1,0) for the loop, execute the first loop based on the code metadata set and control flow graph, and obtain the final value abs_data(x,1,1) after the loop. The difference between the latent monotone scalar data before and after the first loop is abs_gap(x,1) = abs_data(x,1,1) - abs_data(x,1,0). Use the constraint solving method to determine whether the positive and negative attributes of abs_gap(x,1) and gap(x,1) are the same. If they are the same, execute the second loop; the second... The initial value of the potential monotonically scalar data in the loop is abs_data(x,2,0) = abs_data(x,1,1). Based on the code metadata set and control flow graph, a second loop is executed to obtain the final value abs_data(x,2,1). The difference between the loop before and after the second loop in the monotonically scalar data is abs_gap(x,2) = abs_data(x,2,1) - abs_data(x,2,0). It is then determined whether abs_gap(x,2) and gap(k,1) have the same positive or negative attribute. If they do, the potential monotonically scalar data is considered monotonically scalar data. All potential monotonically scalar data in the potential monotonically scalar data set are traversed to obtain the monotonically scalar data set. By abstracting the data and performing two consecutive loops, the relevant characteristics of the potential loop counter can be accurately determined, thus enabling the rapid extraction of the loop counter.

[0075] S7, Based on obtaining the loop definition exit

[0076] Each loop code structure has a loop definition entry point. When the entry condition is not met, the loop code structure will exit. The negation of the entry condition is called the loop definition exit point. Each loop code structure has only one loop definition exit point.

[0077] Loop definition data is obtained from the code metadata set; the loop definition entry condition is extracted from the loop definition data, and the loop entry condition expression is in_exp(0); the loop definition exit is obtained by performing a NOT operation on the loop entry condition, and the loop definition exit statement expression out(0) = ! in_exp(0) is obtained by adding a NOT operator before the loop entry condition expression.

[0078] S8. Obtain the loop exit set

[0079] The exit statements of the loop code structure are obtained from the code metadata set. The expression corresponding to the exit statement is the exit condition of the loop code structure. The exit statement is used to obtain the statement exit set. The loop exit set is obtained from the loop definition exit and the statement exit set.

[0080] Specifically, the loop code structure includes break and return exit statements.

[0081] The break statements of the loop code structure are obtained based on the code metadata set. The conditional expressions corresponding to the break statements are all the exit conditions of the loop code structure. The exit set of break statements is obtained as out_break_set.

[0082] The return statements of the loop code structure are obtained based on the code metadata set. The conditional expressions corresponding to the return statements are all the exit conditions of the loop code structure. The exit set of return statements is obtained as out_return_set.

[0083] The loop exit set consists of the loop definition exit, the break statement exit set, and the return statement exit set, and is defined as loop_out_set = {out(0), out_break_set, out_return_set}.

[0084] S9. Obtain the set of loop counters based on the set of loop exits and the set of monotonic scalar constant values.

[0085] Traverse the loop exit set, convert the exit statement expression into a binary tree expression, analyze the binary tree expression, obtain the exit data used by the exit statement and exit expression, construct an exit data set from the exit data, take the intersection of the exit dataset and the monotonic scalar constant value data set, the data in the intersection is the loop counter, thus obtaining the loop counter set.

[0086] Specifically, the loop exit set is traversed, and the loop definition exit statement expression, break statement exit expression, and return statement exit expression are converted into binary tree expressions. The binary tree expressions are analyzed to obtain the exit data set used by the loop definition exit statement expression, break statement exit expression, and return statement exit expression. The exit dataset is intersected with the monotonic scalar set of the fixed value data set. The data in the intersection is the loop counter, thus obtaining the loop counter set.

[0087] On the other hand, the present invention also provides an apparatus for extracting loop counters from loop code, which is used to extract loop counters from loop code. Using the aforementioned extraction method, the apparatus includes:

[0088] The code parsing module is used to analyze the code being analyzed, and to obtain the functions, variable data, and keyword metadata within those functions.

[0089] The control flow graph generation module is used to generate the control flow graph of the functions of the analyzed code and to obtain the loop code structure in the functions;

[0090] The loop counter extraction module is used to extract loop counters from the control flow graph of the function being analyzed, generated by the control flow graph generation module, and to obtain the loop code structure within the function.

[0091] Thirdly, the present invention also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the aforementioned method.

[0092] Meanwhile, the present invention also provides an electronic device, comprising: a memory storing a computer program thereon; and a processor for executing the computer program in the memory to implement the steps of the aforementioned method.

[0093] Example 1

[0094] Taking program segment (a) as an example, program segment (a) is as follows:

[0095]

[0096]

[0097] The loop counter is extracted from the loop code in program segment (a) using the following steps:

[0098] Step 1: Analyze code segment (a):

[0099] Lexical analysis, syntax analysis, and semantic analysis are performed on code segment (a) to obtain the functions, variable application data, and keyword metadata of code segment (a); a set of code metadata is obtained from the functions, variable application data, and keyword metadata.

[0100] Since there are many functions in the metadata collection, this example uses the metadata of the while keyword. The line numbers in the metadata of the while keyword correspond to the line numbers in the formatted file, as shown in Table 1.

[0101] Table 1. Metadata for keywords during...

[0102] Serial Number name value 1 serial number 1 2 Starting line number 15 3 Entry expression line number 15 4 Loop body start line 16 5 Loop body termination line 23 6 Entry condition expression i<5

[0103] Step 2: Generate the control flow graph and obtain the loop code structure:

[0104] Based on the code metadata set, code block analysis is performed on the loop_example_3 function of program segment (a). Code blocks are formed by combining code from the keyword metadata in the loop_example_3 function, and a code block tree is constructed. A depth-first traversal is then performed on the code block tree to construct the control flow graph of the function. (See [link to documentation]). Figure 2 Traverse the control flow graph to obtain the while loop code structure in the loop_example_3 function.

[0105] The third step is to analyze the while loop definition data and variable application data of the while loop code structure based on the code metadata set, obtain the fixed value data i and j in the while loop code structure, and obtain the fixed value data set ass_set(while) = {i,j} from the fixed value data.

[0106] Step 4: Iterate through and determine the data types of the fixed data i and j in the fixed data set ass_set(while) = {i,j} of the while loop code structure. Since the data types of fixed data i and j are both numeric, obtain the scalar data set scalar_ass_set(while) = {i,j}.

[0107] Step 5: Use the method described in step S5 above to process the scalar data in the scalar data set scalar_ass_set(while) = {i,j}, and get set(i) = 1, set(j) = 0. Therefore, i is a potential monotonic scalar data, j is discarded, and the potential monotonic scalar data set pre_scalar_ass_set(while) = {i} is obtained.

[0108] Step 6: Use the method described in step S6 above to process the scalar data in the scalar data set pre_scalar_ass_set(while)={i}. Loop twice to get abs_gap(1,1)=1 and abs_gap(i,2)=1. Thus, i is a monotonic scalar fixed-value data, denoted as mono_scalar_ass_set(while)={i}.

[0109] Step 7: Obtain the while loop definition data based on the code metadata set; extract the loop entry condition from the while loop definition data, the loop entry condition expression is in_exp(while); perform NOT operation on the loop entry condition to obtain the loop exit, add a NOT operator before the loop entry condition expression to obtain the loop exit statement expression out(while) = ! in_exp(while).

[0110] Step 8: Extract the loop exit information from the code metadata. The while loop code structure of the loop_example_3 function contains one return statement exit and one break statement exit. The loop exit set is composed of the loop definition exit, the break statement exit set, and the return statement exit set, which is loop_out_set(while) = {out(while), i < 5, n_param == gn_var}.

[0111] Step 9: Traverse the loop exit set loop_out_set(while) = {out(while), i < 5, n_param == gn_var}, convert the loop definition exit statement expression, break statement exit expression, and return statement exit expression into binary tree expressions, analyze the binary tree expressions, obtain the exit data sets used by the loop definition exit statement expression, break statement exit expression, and return statement exit expression, and take the intersection of the exit dataset and the monotonic scalar constant value data set to obtain the loop counter set result(while) = {i}.

[0112] The above description is only a preferred embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in the present invention should be included within the scope of protection of the present invention.

Claims

1. A method for extracting a code loop counter, characterized in that, Specifically, the following steps are included: S1. Parsing the code being analyzed: The code is analyzed to obtain the functions, variable data, and keyword metadata within those functions; a set of code metadata is obtained from the functions, variable application data, and keyword metadata. S2. Generate the control flow graph and obtain the loop code structure: Based on the code metadata set, code block analysis is performed using keyword metadata at the function level to build a code block tree; Perform a depth-first traversal of the code block tree to construct the control flow graph of the function; traverse the control flow graph to obtain the loop code structure in the function; S3. Obtain the set of constant data for the loop code structure: Based on the code metadata set, analyze the loop definition data and variable application data in the variable data of the loop code structure to obtain the fixed value data in the loop code structure, and obtain the fixed value data set from the fixed value data; S4. Obtaining a scalar data set based on a fixed-value data set: Iterate through the set of fixed-value data to obtain the data type of the fixed-value data; if the data type is numeric, the corresponding fixed-value data is scalar data; obtain the set of scalar data from the scalar data; S5. Obtain the potential monotonic scalar data set: Obtain scalar data from the scalar data set; execute a loop based on the control flow graph and code metadata set. The loop process is as follows: starting from the loop entry point of the loop code structure, find the corresponding node in the control flow graph, and traverse the control flow graph using a depth-first search method for one loop; during each loop execution, record the initial value of the scalar data before the loop, data(x,n,0), and the final value after the loop, data(x,n,1). The difference between the scalar data after the nth loop and before the loop is gap(x,n) = data(x,n,1) - data(x,n,0), where x is the scalar data and n is the nth loop of the scalar data x; obtain the scalar data difference set set(x) = {gap(x,1), gap(x,2),..., gap(x,n)} from the loop difference gap(x,n); determine each gap(x,n) in the data difference set set(x) using the constraint solving method. If the positive and negative attributes of n are the same, the corresponding scalar data is the latent monotonic scalar data. Traverse the scalar data in the scalar data set to obtain the latent monotonic scalar data set from the latent monotonic scalar data. S6. Obtain the set of monotonic scalar constant values. Retrieve latent monotone scalar data from the latent monotone scalar data set; abstract the latent monotone scalar data into built-in identifier values; use the built-in identifier values ​​as the initial data abs_data(x,1,0) for the loop, execute the first loop based on the code metadata set and control flow graph, and obtain the final value abs_data(x,1,1) after the loop. The difference between the latent monotone scalar data before and after the first loop is abs_gap(x,1) = abs_data(x,1,1) - abs_data(x,1,0). Use the constraint solving method to determine whether the positive and negative attributes of abs_gap(x,1) and gap(x,1) are the same. If they are the same, execute the second loop; the initial value of the latent monotone scalar data in the second loop is abs_data(x,2,0) = `abs_data(x,1,1)` executes the second loop based on the code metadata set and control flow graph, obtaining the final value `abs_data(x,2,1)` after the loop. The difference between the loop before and after the second loop in the monotonically scalar data is obtained as `abs_gap(x,2) = abs_data(x,2,1) - abs_data(x,2,0)`. It then checks if the positive and negative attributes of `abs_gap(x,2)` and `gap(x,1)` are the same. If they are, the potential monotonically scalar data is considered monotonically scalar data. Finally, it iterates through all potential monotonically scalar data in the potential monotonically scalar data set to obtain the monotonically scalar constant value data set. S7. Obtain the loop definition exit based on the loop definition data; S8. Obtain the loop exit set The exit statements of the loop code structure are obtained from the code metadata set. The expression corresponding to the exit statement is the exit condition of the loop code structure. The statement exit set is obtained from the exit statement. The loop exit set is obtained from the loop definition exit and the statement exit set. S9. Obtain the set of loop counters based on the set of loop exits and the set of monotonic scalar constant values. Traverse the loop exit set to obtain the exit data used by the exit statement and exit expression. The exit data forms an exit data set. Take the intersection of the exit dataset and the monotonic scalar data set. The data in the intersection is the loop counter. Thus, the loop counter set is obtained.

2. The method for extracting a code loop counter according to claim 1, characterized in that, The specific process of building the code block tree in step S2 is as follows: code blocks are formed by combining the code in the keyword metadata of the function; the first-level code block in the function is obtained; the second-level code block in the first-level code block is obtained; the child code blocks are obtained in sequence according to the hierarchy and order of the code blocks until the process is completed; and the code block tree is built from the obtained child code blocks.

3. The method for extracting a code loop counter according to claim 1, characterized in that, The loop definition data includes the starting line number, ending line number, loop entry point, entry condition, entry line number, and loop code structure type information of the loop code structure.

4. The method for extracting a code loop counter according to claim 3, characterized in that, The specific process of step S3 is as follows: obtain the starting line number of the loop code structure from the loop definition data, find the ending line number, loop entry, entry condition, entry line number and loop code structure type information of the loop code structure based on the starting line number of the loop code structure, and determine the range of code lines contained in the loop code structure based on the information. Get variable application data from the code line range, and filter out fixed value data from the variable application data; Obtain a set of constant-value data from constant-value data.

5. The method for extracting a code loop counter according to any one of claims 1-4, characterized in that, The set value data is the data that is assigned a value.

6. The method for extracting a code loop counter according to any one of claims 1-4, characterized in that, The specific steps of step S4 are as follows: S41. Obtain the set of fixed value data; S42. Determine if there are any untraversed fixed-value data in the fixed-value data set; if not, end the acquisition of scalar data and obtain the scalar data set; if so, take out one untraversed fixed-value data in sequence and proceed to the next step. S43. Convert the string of the untraversed fixed value data into a binary tree expression; analyze the binary tree expression to obtain the data type of the untraversed fixed value data; S44. Determine if the data type is numeric; if not, return to S42. If so, determine the fixed-value data corresponding to this data type as scalar data and proceed to the next step; S45. Place the scalar data into the scalar data set; Return to S42.

7. The method for extracting a code loop counter according to any one of claims 1-4, characterized in that, The specific steps of step S7 are as follows: Loop definition data is obtained based on the code metadata set; the loop definition entry condition is extracted from the loop definition data, and the loop entry condition expression is in_exp(0); The loop definition exit is obtained by performing a NOT operation on the loop entry condition. The loop definition exit statement expression is out(0)=!in_exp(0).

8. An apparatus for extracting a code loop counter, characterized in that, An apparatus for extracting a loop counter from loop code, used to perform the method of any one of claims 1 to 7, the apparatus comprising: The code parsing module is used to analyze the code being analyzed, and to obtain the functions, variable data, and keyword metadata within those functions. The control flow graph generation module is used to generate the control flow graph of the functions of the analyzed code and to obtain the loop code structure in the functions; The loop counter extraction module is used to extract loop counters from the control flow graph of the function being analyzed, generated by the control flow graph generation module, and to obtain the loop code structure within the function.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the steps of the method according to any one of claims 1 to 7.

10. An electronic device, characterized in that, include: A memory on which computer programs are stored; A processor for executing the computer program in the memory to implement the steps of the method according to any one of claims 1 to 7.