use of org.jf.dexlib2.iface.instruction.Instruction in project smali by JesusFreke.
the class AccessorTest method testAccessors.
@Test
public void testAccessors() throws IOException {
URL url = AccessorTest.class.getClassLoader().getResource("accessorTest.dex");
Assert.assertNotNull(url);
DexFile f = DexFileFactory.loadDexFile(url.getFile(), Opcodes.getDefault());
SyntheticAccessorResolver sar = new SyntheticAccessorResolver(f.getOpcodes(), f.getClasses());
ClassDef accessorTypesClass = null;
ClassDef accessorsClass = null;
for (ClassDef classDef : f.getClasses()) {
String className = classDef.getType();
if (className.equals("Lorg/jf/dexlib2/AccessorTypes;")) {
accessorTypesClass = classDef;
} else if (className.equals("Lorg/jf/dexlib2/AccessorTypes$Accessors;")) {
accessorsClass = classDef;
}
}
Assert.assertNotNull(accessorTypesClass);
Assert.assertNotNull(accessorsClass);
for (Method method : accessorsClass.getMethods()) {
Matcher m = accessorMethodPattern.matcher(method.getName());
if (!m.matches()) {
continue;
}
String type = m.group(1);
String operation = m.group(2);
MethodImplementation methodImpl = method.getImplementation();
Assert.assertNotNull(methodImpl);
for (Instruction instruction : methodImpl.getInstructions()) {
Opcode opcode = instruction.getOpcode();
if (opcode == Opcode.INVOKE_STATIC || opcode == Opcode.INVOKE_STATIC_RANGE) {
MethodReference accessorMethod = (MethodReference) ((ReferenceInstruction) instruction).getReference();
SyntheticAccessorResolver.AccessedMember accessedMember = sar.getAccessedMember(accessorMethod);
Assert.assertNotNull(String.format("Could not resolve accessor for %s_%s", type, operation), accessedMember);
int operationType = operationTypes.get(operation);
Assert.assertEquals(operationType, accessedMember.accessedMemberType);
Assert.assertEquals(String.format("%s_val", type), ((FieldReference) accessedMember.accessedMember).getName());
}
}
}
}
use of org.jf.dexlib2.iface.instruction.Instruction in project smali by JesusFreke.
the class PayloadAlignmentTest method testPayloadAlignmentRemoveNopWithReferent.
@Test
public void testPayloadAlignmentRemoveNopWithReferent() {
MethodImplementationBuilder implBuilder = new MethodImplementationBuilder(10);
Label label = implBuilder.getLabel("array_payload");
implBuilder.addInstruction(new BuilderInstruction31t(Opcode.FILL_ARRAY_DATA, 0, label));
implBuilder.addInstruction(new BuilderInstruction12x(Opcode.MOVE, 0, 0));
implBuilder.addInstruction(new BuilderInstruction12x(Opcode.MOVE, 0, 0));
implBuilder.addInstruction(new BuilderInstruction12x(Opcode.MOVE, 0, 0));
implBuilder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
implBuilder.addLabel("array_payload");
implBuilder.addInstruction(new BuilderArrayPayload(4, null));
List<Instruction> instructions = Lists.newArrayList(implBuilder.getMethodImplementation().getInstructions());
checkInstructions(instructions, new Opcode[] { Opcode.FILL_ARRAY_DATA, Opcode.MOVE, Opcode.MOVE, Opcode.MOVE, Opcode.ARRAY_PAYLOAD });
Instruction31t referent = (Instruction31t) instructions.get(0);
Assert.assertEquals(6, referent.getCodeOffset());
}
use of org.jf.dexlib2.iface.instruction.Instruction in project smali by JesusFreke.
the class PayloadAlignmentTest method testPayloadAlignmentRemoveNop.
@Test
public void testPayloadAlignmentRemoveNop() {
MethodImplementationBuilder implBuilder = new MethodImplementationBuilder(10);
implBuilder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
implBuilder.addInstruction(new BuilderArrayPayload(4, null));
List<? extends Instruction> instructions = Lists.newArrayList(implBuilder.getMethodImplementation().getInstructions());
Assert.assertEquals(instructions.size(), 1);
Instruction instruction = instructions.get(0);
Assert.assertEquals(instruction.getOpcode(), Opcode.ARRAY_PAYLOAD);
}
use of org.jf.dexlib2.iface.instruction.Instruction in project smali by JesusFreke.
the class SmalideaMethodTest method testArrayData.
public void testArrayData() {
String text = ".class public LFormat31t;\n" + ".super Ljava/lang/Object;\n" + ".source \"Format31t.smali\"" + "\n" + ".method public test_fill-array-data()V\n" + " .registers 3\n" + " .annotation runtime Lorg/junit/Test;\n" + " .end annotation\n" + "\n" + " const v0, 6\n" + " new-array v0, v0, [I\n" + " fill-array-data v0, :ArrayData\n" + "\n" + " const v1, 0\n" + " aget v2, v0, v1\n" + " const v1, 1\n" + " invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + " const v1, 1\n" + " aget v2, v0, v1\n" + " const v1, 2\n" + " invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + " const v1, 2\n" + " aget v2, v0, v1\n" + " const v1, 3\n" + " invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + " const v1, 3\n" + " aget v2, v0, v1\n" + " const v1, 4\n" + " invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + " const v1, 4\n" + " aget v2, v0, v1\n" + " const v1, 5\n" + " invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + " const v1, 5\n" + " aget v2, v0, v1\n" + " const v1, 6\n" + " invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + " return-void\n" + "\n" + ":ArrayData\n" + " .array-data 4\n" + " 1 2 128 -256 65536 0x7fffffff\n" + " .end array-data\n" + ".end method";
SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", text);
SmaliClass smaliClass = file.getPsiClass();
SmaliMethod smaliMethod = smaliClass.getMethods()[0];
SmalideaMethod method = new SmalideaMethod(smaliMethod);
MethodImplementation impl = method.getImplementation();
Assert.assertNotNull(impl);
List<Instruction> instructions = Lists.newArrayList(impl.getInstructions());
ArrayPayload arrayPayload = (ArrayPayload) instructions.get(28);
Assert.assertEquals(4, arrayPayload.getElementWidth());
List<Number> elements = arrayPayload.getArrayElements();
Assert.assertEquals(6, elements.size());
Assert.assertEquals(1L, elements.get(0).longValue());
Assert.assertEquals(2L, elements.get(1).longValue());
Assert.assertEquals(128L, elements.get(2));
Assert.assertEquals(-256L, elements.get(3));
Assert.assertEquals(65536L, elements.get(4));
Assert.assertEquals(0x7fffffffL, elements.get(5));
}
use of org.jf.dexlib2.iface.instruction.Instruction in project smali by JesusFreke.
the class SmalideaMethodTest method testPackedSwitch.
public void testPackedSwitch() {
String text = ".class public LFormat31t;\n" + ".super Ljava/lang/Object;\n" + ".source \"Format31t.smali\"" + "\n" + ".method public test_packed-switch()V\n" + " .registers 1\n" + " .annotation runtime Lorg/junit/Test;\n" + " .end annotation\n" + "\n" + " const v0, 12\n" + "\n" + ":switch\n" + " packed-switch v0, :PackedSwitch\n" + "\n" + ":Label10\n" + " invoke-static {}, Lorg/junit/Assert;->fail()V\n" + " return-void\n" + "\n" + ":Label11\n" + " invoke-static {}, Lorg/junit/Assert;->fail()V\n" + " return-void\n" + "\n" + ":Label12\n" + " return-void\n" + "\n" + ":Label13\n" + " invoke-static {}, Lorg/junit/Assert;->fail()V\n" + " return-void\n" + "\n" + ":PackedSwitch\n" + " .packed-switch 10\n" + " :Label10\n" + " :Label11\n" + " :Label12\n" + " :Label13\n" + " .end packed-switch\n" + ".end method";
SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", text);
SmaliClass smaliClass = file.getPsiClass();
SmaliMethod smaliMethod = smaliClass.getMethods()[0];
SmalideaMethod method = new SmalideaMethod(smaliMethod);
MethodImplementation impl = method.getImplementation();
Assert.assertNotNull(impl);
List<Instruction> instructions = Lists.newArrayList(impl.getInstructions());
PackedSwitchPayload packedSwitchPayload = (PackedSwitchPayload) instructions.get(9);
List<? extends SwitchElement> switchElements = packedSwitchPayload.getSwitchElements();
Assert.assertEquals(4, switchElements.size());
checkSwitchElement(switchElements.get(0), 10, 6);
checkSwitchElement(switchElements.get(1), 11, 14);
checkSwitchElement(switchElements.get(2), 12, 22);
checkSwitchElement(switchElements.get(3), 13, 24);
}
Aggregations