A CPG+ graph-based implicit dependency pattern analysis method
By introducing an implicit dependency pattern analysis method based on CPG+ graph in architectural analysis, and combining abstract syntax trees, control flow graphs and program dependency graphs to identify and mine implicit dependencies, the problem of invisible dependencies in dynamic languages is solved. , improving the accuracy and efficiency of architectural analysis.
Patent Information
- Application Number
- CN202210647102.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-09
- Publication Date
- 2024-09-13
- Estimated Expiration
- 2042-06-09
AI Technical Summary
Existing architecture analysis tools fail to effectively handle implicit dependencies in dynamic languages such as Python, JavaScript, and Ruby, resulting in these relationships being invisible in the source code, affecting the accuracy of architecture analysis and increasing maintenance workload.
The implicit dependency pattern analysis method based on CPG+ graph reduces the complexity of the intermediate representation of the graph structure by generating abstract syntax trees, control flow graphs and program dependency graphs, combined with subgraph matching algorithms, to identify and mine implicit dependency patterns.
It realizes the identification and analysis of implicit dependencies, quickly obtains the types and quantities of implicit dependencies in the project, and improves the accuracy and efficiency of architecture analysis.
Smart Images

Figure CN114879974B
Abstract
Description
Technical Field
[0001] The invention belongs to the field of program analysis, and in particular relates to an implicit dependency pattern analysis method based on a CPG+ graph. Background Art
[0002] Dependencies between software entities are the cornerstone of most software architecture analysis and architecture analysis tool research, including architecture recovery, architecture problem or anti-pattern detection, defect prediction and change impact analysis. However, many languages have their own characteristics that make grammatical dependencies invisible in the source code. For example, dynamic languages such as Python, JavaScript and Ruby do not have explicit type references, so some dependencies are invisible when performing static grammatical analysis of the source code, which is contrary to the explicit dependencies that can be directly identified. Such invisible grammatical dependencies caused by dynamic types are called implicit dependencies.
[0003] Existing architecture analysis tools usually do not take implicit dependencies into account, ignoring the impact of implicit dependencies on architecture analysis, however, implicit dependencies have a great impact on architecture. Studies have shown that implicit dependencies have a higher impact on architecture than explicit dependencies, and considering implicit dependencies can also help identify previously unrecognizable architectural anti-pattern instances and detect serious problem files with architectural connections. Typically, files involving implicit dependencies require 32% more maintenance work than files involving explicit dependencies.
[0004] However, the "invisibility" of implicit dependencies makes it more difficult to find, understand, and change them. In fact, file-level implicit dependencies account for more than 25% of all file-level dependencies, making it impossible to ignore them. Although there have been studies on the extraction of implicit dependencies and their impact on software structure, the implicit dependency pattern of the code is still unknown, and the practical significance of the implicit dependency pattern is still unknown. In addition, the implicit dependency pattern is naturally a graph, a high-dimensional data, so we hope to detect and identify implicit dependencies through graphs. Summary of the invention
[0005] In order to solve the above problems, the present invention provides an implicit dependency pattern analysis method based on CPG+ graph. A new type of property graph that focuses more on process control and implicit dependency is proposed on the basis of Code Property Graph (CPG), which is called Code Property Graph Plus (CPG+ graph) based on implicit dependency. By parsing and analyzing the source code, an Abstract Syntax Tree (AST), a Control Flow Graph (CFG) and a Program Dependency Graph (PDG) are generated, and a CPG+ graph is jointly constructed. Compared with the original Code Property Graph CPG graph, the Program Dependency Graph PDG is conditionally obtained, thereby reducing the complexity of the intermediate representation of the graph structure of the generated code.
[0006] In order to achieve the above object, the technical solution adopted by the present invention is:
[0007] A CPG+ graph-based implicit dependency pattern analysis method comprises the following steps:
[0008] S1: Obtain object dependencies through ENRE;
[0009] S2: Get the abstract syntax tree of the object and convert it into the required format;
[0010] S3: preprocessing for subsequent operations on the graph;
[0011] S4: Get the control flow graph of the method in the code and get the node order;
[0012] S5: If the method contains implicit dependencies, obtain the data flow of the node where the implicit dependencies occur;
[0013] S6: Merge abstract syntax tree, control flow graph, and data flow graph based on implicit dependencies;
[0014] S7: Process implicit dependency patterns and use subgraph matching algorithm to mine and analyze implicit dependency patterns.
[0015] A further improvement of the present invention is that in step S1, the implicit dependency pattern of the object is analyzed using the ENRE tool and outputted as information input into the program.
[0016] A further improvement of the present invention is that in step S2, the abstract syntax tree json format of the object is obtained, and an abstract syntax tree (AST) is drawn according to the json data, and the required information (lineno attribute, type attribute, value attribute, etc.) is retained in the process.
[0017] A further improvement of the present invention is that in step S3, in order to facilitate the subsequent merging of the three graphs, a series of measures need to be taken for preprocessing, and step S3 specifically includes:
[0018] S301: When a method with the same function name as the control flow start node is called in the function body, an edge pointing to the control flow start node will appear, causing a loop. The control flow start node is the root node of the function control flow, and should not have a parent node. It is deleted during the control flow generation process;
[0019] S302: Since the abstract syntax tree is generated based on json data, and the ast2json project simply returns all the attributes and corresponding values of the syntax tree of the Python file after being parsed by the AST library, in the actual code, the "lineno (start line)" attribute and "end_lineno (end line)" attribute of the node will be separated by some attributes. When recursively processing json data, it will cause the node to have only the "lineno (start line) attribute", which will cause errors in subsequent processing. Therefore, it is necessary to adjust the json data returned after parsing the abstract syntax tree. Specify the sorting of the statement nodes returned by the control flow graph to ensure consistent order, which is convenient for subsequent graph merging operations;
[0020] S303: Since both the Program Dependency Graph (PDG) and the Control Flow Graph (CFG) are parsed at the statement level, the information of the PDG and CFG is first combined to facilitate the establishment of subsequent information. To this end, implicit dependencies are found through CFG, so there is no need to consider the node order relationship of the PDG.
[0021] S304: The initial control flow graph drawing only has the basic if and for statement processing. According to the frequency of occurrence, the processing of raise, with, and try-except statements is increased first; and Python-specific for-else and while-else statements are added.
[0022] A further improvement of the present invention is that in step S4, for the control flow graph obtained for for else, with, and try-except statements, the order of control flow graph nodes obtained is different from the order of nodes in the abstract syntax tree, and the order needs to be adjusted so that it can be merged into the abstract syntax tree.
[0023] A further improvement of the present invention is that in step S5, the source code of the implicit dependency in the method is first found, and then the data stream related to the dependency is found upward, and a series of processing is performed on this. Step S5 specifically includes:
[0024] S501: Regularize implicit dependency information and match it with the source code to find the source code of implicit dependency;
[0025] S502: Searching for data flows related to the implicit dependency from the implicit dependency source code upwards. The data flows may have branches, so there may be multiple data flows pointing to the implicit dependency in the end.
[0026] A further improvement of the present invention is that in step S6, the acquired abstract syntax tree, control flow graph, and implicit dependency data flow graph are merged to form a joint data structure, so that batch processing and mining can be carried out.
[0027] A further improvement of the present invention is that in step S7, based on the data structure obtained in S6, a subgraph matching algorithm is used to find all the dependency occurrences, and implicit dependency patterns are mined and determined therefrom.
[0028] Compared with the prior art, the present invention has the following beneficial effects:
[0029] (1) A specific implicit dependency model is proposed to solve the problem that the implicit dependencies of the code are still unknown;
[0030] (2) This topic proposes a new type of property graph based on the Code Property Graph (CPG) that focuses more on process control and implicit dependencies. It is called the Code Property Graph Plus (CPG+) based on implicit dependencies. It is necessary to parse and analyze the source code to generate the Abstract Syntax Tree (AST), Control Flow Graph (CFG) and Program Dependency Graph (PDG), and jointly construct the CPG+ graph. Compared with the original Code Property Graph (CPG), the program dependency graph is conditionally obtained, thereby reducing the complexity of the intermediate representation of the graph structure of the generated code;
[0031] (3) We propose a CPG+ graph-based detection method for implicit dependency patterns, which can quickly obtain the types and quantities of implicit dependencies in a project. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 The overall flow chart of the method of the present invention is
[0033] Figure 2 Generate CPG+ graph for Django 3.2a1 function conversion
[0034] Table 1 shows some implicit dependencies and their occurrence times in the Django 3.2a1 project obtained by the program. DETAILED DESCRIPTION
[0035] In order to make the purpose, features and advantages of the present invention more obvious and understandable, the specific implementation of the present invention is described in detail below with reference to the accompanying drawings and examples.
[0036] Take the known open source project Django-3.2a1 as an example.
[0037] like Figure 1 As shown, a CPG+ graph-based implicit dependency pattern analysis method includes the following steps:
[0038] Step S1: Input the Django-3.2a1 project into the ENRE tool to obtain its implicit dependencies.
[0039] Step S2: Get the abstract syntax tree of the Django-3.2a1 project, retain the required attributes, convert it into json data, and draw an abstract syntax tree based on the json data.
[0040] Step S3: To facilitate the subsequent graph merging operation, the graph needs to be processed:
[0041] Step S301: Convert the json data into an abstract syntax tree, ignore the overly detailed parts in the middle (function parameters are wrapped by multiple layers of "arg (parameter)" attributes), and obtain the statement nodes in the method as the basis for subsequent merging;
[0042] Step S302: Supplement and improve the tool for drawing control flow;
[0043] Step S4: Draw the control flow chart of the method and obtain the node relationship:
[0044] Step S5: When the function contains implicit dependencies, find the data flow related to the implicit dependencies. The specific steps are as follows:
[0045] S501: Regularize implicit dependencies to facilitate matching with source code and find implicit dependency source code
[0046] S502: Find the data flow related to implicit dependencies, and consider the situation of multiple branches
[0047] S503: Based on the data flow found, obtain the statement where the dependency occurs, and parse the program dependency graph related to the dependency.
[0048] Step S6: Merge the acquired abstract syntax tree, control flow graph, and implicit dependency data flow graph to form a joint data structure, so that batch processing and mining can be carried out, such as Figure 2As shown, this is the CPG+ diagram of the function in the Django 3.2a1 project after parsing:
[0049] Step S7: According to the data structure obtained in S6, the discovered implicit dependency pattern is generalized, and the implicit dependency is analyzed using the subgraph matching algorithm. Table 1 is the result obtained after the analysis of the Django3.2a1 project. That is, the program concludes that there are 424 implicit dependencies of this type that are directly called using parameter passing in the Django3.2a1 project, and 201 implicit dependencies of this type that are chain called using parameter passing.
[0050] Summary: This paper proposes a specific implicit dependency pattern, solves the problem that the current implicit dependency pattern is still unknown, and provides a specific method for mining implicit dependencies from the CPG+ graph. The mining results are shown in Table 1.
[0051] Table 1
[0052]
Claims
1. An implicit dependency pattern analysis method based on CPG+ graph, It is characterized in that The steps include: S1: Input the Python project and obtain the object dependencies through the ENRE tool; S2: Get the abstract syntax tree of the object; S3: preprocessing for subsequent operations on the graph; Step S3 specifically includes: S301: When a method with the same function name as the control flow start node is called in the function body, an edge pointing to the control flow start node will appear, causing a loop. The control flow start node is the root node of the function control flow and should not have a parent node. It is deleted during the control flow generation process. S302: Since the abstract syntax tree is generated based on json data, and the ast2json project simply returns all the attributes and corresponding values of the syntax tree of the Python file after being parsed by the AST library, in actual code analysis, the "lineno (start line)" attribute and the "end_lineno (end line)" attribute of the node will be separated by some attributes. When recursively processing json data, it will cause the node to have only the "lineno (start line) attribute", which will cause errors in subsequent processing. Therefore, it is necessary to adjust the json data returned after parsing the syntax tree, and specify the sorting of the statement nodes returned by the control flow graph to ensure the order consistency, so as to facilitate the subsequent graph merging operation; S303: Since both the Program Dependency Graph (PDG) and the Control Flow Graph (CFG) are parsed at the statement level, the information of the PDG and CFG are first combined to facilitate the establishment of subsequent information. For this purpose, the implicit dependency is found through the CFG, so there is no need to consider the node order relationship of the PDG. S304: The initial control flow graph only processes the basic if and for statements. According to the frequency of occurrence, the raise, with, and try-except statements are added first; the Python-specific for-else and while-else statements are added; S4: Get the control flow graph of the method in the code and get the node order; S5: If the method contains implicit dependencies, obtain the data flow of the node where the implicit dependencies occur; Step S5 specifically includes: S501: Regularize implicit dependency information and match it with the source code to find the source code of implicit dependency; S502: Searching for data flows related to the implicit dependency from the implicit dependency source code upwards. The data flows may have branches, so there may be multiple data flows pointing to the implicit dependency in the end; S6: Merge abstract syntax tree, control flow graph, and data flow graph based on implicit dependencies; S7: Obtain the implicit dependency locations from the merged graph using the VF2 algorithm, find their parent nodes, assign and replace them, and obtain the transformation from the function to the dependency location, thereby determining the implicit dependency pattern.
2. According to the implicit dependency pattern analysis method based on CPG+ graph according to claim 1, It is characterized in that In step S1, the implicit dependency pattern of the object is analyzed using the ENRE tool and output, which is input into the program as information.
3. According to the implicit dependency pattern analysis method based on CPG+ graph according to claim 1, It is characterized in that In the step S2, the abstract syntax tree json format of the object is obtained, and the abstract syntax tree (Abstract SyntaxTree, AST) is drawn according to the json data, and the required information lineno attribute, type attribute, and value attribute are retained in the process.
4. According to the implicit dependency pattern analysis method based on CPG+ graph according to claim 1, It is characterized in that In step S4, for the control flow graph obtained for for else, with, and try-except statements, the node order of the control flow graph obtained is different from the node order in the abstract syntax tree, and the order needs to be adjusted so that it can be merged into the abstract syntax tree.
5. According to the implicit dependency pattern analysis method based on CPG+ graph according to claim 1, It is characterized in that In step S6, the acquired abstract syntax tree, control flow graph, and implicit dependency data flow graph are merged to form a joint data structure for batch processing and mining.
6. According to the implicit dependency pattern analysis method based on CPG+ graph according to claim 1, It is characterized in that In step S7, based on the data structure obtained in S6, a subgraph matching algorithm is used to find all the dependency locations, and implicit dependency patterns are mined and determined therefrom.
Citation Information
Patent Citations
Binary code anti-obfuscation method based on semanteme
CN104834837A
Code homology detection method based on code fingerprint and device thereof
CN107169358A
A method of mining and analyzing information security vulnerabilities
CN109002721A
Java file dependency analysis method and module for distributed static detection system
CN109033843A
Program analyzing method, program analyzing apparatus and program analyzing program
US20090007087A1
