Add support for the payload instructions while calculating offsets

This commit is contained in:
Ben Gruver 2015-01-21 09:31:09 -08:00
parent a7f77ff8d0
commit 0a2debe4e6
4 changed files with 26 additions and 4 deletions

View File

@ -57,4 +57,8 @@ public class SmalideaArrayPayload extends SmalideaInstruction implements ArrayPa
}
});
}
@Override public int getCodeUnits() {
return psiInstruction.getInstructionSize()/2;
}
}

View File

@ -95,6 +95,6 @@ public class SmalideaPackedSwitchPayload extends SmalideaInstruction implements
}
@Override public int getCodeUnits() {
return 4 + psiInstruction.getPackedSwitchElements().size() * 2;
return psiInstruction.getInstructionSize()/2;
}
}

View File

@ -87,6 +87,6 @@ public class SmalideaSparseSwitchPayload extends SmalideaInstruction implements
}
@Override public int getCodeUnits() {
return 2 + psiInstruction.getSparseSwitchElements().size() * 4;
return psiInstruction.getInstructionSize()/2;
}
}

View File

@ -35,6 +35,7 @@ import com.google.common.base.Preconditions;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jf.dexlib2.Format;
import org.jf.dexlib2.Opcode;
import org.jf.dexlib2.Opcodes;
import org.jf.dexlib2.analysis.AnalyzedInstruction;
@ -99,8 +100,7 @@ public class SmaliInstruction extends SmaliCompositeElement {
if (previousInstruction == null) {
offset = 0;
} else {
// TODO: handle variable size instructions
offset = previousInstruction.getOffset() + previousInstruction.getOpcode().format.size;
offset = previousInstruction.getOffset() + previousInstruction.getInstructionSize();
}
}
return offset;
@ -172,6 +172,24 @@ public class SmaliInstruction extends SmaliCompositeElement {
return Arrays.asList(findChildrenByClass(SmaliArrayDataElement.class));
}
public int getInstructionSize() {
Opcode opcode = getOpcode();
if (!opcode.format.isPayloadFormat) {
return opcode.format.size;
} else if (opcode.format == Format.ArrayPayload) {
int elementWidth = (int)getArrayDataWidth().getIntegralValue();
int elementCount = getArrayDataElements().size();
return 8 + (elementWidth * elementCount + 1);
} else if (opcode.format == Format.PackedSwitchPayload) {
return 8 + getPackedSwitchElements().size() * 4;
} else if (opcode.format == Format.SparseSwitchPayload) {
return 2 + getSparseSwitchElements().size() * 4;
}
assert false;
throw new RuntimeException();
}
private AnalyzedInstruction analyzedInstruction = null;
@NotNull