NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Secure Code Generation via Online Reinforcement Learning with Vulnerability Reward Model

Tianyi Wu, Mingzhe Du, Yue Liu, Chengran Yang, Terry Yue Zhuo, Jiaheng Zhang, See-Kiong Ng

TL;DR

This paper introduces SecCoderX, a novel framework that uses online reinforcement learning to train Large Language Models (LLMs) to generate secure code without sacrificing functionality. By repurposing vulnerability detection datasets to create realistic coding tasks and training a specialized reward model, SecCoderX significantly improves the "Effective Safety Rate" (ESR) by about 10%, overcoming a critical trade-off that plagued previous methods.

What Problem Does This Paper Address?

Modern LLMs are revolutionizing software development, assisting with everything from writing functions to refactoring codebases. However, a major hurdle to their widespread adoption is their tendency to generate insecure code, riddled with vulnerabilities that pose significant risks. While existing methods aim to align LLMs for security, they often fall into a "functionality-security paradox": improving code security comes at the cost of degrading its functional correctness. This means that even if the code is secure, it might not work as intended, making it unusable for developers. The paper tackles this challenge by developing a method that achieves both security and functionality preservation.

Methods

SecCoderX employs a three-stage pipeline:

  1. Reality-Grounded Vulnerability-Inducing Task Synthesis: This stage repurposes existing vulnerability detection datasets. It first infers plausible real-world repository contexts where a given vulnerable code snippet might naturally appear. Then, it synthesizes diverse, "vulnerability-inducing" coding prompts tailored to these contexts and specific programming languages. This creates a rich dataset of tasks for the RL training.
  2. CWE-Conditioned Vulnerability Reward Model Training: A key innovation is a reasoning-based vulnerability reward model. This model is trained on augmented vulnerability detection data, incorporating structured reasoning from a teacher LLM. It's conditioned on specific Common Weakness Enumeration (CWE) categories, allowing it to provide precise and scalable security supervision.
  3. Online RL Alignment: The synthesized prompts and the trained reward model are integrated into an online reinforcement learning loop. The LLM is trained to generate code that is both secure (as judged by the reward model) and functionally correct (guided by a composite reward that includes length and AST similarity to the original, unaligned code).

Datasets

  • Vulnerability Detection Datasets: PrimeVul, CrossVul, R2Vul, SVEN, and ProSec were used for training the vulnerability reward model and for evaluation.
  • Synthesized Dataset: The paper introduces a new dataset of 24,000 vulnerability-inducing prompts spanning 24 CWE categories and 5 programming languages, created through their synthesis pipeline.
  • General Coding Benchmarks: HumanEval+ and MBPP+ were used to evaluate if the secure code alignment degraded general coding performance.

Key Insights and Ideas

  • Bridging Vulnerability Detection and Code Generation: The paper highlights that mature vulnerability detection datasets, often overlooked for code generation alignment, are a valuable resource. By repurposing them, SecCoderX overcomes data scarcity issues.
  • The Power of Reasoning in Security Supervision: Training a reward model that not only detects vulnerabilities but also provides structured reasoning significantly improves its accuracy and reliability, making it a more effective supervisor for LLMs.
  • Functionality Preservation is Paramount: The "functionality-security paradox" is a critical issue. SecCoderX's composite reward function, which explicitly penalizes deviations from original functionality (via length and AST similarity rewards), is crucial for achieving usable secure code.
  • Online RL for Latent Security Knowledge: The success of online RL suggests that LLMs already possess latent knowledge about secure coding practices, which can be unlocked and guided with appropriate reward signals.

Key Results

  • Improved Effective Safety Rate (ESR): SecCoderX achieved an approximate 10% improvement in ESR over unaligned models.
  • Mitigating Functionality Degradation: In stark contrast to prior methods that degraded ESR by 14-54%, SecCoderX maintained or improved it, demonstrating its ability to preserve functionality.
  • State-of-the-Art Vulnerability Detection: The trained vulnerability reward model outperformed strong commercial LLMs (GPT-4.1, Gemini-2.5-Flash) on vulnerability detection benchmarks.
  • Comparable General Coding Performance: SecCoderX maintained strong performance on general coding benchmarks (HumanEval+, MBPP+) without needing additional general coding fine-tuning data.

Limitations and Future Work

  • Limitations: The paper acknowledges that while SecCoderX significantly improves security without sacrificing functionality, it still relies on the quality of the underlying vulnerability detection datasets and the teacher LLM used for distillation. The effectiveness of the LLM-as-a-judge for functionality evaluation in CyberSecEval SCG is noted as a potential area for further validation.
  • Future Work: The authors suggest that their framework can be extended to more complex security properties and that further research into more robust and efficient reward modeling techniques could yield even better results. The release of their code, dataset, and model checkpoints aims to facilitate future research in this critical area.

Summary updated

BibTeX
@inproceedings{wu2026secure,
  author = {Tianyi Wu and Mingzhe Du and Yue Liu and Chengran Yang and Terry Yue Zhuo and Jiaheng Zhang and See-Kiong Ng},
  title = {{Secure Code Generation via Online Reinforcement Learning with Vulnerability Reward Model}},
  booktitle = {AIWILD @ ICLR},
  year = {2026},
  url = {https://openreview.net/forum?id=hNqUwAmhVv},
}
Figure