Skip to content

Commit b77695c

Browse files
committed
Move demangle helper to library_legacy.js
Followup to emscripten-core#21156 We no longer use this function anywhere in emscripten.
1 parent 9710247 commit b77695c

File tree

8 files changed

+48
-46
lines changed

8 files changed

+48
-46
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ See docs/process.md for more on how version tagging works.
2020

2121
3.1.54 (in development)
2222
-----------------------
23+
- The `DEMANGLE_SUPPORT` setting and the associated `demangle` function are
24+
now deprecated since Wasm stack traces always contain demangled symbols these
25+
days. (#21346)
2326
- The type of `EMSCRIPTEN_WEBGL_CONTEXT_HANDLE` was changed to unsigned and
2427
the only valid error returned from `emscripten_webgl_create_context` is
2528
now zero. This allows `EMSCRIPTEN_WEBGL_CONTEXT_HANDLE` to hold a pointer

site/source/docs/tools_reference/settings_reference.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,9 @@ Print out exceptions in emscriptened code.
409409
DEMANGLE_SUPPORT
410410
================
411411

412-
If 1, export `demangle` and `stackTrace` helper function.
412+
If 1, export `demangle` and `stackTrace` JS library functions.
413+
414+
.. note:: This setting is deprecated
413415

414416
.. _library_debug:
415417

src/library_legacy.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,34 @@ addToLibrary({
9090
return 0;
9191
},
9292
#endif
93+
94+
#if LINK_AS_CXX
95+
$demangle__deps: ['$withStackSave', '__cxa_demangle', 'free', '$stringToUTF8OnStack'],
96+
$demangle: (func) => {
97+
// If demangle has failed before, stop demangling any further function names
98+
// This avoids an infinite recursion with malloc()->abort()->stackTrace()->demangle()->malloc()->...
99+
demangle.recursionGuard = (demangle.recursionGuard|0)+1;
100+
if (demangle.recursionGuard > 1) return func;
101+
return withStackSave(() => {
102+
try {
103+
var s = func;
104+
if (s.startsWith('__Z'))
105+
s = s.substr(1);
106+
var buf = stringToUTF8OnStack(s);
107+
var status = stackAlloc(4);
108+
var ret = ___cxa_demangle(buf, 0, 0, status);
109+
if ({{{ makeGetValue('status', '0', 'i32') }}} === 0 && ret) {
110+
return UTF8ToString(ret);
111+
}
112+
// otherwise, libcxxabi failed
113+
} catch(e) {
114+
} finally {
115+
_free(ret);
116+
if (demangle.recursionGuard < 2) --demangle.recursionGuard;
117+
}
118+
// failure when using libcxxabi, don't demangle
119+
return func;
120+
});
121+
},
122+
#endif
93123
});

src/library_stack_trace.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,6 @@
55
*/
66

77
var LibraryStackTrace = {
8-
#if DEMANGLE_SUPPORT
9-
$demangle__deps: ['$withStackSave', '__cxa_demangle', 'free', '$stringToUTF8OnStack'],
10-
#endif
11-
$demangle: (func) => {
12-
#if DEMANGLE_SUPPORT
13-
// If demangle has failed before, stop demangling any further function names
14-
// This avoids an infinite recursion with malloc()->abort()->stackTrace()->demangle()->malloc()->...
15-
demangle.recursionGuard = (demangle.recursionGuard|0)+1;
16-
if (demangle.recursionGuard > 1) return func;
17-
return withStackSave(() => {
18-
try {
19-
var s = func;
20-
if (s.startsWith('__Z'))
21-
s = s.substr(1);
22-
var buf = stringToUTF8OnStack(s);
23-
var status = stackAlloc(4);
24-
var ret = ___cxa_demangle(buf, 0, 0, status);
25-
if ({{{ makeGetValue('status', '0', 'i32') }}} === 0 && ret) {
26-
return UTF8ToString(ret);
27-
}
28-
// otherwise, libcxxabi failed
29-
} catch(e) {
30-
} finally {
31-
_free(ret);
32-
if (demangle.recursionGuard < 2) --demangle.recursionGuard;
33-
}
34-
// failure when using libcxxabi, don't demangle
35-
return func;
36-
});
37-
#else // DEMANGLE_SUPPORT
38-
#if ASSERTIONS
39-
warnOnce('warning: build with -sDEMANGLE_SUPPORT to link in libcxxabi demangling');
40-
#endif // ASSERTIONS
41-
return func;
42-
#endif // DEMANGLE_SUPPORT
43-
},
44-
458
$jsStackTrace: function() {
469
var error = new Error();
4710
if (!error.stack) {

src/settings.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,9 @@ var EMULATE_FUNCTION_POINTER_CASTS = false;
337337
// [link]
338338
var EXCEPTION_DEBUG = false;
339339

340-
// If 1, export `demangle` and `stackTrace` helper function.
340+
// If 1, export `demangle` and `stackTrace` JS library functions.
341341
// [link]
342+
// [deprecated]
342343
var DEMANGLE_SUPPORT = false;
343344

344345
// Print out when we enter a library call (library*.js). You can also unset

test/test_core.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7213,7 +7213,6 @@ def test_emulate_function_pointer_casts(self):
72137213
})
72147214
def test_demangle_stacks(self, extra_args):
72157215
self.emcc_args += extra_args
7216-
self.set_setting('DEMANGLE_SUPPORT')
72177216
self.set_setting('ASSERTIONS')
72187217
# disable aggressive inlining in binaryen
72197218
self.set_setting('BINARYEN_EXTRA_PASSES', '--one-caller-inline-max-function-size=1')
@@ -7234,7 +7233,6 @@ def test_demangle_stacks_symbol_map(self):
72347233
self.set_setting('BINARYEN_EXTRA_PASSES', '--one-caller-inline-max-function-size=1')
72357234
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', '$stackTrace')
72367235

7237-
self.set_setting('DEMANGLE_SUPPORT')
72387236
self.set_setting('ENVIRONMENT', 'node,shell')
72397237
if '-O' not in str(self.emcc_args) or '-O0' in self.emcc_args or '-O1' in self.emcc_args or '-g' in self.emcc_args:
72407238
self.skipTest("without opts, we don't emit a symbol map")
@@ -7943,7 +7941,6 @@ def test_modularize_closure_pre(self):
79437941
@no_wasm2js('symbol names look different wasm2js backtraces')
79447942
@also_with_wasm_bigint
79457943
def test_emscripten_log(self):
7946-
self.set_setting('DEMANGLE_SUPPORT')
79477944
if '-g' not in self.emcc_args:
79487945
self.emcc_args.append('-g')
79497946
self.emcc_args += ['-DRUN_FROM_JS_SHELL', '-Wno-deprecated-pragma']

test/test_other.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,10 +3466,13 @@ def test_syntax_only_invalid(self):
34663466
self.assertContained("src.c:1:13: error: expected '}'", err)
34673467
self.assertNotExists('a.out.js')
34683468

3469+
# `demangle` is a legacy JS function on longer used by emscripten
3470+
# TODO(sbc): Remove `demangle` and this test.
34693471
def test_demangle(self):
34703472
create_file('src.cpp', '''
34713473
#include <stdio.h>
34723474
#include <emscripten.h>
3475+
34733476
void two(char c) {
34743477
EM_ASM(out(stackTrace()));
34753478
}
@@ -3547,10 +3550,10 @@ def test_demangle_cpp(self):
35473550

35483551
self.do_runf('src.cpp', 'Waka::f::a23412341234::point()')
35493552

3550-
# Test that malloc() -> OOM -> abort() -> stackTrace() -> jsStackTrace() -> demangleAll() -> demangle() -> malloc()
3551-
# cycle will not produce an infinite loop.
3553+
# Test that malloc() -> OOM -> abort() -> stackTrace() -> jsStackTrace()
3554+
# cycle will not cycle back to malloc to produce an infinite loop.
35523555
def test_demangle_malloc_infinite_loop_crash(self):
3553-
self.run_process([EMXX, test_file('malloc_demangle_infinite_loop.cpp'), '-g', '-sABORTING_MALLOC', '-sDEMANGLE_SUPPORT'])
3556+
self.run_process([EMXX, test_file('malloc_demangle_infinite_loop.cpp'), '-g', '-sABORTING_MALLOC'])
35543557
output = self.run_js('a.out.js', assert_returncode=NON_ZERO)
35553558
if output.count('Cannot enlarge memory arrays') > 5:
35563559
print(output)
@@ -8423,7 +8426,7 @@ def test_metadce_minimal_pthreads(self):
84238426
'except': (['-O2', '-fexceptions'], [], ['waka']), # noqa
84248427
# exceptions does not pull in demangling by default, which increases code size
84258428
'mangle': (['-O2', '-fexceptions',
8426-
'-sDEMANGLE_SUPPORT'], [], ['waka']), # noqa
8429+
'-sDEMANGLE_SUPPORT', '-Wno-deprecated'], [], ['waka']), # noqa
84278430
# Wasm EH's code size increase is smaller than that of Emscripten EH
84288431
'except_wasm': (['-O2', '-fwasm-exceptions'], [], ['waka']), # noqa
84298432
# eval_ctors 1 can partially optimize, but runs into getenv() for locale

tools/link.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,9 @@ def phase_linker_setup(options, state, newargs):
701701
if 'SUPPORT_ERRNO' in user_settings:
702702
diagnostics.warning('deprecated', 'SUPPORT_ERRNO is deprecated since emscripten no longer uses the setErrNo library function')
703703

704+
if 'DEMANGLE_SUPPORT' in user_settings:
705+
diagnostics.warning('deprecated', 'DEMANGLE_SUPPORT is deprecated since mangled names no longer appear in stack traces')
706+
704707
if settings.EXTRA_EXPORTED_RUNTIME_METHODS:
705708
diagnostics.warning('deprecated', 'EXTRA_EXPORTED_RUNTIME_METHODS is deprecated, please use EXPORTED_RUNTIME_METHODS instead')
706709
settings.EXPORTED_RUNTIME_METHODS += settings.EXTRA_EXPORTED_RUNTIME_METHODS

0 commit comments

Comments
 (0)