Interface IVirtualMemory
- All Known Subinterfaces:
IDebuggerVirtualMemory,IVirtualMemoryShim
- All Known Implementing Classes:
AbstractVirtualMemory
Addresses are represented as long; they are always considered unsigned. Sizes are represented as int; they are also always considered unsigned.
Implementation of the eventing system (for alloc, free, protection change, and write
notifications) is optional. If not implemented, the methods should throw
UnsupportedOperationException. If implemented, remember that the observer objects should
not be persisted with the memory object.
Some features exposed by this interface are optional. If the implementation does not provide
them, it should throw UnsupportedOperationException.
Implementors may decide to provide support for concurrency.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intExecute access.static final intNo access.static final intRead access.static final intRead/Write access.static final intRead/Write/Execute access.static final intRead/Execute access.static final intWrite access.static final intAddress massager type to specify an Arm64 MMU address regime that ignores the top 2 bytes. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAllocListener(IMemoryAllocListener listener) Add a memory allocation listener.voidaddFreeListener(IMemoryFreeListener listener) Add a memory free listener.voidaddPreWriteListener(IMemoryWriteListener listener) Note: The callback is invoked before the write operation takes place.voidaddPropertyListener(IMemoryPropertyListener listener) Add a memory property listener.voidAdd a memory protection listener.voidaddWriteListener(IMemoryWriteListener listener) Add a memory write listener.voidallocate(long address, int size, int protection) Allocate the entire memory range or nothing (fail).voidallocatePage(long address, int protection) Allocate a single page.intcheck(long address, int size, int protection) Check if a memory range has at least the given set of protection flags.Make a deep copy of this virtual memory.voidfree(long address, int size) Free the entire memory range or fail.voidfreePage(long address) Free a single page.default intRetrieve the address massager type.Retrieve the base addresses of the pages that are allocated (in the most general sense: that includes pages "reserved", depending on VM implementation).intGet the number of pages allocated (in the most general sense: that includes pages "reserved", depending on VM implementation).intDetermine the approximate memory footprint of this object, in kilobytes.default intGet the width of a memory page in bits.intgetPageProtection(long address) Get the protection flags for a page.intGet the size of a page in bytes.intGet the width of this memory space in bits.Get the standard endianness.default booleanisAllocatedPage(long address) Determine whether the provided address belongs to an allocated page.booleanisValidAddress(long address) Determine if an address is valid for this memory space.default intread(long address, int size, byte[] dst, int dstOffset) Read a range of bytes.intread(long address, int size, byte[] dst, int dstOffset, boolean bypassProtection) Read a range of bytes.intreadBEInt(long address) Read a big-endian 32-bit integer.longreadBELong(long address) Read a big-endian 64-bit integer.shortreadBEShort(long address) Read a big-endian 16-bit integer.bytereadByte(long address) Convenience method.intreadInt(long address) Read a 32-bit integer using thestandard endianness.intreadInt(long address, Endianness end) Read a 32-bit integer.intreadLEInt(long address) Read a little-endian 32-bit integer.longreadLELong(long address) Read a little-endian 64-bit integer.shortreadLEShort(long address) Read a little-endian 16-bit integer.longreadLong(long address) Read a 64-bit integer using thestandard endianness.longreadLong(long address, Endianness end) Read a 64-bit integer.longreadPointer(long address) Read a pointer-sized integer.shortreadShort(long address) Read a 16-bit integer using thestandard endianness.shortreadShort(long address, Endianness end) Read a 16-bit integer.voidremoveAllocListener(IMemoryAllocListener listener) Remove a memory allocation listener.voidremoveFreeListener(IMemoryFreeListener listener) Remove a memory free listener.voidremovePreWriteListener(IMemoryWriteListener listener) Remove a pre-write listener.voidRemove a memory property listener.voidRemove a memory protection listener.voidremoveWriteListener(IMemoryWriteListener listener) Remove a memory write listener.longroundToPage(long address) Round an address to the page this address currently resides in, that is the highest page so that boundary ≤ address.longroundToSize(long address) Round an address to the lowest page boundary so that address ≤ boundary.default voidsetAddressMassagerType(int type) Set the address massager type.voidsetLazyMemoryProvider(ILazyMemoryProvider lazyMemoryProvider, boolean skipFailedAllocations) Set a lazy memory provider.voidsetPageProtection(long address, int protection) Set the protection flags for a page.voidsetStandardEndianness(Endianness endianness) Set the standard endianness.default intwrite(long address, int size, byte[] src, int srcOffset) Write a range of bytes.intwrite(long address, int size, byte[] src, int srcOffset, boolean bypassProtection) Write a range of bytes.voidwriteBEInt(long address, int v) Write a big-endian 32-bit integer.voidwriteBELong(long address, long v) Write a big-endian 64-bit integer.voidwriteBEShort(long address, short v) Write a big-endian 16-bit integer.voidwriteByte(long address, byte v) Write a byte.voidwriteInt(long address, int v) Write a 32-bit integer using thestandard endianness.voidwriteInt(long address, int v, Endianness end) Write a 32-bit integer.voidwriteLEInt(long address, int v) Write a little-endian 32-bit integer.voidwriteLELong(long address, long v) Write a little-endian 64-bit integer.voidwriteLEShort(long address, short v) Write a little-endian 16-bit integer.voidwriteLong(long address, long v) Write a 64-bit integer using thestandard endianness.voidwriteLong(long address, long v, Endianness end) Write a 64-bit integer.voidwritePointer(long address, long ptr) Write a pointer-sized integer.voidwriteShort(long address, short v) Write a 16-bit integer using thestandard endianness.voidwriteShort(long address, short v, Endianness end) Write a 16-bit integer.
-
Field Details
-
ACCESS_NONE
static final int ACCESS_NONENo access. The page exists but any attempt to write, read, or execute from/to it will raise.- See Also:
-
ACCESS_READ
static final int ACCESS_READRead access.- See Also:
-
ACCESS_WRITE
static final int ACCESS_WRITEWrite access.- See Also:
-
ACCESS_EXECUTE
static final int ACCESS_EXECUTEExecute access.- See Also:
-
ACCESS_RW
static final int ACCESS_RWRead/Write access.- See Also:
-
ACCESS_RX
static final int ACCESS_RXRead/Execute access.- See Also:
-
ACCESS_RWX
static final int ACCESS_RWXRead/Write/Execute access.- See Also:
-
AM_ARM64_48BIT
static final int AM_ARM64_48BITAddress massager type to specify an Arm64 MMU address regime that ignores the top 2 bytes.- See Also:
-
-
Method Details
-
getPageSize
int getPageSize()Get the size of a page in bytes. A page size should always be a multiple of 2.This method is mostly applicable to page-based virtual memories, but can also be used for other types of memories to indicate the optimal (in terms of performance) unit size of access.
- Returns:
- page size in bytes
-
getPageBits
default int getPageBits()Get the width of a memory page in bits. Example: a 4Kb page has a 12-bit width.- Returns:
- page width in bits
-
getSpaceBits
int getSpaceBits()Get the width of this memory space in bits. Example: 32 for a 32-bit address space.- Returns:
- address-space width in bits
-
getStandardEndianess
Endianness getStandardEndianess()Get the standard endianness. It is used by thereadShort(long),writeShort(long, short),readInt(long),writeInt(long, int),readLong(long), andwriteLong(long, long)methods.- Returns:
- standard endianness
-
setStandardEndianness
Set the standard endianness. It is used by thereadShort(long),writeShort(long, short),readInt(long),writeInt(long, int),readLong(long), andwriteLong(long, long)methods.- Parameters:
endianness- standard endianness- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
duplicate
IVirtualMemory duplicate()Make a deep copy of this virtual memory.Implementation note: the resulting object may not be of the same type as the source (this) object.
- Returns:
- a new memory object
- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
isValidAddress
boolean isValidAddress(long address) Determine if an address is valid for this memory space.- Parameters:
address- address to test- Returns:
- true if the address is not larger than what this memory space allows, that is, 0 ≤ address < spaceSize
-
roundToPage
long roundToPage(long address) Round an address to the page this address currently resides in, that is the highest page so that boundary ≤ address. This method does not raise, even if the address is invalid for this memory space.- Parameters:
address- an address- Returns:
- the containing page address
-
roundToSize
long roundToSize(long address) Round an address to the lowest page boundary so that address ≤ boundary. This method does not raise, even if the address is invalid for this memory space.- Parameters:
address- an address or a size- Returns:
- rounded address
-
getAllocatedPageCount
int getAllocatedPageCount()Get the number of pages allocated (in the most general sense: that includes pages "reserved", depending on VM implementation).- Returns:
- the number of allocated pages (limited to 2B)
- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
getAllocatedPageBases
Collection<Long> getAllocatedPageBases()Retrieve the base addresses of the pages that are allocated (in the most general sense: that includes pages "reserved", depending on VM implementation).- Returns:
- a sorted (ascending) collection of unique page addresses
- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
allocate
Allocate the entire memory range or nothing (fail). On error, this method should throw aMemoryException, and no page should have been allocated. If successful, all pages within the range should have been been allocated.- Parameters:
address- address (unsigned)size- size to be allocated (unsigned)protection- protection flag (seeACCESS_READ,ACCESS_WRITE...)- Throws:
MemoryException- if allocation fails
-
free
Free the entire memory range or fail. On error, this method should throw aMemoryException, and no page should have been freed. If successful, all pages within the range should have been been freed.- Parameters:
address- start addresssize- size to free- Throws:
MemoryException- if freeing fails
-
allocatePage
Allocate a single page. Seeallocate(long, int, int).- Parameters:
address- page addressprotection- protection flags- Throws:
MemoryException- if allocation fails
-
freePage
Free a single page. Seefree(long, int).- Parameters:
address- page address- Throws:
MemoryException- if freeing fails
-
setAddressMassagerType
default void setAddressMassagerType(int type) Set the address massager type.- Parameters:
type- one of theAM_xxxconstants- Throws:
UnsupportedOperationException- not all memory objects support this operation
-
getAddressMassager
default int getAddressMassager()Retrieve the address massager type.- Returns:
- one of the
AM_xxxconstants
-
setPageProtection
Set the protection flags for a page. This method raises on error (invalid address, no page, etc.)- Parameters:
address- address within the pageprotection- the new protection for the page- Throws:
MemoryException- if protection cannot be updated
-
getPageProtection
Get the protection flags for a page. This method raises on error (invalid address, no page, etc.)- Parameters:
address- address within the page- Returns:
- the protection bits
- Throws:
MemoryException- if protection cannot be retrieved
-
isAllocatedPage
default boolean isAllocatedPage(long address) Determine whether the provided address belongs to an allocated page.This method is applicable to page-based virtual memories only.
- Parameters:
address- a page address or address in the page- Returns:
- true if the page is allocated
-
check
int check(long address, int size, int protection) Check if a memory range has at least the given set of protection flags.- Parameters:
address- start address (unsigned)size- range size (unsigned)protection- protection to check- Returns:
- the number of contiguous bytes, starting from
address, that have the given protection flags. The amount may be less thansize. If the check is successful, the returned value should be equal tosize
-
read
int read(long address, int size, byte[] dst, int dstOffset, boolean bypassProtection) throws MemoryException Read a range of bytes. This method should do its best to transact: the entire range is read, or nothing is and an error is thrown. If the implementor cannot support transaction, this method simply returns the amount of bytes read.- Parameters:
address- start addresssize- number of bytes to readdst- destination bufferdstOffset- destination offsetbypassProtection- if true, memory page protection is disregarded and non-readable bytes may be read- Returns:
- the amount of bytes read (potentially less than requested if the method does not transact)
- Throws:
MemoryException- if the method failed
-
read
Read a range of bytes. This method should do its best to transact: the entire range is read, or nothing is and an error is thrown. If the implementor cannot support transaction, this method simply returns the amount of bytes read. The address range must be readable, else the method will throw.- Parameters:
address- start addresssize- number of bytes to readdst- destination bufferdstOffset- destination offset- Returns:
- the amount of bytes read (potentially less than requested if the method does not transact)
- Throws:
MemoryException- if the method failed
-
write
int write(long address, int size, byte[] src, int srcOffset, boolean bypassProtection) throws MemoryException Write a range of bytes. This method should do its best to transact: the entire range is written, or nothing is and an error is thrown. If the implementor cannot support transaction, this method returns the amount of bytes written, or throws on error.- Parameters:
address- start addresssize- number of bytes to writesrc- source buffersrcOffset- source offsetbypassProtection- if true, memory page protection is disregarded and non-writable bytes may be written- Returns:
- the amount of bytes written (potentially less than requested if the method does not transact)
- Throws:
MemoryException- if the method failed
-
write
Write a range of bytes. This method should do its best to transact: the entire range is written, or nothing is and an error is thrown. If the implementor cannot support transaction, this method returns the amount of bytes written, or throws on error. The address range must be writable, else the method will throw.- Parameters:
address- start addresssize- number of bytes to writesrc- source buffersrcOffset- source offset- Returns:
- the amount of bytes written (potentially less than requested if the method does not transact)
- Throws:
MemoryException- if the method failed
-
readByte
Convenience method. Read a byte.- Parameters:
address- address to read- Returns:
- byte value
- Throws:
MemoryException- if reading fails
-
writeByte
Write a byte.- Parameters:
address- address to writev- byte value- Throws:
MemoryException- if writing fails
-
readLEShort
Read a little-endian 16-bit integer. This method does not do partial reads.- Parameters:
address- address to read- Returns:
- 16-bit value
- Throws:
MemoryException- if reading fails
-
writeLEShort
Write a little-endian 16-bit integer. This method does not do partial writes.- Parameters:
address- address to writev- 16-bit value- Throws:
MemoryException- if writing fails
-
readLEInt
Read a little-endian 32-bit integer. This method does not do partial reads.- Parameters:
address- address to read- Returns:
- 32-bit value
- Throws:
MemoryException- if reading fails
-
writeLEInt
Write a little-endian 32-bit integer. This method does not do partial writes.- Parameters:
address- address to writev- 32-bit value- Throws:
MemoryException- if writing fails
-
readLELong
Read a little-endian 64-bit integer. This method does not do partial reads.- Parameters:
address- address to read- Returns:
- 64-bit value
- Throws:
MemoryException- if reading fails
-
writeLELong
Write a little-endian 64-bit integer. This method does not do partial writes.- Parameters:
address- address to writev- 64-bit value- Throws:
MemoryException- if writing fails
-
readBEShort
Read a big-endian 16-bit integer. This method does not do partial reads.- Parameters:
address- address to read- Returns:
- 16-bit value
- Throws:
MemoryException- if reading fails
-
writeBEShort
Write a big-endian 16-bit integer. This method does not do partial writes.- Parameters:
address- address to writev- 16-bit value- Throws:
MemoryException- if writing fails
-
readBEInt
Read a big-endian 32-bit integer. This method does not do partial reads.- Parameters:
address- address to read- Returns:
- 32-bit value
- Throws:
MemoryException- if reading fails
-
writeBEInt
Write a big-endian 32-bit integer. This method does not do partial writes.- Parameters:
address- address to writev- 32-bit value- Throws:
MemoryException- if writing fails
-
readBELong
Read a big-endian 64-bit integer. This method does not do partial reads.- Parameters:
address- address to read- Returns:
- 64-bit value
- Throws:
MemoryException- if reading fails
-
writeBELong
Write a big-endian 64-bit integer. This method does not do partial writes.- Parameters:
address- address to writev- 64-bit value- Throws:
MemoryException- if writing fails
-
readShort
Read a 16-bit integer using thestandard endianness. This method does not do partial reads.- Parameters:
address- address to read- Returns:
- 16-bit value
- Throws:
MemoryException- if reading fails
-
writeShort
Write a 16-bit integer using thestandard endianness. This method does not do partial writes.- Parameters:
address- address to writev- 16-bit value- Throws:
MemoryException- if writing fails
-
readInt
Read a 32-bit integer using thestandard endianness. This method does not do partial reads.- Parameters:
address- address to read- Returns:
- 32-bit value
- Throws:
MemoryException- if reading fails
-
writeInt
Write a 32-bit integer using thestandard endianness. This method does not do partial writes.- Parameters:
address- address to writev- 32-bit value- Throws:
MemoryException- if writing fails
-
readLong
Read a 64-bit integer using thestandard endianness. This method does not do partial reads.- Parameters:
address- address to read- Returns:
- 64-bit value
- Throws:
MemoryException- if reading fails
-
writeLong
Write a 64-bit integer using thestandard endianness. This method does not do partial writes.- Parameters:
address- address to writev- 64-bit value- Throws:
MemoryException- if writing fails
-
readShort
Read a 16-bit integer. This method does not do partial reads.- Parameters:
address- address to readend- endianness- Returns:
- 16-bit value
- Throws:
MemoryException- if reading fails
-
writeShort
Write a 16-bit integer. This method does not do partial writes.- Parameters:
address- address to writev- 16-bit valueend- endianness- Throws:
MemoryException- if writing fails
-
readInt
Read a 32-bit integer. This method does not do partial reads.- Parameters:
address- address to readend- endianness- Returns:
- 32-bit value
- Throws:
MemoryException- if reading fails
-
writeInt
Write a 32-bit integer. This method does not do partial writes.- Parameters:
address- address to writev- 32-bit valueend- endianness- Throws:
MemoryException- if writing fails
-
readLong
Read a 64-bit integer. This method does not do partial reads.- Parameters:
address- address to readend- endianness- Returns:
- 64-bit value
- Throws:
MemoryException- if reading fails
-
writeLong
Write a 64-bit integer. This method does not do partial writes.- Parameters:
address- address to writev- 64-bit valueend- endianness- Throws:
MemoryException- if writing fails
-
readPointer
Read a pointer-sized integer.- Parameters:
address- address to read- Returns:
- pointer value
- Throws:
MemoryException- if reading fails
-
writePointer
Write a pointer-sized integer.- Parameters:
address- address to writeptr- pointer value- Throws:
MemoryException- if writing fails
-
addPropertyListener
Add a memory property listener.- Parameters:
listener- listener to add- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
removePropertyListener
Remove a memory property listener.- Parameters:
listener- listener to remove- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
addAllocListener
Add a memory allocation listener.- Parameters:
listener- listener to add- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
removeAllocListener
Remove a memory allocation listener.- Parameters:
listener- listener to remove- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
addFreeListener
Add a memory free listener.- Parameters:
listener- listener to add- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
removeFreeListener
Remove a memory free listener.- Parameters:
listener- listener to remove- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
addProtectionListener
Add a memory protection listener.- Parameters:
listener- listener to add- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
removeProtectionListener
Remove a memory protection listener.- Parameters:
listener- listener to remove- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
addPreWriteListener
Note: The callback is invoked before the write operation takes place.- Parameters:
listener- listener to add- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
removePreWriteListener
Remove a pre-write listener.- Parameters:
listener- listener to remove- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
addWriteListener
Add a memory write listener.- Parameters:
listener- listener to add- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
removeWriteListener
Remove a memory write listener.- Parameters:
listener- listener to remove- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-
setLazyMemoryProvider
void setLazyMemoryProvider(ILazyMemoryProvider lazyMemoryProvider, boolean skipFailedAllocations) throws MemoryException, UnsupportedOperationException Set a lazy memory provider. This operation is optional. If the memory object supports it, theILazyMemoryProvider.getRanges()method will be called to reserve the ranges of memory that the provider can provide. A lazy provider provides memory data on demand, i.e. when the data bytes are accessed (read or write).- Parameters:
lazyMemoryProvider- a non-null lazy memory providerskipFailedAllocations- if true, the provider will not hard-fail if it provides data for a range for which data already exists in the virtual memory; else, the method will throw- Throws:
MemoryException- if a page reservation failed and skipFailedAllocations isfalse(example: the memory provider requests to allocate an already allocated range)UnsupportedOperationException- if the implementation does not support that feature
-
getAproximateFootprint
int getAproximateFootprint()Determine the approximate memory footprint of this object, in kilobytes.- Returns:
- a size in Kb
- Throws:
UnsupportedOperationException- if the implementation does not support that feature
-