From 20791fe1dec7c8f70ba5d490ef3bd4b7e68740b8 Mon Sep 17 00:00:00 2001 From: Codrut Stancu Date: Fri, 17 Jan 2025 16:28:30 +0100 Subject: [PATCH] Fix base layer component hub rescanning. --- .../analysis/DynamicHubInitializer.java | 27 +++++++++++-------- .../analysis/NativeImagePointsToAnalysis.java | 4 +++ .../imagelayer/SVMImageLayerLoader.java | 2 +- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/analysis/DynamicHubInitializer.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/analysis/DynamicHubInitializer.java index f503a0cdc90e..953680eef5d7 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/analysis/DynamicHubInitializer.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/analysis/DynamicHubInitializer.java @@ -106,16 +106,7 @@ public void initializeMetaData(ImageHeapScanner heapScanner, AnalysisType type) registerPackage(heapScanner, javaClass, hub); } - boolean rescan = true; - /* - * The constant should not be rescanned directly if it is from the base layer, as it would - * try to access the constant again, which would trigger the dynamic hub initialization - * again. The constant has to be rescanned after the initialization is finished. - */ - if (hostVM.useBaseLayer()) { - ImageHeapConstant hubConstant = (ImageHeapConstant) heapScanner.createImageHeapConstant(hub, OtherReason.HUB); - rescan = hubConstant == null || !hubConstant.isInBaseLayer(); - } + boolean rescan = shouldRescanHub(heapScanner, hub); /* * Start by rescanning the hub itself. This ensures the correct scan reason in case this is @@ -131,7 +122,7 @@ public void initializeMetaData(ImageHeapScanner heapScanner, AnalysisType type) if (type.isArray()) { AnalysisError.guarantee(hub.getComponentHub().getArrayHub() == null, "Array hub already initialized for %s.", type.getComponentType().toJavaName(true)); hub.getComponentHub().setArrayHub(hub); - if (rescan) { + if (shouldRescanHub(heapScanner, hub.getComponentHub())) { heapScanner.rescanField(hub.getComponentHub().getCompanion(), hubCompanionArrayHubField); } } @@ -156,6 +147,20 @@ public void initializeMetaData(ImageHeapScanner heapScanner, AnalysisType type) } } + /** + * The hub should not be rescanned directly if it is from the base layer, as it would try to + * access the constant again, which would trigger the dynamic hub initialization again. The hub + * has to be rescanned after the initialization is finished. This will be simplified by + * GR-60254. + */ + private boolean shouldRescanHub(ImageHeapScanner heapScanner, DynamicHub hub) { + if (hostVM.useBaseLayer()) { + ImageHeapConstant hubConstant = (ImageHeapConstant) heapScanner.createImageHeapConstant(hub, OtherReason.HUB); + return hubConstant == null || !hubConstant.isInBaseLayer(); + } + return true; + } + /** * For reachable classes, register class's package in appropriate class loader. */ diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/analysis/NativeImagePointsToAnalysis.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/analysis/NativeImagePointsToAnalysis.java index 14325f9599c3..cf61550f735a 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/analysis/NativeImagePointsToAnalysis.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/analysis/NativeImagePointsToAnalysis.java @@ -148,6 +148,10 @@ public void onTypeReachable(AnalysisType type) { */ HostedImageLayerBuildingSupport.singleton().getLoader().rescanHub(type, ((SVMHost) hostVM).dynamicHub(type)); } + if (type.isArray() && type.getComponentType().isInBaseLayer()) { + /* Rescan the component hub. This will be simplified by GR-60254. */ + HostedImageLayerBuildingSupport.singleton().getLoader().rescanHub(type.getComponentType(), ((SVMHost) hostVM).dynamicHub(type).getComponentHub()); + } if (SubstrateOptions.includeAll()) { /* * Using getInstanceFields and getStaticFields allows to include the fields from the diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SVMImageLayerLoader.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SVMImageLayerLoader.java index 02ded48d5b61..302672361238 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SVMImageLayerLoader.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SVMImageLayerLoader.java @@ -1476,7 +1476,7 @@ private void ensureHubInitialized(ImageHeapConstant constant) { * created and the initializeMetaDataTask needs to be executed to ensure the hosted * object matches the persisted constant. */ - PersistedAnalysisType.Reader typeData = findBaseLayerType(type); + PersistedAnalysisType.Reader typeData = findType(getBaseLayerTypeId(type)); if (typeData != null && typeData.getHasArrayType()) { AnalysisType arrayClass = type.getArrayClass(); ensureHubInitialized(arrayClass);