Abstract
Memory leak detection in large C/C++ codebases often requires manual annotation of custom memory management functions, a process that is unscalable and error-prone. We present LAMeD, a framework that leverages Large Language Models (LLMs) to automatically generate these annotations. By integrating LLM-derived sources and sinks into static analysis tools like CodeQL and Cooddy, LAMeD doubles the detection rate of real-world memory leaks, demonstrating a powerful synergy between semantic AI and formal static analysis.
Introduction
Memory leaks—instances where a program allocates memory but fails to release it—remain a persistent threat to software stability and security. While static analysis tools like CodeQL, Infer, and Cooddy are designed to trace these leaks, their effectiveness is heavily dependent on "sources" (allocation functions) and "sinks" (deallocation functions).
In complex, real-world codebases, developers often use custom wrappers for memory management. Standard analyzers typically miss these unless a security expert manually writes configuration rules (annotations). This manual process is slow and does not scale. LAMeD addresses this bottleneck by using LLMs to automate the generation of these critical annotations.
Methodology: The LAMeD Pipeline
The LAMeD framework operates by augmenting traditional static analyzers with the semantic reasoning capabilities of LLMs. The pipeline consists of four primary stages:
- Preprocessing: The Joern engine parses the C/C++ codebase to extract function source code and construct a call graph to understand caller-callee relationships.
- LLM Prompting: Models such as Codestral or DeepSeek-Coder are provided with a function’s source code along with its callees. The LLM identifies variables holding pointers to newly allocated or freed memory.
- Validation: A secondary prompt verifies the LLM's findings to ensure memory is indeed being allocated to new variables, mitigating potential hallucinations.
- Integration: The validated JSON output is converted into tool-specific formats and fed into analyzers like CodeQL and Cooddy.
Experimental Evaluation
The authors evaluated LAMeD using two datasets: a manually labeled set of 152 functions from the cJSON library to measure annotation accuracy, and a Real-life Memory Leak Dataset comprising 7 open-source projects (e.g., curl, libtiff, libxml2) with 43 documented vulnerabilities.
Key Results
The integration of LLM-generated annotations significantly boosted the performance of existing static analysis tools. While baseline versions of CodeQL and Cooddy found only 5 of the 43 target leaks, the LAMeD-augmented versions found 10 bugs each—effectively doubling the detection rate.
Furthermore, on the cJSON ground truth dataset, the Codestral model achieved a precision of 93.3% and a recall of 58.3% after post-filtering, indicating high reliability in the annotations it produces.
Discussion and Limitations
The primary strength of LAMeD lies in the synergy between AI and formal methods. LLMs excel at semantic inference—understanding intent from variable names and comments—while static analyzers provide the mathematical rigor for path traversal. Providing the LLM with the source code of called functions (callees) was found to be crucial for accurate labeling.
However, this increased sensitivity comes at a cost: a "warning explosion." CodeQL warnings increased from 139 to 653, and Cooddy warnings rose from 86 to 391. This occurs because the analyzers become hyper-vigilant, flagging every annotated allocation that lacks a strictly traceable deallocation, even if the memory is managed correctly through complex data structure lifecycles.
Conclusion
LAMeD demonstrates that LLMs can significantly lower the barrier to effective static analysis in large-scale software projects. Future work will focus on reducing false positives through better triaging and extending the framework to other vulnerability classes, such as buffer overflows and race conditions.
Summary updated