public class

VirtualMemoryUtil

extends Object
java.lang.Object
   ↳ com.pnfsoftware.jeb.core.units.code.asm.memory.VirtualMemoryUtil

Class Overview

Utility methods for virtual memory classes.

Summary

Public Constructors
VirtualMemoryUtil()
Public Methods
static long allocate(IVirtualMemory mem, long start, int size, int protection)
Search an available range and allocate at least the requested amount of bytes.
static int allocateFillGaps(IVirtualMemory mem, long address, int size, int protection)
Allocate a memory range, filling gaps in between already allocated pages if needed.
static int convertSegmentFlagsToVMFlags(int segmentFlags)
static IVirtualMemory createMemory(int spaceBits, int pageBits, Endianness standardEndianness)
Facade factory method returning a concrete, private implementation of an efficient page-based virtual memory.
static long findAvailableRange(IVirtualMemory mem, long start, int size)
Search for an available memory range.
static long findBytes(IVirtualMemory mem, boolean bypassProtection, long address, long lastAddress, byte[] pattern, byte[] patternMask)
Search for bytes in memory, with an optional binary mask.
static IVirtualMemoryShim getCopyOnWriteShim(IVirtualMemory mem)
static boolean isPageAllocated(IVirtualMemory mem, long address)
Verify if a page was allocated (regardless of protection rights on the page).
static int read(IVirtualMemory mem, long address)
Read a single byte and perform a zero-extension.
static boolean readByteSafe(IVirtualMemory mem, long address, byte[] out)
Read a byte safely (this method does not raise MemoryException).
static int readBytesSafe(IVirtualMemory mem, long address, int size, byte[] dst, int dstOffset, boolean bypassProtection)
Safely read at most the requested amount of bytes.
static int readBytesSafe(IVirtualMemory mem, long address, int size, byte[] dst, int dstOffset, int protection)
Safely read at most the requested amount of bytes.
static boolean readIntSafe(Endianness end, IVirtualMemory mem, long address, int[] out)
static boolean readLEIntSafe(IVirtualMemory mem, long address, int[] out)
static boolean readLELongSafe(IVirtualMemory mem, long address, long[] out)
static boolean readLEShortSafe(IVirtualMemory mem, long address, short[] out)
static boolean readLongSafe(Endianness end, IVirtualMemory mem, long address, long[] out)
static String readNullTerminatedStringSafe(IVirtualMemory mem, long address)
Read from memory a series of ASCII characters, terminated by a null byte, and build the corresponding String.
static String readNullTerminatedStringSafe(IVirtualMemory mem, long address, int maxSize)
Read from memory a series of ASCII characters, terminated by a null byte, and build the corresponding String.
static boolean readSafe(IVirtualMemory mem, long address, byte[] data)
Read an array of bytes from memory (this method does not raise MemoryException).
static boolean readShortSafe(Endianness end, IVirtualMemory mem, long address, short[] out)
static boolean writeByteSafe(IVirtualMemory mem, long address, byte v)
Write a byte safely (this method does not raise MemoryException).
static boolean writeIntSafe(Endianness end, IVirtualMemory mem, long address, int v)
static boolean writeLEIntSafe(IVirtualMemory mem, long address, int v)
static boolean writeLELongSafe(IVirtualMemory mem, long address, long v)
static boolean writeLEShortSafe(IVirtualMemory mem, long address, short v)
static boolean writeLongSafe(Endianness end, IVirtualMemory mem, long address, long v)
static boolean writeSafe(IVirtualMemory mem, long address, byte[] data)
Write an array of bytes to memory (this method does not raise MemoryException).
static boolean writeShortSafe(Endianness end, IVirtualMemory mem, long address, short v)
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public VirtualMemoryUtil ()

Public Methods

public static long allocate (IVirtualMemory mem, long start, int size, int protection)

Search an available range and allocate at least the requested amount of bytes. Do not confuse this method with allocate(long, int, int).

Parameters
start soft start address, considered a hint by this allocator routine; if you wish to allocate at a fixed, given address, use allocate(long, int, int)
size range size
Returns
  • the base address of the allocated memory range; 0 on error

public static int allocateFillGaps (IVirtualMemory mem, long address, int size, int protection)

Allocate a memory range, filling gaps in between already allocated pages if needed. (The given range [address, address + size) may have already allocated pages. Those pages will not be re-allocated.) When the method returns, the range [address, address + size) is guaranteed to be allocated.

Returns
  • the number of pages that were actually allocated

public static int convertSegmentFlagsToVMFlags (int segmentFlags)

public static IVirtualMemory createMemory (int spaceBits, int pageBits, Endianness standardEndianness)

Facade factory method returning a concrete, private implementation of an efficient page-based virtual memory.

public static long findAvailableRange (IVirtualMemory mem, long start, int size)

Search for an available memory range.

Parameters
start soft start address, a hint
size range size
Returns
  • the range start address, 0 on error

public static long findBytes (IVirtualMemory mem, boolean bypassProtection, long address, long lastAddress, byte[] pattern, byte[] patternMask)

Search for bytes in memory, with an optional binary mask. The search can either be forward (default), in which case the resulting found address will be either -1 or ≥ the start address, or backward, in which case the resulting found address will be either -1 or ≤ the start address.

Parameters
mem memory to be searched
address start address for the search (this value is treated as unsigned)
lastAddress end address (the value is treated as unsigned); may be less than the start address, in which case a reverse search will be performed
pattern bytes to search for; the maximum allowed length is the memory's page size
patternMask optional binary mask for a wildcard search; if non-null, must be the same length as the pattern's
Returns
  • -1 if not found, else, the resulting address

public static IVirtualMemoryShim getCopyOnWriteShim (IVirtualMemory mem)

public static boolean isPageAllocated (IVirtualMemory mem, long address)

Verify if a page was allocated (regardless of protection rights on the page).

public static int read (IVirtualMemory mem, long address)

Read a single byte and perform a zero-extension. This method does not raise MemoryException; on error, -1 is returned.

Returns
  • the byte, zero-extended over 32-bit; in other words, an integer in [0, 255]. On error, -1 is returned.

public static boolean readByteSafe (IVirtualMemory mem, long address, byte[] out)

Read a byte safely (this method does not raise MemoryException).

Parameters
mem virtual memory
address address
out a 1-unit long array that will receive the value read
Returns
  • success indicator

public static int readBytesSafe (IVirtualMemory mem, long address, int size, byte[] dst, int dstOffset, boolean bypassProtection)

Safely read at most the requested amount of bytes.

Parameters
mem the memory
address target address
size requested read size
dst destination buffer
dstOffset position within the destination buffer
bypassProtection if true, all pages will be read, including non-resable pages; else, only readable pages will be read
Returns
  • the number of bytes read; may be less than requested. -1 if an unknown error occurs

public static int readBytesSafe (IVirtualMemory mem, long address, int size, byte[] dst, int dstOffset, int protection)

Safely read at most the requested amount of bytes.

Parameters
mem the memory
address target address
size requested read size
dst destination buffer
dstOffset position within the destination buffer
protection the requested protection level, must contain at least ACCESS_READ
Returns
  • the number of bytes read; may be less than requested. -1 if an unknown error occurs

public static boolean readIntSafe (Endianness end, IVirtualMemory mem, long address, int[] out)

public static boolean readLEIntSafe (IVirtualMemory mem, long address, int[] out)

public static boolean readLELongSafe (IVirtualMemory mem, long address, long[] out)

public static boolean readLEShortSafe (IVirtualMemory mem, long address, short[] out)

public static boolean readLongSafe (Endianness end, IVirtualMemory mem, long address, long[] out)

public static String readNullTerminatedStringSafe (IVirtualMemory mem, long address)

Read from memory a series of ASCII characters, terminated by a null byte, and build the corresponding String. If no null-byte is found, the search stops at 100 characters.

This method does not raise MemoryException.

Returns
  • string, or null if failure (i.e. no null byte found)

public static String readNullTerminatedStringSafe (IVirtualMemory mem, long address, int maxSize)

Read from memory a series of ASCII characters, terminated by a null byte, and build the corresponding String. If no null-byte is found, the search stops at the provided maxSize.

This method does not raise MemoryException.

Returns
  • string, or null if failure (i.e. no null byte found)

public static boolean readSafe (IVirtualMemory mem, long address, byte[] data)

Read an array of bytes from memory (this method does not raise MemoryException).

Returns
  • success indicator

public static boolean readShortSafe (Endianness end, IVirtualMemory mem, long address, short[] out)

public static boolean writeByteSafe (IVirtualMemory mem, long address, byte v)

Write a byte safely (this method does not raise MemoryException).

Parameters
mem virtual memory
address address
v the value to be written
Returns
  • success indicator

public static boolean writeIntSafe (Endianness end, IVirtualMemory mem, long address, int v)

public static boolean writeLEIntSafe (IVirtualMemory mem, long address, int v)

public static boolean writeLELongSafe (IVirtualMemory mem, long address, long v)

public static boolean writeLEShortSafe (IVirtualMemory mem, long address, short v)

public static boolean writeLongSafe (Endianness end, IVirtualMemory mem, long address, long v)

public static boolean writeSafe (IVirtualMemory mem, long address, byte[] data)

Write an array of bytes to memory (this method does not raise MemoryException).

Returns
  • success indicator

public static boolean writeShortSafe (Endianness end, IVirtualMemory mem, long address, short v)