Kirill Kulakov
Kirill Kulakov

Reputation: 10255

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

Upvotes: 9

Views: 2752

Answers (2)

Nabin Bhandari
Nabin Bhandari

Reputation: 16429

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

Upvotes: 2

fadden
fadden

Reputation: 52353

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.

Upvotes: 3

Related Questions