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 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 @@ -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 @@ -78,9 +76,9 @@ private static Boolean initEventClass(ResolvedJavaType eventType) throws Runtime
eventType.initialize();
SecuritySupport.registerEvent(newEventClass);
JfrJavaEvents.registerEventClass(newEventClass);
// the reflection registration for the event handler field is delayed to the JfrFeature
// duringAnalysis callback so it does not not race/interfere with other retransforms
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,15 @@
*/
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 com.oracle.svm.core.util.VMError;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
Expand Down Expand Up @@ -55,6 +60,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 +103,7 @@
@Platforms({Platform.LINUX.class, Platform.DARWIN.class})
@AutomaticFeature
public class JfrFeature implements Feature {

@Override
public boolean isInConfiguration(IsInConfigurationAccess access) {
return JfrEnabled.get();
Expand Down Expand Up @@ -163,4 +170,26 @@ 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) {
// Use canonical name for package private AbstractJDKEvent
if (c.getCanonicalName().equals("jdk.jfr.Event")
|| c.getCanonicalName().equals("jdk.internal.event.Event")
|| c.getCanonicalName().equals("jdk.jfr.events.AbstractJDKEvent")) {
continue;
}
try {
Field f = c.getDeclaredField("eventHandler");
RuntimeReflection.register(f);
} catch (Exception e) {
throw VMError.shouldNotReachHere("Unable to register eventHandler for: " + c.getCanonicalName(), e);
}
}
}
}
}