Table of Contents
Fetching ...

Search-Based Multi-Trajectory Refinement for Safe C-to-Rust Translation with Large Language Models

HoHyun Sim, Hyeonjoong Cho, Yeonghyeon Go, Sadegh AlMahdi Kazemi Zarkouei, Zhoulai Fu, Ali Shokri, Binoy Ravindran

Abstract

The C programming language has been foundational in building system-level software. However, its manual memory management model frequently leads to memory safety issues. In response, Rust has emerged as a memory-safe alternative. Moreover, automating the C-to-Rust translation empowered by the rapid advancements of the generative capabilities of LLMs is gaining growing interest for large volumes of legacy C code. Leveraging LLM for the C-to-Rust translation introduces distinct challenges, unlike the math or commonsense QA domains where the LLMs have been predominantly applied. First, the scarcity of parallel C-to-Rust datasets hinders the retrieval of suitable code translation exemplars for in-context learning. Second, unlike math or commonsense QA problems, the intermediate steps required for C-to-Rust are not well-defined. Third, it remains unclear how to organize and cascade these intermediate steps to construct a correct translation trajectory. While existing LLM-based approaches have achieved some success, they have relied on iterative code refinement along a single search trajectory on a C-to-Rust problem space and have not explored the use of systematic search mechanisms to navigate the space of possible refinement trajectories. To address these challenges in the C-to-Rust translation, we propose the MCTS-Guided LLM refinement technique for automated C-to-safe-Rust translation (LAC2R). LAC2R uses MCTS to systematically explore multiple refinement trajectories and organize the LLM-induced intermediate steps for correct translation. We experimentally demonstrated that LAC2R effectively conducts C-to-Rust translation on large-scale, real-world benchmarks. On small-scale benchmarks, LAC2R is the only method that simultaneously attains the highest safety ratio, perfect project-level correctness, and the fewest linter warnings among the compared methods.

Search-Based Multi-Trajectory Refinement for Safe C-to-Rust Translation with Large Language Models

Abstract

The C programming language has been foundational in building system-level software. However, its manual memory management model frequently leads to memory safety issues. In response, Rust has emerged as a memory-safe alternative. Moreover, automating the C-to-Rust translation empowered by the rapid advancements of the generative capabilities of LLMs is gaining growing interest for large volumes of legacy C code. Leveraging LLM for the C-to-Rust translation introduces distinct challenges, unlike the math or commonsense QA domains where the LLMs have been predominantly applied. First, the scarcity of parallel C-to-Rust datasets hinders the retrieval of suitable code translation exemplars for in-context learning. Second, unlike math or commonsense QA problems, the intermediate steps required for C-to-Rust are not well-defined. Third, it remains unclear how to organize and cascade these intermediate steps to construct a correct translation trajectory. While existing LLM-based approaches have achieved some success, they have relied on iterative code refinement along a single search trajectory on a C-to-Rust problem space and have not explored the use of systematic search mechanisms to navigate the space of possible refinement trajectories. To address these challenges in the C-to-Rust translation, we propose the MCTS-Guided LLM refinement technique for automated C-to-safe-Rust translation (LAC2R). LAC2R uses MCTS to systematically explore multiple refinement trajectories and organize the LLM-induced intermediate steps for correct translation. We experimentally demonstrated that LAC2R effectively conducts C-to-Rust translation on large-scale, real-world benchmarks. On small-scale benchmarks, LAC2R is the only method that simultaneously attains the highest safety ratio, perfect project-level correctness, and the fewest linter warnings among the compared methods.

Paper Structure

This paper contains 12 sections, 6 equations, 11 figures, 6 tables, 2 algorithms.

Figures (11)

  • Figure 1: The common execution flow of existing LLM-based C-to-Rust translation methods. The dotted connections are selectively established.
  • Figure 2: Overview of LAC2R’s execution flow with an illustration of its key components.
  • Figure 3: LAC2R's translation example including a target C function, its unsafe Rust conversion by C2Rust, and its final Rust translation. argmatch_exact() is a sub-function of tail benchmark in the GNU coreutils suite.
  • Figure 4: The FRR distribution of C2SaferRust's iterative Rust refinement over iterations.
  • Figure 5: Main prompt used in LAC2R. When compilation or test-run errors occur, the error messages are appended to the conversation history and passed back to the LLM as feedback for the next Fix-node expansion.
  • ...and 6 more figures