From 18248a5437c934b28a93ed3975f181e8bf3124c2 Mon Sep 17 00:00:00 2001 From: Peter Hofer Date: Thu, 19 Dec 2024 15:47:39 +0100 Subject: [PATCH] Consistently use DynamicHubCompanion directly from DynamicHub. --- .../snippets/SubstrateAllocationSnippets.java | 6 +- .../com/oracle/svm/core/hub/DynamicHub.java | 68 ++++++--- .../svm/core/hub/DynamicHubCompanion.java | 131 +++--------------- .../src/com/oracle/svm/hosted/SVMHost.java | 4 +- 4 files changed, 73 insertions(+), 136 deletions(-) diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/SubstrateAllocationSnippets.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/SubstrateAllocationSnippets.java index 657a47069e9f..f91f621ebae8 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/SubstrateAllocationSnippets.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/SubstrateAllocationSnippets.java @@ -36,8 +36,6 @@ import java.util.Arrays; import java.util.Map; -import com.oracle.svm.core.graal.meta.KnownOffsets; -import jdk.graal.compiler.core.common.NumUtil; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.word.LocationIdentity; import org.graalvm.word.UnsignedWord; @@ -48,6 +46,7 @@ import com.oracle.svm.core.allocationprofile.AllocationSite; import com.oracle.svm.core.config.ConfigurationValues; import com.oracle.svm.core.configure.ConfigurationFile; +import com.oracle.svm.core.graal.meta.KnownOffsets; import com.oracle.svm.core.graal.meta.SubstrateForeignCallsProvider; import com.oracle.svm.core.graal.nodes.NewPodInstanceNode; import com.oracle.svm.core.graal.nodes.NewStoredContinuationNode; @@ -75,6 +74,7 @@ import jdk.graal.compiler.api.replacements.Snippet.NonNullParameter; import jdk.graal.compiler.api.replacements.Snippet.VarargsParameter; import jdk.graal.compiler.core.common.GraalOptions; +import jdk.graal.compiler.core.common.NumUtil; import jdk.graal.compiler.core.common.spi.ForeignCallDescriptor; import jdk.graal.compiler.core.common.spi.MetaAccessExtensionProvider; import jdk.graal.compiler.core.common.type.StampFactory; @@ -346,7 +346,7 @@ private static DynamicHub slowPathHubOrUnsafeInstantiationError(DynamicHub hub) throw new InstantiationException("Cannot allocate objects of special hybrid types: " + DynamicHub.toClass(hub).getTypeName()); } else { if (hub.canUnsafeInstantiateAsInstanceSlowPath()) { - hub.getCompanion().setUnsafeAllocate(); + hub.setCanUnsafeAllocate(); return hub; } else { if (MissingRegistrationUtils.throwMissingRegistrationErrors()) { diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java index 1c0ac3bab6e3..d69ca0e81a6e 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java @@ -113,6 +113,7 @@ import com.oracle.svm.core.imagelayer.DynamicImageLayerInfo; import com.oracle.svm.core.jdk.JDK21OrEarlier; import com.oracle.svm.core.jdk.JDKLatest; +import com.oracle.svm.core.jdk.ProtectionDomainSupport; import com.oracle.svm.core.jdk.Resources; import com.oracle.svm.core.meta.SharedType; import com.oracle.svm.core.reflect.MissingReflectionRegistrationUtils; @@ -170,6 +171,9 @@ public final class DynamicHub implements AnnotatedElement, java.lang.reflect.Typ @Substitute // private static final Class[] EMPTY_CLASS_ARRAY = new Class[0]; + /** Marker value for {@link DynamicHubCompanion#classLoader}. */ + static final Object NO_CLASS_LOADER = new Object(); + @Platforms(Platform.HOSTED_ONLY.class) // private final Class hostedJavaClass; @@ -395,8 +399,9 @@ public DynamicHub(Class hostedJavaClass, String name, byte hubType, Reference this.flags = flags; - this.companion = new DynamicHubCompanion(hostedJavaClass, hostedJavaClass.getModule(), superType, sourceFileName, - modifiers, classLoader, nestHost, simpleBinaryName, declaringClass, signature); + Object loader = PredefinedClassesSupport.isPredefined(hostedJavaClass) ? NO_CLASS_LOADER : classLoader; + this.companion = DynamicHubCompanion.createHosted(hostedJavaClass.getModule(), superType, sourceFileName, + modifiers, loader, nestHost, simpleBinaryName, declaringClass, signature); } /** @@ -430,10 +435,10 @@ public static DynamicHub allocate(String name, DynamicHub superHub, DynamicHub c // GR-59683 Module module = null; - DynamicHubCompanion companion = new DynamicHubCompanion(classLoader, module, superHub, sourceFileName, modifiers, nestHost, simpleBinaryName, declaringClass, signature); + DynamicHubCompanion companion = DynamicHubCompanion.createAtRuntime(module, superHub, sourceFileName, modifiers, classLoader, nestHost, simpleBinaryName, declaringClass, signature); /* Always allow unsafe allocation for classes that were loaded at run-time. */ - companion.setUnsafeAllocate(); + companion.canUnsafeAllocate = true; // GR-59687: Correct size and content for vtable int vTableEntries = 0x100; @@ -840,18 +845,26 @@ public boolean isInstantiated() { } public boolean canUnsafeInstantiateAsInstanceFastPath() { - return companion.canUnsafeAllocate(); + return canUnsafeAllocate(); } public boolean canUnsafeInstantiateAsInstanceSlowPath() { if (ClassForNameSupport.canUnsafeInstantiateAsInstance(this)) { - companion.setUnsafeAllocate(); + setCanUnsafeAllocate(); return true; } else { return false; } } + public boolean canUnsafeAllocate() { + return companion.canUnsafeAllocate; + } + + public void setCanUnsafeAllocate() { + companion.canUnsafeAllocate = true; + } + public boolean isProxyClass() { return isFlagSet(flags, IS_PROXY_CLASS_BIT); } @@ -1010,18 +1023,21 @@ public InputStream getResourceAsStream(String resourceName) { @Substitute public ClassLoader getClassLoader() { - return companion.getClassLoader(); + Object loader = companion.classLoader; + VMError.guarantee(loader != NO_CLASS_LOADER); + return (ClassLoader) loader; } @KeepOriginal private native ClassLoader getClassLoader0(); public boolean isLoaded() { - return companion.hasClassLoader(); + return companion.classLoader != NO_CLASS_LOADER; } void setClassLoaderAtRuntime(ClassLoader loader) { - companion.setClassLoader(loader); + VMError.guarantee(companion.classLoader == NO_CLASS_LOADER && loader != NO_CLASS_LOADER); + companion.classLoader = loader; } @KeepOriginal @@ -1613,7 +1629,10 @@ public String getPackageName() { if (SubstrateUtil.HOSTED) { // avoid eager initialization in image heap return computePackageName(); } - return companion.getPackageName(this); + if (companion.packageName == null) { + companion.packageName = computePackageName(); + } + return companion.packageName; } private boolean isHybrid() { @@ -1681,7 +1700,10 @@ public Object[] getSigners() { @Substitute public ProtectionDomain getProtectionDomain() { - return companion.getProtectionDomain(); + if (companion.protectionDomain == null) { + companion.protectionDomain = ProtectionDomainSupport.allPermDomain(); + } + return companion.protectionDomain; } @Substitute @@ -1690,7 +1712,8 @@ private ProtectionDomain protectionDomain() { } void setProtectionDomainAtRuntime(ProtectionDomain protectionDomain) { - companion.setProtectionDomain(protectionDomain); + VMError.guarantee(companion.protectionDomain == null && protectionDomain != null); + companion.protectionDomain = protectionDomain; } @Substitute @@ -1940,7 +1963,10 @@ private Class[] getNestMembers0() { @Substitute private ClassRepository getGenericInfo() { - return companion.getGenericInfo(this); + if (companion.genericInfo == null) { + companion.genericInfo = computeGenericInfo(); + } + return (companion.genericInfo != ClassRepository.NONE) ? companion.genericInfo : null; } ClassRepository computeGenericInfo() { @@ -2061,11 +2087,11 @@ private static Constructor[] filterConstructors(Constructor... constructor } public void setJrfEventConfiguration(Object configuration) { - companion.setJfrEventConfiguration(configuration); + companion.jfrEventConfiguration = configuration; } public Object getJfrEventConfiguration() { - return companion.getJfrEventConfiguration(); + return companion.jfrEventConfiguration; } public boolean isReached() { @@ -2075,7 +2101,7 @@ public boolean isReached() { private static final class ReflectionDataAccessors { @SuppressWarnings("unused") private static SoftReference> getReflectionData(DynamicHub that) { - return that.companion.getReflectionData(); + return that.companion.reflectionData; } } @@ -2089,21 +2115,21 @@ private static int getClassRedefinedCount(DynamicHub that) { private static final class ClassLoaderAccessors { @SuppressWarnings("unused") private static ClassLoader getClassLoader(DynamicHub that) { - return that.companion.getClassLoader(); + return that.getClassLoader(); } } private static final class AnnotationDataAccessors { @SuppressWarnings("unused") private static Target_java_lang_Class_AnnotationData getAnnotationData(DynamicHub that) { - return that.companion.getAnnotationData(); + return that.companion.annotationData; } } private static final class AnnotationTypeAccessors { @SuppressWarnings("unused") private static AnnotationType getAnnotationType(DynamicHub that) { - return that.companion.getAnnotationType(); + return that.companion.annotationType; } } @@ -2117,12 +2143,12 @@ private static Constructor getCachedConstructor(DynamicHub that) { * initialized. We eagerly initialize the class to conform with JCK tests. */ that.ensureInitialized(); - return that.companion.getCachedConstructor(); + return that.companion.cachedConstructor; } @SuppressWarnings("unused") private static void setCachedConstructor(DynamicHub that, Constructor value) { - that.companion.setCachedConstructor(value); + that.companion.cachedConstructor = value; } } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHubCompanion.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHubCompanion.java index a3b995654323..62281b4b279a 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHubCompanion.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHubCompanion.java @@ -36,9 +36,7 @@ import com.oracle.svm.core.classinitialization.ClassInitializationInfo; import com.oracle.svm.core.heap.UnknownObjectField; import com.oracle.svm.core.heap.UnknownPrimitiveField; -import com.oracle.svm.core.jdk.ProtectionDomainSupport; import com.oracle.svm.core.meta.SharedType; -import com.oracle.svm.core.util.VMError; import jdk.internal.vm.annotation.Stable; import sun.reflect.annotation.AnnotationType; @@ -51,8 +49,6 @@ * improve sharing between isolates and processes, but could increase image size. */ public final class DynamicHubCompanion { - /** Marker value for {@link #classLoader}. */ - private static final Object NO_CLASS_LOADER = new Object(); /** Field used for module information access at run-time. */ final Module module; @@ -125,41 +121,39 @@ public final class DynamicHubCompanion { @UnknownObjectField(canBeNull = true, availability = BuildPhaseProvider.AfterCompilation.class) // @Stable DynamicHub.DynamicHubMetadata hubMetadata; - private String packageName; /** * Classloader used for loading this class. Most classes have the correct class loader set * already at image build time. {@link PredefinedClassesSupport Predefined classes} get their - * classloader only at run time, before "loading" the field value is {@link #NO_CLASS_LOADER}. + * classloader only at run time, before "loading" the field value is + * {@link DynamicHub#NO_CLASS_LOADER}. */ - private Object classLoader; - private ProtectionDomain protectionDomain; - private ClassRepository genericInfo; - private SoftReference> reflectionData; - private AnnotationType annotationType; - private Target_java_lang_Class_AnnotationData annotationData; - private Constructor cachedConstructor; - private Class newInstanceCallerCache; - private Object jfrEventConfiguration; - private boolean canUnsafeAllocate; + Object classLoader; + + String packageName; + ProtectionDomain protectionDomain; + ClassRepository genericInfo; + SoftReference> reflectionData; + AnnotationType annotationType; + Target_java_lang_Class_AnnotationData annotationData; + Constructor cachedConstructor; + Object jfrEventConfiguration; + @Stable boolean canUnsafeAllocate; @Platforms(Platform.HOSTED_ONLY.class) - DynamicHubCompanion(Class hostedJavaClass, Module module, DynamicHub superHub, String sourceFileName, int modifiers, - ClassLoader classLoader, Class nestHost, String simpleBinaryName, Object declaringClass, String signature) { + static DynamicHubCompanion createHosted(Module module, DynamicHub superHub, String sourceFileName, int modifiers, + Object classLoader, Class nestHost, String simpleBinaryName, Object declaringClass, String signature) { - this(module, superHub, sourceFileName, modifiers, nestHost, simpleBinaryName, declaringClass, signature); - this.classLoader = PredefinedClassesSupport.isPredefined(hostedJavaClass) ? NO_CLASS_LOADER : classLoader; + return new DynamicHubCompanion(module, superHub, sourceFileName, modifiers, classLoader, nestHost, simpleBinaryName, declaringClass, signature); } - DynamicHubCompanion(ClassLoader classLoader, Module module, DynamicHub superHub, String sourceFileName, int modifiers, - Class nestHost, String simpleBinaryName, Object declaringClass, String signature) { - this(module, superHub, sourceFileName, modifiers, nestHost, simpleBinaryName, declaringClass, signature); - + static DynamicHubCompanion createAtRuntime(Module module, DynamicHub superHub, String sourceFileName, int modifiers, + ClassLoader classLoader, Class nestHost, String simpleBinaryName, Object declaringClass, String signature) { assert RuntimeClassLoading.isSupported(); - this.classLoader = classLoader; + return new DynamicHubCompanion(module, superHub, sourceFileName, modifiers, classLoader, nestHost, simpleBinaryName, declaringClass, signature); } private DynamicHubCompanion(Module module, DynamicHub superHub, String sourceFileName, int modifiers, - Class nestHost, String simpleBinaryName, Object declaringClass, String signature) { + Object classLoader, Class nestHost, String simpleBinaryName, Object declaringClass, String signature) { this.module = module; this.superHub = superHub; this.sourceFileName = sourceFileName; @@ -168,90 +162,7 @@ private DynamicHubCompanion(Module module, DynamicHub superHub, String sourceFil this.simpleBinaryName = simpleBinaryName; this.declaringClass = declaringClass; this.signature = signature; - } - - String getPackageName(DynamicHub hub) { - if (packageName == null) { - packageName = hub.computePackageName(); - } - return packageName; - } - - boolean hasClassLoader() { - return classLoader != NO_CLASS_LOADER; - } - - ClassLoader getClassLoader() { - Object loader = classLoader; - VMError.guarantee(loader != NO_CLASS_LOADER); - return (ClassLoader) loader; - } - - void setClassLoader(ClassLoader loader) { - VMError.guarantee(classLoader == NO_CLASS_LOADER && loader != NO_CLASS_LOADER); - classLoader = loader; - } - - ProtectionDomain getProtectionDomain() { - if (protectionDomain == null) { - protectionDomain = ProtectionDomainSupport.allPermDomain(); - } - return protectionDomain; - } - - void setProtectionDomain(ProtectionDomain domain) { - VMError.guarantee(protectionDomain == null && domain != null); - protectionDomain = domain; - } - - public ClassRepository getGenericInfo(DynamicHub hub) { - if (genericInfo == null) { - genericInfo = hub.computeGenericInfo(); - } - return (genericInfo != ClassRepository.NONE) ? genericInfo : null; - } - - SoftReference> getReflectionData() { - return reflectionData; - } - - AnnotationType getAnnotationType() { - return annotationType; - } - - Target_java_lang_Class_AnnotationData getAnnotationData() { - return annotationData; - } - - Constructor getCachedConstructor() { - return cachedConstructor; - } - - void setCachedConstructor(Constructor constructor) { - cachedConstructor = constructor; - } - - Class getNewInstanceCallerCache() { - return newInstanceCallerCache; - } - - void setNewInstanceCallerCache(Class constructor) { - newInstanceCallerCache = constructor; - } - - public void setJfrEventConfiguration(Object configuration) { - jfrEventConfiguration = configuration; - } - public Object getJfrEventConfiguration() { - return jfrEventConfiguration; - } - - public boolean canUnsafeAllocate() { - return canUnsafeAllocate; - } - - public void setUnsafeAllocate() { - canUnsafeAllocate = true; + this.classLoader = classLoader; } } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SVMHost.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SVMHost.java index 8b617d02d40a..7317be302f81 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SVMHost.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SVMHost.java @@ -409,7 +409,7 @@ public void onTypeInstantiated(BigBang bb, AnalysisType type) { if (optionAllowUnsafeAllocationOfAllInstantiatedTypes != null) { if (optionAllowUnsafeAllocationOfAllInstantiatedTypes) { type.registerAsUnsafeAllocated("All types are registered as Unsafe allocated via option -H:+AllowUnsafeAllocationOfAllInstantiatedTypes"); - typeToHub.get(type).getCompanion().setUnsafeAllocate(); + typeToHub.get(type).setCanUnsafeAllocate(); } else { /* * No default registration for unsafe allocation, setting the explicit option has @@ -418,7 +418,7 @@ public void onTypeInstantiated(BigBang bb, AnalysisType type) { } } else if (!missingRegistrationSupport.reportMissingRegistrationErrors(type.getJavaClass())) { type.registerAsUnsafeAllocated("Type is not listed as ThrowMissingRegistrationError and therefore registered as Unsafe allocated automatically for compatibility reasons"); - typeToHub.get(type).getCompanion().setUnsafeAllocate(); + typeToHub.get(type).setCanUnsafeAllocate(); } }