arXiv:2606.03489cs.CRcs.AI2026-06中稿 · ICML

让大模型通过自博弈纠错,精准修复代码漏洞。

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

论文配图:Learn from Your Mistakes: Tree-like Self-Play for Secure Code LLMs
图 1 · 摘自论文原文
  • 构建决策树模拟自博弈,逐节点识别并修正错误
  • 在Python安全测试中准确率提升至75.8%,优于传统方法
  • 能跨语言迁移安全逻辑,有效防范未知漏洞

尽管大型语言模型在代码生成方面表现优异,但仍易复制训练数据中的细微但关键的安全漏洞。当前对齐技术如监督微调(SFT)和强化学习通常在序列层面进行粗粒度优化,难以应对漏洞的局部性——一个错误的词元选择即可破坏整个程序。为此,我们提出树状自博弈(TSP)框架,将安全代码生成重构为细粒度的序列决策过程。与盲目最大化似然的标准方法不同,TSP构建决策树,让模型探索分支路径,生成安全的‘黄金路径’和有漏洞的变体。通过将代码生成视为自博弈游戏,模型学会严格区分自身局部错误,从而在漏洞常出现的关键决策节点获得密集的、在策略的学习信号,实现精准自我纠正。实验表明,TSP显著提升模型可靠性:在Python安全基准测试中,CodeLlama-7B的通过率(SPR@1)达75.8%,远超SFT(57.0%)和无结构自博弈基线。关键的是,TSP实现了稳健的分布外泛化:模型不仅使未见漏洞类别(CWEs)减少24.5%,还能将从C/C++中学到的安全原则成功迁移到Python、Go和JavaScript等多语言中,表明TSP并非简单记忆补丁,而是内化了抽象的、与语言无关的安全逻辑。

原文摘要 · Abstract (English)

While Large Language Models (LLMs) excel in code generation, they remain prone to replicating subtle yet critical vulnerabilities endemic to their training data. Current alignment techniques, such as Supervised Fine-Tuning (SFT) and Reinforcement Learning (RL), typically apply coarse-grained optimization at the sequence level. This approach often fails to address the localized nature of security flaws, where a single incorrect token choice can compromise an entire program. To bridge this gap, we introduce Tree-like Self-Play (TSP), a framework that reframes secure code generation as a fine-grained sequential decision process. Unlike standard methods that blindly maximize likelihood, TSP constructs a decision tree where the model explores branching trajectories--generating both secure "golden paths" and vulnerable variants. By treating code generation as a self-play game, the model learns to strictly discriminate against its own localized errors. This provides a dense, on-policy learning signal that forces self-correction precisely at the critical decision nodes where vulnerabilities typically emerge. Our experiments demonstrate that TSP fundamentally enhances model reliability. In Python security benchmarks, TSP boosts CodeLlama-7B's pass rate (SPR@1) to 75.8%, significantly outperforming SFT (57.0%) and unstructured self-play baselines. Crucially, TSP induces robust out-of-distribution generalization: the model not only reduces vulnerabilities in unseen categories (CWEs) by 24.5% but also successfully transfers security principles learned from C/C++ to diverse languages, including Python, Go, and JavaScript. This suggests that TSP does not merely memorize patches, but internalizes abstract, language-agnostic security logic.

代码生成安全检测自博弈LLM对齐

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