wasmer_vm/libcalls/eh/
gcc.rs1use std::ffi::c_void;
38
39use libunwind::{self as uw};
40use wasmer_types::TagIndex;
41
42use crate::{InternalStoreHandle, StoreHandle, StoreObjects, VMContext, VMExceptionRef};
43
44use super::dwarf::eh::{self, EHAction, EHContext};
45
46static CANARY: u8 = 0;
50const WASMER_EXCEPTION_CLASS: uw::_Unwind_Exception_Class = u64::from_ne_bytes(*b"WMERWASM");
51
52const CATCH_ALL_TAG_VALUE: i32 = i32::MAX;
53const NO_MATCH_FOUND_TAG_VALUE: i32 = i32::MAX - 1;
57
58#[repr(C)]
59pub struct UwExceptionWrapper {
60 pub _uwe: uw::_Unwind_Exception,
61 pub canary: *const u8,
62 pub exnref: u32,
63
64 pub current_frame_info: Option<Box<CurrentFrameInfo>>,
66}
67
68#[repr(C)]
69pub struct CurrentFrameInfo {
70 pub catch_tags: Vec<u32>,
71 pub has_catch_all: bool,
72}
73
74impl UwExceptionWrapper {
75 pub fn new(exnref: u32) -> Self {
76 Self {
77 _uwe: uw::_Unwind_Exception {
78 exception_class: WASMER_EXCEPTION_CLASS,
79 exception_cleanup: Some(deallocate_exception),
80 private_1: core::ptr::null::<u8>() as usize as _,
81 private_2: 0,
82 },
83 canary: &CANARY,
84 exnref,
85 current_frame_info: None,
86 }
87 }
88}
89
90#[cfg(target_arch = "x86_64")]
91const UNWIND_DATA_REG: (i32, i32) = (0, 1); #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
94const UNWIND_DATA_REG: (i32, i32) = (0, 1); #[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))]
97const UNWIND_DATA_REG: (i32, i32) = (10, 11); #[cfg(target_arch = "loongarch64")]
100const UNWIND_DATA_REG: (i32, i32) = (4, 5); #[unsafe(no_mangle)]
103pub unsafe extern "C" fn wasmer_eh_personality(
109 version: std::ffi::c_int,
110 actions: uw::_Unwind_Action,
111 exception_class: uw::_Unwind_Exception_Class,
112 exception_object: *mut uw::_Unwind_Exception,
113 context: *mut uw::_Unwind_Context,
114) -> uw::_Unwind_Reason_Code {
115 unsafe {
116 if version != 1 {
117 return uw::_Unwind_Reason_Code__URC_FATAL_PHASE1_ERROR;
118 }
119
120 let uw_exc = std::mem::transmute::<*mut uw::_Unwind_Exception, *mut UwExceptionWrapper>(
121 exception_object,
122 );
123
124 if exception_class != WASMER_EXCEPTION_CLASS {
125 return uw::_Unwind_Reason_Code__URC_CONTINUE_UNWIND;
126 }
127
128 let eh_action = match find_eh_action(context) {
129 Ok(action) => action,
130 Err(_) => {
131 return uw::_Unwind_Reason_Code__URC_FATAL_PHASE1_ERROR;
132 }
133 };
134
135 if actions as i32 & uw::_Unwind_Action__UA_SEARCH_PHASE as i32 != 0 {
136 match eh_action {
137 EHAction::None => uw::_Unwind_Reason_Code__URC_CONTINUE_UNWIND,
138 EHAction::CatchAll { .. }
139 | EHAction::CatchSpecific { .. }
140 | EHAction::CatchSpecificOrAll { .. } => uw::_Unwind_Reason_Code__URC_HANDLER_FOUND,
141 EHAction::Terminate => uw::_Unwind_Reason_Code__URC_FATAL_PHASE1_ERROR,
142 }
143 } else {
144 let has_catch_all = matches!(eh_action, EHAction::CatchSpecificOrAll { .. });
147
148 match eh_action {
149 EHAction::None => uw::_Unwind_Reason_Code__URC_CONTINUE_UNWIND,
150 EHAction::CatchAll { lpad } => {
151 uw::_Unwind_SetGR(context, UNWIND_DATA_REG.0, uw_exc as _);
152 uw::_Unwind_SetGR(context, UNWIND_DATA_REG.1, 0);
154 uw::_Unwind_SetIP(context, lpad as usize as _);
155 uw::_Unwind_Reason_Code__URC_INSTALL_CONTEXT
156 }
157 EHAction::CatchSpecific { lpad, tags }
158 | EHAction::CatchSpecificOrAll { lpad, tags } => {
159 (*uw_exc).current_frame_info = Some(Box::new(CurrentFrameInfo {
160 catch_tags: tags,
161 has_catch_all,
162 }));
163 uw::_Unwind_SetGR(context, UNWIND_DATA_REG.0, uw_exc as _);
164 uw::_Unwind_SetGR(context, UNWIND_DATA_REG.1, 1);
166 uw::_Unwind_SetIP(context, lpad as usize as _);
167 uw::_Unwind_Reason_Code__URC_INSTALL_CONTEXT
168 }
169 EHAction::Terminate => uw::_Unwind_Reason_Code__URC_FATAL_PHASE2_ERROR,
170 }
171 }
172 }
173}
174
175#[unsafe(no_mangle)]
176pub unsafe extern "C" fn wasmer_eh_personality2(
183 vmctx: *mut VMContext,
184 exception_object: *mut UwExceptionWrapper,
185) -> i32 {
186 unsafe {
187 let Some(current_frame_info) = (*exception_object).current_frame_info.take() else {
188 unreachable!("wasmer_eh_personality2 called without current_frame_info");
190 };
191
192 let instance = (*vmctx).instance();
193 let exn = super::exn_obj_from_exnref(vmctx, (*exception_object).exnref);
194
195 for tag in current_frame_info.catch_tags {
196 let unique_tag = instance.shared_tag_ptr(TagIndex::from_u32(tag)).index();
197 if unique_tag == (*exn).tag_index() {
198 return tag as i32;
199 }
200 }
201
202 if current_frame_info.has_catch_all {
203 CATCH_ALL_TAG_VALUE
204 } else {
205 NO_MATCH_FOUND_TAG_VALUE
206 }
207 }
208}
209
210unsafe fn find_eh_action(context: *mut uw::_Unwind_Context) -> Result<EHAction, ()> {
211 unsafe {
212 let lsda = uw::_Unwind_GetLanguageSpecificData(context) as *const u8;
213 let mut ip_before_instr: std::ffi::c_int = 0;
214 let ip = uw::_Unwind_GetIPInfo(context, &mut ip_before_instr);
215 let eh_context = EHContext {
216 ip: if ip_before_instr != 0 {
221 ip as _
222 } else {
223 ip.wrapping_sub(1) as _
224 },
225 func_start: uw::_Unwind_GetRegionStart(context) as *const _,
226 get_text_start: &|| uw::_Unwind_GetTextRelBase(context) as *const _,
227 get_data_start: &|| uw::_Unwind_GetDataRelBase(context) as *const _,
228 };
229 eh::find_eh_action(lsda, &eh_context)
230 }
231}
232
233pub unsafe fn read_exnref(exception: *mut c_void) -> u32 {
234 if exception.is_null() {
235 0
236 } else {
237 unsafe { (*(exception as *mut UwExceptionWrapper)).exnref }
238 }
239}
240
241pub unsafe fn throw(ctx: &StoreObjects, exnref: u32) -> ! {
245 unsafe {
246 if exnref == 0 {
247 crate::raise_lib_trap(crate::Trap::lib(
248 wasmer_types::TrapCode::UninitializedExnRef,
249 ))
250 }
251
252 let exception = Box::new(UwExceptionWrapper::new(exnref));
253 let exception_ptr = Box::into_raw(exception);
254
255 match uw::_Unwind_RaiseException(exception_ptr as *mut libunwind::_Unwind_Exception) {
256 libunwind::_Unwind_Reason_Code__URC_END_OF_STACK => {
257 delete_exception(exception_ptr as *mut c_void);
258
259 let exnref = VMExceptionRef(StoreHandle::from_internal(
260 ctx.id(),
261 InternalStoreHandle::from_index(exnref as usize).unwrap(),
262 ));
263 crate::raise_lib_trap(crate::Trap::uncaught_exception(exnref, ctx))
264 }
265 _ => {
266 unreachable!()
267 }
268 }
269 }
270}
271
272pub unsafe fn delete_exception(exception: *mut c_void) {
273 unsafe {
274 if !exception.is_null() {
275 uw::_Unwind_DeleteException(exception as *mut uw::_Unwind_Exception);
276 }
277 }
278}
279
280unsafe extern "C" fn deallocate_exception(
281 _: uw::_Unwind_Reason_Code,
282 exception: *mut uw::_Unwind_Exception,
283) {
284 unsafe {
285 let exception = Box::from_raw(exception as *mut UwExceptionWrapper);
286 drop(exception);
287 }
288}