Search in sources :

Example 16 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 + " 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)

Example 17 with CheckClassAdapter

use of org.objectweb.asm.util.CheckClassAdapter in project closure-templates by google.

the class ClassData method checkClass.

/**
 * Runs the {@link CheckClassAdapter} on this class in basic analysis mode.
 *
 * <p>Basic anaylsis mode can flag verification errors that don't depend on knowing complete type
 * information for the classes and methods being called. This is useful for flagging simple
 * generation mistakes (e.g. stack underflows, method return type mismatches, accessing invalid
 * locals). Additionally, the error messages are more useful than what the java verifier normally
 * presents.
 */
public void checkClass() {
    ClassNode cv = new ClassNode();
    new ClassReader(data).accept(new CheckClassAdapter(cv, true), 0);
    // double check our fields while we are here.
    checkState(type.internalName().equals(cv.name));
    checkState(numberOfFields == cv.fields.size());
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader)

Example 18 with CheckClassAdapter

use of org.objectweb.asm.util.CheckClassAdapter in project phosphor by gmu-swe.

the class SourceSinkTransformer method transform.

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    if (classBeingRedefined == null) {
        // should be performed
        return null;
    } else if (Throwable.class.isAssignableFrom(classBeingRedefined)) {
        return null;
    }
    try {
        ClassReader cr = new ClassReader(classfileBuffer);
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        ClassVisitor cv = cw;
        if (PreMain.DEBUG || TaintUtils.VERIFY_CLASS_GENERATION) {
            cv = new CheckClassAdapter(cw, false);
        }
        cr.accept(new SourceSinkTaintingClassVisitor(cv), ClassReader.EXPAND_FRAMES);
        if (PreMain.DEBUG) {
            try {
                File f = new File("debug-source-sink/" + className + ".class");
                f.getParentFile().mkdirs();
                FileOutputStream fos = new FileOutputStream(f);
                fos.write(cw.toByteArray());
                fos.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return cw.toByteArray();
    } catch (Throwable t) {
        // If we don't try/catch and print the error, then it will be silently swallowed by the JVM
        // and we'll never know the instrumentation failed :(
        t.printStackTrace();
        throw t;
    }
}
Also used : CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) SourceSinkTaintingClassVisitor(edu.columbia.cs.psl.phosphor.instrumenter.SourceSinkTaintingClassVisitor) SourceSinkTaintingClassVisitor(edu.columbia.cs.psl.phosphor.instrumenter.SourceSinkTaintingClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) File(java.io.File) ClassWriter(org.objectweb.asm.ClassWriter)

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