Source code vulnerability detection method and device based on graph neural network and multi-branch feature fusion and medium

By constructing a program dependency graph and combining it with a pre-trained language model and graph neural network, and adopting a multi-branch feature fusion model, the problem of insufficient fusion of structural and semantic features in source code vulnerability detection is solved, achieving high-precision vulnerability detection, which is suitable for automated scanning and early warning in software development.

CN121093344APending Publication Date: 2025-12-09HUZHOU UNIVERSITY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511188545.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-08-25
Publication Date
2025-12-09

AI Technical Summary

Technical Problem

Existing technologies for source code vulnerability detection suffer from insufficient integration of structural and semantic features, resulting in low detection accuracy, especially when identifying complex vulnerabilities.

Method used

A program dependency graph is constructed, and a pre-trained language model and graph neural network are combined. Through a multi-branch feature fusion model, including graph structure, text semantics and image features, the centrality index is used to weight semantic embedding. Graph convolutional network and graph attention network are used to extract features, and information is fused through an adaptive feature fusion module. Finally, the information is input into a fully connected layer for vulnerability classification.

Benefits of technology

It significantly improves the accuracy and precision of vulnerability detection, enhances the spatial feature perception capability of the model, is suitable for static source code analysis, has good versatility and ease of deployment, and is suitable for automated vulnerability scanning and early warning in actual software development.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121093344A_ABST
    Figure CN121093344A_ABST
Patent Text Reader

Abstract

The invention provides a source code vulnerability detection method and device based on a graph neural network and multi-branch feature fusion and a medium, and the method comprises the steps: carrying out the static analysis of an input source code, and constructing a program dependency graph; coding the text information of the nodes in the program dependency graph based on a pre-training language model to generate a semantic embedding representation of structure perception; in combination with centrality index analysis, converting the program dependency graph, and constructing a converted three-channel image; constructing a feature extraction network, and performing feature extraction on the program dependency graph, the text information and the three-channel image through the feature extraction network to obtain graph features, text features and image features; and fusing the graph features, the text features and the image features through an adaptive feature fusion module, and inputting the fused features into a full-connection layer classifier to complete vulnerability classification. According to the method, potential problems in source codes can be comprehensively understood and analyzed, and the accuracy of vulnerability detection is remarkably improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application belongs to the technical field of software security, and particularly relates to a source code vulnerability detection method based on a graph neural network and multi-branch feature fusion, a device and a medium. BACKGROUND

[0002] With the wide application of information technology, software systems have been deeply integrated into various fields of social operation and personal life, becoming an important foundation support for modern information society. However, with the increasing complexity and accelerated update iteration of software systems, security vulnerabilities have become increasingly serious. Vulnerabilities not only can cause system function abnormalities, but also can be exploited by malicious attackers, causing information leakage, service interruption and other serious consequences. Therefore, how to timely discover and locate security vulnerabilities in software systems has become an important technical problem in the software development and maintenance process.

[0003] At present, common source code vulnerability detection methods mainly include manual review and rule-based static analysis technology. The manual method relies on the experience of developers and has certain identification ability for specific types of vulnerabilities, but it is low in efficiency and strong in subjectivity, and is difficult to apply to large-scale code libraries. The rule-based static analysis tool scans for vulnerabilities through predefined pattern rules, although it has good effect in identifying some typical vulnerabilities, but when facing source code with complex structure or diverse logic, it still has problems such as high false positive rate and serious false negative, and it is difficult to meet the actual needs of high accuracy and high automation.

[0004] In recent years, with the development of deep learning technology, researchers have tried to introduce automatic learning models to improve the efficiency and accuracy of vulnerability detection. Among them, the graph neural network has gradually become a research hotspot because it is suitable for processing the graph structure features of source code. Through the construction of abstract syntax tree (AST), control flow graph (CFG), program dependency graph PDG and other forms, the graph convolution network (GCN) can effectively extract structural features and model the control logic and data dependency relationship in the program; the graph attention network further introduces the attention mechanism, enhancing the modeling ability of key nodes.

[0005] In addition, source code itself also contains a large amount of semantic information, such as variable naming, comment explanation, context, etc., and in recent years, natural language processing (NLP) technology has also been used to assist vulnerability detection. Through pre-training language models (such as BERT) to model the semantics of source code, the understanding ability of potential vulnerability patterns can be further improved. However, in actual application, structural modeling and semantic modeling are often carried out independently, and information fusion stage is prone to problems such as inconsistency between structure and semantics, missing of cross-modal information, etc., which limits the ability of the model in identifying complex vulnerabilities.

[0006] Although some studies have attempted to integrate structural features and semantic features to improve detection results, current methods still have deficiencies in model design, information fusion mechanism and feature expression ability. For example, some methods simply concatenate the graph structure and semantic representation without fully exploiting the relationship between the two; some schemes have complex structure in multi-modal modeling process, low training efficiency and limited generalization ability. Therefore, how to more effectively integrate the structural information and semantic information in the source code while maintaining the rationality of the model structure and improving the detection ability of complex vulnerabilities is still a problem to be solved in the current technical field. SUMMARY

[0007] In view of the problems of low source code vulnerability detection precision and insufficient feature modeling in the prior art, the present application provides a source code vulnerability detection method based on a graph neural network and multi-branch feature fusion, a device and a medium.

[0008] The technical scheme of the present application is as follows:

[0009] Performing static analysis on the input source code to construct a program dependency graph;

[0010] Encoding the text information of the nodes in the program dependency graph based on a pre-trained language model to generate a structure-aware semantic embedding representation;

[0011] Converting the program dependency graph in combination with centrality index analysis to construct a converted three-channel image;

[0012] Constructing a feature extraction network to extract features of the program dependency graph, text information and three-channel image through the feature extraction network to obtain graph features, text features and image features;

[0013] Fusing the graph features, text features and image features through an adaptive feature fusion module and inputting a fully connected layer classifier to complete vulnerability classification.

[0014] Further, each node in the program dependency graph represents a program statement or function call, and the edge represents a dependency relationship.

[0015] Further, the encoding of the text information of the nodes in the program dependency graph based on the pre-trained language model is as follows:

[0016]

[0017] wherein Line i is the source code text corresponding to the node, and the BERT output semantic embedding vector

[0018] Further, the centrality index includes degree centrality, closeness centrality and Katz centrality.

[0019] Further, the feature extraction network comprises a three-branch parallel structure:

[0020] The graph structure branch adopts a graph neural network, combines a graph convolution network and a graph attention network hybrid structure, processes a program dependency graph structure, and obtains graph feature information;

[0021] The text semantic branch adopts a bidirectional gated recurrent unit structure, is used for modeling a context relationship of a semantic vector of a node, enhances a semantic sequential modeling capability, and captures context information between semantics;

[0022] The image feature branch converts a structure matrix into an image input form, and extracts structure difference features of an image level through an image convolution network.

[0023] Further, the graph feature, the text feature and the image feature are fused through an adaptive feature fusion module, specifically:

[0024]

[0025] Wherein, is the graph feature, is the text feature, is the image feature, is the fusion feature.

[0026] Further, an input full connection layer classifier is used to complete vulnerability classification, specifically:

[0027]

[0028] Wherein, represents a prediction result, W o and b are output layer parameters, is the fusion feature.

[0029] Further, the method is trained with a binary classification target, a label "1" represents that there is a vulnerability, and "0" represents that there is no vulnerability, and is used for automatic vulnerability detection of static source code after training.

[0030] An electronic device comprising a memory and a processor, the memory being used to store a computer program, and the processor running the computer program to make the electronic device execute the above-mentioned source code vulnerability detection method based on a graph neural network and multi-branch feature fusion.

[0031] A computer readable storage medium having a computer program stored thereon, the program being executed by a processor to implement the above-mentioned source code vulnerability detection method based on a graph neural network and multi-branch feature fusion.

[0032] Compared with the prior art, the present application has the following advantages:

[0033] The present application proposes a three-branch multi-modal vulnerability detection model that fuses program dependency graph structure, text semantic information and image structure features. Compared with traditional single-modal models, the present application significantly improves the representation ability and accuracy of vulnerability detection.

[0034] By graph centrality weighted semantic embedding, the role of key nodes in program logic is effectively highlighted, and the accuracy of structural semantic modeling is improved.

[0035] A structural semantic image visualization mechanism is designed, so that the model can capture potential vulnerability patterns in the structure from the image space, enhancing the spatial feature perception ability of the model.

[0036] A multi-branch adaptive feature fusion module is introduced, which enhances the complementarity and fusion effect between multi-modalities and improves the discrimination performance of the model.

[0037] Without relying on runtime information, static source code analysis can be performed, and good universality and deployment convenience are achieved, which is suitable for automatic vulnerability scanning and early warning tasks in actual software development. BRIEF DESCRIPTION OF DRAWINGS

[0038] The accompanying drawings generally illustrate various embodiments of the present application, and are not intended to limit the present application or the application and features thereof described in the specification and claims. Wherever possible, the same reference numbers are used in all drawings and the specification to refer to the same or similar parts. Such embodiments are illustrative rather than limiting, and are not intended to exhaustively enumerate all possible implementations of the present device or method.

[0039] Fig. 1 is a whole flow chart of a source code vulnerability detection method.

[0040] Fig. 2 is a program dependency graph structure schematic diagram.

[0041] Fig. 3 is a whole block diagram of a multi-branch feature fusion source code vulnerability detection algorithm. DETAILED DESCRIPTION

[0042] It should be noted that the embodiments in the present application and the features in the embodiments can be combined with each other without conflict. The present application will be described in detail below with reference to the accompanying drawings and in combination with embodiments.

[0043] As shown in Figs. 1-3 , the present application provides a source code vulnerability detection method based on graph neural network and multi-branch feature fusion, comprising the following steps:

[0044] Step S1: First, the source code is split into independent line-level statements. Each line of code is treated as an independent unit, which allows for clearer dependency analysis and vulnerability detection. The purpose of this step is to provide basic units for subsequent analysis and feature extraction, ensuring that each node (line-level code) can be processed independently.

[0045] Step S2: Lexical and syntactic analysis of the source code is performed to identify functions, statements, and their control structures in the program, and a vector sequence is constructed. This step helps understand the structure of the source code and the control flow and data flow information within it. Through the construction of a syntax tree, we can capture the relationships between various statements and control structures in the code and generate corresponding representations.

[0046] Step S3: Source code image feature extraction branch, in this part, the image features of the source code are extracted, and the feature extraction process is completed through the following sub-steps:

[0047] Step S3.1: Extract control and data dependency relationships in the program. First, analyze the control and data dependency relationships in the source code and extract the relevant dependencies. Control dependency represents the order of control flow in the code, while data dependency represents the data flow between different statements. By extracting these relationships, we can better understand the behavior patterns of the program during execution and further discover potential vulnerabilities.

[0048] Step S3.2: Construct a program dependency graph based on control and data dependency relationships. Based on control and data dependency relationships, construct a program dependency graph PDG. This structured graph representation provides a foundation for subsequent graph neural network modeling and vulnerability analysis.

[0049] Where each node represents a line of code, and each edge represents a control or data dependency. Node: code line, such as Fig. 2 Program dependency graph structure diagram, according to control and data dependency analysis, the resulting graph structure is:

[0050] G = (V, E), V = {v1, v2,..., vn}, E = {e1, e2,..., en} n}

[0051] Step S3.3: Extract the code text corresponding to each node in the PDG to provide raw input for subsequent semantic modeling. The code text of each node contains key semantic information of the program, which provides a basis for subsequent semantic analysis and vulnerability detection.

[0052] Step S3.4: Use the pre-trained language model BERT to perform semantic encoding on the code text of each node in S3.3:

[0053]

[0054] where Line i is the source code text corresponding to the node, and BERT outputs semantic embedding vectors The code is converted into a high-dimensional semantic vector. The BERT pre-training model can capture the complex semantic information in the source code.

[0055] Step S3.5: Based on the PDG structure, calculate multiple graph centrality indicators for each node, and calculate centrality indicators: degree centrality, closeness centrality and Katz centrality. The centrality indicators help measure the importance of the nodes in the program graph, and further evaluate the potential role of these nodes in vulnerability propagation:

[0056] Degree Centrality (Degree Centrality): used to measure the connection degree of a node in a graph, that is, the number of other nodes directly connected to the node. Degree centrality reflects whether the position of the node in the graph structure is critical, and the more connections, the higher the importance of the node in information transmission.

[0057] The formula for calculating degree centrality is:

[0058]

[0059] where x i is the degree centrality of node i, deg(i) is the degree of node i (i.e. the number of directly adjacent nodes), N is the total number of nodes in the graph, and N-1 is the maximum number of connections that the node can reach (i.e. the normalization factor).

[0060] Closeness Centrality (Closeness Centrality): Closeness Centrality is used to quantify the closeness of a node to all other nodes in the network. The core idea is that the shorter the path from a node to other nodes, the more important the node is in the network.

[0061]

[0062] where d(i,j) is the shortest path length from node i to node j (i.e. the minimum number of edges), and N is the total number of nodes in the network

[0063] Katz Centrality (Katz Centrality): considers both path length and node influence.

[0064] The centrality of a node is calculated based on the centrality of its neighbors. The Katz centrality of node i is:

[0065]

[0066] where x iis the Katz centrality value of node i. A ij is an element of the adjacency matrix, indicating whether node j is connected to node i.∑ j denotes the summation of all neighbor nodes j of node i. β is a constant to prevent centrality value from being zero. α is the decay factor, and the parameter α needs to meet the following conditions to ensure convergence:

[0067]

[0068] Step S3.6: Weighted fusion of the semantic encoding vector of each node with its corresponding graph centrality indicator to generate a structure-aware feature vector. Specifically, for each node's semantic vector v i , respectively, with its degree centrality d i , Katz centrality k i , and closeness centrality c i , element-wise multiplication is performed to obtain three groups of weighted vectors:

[0069] Red channel: v i × d i

[0070] Green channel: v i × k i

[0071] Blue channel: v i × c i

[0072] where d i , k i , and c i are scalars, v i is a vector of dimension n, and the multiplication operation represents applying scalar weights to all elements of the vector.

[0073] Finally, a vector matrix of three channels is formed, representing different structure information weighted features.

[0074] Step S3.7: The three weighted feature matrices obtained in step S3.6 are taken as the three channels (red, green, blue) of the image, respectively, to generate a structured image representation for input into an image classification model for defect prediction. Each row of the image corresponds to a line of source code, and the column is the dimension of the semantic vector. Different channels reflect different centrality weighted features.

[0075] Step S4: GAT-based graph structure feature extraction branch

[0076] Step S4.1: According to the PDG in step S3, each line of statements in the source code is taken as a node of the graph, and control dependency and data dependency are taken as edges to construct the graph structure of the source code.

[0077] Step S4.2: Model the graph structure using a graph convolutional network (GCN) that combines a graph attention network (GAT) to adaptively weight (i.e., attention scores) the neighboring nodes of each node, enabling the model to dynamically adjust the information passing weights between nodes.

[0078] The GCN can effectively capture local dependencies in the code, which is particularly important for detecting vulnerabilities such as uninitialized variables, null pointer references, and other local properties. It updates the representation of the central node by aggregating the information of neighboring nodes, with the node formula as follows:

[0079]

[0080] where, denotes the normalized adjacency matrix, z l denotes the node features at the l-th layer, W l denotes a learnable weight matrix, and σ denotes a nonlinear activation function.

[0081] Step S4.3: Model the importance of the graph structure using the graph attention network GAT. GAT dynamically calculates the importance of neighboring nodes using the attention mechanism, and the node update method adjusts the representation of the node according to the relative importance between nodes. GAT enables the model to learn high-order dependencies between nodes in the graph structure. The node update method is as follows:

[0082]

[0083] where, denotes the attention coefficient of node j to node i in the l-th layer, W (l) is a linear transformation matrix, is the input representation of node j

[0084] Step S4.4: Capture potential vulnerability features in the program by analyzing the upstream and downstream influence relationships of the nodes. These potential features are usually the causes and consequences of vulnerability occurrence, which can help the model more accurately detect vulnerabilities.

[0085] Step S4.5: Output the graph structure feature vector.

[0086] Step S5: Bi-GRU-based text semantic feature extraction branch

[0087] Step S5.1: Based on the vector sequence obtained in step S2, use the bidirectional gated recurrent unit Bi-GRU to model the sequence. Bi-GRU can capture the semantic information of the context in the source code, enhancing the model's ability to perceive semantic vulnerabilities.

[0088] Step S5.2: Through Bi-GRU, the model can effectively learn the semantic patterns such as variables, function calls, conditional logic, etc. in the source code, thereby enhancing the ability to identify potential semantic vulnerabilities in the code.

[0089] Step S5.3: Through Bi-GRU, output the text semantic feature vector, providing data for subsequent feature fusion.

[0090] Step S6: Concatenate the feature vectors extracted by the three branches (graph structure branch, text semantic branch, image feature branch) to obtain a fused feature representation. This representation combines various features of the source code and can comprehensively reflect the structure, semantics, and image features of the source code.

[0091] The feature representation output by each branch is as follows:

[0092] Graph structure branch:

[0093] Text semantic branch:

[0094] Image feature branch:

[0095] Step S7: Through the adaptive fusion module, use the gating mechanism to weight and fuse the concatenated features. The gating mechanism can dynamically adjust the contribution of each modal feature according to its importance, thereby achieving more accurate feature fusion.

[0096] Concatenated feature representation:

[0097]

[0098] Step S8: Input the fused features into the fully connected layer classifier to perform vulnerability classification and output the prediction label of whether the source code has vulnerabilities. The fully connected layer classifier classifies the source code based on the learned features to determine the presence of vulnerabilities.

[0099]

[0100] where, represents the prediction result, W o and b are the output layer parameters.

[0101] Step S9: During training, set the sample labels, "1" indicating that the source code has vulnerabilities and "0" indicating that the source code has no vulnerabilities, to supervise model training.

[0102] Step S10: Finally, output the detection result of whether the source code has vulnerabilities based on the classification result.

[0103] The above merely describes preferred specific embodiments of the present application, but the protection scope of the present application is not limited thereto, any person skilled in the art can make equivalent replacements or changes according to the technical scheme and inventive concept of the present application within the technical range disclosed by the present application, which should be covered in the protection scope of the present application.

Claims

1. A source code vulnerability detection method based on graph neural networks and multi-branch feature fusion, characterized in that, The method includes: Perform static analysis on the input source code to construct a program dependency graph; The text information of the nodes in the program dependency graph is encoded based on a pre-trained language model to generate a structure-aware semantic embedding representation. By combining centrality index analysis, the program dependency graph is transformed to construct a transformed three-channel image; A feature extraction network is constructed, and features are extracted from the program dependency graph, text information, and three-channel image through the feature extraction network to obtain graph features, text features, and image features. The graph features, text features, and image features are fused by the adaptive feature fusion module and then input into the fully connected layer classifier to complete the vulnerability classification.

2. The source code vulnerability detection method based on graph neural network and multi-branch feature fusion according to claim 1, characterized in that, Each node in the program dependency graph represents a program statement or function call, and the edges represent dependencies.

3. The source code vulnerability detection method based on graph neural network and multi-branch feature fusion according to claim 1, characterized in that, The text information encoding of nodes in the program dependency graph based on the pre-trained language model is specifically as follows: Among them, Line i The source code text corresponding to the node; BERT outputs a semantic embedding vector.

4. The source code vulnerability detection method based on graph neural network and multi-branch feature fusion according to claim 1, characterized in that, The centrality indices include degree centrality, proximity centrality, and Katz centrality.

5. The source code vulnerability detection method based on graph neural network and multi-branch feature fusion according to claim 1, characterized in that, The feature extraction network includes a three-branch parallel structure: Graph structure branch: A graph neural network is used, which combines a graph convolutional network and a graph attention network. The processing program depends on the graph structure to obtain graph feature information. Text semantic branching: A bidirectional gated cyclic unit structure is adopted to model the semantic vector context relationship of nodes, so as to enhance the semantic order modeling capability and capture the contextual information between semantics; Image feature branch: Transforms the structure matrix into image input form and extracts structural difference features at the image level through an image convolutional network.

6. The source code vulnerability detection method based on graph neural network and multi-branch feature fusion according to claim 1, characterized in that, The fusion of graph features, text features, and image features via the adaptive feature fusion module is specifically as follows: in, For graph features, For text features, For image features, This is a feature of fusion.

7. The source code vulnerability detection method based on graph neural network and multi-branch feature fusion according to claim 1, characterized in that, The specific steps for classifying vulnerabilities using a fully connected layer classifier are as follows: in, W represents the prediction result. o b are the output layer parameters. This is a feature of fusion.

8. The source code vulnerability detection method based on graph neural network and multi-branch feature fusion according to any one of claims 1-7, characterized in that, The method is trained using a binary classification target, where "1" indicates the presence of a vulnerability and "0" indicates the absence of a vulnerability. After training, it is used for automatic vulnerability detection in static source code.

9. An electronic device, characterized in that, The device includes a memory and a processor, the memory being used to store a computer program, and the processor running the computer program to cause the electronic device to perform the source code vulnerability detection method based on graph neural network and multi-branch feature fusion as described in any one of claims 1-8.

10. A computer-readable storage medium, characterized in that, It stores a computer program, which, when executed by a processor, implements the source code vulnerability detection method based on graph neural network and multi-branch feature fusion as described in any one of claims 1-8.