Search in sources :

Example 6 with FieldReference

use of org.jf.dexlib2.iface.reference.FieldReference in project atlas by alibaba.

the class InstructionMethodItem method writeTo.

@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    Opcode opcode = instruction.getOpcode();
    String verificationErrorName = null;
    String referenceString = null;
    boolean commentOutInstruction = false;
    if (instruction instanceof Instruction20bc) {
        int verificationError = ((Instruction20bc) instruction).getVerificationError();
        verificationErrorName = VerificationError.getVerificationErrorName(verificationError);
        if (verificationErrorName == null) {
            writer.write("#was invalid verification error type: ");
            writer.printSignedIntAsDec(verificationError);
            writer.write("\n");
            verificationErrorName = "generic-error";
        }
    }
    if (instruction instanceof ReferenceInstruction) {
        ReferenceInstruction referenceInstruction = (ReferenceInstruction) instruction;
        try {
            Reference reference = referenceInstruction.getReference();
            String classContext = null;
            if (methodDef.classDef.options.useImplicitReferences) {
                classContext = methodDef.method.getDefiningClass();
            }
            referenceString = ReferenceUtil.getReferenceString(reference, classContext);
            if (methodDef.method.getName().equals("<clinit>")) {
                String clazz = methodDef.method.getDefiningClass();
                if (DexDiffInfo.getModifiedClasses(clazz) != null) {
                    referenceString = referenceString.replace(clazz, TypeGenUtil.newType(clazz));
                }
            }
            if (!methodDef.classDef.fullMethod) {
                if (reference instanceof MethodReference) {
                    MethodReference methodReference = (MethodReference) reference;
                    //                        String className = methodReference.getDefiningClass();
                    //                        String out1 = TypeGenUtil.getOuterClass(className);
                    //                        String out2 = TypeGenUtil.getOuterClass(ApkPatch.currentClassType);
                    //                        if (out1.equalsIgnoreCase(out2)) {
                    DexDiffInfo.addUsedMethods(methodReference);
                //                        }
                } else if (reference instanceof TypeReference) {
                    TypeReference typeReference = (TypeReference) reference;
                    DexDiffInfo.addUsedClass(typeReference.getType());
                } else if (reference instanceof FieldReference) {
                    FieldReference fieldReference = (FieldReference) reference;
                    DexDiffInfo.addUsedClass(fieldReference.getDefiningClass());
                }
            }
            assert referenceString != null;
        } catch (InvalidItemIndex ex) {
            writer.write("#");
            writer.write(ex.getMessage());
            writer.write("\n");
            commentOutInstruction = true;
            referenceString = String.format("%s@%d", ReferenceType.toString(referenceInstruction.getReferenceType()), ex.getInvalidIndex());
        } catch (ReferenceType.InvalidReferenceTypeException ex) {
            writer.write("#invalid reference type: ");
            writer.printSignedIntAsDec(ex.getReferenceType());
            commentOutInstruction = true;
            referenceString = "invalid_reference";
        }
    }
    if (instruction instanceof Instruction31t) {
        boolean validPayload = true;
        switch(instruction.getOpcode()) {
            case PACKED_SWITCH:
                int baseAddress = methodDef.getPackedSwitchBaseAddress(this.codeAddress + ((Instruction31t) instruction).getCodeOffset());
                if (baseAddress == -1) {
                    validPayload = false;
                }
                break;
            case SPARSE_SWITCH:
                baseAddress = methodDef.getSparseSwitchBaseAddress(this.codeAddress + ((Instruction31t) instruction).getCodeOffset());
                if (baseAddress == -1) {
                    validPayload = false;
                }
                break;
            case FILL_ARRAY_DATA:
                try {
                    methodDef.findPayloadOffset(this.codeAddress + ((Instruction31t) instruction).getCodeOffset(), Opcode.ARRAY_PAYLOAD);
                } catch (MethodDefinition.InvalidSwitchPayload ex) {
                    validPayload = false;
                }
                break;
            default:
                throw new ExceptionWithContext("Invalid 31t opcode: %s", instruction.getOpcode());
        }
        if (!validPayload) {
            writer.write("#invalid payload reference\n");
            commentOutInstruction = true;
        }
    }
    if (opcode.odexOnly()) {
        if (!isAllowedOdex(opcode)) {
            writer.write("#disallowed odex opcode\n");
            commentOutInstruction = true;
        }
    }
    if (commentOutInstruction) {
        writer.write("#");
    }
    switch(instruction.getOpcode().format) {
        case Format10t:
            writeOpcode(writer);
            writer.write(' ');
            writeTargetLabel(writer);
            break;
        case Format10x:
            if (instruction instanceof UnknownInstruction) {
                writer.write("#unknown opcode: 0x");
                writer.printUnsignedLongAsHex(((UnknownInstruction) instruction).getOriginalOpcode());
                writer.write('\n');
            }
            writeOpcode(writer);
            break;
        case Format11n:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeLiteral(writer);
            break;
        case Format11x:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            break;
        case Format12x:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            break;
        case Format20bc:
            writeOpcode(writer);
            writer.write(' ');
            writer.write(verificationErrorName);
            writer.write(", ");
            writer.write(referenceString);
            break;
        case Format20t:
        case Format30t:
            writeOpcode(writer);
            writer.write(' ');
            writeTargetLabel(writer);
            break;
        case Format21c:
        case Format31c:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writer.write(referenceString);
            break;
        case Format21ih:
        case Format21lh:
        case Format21s:
        case Format31i:
        case Format51l:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeLiteral(writer);
            if (instruction.getOpcode().setsWideRegister()) {
                writeCommentIfLikelyDouble(writer);
            } else {
                boolean isResourceId = writeCommentIfResourceId(writer);
                if (!isResourceId)
                    writeCommentIfLikelyFloat(writer);
            }
            break;
        case Format21t:
        case Format31t:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeTargetLabel(writer);
            break;
        case Format22b:
        case Format22s:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            writer.write(", ");
            writeLiteral(writer);
            break;
        case Format22c:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            writer.write(", ");
            writer.write(referenceString);
            break;
        case Format22cs:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            writer.write(", ");
            writeFieldOffset(writer);
            break;
        case Format22t:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            writer.write(", ");
            writeTargetLabel(writer);
            break;
        case Format22x:
        case Format32x:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            break;
        case Format23x:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            writer.write(", ");
            writeThirdRegister(writer);
            break;
        case Format25x:
            writeOpcode(writer);
            writer.write(' ');
            // vC, {vD, ...}
            writeInvoke25xRegisters(writer);
            break;
        case Format35c:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRegisters(writer);
            writer.write(", ");
            writer.write(referenceString);
            break;
        case Format35mi:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRegisters(writer);
            writer.write(", ");
            writeInlineIndex(writer);
            break;
        case Format35ms:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRegisters(writer);
            writer.write(", ");
            writeVtableIndex(writer);
            break;
        case Format3rc:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRangeRegisters(writer);
            writer.write(", ");
            writer.write(referenceString);
            break;
        case Format3rmi:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRangeRegisters(writer);
            writer.write(", ");
            writeInlineIndex(writer);
            break;
        case Format3rms:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRangeRegisters(writer);
            writer.write(", ");
            writeVtableIndex(writer);
            break;
        default:
            assert false;
            return false;
    }
    if (commentOutInstruction) {
        writer.write("\nnop");
    }
    return true;
}
Also used : InvalidItemIndex(org.jf.dexlib2.dexbacked.DexBackedDexFile.InvalidItemIndex) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) TypeReference(org.jf.dexlib2.iface.reference.TypeReference) Reference(org.jf.dexlib2.iface.reference.Reference) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) Opcode(org.jf.dexlib2.Opcode) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) ReferenceType(org.jf.dexlib2.ReferenceType) UnknownInstruction(org.jf.dexlib2.iface.instruction.formats.UnknownInstruction) Instruction20bc(org.jf.dexlib2.iface.instruction.formats.Instruction20bc) ExceptionWithContext(org.jf.util.ExceptionWithContext) MethodDefinition(com.taobao.android.baksmali.adaptors.MethodDefinition) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) TypeReference(org.jf.dexlib2.iface.reference.TypeReference) Instruction31t(org.jf.dexlib2.iface.instruction.formats.Instruction31t)

Example 7 with FieldReference

use of org.jf.dexlib2.iface.reference.FieldReference in project atlas by alibaba.

the class ReferenceUtil method getReferenceString.

@Nullable
public static String getReferenceString(@Nonnull Reference reference, @Nullable String containingClass) {
    if (reference instanceof StringReference) {
        return String.format("\"%s\"", StringUtils.escapeString(((StringReference) reference).getString()));
    }
    if (reference instanceof TypeReference) {
        //TypeGenUtil.newType(((TypeReference)reference).getType());
        String clazz = ((TypeReference) reference).getType();
        return clazz;
    }
    if (reference instanceof FieldReference) {
        FieldReference fieldReference = (FieldReference) reference;
        boolean useImplicitReference = fieldReference.getDefiningClass().equals(containingClass);
        return getFieldDescriptor((FieldReference) reference, useImplicitReference);
    }
    if (reference instanceof MethodReference) {
        MethodReference methodReference = (MethodReference) reference;
        boolean useImplicitReference = methodReference.getDefiningClass().equals(containingClass);
        return getMethodDescriptor((MethodReference) reference, useImplicitReference);
    }
    return null;
}
Also used : FieldReference(org.jf.dexlib2.iface.reference.FieldReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) TypeReference(org.jf.dexlib2.iface.reference.TypeReference) StringReference(org.jf.dexlib2.iface.reference.StringReference) Nullable(javax.annotation.Nullable)

Example 8 with FieldReference

use of org.jf.dexlib2.iface.reference.FieldReference in project smali by JesusFreke.

the class MethodAnalyzer method analyzeIputIgetQuick.

private boolean analyzeIputIgetQuick(@Nonnull AnalyzedInstruction analyzedInstruction) {
    Instruction22cs instruction = (Instruction22cs) analyzedInstruction.instruction;
    int fieldOffset = instruction.getFieldOffset();
    RegisterType objectRegisterType = getAndCheckSourceRegister(analyzedInstruction, instruction.getRegisterB(), ReferenceOrUninitCategories);
    if (objectRegisterType.category == RegisterType.NULL) {
        return false;
    }
    TypeProto objectRegisterTypeProto = objectRegisterType.type;
    assert objectRegisterTypeProto != null;
    TypeProto classTypeProto = classPath.getClass(objectRegisterTypeProto.getType());
    FieldReference resolvedField = classTypeProto.getFieldByOffset(fieldOffset);
    if (resolvedField == null) {
        throw new AnalysisException("Could not resolve the field in class %s at offset %d", objectRegisterType.type.getType(), fieldOffset);
    }
    ClassDef thisClass = classPath.getClassDef(method.getDefiningClass());
    if (!TypeUtils.canAccessClass(thisClass.getType(), classPath.getClassDef(resolvedField.getDefiningClass()))) {
        // the class is not accessible. So we start looking at objectRegisterTypeProto (which may be different
        // than resolvedField.getDefiningClass()), and walk up the class hierarchy.
        ClassDef fieldClass = classPath.getClassDef(objectRegisterTypeProto.getType());
        while (!TypeUtils.canAccessClass(thisClass.getType(), fieldClass)) {
            String superclass = fieldClass.getSuperclass();
            if (superclass == null) {
                throw new ExceptionWithContext("Couldn't find accessible class while resolving field %s", ReferenceUtil.getShortFieldDescriptor(resolvedField));
            }
            fieldClass = classPath.getClassDef(superclass);
        }
        // fieldClass is now the first accessible class found. Now. we need to make sure that the field is
        // actually valid for this class
        FieldReference newResolvedField = classPath.getClass(fieldClass.getType()).getFieldByOffset(fieldOffset);
        if (newResolvedField == null) {
            throw new ExceptionWithContext("Couldn't find accessible class while resolving field %s", ReferenceUtil.getShortFieldDescriptor(resolvedField));
        }
        resolvedField = new ImmutableFieldReference(fieldClass.getType(), newResolvedField.getName(), newResolvedField.getType());
    }
    String fieldType = resolvedField.getType();
    Opcode opcode = classPath.getFieldInstructionMapper().getAndCheckDeodexedOpcode(fieldType, instruction.getOpcode());
    Instruction22c deodexedInstruction = new ImmutableInstruction22c(opcode, (byte) instruction.getRegisterA(), (byte) instruction.getRegisterB(), resolvedField);
    analyzedInstruction.setDeodexedInstruction(deodexedInstruction);
    analyzeInstruction(analyzedInstruction);
    return true;
}
Also used : FieldReference(org.jf.dexlib2.iface.reference.FieldReference) ImmutableFieldReference(org.jf.dexlib2.immutable.reference.ImmutableFieldReference) ImmutableFieldReference(org.jf.dexlib2.immutable.reference.ImmutableFieldReference) Opcode(org.jf.dexlib2.Opcode) ExceptionWithContext(org.jf.util.ExceptionWithContext)

Example 9 with FieldReference

use of org.jf.dexlib2.iface.reference.FieldReference in project smali by JesusFreke.

the class ListFieldOffsetsCommand method run.

@Override
public void run() {
    if (help || inputList == null || inputList.isEmpty()) {
        usage();
        return;
    }
    if (inputList.size() > 1) {
        System.err.println("Too many files specified");
        usage();
        return;
    }
    String input = inputList.get(0);
    loadDexFile(input);
    BaksmaliOptions options = getOptions();
    try {
        for (ClassDef classDef : dexFile.getClasses()) {
            ClassProto classProto = (ClassProto) options.classPath.getClass(classDef);
            SparseArray<FieldReference> fields = classProto.getInstanceFields();
            String className = "Class " + classDef.getType() + " : " + fields.size() + " instance fields\n";
            System.out.write(className.getBytes());
            for (int i = 0; i < fields.size(); i++) {
                String field = fields.keyAt(i) + ":" + fields.valueAt(i).getType() + " " + fields.valueAt(i).getName() + "\n";
                System.out.write(field.getBytes());
            }
            System.out.write("\n".getBytes());
        }
        System.out.close();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : ClassProto(org.jf.dexlib2.analysis.ClassProto) ClassDef(org.jf.dexlib2.iface.ClassDef) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) IOException(java.io.IOException)

Example 10 with FieldReference

use of org.jf.dexlib2.iface.reference.FieldReference in project smali by JesusFreke.

the class DexBackedClassDef method getInstanceFields.

@Nonnull
public Iterable<? extends DexBackedField> getInstanceFields(final boolean skipDuplicates) {
    if (instanceFieldCount > 0) {
        DexReader reader = dexFile.readerAt(getInstanceFieldsOffset());
        final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
        final int fieldsStartOffset = reader.getOffset();
        return new Iterable<DexBackedField>() {

            @Nonnull
            @Override
            public Iterator<DexBackedField> iterator() {
                final AnnotationsDirectory.AnnotationIterator annotationIterator = annotationsDirectory.getFieldAnnotationIterator();
                return new VariableSizeLookaheadIterator<DexBackedField>(dexFile, fieldsStartOffset) {

                    private int count;

                    @Nullable
                    private FieldReference previousField;

                    private int previousIndex;

                    @Nullable
                    @Override
                    protected DexBackedField readNextItem(@Nonnull DexReader reader) {
                        while (true) {
                            if (++count > instanceFieldCount) {
                                directMethodsOffset = reader.getOffset();
                                return endOfData();
                            }
                            DexBackedField item = new DexBackedField(reader, DexBackedClassDef.this, previousIndex, annotationIterator);
                            FieldReference currentField = previousField;
                            FieldReference nextField = ImmutableFieldReference.of(item);
                            previousField = nextField;
                            previousIndex = item.fieldIndex;
                            if (skipDuplicates && currentField != null && currentField.equals(nextField)) {
                                continue;
                            }
                            return item;
                        }
                    }
                };
            }
        };
    } else {
        if (instanceFieldsOffset > 0) {
            directMethodsOffset = instanceFieldsOffset;
        }
        return ImmutableSet.of();
    }
}
Also used : AnnotationsDirectory(org.jf.dexlib2.dexbacked.util.AnnotationsDirectory) ImmutableFieldReference(org.jf.dexlib2.immutable.reference.ImmutableFieldReference) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) Nonnull(javax.annotation.Nonnull) VariableSizeLookaheadIterator(org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator) Nonnull(javax.annotation.Nonnull)

Aggregations

FieldReference (org.jf.dexlib2.iface.reference.FieldReference)11 ImmutableFieldReference (org.jf.dexlib2.immutable.reference.ImmutableFieldReference)6 Opcode (org.jf.dexlib2.Opcode)4 Instruction (org.jf.dexlib2.iface.instruction.Instruction)4 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)4 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)4 ClassDef (org.jf.dexlib2.iface.ClassDef)3 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)3 StringReference (org.jf.dexlib2.iface.reference.StringReference)3 TypeReference (org.jf.dexlib2.iface.reference.TypeReference)3 ExceptionWithContext (org.jf.util.ExceptionWithContext)3 Nonnull (javax.annotation.Nonnull)2 AnnotationsDirectory (org.jf.dexlib2.dexbacked.util.AnnotationsDirectory)2 VariableSizeLookaheadIterator (org.jf.dexlib2.dexbacked.util.VariableSizeLookaheadIterator)2 Method (org.jf.dexlib2.iface.Method)2 Instruction20bc (org.jf.dexlib2.iface.instruction.formats.Instruction20bc)2 Instruction21c (org.jf.dexlib2.iface.instruction.formats.Instruction21c)2 Test (org.junit.Test)2 MethodDefinition (com.taobao.android.baksmali.adaptors.MethodDefinition)1 IOException (java.io.IOException)1