forked from compiler-explorer/compiler-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demangler-tests.ts
443 lines (374 loc) · 17 KB
/
demangler-tests.ts
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
// Copyright (c) 2018, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import {describe, expect, it} from 'vitest';
import {unwrap} from '../lib/assert.js';
import {BaseCompiler} from '../lib/base-compiler.js';
import {CompilationEnvironment} from '../lib/compilation-env.js';
import {CppDemangler, Win32Demangler} from '../lib/demangler/index.js';
import {LLVMIRDemangler} from '../lib/demangler/llvm.js';
import {PrefixTree} from '../lib/demangler/prefix-tree.js';
import * as exec from '../lib/exec.js';
import * as properties from '../lib/properties.js';
import {SymbolStore} from '../lib/symbol-store.js';
import * as utils from '../lib/utils.js';
import {fs, makeFakeCompilerInfo, path, resolvePathFromTestRoot} from './utils.js';
const cppfiltpath = 'c++filt';
class DummyCompiler extends BaseCompiler {
constructor() {
const env = {
ceProps: properties.fakeProps({}),
getCompilerPropsForLanguage: () => {
return (prop, def) => def;
},
} as unknown as CompilationEnvironment;
// using c++ as the compiler needs at least one language
const compiler = makeFakeCompilerInfo({lang: 'c++'});
super(compiler, env);
}
override exec(command, args, options) {
return exec.execute(command, args, options);
}
}
class DummyCppDemangler extends CppDemangler {
public override collectLabels = super.collectLabels;
}
class DummyLlvmDemangler extends LLVMIRDemangler {
public override collectLabels = super.collectLabels;
}
class DummyWin32Demangler extends Win32Demangler {
public override collectLabels = super.collectLabels;
}
const catchCppfiltNonexistence = err => {
if (!err.message.startsWith('spawn c++filt')) {
throw err;
}
};
describe('Basic demangling', () => {
it('One line of asm', () => {
const result = {
asm: [{text: 'Hello, World!'}],
};
const demangler = new DummyCppDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
return Promise.all([
demangler.process(result).then(output => {
expect(output.asm[0].text).toEqual('Hello, World!');
}),
]);
});
it('One label and some asm', () => {
const result = {asm: [{text: '_Z6squarei:'}, {text: ' ret'}]};
const demangler = new DummyCppDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
return Promise.all([
demangler
.process(result)
.then(output => {
expect(output.asm[0].text).toEqual('square(int):');
expect(output.asm[1].text).toEqual(' ret');
})
.catch(catchCppfiltNonexistence),
]);
});
it('One label and use of a label', () => {
const result = {asm: [{text: '_Z6squarei:'}, {text: ' mov eax, $_Z6squarei'}]};
const demangler = new DummyCppDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
return Promise.all([
demangler
.process(result)
.then(output => {
expect(output.asm[0].text).toEqual('square(int):');
expect(output.asm[1].text).toEqual(' mov eax, $square(int)');
})
.catch(catchCppfiltNonexistence),
]);
});
it('Mov with OFFSET FLAT', () => {
// regression test for https://github.com/compiler-explorer/compiler-explorer/issues/6348
const result = {asm: [{text: 'mov eax, OFFSET FLAT:_ZN1a1gEi'}]};
const demangler = new DummyCppDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
return Promise.all([
demangler
.process(result)
.then(output => {
expect(output.asm[0].text).toEqual('mov eax, OFFSET FLAT:a::g(int)');
})
.catch(catchCppfiltNonexistence),
]);
});
it('rip-relative jump', () => {
// regression test for https://github.com/compiler-explorer/compiler-explorer/issues/6348
const result = {
asm: [
{
text: 'jmp qword ptr [rip + _ZN4core3fmt3num3imp54_$LT$impl$u20$core..fmt..Display$u20$for$u20$usize$GT$3fmt17h7bbbd896a38dcccaE@GOTPCREL]',
},
],
};
const demangler = new DummyCppDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
return Promise.all([
demangler
.process(result)
.then(output => {
if (process.platform === 'win32') {
expect(output.asm[0].text).toEqual(
'jmp qword ptr [rip + core::fmt::num::imp::<impl core::fmt::Display for usize>::fmt@GOTPCREL]',
);
} else {
expect(output.asm[0].text).toEqual(
'jmp qword ptr [rip + core::fmt::num::imp::<impl core::fmt::Display for usize>::fmt::h7bbbd896a38dccca@GOTPCREL]',
);
}
})
.catch(catchCppfiltNonexistence),
]);
});
it('Two destructors', () => {
const result = {
asm: [
{text: '_ZN6NormalD0Ev:'},
{text: ' callq _ZdlPv'},
{text: '_Z7caller1v:'},
{text: ' rep ret'},
{text: '_Z7caller2P6Normal:'},
{text: ' cmp rax, OFFSET FLAT:_ZN6NormalD0Ev'},
{text: ' jmp _ZdlPvm'},
{text: '_ZN6NormalD2Ev:'},
{text: ' rep ret'},
],
};
const demangler = new DummyCppDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
return demangler
.process(result)
.then(output => {
expect(output.asm[0].text).toEqual('Normal::~Normal() [deleting destructor]:');
expect(output.asm[1].text).toEqual(' callq operator delete(void*)');
expect(output.asm[6].text).toEqual(' jmp operator delete(void*, unsigned long)');
})
.catch(catchCppfiltNonexistence);
});
it('Should ignore comments (CL)', () => {
const result = {asm: [{text: ' call ??3@YAXPEAX_K@Z ; operator delete'}]};
const demangler = new DummyWin32Demangler(cppfiltpath, new DummyCompiler());
demangler.result = result;
demangler.symbolstore = new SymbolStore();
demangler.collectLabels();
const output = demangler.win32RawSymbols;
expect(unwrap(output)).toEqual(['??3@YAXPEAX_K@Z']);
});
it('Should ignore comments (CPP)', () => {
const result = {asm: [{text: ' call hello ; operator delete'}]};
const demangler = new DummyCppDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
demangler.result = result;
demangler.symbolstore = new SymbolStore();
demangler.collectLabels();
const output = demangler.othersymbols.listSymbols();
expect(output).toEqual(['hello']);
});
it('Should also support ARM branch instructions', () => {
const result = {asm: [{text: ' bl _ZN3FooC1Ev'}]};
const demangler = new DummyCppDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
demangler.result = result;
demangler.symbolstore = new SymbolStore();
demangler.collectLabels();
const output = demangler.othersymbols.listSymbols();
expect(output).toEqual(['_ZN3FooC1Ev']);
});
it('Should NOT handle undecorated labels', () => {
const result = {asm: [{text: '$LN3@caller2:'}]};
const demangler = new DummyWin32Demangler(cppfiltpath, new DummyCompiler());
demangler.result = result;
demangler.symbolstore = new SymbolStore();
demangler.collectLabels();
const output = demangler.win32RawSymbols;
expect(output).toEqual([]);
});
it('Should ignore comments after jmps', () => {
const result = {asm: [{text: ' jmp _Z1fP6mytype # TAILCALL'}]};
const demangler = new DummyCppDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
demangler.result = result;
demangler.symbolstore = new SymbolStore();
demangler.collectLabels();
const output = demangler.othersymbols.listSymbols();
expect(output).toEqual(['_Z1fP6mytype']);
});
it('Should still work with normal jmps', () => {
const result = {asm: [{text: ' jmp _Z1fP6mytype'}]};
const demangler = new DummyCppDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
demangler.result = result;
demangler.symbolstore = new SymbolStore();
demangler.collectLabels();
const output = demangler.othersymbols.listSymbols();
expect(output).toEqual(['_Z1fP6mytype']);
});
it('Should support CUDA PTX', () => {
const result = {
asm: [
{text: ' .visible .entry _Z6squarePii('},
{text: ' .param .u64 _Z6squarePii_param_0,'},
{text: ' ld.param.u64 %rd1, [_Z6squarePii_param_0];'},
{text: ' .func (.param .b32 func_retval0) _Z4cubePii('},
{text: '.global .attribute(.managed) .align 4 .b8 _ZN2ns9mymanagedE[16];'},
{text: '.global .texref _ZN2ns6texRefE;'},
{text: '.const .align 8 .u64 _ZN2ns5mystrE = generic($str);'},
],
};
const demangler = new DummyCppDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
return Promise.all([
demangler
.process(result)
.then(output => {
expect(output.asm[0].text).toEqual(' .visible .entry square(int*, int)(');
expect(output.asm[1].text).toEqual(' .param .u64 square(int*, int)_param_0,');
expect(output.asm[2].text).toEqual(' ld.param.u64 %rd1, [square(int*, int)_param_0];');
expect(output.asm[3].text).toEqual(' .func (.param .b32 func_retval0) cube(int*, int)(');
expect(output.asm[4].text).toEqual('.global .attribute(.managed) .align 4 .b8 ns::mymanaged[16];');
expect(output.asm[5].text).toEqual('.global .texref ns::texRef;');
expect(output.asm[6].text).toEqual('.const .align 8 .u64 ns::mystr = generic($str);');
})
.catch(catchCppfiltNonexistence),
]);
});
});
async function readResultFile(filename: string) {
const data = await fs.readFile(filename);
const asm = utils.splitLines(data.toString()).map(line => {
return {text: line};
});
return {asm};
}
async function DoDemangleTest(filename: string) {
const resultIn = await readResultFile(filename);
const resultOut = await readResultFile(filename + '.demangle');
const demangler = new DummyCppDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
await expect(demangler.process(resultIn)).resolves.toEqual(resultOut);
}
if (process.platform === 'linux') {
describe('File demangling', () => {
const testcasespath = resolvePathFromTestRoot('demangle-cases');
/*
* NB: this readdir must *NOT* be async
*
* Mocha calls the function passed to `describe` synchronously
* and expects the test suite to be fully configured upon return.
*
* If you pass an async function to describe and setup test cases
* after an await there is no guarantee they will be found, and
* if they are they will not end up in the expected suite.
*/
const files = fs.readdirSync(testcasespath);
for (const filename of files) {
if (filename.endsWith('.asm')) {
it(filename, async () => {
await DoDemangleTest(path.join(testcasespath, filename));
});
}
}
});
}
describe('Demangler prefix tree', () => {
const replacements = new PrefixTree([]);
replacements.add('a', 'short_a');
replacements.add('aa', 'long_a');
replacements.add('aa_shouldnotmatch', 'ERROR');
it('should replace a short match', () => {
expect(replacements.replaceAll('a').newText).toEqual('short_a');
});
it('should replace using the longest match', () => {
expect(replacements.replaceAll('aa').newText).toEqual('long_a');
});
it('should replace using both', () => {
expect(replacements.replaceAll('aaa').newText).toEqual('long_ashort_a');
});
it('should replace using both', () => {
expect(replacements.replaceAll('a aa a aa').newText).toEqual('short_a long_a short_a long_a');
});
it('should work with empty replacements', () => {
expect(new PrefixTree([]).replaceAll('Testing 123').newText).toEqual('Testing 123');
});
it('should leave unmatching text alone', () => {
expect(
replacements.replaceAll('Some text with none of the first letter of the ordered letter list').newText,
).toEqual('Some text with none of the first letter of the ordered letter list');
});
it('should handle a mixture', () => {
expect(replacements.replaceAll('Everyone loves an aardvark').newText).toEqual(
'Everyone loves short_an long_ardvshort_ark',
);
});
it('should find exact matches', () => {
expect(unwrap(replacements.findExact('a'))).toEqual('short_a');
expect(unwrap(replacements.findExact('aa'))).toEqual('long_a');
expect(unwrap(replacements.findExact('aa_shouldnotmatch'))).toEqual('ERROR');
});
it('should find not find mismatches', () => {
expect(replacements.findExact('aaa')).toBeNull();
expect(replacements.findExact(' aa')).toBeNull();
expect(replacements.findExact(' a')).toBeNull();
expect(replacements.findExact('Oh noes')).toBeNull();
expect(replacements.findExact('')).toBeNull();
});
});
// FIXME: The `c++filt` installed on `windows-2019` runners is so old that it produces
// different output, so we skip this test on Windows for now.
describe.skipIf(process.platform === 'win32')('LLVM IR demangler', () => {
it('demangles normal identifiers', () => {
const result = {
asm: [
{text: 'define dso_local noundef i32 @_Z6squarei(i32 noundef %num)'},
{text: 'define i32 @_ZN7example6square17hf2a64558a18ed1c1E(i32 %num) unnamed_addr'},
],
};
const demangler = new DummyLlvmDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
return Promise.all([
demangler
.process(result)
.then(output => {
expect(output.asm[0].text).toEqual('define dso_local noundef i32 @square(int)(i32 noundef %num)');
expect(output.asm[1].text).toEqual(
'define i32 @example::square::hf2a64558a18ed1c1(i32 %num) unnamed_addr',
);
})
.catch(catchCppfiltNonexistence),
]);
});
it('demangles quoted identifiers', () => {
const result = {
asm: [
{
text: ' invoke void @"_ZN4core3ptr53drop_in_place$LT$alloc..raw_vec..RawVec$LT$u8$GT$$GT$17h2e3e5a8e7287bb5aE"(ptr align 8 %_1) #17',
},
],
};
const demangler = new DummyLlvmDemangler(cppfiltpath, new DummyCompiler(), ['-n']);
return Promise.all([
demangler
.process(result)
.then(output => {
expect(output.asm[0].text).toEqual(
' invoke void @"core::ptr::drop_in_place<alloc::raw_vec::RawVec<u8>>::h2e3e5a8e7287bb5a"(ptr align 8 %_1) #17',
);
})
.catch(catchCppfiltNonexistence),
]);
});
});