CN113760700A - Program endless loop detection method, device, electronic equipment and storage medium - Google Patents

Program endless loop detection method, device, electronic equipment and storage medium Download PDF

Info

Publication number
CN113760700A
CN113760700A CN202010784325.9A CN202010784325A CN113760700A CN 113760700 A CN113760700 A CN 113760700A CN 202010784325 A CN202010784325 A CN 202010784325A CN 113760700 A CN113760700 A CN 113760700A
Authority
CN
China
Prior art keywords
function
loop
target
program
execution
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202010784325.9A
Other languages
Chinese (zh)
Inventor
蒋俊弛
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Jingdong Zhenshi Information Technology Co Ltd
Original Assignee
Beijing Jingdong Zhenshi Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Jingdong Zhenshi Information Technology Co Ltd filed Critical Beijing Jingdong Zhenshi Information Technology Co Ltd
Priority to CN202010784325.9A priority Critical patent/CN113760700A/en
Publication of CN113760700A publication Critical patent/CN113760700A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/362Software debugging
    • G06F11/3624Software debugging by performing operations on the source code, e.g. via a compiler
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/362Software debugging
    • G06F11/366Software debugging using diagnostics

Abstract

The embodiment of the application provides a program endless loop detection method, a device, an electronic device and a storage medium, wherein a target loop function in a program source code is obtained; determining a control flow graph according to the target cyclic function, wherein the control flow graph is used for representing the cyclic structure of the target cyclic function; according to the method for detecting the dead loop in the target loop function, the dead loop in the program dead loop test process is detected by analyzing the target loop function to be tested in the source code from the structural characteristics of the code, the corresponding control flow graph is determined, and the dead loop in the target function is determined according to the control flow graph, so that the problem that the test personnel depends on experience and level in the manual analysis and test process is solved, and the test accuracy and test efficiency in the program dead loop test process are improved.

Description

Program endless loop detection method, device, electronic equipment and storage medium
Technical Field
The embodiment of the application relates to the technical field of computers, in particular to a program endless loop detection method and device, electronic equipment and a storage medium.
Background
In the development stage of software program products, the testing link of the program products is more and more emphasized by various enterprises and manufacturers, and the stability and the quality of the program products can be greatly improved through high-quality program testing. The test of the dead cycle in the program product is always an important part in the test link of the program product.
In the prior art, for a test project of a program product, a tester generally researches a dead loop by using a method of artificial theory analysis through an abstract program structure; or to detect the presence of dead loops in a program through a large number of test cases.
However, whether the method of manual theoretical analysis or the method of detecting the dead loop in the program by a large number of test cases have the problems of low accuracy of the detection result and poor detection efficiency.
Disclosure of Invention
The embodiment of the application provides a program endless loop detection method and device, an electronic device and a storage medium, and aims to solve the problems of low accuracy and poor detection efficiency of detection results existing in the process of detecting endless loops in a program.
In a first aspect, an embodiment of the present application provides a program loop detection method, including:
acquiring a target cyclic function in a program source code; determining a control flow graph according to the target cyclic function, wherein the control flow graph is used for representing a cyclic structure of the target cyclic function; and detecting dead cycles in the target cyclic function according to the control flow graph.
In one possible implementation, determining a control flow graph according to the target cyclic function includes: compiling the target cyclic function by using a preset compiler to generate an intermediate code file; analyzing the intermediate code file to obtain the cyclic structure data of the target cyclic function; and generating a control flow graph according to the circulation structure data.
In a possible implementation manner, the compiling the target loop function by using the preset compiler to generate an intermediate code file includes: and compiling the target loop function by using the fdump-tree option of the GCC compiler, and generating the intermediate code file by using the cfg sub-option.
In a possible implementation manner, the intermediate code file includes function code blocks and jump statements, the function code blocks are used for characterizing the operation content of the target loop function, and the jump statements are used for characterizing the execution sequence of the function code blocks in the target loop function; analyzing the intermediate code file to obtain the cyclic structure data of the target cyclic function, wherein the cyclic structure data comprises: analyzing the intermediate code file according to a preset first regular expression rule to obtain the function code block and the jump statement; determining execution nodes and an execution sequence according to the function code blocks and the jump statements, wherein the execution nodes correspond to the function code blocks one to one; the execution sequence is used for representing the sequence of executing nodes on a preset execution path in the target cyclic function; and determining the loop structure data of the target loop function according to the execution nodes and the execution sequence.
In a possible implementation manner, the determining the loop structure data of the target loop function according to the execution node and the execution order, where the loop structure data is a key/value pair structure, includes: acquiring execution nodes on a preset execution path in the target cyclic function and a corresponding execution sequence; and converting the execution nodes on the preset execution path in the target cyclic function and the corresponding execution sequence into a pass key/value pair structure to generate cyclic structure data.
In a possible implementation manner, after determining loop structure data of the target loop function according to the execution nodes and the execution sequence, the method further includes: converting the circular data structure into a preset dictionary structure, wherein the dictionary structure is a variable container model based on a key/value pair structure, and keys of the dictionary structure are used for representing initial execution nodes on an execution path; the values of the dictionary structure are used to characterize a list of end execution nodes on the execution path.
In one possible implementation, detecting a dead loop in the target loop function according to the control flow graph includes: acquiring a cycle termination condition according to the control flow diagram; and if the loop structure corresponding to the control flow graph cannot meet the loop termination condition, determining that the target loop function contains dead loops.
In one possible implementation manner, after obtaining the target loop function in the program source code, the method further includes: and judging whether the target circulation function contains absolute dead circulation or not according to a preset second regular expression rule.
In a possible implementation manner, judging whether the target loop function includes an absolute dead loop according to a preset second regular expression rule includes: judging whether a jump-out instruction exists in the loop structure of the target loop function or not according to a preset second regular expression rule; and if the jump-out instruction does not exist, determining that the target loop function comprises the absolute dead loop.
In a second aspect, an embodiment of the present application provides a program loop detection apparatus, including:
the acquisition module is used for acquiring a target cyclic function in a program source code;
a determining module, configured to determine a control flow graph according to the target cyclic function, where the control flow graph is used to represent a cyclic structure of the target cyclic function;
and the detection module is used for detecting the dead loop in the target cyclic function according to the control flow graph.
In a possible implementation manner, the determining module is specifically configured to: compiling the target cyclic function by using a preset compiler to generate an intermediate code file; analyzing the intermediate code file to obtain the cyclic structure data of the target cyclic function; and generating a control flow graph according to the circulation structure data.
In a possible implementation manner, the preset compiler is a GCC compiler, and the determining module is specifically configured to, when the target loop function is compiled by using the preset compiler to generate an intermediate code file: and compiling the target loop function by using the fdump-tree option of the GCC compiler, and generating the intermediate code file by using the cfg sub-option.
In a possible implementation manner, the intermediate code file includes function code blocks and jump statements, the function code blocks are used for characterizing the operation content of the target loop function, and the jump statements are used for characterizing the execution sequence of the function code blocks in the target loop function; the determining module is specifically configured to, when analyzing the intermediate code file to obtain the loop structure data of the target loop function: analyzing the intermediate code file according to a preset first regular expression rule to obtain the function code block and the jump statement; determining execution nodes and an execution sequence according to the function code blocks and the jump statements, wherein the execution nodes correspond to the function code blocks one to one; the execution sequence is used for representing the sequence of executing nodes on a preset execution path in the target cyclic function; and determining the loop structure data of the target loop function according to the execution nodes and the execution sequence.
In a possible implementation manner, the loop structure data is a key/value pair structure, and when the determining module determines the loop structure data of the target loop function according to the execution node and the execution sequence, the determining module is specifically configured to: acquiring execution nodes on a preset execution path in the target cyclic function and a corresponding execution sequence; and converting the execution nodes on the preset execution path in the target cyclic function and the corresponding execution sequence into a pass key/value pair structure to generate cyclic structure data.
In a possible implementation manner, after determining the loop structure data of the target loop function according to the execution node and the execution sequence, the determining module is specifically configured to: converting the circular data structure into a preset dictionary structure, wherein the dictionary structure is a variable container model based on a key/value pair structure, and keys of the dictionary structure are used for representing initial execution nodes on an execution path; the values of the dictionary structure are used to characterize a list of end execution nodes on the execution path.
In a possible implementation manner, the detection module is specifically configured to: acquiring a cycle termination condition according to the control flow diagram; and if the loop structure corresponding to the control flow graph cannot meet the loop termination condition, determining that the target loop function contains dead loops.
In a possible implementation manner, the program loop detection apparatus further includes a preprocessing module, configured to: after a target circulation function in a program source code is obtained, whether the target circulation function contains absolute dead circulation or not is judged according to a preset second regular expression rule.
In a possible implementation manner, the preprocessing module is configured to determine whether the target cyclic function includes an absolute dead loop according to a preset second regular expression rule, and specifically is configured to: judging whether a jump-out instruction exists in the loop structure of the target loop function or not according to a preset second regular expression rule; and if the jump-out instruction does not exist, determining that the target loop function comprises the absolute dead loop.
In a third aspect, an embodiment of the present application provides an electronic device, including: the device comprises a memory and a processor, wherein the memory stores executable instructions of the processor; wherein the processor is configured to perform the program loop detection method of any of the first aspect via execution of the executable instructions.
In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, on which a computer program is stored, where the computer program, when executed by a processor, implements the program dead loop detection method described in any one of the first aspects.
The embodiment of the application provides a program endless loop detection method, a device, an electronic device and a storage medium, wherein a target loop function in a program source code is obtained; determining a control flow graph according to the target cyclic function, wherein the control flow graph is used for representing a cyclic structure of the target cyclic function; according to the method for detecting the dead loop in the target loop function, the corresponding control flow diagram is determined by analyzing the target loop function to be tested in the source code according to the structural characteristics of the code, and the dead loop in the target function is determined according to the control flow diagram, so that the problem that the test personnel depend on experience and level in the manual analysis and test process is solved, and the test accuracy and test efficiency in the program dead loop test process are improved.
Drawings
In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings needed to be used in the description of the embodiments or the prior art will be briefly introduced below, and it is obvious that the drawings in the following description are some embodiments of the present application, and for those skilled in the art, other drawings can be obtained according to these drawings without inventive exercise.
Fig. 1 is a schematic view of an application scenario of a program loop detection method according to an embodiment of the present application;
FIG. 2 is a flowchart of a program loop detection method according to an embodiment of the present application;
fig. 3 is a schematic view of another application scenario of the program loop detection method according to the embodiment of the present application;
FIG. 4 is a flowchart of a program loop detection method according to another embodiment of the present application;
FIG. 5 is a flowchart of step S203 in the embodiment shown in FIG. 4;
fig. 6 is a schematic structural diagram of a program loop detection apparatus according to an embodiment of the present disclosure;
fig. 7 is a schematic structural diagram of another program loop detection apparatus according to an embodiment of the present application;
fig. 8 is a schematic structural diagram of an electronic device according to an embodiment of the present application.
With the above figures, there are shown specific embodiments of the present application, which will be described in more detail below. These drawings and written description are not intended to limit the scope of the inventive concepts in any manner, but rather to illustrate the inventive concepts to those skilled in the art by reference to specific embodiments.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are some embodiments of the present application, but not all embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
The terms "first," "second," "third," "fourth," and the like in the description and in the claims of the present application and in the drawings described above, if any, are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It is to be understood that the data so used is interchangeable under appropriate circumstances such that the embodiments of the application described herein are, for example, capable of operation in sequences other than those illustrated or otherwise described herein. Furthermore, the terms "comprises," "comprising," and "having," and any variations thereof, are intended to cover a non-exclusive inclusion, such that a process, method, system, article, or apparatus that comprises a list of steps or elements is not necessarily limited to those steps or elements expressly listed, but may include other steps or elements not expressly listed or inherent to such process, method, article, or apparatus.
First, terms related to embodiments of the present application will be explained:
a Control Flow Graph (CFG), also called a Control Flow Graph, is an abstract representation of a process or program, is an abstract data structure used in a compiler, is maintained internally by the compiler, and represents all paths traversed during the execution of a program. The method can graphically represent the possible flow direction of all basic block execution in a process, and can also reflect the real-time execution process of a program code block.
GCC Compiler, GNU Compiler suite (GNU Compiler Collection), a programming language interpreter developed by GNU, where GNU is a free operating system similar to Unix, and GCC was originally designed to write a Compiler specifically for GNU operating systems, and is now adopted as a standard Compiler by most Unix-like operating systems (e.g., Linux, BSD, MacOS X, etc.).
Fig. 1 is a schematic view of an application scenario of the program loop detection method according to the embodiment of the present application. As shown in fig. 1, in the application scenario provided in this embodiment, before the program product is actually used or released, a developer sends the program source code to a tester to perform various tests, including program loop detection. For example, the program loop detection method provided in the embodiment of the present application may be executed on a terminal device used by a developer or a terminal device used by a tester.
In the prior art, there are generally two main implementation schemes for a program product dead loop test project, the first is that a program developer or a program tester researches and identifies a dead loop existing in a program code by analyzing a source code of the program, abstracting a program structure, and utilizing a method of artificial theory analysis. The second is to test the program through a large number of test cases, and to judge whether there is a dead loop in the program code according to the time length of execution after the program runs the test cases. However, in the first method, the method of manual analysis depends on the analysis ability and experience of the program developer or the program tester, and the program developer or the program tester with poor ability and experience cannot identify the dead loop in the program code or takes a long time, thereby causing the problems of low accuracy and low efficiency of dead loop detection. For the second method, firstly, the compiling of the test case needs manual participation, and the quality of the test case is also related to the capability of a program developer or a program tester participating in the compiling of the test case; meanwhile, when different programs and functions are executed under different test cases, the running time does not have a certain standard, and manual processing is still needed, so that whether the program has dead circulation or not is judged according to the length of the execution time of the test cases, and the problems of low accuracy and low efficiency also exist.
In view of the above technical problems, the present invention provides a program loop detection method, apparatus, electronic device, and storage medium, which analyze a target loop function to be tested in a source code based on the structural characteristics of the code itself, determine a corresponding control flow graph, and determine a loop in the target function according to the control flow graph, thereby avoiding the problem of relying on experience and level of a tester in the manual analysis and test processes, and improving the test accuracy and test efficiency in the program loop test process.
The following describes the technical solutions of the present application and how to solve the above technical problems with specific embodiments. The following several specific embodiments may be combined with each other, and details of the same or similar concepts or processes may not be repeated in some embodiments. Embodiments of the present application will be described below with reference to the accompanying drawings.
Fig. 2 is a flowchart of a program loop detection method according to an embodiment of the present application, and as shown in fig. 2, an execution main body of the program loop detection method according to the embodiment may be a terminal device or a server, and the method includes:
s101, acquiring a target circulation function in a program source code.
Illustratively, the program source code refers to source code for completing all or part of the functions of a program, and the program source code has different contents according to the language used for developing the program, and generally has a main function, such as main { } function, and in the main function, also includes or refers to other functions for realizing different functions, and here, the specific contents and forms of the program source code are not limited.
In the main function or other functions of the program source code, a specific loop structure is required to be used for realizing a specific function, for example, 1 to 10 accumulation is calculated, and then a loop structure may be used, and the loop performs an addition calculation of arguments from 1 to 10, thereby obtaining 1 to 10 accumulation results. Specifically, for example, for { } round, while { } round, do while { } round, and the like. The specific keywords of the loop structure may vary according to different development languages, and are not specifically limited herein. The target cyclic function is a function to be measured with a cyclic structure such as for { } cycle, while { } cycle and the like inside.
Further, there are multiple methods for obtaining the target loop function, and for example, the method may be determined by a code line identifier where a preset function is located, for example, the function a with a loop structure body, where the code line identifier is 201, and then the position of the target loop function a may be determined according to the code line identifier 201, so as to obtain the target loop function a. For example, the name of the Function may also be analyzed by a regular expression or the like, so as to locate the target round Function, for example, the Function B with a round structure, where the name of the Function is home _ Function (), and then the program source code is searched according to a preset regular expression, so as to locate the home _ Function () and determine the target round Function B. In this embodiment, a specific method of the method of obtaining the target cyclic function is not limited.
S102, determining a control flow graph according to the target cyclic function, wherein the control flow graph is used for representing the cyclic structure of the target cyclic function.
A control flow graph is an abstract data structure used in a compiler that is maintained internally by the compiler and can be used to represent all the paths traversed during the execution of a program. The method can graphically represent the possible flow direction of all basic block execution in a process, and can also reflect the real-time execution process of a program code block. Because the target cyclic function comprises the cyclic structure, the specific cyclic structure for realizing the cyclic function in the target cyclic function can be represented by the control flow graph.
For example, for an object loop function in a program source code, the code containing the object loop function may be compiled into a compiled file, and the compiled file is subjected to structural feature extraction and transformation, so as to form a control flow graph capable of characterizing a loop structure of the object loop function. The specific implementation method for constructing the control flow graph will be described in detail in the following embodiments.
And S103, detecting dead cycles in the target cyclic function according to the control flow graph.
Illustratively, since the control flow graph can represent the interrelation and the execution sequence between all execution nodes in the execution path corresponding to one process, according to the control flow graph, it can be determined whether a loop structure in the target loop function can have a loop ending exit, and if the loop structure has the loop ending exit, it indicates that the loop structure can jump out of a loop under a specific condition, that is, a non-dead loop; at this time, if the loop structure does not have a loop ending exit, it indicates that the loop structure cannot jump out of the loop under any condition in the preset execution path, i.e. a dead loop. Further, if all loop structures in the target loop function are not dead loops, the target loop function does not include dead loops; if at least one of the loop structures in the target loop function is a dead loop, the target loop function includes the dead loop.
According to the program endless loop detection method provided by the embodiment of the application, a target loop function in a program source code is obtained; determining a control flow graph according to the target cyclic function, wherein the control flow graph is used for representing the cyclic structure of the target cyclic function; according to the method for detecting the dead loop in the target loop function, the dead loop in the program dead loop test process is detected by analyzing the target loop function to be tested in the source code from the structural characteristics of the code, the corresponding control flow graph is determined, and the dead loop in the target function is determined according to the control flow graph, so that the problem that the test personnel depends on experience and level in the manual analysis and test process is solved, and the test accuracy and test efficiency in the program dead loop test process are improved.
Fig. 3 is a schematic view of another application scenario of the program loop detection method provided in the embodiment of the present application, and as shown in fig. 3, the program loop detection method provided in the embodiment of the present application can automatically complete program detection without manual supervision, so that the method is not only suitable for terminal devices used for testing and development, but also suitable for devices such as a cloud server. After the development of the program source code is completed by a developer, the automatic detection can be performed through the terminal device or the cloud server, the overall efficiency of the program development is improved, and the comprehensive cost of the program development is reduced.
Fig. 4 is a flowchart of a program loop detection method according to another embodiment of the present application, and as shown in fig. 4, the program loop detection method according to this embodiment further refines step S102 on the basis of the program loop detection method according to the embodiment shown in fig. 2, and adds a preprocessing step before S102, so that the program loop detection method according to this embodiment may include:
step S201, obtaining a target loop function in the program source code.
And S202, compiling the target loop function by using a preset compiler to generate an intermediate code file.
In one possible implementation, the pre-compiler is a GCC compiler. GCC is a programming language interpreter developed by GNU, which can compile C, C + +, Objective-C, Fortran, Java, and other languages. The GCC compiler has more than 100 compiling options available, and different compiling functions can be realized by different compiling options, which are not listed here.
The target loop function may be compiled by using an fdump-tree compiling option of the GCC compiler, and specifically, the fdump-tree compiling option of the GCC compiler generates intermediate information in the compiling process, and the output intermediate information can represent valuable information in the compiled target loop function, for example, a detailed execution process of the target loop function. Then, the intermediate information is processed by using the cfg sub option, and the intermediate information capable of representing the detailed execution process of the target cyclic function is solidified into a configuration file, namely an intermediate code file.
Table 1 is a schematic comparison table of a target loop function and an intermediate code file provided in this embodiment, and after the target loop function is compiled by a preset compiler, the intermediate code file capable of describing a detailed execution process of the target loop function is formed. More specifically, as shown in table 1, the target loop function is a main () function, the intermediate code file generated by compiling includes a corresponding main () function body, the function body part is composed of code blocks in the < bb > or < L > format and goto jump statements, the code blocks and the goto statements provide very important information for extracting structural features of the program, and for a statement of multi-condition judgment, that is, an if-else statement in the target loop function, after compiling by GCC, the multiple condition statements are split into single condition statements, and then jump to the corresponding code blocks by using the goto statements, so as to realize the expression of the detailed execution process of the target loop function.
TABLE 1
Figure BDA0002621375150000101
And step S203, analyzing the intermediate code file to obtain the cyclic structure data of the target cyclic function.
In one possible implementation manner, the intermediate code file includes function code blocks and jump statements, the function code blocks are used for representing the operation content of the target loop function, and the jump statements are used for representing the execution sequence of the function code blocks in the target loop function. According to the function code block and the jump statement, parsing and positioning of the content in the intermediate code file can be realized, for example, as shown in fig. 5, step S203 may include three specific implementation steps S2031, S2032, and S2033:
step S2031, analyzing the intermediate code file according to a preset first regular expression rule to obtain a function code block and a jump statement.
Exemplarily, a regular expression (regular expression) describes a pattern (pattern) of string matching. Regular expressions are commonly used to retrieve, replace, or otherwise replace text that conforms to a particular pattern or rule. The regular expression rule is a rule for characterizing the mapping relationship between the regular expression and specific codes. The preset first regular expression rule may be a preset configuration table, and is used for matching a specific code. Table 2 shows an example of a first regular expression rule provided in the embodiment of the present application, and as shown in table 2, the functions, the function code blocks < bb >, the function code blocks < L >, the jump statements goto, and other contents are respectively matched through different regular expressions, so that the intermediate code file is analyzed, and the function code blocks and the jump statements are obtained.
TABLE 2
Figure BDA0002621375150000111
It should be noted that table 2 shows only an exemplary regular expression rule, which may be different in different development languages and environments, and the method of using the regular expression is a prior art means in the art, and the code meaning in table 2 is not described in detail here.
And S2032, determining an execution node and an execution sequence according to the function code block and the jump statement.
The execution nodes correspond to the function code blocks one by one; the execution sequence is used for representing the sequence of executing the execution nodes on the preset execution path in the target cyclic function. After determining the function code block and the jump statement, all preset execution paths existing in the target loop function can be correspondingly determined. The method comprises the steps that one or more execution nodes are included on a preset execution path, and the execution nodes on each execution path correspond to a specific execution sequence. The execution nodes correspond to the function code blocks one by one, and when the execution nodes pass through, the function code blocks corresponding to the execution nodes are executed.
And S2033, determining the loop structure data of the target loop function according to the execution nodes and the execution sequence.
According to the execution nodes and the execution sequence, the characteristics of the cyclic structure in the target cyclic function can be completely determined, the characteristics of the cyclic structure are solidified, and the formed data with a specific data structure is cyclic structure data.
In a possible implementation manner, the loop structure data is a key/value pair structure, and the determining of the loop structure data of the target loop function according to the execution node and the execution sequence includes:
acquiring execution nodes on a preset execution path in a target cyclic function and a corresponding execution sequence; and converting execution nodes on a preset execution path in the target cyclic function and the corresponding execution sequence into a pass key/value pair structure to generate cyclic structure data. Illustratively, the loop structure data may be stored in a JSON (JavaScript Object Notation) file.
In a possible implementation manner, after step S203, the method may further include:
and step S204, converting the cyclic data structure into a preset dictionary structure.
The dictionary structure is a variable container model based on a key/value pair structure, and keys of the dictionary structure are used for representing initial execution nodes on an execution path; the values of the dictionary structure are used to characterize a list of end execution nodes on the execution path.
In the embodiment, the cycle structure data is converted into the dictionary structure, the JSON file does not need to be analyzed in the subsequent process, and the data in the dictionary structure can be directly used, so that the data processing speed can be increased, and the test efficiency can be improved.
And S205, generating a control flow graph according to the circulation structure data.
Illustratively, each execution node in the loop structure data corresponds to a node in the control flow graph, and the execution order is an edge of the control flow graph from the node to another node. The JSON file is read, the structure of the JSON file is analyzed, the JSON file is converted into a data format of a dictionary, and a control flow graph is drawn through a geometric figure tool Graphviz, wherein the process of drawing the control flow graph through the Graphviz is the prior art in the field, and is not repeated here.
And S206, acquiring a circulation termination condition according to the control flow graph.
Specifically, according to the description of the execution process of the target loop function by the control flow graph, a loop termination condition for terminating the loop in the multiple execution paths of the target loop function can be determined. For example, in a for (i ═ 1: a) loop, the influencing variable i satisfies a predetermined condition, i.e. i > a; or the influencing variable i in the while (i ≠ b) loop satisfies a preset condition i ≠ b.
And step S207, if the loop structure corresponding to the control flow graph cannot meet the loop termination condition, determining that the target loop function contains a dead loop.
And S208, if the loop structure corresponding to the control flow graph meets the loop termination condition, determining that the target loop function does not contain a dead loop.
Whether a loop structure is a dead loop may be determined by a number of factors, and thus whether a loop can satisfy termination conditions should be classified for processing. In one possible case, if there is a loop condition expression or a loop ending condition in a loop, the corresponding loop variable information or other control conditions can be extracted by using a program slicing technique, and then analyzed to determine whether the value thereof can satisfy the loop ending condition, thereby determining whether the loop structure is a dead loop. For searching the outlet of the conditional circulation closed loop, a backtracking method can be adopted, and the backtracking is started from the end node of the control flow graph to search the outlet of the closed loop.
In a possible implementation manner, after step S201, the method may further include:
s201a, judging whether the target circulation function contains an absolute dead circulation according to a preset second regular expression rule.
In a possible implementation manner, whether a jump-out instruction exists in a loop structure of a target loop function is judged according to a preset second regular expression rule; and if the jump-out instruction does not exist, determining that the target loop function contains the absolute dead loop.
Illustratively, table 3 is an illustration of a second regular expression rule provided in the embodiment of the present application, and as shown in table 3, a loop structure in the target loop function may be determined through the second regular expression. Illustratively, a while () loop, a do () loop, a for () loop, etc. may be included. When the determination condition in the loop is true, for example, the loop structure is while (1) { }, at this time, if the loop structure does not include a jump instruction, for example, a break instruction, the loop structure will always run in a loop, that is, the loop structure is a dead loop. The loop structure which can run all the time is positioned through a preset first positive expression rule, whether a jump-out instruction exists in the loop structure or not is searched, and whether an absolute dead loop exists in a target loop function or not can be judged.
TABLE 3
Figure BDA0002621375150000131
It should be noted that table 3 shows only an exemplary regular expression rule, which may be different in different development languages and environments, and the method of using the regular expression is a prior art means in the art, and the code meaning in table 3 is not described in detail here.
In this embodiment, because the judgment of the absolute dead loop is implemented only by searching the jump instruction in the function body, and specific iterative judgment on the function body is not required, the execution process is simple and efficient, and before the target loop function is analyzed and a control flow graph is generated, the absolute dead loop in the target loop function is judged first, so that the detection of the absolute dead loop in a complex manner such as generating the control flow graph is avoided, and the dead loop detection efficiency of the target loop function can be improved.
Fig. 6 is a schematic structural diagram of a program loop detection apparatus according to an embodiment of the present application, and as shown in fig. 6, a program loop detection apparatus 3 according to the embodiment includes:
an obtaining module 31, configured to obtain a target loop function in a program source code;
a determining module 32, configured to determine a control flow graph according to the target cyclic function, where the control flow graph is used to represent a cyclic structure of the target cyclic function;
and a detection module 33, configured to detect a dead loop in the target loop function according to the control flow graph.
The obtaining module 31, the determining module 32 and the detecting module 33 are connected in sequence. The program loop detection apparatus 3 provided in this embodiment may implement the technical solutions of the method embodiments shown in fig. 2 to fig. 3, and the implementation principles and technical effects thereof are similar, and are not described herein again.
Fig. 7 is a schematic structural diagram of another program loop detector according to an embodiment of the present application, in which the program loop detector 4 shown in fig. 7 is added with a preprocessing module 41 on the basis of the program loop detector shown in fig. 6, wherein:
in a possible implementation manner, the determining module 32 is specifically configured to: compiling the target loop function by using a preset compiler to generate an intermediate code file; analyzing the intermediate code file to obtain the cyclic structure data of the target cyclic function; and generating a control flow graph according to the circulation structure data.
In a possible implementation manner, the preset compiler is a GCC compiler, and when the determining module 32 compiles the target loop function by using the preset compiler to generate the intermediate code file, the determining module is specifically configured to: and compiling the target loop function by using the fdump-tree option of the GCC compiler, and generating an intermediate code file by using the cfg sub-option.
In one possible implementation manner, the intermediate code file comprises a function code block and a jump statement, the function code block is used for representing the operation content of the target loop function, and the jump statement is used for representing the execution sequence of the function code block in the target loop function; when the determining module 32 analyzes the intermediate code file to obtain the loop structure data of the target loop function, it is specifically configured to: analyzing the intermediate code file according to a preset first regular expression rule to obtain a function code block and a jump statement; determining execution nodes and an execution sequence according to the function code blocks and the jump statements, wherein the execution nodes correspond to the function code blocks one to one; the execution sequence is used for representing the sequence of executing nodes on a preset execution path in the target cyclic function; and determining the loop structure data of the target loop function according to the execution nodes and the execution sequence.
In a possible implementation manner, the loop structure data is a key/value pair structure, and when the determining module 32 determines the loop structure data of the target loop function according to the execution node and the execution sequence, the determining module is specifically configured to: acquiring execution nodes on a preset execution path in a target cyclic function and a corresponding execution sequence; and converting execution nodes on a preset execution path in the target cyclic function and the corresponding execution sequence into a pass key/value pair structure to generate cyclic structure data.
In a possible implementation manner, after determining the loop structure data of the target loop function according to the execution nodes and the execution sequence, the determining module 32 is specifically configured to: converting the cyclic data structure into a preset dictionary structure, wherein the dictionary structure is a variable container model based on a key/value pair structure, and keys of the dictionary structure are used for representing initial execution nodes on an execution path; the values of the dictionary structure are used to characterize a list of end execution nodes on the execution path.
In a possible implementation manner, the detection module 33 is specifically configured to: acquiring a cycle termination condition according to the control flow graph; and if the loop structure corresponding to the control flow graph cannot meet the loop termination condition, determining that the target loop function contains dead loops.
In one possible implementation, the preprocessing module 41 is configured to: and after a target circulation function in the program source code is obtained, judging whether the target circulation function contains absolute dead circulation or not according to a preset second regular expression rule.
In a possible implementation manner, the preprocessing module 41 determines whether the target loop function includes an absolute dead loop according to a preset second regular expression rule, and is specifically configured to: judging whether a jump-out instruction exists in a loop structure of the target loop function or not according to a preset second regular expression rule; and if the jump-out instruction does not exist, determining that the target loop function contains the absolute dead loop.
The acquiring module 31, the preprocessing module 41, the determining module 32 and the detecting module 33 are connected in sequence. The program loop detection apparatus 4 provided in this embodiment may implement the technical solutions of the method embodiments shown in fig. 4 to fig. 5, and the implementation principles and technical effects thereof are similar, and are not described herein again.
Fig. 8 is a schematic structural diagram of an electronic device provided in an embodiment of the present application, and as shown in fig. 8, the electronic device 5 of the present embodiment may include: a processor 51 and a memory 52.
A memory 52 for storing programs; the Memory 52 may include a volatile Memory (RAM), such as a Static Random Access Memory (SRAM), a Double Data Rate Synchronous Dynamic Random Access Memory (DDR SDRAM), and the like; the memory may also comprise a non-volatile memory, such as a flash memory. The memory 52 is used to store computer programs (e.g., applications, functional modules, etc. that implement the above-described methods), computer instructions, etc., which may be stored in one or more of the memories 52 in a partitioned manner. And the above-mentioned computer program, computer instructions, data, etc. can be called by the processor 51.
The computer programs, computer instructions, etc. described above may be stored in one or more memories 52 in partitions. And the above-mentioned computer program, computer instructions, data, etc. can be called by the processor 51.
A processor 51 for executing the computer program stored in the memory 52 to implement the steps of the method according to the above embodiments.
Reference may be made in particular to the description relating to the preceding method embodiment.
The processor 51 and the memory 52 may be separate structures or may be integrated structures integrated together. When the processor 51 and the memory 52 are separate structures, the memory 52 and the processor 51 may be coupled by a bus 53.
The electronic device of this embodiment may execute the technical solution of the method embodiment shown in any one of fig. 2 to fig. 5, and the implementation principle and the technical effect are similar, which are not described herein again.
One embodiment of the present application provides a computer-readable storage medium, on which a computer program is stored, where the computer program is executed by a processor to implement the program loop detection method provided in any embodiment of the present invention corresponding to fig. 2 to fig. 5.
The computer readable storage medium may be, among others, ROM, Random Access Memory (RAM), CD-ROM, magnetic tape, floppy disk, optical data storage device, and the like.
Other embodiments of the present application will be apparent to those skilled in the art from consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the invention following, in general, the principles of the application and including such departures from the present disclosure as come within known or customary practice within the art to which the invention pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the application being indicated by the following claims.
It will be understood that the present application is not limited to the precise arrangements described above and shown in the drawings and that various modifications and changes may be made without departing from the scope thereof. The scope of the application is limited only by the appended claims.

Claims (12)

1. A program loop detection method, comprising:
acquiring a target cyclic function in a program source code;
determining a control flow graph according to the target cyclic function, wherein the control flow graph is used for representing a cyclic structure of the target cyclic function;
and detecting dead cycles in the target cyclic function according to the control flow graph.
2. The method of claim 1, wherein determining a control flow graph from the target cyclic function comprises:
compiling the target cyclic function by using a preset compiler to generate an intermediate code file;
analyzing the intermediate code file to obtain the cyclic structure data of the target cyclic function;
and generating a control flow graph according to the circulation structure data.
3. The method of claim 2, wherein the pre-compiler is a GCC compiler, and wherein compiling the target loop function with the pre-compiler to generate the intermediate code file comprises:
and compiling the target loop function by using the fdump-tree option of the GCC compiler, and generating the intermediate code file by using the cfg sub-option.
4. The method according to claim 2, wherein the intermediate code file comprises function code blocks and jump statements, the function code blocks are used for characterizing the operation content of the target loop function, and the jump statements are used for characterizing the execution sequence of the function code blocks in the target loop function; analyzing the intermediate code file to obtain the cyclic structure data of the target cyclic function, wherein the cyclic structure data comprises:
analyzing the intermediate code file according to a preset first regular expression rule to obtain the function code block and the jump statement;
determining execution nodes and an execution sequence according to the function code blocks and the jump statements, wherein the execution nodes correspond to the function code blocks one to one; the execution sequence is used for representing the sequence of executing nodes on a preset execution path in the target cyclic function;
and determining the loop structure data of the target loop function according to the execution nodes and the execution sequence.
5. The method of claim 4, wherein the loop structure data is a key/value pair structure, and determining the loop structure data of the target loop function according to the execution nodes and the execution sequence comprises:
acquiring execution nodes on a preset execution path in the target cyclic function and a corresponding execution sequence;
and converting the execution nodes on the preset execution path in the target cyclic function and the corresponding execution sequence into a pass key/value pair structure to generate cyclic structure data.
6. The method of claim 4, after determining loop structure data for a target loop function according to the execution nodes and the execution order, further comprising:
converting the circular data structure into a preset dictionary structure, wherein the dictionary structure is a variable container model based on a key/value pair structure, and keys of the dictionary structure are used for representing initial execution nodes on an execution path; the values of the dictionary structure are used to characterize a list of end execution nodes on the execution path.
7. The method of any of claims 1-6, wherein detecting dead loops in the target loop function from the control flow graph comprises:
acquiring a cycle termination condition according to the control flow diagram;
and if the loop structure corresponding to the control flow graph cannot meet the loop termination condition, determining that the target loop function contains dead loops.
8. The method of any of claims 1-6, after obtaining the target loop function in the program source code, further comprising:
and judging whether the target circulation function contains absolute dead circulation or not according to a preset second regular expression rule.
9. The method of claim 8, wherein judging whether the target loop function includes an absolute dead loop according to a preset second regular expression rule comprises:
judging whether a jump-out instruction exists in the loop structure of the target loop function or not according to a preset second regular expression rule; and if the jump-out instruction does not exist, determining that the target loop function comprises the absolute dead loop.
10. A program loop detection apparatus, comprising:
the acquisition module is used for acquiring a target cyclic function in a program source code;
a determining module, configured to determine a control flow graph according to the target cyclic function, where the control flow graph is used to represent a cyclic structure of the target cyclic function;
and the detection module is used for detecting the dead loop in the target cyclic function according to the control flow graph.
11. An electronic device, comprising: a memory, a processor, and a computer program;
wherein the computer program is stored in the memory and configured to be executed by the processor to implement the program dead loop detection method of any of claims 1-9.
12. A computer-readable storage medium, on which a computer program is stored, which program, when being executed by a processor, is adapted to carry out the method of program loop detection of claims 1-9.
CN202010784325.9A 2020-08-06 2020-08-06 Program endless loop detection method, device, electronic equipment and storage medium Pending CN113760700A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010784325.9A CN113760700A (en) 2020-08-06 2020-08-06 Program endless loop detection method, device, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010784325.9A CN113760700A (en) 2020-08-06 2020-08-06 Program endless loop detection method, device, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN113760700A true CN113760700A (en) 2021-12-07

Family

ID=78785665

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010784325.9A Pending CN113760700A (en) 2020-08-06 2020-08-06 Program endless loop detection method, device, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN113760700A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116467220A (en) * 2023-06-13 2023-07-21 北京航空航天大学 Software static analysis-oriented cyclic code processing method and device

Citations (14)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20080307397A1 (en) * 2007-06-08 2008-12-11 Bill Angell Program Analysis by Partial Emulation
CN102385550A (en) * 2010-08-30 2012-03-21 北京理工大学 Detection method for software vulnerability
US20130031531A1 (en) * 2011-07-27 2013-01-31 Oracle International Corporation Method and system for performing backward-driven path-sensitive dataflow analysis
CN103164332A (en) * 2011-12-15 2013-06-19 迈普通信技术股份有限公司 Detection and processing method of endless loop task
CN103955426A (en) * 2014-04-21 2014-07-30 中国科学院计算技术研究所 Method and device for detecting code C null-pointer reference
US8826255B1 (en) * 2007-06-18 2014-09-02 The Mathworks, Inc. Restructuring control flow graphs generated from a model
CN105138335A (en) * 2015-08-28 2015-12-09 牟永敏 Function call path extracting method and device based on control flow diagram
CN106227573A (en) * 2016-07-11 2016-12-14 北京信息科技大学 Function call path extraction method based on controlling stream graph
WO2016202157A2 (en) * 2015-06-16 2016-12-22 深圳市中兴微电子技术有限公司 Random testing program generation method and device, apparatus, and storage medium
US20170017789A1 (en) * 2014-08-15 2017-01-19 Securisea, Inc. High Performance Software Vulnerabilities Detection System and Methods
WO2019223247A1 (en) * 2018-05-23 2019-11-28 华南理工大学 Unit testing method based on automatic generation of path coverage test use cases
CN110928778A (en) * 2019-11-19 2020-03-27 百富计算机技术(深圳)有限公司 Endless loop positioning method and device, computer equipment and storage medium
CN111240982A (en) * 2020-01-09 2020-06-05 华东师范大学 Static analysis method for source code
CN111488579A (en) * 2020-03-25 2020-08-04 腾讯科技(深圳)有限公司 Vulnerability detection method and device, electronic equipment and computer readable storage medium

Patent Citations (14)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20080307397A1 (en) * 2007-06-08 2008-12-11 Bill Angell Program Analysis by Partial Emulation
US8826255B1 (en) * 2007-06-18 2014-09-02 The Mathworks, Inc. Restructuring control flow graphs generated from a model
CN102385550A (en) * 2010-08-30 2012-03-21 北京理工大学 Detection method for software vulnerability
US20130031531A1 (en) * 2011-07-27 2013-01-31 Oracle International Corporation Method and system for performing backward-driven path-sensitive dataflow analysis
CN103164332A (en) * 2011-12-15 2013-06-19 迈普通信技术股份有限公司 Detection and processing method of endless loop task
CN103955426A (en) * 2014-04-21 2014-07-30 中国科学院计算技术研究所 Method and device for detecting code C null-pointer reference
US20170017789A1 (en) * 2014-08-15 2017-01-19 Securisea, Inc. High Performance Software Vulnerabilities Detection System and Methods
WO2016202157A2 (en) * 2015-06-16 2016-12-22 深圳市中兴微电子技术有限公司 Random testing program generation method and device, apparatus, and storage medium
CN105138335A (en) * 2015-08-28 2015-12-09 牟永敏 Function call path extracting method and device based on control flow diagram
CN106227573A (en) * 2016-07-11 2016-12-14 北京信息科技大学 Function call path extraction method based on controlling stream graph
WO2019223247A1 (en) * 2018-05-23 2019-11-28 华南理工大学 Unit testing method based on automatic generation of path coverage test use cases
CN110928778A (en) * 2019-11-19 2020-03-27 百富计算机技术(深圳)有限公司 Endless loop positioning method and device, computer equipment and storage medium
CN111240982A (en) * 2020-01-09 2020-06-05 华东师范大学 Static analysis method for source code
CN111488579A (en) * 2020-03-25 2020-08-04 腾讯科技(深圳)有限公司 Vulnerability detection method and device, electronic equipment and computer readable storage medium

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
钱雪忠 等: "《新编C语言程序设计教程》", 30 June 2020, 机械工业出版社, pages: 113 - 114 *
阮辉等: ""基于路径分析的死循环检测"", 《计算机学报》, vol. 32, no. 9, pages 1750 - 1758 *

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116467220A (en) * 2023-06-13 2023-07-21 北京航空航天大学 Software static analysis-oriented cyclic code processing method and device
CN116467220B (en) * 2023-06-13 2023-09-08 北京航空航天大学 Software static analysis-oriented cyclic code processing method and device

Similar Documents

Publication Publication Date Title
US5651111A (en) Method and apparatus for producing a software test system using complementary code to resolve external dependencies
CN104899147B (en) A kind of code Static Analysis Method towards safety inspection
CN104536898B (en) The detection method of c program parallel regions
Chen et al. Coverage prediction for accelerating compiler testing
CN111104335B (en) C language defect detection method and device based on multi-level analysis
Li et al. Precise semantic history slicing through dynamic delta refinement
Sottile et al. Semi-automatic extraction of software skeletons for benchmarking large-scale parallel applications
Edelkamp et al. Trail-directed model checking
CN113760700A (en) Program endless loop detection method, device, electronic equipment and storage medium
US20180217922A1 (en) Application testing
Saumya et al. Xstressor: Automatic generation of large-scale worst-case test inputs by inferring path conditions
Hills Variable feature usage patterns in PHP (T)
CN109359055B (en) Data testing method and device
CN109032946B (en) Test method and device and computer readable storage medium
CN115080978A (en) Runtime vulnerability detection method and system based on fuzzy test
Boockmann et al. Learning Data Structure Shapes from Memory Graphs.
Agarwal et al. Copilot Evaluation Harness: Evaluating LLM-Guided Software Programming
KR101306842B1 (en) System and method for generating verification conditions of java program
Attallah et al. Towards a COSMIC FSM Programming Language Compiler
CN112579440A (en) Method and device for determining virtual test dependent object
Sun et al. A hierarchical CPN model automatically generating method aiming at multithreading program algorithm error detection
Cui et al. Binary Code Vulnerability Location Identification with Fine-grained Slicing
CN116383070B (en) Symbol execution method for high MC/DC
Yokomori et al. Assessing the impact of framework changes using component ranking
KR101192438B1 (en) System and method for generating intermediate representation specification of java program

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination