-
Notifications
You must be signed in to change notification settings - Fork 1
/
SymbolAliasMap.cpp
438 lines (399 loc) · 14.3 KB
/
SymbolAliasMap.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
/*
* SymbolAliasMap.cpp
*
* Created on: ۱۸ مهر ۱۳۹۶
*
* Copyright Hedayat Vatankhah <[email protected]>, 2017-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 "SymbolAliasMap.h"
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include "powerfake.h"
#include "ParseUtils.h"
using namespace PowerFake;
using namespace std;
SymbolAliasMap::SymbolAliasMap(Functions &functions, bool approximate_matching,
bool verbose, bool verify_mode) :
functions(functions), approximate_matching(approximate_matching),
verbose(verbose), verify_mode(verify_mode)
{
CreateFunctionMap(functions);
}
void SymbolAliasMap::Load(std::string_view filename)
{
ifstream in(filename.data());
internal::FunctionInfo fi;
if (in && verbose)
cout << "Looking for symbol names in symbol cache"
<< "\n-----------------------------------------------\n";
while (in >> fi.symbol)
{
in >> fi.prototype.name >> fi.prototype.qual;
in.ignore(10, ' ');
std::getline(in, fi.prototype.params);
std::getline(in, fi.prototype.return_type);
auto name = GetSimpleName(fi.prototype);
auto range = unresolved_functions.equal_range(string(name));
for (auto p = range.first; p != range.second; )
{
const auto &proto = p->second->prototype;
if (fi.prototype.name == proto.name
&& fi.prototype.params == proto.params
&& fi.prototype.qual == proto.qual
&& fi.prototype.return_type == proto.return_type)
{
if (verbose)
cout << "Found symbol for " << proto.Str() << " == "
<< fi.symbol << '\n';
p->second->symbol = fi.symbol;
p = verify_mode ? std::next(p) : unresolved_functions.erase(p);
}
else
++p;
}
}
}
void SymbolAliasMap::Save(std::string_view filename)
{
ofstream of(filename.data());
for (auto [name, it]: functions_map)
{
const internal::FunctionInfo &fi = *it;
of << fi.symbol << ' ' << fi.prototype.name << ' ' << fi.prototype.qual
<< ' ' << fi.prototype.params << '\n';
of << fi.prototype.return_type << '\n';
}
}
/**
* For each symbol in the main library, finds its alias if it is wrapped and
* inserts the alias and the actual symbol of the target function in sym_map.
*
* @param symbol_name the name of a symbol in main library, which might be faked
*/
void SymbolAliasMap::AddSymbol(const char *symbol_name)
{
std::string demangled = boost::core::demangle(symbol_name);
FindWrappedSymbol(demangled, symbol_name);
}
/**
* @return if all wrapped symbols were found
*/
bool SymbolAliasMap::FoundAllWrappedSymbols() const
{
if (!verify_mode)
return unresolved_functions.empty();
bool found_all = true;
for (const auto &wfp: functions)
{
if (wfp.symbol.empty())
{
found_all = false;
const auto &wf = wfp.prototype;
cerr << "Error: Cannot find symbol for function: "
<< wf.return_type << ' ' << wf.name << wf.params
<< " (alias: " << wf.alias << ")" << endl;
}
}
return found_all;
}
void SymbolAliasMap::PrintUnresolvedSymbols()
{
if (verify_mode)
{
for (const auto &wfp: functions)
{
if (wfp.symbol.empty())
{
const auto &wf = wfp.prototype;
cerr << "Error: Cannot find symbol for function: "
<< wf.Str() << " (alias: " << wf.alias << ")" << endl;
}
}
}
else
{
for (auto [sn, it]: unresolved_functions)
{
const auto &wf = it->prototype;
cerr << "Error: Cannot find symbol for function: "
<< wf.Str() << " (alias: " << wf.alias << ")" << endl;
}
}
}
void SymbolAliasMap::ApplyApproximateMatching()
{
if (!approximate_matching)
return;
/*
* In verify mode, unresolved_functions includes all symbols till now, lets
* remove resolved symbols from it
*/
if (verify_mode)
for (auto it = unresolved_functions.begin();
it != unresolved_functions.end();
it = it->second->symbol.empty() ? std::next(it) :
unresolved_functions.erase(it))
{
}
if (unresolved_functions.empty())
return;
if (verbose)
cout << "\nTry doing inexact function prototype matching "
<< "\n----------------------------------------------------" << endl;
size_t prev_unresolved = unresolved_functions.size() + 1;
while (!unresolved_functions.empty()
&& prev_unresolved > unresolved_functions.size())
{
prev_unresolved = unresolved_functions.size();
bool erase = false;
for (auto it = unresolved_functions.begin();
it != unresolved_functions.end();
it = erase ? unresolved_functions.erase(it) : std::next(it))
{
auto &finfo = *(it->second);
erase = false;
auto &v = candidates[finfo.prototype.name];
if (v.empty())
continue; // unresolvable symbol found, skip for now
vector<int> max_score;
const ExtendedPrototype* best_candidate = nullptr;
for (const auto &c: v)
{
if (!IsApproximate(finfo.prototype, c))
continue;
auto score = GetNumMatchingTypes(finfo.prototype, c);
if (score == max_score)
best_candidate = nullptr;
else if (score > max_score)
{
max_score = score;
best_candidate = &c;
}
}
if (best_candidate)
{
finfo.symbol = best_candidate->symbol;
erase = true;
if (verbose)
cout << "Found symbol for " << finfo.prototype.Str()
<< " == " << best_candidate->symbol
<< " (" << best_candidate->Str() << ") \n";
v.erase(v.begin() + (best_candidate - &v[0]));
}
}
}
// Log unmatched functions
bool type_resolution_error = false;
for (auto it = unresolved_functions.begin();
it != unresolved_functions.end(); ++it)
{
vector<int> max_score;
auto &finfo = *(it->second);
auto &v = candidates[finfo.prototype.name];
vector<const ExtendedPrototype*> best_candidate;
for (const auto &c: v)
{
if (!IsApproximate(finfo.prototype, c))
continue;
auto score = GetNumMatchingTypes(finfo.prototype, c);
if (score == max_score)
best_candidate.push_back(&c);
else if (score > max_score)
{
max_score = score;
best_candidate.clear();
best_candidate.push_back(&c);
}
}
if (!best_candidate.empty())
{
type_resolution_error = true;
cout << "\nCannot find matching prototype for: "
<< finfo.prototype.Str() << ", candidates: " << endl;
for (auto c: best_candidate)
cout << "\t" << c->Str() << endl;
}
}
if (type_resolution_error)
cout << "\nConsider using TYPE_HINT() macro to guide PowerFake in"
" overload resolution in standalone mode.\n" << endl;
}
void SymbolAliasMap::CreateFunctionMap(Functions &functions)
{
for (auto it = functions.begin(); it != functions.end(); ++it)
functions_map.insert({ GetSimpleName(it->prototype), it });
unresolved_functions = functions_map;
}
std::string_view SymbolAliasMap::GetSimpleName(
const PowerFake::internal::FunctionPrototype &prototype)
{
std::string_view name = prototype.name;
auto nstart = name.rfind(':', prototype.name.length() - 1);
if (nstart != std::string::npos)
return name.substr(nstart + 1);
return name;
}
/**
* For a given symbol and its demangled name, finds corresponding prototype
* from @a protos set and stores the mapping
* @param protos all wrapped function prototypes
* @param demangled the demangled form of @a symbol_name
* @param symbol_name a symbol in the object file
*/
void SymbolAliasMap::FindWrappedSymbol(const std::string &demangled,
const char *symbol_name)
{
if (!IsFunction(symbol_name, demangled))
return;
size_t name_start, name_end;
auto name = FunctionName(demangled, name_start, name_end);
auto range = unresolved_functions.equal_range(name);
for (auto p = range.first; p != range.second; ++p)
{
auto func = p->second->prototype;
if (IsSameFunction(demangled, func))
{
const string sig = func.name + func.params;
if (verify_mode && !p->second->symbol.empty()
&& p->second->symbol != symbol_name)
throw runtime_error(
"Error: (BUG) duplicate symbols found for: "
+ func.return_type + ' ' + sig + ":\n"
+ '\t' + p->second->symbol + '\n'
+ '\t' + symbol_name);
p->second->symbol = symbol_name;
if (verbose)
cout << "Found symbol for " << func.return_type << ' ' << sig
<< " == " << symbol_name << " (" << demangled << ") \n";
if (approximate_matching)
{
auto &v = candidates[func.name];
auto it = std::remove_if(v.begin(), v.end(),
[symbol_name](const auto &p)
{
return p.symbol == symbol_name;
});
v.erase(it, v.end());
}
if (!verify_mode)
{
unresolved_functions.erase(p);
break;
}
}
else if (approximate_matching)
{
auto proto = ParseDemangledFunction(demangled, name_start, name_end);
if (IsApproximate(func, proto))
{
auto &v = candidates[proto.name];
auto it = std::find_if(v.begin(), v.end(),
[symbol_name](const auto &p)
{
return p.symbol == symbol_name;
});
if (it == v.end())
{
proto.symbol = symbol_name;
v.push_back(std::move(proto));
if (verbose)
cout << "Mark " << demangled
<< " as candidate for matching: " << func.Str()
<< endl;
}
}
}
}
}
bool SymbolAliasMap::IsFunction(const char *symbol_name [[maybe_unused]],
const std::string &demangled)
{
// detect static variables inside functions, which are demangled in
// this format: function_name()[ cv-qualification]::static_var_name
auto params_end = demangled.rfind(')');
if (params_end != string::npos)
{
auto static_var_separator = demangled.find("::", params_end);
if (static_var_separator != string::npos
&& demangled.find('(', static_var_separator) == string::npos)
return false;
}
return true;
}
bool SymbolAliasMap::IsSameFunction(const std::string &demangled,
const PowerFake::internal::FunctionPrototype &proto)
{
const string base_sig = proto.name + proto.params;
string qs = internal::ToStr(proto.qual, true);
if (!qs.empty())
qs = ' ' + qs;
if (demangled == proto.name) // C functions
return true;
if (demangled == base_sig + qs) // Normal C++ functions
return true;
// template functions also have return type
if (demangled == proto.return_type + ' ' + base_sig + qs)
return true;
// signatures with an abi tag, e.g. func[abi:cxx11](int)
bool prefix_found = (demangled.find(proto.name + "[") == 0
|| demangled.find(proto.return_type + ' ' + proto.name + "[") == 0);
if (prefix_found)
{
auto postfix_pos = demangled.find("]" + proto.params + qs);
if (postfix_pos != string::npos)
return demangled.size() - postfix_pos
== (proto.params + qs).size() + 1;
}
// TODO: Warn for similar symbols, e.g. <signature> [clone .cold]
return false;
}
bool SymbolAliasMap::IsApproximate(
const PowerFake::internal::FunctionPrototype &proto,
const ExtendedPrototype &ex_proto)
{
return ex_proto.name == proto.name
&& ex_proto.qual == (proto.qual & ~internal::Qualifiers::NOEXCEPT)
&& ex_proto.expanded_params.size() == SplitParams(proto.params).size();
}
std::vector<int> SymbolAliasMap::GetNumMatchingTypes(
const PowerFake::internal::FunctionPrototype &proto,
const ExtendedPrototype &ex_proto)
{
int score = 0;
int tpl_name_score = 0;
if (!proto.return_type.empty() && proto.return_type == ex_proto.return_type)
++score;
auto ref_params = SplitParams(proto.params);
for (unsigned i = 0; i < ref_params.size(); ++i)
{
if (ref_params[i] == ex_proto.expanded_params[i])
++score;
else
{
auto ref_type = ref_params[i];
auto ex_type = ex_proto.expanded_params[i];
auto tpos = ref_type.find_last_of(")>");
// skip function types and detect templates
if (tpos != string_view::npos && ref_type[tpos] == '>')
{
auto ex_tpos = ex_type.find_last_of(")>");
if (ex_tpos == string_view::npos || ex_type[ex_tpos] != '>')
continue;
auto tpl_type = ref_type.substr(0, ref_type.find('<'));
auto ex_tpl_type = ex_type.substr(0, ex_type.find('<'));
if (tpl_type == ex_tpl_type)
++tpl_name_score;
}
}
}
if (tpl_name_score)
return { score, tpl_name_score };
else
return { score };
}