一句话:首个将 C++ 程序自动翻译为功能等价且内存安全 Rust 代码的系统,以运行时所有权检查换取安全,并用源到源优化降低开销。
C/C++ 中的内存安全缺陷导致了广泛部署软件约 70% 的安全漏洞,而手工将遗留 C++ 代码迁移到 Rust 成本高昂且易出错。本文提出 Cpp2Rust,首个能把 C++ 程序自动翻译为功能等价且内存安全 Rust 代码的系统。针对 C++ 无限制别名与 Rust 所有权模型之间的根本不匹配,Cpp2Rust 选择以部分性能换取安全性:插入运行时强制执行的所有权与可变性检查,在保持原语义的同时保证内存安全。为缓解动态检查带来的开销,作者设计了一套面向 Rust 的源到源优化,消除冗余的所有权操作以恢复性能。在 WOFF2 字体压缩库和 Brunsli JPEG 无损压缩库两个真实 C++ 程序(共 1.3 万行代码)上评估:WOFF2 实现完全内存安全且压缩性能仅下降 2%,而 Brunsli 因大量指针运算慢 6 倍。结果表明,自动、保持语义的 C++ 到安全 Rust 翻译对部分安全关键应用是可行的。
About 70% of security vulnerabilities in widely deployed software originate from memory-safety bugs in languages such as C and C++. Despite decades of investment in mitigations, from static analysis and sanitizers to hardware isolation, attackers continue to exploit unsafe memory operations. A promising long-term solution is to migrate existing C++ codebases to memory-safe languages such as Rust, but doing so manually is prohibitively expensive and error-prone. In this paper, we present Cpp2Rust, the first system capable of translating C++ programs into functionally equivalent and memory-safe Rust code automatically. By trading some performance for security, Cpp2Rust addresses the fundamental mismatch between C++’s unrestricted aliasing and Rust’s ownership model by inserting runtime-enforced ownership and mutability checks, ensuring safety while preserving semantics. To mitigate the performance overhead of dynamic checks, we developed a suite of source-to-source optimizations for Rust code that eliminate redundant ownership operations and recover much of the lost performance. We evaluate Cpp2Rust on two real-world C++ programs, totaling 13k lines of code: WOFF2, a font compression library, and Brunsli, a JPEG lossless compression library. Cpp2Rust achieves full memory safety with only a 2% performance penalty on WOFF2 compression, while being 6× slower on Brunsli due to heavy usage of pointer arithmetic. These results demonstrate that automated, semantics-preserving translation from C++ to safe Rust is practical for some safety-critical applications, offering a viable path toward eliminating memory-safety vulnerabilities in legacy systems.
@article{PopescuGPSHSL26,
title = {Cpp2Rust: Automatic Translation of C++ to Safe Rust},
author = {Lucian Popescu and Francisco Gouveia and Henrique Preto and João Silveira and Dmytro Hrybenko and José Fragoso Santos and Nuno P. Lopes},
journal = {Proceedings of the ACM on Programming Languages},
volume = {10},
number = {PLDI},
year = {2026},
doi = {10.1145/3808266}
}