Today I've got a stacktrace with a very strange error. Actually, I may be the first person ever to get this (YAY!), since before publishing this question the only occurences of "Bad sparse switch magic" in Google are in the Android source code.
Here's the part of stacktrace (Android 2.3.4):
java.lang.InternalError: bad sparse switch magic
at org.my.app.MyItemAdapter.(MyItemAdapter.java:64)
at org.my.app.MyActivity.onCreate(MyActivity.java:78)
The error was thrown when exiting from MyItemAdapter constructor. Since it's internal I'm pretty sure it's not directly my fault - but I just would like to know, what bad happened inside Dalvik VM.
This error seems to be connected with the switch instruction, just to clarify - I didn't directly used it in the MyItemAdapter's constructor. In order to understand what gone wrong I would probably have to carefully study a lot of dalvik-related code, so I'm asking you - maybe there's somebody who can explain me - what went wrong? I'm just curious.
EDIT
Here's the piece of Android code which throws that error: http://androidxref.com/source/xref/dalvik/vm/interp/Interp.cpp#1070
There is a sparse-switch dex byte code that is pointing the Android interpreter at an area of memory that is not actually a sparse-switch statement.
Dex byte code can represent two types of switch statements: packed or sparse. A packed switch statement only has to store the lowest value to switch on. Each subsequent switch value is incremented by one from the previous value, so that the case statements only store a branch target in the byte code. The sparse switch format has an entry for one value and branch target per case statement. See the "sparse-switch-payload" section in the "Bytecode for the Dalvik VM" document (http://source.android.com/tech/dalvik/dalvik-bytecode.html).
Sparse switch statements in dex are indicated by a noop byte code instruction with a second byte of 0x02 (http://androidxref.com/source/xref/dalvik/libdex/DexOpcodes.h#53). The first byte of a noop instruction is always 0x00, so that the full "magic signature" a sparse switch statement is 0x0200.
The dex byte code instruction to actually execute a sparse switch statement is called sparse-switch. It's code is 0x2c, and it also takes a register to test against the switch statement and an address of the switch table. I believe that the address of the switch table in your dex file is incorrect. Without more information, it would be hard to say why.
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