Function-level defect localization method based on call graph and multi-level contrastive learning
By constructing local and global call graphs, combining graph neural networks and pre-trained models, the problem of unutilized calling relationships between functions in existing technologies is solved, and efficient and accurate function-level defect localization is achieved, which is suitable for large-scale software systems.
Patent Information
- Application Number
- CN202411580479.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-07
- Publication Date
- 2025-10-21
- Estimated Expiration
- 2044-11-07
AI Technical Summary
Existing technologies fail to effectively utilize the complex calling relationships between functions in function-level defect localization, resulting in insufficient positioning accuracy in large-scale software systems and making it difficult to meet the actual needs of developers.
Construct local call graphs and global call graphs, combine graph neural networks and pre-trained models, use multi-level contrastive learning methods to calculate the similarity between defect reports and functions, and use cosine similarity and multi-level contrastive loss functions to locate defects.
It significantly improves the accuracy and efficiency of defect localization, can understand function dependencies locally and globally, enhances the ability to identify cross-module defect paths, and is suitable for efficient processing of large-scale code bases.
Smart Images

Figure CN119441007B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of software engineering technology, and specifically to a function-level defect localization method based on call graphs and multi-level comparative learning. The method automatically identifies and locates defective functions in software projects by combining local call graphs with global call graphs, utilizing graph neural networks and pre-trained models. Background Art
[0002] With the rapid development of information technology, software systems are increasingly being used in key sectors such as finance, healthcare, transportation, and communications. The increasing complexity and scale of these systems have led to software defects becoming a major factor impacting system stability, security, and user experience. Efficiently and accurately locating and fixing software defects is crucial for ensuring system operation, reducing maintenance costs, and improving development efficiency. Defect localization involves identifying the source code that causes abnormal software behavior based on defect reports. Development teams typically rely on defect tracking systems such as Bugzilla and JIRA to record, track, and manage these defects. However, as software projects scale, the number of defect reports increases dramatically. For example, in the large-scale Eclipse software, users report nearly 200 defects daily. Manual analysis and localization alone consumes significant time and effort and requires developers to possess highly specialized skills.
[0003] To address this challenge, researchers have introduced various automated defect localization methods. In recent years, with the rapid development of artificial intelligence and machine learning technologies, more and more research has applied these advanced technologies to the field of software defect localization to improve the automation level and intelligent capabilities of localization.
[0004] Currently, related research mainly focuses on file-level defect location. Such methods are usually based on the analysis of the file's modification history, code features, or dependencies between files. However, a research survey shows that approximately 52% of software practitioners prefer to use function-level granularity for defect location, while only 26% prefer file-level granularity. This shows that in actual work, developers prefer to be able to quickly and accurately locate specific functions rather than just identify potentially problematic files. Although some studies have attempted to improve location accuracy by analyzing the code features of individual functions at the function level, these methods often treat functions as independent units and ignore the complex calling relationships and interactions between functions, resulting in insufficient performance in complex software systems. Therefore, how to better understand and utilize the relationships between functions at the function level to improve the accuracy of defect location remains an urgent research topic. Summary of the Invention
[0005] In view of the shortcomings of the existing technology, the present invention discloses a function-level defect localization method based on call graph and multi-level comparative learning to solve the problems raised in the above background technology.
[0006] To achieve the above objectives, the present invention provides the following technical solution: a function-level defect localization method based on call graph and multi-level comparative learning, comprising the following steps:
[0007] S1. Construct local call graphs and global call graphs. Use a parsing tool to parse the source code of the target class and generate an abstract syntax tree. By analyzing the abstract syntax tree, class names, fields, and member functions are identified, and the calling relationships between functions are extracted. This allows the construction of a local call graph containing class nodes and their internal function nodes. Use the same parsing tool to parse the source code of the entire software project and generate an abstract syntax tree for the project. By analyzing the abstract syntax tree, all functions and their calling relationships are identified, thereby constructing a global call graph covering the entire project.
[0008] S2. Use graph neural network for embedding learning, input the constructed local call graph and global call graph into Light-GCN respectively to obtain the embedding vector of each function in the local call graph and global call graph;
[0009] S3. Extract the semantic embedding vector and map it to a unified feature space. Use a pre-trained code model to extract the semantic embedding vector of the target function code. Use a pre-trained language model to extract the semantic embedding vector of the bug report text. Use a two-layer fully connected neural network and ReLU activation function to map the bug report embedding vector and the function's multi-dimensional embedding vector to the same feature space.
[0010] S4. Calculate the similarity between the bug report and the function. Use cosine similarity as the similarity measurement method to calculate the similarity between the semantic embedding vector of the bug report and the local embedding vector, global embedding vector, and code semantic embedding vector of the function.
[0011] S5. Build a multi-level contrast loss function model, and set a multi-level contrast loss function including local contrast loss, global contrast loss, and semantic contrast loss;
[0012] S6. Defect localization implementation: For each function, its comprehensive score is calculated based on the local similarity, global similarity and code semantic similarity between it and the defect report; each function is sorted in descending order according to its comprehensive score, and the top k functions with the highest scores are selected as the defect localization results.
[0013] Preferably, in step S1, the nodes in the local call graph include a node C representing a class and multiple nodes {F1, F2, ..., F m}; Class node C contains the class name and its attribute information, and is connected to all function nodes; the directed edges between function nodes represent function call relationships; the local call graph is represented as:
[0014] G LCG (V, E)
[0015] in,
[0016] V={C,F1,F2,...,F m},
[0017] E={(C,F i )|1≤i≤m}∪{(F i , F j )|F i Call F j}.
[0018] Preferably, in step S1, all function sets in the project are represented as {F1, F2, ..., F n}, the global call graph is represented as:
[0019] G GCG (V, E)
[0020] Where V = {F1, F2, ..., F n}, E={(F i , F j )|F i Call F j}.
[0021] Preferably, in step S2, the embedding vectors of each function in the local call graph and the global call graph are and The calculation formula is:
[0022]
[0023] in, and are the embedding vectors of nodes in the local call graph and the global call graph of the kth layer, respectively, and l is the number of layers of Light-GCN.
[0024] Preferably, in step S3, the semantic embedding vector of the function code is represented as:
[0025]
[0026] in, is the function F i The semantic embedding vector of .
[0027] Preferably, in step S3, the semantic embedding vector of the defect report is represented as:
[0028] r i =BERT(R i )
[0029] Among them, r i Report a bug for R i The semantic embedding vector of .
[0030] Preferably, in step S3, the embedding vector mapping is expressed as:
[0031]
[0032] Among them, δ represents the ReLU activation function, W (1) and W (2) is the weight matrix, b (1) and b (2) is the bias term.
[0033] Preferably, in step S4, the cosine similarity calculated in the similarity calculation is expressed as:
[0034]
[0035] Among them, z a and z b are two embedding vectors used to calculate similarity.
[0036] Preferably, the multi-level contrast loss in step S5 is expressed as:
[0037]
[0038] in,
[0039]
[0040]
[0041] Where τ is the temperature parameter, and Represent the similarity between the defect report and the local call graph, global call graph and code semantics respectively.
[0042] Preferably, the comprehensive score calculation in step S6 is expressed as:
[0043]
[0044] in, and Represent the similarity between the defect report and the local call graph, global call graph and code semantics respectively.
[0045] Compared with the prior art, the present invention has the following beneficial effects:
[0046] 1. This invention significantly improves the accuracy and efficiency of defect location by constructing a Local Call Graph (LCG) and a Global Call Graph (GCG), fully leveraging the calling relationships between functions. Specifically, LCG focuses on function calls within a single class, capturing intra-class dependencies and interactions at a fine-grained level, making it suitable for locating local defects. GCG, on the other hand, covers the function call relationships of the entire project, providing macro-level dependency patterns and contextual information. This helps identify cross-class and cross-module defect paths, enabling a comprehensive understanding of software structure and effectively locating complex defects.
[0047] 2. In the present invention, LCG and GCG complement each other. The former provides in-depth analysis capabilities, while the latter provides an overall structural perspective. The combination of the two not only covers multiple defect types but also significantly enhances the comprehensiveness and accuracy of positioning.
[0048] 3. In order to process large-scale code bases, the present invention introduces a lightweight graph convolutional network (Light-GCN) to extract the call graph representation, ensuring high efficiency and scalability, reducing computational complexity and accelerating processing speed.
[0049] 4. This paper uses pre-trained models (such as BERT and CodeBERT) to extract semantic representations of defect reports and codes, improving the quality of semantic representations.
[0050] 5. The present invention effectively optimizes the matching relationship between defect reports and function embeddings by designing a multi-level contrast loss function, including local contrast loss, global contrast loss and semantic contrast loss, thereby enhancing the overall positioning capability. BRIEF DESCRIPTION OF THE DRAWINGS
[0051] The accompanying drawings are used to provide further understanding of the present invention and constitute a part of the specification. They are used to explain the present invention together with the embodiments of the present invention and do not constitute a limitation of the present invention.
[0052] In the attached figure:
[0053] Figure 1 is a flow chart of the method of the present invention. DETAILED DESCRIPTION
[0054] The preferred embodiments of the present invention are described below with reference to the accompanying drawings. It should be understood that the preferred embodiments described herein are only used to illustrate and explain the present invention, and are not used to limit the present invention.
[0055] Example: Figure 1 As shown in the figure, the function-level defect localization method based on call graph and multi-level contrastive learning includes the following steps:
[0056] 1. Build a local call graph:
[0057] Use tools such as Spoon or JavaParser to parse the source code of the target class and generate the abstract syntax tree (AST) of the class. By analyzing the AST, the class name, fields, and all functions of the target class are identified, and the calling relationships between functions in the class are extracted. A local call graph is constructed, where the nodes include a node C representing the class and multiple nodes {F1, F2, ..., F m The class node C contains the class name and its attribute information and is connected to all function nodes; the directed edges between function nodes represent function call relationships.
[0058] The local call graph can be represented as:
[0059] G LCG (V, E)
[0060] in,
[0061] V={C,F1,F2,...,F m},
[0062] E={(C,F i )|1≤i≤m}∪{(F i ,F j )∣F i Call F j}
[0063] 2. Build a global call graph
[0064] Use Spoon or JavaParser to parse the source code of the entire software project and generate the project's AST. By analyzing the AST, identify all functions in the project, extract the call relationships between all functions in the project, and identify the call paths between functions. Then build the project's global call graph, where the nodes are all the functions defined in the project {F1, F2, ..., F n}, the edge is a directed edge from the caller function to the called function, indicating the calling relationship between functions.
[0065] The global call graph can be represented as:
[0066] G GCG (V,E)
[0067] in,
[0068] V={F1,F2,…,F n}
[0069] E={(F i ,F j )|F i Call F j}
[0070] 3. Graph Neural Network Embedding
[0071] The constructed local call graph and global call graph are input into Light-GCN respectively to obtain the embedding vector of each function in the local call graph and the global call graph.
[0072] 1) Node feature initialization:
[0073] For each node in the local call graph and the global call graph, the pre-trained code model (CodeBERT) is used to extract the semantic features of the node as the initial feature vector of the node:
[0074]
[0075] in, For node V v The initial eigenvector of .
[0076] 2) Light-GCN layer processing:
[0077] The core idea of Light-GCN is to reduce computational complexity through linear message passing. Its layer update formula is:
[0078]
[0079] Among them, h (k) is the node embedding vector of the k-th layer, is the normalized adjacency matrix. The specific normalization process is as follows:
[0080]
[0081] Among them, A is the adjacency matrix of the graph and D is the node degree matrix.
[0082] 3) Graph embedding vector generation:
[0083] Through multiple layers of Light-GCN, we can obtain multi-layer node embeddings for each function in the local call graph and the global call graph. The final node embedding vector can be obtained by summing the embeddings of each layer:
[0084]
[0085] in, is the function F in the local call graph of the kth layer iThe embedding vector of , l is the number of layers of Light-GCN.
[0086]
[0087] in, is the function F in the global call graph of the kth layer i The embedding vector of , l is the number of layers of Light-GCN.
[0088] 4. Semantic Embedding Vector Extraction
[0089] Use a pre-trained code model (such as CodeBERT) to extract the code semantic embedding vector of the target function; use a pre-trained language model (such as BERT) to extract the semantic embedding vector of the pre-processed defect report.
[0090] 1) Function code semantic embedding:
[0091]
[0092] in, is the function F i The semantic embedding vector of .
[0093] 2) Defect report semantic embedding:
[0094] r i =BERT(R i )
[0095] Among them, r i Report a bug for R i The semantic embedding vector of .
[0096] 5. Embedding Vector Mapping
[0097] Through two layers of fully connected neural network (Multi-Layer Perceptron, MLP) plus ReLU activation function, the function is locally embedded in the vector Function global embedding vector Function code semantic embedding vector and the defect report semantic embedding vector r i Mapped to a unified feature space. The mapping process is achieved through the following formula:
[0098] z'=W (2) δ(W (1) z+b (1) )+b (2)
[0099] Specifically, a two-layer fully connected neural network (MLP) and ReLU activation function are used to map the embedded vectors to the same feature space:
[0100]
[0101] Among them, W (1) and W (2) is the weight matrix of the fully connected layer, b (1) and b (2) is the bias, δ is the ReLU activation function, z is the input embedding vector, and z' is the mapped feature vector.
[0102] 6. Similarity calculation
[0103] Calculate the similarity between the defect report and the function in different dimensions to measure the degree of matching. Use cosine similarity as the similarity measurement method to calculate the semantic embedding vector r of the defect report respectively. i and embedding the function locally into a vector Function global embedding vector Function code semantic embedding vector Similarity between:
[0104]
[0105] Among them, z a and z b are two embedding vectors used to calculate similarity.
[0106] The specific similarity calculation is as follows:
[0107]
[0108] in, represents the similarity between defect report i and function j at the local level; represents the similarity between defect report i and function j at the global level; Indicates the similarity between defect report i and function j at the semantic level.
[0109] 7. Multi-level contrast loss function design
[0110] In order to optimize the model performance, a multi-level contrast loss function including local contrast loss, global contrast loss and semantic contrast loss is designed.
[0111] Local contrast loss:
[0112]
[0113] Global contrast loss:
[0114]
[0115] Semantic Contrastive Loss:
[0116]
[0117] Total contrast loss:
[0118]
[0119] 8. Defect location implementation
[0120] Using the trained model to locate defects includes the following steps:
[0121] 1) Calculate the comprehensive score
[0122] For each function F j , based on its comparison with the defect report R i The local similarity between Global similarity and code semantic similarity Calculate its comprehensive score S j :
[0123]
[0124] 2) Function sorting and result selection
[0125] According to the comprehensive score S of each function j Sort the functions in descending order and select the top k functions with the highest scores as the results of defect localization.
[0126] Finally, it should be noted that the above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art will be able to modify the technical solutions described in the aforementioned embodiments or substitute equivalents for some of the technical features. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention shall be included within the scope of protection of the present invention.
Claims
1. A function-level defect localization method based on call graph and multi-level contrastive learning, characterized by: The following steps are involved: S1. Construct local call graphs and global call graphs. Use a parsing tool to parse the source code of the target class and generate an abstract syntax tree. By analyzing the abstract syntax tree, class names, fields, and member functions are identified, and the calling relationships between functions are extracted. This allows the construction of a local call graph containing class nodes and their internal function nodes. Use the same parsing tool to parse the source code of the entire software project and generate an abstract syntax tree for the project. By analyzing the abstract syntax tree, all functions and their calling relationships are identified, thereby constructing a global call graph covering the entire project. S2. Use graph neural network for embedding learning, input the constructed local call graph and global call graph into Light-GCN respectively to obtain the embedding vector of each function in the local call graph and global call graph; S3. Extract the semantic embedding vector and map it to a unified feature space. Use a pre-trained code model to extract the semantic embedding vector of the target function code. Use a pre-trained language model to extract the semantic embedding vector of the bug report text. Use a two-layer fully connected neural network and ReLU activation function to map the bug report embedding vector and the function's multi-dimensional embedding vector to the same feature space. S4. Calculate the similarity between the bug report and the function. Use cosine similarity as the similarity measurement method to calculate the similarity between the semantic embedding vector of the bug report and the local embedding vector, global embedding vector, and code semantic embedding vector of the function. S5. Build a multi-level contrast loss function model, and set a multi-level contrast loss function including local contrast loss, global contrast loss, and semantic contrast loss; S6. Defect localization implementation: For each function, its comprehensive score is calculated based on the local similarity, global similarity and code semantic similarity between it and the defect report; each function is sorted in descending order according to its comprehensive score, and the top k functions with the highest scores are selected as the defect localization results.
2. The method for function-level defect localization based on call graph and multi-level contrastive learning according to claim 1 is characterized in that: In step S1, the nodes in the local call graph include a node C representing a class and multiple nodes {F1, F2, ..., F m }; Class node C contains the class name and its attribute information, and is connected to all function nodes; the directed edges between function nodes represent function call relationships; the local call graph is represented as: G LCG (V,E) in, V={C,F1,F2,…,F m }, E={(C,F i )|1≤i≤m}∪{(F i ,F j )∣F i Call F j ,1≤i≤m,1≤j≤m}.
3. The method for function-level defect localization based on call graph and multi-level contrastive learning according to claim 1 is characterized in that: In step S1, the set of all functions in the project is represented as {F1, F2, ..., F n }, the global call graph is represented as: G GCG (V,E) Where V={F1,F2,…,F n }, E={(F i ,F j )|F i Call F j ,1≤i≤n,1≤j≤n}.
4. The method for function-level defect localization based on call graph and multi-level contrastive learning according to claim 1 is characterized in that: In step S2, the embedding vectors of each function in the local call graph and the global call graph are and The calculation formula is: in, and are the embedding vectors of nodes in the local call graph and the global call graph of the kth layer, respectively, and l is the number of layers of Light-GCN.
5. The method for locating function-level defects based on call graph and multi-level contrastive learning according to claim 1 is characterized in that: In step S3, the semantic embedding vector of the function code is represented as: in, is the function F i The semantic embedding vector of .
6. The method for function-level defect localization based on call graph and multi-level contrastive learning according to claim 1 is characterized in that: In step S3, the semantic embedding vector of the defect report is represented as: r i =BERT(R i ) Among them, R i represents the i-th defect report, r i R i The semantic embedding vector obtained after BERT encoding.
7. The method for function-level defect localization based on call graph and multi-level contrastive learning according to claim 1 is characterized in that: In step S3, the embedding vector mapping is expressed as: Among them, δ represents the ReLU activation function, W (1) and W (2) is the weight matrix, b (1) and b (2) is the bias term, represents the semantic embedding vector of function code, represents the global embedding vector of the function, Represents the local embedding vector of the function.
8. The method for locating function-level defects based on call graph and multi-level contrastive learning according to claim 1 is characterized in that: In step S4, the cosine similarity of the similarity calculation is expressed as: Among them, z a and z b are two embedding vectors used to calculate similarity.
9. The method for locating function-level defects based on call graph and multi-level contrastive learning according to claim 1, characterized in that: The multi-level contrast loss in step S5 is expressed as: in, Where τ is the temperature parameter, and Represent the similarity between the defect report and the local call graph, global call graph and code semantics respectively.
10. The method for function-level defect localization based on call graph and multi-level contrastive learning according to claim 1, characterized in that: The comprehensive score calculation in step S6 is expressed as: in, and Represent the similarity between the defect report and the local call graph, global call graph and code semantics respectively.
Citation Information
Patent Citations
Bug report-oriented fine-grained defect positioning method
CN110502361A
Function-level defect positioning method based on embedding technology
CN112328469A