|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.test; |
| 7 | + |
| 8 | +import static io.opentelemetry.instrumentation.test.utils.ClasspathUtils.isClassLoaded; |
| 9 | +import static io.opentelemetry.instrumentation.test.utils.GcUtils.awaitGc; |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
| 11 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 12 | + |
| 13 | +import io.opentelemetry.javaagent.tooling.AgentInstaller; |
| 14 | +import io.opentelemetry.javaagent.tooling.HelperInjector; |
| 15 | +import io.opentelemetry.javaagent.tooling.Utils; |
| 16 | +import io.opentelemetry.javaagent.tooling.config.EarlyInitAgentConfig; |
| 17 | +import java.lang.ref.WeakReference; |
| 18 | +import java.net.URL; |
| 19 | +import java.net.URLClassLoader; |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.concurrent.atomic.AtomicReference; |
| 24 | +import net.bytebuddy.agent.ByteBuddyAgent; |
| 25 | +import net.bytebuddy.description.type.TypeDescription; |
| 26 | +import net.bytebuddy.dynamic.ClassFileLocator; |
| 27 | +import net.bytebuddy.dynamic.loading.ClassInjector; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +class HelperInjectionTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + void helpersInjectedToNonDelegatingClassloader() throws Exception { |
| 34 | + URL[] helpersSourceUrls = new URL[1]; |
| 35 | + helpersSourceUrls[0] = HelperClass.class.getProtectionDomain().getCodeSource().getLocation(); |
| 36 | + ClassLoader helpersSourceLoader = new URLClassLoader(helpersSourceUrls); |
| 37 | + |
| 38 | + String helperClassName = HelperInjectionTest.class.getPackage().getName() + ".HelperClass"; |
| 39 | + HelperInjector injector = new HelperInjector("test", List.of(helperClassName), List.of(), helpersSourceLoader, null); |
| 40 | + AtomicReference<URLClassLoader> emptyLoader = new AtomicReference<>(new URLClassLoader(new URL[0], null)); |
| 41 | + |
| 42 | + assertThatThrownBy(() -> emptyLoader.get().loadClass(helperClassName)) |
| 43 | + .isInstanceOf(ClassNotFoundException.class); |
| 44 | + |
| 45 | + injector.transform(null, null, emptyLoader.get(), null, null); |
| 46 | + HelperInjector.loadHelperClass(emptyLoader.get(), helperClassName); |
| 47 | + emptyLoader.get().loadClass(helperClassName); |
| 48 | + |
| 49 | + assertThat(isClassLoaded(helperClassName, emptyLoader.get())).isTrue(); |
| 50 | + // injecting into emptyLoader should not cause helper class to be load in the helper source classloader |
| 51 | + assertThat(isClassLoaded(helperClassName, helpersSourceLoader)).isFalse(); |
| 52 | + |
| 53 | + // references to emptyLoader are gone |
| 54 | + emptyLoader.get().close(); // cleanup |
| 55 | + WeakReference<URLClassLoader> ref = new WeakReference<>(emptyLoader.get()); |
| 56 | + emptyLoader.set(null); |
| 57 | + |
| 58 | + awaitGc(ref, Duration.ofSeconds(10)); |
| 59 | + |
| 60 | + // HelperInjector doesn't prevent it from being collected |
| 61 | + assertThat(ref.get()).isNull(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void helpersInjectedOnBootstrapClassloader() throws Exception { |
| 66 | + ByteBuddyAgent.install(); |
| 67 | + AgentInstaller.installBytebuddyAgent( |
| 68 | + ByteBuddyAgent.getInstrumentation(), |
| 69 | + this.getClass().getClassLoader(), |
| 70 | + EarlyInitAgentConfig.create()); |
| 71 | + |
| 72 | + String helperClassName = HelperInjectionTest.class.getPackage().getName() + ".HelperClass"; |
| 73 | + HelperInjector injector = new HelperInjector( |
| 74 | + "test", |
| 75 | + List.of(helperClassName), |
| 76 | + List.of(), |
| 77 | + this.getClass().getClassLoader(), |
| 78 | + ByteBuddyAgent.getInstrumentation()); |
| 79 | + URLClassLoader bootstrapChild = new URLClassLoader(new URL[0], null); |
| 80 | + |
| 81 | + assertThatThrownBy(() -> bootstrapChild.loadClass(helperClassName)) |
| 82 | + .isInstanceOf(ClassNotFoundException.class); |
| 83 | + |
| 84 | + ClassLoader bootstrapClassloader = null; |
| 85 | + injector.transform(null, null, bootstrapClassloader, null, null); |
| 86 | + Class<?> helperClass = bootstrapChild.loadClass(helperClassName); |
| 87 | + |
| 88 | + assertThat(helperClass.getClassLoader()).isEqualTo(bootstrapClassloader); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void checkHardReferencesOnClassInjection() throws Exception { |
| 93 | + String helperClassName = HelperInjectionTest.class.getPackage().getName() + ".HelperClass"; |
| 94 | + |
| 95 | + // Copied from HelperInjector: |
| 96 | + ClassFileLocator locator = ClassFileLocator.ForClassLoader.of(Utils.getAgentClassLoader()); |
| 97 | + byte[] classBytes = locator.locate(helperClassName).resolve(); |
| 98 | + TypeDescription typeDesc = new TypeDescription.Latent( |
| 99 | + helperClassName, 0, null, Collections.emptyList()); |
| 100 | + |
| 101 | + AtomicReference<URLClassLoader> emptyLoader = new AtomicReference<>(new URLClassLoader(new URL[0], null)); |
| 102 | + AtomicReference<ClassInjector> injector = new AtomicReference<>(new ClassInjector.UsingReflection(emptyLoader.get())); |
| 103 | + injector.get().inject(Collections.singletonMap(typeDesc, classBytes)); |
| 104 | + |
| 105 | + WeakReference<ClassInjector> injectorRef = new WeakReference<>(injector.get()); |
| 106 | + injector.set(null); |
| 107 | + |
| 108 | + awaitGc(injectorRef, Duration.ofSeconds(10)); |
| 109 | + |
| 110 | + assertThat(injectorRef.get()).isNull(); |
| 111 | + |
| 112 | + WeakReference<URLClassLoader> loaderRef = new WeakReference<>(emptyLoader.get()); |
| 113 | + emptyLoader.set(null); |
| 114 | + |
| 115 | + awaitGc(loaderRef, Duration.ofSeconds(10)); |
| 116 | + |
| 117 | + assertThat(loaderRef.get()).isNull(); |
| 118 | + } |
| 119 | +} |
0 commit comments