|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.tooling; |
| 7 | + |
| 8 | +import io.opentelemetry.javaagent.bootstrap.DefineClassHelper.Handler; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import org.objectweb.asm.ClassReader; |
| 11 | + |
| 12 | +public class DefineClassHandler implements Handler { |
| 13 | + public static final DefineClassHandler INSTANCE = new DefineClassHandler(); |
| 14 | + private static final ThreadLocal<DefineClassContextImpl> defineClassContext = |
| 15 | + ThreadLocal.withInitial(() -> DefineClassContextImpl.NOP); |
| 16 | + |
| 17 | + private DefineClassHandler() {} |
| 18 | + |
| 19 | + @Override |
| 20 | + public DefineClassContext beforeDefineClass( |
| 21 | + ClassLoader classLoader, String className, byte[] classBytes, int offset, int length) { |
| 22 | + // with OpenJ9 class data sharing we don't get real class bytes |
| 23 | + if (classBytes == null |
| 24 | + || (classBytes.length == 40 |
| 25 | + && new String(classBytes, StandardCharsets.ISO_8859_1) |
| 26 | + .startsWith("J9ROMCLASSCOOKIE"))) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + DefineClassContextImpl context = null; |
| 31 | + // attempt to load super types of currently loaded class |
| 32 | + // for a class to be loaded all of its super types must be loaded, here we just change the order |
| 33 | + // of operations and load super types before transforming the bytes for current class so that |
| 34 | + // we could use these super types for resolving the advice that needs to be applied to current |
| 35 | + // class |
| 36 | + try { |
| 37 | + ClassReader cr = new ClassReader(classBytes, offset, length); |
| 38 | + String superName = cr.getSuperName(); |
| 39 | + if (superName != null) { |
| 40 | + Class.forName(superName.replace('/', '.'), false, classLoader); |
| 41 | + } |
| 42 | + String[] interfaces = cr.getInterfaces(); |
| 43 | + for (String interfaceName : interfaces) { |
| 44 | + Class.forName(interfaceName.replace('/', '.'), false, classLoader); |
| 45 | + } |
| 46 | + } catch (Throwable throwable) { |
| 47 | + // loading of super class or interface failed |
| 48 | + // mark current class as failed to skip matching and transforming it |
| 49 | + // we'll let defining the class proceed as usual so that it would throw the same exception as |
| 50 | + // it does when running without the agent |
| 51 | + context = DefineClassContextImpl.enter(className); |
| 52 | + } |
| 53 | + return context; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void afterDefineClass(DefineClassContext context) { |
| 58 | + if (context != null) { |
| 59 | + context.exit(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Detect whether loading the specified class is known to fail. |
| 65 | + * |
| 66 | + * @param className class being loaded |
| 67 | + * @return true if it is known that loading class with given name will fail |
| 68 | + */ |
| 69 | + public static boolean isFailedClass(String className) { |
| 70 | + DefineClassContextImpl context = defineClassContext.get(); |
| 71 | + return context.failedClassName != null && context.failedClassName.equals(className); |
| 72 | + } |
| 73 | + |
| 74 | + private static class DefineClassContextImpl implements DefineClassContext { |
| 75 | + private static final DefineClassContextImpl NOP = new DefineClassContextImpl(); |
| 76 | + |
| 77 | + private final DefineClassContextImpl previous; |
| 78 | + private final String failedClassName; |
| 79 | + |
| 80 | + private DefineClassContextImpl() { |
| 81 | + previous = null; |
| 82 | + failedClassName = null; |
| 83 | + } |
| 84 | + |
| 85 | + private DefineClassContextImpl(DefineClassContextImpl previous, String failedClassName) { |
| 86 | + this.previous = previous; |
| 87 | + this.failedClassName = failedClassName; |
| 88 | + } |
| 89 | + |
| 90 | + static DefineClassContextImpl enter(String failedClassName) { |
| 91 | + DefineClassContextImpl previous = defineClassContext.get(); |
| 92 | + DefineClassContextImpl context = new DefineClassContextImpl(previous, failedClassName); |
| 93 | + defineClassContext.set(context); |
| 94 | + return context; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void exit() { |
| 99 | + defineClassContext.set(previous); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments