Skip to content

Wasmtime C API: Possibility of switching to "abort" panic mode? #5399

Open
@kpreisser

Description

@kpreisser

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. an catch (Exception) or catch (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 catch SEHException:
When a .NET exception occurs in a wasm callback, wasmtime-dotnet will catch the exception (by using an catch (Exception ex) clause to catch all .NET exceptions), and transform it into a wasm_trap_t*, which is then returned at the native wasmtime callback. 1
Now, 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 a catch (Exception) or catch (SEHException) clause, the SEHException will be caught by Function's callback handler and transformed into a wasm_trap_t*, which is then reported at the managed-to-native transition (like wasmtime_call_func) as wasmtime_error_t*, and that is thrown in .NET code as a WasmtimeException.
So even if you just use catch (WasmtimeException ...) e.g. around Function.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 in Environment.FailFast()).
From Rust PR rust-lang/rust#32900, I understand that there is another panic mode (abort) which would have a similar effect to calling RaiseFailFastException.

Quoting @peterhuene from bytecodealliance/wasmtime-dotnet#192 (comment) (please see the conversation there for more background on this in wasmtime-dotnet):

That said, I do think changing the panic mode for the C API to abort would make the most sense to address this issue. Would you mind opening an issue in the Wasmtime repo to explore doing so and reference this conversation?

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

  1. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions