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

CONTRACTFIX: A Framework for Automatically Fixing Vulnerabilities in Smart Contracts

Pengcheng Fang, Peng Gao, Yun Peng, Qingzhao Zhang, Tao Xie, Dawn Song, Prateek Mittal, Sanjeev R. Kulkarni, Zhuotao Liu, Xusheng XiaoarXiv 2023

Abstract

The increased adoption of smart contracts in many industries has made them an attractive target for cybercriminals, leading to millions of dollars in losses. Deploying smart contracts with detected vulnerabilities is unacceptable, and fixing all detected vulnerabilities manually incurs high labor costs. To address this, we propose ContractFix, a novel framework that automatically generates security patches for vulnerable smart contracts. ContractFix is a general framework that incorporates different fix patterns for various vulnerability types. It conducts ensemble identification based on multiple static verification tools to identify fixable vulnerabilities, generates patches using template-based patterns informed by program analysis (dependency computation and pointer analysis), and performs static verification to guarantee the patched contract is free of vulnerabilities. Our evaluation of 144 real vulnerable contracts demonstrates that ContractFix successfully fixed 94% of detected vulnerabilities (565 out of 601) while preserving expected behaviors.

Introduction

In the blockchain ecosystem, "code is law." However, once a smart contract is deployed, its immutability becomes a double-edged sword: a single bug can lead to permanent financial loss. While automated detection tools have matured, the process of fixing these bugs remains manual, error-prone, and expensive. Existing Automated Program Repair (APR) techniques from traditional software engineering often fail in the smart contract domain because they rely on extensive test suites that contracts typically lack, or they struggle with the unique state-management logic of Solidity.

The ContractFix Framework

ContractFix is designed as an end-to-end pipeline that moves from a vulnerable source file to a verified, patched contract. The system operates in three distinct phases:

The three-phase architecture of ContractFix: Detection, Patch Generation, and Verification.

Phase I: Vulnerability Detection

To minimize false positives, ContractFix employs an ensemble approach. It integrates three state-of-the-art static analysis tools—Securify, Slither, and Smartcheck. By using a majority voting mechanism, the framework filters out noise and focuses on vulnerabilities confirmed by multiple detectors, ensuring that the subsequent patching process is targeted and efficient.

Phase II: Patch Generation

This phase is the core of the framework. ContractFix uses template-based fix patterns that are populated through deep program analysis. Unlike simple text-replacement tools, ContractFix performs:

  • Intra-procedural Data-flow Analysis: To track how variables are manipulated within functions.
  • Solidity-Specific Pointer Analysis: To distinguish between data in temporary "memory" and persistent "storage."
  • Statement Reordering: Specifically for reentrancy bugs, it implements the "Checks-Effects-Interactions" pattern by moving state updates before external calls.
Example of a reentrancy fix where state updates are moved before the external call to prevent recursive exploitation.

Phase III: Verification

After a patch is generated, the framework performs static verification to ensure the original vulnerability is gone. Furthermore, it conducts Semantic Validation by replaying historical transactions (crawled from the Ethereum mainnet) against the patched contract on a local testnet. This ensures that the security fix has not introduced regressions or altered the contract's intended business logic.

Evaluation and Results

The researchers evaluated ContractFix using a dataset of 144 contracts, including 50 with injected vulnerabilities and 94 real-world vulnerable contracts from Etherscan. The framework was tested against approximately 125,000 real-world transactions.

  • Effectiveness: ContractFix successfully fixed 94% (565 out of 601) of the detected vulnerabilities.
  • Reliability: In all transaction replay tests, the patched contracts produced state results identical to the originals, confirming zero functional regressions.
  • Efficiency: The average time to generate and verify a patch (excluding initial detection) was only 3.75 seconds.
  • Gas Impact: The security enhancements added negligible overhead, with an average cost increase of roughly $0.000027 per transaction. For reentrancy, the tool prioritizes statement reordering (low gas) over expensive global mutex locks.

Conclusion and Future Work

ContractFix demonstrates that automated repair is a viable path for securing the smart contract ecosystem. By combining ensemble detection with context-aware program analysis, it provides a high-confidence tool for developers to secure their code before deployment. While the current version focuses on major categories like Reentrancy and Missing Input Validation, future iterations aim to address blockchain-specific logic issues such as transaction-ordering dependence and complex modifier-based vulnerabilities.

Summary updated

BibTeX
@misc{fang2023contractfix,
  author = {Pengcheng Fang and Peng Gao and Yun Peng and Qingzhao Zhang and Tao Xie and Dawn Song and Prateek Mittal and Sanjeev R. Kulkarni and Zhuotao Liu and Xusheng Xiao},
  title = {{CONTRACTFIX: A Framework for Automatically Fixing Vulnerabilities in Smart Contracts}},
  year = {2023},
  eprint = {2307.08912},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2307.08912},
}
Figure