diff --git a/CHANGELOG.next-release.md b/CHANGELOG.next-release.md index 6850e01088..a0eb31f7f9 100644 --- a/CHANGELOG.next-release.md +++ b/CHANGELOG.next-release.md @@ -9,6 +9,7 @@ This file contains all changes which are not released yet. # Fixes +- Fix invokedynamic dispatcher initialization when Byte Buddy disables Unsafe class injection by default on JDK 26+ - [#4519](https://github.com/elastic/apm-agent-java/issues/4519) - Fix JVM tool bootstrap check when program arguments contain slashes - [#4269](https://github.com/elastic/apm-agent-java/issues/4269) # Features and enhancements diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/bci/IndyBootstrap.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/bci/IndyBootstrap.java index 3380bc1b06..9e131f074f 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/bci/IndyBootstrap.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/bci/IndyBootstrap.java @@ -31,6 +31,7 @@ import net.bytebuddy.asm.Advice; import net.bytebuddy.dynamic.ClassFileLocator; import net.bytebuddy.dynamic.loading.ClassInjector; +import net.bytebuddy.utility.JavaModule; import org.stagemonitor.configuration.ConfigurationOptionProvider; import org.stagemonitor.util.IOUtils; @@ -265,9 +266,10 @@ public static Method getExceptionHandlerMethod(final Logger logger) { * Injects the {@code java.lang.IndyBootstrapDispatcher} class into the bootstrap class loader if it wasn't already. */ private static Class initIndyBootstrap(final Logger logger) throws Exception { - Class indyBootstrapDispatcherClass = loadClassInBootstrap(INDY_BOOTSTRAP_CLASS_NAME, INDY_BOOTSTRAP_RESOURCE); + Class indyBootstrapDispatcherClass = loadClassInBootstrap(INDY_BOOTSTRAP_CLASS_NAME, INDY_BOOTSTRAP_RESOURCE, Object.class); - if (JvmRuntimeInfo.ofCurrentVM().getMajorVersion() >= 9 && JvmRuntimeInfo.ofCurrentVM().isJ9VM()) { + if (JvmRuntimeInfo.ofCurrentVM().getMajorVersion() >= 9 && JvmRuntimeInfo.ofCurrentVM().isJ9VM() + && !JavaModule.ofType(Object.class).equals(JavaModule.ofType(indyBootstrapDispatcherClass))) { try { logger.info("Overriding IndyBootstrapDispatcher class's module to java.base module. This is required in J9 VMs."); setJavaBaseModule(indyBootstrapDispatcherClass); @@ -291,11 +293,13 @@ private static Class initIndyBootstrap(final Logger logger) throws Exception * * @param className class name * @param resourceName class resource name + * @param lookupAnchor class in the target bootstrap package for lookup injection on Java 9+, or null to use Unsafe * @return class loaded in bootstrap classloader * @throws IOException if unable to open provided resource * @throws ClassNotFoundException if unable to load class in bootstrap CL */ - private static Class loadClassInBootstrap(String className, String resourceName) throws IOException, ClassNotFoundException { + private static Class loadClassInBootstrap(String className, String resourceName, @Nullable Class lookupAnchor) + throws IOException, ClassNotFoundException { Class bootstrapClass; try { // Will return non-null value only if the class has already been loaded. @@ -307,7 +311,23 @@ private static Class loadClassInBootstrap(String className, String resourceNa if (classBytes == null || classBytes.length == 0) { throw new IllegalStateException("Could not locate " + resourceName); } - ClassInjector.UsingUnsafe.ofBootLoader().injectRaw(Collections.singletonMap(className, classBytes)); + if (lookupAnchor != null && JvmRuntimeInfo.ofCurrentVM().getMajorVersion() >= 9) { + String packageName = lookupAnchor.getPackage().getName(); + try { + if (!ElasticApmAgent.openModule(lookupAnchor, IndyBootstrap.class.getClassLoader(), Collections.singleton(packageName))) { + throw new IllegalStateException("Could not open package " + packageName); + } + // Reflective access preserves the Java 7 API baseline. + Object lookup = MethodHandles.class.getMethod("privateLookupIn", Class.class, MethodHandles.Lookup.class) + .invoke(null, lookupAnchor, MethodHandles.lookup()); + ClassInjector.UsingLookup.of(lookup).injectRaw(Collections.singletonMap(className, classBytes)); + } catch (Exception lookupException) { + throw new IllegalStateException("Could not inject " + className + " into bootstrap package " + packageName, + lookupException); + } + } else { + ClassInjector.UsingUnsafe.ofBootLoader().injectRaw(Collections.singletonMap(className, classBytes)); + } bootstrapClass = Class.forName(className, false, null); } return bootstrapClass; @@ -324,7 +344,7 @@ static void setJavaBaseModule(Class targetClass) throws Throwable { // In order to override the original unnamed module assigned to the IndyBootstrapDispatcher, we rely on the // Unsafe API, which requires the caller to be loaded by the Bootstrap CL - Class moduleSetterClass = loadClassInBootstrap(INDY_BOOTSTRAP_MODULE_SETTER_CLASS_NAME, INDY_BOOTSTRAP_MODULE_SETTER_RESOURCE); + Class moduleSetterClass = loadClassInBootstrap(INDY_BOOTSTRAP_MODULE_SETTER_CLASS_NAME, INDY_BOOTSTRAP_MODULE_SETTER_RESOURCE, null); MethodHandles.lookup() .findStatic(moduleSetterClass, "setJavaBaseModule", MethodType.methodType(void.class, Class.class)) .invoke(targetClass); diff --git a/apm-agent-core/src/test/java/co/elastic/apm/agent/bci/IndyBootstrapTest.java b/apm-agent-core/src/test/java/co/elastic/apm/agent/bci/IndyBootstrapTest.java index 5139cf7014..0f8b39ec6c 100644 --- a/apm-agent-core/src/test/java/co/elastic/apm/agent/bci/IndyBootstrapTest.java +++ b/apm-agent-core/src/test/java/co/elastic/apm/agent/bci/IndyBootstrapTest.java @@ -19,18 +19,136 @@ package co.elastic.apm.agent.bci; import co.elastic.apm.agent.AbstractInstrumentationTest; +import co.elastic.apm.agent.MockTracer; +import co.elastic.apm.agent.common.util.ProcessExecutionUtil; +import co.elastic.apm.agent.sdk.logging.Logger; +import co.elastic.apm.agent.sdk.logging.LoggerFactory; +import co.elastic.apm.agent.test.JavaExecutable; +import net.bytebuddy.agent.ByteBuddyAgent; +import net.bytebuddy.dynamic.loading.ClassInjector; import org.junit.jupiter.api.Test; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.concurrent.TimeUnit; + import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assumptions.assumeTrue; class IndyBootstrapTest extends AbstractInstrumentationTest { + @Test + void testDispatcherInitialization() throws Exception { + Logger logger = LoggerFactory.getLogger(IndyBootstrapTest.class); + Method bootstrap = IndyBootstrap.getIndyBootstrapMethod(logger); + Method exceptionHandler = IndyBootstrap.getExceptionHandlerMethod(logger); + Class dispatcher = bootstrap.getDeclaringClass(); + assertDispatcherInJavaBase(dispatcher, exceptionHandler); + + // Exercise initialization again as well as the cached entry points. + Method init = IndyBootstrap.class.getDeclaredMethod("initIndyBootstrap", Logger.class); + init.setAccessible(true); + for (int i = 0; i < 3; i++) { + assertThat(init.invoke(null, logger)).isSameAs(dispatcher); + assertThat(IndyBootstrap.getIndyBootstrapMethod(logger)).isSameAs(bootstrap); + assertThat(IndyBootstrap.getExceptionHandlerMethod(logger)).isSameAs(exceptionHandler); + assertThat(dispatcher.getField("bootstrap").get(null)).isEqualTo(IndyBootstrap.class.getMethod("bootstrap", + MethodHandles.Lookup.class, String.class, MethodType.class, Object[].class)); + assertThat(dispatcher.getField("logAdviceException").get(null)) + .isEqualTo(IndyBootstrap.class.getMethod("logExceptionThrownByAdvice", Throwable.class)); + } + } + + /** + * Byte Buddy reads {@code net.bytebuddy.safe} once and {@link AbstractInstrumentationTest#beforeAll()} has already + * started the agent here, so startup with Unsafe disabled has to be tested in a JVM of its own. + */ + @Test + void testDispatcherInitializationWithoutUnsafe() { + ProcessExecutionUtil.CommandOutput result = ProcessExecutionUtil.executeCommand(Arrays.asList( + JavaExecutable.getBinaryPath(), + "-Dnet.bytebuddy.safe=true", + "-cp", System.getProperty("java.class.path"), + UnsafeDisabledStartup.class.getName() + ), TimeUnit.MINUTES.toMillis(2)); + + assertThat(result.exitedNormally()).as("%s", result).isTrue(); + } + + @Test + void testMissingBootstrapResource() throws Exception { + String className = "java.lang.MissingIndyBootstrapDispatcher"; + String resourceName = "bootstrap/java/lang/MissingIndyBootstrapDispatcher.esclazz"; + assertThatThrownBy(() -> loadClassInBootstrap(className, resourceName)) + .hasCauseInstanceOf(IllegalStateException.class) + .hasRootCauseMessage("Could not locate " + resourceName); + assertThatThrownBy(() -> Class.forName(className, false, null)).isInstanceOf(ClassNotFoundException.class); + } + + @Test + void testModuleOpeningFailure() throws Exception { + String className = "java.lang.UninitializedIndyBootstrapDispatcher"; + Field instrumentation = ElasticApmAgent.class.getDeclaredField("instrumentation"); + instrumentation.setAccessible(true); + Object originalInstrumentation = instrumentation.get(null); + try { + instrumentation.set(null, null); + assertThatThrownBy(() -> loadClassInBootstrap(className, "bootstrap/java/lang/IndyBootstrapDispatcher.esclazz")) + .hasCauseInstanceOf(IllegalStateException.class) + .cause().hasMessageContaining(className).hasMessageContaining("java.lang") + .hasRootCauseMessage("Can't open modules before the agent has been initialized"); + assertThatThrownBy(() -> Class.forName(className, false, null)).isInstanceOf(ClassNotFoundException.class); + } finally { + instrumentation.set(null, originalInstrumentation); + } + } + + private static void assertDispatcherInJavaBase(Class dispatcher, Method exceptionHandler) { + assertThat(dispatcher.getName()).isEqualTo("java.lang.IndyBootstrapDispatcher"); + assertThat(dispatcher.getClassLoader()).isNull(); + assertThat(dispatcher.getModule()).isSameAs(Object.class.getModule()); + assertThat(exceptionHandler.getDeclaringClass()).isSameAs(dispatcher); + } + + private static Object loadClassInBootstrap(String className, String resourceName) throws Exception { + Method load = IndyBootstrap.class.getDeclaredMethod("loadClassInBootstrap", String.class, String.class, Class.class); + load.setAccessible(true); + return load.invoke(null, className, resourceName, Object.class); + } + @Test void testSetJavaBaseModule() throws Throwable { + assumeTrue(ClassInjector.UsingUnsafe.isAvailable(), "The legacy module setter requires Unsafe injection"); Module javaBaseModule = Class.class.getModule(); - assertThat(IndyBootstrapTest.class.getModule()).isNotEqualTo(javaBaseModule); + assertThat(ModuleSetterTarget.class.getModule()).isNotEqualTo(javaBaseModule); + + IndyBootstrap.setJavaBaseModule(ModuleSetterTarget.class); + assertThat(ModuleSetterTarget.class.getModule()).isEqualTo(javaBaseModule); + } + + private static class ModuleSetterTarget { + } + + static class UnsafeDisabledStartup { + + public static void main(String[] args) { + try { + assertThat(ClassInjector.UsingUnsafe.isAvailable()).isFalse(); + ElasticApmAgent.initInstrumentation(MockTracer.createMockInstrumentationSetup().getTracer(), ByteBuddyAgent.install()); - IndyBootstrap.setJavaBaseModule(IndyBootstrapTest.class); - assertThat(IndyBootstrapTest.class.getModule()).isEqualTo(javaBaseModule); + Logger logger = LoggerFactory.getLogger(UnsafeDisabledStartup.class); + Method bootstrap = IndyBootstrap.getIndyBootstrapMethod(logger); + assertDispatcherInJavaBase(bootstrap.getDeclaringClass(), IndyBootstrap.getExceptionHandlerMethod(logger)); + } catch (Throwable t) { + t.printStackTrace(); + System.exit(1); + } + // the tracer leaves non-daemon threads behind + System.exit(0); + } } }