Search in sources :

Example 11 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 12 with CheckClassAdapter

use of org.objectweb.asm.util.CheckClassAdapter in project runelite by runelite.

the class JarUtil method writeClass.

public static byte[] writeClass(ClassGroup group, ClassFile cf) {
    ClassWriter writer = new NonloadingClassWriter(group, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    CheckClassAdapter cca = new CheckClassAdapter(writer, false);
    cf.accept(cca);
    byte[] data = writer.toByteArray();
    validateDataFlow(cf.getName(), data);
    return data;
}
Also used : NonloadingClassWriter(net.runelite.asm.objectwebasm.NonloadingClassWriter) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassWriter(org.objectweb.asm.ClassWriter) NonloadingClassWriter(net.runelite.asm.objectwebasm.NonloadingClassWriter)

Example 13 with CheckClassAdapter

use of org.objectweb.asm.util.CheckClassAdapter in project HeapDL by plast-lab.

the class Transformer method checkBytecode.

// Check bytecode using ASM's CheckClassAdapter.
private void checkBytecode(ClassLoader loader, String className, byte[] bc, int wFlags) {
    ClassReader debugReader = new ClassReader(bc);
    ClassWriter debugWriter = new ContextClassWriter(loader, wFlags);
    try {
        ClassNode debugNode = new ClassNode(Opcodes.ASM5);
        debugReader.accept(debugNode, ClassReader.EXPAND_FRAMES);
        debugNode.accept(new CheckClassAdapter(debugWriter));
    // debugReader.accept(new CheckClassAdapter(debugWriter), 0);
    } catch (RuntimeException ex) {
        System.err.println("Bytecode check failed for " + className + ":");
        ex.printStackTrace();
        System.exit(-1);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter)

Example 14 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 15 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)

Aggregations

CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)18 ClassReader (org.objectweb.asm.ClassReader)13 ClassWriter (org.objectweb.asm.ClassWriter)9 IOException (java.io.IOException)8 ClassNode (org.objectweb.asm.tree.ClassNode)8 ClassVisitor (org.objectweb.asm.ClassVisitor)6 InputStream (java.io.InputStream)4 MethodNode (org.objectweb.asm.tree.MethodNode)4 Analyzer (org.objectweb.asm.tree.analysis.Analyzer)4 File (java.io.File)3 TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)3 FileInputStream (java.io.FileInputStream)2 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 NonloadingClassWriter (net.runelite.asm.objectwebasm.NonloadingClassWriter)2 BuildException (org.apache.tools.ant.BuildException)2 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)2 BasicVerifier (org.objectweb.asm.tree.analysis.BasicVerifier)2 SimpleVerifier (org.objectweb.asm.tree.analysis.SimpleVerifier)2