|
| 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 java.lang.reflect.Modifier; |
| 9 | +import net.bytebuddy.ByteBuddy; |
| 10 | +import net.bytebuddy.ClassFileVersion; |
| 11 | +import net.bytebuddy.asm.AsmVisitorWrapper; |
| 12 | +import net.bytebuddy.description.method.MethodDescription; |
| 13 | +import net.bytebuddy.dynamic.DynamicType; |
| 14 | +import net.bytebuddy.dynamic.scaffold.InstrumentedType; |
| 15 | +import net.bytebuddy.implementation.Implementation; |
| 16 | +import net.bytebuddy.implementation.bytecode.ByteCodeAppender; |
| 17 | +import org.objectweb.asm.Label; |
| 18 | +import org.objectweb.asm.MethodVisitor; |
| 19 | +import org.objectweb.asm.Opcodes; |
| 20 | +import org.objectweb.asm.Type; |
| 21 | + |
| 22 | +public class OldBytecode { |
| 23 | + |
| 24 | + private OldBytecode() {} |
| 25 | + |
| 26 | + /** |
| 27 | + * Generates and run a simple class with a {@link #toString()} implementation as if it had been |
| 28 | + * compiled on an older java compiler |
| 29 | + * |
| 30 | + * @param className class name |
| 31 | + * @param version bytecode version |
| 32 | + * @return "toString" |
| 33 | + */ |
| 34 | + public static String generateAndRun(String className, ClassFileVersion version) { |
| 35 | + try (DynamicType.Unloaded<Object> unloadedClass = makeClass(className, version)) { |
| 36 | + Class<?> generatedClass = unloadedClass.load(OldBytecode.class.getClassLoader()).getLoaded(); |
| 37 | + |
| 38 | + return generatedClass.getConstructor().newInstance().toString(); |
| 39 | + |
| 40 | + } catch (Throwable e) { |
| 41 | + throw new RuntimeException(e); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static DynamicType.Unloaded<Object> makeClass( |
| 46 | + String className, ClassFileVersion version) { |
| 47 | + return new ByteBuddy(version) |
| 48 | + .subclass(Object.class) |
| 49 | + // required otherwise stack frames aren't computed when needed |
| 50 | + .visit( |
| 51 | + version.isAtLeast(ClassFileVersion.JAVA_V7) |
| 52 | + ? new ComputeFramesAsmVisitorWrapper() |
| 53 | + : AsmVisitorWrapper.NoOp.INSTANCE) |
| 54 | + .name(className) |
| 55 | + .defineMethod("toString", String.class, Modifier.PUBLIC) |
| 56 | + .intercept(new ToStringMethod()) |
| 57 | + .make(); |
| 58 | + } |
| 59 | + |
| 60 | + private static class ToStringMethod implements Implementation, ByteCodeAppender { |
| 61 | + |
| 62 | + @Override |
| 63 | + public ByteCodeAppender appender(Target implementationTarget) { |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public InstrumentedType prepare(InstrumentedType instrumentedType) { |
| 69 | + return instrumentedType; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Size apply( |
| 74 | + MethodVisitor methodVisitor, |
| 75 | + Context implementationContext, |
| 76 | + MethodDescription instrumentedMethod) { |
| 77 | + |
| 78 | + // Bytecode archeology: |
| 79 | + // |
| 80 | + // JSR and RET bytecode instructions were used to create "subroutines". Those were used |
| 81 | + // in try/catch blocks as an attempt to avoid some bytecode duplication, this was later |
| 82 | + // replaced with inlining. |
| 83 | + // Starting from Java 5, no java compiler is expected to issue bytecode containing them and |
| 84 | + // the JVM bytecode validation will reject it. |
| 85 | + // |
| 86 | + // Java 7 bytecode introduced the concept of "stack map frames", which describe the types of |
| 87 | + // the objects that are stored on the stack during method body execution. |
| 88 | + // |
| 89 | + // As a consequence, the code below allows to test the following combinations: |
| 90 | + // - java 1 to java 4 bytecode with JSR/RET opcodes |
| 91 | + // - java 5 and java 6 bytecode without stack map frames |
| 92 | + // - java 7 and later bytecode with stack map frames, those are automatically added by the |
| 93 | + // ComputeFramesAsmVisitorWrapper. |
| 94 | + // |
| 95 | + boolean useJsrRet = |
| 96 | + implementationContext.getClassFileVersion().isLessThan(ClassFileVersion.JAVA_V5); |
| 97 | + |
| 98 | + if (useJsrRet) { |
| 99 | + // return "toString"; |
| 100 | + // |
| 101 | + // using obsolete JSR/RET instructions |
| 102 | + Label target = new Label(); |
| 103 | + methodVisitor.visitJumpInsn(Opcodes.JSR, target); |
| 104 | + |
| 105 | + methodVisitor.visitVarInsn(Opcodes.ALOAD, 1); |
| 106 | + methodVisitor.visitInsn(Opcodes.ARETURN); |
| 107 | + methodVisitor.visitLabel(target); |
| 108 | + methodVisitor.visitVarInsn(Opcodes.ASTORE, 2); |
| 109 | + methodVisitor.visitLdcInsn("toString"); |
| 110 | + methodVisitor.visitVarInsn(Opcodes.ASTORE, 1); |
| 111 | + methodVisitor.visitVarInsn(Opcodes.RET, 2); |
| 112 | + return new Size(1, 3); |
| 113 | + } else { |
| 114 | + // try { |
| 115 | + // return "toString"; |
| 116 | + // } catch (Throwable e) { |
| 117 | + // return e.getMessage(); |
| 118 | + // } |
| 119 | + // |
| 120 | + // the Throwable exception is added to stack map frames with java7+, and needs to be |
| 121 | + // added when upgrading the bytecode |
| 122 | + Label start = new Label(); |
| 123 | + Label end = new Label(); |
| 124 | + Label handler = new Label(); |
| 125 | + |
| 126 | + methodVisitor.visitTryCatchBlock( |
| 127 | + start, end, handler, Type.getInternalName(Throwable.class)); |
| 128 | + methodVisitor.visitLabel(start); |
| 129 | + methodVisitor.visitLdcInsn("toString"); |
| 130 | + methodVisitor.visitLabel(end); |
| 131 | + |
| 132 | + methodVisitor.visitInsn(Opcodes.ARETURN); |
| 133 | + |
| 134 | + methodVisitor.visitLabel(handler); |
| 135 | + methodVisitor.visitVarInsn(Opcodes.ASTORE, 1); |
| 136 | + methodVisitor.visitVarInsn(Opcodes.ALOAD, 1); |
| 137 | + |
| 138 | + methodVisitor.visitMethodInsn( |
| 139 | + Opcodes.INVOKEVIRTUAL, |
| 140 | + Type.getInternalName(Throwable.class), |
| 141 | + "getMessage", |
| 142 | + Type.getMethodDescriptor(Type.getType(String.class)), |
| 143 | + false); |
| 144 | + methodVisitor.visitInsn(Opcodes.ARETURN); |
| 145 | + |
| 146 | + return new Size(1, 2); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments