llvm-project
123 строки · 4.8 Кб
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//
8// Implements Wasm exception handling proposal
9// (https://github.com/WebAssembly/exception-handling) based C++ exceptions
10//
11//===----------------------------------------------------------------------===//
12
13#include <stdbool.h>
14
15#include "config.h"
16
17#ifdef __WASM_EXCEPTIONS__
18
19#include "unwind.h"
20#include <threads.h>
21
22_Unwind_Reason_Code __gxx_personality_wasm0(int version, _Unwind_Action actions,
23uint64_t exceptionClass,
24_Unwind_Exception *unwind_exception,
25_Unwind_Context *context);
26
27struct _Unwind_LandingPadContext {
28// Input information to personality function
29uintptr_t lpad_index; // landing pad index
30uintptr_t lsda; // LSDA address
31
32// Output information computed by personality function
33uintptr_t selector; // selector value
34};
35
36// Communication channel between compiler-generated user code and personality
37// function
38thread_local struct _Unwind_LandingPadContext __wasm_lpad_context;
39
40/// Calls to this function is in landing pads in compiler-generated user code.
41/// In other EH schemes, stack unwinding is done by libunwind library, which
42/// calls the personality function for each each frame it lands. On the other
43/// hand, WebAssembly stack unwinding process is performed by a VM, and the
44/// personality function cannot be called from there. So the compiler inserts
45/// a call to this function in landing pads in the user code, which in turn
46/// calls the personality function.
47_Unwind_Reason_Code _Unwind_CallPersonality(void *exception_ptr) {
48struct _Unwind_Exception *exception_object =
49(struct _Unwind_Exception *)exception_ptr;
50_LIBUNWIND_TRACE_API("_Unwind_CallPersonality(exception_object=%p)",
51(void *)exception_object);
52
53// Reset the selector.
54__wasm_lpad_context.selector = 0;
55
56// Call personality function. Wasm does not have two-phase unwinding, so we
57// only do the cleanup phase.
58return __gxx_personality_wasm0(
591, _UA_SEARCH_PHASE, exception_object->exception_class, exception_object,
60(struct _Unwind_Context *)&__wasm_lpad_context);
61}
62
63/// Called by __cxa_throw.
64_LIBUNWIND_EXPORT _Unwind_Reason_Code
65_Unwind_RaiseException(_Unwind_Exception *exception_object) {
66_LIBUNWIND_TRACE_API("_Unwind_RaiseException(exception_object=%p)",
67(void *)exception_object);
68// Use Wasm EH's 'throw' instruction.
69__builtin_wasm_throw(0, exception_object);
70}
71
72/// Called by __cxa_end_catch.
73_LIBUNWIND_EXPORT void
74_Unwind_DeleteException(_Unwind_Exception *exception_object) {
75_LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
76(void *)(exception_object));
77if (exception_object->exception_cleanup != NULL)
78(*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
79exception_object);
80}
81
82/// Called by personality handler to alter register values.
83_LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
84uintptr_t value) {
85_LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, index=%d, value=%lu)",
86(void *)context, index, value);
87// We only use this function to set __wasm_lpad_context.selector field, which
88// is index 1 in the personality function.
89if (index == 1)
90((struct _Unwind_LandingPadContext *)context)->selector = value;
91}
92
93/// Called by personality handler to get instruction pointer.
94_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
95// The result will be used as an 1-based index after decrementing 1, so we
96// increment 2 here
97uintptr_t result =
98((struct _Unwind_LandingPadContext *)context)->lpad_index + 2;
99_LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => %lu", (void *)context,
100result);
101return result;
102}
103
104/// Not used in Wasm.
105_LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
106uintptr_t value) {}
107
108/// Called by personality handler to get LSDA for current frame.
109_LIBUNWIND_EXPORT uintptr_t
110_Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
111uintptr_t result = ((struct _Unwind_LandingPadContext *)context)->lsda;
112_LIBUNWIND_TRACE_API("_Unwind_GetLanguageSpecificData(context=%p) => 0x%lx",
113(void *)context, result);
114return result;
115}
116
117/// Not used in Wasm.
118_LIBUNWIND_EXPORT uintptr_t
119_Unwind_GetRegionStart(struct _Unwind_Context *context) {
120return 0;
121}
122
123#endif // defined(__WASM_EXCEPTIONS__)
124