-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Transform lambda classes * improve comment
- Loading branch information
Showing
21 changed files
with
283 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
instrumentation/internal/internal-lambda/javaagent/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
dependencies { | ||
compileOnly(project(":javaagent-bootstrap")) | ||
|
||
testImplementation(project(":javaagent-bootstrap")) | ||
} |
118 changes: 118 additions & 0 deletions
118
...javaagent/instrumentation/internal/lambda/InnerClassLambdaMetafactoryInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.internal.lambda; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.AsmVisitorWrapper; | ||
import net.bytebuddy.description.field.FieldDescription; | ||
import net.bytebuddy.description.field.FieldList; | ||
import net.bytebuddy.description.method.MethodList; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.implementation.Implementation; | ||
import net.bytebuddy.jar.asm.ClassVisitor; | ||
import net.bytebuddy.jar.asm.ClassWriter; | ||
import net.bytebuddy.jar.asm.MethodVisitor; | ||
import net.bytebuddy.jar.asm.Opcodes; | ||
import net.bytebuddy.jar.asm.Type; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import net.bytebuddy.pool.TypePool; | ||
|
||
public class InnerClassLambdaMetafactoryInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("java.lang.invoke.InnerClassLambdaMetafactory"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyTransformer( | ||
(builder, typeDescription, classLoader, module) -> | ||
builder.visit( | ||
new AsmVisitorWrapper() { | ||
@Override | ||
public int mergeWriter(int flags) { | ||
return flags | ClassWriter.COMPUTE_MAXS; | ||
} | ||
|
||
@Override | ||
public int mergeReader(int flags) { | ||
return flags; | ||
} | ||
|
||
@Override | ||
public ClassVisitor wrap( | ||
TypeDescription instrumentedType, | ||
ClassVisitor classVisitor, | ||
Implementation.Context implementationContext, | ||
TypePool typePool, | ||
FieldList<FieldDescription.InDefinedShape> fields, | ||
MethodList<?> methods, | ||
int writerFlags, | ||
int readerFlags) { | ||
return new MetaFactoryClassVisitor( | ||
classVisitor, instrumentedType.getInternalName()); | ||
} | ||
})); | ||
} | ||
|
||
private static class MetaFactoryClassVisitor extends ClassVisitor { | ||
private final String slashClassName; | ||
|
||
MetaFactoryClassVisitor(ClassVisitor cv, String slashClassName) { | ||
super(Opcodes.ASM7, cv); | ||
this.slashClassName = slashClassName; | ||
} | ||
|
||
@Override | ||
public MethodVisitor visitMethod( | ||
int access, String name, String descriptor, String signature, String[] exceptions) { | ||
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions); | ||
// The version of InnerClassLambdaMetafactory used in first version of jdk8 can be seen at | ||
// https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java | ||
// Depending on jdk version we instrument either spinInnerClass or generateInnerClass. | ||
// We look for a call to ASM ClassWriter.toByteArray() and insert our lambda class | ||
// transformation after it so that defining lambda class will proceed with replaced bytecode. | ||
// This transformation uses ASM instead of Byte-Buddy advice because advice allows adding | ||
// code to the start and end of the method, but here we are modifying a call in the middle of | ||
// the method. | ||
if (("spinInnerClass".equals(name) || "generateInnerClass".equals(name)) | ||
&& "()Ljava/lang/Class;".equals(descriptor)) { | ||
mv = | ||
new MethodVisitor(api, mv) { | ||
@Override | ||
public void visitMethodInsn( | ||
int opcode, String owner, String name, String descriptor, boolean isInterface) { | ||
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface); | ||
// if current instruction is a call to ASM ClassWriter.toByteArray() insert call to | ||
// our lambda transformer | ||
if (opcode == Opcodes.INVOKEVIRTUAL | ||
&& "toByteArray".equals(name) | ||
&& "()[B".equals(descriptor)) { | ||
mv.visitVarInsn(Opcodes.ALOAD, 0); | ||
mv.visitFieldInsn( | ||
Opcodes.GETFIELD, slashClassName, "lambdaClassName", "Ljava/lang/String;"); | ||
mv.visitVarInsn(Opcodes.ALOAD, 0); | ||
// targetClass is used to get the ClassLoader where lambda class will be defined | ||
mv.visitFieldInsn( | ||
Opcodes.GETFIELD, slashClassName, "targetClass", "Ljava/lang/Class;"); | ||
mv.visitMethodInsn( | ||
Opcodes.INVOKESTATIC, | ||
Type.getInternalName(LambdaTransformer.class), | ||
"transform", | ||
"([BLjava/lang/String;Ljava/lang/Class;)[B", | ||
false); | ||
} | ||
} | ||
}; | ||
} | ||
return mv; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
.../opentelemetry/javaagent/instrumentation/internal/lambda/LambdaInstrumentationModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.internal.lambda; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.List; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class LambdaInstrumentationModule extends InstrumentationModule { | ||
public LambdaInstrumentationModule() { | ||
super("internal-lambda"); | ||
} | ||
|
||
@Override | ||
public boolean defaultEnabled() { | ||
// internal instrumentations are always enabled by default | ||
return true; | ||
} | ||
|
||
@Override | ||
public List<String> getMuzzleHelperClassNames() { | ||
// this instrumentation uses ASM not ByteBuddy so muzzle doesn't automatically add helper | ||
// classes | ||
return singletonList( | ||
"io.opentelemetry.javaagent.instrumentation.internal.lambda.LambdaTransformer"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return singletonList(new InnerClassLambdaMetafactoryInstrumentation()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...in/java/io/opentelemetry/javaagent/instrumentation/internal/lambda/LambdaTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.internal.lambda; | ||
|
||
import io.opentelemetry.javaagent.bootstrap.ClassFileTransformerHolder; | ||
import java.lang.instrument.ClassFileTransformer; | ||
|
||
/** Helper class for transforming lambda class bytes. */ | ||
public final class LambdaTransformer { | ||
|
||
private LambdaTransformer() {} | ||
|
||
/** | ||
* Called from {@code java.lang.invoke.InnerClassLambdaMetafactory} to transform lambda class | ||
* bytes. | ||
*/ | ||
public static byte[] transform(byte[] classBytes, String slashClassName, Class<?> targetClass) { | ||
ClassFileTransformer transformer = ClassFileTransformerHolder.getClassFileTransformer(); | ||
if (transformer != null) { | ||
try { | ||
byte[] result = | ||
transformer.transform( | ||
targetClass.getClassLoader(), slashClassName, null, null, classBytes); | ||
if (result != null) { | ||
return result; | ||
} | ||
} catch (Throwable throwable) { | ||
// sun.instrument.TransformerManager catches Throwable from ClassFileTransformer and ignores | ||
// it, we do the same. | ||
} | ||
} | ||
|
||
return classBytes; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ation/internal/internal-lambda/javaagent/src/test/groovy/LambdaInstrumentationTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification | ||
import io.opentelemetry.javaagent.bootstrap.FieldBackedContextStoreAppliedMarker | ||
|
||
class LambdaInstrumentationTest extends AgentInstrumentationSpecification { | ||
|
||
def "test transform Runnable lambda"() { | ||
setup: | ||
Runnable runnable = TestLambda.makeRunnable() | ||
|
||
expect: | ||
// RunnableInstrumentation adds a ContextStore to all implementors of Runnable. If lambda class | ||
// is transformed then it must have context store marker interface. | ||
runnable instanceof FieldBackedContextStoreAppliedMarker | ||
!FieldBackedContextStoreAppliedMarker.isAssignableFrom(Runnable) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
instrumentation/internal/internal-lambda/javaagent/src/test/java/TestLambda.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
public class TestLambda { | ||
static Runnable makeRunnable() { | ||
return () -> {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.