wrong label offset after adding a line in a catch clause
I’m trying to add simple log call to every catch clause in an apk.
When doing so, there are cases where the try end label is out of the files
scope. (For a very short method the try end label position is 65774 ).
I attached a small jar containing an application that inserts a simple hook in
every catch clause, and a sample apk (Boat_Browser) on which I experienced the
problem.
I also debuged ASMDEX and found the point where the label offset is changed to
the mysterious value.
The flow is simple:
1. org.ow2.asmdex.tree.ApplicationNode#accept
2. org.ow2.asmdex.tree.ClassNode#accept
3. org.ow2.asmdex.tree.MethodNode#accept(org.ow2.asmdex.ClassVisitor),
here before the line: instructions.accept(mv); everything is great, here for
every label we get to
a. org.ow2.asmdex.tree.LabelNode#accept
b. org.ow2.asmdex.MethodVisitor#visitLabel
c. org.ow2.asmdex.structureCommon.Label#setOffset , here the offset of
the last try catch start label is changed to a strange number (after the end
label & after the end of the method end which is strange because the code is
CodeItem codeItem = getCodeItem();
label.setOffset(codeItem.getSize()); ). In the apk attached you will find it in
clz.name.equals("Lcom/boatbrowser/free/bw;")&&method.name.equals("a")&&method.d
esc.equals("VLandroid/os/Bundle;)
the start label of the last try catch block was changed from 180 to 198 where
the method length is 188
.
Also attached is the source code for my small application (all is included in
the zip).
I tried debugging it and could not find the root cause for the problem.
You can run my application by running: java –jar my-app.jar <path to
Boat_Browser.apk>.
java.lang.RuntimeException: Try end offset 65769 is past the end of the code
block.
at
org.jf.baksmali.Adaptors.MethodDefinition.addTries(MethodDefinition.java:476)
at
org.jf.baksmali.Adaptors.MethodDefinition.getMethodItems(MethodDefinition.java:
316)
at org.jf.baksmali.Adaptors.MethodDefinition.
(MethodDefinition.java:132)
at
org.jf.baksmali.Adaptors.ClassDefinition.writeMethods(ClassDefinition.java:338)
at
org.jf.baksmali.Adaptors.ClassDefinition.writeDirectMethods(ClassDefinition.jav
a:294)
at
org.jf.baksmali.Adaptors.ClassDefinition.writeTo(ClassDefinition.java:116)
at org.jf.baksmali.baksmali.disassembleDexFile(baksmali.java:186)
at brut.androlib.src.SmaliDecoder.decode(SmaliDecoder.java:49)
at brut.androlib.src.SmaliDecoder.decode(SmaliDecoder.java:34)