SmartInv: Multimodal Learning for Smart Contract Invariant Inference

Sally Junsong Wang, Kexin Pei, Junfeng Yang

Abstract

Smart contracts are foundational to decentralized finance but remain vulnerable to complex functional bugs that traditional automated tools often miss. We present SMARTINV, a multimodal framework that leverages foundation models to infer contract invariants by reasoning across source code and natural language. Utilizing a structured "Tier of Thought" (ToT) prompting strategy and a formal verification loop, SMARTINV significantly outperforms existing tools in bug detection and efficiency, uncovering 119 zero-day vulnerabilities in the wild.

Introduction

Smart contracts are notoriously difficult to secure. While "implementation bugs" (e.g., integer overflows or reentrancy) follow predictable patterns, "functional bugs" arising from high-level business logic failures are far more elusive. These bugs account for approximately 81% of exploitable vulnerabilities but are often considered "machine un-auditable" because the intended behavior is frequently described in natural language—comments, variable names, and documentation—rather than just code. Existing analyzers lack the multimodal reasoning required to bridge the gap between what the code does and what the developer intended.

Comparison of bug types and detectability. Functional bugs are often undetectable by pattern-based methods but are captured by SMARTINV's ToT approach.

Methodology

SMARTINV treats invariant inference as a multimodal learning task, bridging the gap between code implementation and developer intent. The framework operates in three distinct stages:

  1. Tier of Thought (ToT) Finetuning: A structured prompting strategy for foundation models (specifically LLaMA-7B) that decomposes reasoning into hierarchical tiers: identifying transactional context, generating specific invariants, and ranking them by bug-prevention likelihood.
  2. Multimodal Reasoning: The model processes both Solidity source code and natural language hints (docstrings, comments) to capture the full context of the contract. Quantitative tests show that removing natural language modalities causes bug detection performance to plummet by 40x.
  3. Formal Verification Loop: To mitigate LLM hallucinations, generated invariants are passed to a formal verification backend using Boogie and the Corral bounded model checker. This ensures that reported bugs are verified through rigorous program analysis rather than probabilistic guesses.
The SMARTINV workflow, illustrating the Finetuning, Inference, and Verification stages.

Datasets and Evaluation

The researchers utilized a comprehensive dataset for training and benchmarking:

  • Training Set: 2,173 annotated samples derived from 1,381 real-world contracts.
  • Evaluation Set: A large-scale collection of 89,621 real-world Solidity contracts crawled from Etherscan and public dApp repositories (2016–2023).
  • Refined Benchmark: 60 high-profile hacked projects (1,241 buggy contracts) used to measure precision and recall against known exploits.

Key Results and Insights

  • Superior Bug Detection: SMARTINV detected 4x more critical bugs than state-of-the-art tools like VeriSol and Mythril, maintaining a low false-negative rate of just 0.3%.
  • Zero-Day Discovery: The tool identified 119 zero-day vulnerabilities in the wild. Five were confirmed as "high severity," earning researchers $17,600 in bug bounties.
  • Efficiency: By avoiding the "path explosion" common in symbolic execution, SMARTINV runs 150x faster than traditional tools.
  • Structured Reasoning: The "Tier of Thought" approach proved significantly more effective than standard zero-shot or Chain of Thought prompting by forcing the model to establish context before generating code.

Limitations and Future Directions

While effective, SMARTINV faces constraints such as the 4096-token context window of LLaMA, which can limit reasoning on very large contracts. Additionally, the formal verification backend currently supports Solidity versions up to 0.7.0. Future work aims to integrate Retrieval-Augmented Generation (RAG) for larger codebases and automate the generation of exploit payloads to further validate the severity of detected vulnerabilities.

Summary updated

BibTeX
@inproceedings{wang2024smartinv,
  author = {Sally Junsong Wang and Kexin Pei and Junfeng Yang},
  title = {{SmartInv: Multimodal Learning for Smart Contract Invariant Inference}},
  booktitle = {SP},
  year = {2024},
  doi = {10.1109/sp54263.2024.00126},
}
Figure