一句话:提出字节码级内存检查器 SafeCheck,防止 Java Unsafe API 误用导致 JVM 崩溃。
Java 通过字节码验证和内存保护提供安全性,但 Java 运行时仍以 Unsafe API 作为访问底层系统代码的后门。越来越多第三方库为追求高性能而使用该 API,而误用会造成数据损坏、资源泄漏和难以诊断的 JVM 崩溃。本文研究 Unsafe 的崩溃模式,并提出一个字节码层面的内存检查器 SafeCheck,对 Unsafe API 的使用强制执行内存安全,从而避免误用导致的 JVM 崩溃。作者在 OpenJDK bug 系统的真实崩溃案例和 AJDK 的真实应用上评估该技术,将开发者诊断相关崩溃的时间从数天缩短到数分钟;在密集使用 Unsafe 操作的项目上,该工具带来的运行时开销可忽略。
Java is a safe programming language by providing bytecode verification and enforcing memory protection. For instance, programmers cannot directly access the memory but have to use object references. Yet, the Java runtime provides an Unsafe API as a backdoor for the developers to access the low- level system code. Whereas the Unsafe API is designed to be used by the Java core library, a growing community of third-party libraries use it to achieve high performance. The Unsafe API is powerful, but dangerous, which leads to data corruption, resource leaks and difficult-to-diagnose JVM crash if used improperly. In this work, we study the Unsafe crash patterns and propose a memory checker to enforce memory safety, thus avoiding the JVM crash caused by the misuse of the Unsafe API at the bytecode level. We evaluate our technique on real crash cases from the openJDK bug system and real-world applications from AJDK. Our tool reduces the efforts from several days to a few minutes for the developers to diagnose the Unsafe related crashes. We also evaluate the runtime overhead of our tool on projects using intensive Unsafe operations, and the result shows that our tool causes a negligible perturbation to the execution of the applications.