Abstract
The rapid growth of Decentralized Finance (DeFi) has been accompanied by a surge in smart contract exploits, resulting in billions of dollars in losses. Traditional security tools, while useful, often struggle with high false-positive rates or prohibitive computational costs. This paper presents Detect Llama, a suite of models based on Meta’s Code Llama and OpenAI’s GPT-3.5 Turbo, fine-tuned specifically for smart contract vulnerability detection. Our research demonstrates that domain-specific fine-tuning allows these models to significantly outperform general-purpose frontier models like GPT-4. By employing a dual-perspective training strategy—focusing on both detection and generation—Detect Llama achieves superior F1 scores across various vulnerability classes, proving that specialization can overcome the limitations of raw model scale in technical security domains.
Introduction
Smart contract security is a critical bottleneck for the blockchain ecosystem. Current automated solutions typically fall into two categories: static analysis and dynamic analysis. Static analysis tools are fast but frequently produce false positives, leading to "alert fatigue." Dynamic analysis tools, such as fuzzers or formal verification, are more accurate but are often slow and require significant manual configuration.
Large Language Models (LLMs) represent a potential "middle way," offering the speed of static analysis with a more nuanced understanding of complex code logic. However, general-purpose models like GPT-4 often lack the specific adversarial reasoning required to identify subtle smart contract bugs. This study investigates whether fine-tuning open-source and proprietary models on specialized security datasets can bridge this performance gap.
Methodology
The researchers developed three primary models to test the efficacy of fine-tuning:
- Detect Llama - Foundation: Based on the Code Llama 34b base model.
- Detect Llama - Instruct: Based on the instruction-tuned variant of Code Llama 34b.
- GPT-3.5FT: A fine-tuned version of OpenAI’s GPT-3.5 Turbo.
The Dual-Perspective Strategy
To move beyond simple pattern matching, the models were trained using a dual-perspective prompt strategy. This involved training the models to both detect vulnerabilities in existing code and generate vulnerable code from scratch. This approach encourages the model to internalize the underlying logic and structural weaknesses of specific vulnerabilities. To manage the computational demands of the 34b-parameter models, the team utilized QLoRA (Quantized Low-Rank Adaptation) and Flash Attention V2 for efficient training on high-end hardware.
Data and Evaluation
Training Dataset
The models were trained on the ScrawlD dataset, which consists of real-world Ethereum smart contracts. The dataset includes labels for eight distinct vulnerability types, including Reentrancy, Locked Ether, and Arithmetic overflows. The researchers processed this data into approximately 17,000 specialized prompts.
Evaluation Framework
Performance was measured using a custom test set of 600 smart contracts. A notable constraint of the study is the focus on Solidity 0.4.x. This version was selected because the legacy tools used to establish the "ground truth" labels for the training data do not support the more modern syntax of Solidity 0.8.x.
Results and Key Insights
The evaluation focused on two primary tasks: Binary Classification (determining if a contract is vulnerable) and Vulnerability Identification (classifying the specific type of bug).
Specialization vs. Scale
The results revealed a clear advantage for fine-tuned models over larger, general-purpose counterparts. In binary classification, GPT-3.5FT (0.776 F1) and Detect Llama - Foundation (0.68 F1) both outperformed GPT-4 (0.66) and GPT-4 Turbo (0.675).
In the more difficult task of detailed identification, the gap widened significantly. GPT-3.5FT achieved a weighted F1 score of 0.61, while Detect Llama - Foundation reached 0.56. In contrast, GPT-4 and GPT-4 Turbo lagged behind with scores of 0.218 and 0.243, respectively. This suggests that for technical niches like blockchain security, domain-specific data and steerability are more impactful than raw parameter count.
Foundation vs. Instruct Models
An unexpected finding was that the Foundation version of Code Llama performed better than the Instruct version after fine-tuning. This suggests that the pre-existing conversational tuning in "Instruct" models may create a bias that interferes with the acquisition of highly specialized technical knowledge during the fine-tuning process.
Vulnerability-Specific Performance
LLM performance was not uniform across all bug types. The models were highly effective at identifying Arithmetic (integer overflows) and Locked Ether vulnerabilities, with GPT-3.5FT reaching an F1 of 0.719 on these categories. However, they struggled with more ephemeral or context-dependent issues, such as Timestamp Ordering.
Limitations and Future Work
While the results are promising, several challenges remain:
- Language Versioning: The reliance on Solidity 0.4.x is a significant limitation, as modern contracts (0.8.x+) have built-in protections against common bugs like arithmetic overflows. Future work must focus on developing modern datasets.
- Hardware Requirements: Running 34b-parameter models requires substantial VRAM (e.g., an A100 80GB GPU). Investigating whether smaller models (7b or 13b) can achieve similar results through better data curation is a priority.
- Multi-Step Reasoning: Initial tests involving a "Critic" step—where one LLM reviews the output of another—did not consistently improve performance, indicating that multi-step reasoning for security audits requires further refinement.
Conclusion
Detect Llama demonstrates that specialized fine-tuning is a viable path toward creating automated, LLM-based security auditors. By outperforming GPT-4, these models show that the future of AI-driven security may lie in smaller, domain-specific models rather than ever-larger general-purpose systems.
Summary updated