Abstract
Memory corruption vulnerabilities, such as buffer overflows, continue to plague C-based software, representing approximately 70% of vulnerabilities in major products. While safe dialects like Checked C offer a path to mitigation, the manual effort required for code annotation and refactoring is a significant barrier to adoption. This paper introduces MSA, a hybrid framework that combines Large Language Models (LLMs) with symbolic analysis to automate the porting of legacy C code to Checked C. By utilizing a modular, dependency-aware transformation strategy, MSA correctly infers 86% of required memory-safety annotations, significantly outperforming both traditional symbolic tools and standard LLM approaches.
Introduction
The "Achilles' heel" of software security remains memory safety. Despite the emergence of safe languages, legacy C codebases are vast and critical. Checked C provides a mechanism to introduce memory safety into C through type annotations like ptr<T> and count(n). However, existing symbolic analysis tools, such as 3C, are often incomplete, struggling with complex pointer arithmetic and nested structures. MSA bridges this gap by leveraging the reasoning capabilities of LLMs to handle refactoring tasks that symbolic tools find intractable.
The MSA Framework
MSA operates by tightly coupling LLMs with symbolic representations of the program. The process begins with Symbolic Pre-processing, where the tool 3C is used to resolve straightforward safety cases. Subsequently, MSA employs clang to construct a comprehensive Dependency Graph of the program's declarations, including functions, structs, and global variables.
Modular Transformation Strategy
A key insight of this work is that LLM performance degrades when processing large, monolithic files. MSA addresses this by breaking the program into small, manageable declarations.
The framework performs a Bottom-Up Traversal of the dependency graph. When prompting the LLM for a specific function, MSA provides a "prelude" containing the already-transformed signatures of its dependencies. This context-aware approach ensures that if a callee's signature changes (e.g., adding a parameter for array length), the LLM can correctly update all corresponding call sites in the caller.
The transformation occurs in three phases:
- Structural Refactoring: Replacing unsupported nested arrays (e.g.,
int**) with arrays of structs. - Bounds Inference: Inferring pointer bounds based on code usage.
- Global Consistency: Ensuring consistent bounds for global variables and struct fields across the entire program.
Evaluation and Results
MSA was evaluated on micro-benchmarks (Olden and Ptrdist suites) and five significant open-source projects, including lua, icecast, and libarchive.
- High Annotation Accuracy: MSA correctly inferred 86% of the annotations that the symbolic tool 3C missed in real-world benchmarks.
- Modularity Advantage: In micro-benchmarks, the modular approach achieved 100% accuracy, whereas standard LLM prompts (without modular context) achieved as little as 14%.
- Real-world Utility: In a case study of vsftpd, MSA automated approximately 58% of the total porting effort, producing 123 correct annotations and 250 code edits.
Discussion and Conclusion
A critical safety feature of MSA is its reliance on the Checked C compiler. Any incorrect annotations generated by the LLM are caught during compilation, ensuring that the final binary remains safe. While challenges like LLM hallucinations (affecting ~9% of annotations) and the cost of high-end models remain, MSA demonstrates that a hybrid symbolic-LLM approach can drastically reduce the manual burden of securing legacy C code. Future work will explore fine-tuning smaller, specialized models to improve efficiency and reduce costs.
Summary updated