9

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

3
  • 5
    Write *(int*)0 = 0; in a native SO. Commented Jul 1, 2015 at 17:51
  • Can I do something from the java? Commented Jul 1, 2015 at 17:52
  • There was a question on SO to that effect. As far as I can recall, they couldn't find a good way. Exploiting known Android bugs might get you somewhere, but that's hardly device independent. Commented Jul 1, 2015 at 17:53

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This method no longer exists in modern copies of AOSP.
2

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

1 Comment

This method no longer exists in modern copies of AOSP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.