Search in sources :

Example 1 with CheckClassAdapter

use of org.objectweb.asm.util.CheckClassAdapter in project felix by apache.

the class Manipulator method manipulate.

/**
 * Manipulate the given byte array.
 * @param origin : original class.
 * @return the manipulated class, if the class is already manipulated, the original class.
 * @throws IOException : if an error occurs during the manipulation.
 */
public byte[] manipulate(byte[] origin) throws IOException {
    if (!m_alreadyManipulated) {
        InputStream is2 = new ByteArrayInputStream(origin);
        ClassReader reader = new ClassReader(is2);
        ClassWriter writer = new ClassLoaderAwareClassWriter(ClassWriter.COMPUTE_FRAMES, m_className, m_superClass, m_classLoader);
        ClassManipulator process = new ClassManipulator(new CheckClassAdapter(writer, false), this);
        if (m_version >= Opcodes.V1_6) {
            reader.accept(process, ClassReader.EXPAND_FRAMES);
        } else {
            reader.accept(process, 0);
        }
        is2.close();
        return writer.toByteArray();
    } else {
        return origin;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Example 2 with CheckClassAdapter

use of org.objectweb.asm.util.CheckClassAdapter in project evosuite by EvoSuite.

the class TracingTestRunner method loadClass.

/**
 * {@inheritDoc}
 */
@Override
public Class<?> loadClass(String fullyQualifiedTargetClass) throws ClassNotFoundException {
    if (isSystemClass(fullyQualifiedTargetClass)) {
        logger.info("Not instrumenting class '{}'.", fullyQualifiedTargetClass);
        return super.loadClass(fullyQualifiedTargetClass);
    }
    logger.info("Instrumenting class '{}'.", fullyQualifiedTargetClass);
    String className = fullyQualifiedTargetClass.replace('.', '/');
    InputStream is = java.lang.ClassLoader.getSystemResourceAsStream(className + ".class");
    if (is == null) {
        throw new ClassNotFoundException("Class " + fullyQualifiedTargetClass + "could not be found!");
    }
    ClassReader reader = null;
    try {
        reader = new ClassReader(is);
    } catch (IOException exc) {
        throw new ClassNotFoundException();
    }
    ClassWriter writer = new ClassWriter(reader, ClassWriter.COMPUTE_MAXS);
    ClassVisitor cv = new StatementTracingVisitor(fullyQualifiedTargetClass, writer);
    CheckClassAdapter checkClassAdapter = new CheckClassAdapter(cv);
    reader.accept(checkClassAdapter, ClassReader.SKIP_FRAMES);
    byte[] byteBuffer = writer.toByteArray();
    Class<?> result = defineClass(fullyQualifiedTargetClass, byteBuffer, 0, byteBuffer.length);
    return result;
}
Also used : InputStream(java.io.InputStream) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter)

Example 3 with CheckClassAdapter

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

the class JarUtil method validateDataFlow.

private static void validateDataFlow(String name, byte[] data) {
    try {
        ClassReader cr = new ClassReader(data);
        ClassWriter cw = new ClassWriter(cr, 0);
        ClassVisitor cv = new CheckClassAdapter(cw, true);
        cr.accept(cv, 0);
    } catch (Exception ex) {
        logger.warn("Class {} failed validation", name, ex);
    }
}
Also used : CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) NonloadingClassWriter(net.runelite.asm.objectwebasm.NonloadingClassWriter) IOException(java.io.IOException)

Example 4 with CheckClassAdapter

use of org.objectweb.asm.util.CheckClassAdapter in project presto by prestodb.

the class ClassInfoLoader method readClassInfoQuick.

private ClassInfo readClassInfoQuick(ParameterizedType type) {
    // check for user supplied class node
    ClassNode classNode = classNodes.get(type);
    if (classNode != null) {
        return new ClassInfo(this, classNode);
    }
    // check for user supplied byte code
    ClassReader classReader;
    byte[] bytecode = bytecodes.get(type);
    if (bytecode != null) {
        classReader = new ClassReader(bytecode);
    } else {
        // load class file from class loader
        String classFileName = type.getClassName() + ".class";
        try (InputStream is = classLoader.getResourceAsStream(classFileName)) {
            classReader = new ClassReader(is);
        } catch (IOException e) {
            // check if class is already loaded
            try {
                Class<?> aClass = classLoader.loadClass(type.getJavaClassName());
                return new ClassInfo(this, aClass);
            } catch (ClassNotFoundException e1) {
                throw new RuntimeException("Class not found " + type, e);
            }
        }
    }
    if (loadMethodNodes) {
        // slower version that loads all operations
        classNode = new ClassNode();
        classReader.accept(new CheckClassAdapter(classNode, false), ClassReader.SKIP_DEBUG);
        return new ClassInfo(this, classNode);
    } else {
        // optimized version
        int header = classReader.header;
        int access = classReader.readUnsignedShort(header);
        char[] buf = new char[2048];
        // read super class name
        int superClassIndex = classReader.getItem(classReader.readUnsignedShort(header + 4));
        ParameterizedType superClass;
        if (superClassIndex == 0) {
            superClass = null;
        } else {
            superClass = typeFromPathName(classReader.readUTF8(superClassIndex, buf));
        }
        // read each interface name
        int interfaceCount = classReader.readUnsignedShort(header + 6);
        ImmutableList.Builder<ParameterizedType> interfaces = ImmutableList.builder();
        header += 8;
        for (int i = 0; i < interfaceCount; ++i) {
            interfaces.add(typeFromPathName(classReader.readClass(header, buf)));
            header += 2;
        }
        return new ClassInfo(this, type, access, superClass, interfaces.build(), null);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) InputStream(java.io.InputStream) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader)

Example 5 with CheckClassAdapter

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

the class ByteCodeVerifier method classNode.

private static ClassNode classNode(ByteBuffer bytecode) {
    byte[] bytes;
    if (bytecode.hasArray()) {
        bytes = bytecode.array();
    } else {
        bytes = new byte[bytecode.limit()];
        bytecode.get(bytes);
    }
    ClassNode classNode = new ClassNode();
    new ClassReader(bytes).accept(new CheckClassAdapter(classNode, false), SKIP_DEBUG);
    return classNode;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader)

Aggregations

CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)17 ClassReader (org.objectweb.asm.ClassReader)12 ClassWriter (org.objectweb.asm.ClassWriter)8 ClassNode (org.objectweb.asm.tree.ClassNode)8 IOException (java.io.IOException)7 ClassVisitor (org.objectweb.asm.ClassVisitor)5 InputStream (java.io.InputStream)4 MethodNode (org.objectweb.asm.tree.MethodNode)4 Analyzer (org.objectweb.asm.tree.analysis.Analyzer)4 TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)3 File (java.io.File)2 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