com.android.dx.rop.cst.CstMethodRef Java Examples

The following examples show how to use com.android.dx.rop.cst.CstMethodRef. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EncodedMethod.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param method {@code non-null;} constant for the method
 * @param accessFlags access flags
 * @param code {@code null-ok;} code for the method, if it is neither
 * {@code abstract} nor {@code native}
 * @param throwsList {@code non-null;} list of possibly-thrown exceptions,
 * just used in generating debugging output (listings)
 */
public EncodedMethod(CstMethodRef method, int accessFlags,
        DalvCode code, TypeList throwsList) {
    super(accessFlags);

    if (method == null) {
        throw new NullPointerException("method == null");
    }

    this.method = method;

    if (code == null) {
        this.code = null;
    } else {
        boolean isStatic = (accessFlags & AccessFlags.ACC_STATIC) != 0;
        this.code = new CodeItem(method, code, isStatic, throwsList);
    }
}
 
Example #2
Source File: CodeItem.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param ref {@code non-null;} method that this code implements
 * @param code {@code non-null;} the underlying code
 * @param isStatic whether this instance is for a {@code static}
 * method
 * @param throwsList {@code non-null;} list of possibly-thrown exceptions,
 * just used in generating debugging output (listings)
 */
public CodeItem(CstMethodRef ref, DalvCode code, boolean isStatic,
        TypeList throwsList) {
    super(ALIGNMENT, -1);

    if (ref == null) {
        throw new NullPointerException("ref == null");
    }

    if (code == null) {
        throw new NullPointerException("code == null");
    }

    if (throwsList == null) {
        throw new NullPointerException("throwsList == null");
    }

    this.ref = ref;
    this.code = code;
    this.isStatic = isStatic;
    this.throwsList = throwsList;
    this.catches = null;
    this.debugInfo = null;
}
 
Example #3
Source File: ClassReferenceListBuilder.java    From RocooFix with MIT License 6 votes vote down vote up
private void addDependencies(ConstantPool pool) {

        for (Constant constant : pool.getEntries()) {
            if (constant instanceof CstType) {
                checkDescriptor(((CstType) constant).getClassType());
            } else if (constant instanceof CstFieldRef) {
                checkDescriptor(((CstFieldRef) constant).getType());
            } else if (constant instanceof CstMethodRef) {
                Prototype proto = ((CstMethodRef) constant).getPrototype();
                checkDescriptor(proto.getReturnType());
                StdTypeList args = proto.getParameterTypes();
                for (int i = 0; i < args.size(); i++) {
                    checkDescriptor(args.get(i));
                }
            }
        }
    }
 
Example #4
Source File: InvokePolymorphicInsn.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param opcode {@code non-null;} the opcode
 * @param position {@code non-null;} source position
 * @param sources {@code non-null;} specs for all the sources
 * @param catches {@code non-null;} list of exceptions caught
 * @param callSiteMethod {@code non-null;} the method called by
 * invoke-virtual that this instance will replace.
 */
public InvokePolymorphicInsn(Rop opcode, SourcePosition position, RegisterSpecList sources, TypeList catches,
        CstMethodRef callSiteMethod) {
    super(opcode, position, null, sources);

    if (opcode.getBranchingness() != Rop.BRANCH_THROW) {
        throw new IllegalArgumentException("opcode with invalid branchingness: " + opcode.getBranchingness());
    }

    if (catches == null) {
        throw new NullPointerException("catches == null");
    }
    this.catches = catches;

    if (callSiteMethod == null) {
        throw new NullPointerException("callSiteMethod == null");
    } else if (!callSiteMethod.isSignaturePolymorphic()) {
        throw new IllegalArgumentException("callSiteMethod is not signature polymorphic");
    }

    this.callSiteMethod = callSiteMethod;
    this.polymorphicMethod = makePolymorphicMethod(callSiteMethod);
    this.callSiteProto = makeCallSiteProto(callSiteMethod);
}
 
Example #5
Source File: AttributeTranslator.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the {@code EnclosingMethod} attribute out of a given
 * {@link AttributeList}, if any, translating it to an annotation.
 * If the class really has an enclosing method, this returns an
 * {@code EnclosingMethod} annotation; if not, this returns
 * an {@code EnclosingClass} annotation.
 *
 * @param attribs {@code non-null;} the attributes list to search in
 * @return {@code null-ok;} the converted {@code EnclosingMethod} or
 * {@code EnclosingClass} annotation, if there was an
 * attribute to translate
 */
private static Annotation translateEnclosingMethod(AttributeList attribs) {
    AttEnclosingMethod enclosingMethod = (AttEnclosingMethod)
        attribs.findFirst(AttEnclosingMethod.ATTRIBUTE_NAME);

    if (enclosingMethod == null) {
        return null;
    }

    CstType enclosingClass = enclosingMethod.getEnclosingClass();
    CstNat nat = enclosingMethod.getMethod();

    if (nat == null) {
        /*
         * Dalvik doesn't use EnclosingMethod annotations unless
         * there really is an enclosing method. Anonymous classes
         * are unambiguously identified by having an InnerClass
         * annotation with an empty name along with an appropriate
         * EnclosingClass.
         */
        return AnnotationUtils.makeEnclosingClass(enclosingClass);
    }

    return AnnotationUtils.makeEnclosingMethod(
            new CstMethodRef(enclosingClass, nat));
}
 
Example #6
Source File: CodeItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param ref {@code non-null;} method that this code implements
 * @param code {@code non-null;} the underlying code
 * @param isStatic whether this instance is for a {@code static}
 * method
 * @param throwsList {@code non-null;} list of possibly-thrown exceptions,
 * just used in generating debugging output (listings)
 */
public CodeItem(CstMethodRef ref, DalvCode code, boolean isStatic,
        TypeList throwsList) {
    super(ALIGNMENT, -1);

    if (ref == null) {
        throw new NullPointerException("ref == null");
    }

    if (code == null) {
        throw new NullPointerException("code == null");
    }

    if (throwsList == null) {
        throw new NullPointerException("throwsList == null");
    }

    this.ref = ref;
    this.code = code;
    this.isStatic = isStatic;
    this.throwsList = throwsList;
    this.catches = null;
    this.debugInfo = null;
}
 
Example #7
Source File: CodeItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param ref {@code non-null;} method that this code implements
 * @param code {@code non-null;} the underlying code
 * @param isStatic whether this instance is for a {@code static}
 * method
 * @param throwsList {@code non-null;} list of possibly-thrown exceptions,
 * just used in generating debugging output (listings)
 */
public CodeItem(CstMethodRef ref, DalvCode code, boolean isStatic,
        TypeList throwsList) {
    super(ALIGNMENT, -1);

    if (ref == null) {
        throw new NullPointerException("ref == null");
    }

    if (code == null) {
        throw new NullPointerException("code == null");
    }

    if (throwsList == null) {
        throw new NullPointerException("throwsList == null");
    }

    this.ref = ref;
    this.code = code;
    this.isStatic = isStatic;
    this.throwsList = throwsList;
    this.catches = null;
    this.debugInfo = null;
}
 
Example #8
Source File: AttributeTranslator.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the {@code EnclosingMethod} attribute out of a given
 * {@link AttributeList}, if any, translating it to an annotation.
 * If the class really has an enclosing method, this returns an
 * {@code EnclosingMethod} annotation; if not, this returns
 * an {@code EnclosingClass} annotation.
 *
 * @param attribs {@code non-null;} the attributes list to search in
 * @return {@code null-ok;} the converted {@code EnclosingMethod} or
 * {@code EnclosingClass} annotation, if there was an
 * attribute to translate
 */
private static Annotation translateEnclosingMethod(AttributeList attribs) {
    AttEnclosingMethod enclosingMethod = (AttEnclosingMethod)
        attribs.findFirst(AttEnclosingMethod.ATTRIBUTE_NAME);

    if (enclosingMethod == null) {
        return null;
    }

    CstType enclosingClass = enclosingMethod.getEnclosingClass();
    CstNat nat = enclosingMethod.getMethod();

    if (nat == null) {
        /*
         * Dalvik doesn't use EnclosingMethod annotations unless
         * there really is an enclosing method. Anonymous classes
         * are unambiguously identified by having an InnerClass
         * annotation with an empty name along with an appropriate
         * EnclosingClass.
         */
        return AnnotationUtils.makeEnclosingClass(enclosingClass);
    }

    return AnnotationUtils.makeEnclosingMethod(
            new CstMethodRef(enclosingClass, nat));
}
 
Example #9
Source File: EncodedMethod.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param method {@code non-null;} constant for the method
 * @param accessFlags access flags
 * @param code {@code null-ok;} code for the method, if it is neither
 * {@code abstract} nor {@code native}
 * @param throwsList {@code non-null;} list of possibly-thrown exceptions,
 * just used in generating debugging output (listings)
 */
public EncodedMethod(CstMethodRef method, int accessFlags,
        DalvCode code, TypeList throwsList) {
    super(accessFlags);

    if (method == null) {
        throw new NullPointerException("method == null");
    }

    this.method = method;

    if (code == null) {
        this.code = null;
    } else {
        boolean isStatic = (accessFlags & AccessFlags.ACC_STATIC) != 0;
        this.code = new CodeItem(method, code, isStatic, throwsList);
    }
}
 
Example #10
Source File: CodeItem.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param ref {@code non-null;} method that this code implements
 * @param code {@code non-null;} the underlying code
 * @param isStatic whether this instance is for a {@code static}
 * method
 * @param throwsList {@code non-null;} list of possibly-thrown exceptions,
 * just used in generating debugging output (listings)
 */
public CodeItem(CstMethodRef ref, DalvCode code, boolean isStatic,
        TypeList throwsList) {
    super(ALIGNMENT, -1);

    if (ref == null) {
        throw new NullPointerException("ref == null");
    }

    if (code == null) {
        throw new NullPointerException("code == null");
    }

    if (throwsList == null) {
        throw new NullPointerException("throwsList == null");
    }

    this.ref = ref;
    this.code = code;
    this.isStatic = isStatic;
    this.throwsList = throwsList;
    this.catches = null;
    this.debugInfo = null;
}
 
Example #11
Source File: DebugInfoDecoder.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Validates an encoded debug info stream against data used to encode it,
 * throwing an exception if they do not match. Used to validate the
 * encoder.
 *
 * @param info encoded debug info
 * @param file {@code non-null;} file to refer to during decoding
 * @param ref {@code non-null;} method whose info is being decoded
 * @param code {@code non-null;} original code object that was encoded
 * @param isStatic whether the method is static
 */
public static void validateEncode(byte[] info, DexFile file,
        CstMethodRef ref, DalvCode code, boolean isStatic) {
    PositionList pl = code.getPositions();
    LocalList ll = code.getLocals();
    DalvInsnList insns = code.getInsns();
    int codeSize = insns.codeSize();
    int countRegisters = insns.getRegistersSize();

    try {
        validateEncode0(info, codeSize, countRegisters,
                isStatic, ref, file, pl, ll);
    } catch (RuntimeException ex) {
        System.err.println("instructions:");
        insns.debugPrint(System.err, "  ", true);
        System.err.println("local list:");
        ll.debugPrint(System.err, "  ");
        throw ExceptionWithContext.withContext(ex,
                "while processing " + ref.toHuman());
    }
}
 
Example #12
Source File: DebugInfoDecoder.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Validates an encoded debug info stream against data used to encode it,
 * throwing an exception if they do not match. Used to validate the
 * encoder.
 *
 * @param info encoded debug info
 * @param file {@code non-null;} file to refer to during decoding
 * @param ref {@code non-null;} method whose info is being decoded
 * @param code {@code non-null;} original code object that was encoded
 * @param isStatic whether the method is static
 */
public static void validateEncode(byte[] info, DexFile file,
        CstMethodRef ref, DalvCode code, boolean isStatic) {
    PositionList pl = code.getPositions();
    LocalList ll = code.getLocals();
    DalvInsnList insns = code.getInsns();
    int codeSize = insns.codeSize();
    int countRegisters = insns.getRegistersSize();

    try {
        validateEncode0(info, codeSize, countRegisters,
                isStatic, ref, file, pl, ll);
    } catch (RuntimeException ex) {
        System.err.println("instructions:");
        insns.debugPrint(System.err, "  ", true);
        System.err.println("local list:");
        ll.debugPrint(System.err, "  ");
        throw ExceptionWithContext.withContext(ex,
                "while processing " + ref.toHuman());
    }
}
 
Example #13
Source File: Form35c.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    if (!(insn instanceof CstInsn)) {
        return false;
    }

    CstInsn ci = (CstInsn) insn;
    int cpi = ci.getIndex();

    if (! unsignedFitsInShort(cpi)) {
        return false;
    }

    Constant cst = ci.getConstant();
    if (!((cst instanceof CstMethodRef) ||
          (cst instanceof CstType))) {
        return false;
    }

    RegisterSpecList regs = ci.getRegisters();
    return (wordCount(regs) >= 0);
}
 
Example #14
Source File: DebugInfoDecoder.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Validates an encoded debug info stream against data used to encode it,
 * throwing an exception if they do not match. Used to validate the
 * encoder.
 *
 * @param info encoded debug info
 * @param file {@code non-null;} file to refer to during decoding
 * @param ref {@code non-null;} method whose info is being decoded
 * @param code {@code non-null;} original code object that was encoded
 * @param isStatic whether the method is static
 */
public static void validateEncode(byte[] info, DexFile file,
        CstMethodRef ref, DalvCode code, boolean isStatic) {
    PositionList pl = code.getPositions();
    LocalList ll = code.getLocals();
    DalvInsnList insns = code.getInsns();
    int codeSize = insns.codeSize();
    int countRegisters = insns.getRegistersSize();

    try {
        validateEncode0(info, codeSize, countRegisters,
                isStatic, ref, file, pl, ll);
    } catch (RuntimeException ex) {
        System.err.println("instructions:");
        insns.debugPrint(System.err, "  ", true);
        System.err.println("local list:");
        ll.debugPrint(System.err, "  ");
        throw ExceptionWithContext.withContext(ex,
                "while processing " + ref.toHuman());
    }
}
 
Example #15
Source File: Form35c.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    if (!(insn instanceof CstInsn)) {
        return false;
    }

    CstInsn ci = (CstInsn) insn;
    int cpi = ci.getIndex();

    if (! unsignedFitsInShort(cpi)) {
        return false;
    }

    Constant cst = ci.getConstant();
    if (!((cst instanceof CstMethodRef) ||
          (cst instanceof CstType))) {
        return false;
    }

    RegisterSpecList regs = ci.getRegisters();
    return (wordCount(regs) >= 0);
}
 
Example #16
Source File: Form4rcc.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    if (!(insn instanceof MultiCstInsn)) {
        return false;
    }

    MultiCstInsn mci = (MultiCstInsn) insn;
    int methodIdx = mci.getIndex(0);
    int protoIdx = mci.getIndex(1);
    if (!unsignedFitsInShort(methodIdx) || !unsignedFitsInShort(protoIdx)) {
        return false;
    }

    Constant methodRef = mci.getConstant(0);
    if (!(methodRef instanceof CstMethodRef)) {
        return false;
    }

    Constant protoRef = mci.getConstant(1);
    if (!(protoRef instanceof CstProtoRef)) {
        return false;
    }

    RegisterSpecList regs = mci.getRegisters();
    int sz = regs.size();
    if (sz == 0) {
        return true;
    }

    return (unsignedFitsInByte(regs.getWordCount()) &&
            unsignedFitsInShort(sz) &&
            unsignedFitsInShort(regs.get(0).getReg()) &&
            isRegListSequential(regs));
}
 
Example #17
Source File: AnnotationsDirectoryItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the parameter annotations for a given method, if any. This is
 * meant for use by debugging / dumping code.
 *
 * @param method {@code non-null;} the method
 * @return {@code null-ok;} the parameter annotations, if any
 */
public AnnotationsList getParameterAnnotations(CstMethodRef method) {
    if (parameterAnnotations == null) {
        return null;
    }

    for (ParameterAnnotationStruct item : parameterAnnotations) {
        if (item.getMethod().equals(method)) {
            return item.getAnnotationsList();
        }
    }

    return null;
}
 
Example #18
Source File: Form3rc.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    if (!(insn instanceof CstInsn)) {
        return false;
    }

    CstInsn ci = (CstInsn) insn;
    int cpi = ci.getIndex();
    Constant cst = ci.getConstant();

    if (! unsignedFitsInShort(cpi)) {
        return false;
    }

    if (!((cst instanceof CstMethodRef) ||
          (cst instanceof CstType))) {
        return false;
    }

    RegisterSpecList regs = ci.getRegisters();

    return (regs.size() == 0) ||
        (isRegListSequential(regs) &&
         unsignedFitsInShort(regs.get(0).getReg()) &&
         unsignedFitsInByte(regs.getWordCount()));
}
 
Example #19
Source File: Form4rcc.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    if (!(insn instanceof MultiCstInsn)) {
        return false;
    }

    MultiCstInsn mci = (MultiCstInsn) insn;
    int methodIdx = mci.getIndex(0);
    int protoIdx = mci.getIndex(1);
    if (!unsignedFitsInShort(methodIdx) || !unsignedFitsInShort(protoIdx)) {
        return false;
    }

    Constant methodRef = mci.getConstant(0);
    if (!(methodRef instanceof CstMethodRef)) {
        return false;
    }

    Constant protoRef = mci.getConstant(1);
    if (!(protoRef instanceof CstProtoRef)) {
        return false;
    }

    RegisterSpecList regs = mci.getRegisters();
    int sz = regs.size();
    if (sz == 0) {
        return true;
    }

    return (unsignedFitsInByte(regs.getWordCount()) &&
            unsignedFitsInShort(sz) &&
            unsignedFitsInShort(regs.get(0).getReg()) &&
            isRegListSequential(regs));
}
 
Example #20
Source File: AnnotationUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code EnclosingMethod} annotation.
 *
 * @param method {@code non-null;} the enclosing method
 * @return {@code non-null;} the annotation
 */
public static Annotation makeEnclosingMethod(CstMethodRef method) {
    Annotation result = new Annotation(ENCLOSING_METHOD_TYPE, SYSTEM);

    result.put(new NameValuePair(VALUE_STRING, method));
    result.setImmutable();
    return result;
}
 
Example #21
Source File: EscapeAnalysis.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces instructions that trigger an ArrayIndexOutofBounds exception
 * with an actual throw of the exception.
 *
 * @param insn {@code non-null;} instruction causing the exception
 * @param index {@code non-null;} index value that is out of bounds
 * @param deletedInsns {@code non-null;} set of instructions marked for
 * deletion
 */
private void insertExceptionThrow(SsaInsn insn, RegisterSpec index,
                                      HashSet<SsaInsn> deletedInsns) {
    // Create a new ArrayIndexOutOfBoundsException
    CstType exception =
        new CstType(Exceptions.TYPE_ArrayIndexOutOfBoundsException);
    insertThrowingInsnBefore(insn, RegisterSpecList.EMPTY, null,
                                 RegOps.NEW_INSTANCE, exception);

    // Add a successor block with a move result pseudo for the exception
    SsaBasicBlock currBlock = insn.getBlock();
    SsaBasicBlock newBlock =
        currBlock.insertNewSuccessor(currBlock.getPrimarySuccessor());
    SsaInsn newInsn = newBlock.getInsns().get(0);
    RegisterSpec newReg =
        RegisterSpec.make(ssaMeth.makeNewSsaReg(), exception);
    insertPlainInsnBefore(newInsn, RegisterSpecList.EMPTY, newReg,
                              RegOps.MOVE_RESULT_PSEUDO, null);

    // Add another successor block to initialize the exception
    SsaBasicBlock newBlock2 =
        newBlock.insertNewSuccessor(newBlock.getPrimarySuccessor());
    SsaInsn newInsn2 = newBlock2.getInsns().get(0);
    CstNat newNat = new CstNat(new CstString("<init>"), new CstString("(I)V"));
    CstMethodRef newRef = new CstMethodRef(exception, newNat);
    insertThrowingInsnBefore(newInsn2, RegisterSpecList.make(newReg, index),
                                 null, RegOps.INVOKE_DIRECT, newRef);
    deletedInsns.add(newInsn2);

    // Add another successor block to throw the new exception
    SsaBasicBlock newBlock3 =
        newBlock2.insertNewSuccessor(newBlock2.getPrimarySuccessor());
    SsaInsn newInsn3 = newBlock3.getInsns().get(0);
    insertThrowingInsnBefore(newInsn3, RegisterSpecList.make(newReg), null,
                                 RegOps.THROW, null);
    newBlock3.replaceSuccessor(newBlock3.getPrimarySuccessorIndex(),
                                   ssaMeth.getExitBlock().getIndex());
    deletedInsns.add(newInsn3);
}
 
Example #22
Source File: AnnotationsDirectoryItem.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the method annotations for a given method, if any. This is
 * meant for use by debugging / dumping code.
 *
 * @param method {@code non-null;} the method
 * @return {@code null-ok;} the method annotations, if any
 */
public Annotations getMethodAnnotations(CstMethodRef method) {
    if (methodAnnotations == null) {
        return null;
    }

    for (MethodAnnotationStruct item : methodAnnotations) {
        if (item.getMethod().equals(method)) {
            return item.getAnnotations();
        }
    }

    return null;
}
 
Example #23
Source File: EscapeAnalysis.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces instructions that trigger an ArrayIndexOutofBounds exception
 * with an actual throw of the exception.
 *
 * @param insn {@code non-null;} instruction causing the exception
 * @param index {@code non-null;} index value that is out of bounds
 * @param deletedInsns {@code non-null;} set of instructions marked for
 * deletion
 */
private void insertExceptionThrow(SsaInsn insn, RegisterSpec index,
                                      HashSet<SsaInsn> deletedInsns) {
    // Create a new ArrayIndexOutOfBoundsException
    CstType exception =
        new CstType(Exceptions.TYPE_ArrayIndexOutOfBoundsException);
    insertThrowingInsnBefore(insn, RegisterSpecList.EMPTY, null,
                                 RegOps.NEW_INSTANCE, exception);

    // Add a successor block with a move result pseudo for the exception
    SsaBasicBlock currBlock = insn.getBlock();
    SsaBasicBlock newBlock =
        currBlock.insertNewSuccessor(currBlock.getPrimarySuccessor());
    SsaInsn newInsn = newBlock.getInsns().get(0);
    RegisterSpec newReg =
        RegisterSpec.make(ssaMeth.makeNewSsaReg(), exception);
    insertPlainInsnBefore(newInsn, RegisterSpecList.EMPTY, newReg,
                              RegOps.MOVE_RESULT_PSEUDO, null);

    // Add another successor block to initialize the exception
    SsaBasicBlock newBlock2 =
        newBlock.insertNewSuccessor(newBlock.getPrimarySuccessor());
    SsaInsn newInsn2 = newBlock2.getInsns().get(0);
    CstNat newNat = new CstNat(new CstString("<init>"), new CstString("(I)V"));
    CstMethodRef newRef = new CstMethodRef(exception, newNat);
    insertThrowingInsnBefore(newInsn2, RegisterSpecList.make(newReg, index),
                                 null, RegOps.INVOKE_DIRECT, newRef);
    deletedInsns.add(newInsn2);

    // Add another successor block to throw the new exception
    SsaBasicBlock newBlock3 =
        newBlock2.insertNewSuccessor(newBlock2.getPrimarySuccessor());
    SsaInsn newInsn3 = newBlock3.getInsns().get(0);
    insertThrowingInsnBefore(newInsn3, RegisterSpecList.make(newReg), null,
                                 RegOps.THROW, null);
    newBlock3.replaceSuccessor(newBlock3.getPrimarySuccessorIndex(),
                                   ssaMeth.getExitBlock().getIndex());
    deletedInsns.add(newInsn3);
}
 
Example #24
Source File: Form45cc.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    if (!(insn instanceof MultiCstInsn)) {
        return false;
    }

    MultiCstInsn mci = (MultiCstInsn) insn;
    if (mci.getNumberOfConstants() != 2) {
        return false;
    }

    int methodIdx = mci.getIndex(0);
    int protoIdx = mci.getIndex(1);
    if (!unsignedFitsInShort(methodIdx) || !unsignedFitsInShort(protoIdx)) {
        return false;
    }

    Constant methodRef = mci.getConstant(0);
    if (!(methodRef instanceof CstMethodRef)) {
        return false;
    }

    Constant protoRef = mci.getConstant(1);
    if (!(protoRef instanceof CstProtoRef)) {
        return false;
    }

    RegisterSpecList regs = mci.getRegisters();
    return (wordCount(regs) >= 0);
}
 
Example #25
Source File: Form3rc.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    if (!(insn instanceof CstInsn)) {
        return false;
    }

    CstInsn ci = (CstInsn) insn;
    int cpi = ci.getIndex();
    Constant cst = ci.getConstant();

    if (! unsignedFitsInShort(cpi)) {
        return false;
    }

    if (!((cst instanceof CstMethodRef) ||
          (cst instanceof CstType) ||
          (cst instanceof CstCallSiteRef))) {
        return false;
    }

    RegisterSpecList regs = ci.getRegisters();
    int sz = regs.size();

    return (regs.size() == 0) ||
        (isRegListSequential(regs) &&
         unsignedFitsInShort(regs.get(0).getReg()) &&
         unsignedFitsInByte(regs.getWordCount()));
}
 
Example #26
Source File: AnnotationsDirectoryItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the method annotations for a given method, if any. This is
 * meant for use by debugging / dumping code.
 *
 * @param method {@code non-null;} the method
 * @return {@code null-ok;} the method annotations, if any
 */
public Annotations getMethodAnnotations(CstMethodRef method) {
    if (methodAnnotations == null) {
        return null;
    }

    for (MethodAnnotationStruct item : methodAnnotations) {
        if (item.getMethod().equals(method)) {
            return item.getAnnotations();
        }
    }

    return null;
}
 
Example #27
Source File: AnnotationsDirectoryItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a parameter annotations item to this instance.
 *
 * @param method {@code non-null;} method in question
 * @param list {@code non-null;} associated list of annotation sets to add
 * @param dexFile {@code non-null;} dex output
 */
public void addParameterAnnotations(CstMethodRef method,
        AnnotationsList list, DexFile dexFile) {
    if (parameterAnnotations == null) {
        parameterAnnotations = new ArrayList<ParameterAnnotationStruct>();
    }

    parameterAnnotations.add(new ParameterAnnotationStruct(method, list, dexFile));
}
 
Example #28
Source File: DebugInfoEncoder.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an instance.
 *
 * @param positions {@code null-ok;} positions (line numbers) to encode
 * @param locals {@code null-ok;} local variables to encode
 * @param file {@code null-ok;} may only be {@code null} if simply using
 * this class to do a debug print
 * @param codeSize
 * @param regSize
 * @param isStatic
 * @param ref
 */
public DebugInfoEncoder(PositionList positions, LocalList locals,
        DexFile file, int codeSize, int regSize,
        boolean isStatic, CstMethodRef ref) {
    this.positions = positions;
    this.locals = locals;
    this.file = file;
    this.desc = ref.getPrototype();
    this.isStatic = isStatic;
    this.codeSize = codeSize;
    this.regSize = regSize;

    output = new ByteArrayAnnotatedOutput();
    lastEntryForReg = new LocalList.Entry[regSize];
}
 
Example #29
Source File: DebugInfoDecoder.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param encoded encoded debug info
 * @param codesize size of code block in code units
 * @param regSize register size, in register units, of the register space
 * used by this method
 * @param isStatic true if method is static
 * @param ref method descriptor of method this debug info is for
 * @param file dex file this debug info will be stored in
 */
DebugInfoDecoder(byte[] encoded, int codesize, int regSize,
        boolean isStatic, CstMethodRef ref, DexFile file) {
    if (encoded == null) {
        throw new NullPointerException("encoded == null");
    }

    this.encoded = encoded;
    this.isStatic = isStatic;
    this.desc = ref.getPrototype();
    this.file = file;
    this.regSize = regSize;

    positions = new ArrayList<PositionEntry>();
    locals = new ArrayList<LocalEntry>();
    this.codesize = codesize;
    lastEntryForReg = new LocalEntry[regSize];

    int idx = -1;

    try {
        idx = file.getStringIds().indexOf(new CstString("this"));
    } catch (IllegalArgumentException ex) {
        /*
         * Silently tolerate not finding "this". It just means that
         * no method has local variable info that looks like
         * a standard instance method.
         */
    }

    thisStringIdx = idx;
}
 
Example #30
Source File: AnnotationsDirectoryItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a method annotations item to this instance.
 *
 * @param method {@code non-null;} method in question
 * @param annotations {@code non-null;} associated annotations to add
 * @param dexFile {@code non-null;} dex output
 */
public void addMethodAnnotations(CstMethodRef method,
        Annotations annotations, DexFile dexFile) {
    if (methodAnnotations == null) {
        methodAnnotations = new ArrayList<MethodAnnotationStruct>();
    }

    methodAnnotations.add(new MethodAnnotationStruct(method,
                    new AnnotationSetItem(annotations, dexFile)));
}