Skip to content

Make wasmOffsetConvert usage more localized in libstack_trace.js. NFC #24599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions src/lib/libstack_trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,29 @@ var LibraryStackTrace = {
$convertFrameToPC__docs: '/** @returns {number} */',
$convertFrameToPC__internal: true,
$convertFrameToPC: (frame) => {
#if !USE_OFFSET_CONVERTER
abort('Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER');
#else
#if ASSERTIONS
assert(wasmOffsetConverter, 'wasmOffsetConverter global not found');
#endif
var match;

if (match = /\bwasm-function\[\d+\]:(0x[0-9a-f]+)/.exec(frame)) {
// some engines give the binary offset directly, so we use that as return address
return +match[1];
} else if (match = /\bwasm-function\[(\d+)\]:(\d+)/.exec(frame)) {
// other engines only give function index and offset in the function,
// Older versions of v8 give function index and offset in the function,
// so we try using the offset converter. If that doesn't work,
// we pack index and offset into a "return address"
#if !USE_OFFSET_CONVERTER
abort('Legacy backtrace format detected but -sUSE_OFFSET_CONVERTER not present.')
#else
#if ASSERTIONS
assert(wasmOffsetConverter, 'wasmOffsetConverter global not found');
#endif
return wasmOffsetConverter.convert(+match[1], +match[2]);
Comment on lines +118 to 119
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#endif
return wasmOffsetConverter.convert(+match[1], +match[2]);
return wasmOffsetConverter.convert(+match[1], +match[2]);
#endif

May as well only emit it when it is enabled.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the case already... this code is already part of the #else block right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I missed the if for ASSERTIONS... lgtm as is

#endif
} else if (match = /:(\d+):\d+(?:\)|$)/.exec(frame)) {
// If we are in js, we can use the js line number as the "return address".
// This should work for wasm2js. We tag the high bit to distinguish this
// from wasm addresses.
return 0x80000000 | +match[1];
}
#endif
// return 0 if we can't find any
return 0;
},
Expand Down Expand Up @@ -242,16 +242,10 @@ var LibraryStackTrace = {
},

// Look up the function name from our stack frame cache with our PC representation.
#if USE_OFFSET_CONVERTER
emscripten_pc_get_function__deps: ['$UNWIND_CACHE', 'free', '$stringToNewUTF8'],
// Don't treat allocation of _emscripten_pc_get_function.ret as a leak
emscripten_pc_get_function__noleakcheck: true,
#endif
emscripten_pc_get_function: (pc) => {
#if !USE_OFFSET_CONVERTER
abort('Cannot use emscripten_pc_get_function without -sUSE_OFFSET_CONVERTER');
return 0;
#else
var name;
if (pc & 0x80000000) {
// If this is a JavaScript function, try looking it up in the unwind cache.
Expand All @@ -267,12 +261,16 @@ var LibraryStackTrace = {
return 0;
}
} else {
#if !USE_OFFSET_CONVERTER
abort('Cannot use emscripten_pc_get_function on native functions without -sUSE_OFFSET_CONVERTER');
return 0;
#else
name = wasmOffsetConverter.getName(pc);
#endif
}
_free(_emscripten_pc_get_function.ret ?? 0);
_emscripten_pc_get_function.ret = stringToNewUTF8(name);
return _emscripten_pc_get_function.ret;
#endif
},

$convertPCtoSourceLocation__deps: ['$UNWIND_CACHE'],
Expand Down