Abstract
Developers frequently reuse smart contract code snippets from Stack Overflow (SO), but these fragments are often uncompilable and contain security vulnerabilities. This paper introduces SOChecker, a hybrid tool that leverages a fine-tuned Large Language Model (LLM) to complete fragmented code and applies symbolic execution to detect flaws. SOChecker significantly outperforms standalone LLMs like GPT-4, achieving an F1 score of 68.2% on real-world snippets.
Introduction
In the rapidly evolving blockchain ecosystem, developers often rely on community-driven platforms like Stack Overflow to resolve complex coding challenges. However, this convenience introduces a significant "security blind spot." A survey of 74 practitioners conducted by the authors revealed that while 88.4% of developers utilize SO code, less than 20% perform rigorous security evaluations on these snippets. This lack of scrutiny is primarily due to the absence of tools capable of analyzing incomplete code.
The Challenge of Fragmented Code
The primary obstacle to securing SO code is its "fragmented" nature. Snippets typically lack necessary boilerplate, imports, or structural context required for compilation. Traditional security tools, such as static analyzers or formal verifiers, require fully compilable code to function, rendering them ineffective for checking snippets at the point of copy-pasting. This creates a window where vulnerabilities are baked into projects before they can be scanned by standard CI/CD pipelines.
The SOChecker Framework
To address this, we propose SOChecker, a two-stage pipeline designed to bridge the gap between fragmented code and formal security analysis.
1. Code Completion
SOChecker utilizes a fine-tuned Llama2-13b model, trained on 1,000 high-volume Ethereum smart contracts. This model transforms function-level snippets into complete, compilable contracts. The process includes structural completion (fixing missing braces) and version adaptation to ensure compatibility with the Solidity compiler.
2. Vulnerability Detection
Once the code is compilable, SOChecker employs Symbolic Execution via the Z3 solver rather than relying on LLM-based detection, which is prone to hallucinations. A novel Control Flow Graph (CFG) pruning strategy ensures the analysis focuses strictly on the original user-provided code, ignoring potential artifacts introduced during the completion phase. The tool targets the "DASP10"—the most common smart contract vulnerabilities, including Reentrancy, Access Control flaws, and Denial of Service.
Experimental Evaluation
SOChecker was evaluated against a curated dataset of 897 Solidity snippets from Stack Overflow posts dated after October 2021 to minimize data leakage.
- Completion Rate: The fine-tuned Llama2 model successfully completed 75.5% of snippets into compilable code, outperforming GPT-3.5.
- Detection Performance: SOChecker achieved an F1 score of 68.2% on real-world snippets, vastly outperforming GPT-3.5 (20.9%) and GPT-4 (33.2%).
- Comparison to SOTA: When tested on fully completed code, SOChecker reached an 83.4% F1 score, significantly higher than traditional tools like Slither (6.6%) or Mythril (12.3%) on the same dataset.
Key Insights and Conclusion
The paper demonstrates that the synergy between LLMs and formal methods provides a robust solution for securing fragmented code. While LLMs excel at the "messy" task of code reconstruction, symbolic execution provides the "ground truth" required for security. By automating the reconstruction of incomplete snippets, SOChecker allows developers to identify vulnerabilities early in the development lifecycle, mitigating the risks associated with code reuse from public forums.
Summary updated