Stream: cranelift

Topic: Unwinding Rust -> Cranelift -> Rust


view this post on Zulip Adrian Willenbücher (Feb 13 2025 at 16:00):

I have a question about unwinding:
I'm JIT-compiling code (directly with frontend and codegen, without using jit and module) and the generated code calls a Rust-defined function in the current process. This function throws a panic, and the panic is supposed to be caught by the Rust code calling the JIT-compiled code. Roughly like this:

extern "C-unwind" fn called_from_jit_code() {
    panic!("in Rust code");
}

...
let addr_value = builder.ins().iconst(I64, called_from_jit_code as i64);
builder.ins().call_indirect(sig_ref, addr_value, &[]);
...

std::panic::catch_unwind(|| {
    jit_compiled_function();
});

Is this possible with the unwind info created by Cranelift?

view this post on Zulip Adrian Willenbücher (Feb 13 2025 at 16:12):

Nothing needs to be cleaned up within the JIT-compiled code, I just want to pass the panic through to the surrounding Rust code.

view this post on Zulip Chris Fallin (Feb 13 2025 at 16:17):

We do emit unwind info (you will need to make sure you're registering it properly when you load/publish the JIT code). So in that sense anything that works with a Rust->C++->Rust stack (for example) should work with CL-compiled code in the middle as well, I think

view this post on Zulip Chris Fallin (Feb 13 2025 at 16:17):

(modulo unwind-info bugs; if you find issues please do let us know)

view this post on Zulip bjorn3 (Feb 13 2025 at 20:43):

What isn't supported yet however is catching exceptions or handling cleanup (running destructors, etc) for exceptions in Cranelift compilrd functions.


Last updated: Feb 27 2025 at 23:03 UTC