欢马劈雪     最近更新时间:2020-01-02 10:19:05

627
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
通过访问位置0x00000000(或如64位:0x0000000000000000)中的内存(也称为空地址),使JVM崩溃。


//package com.nowjava;/** 时 代 J a v a - N o w J a v a . c o m 提供 **/


import java.lang.reflect.Field;


import sun.misc.Unsafe;


public class Main {

    public static void main(String[] argv) throws Exception {

        hardCrash();

    }


    /**

     * 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() {
    /**
     * N o  w  J a v a . c o m - 时  代  Java 提 供 
    **/

        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();

                
展开阅读全文