Search in sources :

Example 1 with BasicVerifier

use of org.objectweb.asm.tree.analysis.BasicVerifier 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) ParseException(com.android.dx.cf.iface.ParseException)

Example 2 with BasicVerifier

use of org.objectweb.asm.tree.analysis.BasicVerifier in project dex2jar by pxb1988.

the class AsmVerify method doCommandLine.

@Override
protected void doCommandLine() throws Exception {
    if (remainingArgs.length < 1) {
        usage();
        return;
    }
    List<Path> files = new ArrayList<>();
    for (String fn : remainingArgs) {
        Path file = new File(fn).toPath();
        if (!Files.exists(file)) {
            System.err.println(fn + " doesn't exist");
            usage();
            return;
        }
        files.add(file);
    }
    for (Path file : files) {
        System.out.println("verify " + file);
        walkJarOrDir(file, new FileVisitorX() {

            @Override
            public void visitFile(Path file, String relative) throws IOException {
                if (file.getFileName().toString().endsWith(".class")) {
                    ClassReader cr = new ClassReader(Files.readAllBytes(file));
                    ClassNode cn = new ClassNode();
                    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG | ClassReader.EXPAND_FRAMES | ClassReader.SKIP_FRAMES);
                    for (MethodNode method : cn.methods) {
                        BasicVerifier verifier = new BasicVerifier();
                        Analyzer<BasicValue> a = new Analyzer<>(verifier);
                        try {
                            a.analyze(cn.name, method);
                        } catch (Exception ex) {
                            System.err.println("Error verify method " + cr.getClassName() + "." + method.name + " " + method.desc);
                            if (detail) {
                                ex.printStackTrace(System.err);
                                printAnalyzerResult(method, a, new PrintWriter(new OutputStreamWriter(System.err, StandardCharsets.UTF_8)));
                            }
                        }
                    }
                }
            }
        });
    }
}
Also used : Path(java.nio.file.Path) BasicVerifier(org.objectweb.asm.tree.analysis.BasicVerifier) ClassNode(org.objectweb.asm.tree.ClassNode) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Analyzer(org.objectweb.asm.tree.analysis.Analyzer) IOException(java.io.IOException) MethodNode(org.objectweb.asm.tree.MethodNode) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

ArrayList (java.util.ArrayList)2 ClassNode (org.objectweb.asm.tree.ClassNode)2 MethodNode (org.objectweb.asm.tree.MethodNode)2 Analyzer (org.objectweb.asm.tree.analysis.Analyzer)2 BasicVerifier (org.objectweb.asm.tree.analysis.BasicVerifier)2 CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)2 ParseException (com.android.dx.cf.iface.ParseException)1 DexException (com.googlecode.d2j.DexException)1 DexClassNode (com.googlecode.d2j.node.DexClassNode)1 DexMethodNode (com.googlecode.d2j.node.DexMethodNode)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Path (java.nio.file.Path)1 List (java.util.List)1 ZipException (java.util.zip.ZipException)1 ClassReader (org.objectweb.asm.ClassReader)1 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)1 AnalyzerException (org.objectweb.asm.tree.analysis.AnalyzerException)1