Skip to content

Commit

Permalink
Fix transforming record types (open-telemetry#10052)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored and heyams committed Dec 13, 2023
1 parent c1c21b5 commit 29c1a3f
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.opentelemetry.javaagent.bootstrap.DefineClassHelper.Handler.DefineClassContext;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.description.field.FieldDescription;
Expand Down Expand Up @@ -89,7 +90,7 @@ private static class MetaFactoryClassVisitor extends ClassVisitor {
private final String slashClassName;

MetaFactoryClassVisitor(ClassVisitor cv, String slashClassName) {
super(Opcodes.ASM7, cv);
super(AsmApi.VERSION, cv);
this.slashClassName = slashClassName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi;
import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.field.FieldList;
Expand Down Expand Up @@ -65,7 +66,7 @@ public ClassVisitor wrap(
private static class ClassClassVisitor extends ClassVisitor {

ClassClassVisitor(ClassVisitor cv) {
super(Opcodes.ASM7, cv);
super(AsmApi.VERSION, cv);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.javaagent.instrumentation.kotlinxcoroutines.instrumentationannotations;

import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
Expand All @@ -22,7 +23,7 @@ class ExpandFramesClassVisitor extends ClassVisitor {
private String className;

ExpandFramesClassVisitor(ClassVisitor classVisitor) {
super(Opcodes.ASM9, classVisitor);
super(AsmApi.VERSION, classVisitor);
}

@Override
Expand Down Expand Up @@ -50,7 +51,7 @@ private static class ExpandFramesMethodVisitor extends MethodVisitor {

ExpandFramesMethodVisitor(
MethodVisitor mv, String className, String methodName, int access, String descriptor) {
super(Opcodes.ASM9, mv);
super(AsmApi.VERSION, mv);
if ("<init>".equals(methodName)) {
currentLocals.add(Opcodes.UNINITIALIZED_THIS);
} else if (!Modifier.isStatic(access)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi;
import io.opentelemetry.javaagent.instrumentation.instrumentationannotations.AnnotationExcludedMethods;
import io.opentelemetry.javaagent.instrumentation.kotlinxcoroutines.instrumentationannotations.SpanAttributeUtil.Parameter;
import java.util.Arrays;
Expand Down Expand Up @@ -136,7 +137,7 @@ private static class WithSpanClassVisitor extends ClassVisitor {
String className;

WithSpanClassVisitor(ClassVisitor cv) {
super(Opcodes.ASM9, cv);
super(AsmApi.VERSION, cv);
}

@Override
Expand Down Expand Up @@ -213,7 +214,8 @@ private static MethodVisitor instrument(
source.signature,
source.exceptions.toArray(new String[0]));
GeneratorAdapter generatorAdapter =
new GeneratorAdapter(Opcodes.ASM9, methodNode, source.access, source.name, source.desc) {
new GeneratorAdapter(
AsmApi.VERSION, methodNode, source.access, source.name, source.desc) {
int requestLocal;
int ourContinuationLocal;
int contextLocal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi;
import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.field.FieldList;
Expand Down Expand Up @@ -64,7 +65,7 @@ public ClassVisitor wrap(
private static class StandardWrapperClassVisitor extends ClassVisitor {

StandardWrapperClassVisitor(ClassVisitor cv) {
super(Opcodes.ASM7, cv);
super(AsmApi.VERSION, cv);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.extension.instrumentation.internal;

import org.objectweb.asm.Opcodes;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public final class AsmApi {
public static final int VERSION = Opcodes.ASM9;

private AsmApi() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.opentelemetry.instrumentation.api.internal.cache.weaklockfree.WeakConcurrentMapCleaner;
import io.opentelemetry.javaagent.bootstrap.AgentInitializer;
import io.opentelemetry.javaagent.bootstrap.AgentStarter;
import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi;
import io.opentelemetry.javaagent.tooling.config.EarlyInitAgentConfig;
import java.io.File;
import java.lang.instrument.ClassFileTransformer;
Expand Down Expand Up @@ -151,7 +152,7 @@ public byte[] transform(
ClassReader cr = new ClassReader(classfileBuffer);
ClassWriter cw = new ClassWriter(cr, 0);
ClassVisitor cv =
new ClassVisitor(Opcodes.ASM7, cw) {
new ClassVisitor(AsmApi.VERSION, cw) {
@Override
public MethodVisitor visitMethod(
int access, String name, String descriptor, String signature, String[] exceptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.javaagent.bootstrap.VirtualFieldInstalledMarker;
import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi;
import io.opentelemetry.javaagent.tooling.Utils;
import java.util.Arrays;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -66,7 +67,7 @@ public ClassVisitor wrap(
int writerFlags,
int readerFlags) {

return new ClassVisitor(Opcodes.ASM7, classVisitor) {
return new ClassVisitor(AsmApi.VERSION, classVisitor) {
// We are using Object class name instead of fieldTypeName here because this gets
// injected onto the bootstrap class loader where context class may be unavailable
private final TypeDescription fieldType = TypeDescription.ForLoadedType.of(Object.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi;
import io.opentelemetry.javaagent.tooling.TransformSafeLogger;
import io.opentelemetry.javaagent.tooling.Utils;
import io.opentelemetry.javaagent.tooling.muzzle.VirtualFieldMappings;
Expand Down Expand Up @@ -81,12 +82,12 @@ public ClassVisitor wrap(
int writerFlags,
int readerFlags) {

return new ClassVisitor(Opcodes.ASM7, classVisitor) {
return new ClassVisitor(AsmApi.VERSION, classVisitor) {
@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(Opcodes.ASM7, mv) {
return new MethodVisitor(api, mv) {
/** The most recent objects pushed to the stack. */
private final Object[] stack = {null, null};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.instrumentation.api.internal.cache.Cache;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi;
import io.opentelemetry.javaagent.tooling.Utils;
import io.opentelemetry.javaagent.tooling.muzzle.VirtualFieldMappings;
import java.util.HashMap;
Expand Down Expand Up @@ -109,7 +110,7 @@ public ClassVisitor wrap(
MethodList<?> methods,
int writerFlags,
int readerFlags) {
return new ClassVisitor(Opcodes.ASM7, classVisitor) {
return new ClassVisitor(AsmApi.VERSION, classVisitor) {

private final TypeDescription accessorInterface =
fieldAccessorInterfaces.find(typeName, fieldTypeName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.javaagent.tooling.instrumentation.indy;

import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -63,7 +64,7 @@ static byte[] transform(byte[] bytes) {

TransformationContext context = new TransformationContext();
ClassVisitor cv =
new ClassVisitor(Opcodes.ASM9, cw) {
new ClassVisitor(AsmApi.VERSION, cw) {

@Override
public MethodVisitor visitMethod(
Expand Down Expand Up @@ -282,7 +283,7 @@ private static MethodVisitor instrumentWritableArguments(
List<OutputArgument> writableArguments,
int returnIndex) {
MethodVisitor result =
new MethodVisitor(Opcodes.ASM9, target) {
new MethodVisitor(AsmApi.VERSION, target) {
@Override
public void visitCode() {
AnnotationVisitor av =
Expand Down Expand Up @@ -348,7 +349,7 @@ public void visitInsn(int opcode) {
private static MethodVisitor instrumentWritableReturn(
MethodVisitor target, MethodNode source, OutputArgument writableReturn, int returnIndex) {
MethodVisitor result =
new MethodVisitor(Opcodes.ASM9, target) {
new MethodVisitor(AsmApi.VERSION, target) {
@Override
public void visitCode() {
AnnotationVisitor av =
Expand Down Expand Up @@ -421,7 +422,7 @@ private static MethodVisitor instrumentAdviceLocals(
AtomicInteger dataIndex = new AtomicInteger();

target =
new MethodVisitor(Opcodes.ASM9, target) {
new MethodVisitor(AsmApi.VERSION, target) {
@Override
public AnnotationVisitor visitParameterAnnotation(
int parameter, String descriptor, boolean visible) {
Expand Down Expand Up @@ -738,7 +739,7 @@ private static Object getAnnotationValue(AnnotationNode annotationNode) {

private static MethodVisitor addReturnArray(
TransformationContext context, MethodVisitor target, int returnArraySize) {
return new MethodVisitor(Opcodes.ASM9, target) {
return new MethodVisitor(AsmApi.VERSION, target) {
@Override
public void visitInsn(int opcode) {
if (Opcodes.RETURN == opcode) {
Expand Down Expand Up @@ -777,7 +778,7 @@ public void visitInsn(int opcode) {
* annotation to {@code 0}.
*/
private static MethodVisitor delegateAdvice(TransformationContext context, MethodVisitor target) {
return new MethodVisitor(Opcodes.ASM9, target) {
return new MethodVisitor(AsmApi.VERSION, target) {
@Override
public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
AnnotationVisitor av = super.visitAnnotation(descriptor, visible);
Expand Down Expand Up @@ -818,7 +819,7 @@ public void visitEnd() {

/** If annotation has {@code readOnly} attribute set it to {@code true}. */
private static MethodVisitor makeReadOnly(Class<?> annotationType, MethodVisitor target) {
return new MethodVisitor(Opcodes.ASM9, target) {
return new MethodVisitor(AsmApi.VERSION, target) {

@Override
public AnnotationVisitor visitParameterAnnotation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.google.common.collect.EvictingQueue;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi;
import io.opentelemetry.javaagent.tooling.muzzle.references.ClassRef;
import io.opentelemetry.javaagent.tooling.muzzle.references.ClassRefBuilder;
import io.opentelemetry.javaagent.tooling.muzzle.references.Flag;
Expand Down Expand Up @@ -126,7 +127,7 @@ private static Type underlyingType(Type type) {

ReferenceCollectingClassVisitor(
HelperClassPredicate helperClassPredicate, boolean isAdviceClass) {
super(Opcodes.ASM7);
super(AsmApi.VERSION);
this.helperClassPredicate = helperClassPredicate;
this.isAdviceClass = isAdviceClass;
}
Expand Down Expand Up @@ -252,7 +253,7 @@ public MethodVisitor visitMethod(
MethodVisitor methodVisitor =
super.visitMethod(access, name, descriptor, signature, exceptions);
MethodVisitor methodNode =
new MethodNode(Opcodes.ASM9, access, name, descriptor, signature, exceptions) {
new MethodNode(AsmApi.VERSION, access, name, descriptor, signature, exceptions) {
@Override
public void visitEnd() {
super.visitEnd();
Expand Down Expand Up @@ -312,7 +313,7 @@ private class AdviceReferenceMethodVisitor extends MethodVisitor {
private int currentLineNumber = -1;

public AdviceReferenceMethodVisitor(MethodVisitor methodVisitor) {
super(Opcodes.ASM7, methodVisitor);
super(AsmApi.VERSION, methodVisitor);
}

@Override
Expand Down Expand Up @@ -526,7 +527,7 @@ private class VirtualFieldCollectingMethodVisitor extends MethodVisitor {
private final EvictingQueue<Type> lastTwoClassConstants = EvictingQueue.create(2);

VirtualFieldCollectingMethodVisitor(MethodVisitor methodVisitor) {
super(Opcodes.ASM7, methodVisitor);
super(AsmApi.VERSION, methodVisitor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi;
import io.opentelemetry.javaagent.tooling.muzzle.HelperResource;
import io.opentelemetry.javaagent.tooling.muzzle.HelperResourceBuilderImpl;
import io.opentelemetry.javaagent.tooling.muzzle.InstrumentationModuleMuzzle;
Expand Down Expand Up @@ -98,7 +99,7 @@ private static class GenerateMuzzleMethodsAndFields extends ClassVisitor {
private boolean generateVirtualFieldsMethod = true;

public GenerateMuzzleMethodsAndFields(ClassVisitor classVisitor, URLClassLoader classLoader) {
super(Opcodes.ASM7, classVisitor);
super(AsmApi.VERSION, classVisitor);
this.classLoader = classLoader;
}

Expand Down

0 comments on commit 29c1a3f

Please sign in to comment.