|
9 | 9 | //LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
|
10 | 10 | #![deny(unsafe_op_in_unsafe_fn)]
|
11 | 11 |
|
12 |
| -use cee_scape::SigJmpBufFields; |
| 12 | +#[cfg(not(all( |
| 13 | + any(target_os = "linux", target_os = "macos"), |
| 14 | + any(target_arch = "x86_64", target_arch = "aarch64") |
| 15 | +)))] |
| 16 | +mod cee_scape { |
| 17 | + #[cfg(not(feature = "cshim"))] |
| 18 | + compile_error!("target platform cannot work without feature cshim"); |
| 19 | + |
| 20 | + use libc::{c_int, c_void}; |
| 21 | + use std::marker::PhantomData; |
| 22 | + |
| 23 | + #[repr(C)] |
| 24 | + pub struct SigJmpBufFields { |
| 25 | + _internal: [u8; 0], |
| 26 | + _neither_send_nor_sync: PhantomData<*const u8>, |
| 27 | + } |
| 28 | + |
| 29 | + pub fn call_with_sigsetjmp<F>(savemask: bool, mut callback: F) -> c_int |
| 30 | + where |
| 31 | + F: for<'a> FnOnce(&'a SigJmpBufFields) -> c_int, |
| 32 | + { |
| 33 | + extern "C" { |
| 34 | + fn call_closure_with_sigsetjmp( |
| 35 | + savemask: c_int, |
| 36 | + closure_env_ptr: *mut c_void, |
| 37 | + closure_code: extern "C" fn( |
| 38 | + jbuf: *const SigJmpBufFields, |
| 39 | + env_ptr: *mut c_void, |
| 40 | + ) -> c_int, |
| 41 | + ) -> c_int; |
| 42 | + } |
| 43 | + |
| 44 | + extern "C" fn call_from_c_to_rust<F>( |
| 45 | + jbuf: *const SigJmpBufFields, |
| 46 | + closure_env_ptr: *mut c_void, |
| 47 | + ) -> c_int |
| 48 | + where |
| 49 | + F: for<'a> FnOnce(&'a SigJmpBufFields) -> c_int, |
| 50 | + { |
| 51 | + let closure_env_ptr: *mut F = closure_env_ptr as *mut F; |
| 52 | + unsafe { (closure_env_ptr.read())(&*jbuf) } |
| 53 | + } |
| 54 | + |
| 55 | + let savemask: libc::c_int = if savemask { 1 } else { 0 }; |
| 56 | + |
| 57 | + unsafe { |
| 58 | + let closure_env_ptr = core::ptr::addr_of_mut!(callback); |
| 59 | + core::mem::forget(callback); |
| 60 | + call_closure_with_sigsetjmp( |
| 61 | + savemask, |
| 62 | + closure_env_ptr as *mut libc::c_void, |
| 63 | + call_from_c_to_rust::<F>, |
| 64 | + ) |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +use cee_scape::{call_with_sigsetjmp, SigJmpBufFields}; |
13 | 70 |
|
14 | 71 | /**
|
15 | 72 | Given a closure that is assumed to be a wrapped Postgres `extern "C"` function, [pg_guard_ffi_boundary]
|
@@ -113,7 +170,7 @@ unsafe fn pg_guard_ffi_boundary_impl<T, F: FnOnce() -> T>(f: F) -> T {
|
113 | 170 | let prev_exception_stack = pg_sys::PG_exception_stack;
|
114 | 171 | let prev_error_context_stack = pg_sys::error_context_stack;
|
115 | 172 | let mut result: std::mem::MaybeUninit<T> = MaybeUninit::uninit();
|
116 |
| - let jump_value = cee_scape::call_with_sigsetjmp(false, |jump_buffer| { |
| 173 | + let jump_value = call_with_sigsetjmp(false, |jump_buffer| { |
117 | 174 | // Make Postgres' error-handling system aware of our new
|
118 | 175 | // setjmp/longjmp restore point.
|
119 | 176 | pg_sys::PG_exception_stack = std::mem::transmute(jump_buffer as *const SigJmpBufFields);
|
|
0 commit comments