|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.tooling.instrumentation.indy; |
| 7 | + |
| 8 | +import io.opentelemetry.javaagent.bootstrap.IndyBootstrapDispatcher; |
| 9 | +import io.opentelemetry.javaagent.bootstrap.advice.AdviceForwardLookupSupplier; |
| 10 | +import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi; |
| 11 | +import io.opentelemetry.javaagent.tooling.HelperInjector; |
| 12 | +import java.security.ProtectionDomain; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.concurrent.atomic.AtomicInteger; |
| 16 | +import java.util.function.Supplier; |
| 17 | +import net.bytebuddy.ClassFileVersion; |
| 18 | +import net.bytebuddy.agent.builder.AgentBuilder; |
| 19 | +import net.bytebuddy.asm.AsmVisitorWrapper; |
| 20 | +import net.bytebuddy.description.field.FieldDescription; |
| 21 | +import net.bytebuddy.description.field.FieldList; |
| 22 | +import net.bytebuddy.description.method.MethodList; |
| 23 | +import net.bytebuddy.description.type.TypeDescription; |
| 24 | +import net.bytebuddy.dynamic.DynamicType; |
| 25 | +import net.bytebuddy.implementation.Implementation; |
| 26 | +import net.bytebuddy.pool.TypePool; |
| 27 | +import net.bytebuddy.utility.JavaModule; |
| 28 | +import org.objectweb.asm.ClassVisitor; |
| 29 | +import org.objectweb.asm.ClassWriter; |
| 30 | +import org.objectweb.asm.Handle; |
| 31 | +import org.objectweb.asm.MethodVisitor; |
| 32 | +import org.objectweb.asm.Opcodes; |
| 33 | +import org.objectweb.asm.Type; |
| 34 | +import org.objectweb.asm.commons.GeneratorAdapter; |
| 35 | + |
| 36 | +/** |
| 37 | + * Replaces {@code INVOKEDYNAMIC} instructions used for invoking advice with {@code INVOKESTATIC} |
| 38 | + * instructions in a helper class that contains the original {@code INVOKEDYNAMIC} instruction in |
| 39 | + * classes that do not support {@code INVOKEDYNAMIC} (i.e. pre Java 7 class files). |
| 40 | + */ |
| 41 | +public class ForwardIndyAdviceTransformer implements AgentBuilder.Transformer { |
| 42 | + private static final AtomicInteger counter = new AtomicInteger(); |
| 43 | + private static final String bootForwardClassPackage = |
| 44 | + AdviceForwardLookupSupplier.class.getPackage().getName(); |
| 45 | + |
| 46 | + private final HelperInjector helperInjector; |
| 47 | + |
| 48 | + public ForwardIndyAdviceTransformer(HelperInjector helperInjector) { |
| 49 | + this.helperInjector = helperInjector; |
| 50 | + } |
| 51 | + |
| 52 | + private static boolean isAtLeastJava7(TypeDescription typeDescription) { |
| 53 | + ClassFileVersion classFileVersion = typeDescription.getClassFileVersion(); |
| 54 | + return classFileVersion != null && classFileVersion.getJavaVersion() >= 7; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public DynamicType.Builder<?> transform( |
| 59 | + DynamicType.Builder<?> builder, |
| 60 | + TypeDescription typeDescription, |
| 61 | + ClassLoader classLoader, |
| 62 | + JavaModule javaModule, |
| 63 | + ProtectionDomain protectionDomain) { |
| 64 | + |
| 65 | + // temporarily disable version check for testing |
| 66 | + if (false) { |
| 67 | + // java 7+ class files already support invokedynamic |
| 68 | + if (isAtLeastJava7(typeDescription)) { |
| 69 | + return builder; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return builder.visit( |
| 74 | + new AsmVisitorWrapper.AbstractBase() { |
| 75 | + @Override |
| 76 | + public ClassVisitor wrap( |
| 77 | + TypeDescription typeDescription, |
| 78 | + ClassVisitor classVisitor, |
| 79 | + Implementation.Context context, |
| 80 | + TypePool typePool, |
| 81 | + FieldList<FieldDescription.InDefinedShape> fieldList, |
| 82 | + MethodList<?> methodList, |
| 83 | + int writerFlags, |
| 84 | + int readerFlags) { |
| 85 | + |
| 86 | + return new ClassVisitor(AsmApi.VERSION, classVisitor) { |
| 87 | + final Map<String, Supplier<byte[]>> injectedClasses = new HashMap<>(); |
| 88 | + |
| 89 | + @Override |
| 90 | + public void visitEnd() { |
| 91 | + super.visitEnd(); |
| 92 | + |
| 93 | + if (!injectedClasses.isEmpty()) { |
| 94 | + helperInjector.injectHelperClasses(classLoader, injectedClasses); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public MethodVisitor visitMethod( |
| 100 | + int access, |
| 101 | + String name, |
| 102 | + String descriptor, |
| 103 | + String signature, |
| 104 | + String[] exceptions) { |
| 105 | + MethodVisitor mv = |
| 106 | + super.visitMethod(access, name, descriptor, signature, exceptions); |
| 107 | + return new MethodVisitor(api, mv) { |
| 108 | + @Override |
| 109 | + public void visitInvokeDynamicInsn( |
| 110 | + String name, |
| 111 | + String descriptor, |
| 112 | + Handle bootstrapMethodHandle, |
| 113 | + Object... bootstrapMethodArguments) { |
| 114 | + if (Type.getInternalName(IndyBootstrapDispatcher.class) |
| 115 | + .equals(bootstrapMethodHandle.getOwner())) { |
| 116 | + |
| 117 | + String adviceClassName = (String) bootstrapMethodArguments[3]; |
| 118 | + String forwardClassDotName = |
| 119 | + classLoader == null |
| 120 | + ? bootForwardClassPackage + ".Forward$$" + counter.incrementAndGet() |
| 121 | + : adviceClassName + "$$Forward$$" + counter.incrementAndGet(); |
| 122 | + String forwardClassSlasName = forwardClassDotName.replace('.', '/'); |
| 123 | + |
| 124 | + Supplier<byte[]> forwardClassBytes = |
| 125 | + generateForwardClass( |
| 126 | + forwardClassSlasName, |
| 127 | + name, |
| 128 | + descriptor, |
| 129 | + bootstrapMethodHandle, |
| 130 | + bootstrapMethodArguments); |
| 131 | + injectedClasses.put(forwardClassDotName, forwardClassBytes); |
| 132 | + |
| 133 | + super.visitMethodInsn( |
| 134 | + Opcodes.INVOKESTATIC, forwardClassSlasName, name, descriptor, false); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + super.visitInvokeDynamicInsn( |
| 139 | + name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments); |
| 140 | + } |
| 141 | + }; |
| 142 | + } |
| 143 | + }; |
| 144 | + } |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + private static Supplier<byte[]> generateForwardClass( |
| 149 | + String forwardClassSlasName, |
| 150 | + String methodName, |
| 151 | + String methodDescriptor, |
| 152 | + Handle bootstrapMethodHandle, |
| 153 | + Object[] bootstrapMethodArguments) { |
| 154 | + return () -> { |
| 155 | + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); |
| 156 | + cw.visit( |
| 157 | + Opcodes.V1_8, |
| 158 | + Opcodes.ACC_PUBLIC, |
| 159 | + forwardClassSlasName, |
| 160 | + null, |
| 161 | + Type.getInternalName(Object.class), |
| 162 | + null); |
| 163 | + MethodVisitor mv = |
| 164 | + cw.visitMethod( |
| 165 | + Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, methodName, methodDescriptor, null, null); |
| 166 | + GeneratorAdapter ga = |
| 167 | + new GeneratorAdapter( |
| 168 | + mv, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, methodName, methodDescriptor); |
| 169 | + ga.loadArgs(); |
| 170 | + mv.visitInvokeDynamicInsn( |
| 171 | + methodName, methodDescriptor, bootstrapMethodHandle, bootstrapMethodArguments); |
| 172 | + ga.returnValue(); |
| 173 | + mv.visitMaxs(0, 0); |
| 174 | + mv.visitEnd(); |
| 175 | + cw.visitEnd(); |
| 176 | + |
| 177 | + return cw.toByteArray(); |
| 178 | + }; |
| 179 | + } |
| 180 | +} |
0 commit comments