跳转至

探究预填充阶段:通过潜在激活检测代码漏洞

文章背景与核心概要

当前,基于大语言模型(LLM)的代码生成技术已广泛应用于关键任务流程中。然而,针对模型生成代码的防御手段大多依赖于事后评估,例如使用静态分析工具、微调分类器或 LLM 判别器来检查已生成的代码,这些方法往往忽略了模型在处理代码时的内部状态。

本文探讨了一个更具前瞻性的技术问题:当 LLM 读取 C/C++ 代码作为上下文时,其隐藏层激活值是否已经包含了关于该代码漏洞状态的信号?研究人员通过提取四个不同模型家族的 LLM 潜在激活值,并训练轻量级 MLP 探针,证明了这些模型原生的表示形式无需大规模微调即可有效筛选代码漏洞,为构建高效、模型原生的安全防御机制提供了新思路。


元数据


摘要

基于 LLM 的代码生成技术现已嵌入关键任务流水线中,但针对漏洞输出的防御手段仍停留在事后处理阶段——即通过静态分析器、微调分类器或 LLM 判别器来筛选已完成的代码,而忽略了生成模型自身的内部状态。

我们测试了一个更狭窄且可直接测量的问题:当 LLM 读取一段 C/C++ 代码作为上下文时,其隐藏层激活值是否已经携带了关于该代码漏洞状态的信号?

LLM-based code generation is now embedded in mission-critical pipelines, but defenses against vulnerable output remain post-hoc — static analyzers, fine-tuned classifiers, or an LLM judge that screen completed code, ignoring the generating model's own internal state.

We test a narrower, directly measurable question: When an LLM reads a piece of C/C++ code as context, do its hidden activations already carry a signal about that code's vulnerability status?

方法论与结果

  • 评估模型: Granite-4.1-8B, Qwen3.5-9B, Qwen3.6-27B 和 Gemma-4-12B(涵盖三个不同的模型家族)。
  • 方法: 提取预填充(prefill)阶段最后一个 token 的激活值,并训练 MLP 探针(参数量为 13.4M–16.0M,占基础模型规模的 0.2% 以下)。
  • 基准测试: 在四个函数级 C/C++ 基准测试(Devign, Big-Vul, Draper VDISCPrimeVul)上进行了测试。
  • 主要发现:
  • 探针平均 F1 分数达到 41.7%
  • Devign 基准测试中,表现最好的探针(Qwen3.5-9B,达到 68.8% F1)与已发表的微调分类器 SOTA(67.9%)持平,且无需对通用 LLM 进行任何微调。
  • 在更困难、更不平衡的基准测试(Big-Vul, Draper VDISCPrimeVul)中,探针的表现明显落后于当前的 SOTA。
  • Models Evaluated: Granite-4.1-8B, Qwen3.5-9B, Qwen3.6-27B, and Gemma-4-12B (spanning three distinct model families).
  • Approach: Extracted last prefill token activations and trained MLP probes (13.4–16.0M parameters, representing under 0.2% of the base-model size).
  • Benchmarks: Tested across four function-level C/C++ benchmarks (Devign, Big-Vul, Draper VDISC, and PrimeVul).
  • Key Findings:
  • Probes achieve an average of 41.7% F1.
  • On the Devign benchmark, the top-performing probe (Qwen3.5-9B, achieving 68.8% F1) matches the published fine-tuned-classifier SOTA (67.9%) despite processing only a frozen, general-purpose LLM's activations.
  • On harder, more imbalanced benchmarks (Big-Vul, Draper VDISC, and PrimeVul), the probes trail current SOTA substantially.

核心结论

这项工作提供了初步证据,表明编码 LLM 对任意代码的内部表示本质上包含了关于其安全状态的信息,这为开发高效、模型原生的漏洞筛选机制铺平了道路。

This work provides early evidence that a coding LLM's internal representation of arbitrary code is inherently informative regarding its security status, paving the way for efficient, model-native vulnerability screening mechanisms.