-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMemory.h
More file actions
86 lines (65 loc) · 2.62 KB
/
Copy pathMemory.h
File metadata and controls
86 lines (65 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef HACKLIB_MEMORY_H
#define HACKLIB_MEMORY_H
#include "hacklib/Handles.h"
#include <string>
#include <vector>
#include <cstdint>
namespace hl
{
using Protection = int;
static const Protection PROTECTION_NOACCESS = 0x0;
static const Protection PROTECTION_READ = 0x1;
static const Protection PROTECTION_WRITE = 0x2;
static const Protection PROTECTION_EXECUTE = 0x4;
static const Protection PROTECTION_GUARD = 0x8; // Only supported on Windows.
static const Protection PROTECTION_READ_WRITE = PROTECTION_READ | PROTECTION_WRITE;
static const Protection PROTECTION_READ_EXECUTE = PROTECTION_READ | PROTECTION_EXECUTE;
static const Protection PROTECTION_READ_WRITE_EXECUTE = PROTECTION_READ_WRITE | PROTECTION_EXECUTE;
// A region of memory on page boundaries with equal protection.
struct MemoryRegion
{
enum class Status
{
// A valid committed memory region. All fields are valid.
Valid,
// A free memory region. Only the base and size fields are valid.
Free,
// An invalid memory region. This is usually kernel address space. No fields are valid.
Invalid
};
Status status = Status::Invalid;
uintptr_t base = 0;
size_t size = 0;
Protection protection = hl::PROTECTION_NOACCESS;
hl::ModuleHandle hModule = 0;
std::string name;
};
// Returns the system page size in bytes.
uintptr_t GetPageSize();
// Allocate memory on own pages. This is useful when the protection
// is adjusted to prevent other data from being affected.
// PageFree must be used to free the retrieved memory block.
void* PageAlloc(size_t n, Protection protection);
void PageFree(void* p, size_t n = 0);
void PageProtect(const void* p, size_t n, Protection protection);
template <typename T, typename A>
void PageProtectVec(const std::vector<T, A>& vec, Protection protection)
{
PageProtect(vec.data(), vec.size() * sizeof(T), protection);
}
void* PageReserve(size_t n);
void PageCommit(void* p, size_t n, Protection protection);
void FlushICache(void* p, size_t n);
// An empty string requests the main module.
hl::ModuleHandle GetModuleByName(const std::string& name = "");
// Returns hl::NullModuleHandle when the address does not reside in a module.
hl::ModuleHandle GetModuleByAddress(uintptr_t adr);
std::string GetModulePath(hl::ModuleHandle hModule);
// On linux it is way more performant to use GetMemoryMap than to build
// one using this function.
MemoryRegion GetMemoryByAddress(uintptr_t adr, int pid = 0);
// The resulting memory map only contains regions with Status::Valid.
// A pid of zero can be passed for the current process.
std::vector<MemoryRegion> GetMemoryMap(int pid = 0);
}
#endif