Search in sources :

Example 46 with Instruction

use of org.jf.dexlib2.iface.instruction.Instruction in project atlas by alibaba.

the class InstructionMethodItem method writeInvokeRegisters.

protected void writeInvokeRegisters(IndentingWriter writer) throws IOException {
    FiveRegisterInstruction instruction = (FiveRegisterInstruction) this.instruction;
    final int regCount = instruction.getRegisterCount();
    writer.write('{');
    switch(regCount) {
        case 1:
            writeRegister(writer, instruction.getRegisterC());
            break;
        case 2:
            writeRegister(writer, instruction.getRegisterC());
            writer.write(", ");
            writeRegister(writer, instruction.getRegisterD());
            break;
        case 3:
            writeRegister(writer, instruction.getRegisterC());
            writer.write(", ");
            writeRegister(writer, instruction.getRegisterD());
            writer.write(", ");
            writeRegister(writer, instruction.getRegisterE());
            break;
        case 4:
            writeRegister(writer, instruction.getRegisterC());
            writer.write(", ");
            writeRegister(writer, instruction.getRegisterD());
            writer.write(", ");
            writeRegister(writer, instruction.getRegisterE());
            writer.write(", ");
            writeRegister(writer, instruction.getRegisterF());
            break;
        case 5:
            writeRegister(writer, instruction.getRegisterC());
            writer.write(", ");
            writeRegister(writer, instruction.getRegisterD());
            writer.write(", ");
            writeRegister(writer, instruction.getRegisterE());
            writer.write(", ");
            writeRegister(writer, instruction.getRegisterF());
            writer.write(", ");
            writeRegister(writer, instruction.getRegisterG());
            break;
    }
    writer.write('}');
}
Also used : FiveRegisterInstruction(org.jf.dexlib2.iface.instruction.FiveRegisterInstruction)

Example 47 with Instruction

use of org.jf.dexlib2.iface.instruction.Instruction 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 48 with Instruction

use of org.jf.dexlib2.iface.instruction.Instruction in project atlas by alibaba.

the class MethodDefinition method findSwitchPayload.

public Instruction findSwitchPayload(int targetOffset, Opcode type) {
    int targetIndex;
    try {
        targetIndex = instructionOffsetMap.getInstructionIndexAtCodeOffset(targetOffset);
    } catch (InvalidInstructionOffset ex) {
        throw new InvalidSwitchPayload(targetOffset);
    }
    //TODO: does dalvik let you pad with multiple nops?
    //TODO: does dalvik let a switch instruction point to a non-payload instruction?
    Instruction instruction = instructions.get(targetIndex);
    if (instruction.getOpcode() != type) {
        // maybe it's pointing to a NOP padding instruction. Look at the next instruction
        if (instruction.getOpcode() == Opcode.NOP) {
            targetIndex += 1;
            if (targetIndex < instructions.size()) {
                instruction = instructions.get(targetIndex);
                if (instruction.getOpcode() == type) {
                    return instruction;
                }
            }
        }
        throw new InvalidSwitchPayload(targetOffset);
    } else {
        return instruction;
    }
}
Also used : InvalidInstructionOffset(org.jf.dexlib2.util.InstructionOffsetMap.InvalidInstructionOffset) OffsetInstruction(org.jf.dexlib2.iface.instruction.OffsetInstruction) AnalyzedInstruction(org.jf.dexlib2.analysis.AnalyzedInstruction) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction)

Example 49 with Instruction

use of org.jf.dexlib2.iface.instruction.Instruction in project atlas by alibaba.

the class MethodDefinition method addAnalyzedInstructionMethodItems.

private void addAnalyzedInstructionMethodItems(List<MethodItem> methodItems) {
    MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classDef.options.classPath, method, classDef.options.inlineResolver);
    AnalysisException analysisException = methodAnalyzer.getAnalysisException();
    if (analysisException != null) {
        // TODO: need to keep track of whether any errors occurred, so we can exit with a non-zero result
        methodItems.add(new CommentMethodItem(String.format("AnalysisException: %s", analysisException.getMessage()), analysisException.codeAddress, Integer.MIN_VALUE));
        analysisException.printStackTrace(System.err);
    }
    List<AnalyzedInstruction> instructions = methodAnalyzer.getAnalyzedInstructions();
    int currentCodeAddress = 0;
    for (int i = 0; i < instructions.size(); i++) {
        AnalyzedInstruction instruction = instructions.get(i);
        MethodItem methodItem = InstructionMethodItemFactory.makeInstructionFormatMethodItem(this, currentCodeAddress, instruction.getInstruction());
        methodItems.add(methodItem);
        if (instruction.getInstruction().getOpcode().format == Format.UnresolvedOdexInstruction) {
            methodItems.add(new CommentedOutMethodItem(InstructionMethodItemFactory.makeInstructionFormatMethodItem(this, currentCodeAddress, instruction.getOriginalInstruction())));
        }
        if (i != instructions.size() - 1) {
            methodItems.add(new BlankMethodItem(currentCodeAddress));
        }
        if (classDef.options.addCodeOffsets) {
            methodItems.add(new MethodItem(currentCodeAddress) {

                @Override
                public double getSortOrder() {
                    return -1000;
                }

                @Override
                public boolean writeTo(IndentingWriter writer) throws IOException {
                    writer.write("#@");
                    writer.printUnsignedLongAsHex(codeAddress & 0xFFFFFFFFL);
                    return true;
                }
            });
        }
        if (classDef.options.registerInfo != 0 && !instruction.getInstruction().getOpcode().format.isPayloadFormat) {
            methodItems.add(new PreInstructionRegisterInfoMethodItem(classDef.options.registerInfo, methodAnalyzer, registerFormatter, instruction, currentCodeAddress));
            methodItems.add(new PostInstructionRegisterInfoMethodItem(registerFormatter, instruction, currentCodeAddress));
        }
        currentCodeAddress += instruction.getInstruction().getCodeUnits();
    }
}
Also used : EndPrologueMethodItem(com.taobao.android.baksmali.adaptors.Debug.EndPrologueMethodItem) DebugMethodItem(com.taobao.android.baksmali.adaptors.Debug.DebugMethodItem) MethodAnalyzer(org.jf.dexlib2.analysis.MethodAnalyzer) IOException(java.io.IOException) AnalyzedInstruction(org.jf.dexlib2.analysis.AnalyzedInstruction) AnalysisException(org.jf.dexlib2.analysis.AnalysisException) IndentingWriter(org.jf.util.IndentingWriter)

Example 50 with Instruction

use of org.jf.dexlib2.iface.instruction.Instruction in project atlas by alibaba.

the class SmaliDiffUtils method diff.

public static boolean diff(DexDiffInfo diffInfo, String outterClassName, String className, File out) throws PatchException {
    String methodName = null;
    DexBackedMethod sDexBackedMethod = null;
    DexBackedClassDef oldDexBackClassDef = null;
    DexBackedClassDef newDexBackClassDef = null;
    File oldTemp = new File(out, "smaliTemp1");
    if (!oldTemp.exists()) {
        oldTemp.mkdirs();
    }
    File newTemp = new File(out, "smaliTemp2");
    if (!newTemp.exists()) {
        newTemp.mkdirs();
    }
    for (DexBackedClassDef dexBackedClassDef : diffInfo.getOldClasses()) {
        if (DexDiffer.getDalvikClassName(dexBackedClassDef.getType()).equals(outterClassName)) {
            oldDexBackClassDef = dexBackedClassDef;
        }
    }
    for (DexBackedClassDef dexBackedClassDef : diffInfo.getNewClasses()) {
        if (DexDiffer.getDalvikClassName(dexBackedClassDef.getType()).equals(outterClassName)) {
            newDexBackClassDef = dexBackedClassDef;
        }
    }
    if (oldDexBackClassDef == null) {
        return false;
    }
    disassemble(oldTemp, oldDexBackClassDef);
    disassemble(newTemp, newDexBackClassDef);
    Iterable<? extends DexBackedMethod> oldDexBackedMethods = oldDexBackClassDef.getMethods();
    Iterable<? extends DexBackedMethod> newDexBackedMethods = newDexBackClassDef.getMethods();
    boolean find = false;
    for (DexBackedMethod dexBackedMethod : newDexBackedMethods) {
        Iterator<? extends Instruction> instructions = dexBackedMethod.getImplementation().getInstructions().iterator();
        while (instructions.hasNext()) {
            Instruction instruction = instructions.next();
            if (instruction instanceof ReferenceInstruction) {
                Opcode opcode = instruction.getOpcode();
                if (opcode.referenceType == ReferenceType.METHOD) {
                    MethodReference methodReference = null;
                    try {
                        methodReference = (MethodReference) ((ReferenceInstruction) instruction).getReference();
                        if (DexDiffer.getDalvikClassName(methodReference.getDefiningClass()).equals(className)) {
                            sDexBackedMethod = dexBackedMethod;
                            find = true;
                            break;
                        }
                    } catch (DexBackedDexFile.InvalidItemIndex ex) {
                        ex.getStackTrace();
                    }
                }
            }
        }
        if (find) {
            break;
        }
    }
    for (DexBackedMethod dexBackedMethod : oldDexBackedMethods) {
        if (compare(dexBackedMethod, sDexBackedMethod)) {
            return true;
        }
    }
    return false;
}
Also used : DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) DexBackedMethod(org.jf.dexlib2.dexbacked.DexBackedMethod) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) Opcode(org.jf.dexlib2.Opcode) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) File(java.io.File)

Aggregations

Instruction (org.jf.dexlib2.iface.instruction.Instruction)35 Test (org.junit.Test)20 Opcode (org.jf.dexlib2.Opcode)16 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)16 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)15 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)13 OffsetInstruction (org.jf.dexlib2.iface.instruction.OffsetInstruction)12 AnalyzedInstruction (org.jf.dexlib2.analysis.AnalyzedInstruction)11 ExceptionWithContext (org.jf.util.ExceptionWithContext)11 TypeReference (org.jf.dexlib2.iface.reference.TypeReference)10 Nonnull (javax.annotation.Nonnull)7 ClassDef (org.jf.dexlib2.iface.ClassDef)7 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)7 IOException (java.io.IOException)6 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)6 Reference (org.jf.dexlib2.iface.reference.Reference)6 ImmutableFieldReference (org.jf.dexlib2.immutable.reference.ImmutableFieldReference)5 BuilderInstruction10t (org.jf.dexlib2.builder.instruction.BuilderInstruction10t)4 InvalidItemIndex (org.jf.dexlib2.dexbacked.DexBackedDexFile.InvalidItemIndex)4 DexFile (org.jf.dexlib2.iface.DexFile)4