用三阶段微调让大模型更精准地把C代码转成安全的Rust代码。
Fine-Tuning Qwen3-27B for C-to-Rust Code Translation: A Three-Stage Curriculum of Pretraining, Debugging-Aware SFT, and Task-Specific SFT
- 分三步微调:先学Rust语法,再学调试修复,最后学跨语言语义转换。
- 在LeetCode数据上微调后,翻译成功率提升,且更少出现不规范或危险代码。
- 适合需要将老旧C系统安全迁移到Rust的开发者和研究者。
将C代码转化为安全、符合习惯的Rust代码是长期存在的软件工程目标,可消除大量内存安全漏洞并保留原有功能。尽管大语言模型(LLMs)在此任务上展现潜力,但通用预训练模型通常表现不佳,因缺乏对地道Rust写法、跨语言语义等价性以及根据编译/运行时反馈进行推理与修复的能力。本报告描述了针对Qwen3-27B的三阶段微调方案:(1) 在以Rust为核心的语料库上继续预训练,强化模型对地道Rust语法与标准库使用的先验;(2) 使用microsoft/Verus_Training_Data数据集进行监督微调(SFT),培养模型对Rust代码的调试与自修复能力;(3) 在来自LeetCode的配对C/Rust解决方案上进行任务特定微调,学习直接语义翻译。我们采用SACTOR的智能体式、静态分析引导的验证框架进行评估,该框架支持结构感知的两阶段(非规范到规范)翻译,并基于FFI实现端到端(E2E)测试。报告包含成功率、地道性(Clippy lint数、不安全代码占比)及失败模式分析,并与基线Qwen3-27B及其他在相同框架下评估的LLM进行对比。
原文摘要 · Abstract (English)
Translating C code into safe, idiomatic Rust is a longstanding software-engineering goal because it can eliminate entire classes of memory-safety vulnerabilities while preserving the functional behavior of legacy systems. Large language models (LLMs) have shown promise for this task but typically underperform when applied off-the-shelf, since general-purpose pretraining rarely emphasizes idiomatic Rust generation, cross-language semantic equivalence, or the ability to reason about and repair compiler/runtime feedback. In this report we describe a three-stage fine-tuning curriculum applied to Qwen3-27B that is designed to progressively specialize the model for the C-to-Rust (C2Rust) translation task: (1) continued pretraining on Rust-centric corpora to strengthen the model's prior over idiomatic Rust syntax and standard-library usage; (2) supervised fine-tuning (SFT) on the microsoft/Verus_Training_Data dataset to instill debugging and self-repair behavior over Rust code; and (3) task-specific SFT on paired C/Rust solutions derived from LeetCode problems to teach direct semantic translation. We evaluate the resulting model using the agentic, static-analysis-guided verification framework of SACTOR, which performs structure-aware, two-phase (unidiomatic to idiomatic) translation with foreign-function-interface (FFI)-based end-to-end (E2E) testing. We report success rate, idiomaticity (Clippy lint counts, unsafe-code fraction), and failure-mode analyses, and compare our fine-tuned model against baseline Qwen3-27B and other LLMs evaluated under the same framework.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。