Skip to content

[Mono WASM + AOT] Environment.Exit does not work together with exception handling #90692

Closed

Description

@SingleAccretion

Reproduction:

dotnet new wasmconsole

Program.cs:

Console.WriteLine("Running...");
try
{
    Environment.Exit(100);
}
finally
{
    Console.WriteLine("Oops");
}

Expected (module the missing JS symbols file):

dotnet run ; Will run under interpreter

MONO_WASM: Error loading symbol file dotnet.native.js.symbols: {}
Running...

Actual:

dotnet publish /p:RunAOTCompilation=true -c Release /p:WasmNativeStrip=false

node main.mjs

Running...
Error: [MONO] * Assertion at /__w/1/s/src/mono/mono/mini/interp/interp.c:2756, condition `<disabled>' not met

    at ht (file:///C:/Users/Accretion/source/investigation/BrowserConsole/bin/Release/net8.0/browser-wasm/AppBundle/_framework/dotnet.runtime.js:3:12516)
    at bt (file:///C:/Users/Accretion/source/investigation/BrowserConsole/bin/Release/net8.0/browser-wasm/AppBundle/_framework/dotnet.runtime.js:3:12770)
    at wasm_trace_logger (wasm://wasm/04dd8aba:wasm-function[33399]:0x5ac684)
    at eglib_log_adapter (wasm://wasm/04dd8aba:wasm-function[22319]:0x3d7c26)
    at monoeg_g_logv_nofree (wasm://wasm/04dd8aba:wasm-function[22213]:0x3d3fcc)
    at monoeg_assertion_message (wasm://wasm/04dd8aba:wasm-function[22217]:0x3d40ff)
    at mono_assertion_message (wasm://wasm/04dd8aba:wasm-function[22219]:0x3d4146)
    at mono_assertion_message_disabled (wasm://wasm/04dd8aba:wasm-function[22218]:0x3d4117)
    at do_jit_call (wasm://wasm/04dd8aba:wasm-function[21558]:0x3b10f4)
    at mono_interp_exec_method (wasm://wasm/04dd8aba:wasm-function[21548]:0x3a4de7)
Error: [MONO] * Assertion at /__w/1/s/src/mono/mono/mini/interp/interp.c:2756, condition `<disabled>' not met

    at ht (file:///C:/Users/Accretion/source/investigation/BrowserConsole/bin/Release/net8.0/browser-wasm/AppBundle/_framework/dotnet.runtime.js:3:12516)
    at bt (file:///C:/Users/Accretion/source/investigation/BrowserConsole/bin/Release/net8.0/browser-wasm/AppBundle/_framework/dotnet.runtime.js:3:12770)
    at wasm_trace_logger (wasm://wasm/04dd8aba:wasm-function[33399]:0x5ac684)
    at eglib_log_adapter (wasm://wasm/04dd8aba:wasm-function[22319]:0x3d7c26)
    at monoeg_g_logv_nofree (wasm://wasm/04dd8aba:wasm-function[22213]:0x3d3fcc)
    at monoeg_assertion_message (wasm://wasm/04dd8aba:wasm-function[22217]:0x3d40ff)
    at mono_assertion_message (wasm://wasm/04dd8aba:wasm-function[22219]:0x3d4146)
    at mono_assertion_message_disabled (wasm://wasm/04dd8aba:wasm-function[22218]:0x3d4117)
    at do_jit_call (wasm://wasm/04dd8aba:wasm-function[21558]:0x3b10f4)
    at mono_interp_exec_method (wasm://wasm/04dd8aba:wasm-function[21548]:0x3a4de7)

Cause: I did not confirm this 100%, but highly suspect the cause to be the fact that EH as emitted by the AOT compiler is translated into WASM's catch_all. catch_all catches all exceptions, even JS ones thrown by Emscripten's exit.

Although, curiously enough, a -c Debug publish, seemingly not using WASM EH, also crashes.

Activity

  1. ghost added
    untriaged
    on Aug 16, 2023
  2. removed
    untriaged
    on Aug 17, 2023
  3. added this to the 8.0.0 milestone on Aug 17, 2023
  4. pavelsavara commented on Aug 30, 2023

    @pavelsavara
    Member

    I was able to reproduce, this is jiterp code, assert at

    cc @kg

  5. kg commented on Aug 30, 2023

    @kg
    Contributor

    I was able to reproduce, this is jiterp code, assert at

    cc @kg

    That code was added by 3a9085e. It is correct IMO, it just happens to be wrong for this specific platform due to how WASM EH works. So we probably need to handle this scenario (a native exception was thrown but there's no exception object) in a special way.

    Though I suppose it is possible this could also occur on say x86-64 if an SEH exception is thrown by a pinvoke and we unwind into the interp without wrapping it in a managed exception first.

  6. kg commented on Aug 30, 2023

    @kg
    Contributor

    cc @BrzVlad @vargaz I think the correct fix here would be for the code that traps the exception and sets the 'thrown' flag to notice that a special exception has been caught, and rethrow it instead of trapping it. Does that sound right? I'll need to figure out how we would identify these special exceptions though.

  7. SingleAccretion commented on Aug 30, 2023

    @SingleAccretion
    ContributorAuthor

    Note that if you are using WASM EH, then, if you use catch <tag index> instead of catch_all (at LLVM level this means emitting the @wasm.get.exception intrinsic), it won't catch JS exceptions such as the one thrown by exit.

  8. kg commented on Aug 30, 2023

    @kg
    Contributor

    Clang is probably already generating catch <tag> for mono_llvm_cpp_catch_exception, right?

    void
    mono_llvm_cpp_catch_exception (MonoLLVMInvokeCallback cb, gpointer arg, gboolean *out_thrown)
    {
    	*out_thrown = FALSE;
    	try {
    		cb (arg);
    	} catch (int*) {
    		*out_thrown = TRUE;
    	}
    }

    while the jiterpreter uses catch_all, so it probably needs to figure out which tag is used by clang and use the same one somehow...

  9. kg commented on Aug 30, 2023

    @kg
    Contributor

    Oh no :(
    image

  10. ghost added
    in-pr
    on Aug 30, 2023
  11. kg commented on Sep 12, 2023

    @kg
    Contributor

    Note that if you are using WASM EH, then, if you use catch <tag index> instead of catch_all (at LLVM level this means emitting the @wasm.get.exception intrinsic), it won't catch JS exceptions such as the one thrown by exit.

    So I've fixed our JIT to no longer use catch_all, but I've discovered our AOT compiler is generating catch_all. The source of them seems to be LLVMBuildCatchRet. Is there documentation anywhere you can point me to that would explain how I could fix our LLVM exception handling to generate tagged catches?

  12. vargaz commented on Sep 12, 2023

    @vargaz
    Contributor

    No idea. The exception we are throwing internally is thrown by mono_llvm_cpp_throw_exception () and it is an int32*.

  13. SingleAccretion commented on Sep 12, 2023

    @SingleAccretion
    ContributorAuthor

    @kg LLVM generated for WASM EH should look something like:

      %catchswitch = catchswitch within none [label BB01]
    BB01:
      %catchpad = catchpad within %catchswitch [null]
      <body of the native handler, it calls some helper to handle the exception>

    Now, this will generate catch_all (alternatively, cleanuppad instead of the catchpad will also generate catch_all). To make it generate catch __cpp_exception (custom are tags not supported), you need to add call ptr @llvm.wasm.get.exception(token %catchpad) right after the catchpad.

    Source: https://github.com/llvm/llvm-project/blob/a1e38e0b8e3e9d02304cf1a578f02a80fdb02bce/llvm/lib/CodeGen/WasmEHPrepare.cpp (you are right that none of this is documented).

  14. ghost removed
    in-pr
    on Oct 5, 2023
  15. kg commented on Oct 5, 2023

    @kg
    Contributor

    Thanks for the report and fantastic information on the LLVM nuances!

  16. ghost locked as resolved and limited conversation to collaborators on Nov 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

arch-wasmarea-Codegen-AOT-mono

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions