Repository navigation
Description
This is a general issue with the Mono interpreter in .NET 8, see #94773 (comment) for generic console repro.
Original report below
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
This Razor Component
@page "/BugRepo"
<h3>BUG</h3>
<button @onclick="GetOS">Get OS Info</button>
@OSInfo
@code
{
string OSInfo = "";
void GetOS()
{
OSInfo += (System.Environment.OSVersion.Platform == PlatformID.Other ? "Browser" : System.Environment.OSVersion);
}
}
either hangs the browser or shows this error in the browser console:
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: RuntimeError: memory access out of bounds
RuntimeError: memory access out of bounds
Expected Behavior
Should display "Browser"
Steps To Reproduce
- Create a "Blazor Web App"
- Interactivity "WebAssembly"
- Location "Global"
- Add the Razor Component
@page "/BugRepo"
<h3>BUG</h3>
<button @onclick="GetOS">Get OS Info</button>
@OSInfo
@code
{
string OSInfo = "";
void GetOS()
{
OSInfo += (System.Environment.OSVersion.Platform == PlatformID.Other ? "Browser" : System.Environment.OSVersion);
}
}
- Run with URL: /BugRepo
Exceptions (if any)
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: RuntimeError: memory access out of bounds
RuntimeError: memory access out of bounds
Ft @ blazor.webassembly.js:1
(anonymous) @ invoke-js.ts:176
Ul @ invoke-js.ts:276
$func2515 @ 00d7d9b2:0xb9f73
$func2411 @ 00d7d9b2:0xb640c
$func2404 @ 00d7d9b2:0xa95f3
$func2438 @ 00d7d9b2:0xb7631
$func5304 @ 00d7d9b2:0x16c4b8
$func4611 @ 00d7d9b2:0x142542
$func4610 @ 00d7d9b2:0x1424d2
$func4022 @ 00d7d9b2:0x11ec5a
$func2515 @ 00d7d9b2:0xb9ff7
$func2411 @ 00d7d9b2:0xb640c
$func2404 @ 00d7d9b2:0xa95f3
$func2438 @ 00d7d9b2:0xb7631
$func5304 @ 00d7d9b2:0x16c4b8
$func4611 @ 00d7d9b2:0x142542
$func4617 @ 00d7d9b2:0x142d04
$func4641 @ 00d7d9b2:0x145357
$mono_wasm_invoke_method_bound @ 00d7d9b2:0x28d8b9
Module._mono_wasm_invoke_method_bound @ dotnet.native.8.0.0.sk1srs0wup.js:8
kr @ invoke-cs.ts:273
(anonymous) @ invoke-cs.ts:247
beginInvokeDotNetFromJS @ blazor.webassembly.js:1
invokeDotNetMethodAsync @ blazor.webassembly.js:1
invokeMethodAsync @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
N @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
invokeWhenHeapUnlocked @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
N @ blazor.webassembly.js:1
C @ blazor.webassembly.js:1
dispatchGlobalEventToAllElements @ blazor.webassembly.js:1
onGlobalEvent @ blazor.webassembly.js:1
.NET Version
8.0.100
Anything else?
Testet in latest versions of Chrome, Edge and Firefox
It can be fixed by adding ToString()
OSInfo += (System.Environment.OSVersion.Platform == PlatformID.Other ? "Browser" : System.Environment.OSVersion.ToString());
However, forgetting ToString() should either produce an error at compile time or runtime. But not crash the app in this way.

Activity
javiercn commentedon Nov 15, 2023
@lewing based on the description this likely seems like a wasm bug.
System.Environment.OSVersion.Platform == PlatformID.Other ? "Browser" : System.Environment.OSVersionSystem.Environment.OSVersionas the claim is that.ToString()addresses the issuelambdageek commentedon Nov 15, 2023
The IL for this function without
ToStringends up like this:
which has the
ldstron lineIL_0019thendupand thenToStringonIL_0025.By contrast adding
ToStringon the else branchthe IL is:
So we do
ldstrand then pass that toConcatdirectly.something about that
ldstr; dup; brtrue; callvirt ToString()sequence is busted.lambdageek commentedon Nov 16, 2023
It's an interpreter bug. Not wasm-specific. Repro:
Program.cs:repro.csproj:Run:
lambdageek commentedon Nov 16, 2023
/cc @SamMonoRT @BrzVlad
[-]RuntimeError: memory access out of bounds in Blazor WebAssembly 8.0.0[/-][+][mono][interp] memory access out of bounds in string manipulation[/+]BrzVlad commentedon Nov 17, 2023
Thank you for the easy repro. Devirtualization problem. Fix is in PR