NationalSecurityAgency/ghidra · error · MemoryAccessException
Couldn't get requested bytes for CodeUnit
Error message
Couldn't get requested bytes for CodeUnit
What it means
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.
Source
Thrown at Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/listing/DBTraceCodeUnitAdapter.java:403
default Memory getMemory() {
return getProgram().getMemory();
}
@Override
default boolean isBigEndian() {
return getLanguage().isBigEndian();
}
@Override
default byte[] getBytes() throws MemoryAccessException {
return getBytesInFull(0, getLength()).array();
}
@Override
default void getBytesInCodeUnit(byte[] buffer, int bufferOffset) throws MemoryAccessException {
int len = Math.min(buffer.length - bufferOffset, getLength());
if (getBytes(ByteBuffer.wrap(buffer, bufferOffset, len), 0) != len) {
throw new MemoryAccessException("Couldn't get requested bytes for CodeUnit");
}
}
}
View on GitHub (pinned to d5f144c24d)
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.
Example fix
// before
codeUnit.getBytesInCodeUnit(buf, 0); // may throw
// after — verify bytes exist first
int readable = memSpace.getBytes(startSnap, codeUnit.getAddress(), buf);
if (readable < codeUnit.getLength()) {
// handle missing bytes
} else {
codeUnit.getBytesInCodeUnit(buf, 0);
} Defensive patterns
Strategy: try-catch
Validate before calling
// Check memory coverage before reading code unit bytes
ByteBuffer buf = ByteBuffer.allocate(codeUnit.getLength());
int readable = memSpace.getBytes(startSnap, codeUnit.getAddress(), buf);
if (readable < codeUnit.getLength()) {
// Memory not fully recorded — skip or handle
return;
}
codeUnit.getBytesInCodeUnit(buf.array(), 0); Try / catch
try {
codeUnit.getBytesInCodeUnit(buffer, offset);
} catch (MemoryAccessException e) {
// Bytes not available at this snap — handle gracefully
Msg.warn(MyClass.class, "Could not read bytes for " + codeUnit.getAddress());
Arrays.fill(buffer, offset, offset + codeUnit.getLength(), (byte) 0);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Code units cannot overlap
- Code units cannot overlap
- Length override of {newLength} conflicts with code unit at {
- Byte arrays cannot exceed length of variable
- Byte array values must be hex enclosed in {}
AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14).
Data as JSON: /api/errors/2833b52b042fa9e5.
Report an issue: GitHub.