QEMU/CPC:静态分析和CPS转换,用于安全、可移植和高效的协同程序
QEMU/CPC: Static Analysis and CPS Conversion for Safe, Portable, and Efficient Coroutines
摘要:
协程和事件是编写并发程序的两种常见抽象。因为协程通常更方便,而事件更可移植和更有效,所以很自然地想要将前者转换为后者。CPC就是这样一个C程序的源代码到源代码翻译器,它基于将注释为协作的函数部分转换为延续传递风格(CPS转换)。在这篇文章中,我们研究了CPC翻译器在QEMU中的应用,QEMU是一个开源的机器仿真器,它也使用带注释的协程函数来实现并发。我们首先提出了一种新的类型的注释,以确定功能,从来没有合作,我们介绍了CoroCheck,一个工具的静态分析和推理的合作注释。然后,我们改进了CPC翻译器,将CPS转换定义为C语言的调用约定,支持通过函数指针间接调用CPS转换后的函数。最后,我们将CoroCheck和CPC应用于QEMU(75万行C代码),修复了数百个缺失的注释,并将翻译后的代码与QEMU中现有的协程实现进行了性能比较。我们的工作表明,静态注释检查,以防止实际的并发错误的重要性,并表明CPS转换是一种灵活的,便携式的,高效的编译技术,即使是非常大的程序编写的命令式语言。
英文摘要:
Coroutines and events are two common abstractions for writing concurrent programs. Because coroutines are often more convenient, but events more portable and efficient, it is natural to want to translate the former into the latter. CPC is such a source-to-source translator for C programs, based on a partial conversion into continuation-passing style (CPS conversion) of functions annotated as cooperative. In this article, we study the application of the CPC translator to QEMU, an open-source machine emulator which also uses annotated coroutine functions for concurrency. We first propose a new type of annotations to identify functions which never cooperate, and we introduce CoroCheck, a tool for the static analysis and inference of cooperation annotations. Then, we improve the CPC translator, defining CPS conversion as a calling convention for the C language, with support for indirect calls to CPS-converted function through function pointers. Finally, we apply CoroCheck and CPC to QEMU (750 000 lines of C code), fixing hundreds of missing annotations and comparing performance of the translated code with existing implementations of coroutines in QEMU. Our work shows the importance of static annotation checking to prevent actual concurrency bugs, and demonstrates that CPS conversion is a flexible, portable, and efficient compilation technique, even for very large programs written in an imperative language.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。