-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathsearch.cpp
518 lines (467 loc) · 13.1 KB
/
search.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include "search.h"
#include "data.h"
#include <unordered_map>
#include <unordered_set>
using namespace Retro;
using namespace std;
vector<DataType> s_defaultTypes{
">d8", ">n8",
">d6", ">n6",
"<u4", ">u4", "><u4", "<>u4", ">=u4", "<=u4", "=u4",
"<i4", ">i4", "><i4", "<>i4", ">=i4", "<=i4", "=i4",
"<d4", ">d4", "><d4", "<>d4", ">=d4", "<=d4", "=d4",
"<n4", ">n4", "><n4", "<>n4", ">=n4", "<=n4", "=n4",
"<u2", ">u2", "=u2",
"<i2", ">i2", "=i2",
"<d2", ">d2", "=d2",
"|u1", "|i1", "|d1"
};
bool SearchResult::operator<(const SearchResult& other) const {
if (address < other.address) {
return true;
}
if (address > other.address) {
return false;
}
if (mult < other.mult) {
return true;
}
if (mult > other.mult) {
return false;
}
if (div < other.div) {
return true;
}
if (div > other.div) {
return false;
}
if (bias < other.bias) {
return true;
}
return false;
}
bool SearchResult::operator==(const SearchResult& other) const {
if (address != other.address) {
return false;
}
if (mult != other.mult) {
return false;
}
if (div != other.div) {
return false;
}
if (bias != other.bias) {
return false;
}
return true;
}
bool SearchResult::operator!=(const SearchResult& other) const {
return !(*this == other);
}
TypedSearchResult::TypedSearchResult(SearchResult&& result, DataType&& type)
: SearchResult(result)
, type(type) {
}
TypedSearchResult::TypedSearchResult(const SearchResult& result, const DataType& type)
: SearchResult(result)
, type(type) {
}
bool TypedSearchResult::operator==(const TypedSearchResult& other) const {
return *static_cast<const SearchResult*>(this) == static_cast<const SearchResult&>(other) && type == other.type;
}
TypedSearchResult::operator Variable() const {
return Variable{ type, address };
}
Search::Search()
: m_types(s_defaultTypes) {
}
Search::Search(const vector<DataType>& types)
: m_types(types) {
}
void Search::search(const AddressSpace& mem, int64_t value) {
vector<SearchResult> results = makeResults(searchValue(mem, value));
int64_t vscale = 1;
int64_t v10 = value;
while (v10 && !(v10 % 10)) {
v10 /= 10;
vscale *= 10;
const auto& r = makeResults(searchValue(mem, v10), 1, vscale);
results.insert(results.end(), r.begin(), r.end());
}
vscale = 1;
int64_t v16 = value;
while (v16 && !(v16 & 0xF)) {
v16 >>= 4;
vscale <<= 4;
const auto& r = makeResults(searchValue(mem, v16), 1, vscale);
results.insert(results.end(), r.begin(), r.end());
}
vscale = 1;
int64_t v2 = value;
while (v2 && v2 < 0x100000000 && vscale < 4) {
v2 <<= 1;
vscale <<= 1;
const auto& r = makeResults(searchValue(mem, v2), vscale);
results.insert(results.end(), r.begin(), r.end());
}
int64_t v1 = value + 1;
{
const auto& r = makeResults(searchValue(mem, v1), 1, 1, 1);
results.insert(results.end(), r.begin(), r.end());
}
v1 = value - 1;
{
const auto& r = makeResults(searchValue(mem, v1), 1, 1, -1);
results.insert(results.end(), r.begin(), r.end());
}
int64_t vBcd = toBcd(value);
if (vBcd != value) {
{
const auto& r = makeResults(searchValue(mem, vBcd));
results.insert(results.end(), r.begin(), r.end());
}
vscale = 1;
while (vBcd && !(vBcd & 0xF)) {
vBcd >>= 4;
vscale <<= 4;
const auto& r = makeResults(searchValue(mem, vBcd), 1, vscale);
results.insert(results.end(), r.begin(), r.end());
}
}
int64_t vNBcd = toLNBcd(value);
if (vNBcd != value) {
{
const auto& r = makeResults(searchValue(mem, vNBcd));
results.insert(results.end(), r.begin(), r.end());
}
vscale = 1;
while (vNBcd && !(vNBcd & 0xF)) {
vNBcd >>= 8;
vscale <<= 8;
const auto& r = makeResults(searchValue(mem, vNBcd), 1, vscale);
results.insert(results.end(), r.begin(), r.end());
}
}
sort(results.begin(), results.end());
auto last = unique(results.begin(), results.end());
results.erase(last, results.end());
reduceOnTypes(mem, results, value);
}
void Search::delta(const AddressSpace& mem, const AddressSpace& oldMem, Operation op, int64_t reference) {
vector<DataType> newTypes;
multimap<size_t, DataType> results;
for (const auto& type : m_types) {
for (const auto& block : mem.blocks()) {
const MemoryView<>& oldBlock = oldMem.block(block.first);
const DynamicMemoryView dynmem(const_cast<void*>(block.second.offset(0)), block.second.size(), type, mem.overlay());
const DynamicMemoryView dynmemOld(const_cast<void*>(oldBlock.offset(0)), oldBlock.size(), type, mem.overlay());
vector<size_t> addresses;
if (m_hasStarted) {
for (const auto& result : m_current) {
if (result.type == type && result.address >= block.first && result.address + type.width - block.first <= block.second.size()) {
addresses.push_back(result.address);
}
}
if (!addresses.size()) {
continue;
}
}
if (addresses.size()) {
for (const auto& i : addresses) {
int64_t delta = dynmem[i - block.first] - dynmemOld[i - block.first];
if (calculate(op, reference, delta)) {
if (!newTypes.size() || newTypes.back() != type) {
newTypes.emplace_back(type);
}
results.emplace(i, type);
}
}
} else {
for (size_t i = 0; i + type.width <= block.second.size(); ++i) {
int64_t delta = dynmem[i] - dynmemOld[i];
if (calculate(op, reference, delta)) {
if (!newTypes.size() || newTypes.back() != type) {
newTypes.emplace_back(type);
}
results.emplace(i + block.first, type);
}
}
}
}
}
m_types = move(newTypes);
vector<TypedSearchResult> results2;
for (const auto& result : results) {
results2.emplace_back(SearchResult{ result.first, 1, 1, 0 }, result.second);
}
intersectCurrent(move(results2));
}
vector<SearchResult> Search::results() const {
vector<SearchResult> results;
for (const auto& iter : m_current) {
if (results.size() && results.back() == iter) {
continue;
}
results.emplace_back(iter);
}
return results;
}
const vector<TypedSearchResult>& Search::typedResults() const {
return m_current;
}
vector<DataType> Search::validTypes() const {
return m_types;
}
void Search::stuff(const vector<TypedSearchResult>& fakeResults) {
m_current = vector<TypedSearchResult>(fakeResults.begin(), fakeResults.end());
m_hasStarted = true;
}
void Search::remove(const vector<TypedSearchResult>& removedResults) {
vector<TypedSearchResult> out;
unordered_set<TypedSearchResult, hash<TypedSearchResult>> results{ removedResults.begin(), removedResults.end() };
for (const auto& result : m_current) {
if (results.find(result) == results.end()) {
out.emplace_back(result);
}
}
m_current = move(out);
}
size_t Search::numResults() const {
return m_current.size();
}
bool Search::hasUniqueResult() const {
if (!m_current.size()) {
return false;
}
const TypedSearchResult& result = m_current.front();
for (const auto& iter : m_current) {
if (result == iter || static_cast<const SearchResult&>(result) == iter) {
continue;
}
if (result.address + result.type.width - 1 != iter.address + iter.type.width - 1) {
return false;
}
}
return true;
}
TypedSearchResult Search::uniqueResult() const {
return m_current.front();
}
Search& Search::operator=(const Search& other) {
m_current.clear();
for (const auto& iter : other.m_current) {
m_current.emplace_back(iter);
}
m_types.clear();
for (const auto& iter : other.m_types) {
m_types.emplace_back(iter);
}
m_hasStarted = other.m_hasStarted;
return *this;
}
vector<SearchResult> Search::makeResults(vector<size_t> addrs, uint64_t mult, uint64_t div, int64_t bias) {
vector<SearchResult> out;
for (const auto& addr : addrs) {
out.emplace_back(SearchResult{ addr, mult, div, bias });
}
return out;
}
vector<size_t> Search::searchValue(const AddressSpace& mem, int64_t value) {
uint64_t unsignedValue = value;
if (value < 0) {
if (value > -0x100) {
unsignedValue = value & 0xFF;
} else if (value > -0x10000) {
unsignedValue = value & 0xFFFF;
} else if (value > -0x100000000) {
unsignedValue = value & 0xFFFFFFFF;
}
}
vector<size_t> start;
vector<size_t> end;
int nbytes = 0;
while (unsignedValue || !nbytes) {
++nbytes;
uint8_t b = unsignedValue;
if (start.size() && end.size()) {
const auto& s = searchByte(mem, b, start, -1);
const auto& e = searchByte(mem, b, end, 1);
start.insert(start.end(), s.begin(), s.end());
end.insert(end.end(), e.begin(), e.end());
} else {
start = searchByte(mem, b);
end = start;
}
unsignedValue >>= 8;
}
sort(start.begin(), start.end());
sort(end.begin(), end.end());
vector<size_t> results = overlap(start, end, nbytes);
vector<size_t> startWide;
int remainingBytes = 4 - nbytes;
int xBytes = 0;
while (xBytes < remainingBytes) {
if (startWide.size()) {
const auto& s = searchByte(mem, 0, startWide, -1);
startWide.insert(startWide.end(), s.begin(), s.end());
} else if (!xBytes && start.size()) {
startWide = searchByte(mem, 0, start, -1);
} else {
break;
}
++xBytes;
}
if (startWide.size()) {
results.insert(results.end(), startWide.begin(), startWide.end());
}
return results;
}
vector<size_t> Search::searchByte(const AddressSpace& mem, uint8_t value, const vector<size_t> addresses, ssize_t offset) {
vector<size_t> results;
if (addresses.size()) {
for (const auto& i : addresses) {
if (offset < 0 && i < -offset) {
continue;
} else if (!mem.hasBlock(i) || !mem.hasBlock(i + offset)) {
continue;
}
if (mem[i + offset] == value) {
results.push_back(i + offset);
}
}
} else {
for (const auto& block : mem.blocks()) {
if (offset < 0) {
offset = 0;
}
for (ssize_t i = offset; i < block.second.size(); ++i) {
if (block.second[i] == value) {
results.push_back(i + block.first);
}
}
}
}
return results;
}
vector<size_t> Search::overlap(const vector<size_t> start, vector<size_t> end, size_t width) {
vector<size_t> results;
auto siter = start.cbegin();
auto eiter = end.cbegin();
while (siter != start.cend() && eiter != end.cend()) {
size_t e = *siter + width - 1;
if (e < *eiter) {
++siter;
} else if (e > *eiter) {
++eiter;
} else {
results.push_back(*siter);
++siter;
++eiter;
}
}
return results;
}
void Search::reduceOnTypes(const AddressSpace& mem, const vector<SearchResult>& in, int64_t value) {
DataType bcd("=d8");
multimap<SearchResult, DataType> results;
for (const auto& type : m_types) {
auto result = in.cbegin();
for (const auto& block : mem.blocks()) {
const DynamicMemoryView dynmem(const_cast<void*>(block.second.offset(0)), block.second.size(), type, mem.overlay());
for (; result != in.cend(); ++result) {
if (type.width + result->address - block.first > block.second.size()) {
break;
}
if (result->address < block.first) {
continue;
}
int64_t inmem = dynmem[result->address - block.first];
if (type.repr == Repr::BCD) {
if (!isBcd(result->mult) || !isBcd(result->div)) {
continue;
}
inmem /= bcd.decode(&result->mult);
inmem *= bcd.decode(&result->div);
} else {
inmem /= result->mult;
inmem *= result->div;
}
inmem -= result->bias;
if (value == inmem) {
results.emplace(*result, type);
}
}
}
}
vector<TypedSearchResult> results2;
for (const auto& result : results) {
results2.emplace_back(result.first, result.second);
}
intersectCurrent(move(results2));
}
void Search::intersectCurrent(vector<TypedSearchResult>&& results) {
if (m_hasStarted) {
vector<TypedSearchResult> out;
auto oldResults = m_current.begin();
for (const auto& result : results) {
if (oldResults == m_current.end()) {
break;
}
// Invariant: always stored ordered by address
while (oldResults != m_current.end() && static_cast<const SearchResult&>(*oldResults) < result) {
++oldResults;
}
auto thisAddress = oldResults;
while (thisAddress != m_current.end() && thisAddress->address == result.address) {
if (*thisAddress == result) {
out.emplace_back(result);
break;
}
++thisAddress;
}
}
m_current = move(out);
} else {
m_current = move(results);
}
m_hasStarted = true;
}
// From CityHash
#if __SIZEOF_SIZE_T__ == 8
template<class T>
inline void hash_combine(std::size_t& seed, const T& v) {
std::hash<T> hasher;
const std::size_t kMul = 0x9ddfea08eb382d69ULL;
std::size_t a = (hasher(v) ^ seed) * kMul;
a ^= (a >> 47);
std::size_t b = (seed ^ a) * kMul;
b ^= (b >> 47);
seed = b * kMul;
}
#elif __SIZEOF_SIZE_T__ == 4
inline void hash_combine(std::size_t& seed, const T& v) {
std::hash<T> hasher;
const std::size_t kMul = 0x9e3779b9;
std::size_t a = (hasher(v) ^ seed) * kMul;
a ^= (a >> 47);
std::size_t b = (seed ^ a) * kMul;
b ^= (b >> 47);
seed = b * kMul;
}
#else
#error Unsupported __SIZEOF_SIZE_T__
#endif
size_t hash<SearchResult>::operator()(const SearchResult& result) const {
size_t seed = 0;
hash_combine(seed, hash<size_t>()(result.address));
hash_combine(seed, hash<uint64_t>()(result.mult));
hash_combine(seed, hash<uint64_t>()(result.div));
hash_combine(seed, hash<int64_t>()(result.bias));
return seed;
}
size_t hash<TypedSearchResult>::operator()(const TypedSearchResult& result) const {
size_t seed = 0;
hash_combine(seed, hash<SearchResult>()(result));
hash_combine(seed, hash<DataType>()(result.type));
return seed;
}