LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
LIEF::runtime::Memory Class Reference

This class exposes API to access and manage memory. More...

#include <Memory.hpp>

Classes

class  Chunk
 Represents a contiguous chunk of memory allocated or inspected by the runtime. More...
class  ScopedPermissions
 RAII interface to change the permission within a determined scope. More...

Public Types

enum  MMAP_FLAGS : uint32_t {
  MP_NONE = 0 , MP_PRIVATE = 1 << 0 , MP_ANONYMOUS = 1 << 1 , MP_SHARED = 1 << 2 ,
  MP_FIXED = 1 << 3 , MP_JIT = 1 << 4
}
 Flags used when creating a memory map (mmap). More...
enum  PERM : uint32_t { P_NONE = 0 , P_READ = 1 << 0 , P_WRITE = 1 << 1 , P_EXEC = 1 << 2 }

Static Public Member Functions

static optional< Chunkmmap (size_t size, uint32_t flags, uint32_t permissions=P_NONE)
 Allocate a memory chunk through mmap-like function.
static ok_error_t munmap (Chunk &C)
 Deallocate a mmaped memory chunk.
static ok_error_t mprotect (Chunk &C, uint32_t flags)
 Sets the permission of the given memory chunk.
static ok_error_t write (const uint8_t *buffer, size_t size, uintptr_t addr)
 Write the buffer at the address given in the third parameter.
static ok_error_t write (const std::vector< uint8_t > &buffer, uintptr_t addr)
 Write the buffer at the address given in the third parameter.
template<class T, typename = typename std::enable_if<std::is_standard_layout<T>::value && std::is_trivial<T>::value>::type>
static ok_error_t write (const T &value, uintptr_t addr)
 Generic function to write a typed value.
template<class T>
static T read (uintptr_t addr)
 Generic function to read a typed value.
static void read (uintptr_t addr, std::vector< uint8_t > &out, size_t size)
 Read the content at the address pointed by the first parameter and write the result in the std::vector provided in the second parameter.
static void read (uintptr_t addr, uint8_t *out, size_t size)
 Read the content at the address pointed by the first parameter and write the result in the buffer provided in the second parameter.
static std::string perm_str (uint32_t flags)
static constexpr bool support_rwx ()

Detailed Description

This class exposes API to access and manage memory.

Member Enumeration Documentation

◆ MMAP_FLAGS

Flags used when creating a memory map (mmap).

Enumerator
MP_NONE 
MP_PRIVATE 

Changes are private to this process (copy-on-write).

MP_ANONYMOUS 

The mapping is not backed by any file.

MP_SHARED 

Changes are shared.

MP_FIXED 

Interpret the address as a fixed requirement.

MP_JIT 

Map for Just-In-Time code generation.

◆ PERM

enum LIEF::runtime::Memory::PERM : uint32_t
Enumerator
P_NONE 
P_READ 
P_WRITE 
P_EXEC 

Member Function Documentation

◆ mmap()

optional< Chunk > LIEF::runtime::Memory::mmap ( size_t size,
uint32_t flags,
uint32_t permissions = P_NONE )
static

Allocate a memory chunk through mmap-like function.

References P_NONE.

◆ mprotect()

ok_error_t LIEF::runtime::Memory::mprotect ( Chunk & C,
uint32_t flags )
static

Sets the permission of the given memory chunk.

Referenced by LIEF::runtime::Memory::Chunk::change_permissions().

◆ munmap()

ok_error_t LIEF::runtime::Memory::munmap ( Chunk & C)
static

Deallocate a mmaped memory chunk.

Referenced by LIEF::runtime::Memory::Chunk::deallocate().

◆ perm_str()

std::string LIEF::runtime::Memory::perm_str ( uint32_t flags)
static

◆ read() [1/3]

template<class T>
T LIEF::runtime::Memory::read ( uintptr_t addr)
inlinestatic

Generic function to read a typed value.

Referenced by read().

◆ read() [2/3]

void LIEF::runtime::Memory::read ( uintptr_t addr,
std::vector< uint8_t > & out,
size_t size )
inlinestatic

Read the content at the address pointed by the first parameter and write the result in the std::vector provided in the second parameter.

References read().

◆ read() [3/3]

void LIEF::runtime::Memory::read ( uintptr_t addr,
uint8_t * out,
size_t size )
inlinestatic

Read the content at the address pointed by the first parameter and write the result in the buffer provided in the second parameter.

This function assumes that the buffer in the second parameter is large enough to contain the data being read.

◆ support_rwx()

constexpr bool LIEF::runtime::Memory::support_rwx ( )
inlinestaticconstexpr

References LIEF::runtime::IOS.

◆ write() [1/3]

ok_error_t LIEF::runtime::Memory::write ( const std::vector< uint8_t > & buffer,
uintptr_t addr )
inlinestatic

Write the buffer at the address given in the third parameter.

This function assumes that the memory pointed by addr has the correct permission to write this buffer.

References write().

◆ write() [2/3]

template<class T, typename = typename std::enable_if<std::is_standard_layout<T>::value && std::is_trivial<T>::value>::type>
ok_error_t LIEF::runtime::Memory::write ( const T & value,
uintptr_t addr )
inlinestatic

Generic function to write a typed value.

References write().

◆ write() [3/3]

ok_error_t LIEF::runtime::Memory::write ( const uint8_t * buffer,
size_t size,
uintptr_t addr )
inlinestatic

Write the buffer at the address given in the third parameter.

This function assumes that the memory pointed by addr has the correct permission to write this buffer.

References LIEF::ok().

Referenced by write(), and write().


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