java 编写Java代码使Java虚拟机崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7771068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write Java code to crash the java virtual machine
提问by java_mouse
In one of my past interviews , someone asked me to write a code to crash the JVM. I said System.exit(). Is this correct? Are there any better answers?
在我过去的一次采访中,有人让我写一段代码来让 JVM 崩溃。我说的是 System.exit()。这个对吗?有没有更好的答案?
clarification: I can include my piece of code during development and deploy. It is not that JVM is already running and I have to write a hacking code to crash the other JVM.
说明:我可以在开发和部署过程中包含我的一段代码。并不是 JVM 已经在运行,我必须编写一个黑客代码来使另一个 JVM 崩溃。
回答by Peter Lawrey
You can use the Unsafe
class which is unsafe to use as you might guess.
您可以使用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);
}
prints
印刷
#
# 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
#
I have used this to test when a change has been made to a file. I made the change and crashed the JVM to ensure something else wasn't flushing or something later. Then I check I saw the update I expected.
我用它来测试何时对文件进行了更改。我进行了更改并使 JVM 崩溃,以确保其他内容不会刷新或稍后发生。然后我检查我看到了我期望的更新。
回答by Jon Skeet
I don't think politely requesting that the JVM terminates really counts as "crashing" it. You'd have to ask the interviewew exactlywhat they meant by "crash". For example:
我不认为礼貌地要求 JVM 终止真的算作“崩溃”了。你必须问面试官他们所说的“崩溃”到底是什么意思。例如:
- Would forcing a stack overflow or an out-of-memory condition count?
- Can you use JNI or any other sort of native code?
- Tell me what exactVM version you're running, and I'll try to find a JIT compiler bug...
- 是否会强制堆栈溢出或内存不足情况计数?
- 您可以使用 JNI 或任何其他类型的本机代码吗?
- 告诉我您正在运行的确切VM 版本,我将尝试查找 JIT 编译器错误...
(There have certainly been JIT compiler problems in the past - ages ago I had a pure Java program which would consistently segfault, but could be "fixed" by making what looked like a no-op change.)
(过去肯定存在 JIT 编译器问题——很久以前,我有一个纯 Java 程序,它会一直出现段错误,但可以通过进行看似无操作的更改来“修复”。)
回答by Hot Licks
Simple: Either blow the stack with infinite recursion or run the JVM out of object heap.
简单:要么用无限递归炸毁堆栈,要么用对象堆运行JVM。
More complicated would be to run the JVM out of internal heap -- you could probably do this with some sort of class loading loop, but it would take work.
更复杂的是从内部堆中运行 JVM——您可能可以使用某种类加载循环来完成此操作,但这需要工作。
Otherwise you'd have to exploit some sort of bug, or at least drop into JNI.
否则,您将不得不利用某种错误,或者至少使用 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
#
Ta da!!
哒哒!!
(And you can restrict access to Unsafe, but not any of the above.)
(您可以限制对 Unsafe 的访问,但不能限制上述任何一项。)
回答by Aaron Esau
To crash the entire computer, I would do:
要使整个计算机崩溃,我会这样做:
public static void crashComputer() {
while(true) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while(true) {
crashComputer();
}
}
});
thread.start();
}
}
public static void crashJVM() {
while(true)
crashJVM();
}
The crashComputer function takes about 2 seconds to crash the entire computer. You can stop it from crashing by holding power button.
crashComputer 函数大约需要 2 秒才能使整个计算机崩溃。您可以通过按住电源按钮阻止它崩溃。
The crashJVM function crashed only the JVM by overloading the stack, causing a stack overflow (That's where the name of this website comes from).
crashJVM 函数通过重载堆栈使JVM 崩溃,导致堆栈溢出(这就是本网站名称的由来)。
WARNING:Use at your own risk. This will not damage your computer, but I'm not taking the blame if you forget to click save on a document or something.
警告:使用风险自负。这不会损坏您的计算机,但如果您忘记单击保存文档或其他内容,我不会承担任何责任。
回答by Balakrishna Konduri
Crashing the application can be done using the OutOfMemoryError also.
也可以使用 OutOfMemoryError 使应用程序崩溃。
public class StringOutOfMemory {
public static void main(String[] args) {
int i = 0;
StringBuilder s = new StringBuilder("a");
while (true) {
s.append("ahgsdgjsdffsdfhsdgfsdfsdsfhdsgksdgkfgsdkfkdghfksdkfsdkfdkjfskdfkjsdfkdksdkdfjksjkhsdfjkfsdkjfsdhjkfsdjkfhdjkfkjsdfkjdsjkfd");
System.out.println(i++);
}
}
}
Output:
输出:
java.lang.OutOfMemoryError}}}