Skip to content

WASM handling of optimized stackalloc is incorrect #131557

Closed

Description

@davidwrighton

Description

When compiling the checked System.Private.CoreLib, the Interop.Sys.GetCwd() method has a stackalloc which is improperly handled. Notably, after it does the stackalloc, calls to the Interop.Sys.GetCwd(byte* buffer, int bufferLength) and other functions do not actually call the method with the updated stack pointer, instead they use the stack pointer computed in the prolog. This causes memory corruption in the subsequent logic which converts the result into a System.String object.

Reproduction Steps

This is known to repro in the checked build of System.Private.CoreLib.

Expected behavior

After stackallocating, all subsequent calls should pass the updated stack pointer down.

Actual behavior

Stack pointer in called functions is in the middle of the GetCwd stack frame, causing memory corruption.

Regression?

No response

Known Workarounds

No response

Configuration

WASM. Checked build of System.Private.CoreLib.

Other information

No response

Activity

  1. added
    untriaged
    on Jul 29, 2026
  2. added
    area-CodeGen-coreclr
    on Jul 29, 2026
  3. dotnet-policy-service commented on Jul 29, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. davidwrighton commented on Jul 29, 2026

    @davidwrighton
    MemberAuthor
    (func $S_P_CoreLib_Interop_Sys__GetCwd_0 (;68;) (export "S_P_CoreLib_Interop_Sys__GetCwd_0") (param $var0 i32) (param $var1 i32) (result i32)
        (local $var2 i32)
        (local $var3 i32)
        (local $var4 i32)
        (local $var5 i32)
        (local $var6 i32)
        (local $var7 i32)
        (local $var8 i32)
        (local $var9 i32)
        (local $var10 i32)
        (local $var11 i32)
        (local $var12 i32)
        (local $var13 i32)
        (local $var14 i32)
        (local $var15 i32)
        (local $var16 i32)
        (local $var17 i64)
        global.get $webcil.imageBase
        local.set $var16
        local.get $var0
        i32.const 64
        i32.sub
        local.set $var0
        local.get $var0
        local.set $var2
        local.get $var2
        global.get $webcil.tableBase
        i32.const 68
        i32.add
        i32.store align=1
        local.get $var2
        i32.const 12
        i32.add
        i32.const 0
        i32.const 48
        memory.fill
        block $label1
          block $label2
            block $label0
              local.get $var2 // THIS IS WHAT IS BEING PASSED AS THE STACK POINTER TO THE OTHER GetCwd ... NOTE THAT THE STACKALLOC HAPPENS LATER
              i32.const 4
              i32.add
              i32.const 1
              i32.store align=1
              local.get $var2
              i32.const 32
              i32.add
              local.get $var0
              local.get $var0
              i32.const 272
              i32.sub
              local.set $var0 // STACK ALLOC HAPPENS HERE
              local.get $var0
              local.get $var2
              i32.store offset=4 align=1
              local.get $var0
              i32.const 0
              i32.store align=1
              local.get $var0
              i32.const 16
              i32.add
              i32.const 256
              local.get $var16
              i32.load offset=6981532 align=1
              local.set $var11
              local.get $var11
              local.get $var11
              i32.load align=1
              call_indirect (param i32 i32 i32 i32) (result i32)
    
  5. self-assigned this
    on Jul 29, 2026
  6. removed
    untriaged
    on Jul 29, 2026
  7. AndyAyersMS commented on Jul 29, 2026

    @AndyAyersMS
    Member

    Looks like we need to force the stackalloc eval earlier, if the stackalloc is a call arg, otherwise we will push the old SP, not the new SP.

  8. added this to the 11.0.0 milestone on Jul 30, 2026
  9. added
    arch-wasm
    on Jul 30, 2026
  10. dotnet-policy-service commented on Jul 30, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
    See info in area-owners.md if you want to be subscribed.

  11. added
    Priority:1
    on Jul 30, 2026
  12. added a commit that references this issue on Jul 30, 2026
    bdfac9a
  13. locked and limited conversation to collaborators on Aug 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Priority:1arch-wasmarea-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions