Search in sources :

Example 11 with ConstantPool

use of com.android.dx.rop.cst.ConstantPool in project J2ME-Loader by nikita36078.

the class StdAttributeFactory method code.

/**
 * Parses a {@code Code} attribute.
 */
private Attribute code(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length < 12) {
        return throwSeverelyTruncated();
    }
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    // u2 max_stack
    int maxStack = bytes.getUnsignedShort(offset);
    // u2 max_locals
    int maxLocals = bytes.getUnsignedShort(offset + 2);
    // u4 code_length
    int codeLength = bytes.getInt(offset + 4);
    int origOffset = offset;
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "max_stack: " + Hex.u2(maxStack));
        observer.parsed(bytes, offset + 2, 2, "max_locals: " + Hex.u2(maxLocals));
        observer.parsed(bytes, offset + 4, 4, "code_length: " + Hex.u4(codeLength));
    }
    offset += 8;
    length -= 8;
    if (length < (codeLength + 4)) {
        return throwTruncated();
    }
    int codeOffset = offset;
    offset += codeLength;
    length -= codeLength;
    BytecodeArray code = new BytecodeArray(bytes.slice(codeOffset, codeOffset + codeLength), pool);
    if (observer != null) {
        code.forEach(new CodeObserver(code.getBytes(), observer));
    }
    // u2 exception_table_length
    int exceptionTableLength = bytes.getUnsignedShort(offset);
    ByteCatchList catches = (exceptionTableLength == 0) ? ByteCatchList.EMPTY : new ByteCatchList(exceptionTableLength);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "exception_table_length: " + Hex.u2(exceptionTableLength));
    }
    offset += 2;
    length -= 2;
    if (length < (exceptionTableLength * 8 + 2)) {
        return throwTruncated();
    }
    for (int i = 0; i < exceptionTableLength; i++) {
        if (observer != null) {
            observer.changeIndent(1);
        }
        int startPc = bytes.getUnsignedShort(offset);
        int endPc = bytes.getUnsignedShort(offset + 2);
        int handlerPc = bytes.getUnsignedShort(offset + 4);
        int catchTypeIdx = bytes.getUnsignedShort(offset + 6);
        CstType catchType = (CstType) pool.get0Ok(catchTypeIdx);
        catches.set(i, startPc, endPc, handlerPc, catchType);
        if (observer != null) {
            observer.parsed(bytes, offset, 8, Hex.u2(startPc) + ".." + Hex.u2(endPc) + " -> " + Hex.u2(handlerPc) + " " + ((catchType == null) ? "<any>" : catchType.toHuman()));
        }
        offset += 8;
        length -= 8;
        if (observer != null) {
            observer.changeIndent(-1);
        }
    }
    catches.setImmutable();
    AttributeListParser parser = new AttributeListParser(cf, CTX_CODE, offset, this);
    parser.setObserver(observer);
    StdAttributeList attributes = parser.getList();
    attributes.setImmutable();
    int attributeByteCount = parser.getEndOffset() - offset;
    if (attributeByteCount != length) {
        return throwBadLength(attributeByteCount + (offset - origOffset));
    }
    return new AttCode(maxStack, maxLocals, code, catches, attributes);
}
Also used : BytecodeArray(com.android.dx.cf.code.BytecodeArray) StdAttributeList(com.android.dx.cf.iface.StdAttributeList) ByteCatchList(com.android.dx.cf.code.ByteCatchList) ConstantPool(com.android.dx.rop.cst.ConstantPool) CstType(com.android.dx.rop.cst.CstType) AttCode(com.android.dx.cf.attrib.AttCode) ByteArray(com.android.dx.util.ByteArray)

Example 12 with ConstantPool

use of com.android.dx.rop.cst.ConstantPool in project buck by facebook.

the class StdAttributeFactory method code.

/**
     * Parses a {@code Code} attribute.
     */
private Attribute code(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length < 12) {
        return throwSeverelyTruncated();
    }
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    // u2 max_stack
    int maxStack = bytes.getUnsignedShort(offset);
    // u2 max_locals
    int maxLocals = bytes.getUnsignedShort(offset + 2);
    // u4 code_length
    int codeLength = bytes.getInt(offset + 4);
    int origOffset = offset;
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "max_stack: " + Hex.u2(maxStack));
        observer.parsed(bytes, offset + 2, 2, "max_locals: " + Hex.u2(maxLocals));
        observer.parsed(bytes, offset + 4, 4, "code_length: " + Hex.u4(codeLength));
    }
    offset += 8;
    length -= 8;
    if (length < (codeLength + 4)) {
        return throwTruncated();
    }
    int codeOffset = offset;
    offset += codeLength;
    length -= codeLength;
    BytecodeArray code = new BytecodeArray(bytes.slice(codeOffset, codeOffset + codeLength), pool);
    if (observer != null) {
        code.forEach(new CodeObserver(code.getBytes(), observer));
    }
    // u2 exception_table_length
    int exceptionTableLength = bytes.getUnsignedShort(offset);
    ByteCatchList catches = (exceptionTableLength == 0) ? ByteCatchList.EMPTY : new ByteCatchList(exceptionTableLength);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "exception_table_length: " + Hex.u2(exceptionTableLength));
    }
    offset += 2;
    length -= 2;
    if (length < (exceptionTableLength * 8 + 2)) {
        return throwTruncated();
    }
    for (int i = 0; i < exceptionTableLength; i++) {
        if (observer != null) {
            observer.changeIndent(1);
        }
        int startPc = bytes.getUnsignedShort(offset);
        int endPc = bytes.getUnsignedShort(offset + 2);
        int handlerPc = bytes.getUnsignedShort(offset + 4);
        int catchTypeIdx = bytes.getUnsignedShort(offset + 6);
        CstType catchType = (CstType) pool.get0Ok(catchTypeIdx);
        catches.set(i, startPc, endPc, handlerPc, catchType);
        if (observer != null) {
            observer.parsed(bytes, offset, 8, Hex.u2(startPc) + ".." + Hex.u2(endPc) + " -> " + Hex.u2(handlerPc) + " " + ((catchType == null) ? "<any>" : catchType.toHuman()));
        }
        offset += 8;
        length -= 8;
        if (observer != null) {
            observer.changeIndent(-1);
        }
    }
    catches.setImmutable();
    AttributeListParser parser = new AttributeListParser(cf, CTX_CODE, offset, this);
    parser.setObserver(observer);
    StdAttributeList attributes = parser.getList();
    attributes.setImmutable();
    int attributeByteCount = parser.getEndOffset() - offset;
    if (attributeByteCount != length) {
        return throwBadLength(attributeByteCount + (offset - origOffset));
    }
    return new AttCode(maxStack, maxLocals, code, catches, attributes);
}
Also used : BytecodeArray(com.android.dx.cf.code.BytecodeArray) StdAttributeList(com.android.dx.cf.iface.StdAttributeList) ByteCatchList(com.android.dx.cf.code.ByteCatchList) ConstantPool(com.android.dx.rop.cst.ConstantPool) CstType(com.android.dx.rop.cst.CstType) AttCode(com.android.dx.cf.attrib.AttCode) ByteArray(com.android.dx.util.ByteArray)

Example 13 with ConstantPool

use of com.android.dx.rop.cst.ConstantPool in project buck by facebook.

the class StdAttributeFactory method innerClasses.

/**
     * Parses an {@code InnerClasses} attribute.
     */
private Attribute innerClasses(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length < 2) {
        return throwSeverelyTruncated();
    }
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    // number_of_classes
    int count = bytes.getUnsignedShort(offset);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "number_of_classes: " + Hex.u2(count));
    }
    offset += 2;
    length -= 2;
    if (length != (count * 8)) {
        throwBadLength((count * 8) + 2);
    }
    InnerClassList list = new InnerClassList(count);
    for (int i = 0; i < count; i++) {
        int innerClassIdx = bytes.getUnsignedShort(offset);
        int outerClassIdx = bytes.getUnsignedShort(offset + 2);
        int nameIdx = bytes.getUnsignedShort(offset + 4);
        int accessFlags = bytes.getUnsignedShort(offset + 6);
        CstType innerClass = (CstType) pool.get(innerClassIdx);
        CstType outerClass = (CstType) pool.get0Ok(outerClassIdx);
        CstString name = (CstString) pool.get0Ok(nameIdx);
        list.set(i, innerClass, outerClass, name, accessFlags);
        if (observer != null) {
            observer.parsed(bytes, offset, 2, "inner_class: " + DirectClassFile.stringOrNone(innerClass));
            observer.parsed(bytes, offset + 2, 2, "  outer_class: " + DirectClassFile.stringOrNone(outerClass));
            observer.parsed(bytes, offset + 4, 2, "  name: " + DirectClassFile.stringOrNone(name));
            observer.parsed(bytes, offset + 6, 2, "  access_flags: " + AccessFlags.innerClassString(accessFlags));
        }
        offset += 8;
    }
    list.setImmutable();
    return new AttInnerClasses(list);
}
Also used : ConstantPool(com.android.dx.rop.cst.ConstantPool) CstType(com.android.dx.rop.cst.CstType) CstString(com.android.dx.rop.cst.CstString) ByteArray(com.android.dx.util.ByteArray) InnerClassList(com.android.dx.cf.attrib.InnerClassList) AttInnerClasses(com.android.dx.cf.attrib.AttInnerClasses)

Example 14 with ConstantPool

use of com.android.dx.rop.cst.ConstantPool in project buck by facebook.

the class StdAttributeFactory method constantValue.

/**
     * Parses a {@code ConstantValue} attribute.
     */
private Attribute constantValue(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length != 2) {
        return throwBadLength(2);
    }
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    int idx = bytes.getUnsignedShort(offset);
    TypedConstant cst = (TypedConstant) pool.get(idx);
    Attribute result = new AttConstantValue(cst);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "value: " + cst);
    }
    return result;
}
Also used : TypedConstant(com.android.dx.rop.cst.TypedConstant) Attribute(com.android.dx.cf.iface.Attribute) ConstantPool(com.android.dx.rop.cst.ConstantPool) ByteArray(com.android.dx.util.ByteArray) AttConstantValue(com.android.dx.cf.attrib.AttConstantValue)

Example 15 with ConstantPool

use of com.android.dx.rop.cst.ConstantPool in project buck by facebook.

the class CfTranslator method translate0.

/**
     * Performs the main act of translation. This method is separated
     * from {@link #translate} just to keep things a bit simpler in
     * terms of exception handling.
     *
     *
     * @param context
     * @param cf {@code non-null;} the class file
     * @param bytes {@code non-null;} contents of the file
     * @param cfOptions options for class translation
     * @param dexOptions options for dex output
     * @param dexFile {@code non-null;} dex output
     * @return {@code non-null;} the translated class
     */
private static ClassDefItem translate0(DxContext context, DirectClassFile cf, byte[] bytes, CfOptions cfOptions, DexOptions dexOptions, DexFile dexFile) {
    context.optimizerOptions.loadOptimizeLists(cfOptions.optimizeListFile, cfOptions.dontOptimizeListFile);
    // Build up a class to output.
    CstType thisClass = cf.getThisClass();
    int classAccessFlags = cf.getAccessFlags() & ~AccessFlags.ACC_SUPER;
    CstString sourceFile = (cfOptions.positionInfo == PositionList.NONE) ? null : cf.getSourceFile();
    ClassDefItem out = new ClassDefItem(thisClass, classAccessFlags, cf.getSuperclass(), cf.getInterfaces(), sourceFile);
    Annotations classAnnotations = AttributeTranslator.getClassAnnotations(cf, cfOptions);
    if (classAnnotations.size() != 0) {
        out.setClassAnnotations(classAnnotations, dexFile);
    }
    FieldIdsSection fieldIdsSection = dexFile.getFieldIds();
    MethodIdsSection methodIdsSection = dexFile.getMethodIds();
    processFields(cf, out, dexFile);
    processMethods(context, cf, cfOptions, dexOptions, out, dexFile);
    // intern constant pool method, field and type references
    ConstantPool constantPool = cf.getConstantPool();
    int constantPoolSize = constantPool.size();
    for (int i = 0; i < constantPoolSize; i++) {
        Constant constant = constantPool.getOrNull(i);
        if (constant instanceof CstMethodRef) {
            methodIdsSection.intern((CstBaseMethodRef) constant);
        } else if (constant instanceof CstInterfaceMethodRef) {
            methodIdsSection.intern(((CstInterfaceMethodRef) constant).toMethodRef());
        } else if (constant instanceof CstFieldRef) {
            fieldIdsSection.intern((CstFieldRef) constant);
        } else if (constant instanceof CstEnumRef) {
            fieldIdsSection.intern(((CstEnumRef) constant).getFieldRef());
        }
    }
    return out;
}
Also used : Constant(com.android.dx.rop.cst.Constant) TypedConstant(com.android.dx.rop.cst.TypedConstant) CstString(com.android.dx.rop.cst.CstString) CstFieldRef(com.android.dx.rop.cst.CstFieldRef) CstEnumRef(com.android.dx.rop.cst.CstEnumRef) FieldIdsSection(com.android.dx.dex.file.FieldIdsSection) MethodIdsSection(com.android.dx.dex.file.MethodIdsSection) CstMethodRef(com.android.dx.rop.cst.CstMethodRef) Annotations(com.android.dx.rop.annotation.Annotations) ClassDefItem(com.android.dx.dex.file.ClassDefItem) ConstantPool(com.android.dx.rop.cst.ConstantPool) CstType(com.android.dx.rop.cst.CstType) CstInterfaceMethodRef(com.android.dx.rop.cst.CstInterfaceMethodRef)

Aggregations

ConstantPool (com.android.dx.rop.cst.ConstantPool)20 ByteArray (com.android.dx.util.ByteArray)18 CstString (com.android.dx.rop.cst.CstString)12 Attribute (com.android.dx.cf.iface.Attribute)10 CstType (com.android.dx.rop.cst.CstType)8 ParseException (com.android.dx.cf.iface.ParseException)4 StdAttributeList (com.android.dx.cf.iface.StdAttributeList)4 CstNat (com.android.dx.rop.cst.CstNat)4 TypedConstant (com.android.dx.rop.cst.TypedConstant)4 AttCode (com.android.dx.cf.attrib.AttCode)2 AttConstantValue (com.android.dx.cf.attrib.AttConstantValue)2 AttEnclosingMethod (com.android.dx.cf.attrib.AttEnclosingMethod)2 AttInnerClasses (com.android.dx.cf.attrib.AttInnerClasses)2 AttSignature (com.android.dx.cf.attrib.AttSignature)2 AttSourceFile (com.android.dx.cf.attrib.AttSourceFile)2 InnerClassList (com.android.dx.cf.attrib.InnerClassList)2 RawAttribute (com.android.dx.cf.attrib.RawAttribute)2 ByteCatchList (com.android.dx.cf.code.ByteCatchList)2 BytecodeArray (com.android.dx.cf.code.BytecodeArray)2 Member (com.android.dx.cf.iface.Member)2