Introduction
As Large Language Models (LLMs) become integral to modern software development, ensuring the security of the code they generate is paramount. Current research into Secure Code Generation (SCG) focuses on techniques like fine-tuning (e.g., SVEN, SafeCoder) or inference-time constraints (e.g., CodeGuard+, PromSec) to mitigate vulnerabilities. However, a critical gap exists in how these techniques are evaluated. Traditional evaluations often treat security and functional correctness as independent variables, testing them on separate datasets. This "siloed" approach fails to account for the trade-offs between safety and utility, potentially rewarding models that achieve security by simply breaking the code's functionality.
The SAFE Evaluation Framework
To address these limitations, this study introduces SAFE (Security and Functionality Evaluation), a unified metric designed to evaluate security and functional correctness simultaneously. Unlike binary "all-or-nothing" metrics, SAFE employs an exponential function to provide a granular score, rewarding code that is both secure and passes a high percentage of unit tests.
The researchers also moved beyond the industry-standard CodeQL, which they found can miss over 20% of vulnerabilities. Instead, they utilized a multi-scanner ensemble comprising three static analyzers (CodeQL, Bearer, and Bandit) and two high-performing LLMs (Llama 3.3-70B and Qwen 2.5-72B) to establish a more robust security baseline.
The "Security by Deletion" Trap
One of the most significant findings is the prevalence of "Security by Deletion." Many SCG techniques improve security scores not by fixing vulnerabilities, but by removing the logic required to complete the task entirely.
As shown in the comparison above, while the base model generates a functional (if potentially insecure) snippet, the "secured" version often replaces logic with placeholders or empty blocks. In extreme cases, techniques like PromSec were found to remove insecure code in over 92% of "fixed" samples, rendering the output useless for the developer.
Performance and Functionality Trade-offs
When evaluated using the unified SAFE metric, many state-of-the-art SCG techniques actually underperform compared to their base models. The study observed that between 10% and 30% of tasks that were functional in the base model became non-functional after applying security enhancements. In some instances, the overall performance drop exceeded 50%.
The variability in performance across different score types highlights the danger of relying on a single tool for evaluation. Furthermore, the study noted "non-monotonic" improvements, where security techniques occasionally introduced new vulnerabilities, such as replacing secure Python file-handling with insecure system calls.
Conclusion
The current landscape of secure code generation is marred by a fundamental trade-off: security is often bought at the cost of utility. The introduction of the SAFE metric and enhanced datasets like SecCodePLT+ provides a path forward for the research community. Future efforts must focus on "monotonic" security techniques—methods that enhance safety without compromising the functional integrity of the generated code. Only by adopting unified evaluation frameworks can we ensure that LLMs become truly safe and effective partners in software engineering.
Summary updated