cs.CRFeb 26, 2026

Learning to Generate Secure Code via Token-Level Rewards

Authors: Jiazheng Quan, Xiaodong Li, Bin Wang, Guo An, Like Liu, Degen Huang, Lin Liu, Chengbin Hou

Organizations: Fuyao University of Science and Technology · Xiamen University · Peking University · Huawei

Abstract

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.

Figures & tables

Appendix figures & tables3 assets

Supplementary material from the paper’s appendix.

Appendix

Explore similar work

CardsList
  1. Enhancing Reliability in LLM-Based Secure Code Generation

    May 22, 2026Mohammed F. Kharma, Mohammad Alkhanafseh, Ahmed Sabbah +1LLM EvaluationCoT Reasoning

  2. Learn from Your Mistakes: Tree-like Self-Play for Secure Code LLMs

    Jun 2, 2026Wenqi Chen, Ziyan Zhang, Bin Wang +3RL for Code GenerationLLM Self-Correction

  3. DeepGuard: Secure Code Generation via Multi-Layer Semantic Aggregation

    Apr 10, 2026Li Huang, Zhongxin Liu, Yifan Wu +6Secure Code GenerationLLM Code Generation