Method and device for constructing code complex defect detection model
By constructing a predicate event relationship diagram and graph attention network combined with a large language model, the accuracy and stability of complex defect detection in the prior art are solved, and efficient identification and correction of complex code defects are achieved.
Patent Information
- Application Number
- CN202510984911.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-17
- Publication Date
- 2025-08-15
- Estimated Expiration
- 2045-07-17
AI Technical Summary
The existing technology is difficult to accurately identify complex context-dependent and semantic-driven code defects. The false positive rate of static analysis tools is high, and the large language model performs unstable when detecting fine-grained defects, ignoring the complex data flow dependencies in the code.
Through the knowledge mutual distillation algorithm combined with static analysis tools and large language models, a predicate event relationship diagram is constructed and a risk path subgraph is output using the graph attention network, and a large language model reasoning is guided with the help of defect rule chains. The dual-stream encoder structure is used to guide interactive guidance and supervise fine-tuning to form a complex defect detection model.
It improves the accuracy and interpretability of complex defect detection, can identify complex context dependencies and semantic-driven defects, reduces the misjudgment rate, and improves the detection ability of the model.
Smart Images

Figure CN120492311A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of code analysis technology, and in particular to a method and device for constructing a complex code defect detection model. Background Art
[0002] Code defect detection refers to the use of static or dynamic analysis techniques to identify possible errors, vulnerabilities, security updates, or issues that do not comply with coding standards in software code in order to improve code quality and reliability. It is a key link in the software development or testing process.
[0003] In related technologies, static analysis tools or static extraction tools are often used for defect detection, which relies on manually defined rule template detection and can better cover shallow grammatical problems, such as uninitialized variables, explicit null pointer dereferences, etc. However, the above technical means cannot accurately identify defect scenarios with complex context dependencies and semantics-driven, such as null pointer dereferences under complex conditions, concurrency conflicts, and logical deadlocks. Such defects are called complex defects, and static tools have a high false alarm rate, requiring a lot of manual confirmation. With the rapid development of artificial intelligence, existing large language models can master relatively rich grammatical and semantic knowledge through training, but existing models have poor reasoning capabilities for difficult-to-understand code texts, and are extremely unstable when detecting fine-grained defects, making it difficult to capture complex data flow dependencies in the code. In addition, some shallow grammatical problems can assist in locating potential deeper complex defects, but the existing technology ignores this mutual influence relationship.
[0004] Based on the above analysis of the development status of this technology field, the existing technology lacks a solution that can extract predicate event relationship graphs based on static extraction tools, use graph neural networks to capture complex relationships and output risk path subgraphs, use them together with code snippets as input instructions for large language models, and use defect rule chains to guide the reasoning effect of large language models. Summary of the Invention
[0005] The purpose of the present invention is to provide a method and device for constructing a code complex defect detection model, aiming to solve the above-mentioned problems in the prior art.
[0006] According to a first aspect of an embodiment of the present invention, a method for constructing a code complex defect detection model is provided, comprising: We conduct knowledge distillation between static analysis tools and untrained large language models to build a dataset that includes code snippets and complex defect labels. Use static extraction tools to extract events and relationships in code snippets, build a predicate event relationship graph, and use the predicate event relationship graph and complex defect labels to train a graph attention network. The trained graph attention network then outputs a risk path subgraph. Risk path subgraphs and code snippets are used as input instructions for the large language model. A two-stream encoder structure is used for interactive guidance. The defect rule chain guides model reasoning, and supervised fine-tuning is used to adjust the large language model parameters. The trained large language model outputs complex defect results. The trained graph attention network and the large language model are jointly constructed into a complex defect detection model.
[0007] According to a second aspect of an embodiment of the present invention, there is provided an apparatus for constructing a code complex defect detection model, comprising: The mutual distillation module is used to perform knowledge mutual distillation based on static analysis tools and untrained large language models, and build a dataset including code snippets and complex defect labels; The graph processing module uses static extraction tools to extract events and relationships in code snippets, constructs a predicate-event relationship graph, and uses the predicate-event relationship graph and complex defect labels to train a graph attention network. The trained graph attention network then outputs a risk path subgraph. The inference training module uses risk path subgraphs and code snippets as input instructions for the large language model. It uses a dual-stream encoder structure for interactive guidance, guides model reasoning with the help of defect rule chains, and uses supervised fine-tuning to adjust the large language model parameters. The trained large language model then outputs complex defect results. A building block for combining the trained graph attention network and a large language model into a complex defect detection model.
[0008] According to a third aspect of an embodiment of the present invention, an electronic device is provided, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program, when executed by the processor, implements the steps of the method for constructing a code complex defect detection model as provided in the first aspect of the present disclosure.
[0009] According to a fourth aspect of an embodiment of the present invention, a computer-readable storage medium is provided, on which an implementation program for information transmission is stored. When the program is executed by a processor, the steps of the method for constructing a code complex defect detection model provided in the first aspect of the present disclosure are implemented.
[0010] The technical solution provided by the embodiment of the present invention includes the following beneficial effects: using the knowledge mutual distillation algorithm to fully utilize the characteristics of traditional tools and large language models to efficiently generate high-quality and diverse training data; constructing a predicate event relationship graph including explicit defect nodes, and using the predicate event relationship graph as the input of the graph attention network. While capturing the complex control flow and data flow dependencies in the code, the graph attention network can also dig out deeper design defects from the displayed syntax errors; the risk path subgraph and code snippets output by the graph attention network are used together as the input of the large language model to avoid the pure code input model from misjudging due to the lack of program structure information, or the result input model of the pure static tool analysis lacks semantic understanding; setting a dual-stream decoder to achieve dynamic information complementation through interactive guidance, so that the model can identify complex context dependencies and semantically driven complex defects.
[0011] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] In order to more clearly illustrate one or more embodiments of this specification or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments recorded in this specification. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0013] Figure 1 is a flowchart of a method for constructing a code complex defect detection model according to an embodiment of the present invention; Figure 2 is a schematic diagram of a training framework according to an embodiment of the present invention; Figure 3 2 is a schematic diagram of a device for constructing a code complex defect detection model according to an embodiment of the present invention; Figure 4 is a schematic diagram of an electronic device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0014] In order to enable those skilled in the art to better understand the technical solutions in one or more embodiments of this specification, the technical solutions in one or more embodiments of this specification will be clearly and completely described below in conjunction with the drawings in one or more embodiments of this specification. Obviously, the described embodiments are only part of the embodiments of this specification, not all of the embodiments. Based on one or more embodiments of this specification, all other embodiments obtained by ordinary technicians in this field without making creative efforts should fall within the scope of protection of this document.
[0015] Method Example According to an embodiment of the present invention, a method for constructing a code complex defect detection model is provided. Figure 1 FIG. 1 is a flow chart of a method for constructing a code complex defect detection model according to an embodiment of the present invention. Figure 1 As shown, the method for constructing a code complex defect detection model according to an embodiment of the present invention specifically includes: In step S110, knowledge distillation is performed based on the static analysis tool and the untrained large language model to construct a dataset including code snippets and complex defect labels, specifically including: Get original code snippets from large code repositories; Input the original code snippet into a traditional static analysis tool for testing. The static analysis tool uses Coverity or Fortify tools to obtain an initial report. The same code snippets are then fed into a baseline large language model that has not been trained with SFT, leveraging its initial semantic understanding capabilities to analyze the code. The large language model uses a 7B parameter model based on the Transformer architecture, where 7B refers to the parameter size. Specifically, either the LLaMA model or the DeepSeek model can be used as the baseline model. During the knowledge inter-distillation phase, only the text stream encoder is enabled in the large language model, using a common initial structure, because the inter-distillation phase processes code snippets. Knowledge mutual distillation is achieved through knowledge transfer. Static analysis tools are used to correct the shortcomings of untrained large language models in identifying grammatical errors. Untrained large language models are also used to correct the shortcomings of static analysis tools in identifying complex semantic errors. It should be noted that error correction in knowledge mutual distillation is not equivalent to parameter adjustment during training, but focuses more on correcting the results. The purpose of the interaction between the two is to build a high-quality defect detection dataset to cover rare defect types.
[0016] After repeated iterations of correcting the knowledge mutual distillation, the detection results output by the static analysis tool are used as the first label, and the detection results output by the untrained large language model for the same code snippet are used as the second label; If both the first and second label results are judged as defects, the result with higher confidence is selected as the complex defect label and marked as a positive sample; if one of the first or second label results is judged as a defect and the defect is confirmed to exist after verification, it is also marked as a positive sample. Otherwise, other cases are marked as negative samples. That is, if both the first and second label results are judged as non-defective, or if one of the first or second label results is judged as a defect but the sample is not defective after verification and confirmation, it is a negative sample; the final determined first or second label is defined as a complex defect label; The generated positive and negative samples and their corresponding complex defect labels are used as datasets for subsequent training.
[0017] In step S120, a static extraction tool is used to extract events and relationships in the code snippet, and a predicate event relationship graph is constructed. The predicate event relationship graph and complex defect labels are used to train a graph attention network. The trained graph attention network outputs a risk path subgraph, which specifically includes: Static extraction tools built using AST parsing or CFG extract events and relationships in code snippets: Extract events from code snippets, including execution, copy, and conditional judgment, as general nodes. Events that trigger preset specific types of defects are extracted as explicit defect nodes. Preset specific types of defects refer to all types of defects that existing static extraction tools can accurately identify. Based on user-defined settings based on actual needs, the sequential calls or causal relationships between events are used as paths between nodes to form an initial relationship graph. Compared with static analysis tools, AST tools or CFG tools focus more on extracting code structure and can also parse relatively simple explicit defect nodes at the syntactic level, such as uninitialized variables or explicit null pointer dereferences. In addition to the obvious calling relationship, causal relationships can be roughly understood as trigger conditions, similar to the condition under which an event will also occur when certain events occur. The same event may trigger the generation of paths through different events. A path that can generate this event is considered a trigger branch, and the description of this event should include all its trigger branches. Preferably, causal relationships can also introduce symbolic execution or taint analysis technology to track variable state changes and data flow. Further optimization based on the completion of the initial relationship diagram: If the node type is an explicit defect node, then backtrack from that node layer by layer in the code snippet until the initial state or boundary in the code snippet is reached, and the complete backtracking path corresponding to the explicit defect node is added to the initial relationship graph. If different events are caused by value changes during program execution, the state value changes are added to the corresponding nodes in the initial relationship graph. After the supplementation is completed, a complete relationship graph is formed, and the complete relationship graph is verified. The verification specifically includes semantic retrieval and supplementation to improve the accuracy and completeness of the relationship description and obtain a predicate event relationship graph.
[0018] The graph attention network (GAT) is used to aggregate multi-hop neighbor information to capture long-term dependencies. Multi-hop neighbors refer to nodes that can be reached indirectly by traversing multiple hops from the current node. After node features are aggregated, high-risk nodes for preliminary judgment of complex defect detection are output. Starting from the high-risk nodes, along the path with high weight values assigned by the attention mechanism, a risk path subgraph is output.
[0019] In step S130, the risk path subgraph and code snippets are used as input instructions for the large language model. The dual-stream encoder structure is used for interactive guidance, the defect rule chain is used to guide model reasoning, and the large language model parameters are adjusted using supervised fine-tuning. The trained large language model outputs complex defect results, specifically including: The risk path subgraph and code snippet are used together as input instructions. In the embodiment of the present invention, in order to facilitate the construction of a standardized input interface, the defect rule chain is also used as part of the input instruction. Other built-in solutions in the field are also within the scope of protection of the embodiment of the present invention. To achieve dynamic information complementarity between risk path subgraphs and code snippets, the text stream encoder of the original large language model is replaced with a dual-stream encoder structure: Input the risk path subgraph into the lightweight graph encoder to convert the risk path subgraph into a structured graph feature sequence consisting of text and arrows; input the code snippet into the text stream encoder to output a text feature sequence; And add an interactive guidance module after the encoding layer of the large language model: The node features in the graph feature sequence are associated with the corresponding positions in the text feature sequence. Based on the contextual description of the corresponding information at the associated positions in the text feature sequence, the graph feature sequence is tested to see if it ignores hidden conditions in the code snippet. In other words, the graph feature sequence and the text feature sequence are associated through events. This association helps to quickly locate the corresponding position of the event in the code text. If the graph attention network has an error in the risk path subgraph due to incomplete graph construction, the code context can be used to correct it. In the embodiment of the present invention, the hidden condition refers to the missing information that causes the risk path subgraph to be constructed incorrectly, such as missing pre-restriction information or call relationship; The cross-modal attention mechanism is used to fuse features as interactive guidance text. If no hidden conditions are found, the image feature sequence and the text feature sequence are fused; otherwise, the image feature sequence, the text feature sequence and the hidden conditions are fused.
[0020] Obtaining a manually defined defect rule chain based on domain knowledge transformation. The defect rule chain is a natural language judgment step for determining complex defects. The defect rule chain is used as a verification constraint rule for large language model reasoning. In this embodiment of the present invention, there is no restriction on the way the defect rule chain guides reasoning, and any feasible method can be used. Since the defect rule chain is already part of the input instruction, obtaining here refers to obtaining the encoded defect rule chain. Defect rule chaining enables large language models to not only rely on implicit patterns learned from the corpus when performing complex defect detection, but also explicitly follow preset logical rules, thereby improving detection accuracy and explainability.
[0021] Use SFT supervised fine-tuning to train a large language model and provide it with the desired output format using a small number of examples. Based on the interactive guidance text, the defect rule chain is used to generate the reasoning process and the initial prediction results and descriptions of complex defects. This process reflects the model's understanding of the input code. The loss is calculated based on the difference between the initial prediction results and the actual complex defect labels, and the large language model parameters are updated through backpropagation. The training process is iterative until the large language model converges or reaches a preset number of training rounds.
[0022] After SFT training, the large language model can significantly improve its performance in detecting complex code defects, and learn to combine the defect rule thinking chain with its own semantic understanding ability to form a more powerful defect detection capability.
[0023] In step S140, the trained graph attention network and the large language model are jointly constructed into a complex defect detection model.
[0024] The method further comprises: In step S150 , a complex defect detection model is used to identify complex defect results of the verification set code.
[0025] Taking the detection of "conditional null pointer dereference" in a C / C++ code as an example, the code snippet to be detected is as follows: #include <iostream>#include <string> int main(int argc, char argv[ ]) { std::string ptr_str = nullptr; if (argc>1) { ptr_str = new std::string(argv[1]); } std::cout<<"Length: "<<ptr_str-> length()< <std::endl; if (ptr_str != nullptr) { delete ptr_str; ptr_str = nullptr; } return 0; } For the above code snippet, the events extracted by the predicate event graph include: 1. argc>1 (conditional judgment, indicating whether the command line parameter exists); 2. ptr_str = new std::string(argv[1]) (dynamically allocate memory and point the pointer ptr_str to a valid object); 3. ptr_str->length() (member function call / pointer dereference operation, which may trigger a null pointer dereference); Static extraction tools may not be able to detect that ptr_str->length() is a complex defect because this statement is not an explicit syntax defect. However, to facilitate the description of concepts such as the risk path of this solution, in this embodiment of the present invention, events 1 and 2 are general events, and event 3 is defined as an explicit defect node. It should be noted that even if event 3 is also a general event, it will not affect the output results of the large language model. The relationships in the predicate event graph can be described as: 1. If argc <= 1 (that is, no command line parameters or only one program name parameter), ptr_str remains in the nullptr state; 2. If ptr_str is nullptr and ptr_str->length() is executed, a null pointer dereference vulnerability is triggered; The recorded status value changes include the status of the ptr_str pointer, such as nullptr or non-nullptr; The defect rule thinking chain is defined as follows: 1. Check if the pointer ptr_str is initialized; 2. Check whether the pointer ptr_str has been null-checked before use (i.e. before calling ptr_str->length()); 3. Analyze the assignment path of ptr_str to determine whether ptr_str is dereferenced even though it is nullptr.
[0026] The graph feature sequence of the risk path subgraph output by the graph attention network is similar to: "argc<= 1"->"ptr_str == nullptr"->"ptr_str->length()". It interacts with the code snippets and uses the defect rule chain to guide model reasoning. The final reasoning process and detection results are as follows: Reasoning: "According to the instruction, the initialization of the pointer ptr_str is first checked. If the if (argc>1) condition is not met (i.e., the program has no command-line arguments), ptr_str remains in the nullptr state. At line X, at ptr_str->length(), ptr_str is dereferenced. Considering that ptr_str may be nullptr in some paths, it is determined that there is a null pointer dereference risk here." Detection Result: Defect Type: Null Pointer Dereference (located at line Y). Cause: The pointer variable ptr_str may be null when no command line arguments are provided, causing a segmentation fault or abnormal program termination when calling ptr_str->length().
[0027] It should be noted that this detection example is only to assist in understanding the concept, and the implementation effect of the embodiment of the present invention is better in actual complex scenarios.
[0028] The above technical solutions of the embodiments of the present invention are illustrated with reference to the following drawings.
[0029] Figure 2 Schematic diagram of the training framework of an embodiment of the present invention, such as Figure 2 As shown in the figure, the basic training architecture of the complex code defect detection model is demonstrated, including knowledge mutual distillation to construct a training dataset; the extracted predicate event relationship graph and code snippets are used together to train the graph attention network, and the risk path subgraph is output; the risk path subgraph and code snippets are then used as input instructions for the dual-stream decoding large language model, and the model reasoning is guided under the preset defect rule chain. After the supervised fine-tuning iteration is completed, the complex defect results are obtained.
[0030] In summary, in response to the existing problems, the method for constructing a complex defect detection model for code invented in this paper uses the knowledge mutual distillation algorithm to fully utilize the characteristics of traditional tools and large language models to efficiently generate high-quality and diverse training data; constructs a predicate event relationship graph including explicit defect nodes, and uses the predicate event relationship graph as the input of the graph attention network. While capturing the complex control flow and data flow dependencies in the code, the graph attention network can also dig deeper design defects from the displayed syntax errors. In addition, the graph attention network aggregates multi-hop neighbor information in the processing process to capture long dependencies and can automatically output risk path subgraphs; risk path Path graphs and code snippets are used together as input to the large language model, avoiding possible misjudgments by the pure code input model due to lack of program structure information, or the lack of semantic understanding of the input model due to the result of pure static tool analysis. The dual-stream decoder is set up for independent processing and dynamic information complementation is achieved through interactive guidance, enabling the model to identify complex contextual dependencies and semantically driven complex defects. In addition, the defect rule chain is used to guide model reasoning, effectively injecting existing rule knowledge into the large language model, enabling it to have the ability to perform structured reasoning and accurately identify fine-grained defects. The supervised fine-tuning (SFT) method is used to adjust the parameters of the large language model to form a powerful complex defect detection capability.
[0031] Device embodiment According to an embodiment of the present invention, a device for constructing a code complex defect detection model is provided. Figure 3 Schematic diagram of a device for constructing a code complex defect detection model according to an embodiment of the present invention. Figure 3 As shown, the device for constructing a code complex defect detection model according to an embodiment of the present invention specifically includes: The mutual distillation module 30 is used to perform knowledge mutual distillation based on static analysis tools and an untrained large language model to construct a dataset including code snippets and complex defect labels. Specifically, it is used to: Knowledge distillation is achieved through knowledge transfer. Static analysis tools are used to correct the shortcomings of untrained large language models in identifying grammatical errors. Static analysis tools such as Coverity or Fortify are used for static analysis, and the large language model uses a 7-byte parameter model based on the Transformer architecture. During the knowledge distillation phase, only the text stream encoder is enabled. After the knowledge mutual distillation iteration is completed, the detection result output by the static analysis tool is used as the first label, and the detection result output by the untrained large language model for the same code snippet is used as the second label; If both the first label and the second label are judged to be defects, the result with higher confidence is selected as the complex defect label and marked as a positive sample; if one of the first label or the second label is judged to be a defect, and it is confirmed to exist after review and confirmation, it is also marked as a positive sample. Otherwise, other cases are marked as negative samples.
[0032] The graph processing module 32 is used to extract events and relationships in code snippets using static extraction tools, construct a predicate event relationship graph, and use the predicate event relationship graph and complex defect labels to train a graph attention network. The trained graph attention network outputs a risk path subgraph. Specifically, it is used to: Static extraction tools built using AST parsing or CFG extract events and relationships in code snippets: Extract events including execution, copying, and conditional judgment in code snippets as general nodes, extract events that trigger preset specific types of defects as explicit defect nodes, and use the sequential calls or causal relationships between events as paths between nodes to form an initial relationship graph; If the node type is an explicit defect node, then backtrack from that node layer by layer in the code snippet until the initial state or boundary in the code snippet is reached, and the complete backtracking path corresponding to the explicit defect node is added to the initial relationship graph. If different events are caused by value changes during program execution, the state value changes are added to the corresponding nodes in the initial relationship graph. After the supplementation is completed, a complete relationship graph is formed, and after verifying the complete relationship graph, a predicate event relationship graph is obtained.
[0033] The graph attention network (GAT) aggregates multi-hop neighbor information to capture long-term dependencies. After node feature aggregation, high-risk nodes for preliminary judgment of complex defect detection are output. Starting from the high-risk nodes, the risk path subgraph is output along the path with high weight values assigned by the attention mechanism.
[0034] The inference training module 34 is used to use the risk path subgraph and code snippets as input instructions for the large language model. It uses a dual-stream encoder structure for interactive guidance, guides model inference with the help of defect rule chains, and uses supervised fine-tuning to adjust the parameters of the large language model. The trained large language model outputs complex defect results. Specifically, it is used to: Input the risk path subgraph into the lightweight graph encoder to convert the risk path subgraph into a structured graph feature sequence consisting of text and arrows; input the code snippet into the text stream encoder to output a text feature sequence; Add an interactive guidance module after the encoding layer of the large language model: The node features included in the graph feature sequence in the lightweight graph encoder are associated with the corresponding positions in the text stream encoder, so that the graph feature sequence can dynamically query the corresponding information in the text feature sequence. According to the context description of the corresponding information in the text feature sequence, it is tested whether the graph feature sequence ignores the hidden conditions in the code snippet. The cross-modal attention mechanism is used to fuse features as interactive guidance text. If no hidden conditions are found, the image feature sequence and the text feature sequence are fused; otherwise, the image feature sequence, the text feature sequence and the hidden conditions are fused.
[0035] Obtaining manually defined defect rule chains based on domain knowledge transformation. The defect rule chains are natural language judgment steps for determining complex defects. These defect rule chains are used as verification constraint rules for large language model reasoning. Use SFT supervised fine-tuning to train the large language model and provide the desired output format. Utilize the defect rule chain based on the interactive guidance text to generate the reasoning process and initial prediction results and descriptions of complex defects. Calculate the loss based on the difference between the initial prediction results and the actual complex defect labels, and update the large language model parameters through backpropagation.
[0036] The construction module 36 is used to construct the trained graph attention network and the large language model into a complex defect detection model.
[0037] The device further comprises: The application module 38 is used to use the complex defect detection model to identify complex defect results of the verification set code.
[0038] In summary, in response to the existing problems, this invention proposes a device for constructing a complex defect detection model for code. It uses a knowledge mutual distillation algorithm to fully utilize the characteristics of traditional tools and large language models to efficiently generate high-quality and diverse training data. It constructs a predicate event relationship graph including explicit defect nodes, and uses the predicate event relationship graph as the input of the graph attention network. While capturing the complex control flow and data flow dependencies in the code, the graph attention network can also dig deeper design defects from displayed syntax errors. In addition, the graph attention network aggregates multi-hop neighbor information in the processing process to capture long dependencies and can automatically output risk path subgraphs. Risk path Path graphs and code snippets are used together as input to the large language model, avoiding possible misjudgments by the pure code input model due to lack of program structure information, or the lack of semantic understanding of the input model due to the result of pure static tool analysis. The dual-stream decoder is set up for independent processing and dynamic information complementation is achieved through interactive guidance, enabling the model to identify complex contextual dependencies and semantically driven complex defects. In addition, the defect rule chain is used to guide model reasoning, effectively injecting existing rule knowledge into the large language model, enabling it to have the ability to perform structured reasoning and accurately identify fine-grained defects. The supervised fine-tuning (SFT) method is used to adjust the parameters of the large language model to form a powerful complex defect detection capability.
[0039] Electronic device embodiment Figure 4 is a schematic diagram of an electronic device according to an embodiment of the present invention. Electronic device 400 may include at least one processor 410 and memory 420. Processor 410 can execute instructions stored in memory 420. Processor 410 is communicatively coupled to memory 420 via a data bus. In addition to memory 420, processor 410 may also be communicatively coupled to input device 430, output device 440, and communication device 450 via the data bus.
[0040] Processor 410 may be any conventional processor, such as a commercially available CPU. The processor may also include a graphics processor (GPU), a field programmable gate array (FPGA), a system on chip (SOC), an application specific integrated circuit (ASIC), or a combination thereof.
[0041] The memory 420 may be implemented by any type of volatile or non-volatile memory device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk, or optical disk.
[0042] In an embodiment of the present disclosure, executable instructions are stored in the memory 420, and the processor 410 can read the executable instructions from the memory 420 and execute the instructions to implement all or part of the steps of the method for constructing a code complex defect detection model in any of the above exemplary embodiments.
[0043] Computer readable storage medium embodiments In addition to the above-mentioned methods and devices, the exemplary embodiments of the present disclosure may also be a computer program product or a computer-readable storage medium storing the computer program product. The computer product includes computer program instructions, and the computer program instructions can be executed by a processor to implement all or part of the steps described in the method for constructing a code complex defect detection model in any of the above-mentioned exemplary embodiments.
[0044] The computer program product may be written in any combination of one or more programming languages to implement the program code for performing the operations of the embodiments of the present application, including object-oriented programming languages such as Java, C++, etc., as well as conventional procedural programming languages such as "C" or similar programming languages and scripting languages (e.g., Python). The program code may be executed entirely on the user's computing device, partially on the user's computing device, as a standalone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server.
[0045] Computer-readable storage media may be any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium may include, for example, but is not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices, or components, or any combination thereof. More specific examples of readable storage media include: static random access memory (SRAM) with one or more wires electrically connected, electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic or optical disk, or any suitable combination thereof.
[0046] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the above embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.< / string> < / iostream>
Claims
1. A method for constructing a complex code defect detection model, characterized in that: include: We conduct knowledge distillation between static analysis tools and untrained large language models to build a dataset that includes code snippets and complex defect labels. Using a static extraction tool to extract events and relationships in the code snippet, constructing a predicate event relationship graph, and using the predicate event relationship graph and the complex defect labels to train a graph attention network, outputting a risk path subgraph through the trained graph attention network; The risk path subgraph and the code snippet are used as input instructions for the large language model. A dual-stream encoder structure is used for interactive guidance. The defect rule chain is used to guide model reasoning. The parameters of the large language model are adjusted using supervised fine-tuning. The trained large language model then outputs complex defect results. The trained graph attention network and the large language model are jointly constructed into a complex defect detection model.
2. The method according to claim 1, characterized in that The method further comprises: The complex defect detection model is used to identify complex defect results of the verification set code.
3. The method according to claim 1, characterized in that The process of performing knowledge distillation between static analysis tools and an untrained large language model to construct a dataset containing code snippets and complex defect labels specifically includes: Knowledge mutual distillation is achieved through knowledge transfer. Static analysis tools are used to correct the shortcomings of untrained large language models in identifying grammatical errors. The shortcomings of static analysis tools in identifying complex semantic errors are corrected using untrained large language models. The static analysis tools use Coverity or Fortify tools, and the large language model uses a 7B parameter model based on the Transformer architecture. In the knowledge mutual distillation stage, only the text stream encoder is enabled. After the knowledge mutual distillation iteration is completed, the detection result output by the static analysis tool is used as the first label, and the detection result output by the untrained large language model for the same code snippet is used as the second label; If both the first label and the second label are judged to be defects, the result with higher confidence is selected as the complex defect label and marked as a positive sample; if one of the first label or the second label is judged to be a defect, and the existence of this defect is confirmed after review and confirmation, it is also marked as a positive sample. Otherwise, other cases are marked as negative samples.
4. The method according to claim 1, wherein The extracting of events and relationships in the code snippet using a static extraction tool and constructing a predicate event relationship graph specifically includes: Static extraction tools built using AST parsing or CFG extract events and relationships in code snippets: Extract events including execution, copying, and conditional judgment in code snippets as general nodes, extract events that trigger preset specific types of defects as explicit defect nodes, and use the sequential calls or causal relationships between events as paths between nodes to form an initial relationship graph; If the node type is an explicit defect node, then backtrack from this node layer by layer in the code snippet until the initial state or boundary in the code snippet is reached, and the complete backtracking path corresponding to the explicit defect node is added to the initial relationship graph; if different events are caused by value changes during program execution, the state value changes are added to the corresponding nodes in the initial relationship graph; After the supplementation is completed, a complete relationship graph is formed, and after verifying the complete relationship graph, a predicate event relationship graph is obtained.
5. The method according to claim 1, wherein The risk path subgraph output by the trained graph attention network specifically includes: The graph attention network (GAT) aggregates multi-hop neighbor information to capture long dependencies. After node feature aggregation, high-risk nodes for preliminary judgment of complex defect detection are output. Starting from the high-risk nodes, a risk path subgraph is output along the path with high weight values assigned by the attention mechanism.
6. The method according to claim 1, characterized in that The step of using the risk path subgraph and the code snippet as input instructions for a large language model and adopting a dual-stream encoder structure for interactive guidance specifically includes: Input the risk path subgraph into a lightweight graph encoder to convert the risk path subgraph into a structured graph feature sequence consisting of text and arrows; input the code snippet into a text stream encoder to output a text feature sequence; An interactive guidance module is added after the encoding layer of the large language model: the node features in the graph feature sequence are associated with the corresponding positions in the text feature sequence. Based on the contextual description of the corresponding information of the associated positions in the text feature sequence, the graph feature sequence is checked to see whether it ignores the hidden conditions in the code snippet. A cross-modal attention mechanism is used to fuse features as interactive guidance text. If the hidden condition is not found, the graph feature sequence and the text feature sequence are fused; otherwise, the graph feature sequence, the text feature sequence and the hidden condition are fused.
7. The method according to claim 1, characterized in that The method of guiding model reasoning with the defect rule chain and adjusting the parameters of the large language model using supervised fine-tuning specifically includes: Obtaining a manually defined defect rule chain based on domain knowledge transformation, wherein the defect rule chain is a natural language judgment step for judging complex defects, and using the defect rule chain as a verification constraint rule for large language model reasoning; The large language model is trained using the SFT supervised fine-tuning method and the desired output format is provided. Based on the interactive guidance text, the defect rule chain is used to generate the reasoning process and the initial prediction results and descriptions of complex defects. The loss is calculated based on the difference between the initial prediction results and the actual complex defect labels, and the large language model parameters are updated through backpropagation.
8. A device for constructing a code complex defect detection model, characterized in that: include: The mutual distillation module is used to perform knowledge mutual distillation based on static analysis tools and untrained large language models, and build a dataset including code snippets and complex defect labels; A graph processing module is configured to extract events and relationships in the code snippet using a static extraction tool, construct a predicate event relationship graph, train a graph attention network using the predicate event relationship graph and the complex defect labels, and output a risk path subgraph through the trained graph attention network; An inference training module, which uses the risk path subgraph and the code snippet as input instructions for the large language model, adopts a dual-stream encoder structure for interactive guidance, guides model inference with the help of defect rule chains, and uses supervised fine-tuning to adjust the parameters of the large language model. The trained large language model then outputs complex defect results. A building block for combining the trained graph attention network and a large language model into a complex defect detection model.
9. An electronic device, characterized in that: include: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the computer program is executed by the processor, the steps of the method for constructing a code complex defect detection model as claimed in any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium, characterized in that The computer-readable storage medium stores an implementation program for information transmission, and when the program is executed by a processor, the steps of the method for constructing a code complex defect detection model according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Software source code defect detection method and system, electronic equipment and storage medium
CN113641586A
Semi-supervised software defect prediction method based on graph representation learning and knowledge distillation
CN115328782A
Code defect detection method and system based on large model
CN119690512A
Cited By
Metallurgy control method and system, electronic equipment and storage medium
CN120848435A
