Function call graph construction method, device and storage medium for SAST
By generating abstract syntax trees and class inheritance graphs, and directly constructing function call graphs from source code, the problem of low construction efficiency and accuracy in the existing technology is solved, and efficient and accurate function call graph generation and cross-function risk positioning are achieved.
Patent Information
- Application Number
- CN202410756692.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-12
- Publication Date
- 2024-12-06
- Estimated Expiration
- 2044-06-12
AI Technical Summary
The prior art has low accuracy and efficiency when building function call graphs. Especially when automatically generating function call graphs, the program needs to be compiled into runnable files to obtain function signature information, resulting in inefficiency.
By generating the abstract syntax tree of the target program, constructing class inheritance graphs and method call information, and using class inheritance graphs to generate function call graphs, thereby realizing the direct construction of function call graphs from the source code, avoiding the manual determination and compilation process.
It improves the construction efficiency and accuracy of function call graphs, enhances the coverage of method call relationships, and realizes automated cross-function risk positioning.
Smart Images

Figure CN118708172B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a method, device and storage medium for constructing a function call graph for SAST. Background Art
[0002] A function call graph is a graphical representation that describes the calling relationship between functions in a program. It can represent the data flow within a program. Based on the function call graph, the potential efficiency performance of the program can be identified, or cross-function risk positioning can be performed. It plays an important role in static application security testing (SAST). Usually, function call graphs need to be constructed manually, which has low accuracy and low efficiency. If you need to automatically generate a function call graph, since information such as function signatures for the accurate positioning of methods in Java needs to be obtained in the bytecode, the program needs to be compiled into an executable file (such as bytecode) to achieve this, which is inefficient. Summary of the invention
[0003] The embodiments of the present disclosure at least provide a method, device and storage medium for constructing a function call graph for SAST.
[0004] In a first aspect, an embodiment of the present disclosure provides a method for constructing a function call graph for SAST, including:
[0005] Based on the source code of the target program, generate an abstract syntax tree of the target program;
[0006] Based on the abstract syntax tree, a class inheritance graph of the target program is generated; the class inheritance graph includes a plurality of nodes representing first objects and connecting edges representing inheritance relationships between the first objects; the first objects include class or interface declarations in the target program;
[0007] Based on the abstract syntax tree, determining at least one method call information corresponding to a plurality of second objects in the target program; the second objects include methods in the target program;
[0008] Based on the method call information and the class inheritance graph, a function call graph of the target program is generated; the function call graph includes multiple nodes representing second objects, and connecting edges representing call relationships between second objects; the function call graph is used to locate cross-function risks for the target program.
[0009] In an optional implementation, generating a class inheritance graph of the target program based on the abstract syntax tree includes:
[0010] Traversing the abstract syntax tree to determine a plurality of first objects corresponding to the target program;
[0011] For any of the first objects, perform symbol resolution on the first object to obtain first reference type information of the first object and second reference type information of a parent class of the first object;
[0012] A class inheritance graph of the target program is generated based on the first reference type information and the second reference type information.
[0013] In an optional implementation, determining at least one method call information corresponding to multiple second objects in the target program based on the abstract syntax tree includes:
[0014] Traversing the abstract syntax tree to determine method definitions corresponding to a plurality of second objects in the target program;
[0015] At least one method call information of the second object is determined from the method definition.
[0016] In an optional implementation, generating a function call graph of the target program based on the method call information and the class inheritance graph includes:
[0017] For any of the second objects, performing symbol resolution on the at least one method call information to obtain a method definition package corresponding to the method call information;
[0018] Determining a first calling relationship based on the method definition signature carried in the method definition package;
[0019] Determine a second calling relationship based on the class identification information indicated in the method signature of the second object and the class inheritance graph;
[0020] Based on the first calling relationship and the second calling relationship, a function call graph of the target program is generated.
[0021] In an optional implementation, determining the second calling relationship based on the class identification information indicated in the method signature of the second object and the class inheritance graph includes:
[0022] Determine, from the class inheritance graph, a third object having an inheritance relationship with the target class corresponding to the class identification information; the third object includes a subclass and / or an implementation class of the target class;
[0023] Determine a method signature of a target method corresponding to the third object;
[0024] A second calling relationship is determined based on the method signature of the target method and the method signature corresponding to the second object.
[0025] In an optional implementation, determining the second calling relationship based on the method signature of the target method and the method signature corresponding to the second object includes:
[0026] Comparing the method signature of the target method with the method signature corresponding to the second object;
[0027] When the method signature of the target method is consistent with the method signature corresponding to the second object, it is determined that the target method calls the second object.
[0028] In an optional implementation, the first node in the class inheritance graph carries first unique identification information corresponding to the first node; the first unique identification information includes class identification information corresponding to the first node; the second node in the function call graph carries second unique identification information corresponding to the second node; the second unique identification information is determined based on the class identification information indicated in the method signature corresponding to the second node.
[0029] In a second aspect, the embodiment of the present disclosure further provides a function call graph construction device for SAST, including:
[0030] A first generating module, configured to generate an abstract syntax tree of the target program based on the source code of the target program;
[0031] A second generation module is used to generate a class inheritance graph of the target program based on the abstract syntax tree; the class inheritance graph includes a plurality of nodes representing first objects and connecting edges representing inheritance relationships between the first objects; the first objects include class or interface declarations in the target program;
[0032] A determination module, configured to determine, based on the abstract syntax tree, at least one method call information corresponding to a plurality of second objects in the target program; the second objects include methods in the target program;
[0033] The third generation module is used to generate a function call graph of the target program based on the method call information and the class inheritance graph; the function call graph includes multiple nodes representing second objects, and connecting edges representing the call relationship between the second objects; the function call graph is used to locate cross-function risks for the target program.
[0034] In an optional implementation manner, the second generating module is specifically configured to:
[0035] Traversing the abstract syntax tree to determine a plurality of first objects corresponding to the target program;
[0036] For any of the first objects, perform symbol resolution on the first object to obtain first reference type information of the first object and second reference type information of a parent class of the first object;
[0037] A class inheritance graph of the target program is generated based on the first reference type information and the second reference type information.
[0038] In an optional implementation manner, the determining module is specifically configured to:
[0039] Traversing the abstract syntax tree to determine method definitions corresponding to a plurality of second objects in the target program;
[0040] At least one method call information of the second object is determined from the method definition.
[0041] In an optional implementation manner, the third generating module is specifically used to:
[0042] For any of the second objects, performing symbol resolution on the at least one method call information to obtain a method definition package corresponding to the method call information;
[0043] Determining a first calling relationship based on the method definition signature carried in the method definition package;
[0044] Determine a second calling relationship based on the class identification information indicated in the method signature of the second object and the class inheritance graph;
[0045] Based on the first calling relationship and the second calling relationship, a function call graph of the target program is generated.
[0046] In an optional implementation manner, when the third generation module determines the second calling relationship based on the class identification information indicated in the method signature of the second object and the class inheritance graph, it is specifically used to:
[0047] Determine, from the class inheritance graph, a third object having an inheritance relationship with the target class corresponding to the class identification information; the third object includes a subclass and / or an implementation class of the target class;
[0048] Determine a method signature of a target method corresponding to the third object;
[0049] A second calling relationship is determined based on the method signature of the target method and the method signature corresponding to the second object.
[0050] In an optional implementation manner, when determining the second calling relationship based on the method signature of the target method and the method signature corresponding to the second object, the third generating module is specifically used to:
[0051] Comparing the method signature of the target method with the method signature corresponding to the second object;
[0052] When the method signature of the target method is consistent with the method signature corresponding to the second object, it is determined that the target method calls the second object.
[0053] In an optional implementation, the first node in the class inheritance graph carries first unique identification information corresponding to the first node; the first unique identification information includes class identification information corresponding to the first node; the second node in the function call graph carries second unique identification information corresponding to the second node; the second unique identification information is determined based on the class identification information indicated in the method signature corresponding to the second node.
[0054] In a third aspect, an embodiment of the present disclosure further provides an electronic device, comprising: a processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor and the memory communicate via the bus, and when the machine-readable instructions are executed by the processor, the steps of the above-mentioned first aspect, or any possible implementation of the first aspect are performed.
[0055] In a fourth aspect, an embodiment of the present disclosure further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the above-mentioned first aspect or any possible implementation of the first aspect are executed.
[0056] The method, device and storage medium for constructing a function call graph for SAST provided by the embodiments of the present disclosure can generate an abstract syntax tree of the target program using the source code of the target program, and generate a class inheritance graph and method call information between multiple methods in the target program based on the abstract syntax tree, and then generate a function call graph of the target program based on the class inheritance graph and the method call information, thereby directly generating a function call graph from the source code without the need for manual determination, nor the need to compile the source code into an executable file and run the target program, and the construction of the function call graph is more efficient and more accurate. In addition, since the class inheritance graph is used when constructing the function call graph, the potential method call relationship can be determined more comprehensively, and the coverage of the function call graph is improved.
[0057] In order to make the above-mentioned objectives, features and advantages of the present disclosure more obvious and easy to understand, preferred embodiments are specifically cited below and described in detail with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0058] In order to more clearly illustrate the technical solutions of the embodiments of the present disclosure, the following is a brief introduction to the drawings required for use in the embodiments. The drawings herein are incorporated into the specification and constitute a part of the specification. These drawings illustrate embodiments consistent with the present disclosure and are used together with the specification to illustrate the technical solutions of the present disclosure. It should be understood that the following drawings only illustrate certain embodiments of the present disclosure and should not be regarded as limiting the scope. For ordinary technicians in this field, other relevant drawings can also be obtained based on these drawings without creative work.
[0059] Figure 1 A flowchart of a method for constructing a function call graph for SAST provided by an embodiment of the present disclosure is shown;
[0060] Figure 2 A schematic diagram of a class inheritance diagram provided by an embodiment of the present disclosure is shown;
[0061] Figure 3 A schematic diagram showing a function call graph provided by an embodiment of the present disclosure is shown;
[0062] Figure 4 A flowchart of another method for constructing a function call graph for SAST provided by an embodiment of the present disclosure is shown;
[0063] Figure 5 A schematic diagram of a function call graph construction device for SAST provided by an embodiment of the present disclosure is shown;
[0064] Figure 6 A schematic diagram of an electronic device provided by an embodiment of the present disclosure is shown. DETAILED DESCRIPTION
[0065] In order to make the purpose, technical scheme and advantages of the embodiments of the present disclosure clearer, the technical scheme in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the drawings in the embodiments of the present disclosure. Obviously, the described embodiments are only part of the embodiments of the present disclosure, rather than all of the embodiments. The components of the embodiments of the present disclosure generally described and shown in the drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present disclosure provided in the drawings is not intended to limit the scope of the present disclosure for protection, but merely represents the selected embodiments of the present disclosure. Based on the embodiments of the present disclosure, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present disclosure.
[0066] It should be noted that similar reference numerals and letters denote similar items in the following drawings, and therefore, once an item is defined in one drawing, further definition and explanation thereof is not required in subsequent drawings.
[0067] The term "and / or" herein only describes an association relationship, indicating that three relationships may exist. For example, A and / or B may represent the following three situations: A exists alone, A and B exist at the same time, and B exists alone. In addition, the term "at least one" herein represents any combination of at least two of any one or more of a plurality of. For example, including at least one of A, B, and C may represent including any one or more elements selected from the set consisting of A, B, and C.
[0068] Research has found that the function call graph needs to be constructed manually, which is less accurate and inefficient. If the function call graph needs to be automatically generated, since the function signature and other information for the accurate location of the method in Java need to be obtained in the bytecode, the program needs to be compiled into an executable file (such as bytecode) to achieve this, which is inefficient.
[0069] Based on the above research, the present disclosure provides a method, device and storage medium for constructing a function call graph for SAST, which can generate an abstract syntax tree of the target program using the source code of the target program, and generate a class inheritance graph and method call information between multiple methods in the target program based on the abstract syntax tree, and then generate a function call graph of the target program based on the class inheritance graph and the method call information, thereby directly generating a function call graph through the source code, without the need for manual determination, and without the need to compile the source code into an executable file and run the target program, and the construction of the function call graph is more efficient and more accurate. In addition, since the class inheritance graph is used when constructing the function call graph, the potential method call relationship can be determined more comprehensively, and the coverage of the function call graph is improved.
[0070] To facilitate understanding of this embodiment, a method for constructing a function call graph for SAST disclosed in an embodiment of the present disclosure is first introduced in detail. The execution subject of the method for constructing a function call graph for SAST provided in an embodiment of the present disclosure is generally a computer device with certain computing capabilities, and the computer device includes, for example, a terminal device or a server or other processing device. In some possible implementations, the method for constructing a function call graph for SAST can be implemented by a processor calling computer-readable instructions stored in a memory.
[0071] See also Figure 1 As shown, it is a flowchart of a method for constructing a function call graph for SAST provided by an embodiment of the present disclosure, and the method includes steps S101 to S104, wherein:
[0072] S101. Generate an abstract syntax tree of a target program based on source code of the target program.
[0073] In this step, the source code of the target program may be obtained first, and the source code may be a complete code or a code fragment.
[0074] Abstract Syntax Tree (AST) is a tree structure used in the computer field to represent the structure of a programming language. In a compiler or interpreter, the source code is first parsed into an AST before subsequent compilation or interpretation. The nodes in the AST represent the grammatical structure in the source code, such as expressions, statements, function definitions, etc. Each node has zero or more child nodes, which correspond to different parts of the source code. Through AST, the compiler or interpreter can more easily analyze and process the source code, perform syntax checking, optimization, and generate target code. Because AST has abstracted and standardized the structure of the source code, the compiler or interpreter can process it more easily without having to worry about the specific grammatical details.
[0075] In the process of generating AST, the source code can be first subjected to lexical analysis and grammatical analysis. Lexical analysis can decompose the source code into a series of lexical units (Tokens), such as identifiers, keywords, operators, etc. Syntactic analysis can establish the relationship between grammatical units based on word segmentation, parse the lexical symbol stream according to the grammatical rules of the Java language, and construct a structured representation, namely the abstract syntax tree (AST). The method commonly used in this process is context-free grammar (Context-Free Grammar) and corresponding analysis techniques, such as recursive descent parsing, left-to-right, rightmost derivation (LR) analysis, etc. In the process of grammatical analysis, the recognized grammatical structure can be mapped to the corresponding AST node. Each node represents a grammatical structure, and the nodes are connected through a tree structure to reflect the hierarchical structure of the source code.
[0076] In a possible implementation, the abstract syntax tree may be generated by a Java parser.
[0077] S102. Generate a class inheritance graph of the target program based on the abstract syntax tree; the class inheritance graph includes multiple nodes representing first objects and connecting edges representing inheritance relationships between first objects; the first objects include class or interface declarations in the target program.
[0078] The above class inheritance graph is a directed graph. The nodes in the graph can correspond to each class or interface declaration in the target program. The connecting edges indicate the parent-child class inheritance relationship, and the child class can point to the parent class.
[0079] The sources of the above classes or interfaces usually include the following three types: self-developed source code, Java Runtime Environment Standard Package (JRE) standard package, or third-party Jar package that the source code depends on.
[0080] Among them, a class or interface can be uniquely identified by a fully qualified name, and the mapping relationship between the fully qualified name and the class can be maintained in the class inheritance graph.
[0081] The fully qualified name is the complete path name that includes the package name of the class or interface, as well as the name of the class or interface itself. The fully qualified name is unique throughout the Java virtual machine and can be used to accurately locate the location. In Java, the fully qualified name can start with the package name, separated by a period, and end with the name of the class or interface.
[0082] When generating the class inheritance graph of the target program, the abstract syntax tree can be traversed to determine multiple first objects (i.e., class and interface declarations) corresponding to the target program. For each first object, the first object can be symbolically parsed to obtain the first reference type information of the first object and the second reference type information of the parent class of the first object. Then, based on the above first reference type information and second reference type information, the class inheritance graph of the target program can be generated.
[0083] In the specific implementation process, you can traverse the AST, obtain all class or interface declarations (that is, the first object), exclude single classes without base classes or interface implementations (no inheritance relationship), and store them in a collection, and store the fully qualified name of the class in a mapping. After that, you can traverse the collection, perform type resolution on any class or interface, and obtain the reference type of the current class or interface after interest calculation, that is, the first reference type information mentioned above). In this process, you can also obtain the reference type of its parent class (that is, the second reference type information mentioned above). For each parent class, you can also obtain the class or interface declaration it references.
[0084] After that, you can establish the inheritance relationship between classes or interfaces according to each reference type. Specifically, you can determine whether the fully qualified name corresponding to the current class or interface exists in the class inheritance graph. If it does, you can establish the inheritance relationship (that is, establish a connection edge) according to the reference type; if it does not, you can first establish the node corresponding to the class or interface, and then establish the inheritance relationship according to the reference type. When all the classes or interfaces in the collection are analyzed, you can get the class inheritance graph.
[0085] In a possible implementation, a Java parser can be used to perform symbolic analysis on the AST to obtain the result of type analysis. In the symbolic analysis stage, the parser will traverse the AST, collect and process information about the program semantics, so as to perform subsequent type checking, scope analysis, semantic checking and other operations.
[0086] To facilitate the use of subsequent steps, the class inheritance diagram can also record the method signature corresponding to the first object. A method signature is a string representation used to uniquely identify a method. It usually includes information such as the method name, parameter types, and return type, and is used to distinguish different methods.
[0087] See also Figure 2 As shown, it is a schematic diagram of a class inheritance diagram provided by an embodiment of the present disclosure. For example, Figure 2 It contains three nodes, namely class Dog, class Cat, and class Animal. Class Dog and class Cat inherit from class Animal. Figure 2 The type field records the type of the first object, the qualified-name records the fully qualified name of the first object, and the method_signature field records the method signature corresponding to the first object.
[0088] S103: Determine, based on the abstract syntax tree, at least one method call information corresponding to a plurality of second objects in the target program; the second object includes a method in the target program.
[0089] After obtaining the AST, at least one method call information corresponding to the second object in the target program can also be determined according to the abstract syntax tree. This step can be performed in parallel with step S102 or separately. The embodiment of the present disclosure does not limit the order between S102 and S103.
[0090] In this step, the second object may include a method in the target program. The method in the embodiment of the present disclosure refers to a function or operation in a class, which is used to describe the behavior of the class. The class defines the properties and behaviors of the object, and the method is the specific code that implements these behaviors. The method can access the properties and states of the class and change their values. The method can also work with other methods to complete complex operations.
[0091] In the specific implementation process, you can first traverse the AST to determine the method definitions of multiple second objects in the target program and store them in the method collection. At the same time, get the method signature, map it with the second object, and store the mapping relationship in the method collection.
[0092] It should be noted that the method signature obtained from the Java parser cannot uniquely identify a method. For example, methods with the same method name, parameters, and return value types in different classes will have the same method signature. Therefore, the fully qualified name of the class where the method is located can be obtained in addition. The method signature and the fully qualified name of the class where the method is located form a standard method signature to uniquely identify a method definition. The method signature in the mapping relationship is the above-mentioned standard method signature (the method signatures stored in the mapping in the disclosed embodiment are all standard method signatures).
[0093] After determining each second object, the method call information corresponding to the second object can be determined. The method call information is relevant information about the method call, usually including the name of the called method, the parameter list, the location of the caller, etc. Among them, the location of the caller usually includes information such as the class, method, and number of lines of code. Specifically, you can first traverse the nodes of the AST, visit each node and check its type. In the process of traversing the AST, when you encounter a node representing a method call, you can identify and extract relevant information.
[0094] S104. Generate a function call graph of the target program based on the method call information and the class inheritance graph; the function call graph includes multiple nodes representing second objects, and connecting edges representing call relationships between second objects; the function call graph is used to locate cross-function risks for the target program.
[0095] After obtaining the method call information and the class inheritance graph, the function call graph of the target program can be generated based on the above information.
[0096] Specifically, the function call graph of the target function can be generated by the following steps:
[0097] For any of the second objects, performing symbol resolution on the at least one method call information to obtain a method definition package corresponding to the method call information;
[0098] Determining a first calling relationship based on the method definition signature carried in the method definition package;
[0099] Determine a second calling relationship based on the class identification information indicated in the method signature of the second object and the class inheritance graph;
[0100] Based on the first calling relationship and the second calling relationship, a function call graph of the target program is generated.
[0101] Specifically, a Java parser can be used to perform symbolic parsing on the method call information. When performing symbolic parsing, the Java parser can encapsulate the method definition obtained by traversing the AST into a data structure or object, which is the above-mentioned method definition encapsulation. The method definition encapsulation can usually include information such as the method name, parameter list, return type, and method body statements.
[0102] The method definition package may be a method definition package of another second object called by the current second object indicated in the method call information.
[0103] Afterwards, the method definition signature can be extracted from the method definition encapsulation. In this way, the extracted method definition signature can be used to locate other second objects called by the current second object, and the method definitions of other called second objects can be obtained, thereby establishing a calling relationship between the current second object and other second objects (that is, the first calling relationship mentioned above).
[0104] It should be noted that Java has inheritance and polymorphism mechanisms. The inheritance mechanism refers to the ability of a class to inherit properties and methods from another class, and the subclass (derived class) inherits the properties and methods of the parent class (base class) and can be expanded and modified on this basis; the polymorphism mechanism means that the same method call can have different behaviors depending on the type of the object. In Java, polymorphism is achieved through method overriding (Override) and method overloading (Overload). Method overriding means that the subclass can override the method defined in the parent class, thereby changing the behavior of the method. At runtime, the virtual machine will call the corresponding overridden method according to the actual type of the object.
[0105] Due to the above mechanism, the parsed method call may be a virtual function call. In Java, virtual function calls are implemented through dynamic binding of methods, which is a manifestation of polymorphism in Java. When a method is declared as a virtual method in the parent class (usually modified by the keywords public or protected) and is overridden in the subclass, the method becomes a virtual method. At runtime, the Java virtual machine determines which version of the method to call based on the actual type of the object, which is dynamic binding. In this case, the object type to which the method belongs is only known when the program is running. Therefore, in order to expand the coverage of the function call graph as much as possible, the class inheritance graph can be used to analyze and obtain possible subclass calls (that is, the second call relationship mentioned above).
[0106] During the specific implementation process, a third object having an inheritance relationship with the target class corresponding to the class identification information can be determined from the class inheritance diagram; the third object includes a subclass and / or implementation class of the target class; then, the method signature of the target method corresponding to the third object can be determined; finally, the second calling relationship can be determined based on the method signature of the target method and the method signature corresponding to the second object.
[0107] Specifically, the fully qualified name of the corresponding class can be separated from the stored standard method signature, and then the class or interface definition corresponding to the fully qualified name can be screened out in the class inheritance diagram, and then the subclass or implementation class (that is, the third object) corresponding to the class or definition can be determined. Then, the method signature of the target method corresponding to the third object can be determined, and compared with the method signature corresponding to the second object, so as to determine whether the method corresponding to the second object is overridden.
[0108] If the method signature of the target method is consistent with the method signature of the second object, it means that the method corresponding to the second object is overwritten, and it can be determined that the target method calls the second object.
[0109] In the process of obtaining the first call relationship and the second call relationship, a function call graph can be constructed based on the determined first call relationship and the second call relationship. When all methods in the method set are analyzed, a complete function call graph can be obtained.
[0110] See also Figure 3 , which is a schematic diagram of a function call graph provided by an embodiment of the present disclosure. For example, Figure 3 It includes 5 nodes, and the nodes also carry the standard method signature of the second object.
[0111] In one possible implementation, the first node in the class inheritance graph carries first unique identification information corresponding to the first node; the first unique identification information includes class identification information corresponding to the first node (that is, the fully qualified name of the above class); the second node in the function call graph carries second unique identification information corresponding to the second node (that is, the above-mentioned standard method signature); the second unique identification information is determined based on the class identification information indicated in the method signature corresponding to the second node (consisting of the method signature and the fully qualified name of the corresponding class).
[0112] See also Figure 4 As shown, it is a flowchart of another method for constructing a function call graph for SAST provided by an embodiment of the present disclosure. Figure 4In the process, the source code (that is, the above-mentioned source code) is first obtained, and the source code is subjected to lexical analysis and syntax analysis to obtain the AST corresponding to the source code, and the AST is constructed by symbolic analysis, and the classes or interfaces in the target program and the method definitions in the target program are traversed. Then, for the class or interface, its reference type can be determined according to the result of symbolic analysis, the inheritance relationship of the class or interface can be obtained, and the mapping between the classes or interfaces can be obtained according to its fully qualified name; at the same time, for the method definition, the method call corresponding to the method definition can be determined, and the method definition encapsulation corresponding to the method call can be determined according to the result of symbolic analysis, the method definition of the called method can be obtained, and the subclass method definition of the method can be determined according to the class inheritance analysis (such as the above-mentioned class inheritance diagram). According to the method signatures of the determined method definitions and subclass method definitions, a method mapping is generated to obtain the mapping relationship between the method signature and the method definition. After that, a function call graph can be generated according to the determined calling relationship between the methods and the method mapping.
[0113] The method for constructing a function call graph for SAST provided by the embodiment of the present disclosure can generate an abstract syntax tree of the target program using the source code of the target program, and generate a class inheritance graph and method call information between multiple methods in the target program based on the abstract syntax tree, and then generate a function call graph of the target program based on the class inheritance graph and the method call information, thereby directly generating a function call graph from the source code without the need for manual determination, nor the need to compile the source code into an executable file and run the target program, and the construction of the function call graph is more efficient and more accurate. In addition, since the class inheritance graph is used when constructing the function call graph, the potential method call relationship can be determined more comprehensively, and the coverage of the function call graph is improved.
[0114] Those skilled in the art will appreciate that, in the above method of specific implementation, the order in which the steps are written does not imply a strict execution order and does not constitute any limitation on the implementation process. The specific execution order of the steps should be determined by their functions and possible internal logic.
[0115] Based on the same inventive concept, the embodiment of the present disclosure also provides a function call graph construction device for SAST corresponding to the function call graph construction method for SAST. Since the principle of solving the problem by the device in the embodiment of the present disclosure is similar to the function call graph construction method for SAST in the embodiment of the present disclosure, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be repeated.
[0116] Reference Figure 5 As shown, it is a schematic diagram of a function call graph construction device for SAST provided by an embodiment of the present disclosure, and the device includes:
[0117] A first generating module 510, configured to generate an abstract syntax tree of a target program based on a source code of the target program;
[0118] A second generation module 520 is used to generate a class inheritance graph of the target program based on the abstract syntax tree; the class inheritance graph includes a plurality of nodes representing first objects and connecting edges representing inheritance relationships between first objects; the first objects include class or interface declarations in the target program;
[0119] A determination module 530, configured to determine at least one method call information corresponding to a plurality of second objects in the target program based on the abstract syntax tree; the second objects include methods in the target program;
[0120] The third generation module 540 is used to generate a function call graph of the target program based on the method call information and the class inheritance graph; the function call graph includes multiple nodes representing second objects, and connecting edges representing the call relationship between second objects; the function call graph is used to locate cross-function risks for the target program.
[0121] The function call graph construction device for SAST of the disclosed embodiment can generate the target program abstract syntax tree by using the source code of the target program, and generate the class inheritance graph and the method call information between multiple methods in the target program according to the abstract syntax tree, and then generate the function call graph of the target program according to the class inheritance graph and the method call information, thereby realizing the direct generation of the function call graph through the source code, without the need for manual determination, and without the need to compile the source code into an executable file and run the target program, and the construction of the function call graph is more efficient and more accurate. In addition, since the class inheritance graph is used when constructing the function call graph, the potential method call relationship can be determined more comprehensively, and the coverage of the function call graph is improved.
[0122] In an optional implementation manner, the second generating module 520 is specifically configured to:
[0123] Traversing the abstract syntax tree to determine a plurality of first objects corresponding to the target program;
[0124] For any of the first objects, perform symbol resolution on the first object to obtain first reference type information of the first object and second reference type information of a parent class of the first object;
[0125] A class inheritance graph of the target program is generated based on the first reference type information and the second reference type information.
[0126] In an optional implementation manner, the determining module 530 is specifically configured to:
[0127] Traversing the abstract syntax tree to determine method definitions corresponding to a plurality of second objects in the target program;
[0128] At least one method call information of the second object is determined from the method definition.
[0129] In an optional implementation manner, the third generating module 540 is specifically configured to:
[0130] For any of the second objects, performing symbol resolution on the at least one method call information to obtain a method definition package corresponding to the method call information;
[0131] Determining a first calling relationship based on the method definition signature carried in the method definition package;
[0132] Determine a second calling relationship based on the class identification information indicated in the method signature of the second object and the class inheritance graph;
[0133] Based on the first calling relationship and the second calling relationship, a function call graph of the target program is generated.
[0134] In an optional implementation manner, when determining the second calling relationship based on the class identification information indicated in the method signature of the second object and the class inheritance graph, the third generation module 540 is specifically used to:
[0135] Determine, from the class inheritance graph, a third object having an inheritance relationship with the target class corresponding to the class identification information; the third object includes a subclass and / or an implementation class of the target class;
[0136] Determine a method signature of a target method corresponding to the third object;
[0137] A second calling relationship is determined based on the method signature of the target method and the method signature corresponding to the second object.
[0138] In an optional implementation manner, when determining the second call relationship based on the method signature of the target method and the method signature corresponding to the second object, the third generation module 540 is specifically used to:
[0139] Comparing the method signature of the target method with the method signature corresponding to the second object;
[0140] When the method signature of the target method is consistent with the method signature corresponding to the second object, it is determined that the target method calls the second object.
[0141] In an optional implementation, the first node in the class inheritance graph carries first unique identification information corresponding to the first node; the first unique identification information includes class identification information corresponding to the first node; the second node in the function call graph carries second unique identification information corresponding to the second node; the second unique identification information is determined based on the class identification information indicated in the method signature corresponding to the second node.
[0142] For descriptions of the processing flow of each module in the device and the interaction flow between each module, reference may be made to the relevant descriptions in the above method embodiment, which will not be described in detail here.
[0143] Corresponds to Figure 1 The method for constructing a function call graph for SAST in the embodiment of the present disclosure further provides an electronic device 600, such as Figure 6 FIG. 6 is a schematic diagram of the structure of an electronic device 600 provided in an embodiment of the present disclosure, including:
[0144] Processor 61, memory 62, and bus 63; memory 62 is used to store execution instructions, including internal memory 621 and external memory 622; the internal memory 621 is also called internal memory, which is used to temporarily store the operation data in the processor 61 and the data exchanged with the external memory 622 such as a hard disk. The processor 61 exchanges data with the external memory 622 through the internal memory 621. When the electronic device 600 is running, the processor 61 communicates with the memory 62 through the bus 63, so that the processor 61 executes the following instructions:
[0145] Based on the source code of the target program, generate an abstract syntax tree of the target program;
[0146] Based on the abstract syntax tree, a class inheritance graph of the target program is generated; the class inheritance graph includes a plurality of nodes representing first objects and connecting edges representing inheritance relationships between the first objects; the first objects include class or interface declarations in the target program;
[0147] Based on the abstract syntax tree, determining at least one method call information corresponding to a plurality of second objects in the target program; the second objects include methods in the target program;
[0148] Based on the method call information and the class inheritance graph, a function call graph of the target program is generated; the function call graph includes multiple nodes representing second objects, and connecting edges representing call relationships between second objects; the function call graph is used to locate cross-function risks for the target program.
[0149] In an optional implementation manner, in the instructions executed by the processor 61, generating a class inheritance graph of the target program based on the abstract syntax tree includes:
[0150] Traversing the abstract syntax tree to determine a plurality of first objects corresponding to the target program;
[0151] For any of the first objects, perform symbol resolution on the first object to obtain first reference type information of the first object and second reference type information of a parent class of the first object;
[0152] A class inheritance graph of the target program is generated based on the first reference type information and the second reference type information.
[0153] In an optional implementation manner, in the instructions executed by the processor 61, determining at least one method call information corresponding to the plurality of second objects in the target program based on the abstract syntax tree includes:
[0154] Traversing the abstract syntax tree to determine method definitions corresponding to a plurality of second objects in the target program;
[0155] At least one method call information of the second object is determined from the method definition.
[0156] In an optional implementation, in the instructions executed by the processor 61, generating a function call graph of the target program based on the method call information and the class inheritance graph includes:
[0157] For any of the second objects, performing symbol resolution on the at least one method call information to obtain a method definition package corresponding to the method call information;
[0158] Determining a first calling relationship based on the method definition signature carried in the method definition package;
[0159] Determine a second calling relationship based on the class identification information indicated in the method signature of the second object and the class inheritance graph;
[0160] Based on the first calling relationship and the second calling relationship, a function call graph of the target program is generated.
[0161] In an optional implementation, in the instructions executed by the processor 61, determining the second calling relationship based on the class identification information indicated in the method signature of the second object and the class inheritance graph includes:
[0162] Determine, from the class inheritance graph, a third object having an inheritance relationship with the target class corresponding to the class identification information; the third object includes a subclass and / or an implementation class of the target class;
[0163] Determine a method signature of a target method corresponding to the third object;
[0164] A second calling relationship is determined based on the method signature of the target method and the method signature corresponding to the second object.
[0165] In an optional implementation manner, in the instruction executed by the processor 61, determining the second calling relationship based on the method signature of the target method and the method signature corresponding to the second object includes:
[0166] Comparing the method signature of the target method with the method signature corresponding to the second object;
[0167] When the method signature of the target method is consistent with the method signature corresponding to the second object, it is determined that the target method calls the second object.
[0168] In an optional implementation, the first node in the class inheritance graph carries first unique identification information corresponding to the first node; the first unique identification information includes class identification information corresponding to the first node; the second node in the function call graph carries second unique identification information corresponding to the second node; the second unique identification information is determined based on the class identification information indicated in the method signature corresponding to the second node.
[0169] The present disclosure also provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of the method for constructing a function call graph for SAST described in the above method embodiment are executed. The storage medium may be a volatile or non-volatile computer-readable storage medium.
[0170] The embodiments of the present disclosure also provide a computer program product, which carries a program code. The instructions included in the program code can be used to execute the steps of the function call graph construction method for SAST described in the above method embodiment. Please refer to the above method embodiment for details, which will not be repeated here.
[0171] The computer program product may be implemented in hardware, software or a combination thereof. In one optional embodiment, the computer program product is implemented as a computer storage medium. In another optional embodiment, the computer program product is implemented as a software product, such as a software development kit (SDK).
[0172] Those skilled in the art can clearly understand that, for the convenience and simplicity of description, the specific working process of the system and device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here. In the several embodiments provided in the present disclosure, it should be understood that the disclosed system, device and method can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, and the indirect coupling or communication connection of the device or unit can be electrical, mechanical or other forms.
[0173] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0174] In addition, each functional unit in each embodiment of the present disclosure may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0175] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a non-volatile computer-readable storage medium that is executable by a processor. Based on this understanding, the technical solution of the present disclosure, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present disclosure. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0176] Finally, it should be noted that the above-described embodiments are only specific implementation methods of the present disclosure, which are used to illustrate the technical solutions of the present disclosure, rather than to limit them. The protection scope of the present disclosure is not limited thereto. Although the present disclosure is described in detail with reference to the above-described embodiments, ordinary technicians in the field should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above-described embodiments within the technical scope disclosed in the present disclosure, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present disclosure, and should be included in the protection scope of the present disclosure. Therefore, the protection scope of the present disclosure should be based on the protection scope of the claims.
Claims
1. A method for constructing a function call graph for SAST, characterized in that: include: Based on the source code of the target program, generate an abstract syntax tree of the target program; Based on the abstract syntax tree, generating a class inheritance graph of the target program; The class inheritance graph includes a plurality of nodes representing first objects and connecting edges representing inheritance relationships between first objects; the first objects include class or interface declarations in the target program; Based on the abstract syntax tree, determining at least one method call information corresponding to a plurality of second objects in the target program; The second object includes a method in the target program; Based on the method call information and the class inheritance graph, a function call graph of the target program is generated; the function call graph includes a plurality of nodes representing second objects and connection edges representing call relationships between second objects; The function call graph is used to locate cross-function risks for the target program; The generating of the function call graph of the target program based on the method call information and the class inheritance graph includes: for any second object, performing symbol resolution on the at least one method call information to obtain a method definition package corresponding to the method call information; determining a first call relationship based on a method definition signature carried in the method definition package; determining a second call relationship based on class identification information indicated in the method signature of the second object and the class inheritance graph; generating the function call graph of the target program based on the first call relationship and the second call relationship; The second calling relationship is determined based on the class identification information indicated in the method signature of the second object and the class inheritance graph, including: from the class inheritance graph, determining a third object that has an inheritance relationship with the target class corresponding to the class identification information; the third object includes a subclass and / or an implementation class of the target class; determining the method signature of the target method corresponding to the third object; comparing the method signature of the target method with the method signature corresponding to the second object; and when the method signature of the target method is consistent with the method signature corresponding to the second object, determining that the target method calls the second object.
2. The method according to claim 1, characterized in that The step of generating a class inheritance graph of the target program based on the abstract syntax tree includes: Traversing the abstract syntax tree to determine a plurality of first objects corresponding to the target program; For any of the first objects, perform symbol resolution on the first object to obtain first reference type information of the first object and second reference type information of a parent class of the first object; A class inheritance graph of the target program is generated based on the first reference type information and the second reference type information.
3. The method according to claim 1 or 2, characterized in that: The determining, based on the abstract syntax tree, at least one method call information corresponding to the plurality of second objects in the target program includes: Traversing the abstract syntax tree to determine method definitions corresponding to a plurality of second objects in the target program; At least one method call information of the second object is determined from the method definition.
4. The method according to claim 1, characterized in that: The first node in the class inheritance graph carries the first unique identification information corresponding to the first node; the first unique identification information includes the class identification information corresponding to the first node; The second node in the function call graph carries second unique identification information corresponding to the second node; The second unique identification information is determined based on class identification information indicated in a method signature corresponding to the second node.
5. A function call graph construction device for SAST, characterized in that: include: A first generating module, configured to generate an abstract syntax tree of the target program based on the source code of the target program; A second generation module is used to generate a class inheritance graph of the target program based on the abstract syntax tree; the class inheritance graph includes a plurality of nodes representing first objects and connecting edges representing inheritance relationships between the first objects; the first objects include class or interface declarations in the target program; A determination module, configured to determine at least one method call information corresponding to a plurality of second objects in the target program based on the abstract syntax tree; The second object includes a method in the target program; A third generating module is used to generate a function call graph of the target program based on the method call information and the class inheritance graph; the function call graph includes a plurality of nodes representing second objects and connecting edges representing call relationships between second objects; the function call graph is used to locate cross-function risks for the target program; The third generation module is specifically used to: for any second object, perform symbol resolution on the at least one method call information to obtain a method definition package corresponding to the method call information; and determine a first call relationship based on a method definition signature carried in the method definition package; Determine a second calling relationship based on the class identification information indicated in the method signature of the second object and the class inheritance graph; Based on the first calling relationship and the second calling relationship, generating a function call graph of the target program; The third generation module is specifically used to: determine, from the class inheritance diagram, a third object having an inheritance relationship with the target class corresponding to the class identification information; the third object includes a subclass and / or an implementation class of the target class; determine a method signature of a target method corresponding to the third object; compare the method signature of the target method with the method signature corresponding to the second object; and when the method signature of the target method is consistent with the method signature corresponding to the second object, determine that the target method calls the second object.
6. An electronic device, characterized in that: include: A processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor and the memory communicate via the bus, and when the machine-readable instructions are executed by the processor, the steps of the function call graph construction method for SAST as described in any one of claims 1 to 4 are performed.
7. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, which, when executed by a processor, executes the steps of the method for constructing a function call graph for SAST as described in any one of claims 1 to 4.
