A software vulnerability detection method based on adaptive fusion of multimodal features
By comprehensively utilizing the compiler front-end information and building collaborative loss functions, extracting and fusing the characteristics of code sequences and graph structure modalities, the problem of low modal interaction fusion in the existing technology is solved, and efficient and accurate software vulnerability detection is achieved .
Patent Information
- Application Number
- CN202411482085.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-23
- Publication Date
- 2025-02-07
- Estimated Expiration
- 2044-10-23
AI Technical Summary
Existing software vulnerability detection methods ignore the comprehensive interaction of lexical, grammatical and semantic information in the code sequence modality, and lack deep interaction and efficient fusion during modal interaction fusion, resulting in poor detection results.
By comprehensively utilizing the compiler front-end information, including the lexical, syntax and semantic information of the code sequence, a collaborative loss function is constructed to alleviate the gap between the different modes of the code sequence and the code graph structure, and the in-sample loss function and the comparison learning loss function are extracted using in-sample loss function and comparative learning loss function. Share features and unique features, and generate comprehensive code representations through adaptive fusion.
It significantly improves the detection accuracy of real vulnerability code, improves the efficiency and accuracy of vulnerability detection, and reduces the possibility of missed and missed detection.
Smart Images

Figure CN119004491B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to a software vulnerability detection method based on multi-modal feature adaptive fusion, and belongs to the technical field of computer security. Background Art
[0002] With the widespread application of information technology, the complexity and scale of software systems have increased significantly, resulting in increasingly prominent software security issues. Software vulnerabilities are a major risk to network security. If they are not identified and repaired in a timely manner, they may cause serious security incidents.
[0003] Traditional vulnerability detection methods mainly rely on predefined rules developed by experts for code analysis. Although these rules can work in some cases, their significant drawbacks include high cost of rule development and maintenance, and difficulty in covering all potential vulnerabilities, which affects the comprehensiveness and accuracy of detection. In recent years, the application of deep learning technology has significantly improved the ability of vulnerability detection. Deep learning models do not rely on predefined rules, but improve detection performance by automatically learning vulnerability patterns. These models can extract features from large-scale data sets, enhance detection accuracy and generalization capabilities, and simplify the system construction and deployment process, thereby improving efficiency and security.
[0004] Software vulnerability detection methods based on deep learning can be mainly divided into two categories: one category regards the code as a special natural language problem, converts it into a natural language processing task, and extracts features from the code sequence; the other category uses the graph structure of the code, such as control flow graphs, program dependency graphs, and code property graphs, to learn the hierarchical information and dependencies in the code through graph neural network models (such as graph convolutional networks).
[0005] However, the existing technologies have the following deficiencies: In terms of code sequence modality, current methods usually convert the code into a single-line sequence, or process it through code slicing, symbolization, or path extraction. These methods mainly focus on the lexical level information of the code sequence, but ignore the comprehensive interaction of lexical, grammatical, and semantic information. In terms of modal interaction fusion, although some methods try to demonstrate the potential of multimodality by building a code behavior graph model and fusing it with other models, these methods often simply concatenate features at the final layer when fusing modalities, lacking deep interaction and efficient fusion, which limits their effectiveness in vulnerability detection. Summary of the invention
[0006] In view of the shortcomings of the prior art, the present invention proposes a software vulnerability detection method based on adaptive fusion of multimodal features. This method significantly enhances the code sequence features by comprehensively utilizing compiler front-end information, including the lexical and grammatical information of the code sequence and the semantic information in code pre-training. Secondly, a collaborative loss function is constructed to alleviate the gap between the code sequence and the different modes of the code graph structure. The intra-sample loss function is used to extract the shared features and unique features of the code modality, and the contrastive learning loss function is introduced to achieve effective distinction of features between different samples. Finally, by adaptively fusing the shared features with the unique features of each modality, the detection accuracy of the real vulnerability code is significantly improved.
[0007] A software vulnerability detection method based on multimodal feature adaptive fusion includes the following steps:
[0008] Step S1: preprocessing the software code sample;
[0009] Step S2: construct a software vulnerability detection model, which includes a code sequence information extraction module, a graph structure information extraction module, a modal optimization module, a code generation module and a classifier;
[0010] The code sequence information extraction module extracts code sequence feature information from the preprocessed code sample, and in this process enhances the code sequence feature information by integrating the front-end information of the compiler, wherein the front-end information of the compiler includes the lexical information and syntactic information of the code sample;
[0011] Step S3: The graph structure information extraction module obtains the graph structure feature representation of the code sample;
[0012] The code sequence feature information of step S2 and the graph structure feature of step S3 represent multimodal information constituting the code sample;
[0013] Step S4: The modal optimization module constructs an internal loss function, imports multimodal information into the modal optimization module, obtains the shared features and unique features of the code samples in the two modes, and uses a contrastive learning loss function in this process to enhance the discrimination between code samples;
[0014] Step S5: the code generation module adaptively fuses the unique features of the two modes based on the shared features in step S4 to generate a comprehensive code representation of the code sample;
[0015] Step S6: Input the comprehensive code representation of the code sample in step S5 into the classifier, and determine whether the code sample has a vulnerability based on the prediction score.
[0016] Furthermore, step S1 is specifically as follows:
[0017] Step S11: removing redundant information of code samples through code cleaning, the redundant information includes comments and logs, improving the validity of sample data and reducing noise interference;
[0018] Step S12: Delete duplicate code snippets to prevent the model from relying on duplicate samples during training, thereby ensuring the accuracy of vulnerability identification;
[0019] Step S13: Use a static analysis tool to parse the cleaned code sample and generate a code property graph to capture data flow and control flow information; if the code property graph cannot be generated or the generated code property graph structure is incomplete, delete the code sample.
[0020] Furthermore, step S2 is specifically as follows:
[0021] Step S21: convert the code sample into a character sequence format, and apply the BPE method to perform word segmentation processing to obtain the lexical information of the code sample; expressed as:
[0022] ;
[0023] in, Represents the lexical information of the code sample, Represents the lexical unit generated by BPE segmentation, Indicates the BPE word segmentation method, Code samples representing character sequence formats;
[0024] Step S22: Use the Tree-sitter tool to parse the code sample, generate an abstract syntax tree AST, and convert the abstract syntax tree AST into a linear sequence through pre-order traversal to obtain the syntax information of the code sample. ; expressed as:
[0025] AST = Tree-sitter (Code);
[0026] ;
[0027] Among them, Tree-sitter represents the parser generation tool and incremental parsing library, Code represents the code sample, represents pre-order traversal; , n is the current node, children(n) returns the child nodes of the current node;
[0028] Step S23: lexical information of the code sample of step S21 and grammatical information of the code sample of step S22 Use pre-trained models to generate lexical features and grammatical features respectively;
[0029] Specifically, the lexical and grammatical features of the code sample are captured through the multi-head attention mechanism to generate combined embeddings; these combined embeddings are concatenated to obtain the embedding vector of the code sample; finally, these combined embeddings are concatenated to obtain the complete code sequence feature vector ;
[0030] ;
[0031] ;
[0032] ;
[0033] ;
[0034] in, Represents the lexical features of the code sample, A code sample represents the grammatical features of a code sample. represents the pre-trained model, represents the multi-head attention mechanism, represents the combined embedding of code samples, that is, the feature information of code sequences, Indicates concatenation along a specific dimension.
[0035] Furthermore, step S3 is specifically as follows:
[0036] Step S31: Obtain the code property graph CPG corresponding to the code sample in step S13, perform node embedding on the generated code property graph CPG, and obtain graph node features corresponding to the code sample;
[0037] Specifically: Use the word embedding method to embed each node and its type in the code sample Converted into a comprehensive vector representation of the node, that is, the graph node feature, is expressed as:
[0038] ;
[0039] Among them, each node By Type and the corresponding lines of code Composition, V is the graph node feature, Representation word embedding method;
[0040] Step S32: Apply the enhanced graph representation learning method to optimize the node embedding expression by combining heterogeneous edge type information, and capture the relationship between distant nodes by expanding the convolution kernel size, and finally generate the feature representation of the graph structure of the code sample , expressed as:
[0041] ;
[0042] G = (V, E);
[0043] Among them, G represents the code property graph CPG, V represents the nodes in the graph, and E represents the edges. represents the parameters of the enhanced graph representation learning method, Indicates expanding the convolution kernel size.
[0044] Furthermore, in step S3, each code modal feature is separated into shared features and unique features, and a collaborative loss function is constructed, including an intra-sample loss function, a contrastive learning loss function, and a cross-entropy loss function, to optimize the learning process of shared features and unique features, and ensure the full separation and effective fusion of different modal features. Step S3 is specifically as follows:
[0045] Step S31: Designing a modal common encoder and modality-specific encoders ; Modal common encoder Used to map code modality features into a shared feature vector space, modality-specific encoder Used to map code modal features to a unique feature vector space, modal common encoder and modality-specific encoders They are all multi-layer perceptron layers MLP with activation functions, expressed as;
[0046] ;
[0047] ;
[0048] ;
[0049] ;
[0050] in, and All belong to , express dimensional real vector space; Code sequence feature information and graph structure feature vector Alignment feature representation of Represents the shared parameters under the code bimodality, Indicates the first proprietary parameter of a unique encoder. Indicates the second proprietary parameter of a unique encoder; and Respectively represent the code sequence feature information and graph structure feature vector The common feature vector of , that is, the shared feature; and Respectively represent the code sequence feature information and graph structure feature vector The unique feature vector of , that is, the unique feature;
[0051] Step S32: constructing a collaborative loss function to optimize the acquired shared features and unique features, the collaborative loss function includes an intra-sample loss function, a contrastive learning loss function, and a cross entropy loss function;
[0052] In-sample loss function It is expressed as:
[0053] ;
[0054] in, Represents minimizing the similarity loss and reducing the differences in latent feature representations between different domains. represents the orthogonality constraint loss, represents the mean square error loss;
[0055] Step S321: Minimizing the Similarity Loss The CMD loss function is used to measure the similarity of feature distributions of different modalities. The CMD loss function measures the difference between distributions by comparing the high-order central moments of different modalities. The formula is as follows:
[0056] ;
[0057] in, and They represent the i-th order central moment of the code sequence modality CT and the code graph structure modality CG in the shared feature space, respectively, and k is the order of the maximum central moment;
[0058] By minimizing the CMD loss function, the distribution differences of different modalities in the shared feature space can be effectively reduced, ensuring that the shared features of different modalities can be better aligned;
[0059] Step S322: Orthogonality Constraint Loss A minimization of the orthogonality loss function is constructed to reduce the differences in modality-specific features, and its formula is as follows:
[0060] ;
[0061] in, and Represents the modal common encoder and modality-specific encoders The output feature representation matrix, represents the Frobenius norm, which is used to calculate the square root of the sum of the squares of the elements of a matrix;
[0062] By minimizing the orthogonality loss function, and Orthogonality, thus ensuring that different aspects of the different modal data can be modeled independently;
[0063] Step S323: Design a decoder , input the shared features and unique features of the code mode m, output the initial features that can reconstruct the mode, and use the mean square error to measure the reconstruction error. The formula is as follows:
[0064] ;
[0065] ;
[0066] in, represents the feature representation of the code mode m after being extracted by the encoder and aligned; the decoder Combine the shared features of modality m with the unique features to form a new feature representation to effectively reconstruct the original features of modality m ;in, are the parameters of the decoder, is a regularization term used to prevent overfitting;
[0067] Step S33: Constructing contrastive learning loss function , used to solve the in-sample loss function The feature alignment problem between different samples is not considered, and the global representation between different samples is optimized, specifically:
[0068] Use the feature embedding of the last layer of the respective modality encoder model as the global representation of each code sequence and graph structure sample; maximize the cosine similarity of the code sequence embedding and graph structure embedding of B pairs of true pairs in the batch, while minimizing The cosine similarity of the error pair embeddings is expressed as:
[0069] ;
[0070] In the above formula, represents the expected value calculation of the modal feature embedding z, Represents the negative sample indicator function, which is used to select other samples that are not equal to the current sample index j. represents the features obtained from the code sequence modality CT with sample index j, represents the positive sample features obtained from the code graph structure modality CG with sample index j, Represents the negative sample features obtained from the code graph structure modality CG with sample index k; represents the features obtained from the code graph structure modality CG with sample index j, represents the positive sample features obtained from the code sequence modality CT with sample index j, Represents the negative sample features obtained from the code sequence modality CT with sample index k;
[0071] is the standard contrast loss function, and the formula is:
[0072] ;
[0073] in, are learnable parameters.
[0074] Furthermore, the step S5 adaptively fuses the modal difference features based on the modal shared features, including:
[0075] Step S51: Apply the cross-attention mechanism to extract code sequence feature information and graph structure feature vector The common information of the modalities is combined with the modal shared features to obtain enhanced shared features. ;
[0076] Specifically:
[0077] In the cross-attention mechanism, the inputs of the query matrix Q, key matrix K, and value matrix V come from two different modalities, where the sequence X is used as the input of the query matrix Q, and the sequence Y is used as the input of the key matrix K and the value matrix V. The formula is as follows:
[0078] ;
[0079] in, , and are the projection matrices of queries, keys, and values, is the scaling factor of the dimension, used to stabilize the gradient during training, represents the cross attention mechanism;
[0080] Next, and Shared features and The two are input together into the multi-head attention mechanism, which further extracts and fuses features through the following formula:
[0081] ;
[0082] ;
[0083] in, Represents a long mechanism, represents the attention mechanism, represents the multi-head attention adjustment parameter, represents the i-th head output, , and They represent the adjustment parameters of the query matrix, key matrix, and value matrix corresponding to the i-th head in the multi-head attention mechanism respectively;
[0084] Finally, by splicing the outputs of each head, the enhanced modality shared features are obtained ;
[0085] Step S52: Calculate the enhanced modality shared features based on the cross-attention mechanism With unique features and These correlation features are input into the gated neural unit to obtain the first fusion weight that can be learned. and the second fusion weight , expressed as:
[0086] ;
[0087] ;
[0088] in, Represents a gated neural unit, implemented by a sigmoid activation function, which regulates the flow of information by assigning importance weights to input features;
[0089] Step S53: Sharing features based on enhanced modalities , unique features and , adaptively obtain the final multimodal fusion features , that is, the comprehensive code representation of the code sample, is expressed as:
[0090] ;
[0091] in, represents the first fusion weight, Represents the second fusion weight.
[0092] Furthermore, step S6 specifically includes:
[0093] Inputting the synthetic code representation of the code sample into the classifier to obtain the prediction result of vulnerability detection;
[0094] ;
[0095] In the above formula, is the weight matrix, is the bias term, is a comprehensive code representation of the code sample, is the Sigmoid activation function, Represents the prediction score, which is used to express probability;
[0096] Among them, the classifier processes the multimodal fusion features through linear transformation and activation function, and outputs the prediction score of whether the code sample has vulnerabilities or not, and predicts the output result according to the size of the prediction score. The output result includes vulnerability samples or non-vulnerability samples.
[0097] The beneficial effects of the present invention are:
[0098] (1) The present invention proposes a software vulnerability detection method based on adaptive fusion of multimodal features. It comprehensively utilizes code sequence feature information and graph structure feature representation to automatically detect potential vulnerabilities in unknown code and accurately locate them at the function level. This method simulates the front end of the compiler by combining the lexical and grammatical information of the code sequence and the powerful semantic capabilities of the pre-trained model, thereby enhancing the modal features of the code sequence. In addition, the code graph structure information is fully utilized. This comprehensive method effectively improves the efficiency and accuracy of vulnerability detection, assists manual review, and significantly reduces the possibility of missed detection and false detection.
[0099] (2) Existing multimodal code vulnerability detection methods often fail to fully consider the differences between modalities when fusing features of different modalities. They usually use simple feature splicing and lack effective interaction methods, resulting in poor detection results. In contrast, the present invention fully considers the differences between different modalities, mines shared features and unique features by constructing an intra-sample loss function, and uses contrastive learning to enhance the discrimination of samples. By extracting modal shared features as a bridge and adaptively fusing the unique features of each modality, an efficient comprehensive code representation is generated, which significantly improves the accuracy and efficiency of vulnerability detection. BRIEF DESCRIPTION OF THE DRAWINGS
[0100] Figure 1 It is a flow chart of a software vulnerability detection method based on multi-modal feature adaptive fusion of the present invention. DETAILED DESCRIPTION
[0101] The technical solution of the present invention will be described clearly and completely below in conjunction with the accompanying drawings in the embodiments of the present invention. The described embodiments are only a part of the present invention, not all of it. All other embodiments based on the embodiments of the present invention and obtained without creative work belong to the protection scope of the present invention.
[0102] Reference Figure 1, a software vulnerability detection method based on multimodal feature adaptive fusion, in one example, includes the following steps:
[0103] Step S1: preprocessing the software code sample;
[0104] Step S2: construct a software vulnerability detection model, which includes a code sequence information extraction module, a graph structure information extraction module, a modal optimization module, a code generation module and a classifier;
[0105] The code sequence information extraction module extracts code sequence feature information from the preprocessed code sample, and in this process enhances the code sequence feature information by integrating the front-end information of the compiler, wherein the front-end information of the compiler includes the lexical information and syntactic information of the code sample;
[0106] Step S3: The graph structure information extraction module obtains the graph structure feature representation of the code sample;
[0107] The code sequence feature information of step S2 and the graph structure feature of step S3 represent multimodal information constituting the code sample;
[0108] Step S4: The modal optimization module constructs an internal loss function, imports multimodal information into the modal optimization module, obtains the shared features and unique features of the code samples in the two modes, and uses a contrastive learning loss function in this process to enhance the discrimination between code samples;
[0109] Step S5: the code generation module adaptively fuses the unique features of the two modes based on the shared features in step S4 to generate a comprehensive code representation of the code sample;
[0110] Step S6: Input the comprehensive code representation of the code sample in step S5 into the classifier, and determine whether the code sample has a vulnerability based on the prediction score.
[0111] Furthermore, in one example, step S1 is specifically as follows:
[0112] Step S11: removing redundant information of code samples through code cleaning, the redundant information includes comments and logs, improving the validity of sample data and reducing noise interference;
[0113] Step S12: Delete duplicate code snippets to prevent the model from relying on duplicate samples during training, thereby ensuring the accuracy of vulnerability identification;
[0114] Step S13: Use a static analysis tool to parse the cleaned code sample and generate a code property graph to capture data flow and control flow information; if the code property graph cannot be generated or the generated code property graph structure is incomplete, delete the code sample.
[0115] Furthermore, in one example, step S2 is specifically as follows:
[0116] Step S21: convert the code sample into a character sequence format, and apply the BPE method to perform word segmentation processing to obtain the lexical information of the code sample; expressed as:
[0117] ;
[0118] in, Represents the lexical information of the code sample, Represents the lexical unit generated by BPE segmentation, Indicates the BPE word segmentation method, Code samples representing character sequence formats;
[0119] Step S22: Use the Tree-sitter tool to parse the code sample, generate an abstract syntax tree AST, and convert the abstract syntax tree AST into a linear sequence through pre-order traversal to obtain the syntax information of the code sample. ; expressed as:
[0120] AST = Tree-sitter (Code);
[0121] ;
[0122] Among them, Tree-sitter represents the parser generation tool and incremental parsing library, Code represents the code sample, represents pre-order traversal; , n is the current node, children(n) returns the child nodes of the current node;
[0123] Step S23: lexical information of the code sample of step S21 and grammatical information of the code sample of step S22 Use pre-trained models to generate lexical features and grammatical features respectively;
[0124] Specifically, the lexical and grammatical features of the code sample are captured through the multi-head attention mechanism to generate combined embeddings; these combined embeddings are concatenated to obtain the embedding vector of the code sample; finally, these combined embeddings are concatenated to obtain the complete code sequence feature vector ;
[0125] ;
[0126] ;
[0127] ;
[0128] ;
[0129] in, Represents the lexical features of the code sample, A code sample represents the grammatical features of a code sample. represents the pre-trained model, represents the multi-head attention mechanism, represents the combined embedding of code samples, that is, the feature information of code sequences, Indicates concatenation along a specific dimension.
[0130] Furthermore, in one example, step S3 is specifically:
[0131] Step S31: Obtain the code property graph CPG corresponding to the code sample in step S13, perform node embedding on the generated code property graph CPG, and obtain graph node features corresponding to the code sample;
[0132] Specifically: Use the word embedding method to embed each node and its type in the code sample Converted into a comprehensive vector representation of the node, that is, the graph node feature, is expressed as:
[0133] ;
[0134] Among them, each node By Type and the corresponding lines of code composition, is the graph node feature, Representation word embedding method;
[0135] Step S32: Apply an enhanced graph representation learning method to optimize node embedding expression by combining heterogeneous edge type information, and capture the relationship between distant nodes by expanding the convolution kernel size, and finally generate a feature representation of the graph structure of the code sample , expressed as:
[0136] ;
[0137] G = (V, E);
[0138] Among them, G represents the code property graph CPG, V represents the nodes in the graph, and E represents the edges. represents the parameters of the enhanced graph representation learning method, Indicates expanding the convolution kernel size.
[0139] Furthermore, in one example, in step S3, each code modal feature is separated into shared features and unique features, and a collaborative loss function is constructed, including an intra-sample loss function, a contrastive learning loss function, and a cross-entropy loss function, to optimize the learning process of shared features and unique features, and ensure the full separation and effective fusion of different modal features. Step S3 is specifically as follows:
[0140] Step S31: Designing a modal common encoder and modality-specific encoders ; Modal common encoder Used to map code modality features into a shared feature vector space, modality-specific encoder Used to map code modal features to a unique feature vector space, modal common encoder and modality-specific encoders They are all multi-layer perceptron layers MLP with activation functions, expressed as;
[0141] ;
[0142] ;
[0143] ;
[0144] ;
[0145] in, and All belong to , express dimensional real vector space; Code sequence feature information and graph structure feature vector Alignment feature representation of Represents the shared parameters under the code bimodality, Indicates the first proprietary parameter of a unique encoder. Indicates the second proprietary parameter of a unique encoder; and Respectively represent the code sequence feature information and graph structure feature vector The common feature vector of , that is, the shared feature; and Respectively represent the code sequence feature information and graph structure feature vector The unique feature vector of , that is, the unique feature;
[0146] Step S32: constructing a collaborative loss function to optimize the acquired shared features and unique features, the collaborative loss function includes an intra-sample loss function, a contrastive learning loss function, and a cross entropy loss function;
[0147] In-sample loss function It is expressed as:
[0148] ;
[0149] in, Represents minimizing the similarity loss and reducing the differences in latent feature representations between different domains. represents the orthogonality constraint loss, represents the mean square error loss;
[0150] Step S321: Minimizing the Similarity Loss The CMD loss function is used to measure the similarity of feature distributions of different modalities. The CMD loss function measures the difference between distributions by comparing the high-order central moments of different modalities. The formula is as follows:
[0151] ;
[0152] in, and They represent the i-th order central moment of the code sequence modality CT and the code graph structure modality CG in the shared feature space, respectively, and k is the order of the maximum central moment;
[0153] By minimizing the CMD loss function, the distribution differences of different modalities in the shared feature space can be effectively reduced, ensuring that the shared features of different modalities can be better aligned;
[0154] Step S322: Orthogonality Constraint Loss The differences in modality-specific features are reduced by minimizing the orthogonality loss function, which is formulated as follows:
[0155] ;
[0156] in, and Represents the modal common encoder and modality-specific encoders The output feature representation matrix, represents the Frobenius norm, which is used to calculate the square root of the sum of the squares of the elements of a matrix;
[0157] By minimizing the orthogonality loss function, and Orthogonality, thus ensuring that different aspects of the different modal data can be modeled independently;
[0158] Step S323: Design a decoder , input the shared features and unique features of the code mode m, output the initial features that can reconstruct the mode, and use the mean square error to measure the reconstruction error. The formula is as follows:
[0159] ;
[0160] ;
[0161] in, represents the feature representation of the code mode m after being extracted by the encoder and aligned; the decoder Combine the shared features of modality m with the unique features to form a new feature representation to effectively reconstruct the original features of modality m ;in, are the parameters of the decoder, is a regularization term used to prevent overfitting;
[0162] Step S33: Constructing contrastive learning loss function , used to solve the in-sample loss function The feature alignment problem between different samples is not considered, and the global representation between different samples is optimized, specifically:
[0163] Use the feature embedding of the last layer of the respective modality encoder model as the global representation of each code sequence and graph structure sample; maximize the cosine similarity of the code sequence embedding and graph structure embedding of B pairs of true pairs in the batch, while minimizing The cosine similarity of the error pair embeddings is expressed as:
[0164] ;
[0165] In the above formula, represents the expected value calculation of the modal feature embedding z, Represents the negative sample indicator function, which is used to select other samples that are not equal to the current sample index j. represents the features obtained from the code sequence modality CT with sample index j, represents the positive sample features obtained from the code graph structure modality CG with sample index j, Represents the negative sample features obtained from the code graph structure modality CG with sample index k; represents the features obtained from the code graph structure modality CG with sample index j, represents the positive sample features obtained from the code sequence modality CT with sample index j, Represents the negative sample features obtained from the code sequence modality CT with sample index k;
[0166] is the standard contrast loss function, and the formula is:
[0167] ;
[0168] in, are learnable parameters.
[0169] Furthermore, in one example, step S5 adaptively fuses the modal difference features based on the modal shared features, including:
[0170] Step S51: Apply the cross-attention mechanism to extract code sequence feature information and graph structure feature vector The common information of the modalities is combined with the modal shared features to obtain enhanced shared features. ;
[0171] Specifically:
[0172] In the cross-attention mechanism, the inputs of the query matrix Q, key matrix K, and value matrix V come from two different modalities, where the sequence X is used as the input of the query matrix Q, and the sequence Y is used as the input of the key matrix K and the value matrix V. The formula is as follows:
[0173] ;
[0174] in, , and are the projection matrices of queries, keys, and values, is the scaling factor of the dimension, used to stabilize the gradient during training, represents the cross attention mechanism;
[0175] Next, and Shared features and The two are input together into the multi-head attention mechanism, which further extracts and fuses features through the following formula:
[0176] ;
[0177] ;
[0178] in, Represents a long mechanism, represents the attention mechanism, represents the multi-head attention adjustment parameter, represents the i-th head output, , and They represent the adjustment parameters of the query matrix, key matrix, and value matrix corresponding to the i-th head in the multi-head attention mechanism respectively;
[0179] Finally, by splicing the outputs of each head, the enhanced modality shared features are obtained ;
[0180] Step S52: Calculate the enhanced modality shared features based on the cross-attention mechanism With unique features and These correlation features are input into the gated neural unit to obtain the first fusion weight that can be learned. and the second fusion weight , expressed as:
[0181] ;
[0182] ;
[0183] in, Represents a gated neural unit, implemented by a sigmoid activation function, which regulates the flow of information by assigning importance weights to input features;
[0184] Step S53: Sharing features based on enhanced modalities , unique features and , adaptively obtain the final multimodal fusion features , that is, the comprehensive code representation of the code sample, is expressed as:
[0185] ;
[0186] in, represents the first fusion weight, Represents the second fusion weight.
[0187] Furthermore, in one example, step S6 specifically includes:
[0188] Inputting the synthetic code representation of the code sample into the classifier to obtain the prediction result of vulnerability detection;
[0189] ;
[0190] In the above formula, is the weight matrix, is the bias term, is a comprehensive code representation of the code sample, is the Sigmoid activation function, Represents the prediction score, which is used to express probability;
[0191] Among them, the classifier processes the multimodal fusion features through linear transformation and activation function, and outputs the prediction score of whether the code sample has vulnerabilities or not. The output result is predicted according to the size of the prediction score, and the output result includes a vulnerable sample or a non-vulnerable sample. A prediction score close to 0 indicates that the probability of vulnerability of the sample is low, that is, the sample may be non-vulnerable; a prediction score close to 1 indicates that the probability of vulnerability of the sample is high, that is, the sample may be vulnerable. Through this prediction score, vulnerable samples and non-vulnerable samples can be effectively distinguished.
[0192] It should be noted that a person skilled in the art can understand that all or part of the steps of implementing the above embodiments can be completed by hardware, or can be completed by instructing related hardware through a program, and the program can be stored in a computer-readable storage medium, and the above-mentioned storage medium can be a read-only memory, a disk or an optical disk, etc.
[0193] The above specifically describes the preferred implementation method of the present invention, but the invention is not limited to the described embodiments. Those skilled in the art may make various equivalent modifications or substitutions without violating the spirit of the present invention. These equivalent modifications or substitutions are all included in the scope defined by the claims of this application.
Claims
1. A software vulnerability detection method based on multimodal feature adaptive fusion, characterized in that: The following steps are involved: Step S1: preprocessing the software code sample; Step S2: construct a software vulnerability detection model, which includes a code sequence information extraction module, a graph structure information extraction module, a modal optimization module, a code generation module and a classifier; The code sequence information extraction module extracts code sequence feature information from the preprocessed code sample, and in this process enhances the code sequence feature information by integrating the front-end information of the compiler, wherein the front-end information of the compiler includes the lexical information and syntactic information of the code sample; Step S3: The graph structure information extraction module obtains the graph structure feature representation of the code sample; The code sequence feature information of step S2 and the graph structure feature of step S3 represent multimodal information constituting the code sample; Step S4: The modal optimization module constructs an internal loss function, imports multimodal information into the modal optimization module, obtains the shared features and unique features of the code samples in the two modes, and uses a contrastive learning loss function in this process to enhance the discrimination between code samples; Step S5: the code generation module adaptively fuses the unique features of the two modes based on the shared features in step S4 to generate a comprehensive code representation of the code sample; Step S6: input the comprehensive code representation of the code sample in step S5 into the classifier, and determine whether the code sample has a vulnerability based on the prediction score; Step S2 is specifically as follows: Step S21: convert the code sample into a character sequence format, and apply the BPE method to perform word segmentation processing to obtain the lexical information of the code sample; expressed as: ; in, Represents the lexical information of the code sample, Represents the lexical unit generated by BPE segmentation, Indicates the BPE word segmentation method, Code samples representing character sequence formats; Step S22: Use the Tree-sitter tool to parse the code sample, generate an abstract syntax tree AST, and convert the abstract syntax tree AST into a linear sequence through pre-order traversal to obtain the syntax information of the code sample. ; expressed as: AST = Tree-sitter (Code); ; Among them, Tree-sitter represents the parser generation tool and incremental parsing library, Code represents the code sample, represents pre-order traversal; , n is the current node, children(n) returns the child nodes of the current node; Step S23: lexical information of the code sample of step S21 and grammatical information of the code sample of step S22 Use pre-trained models to generate lexical features and grammatical features respectively; Specifically, the lexical and grammatical features of the code sample are captured through the multi-head attention mechanism to generate combined embeddings; these combined embeddings are concatenated to obtain the embedding vector of the code sample; finally, these combined embeddings are concatenated to obtain the complete code sequence feature vector ; ; ; ; ; in, represents the lexical features of the code sample, A code sample represents the grammatical features of a code sample. represents the pre-trained model, represents the multi-head attention mechanism, represents the combined embedding of code samples, that is, the feature information of code sequences, Indicates splicing along a specific dimension; Step S3 is specifically as follows: Step S31: Obtain the code property graph CPG corresponding to the code sample in step S13, perform node embedding on the generated code property graph CPG, and obtain graph node features corresponding to the code sample; Specifically: Use the word embedding method to embed each node and its type in the code sample Converted into a comprehensive vector representation of the node, that is, the graph node feature, is expressed as: ; Among them, each node By Type and the corresponding lines of code Composition, V is the graph node feature, Representation word embedding method; Step S32: Apply the enhanced graph representation learning method to optimize the node embedding expression by combining heterogeneous edge type information, and capture the relationship between distant nodes by expanding the convolution kernel size, and finally generate the feature representation of the graph structure of the code sample , expressed as: ; G = (V, E); Among them, G represents the code property graph CPG, V represents the nodes in the graph, and E represents the edges. represents the parameters of the enhanced graph representation learning method, Indicates expanding the convolution kernel size; In step S3, each code modal feature is separated into shared features and unique features, and a collaborative loss function is constructed, including an intra-sample loss function, a contrastive learning loss function, and a cross-entropy loss function, to optimize the learning process of shared features and unique features and ensure the full separation and effective fusion of different modal features. Step S3 is specifically as follows: Step S31: Designing a modal common encoder and modality-specific encoders ; Modal common encoder Used to map code modality features into a shared feature vector space, modality-specific encoder Used to map code modal features to a unique feature vector space, modal common encoder and modality-specific encoders They are all multi-layer perceptron layers MLP with activation functions, expressed as; ; ; ; ; in, and All belong to , express dimensional real vector space; Code sequence feature information and graph structure feature vector Alignment feature representation of Represents the shared parameters under the code bimodality, Indicates the first proprietary parameter of a unique encoder. Indicates the second proprietary parameter of a unique encoder; and Respectively represent the code sequence feature information and graph structure feature vector The common feature vector of , that is, the shared feature; and Respectively represent the code sequence feature information and graph structure feature vector The unique feature vector of , that is, the unique feature; Step S32: constructing a collaborative loss function to optimize the acquired shared features and unique features, the collaborative loss function includes an intra-sample loss function, a contrastive learning loss function, and a cross entropy loss function; In-sample loss function It is expressed as: ; in, Represents minimizing the similarity loss and reducing the differences in latent feature representations between different domains. Orthogonality constraint loss, represents the mean square error loss; Step S321: Minimizing the Similarity Loss The CMD loss function is used to measure the similarity of feature distributions of different modalities. The CMD loss function measures the difference between distributions by comparing the high-order central moments of different modalities. The formula is as follows: ; in, and They represent the i-th order central moment of the code sequence modality CT and the code graph structure modality CG in the shared feature space, respectively, and k is the order of the maximum central moment; By minimizing the CMD loss function, the distribution differences of different modalities in the shared feature space can be effectively reduced, ensuring that the shared features of different modalities can be better aligned; Step S322: Orthogonality constraint loss constructs a function that minimizes the orthogonality loss to reduce the differences in modality-specific features. The formula is as follows: ; in, and Represents the modal common encoder and modality-specific encoders The output feature representation matrix, represents the Frobenius norm, which is used to calculate the square root of the sum of the squares of the elements of a matrix; By minimizing the orthogonality loss function, and Orthogonality, thus ensuring that different aspects of the different modal data can be modeled independently; Step S323: Design a decoder , input the shared features and unique features of the code mode m, output the initial features that can reconstruct the mode, and use the mean square error to measure the reconstruction error. The formula is as follows: ; ; in, represents the feature representation of the code mode m after being extracted by the encoder and aligned; the decoder Combine the shared features of modality m with the unique features to form a new feature representation to effectively reconstruct the original features of modality m ;in, are the parameters of the decoder, is a regularization term used to prevent overfitting; Step S33: Constructing contrastive learning loss function , used to solve the in-sample loss function The feature alignment problem between different samples is not considered, and the global representation between different samples is optimized, specifically: Use the feature embedding of the last layer of the respective modality encoder model as the global representation of each code sequence and graph structure sample; maximize the cosine similarity of the code sequence embedding and graph structure embedding of B pairs of true pairs in the batch, while minimizing The cosine similarity of the error pair embeddings is expressed as: ; In the above formula, represents the expected value calculation of the modal feature embedding z, Represents the negative sample indicator function, which is used to select other samples that are not equal to the current sample index j. represents the features obtained from the code sequence modality CT with sample index j, represents the positive sample features obtained from the code graph structure modality CG with sample index j, Represents the negative sample features obtained from the code graph structure modality CG with sample index k; represents the features obtained from the code graph structure modality CG with sample index j, represents the positive sample features obtained from the code sequence modality CT with sample index j, represents the negative sample features obtained from the code sequence modality CT with sample index k; is the standard contrast loss function, and the formula is: ; in, are learnable parameters; The step S5 adaptively fuses the modal difference features based on the modal shared features, including: Step S51: Apply the cross-attention mechanism to extract code sequence feature information and graph structure feature vector The common information of the modalities is combined with the modal shared features to obtain enhanced shared features. ; Specifically: In the cross-attention mechanism, the inputs of the query matrix Q, key matrix K, and value matrix V come from two different modalities, where the sequence X is used as the input of the query matrix Q, and the sequence Y is used as the input of the key matrix K and the value matrix V. The formula is as follows: ; in, , and are the projection matrices of queries, keys, and values, is the scaling factor of the dimension, used to stabilize the gradient during training, represents the cross attention mechanism; Next, and Shared features and The two are input together into the multi-head attention mechanism, which further extracts and fuses features through the following formula: ; ; in, Represents a long mechanism, represents the attention mechanism, represents the multi-head attention adjustment parameter, represents the i-th head output, , and They represent the adjustment parameters of the query matrix, key matrix, and value matrix corresponding to the i-th head in the multi-head attention mechanism respectively; Finally, by splicing the outputs of each head, the enhanced modality shared features are obtained ; Step S52: Calculate the enhanced modality shared features based on the cross-attention mechanism With unique features and These correlation features are input into the gated neural unit to obtain the first fusion weight that can be learned. and the second fusion weight , expressed as: ; ; in, Represents a gated neural unit, implemented by a sigmoid activation function, which regulates the flow of information by assigning importance weights to input features; Step S53: Sharing features based on enhanced modalities , unique features and , adaptively obtain the final multimodal fusion features , that is, the comprehensive code representation of the code sample, is expressed as: ; in, represents the first fusion weight, Represents the second fusion weight.
2. According to claim 1, a software vulnerability detection method based on multimodal feature adaptive fusion is characterized in that: Step S1 is specifically as follows: Step S11: removing redundant information of code samples through code cleaning, the redundant information includes comments and logs, improving the validity of sample data and reducing noise interference; Step S12: Delete duplicate code snippets to prevent the model from relying on duplicate samples during training, thereby ensuring the accuracy of vulnerability identification; Step S13: using a static analysis tool to parse the cleaned code sample and generate a code property graph to capture data flow and control flow information; If a code property graph cannot be generated or the structure of the generated code property graph is incomplete, the code sample is deleted.
3. According to claim 1, a software vulnerability detection method based on multimodal feature adaptive fusion is characterized in that: Step S6 specifically includes: The comprehensive code representation of the code sample is input into the classifier to obtain the prediction result of vulnerability detection; it is expressed as: ; In the above formula, is the weight matrix, is the bias term, is a comprehensive code representation of the code sample, is the Sigmoid activation function, Represents the prediction score, which is used to express probability; Among them, the classifier processes the multimodal fusion features through linear transformation and activation function, and outputs the prediction score of whether the code sample has vulnerabilities or not, and predicts the output result according to the size of the prediction score. The output result includes vulnerability samples or non-vulnerability samples.
Citation Information
Patent Citations
Multi-mode-based function level vulnerability detection method
CN116611071A