Skip to content

Instantly share code, notes, and snippets.

@jzelenkov
Created December 18, 2013 00:31
Show Gist options
  • Save jzelenkov/8015380 to your computer and use it in GitHub Desktop.
Save jzelenkov/8015380 to your computer and use it in GitHub Desktop.
cheater way to crush the JVM.
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class KillVM {
public static void main(String... args) {
Unsafe unsafe = null;
try {
Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (sun.misc.Unsafe)field.get(null);
} catch(Exception e) {
throw new AssertionError(e);
}
unsafe.getAdress(123123); // or any other random gibberish
// chances that you will read a valid java object are virtually zero
// just to be safe, let's kill the VM
unsafe.putAddress(123, 0); // trying to write into lower level memory area will crash the VM
// actually working code. just kept for reference
//
// long value = 12345;
// byte size = 1;
// long value_ptr = unsafe.allocateMemory(size);
// unsafe.putAddress(value_ptr, value);
// long readValue = unsafe.getAddress(value_ptr);
// System.out.println("stored value is: " + readValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment