-
Notifications
You must be signed in to change notification settings - Fork 3
/
Memory.cpp
352 lines (281 loc) · 11.5 KB
/
Memory.cpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "Memory.h"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <sstream>
#include <absl/log/log.h>
Memory::Memory(std::filesystem::path mmapPath,
std::shared_ptr<PCILeechWrapper> pcileech) : mmapPath_(mmapPath.string()),
pcileech(pcileech) {
}
Memory::~Memory() {
shutdown();
if (hVMM) pcileech->VMMDLL_Close(hVMM);
}
std::string Memory::mmapPath() const {
return mmapPath_;
}
bool Memory::generateMemoryMap() const {
LOG(INFO) << "Generating memory map...";
LPCSTR args[] = {(LPCSTR) "", (LPCSTR) "-v", (LPCSTR) "-printf", (LPCSTR) "-device", (LPCSTR) "fpga://algo=0"};
int argc = sizeof(args) / sizeof(args[0]);
VMM_HANDLE handle = pcileech->VMMDLL_Initialize(argc, args);
if (!handle) {
LOG(ERROR) << "pcileech->VMMDLL_Initialize failed" << std::endl;
return false;
}
PVMMDLL_MAP_PHYSMEM pPhysMemMap = NULL;
if (!pcileech->VMMDLL_Map_GetPhysMem(handle, &pPhysMemMap)) {
LOG(ERROR) << "pcileech->VMMDLL_Map_GetPhysMem failed" << std::endl;
pcileech->VMMDLL_Close(handle);
return false;
}
if (pPhysMemMap->dwVersion != VMMDLL_MAP_PHYSMEM_VERSION) {
LOG(ERROR) << "Invalid VMM Map Version" << std::endl;
pcileech->VMMDLL_MemFree(pPhysMemMap);
pcileech->VMMDLL_Close(handle);
return false;
}
if (pPhysMemMap->cMap == 0) {
LOG(ERROR) << "Failed to get physical memory map" << std::endl;
pcileech->VMMDLL_MemFree(pPhysMemMap);
pcileech->VMMDLL_Close(handle);
return false;
}
std::stringstream sb;
for (DWORD i = 0; i < pPhysMemMap->cMap; i++) {
sb << std::setfill('0') << std::setw(4) << i << " " << std::hex << pPhysMemMap->pMap[i].pa << " - " << (
pPhysMemMap->pMap[i].pa + pPhysMemMap->pMap[i].cb - 1) << " -> " << pPhysMemMap->pMap[i].pa <<
std::endl;
}
std::ofstream mmapFile(mmapPath_);
mmapFile << sb.str();
mmapFile.flush();
mmapFile.close();
pcileech->VMMDLL_MemFree(pPhysMemMap);
pcileech->VMMDLL_Close(handle);
return true;
}
bool Memory::init() {
if (!generateMemoryMap()) {
LOG(ERROR) << "Failed to generate memory map" << std::endl;
return false;
}
LPCSTR argv[] = {
(LPCSTR) "-printf", (LPCSTR) "-v", (LPCSTR) "-device", (LPCSTR) "fpga", (LPCSTR) "-memmap",
(LPCSTR) mmapPath_.c_str()
};
DWORD argc = sizeof(argv) / sizeof(LPCSTR);
hVMM = pcileech->VMMDLL_Initialize(argc, argv);
start();
return hVMM != NULL;
}
void Memory::start() {
if (dispatchThread == nullptr) {
bStop = false;
dispatchThread = new std::thread(&Memory::dispatchLoop, this);
}
}
void Memory::shutdown() {
bStop = true;
backlogPush.notify_all();
if (dispatchThread != nullptr) {
if (dispatchThread->joinable()) dispatchThread->join();
delete dispatchThread;
}
}
std::vector<ProcessInfo> Memory::getProcessSnapshot() const {
std::vector<ProcessInfo> procInfos;
size_t numProcs = 0;
if (!pcileech->VMMDLL_PidList(hVMM, NULL, &numProcs)) {
LOG(ERROR) << "Failed to get number of process IDs" << std::endl;
return procInfos;
}
PDWORD ptrProcessIDs = (PDWORD) malloc(numProcs * sizeof(DWORD));
if (!pcileech->VMMDLL_PidList(hVMM, ptrProcessIDs, &numProcs)) {
LOG(ERROR) << "Failed to get list of process IDs" << std::endl;
return procInfos;
}
for (int i = 0; i < numProcs; i++) {
auto pid = ptrProcessIDs[i];
auto name = pcileech->VMMDLL_ProcessGetInformationString(
hVMM, pid, VMMDLL_PROCESS_INFORMATION_OPT_STRING_PATH_USER_IMAGE);
if (name != NULL) procInfos.push_back(ProcessInfo(name, pid));
}
pcileech->VMMDLL_MemFree(ptrProcessIDs);
return procInfos;
}
std::vector<MemoryRegion> Memory::getModuleSnapshot(int processID) const {
std::vector<MemoryRegion> regions;
PVMMDLL_MAP_VAD memoryMap = nullptr;
// VAD == Virtual Address Descriptor
if (!pcileech->VMMDLL_Map_GetVadU(hVMM, processID, true, &memoryMap)) {
LOG(ERROR) << "Failed to get Virtual Address Descriptor for PID " << processID << std::endl;
return regions;
}
for (int i = 0; i < memoryMap->cMap; i++) {
regions.push_back(MemoryRegion(memoryMap->pMap[i]));
}
pcileech->VMMDLL_MemFree(memoryMap);
return regions;
}
std::vector<ThreadInfo> Memory::getThreadSnapshot(int processID) const {
std::vector<ThreadInfo> threads;
PVMMDLL_MAP_THREAD threadMap = nullptr;
if (!pcileech->VMMDLL_Map_GetThread(hVMM, processID, &threadMap)) {
LOG(ERROR) << "Failed ot get therad map for PID " << processID << std::endl;
return threads;
}
for (int i = 0; i < threadMap->cMap; i++) {
threads.push_back(ThreadInfo(threadMap->pMap[i]));
}
pcileech->VMMDLL_MemFree(threadMap);
return threads;
}
size_t Memory::read(char *buffer, int32_t processID, uint64_t address, size_t size) {
mtx.lock();
std::shared_ptr<BacklogItem> item = nullptr;
std::string key = std::to_string(processID) + ":" + std::to_string(address) + ":" + std::to_string(size);
if (backlog.contains(key)) {
item = backlog[key];
} else {
item = std::make_shared<BacklogItem>(processID, address, size);
backlog[key] = item;
backlogPush.notify_all();
}
mtx.unlock();
std::pair<size_t, const char *> result = item->get();
memcpy(buffer, result.second, result.first);
return result.first;
}
size_t Memory::write(const char *buffer, int32_t processID, uint64_t address, size_t size) {
sem.acquire();
if (!pcileech->VMMDLL_MemWrite(hVMM, processID, (ULONG64) address, (PBYTE) buffer, (DWORD) size)) {
sem.release();
return -1;
}
sem.release();
return size;
}
MemoryModel Memory::getMemoryModel(int processID) const {
VMMDLL_PROCESS_INFORMATION procInfo;
auto procInfoSize = sizeof(procInfo);
memset(&procInfo, 0, sizeof(VMMDLL_PROCESS_INFORMATION));
procInfo.magic = VMMDLL_PROCESS_INFORMATION_MAGIC;
procInfo.wVersion = VMMDLL_PROCESS_INFORMATION_VERSION;
if (!pcileech->VMMDLL_ProcessGetInformation(hVMM, processID, &procInfo, &procInfoSize)) {
LOG(ERROR) << "Failed to get process information data" << std::endl;
return MemoryModel::MemoryModel_UNKNOWN;
}
MemoryModel model;
switch (procInfo.tpMemoryModel) {
case VMMDLL_MEMORYMODEL_NA:
model = MemoryModel::MemoryModel_NA;
break;
case VMMDLL_MEMORYMODEL_X86PAE:
model = MemoryModel::MemoryModel_X86PAE;
break;
case VMMDLL_MEMORYMODEL_ARM64:
model = MemoryModel::MemoryModel_ARM64;
break;
case VMMDLL_MEMORYMODEL_X64:
model = MemoryModel::MemoryModel_X64;
break;
case VMMDLL_MEMORYMODEL_X86:
model = MemoryModel::MemoryModel_X86;
break;
default:
model = MemoryModel::MemoryModel_UNKNOWN;
break;
}
return model;
}
void Memory::dispatchLoop() {
VLOG(1) << "Dispatch loop started";
auto nextDispatchTime = Clock::now();
auto lastDispatchTime = Clock::now();
while (!bStop) {
mtx.lock();
if (backlog.empty()) {
mtx.unlock();
std::unique_lock<std::mutex> lock(mtx);
backlogPush.wait(lock, [&]() { return bStop || backlog.size() > 0; });
continue;
}
mtx.unlock();
while (backlog.size() < MAX_DISPATCH_SIZE && nextDispatchTime > Clock::now() && Clock::now() - lastDispatchTime
< std::chrono::nanoseconds(DMA_MAX_BACKLOG_BUILD_TIME_NS)) {
std::this_thread::sleep_for(std::chrono::nanoseconds(DMA_MAX_BACKLOG_BUILD_TIME_NS));
}
mtx.lock();
Backlog dispatchBuffer;
int pid = backlog.begin()->second->pid;
size_t requestedSoFar = 0;
for (auto &item: backlog) {
if (item.second->pid != pid) continue;
// we can only read up to 1G at a time
if (requestedSoFar + item.second->size >= MAX_BYTES_PER_DISPATCH) break;
dispatchBuffer[item.first] = item.second;
requestedSoFar += item.second->size;
if (dispatchBuffer.size() >= MAX_DISPATCH_SIZE) break;
}
for (auto &item: dispatchBuffer) {
backlog.erase(item.first);
}
lastDispatchTime = Clock::now();
nextDispatchTime = lastDispatchTime + std::chrono::nanoseconds(DMA_BACKOFF_TIMER_EXTEND_NS);
mtx.unlock();
std::async(std::launch::async, [this, &pid, &dispatchBuffer]() {
sem.acquire();
dispatch(pid, dispatchBuffer);
sem.release();
});
}
}
void Memory::dispatch(int pid, Backlog &snapshot) {
if (snapshot.size() <= SCATTER_READ_THRESHOLD) {
// sequential read
VLOG(10) << "Performing sequential read for " << snapshot.size() << " request(s)";
for (auto &item: snapshot) {
char *promiseBuffer = new char[item.second->size];
memset(promiseBuffer, 0, item.second->size);
DWORD read = 0;
if (!pcileech->VMMDLL_MemReadEx(hVMM, pid, (ULONG64) item.second->address, (PBYTE) promiseBuffer,
(DWORD) item.second->size, &read, scatterFlags)) {
LOG(ERROR) << "Failed to sequentially read " << item.second->size << " bytes at " << std::hex << item.
second->address;
}
item.second->promise.set_value({read, promiseBuffer});
pcileech->VMMDLL_MemFree(promiseBuffer);
}
} else {
// scatter read
VLOG(10) << "Performing scatter read for " << snapshot.size() << " request(s)";
VMMDLL_SCATTER_HANDLE hndScatter = pcileech->VMMDLL_Scatter_Initialize(hVMM, pid, scatterFlags);
for (auto &item: snapshot) {
if (!pcileech->VMMDLL_Scatter_Prepare(hndScatter, (QWORD) (item.second->address), item.second->size)) {
LOG(ERROR) << "Failed to prepare scatter read for " << item.second->size << " bytes at "
<< std::hex << item.second->address;
}
}
if (!pcileech->VMMDLL_Scatter_Execute(hndScatter)) {
LOG(ERROR) << "Failed to execute scatter read";
}
for (auto &item: snapshot) {
char *promiseBuffer = new char[item.second->size];
memset(promiseBuffer, 0, item.second->size);
DWORD read = 0;
if (!pcileech->VMMDLL_Scatter_Read(hndScatter, (QWORD) item.second->address, item.second->size,
(PBYTE) promiseBuffer,
&read)) {
LOG(ERROR) << "Failed to scatter read " << item.second->size << " bytes at " << std::hex << item.second
->address;
}
item.second->promise.set_value({read, promiseBuffer});
}
//pcileech->VMMDLL_Scatter_Clear(hndScatter, 0, VMMDLL_FLAG_NOCACHE);
pcileech->VMMDLL_Scatter_CloseHandle(hndScatter);
}
}