APPATCH: Automated Adaptive Prompting Large Language Models for Real-World Software Vulnerability Patching

Yu Nong, Haoran Yang, Long Cheng, Hongxin Hu, Haipeng Cai

TL;DR

This paper introduces APPATCH, an automated system that leverages Large Language Models (LLMs) to generate patches for software vulnerabilities. By employing a novel "semantics-aware scoping" and "dynamic adaptive prompting" strategy, APPATCH significantly outperforms existing methods in generating valid and correct patches for real-world vulnerabilities, even zero-day ones.

What Problem Does This Paper Address?

Software vulnerabilities are a constant threat, and timely patching is crucial. While automated patching has been a goal, existing methods struggle. Code-analysis-based approaches often require exploits or test inputs, which aren't always available, and can't handle uncompilable code. Deep learning methods require large, labeled datasets and often fail to generalize to new vulnerabilities. Even recent LLM-based approaches, while promising, often generate incorrect or no patches for real-world scenarios due to challenges in reasoning about code semantics, handling context, and avoiding hallucinations. This paper aims to bridge this gap by providing an LLM-based framework that can effectively and automatically generate patches for real-world vulnerabilities without requiring extensive training data or fine-tuning.

Methods

APPATCH is built around an "automated adaptive prompting methodology" for LLMs. Its core components include:

  • Semantics-Aware Scoping: This technique narrows down the analysis to the essential code segments that directly contribute to a vulnerability. It achieves this by constructing a System Dependence Graph (SDG) and performing interprocedural backward slicing from vulnerability locations to external inputs. This reduces the input size for LLMs, mitigating context limitations and improving focus.
  • Vulnerability Semantics Reasoning: LLMs are guided to analyze vulnerabilities using Chain-of-Thought (CoT) prompting, focusing on the "vulnerability semantics" (the code aspects that make it vulnerable). This helps elicit more accurate root cause analysis.
  • Exemplar Mining: To provide effective examples for prompting, APPATCH automatically mines exemplars from existing vulnerability-fixing samples. It generates root cause analyses and fixing strategies for these exemplars using LLMs.
  • Dynamic Adaptive Prompting: Instead of using fixed prompts, APPATCH dynamically selects the most relevant exemplars from its mined pool based on the root cause analysis of the target vulnerability. This adaptive approach ensures that the LLM receives tailored guidance.
  • Multi-Faceted Patch Validation: To combat LLM hallucinations and ensure patch quality, APPATCH uses an ensemble of LLMs to validate candidate patches for both correctness (fixing the vulnerability) and functionality preservation.

Datasets

The paper utilizes several datasets:

  • Exemplar Mining Datasets: PatchDB (12,073 real-world fixing samples) and CVEFixes (4,120 real-world fixing samples) are used to mine exemplars. Samples are filtered for relevance and cover common CWEs like CWE-787, CWE-125, CWE-190, CWE-401, CWE-457, and CWE-476.
  • Testing Datasets:
    • Zero-Day Dataset: A newly collected dataset of 97 vulnerabilities from 18 open-source projects, reported after the LLMs' cutoff dates to mitigate data leakage. This dataset includes 21 interprocedural vulnerabilities.
  • ExtractFix Dataset: An existing dataset of 20 reproducible vulnerabilities used for comparison with traditional techniques.

Key Insights and Ideas

  • Prompting is Key: The effectiveness of LLMs for vulnerability patching is highly dependent on the prompting strategy. Simple or standard prompting is insufficient; sophisticated, guided prompting is crucial.
  • Vulnerability Semantics Matter: Focusing LLM analysis on the "vulnerability semantics" (the core code responsible for the vulnerability) significantly improves accuracy and reduces distractions compared to analyzing the entire source code.
  • Dynamic Adaptation Outperforms Static: Dynamically selecting exemplars based on the specific vulnerability's root cause leads to better patch generation than using random or fixed exemplars.
  • LLMs as Collaborators, Not Replacements: While LLMs are powerful, they still benefit from traditional code analysis techniques (like slicing) to handle context limitations and provide focused input.
  • Validation is Non-Negotiable: LLMs can hallucinate. A multi-faceted validation process using multiple LLMs is essential to ensure generated patches are both correct and preserve functionality.

Key Results

APPATCH demonstrates significant improvements over baseline methods:

  • Effectiveness: On the Zero-Day dataset, APPATCH achieves F1 scores up to 36.46% (Claude-3.5) and on the ExtractFix dataset, up to 73.86% (Claude-3.5). This is a substantial improvement over standard prompting (e.g., 23.01% and 60.42% F1 respectively) and zero-shot completion (e.g., 12.48% and 24.66% F1).
  • Comparison to SOTA: APPATCH outperforms SOTA traditional vulnerability patching approaches, achieving up to 49.48% recall on the Zero-Day dataset compared to VulRepair's 17.53%, and up to 90.00% recall on the ExtractFix dataset compared to ExtractFix's 85%.
  • Efficiency: Generating a patch takes an average of 37.148-50.209 seconds and uses 5,684-6,802 tokens per sample. The cost per sample is between $0.0026 (Gemini-1.5) and $0.0823 (GPT-4).
  • Component Contributions: Ablation studies show that semantics-aware scoping, dynamic adaptive prompting, and multi-faceted validation all contribute significantly to APPATCH's performance. Removing these components leads to noticeable drops in effectiveness.

Limitations and Future Work

  • Root Cause Analysis Accuracy: The accuracy of the initial root cause analysis is critical. If incorrect, it can mislead the patching process.
  • Dataset Size and Quality: The current datasets, while extensive, are limited in size and may not cover all vulnerability types, patterns, or be fully compilable with test cases. Building larger, more comprehensive datasets with compilable code and exploits is a future direction.
  • Extensibility: While designed to be extensible to other CWEs and languages, the current evaluation is focused on C and specific CWEs.
  • Interprocedural Vulnerabilities: While APPATCH shows promise, there's a slight performance drop on exclusively interprocedural vulnerabilities compared to its performance on simpler cases.
  • Upstream Tool Dependency: For practical end-to-end application, APPATCH relies on accurate vulnerability detection, localization, and classification from upstream tools. The accuracy of these tools directly impacts APPATCH's performance.

Future work could involve integrating more advanced upstream vulnerability analysis tools, exploring more sophisticated LLM prompting techniques, and expanding the datasets to cover a wider range of vulnerabilities and programming languages.

Summary updated

BibTeX
@inproceedings{nong2025appatch,
  author = {Yu Nong and Haoran Yang and Long Cheng and Hongxin Hu and Haipeng Cai},
  title = {{APPATCH: Automated Adaptive Prompting Large Language Models for Real-World Software Vulnerability Patching}},
  booktitle = {USENIX Security},
  year = {2025},
}
Figure