LLM4C2Rust: Large Language Models for Automated Memory-Safe Code Transpilation CCS: Software and its engineering Software evolution CCS: Computing methodologies Artificial intelligence

Sarah Bedell, Nazanin Siavash, Armin Moin · 2026

Read LLM4C2Rust: Large Language Models for Automated Memory-Safe Code Transpilation CCS: Software and its engineering Software evolution CCS: Computing methodologies Artificial intelligence by Sarah Bedell, Nazanin Siavash, Armin Moin (2026) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2604-15485.txt.

Introduction

Bedell, Siavash, and Moin build LLM4C2Rust, a retrieval-assisted scheme that turns C and C++ code into memory-safe Rust. It splits code into balanced blocks and guides a large language model with Rust docs and compiler error notes, paired with a small language model. Tests on three OpenAI models show gains in correctness and security, with several Coreutils programs shedding all raw pointer dereferences and unsafe casts.

Catalog record

CREATORSarah Bedell, Nazanin Siavash, Armin Moin
YEAR2026
COLLECTIONPAPERS
FORMATTXT
FILE SIZE53131
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2604.15485

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

LLM4C2Rust: Large Language Models for Automated Memory-Safe Code Transpilation CCS: Software and its engineering Software evolution CCS: Computing methodologies Artificial intelligence Sarah Bedell, Nazanin Siavash, Armin Moin Abstract . 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 a 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. 1. Introduction For decades, the C programming language has been a cornerstone of low-level software development, powering operating systems, embedded controllers, and applications where efficiency and direct hardware access are paramount. Its fine-grained control over memory and hardware registers has made it indispensable in performance-critical domains. However, this control comes at the cost of manual memory management, which frequently leads to vulnerabilities such as buffer overflows, dangling pointers, and data races. Industry studies have estimated that approximately 70% of reported security vulnerabilities stem from these memory-safety issues ( 32 ) . The U.S. federal government has also called for a shift toward memory-safe languages to reduce such risks by design ( 15 ; 14 ) . Rust has emerged as a modern alternative for software system programming that addresses many of these challenges by enforcing a strict ownership-and-borrowing model at compile time, preventing entire classes of memory-safety bugs before execution. Its performance is comparable to C and C++, while its safety guarantees have led to successful adoption in high-profile projects. Despite these advantages, vast amounts of critical infrastructure remain written in C. Migrating this legacy code base to Rust in a manual manner would be both costly and time-consuming. This challenge has motivated a growing interest in automated C-to-Rust transpilation. Existing automatic transpilation (i.e., translation across programming languages) techniques generally fall into two main categories: rule-based and modern-AI-based. The early generation of AI-enhanced systems was rule-based. While effective at maintaining functional equivalence, such methods often generate Rust code that contains unsafe blocks and retains low-level idioms from C, limiting both its maintainability and the security benefits of Rust. In contrast, state-of-the-art AI approaches, for example, based on Large Language Model (LLMs), such as Generative Pretrained Transformers (GPT), can produce more idiomatic and typically more secure Rust code, as these models learn from large corpora of human-written code. However, LLMs suffer from a lack of semantic guarantees and are prone to hallucinations , producing code that deviates from the original intent of the program. Therefore, we explore hybrid solutions that integrate LLMs with external components to combine the fluency of generative models with the determinism of static tools. In this study, we focus on C/C++-to-Rust transpilation to improve memory safety, leveraging Rust’s compile-time enforcements to yield inherently more secure programs. Unlike higher-level memory-safe languages, such as Python or Java, Rust also preserves near-C-level performance, making it a practical target for system-level transpilation. We aim to automate this using cutting-edge LLM-assisted methods and techniques. In particular, our approach combines an LLM (e.g., GPT-4o, GPT-4-Turbo, or o3-mini) with a Retrieval-Augmented Generation (RAG) pipeline that contextualizes the prompts sent to models with relevant code patterns and domain-specific documentation. This design aims to mitigate hallucination-related risks and enhance the idiomatic quality, correctness, and security of the generated Rust code. Our approach also aligns with several prominent initiatives to improve software memory safety, exemplified by the DARPA TRACTOR initiative ( 1 ) . We evaluate our proposed approach using a subset of the benchmark introduced by Nitin et al. ( 27 ) , which comprises seven diverse Coreutils programs originally written in C along with ten existing C2Rust-translated programs. We limit the experiments to the seven Coreutils applications to make it more feasible in terms of scope. However, the dataset’s diversity in program size and functionality provides a meaningful basis for assessing transpilation quality and performance. Additionally, our exploratory interactions with the LLMs indicated a flexibility to address both C and C++ transpilation to Rust. However, we limited the scope of the experimental study in this paper to C code transpilation only. This paper makes the following contributions: (1) We propose a novel transpilation framework that integrates LLMs with a RAG-enhanced pipeline to improve the quality (including security) of C/C++-to-Rust transpilation. (2) We assess the ability of state-of-the-art LLMs to generate memory-safe Rust code while retaining functional equivalence to the original C code. (3) We investigate whether the common hallucination problem in LLM-based code generation can be mitigated by making the LLM more context-aware through augmenting the prompt with targeted retrieval from relevant sources. Specifically, we address two Research Questions (RQs): RQ1: Can LLMs effectively and efficiently transpile C code to idiomatic and memory-safe code in Rust? RQ2: Can a RAG-based pipeline reduce the LLM hallucinations for code generation, thus improving the correctness of the LLM-generated/transpiled Rust code? The remainder of this paper is structured as follows. Section 2 provides brief background information on various topics. Section 3 reviews related work in the literature. Next, Section 4 proposes our novel approach. The experimental study in Section 5 validates the proposed approach. Section 6 discusses the results and answers the RQs. Furthermore, we point out a few threats to the validity of the outcomes in Section 7 . Finally, Section 8 concludes the paper and outlines directions for future work. 2. Background Figure 1. The RAG similarity search process. External documents are embedded into vectors, and a query is embedded into a query vector. Similarity check (e.g., Cosine Similarity) identifies the most relevant document chunks to augment the query with contextual knowledge. This section provides a general background of code transpilation, LLMs, RAG, memory safety, and Rust, with a central focus on how these concepts relate to the proposed approach of the paper. 2.1. Code Transpilation Code transpilation, the process of transferring source code from one programming language to another, was historically performed manually using extensive hand-crafted rules and requiring deep domain expertise ( 21 ) . While this approach facilitated some early efforts, it proved to be both time-consuming and resource-intensive, ultimately making it inefficient and ineffective. As a result, manual transpilation methods saw limited adoption in practice.

Read the complete text