Search in sources :

Example 6 with CheckClassAdapter

use of org.objectweb.asm.util.CheckClassAdapter 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)

Example 7 with CheckClassAdapter

use of org.objectweb.asm.util.CheckClassAdapter 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 + " is not exists");
            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)

Example 8 with CheckClassAdapter

use of org.objectweb.asm.util.CheckClassAdapter in project RecurrentComplex by Ivorforce.

the class VanillaGenerationClassFactory method createClassBinary.

protected byte[] createClassBinary(String className) {
    ClassWriter writer = new ClassWriter(0);
    ClassVisitor cw = new CheckClassAdapter(writer);
    cw.visit(V1_5, ACC_PUBLIC, className, null, Type.getInternalName(GenericVillagePiece.class), null);
    {
        // empty constructor
        String descriptor = Type.getMethodDescriptor(Type.VOID_TYPE);
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(GenericVillagePiece.class), "<init>", descriptor, false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    {
        // initial constructor
        String descriptor = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(StructureVillagePieces.Start.class), Type.INT_TYPE);
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ILOAD, 2);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(GenericVillagePiece.class), "<init>", descriptor, false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(4, 3);
        mv.visitEnd();
    }
    cw.visitEnd();
    return writer.toByteArray();
}
Also used : StructureVillagePieces(net.minecraft.world.gen.structure.StructureVillagePieces) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 9 with CheckClassAdapter

use of org.objectweb.asm.util.CheckClassAdapter in project slab by RichardWarburton.

the class BytecodeGenerator method generate.

@SuppressWarnings("unchecked")
public Class<T> generate() {
    ClassWriter out = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    CheckClassAdapter writer = new CheckClassAdapter(out);
    int offset = 0;
    declareClass(writer);
    declareConstructor(writer);
    for (Method getter : inspector.getters) {
        offset = declareField(getter, writer, offset);
    }
    writer.visitEnd();
    return (Class<T>) new GeneratedClassLoader(options).defineClass(implementationName, out);
}
Also used : CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) Method(java.lang.reflect.Method) ClassWriter(org.objectweb.asm.ClassWriter)

Example 10 with CheckClassAdapter

use of org.objectweb.asm.util.CheckClassAdapter in project cdap by caskdata.

the class Debugs method debugByteCode.

public static void debugByteCode(ClassDefinition classDefinition, PrintWriter writer) {
    ClassReader reader = new ClassReader(classDefinition.getBytecode());
    reader.accept(new CheckClassAdapter(new TraceClassVisitor(writer)), 0);
    File file = new File("/tmp/" + classDefinition.getInternalName() + ".class");
    file.getParentFile().mkdirs();
    writer.println(file);
    writer.flush();
    try {
        ByteStreams.write(classDefinition.getBytecode(), Files.newOutputStreamSupplier(file));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException) File(java.io.File)

Aggregations

CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)10 ClassReader (org.objectweb.asm.ClassReader)7 ClassNode (org.objectweb.asm.tree.ClassNode)6 IOException (java.io.IOException)5 MethodNode (org.objectweb.asm.tree.MethodNode)4 Analyzer (org.objectweb.asm.tree.analysis.Analyzer)4 List (java.util.List)3 ClassWriter (org.objectweb.asm.ClassWriter)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 BuildException (org.apache.tools.ant.BuildException)2 ClassVisitor (org.objectweb.asm.ClassVisitor)2 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)2 BasicVerifier (org.objectweb.asm.tree.analysis.BasicVerifier)2 SimpleVerifier (org.objectweb.asm.tree.analysis.SimpleVerifier)2 TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)2 TraceMethodVisitor (org.objectweb.asm.util.TraceMethodVisitor)2 VisibleForTesting (co.paralleluniverse.common.util.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1