{"record":{"id":"31cf3e7af5d67ae4","repo":"oracle/graal","slug":"expected-an-espressoexternalobjectconstant-for-des","errorCode":null,"errorMessage":"Expected an EspressoExternalObjectConstant for dest, got ","messagePattern":"Expected an EspressoExternalObjectConstant for dest, got ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"espresso-compiler-stub/src/com.oracle.truffle.espresso.vmaccess/src/com/oracle/truffle/espresso/vmaccess/EspressoExternalVMAccess.java","lineNumber":609,"sourceCode":"    public JavaConstant clonePrimitiveArray(JavaConstant primitiveArray) {\n        if (!(primitiveArray instanceof EspressoExternalObjectConstant objectConstant)) {\n            throw new IllegalArgumentException(\"Expected an EspressoExternalObjectConstant, got \" + safeGetClass(primitiveArray));\n        }\n        EspressoResolvedObjectType arrayType = objectConstant.getType();\n        if (!(arrayType.isArray() && arrayType.getComponentType().isPrimitive())) {\n            throw new IllegalArgumentException(\"Expected a primitive array constant, got \" + arrayType);\n        }\n        Value copy = invokeJVMCIHelper(\"clonePrimitiveArray\", objectConstant.getValue());\n        return new EspressoExternalObjectConstant(this, copy);\n    }\n\n    @Override\n    public void copyArray(JavaConstant src, int srcPos, JavaConstant dest, int destPos, int length) {\n        if (!(src instanceof EspressoExternalObjectConstant srcArray)) {\n            throw new IllegalArgumentException(\"Expected an EspressoExternalObjectConstant for src, got \" + safeGetClass(src));\n        }\n        if (!(dest instanceof EspressoExternalObjectConstant destArray)) {\n            throw new IllegalArgumentException(\"Expected an EspressoExternalObjectConstant for dest, got \" + safeGetClass(dest));\n        }\n        if (!srcArray.getType().isArray()) {\n            throw new IllegalArgumentException(\"Expected an array constant for src, got \" + srcArray.getType());\n        }\n        if (!destArray.getType().isArray()) {\n            throw new IllegalArgumentException(\"Expected an array constant for dest, got \" + destArray.getType());\n        }\n        try {\n            invoke(java_lang_System_arraycopy, null, srcArray, JavaConstant.forInt(srcPos), destArray, JavaConstant.forInt(destPos), JavaConstant.forInt(length));\n        } catch (InvocationException e) {\n            if (e.getCause() instanceof PolyglotException polyglotException) {\n                throw throwHostException(polyglotException);\n            }\n            throw e;\n        }\n    }\n\n    @Override","sourceCodeStart":591,"sourceCodeEnd":627,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/espresso-compiler-stub/src/com.oracle.truffle.espresso.vmaccess/src/com/oracle/truffle/espresso/vmaccess/EspressoExternalVMAccess.java#L591-L627","documentation":"The destination side of copyArray must equally be an EspressoExternalObjectConstant wrapping a guest array, because System.arraycopy executes inside the guest Espresso VM and can only store into guest memory. A dest constant from a foreign backend is rejected with IllegalArgumentException('Expected an EspressoExternalObjectConstant for dest, ...') before any copy is attempted, so no partial write can occur.","triggerScenarios":"Copying guest data into a destination allocated by the host provider (JavaConstant.forObject(hostArray)) or by another Espresso context.","commonSituations":"Result buffers pre-allocated on the host and passed into guest copy routines; mixed provider usage in buffer management code.","solutions":["Allocate the destination through this Espresso provider (createPrimitiveArray / asArrayConstant) before copyArray.","Guard dest instanceof EspressoExternalObjectConstant; otherwise copy into the host array with System.arraycopy after unwrapping the source values.","Keep allocation and copy on the same side of the host/guest boundary."],"exampleFix":"// before\nJavaConstant dest = JavaConstant.forObject(new byte[n]);\nvmAccess.copyArray(src, 0, dest, 0, n);\n\n// after\nJavaConstant dest = vmAccess.createPrimitiveArray(JavaKind.Byte, n);\nvmAccess.copyArray(src, 0, dest, 0, n);","handlingStrategy":"validation","validationCode":"if (!(dest instanceof EspressoExternalObjectConstant)) {\n    dest = vmAccess.createPrimitiveArray(kind, length); // guest-allocated destination\n}","typeGuard":"static boolean isEspressoConstant(JavaConstant c) {\n    return c instanceof EspressoExternalObjectConstant;\n}","tryCatchPattern":"catch (IllegalArgumentException e) with 'for dest' -> allocate dest via createPrimitiveArray/asArrayConstant and retry the copy.","preventionTips":["Never pre-allocate result buffers on the host for guest copies.","Create dest with the same provider as src.","Keep allocation helpers provider-aware."],"tags":["jvmci","espresso","graalvm","arrays","constants"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}