Skip to content

Commit 076a6c3

Browse files
authored
Add downstream libunwind changes from emscripten (#2)
1 parent 3ec3d03 commit 076a6c3

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

libunwind/include/unwind_itanium.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct _Unwind_Exception {
2424
_Unwind_Exception *exc);
2525
#if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
2626
uintptr_t private_[6];
27-
#else
27+
#elif !defined(__USING_WASM_EXCEPTIONS__)
2828
uintptr_t private_1; // non-zero means forced unwind
2929
uintptr_t private_2; // holds sp that phase1 found for phase2 to use
3030
#endif

libunwind/src/Unwind-wasm.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "config.h"
2+
#include "unwind.h"
3+
#include <stdbool.h>
4+
#include <threads.h>
5+
6+
#ifdef __USING_WASM_EXCEPTIONS__
7+
8+
_Unwind_Reason_Code __gxx_personality_wasm0(int version, _Unwind_Action actions,
9+
uint64_t exceptionClass,
10+
_Unwind_Exception *unwind_exception,
11+
_Unwind_Context *context);
12+
13+
struct _Unwind_LandingPadContext {
14+
// Input information to personality function
15+
uintptr_t lpad_index; // landing pad index
16+
uintptr_t lsda; // LSDA address
17+
18+
// Output information computed by personality function
19+
uintptr_t selector; // selector value
20+
};
21+
22+
// Communication channel between compiler-generated user code and personality
23+
// function
24+
thread_local struct _Unwind_LandingPadContext __wasm_lpad_context;
25+
26+
/// Calls to this function is in landing pads in compiler-generated user code.
27+
/// In other EH schemes, stack unwinding is done by libunwind library, which
28+
/// calls the personality function for each each frame it lands. On the other
29+
/// hand, WebAssembly stack unwinding process is performed by a VM, and the
30+
/// personality function cannot be called from there. So the compiler inserts
31+
/// a call to this function in landing pads in the user code, which in turn
32+
/// calls the personality function.
33+
_Unwind_Reason_Code _Unwind_CallPersonality(void *exception_ptr) {
34+
struct _Unwind_Exception *exception_object =
35+
(struct _Unwind_Exception *)exception_ptr;
36+
_LIBUNWIND_TRACE_API("_Unwind_CallPersonality(exception_object=%p)",
37+
(void *)exception_object);
38+
39+
// Reset the selector.
40+
__wasm_lpad_context.selector = 0;
41+
42+
// Call personality function. Wasm does not have two-phase unwinding, so we
43+
// only do the cleanup phase.
44+
_Unwind_Reason_Code ret = __gxx_personality_wasm0(
45+
1, _UA_SEARCH_PHASE, exception_object->exception_class, exception_object,
46+
(struct _Unwind_Context *)&__wasm_lpad_context);
47+
return ret;
48+
}
49+
50+
/// Called by __cxa_throw.
51+
_LIBUNWIND_EXPORT _Unwind_Reason_Code
52+
_Unwind_RaiseException(_Unwind_Exception *exception_object) {
53+
_LIBUNWIND_TRACE_API("_Unwind_RaiseException(exception_object=%p)",
54+
(void *)exception_object);
55+
__builtin_wasm_throw(0, exception_object);
56+
}
57+
58+
/// Called by __cxa_end_catch.
59+
_LIBUNWIND_EXPORT void
60+
_Unwind_DeleteException(_Unwind_Exception *exception_object) {
61+
_LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
62+
(void *)(exception_object));
63+
if (exception_object->exception_cleanup != NULL)
64+
(*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
65+
exception_object);
66+
}
67+
68+
/// Called by personality handler to alter register values.
69+
_LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
70+
uintptr_t value) {
71+
_LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, index=%d, value=%lu)",
72+
(void *)context, index, value);
73+
// We only use this function to set __wasm_lpad_context.selector field, which
74+
// is index 1 in the personality function.
75+
if (index != 1)
76+
return;
77+
((struct _Unwind_LandingPadContext *)context)->selector = value;
78+
}
79+
80+
/// Called by personality handler to get instruction pointer.
81+
_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
82+
// The result will be used as an 1-based index after decrementing 1, so we
83+
// increment 2 here
84+
uintptr_t result =
85+
((struct _Unwind_LandingPadContext *)context)->lpad_index + 2;
86+
_LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => %lu", (void *)context,
87+
result);
88+
return result;
89+
}
90+
91+
/// Not used in wasm.
92+
_LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
93+
uintptr_t value) {}
94+
95+
/// Called by personality handler to get LSDA for current frame.
96+
_LIBUNWIND_EXPORT uintptr_t
97+
_Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
98+
uintptr_t result = ((struct _Unwind_LandingPadContext *)context)->lsda;
99+
_LIBUNWIND_TRACE_API("_Unwind_GetLanguageSpecificData(context=%p) => 0x%lx",
100+
(void *)context, result);
101+
return result;
102+
}
103+
104+
/// Not used in wasm.
105+
_LIBUNWIND_EXPORT uintptr_t
106+
_Unwind_GetRegionStart(struct _Unwind_Context *context) {
107+
return 0;
108+
}
109+
110+
#endif

libunwind/src/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
SYMBOL_NAME(name))) \
9999
extern "C" _LIBUNWIND_EXPORT __typeof(name) aliasname;
100100
#endif
101+
#elif defined(__EMSCRIPTEN__)
101102
#else
102103
#error Unsupported target
103104
#endif

0 commit comments

Comments
 (0)