Skip to content

Commit

Permalink
Redirect constant pool through the shadow heap.
Browse files Browse the repository at this point in the history
  • Loading branch information
cstancu committed Sep 16, 2023
1 parent 6a4a591 commit 1ed2a58
Show file tree
Hide file tree
Showing 20 changed files with 221 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ public interface SnippetReflectionProvider {
*/
<T> T asObject(Class<T> type, JavaConstant constant);

/**
* Get the wrapped constant object, if any.
*/
default JavaConstant unwrapConstant(JavaConstant constant) {
return constant;
}

/**
* Creates a boxed constant for the given kind from an Object. The object needs to be of the
* Java boxed type corresponding to the kind.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ public String toString(BigBang bb) {
}

public static class OtherReason extends ScanReason {
public static final ScanReason UNKNOWN = new OtherReason("manually created constant");
public static final ScanReason RESCAN = new OtherReason("manually triggered rescan");
public static final ScanReason HUB = new OtherReason("scanning a class constant");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import com.oracle.graal.pointsto.api.HostVM;
import com.oracle.graal.pointsto.constraints.UnsupportedFeatureException;
import com.oracle.graal.pointsto.heap.value.ValueSupplier;
import com.oracle.graal.pointsto.infrastructure.UniverseMetaAccess;
import com.oracle.graal.pointsto.meta.AnalysisField;
import com.oracle.graal.pointsto.meta.AnalysisMetaAccess;
import com.oracle.graal.pointsto.meta.AnalysisType;
Expand Down Expand Up @@ -196,9 +195,6 @@ protected ImageHeapConstant getOrCreateImageHeapConstant(JavaConstant javaConsta
ScanReason nonNullReason = Objects.requireNonNull(reason);
Object existingTask = imageHeap.getSnapshot(javaConstant);
if (existingTask == null) {
if (universe.sealed()) {
throw AnalysisError.shouldNotReachHere("Universe is sealed. New constant reachable: " + javaConstant.toValueString());
}
AnalysisFuture<ImageHeapConstant> newTask = new AnalysisFuture<>(() -> {
ImageHeapConstant imageHeapConstant = createImageHeapObject(javaConstant, nonNullReason);
/* When the image heap object is created replace the future in the map. */
Expand Down Expand Up @@ -434,19 +430,20 @@ private void notifyAnalysis(ImageHeapArray array, AnalysisType arrayType, int el
}

private boolean isNonNullObjectConstant(JavaConstant constant) {
return constant.getJavaKind() == JavaKind.Object && constant.isNonNull() && !isWordType(constant, metaAccess);
return constant.getJavaKind() == JavaKind.Object && constant.isNonNull() && !isWordType(constant);
}

public static boolean isWordType(JavaConstant rawElementValue, UniverseMetaAccess metaAccess) {
return metaAccess.isInstanceOf(rawElementValue, WordBase.class);
public boolean isWordType(JavaConstant rawElementValue) {
Object obj = snippetReflection.asObject(Object.class, rawElementValue);
return obj instanceof WordBase;
}

private boolean notifyAnalysis(JavaConstant array, AnalysisType arrayType, JavaConstant elementValue, int elementIndex, ScanReason reason) {
boolean analysisModified;
if (elementValue.isNull()) {
analysisModified = scanningObserver.forNullArrayElement(array, arrayType, elementIndex, reason);
} else {
if (isWordType(elementValue, metaAccess)) {
if (isWordType(elementValue)) {
return false;
}
AnalysisType elementType = metaAccess.lookupJavaType(elementValue);
Expand Down Expand Up @@ -682,7 +679,10 @@ protected Object asObject(JavaConstant constant) {
}

public JavaConstant asConstant(Object object) {
return snippetReflection.forObject(object);
if (object instanceof ImageHeapConstant constant) {
return constant;
}
return universe.getSnippetReflection().forObject(object);
}

public void cleanupAfterAnalysis() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
*/
package com.oracle.graal.pointsto.infrastructure;

import com.oracle.graal.pointsto.api.HostVM;
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;

import com.oracle.graal.pointsto.api.HostVM;

import jdk.vm.ci.meta.ConstantPool;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaField;
Expand Down Expand Up @@ -61,6 +62,9 @@ public interface Universe {

JavaConstant lookup(JavaConstant constant);

/** Redirect constant lookup through the shadow heap. */
JavaConstant shadowHeapLookup(JavaConstant constant);

ResolvedJavaMethod resolveSubstitution(ResolvedJavaMethod method);

ResolvedJavaType objectType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import org.graalvm.compiler.core.common.BootstrapMethodIntrospection;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.serviceprovider.GraalServices;

import com.oracle.graal.pointsto.constraints.UnresolvedElementException;
import com.oracle.svm.util.ReflectionUtil;
Expand All @@ -44,7 +45,6 @@
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
import org.graalvm.compiler.serviceprovider.GraalServices;

public class WrappedConstantPool implements ConstantPool, ConstantPoolPatch {

Expand All @@ -63,6 +63,10 @@ public int length() {
return wrapped.length();
}

private JavaConstant lookupConstant(JavaConstant constant) {
return universe.shadowHeapLookup(constant);
}

/**
* The method jdk.vm.ci.meta.ConstantPool#lookupBootstrapMethodInvocation(int cpi, int opcode)
* was introduced in JVMCI 22.1.
Expand Down Expand Up @@ -155,7 +159,7 @@ public WrappedSignature lookupSignature(int cpi) {

@Override
public JavaConstant lookupAppendix(int cpi, int opcode) {
return universe.lookup(wrapped.lookupAppendix(cpi, opcode));
return lookupConstant(wrapped.lookupAppendix(cpi, opcode));
}

@Override
Expand All @@ -179,7 +183,7 @@ public Object lookupConstant(int cpi, boolean resolve) {
return con;
}
} else if (con instanceof JavaConstant) {
return universe.lookup((JavaConstant) con);
return lookupConstant((JavaConstant) con);
} else if (con == null && resolve == false) {
return null;
} else {
Expand Down Expand Up @@ -251,7 +255,7 @@ public String getName() {
public JavaConstant getType() {
if (bsmGetType != null) {
try {
return universe.lookup((JavaConstant) bsmGetType.invoke(wrapped));
return lookupConstant((JavaConstant) bsmGetType.invoke(wrapped));
} catch (Throwable t) {
throw GraalError.shouldNotReachHere(t); // ExcludeFromJacocoGeneratedReport
}
Expand All @@ -264,7 +268,7 @@ public List<JavaConstant> getStaticArguments() {
if (bsmGetStaticArguments != null) {
try {
List<?> original = (List<?>) bsmGetStaticArguments.invoke(wrapped);
return original.stream().map(e -> universe.lookup((JavaConstant) e)).collect(Collectors.toList());
return original.stream().map(e -> lookupConstant((JavaConstant) e)).collect(Collectors.toList());
} catch (Throwable t) {
throw GraalError.shouldNotReachHere(t); // ExcludeFromJacocoGeneratedReport
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ public Field getJavaField() {

@Override
public JavaConstant getConstantValue() {
return getUniverse().lookup(getWrapped().getConstantValue());
return getUniverse().shadowHeapLookup(getWrapped().getConstantValue());
}

public void addAnalysisFieldObserver(AnalysisFieldObserver observer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

import com.oracle.graal.pointsto.AnalysisPolicy;
import com.oracle.graal.pointsto.BigBang;
import com.oracle.graal.pointsto.ObjectScanner;
import com.oracle.graal.pointsto.api.HostVM;
import com.oracle.graal.pointsto.constraints.UnsupportedFeatureException;
import com.oracle.graal.pointsto.heap.HeapSnapshotVerifier;
Expand Down Expand Up @@ -516,6 +517,14 @@ public JavaConstant lookup(JavaConstant constant) {
}
}

@Override
public JavaConstant shadowHeapLookup(JavaConstant constant) {
if (constant == null || constant.isNull() || constant.getJavaKind().isPrimitive()) {
return constant;
}
return heapScanner.createImageHeapConstant(lookup(constant), ObjectScanner.OtherReason.UNKNOWN);
}

public JavaConstant toHosted(JavaConstant constant) {
if (constant == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public JavaConstant readArrayElement(JavaConstant array, int index) {

if (a instanceof Object[]) {
Object element = ((Object[]) a)[index];
return SubstrateObjectConstant.forObject(element);
return forObject(element);
} else {
return JavaConstant.forBoxedPrimitive(Array.get(a, index));
}
Expand All @@ -92,10 +92,9 @@ public void forEachArrayElement(JavaConstant array, ObjIntConsumer<JavaConstant>
return;
}

if (obj instanceof Object[]) {
Object[] a = (Object[]) obj;
if (obj instanceof Object[] a) {
for (int index = 0; index < a.length; index++) {
consumer.accept((SubstrateObjectConstant.forObject(a[index])), index);
consumer.accept((forObject(a[index])), index);
}
} else {
for (int index = 0; index < Array.getLength(obj); index++) {
Expand Down Expand Up @@ -128,6 +127,10 @@ public JavaConstant forString(String value) {
return SubstrateObjectConstant.forObject(value);
}

protected JavaConstant forObject(Object object) {
return SubstrateObjectConstant.forObject(object);
}

@Override
public MethodHandleAccessProvider getMethodHandleAccess() {
throw shouldNotReachHereAtRuntime(); // ExcludeFromJacocoGeneratedReport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.graalvm.nativeimage.hosted.Feature.BeforeHeapLayoutAccess;

import com.oracle.graal.pointsto.constraints.UnsupportedFeatureException;
import com.oracle.graal.pointsto.heap.ImageHeapConstant;
import com.oracle.graal.pointsto.meta.AnalysisField;
import com.oracle.graal.pointsto.meta.AnalysisMethod;
import com.oracle.graal.pointsto.meta.AnalysisType;
Expand Down Expand Up @@ -195,6 +196,9 @@ public Object apply(Object source) {
dest = createType((ResolvedJavaType) source);
} else if (source instanceof FieldLocationIdentity && !(source instanceof SubstrateFieldLocationIdentity)) {
dest = createFieldLocationIdentity((FieldLocationIdentity) source);
} else if (source instanceof ImageHeapConstant heapConstant) {
dest = heapConstant.getHostedObject();
assert dest != null;
}

assert dest != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@
import com.oracle.svm.core.graal.meta.SubstrateForeignCallsProvider;
import com.oracle.svm.core.graal.meta.SubstrateLoweringProvider;
import com.oracle.svm.core.graal.meta.SubstrateReplacements;
import com.oracle.svm.core.graal.meta.SubstrateSnippetReflectionProvider;
import com.oracle.svm.core.graal.meta.SubstrateStampProvider;
import com.oracle.svm.core.graal.phases.CollectDeoptimizationSourcePositionsPhase;
import com.oracle.svm.core.graal.phases.DeadStoreRemovalPhase;
Expand Down Expand Up @@ -284,6 +283,7 @@
import com.oracle.svm.hosted.image.NativeImageCodeCacheFactory;
import com.oracle.svm.hosted.image.NativeImageHeap;
import com.oracle.svm.hosted.jdk.localization.LocalizationFeature;
import com.oracle.svm.hosted.meta.AnalysisSnippetReflectionProvider;
import com.oracle.svm.hosted.meta.HostedConstantReflectionProvider;
import com.oracle.svm.hosted.meta.HostedField;
import com.oracle.svm.hosted.meta.HostedInterface;
Expand Down Expand Up @@ -614,8 +614,8 @@ protected void doRun(Map<Method, CEntryPointData> entryPoints,
BuildPhaseProvider.markHostedUniverseBuilt();
ClassInitializationSupport classInitializationSupport = bb.getHostVM().getClassInitializationSupport();
SubstratePlatformConfigurationProvider platformConfig = getPlatformConfig(hMetaAccess);
runtimeConfiguration = new HostedRuntimeConfigurationBuilder(options, bb.getHostVM(), hUniverse, hMetaAccess, bb.getProviders(MultiMethod.ORIGINAL_METHOD), classInitializationSupport,
GraalAccess.getOriginalProviders().getLoopsDataProvider(), platformConfig).build();
runtimeConfiguration = new HostedRuntimeConfigurationBuilder(options, aUniverse.getHeapScanner(), bb.getHostVM(), hUniverse, hMetaAccess, bb.getProviders(MultiMethod.ORIGINAL_METHOD),
classInitializationSupport, GraalAccess.getOriginalProviders().getLoopsDataProvider(), platformConfig).build();

registerGraphBuilderPlugins(featureHandler, runtimeConfiguration, (HostedProviders) runtimeConfiguration.getProviders(), bb.getMetaAccess(), aUniverse,
hMetaAccess, hUniverse,
Expand Down Expand Up @@ -937,6 +937,7 @@ protected void setupNativeImage(OptionValues options, Map<Method, CEntryPointDat
ImageHeapScanner heapScanner = new SVMImageHeapScanner(bb, imageHeap, loader, aMetaAccess, aProviders.getSnippetReflection(),
aProviders.getConstantReflection(), aScanningObserver);
aUniverse.setHeapScanner(heapScanner);
((HostedSnippetReflectionProvider) aProviders.getSnippetReflection()).setHeapScanner(heapScanner);
HeapSnapshotVerifier heapVerifier = new SVMImageHeapVerifier(bb, imageHeap, heapScanner);
aUniverse.setHeapVerifier(heapVerifier);

Expand Down Expand Up @@ -1024,7 +1025,7 @@ public static AnalysisUniverse createAnalysisUniverse(OptionValues options, Targ
analysisFactory = new PointsToAnalysisFactory();
}
SubstrateAnnotationExtractor annotationExtractor = (SubstrateAnnotationExtractor) loader.classLoaderSupport.annotationExtractor;
SubstrateSnippetReflectionProvider snippetReflection = new SubstrateSnippetReflectionProvider(new SubstrateWordTypes(originalMetaAccess, FrameAccess.getWordKind()));
AnalysisSnippetReflectionProvider snippetReflection = new AnalysisSnippetReflectionProvider(new SubstrateWordTypes(originalMetaAccess, FrameAccess.getWordKind()));
return new AnalysisUniverse(hostVM, target.wordJavaKind, analysisPolicy, aSubstitutions, originalMetaAccess, snippetReflection, analysisFactory, annotationExtractor);
}

Expand Down Expand Up @@ -1152,7 +1153,7 @@ private static HostedProviders createHostedProviders(TargetDescription target, A

WordTypes aWordTypes = new SubstrateWordTypes(aMetaAccess, FrameAccess.getWordKind());

HostedSnippetReflectionProvider aSnippetReflection = new HostedSnippetReflectionProvider(aWordTypes);
HostedSnippetReflectionProvider aSnippetReflection = new HostedSnippetReflectionProvider(null, aWordTypes);

MetaAccessExtensionProvider aMetaAccessExtensionProvider = HostedConfiguration.instance().createAnalysisMetaAccessExtensionProvider();

Expand Down Expand Up @@ -1314,7 +1315,9 @@ public static void registerGraphBuilderPlugins(FeatureHandler featureHandler, Ru
TargetDescription target, boolean supportsStubBasedPlugins) {
GraphBuilderConfiguration.Plugins plugins = new GraphBuilderConfiguration.Plugins(new SubstitutionInvocationPlugins(annotationSubstitutionProcessor));

WordOperationPlugin wordOperationPlugin = new SubstrateWordOperationPlugins(providers.getSnippetReflection(), providers.getConstantReflection(), providers.getWordTypes(),
HostedSnippetReflectionProvider hostedSnippetReflection = new HostedSnippetReflectionProvider(aUniverse.getHeapScanner(), new SubstrateWordTypes(aMetaAccess, FrameAccess.getWordKind()));

WordOperationPlugin wordOperationPlugin = new SubstrateWordOperationPlugins(hostedSnippetReflection, providers.getConstantReflection(), providers.getWordTypes(),
providers.getPlatformConfigurationProvider().getBarrierSet());

SubstrateReplacements replacements = (SubstrateReplacements) providers.getReplacements();
Expand All @@ -1329,13 +1332,13 @@ public static void registerGraphBuilderPlugins(FeatureHandler featureHandler, Ru
plugins.appendNodePlugin(new MethodHandleWithExceptionPlugin(providers.getConstantReflection().getMethodHandleAccess(), false));
}
} else {
plugins.appendNodePlugin(new IntrinsifyMethodHandlesInvocationPlugin(reason, providers, aUniverse, hUniverse));
plugins.appendNodePlugin(new IntrinsifyMethodHandlesInvocationPlugin(reason, hostedSnippetReflection, providers, aUniverse, hUniverse));
}
plugins.appendNodePlugin(new DeletedFieldsPlugin());
plugins.appendNodePlugin(new InjectedAccessorsPlugin());
plugins.appendNodePlugin(new EarlyConstantFoldLoadFieldPlugin(providers.getMetaAccess()));
plugins.appendNodePlugin(new ConstantFoldLoadFieldPlugin(reason));
plugins.appendNodePlugin(new CInterfaceInvocationPlugin(providers.getMetaAccess(), providers.getSnippetReflection(), providers.getWordTypes(), nativeLibs));
plugins.appendNodePlugin(new CInterfaceInvocationPlugin(providers.getMetaAccess(), hostedSnippetReflection, providers.getWordTypes(), nativeLibs));
plugins.appendNodePlugin(new LocalizationFeature.CharsetNodePlugin());

plugins.appendInlineInvokePlugin(wordOperationPlugin);
Expand All @@ -1347,8 +1350,6 @@ public static void registerGraphBuilderPlugins(FeatureHandler featureHandler, Ru

featureHandler.forEachGraalFeature(feature -> feature.registerGraphBuilderPlugins(providers, plugins, reason));

HostedSnippetReflectionProvider hostedSnippetReflection = new HostedSnippetReflectionProvider(new SubstrateWordTypes(aMetaAccess, FrameAccess.getWordKind()));

NodeIntrinsificationProvider nodeIntrinsificationProvider;
if (SubstrateUtil.isBuildingLibgraal()) {
HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) HotSpotJVMCIRuntime.runtime().getCompiler();
Expand Down Expand Up @@ -1382,7 +1383,7 @@ public <T> T getInjectedArgument(Class<T> type) {
}

final boolean useExactMathPlugins = SubstrateOptions.useLIRBackend();
registerInvocationPlugins(providers.getSnippetReflection(), plugins.getInvocationPlugins(), replacements,
registerInvocationPlugins(hostedSnippetReflection, plugins.getInvocationPlugins(), replacements,
useExactMathPlugins, true, supportsStubBasedPlugins, providers.getLowerer());

Architecture architecture = ConfigurationValues.getTarget().arch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.graalvm.nativeimage.Platforms;
import org.graalvm.word.WordBase;

import com.oracle.graal.pointsto.ObjectScanner;
import com.oracle.graal.pointsto.heap.ImageHeapArray;
import com.oracle.graal.pointsto.heap.ImageHeapConstant;
import com.oracle.graal.pointsto.heap.ImageHeapInstance;
Expand Down Expand Up @@ -366,6 +367,24 @@ public JavaConstant asJavaClass(ResolvedJavaType type) {
return SubstrateObjectConstant.forObject(getHostVM().dynamicHub(type));
}

@Override
public JavaConstant forString(String value) {
if (value == null) {
return JavaConstant.NULL_POINTER;
}
return universe.getHeapScanner().createImageHeapConstant(super.forString(value), ObjectScanner.OtherReason.UNKNOWN);
}

@Override
public JavaConstant forObject(Object object) {
if (object instanceof ImageHeapConstant heapConstant) {
/* This could be a simulated constant. */
return heapConstant;
}
/* Redirect constant lookup through the shadow heap. */
return universe.getHeapScanner().createImageHeapConstant(super.forObject(object), ObjectScanner.OtherReason.UNKNOWN);
}

private SVMHost getHostVM() {
return (SVMHost) universe.hostVM();
}
Expand Down
Loading

0 comments on commit 1ed2a58

Please sign in to comment.