Skip to content

RyuJIT: Missing zeroing of upper bits for small struct used in Volatile.Read #10941

Closed
dotnet/coreclr
#20843

Description

@jakobbotsch

The example is:

// Debug: Outputs 128 (0x80)
// Release: Outputs 4294967168 (0xFFFFFF80)
struct S0
{
    public byte F0;
}

public class Program
{
    static S0 s_2;
    static long s_5;
    public static void Main()
    {
        s_2.F0 = 128;
        M7(s_2);
        System.Console.WriteLine(s_5);
    }

    static void M7(S0 arg0)
    {
        s_5 = System.Threading.Volatile.Read(ref arg0.F0);
    }
}

It produces for the call in Main:

       48B978298897D6010000 mov      rcx, 0x1D697882978
       488B09               mov      rcx, gword ptr [rcx]
       C6410880             mov      byte  ptr [rcx+8], 128
       48B978298897D6010000 mov      rcx, 0x1D697882978
       488B09               mov      rcx, gword ptr [rcx]
       480FBE4908           movsx    rcx, byte  ptr [rcx+8]   ; sign extended
       E8BFFBFFFF           call     Program:M7(struct)

and for M7:

; Assembly listing for method Program:M7(struct)
; Emitting BLENDED_CODE for X64 CPU with AVX
; optimized code
; rsp based frame
; partially interruptible
; Final local variable assignments
;
;* V00 arg0         [V00    ] (  0,  0   )  struct ( 8) zero-ref    ld-addr-op
;# V01 OutArgs      [V01    ] (  1,  1   )  lclBlk ( 0) [rsp+0x00]
;  V02 tmp1         [V02,T00] (  2,  2   )   ubyte  ->  rcx         V00.F0(offs=0x00) P-INDEP
;
; Lcl frame size = 0

G_M23190_IG01:
       0F1F440000           nop

G_M23190_IG02:
       8BC1                 mov      eax, ecx
       4889054A2AEEFF       mov      qword ptr [reloc classVar[0x4bc55298]], rax

G_M23190_IG03:
       C3                   ret

; Total bytes of code 15, prolog size 5 for method Program:M7(struct)

Activity

  1. jakobbotsch commented on Aug 22, 2018

    @jakobbotsch
    MemberAuthor

    I initially thought the problem was that the call in Main sign extends the argument instead of zero extending it, but changing M7 to:

    static void M7(S0 arg0)
    {
        s_5 = arg0.F0;
    }

    gives instead:

    G_M23189_IG01:
           0F1F440000           nop
    
    G_M23189_IG02:
           0FB6C1               movzx    rax, cl
           8BC0                 mov      eax, eax
           488905472AEEFF       mov      qword ptr [reloc classVar[0x3f395298]], rax
    
    G_M23189_IG03:
           C3                   ret

    i.e. the callee does not make any assumptions about the upper bits in this case.
    As an aside is there any documentation on the ABI used in the JIT for cases like this? In other words, is the caller or callee right here?

  2. mikedn commented on Aug 22, 2018

    @mikedn
    Contributor

    Probably another normalize on load mess up:

    ;* V00 arg0         [V00    ] (  0,  0   )  struct ( 8) zero-ref    ld-addr-op
    ;# V01 OutArgs      [V01    ] (  1,  1   )  lclBlk ( 0) [rsp+0x00]
    ;  V02 tmp1         [V02,T00] (  2,  2   )   ubyte  ->  rcx         V00.F0(offs=0x00) P-INDEP
    

    You can see that the arg0 argument was promoted and its promoted ubyte field is V02. Since it's a small type struct field vNormalizeOnLoad() should return true. And it very likely does that but probably morph does not insert the necessary zero extending cast. Or something else removes the cast.

  3. mikedn commented on Aug 22, 2018

    @mikedn
    Contributor

    Heh, GT_ADDR crap.

    • LclVar nodes that are used by GT_ADDR are marked with GTF_DONT_CSE cause their meaning is completely different in address context
    • Then there's the code that folds IND(ADDR(LCLVAR)) to LCLVAR. This one removes GTF_DONT_CSE but copies it from the indir, which happens to have it set. Haven't checked why, probably because it's volatile. Doesn't really matter, the fact is that it can happen.
    • The resulting Lclvar node is re-morphed, that should take care of adding the normalize on load cast. Except that it does not because:
        bool varAddr = (tree->gtFlags & GTF_DONT_CSE) != 0;

    That is, it assumes that GTF_DONT_CSE on a LclVar implies that it's in address context and of course, then the cast is not needed.

    No comment.

  4. mikedn commented on Sep 8, 2018

    @mikedn
    Contributor

    Ideally we'd get rid of GT_ADDR (#19875). That may take a while though so perhaps we need some simple fix for this. If such a fix exists.

  5. transferred this issue fromdotnet/coreclron Jan 31, 2020
  6. added this to the 3.0 milestone on Jan 31, 2020
  7. ghost locked as resolved and limited conversation to collaborators on Dec 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrbug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions