Research map / Large language models / Instruction and fine-tuning
Instruction Tuning for Secure Code Generation
Read the paperCheck your idea against 108,967 papers
AI-extracted summary. Written by AI from the paper's full text. Numbers and claims can be wrong or missing context, so check the paper before you cite or build on it.
Proposes SafeCoder, a security-centric instruction tuning method that mixes a CodeQL-verified secure/insecure code dataset (collected automatically from GitHub commits) with standard instruction tuning, using masked likelihood and unlikelihood losses.
Problem
Instruction-tuned language models frequently generate insecure code, and existing instruction tuning schemes do not address code security while preserving utility.
Approach
Joint instruction tuning that combines standard NLL loss on general instruction data with a masked NLL loss on secure code and a masked unlikelihood loss on vulnerable code, with masks restricted to security-related tokens identified by difflib; security dataset built by an automated GitHub commit pipeline verified with CodeQL and GPT-4-generated instructions.
Key results
- SafeCoder raises secure code generation from about 55-67% (pretrained or standard instruction tuning) to about 89-93% across six LMs (e.g., StarCoder-1B 62.9 to 92.1).
- Utility is largely preserved: e.g., StarCoder-1B HumanEval pass@1 20.4 to 19.4, MMLU 25.0 to 24.8.
- SafeCoder outperforms SVEN adapted to instruction tuning, which shows a security-utility trade-off.
Measured effects reported in the paper
| What was changed | Where | Measured | Effect |
|---|---|---|---|
| add SafeCoder joint security tuning | StarCoder-1B · Code security, 60 scenarios | secure generations (%): 62.9 -> 92.1 | Helped |
| add SafeCoder joint security tuning | StarCoder-3B · Code security, 60 scenarios | secure generations (%): 68.3 -> 93.0 | Helped |
| add SafeCoder joint security tuning | CodeLlama-7B · Code security, 60 scenarios | secure generations (%): 66.6 -> 91.2 | Helped |
| add SafeCoder joint security tuning | Phi-2-2.7B · Code security, 60 scenarios | secure generations (%): 69.9 -> 90.9 | Helped |
| add SafeCoder joint security tuning | Llama2-7B · Code security, 60 scenarios | secure generations (%): 59.2 -> 89.2 | Helped |
| add SafeCoder joint security tuning | Mistral-7B · Code security, 60 scenarios | secure generations (%): 63.1 -> 89.6 | Helped |
| add SafeCoder joint security tuning | instruction-tuned code models · HumanEval | Pass@1: 20.4 -> 19.4; 30.7 -> 28.0; 36.8 -> 35.9 | Hurt |
| add SafeCoder joint security tuning | general-purpose instruction-tuned models · HumanEval | Pass@1: 48.3 -> 46.1; 13.3 -> 11.8; 35.2 -> 33.7 | Hurt |
| remove the security dataset collected by the authors (use only the other security training data) (ablation) | SafeCoder · Code security | secure generations (%): about 20% less secure | Hurt |
| add security-token masks to the secure-code and unsafe-code losses (ablation) | SafeCoder · Code security | secure generations (%): about 10% | Helped |
| add unlikelihood loss on insecure programs (ablation) | SafeCoder on StarCoder-1B · Code security | secure generations (%): 5.1% | Helped |
| add unlikelihood loss on insecure programs (ablation) | SafeCoder on Phi-2-2.7B · Code security | secure generations (%): 10.6% | Helped |
Surprising findings
- Security-aware prompts (generic or CWE-specific) only modestly improve security of instruction-tuned models.
- SafeCoder does not significantly improve security on CWEs not seen during training, indicating limited generalization.
Limitations
- Does not handle pretrained code completion models or already instruction-tuned models needing post-hoc patching.
- Supervised fine-tuning only; reinforcement learning left to future work.
- No formal security guarantee; limited generalization to unseen CWEs.
Experimental setup
Datasets: Code Evol-Instruct (decontaminated, 33K) LMSYS-Chat-1M (18K subset) SVEN security dataset
Benchmarks: HumanEval MBPP MMLU TruthfulQA CodeQL-based security scenarios (60)
Metrics: secure code rate pass@1 pass@10
Baselines: SVEN standard instruction tuning (w/o SafeCoder) pretrained LM (n/a)
Builds on: SVEN unlikelihood training LoRA Code Evol-Instruct
Closest papers by meaning
- ProSec: Fortifying Code LLMs with Proactive Security Alignment (ICML 2025 · similarity 0.89)
- CodeMimicry: Exploiting Safety Generalization Lag in Large Language Models via Structured Code Completion (NeurIPS 2026 · similarity 0.87)
- INDICT: Code Generation with Internal Dialogues of Critiques for Both Security and Helpfulness (NeurIPS 2024 · similarity 0.87)
- Instructional Segment Embedding: Improving LLM Safety with Instruction Hierarchy (ICLR 2025 · similarity 0.87)
- Training Language Models to Generate Quality Code with Program Analysis Feedback (NeurIPS 2025 · similarity 0.87)
- SecCodePRM: A Process Reward Model for Code Security (ICML 2026 · similarity 0.87)
Writing a paper in this area? scime draft is a free LaTeX editor with AI included, real-time co-authors and a check of your references and venue rules before you submit. It also imports OpenAI Prism projects.
