oracle/graal · error · GraalError

Can use GraalDirectives.ensureAllocatedHere only with newins

Error message

Can use GraalDirectives.ensureAllocatedHere only with newinstance, newarray or multianewarray bytecode but found %s

What it means

GraalDirectives.ensureAllocatedHere is an intrinsic handled by a graph-builder plugin in StandardGraphBuilderPlugins. It must receive the result of the immediately preceding newinstance/newarray/multianewarray bytecode: the plugin pattern-matches NewInstanceNode/NewArrayNode/NewMultiArrayNode and replaces the allocation with an exceptional variant. Any other value on the stack hits the default branch and GraalError is thrown.

Source

Thrown at compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/replacements/StandardGraphBuilderPlugins.java:2317

            }
        });
    }

    public static void registerEnsureAllocatedHereIntrinsic(GraphBuilderContext b, ValueNode object) {
        if (object instanceof AllocateWithExceptionNode) {
            // already wrapped (using the intrinsic in a try block)
            b.push(JavaKind.Object, object);
            return;
        }
        FixedNode lastNode = switch (object) {
            case NewInstanceNode ni ->
                b.addPush(JavaKind.Object, new NewInstanceWithExceptionNode(ni.instanceClass(), true));
            case NewArrayNode na ->
                b.addPush(JavaKind.Object, new NewArrayWithExceptionNode(na.elementType(), na.length(), true));
            case NewMultiArrayNode nma ->
                b.addPush(JavaKind.Object, new NewMultiArrayWithExceptionNode(nma.type(), nma.dimensions()));
            default ->
                throw new GraalError("Can use GraalDirectives.ensureAllocatedHere only with newinstance, newarray or multianewarray bytecode but found %s", object);
        };
        GraalError.guarantee(object == lastNode.predecessor(),
                        "Can only use GraalDirectives.ensureAllocatedHere intrinsic if there is no control flow (statements) between the allocation and the call to ensureAllocatedHere %s->%s",
                        object, lastNode);
        GraalError.guarantee(!object.hasMoreThanOneUsage(),
                        "Can only use GraalDirectives.ensureAllocatedHere intrinsic if the parameter allocation is freshly allocated and not a local variable");

        object.replaceAtUsages(lastNode);
        GraphUtil.unlinkFixedNode((FixedWithNextNode) object);
        object.safeDelete();
    }

    private static void registerJMHBlackholePlugins(InvocationPlugins plugins) {
        // The purpose of this plugin is to help Blackhole.consume function mostly correctly even if
        // it's been inlined.
        String[] names = {"org.openjdk.jmh.infra.Blackhole", "org.openjdk.jmh.logic.BlackHole"};
        for (String name : names) {
            Registration r = new Registration(plugins, name);

View on GitHub (pinned to a66e9ccd1d)

Solutions

  1. Call GraalDirectives.ensureAllocatedHere directly on the allocation expression, or keep the allocation and the directive on adjacent statements with no code in between.
  2. Ensure the freshly allocated object has no other usage before the directive (do not log, assign, or wrap it first).
  3. Do not use the directive on values that are not freshly allocated in the same basic block.

Example fix

// before
Object o = new Object();
log("allocated");
GraalDirectives.ensureAllocatedHere(o); // GraalError: not the allocation node

// after
Object o = new Object();
GraalDirectives.ensureAllocatedHere(o);
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Calling ensureAllocatedHere on a value that is not the fresh allocation node: a local variable, parameter, phi, or constant; also note the follow-up guarantees reject control flow between the allocation and the call and allocations with more than one usage (e.g. the allocation is already inside a try block).

Common situations: Storing the allocation in a variable and calling the directive later; putting the allocation inside try-with-resources or a loop; reusing the allocated reference before the directive call.

Related errors


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