Search in sources :

Example 16 with CstType

use of com.taobao.android.dx.rop.cst.CstType in project atlas by alibaba.

the class ClassReferenceListBuilder method addClassWithHierachy.

private void addClassWithHierachy(String classBinaryName) {
    if (classNames.contains(classBinaryName)) {
        return;
    }
    try {
        DirectClassFile classFile = path.getClass(classBinaryName + CLASS_EXTENSION);
        classNames.add(classBinaryName);
        CstType superClass = classFile.getSuperclass();
        if (superClass != null) {
            addClassWithHierachy(superClass.getClassType().getClassName());
        }
        TypeList interfaceList = classFile.getInterfaces();
        int interfaceNumber = interfaceList.size();
        for (int i = 0; i < interfaceNumber; i++) {
            addClassWithHierachy(interfaceList.getType(i).getClassName());
        }
    } catch (FileNotFoundException e) {
    // Ignore: The referenced type is not in the path it must be part of the libraries.
    }
}
Also used : DirectClassFile(com.taobao.android.dx.cf.direct.DirectClassFile) CstType(com.taobao.android.dx.rop.cst.CstType) FileNotFoundException(java.io.FileNotFoundException) TypeList(com.taobao.android.dx.rop.type.TypeList)

Example 17 with CstType

use of com.taobao.android.dx.rop.cst.CstType in project atlas by alibaba.

the class Form31c method isCompatible.

/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    if (!(insn instanceof CstInsn)) {
        return false;
    }
    RegisterSpecList regs = insn.getRegisters();
    RegisterSpec reg;
    switch(regs.size()) {
        case 1:
            {
                reg = regs.get(0);
                break;
            }
        case 2:
            {
                /*
                 * This format is allowed for ops that are effectively
                 * 2-arg but where the two args are identical.
                 */
                reg = regs.get(0);
                if (reg.getReg() != regs.get(1).getReg()) {
                    return false;
                }
                break;
            }
        default:
            {
                return false;
            }
    }
    if (!unsignedFitsInByte(reg.getReg())) {
        return false;
    }
    CstInsn ci = (CstInsn) insn;
    Constant cst = ci.getConstant();
    return (cst instanceof CstType) || (cst instanceof CstFieldRef) || (cst instanceof CstString);
}
Also used : CstInsn(com.taobao.android.dx.dex.code.CstInsn) Constant(com.taobao.android.dx.rop.cst.Constant) CstType(com.taobao.android.dx.rop.cst.CstType) CstFieldRef(com.taobao.android.dx.rop.cst.CstFieldRef) CstString(com.taobao.android.dx.rop.cst.CstString) RegisterSpecList(com.taobao.android.dx.rop.code.RegisterSpecList) RegisterSpec(com.taobao.android.dx.rop.code.RegisterSpec)

Example 18 with CstType

use of com.taobao.android.dx.rop.cst.CstType in project atlas by alibaba.

the class Ropper method processBlock.

/**
     * Processes the given block.
     *
     * @param block {@code non-null;} block to process
     * @param frame {@code non-null;} start frame for the block
     * @param workSet {@code non-null;} bits representing work to do,
     * which this method may add to
     */
private void processBlock(ByteBlock block, Frame frame, int[] workSet) {
    // Prepare the list of caught exceptions for this block.
    ByteCatchList catches = block.getCatches();
    machine.startBlock(catches.toRopCatchList());
    /*
         * Using a copy of the given frame, simulate each instruction,
         * calling into machine for each.
         */
    frame = frame.copy();
    sim.simulate(block, frame);
    frame.setImmutable();
    int extraBlockCount = machine.getExtraBlockCount();
    ArrayList<Insn> insns = machine.getInsns();
    int insnSz = insns.size();
    /*
         * Merge the frame into each possible non-exceptional
         * successor.
         */
    int catchSz = catches.size();
    IntList successors = block.getSuccessors();
    int startSuccessorIndex;
    Subroutine calledSubroutine = null;
    if (machine.hasJsr()) {
        /*
             * If this frame ends in a JSR, only merge our frame with
             * the subroutine start, not the subroutine's return target.
             */
        startSuccessorIndex = 1;
        int subroutineLabel = successors.get(1);
        if (subroutines[subroutineLabel] == null) {
            subroutines[subroutineLabel] = new Subroutine(subroutineLabel);
        }
        subroutines[subroutineLabel].addCallerBlock(block.getLabel());
        calledSubroutine = subroutines[subroutineLabel];
    } else if (machine.hasRet()) {
        /*
             * This block ends in a ret, which means it's the final block
             * in some subroutine. Ultimately, this block will be copied
             * and inlined for each call and then disposed of.
             */
        ReturnAddress ra = machine.getReturnAddress();
        int subroutineLabel = ra.getSubroutineAddress();
        if (subroutines[subroutineLabel] == null) {
            subroutines[subroutineLabel] = new Subroutine(subroutineLabel, block.getLabel());
        } else {
            subroutines[subroutineLabel].addRetBlock(block.getLabel());
        }
        successors = subroutines[subroutineLabel].getSuccessors();
        subroutines[subroutineLabel].mergeToSuccessors(frame, workSet);
        // Skip processing below since we just did it.
        startSuccessorIndex = successors.size();
    } else if (machine.wereCatchesUsed()) {
        /*
             * If there are catches, then the first successors
             * (which will either be all of them or all but the last one)
             * are catch targets.
             */
        startSuccessorIndex = catchSz;
    } else {
        startSuccessorIndex = 0;
    }
    int succSz = successors.size();
    for (int i = startSuccessorIndex; i < succSz; i++) {
        int succ = successors.get(i);
        try {
            mergeAndWorkAsNecessary(succ, block.getLabel(), calledSubroutine, frame, workSet);
        } catch (SimException ex) {
            ex.addContext("...while merging to block " + Hex.u2(succ));
            throw ex;
        }
    }
    if ((succSz == 0) && machine.returns()) {
        /*
             * The block originally contained a return, but it has
             * been made to instead end with a goto, and we need to
             * tell it at this point that its sole successor is the
             * return block. This has to happen after the merge loop
             * above, since, at this point, the return block doesn't
             * actually exist; it gets synthesized at the end of
             * processing the original blocks.
             */
        successors = IntList.makeImmutable(getSpecialLabel(RETURN));
        succSz = 1;
    }
    int primarySucc;
    if (succSz == 0) {
        primarySucc = -1;
    } else {
        primarySucc = machine.getPrimarySuccessorIndex();
        if (primarySucc >= 0) {
            primarySucc = successors.get(primarySucc);
        }
    }
    /*
         * This variable is true only when the method is synchronized and
         * the block being processed can possibly throw an exception.
         */
    boolean synch = isSynchronized() && machine.canThrow();
    if (synch || (catchSz != 0)) {
        /*
             * Deal with exception handlers: Merge an exception-catch
             * frame into each possible exception handler, and
             * construct a new set of successors to point at the
             * exception handler setup blocks (which get synthesized
             * at the very end of processing).
             */
        boolean catchesAny = false;
        IntList newSucc = new IntList(succSz);
        for (int i = 0; i < catchSz; i++) {
            ByteCatchList.Item one = catches.get(i);
            CstType exceptionClass = one.getExceptionClass();
            int targ = one.getHandlerPc();
            catchesAny |= (exceptionClass == CstType.OBJECT);
            Frame f = frame.makeExceptionHandlerStartFrame(exceptionClass);
            try {
                mergeAndWorkAsNecessary(targ, block.getLabel(), null, f, workSet);
            } catch (SimException ex) {
                ex.addContext("...while merging exception to block " + Hex.u2(targ));
                throw ex;
            }
            /*
                 * Set up the exception handler type.
                 */
            CatchInfo handlers = catchInfos[targ];
            if (handlers == null) {
                handlers = new CatchInfo();
                catchInfos[targ] = handlers;
            }
            ExceptionHandlerSetup handler = handlers.getSetup(exceptionClass.getClassType());
            /*
                 * The synthesized exception setup block will have the label given by handler.
                 */
            newSucc.add(handler.getLabel());
        }
        if (synch && !catchesAny) {
            /*
                 * The method is synchronized and this block doesn't
                 * already have a catch-all handler, so add one to the
                 * end, both in the successors and in the throwing
                 * instruction(s) at the end of the block (which is where
                 * the caught classes live).
                 */
            newSucc.add(getSpecialLabel(SYNCH_CATCH_1));
            synchNeedsExceptionHandler = true;
            for (int i = insnSz - extraBlockCount - 1; i < insnSz; i++) {
                Insn insn = insns.get(i);
                if (insn.canThrow()) {
                    insn = insn.withAddedCatch(Type.OBJECT);
                    insns.set(i, insn);
                }
            }
        }
        if (primarySucc >= 0) {
            newSucc.add(primarySucc);
        }
        newSucc.setImmutable();
        successors = newSucc;
    }
    // Construct the final resulting block(s), and store it (them).
    int primarySuccListIndex = successors.indexOf(primarySucc);
    /*
         * If there are any extra blocks, work backwards through the
         * list of instructions, adding single-instruction blocks, and
         * resetting the successors variables as appropriate.
         */
    for (; /*extraBlockCount*/
    extraBlockCount > 0; extraBlockCount--) {
        /*
             * Some of the blocks that the RopperMachine wants added
             * are for move-result insns, and these need goto insns as well.
             */
        Insn extraInsn = insns.get(--insnSz);
        boolean needsGoto = extraInsn.getOpcode().getBranchingness() == Rop.BRANCH_NONE;
        InsnList il = new InsnList(needsGoto ? 2 : 1);
        IntList extraBlockSuccessors = successors;
        il.set(0, extraInsn);
        if (needsGoto) {
            il.set(1, new PlainInsn(Rops.GOTO, extraInsn.getPosition(), null, RegisterSpecList.EMPTY));
            /*
                 * Obviously, this block won't be throwing an exception
                 * so it should only have one successor.
                 */
            extraBlockSuccessors = IntList.makeImmutable(primarySucc);
        }
        il.setImmutable();
        int label = getAvailableLabel();
        BasicBlock bb = new BasicBlock(label, il, extraBlockSuccessors, primarySucc);
        // All of these extra blocks will be in the same subroutine
        addBlock(bb, frame.getSubroutines());
        successors = successors.mutableCopy();
        successors.set(primarySuccListIndex, label);
        successors.setImmutable();
        primarySucc = label;
    }
    Insn lastInsn = (insnSz == 0) ? null : insns.get(insnSz - 1);
    /*
         * Add a goto to the end of the block if it doesn't already
         * end with a branch, to maintain the invariant that all
         * blocks end with a branch of some sort or other. Note that
         * it is possible for there to be blocks for which no
         * instructions were ever output (e.g., only consist of pop*
         * in the original Java bytecode).
         */
    if ((lastInsn == null) || (lastInsn.getOpcode().getBranchingness() == Rop.BRANCH_NONE)) {
        SourcePosition pos = (lastInsn == null) ? SourcePosition.NO_INFO : lastInsn.getPosition();
        insns.add(new PlainInsn(Rops.GOTO, pos, null, RegisterSpecList.EMPTY));
        insnSz++;
    }
    /*
         * Construct a block for the remaining instructions (which in
         * the usual case is all of them).
         */
    InsnList il = new InsnList(insnSz);
    for (int i = 0; i < insnSz; i++) {
        il.set(i, insns.get(i));
    }
    il.setImmutable();
    BasicBlock bb = new BasicBlock(block.getLabel(), il, successors, primarySucc);
    addOrReplaceBlock(bb, frame.getSubroutines());
}
Also used : Insn(com.taobao.android.dx.rop.code.Insn) PlainCstInsn(com.taobao.android.dx.rop.code.PlainCstInsn) PlainInsn(com.taobao.android.dx.rop.code.PlainInsn) ThrowingInsn(com.taobao.android.dx.rop.code.ThrowingInsn) ThrowingCstInsn(com.taobao.android.dx.rop.code.ThrowingCstInsn) BasicBlock(com.taobao.android.dx.rop.code.BasicBlock) InsnList(com.taobao.android.dx.rop.code.InsnList) IntList(com.taobao.android.dx.util.IntList) PlainInsn(com.taobao.android.dx.rop.code.PlainInsn) CstType(com.taobao.android.dx.rop.cst.CstType) SourcePosition(com.taobao.android.dx.rop.code.SourcePosition)

Example 19 with CstType

use of com.taobao.android.dx.rop.cst.CstType in project atlas by alibaba.

the class AnnotationParser method parseAnnotation.

/**
     * Parses a single annotation.
     *
     * @param visibility {@code non-null;} visibility of the parsed annotation
     * @return {@code non-null;} the parsed annotation
     */
private Annotation parseAnnotation(AnnotationVisibility visibility) throws IOException {
    requireLength(4);
    int typeIndex = input.readUnsignedShort();
    int numElements = input.readUnsignedShort();
    CstString typeString = (CstString) pool.get(typeIndex);
    CstType type = new CstType(Type.intern(typeString.getString()));
    if (observer != null) {
        parsed(2, "type: " + type.toHuman());
        parsed(2, "num_elements: " + numElements);
    }
    Annotation annotation = new Annotation(type, visibility);
    for (int i = 0; i < numElements; i++) {
        if (observer != null) {
            parsed(0, "elements[" + i + "]:");
            changeIndent(1);
        }
        NameValuePair element = parseElement();
        annotation.add(element);
        if (observer != null) {
            changeIndent(-1);
        }
    }
    annotation.setImmutable();
    return annotation;
}
Also used : NameValuePair(com.taobao.android.dx.rop.annotation.NameValuePair) CstType(com.taobao.android.dx.rop.cst.CstType) CstString(com.taobao.android.dx.rop.cst.CstString) Annotation(com.taobao.android.dx.rop.annotation.Annotation) CstAnnotation(com.taobao.android.dx.rop.cst.CstAnnotation)

Example 20 with CstType

use of com.taobao.android.dx.rop.cst.CstType in project atlas by alibaba.

the class AnnotationParser method parseValue.

/**
     * Parses an annotation value.
     *
     * @return {@code non-null;} the parsed value
     */
private Constant parseValue() throws IOException {
    int tag = input.readUnsignedByte();
    if (observer != null) {
        CstString humanTag = new CstString(Character.toString((char) tag));
        parsed(1, "tag: " + humanTag.toQuoted());
    }
    switch(tag) {
        case 'B':
            {
                CstInteger value = (CstInteger) parseConstant();
                return CstByte.make(value.getValue());
            }
        case 'C':
            {
                CstInteger value = (CstInteger) parseConstant();
                int intValue = value.getValue();
                return CstChar.make(value.getValue());
            }
        case 'D':
            {
                CstDouble value = (CstDouble) parseConstant();
                return value;
            }
        case 'F':
            {
                CstFloat value = (CstFloat) parseConstant();
                return value;
            }
        case 'I':
            {
                CstInteger value = (CstInteger) parseConstant();
                return value;
            }
        case 'J':
            {
                CstLong value = (CstLong) parseConstant();
                return value;
            }
        case 'S':
            {
                CstInteger value = (CstInteger) parseConstant();
                return CstShort.make(value.getValue());
            }
        case 'Z':
            {
                CstInteger value = (CstInteger) parseConstant();
                return CstBoolean.make(value.getValue());
            }
        case 'c':
            {
                int classInfoIndex = input.readUnsignedShort();
                CstString value = (CstString) pool.get(classInfoIndex);
                Type type = Type.internReturnType(value.getString());
                if (observer != null) {
                    parsed(2, "class_info: " + type.toHuman());
                }
                return new CstType(type);
            }
        case 's':
            {
                return parseConstant();
            }
        case 'e':
            {
                requireLength(4);
                int typeNameIndex = input.readUnsignedShort();
                int constNameIndex = input.readUnsignedShort();
                CstString typeName = (CstString) pool.get(typeNameIndex);
                CstString constName = (CstString) pool.get(constNameIndex);
                if (observer != null) {
                    parsed(2, "type_name: " + typeName.toHuman());
                    parsed(2, "const_name: " + constName.toHuman());
                }
                return new CstEnumRef(new CstNat(constName, typeName));
            }
        case '@':
            {
                Annotation annotation = parseAnnotation(AnnotationVisibility.EMBEDDED);
                return new CstAnnotation(annotation);
            }
        case '[':
            {
                requireLength(2);
                int numValues = input.readUnsignedShort();
                CstArray.List list = new CstArray.List(numValues);
                if (observer != null) {
                    parsed(2, "num_values: " + numValues);
                    changeIndent(1);
                }
                for (int i = 0; i < numValues; i++) {
                    if (observer != null) {
                        changeIndent(-1);
                        parsed(0, "element_value[" + i + "]:");
                        changeIndent(1);
                    }
                    list.set(i, parseValue());
                }
                if (observer != null) {
                    changeIndent(-1);
                }
                list.setImmutable();
                return new CstArray(list);
            }
        default:
            {
                throw new ParseException("unknown annotation tag: " + Hex.u1(tag));
            }
    }
}
Also used : CstFloat(com.taobao.android.dx.rop.cst.CstFloat) CstNat(com.taobao.android.dx.rop.cst.CstNat) CstArray(com.taobao.android.dx.rop.cst.CstArray) CstLong(com.taobao.android.dx.rop.cst.CstLong) CstString(com.taobao.android.dx.rop.cst.CstString) CstAnnotation(com.taobao.android.dx.rop.cst.CstAnnotation) CstDouble(com.taobao.android.dx.rop.cst.CstDouble) CstEnumRef(com.taobao.android.dx.rop.cst.CstEnumRef) Annotation(com.taobao.android.dx.rop.annotation.Annotation) CstAnnotation(com.taobao.android.dx.rop.cst.CstAnnotation) CstType(com.taobao.android.dx.rop.cst.CstType) Type(com.taobao.android.dx.rop.type.Type) CstInteger(com.taobao.android.dx.rop.cst.CstInteger) CstType(com.taobao.android.dx.rop.cst.CstType) AnnotationsList(com.taobao.android.dx.rop.annotation.AnnotationsList) ParseException(com.taobao.android.dx.cf.iface.ParseException)

Aggregations

CstType (com.taobao.android.dx.rop.cst.CstType)36 CstString (com.taobao.android.dx.rop.cst.CstString)14 Constant (com.taobao.android.dx.rop.cst.Constant)12 Type (com.taobao.android.dx.rop.type.Type)9 CstFieldRef (com.taobao.android.dx.rop.cst.CstFieldRef)7 CstMethodRef (com.taobao.android.dx.rop.cst.CstMethodRef)6 CstInsn (com.taobao.android.dx.dex.code.CstInsn)5 Annotations (com.taobao.android.dx.rop.annotation.Annotations)5 RegisterSpec (com.taobao.android.dx.rop.code.RegisterSpec)5 RegisterSpecList (com.taobao.android.dx.rop.code.RegisterSpecList)5 Annotation (com.taobao.android.dx.rop.annotation.Annotation)4 CstNat (com.taobao.android.dx.rop.cst.CstNat)4 IntList (com.taobao.android.dx.util.IntList)4 AttEnclosingMethod (com.taobao.android.dx.cf.attrib.AttEnclosingMethod)3 NameValuePair (com.taobao.android.dx.rop.annotation.NameValuePair)3 ConstantPool (com.taobao.android.dx.rop.cst.ConstantPool)3 TypeList (com.taobao.android.dx.rop.type.TypeList)3 ByteArray (com.taobao.android.dx.util.ByteArray)3 AttInnerClasses (com.taobao.android.dx.cf.attrib.AttInnerClasses)2 AttRuntimeInvisibleAnnotations (com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations)2