Skip to content

[mono][interp] memory access out of bounds in string manipulation #94773

Closed

Description

@HSchwichtenberg

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

image

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

  1. transferred this issue fromdotnet/aspnetcoreon Nov 15, 2023
  2. ghost added
    untriaged
    on Nov 15, 2023
  3. javiercn commented on Nov 15, 2023

    @javiercn
    Member

    @lewing based on the description this likely seems like a wasm bug.

    • System.Environment.OSVersion.Platform == PlatformID.Other ? "Browser" : System.Environment.OSVersion
    • Likely something in System.Environment.OSVersion as the claim is that .ToString() addresses the issue
  4. added
    arch-wasm
    on Nov 15, 2023
  5. lambdageek commented on Nov 15, 2023

    @lambdageek
    Member

    The IL for this function without ToString

        public string M() {
            string OSInfo = "";
            OSInfo += (System.Environment.OSVersion.Platform == PlatformID.Other ? "Browser" : System.Environment.OSVersion);
            return OSInfo;
        }

    ends up like this:

    // Methods
        .method public hidebysig 
            instance string M () cil managed 
        {
            .custom instance void System.Runtime.CompilerServices.NullableContextAttribute::.ctor(uint8) = (
                01 00 01 00 00
            )
            // Method begins at RVA 0x209d
            // Code size 48 (0x30)
            .maxstack 8
    
            IL_0000: ldstr ""
            IL_0005: call class [System.Runtime]System.OperatingSystem [System.Runtime]System.Environment::get_OSVersion()
            IL_000a: callvirt instance valuetype [System.Runtime]System.PlatformID [System.Runtime]System.OperatingSystem::get_Platform()
            IL_000f: ldc.i4.7
            IL_0010: beq.s IL_0019
    
            IL_0012: call class [System.Runtime]System.OperatingSystem [System.Runtime]System.Environment::get_OSVersion()
            IL_0017: br.s IL_001e
    
            IL_0019: ldstr "Browser"
    
            IL_001e: dup
            IL_001f: brtrue.s IL_0025
    
            IL_0021: pop
            IL_0022: ldnull
            IL_0023: br.s IL_002a
    
            IL_0025: callvirt instance string [System.Runtime]System.Object::ToString()
    
            IL_002a: call string [System.Runtime]System.String::Concat(string, string)
            IL_002f: ret
        } // end of method C::M

    which has the ldstr on line IL_0019 then dup and then ToString on IL_0025.

    By contrast adding ToString on the else branch

    using System;
    public class C {
        public string M() {
            string OSInfo = "";
            OSInfo += (System.Environment.OSVersion.Platform == PlatformID.Other ? "Browser" : System.Environment.OSVersion.ToString());
            return OSInfo;
        }
    }

    the IL is:

    // Methods
        .method public hidebysig 
            instance string M () cil managed 
        {
            .custom instance void System.Runtime.CompilerServices.NullableContextAttribute::.ctor(uint8) = (
                01 00 01 00 00
            )
            // Method begins at RVA 0x209d
            // Code size 41 (0x29)
            .maxstack 8
    
            IL_0000: ldstr ""
            IL_0005: call class [System.Runtime]System.OperatingSystem [System.Runtime]System.Environment::get_OSVersion()
            IL_000a: callvirt instance valuetype [System.Runtime]System.PlatformID [System.Runtime]System.OperatingSystem::get_Platform()
            IL_000f: ldc.i4.7
            IL_0010: beq.s IL_001e
    
            IL_0012: call class [System.Runtime]System.OperatingSystem [System.Runtime]System.Environment::get_OSVersion()
            IL_0017: callvirt instance string [System.Runtime]System.Object::ToString()
            IL_001c: br.s IL_0023
    
            IL_001e: ldstr "Browser"
    
            IL_0023: call string [System.Runtime]System.String::Concat(string, string)
            IL_0028: ret
        } // end of method C::M
    

    So we do ldstr and then pass that to Concat directly.

    something about that ldstr; dup; brtrue; callvirt ToString() sequence is busted.

  6. lambdageek commented on Nov 16, 2023

    @lambdageek
    Member

    It's an interpreter bug. Not wasm-specific. Repro:

    Program.cs:

    using System.Runtime.CompilerServices;
    
    var osinfo = "";
    
    var b = AlwaysTrue();
    osinfo += (b ? "Mac" : System.Environment.OSVersion);
    
    Console.WriteLine (osinfo);
    
    [MethodImpl(MethodImplOptions.NoInlining)]
    bool AlwaysTrue() => true;

    repro.csproj:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
    
      <PropertyGroup>
          <UseMonoRuntime>true</UseMonoRuntime>
          <SelfContained>true</SelfContained>
      </PropertyGroup>
    
    </Project>

    Run:

    $ MONO_ENV_OPTIONS=--interp dotnet run
    怀艰ʨ妀⪇GĀȁ쀀ʝ쁀ʝ삀ʝ샀ʝ섀ʝ셀ʝ솀ʝ쇀ʝ숀ʝ쉀ʝ슀ʝ싀ʝ쌀ʝ썀ʝ쎀ʝ쏀ʝ쐀ʝ쑀ʝ쒀ʝ쓀ʝ씀ʝ앀ʝ얀ʝ엀ʝ였ʝ와ʝ욀ʝ움ʝ윀ʝ은ʝ잀ʝᲀƋ怀鎘⪇沚ӵ惰⪆鎨⪇Ӷ惰⪆铐⪇ࣈӶ惰⪆ 铠⪇凗ӹ惰⪆(铰⪇ӵ惰⪆0销⪇ㆍӺ惰⪆8锐⪇ଠӸ惰⪆@锠⪇૦Ӹ惰⪆H锰⪇鰡ӵ惰⪆P镀⪇㳐Ӻ惰⪆X镐⪇ભӻ惰⪆`镠⪇잽Ӷ惰⪆h镰⪇꺄ӵ惰⪆p門⪇圑Ӻ惰⪆x閐⪇順Ӻ惰⪆�閠⪇㎞Ӷ惰⪆�閰⪇ꐘӻ惰⪆寘⩠惰⪆Ⴐ⪃ႰႰ寘⩠惰⪆銐⪇闀⪇蝰⪆蝈⪆霨⪇蛸⪆需⪇靐⪇需⪇雘⪇隰⪇隈⪇陠⪇阸⪇阐⪇门⪇Ⴐ寘⩠惰⪆銐⪇醈
    ... [several megabytes of random memory] ...
    =================================================================
    	Native Crash Reporting
    =================================================================
    Got a SIGSEGV while executing native code. This usually indicates
    a fatal error in the mono runtime or one of the native libraries
    used by your application.
    =================================================================
    
    =================================================================
    	Native stacktrace:
    =================================================================
    	0x10319e3d4 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libcoreclr.dylib : mono_dump_native_crash_info
    	0x10314e488 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libcoreclr.dylib : mono_handle_native_crash
    	0x1030b57a4 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libcoreclr.dylib : mono_sigsegv_signal_handler_debug
    	0x18ab7ba24 - /usr/lib/system/libsystem_platform.dylib : _sigtramp
    	0x1031a6a50 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libcoreclr.dylib : mono_interp_exec_method
    	0x10319fb64 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libcoreclr.dylib : interp_runtime_invoke
    	0x1032cd930 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libcoreclr.dylib : mono_runtime_invoke_checked
    	0x1032d4e08 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libcoreclr.dylib : mono_runtime_exec_main_checked
    	0x10310d014 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libcoreclr.dylib : mono_jit_exec
    	0x10310fb14 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libcoreclr.dylib : mono_main
    	0x103183d68 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libcoreclr.dylib : monovm_execute_assembly
    	0x102981ad4 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libhostpolicy.dylib : _Z19run_app_for_contextRK20hostpolicy_context_tiPPKc
    	0x1029828d0 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libhostpolicy.dylib : corehost_main
    	0x1028b2898 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libhostfxr.dylib : _ZN10fx_muxer_t24handle_exec_host_commandERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEERK19host_startup_info_tS8_RKNS0_13unordered_mapI13known_optionsNS0_6vectorIS6_NS4_IS6_EEEE18known_options_hashNS0_8equal_toISD_EENS4_INS0_4pairIKSD
    	0x1028b1b18 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libhostfxr.dylib : _ZN10fx_muxer_t7executeENSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEEiPPKcRK19host_startup_info_tPciPi
    	0x1028ae6bc - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/libhostfxr.dylib : hostfxr_main_startupinfo
    	0x10282d52c - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/ohwow : _Z9exe_startiPPKc
    	0x10282d740 - /private/tmp/ohwow/bin/Debug/net8.0/osx-arm64/ohwow : main
    	0x18a7d10e0 - /usr/lib/dyld : start
    
    =================================================================
    	External Debugger Dump:
    =================================================================
    
    =================================================================
    	Basic Fault Address Reporting
    =================================================================
    Memory around native instruction pointer (0x18ab7b720):0x18ab7b710  21 80 00 91 42 80 00 f1 68 ff ff 54 21 00 02 8b  !...B...h..T!...
    0x18ab7b720  22 0c 40 ac 60 04 00 ad 63 00 02 8b 62 0c 01 ad  ".@.`...c...b...
    0x18ab7b730  c0 03 5f d6 5f 10 40 f1 03 03 00 54 63 80 00 91  [email protected]...
    0x18ab7b740  63 e8 7b 92 22 0c 40 ad 65 00 00 cb 21 00 05 8b  c.{."[email protected]...!...
    
    =================================================================
    	Managed Stacktrace:
    =================================================================
  7. removed
    arch-wasm
    untriaged
    on Nov 16, 2023
  8. added this to the 8.0.x milestone on Nov 16, 2023
  9. lambdageek commented on Nov 16, 2023

    @lambdageek
    Member
  10. changed the title [-]RuntimeError: memory access out of bounds in Blazor WebAssembly 8.0.0[/-] [+][mono][interp] memory access out of bounds in string manipulation[/+] on Nov 16, 2023
  11. ghost added
    in-pr
    on Nov 17, 2023
  12. BrzVlad commented on Nov 17, 2023

    @BrzVlad
    Member

    Thank you for the easy repro. Devirtualization problem. Fix is in PR

  13. ghost removed
    in-pr
    on Nov 19, 2023
  14. locked and limited conversation to collaborators on Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions