added panic_across_ffi example demonstrating panic crossing FFI boundary - #39
Conversation
|
The CI failure in the "Run Rust and C++ examples" step was due to the intentional abort caused by the unsafe example (trigger_panic), where a Rust panic occurs inside an extern "C" function. |
|
Hi, I've already looked at your PR #24, we're just going to focus on one PR per person for now. After that PR is finished, we can decide what to do with this one. |
teor2345
left a comment
There was a problem hiding this comment.
This is a good example, it just needs tidying up
Since we know the process is going to abort, we should require it to fail during testing
teor2345
left a comment
There was a problem hiding this comment.
Thanks, this is a good example!
I've made some minor cleanups on your branch, and improved test coverage.
Sorry it's taken me a while to get back to this, I didn't get any notifications when it was updated.
Summary
This PR adds a new interop example:
panic_across_ffi.It demonstrates the undefined behavior that occurs when a Rust panic crosses an FFI boundary into C++, and shows the correct way to handle such cases safely.
What This Example Demonstrates
This example includes two scenarios:
Unsafe case (
trigger_panic)A Rust function marked
extern "C"panicsThe panic attempts to unwind across the FFI boundary
Rust aborts the program with:
This represents undefined behavior and must be avoided
Safe case (
trigger_panic_safe)std::panic::catch_unwindWhy This Matters
While FFI safety often focuses on layout compatibility, error handling across language boundaries is equally critical.
Rust and C++ have different exception/unwinding models:
Crossing these boundaries incorrectly can lead to:
How to Run
cd examples/panic_across_ffi ./run.shExpected Behavior
This behavior is intentional and demonstrates the problem clearly.
Notes
CI Compliance
run.shfor building and running the examplecargo build,cargo test,cargo clippy,cargo doc, andcargo fmtRelated Problem Area