When ChatGPT Meets Smart Contract Vulnerability Detection: How Far Are We?

Chong Chen, Jianzhong Su, Jiachi Chen, Tingting Bi, Jianxing Yu, Yanlin Wang, Xingwei Lin, Ting Chen, Zibin Zheng

TL;DR

This paper investigates ChatGPT's effectiveness in detecting smart contract vulnerabilities, finding it has high recall but low precision, often generating false positives. While it covers a broad range of vulnerabilities and is efficient, its performance is inconsistent and limited by context length, highlighting the need for further development before it can reliably replace specialized tools.

What Problem Does This Paper Address?

The rapid adoption of blockchain technology has led to an increased reliance on smart contracts. However, vulnerabilities in these contracts can lead to significant financial losses. While traditional static and dynamic analysis tools exist, the emergence of powerful Large Language Models (LLMs) like ChatGPT presents a new avenue for vulnerability detection. This paper aims to empirically assess how well ChatGPT performs in this domain, identifying its strengths, weaknesses, and limitations compared to existing tools.

Methods

The authors conducted an empirical study using ChatGPT (specifically GPT-3.5-turbo, GPT-4o, and GPT-4) to detect smart contract vulnerabilities. Their methodology involved:

  1. Prompt Engineering: Developing and optimizing prompts to guide ChatGPT's analysis.
  2. Effectiveness Evaluation (RQ1): Evaluating ChatGPT's performance using metrics like precision, recall, and F-score on a curated dataset of vulnerable smart contracts. They also analyzed the root causes of false positives.
  3. Comparative Analysis (RQ2): Comparing ChatGPT's detection performance against 14 other state-of-the-art vulnerability detection tools.
  4. Limitation Assessment (RQ3): Investigating ChatGPT's reliability through repeated detections of the same contracts and estimating its context length limitations.

Datasets

The primary dataset used in this study is the smartbugs-curated dataset. This dataset contains 142 vulnerable Solidity smart contracts, meticulously labeled with specific vulnerability types according to the DASP10 taxonomy. The authors note that this dataset is widely used in the community and provides a good basis for evaluating vulnerability detection tools.

Key Insights and Ideas

  • High Recall, Low Precision: ChatGPT is good at finding most vulnerabilities (high recall) but often flags non-existent ones (low precision), leading to many false positives.
  • Context Matters (and is Limited): ChatGPT's performance is significantly impacted by the length of the smart contract code it analyzes, failing on longer contracts due to token limitations.
  • Vulnerability-Specific Performance: ChatGPT performs better on some vulnerabilities (like "Unchecked Return Values," "Denial of Service," and "Front Running") than others, suggesting its understanding is not uniform across all types.
  • False Positives Have Patterns: The paper identifies key reasons for ChatGPT's false positives, including over-reliance on protected mechanisms, misinterpreting comments, development intent bias, and issues with dead code.
  • Breadth vs. Depth: While ChatGPT can detect a wider range of vulnerabilities than many specialized tools, its accuracy (depth) is often lower.
  • Efficiency is a Strength: ChatGPT is remarkably efficient, often outperforming traditional tools in terms of detection speed.

Key Results

  • Recall: GPT-4 achieved a recall rate of up to 88.2%, while precision remained low, around 22.6%.
  • Comparison: ChatGPT outperformed other tools in detecting "Denial of Service" and "Front Running" but was inferior to the average level of other tools for 5 out of 7 examined vulnerabilities.
  • Efficiency: GPT models completed the detection of 142 smart contracts in under 12 minutes, significantly faster than most other tools.
  • Uncertainty: Approximately 20% of ChatGPT's conclusions were unreliable when tested for consistency across multiple runs.
  • Context Length: The maximum code size detectable was estimated to be around 33KB for GPT-4 and 16KB for GPT-3.5.
  • False Positives: "Lack of understanding of actual meaning" was the most common reason for false positives (50.9%), followed by "Protected Mechanism Bias" (33.1%).

Limitations and Future Work

Limitations: * Uncertainty: ChatGPT can produce inconsistent results for the same input, diminishing reliability. * Context Length: Token limitations restrict the analysis of long smart contracts, leading to detection failures. * Lack of Execution Context: As an LLM, ChatGPT analyzes code as text, lacking the ability to truly execute it, which hinders detection of vulnerabilities dependent on specific execution paths. * Domain-Specific Knowledge: ChatGPT's understanding of specialized smart contract semantics and vulnerabilities is not always perfect.

Future Work: * Explore alternative LLMs like Code Llama. * Utilize more diverse datasets. * Apply techniques like fine-tuning and retrieval-augmented generation to improve performance. * Investigate the impact of updated LLM versions. * Integrate domain knowledge and enhance model architectures to improve contextual understanding and execution capabilities.

Summary updated

BibTeX
@article{chen2023when,
  author = {Chong Chen and Jianzhong Su and Jiachi Chen and Tingting Bi and Jianxing Yu and Yanlin Wang and Xingwei Lin and Ting Chen and Zibin Zheng},
  title = {{When ChatGPT Meets Smart Contract Vulnerability Detection: How Far Are We?}},
  journal = {ACM Trans. Softw. Eng. Methodol.},
  year = {2023},
}
Figure