A software source code defect automatic positioning method and system
Patent Information
- Application Number
- CN202311609183.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-29
- Publication Date
- 2026-08-21
- Estimated Expiration
- 2043-11-29
AI Technical Summary
现代软件通常是使用多种编程语言开发的,只支持单一编程语言的缺陷定位,将会导致缺陷检测的不全面和不准确
[0109]综上,本发明无需人工参与即可自动完成包含缺陷的源代码的缺陷定位;本发明具备高定位准确性;本发明实现软件开发设计阶段的早期安全性保障。
Smart Images

Figure CN117909211B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of code defect localization technology, and in particular relates to a method and system for automatic localization of software source code defects. Background Technology
[0002] Source code defect localization refers to the process of identifying the location and type of security flaws within source code through analysis. Source code defect localization is crucial for source code defect analysis because it helps security researchers more accurately locate and analyze potential security issues, thereby better assessing software security. Existing machine learning-based defect detection methods are mostly at the function level, and their detection results do not provide meaningful details about defects, still requiring expert identification of defect details. This approach is not effective in helping ordinary software security practitioners.
[0003] Furthermore, existing work primarily focuses on defects in specific programming languages, failing to meet the security testing requirements of projects developed using multiple programming languages. Therefore, a defect localization method that supports multiple programming languages is of broader significance. Modern software is typically developed using multiple programming languages; a defect localization method supporting multiple programming languages can cover a wider range of software systems and better identify the types and characteristics of security defects in different programming languages.
[0004] Machine reading comprehension is a benchmark task for evaluating a machine's natural language processing capabilities and has become a challenging task in the field of NLP in recent years. It involves inputting a series of questions into the machine and then evaluating its output using standard answers. This measures the machine's ability to read, process, understand, and answer related questions about a natural language passage. Each instance in the MRC dataset contains a context (C), an associated question (Q), and an answer (A). The goal of the MRC system is to learn a prediction function f that can generate the appropriate answer A from the inputs C and Q.
[0005] f:(C,Q)→A
[0006] The output of an MRC system can be extracted from or generated from the context C. In particular, the goal of a multi-option MRC system is to select the most correct answer using C and Q.
[0007] In the prior art: (1) SySeVR uses deep learning to detect source code defects at the function level. In particular, it introduces and defines the concepts of syntactic defect candidate (SyVC) and semantic defect candidate (SeVC), where SyVC reflects the syntactic features of defects and SeVC extends SyVC to adapt to semantic information related to data dependencies and control dependencies; (2) A defect detection model called Devign, based on a graph neural network (GNN), uses AST as its backbone and explicitly encodes different levels of program control and data dependencies into a joint graph form with heterogeneous edges for source code functions, with each type of representation corresponding to one edge. This comprehensive representation helps to capture as many defect types and patterns as possible and can learn more effective node representations through GNN; (3) A technique called FUNDED takes a multi-relation code graph (with explicit control and data flow or syntactic information) of the source code function as input and learns and aggregates multiple code relationships (including data flow, control flow, operation order, and operation values, etc.) in the code graph through GGNN.
[0008] Most existing defect detection methods focus on the function level, which only indicates the existence of a defect but doesn't provide meaningful details about it. Therefore, they cannot effectively support further defect analysis, such as defect exploitation or remediation. Furthermore, existing methods primarily target defects in specific programming languages, failing to meet the application requirements of projects developed using multiple languages. Therefore, a defect localization method that supports multiple programming languages is far more significant. Modern software is typically developed using multiple programming languages; supporting only a single programming language for defect localization will lead to incomplete and inaccurate defect detection. Summary of the Invention
[0009] The technical problem this invention aims to solve is that existing machine learning-based automatic software source code defect detection methods suffer from coarse-grained defect analysis and only support defects in specific programming languages, hindering early security assurance during software development and design. To address these problems, an automatic software source code defect localization scheme is proposed.
[0010] The first aspect of this invention proposes an automatic defect location method for software source code. The method is based on an automatic defect location system for software source code, which includes a defect location dataset construction module and a defect location model. The defect location dataset construction module includes a basic dataset construction module and a data augmentation strategy. The defect location model includes a precoding module and a defect location module.
[0011] The method includes:
[0012] Step S1: Call the defect localization dataset construction module to convert the function code containing defects into a dataset suitable for defect localization tasks;
[0013] The basic dataset construction module converts the defective function code into MRC-formatted data as the basic dataset. The basic dataset is then augmented using the data augmentation strategy to obtain the dataset suitable for the defect localization task. The MRC-formatted data includes four fields: ID, Context, Question, and Answers.
[0014] Step S2: Call the defect localization model to perform defect localization on the transformed dataset suitable for the defect localization task, so as to obtain the defect localization result;
[0015] The precoding module performs embedded encoding on samples in the dataset suitable for the defect localization task to obtain an encoded dataset, and the defect localization module performs defect localization based on the encoded dataset.
[0016] According to the method of the first aspect of the present invention, in step S1, the basic dataset construction module converts the defective function code into the MRC-formatted data as the basic dataset; wherein:
[0017] Id represents the index number of each sample;
[0018] Context represents the source code function containing the defect;
[0019] Question indicates whether the Context contains a defective code flow from Source to Sink;
[0020] Where Source represents the data source input from an external source, and Sink represents the internal function used for memory operations;
[0021] The answer contains multiple candidate code streams, with the actual defective code stream serving as the real label;
[0022] The CodeQL tool is used to perform defect scanning on the Context to obtain the corresponding defect detection report. Each record in the defect report contains the Context, the Source-Sink pair, and one or more code streams containing defects. One of the code streams containing defects is added to the Answer as a real label.
[0023] Input the Context and Source-Sink pairs into the Joern tool to extract other candidate defect code streams. The Joern tool is used to generate all reachable code streams from a given Source-Sink pair.
[0024] According to the method of the first aspect of the present invention, in step S1, the basic dataset is augmented by the data augmentation strategy to obtain the dataset suitable for the defect localization task; wherein:
[0025] The data augmentation strategy involves selecting and expanding candidate defect code streams in the base dataset;
[0026] The selection strategy for candidate defect code streams includes: selecting a code stream containing at least 2 lines of code from the initial candidate defect code streams based on the length requirement of each candidate defect code stream, wherein the code stream contains at least a Source line and a Sink line; and ensuring that the difference between the number of lines of code in each candidate defect code stream and the number of lines of code in the real label defect code stream does not exceed 1 based on the length of the real label defect code stream.
[0027] The generation strategy for the candidate defect code stream includes: selecting a single defect code stream with fewer lines than the real labeled defect code stream from the initial candidate defect code stream; randomly selecting several lines from the real labeled defect code stream and adding them to the single defect code stream to form a new available candidate defect code stream; and performing a simple line order transformation on all candidate defect code streams.
[0028] According to the method of the first aspect of the present invention, in step S2, the defect localization model consists of a pre-trained UniXcoder with multi-head attention, LSTM, and CNN, and the defect localization model includes three layers: an encoding layer, a bidirectional matching layer, and an inference layer based on key information extraction; wherein:
[0029] The encoding layer uses a pre-trained UniXcoder to encode the input Context, Question, and Answer.
[0030] The bidirectional matching layer uses Bi-LSTM to perform bidirectional information matching on the UniXcoder encoding output, so as to enable better semantic and context-related information interaction between sequence pairs;
[0031] The inference layer uses a CNN-based architecture to extract locally relevant information from the output of the bidirectional matching layer as global information aggregation.
[0032] According to the method of the first aspect of the present invention, in step S2, for the coding layer:
[0033] Given an input sample, the input sample includes code fragments C = {c1, c2, ..., C} i Problem Q = {q1, q2, ..., q} j} and multiple candidate defect code flows A = {a1, a2, ..., a k The coding layer is used to make the correct selection from the candidate defect code stream;
[0034] Where c i q j and a k Let each be the nth word in the paragraph, question, and candidate defect code streams. Use specific tags to concatenate all tags in C, Q, and A into a new sequence:
[0035] [[CLS],C,[SEP],Q,[SEP],A,[SEP]]
[0036] Wherein, [CLS] represents a special marker used for classification, and [SEP] represents a separator;
[0037] By summing the embeddings of the corresponding code tokens and positions from UniXcoder, each token is initialized with a vector and encoded as a hidden state. The resulting encoded output is [H]. CLS H C H Q H A ], H C H Q H A This is the output from the last hidden layer sequence of UniXcoder, used for information exchange through matching.
[0038] According to the method of the first aspect of the present invention, in step S2, for the bidirectional matching layer:
[0039] Multi-head attention and bidirectional LSTM are deployed in the bidirectional matching layer to process the encoded output of the coding layer;
[0040] For Context, two attention weights are calculated to capture the information interaction between Question and Answer: Context-Question attention. and Context-Answer Attention The calculation methods are as follows:
[0041]
[0042]
[0043]
[0044] Among them W c b c Represents the learning parameters, other attention weights and Obtained through the corresponding calculation method;
[0045] Bi-LSTM obtains context-related information from the bidirectional interaction results as the final interaction information between the two input sequences. For Context and Question, the interaction information is calculated as follows:
[0046]
[0047] Where [:] represents the concatenation operation, used to join two vectors;
[0048] The other two interactive information Obtained through the corresponding calculation method;
[0049] All information interactions will be aggregated by connecting three interaction results:
[0050]
[0051] According to the method of the first aspect of the present invention, in step S2, for the inference layer:
[0052] After obtaining the interactive results, a CNN-based architecture is used to effectively extract key local information to distinguish the correct defective code stream from other candidate defective code streams. The calculation method is shown in the following formula:
[0053] F Pool =MaxPool(Conv1d(F Al ))
[0054] F = F Pool .Max(1)
[0055] This includes a 1D convolutional layer (Conv1d), a regular max-pooling layer (MaxPool), and a column max-pooling layer (Max). Max pooling operations obtain the maximum value of all elements in a certain dimension of the tensor, F. Pool The dimensions are [batch size, maximum sequence length, number of hidden layer units], where the index 1 of Max indicates the dimension of sequence length, and F is used as a global vector representation of Context, Question, and specific candidate Answer sets;
[0056] For the classification task, each candidate defect code stream A i The loss is calculated as follows:
[0057]
[0058] The actual defect code stream is obtained after passing through a linear classifier as the localization result.
[0059] A second aspect of this invention proposes an automatic software source code defect localization system. The automatic software source code defect localization system includes a defect localization dataset construction module and a defect localization model. The defect localization dataset construction module includes a basic dataset construction module and a data augmentation strategy. The defect localization model includes a precoding module and a defect localization module; wherein:
[0060] The defect localization dataset construction module is invoked to transform the function code containing defects into a dataset suitable for defect localization tasks;
[0061] The basic dataset construction module converts the defective function code into MRC-formatted data as the basic dataset. The basic dataset is then augmented using the data augmentation strategy to obtain the dataset suitable for the defect localization task. The MRC-formatted data includes four fields: ID, Context, Question, and Answers.
[0062] The defect localization model is invoked to perform defect localization on the transformed dataset suitable for the defect localization task, so as to obtain the defect localization result;
[0063] The precoding module performs embedded encoding on samples in the dataset suitable for the defect localization task to obtain an encoded dataset, and the defect localization module performs defect localization based on the encoded dataset.
[0064] According to the system of the second aspect of the present invention, the basic dataset construction module converts the defective function code into the MRC-formatted data as the basic dataset; wherein:
[0065] Id represents the index number of each sample;
[0066] Context represents the source code function containing the defect;
[0067] Question indicates whether the Context contains a defective code flow from Source to Sink;
[0068] Wherein, Source represents the data source input from an external source, and Sink represents the internal function used for memory operations;
[0069] The answer contains multiple candidate code streams, with the actual defective code streams serving as the actual labels;
[0070] The CodeQL tool is used to perform defect scanning on the Context to obtain the corresponding defect detection report. Each record in the defect report contains the Context, the Source-Sink pair, and one or more code streams containing defects. One of the code streams containing defects is added to the Answer as a real label.
[0071] Input the Context and Source-Sink pairs into the Joern tool to extract other candidate defect code streams. The Joern tool is used to generate all reachable code streams from a given Source-Sink pair.
[0072] According to the system of the second aspect of the present invention, the basic dataset is augmented by the data augmentation strategy to obtain the dataset suitable for the defect localization task; wherein:
[0073] The data augmentation strategy involves selecting and expanding candidate defect code streams in the base dataset;
[0074] The selection strategy for candidate defect code streams includes: selecting a code stream containing at least 2 lines of code from the initial candidate defect code streams based on the length requirement of each candidate defect code stream, wherein the code stream contains at least a Source line and a Sink line; and ensuring that the difference between the number of lines of code in each candidate defect code stream and the number of lines of code in the real label defect code stream does not exceed 1 based on the length of the real label defect code stream.
[0075] The generation strategy for the candidate defect code stream includes: selecting a single defect code stream with fewer lines than the real labeled defect code stream from the initial candidate defect code stream; randomly selecting several lines from the real labeled defect code stream and adding them to the single defect code stream to form a new available candidate defect code stream; and performing a simple line order transformation on all candidate defect code streams.
[0076] According to a system based on a second aspect of the present invention, the defect localization model comprises a pre-trained UniXcoder with multi-head attention, LSTM, and CNN, and the defect localization model includes three layers: an encoding layer, a bidirectional matching layer, and an inference layer based on key information extraction; wherein:
[0077] The encoding layer uses a pre-trained UniXcoder to encode the input Context, Question, and Answer.
[0078] The bidirectional matching layer uses Bi-LSTM to perform bidirectional information matching on the UniXcoder encoding output, so as to enable better semantic and context-related information interaction between sequence pairs;
[0079] The inference layer uses a CNN-based architecture to extract locally relevant information from the output of the bidirectional matching layer as global information aggregation.
[0080] According to the system of the second aspect of the invention, for the coding layer:
[0081] Given an input sample, the input sample includes code fragments C = {c1, c2, ..., C} i Problem Q = {q1, q2, ..., q} j} and multiple candidate defect code flows A = {a1, a2, ..., a k The coding layer is used to make the correct selection from the candidate defect code stream;
[0082] Where c i q j and a k Let each be the nth word in the paragraph, question, and candidate defect code streams. Use specific tags to concatenate all tags in C, Q, and A into a new sequence:
[0083] [[CLS],C,[SEP],Q,[SEP],A,[SEP]]
[0084] Wherein, [CLS] represents a special marker used for classification, and [SEP] represents a separator;
[0085] By summing the embeddings of the corresponding code tokens and positions from UniXcoder, each token is initialized with a vector and encoded as a hidden state. The resulting encoded output is [H]. CLS H C H Q H A ], H C H Q H A This is the output from the last hidden layer sequence of UniXcoder, used for information exchange through matching.
[0086] According to the system of the second aspect of the invention, for the bidirectional matching layer:
[0087] Multi-head attention and bidirectional LSTM are deployed in the bidirectional matching layer to process the encoded output of the coding layer;
[0088] For Context, two attention weights are calculated to capture the information interaction between Question and Answer: Context-Question attention. and Context-Answer Attention The calculation methods are as follows:
[0089]
[0090]
[0091]
[0092] Among them W c b c Represents the learning parameters, other attention weights and Obtained through the corresponding calculation method;
[0093] Bi-LSTM obtains context-related information from the bidirectional interaction results as the final interaction information between the two input sequences. For Context and Question, the interaction information is calculated as follows:
[0094]
[0095] Where [:] represents the concatenation operation, used to join two vectors;
[0096] The other two interactive information Obtained through the corresponding calculation method;
[0097] All information interactions will be aggregated by connecting three interaction results:
[0098]
[0099] According to the system of the second aspect of the present invention, for the inference layer:
[0100] After obtaining the interactive results, a CNN-based architecture is used to effectively extract key local information to distinguish the correct defective code stream from other candidate defective code streams. The calculation method is shown in the following formula:
[0101] F Pool =MaxPool(Conv1d(F Al ))
[0102] F = F Pool .Max(1)
[0103] This includes a 1D convolutional layer (Conv1d), a regular max-pooling layer (MaxPool), and a column max-pooling layer (Max). Max pooling operations obtain the maximum value of all elements in a certain dimension of the tensor, F. Pool The dimensions are [batch size, maximum sequence length, number of hidden layer units], where the index 1 of Max indicates the dimension of sequence length, and F is used as a global vector representation of Context, Question, and specific candidate Answer sets;
[0104] For the classification task, each candidate defect code stream A i The loss is calculated as follows:
[0105]
[0106] The actual defect code stream is obtained after passing through a linear classifier as the localization result.
[0107] A third aspect of this invention discloses an electronic device. The electronic device includes a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, it implements the steps of the automatic software source code defect location method described in the first aspect of this disclosure.
[0108] A fourth aspect of this invention discloses a computer-readable storage medium. The computer-readable storage medium stores a computer program, which, when executed by a processor, implements the steps of the automatic software source code defect location method described in the first aspect of this disclosure.
[0109] In summary, this invention can automatically locate defects in source code containing defects without human intervention; this invention has high location accuracy; this invention achieves early security assurance in the software development and design phase. Attached Figure Description
[0110] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0111] Figure 1 This is a schematic diagram of an automatic software source code defect location system according to an embodiment of the present invention.
[0112] Figure 2 This is a schematic diagram of the defect dataset construction process according to an embodiment of the present invention.
[0113] Figure 3This is an illustration of a pre-trained defect localization model according to an embodiment of the present invention.
[0114] Figure 4 This is a schematic diagram of a specific example process according to an embodiment of the present invention.
[0115] Figure 5 This is a structural diagram of an electronic device according to an embodiment of the present invention. Detailed Implementation
[0116] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, 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.
[0117] The first aspect of this invention proposes an automatic method for locating defects in software source code. The method is based on an automatic software source code defect location system (such as...). Figure 1 The method, as shown, is used to automatically locate defects in software source code. The software source code defect automatic location system includes a defect location dataset construction module and a defect location model. The defect location dataset construction module includes a basic dataset construction module and a data augmentation strategy. The defect location model includes a precoding module and a defect location module.
[0118] Step S1: Call the defect localization dataset construction module to convert the function code containing defects into a dataset suitable for defect localization tasks;
[0119] The basic dataset construction module converts the defective function code into MRC-formatted data as the basic dataset. The basic dataset is then augmented using the data augmentation strategy to obtain the dataset suitable for the defect localization task. The MRC-formatted data includes four fields: ID, Context, Question, and Answers.
[0120] Step S2: Call the defect localization model to perform defect localization on the transformed dataset suitable for the defect localization task, so as to obtain the defect localization result;
[0121] The precoding module performs embedded encoding on samples in the dataset suitable for the defect localization task to obtain an encoded dataset, and the defect localization module performs defect localization based on the encoded dataset.
[0122] Specifically, the automatic defect localization method and system for software source code consists of a defect localization dataset construction module and a defect localization model. The defect localization dataset construction module includes a basic dataset construction module and data augmentation strategies. The defect localization model is based on a pre-trained architecture and includes a pre-coding module and a defect localization module. First, the defect localization dataset construction module obtains a dataset suitable for the defect localization task, and then the defect localization model obtains the defect localization result. Specifically: First, the defect localization dataset construction module is based on an MRC-formatted defect localization dataset, containing four fields: ID, Context, Question, and Answers. Then, the data augmentation strategy is used to construct an augmented dataset. Further, the defect localization model takes the dataset as input, first embedding and encoding the dataset samples through the pre-coding module, and then locating the defect position through the defect localization module to obtain the final defect localization result.
[0123] Specifically, an automatic software source code defect localization system is built. It consists of a defect localization dataset construction module and a defect localization model. The defect localization dataset construction module includes a basic dataset construction module and data augmentation strategies. The basic dataset construction module constructs the source code defect localization dataset in MRC format (the basic dataset should be the basic dataset, and the defect localization dataset should be obtained after data augmentation based on the data augmentation strategy), containing four fields: Id, Context, Question, and Answers. The data augmentation strategy expands the basic dataset through custom strategies. The defect localization model obtains the embedded representation of the input data through a precoding module, and then outputs the identified defect code stream through the defect localization module.
[0124] According to the method of the first aspect of the present invention, in step S1, the basic dataset construction module converts the defective function code into the MRC-formatted data as the basic dataset; wherein:
[0125] Id represents the index number of each sample;
[0126] Context represents the source code function containing the defect;
[0127] Question indicates whether the Context contains a defective code flow from Source to Sink;
[0128] Wherein, Source represents the data source input from an external source, and Sink represents the internal function used for memory operations;
[0129] The answer contains multiple candidate code streams, with the actual defective code streams serving as the actual labels;
[0130] The CodeQL tool is used to perform defect scanning on the Context to obtain the corresponding defect detection report. Each record in the defect report contains the Context, the Source-Sink pair, and one or more code streams containing defects. One of the code streams containing defects is added to the Answer as a real label.
[0131] Input the Context and Source-Sink pairs into the Joern tool to extract other candidate defect code streams. The Joern tool is used to generate all reachable code streams from a given Source-Sink pair.
[0132] Specifically, in the basic dataset building module, `Id` is the index number of each dataset sample, and `Context` is a source code function containing defects. `Question` queries whether `Context` contains a defective code stream from `Source` to `Sink`. Here, `Source` represents the data source input from an external source, and `Sink` is a security-sensitive internal function used for things like memory operations. `Answer` includes multiple candidate code streams, with the actual defective code stream serving as the true label. Specifically, the `Context` is first scanned for defects using a CodeQL tool to obtain a corresponding defect detection report. Each record in the report contains a `Context`, `Source`, and `Sink` pair, as well as one or more code streams containing defects. One of these code streams can be added to `Answer` and considered as the true label. Next, the `Context`, `Source`, and `Sink` pair is input into the Joern tool to extract other candidate defective code streams. Joern can generate all reachable code streams from a given `Source` and `Sink` pair.
[0133] According to the method of the first aspect of the present invention, in step S1, the basic dataset is augmented by the data augmentation strategy to obtain the dataset suitable for the defect localization task; wherein:
[0134] The data augmentation strategy involves selecting and expanding candidate defect code streams in the base dataset;
[0135] The selection strategy for candidate defect code streams includes: selecting a code stream containing at least 2 lines of code from the initial candidate defect code streams based on the length requirement of each candidate defect code stream, wherein the code stream contains at least a Source line and a Sink line; and ensuring that the difference between the number of lines of code in each candidate defect code stream and the number of lines of code in the real label defect code stream does not exceed 1 based on the length of the real label defect code stream.
[0136] The generation strategy for the candidate defect code stream includes: selecting a single defect code stream with fewer lines than the real labeled defect code stream from the initial candidate defect code stream; randomly selecting several lines from the real labeled defect code stream and adding them to the single defect code stream to form a new available candidate defect code stream; and performing a simple line order transformation on all candidate defect code streams.
[0137] Specifically, the data augmentation strategy expands the base dataset by selecting and generating candidate defect code streams. The candidate defect code stream selection strategy is as follows: 1) Considering the length requirement of each candidate defect code stream, this strategy selects a code stream containing at least 2 lines of code from the initial candidate defect code streams. This is because a reasonable defect code stream should contain at least two Source and Sink lines. 2) Considering that the model may be sensitive to the length of the real label defect code stream, this strategy ensures that the number of lines in each candidate defect code stream is similar to the number of lines in the real label defect code stream (in the proposed dataset, this line difference is set to 1). This is to avoid a situation where the real label defect code stream is always easily distinguishable from other candidate defect code streams in terms of length, which would cause the model to learn only length information to judge the result. The generation strategy is as follows: 1) Select a single defect code stream with fewer lines than the real label defect code stream from the initial candidate defect code streams, and then randomly select several lines from the real label defect code stream and add them to the single defect code stream to form new usable candidate defect code streams. 2) Perform a simple line order transformation on all candidate defect code streams (including the real label defect code stream). Finally, based on the above strategy, a candidate defect code stream library was constructed, and three of them were randomly selected as samples of candidate defect code streams.
[0138] According to the method of the first aspect of the present invention, in step S2, the defect localization model consists of a pre-trained UniXcoder with multi-head attention, LSTM, and CNN, and the defect localization model includes three layers: an encoding layer, a bidirectional matching layer, and an inference layer based on key information extraction; wherein:
[0139] The encoding layer uses a pre-trained UniXcoder to encode the input Context, Question, and Answer.
[0140] The bidirectional matching layer uses Bi-LSTM to perform bidirectional information matching on the UniXcoder encoding output, so as to enable better semantic and context-related information interaction between sequence pairs;
[0141] The inference layer uses a CNN-based architecture to extract locally relevant information from the output of the bidirectional matching layer as global information aggregation.
[0142] Specifically, the defect localization model consists of a pre-trained UniXcoder with multi-head attention, LSTM, and CNN. It mainly comprises three layers: an encoding layer, a bidirectional matching layer, and an inference layer based on key information extraction. The encoding layer uses a pre-trained UniXcoder to encode the input Context, Question, and Answer. The bidirectional matching layer uses Bi-LSTM on the UniXcoder's encoding output for bidirectional information matching, enabling better semantic and context-related information interaction between sequence pairs. The inference layer uses a CNN-based architecture to further extract locally relevant information from the bidirectional matching layer for global information aggregation.
[0143] According to the method of the first aspect of the present invention, in step S2, for the coding layer:
[0144] Given an input sample, the input sample includes code fragments C = {c1, c2, ..., C} i Problem Q = {q1, q2, ..., q} j} and multiple candidate defect code flows A = {a1, a2, ..., a k The coding layer is used to make the correct selection from the candidate defect code stream;
[0145] Where c i q j and a k Let each be the nth word in the paragraph, question, and candidate defect code streams. Use specific tags to concatenate all tags in C, Q, and A into a new sequence:
[0146] [[CLS],C,[SEP],Q,[SEP],A,[SEP]]
[0147] Wherein, [CLS] represents a special marker used for classification, and [SEP] represents a separator;
[0148] By summing the embeddings of the corresponding code tokens and positions from UniXcoder, each token is initialized with a vector and encoded as a hidden state. The resulting encoded output is [H]. CLS H C H Q HA ], H C H Q H A This is the output from the last hidden layer sequence of UniXcoder, used for information exchange through matching.
[0149] Specifically, the first layer is the precoding layer. Given an input sample, it includes a code fragment C = {c1, c2, ..., C}. i A problem Q = {q1, q2, ..., q} j}, and multiple candidate defect code flows A = {a1, a2, ..., a k The goal is to make the most reasonable and correct choice from the candidate defective code stream. Where c i q j and a k These are the nth words of the paragraph, question, and candidate defect code streams, respectively. Then, all tokens in C, Q, and A are concatenated into a new sequence using specific tags: [[CLS],C,[SEP],Q,[SEP],A,[SEP]], where [CLS] is a special tag used for classification, and [SEP] is the separator. Each token is initialized with a vector by summing the embeddings of the corresponding code tokens and positions from UniXcoder, and then encoded as a hidden state. Finally, the resulting encoded output is [H... CLS H C H Q H A ]. H C H Q H A The outputs from the last hidden layer sequence in UniXcoder can be further matched to enable information exchange.
[0150] According to the method of the first aspect of the present invention, in step S2, for the bidirectional matching layer:
[0151] Multi-head attention and bidirectional LSTM are deployed in the bidirectional matching layer to process the encoded output of the coding layer;
[0152] For Context, two attention weights are calculated to capture the information interaction between Question and Answer: Context-Question attention. and Context-Answer Attention The calculation methods are as follows:
[0153]
[0154]
[0155]
[0156] Among them W c b c Represents the learning parameters, other attention weights and Obtained through the corresponding calculation method;
[0157] Bi-LSTM obtains context-related information from the bidirectional interaction results as the final interaction information between the two input sequences. For Context and Question, the interaction information is calculated as follows:
[0158]
[0159] Where [:] represents the concatenation operation, used to join two vectors;
[0160] The other two interactive information Obtained through the corresponding calculation method;
[0161] All information interactions will be aggregated by connecting three interaction results:
[0162]
[0163] Specifically, in the second bidirectional matching layer, to capture the effective information interaction between sequence pairs (Context, Question, and Answer), multi-head attention and a bidirectional LSTM are deployed to process the encoded output of the encoding layer. Taking Context as an example, two attention weights are calculated to obtain the information interaction between Question and Answer, namely Context-Question attention. and Context-Answer Attention The calculation methods are as follows:
[0164]
[0165]
[0166]
[0167] Among them, W c b c These are learnable parameters. The other four attention weights... and This can be obtained through a similar calculation. Next, Bi-LSTM is used to obtain context-related information from the bidirectional interaction results, serving as the final interaction information between the two input sequences. Taking Context and Question as an example, the final interaction information is calculated as follows:
[0168]
[0169] Where [:] denotes a concatenation operation, used to join two vectors. Then, the other two interaction messages can be similar to... Ultimately, all information interactions will be aggregated by connecting three interaction results:
[0170]
[0171] According to the method of the first aspect of the present invention, in step S2, for the inference layer:
[0172] After obtaining the interactive results, a CNN-based architecture is used to effectively extract key local information to distinguish the correct defective code stream from other candidate defective code streams. The calculation method is shown in the following formula:
[0173] F Pool =MaxPool(Conv1d(F Al ))
[0174] F = F Pool .Max(1)
[0175] This includes a 1D convolutional layer (Conv1d), a regular max-pooling layer (MaxPool), and a column max-pooling layer (Max). Max pooling operations obtain the maximum value of all elements in a certain dimension of the tensor, F. Pool The dimensions are [batch size, maximum sequence length, number of hidden layer units], where the index 1 of Max indicates the dimension of sequence length, and F is used as a global vector representation of Context, Question, and specific candidate Answer sets;
[0176] For the classification task, each candidate defect code stream A i The loss is calculated as follows:
[0177]
[0178] The actual defect code stream is obtained after passing through a linear classifier as the localization result.
[0179] Specifically, the third layer is the inference layer. After obtaining sufficient interactive information, a CNN-based architecture is used to further and more effectively extract key local information to distinguish the correct defective code stream from other candidate defective code streams. The calculation method is as follows:
[0180] F Pool =MaxPool(Conv1d(F All ))
[0181] F = F Pool .Max(1)
[0182] Specifically, it includes a 1D convolutional layer (Conv1d), a regular max-pooling layer (MaxPool), and a column max-pooling layer (Max). The max-pooling operation retrieves the maximum value of all elements in a specific dimension of the tensor. F Pool The dimensions are [batch size, maximum sequence length, number of hidden layer units], so the index 1 of Max indicates the dimension of sequence length. F is used as a global vector representation of Context, Question, and a specific set of candidate Answers. Finally, for the classification task, each candidate defect code stream A... i The loss is calculated as follows:
[0183]
[0184] The actual defect code stream is obtained by passing it through a linear classifier as the localization result.
[0185] Specific Example 1
[0186] During the testing phase, an automatic defect location system for the software source code is initialized. The method is as follows:
[0187] The defect localization dataset construction module builds a defect localization dataset based on the MRC format, which includes four fields: ID, Context, Question, and Answers.
[0188] The data augmentation strategy is used to construct an augmented dataset. The dataset samples are input into the precoding module for embedding encoding; then, the defect location module locates the defect location and trains the defect location model.
[0189] The software source code defect automatic localization system receives the source code file. The method is as follows: the source code data used for defect localization is processed through steps 1) and 2) in the second step to obtain the defect localization dataset, which is then used as input to the trained defect localization model. The model output is the localization result in the form of defect code stream.
[0190] Complete an automatic defect location for source code.
[0191] Specific Example 2
[0192] The automatic software source code defect detection system proposed in this invention consists of an automatic software source code defect localization method and system, comprising a defect localization dataset construction module and a defect localization model. The defect localization dataset construction module includes a basic dataset construction module and data augmentation strategies. The defect localization model is based on a pre-trained architecture and includes a pre-coding module and a defect localization module. First, a dataset suitable for the defect localization task is obtained through the defect localization dataset construction module, and then the defect localization result is obtained through the defect localization model.
[0193] Combination Figure 2 The defect localization dataset construction module includes a basic dataset construction module and data augmentation strategies. The basic dataset construction module builds the source code defect localization dataset in MRC format, containing four fields: Id, Context, Question, and Answers. The data augmentation strategies expand the basic dataset through custom strategies. The defect localization model obtains the embedded representation of the input data through a precoding module, and then outputs the defect code stream options for discrimination through the defect localization module.
[0194] In the basic dataset building module, `Id` is the index number of each sample, and `Context` is a source code function containing defects. `Question` queries whether `Context` contains a defect code stream from `Source` to `Sink`. Here, `Source` represents the data source input from an external source, and `Sink` is a security-sensitive internal function used for things like memory operations. `Answer` includes the defect code stream of the true label and other candidate defect code streams.
[0195] The data augmentation strategy expands the base dataset by selecting and generating candidate defect code streams. The candidate defect code stream selection strategy is as follows: 1) Considering the length requirement of each candidate defect code stream, this strategy selects a code stream containing at least 2 lines of code from the initial candidate defect code streams. This is because a reasonable defect code stream should contain at least two Source and Sink lines. 2) Considering that the model may be sensitive to the length of the real label defect code stream, this strategy ensures that the number of lines in each candidate defect code stream is similar to the number of lines in the real label defect code stream (in the proposed dataset, this line difference is set to 1). This is to avoid a situation where the real label defect code stream is always easily distinguishable from other candidate defect code streams in terms of length, which would cause the model to learn only length information to judge the result. The generation strategy is as follows: 1) Select a single defect code stream with fewer lines than the real label defect code stream from the initial candidate defect code streams, and then randomly select several lines from the real label defect code stream and add them to the single defect code stream to form new usable candidate defect code streams. 2) Perform a simple line order transformation on all candidate defect code streams (including the real label defect code stream). Finally, based on the above strategy, a candidate defect code stream library was constructed, and three of them were randomly selected as samples of candidate defect code streams.
[0196] Combination Figure 3 The defect localization model consists of a pre-trained UniXcoder with multi-head attention, LSTM, and CNN. It mainly comprises three layers: an encoding layer, a bidirectional matching layer, and an inference layer based on key information extraction. The encoding layer uses a pre-trained UniXcoder to encode the input Context, Question, and Answer. The bidirectional matching layer uses Bi-LSTM on the UniXcoder's encoded output for bidirectional information matching, enabling better semantic and context-related information interaction between sequence pairs. The inference layer uses a CNN-based architecture to further extract effective local information from the bidirectional matching layer for global information aggregation. Finally, a linear classification layer outputs the defect code stream.
[0197] Combination Figure 4 The overall process of this invention is as follows: First, build a software source code defect automatic location system; second, initialize the software source code defect automatic location system environment; third, the software source code defect automatic location system receives the source code file; fourth, complete one automatic source code defect location.
[0198] A second aspect of this invention proposes an automatic software source code defect localization system. The automatic software source code defect localization system includes a defect localization dataset construction module and a defect localization model. The defect localization dataset construction module includes a basic dataset construction module and a data augmentation strategy. The defect localization model includes a precoding module and a defect localization module; wherein:
[0199] The defect localization dataset construction module is invoked to transform the function code containing defects into a dataset suitable for defect localization tasks;
[0200] The basic dataset construction module converts the defective function code into MRC-formatted data as the basic dataset. The basic dataset is then augmented using the data augmentation strategy to obtain the dataset suitable for the defect localization task. The MRC-formatted data includes four fields: ID, Context, Question, and Answers.
[0201] The defect localization model is invoked to perform defect localization on the transformed dataset suitable for the defect localization task, so as to obtain the defect localization result;
[0202] The precoding module performs embedded encoding on samples in the dataset suitable for the defect localization task to obtain an encoded dataset, and the defect localization module performs defect localization based on the encoded dataset.
[0203] According to the system of the second aspect of the present invention, the basic dataset construction module converts the defective function code into the MRC-formatted data as the basic dataset; wherein:
[0204] Id represents the index number of each sample;
[0205] Context represents the source code function containing the defect;
[0206] Question indicates whether the Context contains a defective code flow from Source to Sink;
[0207] Where Source represents the data source input from an external source, and Sink represents the internal function used for memory operations;
[0208] The answer contains multiple candidate code streams, with the actual defective code stream serving as the real label;
[0209] The CodeQL tool is used to perform defect scanning on the Context to obtain the corresponding defect detection report. Each record in the defect report contains the Context, the Source-Sink pair, and one or more code streams containing defects. One of the code streams containing defects is added to the Answer as a real label.
[0210] Input the Context and Source-Sink pairs into the Joern tool to extract other candidate defect code streams. The Joern tool is used to generate all reachable code streams from a given Source-Sink pair.
[0211] According to the system of the second aspect of the present invention, the basic dataset is augmented by the data augmentation strategy to obtain the dataset suitable for the defect localization task; wherein:
[0212] The data augmentation strategy involves selecting and expanding candidate defect code streams in the base dataset;
[0213] The selection strategy for candidate defect code streams includes: selecting a code stream containing at least 2 lines of code from the initial candidate defect code streams based on the length requirement of each candidate defect code stream, wherein the code stream contains at least a Source line and a Sink line; and ensuring that the difference between the number of lines of code in each candidate defect code stream and the number of lines of code in the real label defect code stream does not exceed 1 based on the length of the real label defect code stream.
[0214] The generation strategy for the candidate defect code stream includes: selecting a single defect code stream with fewer lines than the real labeled defect code stream from the initial candidate defect code stream; randomly selecting several lines from the real labeled defect code stream and adding them to the single defect code stream to form a new available candidate defect code stream; and performing a simple line order transformation on all candidate defect code streams.
[0215] According to a system based on a second aspect of the present invention, the defect localization model comprises a pre-trained UniXcoder with multi-head attention, LSTM, and CNN, and the defect localization model includes three layers: an encoding layer, a bidirectional matching layer, and an inference layer based on key information extraction; wherein:
[0216] The encoding layer uses a pre-trained UniXcoder to encode the input Context, Question, and Answer.
[0217] The bidirectional matching layer uses Bi-LSTM to perform bidirectional information matching on the UniXcoder encoding output, so as to enable better semantic and context-related information interaction between sequence pairs;
[0218] The inference layer uses a CNN-based architecture to extract locally relevant information from the output of the bidirectional matching layer as global information aggregation.
[0219] According to the system of the second aspect of the invention, for the coding layer:
[0220] Given an input sample, the input sample includes code fragments C = {c1, c2, ..., C} i Problem Q = {q1, q2, ..., q} j} and multiple candidate defect code flows A = {a1, a2, ..., a k The coding layer is used to make the correct selection from the candidate defect code stream;
[0221] Where c i q j and a k Let each be the nth word in the paragraph, question, and candidate defect code streams. Use specific tags to concatenate all tags in C, Q, and A into a new sequence:
[0222] [[CLS],C,[SEP],Q,[SEP],A,[SEP]]
[0223] Wherein, [CLS] represents a special marker used for classification, and [SEP] represents a separator;
[0224] By summing the embeddings of the corresponding code tokens and positions from UniXcoder, each token is initialized with a vector and encoded as a hidden state. The resulting encoded output is [H]. CLS H C H Q H A ], H C H Q H A This is the output from the last hidden layer sequence of UniXcoder, used for information exchange through matching.
[0225] According to the system of the second aspect of the invention, for the bidirectional matching layer:
[0226] Multi-head attention and bidirectional LSTM are deployed in the bidirectional matching layer to process the encoded output of the coding layer;
[0227] For Context, two attention weights are calculated to capture the information interaction between Question and Answer: Context-Question attention. and Context-Answer Attention The calculation methods are as follows:
[0228]
[0229]
[0230]
[0231] Among them W c b c Represents the learning parameters, other attention weights and Obtained through the corresponding calculation method;
[0232] Bi-LSTM obtains context-related information from the bidirectional interaction results as the final interaction information between the two input sequences. For Context and Question, the interaction information is calculated as follows:
[0233]
[0234] Where [:] represents the concatenation operation, used to join two vectors;
[0235] The other two interactive information Obtained through the corresponding calculation method;
[0236] All information interactions will be aggregated by connecting three interaction results:
[0237]
[0238] According to the system of the second aspect of the present invention, for the inference layer:
[0239] After obtaining the interactive results, a CNN-based architecture is used to effectively extract key local information to distinguish the correct defective code stream from other candidate defective code streams. The calculation method is shown in the following formula:
[0240] F Pool =MaxPool(Conv1d(F Al ))
[0241] F = F Pool .Max(1)
[0242] This includes a 1D convolutional layer (Conv1d), a regular max-pooling layer (MaxPool), and a column max-pooling layer (Max). Max pooling operations obtain the maximum value of all elements in a certain dimension of the tensor, F. PoolThe dimensions are [batch size, maximum sequence length, number of hidden layer units], where the index 1 of Max indicates the dimension of sequence length, and F is used as a global vector representation of Context, Question, and specific candidate Answer sets;
[0243] For the classification task, each candidate defect code stream A i The loss is calculated as follows:
[0244]
[0245] The actual defect code stream is obtained after passing through a linear classifier as the localization result.
[0246] A third aspect of this invention discloses an electronic device. The electronic device includes a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, it implements the steps of the automatic software source code defect location method described in the first aspect of this disclosure.
[0247] Figure 5 This is a structural diagram of an electronic device according to an embodiment of the present invention, such as... Figure 5 As shown, the electronic device includes a processor, memory, communication interface, display screen, and input device connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The communication interface is used for wired or wireless communication with external terminals; wireless communication can be achieved through Wi-Fi, carrier networks, Near Field Communication (NFC), or other technologies. The display screen can be an LCD screen or an e-ink screen. The input device can be a touch layer covering the display screen, buttons, a trackball, or a touchpad mounted on the device's casing, or an external keyboard, touchpad, or mouse.
[0248] Those skilled in the art will understand that Figure 5 The structure shown is merely a structural diagram of the part related to the technical solution of this disclosure and does not constitute a limitation on the electronic device to which the solution of this application is applied. The specific electronic device may include more or fewer components than shown in the figure, or combine certain components, or have different component arrangements.
[0249] A fourth aspect of this invention discloses a computer-readable storage medium. The computer-readable storage medium stores a computer program, which, when executed by a processor, implements the steps of the automatic software source code defect location method described in the first aspect of this disclosure.
[0250] In summary, this invention can automatically locate defects in source code containing defects without human intervention; this invention has high location accuracy; this invention achieves early security assurance in the software development and design phase.
[0251] Please note that the technical features of the above embodiments can be combined arbitrarily. For the sake of brevity, not all possible combinations of the technical features in the above embodiments have been described. However, as long as the combination of these technical features does not contradict each other, it should be considered within the scope of this specification. The above embodiments only illustrate several implementation methods of this application, and their descriptions are relatively specific and detailed, but they should not be construed as limiting the scope of the invention patent. It should be pointed out that for those skilled in the art, several modifications and improvements can be made without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this patent application should be determined by the appended claims.
Claims
1. A method for automatically locating defects in software source code, characterized in that: The method is based on an automatic software source code defect localization system to automatically locate defects in software source code. The automatic software source code defect localization system includes a defect localization dataset construction module and a defect localization model. The defect localization dataset construction module includes a basic dataset construction module and a data augmentation strategy. The defect localization model includes a precoding module and a defect localization module. The method includes: Step S1: Call the defect localization dataset construction module to convert the function code containing defects into a dataset suitable for defect localization tasks; The basic dataset construction module converts the defective function code into MRC-formatted data as the basic dataset. The basic dataset is then augmented using the data augmentation strategy to obtain the dataset suitable for the defect localization task. The MRC-formatted data includes four fields: ID, Context, Question, and Answers. Step S2: Call the defect localization model to perform defect localization on the transformed dataset suitable for the defect localization task, so as to obtain the defect localization result; The precoding module performs embedded encoding on samples in the dataset suitable for the defect localization task to obtain an encoded dataset, and the defect localization module performs defect localization based on the encoded dataset. In step S1, the basic dataset is augmented using the data augmentation strategy to obtain the dataset suitable for the defect localization task; wherein: The data augmentation strategy involves selecting and expanding candidate defect code streams in the base dataset; The selection strategy for candidate defect code streams includes: selecting a code stream containing at least 2 lines of code from the initial candidate defect code streams based on the length requirement of each candidate defect code stream, wherein the code stream contains at least a Source line and a Sink line; and ensuring that the difference between the number of lines of code in each candidate defect code stream and the number of lines of code in the real label defect code stream does not exceed 1 based on the length of the real label defect code stream. The generation strategy for the candidate defect code stream includes: selecting a single defect code stream with fewer lines than the real labeled defect code stream from the initial candidate defect code stream; randomly selecting several lines from the real labeled defect code stream and adding them to the single defect code stream to form a new available candidate defect code stream; and performing a simple line order transformation on all candidate defect code streams. In step S2, the defect localization model consists of a pre-trained UniXcoder with multi-head attention, LSTM, and CNN. The defect localization model includes three layers: an encoding layer, a bidirectional matching layer, and an inference layer based on key information extraction; wherein: The encoding layer uses a pre-trained UniXcoder to encode the input Context, Question, and Answer. The bidirectional matching layer uses Bi-LSTM to perform bidirectional information matching on the UniXcoder encoding output, so as to enable better semantic and context-related information interaction between sequence pairs; The inference layer uses a CNN-based architecture to extract locally relevant information from the output of the bidirectional matching layer as global information aggregation.
2. The method for automatically locating software source code defects according to claim 1, characterized in that, In step S1, the basic dataset construction module converts the defective function code into the MRC-formatted data as the basic dataset; wherein: Id represents the index number of each sample; Context represents the source code function containing the defect; Question indicates whether the Context contains a defective code flow from Source to Sink; Where Source represents the data source input from an external source, and Sink represents the internal function used for memory operations; The answer contains multiple candidate code streams, with the actual defective code stream serving as the real label; The CodeQL tool is used to perform defect scanning on the Context to obtain the corresponding defect detection report. Each record in the defect detection report contains the Context, the Source-Sink pair, and one or more code streams containing defects. One of the code streams containing defects is added to the Answer as a real label. Input the Context and Source-Sink pairs into the Joern tool to extract other candidate defect code streams. The Joern tool is used to generate all reachable code streams from a given Source-Sink pair.
3. The method for automatically locating software source code defects according to claim 2, characterized in that, In step S2, for the coding layer: Given an input sample, the input sample includes a code snippet C= { c 1, c 2,…, C i },question Q ={ q 1, q 2,…, q j } and multiple candidate defect code streams A ={ a 1, a 2, …, a k The coding layer is used to make the correct selection from the candidate defect code stream; in c i , q j and a k These are the first three segments of the code flow: snippet, issue, and candidate defect. n Each word, using specific tags C , Q and A All tags are concatenated into a new sequence: [[CLS], C ,[SEP], Q ,[SEP], A ,[SEP]] Wherein, [CLS] represents a special marker used for classification, and [SEP] represents a separator; By summing the embeddings of the corresponding code tokens and positions from UniXcoder, each token is initialized with a vector and encoded as a hidden state. The resulting encoded output is [ H CLS , H C , H Q , H A ], H C , H Q , H A This is the output from the last hidden layer sequence of UniXcoder, used for information exchange through matching.
4. The method for automatically locating software source code defects according to claim 3, characterized in that, In step S2, for the bidirectional matching layer: Multi-head attention and bidirectional LSTM are deployed in the bidirectional matching layer to process the encoded output of the coding layer; For Context, two attention weights are calculated to capture the information interaction between Question and Answer: Context-Question attention. and Context-Answer Attention The calculation methods are as follows: in W c , b c Represents the learning parameters, other attention weights (Question-Context) (Question-Answer) (Answer-Context) and The (Answer-Question) is obtained through the corresponding calculation method; Bi-LSTM obtains context-related information from the bidirectional interaction results as the final interaction information between the two input sequences. For Context and Question, the interaction information is calculated as follows: Where [:] represents the concatenation operation, used to join two vectors; The other two interactive information (Question-Answer) (Context-Answer) is obtained through the corresponding calculation method; All information interactions will be aggregated by connecting three interaction results: 。 5. The method for automatically locating software source code defects according to claim 4, characterized in that, In step S2, for the inference layer: After obtaining the interaction results, a CNN-based architecture is used to effectively extract key local information to distinguish the correct defective code stream from other candidate defective code streams. The calculation method is shown in the following formula: This includes 1-dimensional convolutional layers. Ordinary maximum pooling layer Max pooling layer ; Max Pooling operations retrieve the maximum value of all elements in a certain dimension of a tensor. F Pool The dimensions are [batch size, maximum sequence length, number of hidden layer units], Max Index 1 indicates the dimension of the sequence length, using F As a global vector representation of Context, Question, and a specific set of candidate Answers; For the classification task, each candidate defect code flow A i The loss is calculated as follows: The actual defect code stream is obtained after passing through a linear classifier as the localization result.
6. An automatic software source code defect location system, characterized in that, The software source code defect automatic localization system includes a defect localization dataset construction module and a defect localization model. The defect localization dataset construction module includes a basic dataset construction module and a data augmentation strategy. The defect localization model includes a precoding module and a defect localization module; wherein: The defect localization dataset construction module is invoked to transform the function code containing defects into a dataset suitable for defect localization tasks; The basic dataset construction module converts the defective function code into MRC-formatted data as the basic dataset. The basic dataset is then augmented using the data augmentation strategy to obtain the dataset suitable for the defect localization task. The MRC-formatted data includes four fields: ID, Context, Question, and Answers. The defect localization model is invoked to perform defect localization on the transformed dataset suitable for the defect localization task, so as to obtain the defect localization result; The precoding module performs embedded encoding on samples in the dataset suitable for the defect localization task to obtain an encoded dataset, and the defect localization module performs defect localization based on the encoded dataset. During the process of calling the defect localization dataset construction module, the basic dataset undergoes data augmentation using the data augmentation strategy to obtain the dataset suitable for the defect localization task; wherein: The data augmentation strategy involves selecting and expanding candidate defect code streams in the base dataset; The selection strategy for candidate defect code streams includes: selecting a code stream containing at least 2 lines of code from the initial candidate defect code streams based on the length requirement of each candidate defect code stream, wherein the code stream contains at least a Source line and a Sink line; and ensuring that the difference between the number of lines of code in each candidate defect code stream and the number of lines of code in the real label defect code stream does not exceed 1 based on the length of the real label defect code stream. The generation strategy for the candidate defect code stream includes: selecting a single defect code stream with fewer lines than the real labeled defect code stream from the initial candidate defect code stream; randomly selecting several lines from the real labeled defect code stream and adding them to the single defect code stream to form a new available candidate defect code stream; and performing a simple line order transformation on all candidate defect code streams. The defect localization model consists of a pre-trained UniXcoder with multi-head attention, LSTM, and CNN. The model comprises three layers: an encoding layer, a bidirectional matching layer, and an inference layer based on key information extraction; wherein: The encoding layer uses a pre-trained UniXcoder to encode the input Context, Question, and Answer. The bidirectional matching layer uses Bi-LSTM to perform bidirectional information matching on the UniXcoder encoding output, so as to enable better semantic and context-related information interaction between sequence pairs; The inference layer uses a CNN-based architecture to extract locally relevant information from the output of the bidirectional matching layer as global information aggregation.
7. An electronic device, characterized in that, The electronic device includes a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, it implements the automatic software source code defect location method according to any one of claims 1-5.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, which, when executed by a processor, implements the automatic software source code defect location method according to any one of claims 1-5.
Citation Information
Patent Citations
Software source code defect detection method and system, electronic equipment and storage medium
CN113641586A
Software defect positioning method based on enhanced embedded vector semantic representation
CN116302953A