Generic failure programmatically

Is there an easy way to crash a native crash application to test crash reports?

Note that I am looking for a generic solution for all devices, not a specific device. I was thinking about using the Unsafe class (writing illegal addresses to the stuck one) but it looks like it is not supported

+3


source to share


2 answers


If you want to cause a crash from Java code use dalvik.system.VMDebug.crash()

. This is not part of the public API, so you will need to access it through reflection. It worked for Dahlvik; I don't know if it still works in Art.

Some of the methods are sun.misc.Unsafe

supported, so you can cause crashes by selecting the appropriate value for offset

in calls such as putIntVolatile()

. If the offset is negation of the Object pointer, you can dereference the null address and fail.



The safest way is to create a trivial native library with the NDK. I personally prefer to store the value in a "named" address, for example 0xdeadd00d

, because they tell you that it was your error code, but null pointers also work.

+3


source


As @fadden pointed out, usage dalvik.system.VMDebug.crash()

is a helper method to access it via reflection.



public void crashNatively() {
    try {
        Class.forName("dalvik.system.VMDebug")
                .getMethod("crash")
                .invoke(null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

      

0


source







All Articles