Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write Java code to crash the java virtual machine

Tags:

java

jvm

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?

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.

like image 483
java_mouse Avatar asked Oct 14 '11 17:10

java_mouse


People also ask

How do I crash JVM?

Run your JVM with -noverify or -Xverify:none , which disables all bytecode verification, and write something that would otherwise not be allowed to run.

How do you make a Java application crash?

To crash a JVM, aside from JNI, you need to find a bug in the VM itself. An infinite loop just consumes CPU. Infinitely allocating memory should just cause OutOfMemoryError's in a well built JVM. This would probably cause problems for other threads, but a good JVM still should not crash.

Where should code be executed before JVM crash?

All we have to do is simply write a class that extends the java. lang. Thread class, and provide the logic that we want to perform when the VM is shutting down, inside the public void run() method.

What is Java Virtual Machine code?

A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes what is required in a JVM implementation.


1 Answers

You can use the Unsafe class which is unsafe to use as you might guess.

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.

like image 93
Peter Lawrey Avatar answered Oct 06 '22 11:10

Peter Lawrey