arrow
Return

Steer Your Model: Secure Code Generation With Contrastive Decoding

delete2026-01-05
delete0
PRE
AI
H
Huang Li
鄢
鄢萌 (Meng Yan)
T
Tao Yin
W
Weifeng Sun
刘
刘忠鑫 (Zhongxin Liu)
H
Hongyu Zhang
David Lo cover
David Lo (David Lo)
DOI:10.1109/TSE.2025.3650127delete
deleteOriginal
deleteShare
deleteSave
Abstract

Abstract

En 中文
Large Language Models (LLMs) specialized in code have demonstrated impressive capabilities in various programming tasks such as code generation. However, these models often generate vulnerable code due to inherent flaws in training datasets derived from large-scale, unfiltered open-source repositories. Existing methods like SVEN (prefix tuning) and CoSec (supervised co-decoding) attempt to address these risks but face challenges with transferability or inflexible security constraints. To mitigate these issues, we propose SCoDE, a two-stage approach for secure and functionally correct code generation. After an initial functional tuning phase, we integrate a plug-and-play security steering matrix at the model’s output embedding layer. This matrix can be transferred across models without modifying their original weights. During inference, we introduce a novel contrastive decoding mechanism that adaptively balances the base model’s functional logits with positive and negative security steering signals. Extensive experiments on 60 security scenarios and two standard benchmarks (HumanEval, MBPP) using StarCoder, Qwen2.5-Coder, and CodeLlama demonstrate that SCoDE enhances security while maintaining functional correctness. On average, SCoDE improves security by 28.09% over the original models, 12.07% over CoSec, and 4.82% over SVEN. For functional correctness, it achieves average gains of 55.03% on HumanEval and 41.81% on MBPP over the original models.
Keywords:
Large language models
code generation
code security
contrastive decoding
Fuzzy-Assisted Contrastive Decoding Improving Code Generation of Large Language Models
err2025-08-01
err0
PREAI
errShuai Wang; Liang Ding; Yibing Zhan; Yong Luo; Shuai Liu; Weiping Ding
errShare
errSave
Improving Co-Decoding Based Security Hardening of Code LLMs Leveraging Knowledge Distillation
err
err0
PREAI
errDong Li; Shanfu Shu; Meng Yan; Zhongxin Liu; Chao Liu; Xiaohong Zhang; David Lo
errShare
errSave
A Survey on Large Language Models for Code Generation
err2026-02-01
err33
PREAI
errJiang, Juyong; Wang, Fan; Shen, Jiasi; Kim, Sungju; Kim, Sunghun
errShare
errSave
errShare
errSave
On the Time Efficiency of Code Generated and Optimized by Large Language Models
err2026-07-03
err0
PREAI
errYun Peng; Jun Wan; Xiaoxue Ren; Tim Menzies; Michael R. Lyu
errShare
errSave

Journal

IEEE Transactions on Software Engineering cover
IEEE Transactions on Software Engineering
IF:
5.6
Papers:
3.0K
Citations:
1.3W

Organization

zhe jiang university cover
zhe jiang university
Scholars:
1.5K
Papers: 463
Citations: 9.6W
chongqing university cover
chongqing university
Scholars:
7.1K
Papers: 2.2K
Citations: 3.7W
S
Singapore Management University
Scholars:
229
Papers: 159
Citations: 2.3K
researcher View more organizations
Cited Papers

Cited Papers

MMEvol: Empowering Multimodal Large Language Models with Evol-Instruct
err2025-01-01
err0
PREAI
errLuo,Run; Zhang,Haonan; Chen,Longze; Lin,Ting-En; Liu,Xiong; Wu,Yuchuan; Yang,Min; Li,Yongbin; Wang,Minzheng; Zeng,Pengpeng; Gao,Lianli; Shen,Heng Tao; Li,Yunshui; Alinejad-Rokny,Hamid; Xia,Xiaobo; Song,Jingkuan; Huang,Fei
errShare
errSave
errShare
errSave
Asleep at the Keyboard? Assessing the Security of GitHub Copilot’s Code Contributions
err2022-05-01
err0
errOAAI
errHammond Pearce; Baleegh Ahmad; Benjamin Tan; Brendan Dolan-Gavitt; Ramesh Karri
errShare
errSave
Competition-level code generation with AlphaCode
errScience
IF45.8
err2022-01-01
err0
errOAAI
errYujia Li; David Choi; Junyoung Chung; Nate Kushman; Julian Schrittwieser; Rémi Leblond; Tom Eccles; James Keeling; Felix Gimeno; Agustin Dal Lago; Thomas Hübert; Peter Choy; Cyprien de Masson d’Autume; I. Babuschkin; Xinyun Chen; Po-Sen Huang; Johannes Welbl; Sven Gowal; Alexey Cherepanov; James Molloy; Daniel J. Mankowitz; Esme Sutherland Robson; Pushmeet Kohli; Nando de Freitas; Koray Kavukcuoglu; Oriol Vinyals
errShare
errSave
researcher View more