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

HexaCoder: Secure Code Generation via Oracle-Guided Synthetic Training Data

Hossein Hajipour, Lea Schönherr, Thorsten Holz, Mario FritzarXiv 2024 · DOI

Introduction

Large Language Models (LLMs) like GitHub Copilot have revolutionized software development, yet they suffer from a significant "security debt." Research indicates that approximately 40% of AI-generated code contains vulnerabilities such as SQL injection or buffer overflows. This stems from two primary issues: the models are trained on vast open-source repositories containing human-written bugs, and there is a critical scarcity of high-quality datasets that explicitly demonstrate how to fix specific Common Weakness Enumerations (CWEs).

HexaCoder is a novel framework designed to bridge this gap by automating the creation of secure training data and refining the code generation process to prioritize security-critical elements.

Methodology: The HexaCoder Pipeline

HexaCoder introduces a three-stage pipeline to improve the security of CodeLMs without sacrificing their functional utility.

Overview of the HexaCoder framework's three-stage pipeline.

1. Oracle-Guided Data Synthesis

The first stage involves synthesizing a high-quality dataset of (vulnerable, secure) code pairs. Rather than manually searching for examples, the authors use CodeQL, a static analysis engine, to generate detailed security reports for existing vulnerable code. These reports, along with specific "security hints" (mitigation strategies derived from MITRE), are provided to GPT-4 to produce a fixed, secure version of the code.

The detailed workflow for synthesizing secure code pairs using an LLM and a security oracle.

A "Security Oracle" then verifies the synthesized code to ensure the vulnerability has been successfully mitigated. This automated loop ensures the training data is both accurate and effective.

2. Security Fine-Tuning

Using the synthesized dataset, the researchers fine-tune smaller, open-source CodeLMs using Low-Rank Adaptation (LoRA). To maximize efficiency, they employ a masked loss function. This approach forces the model to focus its learning specifically on the differences between the vulnerable and secure code—the "fix"—rather than redundant general syntax.

3. Two-Step Generation

HexaCoder introduces a novel inference strategy called "two-step generation." Standard models often fail to include necessary security libraries because they attempt to write entire functions in a single pass. HexaCoder-tuned models are prompted to:

  • Step 1: Generate or update necessary security-critical libraries and imports (e.g., sanitization tools).
  • Step 2: Complete the logic based on those updated libraries.

This "primes" the model to write secure code by establishing a secure context from the start.

Experimental Setup and Datasets

The framework was evaluated using a synthesized dataset of 1,776 code pairs (covering 11 CWE types in Python and C/C++) and several benchmarks:

  • CodeLMSec: Testing the breadth of security knowledge across diverse prompts.
  • Pearce et al. Benchmark: Standardized security scenarios.
  • HumanEval: Assessing functional correctness to ensure general coding performance is maintained.

Results and Key Insights

The evaluation demonstrated that HexaCoder significantly improves code security:

  • Vulnerability Reduction: The framework reduced vulnerable code instances by up to 85% compared to baseline models.
  • Outperforming SOTA: On the Python CodeLMSec benchmark, HexaCoder generated only 48 vulnerable samples, compared to 300 from the previous state-of-the-art approach, SVEN.
  • Preserving Utility: Performance on HumanEval remained stable, proving that security enhancements do not necessitate a trade-off in functional correctness.

A key insight from the study was the importance of the "hint." Providing specific mitigation strategies boosted the repair rate during synthesis from 63% to 84%. This suggests that models benefit significantly from targeted security context.

Limitations and Future Directions

While effective, HexaCoder has limitations. It relies on the accuracy of the static analysis oracle (CodeQL); false negatives in the oracle could lead the model to learn insecure patterns as "secure." Currently, the framework focuses on Python and C/C++, though expansion to languages like JavaScript, PHP, and Go is planned. Additionally, the two-step generation process can occasionally result in redundant library imports, an area for future optimization.

Conclusion

HexaCoder provides a scalable, automated solution to the security debt of LLMs. By combining oracle-guided synthesis with a structured two-step generation process, it enables the development of AI coding assistants that are not only productive but fundamentally more secure.

Summary updated

BibTeX
@misc{hajipour2024hexacoder,
  author = {Hossein Hajipour and Lea Schönherr and Thorsten Holz and Mario Fritz},
  title = {{HexaCoder: Secure Code Generation via Oracle-Guided Synthetic Training Data}},
  year = {2024},
  eprint = {2409.06446},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2409.06446},
}
Figure