-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wasmtime C API: Possibility of switching to "abort" panic mode? #5399
Comments
Using |
Hi @bjorn3, thanks for your reply!
If I understand you correctly, that should already be the case in Thanks! |
I agree that changing to |
To my understanding, this is not the case. A panic is a sign that whatever you tried to do was incorrect, and that logical invariants may not be upheld any more. However, |
I'm not sure if this is the right issue, but I'm using the C API and would like to handle panics. I'm seeing cases where there are panics if there is no memory left to allocate memory:
I'd like to handle that and not crash the process. Would patches be accepted to handle this specific case in wasmtime/crates/c-api/src/module.rs Lines 32 to 40 in c4f261a
|
Ah for that case specifically the panic is this call to |
Hi!
(Please note that I don't know much about Rust, so please correct me if I'm saying anything that doesn't make sense.)
As far as I understand panics in Rust, they indicate that an unrecoverable/serious error has occured, after which the process should be terminated (to not run into undefined behavior).
In bytecodealliance/wasmtime-dotnet#192, we found that on Windows, when a panic occurs in the Wasmtime C API, it will raise an SEH Exception (e.g. with Win32's
RaiseException
or_CxxThrowException
from the MSVC runtime).However, because .NET appears to use the same mechanism to handle exceptions, such a panic will surface as
SEHException
in a .NET application on the managed-to-native transition, which can be caught by the user if they have e.g. ancatch (Exception)
orcatch (SEHException)
clause. This means that in such a case, the process will not actually terminate, but will can continue to run, which could be problematic because we may now have undefined behavior with possible security implications.This can even happen in
wasmtime-dotnet
if user code actually doesn't intend to catchSEHException
:When a .NET exception occurs in a wasm callback,
wasmtime-dotnet
will catch the exception (by using ancatch (Exception ex)
clause to catch all .NET exceptions), and transform it into awasm_trap_t*
, which is then returned at the native wasmtime callback. 1Now, imagine there is a host-to-wasm and a wasm-to-host transition on the stack, and you call a wasmtime function that panics, resulting in a
SEHException
on Windows on the managed-to-native transition. Even if the user code on top of the wasm-to-host transition doesn't have acatch (Exception)
orcatch (SEHException)
clause, theSEHException
will be caught byFunction
's callback handler and transformed into awasm_trap_t*
, which is then reported at the managed-to-native transition (likewasmtime_call_func
) aswasmtime_error_t*
, and that is thrown in .NET code as aWasmtimeException
.So even if you just use
catch (WasmtimeException ...)
e.g. aroundFunction.Invoke()
(as it is expected that such an exception may be thrown here), it can happen that you catch a Rust panic that was actually intended to terminate the process.On Windows there is an alternative function
RaiseFailFastException
, which bypasses all exception handlers and ensures the process is terminated (I assume this is also e.g. what .NET internally does inEnvironment.FailFast()
).From Rust PR rust-lang/rust#32900, I understand that there is another panic mode (
abort
) which would have a similar effect to callingRaiseFailFastException
.Quoting @peterhuene from bytecodealliance/wasmtime-dotnet#192 (comment) (please see the conversation there for more background on this in
wasmtime-dotnet
):Would it be possible to change the panic mode for the C API to
abort
, to ensure on Windows the process is terminated when a panic occurs?(Note that e.g. on Linux, this is what already happens in .NET applications using
wasmtime-dotnet
, since the CLR cannot catch the panic there.)Thank you!
Footnotes
This ensures we don't let any .NET exception bubble through the native-to-managed transition. If
wasmtime-dotnet
wouldn't catch an exception thrown in .NET code, on Windows, the .NET CLR would unwind the stack up to the next .NET exception handler (that catches this exception type) even if there are managed-to-native and native-to-managed transitions on the stack, which seems to be incompatible with Wasmtime - see Undefined behavior when callback exception handler throws another exception wasmtime-dotnet#187 for an example. ↩The text was updated successfully, but these errors were encountered: