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 1 commit
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
@@ -0,0 +1,36 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.internal.reflection;

import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

public final class RealInterfaces {
private static final Map<Class<?>, Class<?>[]> realInterfaces =
Collections.synchronizedMap(new WeakHashMap<>());
laurit marked this conversation as resolved.
Show resolved Hide resolved

private RealInterfaces() {}

/**
* Returns the interfaces directly implemented by the given class or interface. We instrument
* Class#getInterfaces() to remove interfaces added by us. This method can be used to get the
* interfaces that the class directly implements including the interfaces that we have added.
*
* @param clazz class or interface
* @return interfaces directly implemented by given class
*/
public static Class<?>[] get(Class<?> clazz) {
// instrumentation calls RealInterfaces#set() from Class#getInterfaces()
laurit marked this conversation as resolved.
Show resolved Hide resolved
Class<?>[] interfaces = clazz.getInterfaces();
Class<?>[] real = realInterfaces.get(clazz);
return real != null ? real : interfaces;
laurit marked this conversation as resolved.
Show resolved Hide resolved
}

static void set(Class<?> clazz, Class<?>[] interfaces) {
realInterfaces.put(clazz, interfaces);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ plugins {
}

dependencies {
bootstrap(project(":instrumentation:internal:internal-reflection:bootstrap"))

compileOnly(project(":javaagent-bootstrap"))

testImplementation(project(":javaagent-bootstrap"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public static Class<?>[] filterInterfaces(Class<?>[] interfaces, Class<?> contai
}
result.add(interfaceClass);
}
if (result.size() != interfaces.length) {
// for classes that have removed interfaces remember what they really have
RealInterfaces.set(containingClass, interfaces);
}
return result.toArray(new Class<?>[0]);
}
}
1 change: 1 addition & 0 deletions javaagent-tooling/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
implementation(project(":instrumentation-api-appender"))
implementation(project(":instrumentation-sdk-appender"))
implementation(project(":muzzle"))
implementation(project(":instrumentation:internal:internal-reflection:bootstrap"))

implementation("io.opentelemetry:opentelemetry-api")
implementation("io.opentelemetry:opentelemetry-api-metrics")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.javaagent.bootstrap.InstrumentationHolder;
import io.opentelemetry.javaagent.bootstrap.VirtualFieldInstalledMarker;
import io.opentelemetry.javaagent.instrumentation.internal.reflection.RealInterfaces;
import io.opentelemetry.javaagent.tooling.HelperInjector;
import io.opentelemetry.javaagent.tooling.TransformSafeLogger;
import io.opentelemetry.javaagent.tooling.instrumentation.InstrumentationModuleInstaller;
Expand Down Expand Up @@ -216,13 +217,12 @@ 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. As we instrument Class#getInterfaces() to remove
* interfaces added by us, we need to use our own helper class to get all the interfaces that
* the class directly implements.
*/
return classBeingRedefined == null
|| Arrays.asList(classBeingRedefined.getInterfaces())
|| Arrays.asList(RealInterfaces.get(classBeingRedefined))
.contains(VirtualFieldInstalledMarker.class);
};
}
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ include(":instrumentation:internal:internal-class-loader:javaagent-integration-t
include(":instrumentation:internal:internal-eclipse-osgi-3.6:javaagent")
include(":instrumentation:internal:internal-lambda:javaagent")
include(":instrumentation:internal:internal-lambda-java9:javaagent")
include(":instrumentation:internal:internal-reflection:bootstrap")
include(":instrumentation:internal:internal-reflection:javaagent")
include(":instrumentation:internal:internal-reflection:javaagent-integration-tests")
include(":instrumentation:internal:internal-url-class-loader:javaagent")
Expand Down