Skip to content

Commit a66b3fb

Browse files
Wasm demangling (emscripten-core#4851)
* Reenable test_demangle_stacks for V8 * Don't inline Aborter constructor in test_demangle_stacks.cpp The test depends on emitting a symbol for Aborter, which wasm_backend was inlining. * Update js stackTrace demangling for wasm_backend wasm_backend emits functions without an underscore prefix, which causes mismatches with js backend when it comes to C++ symbol demangling, particularly in stack traces.
1 parent 2de6ea6 commit a66b3fb

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

src/preamble.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -858,15 +858,20 @@ function lengthBytesUTF32(str) {
858858
{{{ maybeExport('lengthBytesUTF32') }}}
859859

860860
function demangle(func) {
861-
var hasLibcxxabi = !!Module['___cxa_demangle'];
862-
if (hasLibcxxabi) {
861+
var __cxa_demangle_func = Module['___cxa_demangle'] || Module['__cxa_demangle'];
862+
if (__cxa_demangle_func) {
863863
try {
864-
var s = func.substr(1);
864+
var s =
865+
#if WASM_BACKEND
866+
func;
867+
#else
868+
func.substr(1);
869+
#endif
865870
var len = lengthBytesUTF8(s)+1;
866871
var buf = _malloc(len);
867872
stringToUTF8(s, buf, len);
868873
var status = _malloc(4);
869-
var ret = Module['___cxa_demangle'](buf, 0, 0, status);
874+
var ret = __cxa_demangle_func(buf, 0, 0, status);
870875
if (getValue(status, 'i32') === 0 && ret) {
871876
return Pointer_stringify(ret);
872877
}
@@ -886,7 +891,17 @@ function demangle(func) {
886891
}
887892

888893
function demangleAll(text) {
889-
return text.replace(/__Z[\w\d_]+/g, function(x) { var y = demangle(x); return x === y ? x : (x + ' [' + y + ']') });
894+
var regex =
895+
#if WASM_BACKEND
896+
/_Z[\w\d_]+/g;
897+
#else
898+
/__Z[\w\d_]+/g;
899+
#endif
900+
return text.replace(regex,
901+
function(x) {
902+
var y = demangle(x);
903+
return x === y ? x : (x + ' [' + y + ']');
904+
});
890905
}
891906

892907
function jsStackTrace() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace NameSpace {
55
class Class {
66
public:
7+
__attribute__((noinline))
78
void Aborter(double x, char y, int *z) {
89
int addr = x + y + (int)z;
910
void *p = (void *)addr;

tests/test_core.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6115,26 +6115,22 @@ def test_emulate_function_pointer_casts(self):
61156115
'''
61166116
self.do_run(src, '|1.266,1|\n')
61176117

6118-
@no_wasm_backend()
61196118
def test_demangle_stacks(self):
6120-
if self.is_wasm():
6121-
self.banned_js_engines = [V8_ENGINE] # https://bugs.chromium.org/p/v8/issues/detail?id=5632
61226119
Settings.DEMANGLE_SUPPORT = 1
61236120
if '-O' in str(self.emcc_args):
61246121
self.emcc_args += ['--profiling-funcs', '--llvm-opts', '0']
61256122

61266123
self.do_run_in_out_file_test('tests', 'core', 'test_demangle_stacks')
61276124

61286125
@no_emterpreter
6129-
@no_wasm_backend()
61306126
def test_demangle_stacks_symbol_map(self):
61316127
Settings.DEMANGLE_SUPPORT = 1
61326128
if '-O' in str(self.emcc_args) and '-O0' not in self.emcc_args and '-O1' not in self.emcc_args and '-g' not in self.emcc_args:
61336129
self.emcc_args += ['--llvm-opts', '0']
61346130
else:
61356131
return self.skip("without opts, we don't emit a symbol map")
61366132
self.emcc_args += ['--emit-symbol-map']
6137-
self.do_run(open(path_from_root('tests', 'core', 'test_demangle_stacks.c')).read(), 'abort')
6133+
self.do_run(open(path_from_root('tests', 'core', 'test_demangle_stacks.cpp')).read(), 'abort')
61386134
# make sure the shortened name is the right one
61396135
symbols = open('src.cpp.o.js.symbols').read().split('\n')
61406136
for line in symbols:

0 commit comments

Comments
 (0)