-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
test.js
413 lines (329 loc) · 11.7 KB
/
test.js
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
var fs = require('fs');
var Module = require('./vtable.js');
function toArrayBuffer(buffer) {
var arrayBuffer = new ArrayBuffer(buffer.length);
var view = new Uint8Array(arrayBuffer);
for (var i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
return arrayBuffer;
}
function demangleSymbol(func) {
try {
if (typeof func !== 'string') {
throw new Error('input not a string');
}
var len = Module['lengthBytesUTF8'](func);
var buf = Module['_malloc'](len + 1);
Module['stringToUTF8'](func, buf, len + 1);
var status = Module['_malloc'](4);
var ret = Module['___cxa_demangle'](buf, 0, 0, status);
var intStatus = Module['getValue'](status, 'i32');
switch (intStatus) {
case 0:
if (!ret) {
throw new Error('___cxa_demangle returned NULL');
}
return Module['UTF8ToString'](ret);
case -1:
throw new Error('a memory allocation failiure occurred');
case -2:
throw new Error('input is not a valid name under the C++ ABI mangling rules');
case -3:
throw new Error('one of the arguments is invalid');
default:
throw new Error('encountered an unknown error');
}
} catch(e) {
console.error('Failed to demangle \'' + func + '\' (' + e.message + ')');
return func;
} finally {
if (buf) Module['_free'](buf);
if (status) Module['_free'](status);
if (ret) Module['_free'](ret);
}
}
function getDataForSymbol(programInfo, symbol) {
var dataStart;
var dataChunks;
if (symbol.section === 0) {
return null;
} else if (symbol.section === programInfo.rodataIndex) {
dataStart = programInfo.rodataStart;
dataChunks = programInfo.rodataChunks;
} else if (symbol.section === programInfo.relRodataIndex) {
dataStart = programInfo.relRodataStart;
dataChunks = programInfo.relRodataChunks;
} else {
return null;
}
if (dataStart.high !== 0 || symbol.address.high !== 0 || symbol.size.high !== 0) {
throw '>= 32-bit rodata is not supported';
}
for (var i = 0; i < dataChunks.size(); ++i) {
var chunk = dataChunks.get(i);
if (chunk.offset.high !== 0) {
throw '>= 32-bit rodata is not supported';
}
var start = symbol.address.low - (dataStart.low + chunk.offset.low);
var end = start + symbol.size.low;
if (start < 0 || end > chunk.data.length) {
continue;
}
return chunk.data.subarray(start, end);
}
return null;
}
var loaded = 0;
var total = 0;
var lastProgressUpdate = 0;
function sendProgressUpdate(force) {
var now = Date.now();
if (!force && (now - lastProgressUpdate) < 50) {
return;
}
//self.postMessage({ loaded: loaded, total: total });
//console.info(loaded, total);
lastProgressUpdate = now;
}
function hex32(n) {
return (n === 0) ? '00000000' : ('0000000' + ((n|0)+4294967296).toString(16)).substr(-8);
}
function hex64(n) {
return hex32(n.high) + hex32(n.low);
}
function key(n) {
return hex64(n);
}
function isZero(n) {
return n.low === 0 && n.high === 0;
}
//////////////////////////
function shouldSkipWindowsFunction(classInfo, vtableIndex, functionIndex, functionInfo) {
return (
functionInfo.name.indexOf('::~') !== -1
) ? (
functionIndex > 0 &&
functionInfo.name === classInfo.vtables[vtableIndex].functions[functionIndex - 1].name
) : (
classInfo.vtables.find((d, n) => (
n > vtableIndex &&
d.functions.find((d) => (
d.isThunk &&
functionInfo.name === d.name
))
))
);
}
//////////////////////////
const onFileRead = function(err, data) {
if (err) {
throw err;
}
var programInfo = Module['process'](toArrayBuffer(data));
if (programInfo.error.length > 0) {
throw programInfo.error;
}
console.info("address size: " + programInfo.addressSize);
console.info("symbols: " + programInfo.symbols.size());
var listOfVirtualClasses = [];
var addressToSymbolMap = {};
for (var i = 0; i < programInfo.symbols.size(); ++i) {
var symbol = programInfo.symbols.get(i);
if (isZero(symbol.address) || isZero(symbol.size) || symbol.name.length === 0) {
continue;
}
if (symbol.name.substr(0, 4) === '_ZTV') {
listOfVirtualClasses.push(symbol);
}
var addressKey = key(symbol.address);
if (addressToSymbolMap[addressKey] === undefined) {
addressToSymbolMap[addressKey] = [];
}
addressToSymbolMap[addressKey].push(symbol);
}
console.info("virtual classes: " + listOfVirtualClasses.length);
var pureVirtualFunction = {
id: null,
name: '(pure virtual function)',
symbol: null,
searchKey: null,
isThunk: false,
classes: [],
};
var out = {
classes: [],
functions: [],
};
var addressToFunctionMap = {};
total += listOfVirtualClasses.length;
for (var classIndex = 0; classIndex < listOfVirtualClasses.length; ++classIndex) {
loaded += 1;
var symbol = listOfVirtualClasses[classIndex];
var name = demangleSymbol(symbol.name).substr(11);
var data = getDataForSymbol(programInfo, symbol);
if (!data) {
if (symbol.section > 0) {
console.warn('VTable for ' + name + ' is outside data');
}
sendProgressUpdate(false);
continue;
}
var classInfo = {
id: key(symbol.address),
name: name,
searchKey: name.toLowerCase(),
vtables: [],
hasMissingFunctions: false,
hasMultiFunctions: false,
};
var currentVtable;
var dataView = new Uint32Array(data.buffer, data.byteOffset, data.byteLength / Uint32Array.BYTES_PER_ELEMENT);
total += dataView.length;
for (var functionIndex = 0; functionIndex < dataView.length; ++functionIndex) {
loaded += 1;
var functionAddress = {
high: 0,
low: dataView[functionIndex],
unsigned: true,
};
if (programInfo.addressSize > Uint32Array.BYTES_PER_ELEMENT) {
functionAddress.high = dataView[++functionIndex];
loaded += 1;
}
var functionSymbols = addressToSymbolMap[key(functionAddress)];
var functionSymbol = functionSymbols && functionSymbols[functionSymbols.length - 1];
// This could be the end of the vtable, or it could just be a pure/deleted func.
if (!functionSymbol && (classInfo.vtables.length === 0 || !isZero(functionAddress))) {
currentVtable = [];
classInfo.vtables.push({
offset: ~(functionAddress.low - 1),
functions: currentVtable,
});
// Skip the RTTI pointer and thisptr adjuster,
// We'll need to do more work here for virtual bases.
var skip = programInfo.addressSize / Uint32Array.BYTES_PER_ELEMENT;
functionIndex += skip;
loaded += skip;
sendProgressUpdate(false);
continue;
}
var functionSymbol = functionSymbol && functionSymbol.name;
if (!functionSymbol || functionSymbol === '__cxa_deleted_virtual' || functionSymbol === '__cxa_pure_virtual') {
classInfo.hasMissingFunctions = true;
currentVtable.push(pureVirtualFunction);
sendProgressUpdate(false);
continue;
}
var functionInfo = addressToFunctionMap[key(functionAddress)];
if (!functionInfo) {
var functionSignature = demangleSymbol(functionSymbol);
var functionName = functionSignature;
var startOfArgs = functionName.lastIndexOf('(');
if (startOfArgs !== -1) {
functionName = functionName.substr(0, startOfArgs);
}
var functionShortName = functionName;
var startOfName = functionShortName.lastIndexOf('::');
if (startOfName !== -1) {
functionShortName = functionShortName.substr(startOfName + 2);
}
functionInfo = {
id: key(functionAddress),
name: functionSignature,
symbol: functionSymbol,
searchKey: functionName.toLowerCase(),
shortName: functionShortName,
isThunk: false,
isMulti: functionSymbols.length > 1,
classes: [],
};
if (functionSymbol.substr(0, 4) !== '_ZTh') {
out.functions.push(functionInfo);
} else {
functionInfo.isThunk = true;
functionInfo.name = functionSignature.substr(21);
}
addressToFunctionMap[key(functionAddress)] = functionInfo;
}
classInfo.hasMultiFunctions = classInfo.hasMultiFunctions || functionInfo.isMulti;
functionInfo.classes.push(classInfo);
currentVtable.push(functionInfo);
sendProgressUpdate(false);
}
out.classes.push(classInfo);
sendProgressUpdate(false);
}
sendProgressUpdate(true);
//self.postMessage(out);
//return;
for (var classIndex = 0; classIndex < out.classes.length; ++classIndex) {
var classInfo = out.classes[classIndex];
console.log('');
console.log('Class ' + classInfo.name);
if (classInfo.hasMissingFunctions) {
console.log(' Missing (pure or deleted) virtual functions');
}
if (classInfo.vtables.length > 1) {
console.log(' Uses multiple inheritance');
}
if (classInfo.hasMissingFunctions || classInfo.vtables.length > 1) {
console.log(' Windows offsets may be incorrect');
}
if (classInfo.hasMultiFunctions) {
console.log(' Some identical functions have been de-duplicated by the compiler');
}
for (var vtableIndex = 0; vtableIndex < classInfo.vtables.length; ++vtableIndex) {
var vtableInfo = classInfo.vtables[vtableIndex];
console.log(' Table ' + vtableIndex + ' (thisoffs = ' + vtableInfo.offset + ')');
console.log(' Lin Win Function');
var windowsIndex = 0;
for (var linuxIndex = 0; linuxIndex < vtableInfo.functions.length; ++linuxIndex) {
var functionInfo = vtableInfo.functions[linuxIndex];
var displayWindowsIndex = windowsIndex;
if (shouldSkipWindowsFunction(classInfo, vtableIndex, linuxIndex, functionInfo)) {
displayWindowsIndex = ' ';
} else {
if (functionInfo.symbol && !functionInfo.isMulti) {
var previousOverloads = 0;
var remainingOverloads = 0;
while ((linuxIndex - (1 + previousOverloads)) >= 0) {
var previousFunctionIndex = linuxIndex - (1 + previousOverloads);
var previousFunctionInfo = vtableInfo.functions[previousFunctionIndex];
if (!functionInfo.symbol || shouldSkipWindowsFunction(classInfo, vtableIndex, previousFunctionIndex, previousFunctionInfo)) {
break;
}
if (functionInfo.shortName !== previousFunctionInfo.shortName) {
break;
}
previousOverloads++;
}
while ((linuxIndex + 1 + remainingOverloads) < vtableInfo.functions.length) {
var nextFunctionIndex = linuxIndex + 1 + remainingOverloads;
var nextFunctionInfo = vtableInfo.functions[nextFunctionIndex];
if (!functionInfo.symbol || shouldSkipWindowsFunction(classInfo, vtableIndex, nextFunctionIndex, nextFunctionInfo)) {
break;
}
if (functionInfo.shortName !== nextFunctionInfo.shortName) {
break;
}
remainingOverloads++;
}
displayWindowsIndex -= previousOverloads;
displayWindowsIndex += remainingOverloads;
}
windowsIndex++;
}
console.log(' ' + (' ' + linuxIndex).substr(-3) + ' ' + (' ' + displayWindowsIndex).substr(-3) + ' ' + functionInfo.name + (functionInfo.isMulti ? ' [MULTI]' : ''));
}
}
}
};
if (process.argv.length <= 2) {
throw 'Usage: ' + process.argv[0] + ' ' + process.argv[1] + ' <file>';
}
var inputFile = process.argv[2];
Module().then(function(m) {
Module = m;
fs.readFile(inputFile, onFileRead);
});