在我过去的一次采访中,有人让我写一个代码来破坏JVM.我说System.exit().它是否正确?还有更好的答案吗?
澄清:我可以在开发和部署期间包含我的代码.并不是JVM已经在运行,我必须编写一个黑客代码来破坏其他JVM.
Pet*_*rey 18
您可以使用Unsafe您可能猜到的不安全的类.
public static void main(String... args) throws Exception {
getUnsafe().getByte(0);
}
private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(null);
}
Run Code Online (Sandbox Code Playgroud)
版画
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007ff1c2f23368, pid=2630, tid=140676351506176
#
# JRE version: 7.0-b147
# Java VM: Java HotSpot(TM) 64-Bit Server VM (21.0-b17 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V [libjvm.so+0x82c368] Unsafe_GetNativeByte+0xa8
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /nfs/peter/IdeaProjects/scratch/hs_err_pid2630.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
#
Run Code Online (Sandbox Code Playgroud)
我用它来测试何时对文件进行了更改.我做了更改并崩溃了JVM,以确保其他东西没有冲洗或稍后.然后我检查我看到了我预期的更新.
Jon*_*eet 16
我不认为礼貌地请求JVM终止真的被视为"崩溃"它.你必须向受访者询问"崩溃"的确切含义.例如:
(过去肯定存在JIT编译器问题 - 很久以前我有一个纯Java程序,它一直是段错误的,但可以通过制作看似无操作的变化来"修复".)
简单:要么使用无限递归来破坏堆栈,要么将JVM从对象堆中运行出来.
更复杂的是将JVM从内部堆中运行 - 您可以使用某种类加载循环来执行此操作,但这需要工作.
否则你必须利用某种bug,或者至少进入JNI.
public class Recur {
public static void main(String[] argv) {
recur();
}
static void recur() {
Object[] o = null;
try {
while(true) {
Object[] newO = new Object[1];
newO[0] = o;
o = newO;
}
}
finally {
recur();
}
}
}
C:\JavaTools>java Recur
#
# An unexpected error has been detected by Java Runtime Environment:
#
# EXCEPTION_STACK_OVERFLOW (0xc00000fd) at pc=0x000000006dad5c3d, pid=6816, tid
=5432
#
# Java VM: Java HotSpot(TM) 64-Bit Server VM (11.2-b01 mixed mode windows-amd64)
# Problematic frame:
# V [jvm.dll+0x2e5c3d]
#
# An error report file with more information is saved as:
# C:\JavaTools\hs_err_pid6816.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#
Run Code Online (Sandbox Code Playgroud)
塔达!!
(并且您可以限制对不安全的访问,但不能限制以上任何一种.)