-
Notifications
You must be signed in to change notification settings - Fork 1k
Remove the need to bump class version when indy advice is used #15258
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
Open
laurit
wants to merge
2
commits into
open-telemetry:main
Choose a base branch
from
laurit:indy-forward
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
20 changes: 20 additions & 0 deletions
20
...rc/main/java/io/opentelemetry/javaagent/bootstrap/advice/AdviceForwardLookupSupplier.java
This file contains hidden or 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,20 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.bootstrap.advice; | ||
|
|
||
| import java.lang.invoke.MethodHandles; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Helper class that provides a MethodHandles.Lookup that allows defining classes in this package. | ||
| */ | ||
| public final class AdviceForwardLookupSupplier implements Supplier<MethodHandles.Lookup> { | ||
|
|
||
| @Override | ||
| public MethodHandles.Lookup get() { | ||
| return MethodHandles.lookup(); | ||
| } | ||
| } |
This file contains hidden or 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
180 changes: 180 additions & 0 deletions
180
...io/opentelemetry/javaagent/tooling/instrumentation/indy/ForwardIndyAdviceTransformer.java
This file contains hidden or 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,180 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.tooling.instrumentation.indy; | ||
|
|
||
| import io.opentelemetry.javaagent.bootstrap.IndyBootstrapDispatcher; | ||
| import io.opentelemetry.javaagent.bootstrap.advice.AdviceForwardLookupSupplier; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi; | ||
| import io.opentelemetry.javaagent.tooling.HelperInjector; | ||
| import java.security.ProtectionDomain; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.function.Supplier; | ||
| import net.bytebuddy.ClassFileVersion; | ||
| import net.bytebuddy.agent.builder.AgentBuilder; | ||
| 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.dynamic.DynamicType; | ||
| import net.bytebuddy.implementation.Implementation; | ||
| import net.bytebuddy.pool.TypePool; | ||
| import net.bytebuddy.utility.JavaModule; | ||
| import org.objectweb.asm.ClassVisitor; | ||
| import org.objectweb.asm.ClassWriter; | ||
| import org.objectweb.asm.Handle; | ||
| import org.objectweb.asm.MethodVisitor; | ||
| import org.objectweb.asm.Opcodes; | ||
| import org.objectweb.asm.Type; | ||
| import org.objectweb.asm.commons.GeneratorAdapter; | ||
|
|
||
| /** | ||
| * Replaces {@code INVOKEDYNAMIC} instructions used for invoking advice with {@code INVOKESTATIC} | ||
| * instructions in a helper class that contains the original {@code INVOKEDYNAMIC} instruction in | ||
| * classes that do not support {@code INVOKEDYNAMIC} (i.e. pre Java 7 class files). | ||
| */ | ||
| public class ForwardIndyAdviceTransformer implements AgentBuilder.Transformer { | ||
| private static final AtomicInteger counter = new AtomicInteger(); | ||
| private static final String bootForwardClassPackage = | ||
| AdviceForwardLookupSupplier.class.getPackage().getName(); | ||
|
|
||
| private final HelperInjector helperInjector; | ||
|
|
||
| public ForwardIndyAdviceTransformer(HelperInjector helperInjector) { | ||
| this.helperInjector = helperInjector; | ||
| } | ||
|
|
||
| private static boolean isAtLeastJava7(TypeDescription typeDescription) { | ||
| ClassFileVersion classFileVersion = typeDescription.getClassFileVersion(); | ||
| return classFileVersion != null && classFileVersion.getJavaVersion() >= 7; | ||
| } | ||
|
|
||
| @Override | ||
| public DynamicType.Builder<?> transform( | ||
| DynamicType.Builder<?> builder, | ||
| TypeDescription typeDescription, | ||
| ClassLoader classLoader, | ||
| JavaModule javaModule, | ||
| ProtectionDomain protectionDomain) { | ||
|
|
||
| // java 7+ class files already support invokedynamic | ||
| if (isAtLeastJava7(typeDescription)) { | ||
| return builder; | ||
| } | ||
|
|
||
| return builder.visit( | ||
| new AsmVisitorWrapper.AbstractBase() { | ||
| @Override | ||
| public ClassVisitor wrap( | ||
| TypeDescription typeDescription, | ||
| ClassVisitor classVisitor, | ||
| Implementation.Context context, | ||
| TypePool typePool, | ||
| FieldList<FieldDescription.InDefinedShape> fieldList, | ||
| MethodList<?> methodList, | ||
| int writerFlags, | ||
| int readerFlags) { | ||
|
|
||
| return new ClassVisitor(AsmApi.VERSION, classVisitor) { | ||
| final Map<String, Supplier<byte[]>> injectedClasses = new HashMap<>(); | ||
|
|
||
| @Override | ||
| public void visitEnd() { | ||
| super.visitEnd(); | ||
|
|
||
| // inject helper classes that forward to the advice using invokedynamic | ||
| if (!injectedClasses.isEmpty()) { | ||
| helperInjector.injectHelperClasses(classLoader, injectedClasses); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public MethodVisitor visitMethod( | ||
| int access, | ||
| String name, | ||
| String descriptor, | ||
| String signature, | ||
| String[] exceptions) { | ||
| MethodVisitor mv = | ||
| super.visitMethod(access, name, descriptor, signature, exceptions); | ||
| return new MethodVisitor(api, mv) { | ||
| @Override | ||
| public void visitInvokeDynamicInsn( | ||
| String name, | ||
| String descriptor, | ||
| Handle bootstrapMethodHandle, | ||
| Object... bootstrapMethodArguments) { | ||
| if (Type.getInternalName(IndyBootstrapDispatcher.class) | ||
| .equals(bootstrapMethodHandle.getOwner())) { | ||
|
|
||
| String adviceClassName = (String) bootstrapMethodArguments[3]; | ||
| String forwardClassDotName = | ||
| classLoader == null | ||
| ? bootForwardClassPackage + ".Forward$$" + counter.incrementAndGet() | ||
| : adviceClassName + "$$Forward$$" + counter.incrementAndGet(); | ||
| String forwardClassSlasName = forwardClassDotName.replace('.', '/'); | ||
|
|
||
| Supplier<byte[]> forwardClassBytes = | ||
| generateForwardClass( | ||
| forwardClassSlasName, | ||
| name, | ||
| descriptor, | ||
| bootstrapMethodHandle, | ||
| bootstrapMethodArguments); | ||
| injectedClasses.put(forwardClassDotName, forwardClassBytes); | ||
|
|
||
| // replace invokedynamic with invokestatic to the generated forwarder class | ||
| // the forwarder class will contain the original invokedynamic instruction | ||
| super.visitMethodInsn( | ||
| Opcodes.INVOKESTATIC, forwardClassSlasName, name, descriptor, false); | ||
| return; | ||
| } | ||
|
|
||
| super.visitInvokeDynamicInsn( | ||
| name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments); | ||
| } | ||
| }; | ||
| } | ||
| }; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private static Supplier<byte[]> generateForwardClass( | ||
| String forwardClassSlasName, | ||
| String methodName, | ||
| String methodDescriptor, | ||
| Handle bootstrapMethodHandle, | ||
| Object[] bootstrapMethodArguments) { | ||
| return () -> { | ||
| ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); | ||
| cw.visit( | ||
| Opcodes.V1_8, | ||
| Opcodes.ACC_PUBLIC, | ||
| forwardClassSlasName, | ||
| null, | ||
| Type.getInternalName(Object.class), | ||
| null); | ||
| MethodVisitor mv = | ||
| cw.visitMethod( | ||
| Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, methodName, methodDescriptor, null, null); | ||
| GeneratorAdapter ga = | ||
| new GeneratorAdapter( | ||
| mv, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, methodName, methodDescriptor); | ||
| ga.loadArgs(); | ||
| mv.visitInvokeDynamicInsn( | ||
| methodName, methodDescriptor, bootstrapMethodHandle, bootstrapMethodArguments); | ||
| ga.returnValue(); | ||
| mv.visitMaxs(0, 0); | ||
| mv.visitEnd(); | ||
| cw.visitEnd(); | ||
|
|
||
| return cw.toByteArray(); | ||
| }; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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.
for now left a flag for switching back to changing the class version, if there is no interest in keeping it could remove it
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.
I support fewer options 😄