NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Chain-of-Thought Prompting of Large Language Models for Discovering and Fixing Software Vulnerabilities

Yu Nong, Mohammed Aldeen, Long Cheng, Hongxin Hu, Feng Chen, Haipeng CaiarXiv 2024 · DOI

Introduction

Software security traditionally relies on static and dynamic analysis, which often struggle with high false-positive rates and limited scalability. While Large Language Models (LLMs) like GPT-3.5 offer a promising "zero-shot" alternative for code analysis, they frequently fail to grasp complex logical dependencies, often getting distracted by irrelevant code segments. To bridge this gap, researchers have introduced Vulnerability-Semantics-guided Prompting (VSP). This specialized Chain-of-Thought (CoT) strategy guides models to reason about specific data and control flow paths, leading to dramatic improvements in vulnerability identification, discovery, and patching.

Methodology: Vulnerability-Semantics-guided Prompting (VSP)

Unlike standard prompting or naive CoT—which might simply ask a model to explain code line-by-line—VSP focuses the model's attention on the underlying semantics of a vulnerability. The approach is structured around three core tasks:

  1. Vulnerability Identification: Determining if a specific CWE exists in the code.
  2. Vulnerability Discovery: Identifying any present vulnerabilities without prior hints.
  3. Vulnerability Patching: Generating a valid code fix for the identified bug.

The VSP strategy employs a "Thought-Chain" process. The Thought phase identifies the critical data and control flow facts, while the Chain phase traces the specific execution paths that trigger the exploit.

Exemplars for VSP tasks: identification, discovery, and patching.

Experimental Evaluation

The study evaluated VSP against the "Top 5" most dangerous C/C++ vulnerabilities (including CWE-787, 125, 476, 416, and 190) using two primary datasets:

  • SARD: Synthetic test cases for baseline assessment.
  • CVE Dataset: Real-world vulnerabilities from production code, presenting a significantly higher challenge.

Testing was conducted across three major LLMs: GPT-3.5-Turbo, Llama2-7B, and Falcon-7B.

Key Results and Performance

VSP demonstrated substantial performance gains over standard prompting, few-shot learning, and naive CoT, particularly on complex real-world data. On the CVE dataset, VSP achieved a 553.3% improvement in F1-score for identification and a 30.8% increase in patching accuracy.

Performance comparison (F1-score) of VSP against baseline strategies across different models and datasets.

Furthermore, VSP showed strong "zero-day" potential. In tests on 55 vulnerabilities reported after the models' training cutoffs, VSP correctly discovered 40% of them, compared to just 16% using standard prompting.

Insights and Analysis

The researchers identified several critical factors influencing LLM performance in security tasks:

  • The "Oblivion" Effect: Models often lose track of the specific vulnerability type during long reasoning chains. Explicitly re-defining the CWE or placing the task question at the end of the prompt helps maintain focus.
  • Semantics over Syntax: Naive line-by-line analysis can actually degrade performance by introducing noise. VSP’s success confirms that guiding models toward flow dependencies is essential for deep code reasoning.
  • Contextual Bottlenecks: Many failures stem from "inter-procedural" vulnerabilities where the root cause lies in external files not provided in the prompt's context window.

Conclusion and Future Directions

VSP represents a significant step forward in using LLMs for automated software defense. However, challenges remain regarding context window limitations and extremely complex pointer logic. Future work suggests a hybrid approach: using traditional static analysis tools to "slice" relevant code segments, thereby providing LLMs with a focused, high-density context for final reasoning and patching.

Summary updated

BibTeX
@misc{nong2024chain,
  author = {Yu Nong and Mohammed Aldeen and Long Cheng and Hongxin Hu and Feng Chen and Haipeng Cai},
  title = {{Chain-of-Thought Prompting of Large Language Models for Discovering and Fixing Software Vulnerabilities}},
  year = {2024},
  eprint = {2402.17230},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2402.17230},
}
Figure