Search in sources :

Example 46 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project soot by Sable.

the class AbstractASMBackend method generateAnnotations.

/**
 * Emits the bytecode for all annotations of a class, field or method
 * @param visitor A ClassVisitor, FieldVisitor or MethodVisitor to emit the bytecode to
 * @param host A Host (SootClass, SootField or SootMethod) the bytecode is to be emitted for, has to match the visitor
 */
protected void generateAnnotations(Object visitor, Host host) {
    for (Tag t : host.getTags()) {
        // Find all VisibilityAnnotationTags
        if (t instanceof VisibilityAnnotationTag) {
            VisibilityAnnotationTag vat = (VisibilityAnnotationTag) t;
            boolean runTimeVisible = (vat.getVisibility() == AnnotationConstants.RUNTIME_VISIBLE);
            for (AnnotationTag at : vat.getAnnotations()) {
                AnnotationVisitor av = null;
                if (visitor instanceof ClassVisitor) {
                    av = ((ClassVisitor) visitor).visitAnnotation(at.getType(), runTimeVisible);
                } else if (visitor instanceof FieldVisitor) {
                    av = ((FieldVisitor) visitor).visitAnnotation(at.getType(), runTimeVisible);
                } else if (visitor instanceof MethodVisitor) {
                    av = ((MethodVisitor) visitor).visitAnnotation(at.getType(), runTimeVisible);
                }
                generateAnnotationElems(av, at.getElems(), true);
            }
        } else // Visit AnnotationDefault on methods
        if (host instanceof SootMethod && t instanceof AnnotationDefaultTag) {
            AnnotationDefaultTag adt = (AnnotationDefaultTag) t;
            AnnotationVisitor av = ((MethodVisitor) visitor).visitAnnotationDefault();
            generateAnnotationElems(av, Collections.singleton(adt.getDefaultVal()), true);
        }
    }
}
Also used : VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) AnnotationTag(soot.tagkit.AnnotationTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) Tag(soot.tagkit.Tag) SourceFileTag(soot.tagkit.SourceFileTag) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) InnerClassTag(soot.tagkit.InnerClassTag) OuterClassTag(soot.tagkit.OuterClassTag) SignatureTag(soot.tagkit.SignatureTag) AnnotationTag(soot.tagkit.AnnotationTag) EnclosingMethodTag(soot.tagkit.EnclosingMethodTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) ClassVisitor(org.objectweb.asm.ClassVisitor) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) FieldVisitor(org.objectweb.asm.FieldVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 47 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project drill by apache.

the class ReplaceMethodInvoke method check.

private static final void check(final byte[] b) {
    final ClassReader cr = new ClassReader(b);
    final ClassWriter cw = writer();
    final ClassVisitor cv = new DrillCheckClassAdapter(cw);
    cr.accept(cv, 0);
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw);
    DrillCheckClassAdapter.verify(new ClassReader(cw.toByteArray()), false, pw);
    final String checkString = sw.toString();
    if (!checkString.isEmpty()) {
        throw new IllegalStateException(checkString);
    }
}
Also used : StringWriter(java.io.StringWriter) ClassReader(org.objectweb.asm.ClassReader) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) DrillCheckClassAdapter(org.apache.drill.exec.compile.DrillCheckClassAdapter) ClassWriter(org.objectweb.asm.ClassWriter) PrintWriter(java.io.PrintWriter)

Example 48 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project CodeChickenLib by Chicken-Bones.

the class ASMHelper method dump.

public static void dump(Acceptor acceptor, File file, boolean filterImportant, boolean sortLocals, boolean textify) {
    try {
        if (!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        if (!file.exists())
            file.createNewFile();
        PrintWriter pout = new PrintWriter(file);
        ClassVisitor cv = new TraceClassVisitor(null, textify ? new Textifier() : new ASMifier(), pout);
        if (filterImportant)
            cv = new ImportantInsnVisitor(cv);
        if (sortLocals)
            cv = new LocalVariablesSorterVisitor(cv);
        acceptor.accept(cv);
        pout.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ASMifier(org.objectweb.asm.util.ASMifier) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) Textifier(org.objectweb.asm.util.Textifier) PrintWriter(java.io.PrintWriter)

Example 49 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project cassandra by apache.

the class InterceptAgent method transform.

private static byte[] transform(byte[] bytes, BiFunction<Integer, ClassWriter, ClassVisitor> constructor) {
    ClassWriter out = new ClassWriter(0);
    ClassReader in = new ClassReader(bytes);
    ClassVisitor transform = constructor.apply(BYTECODE_VERSION, out);
    in.accept(transform, 0);
    return out.toByteArray();
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter)

Example 50 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project cassandra by apache.

the class InterceptAgent method transformThreadLocalRandom.

/**
 * We require ThreadLocalRandom to be deterministic, so we modify its initialisation method to invoke a
 * global deterministic random value generator
 */
private static byte[] transformThreadLocalRandom(byte[] bytes) {
    class ThreadLocalRandomVisitor extends ClassVisitor {

        public ThreadLocalRandomVisitor(int api, ClassVisitor classVisitor) {
            super(api, classVisitor);
        }

        String unsafeDescriptor;

        String unsafeFieldName;

        @Override
        public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
            if (descriptor.equals("Lsun/misc/Unsafe;") || descriptor.equals("Ljdk/internal/misc/Unsafe;")) {
                unsafeFieldName = name;
                unsafeDescriptor = descriptor;
            }
            return super.visitField(access, name, descriptor, signature, value);
        }

        @Override
        public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
            if (descriptor.equals("()V") && name.equals("localInit")) {
                if (unsafeFieldName == null) {
                    String version = System.getProperty("java.version");
                    if (version.startsWith("11.")) {
                        unsafeFieldName = "U";
                        unsafeDescriptor = "Ljdk/internal/misc/Unsafe;";
                    } else if (version.startsWith("1.8")) {
                        unsafeFieldName = "UNSAFE";
                        unsafeDescriptor = "Lsun/misc/Unsafe;";
                    } else
                        throw new AssertionError("Unsupported Java Version");
                }
                MethodVisitor visitor = super.visitMethod(access, name, descriptor, signature, exceptions);
                visitor.visitLabel(new Label());
                visitor.visitIntInsn(ALOAD, 0);
                visitor.visitFieldInsn(GETSTATIC, "java/util/concurrent/ThreadLocalRandom", unsafeFieldName, unsafeDescriptor);
                visitor.visitMethodInsn(INVOKESTATIC, "java/lang/Thread", "currentThread", "()Ljava/lang/Thread;", false);
                visitor.visitFieldInsn(GETSTATIC, "java/util/concurrent/ThreadLocalRandom", "SEED", "J");
                visitor.visitMethodInsn(INVOKESTATIC, "org/apache/cassandra/simulator/systems/InterceptorOfSystemMethods$Global", "randomSeed", "()J", false);
                visitor.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "putLong", "(Ljava/lang/Object;JJ)V", false);
                visitor.visitFieldInsn(GETSTATIC, "java/util/concurrent/ThreadLocalRandom", unsafeFieldName, unsafeDescriptor);
                visitor.visitMethodInsn(INVOKESTATIC, "java/lang/Thread", "currentThread", "()Ljava/lang/Thread;", false);
                visitor.visitFieldInsn(GETSTATIC, "java/util/concurrent/ThreadLocalRandom", "PROBE", "J");
                visitor.visitLdcInsn(0);
                visitor.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "putInt", "(Ljava/lang/Object;JI)V", false);
                visitor.visitInsn(RETURN);
                visitor.visitLabel(new Label());
                visitor.visitMaxs(6, 1);
                visitor.visitEnd();
                return new MethodVisitor(BYTECODE_VERSION) {
                };
            } else {
                return super.visitMethod(access, name, descriptor, signature, exceptions);
            }
        }
    }
    return transform(bytes, ThreadLocalRandomVisitor::new);
}
Also used : Label(org.objectweb.asm.Label) ClassVisitor(org.objectweb.asm.ClassVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

ClassVisitor (org.objectweb.asm.ClassVisitor)133 ClassReader (org.objectweb.asm.ClassReader)97 ClassWriter (org.objectweb.asm.ClassWriter)80 MethodVisitor (org.objectweb.asm.MethodVisitor)45 IOException (java.io.IOException)19 InputStream (java.io.InputStream)19 Type (org.objectweb.asm.Type)14 TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)14 File (java.io.File)11 PrintWriter (java.io.PrintWriter)11 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)10 Label (org.objectweb.asm.Label)10 FileInputStream (java.io.FileInputStream)9 Test (org.junit.Test)9 FileOutputStream (java.io.FileOutputStream)6 FieldVisitor (org.objectweb.asm.FieldVisitor)6 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)6 ArrayList (java.util.ArrayList)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 FieldList (net.bytebuddy.description.field.FieldList)5