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
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.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With