Skip to content

Commit bbe1752

Browse files
authored
Make wasmOffsetConvert usage more localized in libstack_trace.js. NFC (#24599)
I noticed this while investigating #24587.
1 parent c156626 commit bbe1752

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/lib/libstack_trace.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,29 @@ var LibraryStackTrace = {
101101
$convertFrameToPC__docs: '/** @returns {number} */',
102102
$convertFrameToPC__internal: true,
103103
$convertFrameToPC: (frame) => {
104-
#if !USE_OFFSET_CONVERTER
105-
abort('Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER');
106-
#else
107-
#if ASSERTIONS
108-
assert(wasmOffsetConverter, 'wasmOffsetConverter global not found');
109-
#endif
110104
var match;
111105

112106
if (match = /\bwasm-function\[\d+\]:(0x[0-9a-f]+)/.exec(frame)) {
113107
// some engines give the binary offset directly, so we use that as return address
114108
return +match[1];
115109
} else if (match = /\bwasm-function\[(\d+)\]:(\d+)/.exec(frame)) {
116-
// other engines only give function index and offset in the function,
110+
// Older versions of v8 give function index and offset in the function,
117111
// so we try using the offset converter. If that doesn't work,
118112
// we pack index and offset into a "return address"
113+
#if !USE_OFFSET_CONVERTER
114+
abort('Legacy backtrace format detected but -sUSE_OFFSET_CONVERTER not present.')
115+
#else
116+
#if ASSERTIONS
117+
assert(wasmOffsetConverter, 'wasmOffsetConverter global not found');
118+
#endif
119119
return wasmOffsetConverter.convert(+match[1], +match[2]);
120+
#endif
120121
} else if (match = /:(\d+):\d+(?:\)|$)/.exec(frame)) {
121122
// If we are in js, we can use the js line number as the "return address".
122123
// This should work for wasm2js. We tag the high bit to distinguish this
123124
// from wasm addresses.
124125
return 0x80000000 | +match[1];
125126
}
126-
#endif
127127
// return 0 if we can't find any
128128
return 0;
129129
},
@@ -242,16 +242,10 @@ var LibraryStackTrace = {
242242
},
243243

244244
// Look up the function name from our stack frame cache with our PC representation.
245-
#if USE_OFFSET_CONVERTER
246245
emscripten_pc_get_function__deps: ['$UNWIND_CACHE', 'free', '$stringToNewUTF8'],
247246
// Don't treat allocation of _emscripten_pc_get_function.ret as a leak
248247
emscripten_pc_get_function__noleakcheck: true,
249-
#endif
250248
emscripten_pc_get_function: (pc) => {
251-
#if !USE_OFFSET_CONVERTER
252-
abort('Cannot use emscripten_pc_get_function without -sUSE_OFFSET_CONVERTER');
253-
return 0;
254-
#else
255249
var name;
256250
if (pc & 0x80000000) {
257251
// If this is a JavaScript function, try looking it up in the unwind cache.
@@ -267,12 +261,16 @@ var LibraryStackTrace = {
267261
return 0;
268262
}
269263
} else {
264+
#if !USE_OFFSET_CONVERTER
265+
abort('Cannot use emscripten_pc_get_function on native functions without -sUSE_OFFSET_CONVERTER');
266+
return 0;
267+
#else
270268
name = wasmOffsetConverter.getName(pc);
269+
#endif
271270
}
272271
_free(_emscripten_pc_get_function.ret ?? 0);
273272
_emscripten_pc_get_function.ret = stringToNewUTF8(name);
274273
return _emscripten_pc_get_function.ret;
275-
#endif
276274
},
277275

278276
$convertPCtoSourceLocation__deps: ['$UNWIND_CACHE'],

0 commit comments

Comments
 (0)