Abstract
Large Language Models (LLMs) are increasingly utilized for automated code generation, yet they often produce insecure or logically flawed outputs. Furthermore, these models demonstrate a surprising inability to recognize their own errors. This paper introduces a framework that integrates external unit testing and static analysis tools to create a feedback loop for code repair. By evaluating models like Llama 3, Mixtral, and Gemma on a C-language adaptation of the MBPP dataset, the researchers show that while LLMs are unreliable self-evaluators, they can successfully leverage precise tool-based feedback to significantly improve code safety and correctness.
Introduction
The rapid adoption of AI coding assistants has brought the "black box" nature of LLMs to the forefront of software reliability concerns. Mainstream models frequently generate code containing critical vulnerabilities, such as buffer overflows, or simple logic bugs. This research addresses three pivotal questions: the inherent safety of LLM-generated code, the capacity of models to self-evaluate, and the effectiveness of external feedback in driving code repair. To avoid the pitfalls of training data memorization, the authors refactored the Mostly Basic Python Problems (MBPP) dataset into C, a language requiring explicit memory management and prone to the very safety issues LLMs struggle with.
Methodology and Datasets
The researchers implemented a three-phase framework:
- Code Generation: Models generated C code for 100 tasks, which were then subjected to unit tests and static analysis via Meta's Infer tool.
- Self-Evaluation: Models acted as classifiers to determine the correctness and safety of code samples.
- Code Repair: Models iteratively attempted to fix code based on specific feedback from tests (e.g., assertion failures) or static analysis (e.g., null pointer dereferences).
The study utilized a subset of 100 MBPP tasks translated to C and the GEN Dataset, comprising 356 model-generated C files. The models evaluated included Llama 3 (8B and 70B), Gemma 7B, and Mixtral 8x7B.
The Self-Correction Myth and the "YES-tendency"
A significant finding of the study is that LLMs are remarkably poor at identifying their own errors. They frequently exhibit a "YES-tendency," asserting that code is correct even when it fails fundamental tests. There was no evidence of "self-preference"; models were equally mediocre at evaluating their own code versus code from other models.
As shown in the confusion matrices, the high number of false positives (predicted YES for actual NO) highlights the unreliability of LLMs as autonomous auditors.
Results: The Power of External Feedback
Initial generation results were sobering, with correctness rates ranging from 46% to 65%. While a high percentage of code was initially deemed "safe" by Infer, the remaining portion contained severe vulnerabilities.
The distribution of bugs reveals that null pointer dereferences and buffer overruns are common across models. However, the introduction of external feedback proved to be a powerful catalyst for improvement. Llama 3 70B, the top performer, repaired 62% of incorrect files and 89% of safety vulnerabilities when provided with specific tool-based feedback. This suggests that while models cannot find bugs effectively, they are highly capable of fixing them once identified by external tools.
Conclusion and Future Directions
The study underscores that we cannot yet rely on LLMs to self-audit without external verification. The success of the repair phase highlights the importance of integrating static analysis and testing into AI-driven development workflows. Future work will expand this research to other languages like Rust and Python, and explore more diverse analysis tools to further enhance the robustness of LLM-generated code.
Summary updated