Search in sources :

Example 1 with DexException

use of com.googlecode.d2j.DexException in project dex2jar by pxb1988.

the class TestUtils method verify.

@SuppressWarnings("rawtypes")
public static void verify(final ClassReader cr, PrintWriter out) throws AnalyzerException, IllegalArgumentException, IllegalAccessException {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);
    List methods = cn.methods;
    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = (MethodNode) methods.get(i);
        List tryCatchBlocks = method.tryCatchBlocks;
        for (int j = 0; j < tryCatchBlocks.size(); j++) {
            TryCatchBlockNode tcn = (TryCatchBlockNode) tryCatchBlocks.get(j);
            if (tcn.start.equals(tcn.end)) {
                throw new DexException("try/catch block %d in %s has same start(%s) and end(%s)", j, method.name, tcn.start.getLabel(), tcn.end.getLabel());
            }
        }
        BasicVerifier verifier = new BasicVerifier();
        Analyzer a = new Analyzer(verifier);
        try {
            a.analyze(cn.name, method);
        } catch (Exception e) {
            out.println(cr.getClassName() + "." + method.name + method.desc);
            printAnalyzerResult(method, a, out);
            e.printStackTrace(out);
            out.flush();
            throw new DexException("method " + method.name + " " + method.desc, e);
        }
    }
}
Also used : BasicVerifier(org.objectweb.asm.tree.analysis.BasicVerifier) ClassNode(org.objectweb.asm.tree.ClassNode) DexClassNode(com.googlecode.d2j.node.DexClassNode) TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) DexException(com.googlecode.d2j.DexException) DexMethodNode(com.googlecode.d2j.node.DexMethodNode) MethodNode(org.objectweb.asm.tree.MethodNode) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) List(java.util.List) ArrayList(java.util.ArrayList) Analyzer(org.objectweb.asm.tree.analysis.Analyzer) AnalyzerException(org.objectweb.asm.tree.analysis.AnalyzerException) DexException(com.googlecode.d2j.DexException) ZipException(java.util.zip.ZipException)

Example 2 with DexException

use of com.googlecode.d2j.DexException in project dex2jar by pxb1988.

the class ExDex2Asm method convertCode.

@Override
public void convertCode(DexMethodNode methodNode, MethodVisitor mv) {
    if (!AsmBridge.isMethodWriter(mv)) {
        throw new RuntimeException("We use a MethodWriter tricky here!");
    }
    MethodNode mn = new MethodNode(Opcodes.ASM4, methodNode.access, methodNode.method.getName(), methodNode.method.getDesc(), null, null);
    try {
        super.convertCode(methodNode, mn);
    } catch (Exception ex) {
        if (exceptionHandler == null) {
            throw new DexException(ex, "fail convert code for %s", methodNode.method);
        } else {
            mn.instructions.clear();
            mn.tryCatchBlocks.clear();
            exceptionHandler.handleMethodTranslateException(methodNode.method, methodNode, mn, ex);
        }
    }
    // code convert ok, copy to MethodWriter and check for Size
    mn.accept(mv);
    try {
        AsmBridge.sizeOfMethodWriter(mv);
    } catch (Exception ex) {
        mn.instructions.clear();
        mn.tryCatchBlocks.clear();
        exceptionHandler.handleMethodTranslateException(methodNode.method, methodNode, mn, ex);
        AsmBridge.replaceMethodWriter(mv, mn);
    }
}
Also used : DexException(com.googlecode.d2j.DexException) MethodNode(org.objectweb.asm.tree.MethodNode) DexMethodNode(com.googlecode.d2j.node.DexMethodNode) DexException(com.googlecode.d2j.DexException)

Example 3 with DexException

use of com.googlecode.d2j.DexException in project dex2jar by pxb1988.

the class TestUtils method translateAndCheck.

public static byte[] translateAndCheck(DexFileNode fileNode, DexClassNode clzNode) throws AnalyzerException, IllegalAccessException {
    // 1. convert to .class
    Dex2Asm dex2Asm = new Dex2Asm() {

        @Override
        public void convertCode(DexMethodNode methodNode, MethodVisitor mv) {
            try {
                super.convertCode(methodNode, mv);
            } catch (Exception ex) {
                BaksmaliDumper d = new BaksmaliDumper();
                try {
                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.err, "UTF-8"));
                    d.baksmaliMethod(methodNode, out);
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                throw new DexException(ex, "fail convert code %s", methodNode.method);
            }
        }
    };
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    ClassVisitorFactory cvf = new ClassVisitorFactory() {

        @Override
        public ClassVisitor create(String classInternalName) {
            return cw;
        }
    };
    if (fileNode != null) {
        dex2Asm.convertClass(clzNode, cvf, fileNode);
    } else {
        dex2Asm.convertClass(clzNode, cvf);
    }
    byte[] data = cw.toByteArray();
    // 2. verify .class
    ClassReader cr = new ClassReader(data);
    TestUtils.verify(cr);
    // 3. convert back to dex
    CfOptions cfOptions = new CfOptions();
    cfOptions.strictNameCheck = false;
    DexOptions dexOptions = new DexOptions();
    DirectClassFile dcf = new DirectClassFile(data, clzNode.className.substring(1, clzNode.className.length() - 1) + ".class", true);
    dcf.setAttributeFactory(new StdAttributeFactory());
    com.android.dx.dex.file.DexFile dxFile = new com.android.dx.dex.file.DexFile(dexOptions);
    CfTranslator.translate(dcf, data, cfOptions, dexOptions, dxFile);
    return data;
}
Also used : DexException(com.googlecode.d2j.DexException) BaksmaliDumper(com.googlecode.d2j.smali.BaksmaliDumper) StdAttributeFactory(com.android.dx.cf.direct.StdAttributeFactory) MethodVisitor(org.objectweb.asm.MethodVisitor) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) Dex2Asm(com.googlecode.d2j.dex.Dex2Asm) DexMethodNode(com.googlecode.d2j.node.DexMethodNode) DexOptions(com.android.dx.dex.DexOptions) AnalyzerException(org.objectweb.asm.tree.analysis.AnalyzerException) DexException(com.googlecode.d2j.DexException) ZipException(java.util.zip.ZipException) ClassWriter(org.objectweb.asm.ClassWriter) DirectClassFile(com.android.dx.cf.direct.DirectClassFile) ClassReader(org.objectweb.asm.ClassReader) ClassVisitorFactory(com.googlecode.d2j.dex.ClassVisitorFactory) CfOptions(com.android.dx.dex.cf.CfOptions)

Aggregations

DexException (com.googlecode.d2j.DexException)3 DexMethodNode (com.googlecode.d2j.node.DexMethodNode)3 ZipException (java.util.zip.ZipException)2 MethodNode (org.objectweb.asm.tree.MethodNode)2 AnalyzerException (org.objectweb.asm.tree.analysis.AnalyzerException)2 DirectClassFile (com.android.dx.cf.direct.DirectClassFile)1 StdAttributeFactory (com.android.dx.cf.direct.StdAttributeFactory)1 DexOptions (com.android.dx.dex.DexOptions)1 CfOptions (com.android.dx.dex.cf.CfOptions)1 ClassVisitorFactory (com.googlecode.d2j.dex.ClassVisitorFactory)1 Dex2Asm (com.googlecode.d2j.dex.Dex2Asm)1 DexClassNode (com.googlecode.d2j.node.DexClassNode)1 BaksmaliDumper (com.googlecode.d2j.smali.BaksmaliDumper)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ClassReader (org.objectweb.asm.ClassReader)1 ClassWriter (org.objectweb.asm.ClassWriter)1 MethodVisitor (org.objectweb.asm.MethodVisitor)1 ClassNode (org.objectweb.asm.tree.ClassNode)1 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)1