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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
#include "VirtualMemory.h"
void VirtualMemory::MapRegion(WeakFileAccessor fileAccessor, AddressRange mappedRange, uint64_t fileOffset)
{
// Create a new VirtualMemoryRegion object
VirtualMemoryRegion region(fileOffset, std::move(fileAccessor));
// TODO: How to handle overlapping regions?
for (const auto& [existingRange, existingRegion] : m_regions)
{
if (existingRange.Overlaps(mappedRange))
{
// Handle overlapping regions, e.g., throw an exception or skip the mapping
BinaryNinja::LogError("Overlapping memory region %llx", existingRange.start);
}
}
// Insert the region into the map
m_regions.insert_or_assign(mappedRange, region);
}
std::optional<VirtualMemoryRegion> VirtualMemory::GetRegionAtAddress(uint64_t address, uint64_t& addressOffset)
{
if (const auto& it = m_regions.find(address); it != m_regions.end())
{
// The VirtualMemoryRegion object returned contains the page, and more importantly, the file pointer (there can
// be multiple in newer caches) This is relevant for reading out the data in the rest of this file. The second
// item in the returned pair is the offset of `address` within the file.
const auto& range = it->first;
auto mapping = it->second;
addressOffset = mapping.fileOffset + (address - range.start);
return mapping;
}
return std::nullopt;
}
std::optional<VirtualMemoryRegion> VirtualMemory::GetRegionAtAddress(uint64_t address)
{
uint64_t offset;
return GetRegionAtAddress(address, offset);
}
bool VirtualMemory::IsAddressMapped(uint64_t address)
{
return m_regions.find(address) != m_regions.end();
}
void VirtualMemory::WritePointer(uint64_t address, size_t pointer)
{
uint64_t offset;
auto region = GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
region->fileAccessor.lock()->WritePointer(offset, pointer);
}
uint64_t VirtualMemory::ReadPointer(uint64_t address)
{
switch (m_addressSize)
{
case 8:
return ReadUInt64(address);
case 4:
return ReadUInt32(address);
case 2:
return ReadUInt16(address);
default:
throw std::runtime_error("Unsupported address size");
}
}
std::string VirtualMemory::ReadCString(uint64_t address)
{
uint64_t offset;
auto region = GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
return region->fileAccessor.lock()->ReadNullTermString(offset);
}
uint8_t VirtualMemory::ReadUInt8(uint64_t address)
{
uint64_t offset;
auto region = GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
return region->fileAccessor.lock()->ReadUInt8(offset);
}
int8_t VirtualMemory::ReadInt8(uint64_t address)
{
uint64_t offset;
auto region = GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
return region->fileAccessor.lock()->ReadInt8(offset);
}
uint16_t VirtualMemory::ReadUInt16(uint64_t address)
{
uint64_t offset;
auto region = GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
return region->fileAccessor.lock()->ReadUInt16(offset);
}
int16_t VirtualMemory::ReadInt16(uint64_t address)
{
uint64_t offset;
auto region = GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
return region->fileAccessor.lock()->ReadInt16(offset);
}
uint32_t VirtualMemory::ReadUInt32(uint64_t address)
{
uint64_t offset;
auto region = GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
return region->fileAccessor.lock()->ReadUInt32(offset);
}
int32_t VirtualMemory::ReadInt32(uint64_t address)
{
uint64_t offset;
auto region = GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
return region->fileAccessor.lock()->ReadInt32(offset);
}
uint64_t VirtualMemory::ReadUInt64(uint64_t address)
{
uint64_t offset;
auto region = GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
return region->fileAccessor.lock()->ReadUInt64(offset);
}
int64_t VirtualMemory::ReadInt64(uint64_t address)
{
uint64_t offset;
auto region = GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
return region->fileAccessor.lock()->ReadInt64(offset);
}
BinaryNinja::DataBuffer VirtualMemory::ReadBuffer(uint64_t address, size_t length)
{
uint64_t offset;
auto region = GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
return region->fileAccessor.lock()->ReadBuffer(offset, length);
}
std::pair<const uint8_t*, const uint8_t*> VirtualMemory::ReadSpan(uint64_t address, size_t length)
{
uint64_t offset;
auto region = GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
return region->fileAccessor.lock()->ReadSpan(offset, length);
}
void VirtualMemory::Read(void* dest, uint64_t address, size_t length)
{
uint64_t offset;
auto region = GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
region->fileAccessor.lock()->Read(dest, offset, length);
}
VirtualMemoryReader::VirtualMemoryReader(std::shared_ptr<VirtualMemory> memory)
{
m_memory = memory;
m_cursor = 0;
}
std::string VirtualMemoryReader::ReadCString(uint64_t address, size_t maxLength)
{
uint64_t offset;
auto region = m_memory->GetRegionAtAddress(address, offset);
if (!region.has_value())
throw UnmappedRegionException(address);
// TODO: Advance cursor?
return region->fileAccessor.lock()->ReadNullTermString(offset, maxLength);
}
uint64_t VirtualMemoryReader::ReadULEB128(size_t cursorLimit)
{
uint64_t result = 0;
int bit = 0;
uint64_t offset;
auto mapping = m_memory->GetRegionAtAddress(m_cursor, offset);
auto fileLimit = offset + (cursorLimit - m_cursor);
auto fa = mapping->fileAccessor.lock();
auto* fileBuff = (uint8_t*)fa->Data();
do
{
if (offset >= fileLimit)
return -1;
uint64_t slice = ((uint64_t*)&((fileBuff)[offset]))[0] & 0x7f;
if (bit > 63)
return -1;
else
{
result |= (slice << bit);
bit += 7;
}
} while (((uint64_t*)&(fileBuff[offset++]))[0] & 0x80);
// TODO: There has got to be a better way to prevent this...
fa->Data(); // prevent deallocation of the fileAccessor as we're operating on the raw data buffer
return result;
}
int64_t VirtualMemoryReader::ReadSLEB128(size_t cursorLimit)
{
constexpr size_t BYTE_SIZE = 7; // Number of bits in each SLEB128 byte
constexpr size_t INT64_BITS = 64; // Total number of bits in an int64_t
int64_t value = 0;
size_t shift = 0;
uint64_t offset;
// Retrieve associated memory region and the file buffer
auto mapping = m_memory->GetRegionAtAddress(m_cursor, offset);
auto fileLimit = offset + (cursorLimit - m_cursor);
auto fileAccessor = mapping->fileAccessor.lock();
auto* fileBuffer = static_cast<uint8_t*>(fileAccessor->Data());
// Loop through the SLEB128 encoded bytes
while (offset < fileLimit)
{
uint8_t currentByte = fileBuffer[offset++];
value |= (static_cast<int64_t>(currentByte & 0x7F) << shift);
shift += BYTE_SIZE;
if ((currentByte & 0x80) == 0) // If MSB is not set, we're done
break;
}
// Properly sign-extend the value according to its size
value = (value << (INT64_BITS - shift)) >> (INT64_BITS - shift);
// TODO: There has got to be a better way to prevent this...
// Prevent deallocation of the fileAccessor
fileAccessor->Data();
return value;
}
uint64_t VirtualMemoryReader::ReadPointer()
{
return ReadPointer(m_cursor);
}
uint64_t VirtualMemoryReader::ReadPointer(uint64_t address)
{
m_cursor = address + m_memory->GetAddressSize();
return m_memory->ReadPointer(address);
}
uint8_t VirtualMemoryReader::ReadUInt8()
{
return ReadUInt8(m_cursor);
}
uint8_t VirtualMemoryReader::ReadUInt8(uint64_t address)
{
m_cursor = address + 1;
return m_memory->ReadUInt8(address);
}
int8_t VirtualMemoryReader::ReadInt8()
{
return ReadInt8(m_cursor);
}
int8_t VirtualMemoryReader::ReadInt8(uint64_t address)
{
m_cursor = address + 1;
return m_memory->ReadInt8(address);
}
uint16_t VirtualMemoryReader::ReadUInt16()
{
return ReadUInt16(m_cursor);
}
uint16_t VirtualMemoryReader::ReadUInt16(uint64_t address)
{
m_cursor = address + 2;
return m_memory->ReadUInt16(address);
}
int16_t VirtualMemoryReader::ReadInt16()
{
return ReadInt16(m_cursor);
}
int16_t VirtualMemoryReader::ReadInt16(uint64_t address)
{
m_cursor = address + 2;
return m_memory->ReadInt16(address);
}
uint32_t VirtualMemoryReader::ReadUInt32()
{
return ReadUInt32(m_cursor);
}
uint32_t VirtualMemoryReader::ReadUInt32(uint64_t address)
{
m_cursor = address + 4;
return m_memory->ReadUInt32(address);
}
int32_t VirtualMemoryReader::ReadInt32()
{
return ReadInt32(m_cursor);
}
int32_t VirtualMemoryReader::ReadInt32(uint64_t address)
{
m_cursor = address + 4;
return m_memory->ReadInt32(address);
}
uint64_t VirtualMemoryReader::ReadUInt64()
{
return ReadUInt64(m_cursor);
}
uint64_t VirtualMemoryReader::ReadUInt64(uint64_t address)
{
m_cursor = address + 8;
return m_memory->ReadUInt64(address);
}
int64_t VirtualMemoryReader::ReadInt64()
{
return ReadInt64(m_cursor);
}
int64_t VirtualMemoryReader::ReadInt64(uint64_t address)
{
m_cursor = address + 8;
return m_memory->ReadInt64(address);
}
BinaryNinja::DataBuffer VirtualMemoryReader::ReadBuffer(size_t length)
{
return ReadBuffer(m_cursor, length);
}
BinaryNinja::DataBuffer VirtualMemoryReader::ReadBuffer(uint64_t address, size_t length)
{
m_cursor = address + length;
return m_memory->ReadBuffer(address, length);
}
void VirtualMemoryReader::Read(void* dest, size_t length)
{
Read(dest, m_cursor, length);
}
void VirtualMemoryReader::Read(void* dest, uint64_t address, size_t length)
{
m_cursor = address + length;
m_memory->Read(dest, address, length);
}
|