Arion 1.0.2-alpha
A high-performance C++ framework for emulating executable binaries.
 
Loading...
Searching...
No Matches
arion::MemoryManager Class Reference

#include <memory_manager.hpp>

Public Member Functions

 MemoryManager (std::weak_ptr< Arion > arion, size_t page_sz)
 
ADDR align_up (ADDR addr)
 
bool is_mapped (ADDR addr)
 
bool has_mapping (std::shared_ptr< ARION_MAPPING > mapping)
 
bool can_map (ADDR addr, size_t sz)
 
bool has_mapping_with_info (std::string info)
 
std::shared_ptr< ARION_MAPPINGget_mapping_by_info (std::string info)
 
std::shared_ptr< ARION_MAPPINGget_mapping_at (ADDR addr)
 
std::vector< std::shared_ptr< ARION_MAPPING > > get_mappings ()
 
std::string mappings_str ()
 
ADDR map (ADDR start_addr, size_t sz, PROT_FLAGS perms, std::string info="")
 
ADDR map_anywhere (ADDR addr, size_t sz, PROT_FLAGS perms, bool asc=true, std::string info="")
 
ADDR map_anywhere (size_t sz, PROT_FLAGS perms, bool asc=true, std::string info="")
 
void unmap (std::shared_ptr< ARION_MAPPING > mapping, ADDR start_addr, ADDR end_addr)
 
void unmap (std::shared_ptr< ARION_MAPPING > mapping)
 
void unmap (ADDR start_addr, ADDR end_addr)
 
void unmap (ADDR seg_addr)
 
void unmap_all ()
 
void protect (std::shared_ptr< ARION_MAPPING > mapping, ADDR start_addr, ADDR end_addr, PROT_FLAGS perms)
 
void protect (std::shared_ptr< ARION_MAPPING > mapping, PROT_FLAGS perms)
 
void protect (ADDR start_addr, ADDR end_addr, PROT_FLAGS perms)
 
void protect (ADDR seg_addr, PROT_FLAGS perms)
 
void resize_mapping (std::shared_ptr< ARION_MAPPING > mapping, ADDR start_addr, ADDR end_addr)
 
void resize_mapping (ADDR seg_addr, ADDR start_addr, ADDR end_addr)
 
std::vector< BYTEread (ADDR addr, size_t data_sz)
 
uint64_t read_val (ADDR addr, uint8_t n)
 
ADDR read_ptr (ADDR addr)
 
size_t read_sz (ADDR addr)
 
int read_fd (ADDR addr)
 
std::string read_hex (ADDR addr, size_t data_sz, char sep=0)
 
std::string read_ascii (ADDR addr, size_t data_sz)
 
std::string read_c_string (ADDR addr)
 
std::vector< ADDRread_ptr_arr (ADDR addr)
 
std::vector< cs_insnread_instrs (ADDR addr, size_t count)
 
void write (ADDR addr, BYTE *data, size_t data_sz)
 
void write_string (ADDR addr, std::string data)
 
void write_val (ADDR addr, uint64_t val, uint8_t n)
 
void write_ptr (ADDR addr, ADDR ptr)
 
void write_sz (ADDR addr, size_t sz)
 
void write_fd (ADDR addr, int fd)
 
void stack_push (uint64_t val)
 
void stack_push_bytes (BYTE *data, size_t data_sz)
 
void stack_push_string (std::string data)
 
void stack_align ()
 
uint64_t stack_pop ()
 
void set_brk (ADDR brk)
 
ADDR get_brk ()
 

Static Public Member Functions

static std::unique_ptr< MemoryManagerinitialize (std::weak_ptr< Arion > arion)
 

Public Attributes

std::unique_ptr< MemoryRecorderrecorder
 Used to record memory accesses to the associated Arion instance.
 

Private Member Functions

uint32_t to_uc_perms (PROT_FLAGS perms)
 
void insert_mapping (std::shared_ptr< ARION_MAPPING > mapping)
 
void remove_mapping (std::shared_ptr< ARION_MAPPING > mapping)
 
void merge_uc_mappings (ADDR start, ADDR end)
 
void merge_contiguous_uc_mappings ()
 

Private Attributes

std::weak_ptr< Arionarion
 The Arion instance associated with this instance.
 
size_t page_sz
 The size of a memory page in bytes. Used when allocating new pages.
 
ADDR brk = 0
 The current break address (brk). This is used to determine where new memory should be allocated.
 
std::vector< std::shared_ptr< ARION_MAPPING > > mappings
 A vector of all mappings currently present within the Arion instance.
 

Detailed Description

This class is responsible for managing memory mappings within an Arion emulation session. It allows the user to map and unmap memory regions, change their protection flags, and read/write from/to arbitrary addresses.

Constructor & Destructor Documentation

◆ MemoryManager()

arion::MemoryManager::MemoryManager ( std::weak_ptr< Arion arion,
size_t  page_sz 
)
inline

Builder for MemoryManager instances.

Parameters
[in]arionThe Arion instance associated with this instance.
[in]page_szThe size of a memory page in bytes. Used when allocating new pages.

Member Function Documentation

◆ align_up()

ADDR arion::MemoryManager::align_up ( ADDR  addr)

Aligns a given address to the next page boundary.

Parameters
[in]addrAddress to align.
Returns
Address aligned to the page size.

◆ can_map()

bool arion::MemoryManager::can_map ( ADDR  addr,
size_t  sz 
)

Checks whether a memory region can be mapped at a given address and size.

Parameters
[in]addrDesired starting address.
[in]szSize of the region to map.
Returns
True if the mapping can be created, false otherwise.

◆ get_brk()

ADDR arion::MemoryManager::get_brk ( )

Retrieves the current program break (brk).

Returns
The current break address.

◆ get_mapping_at()

std::shared_ptr< ARION_MAPPING > arion::MemoryManager::get_mapping_at ( ADDR  addr)

Retrieves the mapping that contains a given address.

Parameters
[in]addrThe address to look up.
Returns
The corresponding ARION_MAPPING if found, otherwise nullptr.

◆ get_mapping_by_info()

std::shared_ptr< ARION_MAPPING > arion::MemoryManager::get_mapping_by_info ( std::string  info)

Retrieves a mapping by its info string.

Parameters
[in]infoThe info string identifying the mapping.
Returns
The corresponding ARION_MAPPING if found, otherwise nullptr.

◆ get_mappings()

std::vector< std::shared_ptr< ARION_MAPPING > > arion::MemoryManager::get_mappings ( )

Returns a vector containing all currently active memory mappings.

Returns
A vector of shared pointers to ARION_MAPPING structures.

◆ has_mapping()

bool arion::MemoryManager::has_mapping ( std::shared_ptr< ARION_MAPPING mapping)

Checks whether a mapping exists in the current Arion memory layout.

Parameters
[in]mappingThe ARION_MAPPING to check.
Returns
True if the mapping exists, false otherwise.

◆ has_mapping_with_info()

bool arion::MemoryManager::has_mapping_with_info ( std::string  info)

Checks if a mapping with a specific info string exists.

Parameters
[in]infoThe info string associated with a mapping.
Returns
True if such a mapping exists, false otherwise.

◆ initialize()

static std::unique_ptr< MemoryManager > arion::MemoryManager::initialize ( std::weak_ptr< Arion arion)
static

Instanciates and initializes new MemoryManager objects with some parameters.

Parameters
[in]arionThe Arion instance associated with this instance.
Returns
A new MemoryManager instance.

◆ insert_mapping()

void arion::MemoryManager::insert_mapping ( std::shared_ptr< ARION_MAPPING mapping)
private

Inserts a new mapping in Arion memory.

Parameters
[in]mappingThe ARION_MAPPING to insert.

◆ is_mapped()

bool arion::MemoryManager::is_mapped ( ADDR  addr)

Checks whether a given address is mapped in Arion memory.

Parameters
[in]addrAddress to check.
Returns
True if the address is mapped, false otherwise.

◆ map()

ADDR arion::MemoryManager::map ( ADDR  start_addr,
size_t  sz,
PROT_FLAGS  perms,
std::string  info = "" 
)

Maps a memory region at a specific address.

Parameters
[in]start_addrThe starting address for the mapping.
[in]szSize of the region to map.
[in]permsMemory protection flags.
[in]infoOptional string identifying the mapping.
Returns
The starting address of the mapped region.

◆ map_anywhere() [1/2]

ADDR arion::MemoryManager::map_anywhere ( ADDR  addr,
size_t  sz,
PROT_FLAGS  perms,
bool  asc = true,
std::string  info = "" 
)

Maps a memory region near a specific address, automatically finding a suitable place.

Parameters
[in]addrPreferred starting address.
[in]szSize of the region to map.
[in]permsMemory protection flags.
[in]ascIf true, search upwards in memory; otherwise downwards.
[in]infoOptional info string for the mapping.
Returns
The address where the region was mapped.

◆ map_anywhere() [2/2]

ADDR arion::MemoryManager::map_anywhere ( size_t  sz,
PROT_FLAGS  perms,
bool  asc = true,
std::string  info = "" 
)

Maps a memory region at any available location.

Parameters
[in]szSize of the region to map.
[in]permsMemory protection flags.
[in]ascIf true, search upwards in memory; otherwise downwards.
[in]infoOptional info string for the mapping.
Returns
The address where the region was mapped.

◆ mappings_str()

std::string arion::MemoryManager::mappings_str ( )

Returns a string representation of all mappings.

Returns
A formatted string describing each memory mapping.

◆ merge_contiguous_uc_mappings()

void arion::MemoryManager::merge_contiguous_uc_mappings ( )
private

Merges all contiguous memory mappings (expensive operation).

◆ merge_uc_mappings()

void arion::MemoryManager::merge_uc_mappings ( ADDR  start,
ADDR  end 
)
private

Merges contiguous memory mappings in a given range.

Parameters
[in]startStart memory address of the mappings that should be merged.
[in]endEnd memory address of the mappings that should be merged.

◆ protect() [1/4]

void arion::MemoryManager::protect ( ADDR  seg_addr,
PROT_FLAGS  perms 
)

Changes protection flags for the mapping containing a specific address.

Parameters
[in]seg_addrAddress within the mapping to modify.
[in]permsNew protection flags.

◆ protect() [2/4]

void arion::MemoryManager::protect ( ADDR  start_addr,
ADDR  end_addr,
PROT_FLAGS  perms 
)

Changes protection flags for a memory range.

Parameters
[in]start_addrStart of the range.
[in]end_addrEnd of the range.
[in]permsNew protection flags.

◆ protect() [3/4]

void arion::MemoryManager::protect ( std::shared_ptr< ARION_MAPPING mapping,
ADDR  start_addr,
ADDR  end_addr,
PROT_FLAGS  perms 
)

Changes protection flags for a specific region within a mapping.

Parameters
[in]mappingThe mapping to modify.
[in]start_addrStart of the region.
[in]end_addrEnd of the region.
[in]permsNew protection flags.

◆ protect() [4/4]

void arion::MemoryManager::protect ( std::shared_ptr< ARION_MAPPING mapping,
PROT_FLAGS  perms 
)

Changes protection flags for an entire mapping.

Parameters
[in]mappingThe mapping to modify.
[in]permsNew protection flags.

◆ read()

std::vector< BYTE > arion::MemoryManager::read ( ADDR  addr,
size_t  data_sz 
)

Reads raw bytes from memory.

Parameters
[in]addrStarting address.
[in]data_szNumber of bytes to read.
Returns
Vector containing the read bytes.

◆ read_ascii()

std::string arion::MemoryManager::read_ascii ( ADDR  addr,
size_t  data_sz 
)

Reads a memory region and returns its ASCII representation.

Parameters
[in]addrStarting address.
[in]data_szNumber of bytes to read.
Returns
ASCII string.

◆ read_c_string()

std::string arion::MemoryManager::read_c_string ( ADDR  addr)

Reads a null-terminated C string from memory.

Parameters
[in]addrAddress of the string.
Returns
The read C string.

◆ read_fd()

int arion::MemoryManager::read_fd ( ADDR  addr)

Reads a file descriptor (int) from memory.

Parameters
[in]addrAddress to read from.
Returns
File descriptor value.

◆ read_hex()

std::string arion::MemoryManager::read_hex ( ADDR  addr,
size_t  data_sz,
char  sep = 0 
)

Reads memory as a hexadecimal string.

Parameters
[in]addrStarting address.
[in]data_szNumber of bytes to read.
[in]sepOptional separator character between bytes.
Returns
String representation of the data in hexadecimal.

◆ read_instrs()

std::vector< cs_insn > arion::MemoryManager::read_instrs ( ADDR  addr,
size_t  count 
)

Disassembles a number of instructions from a given address.

Parameters
[in]addrStarting address.
[in]countNumber of instructions to read.
Returns
Vector of Capstone instruction objects.

◆ read_ptr()

ADDR arion::MemoryManager::read_ptr ( ADDR  addr)

Reads a pointer-sized value from memory.

Parameters
[in]addrAddress to read from.
Returns
Pointer value read from memory.

◆ read_ptr_arr()

std::vector< ADDR > arion::MemoryManager::read_ptr_arr ( ADDR  addr)

Reads an array of pointers from memory.

Parameters
[in]addrStarting address of the array.
Returns
Vector of pointer values.

◆ read_sz()

size_t arion::MemoryManager::read_sz ( ADDR  addr)

Reads a size_t value from memory.

Parameters
[in]addrAddress to read from.
Returns
Size value read from memory.

◆ read_val()

uint64_t arion::MemoryManager::read_val ( ADDR  addr,
uint8_t  n 
)

Reads an integer value of a given size from memory.

Parameters
[in]addrAddress to read from.
[in]nNumber of bytes to read.
Returns
The read value as a 64-bit integer.

◆ remove_mapping()

void arion::MemoryManager::remove_mapping ( std::shared_ptr< ARION_MAPPING mapping)
private

Removes a mapping from Arion memory.

Parameters
[in]mappingThe ARION_MAPPING to remove.

◆ resize_mapping() [1/2]

void arion::MemoryManager::resize_mapping ( ADDR  seg_addr,
ADDR  start_addr,
ADDR  end_addr 
)

Resizes the mapping containing a specific address.

Parameters
[in]seg_addrAddress within the mapping.
[in]start_addrNew start address.
[in]end_addrNew end address.

◆ resize_mapping() [2/2]

void arion::MemoryManager::resize_mapping ( std::shared_ptr< ARION_MAPPING mapping,
ADDR  start_addr,
ADDR  end_addr 
)

Resizes an existing mapping to a new range.

Parameters
[in]mappingThe mapping to resize.
[in]start_addrNew start address.
[in]end_addrNew end address.

◆ set_brk()

void arion::MemoryManager::set_brk ( ADDR  brk)

Sets the current program break (brk).

Parameters
[in]brkNew break address.

◆ stack_align()

void arion::MemoryManager::stack_align ( )

Aligns the emulated stack pointer to the current word or ABI boundary.

◆ stack_pop()

uint64_t arion::MemoryManager::stack_pop ( )

Pops a 64-bit value from the emulated stack.

Returns
The popped value.

◆ stack_push()

void arion::MemoryManager::stack_push ( uint64_t  val)

Pushes a 64-bit value onto the emulated stack.

Parameters
[in]valValue to push.

◆ stack_push_bytes()

void arion::MemoryManager::stack_push_bytes ( BYTE data,
size_t  data_sz 
)

Pushes raw bytes onto the emulated stack.

Parameters
[in]dataPointer to the data to push.
[in]data_szSize of the data in bytes.

◆ stack_push_string()

void arion::MemoryManager::stack_push_string ( std::string  data)

Pushes a string onto the emulated stack.

Parameters
[in]dataString to push.

◆ to_uc_perms()

uint32_t arion::MemoryManager::to_uc_perms ( PROT_FLAGS  perms)
private

Converts Arion memory protection rights to Unicorn ones.

Parameters
[in]permsArion memory protection rights.
Returns
Unicorn memory protection rights.

◆ unmap() [1/4]

void arion::MemoryManager::unmap ( ADDR  seg_addr)

Unmaps the mapping containing a specific address.

Parameters
[in]seg_addrAny address within the mapping to remove.

◆ unmap() [2/4]

void arion::MemoryManager::unmap ( ADDR  start_addr,
ADDR  end_addr 
)

Unmaps a range of memory.

Parameters
[in]start_addrStart address of the region to unmap.
[in]end_addrEnd address of the region to unmap.

◆ unmap() [3/4]

void arion::MemoryManager::unmap ( std::shared_ptr< ARION_MAPPING mapping)

Unmaps an entire mapping.

Parameters
[in]mappingThe mapping to unmap.

◆ unmap() [4/4]

void arion::MemoryManager::unmap ( std::shared_ptr< ARION_MAPPING mapping,
ADDR  start_addr,
ADDR  end_addr 
)

Unmaps a memory range from an existing mapping.

Parameters
[in]mappingThe mapping to unmap from.
[in]start_addrStart of the range to unmap.
[in]end_addrEnd of the range to unmap.

◆ unmap_all()

void arion::MemoryManager::unmap_all ( )

Unmaps all memory regions.

◆ write()

void arion::MemoryManager::write ( ADDR  addr,
BYTE data,
size_t  data_sz 
)

Writes raw bytes to memory.

Parameters
[in]addrDestination address.
[in]dataPointer to the data.
[in]data_szSize of the data.

◆ write_fd()

void arion::MemoryManager::write_fd ( ADDR  addr,
int  fd 
)

Writes a file descriptor (int) to memory.

Parameters
[in]addrDestination address.
[in]fdFile descriptor to write.

◆ write_ptr()

void arion::MemoryManager::write_ptr ( ADDR  addr,
ADDR  ptr 
)

Writes a pointer to memory.

Parameters
[in]addrDestination address.
[in]ptrPointer value to write.

◆ write_string()

void arion::MemoryManager::write_string ( ADDR  addr,
std::string  data 
)

Writes a string to memory (without null-termination unless included).

Parameters
[in]addrDestination address.
[in]dataThe string to write.

◆ write_sz()

void arion::MemoryManager::write_sz ( ADDR  addr,
size_t  sz 
)

Writes a size_t value to memory.

Parameters
[in]addrDestination address.
[in]szSize value to write.

◆ write_val()

void arion::MemoryManager::write_val ( ADDR  addr,
uint64_t  val,
uint8_t  n 
)

Writes a fixed-size integer value to memory.

Parameters
[in]addrDestination address.
[in]valThe value to write.
[in]nNumber of bytes to write.

Member Data Documentation

◆ arion

std::weak_ptr<Arion> arion::MemoryManager::arion
private

The Arion instance associated with this instance.

◆ brk

ADDR arion::MemoryManager::brk = 0
private

The current break address (brk). This is used to determine where new memory should be allocated.

◆ mappings

std::vector<std::shared_ptr<ARION_MAPPING> > arion::MemoryManager::mappings
private

A vector of all mappings currently present within the Arion instance.

◆ page_sz

size_t arion::MemoryManager::page_sz
private

The size of a memory page in bytes. Used when allocating new pages.

◆ recorder

std::unique_ptr<MemoryRecorder> arion::MemoryManager::recorder

Used to record memory accesses to the associated Arion instance.


The documentation for this class was generated from the following file: