Abstract
Migrating legacy C codebases to Rust is essential for modern memory safety, yet manual translation is prohibitively expensive. While Large Language Models (LLMs) offer a promising path toward automated migration, they suffer from "laziness"—a tendency to omit code logic in long contexts. This paper presents LLMigrate, a tool that addresses laziness through a divide-and-conquer workflow. By decomposing modules into functions, providing targeted context through dependency probing, and employing iterative compiler-driven repair, LLMigrate significantly reduces manual migration effort. Evaluation on Linux kernel modules demonstrates a reduction in manual modifications by over 85% and a total elimination of code omission (laziness) compared to standard LLM baselines.
Introduction: The Challenge of C-to-Rust Migration
The software industry is increasingly prioritizing the migration of legacy C codebases to Rust to eliminate memory safety vulnerabilities. However, the scale of projects like the Linux kernel makes manual migration a monumental task. Existing automated tools generally fall into two categories: rule-based translators and LLM-based generators. Rule-based tools like C2Rust are accurate but often produce "unsafe" Rust that retains C-style pointer arithmetic. Conversely, LLMs like GPT-4 can produce idiomatic "safe" Rust but are plagued by "laziness."
The "Laziness" Problem
The authors identify a critical failure mode in LLMs: as the length of the input code increases, the model's propensity to skip implementation details grows. Instead of a full translation, the LLM might provide a comment such as // ... implement logic here ....
Empirical evidence shows that while laziness is rare (under 2%) for snippets shorter than 200 lines, it becomes a significant barrier for larger modules, with omission rates jumping to nearly 50% at the 700-line mark. This creates a bottleneck for migrating complex systems code.
Methodology: The LLMigrate Workflow
To circumvent the laziness threshold, LLMigrate introduces a "divide-and-conquer" architecture. The system ensures the LLM operates within its "active" zone by processing individual functions rather than entire modules.
The workflow consists of several key stages:
- Function Splitting: The source C module is broken down into discrete functions.
- Contextual Probing: A C Dependency Prober uses static analysis to identify external variables and dependencies, injecting necessary declarations into the prompt to provide context without overwhelming the LLM's context window.
- Iterative Program Repair: Translated code is passed to the Rust compiler. If errors occur, LLMigrate feeds the error messages back to the LLM for up to three rounds of self-correction.
- Hybrid Translation: Rule-based tools handle boilerplate and type definitions, while the LLM focuses on complex function logic.
- Function Fusion: Functions are reassembled in topological order to ensure interface consistency across the final Rust module.
Evaluation and Results
LLMigrate was evaluated on three representative Linux kernel modules: math, sort, and ramfs. The results demonstrate a significant improvement over standard LLM baselines:
- Elimination of Laziness: While GPT-4o exhibited a 76.7% laziness rate on the complex
ramfsmodule, LLMigrate reduced this to 0%. - Reduced Manual Effort: The tool reduced Manually Modified Lines (MML) to less than 15% across all datasets. For the
ramfsmodule, manual fixes were reduced from thousands of lines to just 277. - Safety and Accuracy: In the
mathmodule, 98.9% of the generated code was categorized as "Safe Rust," successfully transitioning away from dangerous C pointers to Rust's ownership model. - Compilation Success: The iterative repair process improved the Compilation Success Rate (CSR) significantly, resolving most errors within three rounds.
Conclusion
LLMigrate demonstrates that the limitations of LLMs in code migration are not insurmountable. By managing context and leveraging iterative feedback loops, it is possible to transform "lazy" models into highly efficient migrators. Future work will focus on fine-tuning models for low-level systems code and further automating the handling of subtle semantic differences between C and Rust.
Summary updated