Only include debug info within the instruction range of a method.

This commit is contained in:
Ian Zerny 2022-02-04 11:49:20 +01:00
parent cbd41d36cc
commit 6187d13f14
3 changed files with 12 additions and 4 deletions

View File

@ -75,7 +75,7 @@ public class CDexBackedMethodImplementation extends DexBackedMethodImplementatio
} }
@Override @Override
protected int getInstructionsSize() { public int getInstructionsSize() {
int instructionsSize = dexFile.getDataBuffer().readUshort( int instructionsSize = dexFile.getDataBuffer().readUshort(
codeOffset + CDEX_INSTRUCTIONS_SIZE_AND_PREHEADER_FLAGS_OFFSET) >> codeOffset + CDEX_INSTRUCTIONS_SIZE_AND_PREHEADER_FLAGS_OFFSET) >>
CDEX_INSTRUCTIONS_SIZE_SHIFT; CDEX_INSTRUCTIONS_SIZE_SHIFT;

View File

@ -66,7 +66,7 @@ public class DexBackedMethodImplementation implements MethodImplementation {
return dexFile.getDataBuffer().readUshort(codeOffset); return dexFile.getDataBuffer().readUshort(codeOffset);
} }
protected int getInstructionsSize() { public int getInstructionsSize() {
return dexFile.getDataBuffer().readSmallUint(codeOffset + CodeItem.INSTRUCTION_COUNT_OFFSET); return dexFile.getDataBuffer().readSmallUint(codeOffset + CodeItem.INSTRUCTION_COUNT_OFFSET);
} }

View File

@ -32,6 +32,7 @@
package org.jf.dexlib2.dexbacked.util; package org.jf.dexlib2.dexbacked.util;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import org.jf.dexlib2.AccessFlags; import org.jf.dexlib2.AccessFlags;
import org.jf.dexlib2.DebugItemType; import org.jf.dexlib2.DebugItemType;
import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.dexbacked.DexBackedDexFile;
@ -117,6 +118,10 @@ public abstract class DebugInfo implements Iterable<DebugItem> {
DexReader reader = dexFile.getDataBuffer().readerAt(debugInfoOffset); DexReader reader = dexFile.getDataBuffer().readerAt(debugInfoOffset);
final int lineNumberStart = reader.readBigUleb128(); final int lineNumberStart = reader.readBigUleb128();
int registerCount = methodImpl.getRegisterCount(); int registerCount = methodImpl.getRegisterCount();
// Debug information can have events for addresses past the instructions.
// They have no relevance for the method in question and are excluded from the iterator.
final int lastInstructionAddress = methodImpl.getInstructionsSize()
- Iterators.getLast(methodImpl.getInstructions().iterator()).getCodeUnits();
//TODO: does dalvik allow references to invalid registers? //TODO: does dalvik allow references to invalid registers?
final LocalInfo[] locals = new LocalInfo[registerCount]; final LocalInfo[] locals = new LocalInfo[registerCount];
@ -172,7 +177,7 @@ public abstract class DebugInfo implements Iterable<DebugItem> {
@Nullable @Nullable
protected DebugItem readNextItem(@Nonnull DexReader reader) { protected DebugItem readNextItem(@Nonnull DexReader reader) {
while (true) { while (codeAddress <= lastInstructionAddress) {
int next = reader.readUbyte(); int next = reader.readUbyte();
switch (next) { switch (next) {
case DebugItemType.END_SEQUENCE: { case DebugItemType.END_SEQUENCE: {
@ -270,10 +275,13 @@ public abstract class DebugInfo implements Iterable<DebugItem> {
int adjusted = next - 0x0A; int adjusted = next - 0x0A;
codeAddress += adjusted / 15; codeAddress += adjusted / 15;
lineNumber += (adjusted % 15) - 4; lineNumber += (adjusted % 15) - 4;
return new ImmutableLineNumber(codeAddress, lineNumber); return codeAddress <= lastInstructionAddress
? new ImmutableLineNumber(codeAddress, lineNumber)
: endOfData();
} }
} }
} }
return endOfData();
} }
}; };
} }