A code vulnerability detection method based on large language model reasoning
By introducing patch code and hierarchical preference optimization, the generalization ability and interpretability of large language models in code vulnerability detection are improved, solving the problem of insufficient generalization of existing methods on out-of-distribution data, and achieving higher detection performance and better interpretability.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- UNIV OF ELECTRONICS SCI & TECH OF CHINA
- Filing Date
- 2026-02-13
- Publication Date
- 2026-05-29
AI Technical Summary
Existing code vulnerability detection methods based on large language models have insufficient generalization ability on out-of-distribution data, making it difficult to capture deep vulnerability features and ignoring important information in patch code, resulting in insufficient detection accuracy and interpretability.
By introducing patch code and incorporating the differences before and after vulnerability patching into the model training process through a multi-stage training strategy, an inference augmentation dataset is constructed and fine-tuned. Combined with hierarchical preference optimization, the model's capabilities are improved, and detection results containing vulnerability labels and explanations are generated.
It significantly improves the detection accuracy and interpretability of the model, enhances its generalization ability under unseen data, and achieves high-quality vulnerability detection and interpretation.
Smart Images

Figure CN122113118A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of natural language processing technology, and in particular to a code vulnerability detection method based on large language model reasoning. Background Technology
[0002] In recent years, with the rapid development of Large Language Models (LLMs) in the field of natural language processing, they have demonstrated significant advantages in semantic understanding, context modeling, and reasoning generation. Large-scale pre-trained language models based on the Transformer architecture, through learning on massive amounts of data, can capture complex semantic patterns and achieve breakthroughs in various downstream tasks. These models have been widely applied to tasks such as dialogue systems, code generation, question answering, and logical reasoning, changing the application paradigm of traditional artificial intelligence systems to some extent. In the fields of software engineering and program analysis, researchers have also begun to explore the application of LLMs to code understanding-related tasks. Among these, Code Vulnerability Detection (CVD), as an important technology for ensuring the security of software systems, has received widespread attention. Code vulnerability detection aims to identify potential security flaws in source code, which is of great significance for improving the reliability and security of software systems. Especially in the context of the current widespread use of LLMs for assisted development of software systems, the increasing complexity and diversity of potential vulnerabilities in generated code makes the effective detection of code vulnerabilities a critical problem that urgently needs to be solved.
[0003] With the development of deep learning technology, code vulnerability detection methods have evolved from traditional static and dynamic analysis to automated detection methods based on neural networks. Early research mainly utilized model structures such as recurrent neural networks and graph neural networks to extract vulnerability-related information from the syntactic structure and control flow features of code. Subsequently, pre-trained language models (PLMs) were introduced into code vulnerability detection tasks, with representative models such as CodeBERT, GraphCodeBERT, and UniXcoder. These models were pre-trained on large-scale code corpora, possessing good modeling capabilities for code semantics and achieving good results on multiple vulnerability detection benchmark datasets. Building on this, general large language models (LLMs), with their stronger context understanding and generation capabilities, have been further applied to code vulnerability detection tasks. Some studies have attempted to use LLMs to train on multiple tasks such as vulnerability detection, vulnerability localization, and vulnerability interpretation, improving model performance through instruction fine-tuning and multi-task learning. However, existing LLM-based code vulnerability detection methods still face a significant technical bottleneck: insufficient generalization ability of the models on out-of-distribution data.
[0004] The root cause of these problems lies in the generally limited scale of labeled data in current code vulnerability detection tasks, while vulnerability patterns exhibit high diversity across different projects, programming styles, and application scenarios. In this context, models need to learn deep vulnerability features from limited training data that can be transferred to unseen data. However, most existing methods simplify code vulnerability detection to a binary classification problem, simply determining whether a vulnerability exists in the code. This learning approach, relying solely on the "security / vulnerability" binary classification output, makes the model tend to capture surface-level features, failing to truly understand the underlying causes of vulnerabilities, thus performing poorly when faced with unseen data distributions.
[0005] To alleviate these problems, some studies have begun to introduce vulnerability explanations or reasoning processes. For example, researchers use large language models to generate reasoning chains corresponding to vulnerability classification labels and use the content of these reasoning chains as supervision signals for model training. These methods improve the interpretability of the model to some extent. However, existing research shows that relying solely on explanation information is still insufficient to enable the model to learn vulnerability features with good transferability. The main reason is that there is a complex and non-linear mapping relationship between source code and natural language explanations, and the model still struggles to effectively capture the deep correlation between the two during training. Furthermore, current code vulnerability detection methods generally ignore the important information contained in patched code. In actual software development, patch code clearly reflects the correct implementation after the vulnerability is fixed, indicating the location of the vulnerability. However, existing methods typically only focus on the original vulnerability code and its corresponding labels or explanations, failing to systematically integrate patch code as reference information into the model training process, thus missing a crucial opportunity to guide the model to learn deep vulnerability features. In summary, although current code vulnerability detection methods based on large language models have made some progress in terms of performance and interpretability, they still have significant shortcomings in terms of generalization ability and deep vulnerability characteristics. Summary of the Invention
[0006] To address the aforementioned issues, this invention proposes a code vulnerability detection method based on large language model inference. This method integrates patch code into the training process of large language model code vulnerability detection, aiming to improve the detection accuracy, interpretation quality, and generalization ability of the model under limited training data conditions.
[0007] The core idea of this invention is to introduce patch code corresponding to the source code into the code vulnerability detection task, systematically integrate the differences before and after vulnerability repair into the model training and optimization process, and gradually improve the model's capabilities through a multi-stage training strategy.
[0008] A code vulnerability detection method based on large language model inference, the method comprising the following steps:
[0009] Step S1, Task definition and detection target construction;
[0010] Step S2, patch code collection: collect patch code by scraping from a code hosting platform or by generating it using a large language model;
[0011] Step S3: Inference augmentation data construction and fine-tuning training. Based on source code, vulnerability labels and patch code, an inference chain is synthesized as an explanation, and then the large language model is fine-tuned on the inference augmentation dataset.
[0012] Step S4: Hierarchical preference optimization data construction, using opposite labels and correct labels to construct preference data and non-preference data respectively, forming a preference dataset;
[0013] Step S5: Hierarchical preference optimization training;
[0014] Step S6: Use the large language model optimized by preferences for model reasoning and output. Input the source code as the input of the large language model optimized by preferences, and the output will include vulnerability detection results containing vulnerability labels and explanations.
[0015] Further, step S1 specifically includes:
[0016] Step S11, Input source code and define detection targets: For the source code sample set , ,in, Given the number of source code samples, the output target of the code vulnerability detection task is set as follows:
[0017]
[0018] in, This represents a set of vulnerability detection tags. 0 indicates a secure code, and 1 indicates a vulnerability exists. This represents the set of vulnerability explanation information used to support vulnerability detection tags;
[0019] Step S12 defines the code vulnerability detection task as: generating corresponding vulnerability tags given source code. and its explanation .
[0020] Further, step S2 specifically includes:
[0021] Step S21, build the patch code;
[0022] Patch code The data collection methods include:
[0023] The scraping method based on the code hosting platform: For datasets originating from public code repositories, trace the original code repository and obtain the corresponding commit content, extract the code modification content and commit information from the vulnerability fix commits, and the code modification content is used as the patch code;
[0024] Generation based on large language models: For samples where the code repository is inaccessible, a large language model is used to generate samples from given source code. Vulnerability detection tags Under the given conditions, the corresponding patch code can be generated directly;
[0025] Step S22, Patch Code Organization: Organize the obtained patch code and related submission information Paired with source code samples, serving as important reference information for subsequent inference and model training.
[0026] Furthermore, step S3 specifically includes:
[0027] Step S31, Vulnerability Explanation Structure Definition:
[0028] Define vulnerability explanation It consists of the following three parts: vulnerability code line location information, analysis and description of the vulnerability cause, and summary explanation of the above information;
[0029] Step S32, generation of inference chain based on patch code:
[0030] Using a large language model, in the input source code sample Vulnerability Tags Patch code and the information submitted Under the given conditions, generate a vulnerability explanation in the form of a multi-step inference chain:
[0031]
[0032] Among them, patch code As a reference, the guidance model locates and analyzes potential vulnerabilities in the source code and generates vulnerability explanations. ;
[0033] Step S33, Inference Enhancement Fine-tuning:
[0034] Constructing an inference-enhanced training dataset:
[0035]
[0036] in, Represents the dataset The number of samples in the dataset From each source code sample Vulnerability Tags Explanation of vulnerabilities It consists of three parts, and the large language model is supervised and fine-tuned to enable it to generate vulnerability labels and explanations simultaneously. The optimized objective function is:
[0037]
[0038] in, For the large language model to be fine-tuned, inference-enhanced training dataset For each source code sample Predicting vulnerability tags Explanation of vulnerabilities Perform LoRA fine-tuning.
[0039] Furthermore, step S4 specifically includes:
[0040] Step S41, construct based on preference data with opposite labels:
[0041] For each sample Using a large language model, two interpretations are generated: one for the source code and one for the correct label and one for the opposite label.
[0042]
[0043]
[0044] in, For correct labeling, For the opposite label, the correct label and vulnerability tags The values are the same, but the labels are opposite. By vulnerability tag Inverting the result yields... , This indicates a high-quality explanation, i.e., based on the correct label. The generated explanation content, This indicates a lower quality interpretation, i.e., based on the opposite label. The generated explanation content;
[0045] High-quality explanations are defined as preference data. ;
[0046] Lower-quality explanations are defined as unbiased data. ;
[0047] Step S42, construct based on preference data with correct labels:
[0048] For the same source code sample Two different interpretations are generated using a large language model:
[0049]
[0050] in, and These are two interpretations generated by the large language model, and then corresponding patch code is generated based on each interpretation:
[0051]
[0052]
[0053] Based on the patch code by the large language model Generate patch code with the model generated based on the explanation and semantic similarity and Sort the data and then generate patch code based on the model. , and explanation , Based on the correspondence, output the sorted results of the explanation content:
[0054]
[0055] Among them, the model output This indicates a higher quality explanation under the correct label. This indicates a lower quality explanation under the correct label;
[0056] Then, construct preference and non-preference data pairs under the correct label conditions:
[0057] ;
[0058] Step S43, Summarize the hierarchical preference dataset:
[0059] Preference data pairs constructed based on opposite labels and preference data pairs constructed based on correct labels are aggregated to form a preference dataset:
[0060]
[0061] in, This represents a source code sample. Indicates preference for detection results. include and , This indicates the results of the non-biased detection. include and .
[0062] Further, step S5 specifically includes:
[0063] Step S51, Preference Dataset Input and Reference Model Initialization: Input the preference dataset constructed in step S43. The model, after undergoing inference enhancement and fine-tuning, serves as the training input and as the reference model. ;
[0064] Step S52, Preference Optimization Training: For each source code sample Calculate the current model to be optimized respectively. Preference detection results Results of non-preference detection The conditional probability, and compared with the reference model. Compare the outputs with the reference model. The output is the reference model. Regarding the preference results and non-preference results The conditional probability.
[0065] Furthermore, the Direct Preference Optimization (DPO) method is used during preference optimization training, and its objective function is defined as:
[0066]
[0067] in, This represents the model currently being optimized. Source code sample Predicted output The conditional probability, This represents the model currently being optimized. Source code sample Predicted output The conditional probability, Representation of reference model Source code sample Predicted output The conditional probability, Representation of reference model Source code sample Predicted output The conditional probability, This represents the sigmoid function. This is a control factor used to adjust the intensity of preference. The reference model is fine-tuned to enhance inference. For the current model to be optimized, This is a preference dataset.
[0068] The beneficial technical effects of this invention are as follows:
[0069] Hierarchical preference optimization: This invention constructs preference data containing different levels and performs hierarchical preference optimization on the model. On the one hand, it enhances the model's ability to distinguish between correct and incorrect detection results. On the other hand, it uses patch code to indirectly compare the repair code generated by the model under the correct label according to different interpretations, thereby judging the quality of the interpretation. This significantly improves the reliability and generalization ability of the model's generated interpretations, enabling both detection and interpretation to achieve high-quality results.
[0070] Inference Enhancement Fine-tuning: This invention utilizes patch code to guide a large language model to generate multi-step inference vulnerability detection explanations, including vulnerability line location, cause analysis, and summary information. Based on this, an inference enhancement training dataset is constructed. Through supervised fine-tuning of inference enhancement, the model can simultaneously output detection results of vulnerability labels and explanation content, improving the interpretability of detection results and overcoming the limitation of existing methods that only output binary classification vulnerability labels.
[0071] In summary, the method of this invention helps the model capture essential vulnerability features by introducing inference enhancement fine-tuning and hierarchical preference optimization, effectively improving vulnerability detection performance and generalization ability. At the same time, it outputs an inference chain containing explanations, achieving higher detection performance, stronger generalization ability, and better interpretability. It has significant technical advantages and good application prospects. Attached Figure Description
[0072] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the 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 based on these drawings without creative effort.
[0073] Figure 1 This is a flowchart illustrating a code vulnerability detection method based on large language model reasoning provided in an embodiment of the present invention. Detailed Implementation
[0074] 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.
[0075] This invention proposes a code vulnerability detection method based on large language model inference, which mainly includes the following three key technologies:
[0076] I. Patch Code Refinement Collection
[0077] To address the issue of low-quality vulnerability interpretation generated by existing methods, this invention employs a patch code collection approach. This introduces high-quality patch code as a crucial reference for code vulnerability detection tasks. Patch code collection combines automated acquisition from code hosting platforms with generation via a large language model. It crawls the original code repository corresponding to the vulnerability sample, extracting code modifications and related submission information from vulnerability fix submissions. For samples where the original repository is inaccessible, the large language model directly generates corresponding patch code based on the source code and vulnerability tags. Through this method, a corresponding patch code is constructed for each code sample, providing a reliable reference for subsequent model training.
[0078] II. Reasoning-Enhanced Fine-Tuning
[0079] To address the issues of weak generalization and poor interpretability in existing vulnerability detection methods, this invention proposes an inference enhancement fine-tuning method. This method utilizes patch code to guide a large language model in generating structured vulnerability detection explanations. Based on source code, vulnerability tags, and collected patch code, the inference enhancement fine-tuning generates a multi-step inference chain explanation that includes vulnerability line location, vulnerability cause analysis, and summary information. Furthermore, an inference enhancement training dataset is constructed based on this dataset. Through supervised fine-tuning of the large language model, the model gains the ability to simultaneously output vulnerability tags and supporting vulnerability explanations, thereby improving the interpretability of the detection results and the model's generalization ability.
[0080] III. Hierarchical Preference Optimization
[0081] To address the issue of inconsistent interpretation quality in existing methods, this invention further proposes a hierarchical preference optimization approach to continuously improve the model's interpretation quality. This hierarchical preference optimization constructs preference data at different levels, enhancing the model's ability to distinguish between correct and incorrect detection results. Furthermore, it uses patch code as a reference to compare different interpretations under the same correct label, thereby determining the quality of the interpretation. The model is trained on the constructed hierarchical preference dataset using preference optimization, enabling it to maintain the interpretability of detection results while further improving the reliability of the interpretation content.
[0082] like Figure 1As shown, the method collects patch code through code hosting platforms or model generation, then synthesizes an inference chain based on source code, vulnerability tags, and patch code as an explanation, fine-tunes the model on the inference-enhanced dataset, constructs hierarchical preference data with reference to patch code, performs hierarchical preference optimization, thereby capturing deeper vulnerability features, providing the model's explanatory and generalization capabilities, and finally uses the optimized large language model for model inference and output.
[0083] The specific implementation steps of the method are as follows:
[0084] Step S1: Task definition and detection target construction.
[0085] Step S11, Input source code and target definition:
[0086] For source code sample set , ,in, Given the number of source code samples, the output target of the code vulnerability detection task is set as follows:
[0087]
[0088] in, This represents a set of vulnerability detection tags. 0 indicates a secure code, and 1 indicates a vulnerability exists; This represents the set of vulnerability explanation information used to support vulnerability detection tags.
[0089] Step S12, Formal representation of the detection task:
[0090] The code vulnerability detection task is defined as: given source code, generating corresponding vulnerability tags. and its explanation This is to achieve test result output that is both accurate and interpretable.
[0091] Step S2: Patch code collection.
[0092] Step S21, Patch code build:
[0093] For each source code sample Collect its corresponding patch code as patch code It includes the following two data collection methods:
[0094] The scraping method based on the code hosting platform: For datasets originating from public code repositories, trace the original code repository and obtain the corresponding commit content, extract the code modification content and commit information from the vulnerability fix commits, and the code modification content is used as the patch code;
[0095] Generation based on large language models: For samples where the code repository is inaccessible, a large language model is used to generate samples from given source code. Vulnerability detection tags Under the given conditions, the corresponding patch code can be generated directly.
[0096] Step S22, Patch code organization:
[0097] The obtained patch code and related submission information Paired with source code samples, serving as important reference information for subsequent inference and model training.
[0098] Step S3: Construction of Augmented Inference Data and Fine-tuning Training
[0099] Step S31, Vulnerability Explanation Structure Definition:
[0100] Define vulnerability explanation It consists of the following three parts: vulnerability code line location information, analysis and description of the vulnerability cause, and summary explanation of the above information. The vulnerability explanation is used to directly support the judgment of vulnerability label.
[0101] Step S32, generation of inference chain based on patch code:
[0102] Using a large language model, in the input source code sample Vulnerability Tags Patch code and the information submitted Under the given conditions, generate a vulnerability explanation in the form of a multi-step inference chain:
[0103]
[0104] Among them, patch code As a reference, the guidance model locates and analyzes potential vulnerabilities in the source code and generates vulnerability explanations. For secure samples, guide the model to analyze their security mechanisms and generate explanations. .
[0105] Step S33, Inference Enhancement Fine-tuning:
[0106] Constructing an inference-enhanced training dataset:
[0107]
[0108] in, Represents the dataset The number of samples in the dataset From each source code sample Vulnerability Tags Explanation of vulnerabilities It consists of three parts, and the large language model is supervised and fine-tuned to enable it to generate vulnerability labels and explanations simultaneously. The optimized objective function is:
[0109]
[0110] in, For the large language model to be fine-tuned, inference-enhanced training dataset For each source code sample Predict vulnerability tags Explanation of vulnerabilities LoRA fine-tuning, through inference enhancement, enables the model to simultaneously output multi-step inference chains of vulnerability detection labels and explanatory content during the inference phase, thereby improving the interpretability and generalization ability of the detection results.
[0111] Step S4: Hierarchical preference optimization data construction.
[0112] Step S41, construct based on preference data with opposite labels:
[0113] For each sample Using a large language model, two interpretations are generated: one for the source code and one for the correct label and one for the opposite label.
[0114]
[0115]
[0116] in, For correct labeling, For the opposite label, the correct label and vulnerability tags The values are the same, but the labels are opposite. By vulnerability tag Inverting the result yields... , This indicates a high-quality explanation, i.e., based on the correct label. The generated explanation content, This indicates a lower quality interpretation, i.e., based on the opposite label. The generated explanation content.
[0117] Therefore, high-quality explanations (explanations corresponding to the correct labels) are defined as preference data:
[0118]
[0119] Lower-quality explanations (explanations corresponding to opposite labels) are defined as unbiased data.
[0120]
[0121] The preference data constructed in the above manner is used to enhance the model's ability to distinguish between correct and incorrect vulnerability detection results.
[0122] Step S42, construct based on preference data with correct labels:
[0123] Assuming the labels are correct, to further differentiate between different quality interpretations, for the same source code sample... Two different interpretations are generated using a large language model:
[0124]
[0125] in, and These are two interpretations generated by the large language model, and then corresponding patch code is generated based on each interpretation:
[0126]
[0127]
[0128] To avoid making subjective judgments about the explanation, patch code is introduced. For reference, generate patch code for the two models mentioned above. and Comparisons are made by the large language model based on the patch code. Generate patch code with the model generated based on the explanation and Semantic similarity is used to generate patch code for two models. and Sort the data and then generate patch code based on the model. , and explanation , Based on the correspondence, output the sorted results of the explanation content:
[0129]
[0130] Among them, the model output This indicates a high-quality explanation. This indicates an explanation of lower quality.
[0131] Based on this, construct preference and non-preference data pairs under the condition of correct labeling:
[0132]
[0133] This process uses patch code to indirectly evaluate the quality of the interpreted content, enabling a reliable distinction between high-quality and low-quality interpretations.
[0134] Step S43, Summarize the hierarchical preference dataset:
[0135] Preference data pairs constructed based on opposite labels and preference data pairs constructed based on correct labels are aggregated to form a preference dataset:
[0136]
[0137] in, This represents a source code sample. Indicates preference for detection results. include and , This indicates the results of the non-biased detection. include and This preference dataset This will be used as input for subsequent preference optimization training to further improve the model's ability to generate high-quality explanations and its generalization ability.
[0138] Step S5: Hierarchical preference optimization training.
[0139] Step S51, Inputting the preference dataset and initializing the reference model:
[0140] The preference dataset constructed in step S43 The model, after undergoing inference enhancement and fine-tuning, serves as the training input and as the reference model. It is used to provide a stable probabilistic baseline during preference optimization to constrain the direction of model updates and avoid performance degradation during training.
[0141] Step S52, Preference Optimization Training:
[0142] During the preference optimization training process, for each source code sample Calculate the current model to be optimized respectively. Preference detection results Results of non-preference detection The conditional probability, and compared with the reference model. The output is compared to guide the model to update its parameters, making it more inclined to generate high-quality detection results. (Referencing the model) The output is the reference model. Regarding the preference results and non-preference results The conditional probability is calculated based entirely on the autoregressive decoding mechanism of the language model, by multiplying the probabilities of each token to obtain the conditional probability of the entire sequence.
[0143] Specifically, the model training process employs direct preference optimization (DPO), whose objective function is defined as:
[0144]
[0145] in, This represents the model currently being optimized. Source code sample Predicted output The conditional probability, This represents the model currently being optimized. Source code sample Predicted output The conditional probability, Representation of reference model Source code sample Predicted output The conditional probability, Representation of reference model Source code sample Predicted output The conditional probability, This represents the sigmoid function. This is a control factor used to adjust the intensity of preference. The reference model is fine-tuned to enhance inference. For the current model to be optimized, This is a preference dataset.
[0146] Through the above preference optimization process, the model can simultaneously consider preference constraints at the level of label correctness and preference constraints at the level of explanation quality during parameter update, thereby achieving reinforcement learning of detection results containing high-quality explanations.
[0147] Step S6: Model Inference and Output.
[0148] By using a preference-optimized large language model for inference and output, during the model inference stage, only the source code is input as input to the preference-optimized large language model. No additional patch code is required to output vulnerability detection results containing vulnerability labels and explanations. This improves vulnerability detection performance and interpretability while ensuring inference efficiency.
[0149] To verify the effectiveness of the technical solution proposed in this invention, systematic experiments were conducted from multiple aspects, including task setting, dataset construction, evaluation metrics, and comparison methods. The specific details are as follows.
[0150] Task:
[0151] This invention primarily targets code vulnerability detection tasks, specifically determining whether a given source code sample contains security vulnerabilities. Unlike traditional methods that only output binary classification vulnerability labels, this invention introduces an explicit inference chain generation mechanism while detecting the presence of vulnerabilities in the code. This mechanism assists the model in capturing deeper vulnerability features through the inference process, leading to a more accurate understanding of code semantics and vulnerability causes, thereby improving the generalization ability and interpretability of the detection results.
[0152] Dataset:
[0153] To comprehensively evaluate the effectiveness and generalization ability of the method of this invention in different scenarios, five widely used code vulnerability detection datasets were selected for experiments, including DiverseVul, Devign, BigVul, ReVeal, and Juliet datasets. The training and validation sets of DiverseVul and Devign were merged for model training, while the remaining datasets were used as test sets for evaluation. This setup allows for simultaneous examination of the model's performance under both known and unknown data distributions, especially the BigVul, ReVeal, and Juliet datasets, to verify the model's generalization ability in cross-dataset scenarios. Experimental results show that the stable performance of this invention on different datasets effectively reflects the applicability of this technical solution in real-world complex vulnerability detection scenarios.
[0154] Evaluation indicators:
[0155] This invention uses the F1 score as the primary evaluation metric to comprehensively measure the model's ability to balance precision and recall in vulnerability detection tasks. Simultaneously, to further enhance the robustness of the evaluation, the Matthews correlation coefficient (MCC) is introduced as an auxiliary evaluation metric. This metric has stronger discriminative power in binary classification tasks with imbalanced class distributions and can more objectively reflect the overall performance of the model. Through joint evaluation using multiple metrics, the stability and reliability of the method presented in this invention can be more comprehensively verified in practical vulnerability detection applications.
[0156] Comparison method:
[0157] To fully demonstrate the advantages of the method of this invention over the prior art, a variety of representative methods were selected as comparative baselines in the experiment, including two main categories: non-large language model methods and methods based on large language models.
[0158] Among them, non-large language model methods include Devign, ReVeal, CodeBERT, GraphCodeBERT, UniXcoder, ReGVD, and EPVD, which represent the current mainstream vulnerability detection technologies based on deep learning.
[0159] Regarding large language modeling methods, on the one hand, closed-source large language models such as GPT-4o and DeepSeek-R1 were selected for comparison. On the other hand, training methods using the same dataset and basic model as this patent were also introduced, including the standard supervised fine-tuning method (SFT), the chain-inference-based supervised fine-tuning method (SFT-CoT), and the multi-task learning method VulLLM. Through the above multi-dimensional comparison, the performance improvement brought by the patent's technical solution under fair conditions can be fully verified. The code vulnerability detection results of different methods on five datasets are compared in Table 1. The best results of the model trained on CodeLlama are indicated in bold, and the best results on all models are indicated by underline:
[0160] Table 1. Comparison of code vulnerability detection results using different methods on five datasets.
[0161]
[0162] Table 1 shows that the RIRE method proposed in this invention significantly outperforms existing baseline methods on most datasets, demonstrating its effectiveness and generalization ability in vulnerability detection tasks, especially on unseen datasets. This indicates that the present invention can effectively alleviate the problem of existing methods being heavily dependent on the distribution of training data. Furthermore, compared to the baseline method VulLLM, which only outputs vulnerability labels, the present invention achieves better average performance on the CodeLlama base model, indicating that by introducing an inference mechanism that incorporates patch code, the model can not only generate interpretable inference chains but also further improve detection accuracy. This fully demonstrates the important role of explicitly incorporating the inference process into the model optimization objective for code understanding and vulnerability detection tasks. It is noteworthy that the present invention achieves better or comparable detection performance on almost all datasets even with a model parameter size significantly smaller than closed-source large language models such as GPT-4o and DeepSeek-R1, indicating that existing closed-source large models still suffer from insufficient adaptability in code vulnerability detection tasks. The present invention effectively improves the model's inference and generalization abilities in complex vulnerability scenarios through a technical solution combining inference-enhanced supervised fine-tuning and hierarchical preference optimization. In summary, by introducing patch code reasoning and hierarchical preference optimization methods, this invention achieves higher detection performance, stronger generalization ability, and better interpretability while ensuring controllable model size, demonstrating significant technical advantages and promising application prospects.
[0163] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features; and these modifications or substitutions 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.
Claims
1. A code vulnerability detection method based on large language model reasoning, characterized in that, The method includes: Step S1, Task definition and detection target construction; Step S2, patch code collection: collect patch code by scraping from a code hosting platform or by generating it using a large language model; Step S3: Inference augmentation data construction and fine-tuning training. Based on source code, vulnerability labels and patch code, an inference chain is synthesized as an explanation, and then the large language model is fine-tuned on the inference augmentation dataset. Step S4: Hierarchical preference optimization data construction, using opposite labels and correct labels to construct preference data and non-preference data respectively, forming a preference dataset; Step S5: Hierarchical preference optimization training; Step S6: Use the large language model optimized by preferences for model reasoning and output. Input the source code as the input of the large language model optimized by preferences, and the output will include vulnerability detection results containing vulnerability labels and explanations.
2. The code vulnerability detection method based on large language model reasoning according to claim 1, characterized in that, Step S1 further includes: Step S11, Input source code and define detection targets: For the source code sample set , ,in, Given the number of source code samples, the output target of the code vulnerability detection task is set as follows: in, This represents a set of vulnerability detection tags. 0 indicates a secure code, and 1 indicates a vulnerability exists. This represents the set of vulnerability explanation information used to support vulnerability detection tags; Step S12 defines the code vulnerability detection task as: generating corresponding vulnerability tags given source code. and its explanation .
3. The code vulnerability detection method based on large language model reasoning according to claim 2, characterized in that, Step S2 further includes: Step S21, build the patch code; Patch code The data collection methods include: The scraping method based on the code hosting platform: For datasets originating from public code repositories, trace the original code repository and obtain the corresponding commit content, extract the code modification content and commit information from the vulnerability fix commits, and the code modification content is used as the patch code; Generation based on large language models: For samples where the code repository is inaccessible, a large language model is used to generate samples from given source code. Vulnerability detection tags Under the given conditions, the corresponding patch code can be generated directly; Step S22, Patch Code Organization: Organize the obtained patch code and related submission information Paired with source code samples, serving as important reference information for subsequent inference and model training.
4. The code vulnerability detection method based on large language model reasoning according to claim 3, characterized in that, Step S3 further includes: Step S31, Vulnerability Explanation Structure Definition: Define vulnerability explanation It consists of the following three parts: vulnerability code line location information, analysis and description of the vulnerability cause, and summary explanation of the above information; Step S32, generation of inference chain based on patch code: Using a large language model, in the input source code sample Vulnerability Tags Patch code and the information submitted Under the given conditions, generate a vulnerability explanation in the form of a multi-step inference chain: Among them, patch code As a reference, the guidance model locates and analyzes potential vulnerabilities in the source code and generates vulnerability explanations. ; Step S33, Inference Enhancement Fine-tuning: Constructing an inference-enhanced training dataset: in, Represents the dataset The number of samples in the dataset From each source code sample Vulnerability Tags Explanation of vulnerabilities It consists of three parts, and the large language model is supervised and fine-tuned to enable it to generate vulnerability labels and explanations simultaneously. The optimized objective function is: in, For the large language model to be fine-tuned, inference-enhanced training dataset For each source code sample Predicting vulnerability tags Explanation of vulnerabilities Perform LoRA fine-tuning.
5. The code vulnerability detection method based on large language model reasoning according to claim 4, characterized in that, Step S4 further includes: Step S41, construct based on preference data with opposite labels: For each sample Using a large language model, two interpretations are generated: one for the source code and one for the correct label and one for the opposite label. in, For correct labeling, For the opposite label, the correct label and vulnerability tags The values are the same, but the labels are opposite. By vulnerability tag Inverting the result yields... , This indicates a high-quality explanation, i.e., based on the correct label. The generated explanation content, This indicates a lower quality interpretation, i.e., based on the opposite label. The generated explanation content; High-quality explanations are defined as preference data. ; Lower-quality explanations are defined as unbiased data. ; Step S42, construct based on preference data with correct labels: For the same source code sample Two different interpretations are generated using a large language model: in, and These are two interpretations generated by the large language model, and then corresponding patch code is generated based on each interpretation: Based on the patch code by the large language model Generate patch code with the model generated based on the explanation and semantic similarity and Sort the data and then generate patch code based on the model. , and explanation , Based on the correspondence, output the sorted results of the explanation content: Among them, the model output This indicates a higher quality explanation under the correct label. This indicates a lower quality explanation under the correct label; Then, construct preference and non-preference data pairs under the correct labeling conditions: ; Step S43, Summarize the hierarchical preference dataset: Preference data pairs constructed based on opposite labels and preference data pairs constructed based on correct labels are aggregated to form a preference dataset: in, This represents a source code sample. Indicates preference for detection results. include and , This indicates the results of the non-biased detection. include and .
6. The code vulnerability detection method based on large language model reasoning according to claim 5, characterized in that, Step S5 further includes: Step S51, Preference Dataset Input and Reference Model Initialization: Input the preference dataset constructed in step S43. The model, after undergoing inference enhancement and fine-tuning, serves as the training input and as the reference model. ; Step S52, Preference Optimization Training: For each source code sample Calculate the current model to be optimized respectively. Preference detection results Results of non-preference detection The conditional probability, and compared with the reference model. Compare the output with the reference model. The output is the reference model. Regarding the preference results and non-preference results The conditional probability.
7. The code vulnerability detection method based on large language model reasoning according to claim 6, characterized in that, The Direct Preference Optimization (DPO) method is used during preference optimization training, and its objective function is defined as: in, This represents the model currently being optimized. Source code sample Predicted output The conditional probability, This represents the model currently being optimized. Source code sample Predicted output The conditional probability, Representation of reference model Source code sample Predicted output The conditional probability, Representation of reference model Source code sample Predicted output The conditional probability, This represents the sigmoid function. This is a control factor used to adjust the intensity of preference. The reference model is fine-tuned to enhance inference. For the current model to be optimized, This is a preference dataset.