From 83266c5dc4788479a9e8a94fa6499dc991b83cbf Mon Sep 17 00:00:00 2001 From: Loic Ottet Date: Wed, 21 Feb 2024 14:43:28 +0100 Subject: [PATCH] Throw missing registration errors for array instantiation --- .../graal/snippets/SubstrateAllocationSnippets.java | 3 +-- .../svm/core/jdk/JavaLangReflectSubstitutions.java | 7 ++++--- .../reflect/MissingReflectionRegistrationUtils.java | 13 +++++++++++++ 3 files changed, 18 insertions(+), 5 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 a33e289fdd22..6b3a0064ef8c 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 @@ -347,8 +347,7 @@ private static void arrayHubErrorStub(DynamicHub elementType) { } else if (elementType == DynamicHub.fromClass(void.class)) { throw new IllegalArgumentException("Cannot allocate void array."); } else if (elementType.getArrayHub() == null || !elementType.getArrayHub().isInstantiated()) { - throw new IllegalArgumentException("Class " + DynamicHub.toClass(elementType).getTypeName() + "[] is instantiated reflectively but was never registered." + - "Register the class by adding \"unsafeAllocated\" for the class in " + ConfigurationFile.REFLECTION.getFileName() + "."); + throw MissingReflectionRegistrationUtils.errorForArray(DynamicHub.toClass(elementType), 1); } else { VMError.shouldNotReachHereUnexpectedInput(elementType); // ExcludeFromJacocoGeneratedReport } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaLangReflectSubstitutions.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaLangReflectSubstitutions.java index bbaaa52b38de..461644258bd4 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaLangReflectSubstitutions.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaLangReflectSubstitutions.java @@ -29,7 +29,6 @@ import java.lang.reflect.Array; import java.util.Objects; -import jdk.graal.compiler.word.BarrieredAccess; import org.graalvm.word.UnsignedWord; import com.oracle.svm.core.SubstrateUtil; @@ -38,7 +37,9 @@ import com.oracle.svm.core.config.ConfigurationValues; import com.oracle.svm.core.hub.DynamicHub; import com.oracle.svm.core.hub.LayoutEncoding; -import com.oracle.svm.core.util.VMError; +import com.oracle.svm.core.reflect.MissingReflectionRegistrationUtils; + +import jdk.graal.compiler.word.BarrieredAccess; @TargetClass(java.lang.reflect.Array.class) final class Target_java_lang_reflect_Array { @@ -406,7 +407,7 @@ private static Object multiNewArray(Class componentType, int[] dimensions) { for (int i = 0; i < dimensions.length; i++) { arrayHub = arrayHub.getArrayHub(); if (arrayHub == null) { - throw VMError.unsupportedFeature("Cannot allocate " + dimensions.length + "-dimensional array of " + componentType.getName()); + throw MissingReflectionRegistrationUtils.errorForArray(componentType, dimensions.length); } } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/MissingReflectionRegistrationUtils.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/MissingReflectionRegistrationUtils.java index fd4c12077a72..498bdf436af2 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/MissingReflectionRegistrationUtils.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/MissingReflectionRegistrationUtils.java @@ -111,6 +111,19 @@ public static MissingReflectionRegistrationError errorForProxy(Class... inter return exception; } + public static MissingReflectionRegistrationError errorForArray(Class elementClass, int dimension) { + MissingReflectionRegistrationError exception = new MissingReflectionRegistrationError(errorMessage("instantiate the array class", + elementClass.getTypeName() + "[]".repeat(dimension), + "Add \"unsafeAllocated\" to the array class registration to enable runtime instantiation.", "reflection"), + null, null, null, null); + report(exception); + /* + * If report doesn't throw, we throw the exception anyway since this is a Native + * Image-specific error that is unrecoverable in any case. + */ + return exception; + } + private static String errorMessage(String failedAction, String elementDescriptor) { return errorMessage(failedAction, elementDescriptor, null, "reflection"); }