{"record":{"id":"1e4dc3b14de019f6","repo":"oracle/graal","slug":"label-used-by-instructions-at-following-offsets-ha","errorCode":null,"errorMessage":"Label used by instructions at following offsets has not been bound: %s","messagePattern":"Label used by instructions at following offsets has not been bound: (.+?)","errorType":"exception","errorClass":"GraalError","httpStatus":null,"severity":"error","filePath":"compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/asm/Assembler.java","lineNumber":252,"sourceCode":"    private int finalCodeSize = -1;\n\n    /**\n     * Returns the final code size after code emission has been completed.\n     */\n    public int finalCodeSize() {\n        assert codeBuffer.data == null : \"Buffer is expected to be closed\";\n        return finalCodeSize;\n    }\n\n    public byte[] copy(int start, int end) {\n        return codeBuffer.copyData(start, end);\n    }\n\n    private void checkAndClearLabelsWithPatches() throws InternalError {\n        Label label = labelsWithPatches;\n        while (label != null) {\n            if (label.patchPositions != null) {\n                throw new GraalError(\"Label used by instructions at following offsets has not been bound: %s\", label.patchPositions);\n            }\n            Label next = label.nextWithPatches;\n            label.nextWithPatches = null;\n            label = next;\n        }\n        labelsWithPatches = null;\n    }\n\n    public void bind(Label l) {\n        assert !l.isBound() : \"can bind label only once\";\n        l.bind(position(), this);\n    }\n\n    public abstract void align(int modulus);\n\n    /**\n     * Emit an instruction that will fail in some way if it is reached.\n     */","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/asm/Assembler.java#L234-L270","documentation":"close() on the outermost JNIMethodScope (parent == null) asserts that this scope is still the thread's top scope before clearing the ThreadLocal. If the ThreadLocal top is a different scope, scopes were closed out of order: some inner scope was left open (or another outer scope was pushed) so the stack is unbalanced, and IllegalStateException reports the actual top. This typically surfaces when try-with-resources blocks are reordered or a scope leaks.","triggerScenarios":"Closing the outer scope while an inner scope is still open (e.g. inner scope stored and closed later, or exception paths that skip an inner close()); manually constructing scopes and closing them in the wrong order; nested scopes where the inner one was created on a different control-flow branch and never closed.","commonSituations":"Breaking out of nested try-with-resources via exceptions that suppress close; refactors that move scope creation out of lexical order; caching scopes in fields so lifetime no longer matches nesting.","solutions":["Always create JNIMethodScope with try-with-resources so nesting and closing order match lexically, even on exception paths.","Find the leaked inner scope named in the message (scopeName is included in toString output via JNIUtil traces) and ensure it is closed before the outer one.","Avoid storing scopes in fields/collections; keep their lifetime strictly lexical."],"exampleFix":"// before\nJNIMethodScope inner = new JNIMethodScope(\"inner\", env);\ntry (JNIMethodScope outer = new JNIMethodScope(\"outer\", env)) {\n    ...\n} // throws: inner still open, topScope != outer\ninner.close();\n\n// after\ntry (JNIMethodScope outer = new JNIMethodScope(\"outer\", env)) {\n    try (JNIMethodScope inner = new JNIMethodScope(\"inner\", env)) {\n        ...\n    } // inner closes first\n} // outer closes cleanly","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Create every JNIMethodScope with try-with-resources so close order always mirrors creation order.","Enable JNIUtil tracing (level 1) during development to see enter/exit pairs and spot unbalanced scopes early.","Never store scopes in fields or collections; keep their lifetime lexical."],"tags":["jni","scope","lifecycle","ordering"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}