Skip to content

Commit 306bd80

Browse files
committed
Check the value of SubstrateTargetDescription across layer
1 parent ad41afe commit 306bd80

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateTargetDescription.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,32 @@
2525
package com.oracle.svm.core;
2626

2727
import java.util.EnumSet;
28+
import java.util.List;
2829

2930
import org.graalvm.nativeimage.Platform;
3031
import org.graalvm.nativeimage.Platforms;
3132

3233
import com.oracle.svm.core.code.RuntimeCodeCache;
34+
import com.oracle.svm.core.layeredimagesingleton.ImageSingletonLoader;
35+
import com.oracle.svm.core.layeredimagesingleton.ImageSingletonWriter;
36+
import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingleton;
37+
import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingleton.PersistFlags;
38+
import com.oracle.svm.core.traits.BuiltinTraits.AllAccess;
39+
import com.oracle.svm.core.traits.SingletonLayeredCallbacks;
40+
import com.oracle.svm.core.traits.SingletonLayeredCallbacksSupplier;
41+
import com.oracle.svm.core.traits.SingletonLayeredInstallationKind.Independent;
42+
import com.oracle.svm.core.traits.SingletonTrait;
43+
import com.oracle.svm.core.traits.SingletonTraitKind;
44+
import com.oracle.svm.core.traits.SingletonTraits;
45+
import com.oracle.svm.core.util.VMError;
3346

3447
import jdk.vm.ci.code.Architecture;
3548
import jdk.vm.ci.code.TargetDescription;
3649

50+
@SingletonTraits(access = AllAccess.class, layeredCallbacks = SubstrateTargetDescription.LayeredCallbacks.class, layeredInstallationKind = Independent.class)
3751
public class SubstrateTargetDescription extends TargetDescription {
52+
private static final String RUNTIME_CHECKED_CPU_FEATURES = "runtimeCheckedCPUFeatures";
53+
3854
@Platforms(Platform.HOSTED_ONLY.class)
3955
public static boolean shouldInlineObjectsInImageCode() {
4056
return SubstrateOptions.SpawnIsolates.getValue();
@@ -55,4 +71,33 @@ public SubstrateTargetDescription(Architecture arch, boolean isMP, int stackAlig
5571
public EnumSet<?> getRuntimeCheckedCPUFeatures() {
5672
return runtimeCheckedCPUFeatures;
5773
}
74+
75+
static class LayeredCallbacks extends SingletonLayeredCallbacksSupplier {
76+
@Override
77+
public SingletonTrait getLayeredCallbacksTrait() {
78+
SingletonLayeredCallbacks action = new SingletonLayeredCallbacks() {
79+
@Override
80+
public LayeredImageSingleton.PersistFlags doPersist(ImageSingletonWriter writer, Object singleton) {
81+
SubstrateTargetDescription substrateTargetDescription = (SubstrateTargetDescription) singleton;
82+
writer.writeStringList(RUNTIME_CHECKED_CPU_FEATURES, getCPUFeaturesList(substrateTargetDescription));
83+
return PersistFlags.CALLBACK_ON_REGISTRATION;
84+
}
85+
86+
@Override
87+
public void onSingletonRegistration(ImageSingletonLoader loader, Object singleton) {
88+
SubstrateTargetDescription substrateTargetDescription = (SubstrateTargetDescription) singleton;
89+
List<String> previousLayerRuntimeCheckedCPUFeatures = loader.readStringList(RUNTIME_CHECKED_CPU_FEATURES);
90+
List<String> currentLayerRuntimeCheckedCPUFeatures = getCPUFeaturesList(substrateTargetDescription);
91+
VMError.guarantee(previousLayerRuntimeCheckedCPUFeatures.equals(currentLayerRuntimeCheckedCPUFeatures),
92+
"The runtime checked CPU Features should be consistent across layers. The previous layer CPU Features were %s, but the current layer are %s",
93+
previousLayerRuntimeCheckedCPUFeatures, currentLayerRuntimeCheckedCPUFeatures);
94+
}
95+
};
96+
return new SingletonTrait(SingletonTraitKind.LAYERED_CALLBACKS, action);
97+
}
98+
}
99+
100+
private static List<String> getCPUFeaturesList(SubstrateTargetDescription substrateTargetDescription) {
101+
return substrateTargetDescription.runtimeCheckedCPUFeatures.stream().map(Enum::toString).toList();
102+
}
58103
}

0 commit comments

Comments
 (0)