-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParseUtils.cpp
557 lines (509 loc) · 17.6 KB
/
ParseUtils.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
* ParseUtils.cpp
*
* Created on: ۱۵ ژانویهٔ ۲۰۲۲
*
* Copyright Hedayat Vatankhah <[email protected]>, 2022.
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
#include "ParseUtils.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <set>
using namespace std;
using namespace PowerFake;
using namespace PowerFake::internal;
ExtendedPrototype::ExtendedPrototype(std::string ret, std::string name,
std::string params, uint32_t qual) :
FunctionPrototype(ret, name, params, qual), expanded_params(
SplitParams(this->params))
{
}
Functions ReadFunctionsList(vector<string> wrapper_files, bool verbose)
{
const string_view::size_type BUF_SIZE = 1 * 1024 * 1024;
const string_view::size_type MIN_ACCEPTABLE_INCOMPLETE_POS = BUF_SIZE - 10 * 1024;
const string_view PREFIX { PFK_TAG_PREFIX };
const string_view PT_START_MARKER { PFK_PROTO_START };
const string_view PT_END_MARKER { PFK_PROTO_END };
const string_view TH_START_MARKER { PFK_TYPEHINT_START };
const string_view TH_END_MARKER { PFK_TYPEHINT_END };
Functions functions;
set<string> found_prototypes;
set<string> found_typehints;
char buff_mem[BUF_SIZE];
for (const auto &wf_name: wrapper_files)
{
if (verbose)
cout << "\nLooking for marked functions in " << wf_name
<< "\n------------------------------------------------------------------------------------------------------"
<< endl;
ifstream wfile(wf_name, ios::binary);
size_t read_start = 0;
do
{
wfile.read(buff_mem + read_start, BUF_SIZE - read_start);
if (wfile.gcount() <= 0) continue;
string_view strbuf(buff_mem, read_start + wfile.gcount());
auto [p_start, found] = FindStrings(PREFIX, { PT_START_MARKER,
TH_START_MARKER }, strbuf);
while (p_start != string_view::npos)
{
string_view start_marker = found ? TH_START_MARKER : PT_START_MARKER;
string_view end_marker = found ? TH_END_MARKER : PT_END_MARKER;
auto [p_end, marker_found] = FindStrings(PREFIX, { end_marker,
PT_START_MARKER, TH_START_MARKER }, strbuf,
p_start + start_marker.size());
while (p_end != string_view::npos && marker_found)
{
p_start = p_end;
found = marker_found - 1;
start_marker = found ? TH_START_MARKER : PT_START_MARKER;
end_marker = found ? TH_END_MARKER : PT_END_MARKER;
tie(p_end, marker_found) = FindStrings(PREFIX, { end_marker,
PT_START_MARKER, TH_START_MARKER }, strbuf,
p_start + start_marker.size());
}
if (p_end != string_view::npos)
{
auto infostr = strbuf.substr(p_start,
p_end - p_start + end_marker.size());
/*
* There can be duplicate entries in the binary, so we
* use an std::set to eliminate duplicates
*/
if (found)
found_typehints.insert(string(infostr));
else
found_prototypes.insert(string(infostr));
}
else
break;
std::tie(p_start, found) = FindStrings(PREFIX, { PT_START_MARKER,
TH_START_MARKER }, strbuf, p_end + end_marker.size());
}
if (wfile)
{
string_view::size_type keep_start;
if (p_start == string_view::npos || p_start < MIN_ACCEPTABLE_INCOMPLETE_POS)
keep_start = strbuf.size() - PT_START_MARKER.size();
else
keep_start = p_start;
auto handover_data = strbuf.substr(keep_start);
memcpy(buff_mem, handover_data.data(), handover_data.size());
read_start = handover_data.size();
}
} while (wfile);
}
map<string, string> type_hints;
for (const auto &infostr: found_typehints)
{
if (verbose)
cout << "Found Type Hint: " << infostr << endl;
if (auto tinfo = GetTypeHint(infostr))
{
auto [it, inserted] = type_hints.insert(tinfo.value());
if (!inserted)
{
cerr << "Duplicate type hint for type: " << tinfo.value().first
<< " => " << tinfo.value().second;
}
}
else
cout << "Skipped: " << infostr << endl;
}
for (const auto &infostr: found_prototypes)
{
if (verbose)
cout << "Found Prototype: " << infostr << endl;
if (auto finfo = GetFunctionInfo(infostr, type_hints))
functions.push_back(finfo.value());
else
cout << "Skipped: " << infostr << endl;
}
return functions;
}
std::optional<PowerFake::internal::FunctionInfo> GetFunctionInfo(
string_view function_str, const TypeHintMap &type_hints)
{
/*
* Found Prototype: PFKPrototypeStart: WRAPPED | SampleClass::CallThis | Folan__pfkalias__43 | void (FakeTest::SampleClass::*)() const | PFKPrototypeEnd
* Found Prototype: PFKPrototypeStart: WRAPPED | SampleClass::StaticFunc | Folan__pfkalias__40 | void (*)() | FakeTest::SampleClass | PFKPrototypeEnd
* Found Prototype: PFKPrototypeStart: HIDDEN | called_by_normal_func | Folan__pfkalias__37 | void (*)(int) | PFKPrototypeEnd
*/
FunctionInfo finfo;
istringstream is { string(function_str) };
string s;
is >> s >> s;
if (s == "WRAPPED")
finfo.fake_type = FakeType::WRAPPED;
else if (s == "HIDDEN")
finfo.fake_type = FakeType::HIDDEN;
else
return {};
is >> s >> s;
finfo.prototype.name = s;
auto nstart = finfo.prototype.name.rfind("::");
if (nstart == string::npos)
nstart = 0;
is >> s >> finfo.prototype.alias >> s;
std::getline(is, s, '|');
if (!is) return {};
string_view sv = s;
auto ret_end = sv.find('(');
auto param_end = sv.rfind(')');
auto param_start = sv.find('(', ret_end + 1);
finfo.prototype.return_type = NormalizeType(sv.substr(1, ret_end - 2),
type_hints);
finfo.prototype.params = NormalizeParameters(
sv.substr(param_start, param_end - param_start + 1), type_hints);
if (sv.substr(ret_end, 3) != "(*)") // a class member
finfo.prototype.name = string(
sv.substr(ret_end + 1, sv.rfind("::", param_start) - ret_end - 1))
+ finfo.prototype.name.substr(nstart);
istringstream qualis { s.substr(param_end + 1) };
while (qualis >> s)
{
if (s == "const")
finfo.prototype.qual |= Qualifiers::CONST;
else if (s == "volatile")
finfo.prototype.qual |= Qualifiers::VOLATILE;
else if (s == "noexcept")
finfo.prototype.qual |= Qualifiers::NOEXCEPT;
else if (s == "&")
finfo.prototype.qual |= Qualifiers::LV_REF;
else if (s == "&&")
finfo.prototype.qual |= Qualifiers::RV_REF;
else if (s == "const&")
finfo.prototype.qual |= Qualifiers::CONST_REF; // if exists ;)
else
cerr << "Unknown qualifier: " << s << endl;
}
is >> s;
if (s != PFK_PROTO_END)
finfo.prototype.name = s + finfo.prototype.name.substr(nstart);
return finfo;
}
std::optional<std::pair<std::string, std::string>> GetTypeHint(
std::string_view typehint_str)
{
/*
* PFKTypeHintStart: std::string | std::basic_string<char, allocator<char> > | PFKTypeHintEnd
*/
string ct_type, demangled_type, s;
istringstream is { string(typehint_str) };
is >> s;
is.ignore(10, ' ');
std::getline(is, ct_type, '|');
if (!is) return {};
auto ltrim = ct_type.find_last_not_of(' ');
ct_type.resize(ltrim + 1);
is.ignore(10, ' ');
std::getline(is, demangled_type, '|');
if (!is) return {};
ltrim = demangled_type.find_last_not_of(' ');
demangled_type.resize(ltrim + 1);
return pair{ ct_type, demangled_type };
}
ExtendedPrototype ParseDemangledFunction(std::string_view demangled,
size_t name_start, size_t name_end)
{
std::string ret, name, params;
uint32_t qual = Qualifiers::NO_QUAL;
if (name_start > 0)
{
unsigned tpl = 0;
for (--name_start;
name_start > 0 && (tpl || demangled[name_start] != ' ');
--name_start)
{
if (demangled[name_start] == '>')
tpl++;
else if (demangled[name_start] == '<')
tpl--;
}
ret = demangled.substr(0, name_start);
}
if (name_start)
++name_start;
name = demangled.substr(name_start, name_end - name_start);
auto pstart = demangled.find('(', name_end);
auto pend = demangled.rfind(')');
++pend;
params = demangled.substr(pstart, pend - pstart);
istringstream is { string(demangled.substr(pend)) };
string w;
while (is >> w)
qual |= QualifierFromStr(w);
return { ret, name, params, qual };
}
std::string_view FunctionName(std::string_view demangled, size_t &start_pos,
size_t &end_pos)
{
start_pos = 0;
end_pos = demangled.size();
auto name_end = demangled.rfind(")");
if (name_end == string_view::npos)
return demangled;
int num_pr = 1;
for (--name_end; name_end > 0; --name_end)
{
if (demangled[name_end] == ')')
++num_pr;
else if (demangled[name_end] == '(')
--num_pr;
if (num_pr == 0) break;
}
if (num_pr)
throw runtime_error(
"(BUG) Error parsing function name for: " + string(demangled));
// look for ABI tag, but not operator[]
if (demangled[name_end - 1] == ']' && demangled[name_end - 2] != '[')
name_end = demangled.rfind('[', name_end - 1);
end_pos = name_end;
if (name_end == string_view::npos)
throw runtime_error(
"(BUG) Error parsing function name for: " + string(demangled));
auto name_begin = demangled.find_last_of(" >:", name_end);
if (name_begin == string_view::npos)
return demangled.substr(0, name_end);
if (demangled[name_begin] == '>')
{
int tpl = 1;
for (name_begin--; name_begin > 0 && tpl; --name_begin)
{
if ((demangled[name_begin] == ' ' || demangled[name_begin] == ':')
&& demangled.substr(name_begin + 1, strlen("operator>"))
== "operator>")
{
tpl = 0;
break;
}
else if (demangled[name_begin] == '>')
tpl++;
else if (demangled[name_begin] == '<')
tpl--;
}
// "operator<< <>"
if (demangled[name_begin] == ' ' && demangled[name_begin + 1] == '<')
--name_begin;
name_begin = demangled.find_last_of(" :", name_begin);
}
if (name_begin > 0)
{
auto op_begin = demangled.find_last_of(" :", name_begin-1);
std::string op;
if (op_begin == string_view::npos)
{
op = demangled.substr(0, name_begin);
op_begin = 0;
}
else
{
++op_begin;
op = demangled.substr(op_begin, name_begin - op_begin);
}
if (op == "operator")
name_begin = op_begin;
else
++name_begin;
}
start_pos = name_begin;
return demangled.substr(name_begin, name_end - name_begin);
}
PowerFake::internal::Qualifiers QualifierFromStr(std::string_view qs)
{
if (qs == "&")
return Qualifiers::LV_REF;
else if (qs == "&&")
return Qualifiers::RV_REF;
else if (qs == "const&")
return Qualifiers::CONST_REF;
else if (qs == "const")
return Qualifiers::CONST;
else if (qs == "volatile")
return Qualifiers::VOLATILE;
else if (qs == "noexcept")
return Qualifiers::NOEXCEPT;
return Qualifiers::NO_QUAL;
}
std::string NormalizeType(std::string_view compile_type,
const TypeHintMap &type_hints)
{
std::string suffix;
auto param_end = compile_type.find_last_of(")>");
if (param_end != string_view::npos && compile_type[param_end] == ')') // function type
{
auto fparan_start = compile_type.find('(');
auto param_start = compile_type.find('(', fparan_start + 1);
suffix = ' ';
suffix += compile_type.substr(fparan_start, param_start - fparan_start);
suffix += NormalizeParameters(
compile_type.substr(param_start, param_end - param_start + 1),
type_hints);
suffix += compile_type.substr(param_end + 1);
if (compile_type[fparan_start - 1] == ' ')
--fparan_start;
compile_type = compile_type.substr(0, fparan_start);
}
std::string res;
unsigned start_pos = 0;
bool add_const = false;
if (compile_type.substr(0, strlen("const ")) == "const ")
{
add_const = true;
start_pos = strlen("const ");
}
auto translate = [&type_hints](string_view t) {
if (t.back() == ' ')
t = t.substr(0, t.size() - 1);
auto f = type_hints.find(string(t));
if (f != type_hints.end())
return f->second;
return string(t);
};
auto lookup = compile_type.find_first_of("*&<", start_pos);
if (lookup == string_view::npos)
{
res = translate(compile_type.substr(start_pos));
if (add_const) // actually we don't expect this to happen! const without */&?!
res += " const";
return res + suffix;
}
switch (compile_type[lookup])
{
case '&':
case '*':
if (add_const)
{
res = translate(
compile_type.substr(start_pos, lookup - start_pos));
res += " const" + FixSpaces(compile_type.substr(lookup));
}
else
res = translate(compile_type);
break;
case '<':
{
auto tplend = compile_type.rfind('>');
res = translate(compile_type.substr(start_pos, lookup++ - start_pos));
res += '<' + NormalizeParameters(
compile_type.substr(lookup, tplend - lookup), type_hints);
res += '>';
res = translate(res);
if (add_const)
res += " const";
res += FixSpaces(compile_type.substr(tplend + 1));
break;
}
}
return res + suffix;
}
std::string NormalizeParameters(std::string_view params,
const TypeHintMap &type_hints)
{
std::string res;
if (params[0] == '(')
res = '(';
bool first = true;
for (auto p: SplitParams(params))
{
if (!first)
res += ", ";
first = false;
res += NormalizeType(p, type_hints);
}
if (res[0] == '(')
res += ')';
return res;
}
std::vector<std::string_view> SplitParams(std::string_view params)
{
unsigned param_start = 0;
int nested = 0;
vector<string_view> res;
// Note: we assume valid input
if (params[0] == '(' && params.back() == ')')
params = params.substr(1, params.size() - 2);
unsigned i;
for (i = 0; i < params.size(); ++i)
{
switch (params[i])
{
case '(':
case '<':
++nested;
break;
case ')':
case '>':
--nested;
break;
case ' ':
if (param_start == i)
++param_start;
break;
case ',':
if (nested)
break;
if (i != param_start)
res.push_back(params.substr(param_start, i - param_start));
param_start = i + 1;
break;
}
}
if (param_start < i) // no ending parenthesis
res.push_back(params.substr(param_start));
return res;
}
std::string FixSpaces(std::string_view type_str)
{
std::string res;
bool word_mode = type_str[0] != '*' && type_str[0] != '&';
size_t pos = 0;
do
{
if (word_mode)
{
auto next = type_str.find_first_of("*&", pos);
if (next == string_view::npos)
break;
int skip = (type_str[next - 1] == ' ' ? 1 : 0);
res += type_str.substr(pos, next - skip - pos);
pos = next;
}
else
{
auto next = type_str.find_first_not_of("*& ", pos);
if (next == string_view::npos)
break;
res += type_str.substr(pos, next - pos);
if (type_str[next - 1] != ' ')
res += ' ';
pos = next;
}
word_mode = !word_mode;
}
while (true);
res += type_str.substr(pos);
return res;
}
std::pair<std::string_view::size_type, int> FindStrings(std::string_view prefix,
std::vector<std::string_view> srch, std::string_view str,
std::string_view::size_type start_pos)
{
auto pos = str.find(prefix, start_pos);
while (pos != string_view::npos)
{
for (unsigned i = 0; i < srch.size(); ++i)
{
if (str.substr(pos, srch[i].size()) == srch[i])
return { pos, i };
}
pos = str.find(prefix, pos + prefix.size());
}
return { pos, -1 };
}