Detecting Bugs with Substantial Monetary Consequences by LLM and Rule-based Reasoning

Brian Zhang, Zhuo Zhang

In the high-stakes world of decentralized finance (DeFi), a single logic error in a smart contract can lead to the instantaneous loss of hundreds of millions of dollars. In 2024 alone, $247.88 million was lost in just 20 smart contract exploits. While traditional security tools are great at catching low-level technical vulnerabilities, they often miss "accounting bugs"—errors in the actual financial logic of the program.

A new paper introduces ABAUDITOR, a hybrid system that combines the semantic "intuition" of Large Language Models (LLMs) with the rigorous logic of rule-based reasoning to secure the blockchain.

The Challenge of Accounting Bugs

The authors focus on accounting bugs: incorrect implementations of domain-specific financial models. Unlike a "reentrancy" attack (a technical exploit), an accounting bug might look like a perfectly valid piece of code that simply does the wrong math—for example, dividing a "reserve" by another "reserve" to calculate a price, which can then be manipulated by an attacker using a flash loan.

These bugs are notoriously difficult to find because:

  1. Context is King: You cannot find the bug without knowing what the variables mean (e.g., is this variable a "price," a "balance," or a "tax rate"?).
  2. LLM Limitations: While LLMs are good at understanding code comments and variable names, they struggle with long-range logic, have limited context windows, and frequently "hallucinate" (make things up).
  3. Human Effort: Previous state-of-the-art methods required humans to manually annotate every variable's financial type, which is slow, expensive, and unscalable.

Methods: A Hybrid Approach

ABAUDITOR uses a "best of both worlds" architecture, splitting the detection process into semantic understanding and deterministic logic tracking.

ABAUDITOR System Architecture
  1. Semantic Annotation (LLM): Instead of asking an LLM to find bugs directly, ABAUDITOR only asks it to label the "financial meaning" of entry-point variables (parameters and globals). It uses models like GPT-3.5 or GPT-4 to categorize variables into six types: raw balance, net rate, interest rate, debt, price, and reserve.
  2. Logic Propagation (Rule-Based): The system then uses a set of 119 manually defined inference rules to "propagate" these meanings through the code's logic. For example, if you multiply a raw_balance by a net_rate, the rule engine knows the result is a net_balance.
  3. Vulnerability Detection: If the code performs an operation that violates financial logic (e.g., adding a price to a balance), the rule engine flags it as a potential bug.
  4. The Hallucination Remedy (Iterative Feedback): To solve the hallucination problem, ABAUDITOR generates a "reasoning trace" for every flagged bug. It feeds this trace back to the LLM, asking: "Given this logic flow, was your original variable label correct?" This allows the LLM to self-correct its initial mistakes.

Key Insights

  • Decomposition is Key: The paper's biggest insight is realizing that bug detection should be split. LLMs handle the ambiguous task (semantics/meaning), while rules handle the deterministic task (logic/math).
  • Context Window Bypass: By only asking the LLM to label entry variables, the researchers avoid the "token limit" problem. The rule engine handles the thousands of lines of intermediate code that would otherwise overwhelm an LLM.
  • Self-Reflection Works: Providing the LLM with a structured "reasoning trace" of its own potential error reduced the false positive rate by a staggering 54.5%. LLMs are much better at spotting their own mistakes when shown the logical consequences of those mistakes.

Key Results

The researchers evaluated ABAUDITOR using a benchmark dataset of 34 real-world smart contracts containing 40 reported accounting bugs, as well as 8 recent projects from 2024 sourced from the auditing platform Code4rena.

Performance Results of ABAUDITOR
  • High Recall: ABAUDITOR achieved a 90.5% recall on in-scope accounting bugs across 23 real-world projects.
  • Zero-Day Discoveries: When applied to 8 recent projects, it found 4 known bugs and 2 brand-new, zero-day vulnerabilities which were subsequently reported to developers.
  • Accuracy: The LLM correctly labeled financial meanings with 75.6% accuracy compared to human experts.
  • Efficiency: Running the tool on a project costs an average of just $0.05 in API fees and takes about 2 minutes, making it vastly more efficient than human auditing.

Limitations and Future Work

Despite its success, ABAUDITOR has a few hurdles to clear. The current rule engine is path-insensitive, meaning it doesn't handle complex if-else logic perfectly (a variable might have different meanings depending on which branch of code is executed). Additionally, the system currently only recognizes 6 financial types, whereas real-world DeFi is becoming much more complex with concepts like "reward rates" or "liquidity tiers." Finally, the prototype is currently limited to Solidity.

By bridging the gap between human-like understanding and machine-like rigor, ABAUDITOR represents a significant step forward in making the "Wild West" of crypto a little bit safer for everyone.

Summary updated

BibTeX
@inproceedings{zhang2024detecting,
  author = {Brian Zhang and ZHUO ZHANG},
  title = {{Detecting Bugs with Substantial Monetary Consequences by LLM and Rule-based Reasoning}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=hB5NkiET32},
}
Figure