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);
}
}
}
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);
}
}
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);
}
}
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();
}
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);
}
Aggregations