arXiv:2608.30025cs.AIAug 2026Cited by 1 paper on PaperVantage

Manuscript Offprint

arXiv:2608.30025

Interpreting and Steering for Safe and Correct Code Generation

Hao Yan, Ziyu Yao

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Large language models (LLMs) frequently generate source code containing vulnerabilities, yet little work studies the internal mechanisms that distinguish safe from vulnerable generation in them. In this work, we systematically perform a mechanistic interpretation of LLMs, aiming at both understanding how code safety-vs-vulnerability is represented or driven by components in an LM and turning the insights into actionable steering strategies to encourage safer code generation. To this end, we introduce CODESEC-PAIRS, a dataset of 9,342 Python safe-and-vulnerable contrastive code pairs, sampled from Llama-3.1-8B-Instruct. Utilizing the dataset, we explore approaches to localize layers and attention heads that relate to code safety, and further experiment with different steering strategies for inference-time vulnerability reduction. In particular, we propose DUOSTEER, a double-steering approach that simultaneously applies safety and code-correctness steering to attention heads. In experiments over five vulnerability types, DUOSTEER leads to an average of -26.9% vulnerability rate reduction and +7.5% functional correctness improvement, which outperforms not only other steering variants but also prompting and supervised fine-tuning baselines. The advantage also replicates on Qwen-2.5-Coder-7B-Instruct with another 2,500 contrastive pairs sampled from that model.¹

1 Introduction

Large language models (LLMs) have become indispensable tools for code generation, yet a growing body of empirical evidence shows that the code they produce frequently carries security vulnerabilities categorized by the Common Weakness Enumeration (CWE) (e.g., CWE-22: path traversal) across a wide range of conditions and mod-

els (Pearce et al., 2025; Tony et al., 2023; Hajipour et al., 2024; Bhatt et al., 2024; Yang et al., 2024; Nie et al., 2026). Mitigations proposed in prior work include prompt-level interventions (Yan et al., 2025; Lin et al., 2025) and security-tuned supervised fine-tuning (He and Vechev, 2023; He et al., 2024). These methods have made measurable progress, but they primarily operate at the model's input/output boundary. The internal computation that distinguishes a safe from a vulnerable continuation during generation has received comparatively little attention, leaving the components that shape safe-vs-vulnerable behavior largely unexamined.

We address this gap by treating safe code generation as a problem in mechanistic interpretability (Rai et al., 2024; Bereska and Gavves, 2024; Ferrando et al., 2024): identifying the internal components of a neural network that are causally responsible for a given behavior, and turning that picture into a concrete inference-time intervention (Zou et al., 2023; Rimsky et al., 2024) for vulnerability mitigation. Concretely, we ask: (i) where in a code LLM is the safe-vs-vulnerable distinction represented, and which components causally drive it; and (ii) can that internal picture be turned into a training-free edit that lowers the vulnerability rate without sacrificing functional correctness.

Our study utilizes Llama3.1-8B-IT (Grattafiori et al., 2024) as the target LLM. To enable this study, we first construct CODESEC-PAIRS, a dataset consisting of 9,342 safe-and-vulnerable code pairs across five CWEs in Python sampled from the Llama model. From existing safety-oriented coding benchmarks (Betley et al., 2025; Siddiq and Santos, 2022; Hajipour et al., 2024; Bhatt et al., 2024), we sample N=10N=10 responses per task under both a benign prompt and a vulnerability-eliciting prompt that appends an insecurity instruction (e.g., “skip input validation”) to construct safe/vulnerable pairs within and across the two prompts. Each pair is further annotated (assisted by GPT-4.1) with how

¹Our dataset and implementation of DUOSTEER are available at https://github.com/Ziyu-Yao-NLP-Lab/DuoSteer-Safe-Correct-Code-Gen.

the safe and vulnerable code outputs differ, which can be a valuable asset for qualitative analysis.

On top of CODESEC-PAIRS, we then explore two approaches for localization: Linear probes (Belinkov, 2022; Tenney et al., 2019) identify where the safety-vs-vulnerability distinction is encoded at the layer and per-head level, and causal patching (Vig et al., 2020; Meng et al., 2022; Zhang and Nanda, 2024; Sankaranarayanan et al., 2026) identifies which attention heads drive it. At the head level, the two views diverge sharply. The Spearman correlation between probe and causal effect is ∣ρ∣≤0.072|\rho| \leq 0.072 (p>0.25p > 0.25) on every CWE we study, and the most causally important head per CWE sits at probe rank 65–250 out of the top-256.

We then evaluate three steering approaches that vary in granularity (layer vs. head) and head-selection (probe- vs. causal-ranked). On the Sec-CodePLT benchmark (Yang et al., 2024), probe-targeted head steering is the weakest variant, underperforming layer-level by 3.4% and causal-targeted head steering by 14.6% in average vulnerability reduction. All three variants, however, exhibit a safety-correctness trade-off: large vulnerability reductions come at the cost of functional correctness.

To address this issue, we further propose DUOSTEER, a training-free intervention that places a safety direction (from safe vs. vulnerable code) and a correctness direction (from safe-and-correct vs. safe-but-incorrect code, so that the contrast isolates correctness from safety) at their own top causal heads. A single safe-and-correct vs. vulnerable-and-incorrect direction would not suffice: our causal analysis shows the safety- and correctness-causal pools are largely disjoint, so no single placement is optimal for both axes, and a combined-contrast single-vector ablation collapses at moderate-to-high steering strength on three of five CWEs. In experiments, DUOSTEER improves over single-vector safety steering on every CWE. On CWE-502 (unsafe deserialization), vulnerability rate falls from 64.7% to 0% at correctness = 94.1%, and on CWE-079 (cross-site scripting), vulnerability drops further to 3.9% while correctness rises to 82.3%. This advantage is also replicated with Qwen-2.5-Coder-7B-Instruct (Hui et al., 2024), resulting in another dataset of 2,500 pairs. Finally, DUOSTEER also outperforms non-steering baselines, including prompting (Yan et al., 2025) and supervised fine-tuning. We conclude that the mechanistic interpretation offers a principled compass for safer code generation, and DUOSTEER's

two-direction edit at the causally identified heads is what alleviates the safety-correctness trade-off without retraining.