Skip to content

Spilling can overwrite adjacent stack parameters on ARM64 macOS ABI #67188

Closed

Description

@SingleAccretion

Reproduction:

[MethodImpl(MethodImplOptions.NoInlining)]
private static int Problem(int x0, int x1, int x2, int x3, int x4, int x5, int x6, int x7, short stk0, short stk1)
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static int Call(int k) => 0;

    stk1 = (short)(stk0 + stk1 + stk0);

    Call(stk1);

    return stk0 + stk1;
}

Compile with CG2: --targetarch:arm64 --targetos:OSX --codegenopt NgenDump=Problem --codegenopt JitNoCSE=1 --codegenopt JitStressRegs=2.

Expected result: spilling of the small stack parameters cannot overwrite anything.

Actual result: it can, due to using TYP_INT-wide stores:

IN0014: 000008  B94013A1          ldr     w1, [fp,#16]  // [V08 arg8]
IN0015: 00000C  B84123A2          ldr     w2, [fp,#18]  // [V09 arg9]

...

IN0008: 00002C  B90013A1          str     w1, [fp,#16]

Also see #67152.

Activity

  1. ghost added
    area-CodeGen-coreclr
    on Mar 26, 2022
  2. ghost added
    untriaged
    on Mar 26, 2022
  3. added this to the 7.0.0 milestone on Mar 26, 2022
  4. am11 commented on Mar 26, 2022

    @am11
    Member

    Similar case #66720 (comment)

  5. sandreenko commented on Mar 28, 2022

    @sandreenko
    Contributor

    @AndyAyersMS I can't comment in #52039 (comment) because it is locked but I think you are right.
    The root case is that there are 2 scenarios:

    1. local lives in a register and due to register pressure we need to spill it and we allocate a new stack slot for it. This stack slot always is at least TARGET_POINTER_SIZE wide;
    2. local comes on a stack, gets temporary allocated to a register and then needs to be spilled. We don't need to allocate a new spill stack space because because we can reuse the stack index that it came on. However, on mac arm64 abi this stack space can be less than TARGET_POINTER_SIZE.

    I would try a radical fix first in:

    var_types LclVarDsc::GetActualRegisterType() const
    {
    return genActualType(GetRegisterType());
    }

    instead do:

    var_types LclVarDsc::GetActualRegisterType() const
    {
    #ifdef OSX_ARM64_ABI
       if (lclVar->is argument stack slot) 
       {
           return GetRegisterType();
        }
    #endif
    
        return genActualType(GetRegisterType());
    }
  6. removed
    untriaged
    on Mar 28, 2022
  7. self-assigned this
    on Mar 28, 2022
  8. AndyAyersMS commented on Mar 28, 2022

    @AndyAyersMS
    Member

    I'll fix this since OSR is seeing a related issue.

  9. added a commit that references this issue on Mar 29, 2022
    b10d561
  10. ghost added
    in-pr
    on Mar 29, 2022
  11. added a commit that references this issue on Mar 29, 2022
    7bf9ae2
  12. ghost removed
    in-pr
    on Mar 29, 2022
  13. ghost locked as resolved and limited conversation to collaborators on Apr 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrbug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions