16#ifndef LIEF_RUNTIME_MEMORY_H
17#define LIEF_RUNTIME_MEMORY_H
94 return reinterpret_cast<uintptr_t
>(addr_);
154 return addr_ !=
nullptr;
157 operator bool()
const {
175 uint32_t permissions_ =
P_NONE;
183 perms_(chunk.permissions()) {
184 chunk_->change_permissions(perms);
188 chunk_->change_permissions(perms_);
192 Chunk* chunk_ =
nullptr;
197 static std::optional<Chunk>
mmap(
size_t size, uint32_t flags,
198 uint32_t permissions =
P_NONE);
211 std::memcpy(
reinterpret_cast<void*
>(addr), buffer, size);
220 return write(buffer.data(), buffer.size(), addr);
224 template<
class T,
typename = std::enable_if_t<std::is_standard_layout_v<T> &&
225 std::is_trivial_v<T>>>
227 return write(
reinterpret_cast<const uint8_t*
>(&value),
sizeof(T), addr);
232 static T
read(uintptr_t addr) {
234 std::memcpy(&out,
reinterpret_cast<const void*
>(addr),
sizeof(T));
240 static void read(uintptr_t addr, std::vector<uint8_t>& out,
size_t size) {
241 if (out.size() < size) {
242 std::vector<uint8_t> buffer(size);
243 read(addr, buffer.data(), buffer.size());
244 out = std::move(buffer);
247 read(addr, out.data(), size);
255 static void read(uintptr_t addr, uint8_t* out,
size_t size) {
256 std::memcpy(out,
reinterpret_cast<const void*
>(addr), size);
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:119
Represents a contiguous chunk of memory allocated or inspected by the runtime.
Definition runtime/Memory.hpp:68
Chunk & make_rx()
Sets the permissions to Read and Execute.
Definition runtime/Memory.hpp:134
friend std::ostream & operator<<(std::ostream &os, const Chunk &C)
Definition runtime/Memory.hpp:167
Chunk(void *addr, size_t size, uint32_t permissions)
Definition runtime/Memory.hpp:72
uint32_t permissions() const
Returns the current permissions of the memory chunk.
Definition runtime/Memory.hpp:103
friend class Memory
Definition runtime/Memory.hpp:70
Chunk & make_rwx()
Sets the permissions to Read, Write, and Execute.
Definition runtime/Memory.hpp:139
Chunk(void *addr, size_t size)
Definition runtime/Memory.hpp:77
bool is_valid() const
Check if this chunk is valid.
Definition runtime/Memory.hpp:153
uintptr_t page_end() const
Returns the address of the end of the page containing this chunk.
Chunk(void *addr)
Definition runtime/Memory.hpp:80
Chunk & cache_flush()
Flushes the instruction cache for this memory chunk. This should be used when modifying code in memor...
Chunk & make_x()
Sets the permissions to Execute only.
Definition runtime/Memory.hpp:124
std::string to_string() const
const void * addr_ptr() const
Definition runtime/Memory.hpp:88
size_t size() const
Returns the size of the memory chunk in bytes.
Definition runtime/Memory.hpp:98
uintptr_t page_start() const
Returns the address of the start of the page containing this chunk.
Chunk & make_ro()
Sets the permissions to Read Only.
Definition runtime/Memory.hpp:144
Chunk & make_rw()
Sets the permissions to Read and Write.
Definition runtime/Memory.hpp:129
Chunk & change_permissions(uint32_t p)
Changes the permissions of the memory chunk.
Definition runtime/Memory.hpp:115
void * addr_ptr()
Returns the start address of the memory chunk as an opaque pointer.
Definition runtime/Memory.hpp:84
ok_error_t deallocate()
Definition runtime/Memory.hpp:161
uintptr_t addr() const
Returns the start address of the memory chunk.
Definition runtime/Memory.hpp:93
~ScopedPermissions()
Definition runtime/Memory.hpp:187
ScopedPermissions(Chunk &chunk, uint32_t perms)
Definition runtime/Memory.hpp:181
This class exposes API to access and manage memory.
Definition runtime/Memory.hpp:37
MMAP_FLAGS
Flags used when creating a memory map (mmap).
Definition runtime/Memory.hpp:40
@ MP_FIXED
Interpret the address as a fixed requirement.
Definition runtime/Memory.hpp:53
@ MP_JIT
Map for Just-In-Time code generation.
Definition runtime/Memory.hpp:56
@ MP_NONE
Definition runtime/Memory.hpp:41
@ MP_PRIVATE
Changes are private to this process (copy-on-write).
Definition runtime/Memory.hpp:44
@ MP_SHARED
Changes are shared.
Definition runtime/Memory.hpp:50
@ MP_ANONYMOUS
The mapping is not backed by any file.
Definition runtime/Memory.hpp:47
static constexpr bool support_rwx()
Definition runtime/Memory.hpp:261
static ok_error_t mprotect(Chunk &C, uint32_t flags)
Sets the permission of the given memory chunk.
static ok_error_t munmap(Chunk &C)
Deallocate a mmaped memory chunk.
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 pro...
Definition runtime/Memory.hpp:255
static ok_error_t write(const T &value, uintptr_t addr)
Generic function to write a typed value.
Definition runtime/Memory.hpp:226
static std::optional< Chunk > mmap(size_t size, uint32_t flags, uint32_t permissions=P_NONE)
Allocate a memory chunk through mmap-like function.
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.
Definition runtime/Memory.hpp:210
static std::string perm_str(uint32_t flags)
static T read(uintptr_t addr)
Generic function to read a typed value.
Definition runtime/Memory.hpp:232
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.
Definition runtime/Memory.hpp:219
PERM
Definition runtime/Memory.hpp:59
@ P_READ
Definition runtime/Memory.hpp:61
@ P_WRITE
Definition runtime/Memory.hpp:62
@ P_EXEC
Definition runtime/Memory.hpp:63
@ P_NONE
Definition runtime/Memory.hpp:60
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::vecto...
Definition runtime/Memory.hpp:240
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
const char * to_string(lief_errors err)
void err(const std::string &msg)
Definition logging.hpp:143
Definition android/Host.hpp:24
@ IOS
Definition runtime/utils.hpp:34
ok_t ok()
Return success for function with return type ok_error_t.
Definition errors.hpp:103
#define LIEF_API
Definition visibility.h:45