我见过类似的问题:如何崩溃一个jvm和最短代码以提高SIGSEGv。
有一些Java代码可以生成SIGSEGV,例如:
final Constructor<Unsafe> unsafeConstructor = Unsafe.class.getDeclaredConstructor();
unsafeConstructor.setAccessible(true);
final Unsafe unsafe = unsafeConstructor.newInstance();
System.out.println(unsafe.getAddress(0));
Run Code Online (Sandbox Code Playgroud)
并生成SIGSEGV类型的V(VM帧)。
# JRE version: Java(TM) SE Runtime Environment (8.0_101-b13) (build 1.8.0_101-b13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.101-b13 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# V [jvm.dll+0x1e2440]
Run Code Online (Sandbox Code Playgroud)
并且Crash in Compiled Code根据https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/crashes001.html进行。
我想知道是否仍然会产生类型化的J分段错误。
而且我已经看到了一些lib问题,例如JVM崩溃。(所以它表明可以手动生成?)
这是一个在编译后的代码中重现崩溃的程序。
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class Crash extends Thread {
static volatile Object obj = 0;
public static void main(String[] args) throws Exception {
new Crash().start();
// Give some time to compile run() method
Thread.sleep(2000);
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Unsafe unsafe = (Unsafe) f.get(null);
// Overwrite Object's class field, so that 'instanceof' cannot work
unsafe.putInt(obj, 8L, -1);
}
public void run() {
while (!(obj instanceof Runnable)) {
// Loop until crash
}
}
}
Run Code Online (Sandbox Code Playgroud)
如此运作。(或者说“它不起作用 ”是正确的:)
instanceof由于obj不稳定,支票无法优化。obj通过将垃圾写入偏移量#8的类字段来破坏标头。instanceofcheck依赖于对象类。这就是我们将得到的。
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000000368b6ad, pid=9660, tid=0x00000000000032f0
#
# JRE version: Java(TM) SE Runtime Environment (8.0_192-b12) (build 1.8.0_192-b12)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.192-b12 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# J 38% C2 Crash.run()V (13 bytes) @ 0x000000000368b6ad [0x000000000368b640+0x6d]
#
Run Code Online (Sandbox Code Playgroud)