Hard-Crash the JVM by accessing memory at location 0x00000000 (or as in 64-Bit: 0x0000000000000000), also called the null-address. - Java Reflection

Java examples for Reflection:Object

Description

Hard-Crash the JVM by accessing memory at location 0x00000000 (or as in 64-Bit: 0x0000000000000000), also called the null-address.

Demo Code


//package com.java2s;

import java.lang.reflect.Field;

import sun.misc.Unsafe;

public class Main {
    public static void main(String[] argv) throws Exception {
        hardCrash();//from  w w  w .  jav a  2 s.  c o m
    }

    /**
     * Hard-Crash the JVM by accessing memory at location 0x00000000 (or as in 64-Bit: 0x0000000000000000), also called the null-address.
     **/
    public static final void hardCrash() {
        Unsafe theUnsafe = (Unsafe) getFieldContent(Unsafe.class, null,
                "theUnsafe");
        theUnsafe.getByte(0);
    }

    public static final Object getFieldContent(Class<?> objectClass,
            Object object, String fieldName) {
        System.out.println("Reflection Access: " + objectClass + " @--> "
                + fieldName);

        try {
            Field field = objectClass.getDeclaredField(fieldName);

            field.setAccessible(true);

            try {
                return field.get(object);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                return null;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                return null;
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            return null;
        } catch (SecurityException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related Tutorials