use of org.jf.dexlib2.Opcode 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.Opcode in project smali by JesusFreke.
the class SmaliUsageTypeProvider method findClassUsageType.
@Nullable
private UsageType findClassUsageType(@NotNull PsiElement element) {
PsiElement originalElement = element;
while (element != null) {
if (element instanceof SmaliFieldReference) {
PsiElement prev = originalElement.getPrevSibling();
while (prev != null) {
// the declaring class
if (prev.getNode().getElementType() == SmaliTokens.COLON) {
return FIELD_TYPE_REFERENCE;
}
prev = prev.getPrevSibling();
}
return FIELD_DECLARING_TYPE_REFERENCE;
} else if (element instanceof SmaliMethodReferenceParamList) {
return METHOD_PARAM_REFERENCE;
} else if (element instanceof SmaliMethodReference) {
PsiElement prev = originalElement.getPrevSibling();
while (prev != null) {
IElementType elementType = prev.getNode().getElementType();
// "if" for SmaliMethodReferenceParamList
if (elementType == SmaliTokens.CLOSE_PAREN) {
return METHOD_RETURN_TYPE_REFERENCE;
}
prev = prev.getPrevSibling();
}
return METHOD_DECLARING_TYPE_REFERENCE;
} else if (element instanceof SmaliInstruction) {
Opcode opcode = ((SmaliInstruction) element).getOpcode();
if (opcode == Opcode.INSTANCE_OF) {
return UsageType.CLASS_INSTANCE_OF;
} else if (opcode == Opcode.CHECK_CAST) {
return UsageType.CLASS_CAST_TO;
} else if (newArrayInstructions.contains(opcode)) {
return UsageType.CLASS_NEW_ARRAY;
} else if (opcode == Opcode.NEW_INSTANCE) {
return UsageType.CLASS_NEW_OPERATOR;
} else if (opcode == Opcode.CONST_CLASS) {
return UsageType.CLASS_CLASS_OBJECT_ACCESS;
} else if (opcode == Opcode.THROW_VERIFICATION_ERROR) {
return VERIFICATION_ERROR;
}
} else if (element instanceof SmaliSuperStatement || element instanceof SmaliImplementsStatement) {
return UsageType.CLASS_EXTENDS_IMPLEMENTS_LIST;
} else if (element instanceof SmaliClassStatement) {
return CLASS_DECLARATION;
} else if (element instanceof SmaliMethodParamList) {
return UsageType.CLASS_METHOD_PARAMETER_DECLARATION;
} else if (element instanceof SmaliMethodPrototype) {
return UsageType.CLASS_METHOD_RETURN_TYPE;
} else if (element instanceof SmaliField) {
return UsageType.CLASS_FIELD_DECLARATION;
} else if (element instanceof SmaliCatchStatement) {
return UsageType.CLASS_CATCH_CLAUSE_PARAMETER_DECLARATION;
} else if (element instanceof SmaliLocalDebugStatement) {
return UsageType.CLASS_LOCAL_VAR_DECLARATION;
} else if (element instanceof SmaliAnnotation) {
return UsageType.ANNOTATION;
} else if (element instanceof SmaliLiteral) {
return LITERAL;
}
element = element.getParent();
}
return UsageType.UNCLASSIFIED;
}
use of org.jf.dexlib2.Opcode 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());
}
Aggregations