{"record":{"id":"d9815deda5e77ac2","repo":"oracle/graal","slug":"could-not-unwrap-array","errorCode":null,"errorMessage":"Could not unwrap array: {}","messagePattern":"Could not unwrap array: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java","lineNumber":365,"sourceCode":"            unwrappedValue = element.asBoxedPrimitive();\n        } else {\n            if (!element.getJavaKind().isObject()) {\n                throw new IllegalArgumentException(\"Element \" + element + \" should be an object, got \" + componentType);\n            }\n            unwrappedValue = providers.getSnippetReflection().asObject(Object.class, element);\n        }\n        Array.set(array, index, unwrappedValue);\n    }\n\n    @Override\n    public void writeArrayElement(JavaConstant array, int index, JavaConstant element) {\n        ResolvedJavaType arrayType = getProviders().getMetaAccess().lookupJavaType(array);\n        if (arrayType == null || !arrayType.isArray()) {\n            throw new IllegalArgumentException(\"Expected an array constant, got \" + array);\n        }\n        Object asObject = providers.getSnippetReflection().asObject(Object.class, array);\n        if (asObject == null) {\n            throw new IllegalArgumentException(\"Could not unwrap array: \" + array);\n        }\n        doWriteArrayElement(asObject, arrayType.getComponentType(), index, element);\n    }\n\n    @Override\n    public ResolvedJavaMethod asResolvedJavaMethod(Constant constant) {\n        SnippetReflectionProvider snippetReflection = providers.getSnippetReflection();\n        Executable executable = snippetReflection.asObject(Executable.class, (JavaConstant) constant);\n        if (executable != null) {\n            return providers.getMetaAccess().lookupJavaMethod(executable);\n        }\n        return null;\n    }\n\n    @Override\n    public JavaConstant asFieldConstant(ResolvedJavaField field) {\n        if (field.isInternal()) {\n            return null;","sourceCodeStart":347,"sourceCodeEnd":383,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java#L347-L383","documentation":"writeArrayElement requires a real host array object: it unwraps the JavaConstant with snippetReflection.asObject(Object.class, array). If that returns null — the constant is not a materialized object constant (e.g. null, a primitive constant, a compressed placeholder, or a constant from a different/unregistered object graph) — this IllegalArgumentException is thrown. The type check passed, but the constant cannot be turned into an actual array instance.","triggerScenarios":"Calling writeArrayElement with a constant that types as an array (so the isArray check at line 361 passes) but that snippetReflection cannot materialize: JavaConstant.NULL_POINTER typed as Object[], a VM-internal placeholder, or an object constant created by a different SnippetReflectionProvider that this host does not know.","commonSituations":"Mixing constants from an image-build (AOT) world with the host JVM reflection world; constants obtained from a guest VM or another VMAccess instance; serialized/deserialized constants that lost their object identity.","solutions":["Ensure the array constant originates from the same Graal runtime (forObject on the same SnippetReflectionProvider) as the VMAccess used for writing","Guard with providers.getSnippetReflection().asObject(Object.class, arrayConst) != null before calling writeArrayElement","If the constant is null or non-materializable, skip the write or rebuild the constant from the live array object"],"exampleFix":"// before\nvmAccess.writeArrayElement(arrayConst, idx, element);\n\n// after\nObject arr = providers.getSnippetReflection().asObject(Object.class, arrayConst);\nif (arr == null) {\n    // constant not materializable on this host - rebuild from live object or skip\n    return;\n}\nvmAccess.writeArrayElement(arrayConst, idx, element);","handlingStrategy":"validation","validationCode":"Object live = providers.getSnippetReflection().asObject(Object.class, arrayConst);\nif (live == null || !live.getClass().isArray()) {\n    // constant not materializable - rebuild via forObject or skip\n}","typeGuard":null,"tryCatchPattern":"catch (IllegalArgumentException e) { if (e.getMessage().contains(\"Could not unwrap\")) { rebuild constant from live object and retry once; } else throw e; }","preventionTips":["Create and consume object constants within the same Graal runtime","Treat cross-runtime constants as untrusted: validate materializability first","Cache forObject constants next to the live array they wrap"],"tags":["graalvm","vmaccess","snippet-reflection","constants"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}