Skip to content

Commit 08ce726

Browse files
committed
[GR-73503] Make SerializationSupport.constructorAccessors use DynamicHub id instead of Class Object as key
PullRequest: graal/23308
2 parents 4a723d0 + 8bbeef4 commit 08ce726

9 files changed

Lines changed: 173 additions & 172 deletions

File tree

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2563,7 +2563,7 @@ public FieldAccessor newFieldAccessor(Field field0, boolean override) {
25632563

25642564
@Substitute
25652565
private Constructor<?> generateConstructor(Class<?> cl, Constructor<?> constructorToCall) {
2566-
ConstructorAccessor acc = (ConstructorAccessor) SerializationSupport.getSerializationConstructorAccessor(cl, constructorToCall.getDeclaringClass());
2566+
ConstructorAccessor acc = (ConstructorAccessor) SerializationSupport.getRuntimeSerializationConstructorAccessor(cl, constructorToCall.getDeclaringClass());
25672567
/*
25682568
* Unlike other root constructors, this constructor is not copied for mutation but directly
25692569
* mutated, as it is not cached. To cache this constructor, setAccessible call must be done

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/serialize/SerializationRegistry.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/serialize/SerializationSupport.java

Lines changed: 115 additions & 78 deletions
Large diffs are not rendered by default.

substratevm/src/com.oracle.svm.hosted/resources/SharedLayerSnapshotCapnProtoSchema.capnp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ struct PersistedAnalysisType {
5555
wrappedType :union {
5656
none @31 :Void; # default
5757
serializationGenerated :group {
58-
rawDeclaringClass @32 :Text;
59-
rawTargetConstructor @33 :Text;
58+
rawDeclaringClassId @32 :TypeId;
59+
rawTargetConstructorId @33 :TypeId;
6060
}
6161
lambda :group {
6262
capturingClass @34 :Text;

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SVMImageLayerLoader.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
import com.oracle.svm.core.meta.MethodRef;
9393
import com.oracle.svm.core.reflect.proxy.DynamicProxySupport;
9494
import com.oracle.svm.core.reflect.serialize.SerializationSupport;
95-
import com.oracle.svm.shared.util.VMError;
9695
import com.oracle.svm.hosted.FeatureImpl;
9796
import com.oracle.svm.hosted.SVMHost;
9897
import com.oracle.svm.hosted.classinitialization.ClassInitializationSupport;
@@ -128,6 +127,7 @@
128127
import com.oracle.svm.shaded.org.capnproto.PrimitiveList;
129128
import com.oracle.svm.shaded.org.capnproto.StructList;
130129
import com.oracle.svm.shaded.org.capnproto.Text;
130+
import com.oracle.svm.shared.util.VMError;
131131
import com.oracle.svm.util.AnnotationUtil;
132132
import com.oracle.svm.util.GuestAccess;
133133
import com.oracle.svm.util.JVMCIReflectionUtil;
@@ -446,14 +446,18 @@ protected boolean delegateLoadType(PersistedAnalysisType.Reader typeData) {
446446
}
447447
if (wrappedType.isSerializationGenerated()) {
448448
SerializationGenerated.Reader sg = wrappedType.getSerializationGenerated();
449-
String rawDeclaringClassName = sg.getRawDeclaringClass().toString();
450-
String rawTargetConstructorClassName = sg.getRawTargetConstructor().toString();
451-
Class<?> rawDeclaringClass = imageLayerBuildingSupport.lookupClass(false, rawDeclaringClassName);
452-
Class<?> rawTargetConstructorClass = imageLayerBuildingSupport.lookupClass(false, rawTargetConstructorClassName);
449+
int rawDeclaringClassId = sg.getRawDeclaringClassId();
450+
int rawTargetConstructorClassId = sg.getRawTargetConstructorId();
451+
AnalysisType rawDeclaringType = getAnalysisTypeForBaseLayerId(rawDeclaringClassId);
452+
AnalysisType rawTargetConstructorType = getAnalysisTypeForBaseLayerId(rawTargetConstructorClassId);
453+
Class<?> rawDeclaringClass = rawDeclaringType.getJavaClass();
454+
Class<?> rawTargetConstructorClass = rawTargetConstructorType.getJavaClass();
453455
Constructor<?> rawTargetConstructor = ReflectionUtil.lookupConstructor(rawTargetConstructorClass);
454456
Constructor<?> constructor = ReflectionFactory.getReflectionFactory().newConstructorForSerialization(rawDeclaringClass, rawTargetConstructor);
455-
SerializationSupport.currentLayer().addConstructorAccessor(rawDeclaringClass, rawTargetConstructorClass, SerializationFeature.getConstructorAccessor(constructor));
456-
Class<?> constructorAccessor = SerializationSupport.getSerializationConstructorAccessor(rawDeclaringClass, rawTargetConstructorClass).getClass();
457+
DynamicHub rawDeclaringHub = typeToHub(rawDeclaringType);
458+
DynamicHub rawTargetConstructorHub = typeToHub(rawTargetConstructorType);
459+
SerializationSupport.currentLayer().addConstructorAccessor(rawDeclaringHub, rawTargetConstructorHub, SerializationFeature.getConstructorAccessor(constructor));
460+
Class<?> constructorAccessor = SerializationSupport.getHostedSerializationConstructorAccessor(rawDeclaringHub, rawTargetConstructorHub).getClass();
457461
metaAccess.lookupJavaType(constructorAccessor);
458462
return true;
459463
} else if (wrappedType.isLambda()) {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SVMImageLayerSnapshotUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@
8181
import com.oracle.svm.hosted.meta.HostedUniverse;
8282
import com.oracle.svm.hosted.thread.VMThreadLocalCollector;
8383
import com.oracle.svm.shared.util.ModuleSupport;
84+
import com.oracle.svm.shared.util.ReflectionUtil;
8485
import com.oracle.svm.shared.util.VMError;
8586
import com.oracle.svm.util.GuestAccess;
8687
import com.oracle.svm.util.JVMCIReflectionUtil;
8788
import com.oracle.svm.util.OriginalMethodProvider;
88-
import com.oracle.svm.shared.util.ReflectionUtil;
8989

9090
import jdk.graal.compiler.api.replacements.SnippetReflectionProvider;
9191
import jdk.graal.compiler.debug.CounterKey;
@@ -340,7 +340,7 @@ private static String getGeneratedSerializationName(AnalysisType type) {
340340
}
341341

342342
private static String generatedSerializationClassName(SerializationSupport.SerializationLookupKey serializationLookupKey) {
343-
return GENERATED_SERIALIZATION + ":" + serializationLookupKey.getDeclaringClass() + "," + serializationLookupKey.getTargetConstructorClass();
343+
return GENERATED_SERIALIZATION + ":" + serializationLookupKey.declaringClassId() + "," + serializationLookupKey.targetConstructorClassId();
344344
}
345345

346346
private static String addModuleName(String elementName, String moduleName) {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SVMImageLayerWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,8 @@ protected void delegatePersistType(AnalysisType type, PersistedAnalysisType.Buil
535535
if (type.toJavaName(true).contains(GENERATED_SERIALIZATION)) {
536536
WrappedType.SerializationGenerated.Builder b = builder.getWrappedType().initSerializationGenerated();
537537
var key = SerializationSupport.currentLayer().getKeyFromConstructorAccessorClass(type.getJavaClass());
538-
b.setRawDeclaringClass(key.getDeclaringClass().getName());
539-
b.setRawTargetConstructor(key.getTargetConstructorClass().getName());
538+
b.setRawDeclaringClassId(key.declaringClassId());
539+
b.setRawTargetConstructorId(key.targetConstructorClassId());
540540
} else if (LambdaUtils.isLambdaType(type)) {
541541
WrappedType.Lambda.Builder b = builder.getWrappedType().initLambda();
542542
b.setCapturingClass(LambdaUtils.capturingClass(type.toJavaName()));

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SharedLayerSnapshotCapnProtoSchemaHolder.java

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
@SuppressWarnings("all")
4343
public final class SharedLayerSnapshotCapnProtoSchemaHolder {
4444
public static class PersistedAnalysisType {
45-
public static final com.oracle.svm.shaded.org.capnproto.StructSize STRUCT_SIZE = new com.oracle.svm.shaded.org.capnproto.StructSize((short)4,(short)14);
45+
public static final com.oracle.svm.shaded.org.capnproto.StructSize STRUCT_SIZE = new com.oracle.svm.shaded.org.capnproto.StructSize((short)5,(short)13);
4646
public static final class Factory extends com.oracle.svm.shaded.org.capnproto.StructFactory<Builder, Reader> {
4747
public Factory() {
4848
}
@@ -360,8 +360,9 @@ public final WrappedType.Builder getWrappedType() {
360360
}
361361
public final WrappedType.Builder initWrappedType() {
362362
_setShortField(7,(short)0);
363+
_setIntField(7,0);
364+
_setIntField(8,0);
363365
_clearPointerField(12);
364-
_clearPointerField(13);
365366
return new PersistedAnalysisType.WrappedType.Builder(segment, data, pointers, dataSize, pointerCount);
366367
}
367368

@@ -550,7 +551,7 @@ public final boolean getIsRecord() {
550551
}
551552

552553
public static class WrappedType {
553-
public static final com.oracle.svm.shaded.org.capnproto.StructSize STRUCT_SIZE = new com.oracle.svm.shaded.org.capnproto.StructSize((short)4,(short)14);
554+
public static final com.oracle.svm.shaded.org.capnproto.StructSize STRUCT_SIZE = new com.oracle.svm.shaded.org.capnproto.StructSize((short)5,(short)13);
554555
public static final class Factory extends com.oracle.svm.shaded.org.capnproto.StructFactory<Builder, Reader> {
555556
public Factory() {
556557
}
@@ -606,8 +607,8 @@ public final SerializationGenerated.Builder getSerializationGenerated() {
606607
}
607608
public final SerializationGenerated.Builder initSerializationGenerated() {
608609
_setShortField(7, (short)PersistedAnalysisType.WrappedType.Which.SERIALIZATION_GENERATED.ordinal());
609-
_clearPointerField(12);
610-
_clearPointerField(13);
610+
_setIntField(7,0);
611+
_setIntField(8,0);
611612
return new PersistedAnalysisType.WrappedType.SerializationGenerated.Builder(segment, data, pointers, dataSize, pointerCount);
612613
}
613614

@@ -693,7 +694,7 @@ public enum Which {
693694
_NOT_IN_SCHEMA,
694695
}
695696
public static class SerializationGenerated {
696-
public static final com.oracle.svm.shaded.org.capnproto.StructSize STRUCT_SIZE = new com.oracle.svm.shaded.org.capnproto.StructSize((short)4,(short)14);
697+
public static final com.oracle.svm.shaded.org.capnproto.StructSize STRUCT_SIZE = new com.oracle.svm.shaded.org.capnproto.StructSize((short)5,(short)13);
697698
public static final class Factory extends com.oracle.svm.shaded.org.capnproto.StructFactory<Builder, Reader> {
698699
public Factory() {
699700
}
@@ -720,55 +721,33 @@ public static final class Builder extends com.oracle.svm.shaded.org.capnproto.St
720721
public final Reader asReader() {
721722
return new Reader(segment, data, pointers, dataSize, pointerCount, 0x7fffffff);
722723
}
723-
public final boolean hasRawDeclaringClass() {
724-
return !_pointerFieldIsNull(12);
724+
public final int getRawDeclaringClassId() {
725+
return _getIntField(7);
725726
}
726-
public final com.oracle.svm.shaded.org.capnproto.Text.Builder getRawDeclaringClass() {
727-
return _getPointerField(com.oracle.svm.shaded.org.capnproto.Text.factory, 12, null, 0, 0);
728-
}
729-
public final void setRawDeclaringClass(com.oracle.svm.shaded.org.capnproto.Text.Reader value) {
730-
_setPointerField(com.oracle.svm.shaded.org.capnproto.Text.factory, 12, value);
731-
}
732-
public final void setRawDeclaringClass(String value) {
733-
_setPointerField(com.oracle.svm.shaded.org.capnproto.Text.factory, 12, new com.oracle.svm.shaded.org.capnproto.Text.Reader(value));
734-
}
735-
public final com.oracle.svm.shaded.org.capnproto.Text.Builder initRawDeclaringClass(int size) {
736-
return _initPointerField(com.oracle.svm.shaded.org.capnproto.Text.factory, 12, size);
727+
public final void setRawDeclaringClassId(int value) {
728+
_setIntField(7, value);
737729
}
738-
public final boolean hasRawTargetConstructor() {
739-
return !_pointerFieldIsNull(13);
740-
}
741-
public final com.oracle.svm.shaded.org.capnproto.Text.Builder getRawTargetConstructor() {
742-
return _getPointerField(com.oracle.svm.shaded.org.capnproto.Text.factory, 13, null, 0, 0);
743-
}
744-
public final void setRawTargetConstructor(com.oracle.svm.shaded.org.capnproto.Text.Reader value) {
745-
_setPointerField(com.oracle.svm.shaded.org.capnproto.Text.factory, 13, value);
746-
}
747-
public final void setRawTargetConstructor(String value) {
748-
_setPointerField(com.oracle.svm.shaded.org.capnproto.Text.factory, 13, new com.oracle.svm.shaded.org.capnproto.Text.Reader(value));
730+
731+
public final int getRawTargetConstructorId() {
732+
return _getIntField(8);
749733
}
750-
public final com.oracle.svm.shaded.org.capnproto.Text.Builder initRawTargetConstructor(int size) {
751-
return _initPointerField(com.oracle.svm.shaded.org.capnproto.Text.factory, 13, size);
734+
public final void setRawTargetConstructorId(int value) {
735+
_setIntField(8, value);
752736
}
737+
753738
}
754739

755740
public static final class Reader extends com.oracle.svm.shaded.org.capnproto.StructReader {
756741
Reader(com.oracle.svm.shaded.org.capnproto.SegmentReader segment, int data, int pointers,int dataSize, short pointerCount, int nestingLimit){
757742
super(segment, data, pointers, dataSize, pointerCount, nestingLimit);
758743
}
759744

760-
public boolean hasRawDeclaringClass() {
761-
return !_pointerFieldIsNull(12);
762-
}
763-
public com.oracle.svm.shaded.org.capnproto.Text.Reader getRawDeclaringClass() {
764-
return _getPointerField(com.oracle.svm.shaded.org.capnproto.Text.factory, 12, null, 0, 0);
745+
public final int getRawDeclaringClassId() {
746+
return _getIntField(7);
765747
}
766748

767-
public boolean hasRawTargetConstructor() {
768-
return !_pointerFieldIsNull(13);
769-
}
770-
public com.oracle.svm.shaded.org.capnproto.Text.Reader getRawTargetConstructor() {
771-
return _getPointerField(com.oracle.svm.shaded.org.capnproto.Text.factory, 13, null, 0, 0);
749+
public final int getRawTargetConstructorId() {
750+
return _getIntField(8);
772751
}
773752

774753
}
@@ -777,7 +756,7 @@ public com.oracle.svm.shaded.org.capnproto.Text.Reader getRawTargetConstructor()
777756

778757

779758
public static class Lambda {
780-
public static final com.oracle.svm.shaded.org.capnproto.StructSize STRUCT_SIZE = new com.oracle.svm.shaded.org.capnproto.StructSize((short)4,(short)14);
759+
public static final com.oracle.svm.shaded.org.capnproto.StructSize STRUCT_SIZE = new com.oracle.svm.shaded.org.capnproto.StructSize((short)5,(short)13);
781760
public static final class Factory extends com.oracle.svm.shaded.org.capnproto.StructFactory<Builder, Reader> {
782761
public Factory() {
783762
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/reflect/serialize/SerializationFeature.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
import org.graalvm.nativeimage.impl.RuntimeReflectionSupport;
6161
import org.graalvm.nativeimage.impl.RuntimeSerializationSupport;
6262

63+
import com.oracle.graal.pointsto.ObjectScanner.OtherReason;
64+
import com.oracle.graal.pointsto.meta.AnalysisMetaAccess;
65+
import com.oracle.graal.pointsto.meta.AnalysisType;
6366
import com.oracle.svm.configure.ConfigurationFile;
6467
import com.oracle.svm.configure.ConfigurationParserOption;
6568
import com.oracle.svm.configure.SerializationConfigurationParser;
@@ -72,11 +75,12 @@
7275
import com.oracle.svm.core.reflect.serialize.SerializationSupport;
7376
import com.oracle.svm.core.reflect.target.ReflectionSubstitutionSupport;
7477
import com.oracle.svm.core.util.BasedOnJDKFile;
75-
import com.oracle.svm.shared.util.VMError;
7678
import com.oracle.svm.hosted.ConditionalConfigurationRegistry;
7779
import com.oracle.svm.hosted.ConfigurationTypeResolver;
7880
import com.oracle.svm.hosted.FeatureImpl;
81+
import com.oracle.svm.hosted.FeatureImpl.BeforeAnalysisAccessImpl;
7982
import com.oracle.svm.hosted.ImageClassLoader;
83+
import com.oracle.svm.hosted.SVMHost;
8084
import com.oracle.svm.hosted.classinitialization.ClassInitializationSupport;
8185
import com.oracle.svm.hosted.config.ConfigurationParserUtils;
8286
import com.oracle.svm.hosted.lambda.LambdaParser;
@@ -85,6 +89,7 @@
8589
import com.oracle.svm.hosted.reflect.ReflectionFeature;
8690
import com.oracle.svm.hosted.reflect.proxy.DynamicProxyFeature;
8791
import com.oracle.svm.hosted.reflect.proxy.ProxyRegistry;
92+
import com.oracle.svm.shared.util.VMError;
8893
import com.oracle.svm.util.GuestAccess;
8994
import com.oracle.svm.util.LogUtils;
9095
import com.oracle.svm.shared.util.ReflectionUtil;
@@ -458,10 +463,16 @@ private void registerConstructorAccessor(AccessCondition cnd, Class<?> serializa
458463

459464
void beforeAnalysis(Feature.BeforeAnalysisAccess beforeAnalysisAccess) {
460465
setAnalysisAccess(beforeAnalysisAccess);
466+
BeforeAnalysisAccessImpl accessImpl = (BeforeAnalysisAccessImpl) beforeAnalysisAccess;
467+
serializationSupport.setObjectRescanner(object -> accessImpl.rescanObject(object, OtherReason.UNKNOWN));
468+
universe = accessImpl.getUniverse();
461469
stubConstructor = newConstructorForSerialization(SerializationSupport.StubForAbstractClass.class, null);
462470
pendingConstructorRegistrations.forEach(Runnable::run);
463471
pendingConstructorRegistrations = null;
464-
serializationSupport.setStubConstructor(stubConstructor);
472+
AnalysisMetaAccess metaAccess = accessImpl.getMetaAccess();
473+
SVMHost hostVM = accessImpl.getHostVM();
474+
serializationSupport.setStubConstructor(hostVM.dynamicHub(metaAccess.lookupJavaType(stubConstructor.getDeclaringClass())));
475+
serializationSupport.setSerializedLambdaClass(hostVM.dynamicHub(metaAccess.lookupJavaType(DynamicHub.class)));
465476
}
466477

467478
private static void registerQueriesForInheritableMethod(boolean preserved, Class<?> clazz, String methodName, Class<?>... args) {
@@ -715,7 +726,11 @@ private Class<?> addConstructorAccessor(Class<?> serializationTargetClass, Class
715726
}
716727

717728
Class<?> targetConstructorClass = targetConstructor.getDeclaringClass();
718-
serializationSupport.addConstructorAccessor(serializationTargetClass, targetConstructorClass, getConstructorAccessor(targetConstructor));
729+
AnalysisMetaAccess analysisMetaAccess = universe.getBigbang().getMetaAccess();
730+
AnalysisType serializationTargetType = analysisMetaAccess.lookupJavaType(serializationTargetClass);
731+
AnalysisType targetConstructorType = analysisMetaAccess.lookupJavaType(targetConstructorClass);
732+
SVMHost hostVM = (SVMHost) universe.hostVM();
733+
serializationSupport.addConstructorAccessor(hostVM.dynamicHub(serializationTargetType), hostVM.dynamicHub(targetConstructorType), getConstructorAccessor(targetConstructor));
719734
return targetConstructorClass;
720735
}
721736
}

0 commit comments

Comments
 (0)