Search in sources :

Example 1 with ByteArray

use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.

the class ConstantPoolParser method parseUtf8.

/**
     * Parses a utf8 constant.
     *
     * @param at offset to the start of the constant (where the tag byte is)
     * @return {@code non-null;} the parsed value
     */
private CstString parseUtf8(int at) {
    int length = bytes.getUnsignedShort(at + 1);
    // Skip to the data.
    at += 3;
    ByteArray ubytes = bytes.slice(at, at + length);
    try {
        return new CstString(ubytes);
    } catch (IllegalArgumentException ex) {
        // Translate the exception
        throw new ParseException(ex);
    }
}
Also used : ByteArray(com.taobao.android.dx.util.ByteArray) ParseException(com.taobao.android.dx.cf.iface.ParseException)

Example 2 with ByteArray

use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.

the class AttributeFactory method parse.

/**
     * Parses and makes an attribute based on the bytes at the
     * indicated position in the given array. This method figures out
     * the name, and then does all the setup to call on to {@link #parse0},
     * which does the actual construction.
     *
     * @param cf {@code non-null;} class file to parse from
     * @param context context to parse in; one of the {@code CTX_*}
     * constants
     * @param offset offset into {@code dcf}'s {@code bytes}
     * to start parsing at
     * @param observer {@code null-ok;} parse observer to report to, if any
     * @return {@code non-null;} an appropriately-constructed {@link Attribute}
     */
public final Attribute parse(DirectClassFile cf, int context, int offset, ParseObserver observer) {
    if (cf == null) {
        throw new NullPointerException("cf == null");
    }
    if ((context < 0) || (context >= CTX_COUNT)) {
        throw new IllegalArgumentException("bad context");
    }
    CstString name = null;
    try {
        ByteArray bytes = cf.getBytes();
        ConstantPool pool = cf.getConstantPool();
        int nameIdx = bytes.getUnsignedShort(offset);
        int length = bytes.getInt(offset + 2);
        name = (CstString) pool.get(nameIdx);
        if (observer != null) {
            observer.parsed(bytes, offset, 2, "name: " + name.toHuman());
            observer.parsed(bytes, offset + 2, 4, "length: " + Hex.u4(length));
        }
        return parse0(cf, context, name.getString(), offset + 6, length, observer);
    } catch (ParseException ex) {
        ex.addContext("...while parsing " + ((name != null) ? (name.toHuman() + " ") : "") + "attribute at offset " + Hex.u4(offset));
        throw ex;
    }
}
Also used : ConstantPool(com.taobao.android.dx.rop.cst.ConstantPool) CstString(com.taobao.android.dx.rop.cst.CstString) ByteArray(com.taobao.android.dx.util.ByteArray) ParseException(com.taobao.android.dx.cf.iface.ParseException)

Example 3 with ByteArray

use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.

the class AnnotationLister method process.

/** Processes based on configuration specified in constructor. */
void process() {
    for (String path : args.files) {
        ClassPathOpener opener;
        opener = new ClassPathOpener(path, true, new ClassPathOpener.Consumer() {

            public boolean processFileBytes(String name, long lastModified, byte[] bytes) {
                if (!name.endsWith(".class")) {
                    return true;
                }
                ByteArray ba = new ByteArray(bytes);
                DirectClassFile cf = new DirectClassFile(ba, name, true);
                cf.setAttributeFactory(StdAttributeFactory.THE_ONE);
                AttributeList attributes = cf.getAttributes();
                Attribute att;
                String cfClassName = cf.getThisClass().getClassType().getClassName();
                if (cfClassName.endsWith(PACKAGE_INFO)) {
                    att = attributes.findFirst(AttRuntimeInvisibleAnnotations.ATTRIBUTE_NAME);
                    for (; att != null; att = attributes.findNext(att)) {
                        BaseAnnotations ann = (BaseAnnotations) att;
                        visitPackageAnnotation(cf, ann);
                    }
                    att = attributes.findFirst(AttRuntimeVisibleAnnotations.ATTRIBUTE_NAME);
                    for (; att != null; att = attributes.findNext(att)) {
                        BaseAnnotations ann = (BaseAnnotations) att;
                        visitPackageAnnotation(cf, ann);
                    }
                } else if (isMatchingInnerClass(cfClassName) || isMatchingPackage(cfClassName)) {
                    printMatch(cf);
                } else {
                    att = attributes.findFirst(AttRuntimeInvisibleAnnotations.ATTRIBUTE_NAME);
                    for (; att != null; att = attributes.findNext(att)) {
                        BaseAnnotations ann = (BaseAnnotations) att;
                        visitClassAnnotation(cf, ann);
                    }
                    att = attributes.findFirst(AttRuntimeVisibleAnnotations.ATTRIBUTE_NAME);
                    for (; att != null; att = attributes.findNext(att)) {
                        BaseAnnotations ann = (BaseAnnotations) att;
                        visitClassAnnotation(cf, ann);
                    }
                }
                return true;
            }

            public void onException(Exception ex) {
                throw new RuntimeException(ex);
            }

            public void onProcessArchiveStart(File file) {
            }
        });
        opener.process();
    }
}
Also used : ClassPathOpener(com.taobao.android.dx.cf.direct.ClassPathOpener) DirectClassFile(com.taobao.android.dx.cf.direct.DirectClassFile) Attribute(com.taobao.android.dx.cf.iface.Attribute) AttributeList(com.taobao.android.dx.cf.iface.AttributeList) BaseAnnotations(com.taobao.android.dx.cf.attrib.BaseAnnotations) ByteArray(com.taobao.android.dx.util.ByteArray) DirectClassFile(com.taobao.android.dx.cf.direct.DirectClassFile) File(java.io.File)

Example 4 with ByteArray

use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.

the class BlockDumper method ropDump.

/**
     * Does a registerizing dump.
     *
     * @param meth {@code non-null;} method data to dump
     */
private void ropDump(ConcreteMethod meth) {
    TranslationAdvice advice = DexTranslationAdvice.THE_ONE;
    BytecodeArray code = meth.getCode();
    ByteArray bytes = code.getBytes();
    RopMethod rmeth = Ropper.convert(meth, advice, classFile.getMethods());
    StringBuffer sb = new StringBuffer(2000);
    if (optimize) {
        boolean isStatic = AccessFlags.isStatic(meth.getAccessFlags());
        int paramWidth = computeParamWidth(meth, isStatic);
        rmeth = new Optimizer().optimize(rmeth, paramWidth, isStatic, true, advice);
    }
    BasicBlockList blocks = rmeth.getBlocks();
    int[] order = blocks.getLabelsInOrder();
    sb.append("first " + Hex.u2(rmeth.getFirstLabel()) + "\n");
    for (int label : order) {
        BasicBlock bb = blocks.get(blocks.indexOfLabel(label));
        sb.append("block ");
        sb.append(Hex.u2(label));
        sb.append("\n");
        IntList preds = rmeth.labelToPredecessors(label);
        int psz = preds.size();
        for (int i = 0; i < psz; i++) {
            sb.append("  pred ");
            sb.append(Hex.u2(preds.get(i)));
            sb.append("\n");
        }
        InsnList il = bb.getInsns();
        int ilsz = il.size();
        for (int i = 0; i < ilsz; i++) {
            Insn one = il.get(i);
            sb.append("  ");
            sb.append(il.get(i).toHuman());
            sb.append("\n");
        }
        IntList successors = bb.getSuccessors();
        int ssz = successors.size();
        if (ssz == 0) {
            sb.append("  returns\n");
        } else {
            int primary = bb.getPrimarySuccessor();
            for (int i = 0; i < ssz; i++) {
                int succ = successors.get(i);
                sb.append("  next ");
                sb.append(Hex.u2(succ));
                if ((ssz != 1) && (succ == primary)) {
                    sb.append(" *");
                }
                sb.append("\n");
            }
        }
    }
    suppressDump = false;
    setAt(bytes, 0);
    parsed(bytes, 0, bytes.size(), sb.toString());
    suppressDump = true;
}
Also used : BytecodeArray(com.taobao.android.dx.cf.code.BytecodeArray) Insn(com.taobao.android.dx.rop.code.Insn) RopMethod(com.taobao.android.dx.rop.code.RopMethod) Optimizer(com.taobao.android.dx.ssa.Optimizer) BasicBlock(com.taobao.android.dx.rop.code.BasicBlock) TranslationAdvice(com.taobao.android.dx.rop.code.TranslationAdvice) DexTranslationAdvice(com.taobao.android.dx.rop.code.DexTranslationAdvice) InsnList(com.taobao.android.dx.rop.code.InsnList) IntList(com.taobao.android.dx.util.IntList) ByteArray(com.taobao.android.dx.util.ByteArray) BasicBlockList(com.taobao.android.dx.rop.code.BasicBlockList)

Example 5 with ByteArray

use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.

the class BlockDumper method regularDump.

/**
     * Does a regular basic block dump.
     *
     * @param meth {@code non-null;} method data to dump
     */
private void regularDump(ConcreteMethod meth) {
    BytecodeArray code = meth.getCode();
    ByteArray bytes = code.getBytes();
    ByteBlockList list = BasicBlocker.identifyBlocks(meth);
    int sz = list.size();
    CodeObserver codeObserver = new CodeObserver(bytes, BlockDumper.this);
    // Reset the dump cursor to the start of the bytecode.
    setAt(bytes, 0);
    suppressDump = false;
    int byteAt = 0;
    for (int i = 0; i < sz; i++) {
        ByteBlock bb = list.get(i);
        int start = bb.getStart();
        int end = bb.getEnd();
        if (byteAt < start) {
            parsed(bytes, byteAt, start - byteAt, "dead code " + Hex.u2(byteAt) + ".." + Hex.u2(start));
        }
        parsed(bytes, start, 0, "block " + Hex.u2(bb.getLabel()) + ": " + Hex.u2(start) + ".." + Hex.u2(end));
        changeIndent(1);
        int len;
        for (int j = start; j < end; j += len) {
            len = code.parseInstruction(j, codeObserver);
            codeObserver.setPreviousOffset(j);
        }
        IntList successors = bb.getSuccessors();
        int ssz = successors.size();
        if (ssz == 0) {
            parsed(bytes, end, 0, "returns");
        } else {
            for (int j = 0; j < ssz; j++) {
                int succ = successors.get(j);
                parsed(bytes, end, 0, "next " + Hex.u2(succ));
            }
        }
        ByteCatchList catches = bb.getCatches();
        int csz = catches.size();
        for (int j = 0; j < csz; j++) {
            ByteCatchList.Item one = catches.get(j);
            CstType exceptionClass = one.getExceptionClass();
            parsed(bytes, end, 0, "catch " + ((exceptionClass == CstType.OBJECT) ? "<any>" : exceptionClass.toHuman()) + " -> " + Hex.u2(one.getHandlerPc()));
        }
        changeIndent(-1);
        byteAt = end;
    }
    int end = bytes.size();
    if (byteAt < end) {
        parsed(bytes, byteAt, end - byteAt, "dead code " + Hex.u2(byteAt) + ".." + Hex.u2(end));
    }
    suppressDump = true;
}
Also used : BytecodeArray(com.taobao.android.dx.cf.code.BytecodeArray) ByteCatchList(com.taobao.android.dx.cf.code.ByteCatchList) ByteBlock(com.taobao.android.dx.cf.code.ByteBlock) CstType(com.taobao.android.dx.rop.cst.CstType) ByteArray(com.taobao.android.dx.util.ByteArray) CodeObserver(com.taobao.android.dx.cf.direct.CodeObserver) ByteBlockList(com.taobao.android.dx.cf.code.ByteBlockList) IntList(com.taobao.android.dx.util.IntList)

Aggregations

ByteArray (com.taobao.android.dx.util.ByteArray)23 ConstantPool (com.taobao.android.dx.rop.cst.ConstantPool)9 Attribute (com.taobao.android.dx.cf.iface.Attribute)7 CstString (com.taobao.android.dx.rop.cst.CstString)6 DirectClassFile (com.taobao.android.dx.cf.direct.DirectClassFile)4 ParseException (com.taobao.android.dx.cf.iface.ParseException)4 CstType (com.taobao.android.dx.rop.cst.CstType)4 BytecodeArray (com.taobao.android.dx.cf.code.BytecodeArray)3 LocalVariableList (com.taobao.android.dx.cf.code.LocalVariableList)3 ByteCatchList (com.taobao.android.dx.cf.code.ByteCatchList)2 StdAttributeList (com.taobao.android.dx.cf.iface.StdAttributeList)2 CstNat (com.taobao.android.dx.rop.cst.CstNat)2 IntList (com.taobao.android.dx.util.IntList)2 AttCode (com.taobao.android.dx.cf.attrib.AttCode)1 AttConstantValue (com.taobao.android.dx.cf.attrib.AttConstantValue)1 AttEnclosingMethod (com.taobao.android.dx.cf.attrib.AttEnclosingMethod)1 AttExceptions (com.taobao.android.dx.cf.attrib.AttExceptions)1 AttInnerClasses (com.taobao.android.dx.cf.attrib.AttInnerClasses)1 AttLineNumberTable (com.taobao.android.dx.cf.attrib.AttLineNumberTable)1 AttLocalVariableTable (com.taobao.android.dx.cf.attrib.AttLocalVariableTable)1