{"record":{"id":"2833b52b042fa9e5","repo":"NationalSecurityAgency/ghidra","slug":"couldn-t-get-requested-bytes-for-codeunit","errorCode":null,"errorMessage":"Couldn't get requested bytes for CodeUnit","messagePattern":"Couldn't get requested bytes for CodeUnit","errorType":"exception","errorClass":"MemoryAccessException","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/DBTraceCodeUnitAdapter.java","lineNumber":403,"sourceCode":"\tdefault Memory getMemory() {\n\t\treturn getProgram().getMemory();\n\t}\n\n\t@Override\n\tdefault boolean isBigEndian() {\n\t\treturn getLanguage().isBigEndian();\n\t}\n\n\t@Override\n\tdefault byte[] getBytes() throws MemoryAccessException {\n\t\treturn getBytesInFull(0, getLength()).array();\n\t}\n\n\t@Override\n\tdefault void getBytesInCodeUnit(byte[] buffer, int bufferOffset) throws MemoryAccessException {\n\t\tint len = Math.min(buffer.length - bufferOffset, getLength());\n\t\tif (getBytes(ByteBuffer.wrap(buffer, bufferOffset, len), 0) != len) {\n\t\t\tthrow new MemoryAccessException(\"Couldn't get requested bytes for CodeUnit\");\n\t\t}\n\t}\n}\n","sourceCodeStart":385,"sourceCodeEnd":407,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/DBTraceCodeUnitAdapter.java#L385-L407","documentation":"Thrown by DBTraceCodeUnitAdapter.getBytesInCodeUnit() when the underlying getBytes() call returns fewer bytes than the requested length. This indicates the memory backing the code unit's address range does not have enough initialized/available bytes to fill the buffer. It wraps a partial read as a MemoryAccessException so callers know the code unit's byte content is incomplete or unreadable at that snapshot.","triggerScenarios":"Calling codeUnit.getBytesInCodeUnit(buffer, offset) when the trace memory at the code unit's address range does not contain enough bytes — e.g., the memory was never written/recorded for that snap, or only partially recorded. The getBytes(ByteBuffer, 0) return value is less than len.","commonSituations":"Inspecting a code unit at a snapshot where memory was not recorded (e.g., before the trace started recording, or a gap in recorded snaps); memory mapped to an overlay that has no backing bytes; truncated or corrupted trace files; attempting to read bytes for a guest-platform code unit where the host memory is uninitialized.","solutions":["Ensure memory bytes are recorded for the address range and snap before creating/reading the code unit — use trace.getMemoryManager().getBytes().","Check codeUnit existence and memory coverage with memSpace.contains() or getBytes() return value before calling getBytesInCodeUnit.","Catch MemoryAccessException and handle gracefully (treat as uninitialized) if your workflow tolerates missing bytes.","Switch to a snapshot where the memory is known to be fully recorded."],"exampleFix":"// before\ncodeUnit.getBytesInCodeUnit(buf, 0); // may throw\n\n// after — verify bytes exist first\nint readable = memSpace.getBytes(startSnap, codeUnit.getAddress(), buf);\nif (readable < codeUnit.getLength()) {\n    // handle missing bytes\n} else {\n    codeUnit.getBytesInCodeUnit(buf, 0);\n}","handlingStrategy":"try-catch","validationCode":"// Check memory coverage before reading code unit bytes\nByteBuffer buf = ByteBuffer.allocate(codeUnit.getLength());\nint readable = memSpace.getBytes(startSnap, codeUnit.getAddress(), buf);\nif (readable < codeUnit.getLength()) {\n    // Memory not fully recorded — skip or handle\n    return;\n}\ncodeUnit.getBytesInCodeUnit(buf.array(), 0);","typeGuard":null,"tryCatchPattern":"try {\n    codeUnit.getBytesInCodeUnit(buffer, offset);\n} catch (MemoryAccessException e) {\n    // Bytes not available at this snap — handle gracefully\n    Msg.warn(MyClass.class, \"Could not read bytes for \" + codeUnit.getAddress());\n    Arrays.fill(buffer, offset, offset + codeUnit.getLength(), (byte) 0);\n}","preventionTips":["Ensure memory bytes are recorded for the address range and snap before reading code unit bytes.","Use memSpace.getBytes() return value to detect partial reads before calling getBytesInCodeUnit.","Catch MemoryAccessException in any code that reads across potentially unrecorded snapshots."],"tags":["memory-access","bytes","code-unit","trace-modeling"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}