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

Fix redefinition failure on openj9 #5009

Merged
merged 4 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
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
@@ -0,0 +1,3 @@
plugins {
id("otel.javaagent-bootstrap")
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.javaagent.instrumentation.internal.reflection;

import io.opentelemetry.javaagent.bootstrap.VirtualFieldAccessorMarker;
import io.opentelemetry.javaagent.bootstrap.VirtualFieldDetector;
import io.opentelemetry.javaagent.bootstrap.VirtualFieldInstalledMarker;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -60,16 +61,23 @@ public static Class<?>[] filterInterfaces(Class<?>[] interfaces, Class<?> contai
return interfaces;
}
List<Class<?>> result = new ArrayList<>(interfaces.length);
boolean hasVirtualFieldMarker = false;
for (Class<?> interfaceClass : interfaces) {
// filter out virtual field marker and accessor interfaces
if (interfaceClass == VirtualFieldInstalledMarker.class
|| (VirtualFieldAccessorMarker.class.isAssignableFrom(interfaceClass)
&& interfaceClass.isSynthetic()
&& interfaceClass.getName().contains("VirtualFieldAccessor$"))) {
hasVirtualFieldMarker = true;
continue;
}
result.add(interfaceClass);
}

if (hasVirtualFieldMarker) {
VirtualFieldDetector.markVirtualFieldsPresent(containingClass);
}

return result.toArray(new Class<?>[0]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.bootstrap;

import io.opentelemetry.instrumentation.api.cache.Cache;
import java.util.Arrays;

/** Helper class for detecting whether given class has virtual fields. */
public final class VirtualFieldDetector {

private static final Cache<Class<?>, Boolean> classesWithVirtualFields = Cache.weak();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClassValue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As ClassValue is not mutable, it works best if you can compute the value on first use. Here we can't really compute the value, so the best we could do would be to use a mutable object as value of ClassValue, initialize it to false, and update it when the actual value is known. With current solution we can ignore the classes that don't have virtual fields, with ClassValue we would probably end up initializing a ClassValue for these too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, thx!


private VirtualFieldDetector() {}

/**
* Detect whether given class has virtual fields. This method looks for virtual fields only from
* the specified class not its super classes.
*
* @param clazz a class
* @return true if given class has virtual fields
*/
public static boolean hasVirtualFields(Class<?> clazz) {
// clazz.getInterfaces() needs to be called before reading from classesWithVirtualFields
// as the call to clazz.getInterfaces() triggers adding clazz to that map via instrumentation
// calling VirtualFieldDetector#markVirtualFieldsPresent() from Class#getInterfaces()
Class<?>[] interfaces = clazz.getInterfaces();
// to avoid breaking in case internal-reflection instrumentation is disabled check whether
// interfaces array contains virtual field marker interface
if (Arrays.asList(interfaces).contains(VirtualFieldInstalledMarker.class)) {
return true;
}
return classesWithVirtualFields.get(clazz) != null;
}

public static void markVirtualFieldsPresent(Class<?> clazz) {
classesWithVirtualFields.put(clazz, Boolean.TRUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@

import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.javaagent.bootstrap.InstrumentationHolder;
import io.opentelemetry.javaagent.bootstrap.VirtualFieldInstalledMarker;
import io.opentelemetry.javaagent.bootstrap.VirtualFieldDetector;
import io.opentelemetry.javaagent.tooling.HelperInjector;
import io.opentelemetry.javaagent.tooling.TransformSafeLogger;
import io.opentelemetry.javaagent.tooling.instrumentation.InstrumentationModuleInstaller;
import io.opentelemetry.javaagent.tooling.muzzle.VirtualFieldMappings;
import java.lang.instrument.Instrumentation;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -216,14 +215,10 @@ private static AgentBuilder.RawMatcher safeToInjectFieldsMatcher() {
/*
* The idea here is that we can add fields if class is just being loaded
* (classBeingRedefined == null) and we have to add same fields again if class we added
* fields before is being transformed again. Note: here we assume that Class#getInterfaces()
* returns list of interfaces defined immediately on a given class, not inherited from its
* parents. It looks like current JVM implementation does exactly this but javadoc is not
* explicit about that.
* fields before is being transformed again.
*/
return classBeingRedefined == null
|| Arrays.asList(classBeingRedefined.getInterfaces())
.contains(VirtualFieldInstalledMarker.class);
|| VirtualFieldDetector.hasVirtualFields(classBeingRedefined);
};
}

Expand Down