-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMemory_UNIX.cpp
More file actions
201 lines (158 loc) · 5.03 KB
/
Copy pathMemory_UNIX.cpp
File metadata and controls
201 lines (158 loc) · 5.03 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "hacklib/Memory.h"
#include "hacklib/BitManip.h"
#include "hacklib/Logging.h"
#include <algorithm>
#include <dlfcn.h>
#include <fstream>
#include <stdexcept>
#include <sys/mman.h>
#include <unistd.h>
static int ToUnixProt(hl::Protection protection)
{
int unixProt = PROT_NONE;
if (protection & hl::PROTECTION_READ)
unixProt |= PROT_READ;
if (protection & hl::PROTECTION_WRITE)
unixProt |= PROT_WRITE;
if (protection & hl::PROTECTION_EXECUTE)
unixProt |= PROT_EXEC;
if (protection & hl::PROTECTION_GUARD)
throw std::runtime_error("protection not supported");
return unixProt;
}
[[maybe_unused]] static hl::Protection FromUnixProt(int unixProt)
{
hl::Protection protection = hl::PROTECTION_NOACCESS;
if (unixProt & PROT_READ)
protection |= hl::PROTECTION_READ;
if (unixProt & PROT_WRITE)
protection |= hl::PROTECTION_WRITE;
if (unixProt & PROT_EXEC)
protection |= hl::PROTECTION_EXECUTE;
return protection;
}
uintptr_t hl::GetPageSize()
{
static uintptr_t pageSize = 0;
if (!pageSize)
{
pageSize = (uintptr_t)sysconf(_SC_PAGESIZE);
}
return pageSize;
}
void* hl::PageAlloc(size_t n, hl::Protection protection)
{
return mmap(nullptr, n, ToUnixProt(protection), MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
}
void hl::PageFree(void* p, size_t n)
{
if (!n)
throw std::runtime_error("you must specify the free size on linux");
munmap(p, n);
}
void hl::PageProtect(const void* p, size_t n, hl::Protection protection)
{
// Align to page boundary.
auto pAligned = hl::AlignDown(p, hl::GetPageSize());
const size_t nAligned = (uintptr_t)p - (uintptr_t)pAligned + n;
// const_cast: The memory contents will remain unchanged, so this is fine.
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
HL_APICHECK(0 == mprotect(const_cast<void*>(pAligned), nAligned, ToUnixProt(protection)));
}
void* hl::PageReserve(size_t n)
{
return mmap(nullptr, n, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
}
void hl::PageCommit(void* p, size_t n, hl::Protection protection)
{
// Align to page boundary.
auto pAligned = hl::AlignDown(p, hl::GetPageSize());
const size_t nAligned = (uintptr_t)p - (uintptr_t)pAligned + n;
HL_APICHECK(MAP_FAILED !=
mmap(pAligned, nAligned, ToUnixProt(protection), MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0));
}
void hl::FlushICache(void* p, size_t n)
{
__builtin___clear_cache((char*)p, (char*)p + n);
}
hl::ModuleHandle hl::GetModuleByName(const std::string& name)
{
void* handle = nullptr;
if (name == "")
{
handle = dlopen(NULL, RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
}
else
{
handle = dlopen(name.c_str(), RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
}
if (handle)
{
// This is undocumented, but works and is easy.
auto hModule = *(hl::ModuleHandle*)handle;
if (!hModule)
{
hModule = (hl::ModuleHandle)0x400000;
}
dlclose(handle);
return hModule;
}
return hl::NullModuleHandle;
}
hl::ModuleHandle hl::GetModuleByAddress(uintptr_t adr)
{
Dl_info info = { 0 };
dladdr((void*)adr, &info);
return info.dli_fbase;
}
std::string hl::GetModulePath(hl::ModuleHandle hModule)
{
Dl_info info = { 0 };
HL_APICHECK(dladdr((void*)hModule, &info));
return info.dli_fname;
}
hl::MemoryRegion hl::GetMemoryByAddress(uintptr_t adr, int pid)
{
hl::MemoryRegion region;
auto memoryMap = hl::GetMemoryMap(pid);
auto itRegion = std::ranges::find_if(memoryMap, [adr](const hl::MemoryRegion& r)
{ return adr >= r.base && adr < r.base + r.size; });
if (itRegion != memoryMap.end())
{
region = *itRegion;
}
return region;
}
std::vector<hl::MemoryRegion> hl::GetMemoryMap(int pid)
{
std::vector<hl::MemoryRegion> regions;
if (!pid)
pid = getpid();
char fileName[32];
sprintf(fileName, "/proc/%d/maps", pid);
std::ifstream file(fileName);
unsigned long long start = 0, end = 0;
char flags[32];
unsigned long long file_offset = 0;
int dev_major = 0, dev_minor = 0;
unsigned long long inode = 0;
char path[512];
std::string line;
while (std::getline(file, line))
{
path[0] = '\0';
sscanf(line.c_str(), "%Lx-%Lx %31s %Lx %x:%x %Lu %511s", &start, &end, flags, &file_offset, &dev_major,
&dev_minor, &inode, path);
hl::MemoryRegion region;
region.status = hl::MemoryRegion::Status::Valid;
region.base = (uintptr_t)start;
region.size = (size_t)(end - start);
region.protection = ((flags[0] == 'r') ? hl::PROTECTION_READ : 0) |
((flags[1] == 'w') ? hl::PROTECTION_WRITE : 0) |
((flags[2] == 'x') ? hl::PROTECTION_EXECUTE : 0);
region.hModule = hl::GetModuleByAddress(region.base);
region.name = path;
regions.push_back(region);
}
return regions;
}