Towards Translating Real-World Code with LLMs: A Study of Translating to Rust

Hasan Ferit Eniser, Hanliang Zhang, Cristina David, Maria Christakis, Brandon Paulsen, Joey Dodds, Daniel Kroening · DOI · OpenReview

TL;DR

Large Language Models (LLMs) show promise for translating code, but their effectiveness on real-world projects remains largely untested. This paper introduces FLUORINE, a framework that uses differential fuzzing to automatically validate LLM-generated Rust translations of C and Go code. The study reveals that while LLMs can translate a significant portion of real-world code, simple re-generation strategies are more effective for fixing errors than providing counterexamples.

What Problem Does This Paper Address?

Modern software development increasingly relies on safe and efficient programming languages like Rust. However, a vast amount of existing code is written in languages like C and Go, often containing legacy systems that are difficult and costly to maintain. Manually translating this code to Rust is a time-consuming and error-prone process. While Large Language Models (LLMs) have demonstrated impressive capabilities in code generation and translation, their effectiveness on complex, real-world codebases, as opposed to simpler competitive programming problems, has not been thoroughly investigated. Furthermore, ensuring the functional correctness of these translations is a significant challenge, as traditional methods rely on existing unit tests, which are often unavailable for legacy code.

Methods

The core of this work is the FLUORINE framework, designed for automated, validated translation to Rust. FLUORINE addresses two main challenges:

  1. Real-world project size: LLMs have limited context windows. FLUORINE tackles this by extracting manageable code samples (1-25 functions, up to 598 lines) from larger projects.
  2. Lack of target language unit tests: To ensure functional correctness (I/O equivalence), FLUORINE employs a cross-language differential fuzzer. This fuzzer automatically generates inputs, runs both the original source program and its Rust translation, and compares their outputs. If a discrepancy is found, it's reported as a counterexample.

The translation process within FLUORINE involves:

  • LLM-based translation: An LLM (e.g., GPT-4, Claude 3) is prompted to translate the source code.
  • Compilation Repair: If the initial translation doesn't compile, an LLM is used to fix compilation errors.
  • Differential Fuzzing: The compiled translation is then validated against the original code using the cross-language differential fuzzer.
  • Feedback Strategies: If the fuzzer finds counterexamples (indicating incorrect translations), various feedback strategies are employed to prompt the LLM to generate a corrected translation. These include:
  • Simple Restart: Discarding the faulty translation and re-querying the LLM with the original prompt.
  • Hinted Restart: Similar to Simple Restart, but also includes positive and negative examples from the fuzzer in the prompt.
  • BaseRepair: Providing counterexamples directly to the LLM to guide repair.
  • Conversational Repair (CAPR): Maintaining a dialogue with the LLM, including previous faulty translations to discourage repetition.

To facilitate cross-language comparison, FLUORINE uses JSON serialization/deserialization to exchange program states between languages.

Datasets

The authors created a new benchmark dataset by extracting code samples from seven real-world open-source projects written in C and Go. These projects span diverse domains like audio processing, text processing, and geometry. The extracted samples contain complex features often absent in simpler benchmarks, such as global variables, user-defined data structures, and pointer types. The dataset consists of 408 code samples, with sizes ranging from 1 to 25 functions and up to 598 lines of code.

Key Insights and Ideas

  • Real-world code is challenging but translatable: LLMs can indeed translate significant portions of real-world code, which is a crucial step towards modernizing legacy systems.
  • Counterexamples aren't always helpful: Surprisingly, providing specific counterexamples to LLMs for repair (BaseRepair, CAPR) was less effective than simply restarting the translation process (Restart, Hinted). This suggests LLMs may struggle to interpret complex, randomly generated counterexamples.
  • Simplicity wins in feedback: The most effective strategy for fixing translation bugs was to discard the faulty translation and re-prompt the LLM with the original instructions. This indicates that LLMs might perform better when given a fresh start rather than trying to correct specific errors.
  • Code complexity is a major hurdle: As the number of lines of code increases, the success rate of LLM translations drops significantly, especially beyond 100 lines. This highlights the limitations of current LLM context windows and their ability to maintain coherence over longer code segments.
  • LLMs produce generally good Rust code: Successful translations were largely idiomatic and compilable, with few correctness issues. However, there's room for improvement in style, conciseness, and performance, which could potentially be addressed through more specific prompting.

Key Results

  • The most successful LLM (GPT-4) achieved an overall 47.3% success rate in translating the real-world code benchmarks. Other LLMs performed as follows: Claude 2 (44.2%), Claude 3 (38.5%), Gemini Pro (33.8%), and Mixtral (19.5%).
  • Success rates decrease with code complexity, particularly for code exceeding 100 lines.
  • Feedback strategies increased the translation success rate by 7-21%.
  • The Restart and Hinted feedback strategies outperformed BaseRepair and CAPR by a significant margin.
  • LLM-generated Rust code was generally idiomatic, with only 1-15% of successful translations showing linter warnings for style, complexity, or performance issues. Correctness warnings were rare.
  • Serialization failures were the most common reason for translation failure (52.6%), followed by counterexamples found by the fuzzer (40.3%).

Limitations and Future Work

  • Fuzzer limitations: The differential fuzzer, while achieving 97% coverage, cannot guarantee 100% correctness and may miss subtle bugs.
  • Serialization issues: Certain Rust features (e.g., trait definitions, lifetimes) are not fully supported by the JSON serialization, leading to some translation failures.
  • Benchmark scope: The study excluded code using third-party libraries, concurrency, network, or file I/O.
  • Future work: The authors suggest exploring methods for handling larger code chunks by partitioning projects, investigating more intuitive counterexample generation or selection, and refining LLM prompts to improve idiomaticity and performance. They also propose exploring automated program repair techniques that train a second LLM to fix translation errors.

Summary updated

BibTeX
@misc{eniser2024towards,
  author = {Hasan Ferit Eniser and Hanliang Zhang and Cristina David and Maria Christakis and Brandon Paulsen and Joey Dodds and Daniel Kroening},
  title = {{Towards Translating Real-World Code with LLMs: A Study of Translating to Rust}},
  year = {2024},
  eprint = {2405.11514},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2405.11514},
}
Figure