Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ endif::[]
===== Bug fixes
* Fix `HttpUrlConnection` instrumentation issue (affecting distributed tracing as well) when using HTTPS without using
`java.net.HttpURLConnection#disconnect` - {pull}1447[1447]
* Fixes class loading issue that can occur when deploying multiple applications to the same application server - {pull}1458[#1458]

[[release-notes-1.x]]
=== Java Agent version 1.x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import co.elastic.apm.agent.bci.classloading.ExternalPluginClassLoader;
import co.elastic.apm.agent.bci.classloading.IndyPluginClassLoader;
import co.elastic.apm.agent.bci.classloading.LookupExposer;
import co.elastic.apm.agent.sdk.state.GlobalState;
import co.elastic.apm.agent.util.JvmRuntimeInfo;
import co.elastic.apm.agent.util.PackageScanner;
Expand All @@ -43,6 +44,7 @@
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -272,12 +274,13 @@ public static ConstantCallSite bootstrap(MethodHandles.Lookup lookup,
MethodHandle instrumentedMethod = args.length >= 5 ? (MethodHandle) args[4] : null;

ClassLoader adviceClassLoader = ElasticApmAgent.getClassLoader(adviceClassName);
List<String> pluginClasses;
List<String> pluginClasses = new ArrayList<>();
if (adviceClassLoader instanceof ExternalPluginClassLoader) {
pluginClasses = ((ExternalPluginClassLoader) adviceClassLoader).getClassNames();
pluginClasses.addAll(((ExternalPluginClassLoader) adviceClassLoader).getClassNames());
} else {
pluginClasses = getClassNamesFromBundledPlugin(adviceClassName, adviceClassLoader);
pluginClasses.addAll(getClassNamesFromBundledPlugin(adviceClassName, adviceClassLoader));
}
pluginClasses.add(LookupExposer.class.getName());
ClassLoader pluginClassLoader = IndyPluginClassLoaderFactory.getOrCreatePluginClassLoader(
lookup.lookupClass().getClassLoader(),
pluginClasses,
Expand All @@ -287,7 +290,10 @@ public static ConstantCallSite bootstrap(MethodHandles.Lookup lookup,
// also, this plugin is used as a dependency in other plugins
.or(nameStartsWith("co.elastic.apm.agent.concurrent")));
Class<?> adviceInPluginCL = pluginClassLoader.loadClass(adviceClassName);
MethodHandle methodHandle = MethodHandles.lookup().findStatic(adviceInPluginCL, adviceMethodName, adviceMethodType);
Class<LookupExposer> lookupExposer = (Class<LookupExposer>) pluginClassLoader.loadClass(LookupExposer.class.getName());
// can't use MethodHandle.lookup(), see also https://github.com/elastic/apm-agent-java/issues/1450
MethodHandles.Lookup indyLookup = (MethodHandles.Lookup) lookupExposer.getMethod("getLookup").invoke(null);
MethodHandle methodHandle = indyLookup.findStatic(adviceInPluginCL, adviceMethodName, adviceMethodType);
return new ConstantCallSite(methodHandle);
} catch (Exception e) {
// must not be a static field as it would initialize logging before it's ready
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*-
* #%L
* Elastic APM Java agent
* %%
* Copyright (C) 2018 - 2020 Elastic and contributors
* %%
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* #L%
*/
package co.elastic.apm.agent.bci.classloading;

import java.lang.invoke.MethodHandles;

/**
* This class is injected into every {@link IndyPluginClassLoader} in {@link co.elastic.apm.agent.bci.IndyBootstrap#bootstrap}
* so that the bootstrap can use a {@link MethodHandles.Lookup} with a lookup class from within the {@link IndyPluginClassLoader},
* instead of calling {@link MethodHandles#lookup()} which uses the caller class as the lookup class.
* <p>
* This circumvents a nasty JVM bug that's described <a href="https://github.com/elastic/apm-agent-java/issues/1450">here</a>.
* </p>
*/
public class LookupExposer {

public static MethodHandles.Lookup getLookup() {
return MethodHandles.lookup();
}
}