Search code examples
androidandroid-ndkdalvik

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


Solution

  • 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.