| 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, |
| 23 | uint64_t exceptionClass, |
| 24 | _Unwind_Exception *unwind_exception, |
| 25 | _Unwind_Context *context); |
| 26 | |
| 27 | struct _Unwind_LandingPadContext { |
| 28 | // Input information to personality function |
| 29 | uintptr_t lpad_index; // landing pad index |
| 30 | uintptr_t lsda; // LSDA address |
| 31 | |
| 32 | // Output information computed by personality function |
| 33 | uintptr_t selector; // selector value |
| 34 | }; |
| 35 | |
| 36 | // Communication channel between compiler-generated user code and personality |
| 37 | // function |
| 38 | thread_local struct _Unwind_LandingPadContext __wasm_lpad_context; |
| 39 | |
| 40 | /// Calls to this function are 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 frame it lands. On the other hand, |
| 43 | /// WebAssembly stack unwinding process is performed by a VM, and the |
| 44 | /// personality function cannot be called from there. So the compiler inserts a |
| 45 | /// call to this function in landing pads in the user code, which in turn calls |
| 46 | /// the personality function. |
| 47 | _Unwind_Reason_Code _Unwind_CallPersonality(void *exception_ptr) { |
| 48 | struct _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. |
| 58 | return __gxx_personality_wasm0( |
| 59 | 1, _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 | // Define the `__cpp_exception` symbol which `__builtin_wasm_throw` above will |
| 73 | // reference. This is defined here in `libunwind` as the single canonical |
| 74 | // definition for this API and it's required for users to ensure that there's |
| 75 | // only one copy of `libunwind` within a wasm module to ensure this is only |
| 76 | // defined once and exactly once. |
| 77 | __asm__(".globl __cpp_exception\n" |
| 78 | #if defined(__wasm32__) |
| 79 | ".tagtype __cpp_exception i32\n" |
| 80 | #elif defined(__wasm64__) |
| 81 | ".tagtype __cpp_exception i64\n" |
| 82 | #else |
| 83 | #error "Unsupported Wasm architecture" |
| 84 | #endif |
| 85 | "__cpp_exception:\n" ); |
| 86 | |
| 87 | /// Called by __cxa_end_catch. |
| 88 | _LIBUNWIND_EXPORT void |
| 89 | _Unwind_DeleteException(_Unwind_Exception *exception_object) { |
| 90 | _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)" , |
| 91 | (void *)(exception_object)); |
| 92 | if (exception_object->exception_cleanup != NULL) |
| 93 | (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT, |
| 94 | exception_object); |
| 95 | } |
| 96 | |
| 97 | /// Called by personality handler to alter register values. |
| 98 | _LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index, |
| 99 | uintptr_t value) { |
| 100 | _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, index=%d, value=%lu)" , |
| 101 | (void *)context, index, value); |
| 102 | // We only use this function to set __wasm_lpad_context.selector field, which |
| 103 | // is index 1 in the personality function. |
| 104 | if (index == 1) |
| 105 | ((struct _Unwind_LandingPadContext *)context)->selector = value; |
| 106 | } |
| 107 | |
| 108 | /// Called by personality handler to get instruction pointer. |
| 109 | _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) { |
| 110 | // The result will be used as a 1-based index after decrementing 1, so we |
| 111 | // increment 2 here |
| 112 | uintptr_t result = |
| 113 | ((struct _Unwind_LandingPadContext *)context)->lpad_index + 2; |
| 114 | _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => %lu" , (void *)context, |
| 115 | result); |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | /// Not used in Wasm. |
| 120 | _LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context, |
| 121 | uintptr_t value) {} |
| 122 | |
| 123 | /// Called by personality handler to get LSDA for current frame. |
| 124 | _LIBUNWIND_EXPORT uintptr_t |
| 125 | _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) { |
| 126 | uintptr_t result = ((struct _Unwind_LandingPadContext *)context)->lsda; |
| 127 | _LIBUNWIND_TRACE_API("_Unwind_GetLanguageSpecificData(context=%p) => 0x%lx" , |
| 128 | (void *)context, result); |
| 129 | return result; |
| 130 | } |
| 131 | |
| 132 | /// Not used in Wasm. |
| 133 | _LIBUNWIND_EXPORT uintptr_t |
| 134 | _Unwind_GetRegionStart(struct _Unwind_Context *context) { |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | #endif // defined(__WASM_EXCEPTIONS__) |
| 139 | |