Detecting Buggy Contracts via Smart Testing

Sally Junsong Wang, Jianan Yao, Kexin Pei, Hideaki Takahashi, Junfeng YangarXiv 2024 · OpenReview

Abstract

Smart contracts are susceptible to critical vulnerabilities. Hybrid dynamic analyses, such as concolic execution assisted fuzzing and foundation model assisted fuzzing, have emerged as highly effective testing techniques for smart contract bug detection recently. This hybrid approach has shown initial promise in real-world benchmarks, but it still suffers from low scalability to find deep bugs buried in complex code patterns. We observe that performance bottlenecks of existing dynamic analyses and model hallucination are two main factors limiting the scalability of this hybrid approach in finding deep bugs. To overcome the challenges, we design an interactive, self-deciding foundation model based system, called SmartSys, to support hybrid smart contract dynamic analyses. The key idea is to teach foundation models about performance bottlenecks of different dynamic analysis techniques, making it possible to forecast the right technique and generates effective fuzz targets that can reach deep, hidden bugs. To prune hallucinated, incorrect fuzz targets, SmartSys feeds foundation models with feedback from dynamic analysis during compilation and at runtime. The interesting results of SmartSys include: i) discovering a smart contract protocol vulnerability that has escaped eleven tools and survived multiple audits for over a year; ii) improving coverage by up to 14.3% on real-world benchmarks compared to the baselines.

Introduction

Smart contracts currently secure billions of dollars in assets, yet they remain notoriously difficult to test. In early 2024 alone, nearly $400 million was lost to exploits. Security researchers typically rely on two primary techniques: fuzzing, which involves sending random inputs to find crashes, and concolic execution, which uses symbolic mathematics to solve code constraints.

However, both methods possess significant "blind spots." Fuzzers often struggle with "magic numbers"—specific values required by conditional statements—while concolic execution is computationally expensive and frequently fails when encountering complex loops. While Large Language Models (LLMs) offer a potential solution for generating test cases, they often "hallucinate," producing syntactically invalid or ineffective code. The core challenge lies in intelligently orchestrating these tools and generating high-quality test targets for complex, multi-step transactions.

The SmartSys Framework

To address these limitations, we introduce SmartSys, an interactive system that utilizes LLMs as intelligent "traffic controllers" for security analysis. The architecture integrates fine-tuned models with traditional dynamic analysis tools to bypass traditional testing bottlenecks.

SmartSys System Architecture: Integrating fine-tuned forecast models with dynamic analysis feedback loops.

The system is built upon four main pillars:

  1. The Forecast Model: A fine-tuned model (based on GPT-4o) that analyzes a contract and its current coverage report to "self-decide" the optimal strategy—whether to utilize heavy-duty concolic execution or request a smarter fuzz target from the LLM.
  2. The Generator Model: This component produces specific function invocation sequences and seeding values designed to reach "deep" code paths that random fuzzing cannot access.
  3. Hallucination Suppression Algorithm: This creates a feedback loop, feeding compiler error messages and runtime coverage data back to the LLM to iteratively refine generated code.
  4. Tailored Concolic Engine: An EVM-optimized engine designed to handle cryptographic hashing (Keccak) and non-linear mathematical constraints.

Key Insights: Orchestration and Refinement

A primary innovation of SmartSys is the shift from using LLMs solely as code generators to using them as high-level strategy managers. By training the model on the specific limitations of dynamic analysis tools, it can predict which technique will succeed in a given context.

Furthermore, the "Critique Loop" proves essential. Rather than expecting a perfect test case in a single pass, SmartSys treats the compiler and fuzzer as critics. This iterative refinement allows the system to fix syntactical and logical errors that would otherwise prevent bug discovery.

Iterative Hallucination Suppression: The LLM corrects syntax and logic errors over multiple iterations to reach deep bugs.

Experimental Results

SmartSys was evaluated using three distinct datasets: common performance bottlenecks (D1), real-world vulnerabilities from platforms like Immunefi (D2), and a fine-tuning dataset of 121 real-world contracts (D3).

The results demonstrate significant improvements over state-of-the-art baselines:

  • Zero-Day Discovery: SmartSys identified a critical protocol vulnerability that had remained undetected by eleven other tools and multiple professional audits for over a year.
  • Enhanced Coverage: The system improved code coverage by up to 14.3% on real-world benchmarks.
  • High Precision: The Forecast Model achieved 92% accuracy in selecting the optimal analysis strategy.
  • Effective Bug Detection: SmartSys detected over 80% of true positive bugs in categories such as improper input validation and oracle manipulation, outperforming established tools like Manticore and Echidna.

Conclusion

While SmartSys represents a significant advancement, challenges remain. The hallucination suppression algorithm currently operates with iteration caps, which may be insufficient for exceptionally convoluted logic. Additionally, scaling to massive codebases with thousands of functions remains a focus for future work. Future iterations may also explore distilling the system's intelligence into smaller, open-source models to reduce reliance on proprietary APIs and enhance privacy for sensitive audits.

Summary updated

BibTeX
@misc{wang2024detecting,
  author = {Sally Junsong Wang and Jianan Yao and Kexin Pei and Hideaki Takahashi and Junfeng Yang},
  title = {{Detecting Buggy Contracts via Smart Testing}},
  year = {2024},
  eprint = {2409.04597},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2409.04597},
}
Figure