File size: 4,927 Bytes
832a08e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc1796d
0345e18
cc1796d
 
 
 
0345e18
cc1796d
0345e18
cc1796d
 
 
0345e18
cc1796d
0345e18
cc1796d
 
 
 
 
0345e18
832a08e
 
cc1796d
 
 
832a08e
 
 
0345e18
 
 
 
 
 
 
 
 
 
 
 
 
 
832a08e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc1796d
0345e18
cc1796d
 
 
 
0345e18
832a08e
 
cc1796d
 
 
 
832a08e
 
 
cc1796d
 
 
 
832a08e
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
---
license: mit
library_name: transformers
pipeline_tag: text-classification
base_model: microsoft/graphcodebert-base
tags:
  - code
  - solidity
  - smart-contracts
  - vulnerability-detection
  - graphcodebert
  - roberta
language:
  - en
metrics:
  - accuracy
  - f1
model-index:
  - name: graphcodebert-vulnerability-detector
    results:
      - task:
          type: text-classification
          name: Solidity vulnerability classification
        dataset:
          name: SmartBugs-Wild (subset, tool-consensus labels)
          type: smartbugs-wild
        metrics:
          - type: accuracy
            value: 0.5622
            name: Test accuracy
          - type: f1
            value: 0.4655
            name: Macro F1
---

# Graph CodeBERT β€” Solidity Vulnerability Detector

Fine-tuned [`microsoft/graphcodebert-base`](https://huggingface.co/microsoft/graphcodebert-base) for **Solidity smart-contract vulnerability type classification**.

Part of [SolidityGuard](https://github.com/tanaymitra54/solidity_guard_razorpay) β€” used as a first-pass detector alongside Slither and an LLM auditor.

## How it fits in SolidityGuard

1. **Input:** Solidity source  
2. **Detectors:** Slither / patterns + **this Graph CodeBERT model**  
3. **LLM agents:** Scanner β†’ Analyzer β†’ Exploit Gen / Fix Suggester  
4. **Output:** Audit findings with severity and confidence  

## Model

- Tokenizer β†’ `max_length=512`  
- Graph CodeBERT encoder (~125M params)  
- Linear classification head β†’ 12-class softmax β†’ label + confidence  

## Training

1. SmartBugs-Wild contracts (capped at 5,000)  
2. Labels from SmartBugs-Results tool consensus (β‰₯2 tools agree on a category; else `safe`)  
3. Split 70 / 15 / 15 (train / val / test) with light augmentation  
4. Fine-tune `microsoft/graphcodebert-base` with early stopping on validation macro-F1  
5. Best checkpoint published here  

## Intended use

- Input: Solidity source code (string)  
- Output: one of 12 labels + confidence  
- Best as a **screening** signal, not a sole security audit  

## Labels

| ID | Label | Typical severity hint |
|----|-------|------------------------|
| 0 | `safe` | β€” |
| 1 | `reentrancy` | Critical |
| 2 | `access_control` | Critical |
| 3 | `tx_origin_auth` | Critical |
| 4 | `integer_overflow` | Critical |
| 5 | `unsafe_delegatecall` | Critical |
| 6 | `weak_randomness` | Medium |
| 7 | `unbounded_loop` | Medium |
| 8 | `redundant_storage` | Low |
| 9 | `gas_optimization` | Low |
| 10 | `best_practice` | Low |
| 11 | `other` | Medium |

## Held-out test metrics

| Metric | Value |
|--------|-------|
| Accuracy | 0.562 |
| Macro F1 | 0.466 |
| F1 `safe` | 0.694 |
| F1 `integer_overflow` | 0.634 |
| F1 `reentrancy` | 0.461 |
| F1 `other` | 0.340 |
| F1 `access_control` | 0.200 |

Labels are noisy (static-analysis consensus), so scores are moderate by design.

## Quick start

```python
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

repo = "tanaymitra01/graphcodebert-vulnerability-detector"
tok = AutoTokenizer.from_pretrained(repo)
model = AutoModelForSequenceClassification.from_pretrained(repo)
model.eval()

code = """
pragma solidity ^0.8.0;
contract Vault {
    mapping(address => uint) public bal;
    function withdraw() public {
        uint amount = bal[msg.sender];
        (bool ok,) = msg.sender.call{value: amount}("");
        require(ok);
        bal[msg.sender] = 0;
    }
}
"""
inputs = tok(code, return_tensors="pt", truncation=True, max_length=512)
with torch.no_grad():
    probs = torch.softmax(model(**inputs).logits, dim=-1)[0]
pred = int(probs.argmax())
print(model.config.id2label[pred], float(probs[pred]))
```

### With SolidityGuard

```bash
export GRAPHCODEBERT_PATH=tanaymitra01/graphcodebert-vulnerability-detector
```

## Where to get the weights

| Location | Path |
|----------|------|
| **Hugging Face (recommended)** | [`tanaymitra01/graphcodebert-vulnerability-detector`](https://huggingface.co/tanaymitra01/graphcodebert-vulnerability-detector) |
| GitHub LFS | [`training/checkpoints/best/`](https://github.com/tanaymitra54/solidity_guard_razorpay/tree/main/training/checkpoints/best) in the SolidityGuard repo |

## Files

- `model.safetensors` β€” weights  
- `config.json` β€” RobertaForSequenceClassification config + label maps  
- `label_map.json` β€” label list / id maps used in training  
- `README.md` β€” this model card  

## Limitations

- Tool-derived labels β‰  audited ground truth  
- Truncation at 512 tokens; large contracts lose context  
- Rare classes (e.g. access control) have low F1  
- Not a replacement for professional security review  

## Citation

```bibtex
@misc{solidityguard-graphcodebert,
  title  = {Graph CodeBERT Vulnerability Detector for Solidity},
  author = {Tanay Mitra},
  year   = {2026},
  url    = {https://huggingface.co/tanaymitra01/graphcodebert-vulnerability-detector}
}
```