A Secure Code Generation Method Based on Edit-Aware Large Language Model

CN122569901APending Publication Date: 2026-08-14CHONGQING UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-07-13
Publication Date
2026-08-14

AI Technical Summary

Technical Problem

[0008]针对现有技术存在的上述问题,本发明要解决的技术问题是:现有安全代码生成方法难以同时兼顾安全性、功能正确性和推理效率的问题

Benefits of technology

[0063]本发明针对现有大语言模型安全代码生成方法存在的推理阶段依赖辅助安全模型、推理开销较高、安全微调训练成本高、模型编辑容易导致功能退化、功能恢复训练可能覆盖安全编辑效果等问题,提出了一种基于编辑感知的大语言模型安全代码生成方法。该方法将安全代码生成过程划分为安全编辑样本构建、模型级安全知识注入、编辑感知功能恢复以及安全代码生成四个阶段,先通过局部参数编辑将安全修复知识注入目标代码大语言模型,再通过带有编辑感知正则项的功能恢复训练提升模型通用代码生成能力。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122569901A_ABST
    Figure CN122569901A_ABST
Patent Text Reader

Abstract

This invention relates to a secure code generation method for large language models based on edit awareness, belonging to the field of software security technology. It comprises four stages: secure edit sample construction, model-level security knowledge injection, edit-aware function recovery, and secure code generation. First, a secure edit dataset is constructed based on vulnerable code and security patch code. Then, local parameters of the target code's large language model are updated using a model editing algorithm, injecting security knowledge into the model. Next, function recovery training is performed using general code generation data, and edit-aware regularization is used to constrain the parameters modified by secure editing to maintain a state close to the edited state, preventing function recovery training from overwriting injected security behaviors. Finally, code generation is performed using a security-enhanced model. This invention can improve the security of generated code without introducing additional security models during the inference stage, while maintaining the model's general code generation capabilities.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of software security, code generation, artificial intelligence-assisted software engineering, and security enhancement of large language models, and particularly to a method for generating secure code for large language models based on edit awareness. Background Technology

[0002] With the widespread application of large language models in software development, code generation tools based on large language models have been gradually applied to scenarios such as code completion, function generation, program repair, test generation, and development assistance. Developers can guide large language models to automatically generate target code based on natural language requirements, function signatures, code comments, or existing code context, thereby improving software development efficiency.

[0003] However, large language models typically learn code patterns from massive open-source code corpora, and these training corpora inevitably contain code implementations with security flaws. Therefore, when generating code, large language models may reproduce insecure implementation patterns from the training data, leading to security vulnerabilities such as SQL injection, command injection, path traversal, out-of-bounds access, null pointer dereferences, integer overflows, and cross-site scripting. If such insecure code is directly adopted by developers, it may be introduced into real software systems, posing security risks.

[0004] In existing technologies, methods for addressing the problem of insecure code generation from large language models mainly include security enhancement methods during the inference phase and security enhancement methods during the model training phase. Security enhancement methods during the inference phase typically introduce additional security discrimination models, security filters, or security constraint decoding mechanisms during code generation to adjust the output probability of the target large language model, making it more inclined to generate safer code. For example, some methods train an auxiliary security model to determine the security of candidate tokens or candidate code fragments during generation and guide the decoding process accordingly. While these methods can reduce insecure code generation to some extent, they still have the following problems: First, their security enhancement effect depends on the capabilities and generalization of the external auxiliary security model; when the auxiliary model is small or cannot accurately identify complex vulnerability contexts, the security enhancement effect is unstable. Second, these methods usually require running the target code generation model and the auxiliary security model simultaneously during the inference phase, introducing additional computational overhead and inference latency, which is not conducive to deployment in low-latency, resource-constrained, or interactive development environments.

[0005] Another approach involves updating the target code language model during the training phase through safety fine-tuning, continued training, or safety data augmentation, enabling it to learn safe code generation behavior. This type of method incorporates safety knowledge into the model parameters, avoiding reliance on external models during the inference phase. However, conventional fine-tuning typically requires large-scale safety-labeled data and incurs high training costs, and it involves extensive modifications to model parameters, which can easily disrupt the model's original general code generation capabilities, leading to a decrease in the model's functional correctness on general programming tasks.

[0006] Furthermore, existing model editing techniques can alter specific behaviors or knowledge of large language models through local parameter updates, offering advantages such as a smaller update range and lower training costs compared to full fine-tuning. However, current model editing research primarily focuses on factual knowledge modification, API updates, or code semantic correction, and has not yet fully addressed secure code generation scenarios. Directly using model editing for secure code generation may also introduce new problems: overly aggressive editing, while improving the model's security against specific vulnerability types, could disrupt the model's internal general code representation, leading to functional correctness degradation in general code generation tasks; and subsequent use of general code data for functional recovery training could overwrite the security knowledge injected during the model editing phase, weakening the security enhancement effect.

[0007] Therefore, existing secure code generation methods still struggle to simultaneously achieve security, functional correctness, and inference efficiency: security enhancement methods during the inference phase rely on external auxiliary models, which can easily introduce additional inference overhead and deployment complexity; conventional security fine-tuning methods have a large update range, which may damage the model's original code generation capabilities; while directly using model editing may lead to functional degradation or the loss of injected security knowledge in subsequent training. Therefore, there is an urgent need for an edit-aware secure code generation method for large language models that can inject security knowledge into the code-rich language model while maintaining its general code generation capabilities and reducing the additional overhead during the inference phase. Summary of the Invention

[0008] In view of the above-mentioned problems in the existing technology, the technical problem to be solved by the present invention is that the existing secure code generation methods have difficulty in simultaneously taking into account security, functional correctness and reasoning efficiency.

[0009] To solve the above-mentioned technical problems, the present invention adopts the following technical solution:

[0010] A method for generating secure code based on an edit-aware large language model includes the following steps:

[0011] S1: Secure Edit Sample Construction: Obtaining the Large Language Model of the Target Code Vulnerability remediation datasets and general code generation

[0012] Dataset; Secure editing samples are constructed using the vulnerability remediation dataset, and all secure editing instances constitute the secure editing dataset.

[0013] S2: Model-level security knowledge injection: Determine the target code's large language model. The positions of the parameters to be edited in the model are determined; based on the safe editing dataset obtained in S1, local parameter editing is performed, and safety-related knowledge is injected into the local parameters of the target code large language model to obtain the safe editing model. Record the subset of parameters that were modified after safe editing and the parameter values ​​after the editing was completed.

[0014] S3: Obtain several instruction code pairs from the general code generation dataset to construct function recovery training data, construct a joint optimization function recovery loss function, and use the function recovery training data to train the safe editing model. Training is performed, and the parameters of the safe-edited model are updated according to the joint optimization function recovery loss function. Training stops when the value of the joint optimization function recovery loss function no longer decreases or reaches the preset number of iterations, resulting in a safe-enhanced model. .

[0015] S4: Obtain the input context of the code to be generated, input the input context into the security enhancement model, and output the security-enhanced code generation result.

[0016] Preferably, the process of constructing the secure edit dataset in S1 is as follows:

[0017] S1-1: For the first in the vulnerability remediation dataset A sample, let its vulnerable code be... The security fix code is Comparison using code difference analysis methods and Identify the regions of difference between the two entities that have been replaced, deleted, or added, and record the starting position of these regions as the difference start position. .

[0018] S1-2: The starting position of the difference determined in S1-1 From the vulnerable code And located at the starting position of the difference Extract a predetermined number of code terms from the previous code snippet as vulnerability-related code prefixes. From security fix code The difference starts at the position mentioned above. The corresponding position is the starting point, and the cut length is... Code terms as security fix target code snippets .

[0019] Will enter ,get Original code completion results generated before safe editing. :

[0020]

[0021] And record the corresponding vulnerability type as This leads to the construction of a secure editing instance. :

[0022] =( , , , )

[0023] S1-3: Let the number of secure editing instances be... Then safely edit the dataset Represented as:

[0024] .

[0025] As an improvement, the process of obtaining the secure editing model in S2 is as follows:

[0026] S2-1: Targeting Select one or more editable layers as layers to be edited, and use the feedforward network parameters in the layers to be edited as parameters to be edited. Let the set of parameters to be edited be... The corresponding parameter index set is .

[0027] S2-2: For securely editing datasets Safe editing examples in =( , , , ),Will enter Obtain the hidden state representation of the layer to be edited. .

[0028] Set up a secure repair target code snippet The word sequence is:

[0029]

[0030] in, express The length of the lexicon. express The first in Each word element.

[0031] Define the loss of the security objective as:

[0032]

[0033] in, express The current parameters, Indicates the first The target word sequence preceding each word, Indicates that in a given and The first generated under the condition The probability of a safe target word.

[0034] Through the Perform backpropagation to calculate the output gradient corresponding to the output representation of the layer to be edited. Let the output of the layer to be edited be represented as... Then the output gradient Defined as:

[0035]

[0036] Combination and Calculate the set of parameters to be edited The local parameter displacement. For Matrix parameters in Local parameter displacement Represented as:

[0037]

[0038] in To edit the step size, For stability coefficient, Indicates hidden state transpose, Indicates hidden state The square of the second norm.

[0039] Then, update the matrix parameters as follows: :

[0040]

[0041] S2-3: Will Enter all safe editing instances sequentially For each safe editing instance, the corresponding local parameter displacement is calculated based on the current model parameters, and this local parameter displacement is accumulated into the set of parameters to be edited. After the cumulative update of all safe editing instances, the safe editing model is obtained. This process can be represented as:

[0042]

[0043] in, This represents the model editing algorithm; get The corresponding value is denoted as .

[0044] As an improvement, the joint optimization function recovery loss function in S3 is:

[0045] Several instruction code pairs are obtained from the general code generation dataset, denoted as:

[0046]

[0047] in, This indicates that the training dataset for functional recovery is available. Indicates the number of instruction code pairs; Indicates the first One code generation instruction, Indicates and The corresponding reference code.

[0048] For any instruction code pair ,Will enter and with As a monitoring signal, let The word sequence is:

[0049]

[0050] in, Indicates the word length of the reference code sequence. Indicates the first in the reference code If there are 1 word elements, then the function recovery loss will be lost. Defined as:

[0051]

[0052] in, Indicates the first The reference code lexical sequence preceding each lexical unit, The model represents the given and Generate under the conditions The probability of.

[0053] Apply edit-aware regularization constraints to the set of parameters actually modified by secure editing, wherein the edit-aware regularization terms... Defined as:

[0054]

[0055] Joint optimization function recovery loss function for:

[0056]

[0057] in, This is the regular intensity coefficient.

[0058] As an improvement, the process of obtaining the code generation result in S4 is as follows:

[0059] Input context for the code to be generated enter The target code generated by the security enhancement model is generated through autoregressive decoding. The generation process is represented as follows:

[0060]

[0061] in, This represents the target code generated by the security enhancement model.

[0062] Compared with the prior art, the present invention has at least the following advantages:

[0063] This invention addresses the problems of existing secure code generation methods for large language models, such as reliance on auxiliary security models during the inference stage, high inference overhead, high training costs for security fine-tuning, easy functional degradation caused by model editing, and potential overriding of security editing effects by functional recovery training. It proposes an edit-aware secure code generation method for large language models. This method divides the secure code generation process into four stages: secure editing sample construction, model-level security knowledge injection, edit-aware functional recovery, and secure code generation. First, security repair knowledge is injected into the target code large language model through local parameter editing. Then, functional recovery training with edit-aware regularization terms improves the model's general code generation capability.

[0064] Compared to security enhancement methods during the inference phase, this invention directly modifies local parameters in the target code's large language model, injecting security code generation knowledge into the model without requiring additional calls to the auxiliary security model during each inference iteration. Compared to conventional security fine-tuning methods, this invention employs a local model editing approach for security knowledge injection, eliminating the need for large-scale security training on all model parameters. Compared to simple model editing methods, this invention further introduces edit-aware regularization terms, ensuring that edited parameters remain close to their securely edited state during function recovery training, thereby reducing the overwriting of injected security knowledge during function recovery. Attached Figure Description

[0065] Figure 1This is a flowchart of a secure code generation method based on an edit-aware large language model. Detailed Implementation

[0066] The present invention will now be described in further detail.

[0067] See Figure 1 This invention can directly inject secure code generation knowledge into the target code's large language model without relying on an external auxiliary security model during the inference stage. At the same time, through an edit-aware function recovery mechanism, it alleviates the problem of decreased general code generation capability caused by secure editing, thereby achieving a balance between enhanced security, maintained functional correctness, and optimized inference efficiency.

[0068] A method for generating secure code based on an edit-aware large language model includes the following steps:

[0069] S1: Constructing a Secure Edit Sample: Obtaining a Large Language Model of the Target Code The dataset includes a vulnerability remediation dataset and a general code generation dataset; a secure editing sample is constructed using the vulnerability remediation dataset, and all secure editing instances constitute the secure editing dataset.

[0070] The target code large language model This is used to generate target code based on natural language requirements, code context, function signatures, code comments, or existing code prefixes. The target code large language model can be an open-source general-purpose large language model with parameter access permissions or a code-specific large language model, such as LLaMA-3.1-8B, LLaMA-3.2-3B, DeepSeek-Coder-6.7B, CodeGen-6B, Qwen2.5-Coder-7B, Qwen2.5-Coder-3B, etc. Preferably, the target code large language model is an open-source weighted model, allowing for subsequent local editing of the model's internal parameters.

[0071] The vulnerability remediation dataset uses the publicly available secure code generation dataset proposed by HeandVechev et al. This dataset contains 710 pairs of vulnerable functions and security remediation functions, covering two programming languages: Python and C / C++, and nine common CWE vulnerability types, including CWE-089, CWE-125, CWE-078, CWE-476, CWE-416, CWE-022, CWE-787, CWE-079, and CWE-190. Each sample includes a function implementation with a security flaw, a corresponding security remediation function implementation, and a corresponding CWE vulnerability type identifier. The vulnerability remediation dataset is used to extract vulnerability-related code prefixes, security target code snippets, and vulnerability type identifiers from the vulnerable functions and security remediation functions, thereby constructing secure editing instances.

[0072] The general code generation dataset is used for functional recovery training after secure editing to maintain the general code generation capabilities of the target code large language model. The general code generation dataset uses the publicly available CodeEvol-Instruct dataset. This dataset contains diverse imperative code generation samples, each including a code generation instruction and corresponding reference code. The code generation instructions can be natural language programming requirements, function descriptions, function signatures, input / output constraints, or existing code context, while the reference code is the target code implementation that meets the requirements of the instructions. The CodeEvol-Instruct dataset is independent of the vulnerability remediation dataset and is not used to inject vulnerability remediation knowledge. Instead, it is used to restore the model's general code generation capabilities in subsequent functional recovery training and reduce the impact of secure editing on the performance of general programming tasks.

[0073] S2: Model-level security knowledge injection to determine the target code's large language model. The positions of the parameters to be edited in the model are determined; based on the safe editing dataset obtained in S1, local parameter editing is performed, and safety-related knowledge is injected into the local parameters of the target code large language model to obtain the safe editing model. Record the subset of parameters that were modified after safe editing and the parameter values ​​after the editing was completed.

[0074] S3: Obtain several instruction code pairs from the general code generation dataset to construct function recovery training data, construct a joint optimization function recovery loss function, and use the function recovery training data to train the safe editing model. Training is performed, and the parameters of the safe-edited model are updated according to the joint optimization function recovery loss function. Training stops when the value of the joint optimization function recovery loss function no longer decreases or reaches the preset number of iterations, resulting in a safe-enhanced model. .

[0075] S4: Obtain the input context of the code to be generated, input the input context into the security enhancement model, and output the security-enhanced code generation result.

[0076] Specifically, the process of constructing the secure edit dataset in S1 is as follows:

[0077] A difference analysis is performed on the vulnerability code and security fix code in the vulnerability fix dataset to identify the areas of difference where replacements, deletions, or additions have occurred between the vulnerability code and the security fix code, and to determine the starting position of the difference used to construct the security editing instance. Based on the starting position of the difference, vulnerability-related code prefixes are extracted from the code segments in the vulnerability code that are located before the starting position of the difference, and security code suffixes are extracted from the security fix code starting from the fix position corresponding to the starting position of the difference, as the security fix target code segments. The vulnerability-related code prefixes are input into the target code large language model to obtain the original code completion results generated by the model before security editing, and a security editing instance is constructed by combining the vulnerability type identifier. Multiple security editing instances are combined into a security editing dataset.

[0078] S1-1: Perform a difference analysis on the vulnerable code and the security fix code to determine the starting point of the difference. For the first... A sample, let its vulnerable code be... The security fix code is Comparison using code difference analysis methods and The method identifies the regions of difference between the two code snippets that have been replaced, deleted, or added, and uses the starting position of these regions as the starting position for constructing the secure editing instance. Preferably, the `difflib` tool from the Python standard library is used to perform syntax-level difference analysis on the vulnerable code and the security fix code to identify the regions of difference and their starting positions. .

[0079] S1-2: Construct a secure editing instance based on the starting position of the differences determined in S1-1. From the vulnerable code... And located at the starting position of the difference Extract a predetermined number of code terms from the previous code snippet as vulnerability-related code prefixes. Specifically, no more than 150 code terms are truncated before the start of the difference region to preserve the local context relevant to vulnerability triggering and remediation, and to avoid excessively long input context. This is from the security fix code. Starting from the repair position corresponding to the initial position of the difference, a length of [length missing] is extracted. Security code suffixes are used as security fix target code snippets The length of the security code suffix. Set to 50 code words.

[0080] Will Input target code large language model ,get Original code completion results generated before safe editing. :

[0081]

[0082] The corresponding vulnerability type is identified as CWE_i, thereby constructing a security editing instance. :

[0083] =( , , , )

[0084] S1-3: Combine multiple secure edit instances into a secure edit dataset. Let the number of secure edit instances be... Then safely edit the dataset Represented as:

[0085]

[0086] The secure editing dataset covers one or more vulnerability types and is used to inject secure code generation knowledge into the target code's large language model. Preferably, the secure editing dataset can cover vulnerability types such as SQL injection, command injection, path traversal, out-of-bounds read, out-of-bounds write, null pointer dereference, integer overflow, cross-site scripting, and use after release.

[0087] Specifically, the process of obtaining the secure editing model in S2 is as follows:

[0088] S2-1, determine the location of the editable parameters in the target code's large language model. (For...) Select one or more editable layers as the layers to be edited. For example, select the second-to-last Transformer layer of the target code's large language model as the layer to be edited. Use the feedforward network parameters in the selected editable layers as the parameters to be edited. Let the set of parameters to be edited be... The corresponding parameter index set is .

[0089] By selecting and editing local parameters closer to the output, security-related knowledge can be injected into the high-level semantic representation of the model, making the model more inclined to generate secure code snippets in the context of vulnerability-related code, while reducing the disturbance to the underlying general code representation of the model.

[0090] S2-2 performs local parameter editing based on a single secure editing instance. For secure editing datasets... Safe editing examples in =( , , , ),Will enter Obtain the hidden state representation of the layer to be edited. .

[0091] Set up a secure repair target code snippet The word sequence is:

[0092]

[0093] in, This indicates a security fix for the target code snippet. The length of the lexicon. express The first in Each word element.

[0094] Define the loss of the security objective as:

[0095]

[0096] in, Represents the large model of the target code The current parameters, Indicates the first The target word sequence preceding each word, The model represents the code prefix related to a given vulnerability. and preceding target lexicon The first generated under the condition The probability of a safe target word.

[0097] Through the Perform backpropagation to calculate the output gradient corresponding to the output representation of the layer to be edited. Let the output of the layer to be edited be represented as... Then the output gradient Defined as:

[0098]

[0099] Combined with hidden state representation and output gradient Calculate the set of parameters to be edited The local parameter displacement. For the set of parameters to be edited. Matrix parameters in The local parameter displacement can be expressed as:

[0100]

[0101] in To edit the step size, For stability coefficient, Indicates hidden state transpose, Indicates hidden state The square of the second norm.

[0102] Then, update the matrix parameters as follows: :

[0103]

[0104] S2-3: Perform sequential cumulative editing based on the secure editing dataset. (The secure editing dataset will be used in this context.) All safe editing instances in the large language model are entered sequentially as target code. For each safe editing instance, calculate the corresponding local parameter displacement according to S2-2, and accumulate this local parameter displacement into the set of parameters to be edited. After accumulating and updating all secure editing instances, a secure editing model is obtained. This process can be represented as:

[0105]

[0106] in, This represents the model editing algorithm; Set of parameters to be edited The value of is denoted as The aforementioned This indicates the post-edited state of the parameters after security knowledge injection is completed, and serves as a regular expression constraint target in the subsequent editing and perception function recovery phase. It is used to constrain the corresponding parameters during the function recovery training process to ensure that they do not deviate from the values ​​after security editing, thereby preventing the injected security code generation behavior from being overwritten by subsequent training.

[0107] Through the local parameter editing accumulated in the above sequence, the target code large language model Updated to a safe editing model Compared to the original target code large language model, the safe editing model... The probability of generating insecure code snippets is reduced in the context of vulnerability-related code, while the probability of generating secure code snippets is increased. Since the secure editing process only modifies local parameters in the model without performing large-scale security fine-tuning on all parameters, it can reduce training costs and minimize perturbations to the model's general code generation capabilities.

[0108] Specifically, the joint optimization function recovery loss function in S3 is:

[0109] Several instruction code pairs are obtained from the general code generation dataset, denoted as:

[0110]

[0111] in, This indicates that the training dataset for functional recovery is available. Indicates the number of instruction code pairs; Indicates the first A code generation instruction, which may include natural language requirements, function signatures, code comments, input / output constraints, or existing code context; Indicates and The corresponding reference code. The general code generation data is used to restore the safe editing model. Functional correctness in general code generation tasks.

[0112] The general code generation dataset used is the publicly available CodeEvol-Instruct dataset. This dataset belongs to the imperative code generation dataset category and contains diverse code generation tasks and their corresponding reference implementations, used for training the safe editing model to restore functionality.

[0113] The standard function recovery loss is calculated based on data generated from generic code. For any instruction code pair ,Will enter and with As a monitoring signal, let The word sequence is:

[0114]

[0115] in, Indicates the word length of the reference code sequence. Indicates the first in the reference code Each word element. Then the function recovery loss. Defined as:

[0116]

[0117] in, Indicates the first The reference code lexical sequence preceding each lexical unit, The model represents the given instructions. and pre-order morphemes Generate the first under the condition Reference code lexical The probability of this loss is used to enable the safe editing model to restore its sequence generation capability and functional correctness on general code generation tasks.

[0118] An edit-aware regularization term is constructed. To prevent the functional recovery training from overriding the security behaviors injected during the aforementioned secure editing phase, this invention applies edit-aware regularization constraints to the set of parameters actually modified by secure editing. The edit-aware regularization term... Defined as:

[0119]

[0120] This regularization constraint ensures that the edited parameters during training do not deviate from their safe edited state, thus maintaining the injected safe code generation behavior.

[0121] Joint optimization of function recovery loss and edit-aware regularization term. The standard function recovery loss and the edit-aware regularization term are weighted and combined to obtain the joint optimization of function recovery loss function. for:

[0122]

[0123] in, This is the regularization strength coefficient, used to control the trade-off between function restoration and safe editing preservation. When When the value is large, the model tends to maintain the state of the safety parameters injected during the safe editing phase; when When the value is smaller, the model tends to restore general code generation capabilities. Preferably, the regularization strength coefficient is set to... .

[0124] by To initialize the model, use the generic code generation dataset. According to the final training goal Parameter optimization yields an edit-aware security enhancement model. During training, the model can update all parameters or only some parameters; however, the actual set of parameters modified during the safe editing phase... All are subject to edit-aware regularization constraints to keep them close to each other. Thus, a security enhancement model that balances secure code generation capabilities with general-purpose code generation capabilities is obtained. :

[0125]

[0126] in, This indicates the process of restoring the editing and perception functions.

[0127] Specifically, the process of obtaining the code generation result in S4 is as follows:

[0128] The input context of the code to be generated may include one or more of the following: natural language requirement description, function signature, code comments, existing code prefix, call context, input / output constraints, and security constraint description.

[0129] Target code is generated using an edit-aware security enhancement model. The input context for the code to be generated is... enter The target code for the security enhancement model is generated through an autoregressive decoding method. The decoding method can be greedy decoding, beam search, kernel sampling, or temperature sampling. The generation process is represented as follows:

[0130]

[0131] in, This refers to the target code generated by the security enhancement model. Compared to the code generated by the large language model without security enhancement, the target code reduces the occurrence of insecure code patterns while maintaining general code generation capabilities to some extent.

[0132] To verify the effectiveness of this invention, the experiment used a publicly available dataset for training and evaluation. The security-related data was based on the security code generation dataset proposed by HeandVechev et al., which is divided as follows: the known vulnerability portion covers 9 types of CWEs, including 710 pairs of vulnerability function / security fix function training samples and 24 complete function-level evaluation hints; the unseen vulnerability portion covers 17 types of CWEs, including 29 complete function-level evaluation hints. The aforementioned 710 pairs of training samples were used to construct security editing instances, the 24 known vulnerability evaluation hints were used to evaluate the security generation capability on known CWEs, and the 29 unseen vulnerability evaluation hints were used to evaluate the generalization capability on unseen CWEs. (Hint perturbation evaluation set) Based on the 0-Py scenario construction of CWE-089 in the existing vulnerability assessment prompts, robustness is evaluated under changes in the prompt format. Functional recovery training uses the CodeEvol-Instruct imperative code generation dataset; functional correctness, multilingual functional correctness, and joint security-correctness assessments use HumanEval, MultiPL-E, and CodeGuard+, respectively.

[0133] Table 1, based on known vulnerability assessments and HumanEval, presents the security ratio and functional correctness (Pass@1) of eight target models under different decoding temperatures. The table compares three methods: Vanilla (the original large language model without security enhancements); CoSec (a security enhancement method during the inference phase, guiding the decoding process through an auxiliary security model); and SafeEdit (the method of this invention), which injects security knowledge through local model editing and combines edit-aware functionality to restore and maintain general code generation capabilities. The values ​​in parentheses represent the relative changes of SafeEdit compared to the corresponding baseline method.

[0134] Table 1 Comparison of Safety Effectiveness and Functional Correctness

[0135]

[0136]

[0137] As shown in Table 1, SafeEdit maintains both a high security ratio and stronger Pass@1 across most models and temperature settings. Compared to CoSec, SafeEdit's average SecurityRatio is improved by 12.04%, 11.94%, and 7.54% at T=0.1 / 0.4 / 0.8, respectively, while its average Pass@1 is improved by 62.70%, 59.87%, and 60.38%, respectively. Compared to UltraEdit, SafeEdit maintains essentially the same level of security while improving Pass@1 by 11.73, 13.70, and 15.50 percentage points at T=0.1 / 0.4 / 0.8, respectively.

[0138] Table 2 presents the statistical significance analysis results of SafeEdit relative to Vanilla and CoSec, where p-value is used to determine whether the difference is significant, and Cliff's δ is used to measure the effect size.

[0139] Table 2 Statistical significance analysis of SafeEdit compared to the baseline method

[0140]

[0141] As shown in Table 2, SafeEdit's improvements in security rate and Pass@1 compared to Vanilla and CoSec are statistically significant, indicating that its advantages have good stability and reliability.

[0142] Table 3 presents the joint security-correctness evaluation results based on the CodeGuard+ benchmark. CodeGuard+ provides both unit test and CodeQL / SonarQube vulnerability detection results, allowing evaluation of generated code quality from the perspectives of functional correctness, security, and the combined fulfillment of both. In the table, Pass@1 represents the proportion of generated code that passes unit tests, used to measure functional correctness; SecurityRatio represents the proportion of generated code that does not trigger vulnerability detection rules, used to measure security; Secure@1-Pass represents the proportion of functionally correct generated code that also meets security requirements; Secure-Pass@1 represents the proportion of all generated code that passes both functional tests and does not trigger vulnerability detection rules. The values ​​in parentheses represent the relative changes of SafeEdit compared to the corresponding baseline method.

[0143] Table 3 CodeGuard+ Joint Security - Correctness Evaluation Results

[0144]

[0145] As shown in Table 3, this invention can simultaneously improve Pass@1, SecurityRatio, and Secure-Pass@1 in most models, indicating that it can not only improve functional correctness and security respectively, but also increase the proportion of generated code that meets both functional testing and security testing requirements. Among them, Secure-Pass@1 directly reflects the ability to generate usable secure code, and its improvement indicates that this invention has higher practical value in actual secure code generation scenarios.

[0146] Table 4, based on the MultiPL-E multilingual code generation benchmark, reports the average Pass@1 for C++, JavaScript, and TypeScript, to verify the applicability of this invention to languages ​​other than Python.

[0147] Table 4. MultiPL-E Multilingual Functionality Correctness Results (T=0.4, Average Pass@1)

[0148]

[0149] As shown in Table 4, SafeEdit achieved the highest Pass@1 on all eight target models, with improvements of 3.46%–46.07% compared to Vanilla and 19.53%–228.71% compared to CoSec, indicating that its functionality retention effect has cross-language applicability.

[0150] Table 5 presents the security generalization results based on 29 undetected vulnerability assessment suggestions, used to verify whether the recovery of security editing and edit awareness functions can be transferred to CWE types that were not trained.

[0151] Table 5 shows no security generalization results for vulnerability types (SecurityRatio, %).

[0152]

[0153] As shown in Table 5, SafeEdit achieved the highest average security rate on both LLaMA3.2-3B and LLaMA3.1-8B, indicating that the function recovery process did not erase the transferable security behaviors injected by the editor.

[0154] Table 6 reports the offline hardening time and single query inference latency of different methods, used to compare the deployment efficiency of model-level security enhancement methods and inference-stage security enhancement methods.

[0155] Table 6 Efficiency Evaluation Results (Time Unit: Seconds)

[0156]

[0157] As shown in Table 6, CoSec requires dual-model collaborative decoding during the inference phase, which generally increases inference latency. Although SafeEdit increases the cost of one-time offline function recovery, it only needs to run a single model after deployment, and its inference latency is close to that of Vanilla and UltraEdit, making it more suitable for interactive code generation scenarios.

[0158] In summary, the experimental results demonstrate that this invention achieves good results in terms of security rate, Pass@1, joint security-correctness, multilingual functional correctness, generalization of unseen vulnerabilities, and inference efficiency. These results indicate that this invention can improve the security of code generated by a large language model without significantly increasing the overhead of the inference stage, and maintains the model's general code generation capability by editing perceptual regularization terms, thus possessing high engineering practical value and potential for widespread application.

[0159] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit it. Although the present invention has been described in detail with reference to preferred embodiments, those skilled in the art should understand that modifications or equivalent substitutions can be made to the technical solutions of the present invention without departing from the spirit and scope of the technical solutions of the present invention, and all such modifications or substitutions should be covered within the scope of the claims of the present invention.

Claims

1. A method for generating secure code based on an edit-aware large language model, characterized in that: Includes the following steps: S1: Constructing a securely editable sample to obtain a large language model of the target code. The dataset includes a vulnerability remediation dataset and a general code generation dataset; a secure editing sample is constructed using the vulnerability remediation dataset, and all secure editing instances constitute the secure editing dataset; The process of building a secure edit dataset is as follows: S1-1: For the first in the vulnerability remediation dataset A sample, let its vulnerable code be... The security fix code is Comparison using code difference analysis methods and ,Sure and The differences between regions that have been replaced, deleted, or added are recorded, and the starting position of the differences is denoted as . ; S1-2: The starting position of the difference determined in S1-1 ,from In and located in the Extract a predetermined number of code terms from the previous code snippet as vulnerability-related code prefixes. ;from China and Israel The corresponding position is the starting point, and the cut length is... Code terms as security fix target code snippets ; Will enter ,get Original code completion results generated before safe editing. : And record the corresponding vulnerability type as This leads to the construction of a secure editing instance. : =( , , , ) S1-3: Let the number of secure editing instances be... Then safely edit the dataset Represented as: ; S2: Model-level security knowledge injection, to determine The positions of the parameters to be edited in the model are determined; based on the safe editing dataset obtained in S1, local parameter editing is performed, and safety-related knowledge is injected into the local parameters of the target code large language model to obtain the safe editing model. ; S3: Obtain several instruction code pairs from the general code generation dataset to construct function recovery training data, and construct a joint optimization function recovery loss function. Use the function recovery training data to train the safe editing model. Training is performed, and the parameters of the safe-edited model are updated according to the joint optimization function recovery loss function. Training stops when the value of the joint optimization function recovery loss function no longer decreases or reaches the preset number of iterations, resulting in a safe-enhanced model. ; S4: Obtain the input context of the code to be generated, input the input context into the security enhancement model, and output the security-enhanced code generation result.

2. The method for generating secure code based on an edit-aware large language model as described in claim 1, characterized in that: The process of obtaining the secure editing model in S2 is as follows: S2-1: Targeting Select one or more editable layers as layers to be edited, and use the feedforward network parameters in the layers to be edited as parameters to be edited. Let the set of parameters to be edited be... The corresponding parameter index set is ; S2-2: For safe editing examples =( , , , ),Will enter Obtain the hidden state representation of the layer to be edited. ; set up The word sequence is: in, express The length of the lexicon. express The first in Each word element; Define the loss of the security objective as: in, express The current parameters, Indicates the first The target word sequence preceding each word, Indicates that in a given and The first generated under the condition The probability of a safe target word; Through the Perform backpropagation to calculate the output representation of the layer to be edited. Corresponding output gradient : Combination and ,calculate The local parameter displacement, for Matrix parameters in Local parameter displacement Represented as: in, To edit the step size, For stability coefficient, express transpose, express The square of the second norm; Then, update the matrix parameters as follows: : S2-3: Will Enter all safe editing instances sequentially For each safe editing instance, the corresponding local parameter displacement is calculated based on the current model parameters, and this local parameter displacement is accumulated into the set of parameters to be edited. After the cumulative update of all safe editing instances, the safe editing model is obtained. This process can be represented as: in, This represents the model editing algorithm; after safe editing is completed, the result is... The corresponding value is denoted as .

3. The method for generating secure code based on an edit-aware large language model as described in claim 2, characterized in that: The joint optimization function recovery loss function in S3 is: Several instruction code pairs are obtained from the general code generation dataset, denoted as: in, This indicates that the training dataset for functional recovery is available. Indicates the number of instruction code pairs. Indicates the first One code generation instruction, Indicates and Corresponding reference code; For any instruction code pair ,Will enter and with As a monitoring signal, let The word sequence is: in, Indicates the word length of the reference code sequence. Indicates the first in the reference code If there are 1 word elements, then the function recovery loss will be lost. Defined as: in, Indicates the first The reference code lexical sequence preceding each lexical unit, Indicates that in a given and Generate under the conditions The probability of; Apply edit-aware regularization constraints to the set of parameters actually modified by secure editing, wherein the edit-aware regularization terms... Defined as: Joint optimization function recovery loss function for: in, This is the regular intensity coefficient.

4. The method for generating secure code based on an edit-aware large language model as described in claim 3, characterized in that: The process of obtaining the code generation result in S4 is as follows: Input context for the code to be generated enter The target code generated through autoregressive decoding is represented as follows: in, This represents the target code generated by the security enhancement model.