1284990Scy//===----------------------------------------------------------------------===//
2284990Scy//
3284990Scy// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4284990Scy// See https://llvm.org/LICENSE.txt for license information.
5284990Scy// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6284990Scy//
7284990Scy//
8284990Scy//  Implements Wasm exception handling proposal
9284990Scy//  (https://github.com/WebAssembly/exception-handling) based C++ exceptions
10284990Scy//
11284990Scy//===----------------------------------------------------------------------===//
12284990Scy
13284990Scy#include <stdbool.h>
14284990Scy
15284990Scy#include "config.h"
16284990Scy
17284990Scy#ifdef __USING_WASM_EXCEPTIONS__
18284990Scy
19284990Scy#include "unwind.h"
20284990Scy#include <threads.h>
21284990Scy
22284990Scy_Unwind_Reason_Code __gxx_personality_wasm0(int version, _Unwind_Action actions,
23284990Scy                                            uint64_t exceptionClass,
24284990Scy                                            _Unwind_Exception *unwind_exception,
25284990Scy                                            _Unwind_Context *context);
26284990Scy
27284990Scystruct _Unwind_LandingPadContext {
28284990Scy  // Input information to personality function
29284990Scy  uintptr_t lpad_index; // landing pad index
30284990Scy  uintptr_t lsda;       // LSDA address
31284990Scy
32284990Scy  // Output information computed by personality function
33284990Scy  uintptr_t selector; // selector value
34284990Scy};
35284990Scy
36284990Scy// Communication channel between compiler-generated user code and personality
37284990Scy// function
38284990Scythread_local struct _Unwind_LandingPadContext __wasm_lpad_context;
39284990Scy
40284990Scy/// Calls to this function is in landing pads in compiler-generated user code.
41284990Scy/// In other EH schemes, stack unwinding is done by libunwind library, which
42284990Scy/// calls the personality function for each each frame it lands. On the other
43284990Scy/// hand, WebAssembly stack unwinding process is performed by a VM, and the
44284990Scy/// personality function cannot be called from there. So the compiler inserts
45284990Scy/// a call to this function in landing pads in the user code, which in turn
46284990Scy/// calls the personality function.
47284990Scy_Unwind_Reason_Code _Unwind_CallPersonality(void *exception_ptr) {
48284990Scy  struct _Unwind_Exception *exception_object =
49284990Scy      (struct _Unwind_Exception *)exception_ptr;
50284990Scy  _LIBUNWIND_TRACE_API("_Unwind_CallPersonality(exception_object=%p)",
51284990Scy                       (void *)exception_object);
52284990Scy
53284990Scy  // Reset the selector.
54284990Scy  __wasm_lpad_context.selector = 0;
55284990Scy
56284990Scy  // Call personality function. Wasm does not have two-phase unwinding, so we
57284990Scy  // only do the cleanup phase.
58284990Scy  return __gxx_personality_wasm0(
59284990Scy      1, _UA_SEARCH_PHASE, exception_object->exception_class, exception_object,
60284990Scy      (struct _Unwind_Context *)&__wasm_lpad_context);
61284990Scy}
62284990Scy
63284990Scy/// Called by __cxa_throw.
64284990Scy_LIBUNWIND_EXPORT _Unwind_Reason_Code
65284990Scy_Unwind_RaiseException(_Unwind_Exception *exception_object) {
66284990Scy  _LIBUNWIND_TRACE_API("_Unwind_RaiseException(exception_object=%p)",
67284990Scy                       (void *)exception_object);
68284990Scy  // Use Wasm EH's 'throw' instruction.
69284990Scy  __builtin_wasm_throw(0, exception_object);
70284990Scy}
71284990Scy
72284990Scy/// Called by __cxa_end_catch.
73284990Scy_LIBUNWIND_EXPORT void
74284990Scy_Unwind_DeleteException(_Unwind_Exception *exception_object) {
75284990Scy  _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
76284990Scy                       (void *)(exception_object));
77284990Scy  if (exception_object->exception_cleanup != NULL)
78284990Scy    (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
79284990Scy                                           exception_object);
80284990Scy}
81284990Scy
82284990Scy/// Called by personality handler to alter register values.
83284990Scy_LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
84284990Scy                                     uintptr_t value) {
85284990Scy  _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, index=%d, value=%lu)",
86284990Scy                       (void *)context, index, value);
87284990Scy  // We only use this function to set __wasm_lpad_context.selector field, which
88284990Scy  // is index 1 in the personality function.
89284990Scy  if (index == 1)
90284990Scy    ((struct _Unwind_LandingPadContext *)context)->selector = value;
91284990Scy}
92284990Scy
93284990Scy/// Called by personality handler to get instruction pointer.
94284990Scy_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
95284990Scy  // The result will be used as an 1-based index after decrementing 1, so we
96284990Scy  // increment 2 here
97284990Scy  uintptr_t result =
98284990Scy      ((struct _Unwind_LandingPadContext *)context)->lpad_index + 2;
99284990Scy  _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => %lu", (void *)context,
100284990Scy                       result);
101284990Scy  return result;
102284990Scy}
103284990Scy
104284990Scy/// Not used in Wasm.
105284990Scy_LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
106284990Scy                                     uintptr_t value) {}
107284990Scy
108284990Scy/// Called by personality handler to get LSDA for current frame.
109284990Scy_LIBUNWIND_EXPORT uintptr_t
110284990Scy_Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
111284990Scy  uintptr_t result = ((struct _Unwind_LandingPadContext *)context)->lsda;
112284990Scy  _LIBUNWIND_TRACE_API("_Unwind_GetLanguageSpecificData(context=%p) => 0x%lx",
113284990Scy                       (void *)context, result);
114284990Scy  return result;
115284990Scy}
116284990Scy
117284990Scy/// Not used in Wasm.
118284990Scy_LIBUNWIND_EXPORT uintptr_t
119284990Scy_Unwind_GetRegionStart(struct _Unwind_Context *context) {
120284990Scy  return 0;
121284990Scy}
122284990Scy
123284990Scy#endif // defined(__USING_WASM_EXCEPTIONS__)
124284990Scy