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

Backport reflection registration fix for JFR eventHandler fields #281

Merged
merged 3 commits into from
Aug 17, 2021
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
Expand Up @@ -34,13 +34,11 @@
import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.hosted.RuntimeReflection;

import com.oracle.graal.pointsto.infrastructure.OriginalClassProvider;
import com.oracle.graal.pointsto.infrastructure.SubstitutionProcessor;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.hosted.c.GraalAccess;
import com.oracle.svm.util.ReflectionUtil;

import jdk.jfr.Event;
import jdk.jfr.internal.EventWriter;
Expand Down Expand Up @@ -79,8 +77,6 @@ private static Boolean initEventClass(ResolvedJavaType eventType) throws Runtime
SecuritySupport.registerEvent(newEventClass);
JfrJavaEvents.registerEventClass(newEventClass);
JVM.getJVM().retransformClasses(new Class<?>[]{newEventClass});
Field field = ReflectionUtil.lookupField(newEventClass, "eventHandler"); // EventInstrumentation.FIELD_EVENT_HANDLER
RuntimeReflection.register(field);
return Boolean.TRUE;
} catch (Throwable ex) {
throw VMError.shouldNotReachHere(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@
*/
package com.oracle.svm.jfr;

//Checkstyle: allow reflection

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.oracle.svm.core.util.VMError;
import jdk.vm.ci.meta.ResolvedJavaType;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
Expand Down Expand Up @@ -55,6 +62,7 @@
import jdk.jfr.internal.JVM;
import jdk.jfr.internal.jfc.JFC;
import jdk.vm.ci.meta.MetaAccessProvider;
import org.graalvm.nativeimage.hosted.RuntimeReflection;

/**
* Provides basic JFR support. As this support is both platform-dependent and JDK-specific, the
Expand Down Expand Up @@ -97,6 +105,9 @@
@Platforms({Platform.LINUX.class, Platform.DARWIN.class})
@AutomaticFeature
public class JfrFeature implements Feature {

private final ConcurrentHashMap<Class, Boolean> fieldRegistration = new ConcurrentHashMap<>();

@Override
public boolean isInConfiguration(IsInConfigurationAccess access) {
return JfrEnabled.get();
Expand Down Expand Up @@ -163,4 +174,31 @@ public void beforeCompilation(BeforeCompilationAccess a) {
JfrTraceId.assign(clazz, hub.getTypeID() + 1);
}
}

@Override
public void duringAnalysis(DuringAnalysisAccess access) {
Class<?> eventClass = access.findClassByName("jdk.internal.event.Event");
if (eventClass != null && access.isReachable(eventClass)) {
Set<Class<?>> s = access.reachableSubtypes(eventClass);
for (Class<?> c : s) {
if (c.getCanonicalName().equals("jdk.jfr.Event")
|| c.getCanonicalName().equals("jdk.internal.event.Event")
|| c.getCanonicalName().equals("jdk.jfr.events.AbstractJDKEvent")) {
continue;
}
fieldRegistration.computeIfAbsent(c, this::registerEventHandler);

}
}
}

private boolean registerEventHandler(Class c) {
try {
Field f = c.getDeclaredField("eventHandler");
RuntimeReflection.register(f);
} catch (Exception e) {
throw VMError.shouldNotReachHere("Unable to register eventHandler for: " + c.getCanonicalName(), e);
}
return Boolean.TRUE;
}
}