Search in sources :

Example 21 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project drill by axbaretto.

the class AsmUtil method logClass.

/**
 * Write a class to the log.
 *
 * <p>
 * Writes at level TRACE.
 *
 * @param logger
 *          the logger to write to
 * @param logTag
 *          a tag to print to the log
 * @param classNode
 *          the class
 */
public static void logClass(final Logger logger, final String logTag, final ClassNode classNode) {
    if (logger.isTraceEnabled()) {
        logger.trace(logTag);
        final StringWriter stringWriter = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(stringWriter);
        final TraceClassVisitor traceClassVisitor = new TraceClassVisitor(printWriter);
        classNode.accept(traceClassVisitor);
        logger.trace(stringWriter.toString());
    }
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 22 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project drill by axbaretto.

the class ReplaceMethodInvoke method main.

// private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ReplaceMethodInvoke.class);
public static void main(String[] args) throws Exception {
    final String k2 = "org/apache/drill/Pickle.class";
    final URL url = Resources.getResource(k2);
    final byte[] clazz = Resources.toByteArray(url);
    final ClassReader cr = new ClassReader(clazz);
    final ClassWriter cw = writer();
    final TraceClassVisitor visitor = new TraceClassVisitor(cw, new Textifier(), new PrintWriter(System.out));
    final ValueHolderReplacementVisitor v2 = new ValueHolderReplacementVisitor(visitor, true);
    // | ClassReader.SKIP_DEBUG);
    cr.accept(v2, ClassReader.EXPAND_FRAMES);
    final byte[] output = cw.toByteArray();
    Files.write(output, new File("/src/scratch/bytes/S.class"));
    check(output);
    final DrillConfig c = DrillConfig.forClient();
    final SystemOptionManager m = new SystemOptionManager(PhysicalPlanReaderTestFactory.defaultLogicalPlanPersistence(c), new LocalPersistentStoreProvider(c), c);
    m.init();
    try (QueryClassLoader ql = new QueryClassLoader(DrillConfig.create(), m)) {
        ql.injectByteCode("org.apache.drill.Pickle$OutgoingBatch", output);
        Class<?> clz = ql.loadClass("org.apache.drill.Pickle$OutgoingBatch");
        clz.getMethod("x").invoke(null);
    }
}
Also used : LocalPersistentStoreProvider(org.apache.drill.exec.store.sys.store.provider.LocalPersistentStoreProvider) SystemOptionManager(org.apache.drill.exec.server.options.SystemOptionManager) Textifier(org.objectweb.asm.util.Textifier) URL(java.net.URL) ClassWriter(org.objectweb.asm.ClassWriter) QueryClassLoader(org.apache.drill.exec.compile.QueryClassLoader) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) DrillConfig(org.apache.drill.common.config.DrillConfig) ClassReader(org.objectweb.asm.ClassReader) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 23 with TraceClassVisitor

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

Example 24 with TraceClassVisitor

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

the class TaintLoadCoercer method main.

public static void main(String[] args) throws Throwable {
    Configuration.IMPLICIT_TRACKING = true;
    Configuration.MULTI_TAINTING = true;
    // Configuration.IMPLICIT_LIGHT_TRACKING = true;
    // Configuration.ARRAY_LENGTH_TRACKING = true;
    Configuration.ARRAY_INDEX_TRACKING = true;
    // Configuration.ANNOTATE_LOOPS = true;
    // Instrumenter.instrumentClass("asdf", new FileInputStream("z.class"), false);
    ClassReader cr = new ClassReader(new FileInputStream("z.class"));
    // ClassReader cr = new ClassReader(new FileInputStream("target/classes/VarInLoop.class"));
    final String className = cr.getClassName();
    PrintWriter pw = new PrintWriter("z.txt");
    TraceClassVisitor tcv = new TraceClassVisitor(null, new PhosphorTextifier(), pw);
    ClassWriter cw1 = new ClassWriter(ClassWriter.COMPUTE_FRAMES) {

        @Override
        protected String getCommonSuperClass(String arg0, String arg1) {
            try {
                return super.getCommonSuperClass(arg0, arg1);
            } catch (Throwable t) {
                return "java/lang/Object";
            }
        }
    };
    cr.accept(new ClassVisitor(Opcodes.ASM5, cw1) {

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
            mv = new JSRInlinerAdapter(mv, access, name, desc, signature, exceptions);
            return mv;
        }
    }, ClassReader.EXPAND_FRAMES);
    cr = new ClassReader(cw1.toByteArray());
    ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, tcv) {

        String className;

        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            super.visit(version, access, name, signature, superName, interfaces);
            this.className = name;
        }

        HashSet<FieldNode> fields = new HashSet<FieldNode>();

        @Override
        public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
            fields.add(new FieldNode(access, name, desc, signature, value));
            return super.visitField(access, name, desc, signature, value);
        }

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            // TODO Auto-generated method stub
            MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
            mv = new MethodVisitor(Opcodes.ASM5, mv) {

                @Override
                public void visitInsn(int opcode) {
                    super.visitInsn(opcode);
                }
            };
            // mv = new SpecialOpcodeRemovingMV(mv,false,className,false);
            // NeverNullArgAnalyzerAdapter analyzer = new NeverNullArgAnalyzerAdapter(className, access, className, desc, mv);
            mv = new TaintLoadCoercer(className, access, name, desc, signature, exceptions, mv, true);
            // LocalVariableManager lvs = new LocalVariableManager(access, desc, mv, analyzer, mv, false);
            // mv = lvs;
            PrimitiveArrayAnalyzer paa = new PrimitiveArrayAnalyzer(className, access, name, desc, signature, exceptions, mv);
            NeverNullArgAnalyzerAdapter an = new NeverNullArgAnalyzerAdapter(className, access, name, desc, paa);
            paa.setAnalyzer(an);
            // ((PrimitiveArrayAnalyzer) mv).setAnalyzer(an);
            mv = an;
            return mv;
        }
    };
    cr.accept(cv, ClassReader.EXPAND_FRAMES);
    pw.flush();
}
Also used : FieldNode(org.objectweb.asm.tree.FieldNode) ClassVisitor(org.objectweb.asm.ClassVisitor) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) FileInputStream(java.io.FileInputStream) ClassWriter(org.objectweb.asm.ClassWriter) JSRInlinerAdapter(org.objectweb.asm.commons.JSRInlinerAdapter) MethodVisitor(org.objectweb.asm.MethodVisitor) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) NeverNullArgAnalyzerAdapter(edu.columbia.cs.psl.phosphor.instrumenter.analyzer.NeverNullArgAnalyzerAdapter) ClassReader(org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

Example 25 with TraceClassVisitor

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

the class DebugPrinter method main.

public static void main(String[] args) throws Exception {
    // Configuration.IMPLICIT_TRACKING = true;
    File clazz = new File(args[0]);
    final ClassReader cr1 = new ClassReader(new FileInputStream(clazz));
    PrintWriter pw = new PrintWriter(new FileWriter("z.txt"));
    ClassWriter cw1 = new ClassWriter(ClassWriter.COMPUTE_FRAMES) {

        @Override
        protected String getCommonSuperClass(String type1, String type2) {
            try {
                return super.getCommonSuperClass(type1, type2);
            } catch (Exception ex) {
                // System.err.println("err btwn " + type1 + " " +type2);
                return "java/lang/Unknown";
            }
        }
    };
    cr1.accept(new ClassVisitor(Opcodes.ASM4, cw1) {

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            // TODO Auto-generated method stub
            return new JSRInlinerAdapter(super.visitMethod(access, name, desc, signature, exceptions), access, name, desc, signature, exceptions);
        }
    }, ClassReader.EXPAND_FRAMES);
    final ClassReader cr = new ClassReader(cw1.toByteArray());
    TraceClassVisitor tcv = new TraceClassVisitor(new ClassVisitor(Opcodes.ASM5, new ClassWriter(0)) {

        String className;

        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            super.visit(version, access, name, signature, superName, interfaces);
            this.className = name;
        }

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            // TODO Auto-generated method stub
            MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
            mv = new PrimitiveArrayAnalyzer(cr.getClassName(), access, name, desc, signature, exceptions, mv);
            NeverNullArgAnalyzerAdapter an = new NeverNullArgAnalyzerAdapter(cr.getClassName(), access, name, desc, mv);
            ((PrimitiveArrayAnalyzer) mv).setAnalyzer(an);
            mv = an;
            return mv;
        }
    }, pw);
    cr.accept(tcv, ClassReader.EXPAND_FRAMES);
    pw.flush();
}
Also used : FileWriter(java.io.FileWriter) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) FileInputStream(java.io.FileInputStream) ClassWriter(org.objectweb.asm.ClassWriter) JSRInlinerAdapter(org.objectweb.asm.commons.JSRInlinerAdapter) MethodVisitor(org.objectweb.asm.MethodVisitor) PrimitiveArrayAnalyzer(edu.columbia.cs.psl.phosphor.instrumenter.PrimitiveArrayAnalyzer) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) NeverNullArgAnalyzerAdapter(edu.columbia.cs.psl.phosphor.instrumenter.analyzer.NeverNullArgAnalyzerAdapter) ClassReader(org.objectweb.asm.ClassReader) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)39 PrintWriter (java.io.PrintWriter)31 ClassReader (org.objectweb.asm.ClassReader)24 ClassWriter (org.objectweb.asm.ClassWriter)16 StringWriter (java.io.StringWriter)14 ClassVisitor (org.objectweb.asm.ClassVisitor)10 Textifier (org.objectweb.asm.util.Textifier)10 IOException (java.io.IOException)6 File (java.io.File)5 ClassNode (org.objectweb.asm.tree.ClassNode)5 ArrayList (java.util.ArrayList)3 ASMifier (org.objectweb.asm.util.ASMifier)3 CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)3 NeverNullArgAnalyzerAdapter (edu.columbia.cs.psl.phosphor.instrumenter.analyzer.NeverNullArgAnalyzerAdapter)2 FileInputStream (java.io.FileInputStream)2 URL (java.net.URL)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 DrillConfig (org.apache.drill.common.config.DrillConfig)2 QueryClassLoader (org.apache.drill.exec.compile.QueryClassLoader)2