LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
runtime/Memory.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_RUNTIME_MEMORY_H
16#define LIEF_RUNTIME_MEMORY_H
17#include "LIEF/errors.hpp"
18#include <cstdint>
19#include <cstring>
20#include <ostream>
21#include <string>
22#include <utility>
23#include <vector>
24
26#include "LIEF/logging.hpp"
27#include "LIEF/visibility.h"
28#include <optional>
29
31
32
33namespace LIEF::runtime {
34
37 public:
39 enum MMAP_FLAGS : uint32_t {
41
43 MP_PRIVATE = 1 << 0,
44
46 MP_ANONYMOUS = 1 << 1,
47
49 MP_SHARED = 1 << 2,
50
52 MP_FIXED = 1 << 3,
53
55 MP_JIT = 1 << 4,
56 };
57
58 enum PERM : uint32_t {
59 P_NONE = 0,
60 P_READ = 1 << 0,
61 P_WRITE = 1 << 1,
62 P_EXEC = 1 << 2,
63 };
64
68 public:
69 friend class Memory;
70
71 Chunk(void* addr, size_t size, uint32_t permissions) :
72 addr_(addr),
73 size_(size),
74 permissions_(permissions) {}
75
76 Chunk(void* addr, size_t size) :
77 Chunk(addr, size, /*permissions=*/P_NONE) {}
78
79 Chunk(void* addr) :
80 Chunk(addr, /*size=*/0, /*permissions=*/P_NONE) {}
81
83 void* addr_ptr() {
84 return addr_;
85 }
86
87 const void* addr_ptr() const {
88 return addr_;
89 }
90
92 uintptr_t addr() const {
93 return reinterpret_cast<uintptr_t>(addr_);
94 }
95
97 size_t size() const {
98 return size_;
99 }
100
102 uint32_t permissions() const {
103 return permissions_;
104 }
105
107 uintptr_t page_start() const;
108
109
111 uintptr_t page_end() const;
112
115 if (!Memory::mprotect(*this, p)) {
116 logging::err("mprotect failed for chunk: {}", to_string());
117 }
118 permissions_ = p;
119 return *this;
120 }
121
126
131
136
141
146
150
152 bool is_valid() const {
153 return addr_ != nullptr;
154 }
155
156 operator bool() const {
157 return is_valid();
158 }
159
161 return Memory::munmap(*this);
162 }
163
164 std::string to_string() const;
165
166 friend LIEF_API std::ostream& operator<<(std::ostream& os, const Chunk& C) {
167 os << C.to_string();
168 return os;
169 }
170
171 protected:
172 void* addr_ = nullptr;
173 size_t size_ = 0;
174 uint32_t permissions_ = P_NONE;
175 };
176
179 public:
180 explicit ScopedPermissions(Chunk& chunk, uint32_t perms) :
181 chunk_(&chunk),
182 perms_(chunk.permissions()) {
183 chunk_->change_permissions(perms);
184 }
185
187 chunk_->change_permissions(perms_);
188 }
189
190 private:
191 Chunk* chunk_ = nullptr;
192 uint32_t perms_ = P_NONE;
193 };
194
196 static std::optional<Chunk> mmap(size_t size, uint32_t flags,
197 uint32_t permissions = P_NONE);
198
205 static std::optional<Chunk> mmap_hint(uint64_t hint, size_t size, uint32_t flags,
206 uint32_t permissions = P_NONE);
207
210
212 static ok_error_t mprotect(Chunk& C, uint32_t flags);
213
218 static ok_error_t write(const uint8_t* buffer, size_t size, uintptr_t addr) {
219 std::memcpy(reinterpret_cast<void*>(addr), buffer, size);
220 return ok();
221 }
222
227 static ok_error_t write(const std::vector<uint8_t>& buffer, uintptr_t addr) {
228 return write(buffer.data(), buffer.size(), addr);
229 }
230
232 template<class T, typename = std::enable_if_t<std::is_standard_layout_v<T> &&
233 std::is_trivial_v<T>>>
234 static ok_error_t write(const T& value, uintptr_t addr) {
235 return write(reinterpret_cast<const uint8_t*>(&value), sizeof(T), addr);
236 }
237
239 template<class T>
240 static T read(uintptr_t addr) {
241 T out{};
242 std::memcpy(&out, reinterpret_cast<const void*>(addr), sizeof(T));
243 return out;
244 }
245
248 static void read(uintptr_t addr, std::vector<uint8_t>& out, size_t size) {
249 if (out.size() < size) {
250 std::vector<uint8_t> buffer(size);
251 read(addr, buffer.data(), buffer.size());
252 out = std::move(buffer);
253 return;
254 }
255 read(addr, out.data(), size);
256 }
257
263 static void read(uintptr_t addr, uint8_t* out, size_t size) {
264 std::memcpy(out, reinterpret_cast<const void*>(addr), size);
265 }
266
267 static std::string perm_str(uint32_t flags);
268
269 static constexpr bool support_rwx() {
270 return platform() != PLATFORMS::IOS &&
271 !(platform() == PLATFORMS::OSX && arch() == ARCH::ARM64);
272 }
273};
274
275}
276
277#endif
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:118
Represents a contiguous chunk of memory allocated or inspected by the runtime.
Definition runtime/Memory.hpp:67
Chunk & make_rx()
Sets the permissions to Read and Execute.
Definition runtime/Memory.hpp:133
friend std::ostream & operator<<(std::ostream &os, const Chunk &C)
Definition runtime/Memory.hpp:166
Chunk(void *addr, size_t size, uint32_t permissions)
Definition runtime/Memory.hpp:71
uint32_t permissions() const
Returns the current permissions of the memory chunk.
Definition runtime/Memory.hpp:102
friend class Memory
Definition runtime/Memory.hpp:69
Chunk & make_rwx()
Sets the permissions to Read, Write, and Execute.
Definition runtime/Memory.hpp:138
Chunk(void *addr, size_t size)
Definition runtime/Memory.hpp:76
bool is_valid() const
Check if this chunk is valid.
Definition runtime/Memory.hpp:152
uintptr_t page_end() const
Returns the address of the end of the page containing this chunk.
Chunk(void *addr)
Definition runtime/Memory.hpp:79
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:123
std::string to_string() const
const void * addr_ptr() const
Definition runtime/Memory.hpp:87
size_t size() const
Returns the size of the memory chunk in bytes.
Definition runtime/Memory.hpp:97
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:143
Chunk & make_rw()
Sets the permissions to Read and Write.
Definition runtime/Memory.hpp:128
Chunk & change_permissions(uint32_t p)
Changes the permissions of the memory chunk.
Definition runtime/Memory.hpp:114
void * addr_ptr()
Returns the start address of the memory chunk as an opaque pointer.
Definition runtime/Memory.hpp:83
ok_error_t deallocate()
Definition runtime/Memory.hpp:160
uintptr_t addr() const
Returns the start address of the memory chunk.
Definition runtime/Memory.hpp:92
~ScopedPermissions()
Definition runtime/Memory.hpp:186
ScopedPermissions(Chunk &chunk, uint32_t perms)
Definition runtime/Memory.hpp:180
This class exposes API to access and manage memory.
Definition runtime/Memory.hpp:36
MMAP_FLAGS
Flags used when creating a memory map (mmap).
Definition runtime/Memory.hpp:39
@ MP_FIXED
Interpret the address as a fixed requirement.
Definition runtime/Memory.hpp:52
@ MP_JIT
Map for Just-In-Time code generation.
Definition runtime/Memory.hpp:55
@ MP_NONE
Definition runtime/Memory.hpp:40
@ MP_PRIVATE
Changes are private to this process (copy-on-write).
Definition runtime/Memory.hpp:43
@ MP_SHARED
Changes are shared.
Definition runtime/Memory.hpp:49
@ MP_ANONYMOUS
The mapping is not backed by any file.
Definition runtime/Memory.hpp:46
static constexpr bool support_rwx()
Definition runtime/Memory.hpp:269
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:263
static ok_error_t write(const T &value, uintptr_t addr)
Generic function to write a typed value.
Definition runtime/Memory.hpp:234
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:218
static std::string perm_str(uint32_t flags)
static std::optional< Chunk > mmap_hint(uint64_t hint, size_t size, uint32_t flags, uint32_t permissions=P_NONE)
Allocate a memory chunk through mmap-like function and place the allocation at or near the address gi...
static T read(uintptr_t addr)
Generic function to read a typed value.
Definition runtime/Memory.hpp:240
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:227
PERM
Definition runtime/Memory.hpp:58
@ P_READ
Definition runtime/Memory.hpp:60
@ P_WRITE
Definition runtime/Memory.hpp:61
@ P_EXEC
Definition runtime/Memory.hpp:62
@ P_NONE
Definition runtime/Memory.hpp:59
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:248
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
const char * to_string(lief_errors err)
void err(std::string_view msg)
Definition logging.hpp:143
Definition android/Host.hpp:23
@ ARM64
Definition runtime/utils.hpp:39
@ OSX
Definition runtime/utils.hpp:32
@ IOS
Definition runtime/utils.hpp:33
ok_t ok()
Return success for function with return type ok_error_t.
Definition errors.hpp:102
#define LIEF_API
Definition visibility.h:44