arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2006.09973cs.SEcs.LGcs.PL
收藏

围棋中的破式安全--不安全包运用的实证研究

Breaking Type Safety in Go: An Empirical Study on the Usage of the unsafe Package

Concordia University(康考迪亚大学);Queen’s University(女王大学)

Diego Elias Costa, Suhaib Mujahid, Rabe Abdalkareem, Emad Shihab

摘要:

在第一次发布十年后,Go程序设计语言已经成为开发领域的主要程序设计语言。虽然Go语言因其干净的语法和类似C语言的性能而受到称赞,但它也包含一个强大的静态类型系统,可以防止任意类型转换和任意内存访问,使语言在设计上是类型安全的。然而,为了让开发人员能够实现低级代码,Go附带了一个名为unsafe的特殊包,为开发人员提供了一种绕过Go程序类型安全的方法。该包为开发人员提供了更大的灵活性,但存在更高的运行时错误风险,不可移植性的可能性,以及未来版本Go的兼容性保证的损失。在本文中,我们首次对2,438个流行Go项目中不安全包的使用情况进行了大规模研究。我们的调查显示,unsafe在24%的Go项目中使用,主要是为了与操作系统和C代码进行通信,但也常被用作性能优化的来源。开发人员愿意使用unsafe来破坏语言规范(例如,字符串不变性)以获得更好的性能,6%的分析项目使用unsafe执行有风险的指针转换,这可能导致程序崩溃和意外行为。此外,我们报告了使用不安全的项目所面临的一系列实际问题,从崩溃错误和不确定性行为到限制其部署到某些流行环境。我们的研究结果可以用来理解开发人员如何以及为什么在Go中破坏类型安全,并有助于激励进一步的工具和语言开发,使Go中的unsafe用法更加安全。

英文摘要:

A decade after its first release, the Go programming language has become a major programming language in the development landscape. While praised for its clean syntax and C-like performance, Go also contains a strong static type-system that prevents arbitrary type casting and arbitrary memory access, making the language type-safe by design. However, to give developers the possibility of implementing low-level code, Go ships with a special package called unsafe that offers developers a way around the type-safety of Go programs. The package gives greater flexibility to developers but comes at a higher risk of runtime errors, chances of non-portability, and the loss of compatibility guarantees for future versions of Go. In this paper, we present the first large-scale study on the usage of the unsafe package in 2,438 popular Go projects. Our investigation shows that unsafe is used in 24% of Go projects, motivated primarily by communicating with operating systems and C code, but is also commonly used as a source of performance optimization. Developers are willing to use unsafe to break language specifications (e.g., string immutability) for better performance and 6% of analyzed projects that use unsafe perform risky pointer conversions that can lead to program crashes and unexpected behavior. Furthermore, we report a series of real issues faced by projects that use unsafe, from crashing errors and non-deterministic behavior to having their deployment restricted from certain popular environments. Our findings can be used to understand how and why developers break type-safety in Go, and help motivate further tools and language development that could make the usage of unsafe in Go even safer.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑