Search in sources :

Example 1 with DualReferenceInstruction

use of org.jf.dexlib2.iface.instruction.DualReferenceInstruction in project smali by JesusFreke.

the class InstructionMethodItem method writeTo.

@Override
public boolean writeTo(BaksmaliWriter writer) throws IOException {
    Opcode opcode = instruction.getOpcode();
    String verificationErrorName = null;
    Writable referenceWritable = null;
    Writable referenceWritable2 = 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.writeSignedIntAsDec(verificationError);
            writer.write("\n");
            verificationErrorName = "generic-error";
        }
    }
    if (instruction instanceof ReferenceInstruction) {
        ReferenceInstruction referenceInstruction = (ReferenceInstruction) instruction;
        Reference reference = referenceInstruction.getReference();
        try {
            reference.validateReference();
            referenceWritable = () -> writer.writeReference(reference);
        } catch (Reference.InvalidReferenceException ex) {
            commentOutInstruction = true;
            writer.write("#");
            writer.write(ex.getMessage());
            writer.write("\n");
            referenceWritable = () -> writer.write(ex.getInvalidReferenceRepresentation());
        }
        if (instruction instanceof DualReferenceInstruction) {
            DualReferenceInstruction dualReferenceInstruction = (DualReferenceInstruction) instruction;
            try {
                Reference reference2 = dualReferenceInstruction.getReference2();
                reference2.validateReference();
                referenceWritable2 = () -> writer.writeReference(reference2);
            } catch (Reference.InvalidReferenceException ex) {
                commentOutInstruction = true;
                writer.write("#");
                writer.write(ex.getMessage());
                writer.write("\n");
                referenceWritable = () -> writer.write(ex.getInvalidReferenceRepresentation());
            }
        }
    }
    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 (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:
        case Format20t:
        case Format30t:
            writeOpcode(writer);
            writer.write(' ');
            writeTargetLabel(writer);
            break;
        case Format10x:
            if (instruction instanceof UnknownInstruction) {
                writer.write("#unknown opcode: 0x");
                writer.writeUnsignedLongAsHex(((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(", ");
            assert referenceWritable != null;
            referenceWritable.write();
            break;
        case Format21c:
        case Format31c:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            referenceWritable.write();
            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(", ");
            assert referenceWritable != null;
            referenceWritable.write();
            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 Format35c:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRegisters(writer);
            writer.write(", ");
            assert referenceWritable != null;
            referenceWritable.write();
            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(", ");
            assert referenceWritable != null;
            referenceWritable.write();
            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;
        case Format45cc:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRegisters(writer);
            writer.write(", ");
            assert referenceWritable != null;
            referenceWritable.write();
            writer.write(", ");
            assert referenceWritable2 != null;
            referenceWritable2.write();
            break;
        case Format4rcc:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRangeRegisters(writer);
            writer.write(", ");
            assert referenceWritable != null;
            referenceWritable.write();
            writer.write(", ");
            assert referenceWritable2 != null;
            referenceWritable2.write();
            break;
        default:
            assert false;
            return false;
    }
    if (commentOutInstruction) {
        writer.write("\nnop");
    }
    return true;
}
Also used : Reference(org.jf.dexlib2.iface.reference.Reference) InvalidSwitchPayload(org.jf.baksmali.Adaptors.MethodDefinition.InvalidSwitchPayload) Opcode(org.jf.dexlib2.Opcode) UnknownInstruction(org.jf.dexlib2.iface.instruction.formats.UnknownInstruction) Instruction20bc(org.jf.dexlib2.iface.instruction.formats.Instruction20bc) ExceptionWithContext(org.jf.util.ExceptionWithContext) Instruction31t(org.jf.dexlib2.iface.instruction.formats.Instruction31t)

Example 2 with DualReferenceInstruction

use of org.jf.dexlib2.iface.instruction.DualReferenceInstruction in project android-bundle-support by lizhangqu.

the class DexReferences method gatherBackReferences.

/**
 * Goes through all the classes, methods, and fields, and gathers all possible references from
 * one type/method/field to another.
 *
 * @param files dex file
 */
private void gatherBackReferences(@NonNull DexBackedDexFile[] files) {
    Map<Reference, ImmutableReference> immutableReferencesBin = new HashMap<>();
    for (DexBackedDexFile file : files) {
        // build a map from class names (String) to actual TypeReferences,
        // as this information is not readily available to query through
        // the dexlib2 API.
        Map<String, ImmutableTypeReference> typesByName = new HashMap<>();
        for (int i = 0, m = file.getTypeCount(); i < m; i++) {
            ImmutableTypeReference immutableTypeRef = ImmutableTypeReference.of(new DexBackedTypeReference(file, i));
            typesByName.put(immutableTypeRef.getType(), immutableTypeRef);
        }
        // loop through all methods referenced in the dex file, mapping the following:
        for (int i = 0, m = file.getMethodCount(); i < m; i++) {
            MethodReference methodReference = new DexBackedMethodReference(file, i);
            // - return type => method
            ImmutableReference typeRef = typesByName.get(methodReference.getReturnType());
            addReference(typeRef, methodReference, immutableReferencesBin);
            // - all parameter types => method
            for (CharSequence parameterType : methodReference.getParameterTypes()) {
                typeRef = typesByName.get(parameterType.toString());
                addReference(typeRef, methodReference, immutableReferencesBin);
            }
        }
        // loop through all classes defined in the dex file, mapping the following:
        for (DexBackedClassDef classDef : file.getClasses()) {
            // - superclass => class
            ImmutableReference typeRef = typesByName.get(classDef.getSuperclass());
            addReference(typeRef, classDef, immutableReferencesBin);
            // - all implemented interfaces => class
            for (String iface : classDef.getInterfaces()) {
                typeRef = typesByName.get(iface);
                addReference(typeRef, classDef, immutableReferencesBin);
            }
            // map annotations => class
            for (Annotation annotation : classDef.getAnnotations()) {
                addAnnotation(immutableReferencesBin, typesByName, classDef, annotation);
            }
            // loop through all the methods defined in this class
            for (DexBackedMethod method : classDef.getMethods()) {
                // if the method has an implementation, loop through the bytecode
                // mapping any references that exist in dex instructions to the method.
                // Fortunately, dexlib2 marks every bytecode instruction that accepts
                // a reference with one or two interfaces: ReferenceInstruction
                // and DualReferenceInstruction.
                DexBackedMethodImplementation impl = method.getImplementation();
                if (impl != null) {
                    for (Instruction instruction : impl.getInstructions()) {
                        if (instruction instanceof ReferenceInstruction) {
                            Reference reference = ((ReferenceInstruction) instruction).getReference();
                            addReferenceAndEnclosingClass(immutableReferencesBin, typesByName, method, reference);
                        }
                        if (instruction instanceof DualReferenceInstruction) {
                            Reference reference = ((DualReferenceInstruction) instruction).getReference2();
                            addReferenceAndEnclosingClass(immutableReferencesBin, typesByName, method, reference);
                        }
                    }
                }
                // map annotations => method
                for (Annotation annotation : method.getAnnotations()) {
                    addAnnotation(immutableReferencesBin, typesByName, method, annotation);
                }
            }
            for (DexBackedField field : classDef.getFields()) {
                // map annotations => field
                for (Annotation annotation : field.getAnnotations()) {
                    addAnnotation(immutableReferencesBin, typesByName, field, annotation);
                }
            }
        }
        // a mapping from the field type => field
        for (int i = 0, m = file.getFieldCount(); i < m; i++) {
            FieldReference fieldRef = new DexBackedFieldReference(file, i);
            ImmutableReference typeRef = typesByName.get(fieldRef.getType());
            addReference(typeRef, fieldRef, immutableReferencesBin);
        }
    }
}
Also used : DexBackedTypeReference(org.jf.dexlib2.dexbacked.reference.DexBackedTypeReference) DexBackedFieldReference(org.jf.dexlib2.dexbacked.reference.DexBackedFieldReference) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) DexBackedFieldReference(org.jf.dexlib2.dexbacked.reference.DexBackedFieldReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) DexBackedMethodReference(org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference) DexBackedTypeReference(org.jf.dexlib2.dexbacked.reference.DexBackedTypeReference) TypeReference(org.jf.dexlib2.iface.reference.TypeReference) Reference(org.jf.dexlib2.iface.reference.Reference) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) DexBackedFieldReference(org.jf.dexlib2.dexbacked.reference.DexBackedFieldReference) DualReferenceInstruction(org.jf.dexlib2.iface.instruction.DualReferenceInstruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) Instruction(org.jf.dexlib2.iface.instruction.Instruction) DualReferenceInstruction(org.jf.dexlib2.iface.instruction.DualReferenceInstruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) DualReferenceInstruction(org.jf.dexlib2.iface.instruction.DualReferenceInstruction) Annotation(org.jf.dexlib2.iface.Annotation) DexBackedMethodReference(org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) DexBackedMethodReference(org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference)

Example 3 with DualReferenceInstruction

use of org.jf.dexlib2.iface.instruction.DualReferenceInstruction in project smali by JesusFreke.

the class InstructionMethodItem method writeTo.

@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    Opcode opcode = instruction.getOpcode();
    String verificationErrorName = null;
    String referenceString = null;
    String referenceString2 = 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;
        String classContext = null;
        if (methodDef.classDef.options.implicitReferences) {
            classContext = methodDef.method.getDefiningClass();
        }
        try {
            Reference reference = referenceInstruction.getReference();
            referenceString = ReferenceUtil.getReferenceString(reference, classContext);
            assert referenceString != null;
        } catch (InvalidItemIndex ex) {
            commentOutInstruction = true;
            referenceString = writeInvalidItemIndex(ex, referenceInstruction.getReferenceType(), writer);
        } catch (ReferenceType.InvalidReferenceTypeException ex) {
            writer.write("#invalid reference type: ");
            writer.printSignedIntAsDec(ex.getReferenceType());
            commentOutInstruction = true;
            referenceString = "invalid_reference";
        }
        if (instruction instanceof DualReferenceInstruction) {
            DualReferenceInstruction dualReferenceInstruction = (DualReferenceInstruction) instruction;
            try {
                Reference reference2 = dualReferenceInstruction.getReference2();
                referenceString2 = ReferenceUtil.getReferenceString(reference2, classContext);
            } catch (InvalidItemIndex ex) {
                commentOutInstruction = true;
                referenceString2 = writeInvalidItemIndex(ex, dualReferenceInstruction.getReferenceType2(), writer);
            } catch (ReferenceType.InvalidReferenceTypeException ex) {
                writer.write("#invalid reference type: ");
                writer.printSignedIntAsDec(ex.getReferenceType());
                commentOutInstruction = true;
                referenceString2 = "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 (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 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;
        case Format45cc:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRegisters(writer);
            writer.write(", ");
            writer.write(referenceString);
            writer.write(", ");
            writer.write(referenceString2);
            break;
        case Format4rcc:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRangeRegisters(writer);
            writer.write(", ");
            writer.write(referenceString);
            writer.write(", ");
            writer.write(referenceString2);
            break;
        default:
            assert false;
            return false;
    }
    if (commentOutInstruction) {
        writer.write("\nnop");
    }
    return true;
}
Also used : InvalidItemIndex(org.jf.dexlib2.dexbacked.DexBackedDexFile.InvalidItemIndex) Reference(org.jf.dexlib2.iface.reference.Reference) InvalidSwitchPayload(org.jf.baksmali.Adaptors.MethodDefinition.InvalidSwitchPayload) Opcode(org.jf.dexlib2.Opcode) 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) Instruction31t(org.jf.dexlib2.iface.instruction.formats.Instruction31t)

Example 4 with DualReferenceInstruction

use of org.jf.dexlib2.iface.instruction.DualReferenceInstruction 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;
    String referenceString2 = 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;
        String classContext = null;
        if (methodDef.classDef.options.implicitReferences) {
            classContext = methodDef.method.getDefiningClass();
        }
        try {
            Reference reference = referenceInstruction.getReference();
            referenceString = org.jf.dexlib2.util.ReferenceUtil.getReferenceString(reference, classContext);
            assert referenceString != null;
        } catch (InvalidItemIndex ex) {
            commentOutInstruction = true;
            referenceString = writeInvalidItemIndex(ex, referenceInstruction.getReferenceType(), writer);
        } catch (ReferenceType.InvalidReferenceTypeException ex) {
            writer.write("#invalid reference type: ");
            writer.printSignedIntAsDec(ex.getReferenceType());
            commentOutInstruction = true;
            referenceString = "invalid_reference";
        }
        if (instruction instanceof DualReferenceInstruction) {
            DualReferenceInstruction dualReferenceInstruction = (DualReferenceInstruction) instruction;
            try {
                Reference reference2 = dualReferenceInstruction.getReference2();
                referenceString2 = org.jf.dexlib2.util.ReferenceUtil.getReferenceString(reference2, classContext);
            } catch (InvalidItemIndex ex) {
                commentOutInstruction = true;
                referenceString2 = writeInvalidItemIndex(ex, dualReferenceInstruction.getReferenceType2(), writer);
            } catch (ReferenceType.InvalidReferenceTypeException ex) {
                writer.write("#invalid reference type: ");
                writer.printSignedIntAsDec(ex.getReferenceType());
                commentOutInstruction = true;
                referenceString2 = "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 (org.jf.baksmali.Adaptors.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 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;
        case Format45cc:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRegisters(writer);
            writer.write(", ");
            writer.write(referenceString);
            writer.write(", ");
            writer.write(referenceString2);
            break;
        case Format4rcc:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRangeRegisters(writer);
            writer.write(", ");
            writer.write(referenceString);
            writer.write(", ");
            writer.write(referenceString2);
            break;
        default:
            assert false;
            return false;
    }
    if (commentOutInstruction) {
        writer.write("\nnop");
    }
    return true;
}
Also used : InvalidItemIndex(org.jf.dexlib2.dexbacked.DexBackedDexFile.InvalidItemIndex) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) TypeReference(org.jf.dexlib2.iface.reference.TypeReference) Reference(org.jf.dexlib2.iface.reference.Reference) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) Opcode(org.jf.dexlib2.Opcode) 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) Instruction31t(org.jf.dexlib2.iface.instruction.formats.Instruction31t)

Aggregations

Reference (org.jf.dexlib2.iface.reference.Reference)4 Opcode (org.jf.dexlib2.Opcode)3 Instruction20bc (org.jf.dexlib2.iface.instruction.formats.Instruction20bc)3 Instruction31t (org.jf.dexlib2.iface.instruction.formats.Instruction31t)3 UnknownInstruction (org.jf.dexlib2.iface.instruction.formats.UnknownInstruction)3 ExceptionWithContext (org.jf.util.ExceptionWithContext)3 InvalidSwitchPayload (org.jf.baksmali.Adaptors.MethodDefinition.InvalidSwitchPayload)2 ReferenceType (org.jf.dexlib2.ReferenceType)2 InvalidItemIndex (org.jf.dexlib2.dexbacked.DexBackedDexFile.InvalidItemIndex)2 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)2 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)2 TypeReference (org.jf.dexlib2.iface.reference.TypeReference)2 MethodDefinition (com.taobao.android.baksmali.adaptors.MethodDefinition)1 DexBackedFieldReference (org.jf.dexlib2.dexbacked.reference.DexBackedFieldReference)1 DexBackedMethodReference (org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference)1 DexBackedTypeReference (org.jf.dexlib2.dexbacked.reference.DexBackedTypeReference)1 Annotation (org.jf.dexlib2.iface.Annotation)1 DualReferenceInstruction (org.jf.dexlib2.iface.instruction.DualReferenceInstruction)1 Instruction (org.jf.dexlib2.iface.instruction.Instruction)1 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)1