oracle/graal · error · IllegalArgumentException

Expected an EspressoExternalObjectConstant, got

Error message

Expected an EspressoExternalObjectConstant, got 

What it means

writeArrayElement(array, index, element) performs a guest array store via setArrayElement, which requires the array constant to be an EspressoExternalObjectConstant wrapping guest memory. Constants from other JVMCI backends have no guest Value, so the call fails fast with IllegalArgumentException naming the actual class.

Source

Thrown at espresso-compiler-stub/src/com.oracle.truffle.espresso.vmaccess/src/com/oracle/truffle/espresso/vmaccess/EspressoExternalVMAccess.java:630

            throw new IllegalArgumentException("Expected an array constant for src, got " + srcArray.getType());
        }
        if (!destArray.getType().isArray()) {
            throw new IllegalArgumentException("Expected an array constant for dest, got " + destArray.getType());
        }
        try {
            invoke(java_lang_System_arraycopy, null, srcArray, JavaConstant.forInt(srcPos), destArray, JavaConstant.forInt(destPos), JavaConstant.forInt(length));
        } catch (InvocationException e) {
            if (e.getCause() instanceof PolyglotException polyglotException) {
                throw throwHostException(polyglotException);
            }
            throw e;
        }
    }

    @Override
    public void writeArrayElement(JavaConstant array, int index, JavaConstant element) {
        if (!(array instanceof EspressoExternalObjectConstant espressoArray)) {
            throw new IllegalArgumentException("Expected an EspressoExternalObjectConstant, got " + safeGetClass(array));
        }
        EspressoResolvedObjectType arrayType = espressoArray.getType();
        if (!arrayType.isArray()) {
            throw new IllegalArgumentException("Expected an array type, got " + arrayType);
        }
        ResolvedJavaType componentType = arrayType.getComponentType();
        Object unwrappedValue;
        if (componentType.isPrimitive()) {
            if (componentType.getJavaKind() != element.getJavaKind()) {
                throw new IllegalArgumentException("Element " + element + " should be a " + componentType.getJavaKind() + ", got " + element.getJavaKind());
            }
            unwrappedValue = element.asBoxedPrimitive();
        } else if (element.isNull()) {
            unwrappedValue = null;
        } else {
            if (!(element instanceof EspressoExternalObjectConstant objectElement)) {
                throw new IllegalArgumentException("Element " + element + " should be an espresso object constant, got " + safeGetClass(element));
            }

View on GitHub (pinned to a66e9ccd1d)

Solutions

  1. Create the array via this Espresso provider (createPrimitiveArray / asArrayConstant) before writing elements.
  2. Guard array instanceof EspressoExternalObjectConstant; write to host arrays with plain Java instead.
  3. Regenerate cached constants when the owning context is recreated.

Example fix

// before
vmAccess.writeArrayElement(hostArrayConstant, 0, JavaConstant.forInt(7));

// after
JavaConstant guestArr = vmAccess.createPrimitiveArray(JavaKind.Int, len);
vmAccess.writeArrayElement(guestArr, 0, JavaConstant.forInt(7));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(array instanceof EspressoExternalObjectConstant)) {
    array = vmAccess.createPrimitiveArray(kind, length); // ensure guest array
}

Type guard

static boolean isEspressoConstant(JavaConstant c) {
    return c instanceof EspressoExternalObjectConstant;
}

Try / catch

catch (IllegalArgumentException e) with 'Expected an EspressoExternalObjectConstant' at writeArrayElement -> allocate via the provider and retry the write.

Prevention

When it happens

Trigger: Calling writeArrayElement with a host array constant (JavaConstant.forObject(int[])) or a constant from a different Espresso context.

Common situations: Test code building arrays with host factories; constants restored from serialization or caches tied to an older context; refactors mixing host and guest buffers.

Related errors


AI-assisted analysis of oracle/graal@a66e9ccd1d (2026-08-14). Data as JSON: /api/errors/0ed821b8c3cb3912. Report an issue: GitHub.