Search in sources :

Example 1 with SimpleVerifier

use of org.objectweb.asm.tree.analysis.SimpleVerifier in project groovy by apache.

the class VerifyClass method readClass.

private boolean readClass(String clazz) throws IOException {
    ClassReader cr = new ClassReader(new FileInputStream(clazz));
    ClassNode ca = new ClassNode() {

        public void visitEnd() {
        //accept(cv);
        }
    };
    cr.accept(new CheckClassAdapter(ca), ClassWriter.COMPUTE_MAXS);
    boolean failed = false;
    List methods = ca.methods;
    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = (MethodNode) methods.get(i);
        if (method.instructions.size() > 0) {
            Analyzer a = new Analyzer(new SimpleVerifier());
            try {
                a.analyze(ca.name, method);
                continue;
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (!failed) {
                failed = true;
                log("verifying of class " + clazz + " failed");
            }
            if (verbose)
                log(method.name + method.desc);
            TraceMethodVisitor mv = new TraceMethodVisitor(null);
            /*= new TraceMethodVisitor(null) {
                    public void visitMaxs(int maxStack, int maxLocals) {
                        StringBuffer buffer = new StringBuffer();
                        for (int i = 0; i < text.size(); ++i) {
                            String s = frames[i] == null ? "null" : frames[i].toString();
                            while (s.length() < maxStack + maxLocals + 1) {
                                s += " ";
                            }
                            buffer.append(Integer.toString(i + 100000).substring(1));
                            buffer.append(" ");
                            buffer.append(s);
                            buffer.append(" : ");
                            buffer.append(text.get(i));
                        }
                        if (verbose) log(buffer.toString());
                    }
                };*/
            for (int j = 0; j < method.instructions.size(); ++j) {
                Object insn = method.instructions.get(j);
                if (insn instanceof AbstractInsnNode) {
                    ((AbstractInsnNode) insn).accept(mv);
                } else {
                    mv.visitLabel((Label) insn);
                }
            }
            mv.visitMaxs(method.maxStack, method.maxLocals);
        }
    }
    return !failed;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) SimpleVerifier(org.objectweb.asm.tree.analysis.SimpleVerifier) Analyzer(org.objectweb.asm.tree.analysis.Analyzer) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) FileInputStream(java.io.FileInputStream) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) MethodNode(org.objectweb.asm.tree.MethodNode) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) List(java.util.List)

Example 2 with SimpleVerifier

use of org.objectweb.asm.tree.analysis.SimpleVerifier in project bytecode-viewer by Konloch.

the class CheckClassAdapter method verify.

/**
     * Checks a given class.
     * 
     * @param cr
     *            a <code>ClassReader</code> that contains bytecode for the
     *            analysis.
     * @param loader
     *            a <code>ClassLoader</code> which will be used to load
     *            referenced classes. This is useful if you are verifiying
     *            multiple interdependent classes.
     * @param dump
     *            true if bytecode should be printed out not only when errors
     *            are found.
     * @param pw
     *            write where results going to be printed
     */
public static void verify(final ClassReader cr, final ClassLoader loader, final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);
    Type syperType = cn.superName == null ? null : Type.getObjectType(cn.superName);
    List<MethodNode> methods = cn.methods;
    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext(); ) {
        interfaces.add(Type.getObjectType(i.next()));
    }
    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(Type.getObjectType(cn.name), syperType, interfaces, (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) SimpleVerifier(org.objectweb.asm.tree.analysis.SimpleVerifier) Analyzer(org.objectweb.asm.tree.analysis.Analyzer) BasicValue(org.objectweb.asm.tree.analysis.BasicValue) MethodNode(org.objectweb.asm.tree.MethodNode)

Example 3 with SimpleVerifier

use of org.objectweb.asm.tree.analysis.SimpleVerifier in project groovy-core by groovy.

the class VerifyClass method readClass.

private boolean readClass(String clazz) throws IOException {
    ClassReader cr = new ClassReader(new FileInputStream(clazz));
    ClassNode ca = new ClassNode() {

        public void visitEnd() {
        //accept(cv);
        }
    };
    cr.accept(new CheckClassAdapter(ca), ClassWriter.COMPUTE_MAXS);
    boolean failed = false;
    List methods = ca.methods;
    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = (MethodNode) methods.get(i);
        if (method.instructions.size() > 0) {
            Analyzer a = new Analyzer(new SimpleVerifier());
            try {
                a.analyze(ca.name, method);
                continue;
            } catch (Exception e) {
                e.printStackTrace();
            }
            final Frame[] frames = a.getFrames();
            if (!failed) {
                failed = true;
                log("verifying of class " + clazz + " failed");
            }
            if (verbose)
                log(method.name + method.desc);
            TraceMethodVisitor mv = new TraceMethodVisitor(null);
            /*= new TraceMethodVisitor(null) {
                    public void visitMaxs(int maxStack, int maxLocals) {
                        StringBuffer buffer = new StringBuffer();
                        for (int i = 0; i < text.size(); ++i) {
                            String s = frames[i] == null ? "null" : frames[i].toString();
                            while (s.length() < maxStack + maxLocals + 1) {
                                s += " ";
                            }
                            buffer.append(Integer.toString(i + 100000).substring(1));
                            buffer.append(" ");
                            buffer.append(s);
                            buffer.append(" : ");
                            buffer.append(text.get(i));
                        }
                        if (verbose) log(buffer.toString());
                    }
                };*/
            for (int j = 0; j < method.instructions.size(); ++j) {
                Object insn = method.instructions.get(j);
                if (insn instanceof AbstractInsnNode) {
                    ((AbstractInsnNode) insn).accept(mv);
                } else {
                    mv.visitLabel((Label) insn);
                }
            }
            mv.visitMaxs(method.maxStack, method.maxLocals);
        }
    }
    return !failed;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) Frame(org.objectweb.asm.tree.analysis.Frame) SimpleVerifier(org.objectweb.asm.tree.analysis.SimpleVerifier) Analyzer(org.objectweb.asm.tree.analysis.Analyzer) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) FileInputStream(java.io.FileInputStream) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) MethodNode(org.objectweb.asm.tree.MethodNode) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) List(java.util.List)

Aggregations

ClassNode (org.objectweb.asm.tree.ClassNode)3 MethodNode (org.objectweb.asm.tree.MethodNode)3 Analyzer (org.objectweb.asm.tree.analysis.Analyzer)3 SimpleVerifier (org.objectweb.asm.tree.analysis.SimpleVerifier)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 List (java.util.List)2 BuildException (org.apache.tools.ant.BuildException)2 ClassReader (org.objectweb.asm.ClassReader)2 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)2 CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)2 TraceMethodVisitor (org.objectweb.asm.util.TraceMethodVisitor)2 BasicValue (org.objectweb.asm.tree.analysis.BasicValue)1 Frame (org.objectweb.asm.tree.analysis.Frame)1