用大模型与动态分析结合,自动将C代码转为安全的Rust代码
Syzygy: Dual Code-Test C to (safe) Rust Translation using LLMs and Dynamic Analysis
- 利用大模型生成代码+测试用例,动态分析指导翻译顺序
- 成功转换3000行代码的压缩库Zopfli,验证结果与原C程序一致
- 适合想自动化迁移老旧C系统到安全Rust的开发者
尽管C语言广泛应用于高性能、底层系统编程,但其手动内存管理和不安全指针操作易引入漏洞。Rust作为现代系统编程语言,凭借独特的所有权模型和类型系统,在不牺牲性能的前提下保障内存安全。本文提出Syzygy,一种自动将C代码转换为安全Rust的方法。该方法融合大模型驱动的代码与测试用例生成,并借助动态分析提供的执行信息进行协同翻译。翻译过程按代码元素依赖顺序迭代执行,每步保持正确性。该方法揭示了大模型与动态分析在规模化代码生成与测试结合中的新思路。我们成功将包含约3000行代码、98个函数的高性能压缩库Zopfli转换为Rust,并通过输入集上的等价性测试验证了转换结果。据我们所知,这是迄今最大规模且经过测试验证的自动化C到安全Rust代码转换。
原文摘要 · Abstract (English)
Despite extensive usage in high-performance, low-level systems programming applications, C is susceptible to vulnerabilities due to manual memory management and unsafe pointer operations. Rust, a modern systems programming language, offers a compelling alternative. Its unique ownership model and type system ensure memory safety without sacrificing performance. In this paper, we present Syzygy, an automated approach to translate C to safe Rust. Our technique uses a synergistic combination of LLM-driven code and test translation guided by dynamic-analysis-generated execution information. This paired translation runs incrementally in a loop over the program in dependency order of the code elements while maintaining per-step correctness. Our approach exposes novel insights on combining the strengths of LLMs and dynamic analysis in the context of scaling and combining code generation with testing. We apply our approach to successfully translate Zopfli, a high-performance compression library with ~3000 lines of code and 98 functions. We validate the translation by testing equivalence with the source C program on a set of inputs. To our knowledge, this is the largest automated and test-validated C to safe Rust code translation achieved so far.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。