arXiv:2602.23407cs.CRcs.AI2026-02

用细粒度奖励训练模型生成更安全的代码。

Learning to Generate Secure Code via Token-Level Rewards

  • 利用自省生成高置信度修复对,构建真实漏洞数据集
  • 通过词元级奖励显著减少代码漏洞,提升安全性和质量
  • 适合关注代码安全与模型微调的研究者

大型语言模型在代码生成方面表现出强大能力,但仍易产生安全漏洞。现有方法普遍存在高质量安全数据稀缺和粗粒度强化学习奖励信号的问题。为此,我们提出Vul2Safe框架,利用大模型自省从真实漏洞中构建高置信度修复对,并生成多样隐式提示,建立PrimeVul+数据集。同时,提出SRCode训练框架,首次在代码安全强化学习中采用词元级奖励,使模型在训练中持续关注并强化关键细粒度安全模式。相比传统实例级奖励,该方法可更精准优化局部安全实现。大量实验表明,PrimeVul+与SRCode能显著降低生成代码中的安全漏洞,同时提升多个基准上的整体代码质量。

原文摘要 · Abstract (English)

Large language models (LLMs) have demonstrated strong capabilities in code generation, yet they remain prone to producing security vulnerabilities. Existing approaches commonly suffer from two key limitations: the scarcity of high-quality security data and coarse-grained reinforcement learning reward signals. To address these challenges, we propose Vul2Safe, a new secure code generation framework that leverages LLM self-reflection to construct high-confidence repair pairs from real-world vulnerabilities, and further generates diverse implicit prompts to build the PrimeVul+ dataset. Meanwhile, we introduce SRCode, a novel training framework that pioneers the use of token-level rewards in reinforcement learning for code security, which enables the model to continuously attend to and reinforce critical fine-grained security patterns during training. Compared with traditional instance-level reward schemes, our approach allows for more precise optimization of local security implementations. Extensive experiments show that PrimeVul+ and SRCode substantially reduce security vulnerabilities in generated code while improving overall code quality across multiple benchmarks.

代码生成安全强化学习LLM

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。