Adversarial learning improves c to rust translation quality

Translator vs. Challenger: Adversarial Agentic Learning for C-to-Rust Translation

Software Engineering

Summary

Translating code from the C programming language to Rust is difficult because they work very differently. The authors created a method called TRAIL, which uses two smart programs working together: one tries to improve translations by learning from mistakes, and the other looks for any weak spots in these improvements. By challenging and refining these improvements repeatedly, TRAIL turns specific fixes into strong, reusable knowledge for translating many different programs. Tests show TRAIL makes translations more accurate in both the way they look and what they actually do.

What this means in practice

  • For software developers: Improve automated tools that convert legacy C codebases to Rust with more accurate and reliable translation knowledge.
  • For software quality teams: Enhance code migration processes by identifying and addressing corner cases and weaknesses in translation automatically.

Authors

Chaofan Wang, Xiaodong Gu, Yuling Shi, Chao Hu, Beijun Shen

Abstract

C-to-Rust translation remains challenging due to the substantial semantic gap between the two languages. Recent experience-enhanced LLM translators improve translation quality by learning reusable insights from prior failures and repairs. Yet learned insights do not automatically constitute reusable translation knowledge: derived from sparse, program-specific traces, they often contain missing conditions, narrow applicability boundaries, or overlooked corner cases. This limits their robustness and generalizability in new translation scenarios. We present TRAIL, an adversarial agentic learning framework for robust C-to-Rust translation. TRAIL employs two collaborating agents: a Translator that derives candidate insights from translation failures and accepted repairs, and a Challenger that actively searches for weaknesses, gaps, and boundary cases through adversarial challenges. To improve the robustness of individual insights and the completeness of insight collections, TRAIL performs adversarial learning at two levels. Individual-insight adversarial learning repeatedly stress-tests each insight to refine its applicability conditions and constraints, while compositional insight adversarial learning strengthens groups of related insights by exposing conflicts, gaps, and uncovered corner cases. By challenging insights and their compositions with executable counterexamples, TRAIL transforms trace-specific experience into robust, reusable, and generalizable translation knowledge. We evaluate TRAIL on two project-level benchmarks, CRUST-Bench and SmartC2Rust-Bench. Compared with the strongest LLM-based baseline, TRAIL achieves average relative improvements of 23.1% in syntax accuracy and 15.9% in semantic accuracy. Furthermore, the adversarially refined insights transfer effectively across benchmarks, demonstrating strong generalizability across diverse C-to-Rust translation tasks.