Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/lib/libexceptions.js
4150 views
1
/**
2
* @license
3
* Copyright 2010 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*/
6
7
var LibraryExceptions = {
8
#if !WASM_EXCEPTIONS
9
$uncaughtExceptionCount: '0',
10
$exceptionLast: '0',
11
$exceptionCaught: ' []',
12
13
// This class is the exception metadata which is prepended to each thrown object (in WASM memory).
14
// It is allocated in one block among with a thrown object in __cxa_allocate_exception and freed
15
// in ___cxa_free_exception. It roughly corresponds to __cxa_exception structure in libcxxabi. The
16
// class itself is just a native pointer wrapper, and contains all the necessary accessors for the
17
// fields in the native structure.
18
// TODO: Unfortunately this approach still cannot be considered thread-safe because single
19
// exception object can be simultaneously thrown in several threads and its state (except
20
// reference counter) is not protected from that. Also protection is not enough, separate state
21
// should be allocated. libcxxabi has concept of dependent exception which is used for that
22
// purpose, it references the primary exception.
23
$ExceptionInfo: class {
24
// excPtr - Thrown object pointer to wrap. Metadata pointer is calculated from it.
25
constructor(excPtr) {
26
this.excPtr = excPtr;
27
this.ptr = excPtr - {{{ C_STRUCTS.__cxa_exception.__size__ }}};
28
}
29
30
set_type(type) {
31
{{{ makeSetValue('this.ptr', C_STRUCTS.__cxa_exception.exceptionType, 'type', '*') }}};
32
}
33
34
get_type() {
35
return {{{ makeGetValue('this.ptr', C_STRUCTS.__cxa_exception.exceptionType, '*') }}};
36
}
37
38
set_destructor(destructor) {
39
{{{ makeSetValue('this.ptr', C_STRUCTS.__cxa_exception.exceptionDestructor, 'destructor', '*') }}};
40
}
41
42
get_destructor() {
43
return {{{ makeGetValue('this.ptr', C_STRUCTS.__cxa_exception.exceptionDestructor, '*') }}};
44
}
45
46
set_caught(caught) {
47
caught = caught ? 1 : 0;
48
{{{ makeSetValue('this.ptr', C_STRUCTS.__cxa_exception.caught, 'caught', 'i8') }}};
49
}
50
51
get_caught() {
52
return {{{ makeGetValue('this.ptr', C_STRUCTS.__cxa_exception.caught, 'i8') }}} != 0;
53
}
54
55
set_rethrown(rethrown) {
56
rethrown = rethrown ? 1 : 0;
57
{{{ makeSetValue('this.ptr', C_STRUCTS.__cxa_exception.rethrown, 'rethrown', 'i8') }}};
58
}
59
60
get_rethrown() {
61
return {{{ makeGetValue('this.ptr', C_STRUCTS.__cxa_exception.rethrown, 'i8') }}} != 0;
62
}
63
64
// Initialize native structure fields. Should be called once after allocated.
65
init(type, destructor) {
66
#if EXCEPTION_DEBUG
67
dbg('ExceptionInfo init: ' + [type, destructor]);
68
#endif
69
this.set_adjusted_ptr(0);
70
this.set_type(type);
71
this.set_destructor(destructor);
72
}
73
74
set_adjusted_ptr(adjustedPtr) {
75
{{{ makeSetValue('this.ptr', C_STRUCTS.__cxa_exception.adjustedPtr, 'adjustedPtr', '*') }}};
76
}
77
78
get_adjusted_ptr() {
79
return {{{ makeGetValue('this.ptr', C_STRUCTS.__cxa_exception.adjustedPtr, '*') }}};
80
}
81
},
82
83
// Here, we throw an exception after recording a couple of values that we need to remember
84
// We also remember that it was the last exception thrown as we need to know that later.
85
__cxa_throw__deps: ['$ExceptionInfo', '$exceptionLast', '$uncaughtExceptionCount'],
86
__cxa_throw: (ptr, type, destructor) => {
87
#if EXCEPTION_DEBUG
88
dbg('__cxa_throw: ' + [ptrToString(ptr), type, ptrToString(destructor)]);
89
#endif
90
var info = new ExceptionInfo(ptr);
91
// Initialize ExceptionInfo content after it was allocated in __cxa_allocate_exception.
92
info.init(type, destructor);
93
{{{ storeException('exceptionLast', 'ptr') }}}
94
uncaughtExceptionCount++;
95
{{{ makeThrow('exceptionLast') }}}
96
},
97
98
// This exception will be caught twice, but while begin_catch runs twice,
99
// we early-exit from end_catch when the exception has been rethrown, so
100
// pop that here from the caught exceptions.
101
__cxa_rethrow__deps: ['$exceptionCaught', '$exceptionLast', '$uncaughtExceptionCount'],
102
__cxa_rethrow: () => {
103
var info = exceptionCaught.pop();
104
if (!info) {
105
abort('no exception to throw');
106
}
107
var ptr = info.excPtr;
108
if (!info.get_rethrown()) {
109
// Only pop if the corresponding push was through rethrow_primary_exception
110
exceptionCaught.push(info);
111
info.set_rethrown(true);
112
info.set_caught(false);
113
uncaughtExceptionCount++;
114
}
115
#if EXCEPTION_DEBUG
116
dbg('__cxa_rethrow, popped ' +
117
[ptrToString(ptr), exceptionLast, 'stack', exceptionCaught]);
118
#endif
119
{{{ storeException('exceptionLast', 'ptr') }}}
120
{{{ makeThrow('exceptionLast') }}}
121
},
122
123
llvm_eh_typeid_for: (type) => type,
124
125
__cxa_begin_catch__deps: ['$exceptionCaught', '__cxa_increment_exception_refcount',
126
'__cxa_get_exception_ptr',
127
'$uncaughtExceptionCount'],
128
__cxa_begin_catch: (ptr) => {
129
var info = new ExceptionInfo(ptr);
130
if (!info.get_caught()) {
131
info.set_caught(true);
132
uncaughtExceptionCount--;
133
}
134
info.set_rethrown(false);
135
exceptionCaught.push(info);
136
#if EXCEPTION_DEBUG
137
dbg('__cxa_begin_catch ' + [ptrToString(ptr), 'stack', exceptionCaught]);
138
#endif
139
___cxa_increment_exception_refcount(ptr);
140
return ___cxa_get_exception_ptr(ptr);
141
},
142
143
// We're done with a catch. Now, we can run the destructor if there is one
144
// and free the exception. Note that if the dynCall on the destructor fails
145
// due to calling apply on undefined, that means that the destructor is
146
// an invalid index into the FUNCTION_TABLE, so something has gone wrong.
147
__cxa_end_catch__deps: ['$exceptionCaught', '$exceptionLast', '__cxa_decrement_exception_refcount', 'setThrew'],
148
__cxa_end_catch: () => {
149
// Clear state flag.
150
_setThrew(0, 0);
151
#if ASSERTIONS
152
assert(exceptionCaught.length > 0);
153
#endif
154
// Call destructor if one is registered then clear it.
155
var info = exceptionCaught.pop();
156
157
#if EXCEPTION_DEBUG
158
dbg('__cxa_end_catch popped ' + [info, exceptionLast, 'stack', exceptionCaught]);
159
#endif
160
___cxa_decrement_exception_refcount(info.excPtr);
161
exceptionLast = 0; // XXX in decRef?
162
},
163
164
__cxa_uncaught_exceptions__deps: ['$uncaughtExceptionCount'],
165
__cxa_uncaught_exceptions: () => uncaughtExceptionCount,
166
167
__cxa_call_unexpected: (exception) => abort('Unexpected exception thrown, this is not properly supported - aborting'),
168
169
__cxa_current_primary_exception__deps: ['$exceptionCaught', '__cxa_increment_exception_refcount'],
170
__cxa_current_primary_exception: () => {
171
if (!exceptionCaught.length) {
172
return 0;
173
}
174
var info = exceptionCaught[exceptionCaught.length - 1];
175
___cxa_increment_exception_refcount(info.excPtr);
176
return info.excPtr;
177
},
178
179
__cxa_current_exception_type() {
180
if (!exceptionCaught.length) {
181
return 0;
182
}
183
var info = exceptionCaught[exceptionCaught.length - 1];
184
return info.get_type();
185
},
186
187
__cxa_rethrow_primary_exception__deps: ['$ExceptionInfo', '$exceptionCaught', '__cxa_rethrow'],
188
__cxa_rethrow_primary_exception: (ptr) => {
189
if (!ptr) return;
190
var info = new ExceptionInfo(ptr);
191
exceptionCaught.push(info);
192
info.set_rethrown(true);
193
___cxa_rethrow();
194
},
195
196
// Finds a suitable catch clause for when an exception is thrown.
197
// In normal compilers, this functionality is handled by the C++
198
// 'personality' routine. This is passed a fairly complex structure
199
// relating to the context of the exception and makes judgements
200
// about how to handle it. Some of it is about matching a suitable
201
// catch clause, and some of it is about unwinding. We already handle
202
// unwinding using 'if' blocks around each function, so the remaining
203
// functionality boils down to picking a suitable 'catch' block.
204
// We'll do that here, instead, to keep things simpler.
205
$findMatchingCatch__deps: ['$exceptionLast', '$ExceptionInfo', '__cxa_can_catch', '$setTempRet0'],
206
$findMatchingCatch: (args) => {
207
var thrown =
208
#if EXCEPTION_STACK_TRACES
209
exceptionLast?.excPtr;
210
#else
211
exceptionLast;
212
#endif
213
if (!thrown) {
214
// just pass through the null ptr
215
setTempRet0(0);
216
return 0;
217
}
218
var info = new ExceptionInfo(thrown);
219
info.set_adjusted_ptr(thrown);
220
var thrownType = info.get_type();
221
if (!thrownType) {
222
// just pass through the thrown ptr
223
setTempRet0(0);
224
return thrown;
225
}
226
227
// can_catch receives a **, add indirection
228
#if EXCEPTION_DEBUG
229
dbg("findMatchingCatch on " + ptrToString(thrown));
230
#endif
231
// The different catch blocks are denoted by different types.
232
// Due to inheritance, those types may not precisely match the
233
// type of the thrown object. Find one which matches, and
234
// return the type of the catch block which should be called.
235
for (var caughtType of args) {
236
if (caughtType === 0 || caughtType === thrownType) {
237
// Catch all clause matched or exactly the same type is caught
238
break;
239
}
240
var adjusted_ptr_addr = info.ptr + {{{ C_STRUCTS.__cxa_exception.adjustedPtr }}};
241
if (___cxa_can_catch(caughtType, thrownType, adjusted_ptr_addr)) {
242
#if EXCEPTION_DEBUG
243
dbg(" findMatchingCatch found " + [ptrToString(info.get_adjusted_ptr()), caughtType]);
244
#endif
245
setTempRet0(caughtType);
246
return thrown;
247
}
248
}
249
setTempRet0(thrownType);
250
return thrown;
251
},
252
253
__resumeException__deps: ['$exceptionLast'],
254
__resumeException: (ptr) => {
255
#if EXCEPTION_DEBUG
256
dbg("__resumeException " + [ptrToString(ptr), exceptionLast]);
257
#endif
258
if (!exceptionLast) {
259
{{{ storeException('exceptionLast', 'ptr') }}}
260
}
261
{{{ makeThrow('exceptionLast') }}}
262
},
263
264
#endif
265
#if WASM_EXCEPTIONS || !DISABLE_EXCEPTION_CATCHING
266
$getExceptionMessageCommon__deps: ['__get_exception_message', 'free', '$stackSave', '$stackRestore', '$stackAlloc'],
267
$getExceptionMessageCommon: (ptr) => {
268
var sp = stackSave();
269
var type_addr_addr = stackAlloc({{{ POINTER_SIZE }}});
270
var message_addr_addr = stackAlloc({{{ POINTER_SIZE }}});
271
___get_exception_message(ptr, type_addr_addr, message_addr_addr);
272
var type_addr = {{{ makeGetValue('type_addr_addr', 0, '*') }}};
273
var message_addr = {{{ makeGetValue('message_addr_addr', 0, '*') }}};
274
var type = UTF8ToString(type_addr);
275
_free(type_addr);
276
var message;
277
if (message_addr) {
278
message = UTF8ToString(message_addr);
279
_free(message_addr);
280
}
281
stackRestore(sp);
282
return [type, message];
283
},
284
#endif
285
#if WASM_EXCEPTIONS
286
$getCppExceptionTag__deps: ['__cpp_exception'],
287
// In static linking, tags are defined within the wasm module and are
288
// exported, whereas in dynamic linking, tags are defined in libcore.js in
289
// JS code and wasm modules import them.
290
$getCppExceptionTag: () => ___cpp_exception,
291
292
#if EXCEPTION_STACK_TRACES
293
// Throw a WebAssembly.Exception object with the C++ tag with a stack trace
294
// embedded. WebAssembly.Exception is a JS object representing a Wasm
295
// exception, provided by Wasm JS API:
296
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Exception
297
// In release builds, this function is not needed and the native
298
// _Unwind_RaiseException in libunwind is used instead.
299
__throw_exception_with_stack_trace__deps: ['$getCppExceptionTag', '$getExceptionMessage'],
300
__throw_exception_with_stack_trace: (ex) => {
301
var e = new WebAssembly.Exception(getCppExceptionTag(), [ex], {traceStack: true});
302
e.message = getExceptionMessage(e);
303
throw e;
304
},
305
#endif
306
307
// Given an WebAssembly.Exception object, returns the actual user-thrown
308
// C++ object address in the Wasm memory.
309
$getCppExceptionThrownObjectFromWebAssemblyException__deps: ['$getCppExceptionTag', '__thrown_object_from_unwind_exception'],
310
$getCppExceptionThrownObjectFromWebAssemblyException: (ex) => {
311
// In Wasm EH, the value extracted from WebAssembly.Exception is a pointer
312
// to the unwind header. Convert it to the actual thrown value.
313
var unwind_header = ex.getArg(getCppExceptionTag(), 0);
314
return ___thrown_object_from_unwind_exception(unwind_header);
315
},
316
317
$incrementExceptionRefcount__deps: ['__cxa_increment_exception_refcount', '$getCppExceptionThrownObjectFromWebAssemblyException'],
318
$incrementExceptionRefcount: (ex) => {
319
var ptr = getCppExceptionThrownObjectFromWebAssemblyException(ex);
320
___cxa_increment_exception_refcount(ptr);
321
},
322
323
$decrementExceptionRefcount__deps: ['__cxa_decrement_exception_refcount', '$getCppExceptionThrownObjectFromWebAssemblyException'],
324
$decrementExceptionRefcount: (ex) => {
325
var ptr = getCppExceptionThrownObjectFromWebAssemblyException(ex);
326
___cxa_decrement_exception_refcount(ptr);
327
},
328
329
$getExceptionMessage__deps: ['$getCppExceptionThrownObjectFromWebAssemblyException', '$getExceptionMessageCommon'],
330
$getExceptionMessage: (ex) => {
331
var ptr = getCppExceptionThrownObjectFromWebAssemblyException(ex);
332
return getExceptionMessageCommon(ptr);
333
},
334
335
#elif !DISABLE_EXCEPTION_CATCHING
336
$incrementExceptionRefcount__deps: ['__cxa_increment_exception_refcount'],
337
$incrementExceptionRefcount: (ptr) => ___cxa_increment_exception_refcount(ptr),
338
339
$decrementExceptionRefcount__deps: ['__cxa_decrement_exception_refcount'],
340
$decrementExceptionRefcount: (ptr) => ___cxa_decrement_exception_refcount(ptr),
341
342
$getExceptionMessage__deps: ['$getExceptionMessageCommon'],
343
$getExceptionMessage: (ptr) => getExceptionMessageCommon(ptr),
344
345
#endif
346
};
347
348
#if !WASM_EXCEPTIONS
349
// In LLVM, exceptions generate a set of functions of form
350
// __cxa_find_matching_catch_2(), __cxa_find_matching_catch_3(), etc. where the
351
// number specifies the number of arguments. In Emscripten, route all these to
352
// a single function '__cxa_find_matching_catch' that variadically processes all
353
// of these functions using JS 'arguments' object.
354
addCxaCatch = (n) => {
355
const args = [];
356
// Confusingly, the actual number of asrgument is n - 2. According to the llvm
357
// code in WebAssemblyLowerEmscriptenEHSjLj.cpp:
358
// This is because a landingpad instruction contains two more arguments, a
359
// personality function and a cleanup bit, and __cxa_find_matching_catch_N
360
// functions are named after the number of arguments in the original landingpad
361
// instruction.
362
let sig = 'p';
363
for (let i = 0; i < n - 2; i++) {
364
args.push(`arg${i}`);
365
sig += 'p';
366
}
367
const argString = args.join(',');
368
LibraryManager.library[`__cxa_find_matching_catch_${n}__sig`] = sig;
369
LibraryManager.library[`__cxa_find_matching_catch_${n}__deps`] = ['$findMatchingCatch'];
370
LibraryManager.library[`__cxa_find_matching_catch_${n}`] = eval(`(${args}) => findMatchingCatch([${argString}])`);
371
};
372
373
// Add the first 2-5 catch handlers preemptively. Others get added on demand in
374
// jsifier. This is done here primarily so that these symbols end up with the
375
// correct deps in the stub library that we pass to wasm-ld.
376
// Note: __cxa_find_matching_catch_N function uses N = NumClauses + 2 so
377
// __cxa_find_matching_catch_2 is the first such function with zero clauses.
378
// See WebAssemblyLowerEmscriptenEHSjLj.cpp.
379
for (let i = 2; i < 5; i++) {
380
addCxaCatch(i)
381
}
382
#endif
383
384
addToLibrary(LibraryExceptions);
385
386