SPARK: Security Knowledge Priming and Representation-Guided Knowledge Activation for LLM-based Secure Code Generation
作者: Xiaoyun Xu, Lichao Wu, Jona te Lintelo, Siyu Zhang, Stjepan Picek
分类: cs.CR, cs.AI
发布日期: 2026-06-15
💡 一句话要点
提出SPARK以解决大语言模型安全代码生成问题
🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)
关键词: 安全代码生成 大型语言模型 知识激活 公共弱点枚举 推理时工具 模型优化 机器学习
📋 核心要点
- 现有的大语言模型在代码生成中存在安全缺陷,主要原因是缺乏安全知识的激活。
- SPARK通过在推理时激活模型中已有的安全知识,避免了重训练的高成本。
- 在C++、Java和Python的实验中,SPARK在各项设置中均超越了7个基线方法,保持了实用性。
📝 摘要(中文)
大型语言模型在生成代码时常常存在可被利用的安全缺陷。现有文献认为这一局限性源于缺乏安全专业知识,导致当前的防御机制依赖于重训练或外部知识检索,增加了计算开销和数据偏差。本文提出SPARK,一个推理时的安全工具,旨在激活模型中潜在的安全知识,而无需重训练。SPARK由两个部分组成:第一部分为每个编码任务检索相关的公共弱点枚举(CWE)条目,并将简短的结构化提示附加到提示中;第二部分在每个解码步骤中向logits添加预计算的令牌偏置。实验结果表明,SPARK在多个开源模型上表现优异,能够匹配或超越最佳基线,同时保持HumanEval的实用性。
🔬 方法详解
问题定义:本文旨在解决大型语言模型在代码生成中存在的安全缺陷问题。现有方法通常依赖重训练或外部知识检索,导致计算开销大且存在数据偏差。
核心思路:SPARK的核心思路是通过推理时的安全知识激活,利用模型已有的安全表示,而不需要进行重训练。通过简短的提示和令牌偏置,激活潜在的安全知识。
技术框架:SPARK的整体架构分为两个主要模块:第一部分检索相关的CWE条目并附加提示,第二部分在解码过程中添加预计算的令牌偏置。
关键创新:SPARK的创新在于通过简单的提示和偏置激活模型的安全知识,而不是依赖于复杂的重训练或外部知识检索,这与现有方法有本质区别。
关键设计:在设计中,第一部分通过检索CWE条目生成结构化提示,第二部分通过计算安全方向向量的单位差异来生成令牌偏置,偏置计算仅需一次离线处理,应用时仅需进行向量加法。
🖼️ 关键图片
📊 实验亮点
在对9个开源模型的评估中,SPARK在C++、Java和Python的代码生成任务中均表现出色,超越了7个基线方法,且在每个设置中均匹配或改善了最佳基线,同时保持了HumanEval的实用性。
🎯 应用场景
SPARK的研究成果在安全代码生成领域具有广泛的应用潜力,能够帮助开发者在编写代码时自动识别和避免安全漏洞。未来,该方法可以扩展到其他编程语言和应用场景,提升软件开发的安全性和效率。
📄 摘要(原文)
Large language models routinely generate code with exploitable security flaws. Prior literature attributes this limitation to a lack of security expertise, steering current defense mechanisms toward heavy fine-tuning or external knowledge retrieval, which introduces significant computational overhead and data bias through redundant code examples. Contrary to this view, we argue that pretraining corpora are already rich in security material. The bottleneck is activation: without an explicit and brief cue, statistical pressure toward common training-distribution patterns suppresses the model's safety-relevant representations. We present SPARK, an inference-time security harness that activates this latent knowledge without any retraining. The harness has two parts. Component~I retrieves a few of the relevant Common Weakness Enumeration (CWE) entries for each coding task and appends a short structured cue to the prompt; this alone is enough to surface the model's existing security representations. Component~II adds a precomputed token bias to the logits at every decoding step. We obtain the bias by projecting a safe-direction vector, the unit difference between the mean safe and mean unsafe last-layer hidden states, through the language model head. The bias is computed once offline; applying it costs a single vector addition per generated token. We evaluate SPARK on 9 open-source models across C++, Java, and Python, and compare with 7 baselines spanning fine-tuning and retrieval-augmented methods. SPARK matches or improves on the best baseline in every setting while preserving HumanEval utility. We further test Component~I in a black-box setting on 7 of today's strongest models, including Claude, DeepSeek, and GPT, demonstrating the bottleneck of insecure code generation and the improvements enabled by our method.