-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
worker.js
313 lines (252 loc) · 8.5 KB
/
worker.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
var Module = {};
importScripts('vtable.js');
Module().then(function(m) {
Module = m;
});
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 });
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;
}
self.onmessage = function(event) {
var programInfo = Module.process(event.data);
if (programInfo.error.length > 0) {
// Don't bother doing any more work.
self.postMessage({ error: programInfo.error });
return;
}
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 symbolKey = key(symbol.address);
if (addressToSymbolMap[symbolKey] === undefined) {
addressToSymbolMap[symbolKey] = []
}
addressToSymbolMap[symbolKey].push(symbol);
}
console.info("virtual classes: " + listOfVirtualClasses.length);
console.info("relocations: " + programInfo.relocations.size());
var relocationMap = {};
for (var i = 0; i < programInfo.relocations.size(); ++i) {
var relocation = programInfo.relocations.get(i);
relocationMap[key(relocation.address)] = relocation.target;
}
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,
};
var classIndex = out.classes.length;
out.classes.push(classInfo);
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;
}
if (programInfo.addressSize === Uint32Array.BYTES_PER_ELEMENT) {
var localAddress = {
high: 0,
low: symbol.address.low + (functionIndex * Uint32Array.BYTES_PER_ELEMENT),
unsigned: true,
};
var targetAddress = relocationMap[key(localAddress)];
if (targetAddress) {
functionAddress = targetAddress;
}
} else {
console.warn('Relocations not supported for 64-bit bins');
}
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;
}
functionInfo.classes.push(classIndex);
currentVtable.push(functionInfo);
sendProgressUpdate(false);
}
sendProgressUpdate(false);
}
sendProgressUpdate(true);
self.postMessage(out);
};