Organizations: Qingdao University of Technology, Qingdao, Shandong, China · Qingdao University, Qingdao, Shandong, China · Ocean University of China, Qingdao, Shandong, China
Fine-tuning enables safety-aligned code language models to adapt to diverse downstream tasks. However, training on untrusted data can severely undermine their ability to refuse harmful requests. Notably, we find that models comply with harmful requests even though harmfulness information remains highly decodable from their internal representations. Moreover, an initial preference for refusal does not necessarily translate into sustained refusal behavior throughout generation. Together, these findings reveal a critical gap between recognizing harmful requests and consistently refusing to fulfill them. To address this gap, we introduce SAFEHOLD, a novel provider-enforced defense framework that preserves safety alignment during harmful fine-tuning by treating refusal as a sustained generation process rather than a single response decision. Specifically, SAFEHOLD preserves the aligned model's full-vocabulary response distribution at the initial generation position through decision-boundary anchoring, while supervising the complete refusal sequence through refusal-path reinforcement. We conduct extensive experiments evaluating safety, utility, and internal mechanisms. SAFEHOLD reduces average ASR by 72.03 percentage points relative to undefended fine-tuning, outperforming five SOTA defenses by 24.32 to 70.87 percentage points. Meanwhile, it improves average HumanEval pass@1 by 10.24 percentage points over undefended fine-tuning, while maintaining an average XSTest over-refusal rate of only 2.73 percent. Overall, these results demonstrate that SAFEHOLD effectively mitigates safety degradation during harmful fine-tuning while preserving benign-task utility.
Figures & tables
Method
Stage
Safety Reference
Decision
Generation
Optimization
Trusted Safety Data
Aligned Reference
Initial-Response Anchoring
Full-Sequence Refusal Supervision
Joint Update
No Extra Defense Stage
Vaccine [ 21 ]
Before
✓
✓
✓ ✗
✓ ✗
✗
✗
SBR [ 28 ]
During
✓
✓
✓ ✗
✓ ✗
✓
✓
LiSA [ 19 ]
During
✓
✗
✗
✓
✗
✓ ✗
SafeMERGE [ 10 ]
Post
✓
✓
✓ ✗
✗
✗
✗
Buffer-and-Reinforce [ 14 ]
Multi-stage
✓
✗
✗
✓
✓ ✗
✗
Table 1: Comparison of safety-preservation methods across defense stages and design choices. ✓ Explicitly supported, ✗ not explicitly supported, ✓ ✗ partially or indirectly supported through an equivalent mechanism.
Figure 1: Layer-wise harmfulness probe AUC before and after harmful fine-tuning across three code LLMs. Shaded regions indicate standard deviation, and insets highlight late-layer results.
Figure 2: Cross-domain harmful fine-tuning on Qwen2.5-Coder-7B-Instruct using three harmful training corpora across three harmful-code benchmarks.
Figure 3: Refusal initiation and continuation on Qwen2.5-Coder-7B-Instruct . (a) Negative steering on 30 HarmBench cy requests. (b) Refusal outcomes on 110 HarmBench requests after CyberStrike fine-tuning.
Figure 4: Overview of SAFEHOLD , including (1) safety reference preparation, (2) submitted-data loss, (3) decision-boundary anchoring, (4) refusal-path reinforcement, and (5) joint optimization.
Dataset
Size
Use
Fixed safety supervision (prepared once)
AdvBench [ 54 ]
8 probes
Trusted safety supervision
Training corpora
LLM-LAT [ 38 ]
300 pairs
Main harmful fine-tuning
CyberStrike [ 50 ]
300 pairs
Code-domain adaptation
CySecBench [ 43 ]
300 pairs
Code-domain adaptation
Table 2: Datasets used in SAFEHOLD .
Model
Metric ↓
Base Model
undefended
Vaccine
SBR
LiSA
SafeMERGE
BAR
SAFEHOLD
Qwen2.5-Coder-7B
RMC
25.84 +16.3
97.19 +87.6
97.19 +87.6
14.61 +5.1
55.06 +45.5
85.39 +75.8
33.15 +23.6
9.55
HB cy
8.08 − 1.5
98.48 +88.9
98.48 +88.9
40.91 +31.3
47.98 +38.4
88.89 +79.3
9.60 +0.0
9.60
MAL
30.70 +6.1
91.63 +67.0
94.42 +69.8
30.23 +5.6
50.70 +26.1
85.12 +60.5
41.86 +17.2
24.65
Llama-3.1-8B
RMC
73.03 +63.5
97.75 +88.2
89.89 +80.3
93.82 +84.3
56.18 +46.6
63.48 +53.9
64.61 +55.1
9.55
HB cy
25.25 +11.1
96.97 +82.8
97.98 +83.8
48.48 +34.3
47.47 +33.3
87.37 +73.2
18.69 +4.6
14.14
MAL
78.14 +52.6
89.77 +64.2
89.77 +64.2
93.49 +67.9
31.63 +6.1
82.79 +57.2
67.44 +41.9
25.58
Table 3: ASR (%) on three harmful-code benchmarks after 600 fine-tuning updates. Lower is better ( ↓ ). Bold and underlined values indicate the best and second-best results across all configurations. Small signed values indicate differences relative to SAFEHOLD .
Model
Base Model
Undefended
Vaccine
SBR
LiSA
SafeMERGE
BAR
SAFEHOLD
(a) XSTest: over-refusal ( ↓ )
Qwen2.5-Coder-7B
2.01 +1.61
71.89 +71.49
18.07 +17.67
9.24 +8.84
13.25 +12.85
18.47 +18.07
3.21 +2.81
0.40
Llama-3.1-8B
6.83 +1.61
62.65 +57.43
16.06 +10.84
8.43 +3.21
8.84 +3.62
12.85 +7.63
8.03 +2.81
5.22
Llama-3.2-3B
7.63 +5.22
75.90 +73.49
14.06 +11.65
9.24 +6.83
10.84 +8.43
14.86 +12.45
6.43 +4.02
2.41
DeepSeek-Coder-6.7B
0.40 +0.40
48.59 +48.59
26.91 +26.91
20.48 +20.48
6.43 +6.43
23.69 +23.69
2.81 +2.81
0.00
Mistral-7B-v0.2
4.82 − 0.80
35.74 +30.12
16.06 +10.44
9.64 +4.02
14.46 +8.84
13.25 +7.63
6.83 +1.21
5.62
Table 4: Model utility after 600 fine-tuning updates. (a) XSTest over-refusal ( ↓ ). (b) HumanEval pass@1 ( ↑ ). Bold and underlined values indicate the best and second-best results. Small signed values indicate differences relative to SAFEHOLD .
Figure 5: Safety and utility of SAFEHOLD at 300 and 600 training updates across multiple datasets. Light and dark bars represent the two checkpoints. Bar annotations give changes in percentage points: epoch 1 minus epoch 2, except for pass@1, which uses epoch 2 minus epoch 1. (Q: Qwen2.5-Coder-7B; L31: Llama-3.1-8B; L32: Llama-3.2-3B; DS: DeepSeek-Coder-6.7B; MI: Mistral-7B-v0.2.)
Model
harmful benchmarks (ASR)
refusal
capability
RMC ↓
HB cy ↓
MAL ↓
XSTest ↓
HumanEval ↑
Qwen2.5-Coder-7B (1)
10.11
45.45
46.98
10.84
61.59
Qwen2.5-Coder-7B (2)
9.55
9.60
24.65
0.40
68.90
Llama-3.1-8B (1)
10.67
31.82
28.37
12.45
34.76
Llama-3.1-8B (2)
9.55
14.14
25.58
5.22
50.00
Llama-3.2-3B (1)
11.80
27.78
9.77
7.63
43.90
Table 5: Safety and utility of SAFEHOLD after 300 (1) and 600 (2) updates (%). Arrows indicate the preferred direction. Bold indicates the better result for each model and metric.
Model
Variant
Harmful-code ASR (%) ↓
Utility (%)
RMCBench ↓
HarmBench cy ↓
MalwareBench ↓
XSTest ↓
HumanEval ↑
Qwen2.5-Coder-7B
A (anchoring only)
12.92 +3.4
40.40 +30.8
32.09 +7.4
10.44 +10.0
57.32 − 11.6
R (refusal path only)
11.24 +1.7
49.49 +39.9
62.33 +37.7
5.62 +5.2
61.59 − 7.3
F (first token + anchoring)
84.27 +74.7
62.63 +53.0
90.70 +66.1
1.61 +1.2
65.24 − 3.7
J (joint objective)
9.55
9.60
24.65
0.40
68.90
Llama-3.1-8B
A (anchoring only)
74.72 +65.2
75.76 +61.6
79.53 +54.0
11.24 +6.0
32.32 − 17.7
Table 6: Objective ablation of SAFEHOLD after 600 updates (%). Small signed values indicate differences relative to J (A − J, R − J, F − J). Bold indicates the best result for each model and metric.
Configuration
Anchoring
First refusal token
Later refusal tokens
A
✓
✗
✗
R
✗
✓
✓
F
✓
✓
✗
J ( SAFEHOLD )
✓
✓
✓
Table 7: Components of the four objective-ablation variants.
Model
RMC ↓
HB cy ↓
MAL ↓
XSTest ↓
λ=0.6
λ=1.5
λ=3.0
λ=0.6
λ=1.5
λ=3.0
λ=0.6
λ=1.5
λ=3.0
λ=0.6
λ=1.5
λ=3.0
Qwen2.5-Coder-7B
12.92
6.74
9.55
40.40
38.89
43.43
32.09
22.33
48.84
10.44
10.84
12.45
Llama-3.1-8B
74.72
49.44
94.94
75.76
54.55
92.93
79.53
37.67
89.30
11.24
14.86
17.67
Llama-3.2-3B
96.63
97.19
85.39
97.47
94.44
82.83
92.09
88.37
70.70
14.06
3.61
10.44
DeepSeek-Coder-6.7B
64.61
73.60
73.60
55.56
62.12
62.12
71.16
79.07
76.28
16.47
17.27
17.67
Mistral-7B-v0.2
94.38
44.38
96.63
96.46
80.81
99.49
94.88
83.26
93.02
15.66
15.26
18.88
Table 8: Sensitivity to anchoring weight λ under anchor-only training ( 600 updates, λc=0 ). Arrows indicate the preferred direction. Bold marks the best result for each model and metric.
Model
RMC ↓
HB cy ↓
MAL ↓
XSTest ↓
λc=0.5
λc=1.0
λc=2.0
λc=0.5
λc=1.0
λc=2.0
λc=0.5
λc=1.0
λc=2.0
λc=0.5
λc=1.0
λc=2.0
Qwen2.5-Coder-7B
5.06
9.55
3.37
36.87
9.60
36.87
37.21
24.65
29.30
11.65
0.40
11.65
Llama-3.1-8B
27.53
9.55
43.82
49.49
14.14
57.07
20.47
25.58
35.35
9.24
5.22
13.25
Llama-3.2-3B
51.12
6.18
10.11
32.83
15.66
37.37
27.44
8.84
13.02
16.06
2.41
14.86
DeepSeek-Coder-6.7B
71.91
25.28
62.36
62.63
39.90
51.01
74.42
39.07
64.19
20.08
0.00
23.69
Mistral-7B-v0.2
84.27
44.94
30.34
70.20
32.32
50.00
76.74
40.47
40.47
9.64
5.62
14.86
Table 9: Sensitivity to refusal reinforcement weight λc under joint optimization ( 600 updates, λ=0.6 ). Arrows indicate the preferred direction. Bold marks the best result for each model and metric.
Figure 6: Objective ablation and weight sensitivity of SAFEHOLD . Values represent mean ASR across three harmful-code benchmarks ( ↓ ). Q, L31, L32, DS, and MI denote Qwen2.5-Coder-7B, Llama-3.1-8B, Llama-3.2-3B, DeepSeek-Coder-6.7B, and Mistral-7B-v0.2, respectively.
Figure 7: Activation changes of safety-associated neurons across five code LLMs. Points below the diagonal indicate suppression; for these points, greener colors indicate stronger recovery under SAFEHOLD . Annotations show suppression and rebound rates.
Model
Layers
dff
Selected neurons
Qwen2.5-Coder-7B
28
18944
4524
Llama-3.1-8B
32
14336
3139
Llama-3.2-3B
28
8192
1498
DeepSeek-Coder-6.7B
32
11008
2627
Mistral-7B-v0.2
32
14336
3224
Table 10: Safety-associated neuron statistics across five code LLMs.
Model
Suppressed (%)
Rebound (%)
Qwen2.5-Coder-7B
43.7
71.2
Llama-3.1-8B
47.8
63.4
Llama-3.2-3B
46.5
72.3
DeepSeek-Coder-6.7B
45.0
70.4
Mistral-7B-v0.2
55.2
76.7
Table 11: Activation suppression and rebound of safety-associated neurons after 600 updates.
Figure 8: Angular deviations of harmfulness directions from the aligned reference across five code LLMs. Dashed and solid arrows represent undefended and SAFEHOLD checkpoints, respectively; dashed and solid arcs mark the corresponding angular deviations. Smaller angles indicate stronger alignment.
Figure 9: Refusal-opening probability across model states. Blue and dotted segments indicate recovered and remaining probability losses, respectively. Percentages denote the recovered fraction.