{"record":{"id":"57ef112e1379cdbb","repo":"oracle/graal","slug":"expected-an-array-constant-for-src-got","errorCode":null,"errorMessage":"Expected an array constant for src, got {}","messagePattern":"Expected an array constant for src, got (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java","lineNumber":332,"sourceCode":"        Object copy = switch (source) {\n            case boolean[] array -> array.clone();\n            case byte[] array -> array.clone();\n            case short[] array -> array.clone();\n            case char[] array -> array.clone();\n            case int[] array -> array.clone();\n            case long[] array -> array.clone();\n            case float[] array -> array.clone();\n            case double[] array -> array.clone();\n            default -> throw new IllegalArgumentException(\"Expected a primitive array constant, got \" + primitiveArray);\n        };\n        return providers.getSnippetReflection().forObject(copy);\n    }\n\n    @Override\n    public void copyArray(JavaConstant src, int srcPos, JavaConstant dest, int destPos, int length) {\n        Object srcArray = providers.getSnippetReflection().asObject(Object.class, src);\n        if (srcArray == null || !srcArray.getClass().isArray()) {\n            throw new IllegalArgumentException(\"Expected an array constant for src, got \" + src);\n        }\n        Object destArray = providers.getSnippetReflection().asObject(Object.class, dest);\n        if (destArray == null || !destArray.getClass().isArray()) {\n            throw new IllegalArgumentException(\"Expected an array constant for dest, got \" + dest);\n        }\n        System.arraycopy(srcArray, srcPos, destArray, destPos, length);\n    }\n\n    private void doWriteArrayElement(Object array, ResolvedJavaType componentType, int index, JavaConstant element) {\n        Object unwrappedValue;\n        if (componentType.isPrimitive()) {\n            if (componentType.getJavaKind() != element.getJavaKind()) {\n                throw new IllegalArgumentException(\"Element \" + element + \" should be a \" + componentType.getJavaKind() + \", got \" + element.getJavaKind());\n            }\n            unwrappedValue = element.asBoxedPrimitive();\n        } else {\n            if (!element.getJavaKind().isObject()) {\n                throw new IllegalArgumentException(\"Element \" + element + \" should be an object, got \" + componentType);","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java#L314-L350","documentation":"Thrown by HostVMAccess.copyArray when the src constant cannot be unwrapped to a live host array object: asObject(Object.class, src) returned null or the object's class is not an array. It guards the source side of the System.arraycopy delegation.","triggerScenarios":"Calling copyArray(src, srcPos, dest, destPos, length) where src is a non-array constant (a scalar/object constant), a null-valued constant (asObject yields null), or a constant not backed by a host object in the current snippet reflection context.","commonSituations":"Generic buffer-copy helpers invoked with mixed constant kinds; constants from serialized compilation data lacking object backing; wrong argument order (passing an element or dest where src is expected).","solutions":["Check src wraps a real array before calling: unwrap and verify getClass().isArray() (or type it via metaAccess and check isArray())","Ensure src was produced by forObject/asArrayConstant in the same provider context","Fix argument order — src, srcPos, dest, destPos, length"],"exampleFix":"// before\nhostVM.copyArray(elementConstant, 0, destConstant, 0, n);  // scalar src -> throws\n\n// after\nObject srcObj = snippetReflection.asObject(Object.class, srcConstant);\nif (srcObj != null && srcObj.getClass().isArray()) {\n    hostVM.copyArray(srcConstant, 0, destConstant, 0, n);\n}","handlingStrategy":"type-guard","validationCode":"Object s = snippetReflection.asObject(Object.class, src);\nif (s == null || !s.getClass().isArray()) {\n    throw new IllegalArgumentException(\"src is not a live array: \" + src);","typeGuard":"static boolean isLiveArrayConstant(JavaConstant c, SnippetReflectionProvider sp) {\n    Object o = sp.asObject(Object.class, c);\n    return o != null && o.getClass().isArray();\n}","tryCatchPattern":"catch (IllegalArgumentException e) { materialize src via asArrayConstant and retry once }","preventionTips":["Materialize arrays with forObject/asArrayConstant before copying","Keep argument order src,srcPos,dest,destPos,length explicit at call sites","Do not pass element or scalar constants to array-copy helpers"],"tags":["graalvm","hostvmaccess","arrays","validation"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}