将C代码安全转为Rust,通过分步封装与智能修正,避免内存错误。
ENCRUST: Encapsulated Substitution and Agentic Refinement on a Live Scaffold for Safe C-to-Rust Translation
- 用接口保持的包装层分离函数逻辑与边界,支持独立翻译。
- 在15个真实项目中减少大量不安全代码,且测试全部通过。
- 适合需要高安全性迁移的大型项目团队使用。
我们提出Encrust:一种两阶段的C到Rust安全翻译框架。现有方法或生成无内存安全保障的代码,或孤立翻译函数,无法发现跨模块类型不匹配,也无法处理需全程序推理的不安全构造。此外,函数级LLM管道在签名变更时需协调调用方更新,而项目级系统常因依赖复杂性无法生成可编译代码。Encrust通过应用二进制接口(ABI)保持的包装模式,将边界适配与函数逻辑解耦,并在集成代码库中验证每个中间状态。第一阶段(封装替换)利用保留原始指针签名的外壳函数,将每函数拆分为调用透明的外壳和由LLM处理的安全内函数,实现独立类型修改并自动回滚失败,无需协调调用方更新;成功后通过确定性、类型导向的包装消除步骤移除外壳。第二阶段(代理精炼)使用基于基线感知验证门控的LLM代理,在整个代码库上解决静态全局变量、跳过的包装对及翻译失败等跨函数问题。我们在7个GNU Coreutils程序和8个Laertes基准库上评估,所有15个程序均显著减少不安全构造,同时保持完整测试向量正确性。
原文摘要 · Abstract (English)
We present Encapsulated Substitution and Agentic Refinement on a Live Scaffold for Safe C-to-Rust Translation, a two-phase pipeline for translating real-world C projects to safe Rust. Existing approaches either produce unsafe output without memory-safety guarantees or translate functions in isolation, failing to detect cross-unit type mismatches or handle unsafe constructs requiring whole-program reasoning. Furthermore, function-level LLM pipelines require coordinated caller updates when type signatures change, while project-scale systems often fail to produce compilable output under real-world dependency complexity. Encrust addresses these limitations by decoupling boundary adaptation from function logic via an Application Binary Interface (ABI)-preserving wrapper pattern and validating each intermediate state against the integrated codebase. Phase 1 (Encapsulated Substitution) translates each function using an ABI-preserving wrapper that splits it into two components: a caller-transparent shim retaining the original raw-pointer signature, and a safe inner function targeted by the LLM with a clean, scope-limited prompt. This enables independent per-function type changes with automatic rollback on failure, without coordinated caller updates. A deterministic, type-directed wrapper elimination pass then removes wrappers after successful translation. Phase 2 (Agentic Refinement) resolves unsafe constructs beyond per-function scope, including static mut globals, skipped wrapper pairs, and failed translations, using an LLM agent operating on the whole codebase under a baseline-aware verification gate. We evaluate Encrust on 7 GNU Coreutils programs and 8 libraries from the Laertes benchmark, showing substantial unsafe-construct reduction across all 15 programs while maintaining full test-vector correctness.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。