{"record":{"id":"4850f690837a3ca2","repo":"NationalSecurityAgency/ghidra","slug":"code-unit-would-extend-beyond-address-space-4850f6","errorCode":null,"errorMessage":"Code unit would extend beyond address space","messagePattern":"Code unit would extend beyond address space","errorType":"exception","errorClass":"CodeUnitInsertionException","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/program/AbstractDBTraceProgramViewListing.java","lineNumber":721,"sourceCode":"\t@Override\n\tpublic PropertyMap<?> getPropertyMap(String propertyName) {\n\t\t// TODO Auto-generated method stub\n\t\treturn null;\n\t}\n\n\t@Override\n\tpublic Instruction createInstruction(Address addr, InstructionPrototype prototype,\n\t\t\tMemBuffer memBuf, ProcessorContextView context, int forcedLengthOverride)\n\t\t\tthrows CodeUnitInsertionException {\n\t\tint checkLengthOverride =\n\t\t\tInstructionDB.checkLengthOverride(forcedLengthOverride, prototype);\n\t\tint length = checkLengthOverride != 0 ? checkLengthOverride : prototype.getLength();\n\t\tAddressRange range;\n\t\ttry {\n\t\t\trange = new AddressRangeImpl(addr, length);\n\t\t}\n\t\tcatch (AddressOverflowException e) {\n\t\t\tthrow new CodeUnitInsertionException(\"Code unit would extend beyond address space\");\n\t\t}\n\t\tvar mostRecent = program.memory.memoryManager.getViewMostRecentStateEntry(program.snap,\n\t\t\trange, StatePredicate.IS_KNOWN);\n\t\tlong snap = mostRecent == null ? program.snap : mostRecent.getKey().getY2();\n\t\treturn codeOperations.instructions()\n\t\t\t\t.create(Lifespan.nowOn(snap), addr, program.platform, prototype, context,\n\t\t\t\t\tforcedLengthOverride);\n\t}\n\n\t@Override\n\tpublic AddressSetView addInstructions(InstructionSet instructionSet, boolean overwrite)\n\t\t\tthrows CodeUnitInsertionException {\n\t\treturn codeOperations.instructions()\n\t\t\t\t.addInstructionSet(Lifespan.nowOn(program.snap), program.platform, instructionSet,\n\t\t\t\t\toverwrite);\n\t}\n\n\t@Override","sourceCodeStart":703,"sourceCodeEnd":739,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/program/AbstractDBTraceProgramViewListing.java#L703-L739","documentation":"Thrown as CodeUnitInsertionException by createInstruction when constructing the address range addr..addr+length overflows the address space's maximum address. This happens when an instruction is placed near the end of an address space and its length would extend past the space boundary.","triggerScenarios":"Calling createInstruction(addr, prototype, memBuf, context, forcedLengthOverride) where addr is within 'length' bytes of the address space's max address, causing new AddressRangeImpl(addr, length) to throw AddressOverflowException.","commonSituations":"Disassembling or manually placing instructions at the top of a small/overlay address space. Forcing a long instruction override near the end of RAM. Importing data that places code at the very last bytes of a register bank or truncated address space.","solutions":["Check addr.getAddressSpace().getMaxAddress().subtract(addr) >= length-1 before creating the instruction.","Use a larger address space, or relocate the code so instructions do not abut the space boundary.","If the length is a forced override, validate that the override length fits the remaining space."],"exampleFix":"// before\nprogram.getListing().createInstruction(addr, proto, buf, ctx, forcedLen);\n\n// after\nlong remaining = addr.getAddressSpace().getMaxAddress().subtract(addr);\nif (remaining < length - 1) {\n    // instruction would overflow; relocate or truncate\n    return;\n}\nprogram.getListing().createInstruction(addr, proto, buf, ctx, forcedLen);","handlingStrategy":"validation","validationCode":"// Check the instruction fits within the address space before creating it\nlong maxOffset = addr.getAddressSpace().getMaxAddress().getOffset();\nlong addrOffset = addr.getOffset();\nif (Long.compareUnsigned(maxOffset - addrOffset, length - 1) < 0) {\n    // would overflow; relocate or abort\n    return;\n}","typeGuard":null,"tryCatchPattern":"try {\n    listing.createInstruction(addr, proto, buf, ctx, forcedLen);\n} catch (CodeUnitInsertionException e) {\n    // instruction placement failed (e.g. address-space overflow or conflict)\n}","preventionTips":["Check remaining address-space capacity before placing instructions near the boundary.","Validate forced length overrides fit the space.","Use larger address spaces or relocate code away from the max boundary."],"tags":["trace-modeling","instruction","address-space","overflow","ghidra"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}