{"record":{"id":"1268dacea3113085","repo":"oracle/graal","slug":"can-use-graaldirectives-ensureallocatedhere-only-w","errorCode":null,"errorMessage":"Can use GraalDirectives.ensureAllocatedHere only with newinstance, newarray or multianewarray bytecode but found %s","messagePattern":"Can use GraalDirectives\\.ensureAllocatedHere only with newinstance, newarray or multianewarray bytecode but found (.+?)","errorType":"exception","errorClass":"GraalError","httpStatus":null,"severity":"error","filePath":"compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/replacements/StandardGraphBuilderPlugins.java","lineNumber":2317,"sourceCode":"            }\n        });\n    }\n\n    public static void registerEnsureAllocatedHereIntrinsic(GraphBuilderContext b, ValueNode object) {\n        if (object instanceof AllocateWithExceptionNode) {\n            // already wrapped (using the intrinsic in a try block)\n            b.push(JavaKind.Object, object);\n            return;\n        }\n        FixedNode lastNode = switch (object) {\n            case NewInstanceNode ni ->\n                b.addPush(JavaKind.Object, new NewInstanceWithExceptionNode(ni.instanceClass(), true));\n            case NewArrayNode na ->\n                b.addPush(JavaKind.Object, new NewArrayWithExceptionNode(na.elementType(), na.length(), true));\n            case NewMultiArrayNode nma ->\n                b.addPush(JavaKind.Object, new NewMultiArrayWithExceptionNode(nma.type(), nma.dimensions()));\n            default ->\n                throw new GraalError(\"Can use GraalDirectives.ensureAllocatedHere only with newinstance, newarray or multianewarray bytecode but found %s\", object);\n        };\n        GraalError.guarantee(object == lastNode.predecessor(),\n                        \"Can only use GraalDirectives.ensureAllocatedHere intrinsic if there is no control flow (statements) between the allocation and the call to ensureAllocatedHere %s->%s\",\n                        object, lastNode);\n        GraalError.guarantee(!object.hasMoreThanOneUsage(),\n                        \"Can only use GraalDirectives.ensureAllocatedHere intrinsic if the parameter allocation is freshly allocated and not a local variable\");\n\n        object.replaceAtUsages(lastNode);\n        GraphUtil.unlinkFixedNode((FixedWithNextNode) object);\n        object.safeDelete();\n    }\n\n    private static void registerJMHBlackholePlugins(InvocationPlugins plugins) {\n        // The purpose of this plugin is to help Blackhole.consume function mostly correctly even if\n        // it's been inlined.\n        String[] names = {\"org.openjdk.jmh.infra.Blackhole\", \"org.openjdk.jmh.logic.BlackHole\"};\n        for (String name : names) {\n            Registration r = new Registration(plugins, name);","sourceCodeStart":2299,"sourceCodeEnd":2335,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/replacements/StandardGraphBuilderPlugins.java#L2299-L2335","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Call GraalDirectives.ensureAllocatedHere directly on the allocation expression, or keep the allocation and the directive on adjacent statements with no code in between.","Ensure the freshly allocated object has no other usage before the directive (do not log, assign, or wrap it first).","Do not use the directive on values that are not freshly allocated in the same basic block."],"exampleFix":"// before\nObject o = new Object();\nlog(\"allocated\");\nGraalDirectives.ensureAllocatedHere(o); // GraalError: not the allocation node\n\n// after\nObject o = new Object();\nGraalDirectives.ensureAllocatedHere(o);","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call GraalDirectives.ensureAllocatedHere(x) on the same expression that allocates x, or on the immediately following line.","Put no statements (logging, null checks, try blocks) between the allocation and the directive call.","Ensure the allocation has no other usage before the directive; don't reuse the variable first."],"tags":["graal","intrinsic","allocation","bytecode","graph-builder"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}