Repository navigation
Closed
Description
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
pavelsavara commentedon Aug 30, 2023
I was able to reproduce, this is jiterp code, assert at
runtime/src/mono/mono/mini/interp/interp.c
Line 2756 in 65b696c
cc @kg
kg commentedon Aug 30, 2023
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.
kg commentedon Aug 30, 2023
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.
SingleAccretion commentedon Aug 30, 2023
Note that if you are using WASM EH, then, if you use
catch <tag index>instead ofcatch_all(at LLVM level this means emitting the@wasm.get.exceptionintrinsic), it won't catch JS exceptions such as the one thrown byexit.kg commentedon Aug 30, 2023
Clang is probably already generating
catch <tag>for mono_llvm_cpp_catch_exception, right?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...
kg commentedon Aug 30, 2023
Oh no :(

kg commentedon Sep 12, 2023
So I've fixed our JIT to no longer use
catch_all, but I've discovered our AOT compiler is generatingcatch_all. The source of them seems to beLLVMBuildCatchRet. Is there documentation anywhere you can point me to that would explain how I could fix our LLVM exception handling to generate tagged catches?vargaz commentedon Sep 12, 2023
No idea. The exception we are throwing internally is thrown by mono_llvm_cpp_throw_exception () and it is an int32*.
SingleAccretion commentedon Sep 12, 2023
@kg LLVM generated for WASM EH should look something like:
Now, this will generate
catch_all(alternatively,cleanuppadinstead of thecatchpadwill also generatecatch_all). To make it generatecatch __cpp_exception(custom are tags not supported), you need to addcall ptr @llvm.wasm.get.exception(token %catchpad)right after thecatchpad.Source: https://github.com/llvm/llvm-project/blob/a1e38e0b8e3e9d02304cf1a578f02a80fdb02bce/llvm/lib/CodeGen/WasmEHPrepare.cpp (you are right that none of this is documented).
kg commentedon Oct 5, 2023
Thanks for the report and fantastic information on the LLVM nuances!