Cross-project software defect prediction method and system based on multi-modal path semantics
By employing multimodal path semantic modeling and multi-task comparative learning, the coarse-grained and cross-project transfer problems in software defect prediction are solved. This enables accurate identification of defect patterns and provision of repair guidance in new projects, and is applicable to multiple programming languages.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-26
- Publication Date
- 2026-04-03
AI Technical Summary
Existing technologies for software defect prediction suffer from problems such as coarse-grained prediction, insufficient semantic modeling, and weak cross-project domain transferability. They are difficult to identify specific defect patterns and their causes, and their performance degrades when there is a lack of labeled data in new projects.
A cross-project software defect prediction method based on multimodal path semantics is adopted. By establishing a mapping relationship between the node type of the abstract syntax tree and the defect pattern, and combining the vector representation of the multimodal path for context interaction modeling, a multimodal domain correlation matrix is constructed for multi-task comparative learning to achieve cross-project software defect pattern prediction.
It improves the consistency and discriminativeness of defect pattern characteristics across projects, can accurately identify defect patterns in new projects and provide refined remediation guidance, reduces false positive and false negative rates, and is applicable to multiple programming languages.
Smart Images

Figure CN121785930A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the technical field of software defect prediction, specifically to a cross-project software defect prediction method and system based on multimodal path semantics. Background Technology
[0002] Early defect prediction methods often used static metrics at the file or module level (such as lines of code, cyclomatic complexity, number of functions, number of modifications, etc.) combined with traditional machine learning models (such as logistic regression, random forest, SVM, etc.) to determine whether a file / module is prone to defects. These methods are simple to implement and low in cost, but they can only provide coarse-grained conclusions on "whether it is prone to defects," and it is difficult to characterize specific defect patterns and causes, nor can it be refined to the path level or statement level for analysis.
[0003] With the development of deep learning and pre-trained models, some studies have begun to directly treat source code as a sequence of tokens, or to construct structured representations based on Abstract Syntax Trees (ASTs), Control Flow Graphs (CFGs), and Data Flow Graphs (DFGs). These representations are then used to automatically learn semantic features of the code using models such as Convolutional Neural Networks, Recurrent Neural Networks, Graph Neural Networks, and Transformers for defect prediction, vulnerability detection, or code quality assessment. Such methods are superior in expressive power to simple feature measurement and can capture control flow and data flow information to a certain extent. However, many works still primarily focus on function-level or file-level overall representations, rarely using "program paths" as the basic unit of analysis. They lack fine-grained characterization of branch conditions, data dependencies, and contextual semantics along the path, making it difficult to accurately distinguish between genuine defects and semantically similar but defect-free "false positive" code.
[0004] In practical engineering, new projects often lack sufficient, complete, and high-quality defect annotation data, while a large amount of existing defect data is distributed across historical or open-source projects. To improve the usability of models on new projects, researchers have proposed cross-project defect prediction and domain adaptation techniques: one type of method reduces the distribution differences between different projects by manually designing general metric features that are independent of the project; another type of method uses transfer learning, adversarial training, or contrastive learning to align the feature spaces of the source and target projects, thereby maintaining a certain predictive performance even when the target project has scarce or even missing annotation data.
[0005] However, existing technologies still have the following prominent problems: First, most methods only perform binary classification or defect quantity prediction at the file level or function level, and cannot identify specific defect patterns and their causes, making it difficult to provide developers with refined repair guidance; Second, many methods are based only on code representation from a single perspective (such as static metrics, simple source code token sequences, etc.), failing to comprehensively utilize structural information such as Abstract Syntax Trees (AST), Control Flow Graphs (CFG), and Data Flow Graphs (DFG), especially lacking the ability to perform path-sensitive modeling with "program paths" as the basic unit, making it difficult to effectively distinguish between real and pseudo-defects caused by subtle differences in control flow / data flow in similar code snippets, resulting in high false positive and false negative rates; Third, in actual engineering, new projects generally lack high-quality defect annotation data. Traditional supervised learning models rely on a large number of labeled samples. When transferred from the source project to the target project after training, due to significant semantic and structural domain shifts in coding style, domain knowledge, and development environment, existing cross-project defect prediction methods often experience a significant performance drop, making it difficult to work stably in zero-sample or few-sample scenarios. Summary of the Invention
[0006] The purpose of this invention is to overcome the problems of insufficient coarse-grained prediction, semantic modeling, and weak cross-project domain transfer capability mentioned above. The purpose of this invention is to provide a cross-project software defect prediction method and system based on multimodal path semantics.
[0007] This invention is achieved through the following technical solution: a cross-project software defect prediction method based on multimodal path semantics, comprising the following steps: The code of the multi-source domain and the target domain are parsed separately to obtain the abstract syntax tree, and the mapping relationship between the node types of the abstract syntax tree and the defect patterns is established. The mapping relationship between the multi-source domain and the target domain is input into the code pre-trained model to obtain the vector representation of the multimodal path; Vector representation based on multimodal paths performs context interaction modeling and global encoding on the sentence sequence on the path, and obtains complete path semantic embeddings in the multi-source domain and the target domain respectively. A multimodal domain correlation matrix is constructed, and multi-task comparative learning is performed between multiple source domains based on path semantic embedding. This enables the aggregation of representations of the same defect pattern in different projects and the separation of different defect patterns, thereby achieving cross-project software defect pattern prediction.
[0008] Preferably, the abstract syntax tree nodes include one or more of the following: control flow nodes, assignment nodes, function / call nodes, declaration nodes, synchronization nodes, jump nodes, or exception nodes.
[0009] Preferably, establishing the mapping relationship between abstract syntax tree node types and defect patterns specifically involves: By combining the general weakness enumeration, the correspondence between the node types of the abstract syntax tree and the defect patterns is organized and classified to obtain the mapping relationship; The variables and constants in the mapping relationship are normalized and quantized to convert each statement into a unified token sequence, thereby eliminating surface text differences across projects and languages and highlighting numerical and status information related to defect patterns.
[0010] Preferably, the vector representation based on multimodal paths models the contextual interactions and globally encodes the sentence sequences on the path, obtaining complete path semantic embeddings for both the multi-source and target domains, including: Based on the control flow graph, one or more representative paths from the virtual entry node to the virtual exit node are extracted, and each node on the path corresponds to a statement. Perform bidirectional message passing on the path to obtain a sequence of node representations with enhanced context, where each node contains a forward representation of the historical execution context and a backward representation of the future execution impact; The node representation sequence is fed into the multi-layer encoder as a semantic path sequence for global modeling. Based on modeling, a direct dependency relationship is established between any two nodes within the path to complete the coding; By pooling the hidden states of nodes output by the encoder or selecting dedicated aggregation positions, a single path semantic embedding vector is obtained.
[0011] Preferably, the normalization and quantization processing of the variables and constants in the mapping relationship includes: For numeric variables in basic data types, a unified numerical range is set for quantization matching for different types of numeric variables; For array-type variables, a binary label is used based on whether the array has been initialized, uniformly mapping it to initialized or uninitialized, to reflect key signals related to the initialization state in uninitialized read defect patterns and out-of-bounds access defect patterns; a sequential renaming strategy is used to anonymize variable identifiers; and / or, The unified token sequence consists of: abstract operation category, variable placeholder, quantized constant category, and initialization status label.
[0012] Preferably, the step of inputting the mapping relationship of the multi-source domain into the code pre-trained model to obtain the vector representation of the multimodal path includes the following process: The unified token sequence is input to obtain the vector representation of each token in the sequence. The sequence is then encoded, and the contextual dependencies between tokens are modeled comprehensively from both the front and back directions. Finally, the hidden states at each position are pooled or weighted to obtain the semantic vector of a single statement.
[0013] Preferably, the construction of the multimodal domain relevance matrix, based on path semantic embedding, performs multi-task comparative learning across multiple source domains, enabling the aggregation of representations of the same defect pattern in different projects and the separation of different defect patterns, specifically: Based on the contrastive learning results, the defect pattern category with the highest probability is determined according to the distance between the embedding vector of a certain path in the target domain and the prototype vector of each defect pattern, as well as the confidence level output by the contrastive learning results. The prototype vector corresponding to the defect pattern category with the highest probability is then labeled. At the same time, positive samples with similar semantics are selected from samples of the same category, and negative samples with different semantics are selected from other categories. The comparative loss against each defect mode is calculated separately, and then weighted or summed to form an overall comparative learning objective.
[0014] Preferably, after forming the overall contrastive learning objective, a domain discrimination mechanism is introduced, using path semantic embedding as input, to determine whether the sample comes from the source item or the target item.
[0015] A cross-project software defect prediction system based on multimodal path semantics, comprising: The mapping relationship building module is configured to parse the code of the multi-source domain and the target domain respectively, obtain the abstract syntax tree, and establish the mapping relationship between the abstract syntax tree node types and defect patterns. The multimodal path semantic extraction module is configured to input the mapping relationship between the multi-source domain and the target domain into the code pre-trained model to obtain the vector representation of the multimodal path; The path semantic embedding module is configured to perform context interaction modeling and global encoding on the sentence sequence on the path based on the vector representation of the multimodal path, so as to obtain the complete path semantic embedding of the multi-source domain and the target domain respectively. The cross-project software defect prediction module is configured to construct a multimodal domain correlation matrix and perform multi-task comparative learning across multiple source domains based on path semantic embedding. This enables the aggregation of representations of the same defect pattern in different projects and the separation of different defect patterns, thereby achieving cross-project software defect prediction.
[0016] A machine-readable storage medium having instructions stored thereon for causing a machine to execute a cross-project software defect prediction method based on multimodal path semantics.
[0017] The above technical solution achieves the following technical effects: By combining path semantic embedding with multi-task comparative learning across multiple source projects, the representations of the same defect pattern are aggregated across different projects, and different defect patterns are separated, improving the consistency and discriminativeness of cross-project defect pattern features. Furthermore, it automatically aggregates the representations of the same defect pattern across different projects and naturally separates different defect patterns. In addition, it adopts a unified framework of "AST abstract categories + quantified numerical values + path semantics + comparative learning." The input relies only on general syntax parsing and control flow analysis, making it applicable to various mainstream programming languages. The output uses path-level defect pattern prediction results, which can be directly used in static analysis tools or code review platforms to provide targeted defect pattern warnings for high-risk paths, facilitating rapid location and repair by developers. It demonstrates significant comprehensive advantages over existing best-in-class technologies in terms of fine-grained defect pattern characterization, cross-project transfer robustness, and practical implementation effectiveness. Attached Figure Description
[0018] Figure 1 This is a schematic diagram of the cross-project software defect prediction process based on multimodal path semantics of the present invention. Figure 2 This is a schematic diagram illustrating bidirectional message passing on the control flow graph path and path semantic embedding based on Transformer, according to an embodiment of the present invention. Figure 3 This is a schematic diagram illustrating defect pattern prediction in a cross-project scenario according to an embodiment of the present invention. Detailed Implementation
[0019] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0020] This invention provides a cross-project software defect prediction method based on multimodal path semantics, such as... Figure 1 As shown, it includes the following steps: S1. Parse the code of the multi-source domain and the target domain respectively to obtain the abstract syntax tree, and establish the mapping relationship between the node types of the abstract syntax tree and the defect patterns.
[0021] Further, in step S1, the mapping relationship between the abstract syntax tree node types and defect patterns is established, specifically as follows: By combining the general weakness enumeration, the correspondence between the node types of the abstract syntax tree and the defect patterns is organized and classified to obtain the mapping relationship; The variables and constants in the mapping relationship are normalized and quantized to convert each statement into a unified token sequence, thereby eliminating surface differences across projects and languages and highlighting numerical and state information related to defect patterns.
[0022] Abstract Syntax Tree nodes include one or more of the following: control flow nodes, assignment nodes, function / call nodes, declaration nodes, synchronization nodes, jump nodes, or exception nodes.
[0023] Step S1 is used for program path semantic embedding extraction. First, multimodal semantic modeling is performed at the statement granularity, constructing a statement semantic extraction module to convert each statement in the source code into a unified vector representation sensitive to defect patterns. Specifically, this invention parses the source code to obtain structured representations such as Abstract Syntax Trees (ASTs), and divides the analysis objects into units based on statements. On this basis, combined with a Common Weakness Enumeration (CWE) knowledge base, the correspondence between AST node types and typical defect patterns is pre-organized and categorized. For example, nodes such as IfStatement, ForStatement, CatchClause, ThrowStatement, and SynchronizedStatement are categorized into "Control-flownodes," corresponding to control-flow related defects such as CWE-483, CWE-484, and CWE-835. Through the above mapping, this invention no longer directly uses language-related fine-grained syntax nodes, but instead groups the AST nodes appearing in each statement into a limited number of "abstract statement / expression categories." These categories correspond one-to-one or one-to-many to CWE defect patterns, thereby explicitly introducing defect prior knowledge at the statement representation level and achieving enhanced expression of potential defect pattern structural signals.
[0024] After completing the structural category abstraction, this embodiment of the invention further normalizes and quantifies the variables and constants in the statements to eliminate surface differences across projects and languages and highlight numerical and state information related to defect patterns. For example, for numerical variables in basic data types, according to the quantization scheme shown in Table 1, the specific values of Integer, Float / Double, Short, etc., are divided into finite categories based on sign and order of magnitude: PositiveSmall (small absolute value positive number), PositiveShort (medium range positive number), PositiveLong (large absolute value positive number), Zero (zero value), NegativeSmall (small absolute value negative number), NegativeShort (medium range negative number), and NegativeLong (large absolute value negative number); for the Char type, letter characters and non-letter characters are distinguished; for the Boolean type, it is directly mapped to False / True; and for the Void type, it is mapped to Void. For array-type variables, this embodiment of the invention does not concern itself with the specific value of each element in the array. Instead, it performs binary marking based on whether the array has been initialized, uniformly mapping Integer, Float / Double, Character, and other arrays to Initialized / NotInitialized (or Initialization / Uninitialized) to reflect key signals related to the initialization state in typical defect patterns such as "uninitialized read" and "out-of-bounds access." Simultaneously, this invention employs a sequential renaming strategy to anonymize variable identifiers, naming variables appearing for the first time within the same scope v1, v2, v3, etc., eliminating naming style differences between different items while maintaining the relative relationships between variables. After "AST node category abstraction + CWE pattern alignment + numerical quantization + variable anonymization," each statement is converted into a unified token sequence composed of "abstract operation category + variable placeholder + quantized constant category + initialization state label," etc.
[0025] Table 1. Quantization schemes for variable values in basic and array types.
[0026] S2. Input the mapping relationship between the multi-source domain and the target domain into the code pre-trained model to obtain the vector representation of the multimodal path; In this embodiment of the invention, the token sequence is input into the embedding layer of a code pre-trained model (such as CodeBERT) to obtain the vector representation of each token in the sequence. The sequence is then encoded using a bidirectional recurrent neural network or an equivalent structure to comprehensively model the contextual dependencies between tokens from both the preceding and following directions. Finally, pooling or weighted aggregation is performed on the hidden states at each position to obtain the semantic vector representation of a single sentence. The semantic space of the sentences constructed in this way exhibits good consistency and comparability across different projects and languages. At the same time, it embeds structural and numerical prior information related to CWE defect patterns, providing fine-grained, transferable, and highly sensitive basic feature representations for subsequent multimodal semantic modeling based on program paths and cross-project defect pattern prediction.
[0027] The technical solution of this invention is based on the statement abstraction system of AST–CWE mapping. It classifies various AST nodes of specific programming languages into a limited semantic category such as control flow nodes, assignment nodes, function / call nodes, declaration nodes, synchronization nodes, jump nodes, and exception nodes, and establishes a correspondence with CWE defect patterns, explicitly introducing defect pattern priors at the statement representation level.
[0028] Furthermore, a variable value quantization and anonymization mechanism for different data types is adopted. A unified numerical range quantization and initialization state marking scheme (PositiveSmall / Short / Long, Zero, NegativeSmall / Short / Long, Initialized / NotInitialized, etc.) is designed for basic types and array types. A sequential renaming strategy is adopted for variable names to form a cross-project comparable "abstract variable + quantized constant" representation.
[0029] S3. Vector representation based on multimodal paths performs context interaction modeling and global encoding on the sentence sequence on the path, and obtains complete path semantic embeddings for the multi-source domain and the target domain respectively.
[0030] Further, in step S3, the contextual interaction modeling and global encoding of the statement sequence on the path are performed based on the vector representation of the multimodal path, obtaining complete path semantic embeddings for both the multi-source and target domains, including: Based on the control flow graph, one or more representative paths from the virtual entry node to the virtual exit node are extracted, and each node on the path corresponds to a statement. Perform bidirectional message passing on the path to obtain a sequence of node representations with enhanced context, where each node contains a forward representation of the historical execution context and a backward representation of the future execution impact; The node representation sequence is fed into the multi-layer encoder as a semantic path sequence for global modeling. Based on modeling, a direct dependency relationship is established between any two nodes within the path to complete the coding; By pooling the hidden states of nodes output by the encoder or selecting dedicated aggregation positions, a single path semantic embedding vector is obtained.
[0031] For example, after completing the construction of statement-level semantic vectors, this embodiment of the invention uses the control flow graph as a basis, takes the program execution path as a higher-level modeling unit, and performs contextual interaction modeling and global encoding on the statement sequences on the path, thereby obtaining a complete path semantic embedding. Figure 2 As shown in the diagram, the "BEGIN–EXIT" path is illustrated. This invention first extracts one or more representative paths from the virtual entry node BEGIN to the virtual exit node EXIT based on the control flow graph. Each node n1, n2, ..., n on the path... T For each statement (e.g., variable initialization statement "x=1", loop counter initialization statement "i=1", loop condition "i<4", loop body update statements "x=x+1" "i=i+1", etc.), x represents the variable initialization statement and i represents the loop count. For each node, the statement vector output by the aforementioned statement semantic extraction module is used as the initial feature representation of that node.
[0032] Building upon this, bidirectional message passing is performed along the path to explicitly model the control and data dependencies that propagate between statements in the execution order: on the one hand, forward propagation occurs from BEGIN to EXIT in the order of the path, and each node integrates the characteristics of its predecessor node when updating its own representation to obtain a forward representation containing the "historical execution context"; on the other hand, backward propagation occurs from EXIT back to BEGIN, and each node incorporates the information of its successor node when updating to obtain a backward representation containing the "future execution impact". Figure 2 In the diagram, nodes of different colors represent the context enhancement representations h1, h2, ..., h1 obtained after forward and backward propagation. T Through this bidirectional message passing, this invention not only considers the operational semantics of a single statement but also captures the correlation between conditional judgments and loop updates, and between variable definitions and usage throughout the entire path. It is particularly sensitive to structures highly correlated with defect patterns such as "loop condition + counter update," which are prone to out-of-bounds errors and infinite loops. The update formulas for forward and backward message passing are as follows:
[0033] in, υ This represents a node in a control flow graph (CFG) or path graph. u Represents nodes υ Adjacent nodes that have control flow connections; hυ (i) This represents the node during the i-th iteration / i-th layer propagation. υ The hidden state; h u (i) This represents the node during the i-th iteration / i-th layer propagation. u The hidden state; h υ (i+1),forward This represents the forward propagation hidden state obtained after the (i+1)th update; h υ (i+1),backward Represents the backpropagation hidden state obtained after the (i+1)th update; LSTM() represents a Long Short-Term Memory network unit; pred( υ ) represents a node υ The set of predecessor nodes; succ ( υ ) represents a node υ The set of successor nodes; W forward and W backward These represent the trainable weight matrices used in the forward and backward propagation, respectively.
[0034] Obtain the context-enhanced node representation sequence [h1,h2,...,h T Afterwards, it is treated as a "semantic path sequence" and fed into a multi-layer Transformer encoder for global modeling. The Transformer, through a multi-head self-attention mechanism, establishes direct dependencies between any two nodes within the path. This not only focuses on local connections between adjacent statements but also captures long-range dependencies such as "variable declarations / initializations at the beginning of the path" and "resource releases or return statements at the end of the path." After encoding, the hidden states of the nodes output by the Transformer are pooled or have dedicated aggregation positions selected to obtain a single path semantic embedding vector, serving as the high-level semantic representation of the execution path in the multimodal space. This path semantic embedding retains fine-grained operational features at the statement level while incorporating control flow and data flow information propagating along the execution path, providing core features capable of accurately characterizing complex program behavior for subsequent multimodal comparative learning and cross-project defect pattern classification.
[0035] S4. Construct a multimodal domain correlation matrix, perform multi-task comparative learning between multiple source domains based on path semantic embedding, enable the aggregation of representations of the same defect pattern in different projects and the separation of different defect patterns, and realize cross-project software defect prediction.
[0036] After obtaining the path semantic embeddings of programs in the source and target domains, a contrastive learning and domain discrimination mechanism is introduced into a unified semantic space to achieve defect pattern prediction in cross-project scenarios. For example... Figure 3 As shown, "Path_1semanticembedding, Path_2semanticembedding...Path_nsemanticembedding" at the bottom represent the semantic vectors of each program path extracted from the target project. First, this embodiment of the invention utilizes a classifier and class prototypes obtained from comparative learning in the source domain to perform soft classification and clustering of the target domain paths, generating "pseudo labels" for each path: For a path embedding vector, this invention determines the defect pattern category to which it belongs with the highest probability based on its distance to the prototype vectors of each defect pattern and the confidence level output by the classifier, and denotes the prototype vector corresponding to this category as (c_i). Simultaneously, positive samples with similar semantics (c_i^+) are selected from samples of the same category, and negative samples with different semantics (c_i^-) are selected from other categories. In this way, for each defect pattern (i), a "prototype-positive sample-negative sample" triplet or multi-tuple can be constructed to provide training pairs for subsequent comparative learning.
[0037] Based on this, a contrastive learning submodule (labeled "CL" in the figure) is constructed for each defective pattern, and the contrastive loss (Loss1, Loss2, ..., Loss) associated with that pattern is calculated respectively. nIn each CL module, contrastive loss functions such as InfoNCE are used to bring target domain path embeddings and prototype vectors (c_i, c_i^+) with the same pseudo-label closer together, while pushing them away from prototypes or negative samples (c_i^-) of other classes. This creates a "compact within classes, separated between classes" structure in the feature space, giving different defect patterns clear boundaries in high-dimensional space. The contrastive losses corresponding to all defect patterns are then weighted or summed in the upper layer to form the overall contrastive learning objective. To further reduce the distribution difference between the source and target domains, this embodiment of the invention superimposes a "Domain Discriminator Layer" on top of the contrastive learning module. This layer accepts path semantic embeddings as input and determines whether the sample comes from the source item or the target item. The feature extraction network adversarially "confuses" the domain discriminator through gradient inversion and other methods, so that the finally learned path semantic representation can maintain the discriminability between different defect patterns and has good domain invariance between the source and target domains. After training, for any new target domain program path, this invention inputs its semantic embedding into the trained classifier to output the corresponding defect pattern prediction result, achieving automatic identification of multiple defect patterns in target projects lacking true labels. The final loss function consists of three parts: Among them, L total This represents the total loss function for the overall optimization of this invention, used to jointly constrain the discriminative and cross-domain consistency of the semantic representation of the path; L infoNCE L represents a loss function based on contrastive learning, used to bring semantically similar or identical path sample representations closer together and push semantically different path sample representations further apart, thereby enhancing the separability of different defect patterns in the feature space; CE C represents the Categorical Cross Entropy loss function, used to measure the difference between the defect pattern category predicted by the model and the true or false label; C is the defect pattern classifier; E f This represents a feature extraction network or path semantic embedding extraction network. Its input is the program path, and its output is the corresponding path semantic embedding vector. λ1 and λ2 are preset weight coefficients used to adjust the relative importance of the domain adversarial loss term and the classification loss term in the total loss function.
[0038] Compared to existing solutions such as "metrics + machine learning," "deep learning of code text," "AST / graph structure representation," and "multimodal representation + cross-project transfer," the technical solution of this invention first de-emphasizes the modeling granularity from the file / function level to the "statement-path level." Through AST-CWE mapping, variable anonymization, and numerical quantization, it abstracts control flow nodes, assignment nodes, and exception nodes directly related to common defect patterns, and performs bidirectional message passing and Transformer encoding on the control flow path. This allows for precise characterization of "specific defect patterns on a certain path" (such as out-of-bounds, infinite loops, uninitialized access, etc.), not only determining "whether it is defect-prone," but also distinguishing different defect pattern categories. The fine-grainedness and interpretability are significantly superior to existing coarse-grained prediction methods.
[0039] Secondly, regarding cross-project generalization ability, traditional CPDP solutions often only perform simple alignment on the metric space or coarse-grained text features. The present invention, however, constructs a multimodal domain relevance matrix based on path semantic embedding of multiple source projects. It combines class prototypes, pseudo-labels, and multi-task comparative learning to automatically aggregate representations of the same defect pattern across different projects and naturally separate different defect patterns. Furthermore, a domain discriminant layer is superimposed for adversarial training, explicitly compressing the semantic and structural distribution differences between the source and target domains. Therefore, even when the target project lacks or has only a few labeled samples, the present invention maintains stable defect pattern recognition performance, significantly reducing performance degradation caused by project style differences compared to existing methods.
[0040] Furthermore, regarding engineering applicability and scalability, the technical solution of this invention adopts a unified framework of "AST abstract categories + quantified numerical values + path semantics + comparative learning." The input relies only on general syntax parsing and control flow analysis, making it suitable for various mainstream programming languages. The output uses path-level defect pattern prediction results, which can be directly used in static analysis tools or code review platforms to provide targeted defect pattern warnings for high-risk paths, facilitating rapid location and repair by developers. In summary, the technical solution of this invention has significant comprehensive advantages over existing best-in-class technologies in terms of fine-grained defect pattern characterization, cross-project transfer robustness, and practical implementation effectiveness.
[0041] Based on the same inventive concept, a second aspect of this invention provides a cross-project software defect prediction system based on multimodal path semantics, comprising: The mapping relationship building module is configured to parse the code of the multi-source domain and the target domain respectively, obtain the abstract syntax tree, and establish the mapping relationship between the abstract syntax tree node types and defect patterns. The multimodal path semantic extraction module is configured to input the mapping relationship between the multi-source domain and the target domain into the code pre-trained model to obtain the vector representation of the multimodal path; The path semantic embedding module is configured to perform context interaction modeling and global encoding on the sentence sequence on the path based on the vector representation of the multimodal path, so as to obtain the complete path semantic embedding of the multi-source domain and the target domain respectively. The cross-project software defect prediction module is configured to construct a multimodal domain correlation matrix and perform multi-task comparative learning across multiple source domains based on path semantic embedding. This enables the aggregation of representations of the same defect pattern in different projects and the separation of different defect patterns, thereby achieving cross-project software defect prediction.
[0042] like Figure 1 As shown, the system first uses multi-source items at the bottom layer as training data. Then, through multimodal path semantic modeling and domain relevance modeling in the middle layer, the code in the source domain is parsed into control flow / data flow paths and syntactic structures. These "paths + structures" are then fed into the CodeBERT (code pre-trained model) and path semantic embedding model on the right, resulting in a vector representation of each path. A multimodal domain relevance matrix is used to measure the similarity and difference in multimodal semantics between different source domains, providing a basis for subsequent contrastive learning and domain adaptation. Next, the upper-middle layer multimodal domain contrastive learning module implements multimodal domain contrastive learning, aligning different items and constructing multiple contrastive learning tasks for different perspectives / domains. This ensures that semantically similar paths (the same defect pattern) are closer together in the feature space, while semantically different paths are further apart. The LossAveragingLayer, responsible for weighting or averaging the losses from multiple contrastive learning tasks, forms a unified training signal that updates the preceding CodeBERT and path semantic embedding modules, thus obtaining a defect pattern representation with good invariance among multi-source items. Finally, the top-level implementation handles target domain parsing and defect pattern prediction. `Targetdomain` represents the target domain; `Parse` represents performing the same parsing and path semantic extraction on the target domain code before feeding it into the pre-trained representation and classifier; `Classifier` receives the aligned target domain path representation and outputs the prediction result; `Defectmode` indicates whether it belongs to a certain defect pattern, meaning the classifier determines whether the path matches a known defect pattern, thus enabling automatic identification of potential defect patterns in new projects.
[0043] Therefore, the entire framework constructs a multimodal domain relevance matrix and combines path semantic embedding to perform multi-task comparative learning across multiple source projects. This enables the aggregation of representations of the same defect pattern across different projects and the separation of different defect patterns, improving the consistency and discriminativeness of cross-project defect pattern features. A domain discriminant layer is introduced on top of the comparative learning, and adversarial training is performed through gradient inversion or equivalent mechanisms, ensuring that the path semantic embedding possesses both "defect pattern separability" and "source / target domain inseparability." This achieves the following: a universally applicable defect pattern representation is learned across multiple projects through "multimodal path semantic extraction + multimodal domain relevance matrix + multi-task comparative learning," and then transferred to the target project. Parsing and a classifier determine whether a certain defect mode exists in the target domain code, thereby improving the cross-project transfer effect.
[0044] A third aspect of the present invention provides a machine-readable storage medium storing instructions for causing a machine to execute a cross-project software defect prediction method based on multimodal path semantics.
[0045] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.
Claims
1. A cross-project software defect prediction method based on multimodal path semantics, characterized in that, Includes the following steps: The code of the multi-source domain and the target domain are parsed separately to obtain the abstract syntax tree, and the mapping relationship between the node types of the abstract syntax tree and the defect patterns is established. The mapping relationship between the multi-source domain and the target domain is input into the code pre-trained model to obtain the vector representation of the multimodal path; Vector representation based on multimodal paths performs context interaction modeling and global encoding on the sentence sequence on the path, and obtains complete path semantic embeddings in the multi-source domain and the target domain respectively. A multimodal domain correlation matrix is constructed, and multi-task comparative learning is performed between multiple source domains based on path semantic embedding. This enables the aggregation of representations of the same defect pattern in different projects and the separation of different defect patterns, thereby achieving cross-project software defect pattern prediction.
2. The method according to claim 1, characterized in that, The abstract syntax tree nodes include one or more of the following: control flow nodes, assignment nodes, function / call nodes, declaration nodes, synchronization nodes, jump nodes, or exception nodes.
3. The method according to claim 2, characterized in that, The establishment of the mapping relationship between the abstract syntax tree node types and defect patterns is specifically as follows: By combining the general weakness enumeration, the correspondence between the node types of the abstract syntax tree and the defect patterns is organized and classified to obtain the mapping relationship; The variables and constants in the mapping relationship are normalized and quantized to convert each statement into a unified token sequence, thereby eliminating surface text differences across projects and languages and highlighting numerical and state information related to defect patterns.
4. The method according to claim 1, characterized in that, The vector representation based on multimodal paths models the contextual interactions and globally encodes the sentence sequences along the path, obtaining complete path semantic embeddings for both the multi-source and target domains, including: Based on the control flow graph, one or more representative paths from the virtual entry node to the virtual exit node are extracted, and each node on the path corresponds to a statement. Perform bidirectional message passing on the path to obtain a sequence of node representations with enhanced context, where each node contains a forward representation of the historical execution context and a backward representation of the future execution impact; The node representation sequence is fed into the multi-layer encoder as a semantic path sequence for global modeling. Based on modeling, a direct dependency relationship is established between any two nodes within the path to complete the coding; By pooling the hidden states of nodes output by the encoder or selecting dedicated aggregation positions, a single path semantic embedding vector is obtained.
5. The method according to claim 3, characterized in that, The normalization and quantization processing of the variables and constants in the mapping relationship includes: For numeric variables in basic data types, a unified numerical range is set for quantization matching for different types of numeric variables; For array-type variables, a binary label is used based on whether the array has been initialized, uniformly mapping it to initialized or uninitialized, to reflect key signals related to the initialization state in uninitialized read defect patterns and out-of-bounds access defect patterns; a sequential renaming strategy is used to anonymize variable identifiers; and / or, The unified token sequence consists of: abstract operation category, variable placeholder, quantized constant category, and initialization status label.
6. The method according to claim 3, characterized in that, The process of inputting the mapping relationship of the multi-source domain into the code pre-trained model to obtain the vector representation of the multimodal path includes the following steps: The unified token sequence is input to obtain the vector representation of each token in the sequence. The sequence is then encoded, and the contextual dependencies between tokens are modeled comprehensively from both the front and back directions. Finally, the hidden states at each position are pooled or weighted to obtain the semantic vector of a single statement.
7. The method according to any one of claims 1-6, characterized in that, The construction of a multimodal domain correlation matrix, based on path semantic embedding, performs multi-task comparative learning across multiple source domains, enabling the aggregation of representations of the same defect pattern in different projects and the separation of different defect patterns. Specifically: Based on the contrastive learning results, the defect pattern category with the highest probability is determined according to the distance between the embedding vector of a certain path in the target domain and the prototype vector of each defect pattern, as well as the confidence level output by the contrastive learning results. The prototype vector corresponding to the defect pattern category with the highest probability is then labeled. At the same time, positive samples with similar semantics are selected from samples of the same category, and negative samples with different semantics are selected from other categories. The comparative loss against each defect mode is calculated separately, and then weighted or summed to form an overall comparative learning objective.
8. The method according to claim 7, characterized in that, After forming the overall contrastive learning objective, a domain discrimination mechanism is introduced, using path semantic embedding as input, to determine whether the sample comes from the source item or the target item.
9. A cross-project software defect prediction system based on multimodal path semantics, characterized in that, include: The mapping relationship building module is configured to parse the code of the multi-source domain and the target domain respectively, obtain the abstract syntax tree, and establish the mapping relationship between the abstract syntax tree node types and defect patterns. The multimodal path semantic extraction module is configured to input the mapping relationship between the multi-source domain and the target domain into the code pre-trained model to obtain the vector representation of the multimodal path; The path semantic embedding module is configured to perform context interaction modeling and global encoding on the sentence sequence on the path based on the vector representation of the multimodal path, so as to obtain the complete path semantic embedding of the multi-source domain and the target domain respectively. The cross-project software defect prediction module is configured to construct a multimodal domain correlation matrix and perform multi-task comparative learning across multiple source domains based on path semantic embedding. This enables the aggregation of representations of the same defect pattern in different projects and the separation of different defect patterns, thereby achieving cross-project software defect prediction.
10. A machine-readable storage medium having instructions stored thereon for causing a machine to perform the method as described in any one of claims 1-8.