Unwinding through FFI after Rust 1.33

How come? What works 100% of the time is using an error code in FFI, converting from/to each language error handling mechanism. On functions exposed from Rust to FFI you catch panics and return an error code, and in FFI functions called from rust you raise the error code as a panic. On the other side of FFI, e.g., if it is C++, you catch all exceptions in exposed functions and return error codes, and when calling FFI you raise the error codes as exceptions.

Rust does not need to be able to raise / catch C++ exceptions using this method.

In the libjpeg example, where you have Rust -> C -> Rust, you need your extern "C" Rust functions to return an error code, then write a C wrapper for it that raises a SEH, then write a C wrapper that catches the C in the code that calls libjpeg, and then call that wrapper from Rust, so you end up with: Rust -> C libjpeg wrapper -> libpeg -> C wrapper over Rust callback -> Rust callback.

You don't need to be able to trigger SEH from Rust for that to work, and that always works, because you only use the native error reporting mechanism of each language, and via FFI only error codes are propagated.

1 Like