Remove ReplaceIsSafeVisitor

This commit is contained in:
Andrew Kent 2018-05-08 15:15:26 -07:00
parent 08df3cef35
commit 98b44c1dfb
4 changed files with 12 additions and 60 deletions

View File

@ -11,7 +11,7 @@ class MuzzleBytecodeTransformTest extends Specification {
List<Class> unMuzzledClasses = [] List<Class> unMuzzledClasses = []
for (final Object instrumenter : ServiceLoader.load(IntegrationTestUtils.getAgentClassLoader().loadClass("datadog.trace.agent.tooling.Instrumenter"), IntegrationTestUtils.getAgentClassLoader())) { for (final Object instrumenter : ServiceLoader.load(IntegrationTestUtils.getAgentClassLoader().loadClass("datadog.trace.agent.tooling.Instrumenter"), IntegrationTestUtils.getAgentClassLoader())) {
try { try {
instrumenter.getClass().getDeclaredField("referenceMatcher") instrumenter.getClass().getDeclaredField("instrumentationMuzzle")
} catch(NoSuchFieldException nsfe) { } catch(NoSuchFieldException nsfe) {
unMuzzledClasses.add(instrumenter.getClass()) unMuzzledClasses.add(instrumenter.getClass())
} }

View File

@ -39,7 +39,8 @@ public class DatadogClassLoader extends URLClassLoader {
} }
/** /**
* A stand-in for the bootstrap classloader. Used to look up bootstrap resources and resources appended by instrumentation. * A stand-in for the bootstrap classloader. Used to look up bootstrap resources and resources
* appended by instrumentation.
* *
* <p>This class is thread safe. * <p>This class is thread safe.
*/ */

View File

@ -85,7 +85,7 @@ public class AdviceReferenceVisitor extends ClassVisitor {
final Set<String> visitedSources = new HashSet<String>(); final Set<String> visitedSources = new HashSet<String>();
final Map<String, Reference> references = new HashMap<>(); final Map<String, Reference> references = new HashMap<>();
final Queue<String> instrumentationQueue = new LinkedList<>(); final Queue<String> instrumentationQueue = new ArrayDeque<>();
instrumentationQueue.add(entryPointClassName); instrumentationQueue.add(entryPointClassName);
while (!instrumentationQueue.isEmpty()) { while (!instrumentationQueue.isEmpty()) {

View File

@ -12,9 +12,8 @@ import net.bytebuddy.jar.asm.*;
import net.bytebuddy.pool.TypePool; import net.bytebuddy.pool.TypePool;
/** /**
* Add a referenceMatcher field and 2. * Add a instrumentationMuzzle field and for each decorator add a final transformer to assert the
* * classpath is safe instrument.
* <p>And for each decorator add a final transformer to assert the classpath is safe instrument.
*/ */
public class MuzzleVisitor implements AsmVisitorWrapper { public class MuzzleVisitor implements AsmVisitorWrapper {
@Override @Override
@ -71,7 +70,6 @@ public class MuzzleVisitor implements AsmVisitorWrapper {
if ("<init>".equals(name)) { if ("<init>".equals(name)) {
methodVisitor = new InitializeFieldVisitor(methodVisitor); methodVisitor = new InitializeFieldVisitor(methodVisitor);
} }
// return new ReplaceIsSafeVisitor(methodVisitor);
return new InsertMuzzleTransformer(methodVisitor); return new InsertMuzzleTransformer(methodVisitor);
} }
@ -79,7 +77,7 @@ public class MuzzleVisitor implements AsmVisitorWrapper {
public void visitEnd() { public void visitEnd() {
super.visitField( super.visitField(
Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC,
"referenceMatcher", "instrumentationMuzzle",
Type.getDescriptor(ReferenceMatcher.class), Type.getDescriptor(ReferenceMatcher.class),
null, null,
null); null);
@ -91,7 +89,7 @@ public class MuzzleVisitor implements AsmVisitorWrapper {
* &nbsp.transform(DDAdvice.create().advice(named("fooMethod", FooAdvice.class.getname()))) * &nbsp.transform(DDAdvice.create().advice(named("fooMethod", FooAdvice.class.getname())))
* &nbsp.asDecorator(); Into this:<br> * &nbsp.asDecorator(); Into this:<br>
* &nbsp.transform(DDAdvice.create().advice(named("fooMethod", FooAdvice.class.getname()))) * &nbsp.transform(DDAdvice.create().advice(named("fooMethod", FooAdvice.class.getname())))
* &nbsp.transform(this.referenceMatcher.assertSafeTransformation("foo.package.FooAdvice")); * &nbsp.transform(this.instrumentationMuzzle.assertSafeTransformation("foo.package.FooAdvice"));
* &nbsp.asDecorator(); className) * &nbsp.asDecorator(); className)
*/ */
public class InsertMuzzleTransformer extends MethodVisitor { public class InsertMuzzleTransformer extends MethodVisitor {
@ -138,7 +136,7 @@ public class MuzzleVisitor implements AsmVisitorWrapper {
this.visitFieldInsn( this.visitFieldInsn(
Opcodes.GETFIELD, Opcodes.GETFIELD,
instrumentationClassName, instrumentationClassName,
"referenceMatcher", "instrumentationMuzzle",
Type.getDescriptor(ReferenceMatcher.class)); Type.getDescriptor(ReferenceMatcher.class));
mv.visitIntInsn(Opcodes.BIPUSH, adviceClassNames.size()); mv.visitIntInsn(Opcodes.BIPUSH, adviceClassNames.size());
mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String"); mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
@ -152,7 +150,7 @@ public class MuzzleVisitor implements AsmVisitorWrapper {
} }
mv.visitMethodInsn( mv.visitMethodInsn(
Opcodes.INVOKEVIRTUAL, Opcodes.INVOKEVIRTUAL,
"datadog/trace/agent/tooling/muzzle/ReferenceMatcher", "datadog/trace/agent/tooling/muzzle/ReferenceMatcher",
"assertSafeTransformation", "assertSafeTransformation",
"([Ljava/lang/String;)Lnet/bytebuddy/agent/builder/AgentBuilder$Transformer;", "([Ljava/lang/String;)Lnet/bytebuddy/agent/builder/AgentBuilder$Transformer;",
false); false);
@ -184,53 +182,6 @@ public class MuzzleVisitor implements AsmVisitorWrapper {
} }
} }
/**
* Replace:<br>
* &nbsp&nbsp advice(elementMatcher, className)<br>
* Into:<br>
* &nbsp&nbsp advice(this.referenceMatcher.createElementMatcher(elementMatcher, className),
* className)
*/
public class ReplaceIsSafeVisitor extends MethodVisitor {
public ReplaceIsSafeVisitor(MethodVisitor methodVisitor) {
super(Opcodes.ASM6, methodVisitor);
}
@Override
public void visitMethodInsn(
final int opcode,
final String owner,
final String name,
final String descriptor,
final boolean isInterface) {
if (name.equals("advice")) {
// stack: [class, matcher]
this.visitVarInsn(Opcodes.ALOAD, 0);
// stack: [this, class, matcher]
this.visitFieldInsn(
Opcodes.GETFIELD,
instrumentationClassName,
"referenceMatcher",
Type.getDescriptor(ReferenceMatcher.class));
// stack: [referenceMatcher, class, matcher]
this.visitInsn(Opcodes.DUP2_X1);
// stack: [referenceMatcher, class, matcher, referenceMatcher, class]
this.visitInsn(Opcodes.POP);
// stack: [class, matcher, referenceMatcher, class]
this.visitMethodInsn(
Opcodes.INVOKEVIRTUAL,
"datadog/trace/agent/tooling/muzzle/ReferenceMatcher",
"createElementMatcher",
"(Lnet/bytebuddy/matcher/ElementMatcher;Ljava/lang/String;)Lnet/bytebuddy/matcher/ElementMatcher;",
false);
// stack: [safe-matcher, class]
this.visitInsn(Opcodes.SWAP);
// stack: [class, safe-matcher]
}
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
}
}
/** Append a field initializer to the end of a method. */ /** Append a field initializer to the end of a method. */
public class InitializeFieldVisitor extends MethodVisitor { public class InitializeFieldVisitor extends MethodVisitor {
public InitializeFieldVisitor(MethodVisitor methodVisitor) { public InitializeFieldVisitor(MethodVisitor methodVisitor) {
@ -245,14 +196,14 @@ public class MuzzleVisitor implements AsmVisitorWrapper {
mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.DUP);
mv.visitMethodInsn( mv.visitMethodInsn(
Opcodes.INVOKESPECIAL, Opcodes.INVOKESPECIAL,
"datadog/trace/agent/tooling/muzzle/ReferenceMatcher", "datadog/trace/agent/tooling/muzzle/ReferenceMatcher",
"<init>", "<init>",
"()V", "()V",
false); false);
super.visitFieldInsn( super.visitFieldInsn(
Opcodes.PUTFIELD, Opcodes.PUTFIELD,
instrumentationClassName.replace('.', '/'), instrumentationClassName.replace('.', '/'),
"referenceMatcher", "instrumentationMuzzle",
Type.getDescriptor(ReferenceMatcher.class)); Type.getDescriptor(ReferenceMatcher.class));
} }
super.visitInsn(opcode); super.visitInsn(opcode);