Context-Enhanced Vulnerability Detection Based on Large Language Model

Yixin Yang, Bowen Xu, Xiang Gao, Hailong Sun · DOI

Abstract

Detecting software vulnerabilities using Large Language Models (LLMs) faces a significant challenge: the trade-off between context and noise. While isolated functions often lack the necessary inter-procedural context to identify bugs, providing entire codebases introduces excessive noise and exceeds context windows. This paper presents PacVD (Primitive API Abstraction and Context-Enhanced Vulnerability Detection), a framework that utilizes static program analysis to generate concise, abstracted summaries of helper functions. By focusing on "primitive APIs"—the fundamental operations most prone to security flaws—PacVD provides LLMs with targeted context. Our evaluation shows that PacVD significantly improves detection performance across various models, with DeepSeek-R1 achieving a peak F1 score of 69.09%.

Introduction

Memory safety vulnerabilities, such as double-frees, memory leaks, and buffer overflows, are frequently inter-procedural. This means the root cause of a bug and its manifestation often reside in different functions across a codebase. Standard LLM-based detection methods often fail because they analyze functions in isolation, missing the critical logic contained in helper functions.

Conversely, a "brute-force" approach—feeding the entire codebase or all raw helper functions into an LLM—introduces massive noise, computational overhead, and often exceeds the model's context window. The authors of this paper address this "Goldilocks" problem: how to provide the LLM with enough context to understand cross-function vulnerabilities without overwhelming it with irrelevant code. The solution is PacVD, which uses static analysis to extract "just enough" abstracted context.

The PacVD Framework

The PacVD pipeline consists of three main stages: Data Preprocessing, Primitive API Abstraction, and Context-enhanced detection.

The PacVD framework workflow, illustrating the transition from raw code to abstracted API context for LLM detection.

1. Data Preprocessing

In the first stage, the system analyzes vulnerability patches to identify pre-fixed and after-fixed functions. It constructs Code Property Graphs (CPGs) to perform Call Relationship Analysis, identifying multi-layer callee functions that interact with the target code.

2. Primitive API Abstraction

Instead of feeding raw helper functions to the LLM, PacVD identifies "primitive APIs"—standard library functions like malloc, free, open, and close—that are frequent culprits in security flaws.

Mapping of primitive APIs to their associated vulnerability types used for targeted abstraction.

The framework then generates abstractions at four hierarchical levels:

  • A1 (Fuzzy Branches): A high-level summary of whether an API is called in all, some, or no branches.
  • A2 (Concrete Branches): Adds specific if/else control conditions triggering the API.
  • A3 (Number of Calls): Adds the frequency/count of the API calls.
  • A4 (Key Variables): Adds the specific variables or data objects the API operates on.

3. Context-Enhanced Detection

These abstracted summaries are appended to the target function's source code and fed into LLMs (such as GPT-4o, DeepSeek-V3/R1, and CodeLLaMA-34b). The framework utilizes various prompting strategies, including Chain-of-Thought (CoT), Role-playing, and In-Context Learning (ICL), to guide the model's reasoning.

Experimental Results and Insights

The study utilized the NVD-New dataset, consisting of 623 real-world C/C++ samples. The results yielded several key insights:

  • The Information Saturation Point: Performance generally improved from level A1 to A3. However, adding variable names at level A4 actually decreased performance. This suggests that static pointer alias analysis can introduce errors that confuse the LLM, indicating a clear limit to how much detail is beneficial.
  • Model Sensitivity: Powerful reasoning models like DeepSeek-R1 and GPT-4o performed well even with minimal abstraction. In contrast, specialized code models like CodeLLaMA-34b were highly sensitive to context, requiring detailed A2 or A3 abstractions to achieve high performance.
  • Superior Performance: PacVD consistently outperformed baselines that relied on raw code or random context. The best configuration—DeepSeek-R1 with CoT at abstraction level A3—achieved an F1 score of 69.09%, a 9.25% improvement over the closest baseline.

Conclusion

PacVD demonstrates that structured, abstracted context is superior to raw code for inter-procedural vulnerability detection. By filtering out noise and focusing on critical primitive APIs, the framework allows LLMs to catch complex bugs while maintaining high precision. Future work will focus on refining static analysis for complex pointers and extending the framework to dynamically typed languages.

Summary updated

BibTeX
@article{yang2025context,
  author = {Yixin Yang and Bowen Xu and Xiang Gao and Hailong Sun},
  title = {{Context-Enhanced Vulnerability Detection Based on Large Language Model}},
  journal = {ACM Transactions on Software Engineering and Methodology},
  year = {2025},
  doi = {10.1145/3779222},
}
Figure