π€ AI Summary
This work addresses the inefficiency, high error rates, and limited flexibility of existing rule-based approaches for automatically translating legacy C/C++ systems into memory-safe Rust code. To overcome these challenges, we propose a retrieval-augmented generation (RAG) framework that synergistically combines large language models (LLMs) with small language models (SLMs). Our method employs a source code chunking strategy and leverages Rustβs official documentation and compiler error messages as retrieval contexts to enable context-aware translation. Experimental evaluation on multiple Coreutils programs demonstrates that our approach successfully eliminates raw pointer dereferences (RPDs) and unsafe type casts (UTCs), significantly improving the correctness, memory safety, and automation level of the translation process.
π Abstract
Memory safety has long been a critical challenge in software engineering, particularly for legacy systems written in memory-unsafe languages such as C and C++. Rust, one of the youngest modern programming languages, offers built-in memory-safety guarantees that make it a strong candidate for secure systems development. Consequently, transpiling C/C++ code into memory-safe Rust code has become a growing area of research. However, manual transpilation is often time-consuming and error-prone. Additionally, rule-based automated approaches are not as flexible or cost-effective as methods enabled by state-of-the-art AI models, techniques, and methods, such as those that deploy Large Language Models (LLMs), for example, Generative Pretrained Transformers (GPT). In this paper, we propose a Retrieval-Augmented Generation (RAG)-assisted framework that integrates an LLM with a Small Language Model (SLM) to perform C/C++-to-Rust transpilation with a focus on enhancing memory safety. The framework deploys a segmentation strategy that processes C/C++ code in balanced blocks, guiding the LLM with retrieved context from Rust documentation and compiler error references. Our experiments using three OpenAI models (GPT-4o, GPT-4-Turbo, and o3-Mini) demonstrate that the RAG-enhanced pipeline generally improves both code correctness and security for C-to-Rust code transpilation. Several Coreutils programs achieve complete elimination of Raw Pointer Dereferences (RPDs) and Unsafe Type Casts (UTCs) in the final Rust output, indicating the potential of LLM-based transpilation for advancing automated software modernization and repair, as well as memory-safe code generation.