Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject helper resources only once #4573

Merged
merged 2 commits into from
Nov 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public String toString() {

private final Cache<ClassLoader, Boolean> injectedClassLoaders =
Cache.builder().setWeakKeys().build();
private final Cache<ClassLoader, Boolean> resourcesInjectedClassLoaders =
Cache.builder().setWeakKeys().build();

private final List<WeakReference<Object>> helperModules = new CopyOnWriteArrayList<>();

Expand Down Expand Up @@ -147,38 +149,48 @@ public DynamicType.Builder<?> transform(
ClassLoader classLoader,
JavaModule module) {
if (!helperClassNames.isEmpty()) {
classLoader = injectHelperClasses(typeDescription, classLoader, module);
injectHelperClasses(typeDescription, classLoader, module);
}

if (helpersSource != null && !helperResources.isEmpty()) {
for (HelperResource helperResource : helperResources) {
URL resource = helpersSource.getResource(helperResource.getAgentPath());
if (resource == null) {
logger.debug(
"Helper resource {} requested but not found.", helperResource.getAgentPath());
continue;
}

logger.debug(
"Injecting resource onto classloader {} -> {}",
classLoader,
helperResource.getApplicationPath());
HelperResources.register(classLoader, helperResource.getApplicationPath(), resource);
}
if (classLoader != null && helpersSource != null && !helperResources.isEmpty()) {
injectHelperResources(classLoader);
}

return builder;
}

private ClassLoader injectHelperClasses(
private void injectHelperResources(ClassLoader classLoader) {
resourcesInjectedClassLoaders.computeIfAbsent(
classLoader,
cl -> {
for (HelperResource helperResource : helperResources) {
URL resource = helpersSource.getResource(helperResource.getAgentPath());
if (resource == null) {
logger.debug(
"Helper resource {} requested but not found.", helperResource.getAgentPath());
continue;
}

logger.debug(
"Injecting resource onto classloader {} -> {}",
classLoader,
helperResource.getApplicationPath());
HelperResources.register(classLoader, helperResource.getApplicationPath(), resource);
}

return true;
});
}

private void injectHelperClasses(
TypeDescription typeDescription, ClassLoader classLoader, JavaModule module) {
if (classLoader == null) {
classLoader = BOOTSTRAP_CLASSLOADER_PLACEHOLDER;
}
if (classLoader == BOOTSTRAP_CLASSLOADER_PLACEHOLDER && instrumentation == null) {
logger.error(
"Cannot inject helpers into bootstrap classloader without an instance of Instrumentation. Programmer error!");
return classLoader;
return;
}

injectedClassLoaders.computeIfAbsent(
Expand Down Expand Up @@ -217,7 +229,6 @@ private ClassLoader injectHelperClasses(
});

ensureModuleCanReadHelperModules(module);
return classLoader;
}

private Map<String, Class<?>> injectBootstrapClassLoader(Map<String, byte[]> classnameToBytes)
Expand Down