-
Notifications
You must be signed in to change notification settings - Fork 867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transform lambda classes #4182
Merged
Merged
Transform lambda classes #4182
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment on why we can't use an Advice method (assuming we can't)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I wrote this I didn't realize that actually advice could be used. For this we would need to instrument spinInnerClass to enter/exit a ThreadLocal and toByteArray of ClassWriter bundled in jdk to only transform when ThreadLocal is set. I do like the asm way a bit more because it is straightforward. Do you think it would be better to use advice for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Advice is nice for making this instrumentation more consistent with our others. But if it adds significant complexity it's not worth it, a comment just describing that is fine.