NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Enhancing Large Language Models for Secure Code Generation: A Dataset-driven Study on Vulnerability Mitigation

Jiexin Wang, Liuwen Cao, Xitong Luo, Zhiping Zhou, Jiayuan Xie, Adam Jatowt, Yi CaiarXiv 2023 · DOI

Introduction

Large Language Models (LLMs) have transformed the landscape of automated software development. However, because these models are trained on vast, uncurated repositories like GitHub, they often internalize and replicate insecure coding patterns. This "garbage in, garbage out" phenomenon poses a significant security risk, as models may inadvertently generate exploitable code. This study addresses this challenge by introducing SecuCoGen, a specialized Python dataset designed to evaluate and improve the security of LLM-generated code through multi-task evaluation and advanced prompting techniques.

The SecuCoGen Dataset

Existing benchmarks often prioritize functional correctness over security. SecuCoGen bridges this gap by providing a high-quality, curated dataset of 180 samples. These samples cover 21 critical vulnerability types identified in the "2023 CWE Top 25 Most Dangerous Software Weaknesses."

Each entry in SecuCoGen is meticulously structured to support multiple research tasks. Unlike previous datasets that offer only partial prompts, SecuCoGen includes:

  • A natural language problem description.
  • A "CWE-aware" version of the problem description.
  • An insecure code implementation.
  • A natural language explanation of the vulnerability (Insecure Code Explanation, or ICE).
  • A secure, corrected version of the code.

Methodology: Prompting for Security

The researchers evaluated four prominent LLMs—InCoder, CodeGen, StarCoder, and GPT-3.5—across three primary tasks: secure code generation, code repair, and vulnerability classification.

A core focus of the methodology was the impact of prompt engineering. Specifically, the study compared standard prompts with "CWE-aware" prompts, which explicitly mention the security pitfalls to avoid (e.g., "be careful of SQL injection"). Additionally, the researchers explored the efficacy of 1-shot prompting and providing natural language explanations during the repair process.

Prompting techniques for code generation, repair, and classification tasks.

To measure performance, the study utilized CodeBLEU for functional similarity and a specialized metric, SEC@k, to determine if the generated code was both functionally correct and secure against vulnerabilities.

Results and Key Insights

The "Awareness" Boost

The most significant finding was that LLMs often possess the latent knowledge required to write secure code but fail to apply it by default. When GPT-3.5 was provided with CWE-aware instructions, its security performance (SEC@1) improved by a staggering 174.95%, rising from 0.2667 to 0.7333. This suggests that explicit security context is vital for steering models toward safer outputs.

Performance Across Vulnerability Types

Performance varied significantly depending on the type of vulnerability. While GPT-3.5 showed high proficiency in avoiding common issues like SQL injection (CWE-89) when prompted correctly, it struggled with more complex memory-related issues like "Out-of-bounds Write" (CWE-787).

Detailed performance metrics for GPT-3.5 across various CWE types and prompting strategies.

The Difficulty of Code Repair

Surprisingly, models often performed worse at repairing existing insecure code than generating secure code from scratch. The presence of vulnerable code appeared to bias the models, making it harder for them to pivot to a secure implementation. Furthermore, providing natural language explanations (ICE) of the vulnerabilities did not significantly enhance repair performance, indicating a gap in the models' ability to translate abstract reasoning into code fixes.

Conclusion and Future Work

This study demonstrates that while LLMs are prone to generating insecure code, their security can be dramatically improved through better prompting and specialized datasets like SecuCoGen. GPT-3.5 emerged as the most capable model, particularly when guided by CWE-aware instructions.

Future research should aim to expand SecuCoGen to other programming languages like C++ and Java, incorporate more recent frontier models, and explore automated feedback loops—such as integrating static analysis tool outputs—to further refine the security of AI-assisted programming.

Summary updated

BibTeX
@misc{wang2023enhancing,
  author = {Jiexin Wang and Liuwen Cao and Xitong Luo and Zhiping Zhou and Jiayuan Xie and Adam Jatowt and Yi Cai},
  title = {{Enhancing Large Language Models for Secure Code Generation: A Dataset-driven Study on Vulnerability Mitigation}},
  year = {2023},
  eprint = {2310.16263},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2310.16263},
}
Figure