Program analysis method and device, equipment and storage medium
By distinguishing and merging the dependency information of intermediate program nodes in program analysis, the original variable dependency analysis problem of the confounding effects of temporary variables is solved, and a more accurate and clear variable dependency representation is achieved, helping developers better understand and debug programs.
Patent Information
- Application Number
- CN202411373072.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-29
- Publication Date
- 2024-12-24
AI Technical Summary
The existing technology cannot intuitively reflect the original variable dependencies in the source code in program dependency analysis, especially because the dependencies of temporary variables are mixed together, resulting in the analysis results being not clear and accurate enough.
By obtaining the program dependency information of the intermediate program, it is determined that each intermediate program node is the original variable node or the temporary variable node, and merge the dependency information of the temporary variable node to the original variable node to generate clearer original variable dependency representation information.
Improve the accuracy and clarity of variable dependency analysis, and provide more intuitive dependency representation information of original variable data, which facilitates developers to debug and further develop.
Smart Images

Figure CN119179491A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of program analysis, and in particular to a program analysis method, apparatus, device and storage medium. Background Art
[0002] In modern software development, program analysis and optimization techniques are constantly evolving to meet the needs for efficient, reliable, and secure software. The analysis of program dependencies, especially the extraction of variable dependencies, is the core of key tasks such as compiler optimization, program debugging, code understanding, and security analysis. Currently, the analysis of program dependencies is performed by compiling the source program into an intermediate program through the compiler, and then performing program dependency analysis based on the intermediate program to obtain the program dependency analysis results.
[0003] However, although the program dependency analysis results in the prior art can also reflect the variable dependency, they often also involve the dependency of temporary variables generated during the compilation process and cannot intuitively reflect the dependency of the original variables in the source code. Summary of the invention
[0004] The present application provides a program analysis method, apparatus, device and storage medium, which can remove variable node dependency information related to temporary variable nodes, improve the accuracy and clarity of the original variable dependency analysis results, and then generate clearer and more intuitive variable dependency representation information of the original variable data, so as to facilitate developers to debug and further develop programs based on the dependency relationship of the original variables. The technical solution of the present application is as follows:
[0005] In one aspect, a program analysis method is provided, the method comprising:
[0006] Acquire program dependency information of an intermediate program corresponding to an original program, the program dependency information comprising: a plurality of intermediate program nodes and program node dependency information between the plurality of intermediate program nodes;
[0007] Based on the program node dependency information and the instruction type corresponding to each intermediate program node, determine each intermediate program node as an original variable node or a temporary variable node, and extract first variable node dependency information corresponding to each intermediate program node;
[0008] Merging the first variable node dependency information corresponding to each temporary variable node in the plurality of intermediate program nodes with the first variable node dependency information corresponding to the original variable node that depends on each temporary variable node, to obtain the second variable node dependency information corresponding to each original variable node in the plurality of intermediate program nodes;
[0009] Based on the second variable node dependency information, variable dependency representation information of original variable data in the original program is generated.
[0010] In another aspect, a program analysis device is provided, the device comprising:
[0011] A program dependency information acquisition module is used to acquire program dependency information of an intermediate program corresponding to an original program, wherein the program dependency information includes: program node dependency information between multiple intermediate program nodes and the multiple intermediate program nodes;
[0012] A variable dependency information extraction module, configured to determine each intermediate program node as an original variable node or a temporary variable node based on the program node dependency information and the instruction type corresponding to each intermediate program node, and extract first variable node dependency information corresponding to each intermediate program node;
[0013] A variable dependency information merging module, used to merge the first variable node dependency information corresponding to each temporary variable node in the plurality of intermediate program nodes into the first variable node dependency information corresponding to the original variable node that depends on each temporary variable node, to obtain the second variable node dependency information corresponding to each original variable node in the plurality of intermediate program nodes;
[0014] A variable dependency information representation module is used to generate variable dependency representation information of original variable data in the original program based on the second variable node dependency information.
[0015] On the other hand, a program analysis device is provided, which includes a processor and a memory, wherein the memory stores at least one instruction or at least one program segment, and the at least one instruction or the at least one program segment is loaded and executed by the processor to implement the program analysis method as described above.
[0016] On the other hand, a computer-readable storage medium is provided, in which at least one instruction or at least one program is stored, and the at least one instruction or the at least one program is loaded and executed by a processor to implement the program analysis method as described above.
[0017] The program analysis method, device, equipment and storage medium provided by this application have the following technical effects:
[0018] By utilizing the technical solution provided in the present application, program dependency information of an intermediate program corresponding to an original program is obtained, and the program dependency information includes: program node dependency information between multiple intermediate program nodes and multiple intermediate program nodes. Taking a root node among the multiple intermediate program nodes as a starting point, each intermediate program node is determined as an original variable node or a temporary variable node based on the program node dependency information and the instruction type corresponding to each intermediate program node, and the first variable node dependency information corresponding to each intermediate program node is extracted. This can improve the accuracy of variable dependency analysis on the basis of improving the accuracy of node variable classification. Then, the first variable node dependency information corresponding to each temporary variable node in the multiple intermediate program nodes is merged into the first variable node dependency information corresponding to the original variable node that depends on each temporary variable node, so as to obtain the second variable node dependency information corresponding to each original variable node in the multiple intermediate program nodes, and based on the second variable node dependency information, the variable dependency representation information of the original program is generated. By removing the first variable node dependency information related to the temporary variable node, the accuracy and clarity of the original variable dependency analysis result are improved, thereby generating clearer and more intuitive variable dependency representation information of the original variable data, so as to facilitate developers to debug and further develop programs based on the dependency relationship of the original variables. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the technical solutions and advantages in the embodiments of the present application or the prior art, the drawings required for use in the embodiments or the prior art descriptions are briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0020] Figure 1 It is a flowchart of a program analysis method provided in an embodiment of the present application;
[0021] Figure 2 It is a flow chart of a variable dependency analysis provided in an embodiment of the present application;
[0022] Figure 3 is a flow chart of another variable dependency analysis provided in an embodiment of the present application;
[0023] Figure 4 It is a flow chart of a variable dependency extraction process provided by an embodiment of the present application;
[0024] Figure 5 It is a flowchart of a temporary variable dependency replacement provided by an embodiment of the present application;
[0025] Figure 6 It is a flow chart of a variable-dependent optimization process provided in an embodiment of the present application;
[0026] Figure 7 It is a tree representation schematic diagram based on a DOT graph provided in an embodiment of the present application;
[0027] Figure 8 is a schematic diagram of a program analysis system provided in an embodiment of the present application;
[0028] Fig. 9 It is a schematic diagram of a program analysis device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0029] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.
[0030] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or server that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0031] In order to better understand the embodiments of the present application, some professional terms involved in the embodiments of the present application are introduced below:
[0032] Low Level Virtual Machine (LLVM): A compiler infrastructure that is free software written in C++ and includes a series of modular compiler components and tool chains.
[0033] Intermediate representation (IR): is a data structure or code used internally by a compiler or virtual machine to represent source code. IR can also be called an intermediate language. IR is designed to facilitate further processing, such as optimization and translation.
[0034] Program Data Flow Graph (DFG): The result of data flow analysis, which records the data assignment and storage, and memory reading and writing processes during the execution of the code on different paths.
[0035] Data Dependence Graph (DDG): The nodes of DDG represent code statements, and the directed edges represent the data dependency relationships between code statements. If the definition of a variable in statement A can reach the use of the variable in statement B, then statement B is considered to have data dependency on statement A.
[0036] Program Control Flow Graph (CFG): The result of control flow analysis is a directed graph in which the nodes represent basic blocks, the directed edges between the nodes represent the control flow paths, and the reverse edges represent possible loops.
[0037] Basic block: A basic block is a sequence of statements in a program that is executed in the most sequential order. It has only one entry and exit, with the entry being the first statement and the exit being the last statement. The entry statement of a basic block can be the first statement of the program, the target statement of a jump, or the next statement of a conditional jump; the exit statement of a basic block can be a stop statement, a jump statement, or the previous statement of the jump target statement (lexical order). Each basic block satisfies the following conditions: the control flow can only enter from the first instruction of the basic block, and, except for the last instruction of the basic block, the control flow will not stop or jump before leaving the basic block. Therefore, a typical feature of a basic block is that as long as the first instruction in the basic block is executed, all executions in the basic block will be executed only once in sequence.
[0038] Dominator Tree: It is used to represent the dominance relationship between nodes in the control flow graph of a program. If for every path from the entry node (usually the starting point of the program or the entry point of a function) to node n, node d appears on these paths, then node d dominates node n (denoted as d dom n).
[0039] Control Dependence Graph (CDG): A flow chart that can be used to represent the branch dependencies of basic blocks.
[0040] Program Dependency Graph (PDG): It is composed of a control dependency graph and a data dependency graph, and is divided into control dependency and data dependency.
[0041] DOT diagram: A graphical description language that is widely used to describe structured diagrams.
[0042] JSON (JavaScript Object Notation) is a lightweight data exchange format.
[0043] The following describes a program analysis method provided by an embodiment of the present application. Figure 1 A flowchart of a program analysis method provided for an embodiment of the present application. It should be noted that this specification provides method operation steps as described in the embodiment or flowchart, but may include more or fewer operation steps based on conventional or non-creative labor. The order of steps listed in the embodiment is only one way of executing the steps among many orders, and does not represent the only order of execution. When the actual system or product is executed, it can be executed in sequence or in parallel according to the method shown in the embodiment or the accompanying drawings (for example, in a parallel processor or multi-threaded processing environment). Specifically, Figure 1 As shown, the above method may include:
[0044] S101, obtaining program dependency information of an intermediate program corresponding to an original program, wherein the program dependency information includes: a plurality of intermediate program nodes and program node dependency information between the plurality of intermediate program nodes.
[0045] In a specific embodiment, the original program may be a source program, that is, a readable computer language instruction that has not been compiled. The present application does not limit the programming language of the original program. Schematically, the programming language of the original program may include but is not limited to: C language, C++, Java, etc.
[0046] In a specific embodiment, the intermediate program is obtained by compiling the original program by a compiler. Schematically, the intermediate program here may be an LLVM IR program, and the compiler here may be Clang or other similar compilation tools.
[0047] In a specific embodiment, the program node dependency information is used to characterize the node dependency relationship between multiple intermediate program nodes, where the node dependency relationship may include: data dependency relationship and control dependency relationship. Specifically, the intermediate program may be composed of multiple program statements (i.e., code instructions), and each intermediate program node may be used to represent one program statement among the multiple program statements. In a schematic manner, the representation of the program node dependency information may include, but is not limited to: a program dependency graph, a program dependency list, etc.
[0048] In a specific embodiment, program node dependency information may include: program control dependency information and program data dependency information. The program control dependency information can be used to characterize the control dependency relationship between multiple intermediate program nodes. Schematically, the expression form of the program control dependency information may include but is not limited to: a program control dependency graph, a program control dependency list, etc.; the program data dependency information can be used to characterize the data dependency relationship between multiple intermediate program nodes. Schematically, the expression form of the program data dependency information may include but is not limited to: a program data dependency graph, a program data dependency list, etc.
[0049] In an optional embodiment, a program control flow graph and a program data flow graph may be extracted from the intermediate program, and a control dependency graph and a dominator tree may be generated based on the program control flow graph, and a data dependency graph may be generated based on the program data flow graph.
[0050] In an optional embodiment, the program node dependency information between the above-mentioned multiple intermediate program nodes is obtained in the following manner:
[0051] S1011, obtaining initial program dependency information of the intermediate program, where the initial program dependency information includes: a plurality of initial intermediate program nodes and initial node dependency information between the plurality of initial intermediate program nodes.
[0052] Specifically, the program control flow graph and program data flow graph can be extracted from the intermediate program, a control dependency graph and a dominance tree can be generated based on the program control flow graph, a data dependency graph can be generated based on the program data flow graph, and initial program dependency information can be generated based on the control dependency graph and the program data flow graph.
[0053] In a specific embodiment, the initial node dependency information is used to characterize the node dependency relationship between multiple initial intermediate program nodes, where the node dependency relationship may include: data dependency relationship and control dependency relationship.
[0054] S1012, determining a slicing criterion node containing target variable data among a plurality of initial intermediate program nodes.
[0055] Specifically, the target variable data may be variable data of interest to the user among multiple variable data of the original program, and each variable data may include: a variable name and variable parameters.
[0056] In a specific embodiment, a slicing criterion node may be determined from a plurality of initial intermediate program nodes based on the variables of interest and line numbers input by the user.
[0057] S1013, based on the slicing criterion node and the initial node dependency information, reversely slice the initial program dependency information to determine the program node dependency information between multiple intermediate program nodes in the multiple initial intermediate program nodes and each intermediate program node, wherein the root node in the multiple intermediate program nodes is the slicing criterion node.
[0058] Specifically, the initial program dependency information can be expressed as an initial program dependency graph. Starting from the slicing criterion node, the initial sequence dependency graph is traversed to mark the associated initial intermediate program nodes (i.e., the code instructions that affect the target variable data). After the traversal is completed, the unmarked initial intermediate program nodes are removed, and the initial program dependency graph is optimized to obtain the optimized initial program dependency graph (i.e., program node dependency information).
[0059] It can be seen from the above embodiments that the intermediate program is sliced in reverse, code nodes that have no effect on the variables of interest are deleted, and program dependency information is optimized, thereby simplifying subsequent analysis.
[0060] S102, based on the program node dependency information and the instruction type corresponding to each intermediate program node, determine each intermediate program node as an original variable node or a temporary variable node, and extract first variable node dependency information corresponding to each intermediate program node.
[0061] In a specific embodiment, the original variable node can be an intermediate program node containing original variable data, and the original variable data is the variable data defined in the original program; the temporary variable node can be an intermediate program node containing temporary variable data, and the temporary variable data is the variable data defined in the intermediate program but not defined in the original program.
[0062] In a specific embodiment, the first variable node dependency information corresponding to each intermediate program node is used to characterize the dependency relationship between the program variable in the corresponding intermediate program node and the program variables in other intermediate program nodes.
[0063] In a specific embodiment, Figure 2 As shown, the above-mentioned determining each intermediate program node as an original variable node or a temporary variable node based on the program node dependency information and the instruction type corresponding to each intermediate program node, and extracting the first variable node dependency information corresponding to each intermediate program node includes:
[0064] S201, taking a root node among a plurality of intermediate program nodes as a first current analysis node.
[0065] S202, based on the program node dependency information, determine the second node on which the first current analysis node data depends from multiple intermediate program nodes, based on the instruction type corresponding to the second node, determine whether the second node is an original variable node or a temporary variable node, and add the second node to the first set of nodes to be analyzed and the variable dependency node set of the first current analysis node respectively.
[0066] Specifically, the program node dependency information may include: program data dependency information. The above-mentioned determining, from multiple intermediate program nodes, the second node on which the first current analysis node data depends based on the program node dependency information may include: determining, from multiple intermediate program nodes, the second node on which the first current analysis node data depends based on the program data dependency information.
[0067] Specifically, the first to-be-analyzed node set is used to store the to-be-analyzed nodes that require variable dependency analysis. The variable dependency node set of the first current analysis node is used to store the intermediate program nodes on which the first current analysis node has variable dependency.
[0068] In a specific embodiment, the determining, based on the instruction type corresponding to the second node, whether the second node is an original variable node or a temporary variable node may include:
[0069] S2021, when the instruction type corresponding to the second node is a memory access type, determining that the second node is an original variable node;
[0070] Or, S2022, when the instruction type corresponding to the second node is not a memory access type, determine that the second node is a temporary variable node.
[0071] Specifically, the code instructions of the memory access type need to access the memory to read variable data from the memory or write variable data to the memory. Schematically, the memory access type may include but is not limited to: load instructions (load instructions, used to read data from the memory and load it into the register) and store instructions (store instructions, used to write data from the register to the memory). Code instructions other than the memory access type are usually an intermediate operation process. Since the temporary variables generated by the intermediate operation process can be directly used for subsequent calculations, they do not need to be stored in the memory; therefore, when the instruction type corresponding to the second node is a memory access type, the second node is determined to be an original variable node; when the instruction type corresponding to the second node is not a memory access type, the second node is determined to be a temporary variable node.
[0072] It can be seen from the above embodiments that determining whether the second node is an original variable node or a temporary variable node based on the instruction type corresponding to the second node can effectively improve the accuracy of node variable type classification and effectively identify temporary variable nodes containing temporary variable data.
[0073] S203: Update the first current analysis node based on any node to be analyzed in the first set of nodes to be analyzed to obtain an updated first current analysis node.
[0074] Specifically, any node to be analyzed is taken from the first set of nodes to be analyzed as the updated first current analysis node.
[0075] S204, based on the updated first current analysis node, jump to the step of determining a second node on which the first current analysis node data depends from a plurality of intermediate program nodes based on the program node dependency information, until the number of nodes in the first set of nodes to be analyzed is zero.
[0076] S205 , generating first variable node dependency information corresponding to each intermediate program node based on the current variable dependency node set of each intermediate program node.
[0077] Specifically, the current variable dependency node set of each intermediate program node is used as the first variable node dependency information corresponding to each intermediate program node. Specifically, the current variable dependency node set of each intermediate program node is the variable dependency node set of each intermediate program node obtained when the number of nodes in the first node set to be analyzed is zero.
[0078] It can be seen from the above embodiments that, starting from the root node among multiple intermediate program nodes, the data dependent nodes of the current analysis node are determined in sequence, and the intermediate program nodes corresponding to the non-memory access instructions are identified as temporary variable results, thereby extracting the direct and indirect dependencies between the variables in the original sequence.
[0079] In a specific embodiment, Figure 3 As shown, before updating the first current analysis node based on any node to be analyzed in the set of nodes to be analyzed to obtain the updated first current analysis node, the method may further include:
[0080] S206: When the first current analysis node and the second node do not belong to the same basic block, determine the target basic block that dominates the basic block where the second node is located according to the program dependency information.
[0081] Specifically, the first current analysis node and the second node do not belong to the same basic block, which means that the code instructions corresponding to the first current analysis node and the code instructions corresponding to the second node belong to different and logically independent instruction sequences in the program respectively. These sequences will not directly jump to each other during execution, but are connected through other parts of the program.
[0082] In a specific embodiment, the program dependency information may include a dominator tree. Determining the target basic block that dominates the basic block where the second node is located according to the program dependency information may include determining the target basic block that dominates the basic block where the second node is located according to the dominator tree.
[0083] Specifically, the target basic block dominates the basic block where the second node is located, which means that all paths from the entry point of the program to the basic block where the second node is located must pass through the target basic block.
[0084] S207, when the instruction type of the last instruction in the target basic block is a jump type, the intermediate program node corresponding to the last instruction is used as the third node controlled and depended by the second node, and the third node is added to the first node set to be analyzed and the variable dependency node set of the second node respectively.
[0085] Specifically, jump-type code instructions involve jumps between different basic blocks. Schematically, the jump types may include but are not limited to: conditional branch instructions (br instructions, used to jump to a specified basic block based on conditions) and multi-way branch instructions (switch instructions, used to jump to different basic blocks based on input values).
[0086] Specifically, when the instruction type of the last instruction in the target basic block is a jump type, since the target basic block dominates the basic block where the second node is located, all paths to the basic block where the second node is located must pass through the last instruction, that is, the second node has a control dependency on the intermediate program node corresponding to the last instruction.
[0087] It can be seen from the above embodiments that for data dependencies across basic blocks, the control conditions corresponding to the data dependencies are analyzed according to the dominator tree, control dependency nodes are generated, and the association relationship between the data dependency nodes and the control dependency nodes is saved to facilitate the subsequent display of the program execution path containing the execution conditions (control dependencies).
[0088] Indicative, Figure 4 is a flow chart of a variable dependency extraction process provided by an embodiment of the present application. Specifically, Figure 4As shown, starting from the root node among multiple intermediate program nodes (in the program dependency graph after slicing, the root node is the slicing criterion node), find the data dependency node nodeB of the current analysis node nodeA, and for the newly found data dependency node, continue to find its data dependency node, and repeat this process; for data dependencies caused by non-load / store instructions, mark them as data dependencies on a fake node (temporary variable node); for data dependencies across basic blocks, analyze the control conditions corresponding to the data dependencies according to the dominator tree, generate the control dependency node nodeC of nodeB, and save the association between the data dependency nodes and the control dependency nodes.
[0089] S103, merging the first variable node dependency information corresponding to each temporary variable node in the multiple intermediate program nodes into the first variable node dependency information corresponding to the original variable node that depends on each temporary variable node, to obtain the second variable node dependency information corresponding to each original variable node in the multiple intermediate program nodes.
[0090] In a specific embodiment, the second variable node dependency information corresponding to each original variable node is used to characterize the dependency relationship of the original variable in each original variable node on other original variable nodes.
[0091] In a specific embodiment, the first variable node dependency information corresponding to each intermediate program node may include: a variable dependency node set of each intermediate program node, such as Figure 5 As shown, the above-mentioned merging of the first variable node dependency information corresponding to each temporary variable node in the multiple intermediate program nodes into the first variable node dependency information corresponding to the original variable node that depends on each temporary variable node, and obtaining the second variable node dependency information corresponding to each original variable node in the multiple intermediate program nodes may include:
[0092] S501: Use a root node among a plurality of intermediate program nodes as a second current analysis node.
[0093] S502, when the second current analysis node is a temporary variable node, delete the second current analysis node from the variable dependency node set of the fourth node, and add the intermediate program nodes in the variable dependency node set of the second current analysis node to the variable dependency node set of the fourth node, the fourth node is the intermediate program node corresponding to the variable dependency node set to which the second current analysis node belongs.
[0094] Specifically, if the second current analysis node exists in the variable dependency node set corresponding to a certain intermediate program node, it can be considered that the second current analysis node belongs to the variable dependency node set corresponding to the certain intermediate program node. Accordingly, it can be considered that the intermediate program node has a variable dependency on the second current analysis node.
[0095] Specifically, when the second current analysis node is a temporary variable node, the second current analysis node is deleted from the variable dependency node set of the fourth node, and the intermediate program node in the variable dependency node set of the second current analysis node is added to the variable dependency node set of the fourth node, so that the variable dependency on the temporary variable node can be replaced with the dependency on the original variable node that the temporary variable node depends on.
[0096] S503: Add the intermediate program nodes in the variable dependency node set of the second current analysis node to the second set of nodes to be analyzed.
[0097] Specifically, the second set of nodes to be analyzed is used to store nodes to be analyzed that require variable dependency optimization.
[0098] S504: Update the second current analysis node based on any node to be analyzed in the second set of nodes to be analyzed to obtain an updated second current analysis node.
[0099] Specifically, any node to be analyzed is taken from the second set of nodes to be analyzed as the updated second current analysis node.
[0100] S505 , based on the updated second current analysis node, jump to the step of deleting the second current analysis node from the variable dependent node set of the fourth node when the second current analysis node is a temporary variable node, until the number of nodes in the second node set to be analyzed is zero.
[0101] S506: Generate second variable node dependency information corresponding to each original variable node based on the current variable dependency node set of each original variable node.
[0102] Specifically, the current variable dependency node set of each original variable node is used as the second variable node dependency information corresponding to each original variable node. Specifically, the current variable dependency node set of each original variable node is the variable dependency node set of each original variable node obtained when the number of nodes in the second node set to be analyzed is zero.
[0103] It can be seen from the above embodiments that the variable node dependency information corresponding to each temporary variable node in multiple intermediate program nodes is merged into the variable node dependency information corresponding to the original variable node that depends on each temporary variable node, so as to obtain the optimized variable node dependency information corresponding to each original variable node in multiple intermediate program nodes. By removing the variable node dependency information related to the temporary variable nodes, the accuracy and clarity of the original variable dependency analysis results are improved.
[0104] Indicative, Figure 6is a flow chart of a variable-dependent optimization process provided by an embodiment of the present application, specifically, Figure 6 As shown, starting from the root node among multiple intermediate program nodes (in the program dependency graph after slicing, the root node is the slicing criterion node), the dependency on a fake node (temporary variable node) is replaced with the dependency on the dependent node of the fake node. If the dependent node of the fake node is also a fake node, the replacement operation is repeated until there is no dependency on any fake node.
[0105] S104: Generate variable dependency representation information of original variable data in the original program based on the second variable node dependency information.
[0106] In a specific embodiment, the variable dependency representation information is used to characterize the data dependency relationship and control dependency relationship between original variable data in the original program.
[0107] In a specific embodiment, the second variable node dependency information corresponding to each original variable node may include: data dependency information corresponding to each original variable node and control dependency information corresponding to each original variable node. Accordingly, the data dependency information corresponding to each original variable node can be used to indicate that each original variable node has a data-dependent original variable node, and the control dependency information corresponding to each original variable node can be used to indicate that each original variable node has a control-dependent original variable node.
[0108] In a specific embodiment, the above-mentioned generating the variable dependency representation information of the original variable data in the original program based on the second variable node dependency information may include:
[0109] S1041, determining a target representation method from a plurality of preset representation methods.
[0110] In a specific embodiment, multiple preset representation methods may be representation methods of variable dependency representation information. Schematically, the multiple preset representation methods may include but are not limited to: a DOT-based tree representation method and a JSON-based text representation method.
[0111] S1042, based on the target representation mode, data dependency information and control dependency information, the original variable data in each original variable node is represented by dependency relationship to obtain variable dependency representation information.
[0112] In a specific embodiment, each original variable node with variable dependencies can be represented as a child node of each original variable node, thereby generating an initial tree structure expressing the original variable dependencies, and then starting from the root node of the initial tree structure, the original variable data and line numbers corresponding to each node are obtained in turn, and based on the target representation method, different types of dependency representations are performed on the data dependency and control dependency of the original variable data to obtain a variable dependency representation file corresponding to the target representation method.
[0113] For example, take the source code of the following original program as an example:
[0114]
[0115] Specifically, the variable m can be used as a variable of concern to the user. In an optional embodiment, the original variable dependency representation based on the DOT graph is performed on the above source code using the program analysis method provided in the embodiment of the present application, and the following can be obtained: Figure 7 The tree representation diagram based on the DOT graph is shown. Specifically, in the tree representation based on the DOT graph, each node represents an original variable data, including the variable name, the function name, the code line number, etc. The arrow of the node points to the original variable data on which it depends. Data dependency and control dependency can be represented by different arrow types. Schematically, solid arrows represent data dependency, and dotted arrows represent control dependency. This representation method makes it convenient for program developers to intuitively understand the dependency relationship of variables, which can assist in program debugging and program understanding.
[0116] In another optional embodiment, the above source code is subjected to JSON-based original variable dependency representation using the program analysis method provided in the embodiment of the present application, and the following JSON-based representation text can be obtained:
[0117]
[0118] Specifically, in the JSON-based text representation, the same information as the tree representation is contained, and the child nodes represent the dependent nodes of the parent nodes. This representation method is convenient for other tools to do further development based on the dependency relationship of variables, such as functional safety analysis of IOS26262 (international standard for functional safety of road vehicles).
[0119] In addition, the program dependency graph provided by the prior art is a graphical structure that may contain a ring. When a node points to multiple dependent nodes, it is impossible to see which dependent node a branch of the original program should actually point to. For example, the variable m in the above source code will be assigned multiple times under different conditions, so there will be multiple data dependencies and associated control dependencies. Therefore, this dependency graph that superimposes all dependencies can be regarded as a static dependency result; and in the embodiment of the present application, it can be clearly seen from the variable dependency representation information whether each data dependency (program execution path) has a control condition (i.e., the corresponding control dependency), therefore, the variable dependency representation information provided in the embodiment of the present application can be regarded as a dynamic dependency result.
[0120] It can be seen from the above embodiments that, on the one hand, the dependency relationship between the original variables in the source program can be represented by both graphical display and JSON text display, which is convenient for developers to debug and do further development based on the dependency relationship; on the other hand, compared with the static dependency results provided by the prior art, the dynamic dependency results provided in the embodiments of the present application can intuitively display the execution path of the program and the variables on which the control conditions on each execution path depend.
[0121] The present application embodiment provides a program analysis system, such as Figure 8 As shown, the above system may include: a program dependency information generation subsystem 810, a program slicing subsystem 820, a variable dependency extraction subsystem 830 and a variable dependency representation subsystem 840, wherein:
[0122] The program dependency information generation subsystem 810 may include: a program dependency analyzer, a control dependency graph generator, and a data dependency graph generator. Specifically, the program dependency information generation subsystem 810 is responsible for extracting control flow and data flow information from the intermediate program compiled from the original program, and constructing a program dependency graph, including a control dependency graph (CDG) and a data dependency graph (DDG) and a dominator tree.
[0123] The program slicing subsystem 820 may include: a slicing criterion extractor and a slicing algorithm. Specifically, the program slicing subsystem 820 can find the corresponding slicing criterion node based on the concerned variables and line numbers input by the user, and traverse the program dependency graph with the slicing criterion node as the starting point to mark the associated instruction nodes. After the traversal is completed, the unmarked instruction nodes are removed to optimize the program dependency graph, thereby simplifying the analysis of complex programs.
[0124] The variable dependency extraction subsystem 830 may include: a variable dependency analyzer and a variable dependency storage. Specifically, the variable dependency extraction subsystem 830 may analyze the sliced program dependency graph, extract and store the complex data dependency between variables. According to the concerned variables and line numbers provided by the user, the corresponding slicing criterion node is found, and its data dependency node is found from the slicing criterion node. For the newly found data dependency node, continue to find its data dependency node, and repeat this process; for dependencies caused by non-load / store instructions, it is marked as a dependency on a fake node; for data dependencies across basic blocks, the corresponding control conditions are analyzed according to the dominator tree, a control dependency node is generated, and the association between the data dependency node and the control dependency node is saved.
[0125] The variable dependency representation subsystem 840 may include: a variable dependency optimizer, a DOT converter, and a JSON converter. Specifically, the dependency representation subsystem 840 may optimize the dependency based on the extracted variable dependency and output the analysis result. Based on the variable dependency extracted by the previous module, the dependency on a fake node is removed and replaced with the dependency on the dependency node of the fake node; if the dependency node of the fake node is also a fake node, the replacement operation is repeated; the final optimized variable dependency result will not include the dependency on the temporary variables generated by the compiler; the DOT converter represents the optimized variable dependency result as a DOT graph; the JSON converter represents the optimized variable dependency result as a JSON text.
[0126] The system and method embodiments in the above-mentioned system embodiments are based on the same inventive concept.
[0127] The present application embodiment provides a program analysis device, such as Fig. 9 As shown, the above device may include:
[0128] A program dependency information acquisition module 910 is used to acquire program dependency information of an intermediate program corresponding to an original program, wherein the program dependency information includes: program node dependency information between multiple intermediate program nodes and multiple intermediate program nodes;
[0129] A variable dependency information extraction module 920, for determining each intermediate program node as an original variable node or a temporary variable node based on the program node dependency information and the instruction type corresponding to each intermediate program node, and extracting first variable node dependency information corresponding to each intermediate program node;
[0130] A variable dependency information merging module 930 is used to merge the first variable node dependency information corresponding to each temporary variable node in the plurality of intermediate program nodes into the first variable node dependency information corresponding to the original variable node that depends on each temporary variable node, to obtain the second variable node dependency information corresponding to each original variable node in the plurality of intermediate program nodes;
[0131] The variable dependency information representation module 940 is used to generate variable dependency representation information of original variable data in the original program based on the second variable node dependency information.
[0132] In a specific embodiment, the program node dependency information between the above-mentioned multiple intermediate program nodes is obtained by the following means:
[0133] An initial program dependency information acquisition module is used to acquire initial program dependency information of an intermediate program, wherein the initial program dependency information includes: initial node dependency information between multiple initial intermediate program nodes and multiple initial intermediate program nodes;
[0134] A slicing criterion node determination module, used to determine a slicing criterion node containing target variable data in a plurality of initial intermediate program nodes;
[0135] The slicing module is used to reversely slice the initial program dependency information based on the slicing criterion node and the initial node dependency information, and determine the program node dependency information between multiple intermediate program nodes in the multiple initial intermediate program nodes and each intermediate program node, wherein the root node in the multiple intermediate program nodes is the slicing criterion node.
[0136] In a specific embodiment, the variable dependency information extraction module 920 includes:
[0137] A first current analysis node determination unit, configured to use a root node among the plurality of intermediate program nodes as a first current analysis node;
[0138] A variable data dependency analysis unit, configured to determine, based on program node dependency information, from a plurality of intermediate program nodes, a second node on which the first current analysis node data depends, determine, based on an instruction type corresponding to the second node, whether the second node is an original variable node or a temporary variable node, and add the second node to the first set of nodes to be analyzed and the set of variable dependency nodes of the first current analysis node, respectively;
[0139] A first current analysis node updating unit, configured to update the first current analysis node based on any node to be analyzed in the first set of nodes to be analyzed, to obtain an updated first current analysis node;
[0140] A first jump unit, configured to jump to a step of determining, based on the program node dependency information, a second node on which the first current analysis node data depends from a plurality of intermediate program nodes based on the updated first current analysis node, until the number of nodes in the first set of nodes to be analyzed is zero;
[0141] The first variable node dependency information generating unit is used to generate first variable node dependency information corresponding to each intermediate program node based on the current variable dependency node set of each intermediate program node.
[0142] In a specific embodiment, the variable analysis unit may include:
[0143] A temporary variable node determination unit, configured to determine that the second node is an original variable node when the instruction type corresponding to the second node is a memory access type;
[0144] The original variable node determination unit is used to determine that the second node is a temporary variable node when the instruction type corresponding to the second node is not a memory access type.
[0145] In a specific embodiment, the above device may further include:
[0146] A target basic block determination unit, configured to determine, when the first current analysis node and the second node do not belong to the same basic block, a target basic block that dominates the basic block where the second node is located according to program dependency information;
[0147] The variable control dependency analysis unit is used to, when the instruction type of the last instruction in the target basic block is a jump type, use the intermediate program node corresponding to the last instruction as the third node controlled and depended by the second node, and add the third node to the first node set to be analyzed and the variable dependency node set of the second node respectively.
[0148] In a specific embodiment, the first variable node dependency information corresponding to each intermediate program node may include: a variable dependency node set of each intermediate program node, and the variable dependency information merging module 930 may include:
[0149] A second current analysis node determination unit, configured to use a root node among the plurality of intermediate program nodes as a second current analysis node;
[0150] a temporary variable node deletion unit, used for deleting the second current analysis node from the variable dependency node set of the fourth node when the second current analysis node is a temporary variable node, and adding the intermediate program node in the variable dependency node set of the second current analysis node to the variable dependency node set of the fourth node, the fourth node being the intermediate program node corresponding to the variable dependency node set to which the second current analysis node belongs;
[0151] A second to-be-analyzed node set updating unit, configured to add intermediate program nodes in the variable dependent node set of the second current analysis node to the second to-be-analyzed node set;
[0152] A second current analysis node updating unit, configured to update the second current analysis node based on any node to be analyzed in the second set of nodes to be analyzed, to obtain an updated second current analysis node;
[0153] A second jump unit, configured to jump to a step of deleting the second current analysis node from the variable dependent node set of the fourth node based on the updated second current analysis node, when the second current analysis node is a temporary variable node, until the number of nodes in the second set of nodes to be analyzed is zero;
[0154] The second variable node dependency information generating unit is used to generate the second variable node dependency information corresponding to each original variable node based on the current variable dependency node set of each original variable node.
[0155] In a specific embodiment, the second variable node dependency information corresponding to each original variable node may include: data dependency information corresponding to each original variable node and control dependency information corresponding to each original variable node. The variable dependency information representation module 940 may include:
[0156] A target representation mode determining unit, used to determine a target representation mode from a plurality of preset representation modes;
[0157] The variable dependency representation unit is used to represent the dependency of the original variable data in each original variable node based on the target representation mode, data dependency information and control dependency information to obtain variable dependency representation information.
[0158] The device and method embodiments in the above-mentioned device embodiments are based on the same inventive concept.
[0159] An embodiment of the present application provides a program analysis device, which includes a processor and a memory, wherein the memory stores at least one instruction or at least one program, and the at least one instruction or the at least one program is loaded and executed by the processor to implement the program analysis method provided in the above method embodiment.
[0160] The memory can be used to store software programs and modules. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory. The memory can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, application programs required for functions, etc.; the data storage area can store data created according to the use of the above devices, etc. In addition, the memory can include a high-speed random access memory and can also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices. Accordingly, the memory can also include a memory controller to provide the processor with access to the memory.
[0161] The method embodiments provided in the embodiments of the present application can be executed in a mobile terminal, a computer terminal, a server or a similar computing device, that is, the above-mentioned computer device may include a mobile terminal, a computer terminal, a server or a similar computing device.
[0162] An embodiment of the present application also provides a storage medium, which can be set in a server to store at least one instruction or at least one program related to a program analysis method for implementing one of the method embodiments. The at least one instruction or the at least one program is loaded and executed by the processor to implement the program analysis method provided by the above method embodiment.
[0163] Optionally, in this embodiment, the storage medium may be located in at least one of the multiple network servers of the computer network. Optionally, in this embodiment, the storage medium may include, but is not limited to, various media that can store program codes, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk, or an optical disk.
[0164] It can be seen from the embodiments of the program analysis method, device, equipment or storage medium provided by the present application that by obtaining the program dependency information of the intermediate program corresponding to the original program, the program dependency information includes: multiple intermediate program nodes and program node dependency information between multiple intermediate program nodes, taking the root node among the multiple intermediate program nodes as the starting point, based on the program node dependency information and the instruction type corresponding to each intermediate program node, each intermediate program node is determined as an original variable node or a temporary variable node, and the first variable node dependency information corresponding to each intermediate program node is extracted. On the basis of improving the accuracy of node variable classification, the accuracy of variable dependency analysis can be improved, and then the first variable node corresponding to each temporary variable node in the multiple intermediate program nodes is extracted. The point dependency information is merged into the first variable node dependency information corresponding to the original variable node that depends on each temporary variable node, and the second variable node dependency information corresponding to each original variable node in the multiple intermediate program nodes is obtained. By removing the first variable node dependency information related to the temporary variable node, the accuracy and clarity of the original variable dependency analysis results are improved, and the dependency relationship between the original variables in the source program is represented by two methods: graphical display and JSON text display, which is convenient for developers to debug and do further development based on the dependency relationship; in addition, compared with the static dependency results provided by the prior art, the dynamic dependency results provided in the embodiments of the present application can intuitively display the execution path of the program and the variables on which the control conditions on each execution path depend.
[0165] It should be noted that the above-mentioned sequence of the embodiments of the present application is for description only and does not represent the advantages and disadvantages of the embodiments. The above-mentioned specific embodiments of this specification are described. Other embodiments are within the scope of the attached claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0166] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device, equipment and storage medium embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0167] A person skilled in the art will appreciate that all or part of the steps to implement the above embodiments may be accomplished by hardware, or may be accomplished by instructing the relevant hardware through a program, and the above program may be stored in a computer-readable storage medium, and the above-mentioned storage medium may be a read-only memory, a disk or an optical disk, etc.
[0168] The above is only a preferred embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application should be included in the protection scope of the present application.
Claims
1. A program analysis method, characterized in that: The method comprises: Acquire program dependency information of an intermediate program corresponding to an original program, the program dependency information comprising: a plurality of intermediate program nodes and program node dependency information between the plurality of intermediate program nodes; Based on the program node dependency information and the instruction type corresponding to each intermediate program node, determine each intermediate program node as an original variable node or a temporary variable node, and extract first variable node dependency information corresponding to each intermediate program node; Merging the first variable node dependency information corresponding to each temporary variable node in the plurality of intermediate program nodes with the first variable node dependency information corresponding to the original variable node that depends on each temporary variable node, to obtain the second variable node dependency information corresponding to each original variable node in the plurality of intermediate program nodes; Based on the second variable node dependency information, variable dependency representation information of original variable data in the original program is generated.
2. The method according to claim 1, characterized in that: The determining each intermediate program node as an original variable node or a temporary variable node based on the program node dependency information and the instruction type corresponding to each intermediate program node, and extracting the first variable node dependency information corresponding to each intermediate program node comprises: Using a root node among the plurality of intermediate program nodes as a first current analysis node; Based on the program node dependency information, determine a second node on which the first current analysis node data depends from the multiple intermediate program nodes, determine whether the second node is an original variable node or a temporary variable node based on the instruction type corresponding to the second node, and add the second node to the first to-be-analyzed node set and the variable dependency node set of the first current analysis node respectively; updating the first current analysis node based on any node to be analyzed in the first set of nodes to be analyzed to obtain an updated first current analysis node; Jumping to the step of determining, based on the program node dependency information, a second node on which the first current analysis node data depends from the plurality of intermediate program nodes based on the updated first current analysis node, until the number of nodes in the first set of nodes to be analyzed is zero; Based on the current variable dependency node set of each intermediate program node, first variable node dependency information corresponding to each intermediate program node is generated.
3. The method according to claim 2, characterized in that The determining, based on the instruction type corresponding to the second node, whether the second node is an original variable node or a temporary variable node comprises: When the instruction type corresponding to the second node is a memory access type, determining that the second node is an original variable node; Or, when the instruction type corresponding to the second node is not the memory access type, the second node is determined to be a temporary variable node.
4. The method according to claim 2, characterized in that: Before updating the first current analysis node based on any node to be analyzed in the set of nodes to be analyzed to obtain an updated first current analysis node, the method further includes: In a case where the first current analysis node and the second node do not belong to the same basic block, determining, according to the program dependency information, a target basic block that dominates the basic block where the second node is located; When the instruction type of the last instruction in the target basic block is a jump type, the intermediate program node corresponding to the last instruction is used as the third node controlled and depended by the second node, and the third node is added to the first set of nodes to be analyzed and the variable dependency node set of the second node respectively.
5. The method according to claim 1, characterized in that The first variable node dependency information corresponding to each intermediate program node includes: a variable dependency node set of each intermediate program node, and the first variable node dependency information corresponding to each temporary variable node in the plurality of intermediate program nodes is merged into the first variable node dependency information corresponding to the original variable node that depends on each temporary variable node, and the second variable node dependency information corresponding to each original variable node in the plurality of intermediate program nodes is obtained, including: Using a root node among the plurality of intermediate program nodes as a second current analysis node; In the case where the second current analysis node is a temporary variable node, deleting the second current analysis node from the variable dependency node set of the fourth node, and adding the intermediate program nodes in the variable dependency node set of the second current analysis node to the variable dependency node set of the fourth node, the fourth node being the intermediate program node corresponding to the variable dependency node set to which the second current analysis node belongs; Adding the intermediate program nodes in the variable dependent node set of the second current analysis node to the second set of nodes to be analyzed; updating the second current analysis node based on any node to be analyzed in the second set of nodes to be analyzed to obtain an updated second current analysis node; Jumping to the step of deleting the second current analysis node from the variable dependent node set of the fourth node based on the updated second current analysis node, when the second current analysis node is a temporary variable node, until the number of nodes in the second set of nodes to be analyzed is zero; Based on the current variable dependency node set of each original variable node, second variable node dependency information corresponding to each original variable node is generated.
6. The method according to claim 1, characterized in that The second variable node dependency information corresponding to each original variable node includes: data dependency information corresponding to each original variable node and control dependency information corresponding to each original variable node, and the generating of variable dependency representation information of original variable data in the original program based on the second variable node dependency information includes: Determine a target representation from among a plurality of preset representations; Based on the target representation mode, the data dependency information and the control dependency information, the original variable data in each original variable node is represented by a dependency relationship to obtain the variable dependency representation information.
7. The method according to any one of claims 1 to 6, characterized in that: The program node dependency information between the plurality of intermediate program nodes is obtained in the following manner: Acquire initial program dependency information of the intermediate program, the initial program dependency information comprising: a plurality of initial intermediate program nodes and initial node dependency information between the plurality of initial intermediate program nodes; Determining a slicing criterion node including target variable data in the plurality of initial intermediate program nodes; Based on the slicing criterion node and the initial node dependency information, the initial program dependency information is reversely sliced to determine the program node dependency information between the multiple intermediate program nodes in the multiple initial intermediate program nodes and each intermediate program node, wherein the root node in the multiple intermediate program nodes is the slicing criterion node.
8. A program analysis device, characterized in that: The device comprises: A program dependency information acquisition module is used to acquire program dependency information of an intermediate program corresponding to an original program, wherein the program dependency information includes: program node dependency information between multiple intermediate program nodes and the multiple intermediate program nodes; A variable dependency information extraction module, configured to determine each intermediate program node as an original variable node or a temporary variable node based on the program node dependency information and the instruction type corresponding to each intermediate program node, and extract first variable node dependency information corresponding to each intermediate program node; A variable dependency information merging module, used to merge the first variable node dependency information corresponding to each temporary variable node in the plurality of intermediate program nodes into the first variable node dependency information corresponding to the original variable node that depends on each temporary variable node, to obtain the second variable node dependency information corresponding to each original variable node in the plurality of intermediate program nodes; A variable dependency information representation module is used to generate variable dependency representation information of original variable data in the original program based on the second variable node dependency information.
9. An electronic device, characterized in that: The device includes a processor and a memory, wherein the memory stores at least one instruction or at least one program, and the at least one instruction or the at least one program is loaded and executed by the processor to implement the program analysis method as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The storage medium stores at least one instruction or at least one program, and the at least one instruction or the at least one program is loaded and executed by the processor to implement the program analysis method as described in any one of claims 1 to 7.
