Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native crash programmatically

Is there an easy way to crash an app with a native crash, in order to test native crash reporting?

note that I'm looking for a general solution for all devices, and not device specific. I thought about using the Unsafe class (writing illegal addresses into the stuck), but it looks like it's not supported

like image 612
Kirill Kulakov Avatar asked Jul 01 '15 17:07

Kirill Kulakov


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. This worked for Dalvik; I don't know if it still works for Art.

Some of the sun.misc.Unsafe methods are supported, so you may be able to cause a crash by selecting an appropriate value for offset in calls like putIntVolatile(). If the offset is the negation of the Object pointer you'll dereference address zero and crash.

The most reliable way is to create a trivial native library with the NDK. I personally favor storing a value in a "named" address, like 0xdeadd00d, because they let you know that it was your code crashing deliberately, but null pointer derefs work too.

like image 197
fadden Avatar answered Sep 29 '22 10:09

fadden


As @fadden pointed out the use of dalvik.system.VMDebug.crash(), here 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();
    }
}
like image 32
Nabin Bhandari Avatar answered Sep 29 '22 09:09

Nabin Bhandari