Concurrent programs suffer from data races. To prevent data races, programmers use locks. However, programs can eliminate data races only when they acquire and release correct locks at correct timing. The lock API of C, in which people have developed a large portion of legacy system programs, does not validate the correct use of locks. On the other hand, Rust, a recently developed system programming language, provides a lock API that guarantees the correct use of locks via type checking. This makes rewriting legacy system programs in Rust a promising way to retrofit safety into them. Unfortunately, manual C-to-Rust translation is extremely laborious due to the discrepancies between their lock APIs. Even the state-of-the-art automatic C-to-Rust translator retains the C lock API, expecting developers to replace them with the Rust lock API. In this work, we propose an automatic tool to replace the C lock API with the Rust lock API. It facilitates C-to-Rust translation of concurrent programs with less human effort than the current practice. Our tool consists of a Rust code transformer that takes a lock summary as an input and a static analyzer that efficiently generates precise lock summaries. We show that the transformer is scalable and widely applicable while preserving the semantics; it transforms 66 KLOC in 2.6 seconds and successfully handles 74% of real-world programs. We also show that the analyzer is scalable and precise; it analyzes 66 KLOC in 4.3 seconds.
翻译:并发程序常受数据竞争困扰。为防范数据竞争,程序员使用锁机制。然而,程序仅在正确时机获取与释放正确锁时才能消除数据竞争。在大部分遗留系统程序的开发中使用的C语言锁API,无法验证锁使用的正确性。而新近开发的系统编程语言Rust,通过类型检查机制提供了保证锁使用正确性的锁API。这使得用Rust重写遗留系统程序成为提升其安全性的可行途径。遗憾的是,由于两种语言锁API的差异,手动将C代码转换为Rust极其繁琐。即便当前最先进的自动C到Rust转换工具,仍会保留C语言锁API,期待开发者手动替换为Rust锁API。本研究提出一种自动工具,可替代C语言锁API为Rust锁API,相比现有实践大幅减少并发程序C到Rust转换所需的人力投入。该工具由两个模块组成:接收锁摘要作为输入的Rust代码转换器,以及高效生成精确锁摘要的静态分析器。实验表明,转换器具备可扩展性与广泛适用性,能在2.6秒内处理66KLOC代码,并成功适配74%的真实程序;分析器同样兼具可扩展性与精确性,可在4.3秒内完成66KLOC代码的分析。