Repository navigation
Closed
dotnet/coreclr
#21184Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIbug
Milestone
Description
For ARM32, the example is:
interface IRT { void WriteLine<T>(T val); }
class CRT : IRT { public void WriteLine<T>(T val) => System.Console.WriteLine(val); }
public class Program
{
static IRT s_rt;
static byte[] s_1 = new byte[] { 0 };
static int s_3;
static ushort[] s_8 = new ushort[] { 1 };
public static void Main()
{
s_rt = new CRT();
M11(s_8, 0, 0, 0, true, s_1);
}
static ushort M11(ushort[] arg0, ushort arg1, ushort arg3, byte arg4, bool arg7, byte[] arg10)
{
if (arg7)
{
ulong var4 = (ulong)s_3;
arg3 = s_8[0];
ushort var5 = arg3;
s_rt.WriteLine(var4);
s_rt.WriteLine((int)var5); // prints large negative value on my machine
}
if (!arg7)
{
var vr7 = arg0[0];
}
arg10[0] = arg4;
return arg1;
}
}This does not repro on x64. With AltJit I get the following disassembly, which shows (int)var5 loading the argument as 32 bits:
; Assembly listing for method Program:M11(ref,ushort,ushort,ubyte,bool,ref):ushort
; Emitting BLENDED_CODE for generic ARM CPU
; optimized code
; r11 based frame
; partially interruptible
; Final local variable assignments
;
; V00 arg0 [V00,T00] ( 5, 3.50) ref -> r4 class-hnd
; V01 arg1 [V01,T05] ( 3, 3 ) ushort -> r7
; V02 arg2 [V02,T04] ( 5, 3.50) ushort -> [sp+0x0C]
; V03 arg3 [V03,T03] ( 4, 4 ) ubyte -> r5
; V04 arg4 [V04,T10] ( 2, 2 ) bool -> r0
; V05 arg5 [V05,T06] ( 4, 4 ) ref -> r8 class-hnd
;* V06 loc0 [V06 ] ( 0, 0 ) long -> zero-ref
;* V07 loc1 [V07 ] ( 0, 0 ) ushort -> zero-ref
;# V08 OutArgs [V08 ] ( 1, 1 ) lclBlk ( 0) [sp+0x00]
; V09 tmp1 [V09,T07] ( 3, 3 ) ref -> [sp+0x00]
; V10 tmp2 [V10,T11] ( 2, 2 ) int -> [sp+0x08]
; V11 tmp3 [V11,T08] ( 3, 3 ) ref -> r6
; V12 tmp4 [V12,T12] ( 2, 2 ) int -> [sp+0x04]
; V13 tmp5 [V13,T01] ( 6, 6 ) ref -> r0
; V14 cse0 [V14,T02] ( 6, 6 ) int -> r9
; V15 rat0 [V15,T13] ( 2, 1 ) int -> r6 V06.lo(offs=0x00)
; V16 rat1 [V16,T14] ( 2, 1 ) int -> r10 V06.hi(offs=0x04)
; V17 rat2 [V17,T09] ( 3, 3 ) int -> r0
;
; Lcl frame size = 20
G_M58754_IG01:
000000 E92D 4FF0 push {r4,r5,r6,r7,r8,r9,r10,r11,lr}
000004 B085 sub sp, 20
000006 F10D 0B30 add r11, sp, 48
00000A 4604 mov r4, r0
00000C 460F mov r7, r1
00000E 4616 mov r6, r2
000010 461D mov r5, r3
000012 980E ldr r0, [sp+0x38] // [V04 arg4]
000014 F8DD 803C ldr r8, [sp+0x3c] // [V05 arg5]
G_M58754_IG02:
000018 FA5F F980 uxtb r9, r0
00001C F1B9 0F00 cmp r9, 0
000020 D047 beq SHORT G_M58754_IG03
000022 F244 008C movw r0, 0x408c
000026 F2C0 00EF movt r0, 0xef
00002A 6800 ldr r0, [r0]
00002C EA4F 7AE0 asr r10, r0, 31
000030 4606 mov r6, r0
000032 F641 40B4 movw r0, 0x1cb4
000036 F2C0 6024 movt r0, 0x624
00003A 6800 ldr r0, [r0]
00003C 2100 movs r1, 0
00003E 6842 ldr r2, [r0+4]
000040 4291 cmp r1, r2
000042 D248 bhs SHORT G_M58754_IG06
000044 8900 ldrh r0, [r0+8]
000046 F8AD 000C strh r0, [sp+0x0c] // [V02 arg2] <------------------ store as 16 bits
00004A F641 40AC movw r0, 0x1cac
00004E F2C0 6024 movt r0, 0x624
000052 F8D0 E000 ldr lr, [r0]
000056 F8CD E000 str lr, [sp] // [V09 tmp1]
00005A 4670 mov r0, lr
00005C F644 1194 movw r1, 0x4994
000060 F2C0 01EF movt r1, 0xef
000064 F644 3210 movw r2, 0x4b10
000068 F2C0 02EF movt r2, 0xef
00006C F245 5CA0 movw r12, 0x55a0
000070 F6C0 7CCB movt r12, 0xfcb
000074 47E0 blx r12 // CORINFO_HELP_VIRTUAL_FUNC_PTR
000076 9002 str r0, [sp+0x08] // [V10 tmp2]
000078 9800 ldr r0, [sp] // [V09 tmp1]
00007A 4632 mov r2, r6
00007C 4653 mov r3, r10
00007E 9902 ldr r1, [sp+0x08] // [V10 tmp2]
000080 4788 blx r1
000082 F641 40AC movw r0, 0x1cac
000086 F2C0 6024 movt r0, 0x624
00008A 6806 ldr r6, [r0]
00008C 4630 mov r0, r6
00008E F644 1194 movw r1, 0x4994
000092 F2C0 01EF movt r1, 0xef
000096 F644 3274 movw r2, 0x4b74
00009A F2C0 02EF movt r2, 0xef
00009E F245 53A0 movw r3, 0x55a0
0000A2 F6C0 73CB movt r3, 0xfcb
0000A6 4798 blx r3 // CORINFO_HELP_VIRTUAL_FUNC_PTR
0000A8 9001 str r0, [sp+0x04] // [V12 tmp4]
0000AA 4630 mov r0, r6
0000AC 9903 ldr r1, [sp+0x0c] // [V02 arg2] <------------------ load as 32 bits
0000AE 9B01 ldr r3, [sp+0x04] // [V12 tmp4]
0000B0 4798 blx r3
G_M58754_IG03:
0000B2 F1B9 0F00 cmp r9, 0
0000B6 D103 bne SHORT G_M58754_IG04
0000B8 2000 movs r0, 0
0000BA 6863 ldr r3, [r4+4]
0000BC 4298 cmp r0, r3
0000BE D20A bhs SHORT G_M58754_IG06
G_M58754_IG04:
0000C0 2000 movs r0, 0
0000C2 F8D8 3004 ldr r3, [r8+4]
0000C6 4298 cmp r0, r3
0000C8 D205 bhs SHORT G_M58754_IG06
0000CA F888 5008 strb r5, [r8+8]
0000CE B2B8 uxth r0, r7
G_M58754_IG05:
0000D0 B005 add sp, 20
0000D2 E8BD 8FF0 pop {r4,r5,r6,r7,r8,r9,r10,r11,pc}
G_M58754_IG06:
0000D6 F647 33B0 movw r3, 0x7bb0
0000DA F6C0 73CB movt r3, 0xfcb
0000DE 4798 blx r3 // CORINFO_HELP_RNGCHKFAIL
0000E0 DEFE bkpt
; Total bytes of code 226, prolog size 10 for method Program:M11(ref,ushort,ushort,ubyte,bool,ref):ushort
; ============================================================It seems most of the example is only necessary to ensure that arg3 get spilled to the stack. Without the spill, the example runs correctly.
EDIT: With the help of @mikedn, here is an adapted example which shows the same problem on x86. On my machine this prints the result correctly, but the disassembly shows that it potentially passes garbage to the WriteLine call:
interface IRT { void WriteLine<T>(T val); }
class CRT : IRT { public void WriteLine<T>(T val) => System.Console.WriteLine(val); }
public class Program
{
static IRT s_rt;
static byte[] s_1 = new byte[] { 0 };
static int s_3;
static ushort[] s_8 = new ushort[] { 1 };
public static void Main()
{
s_rt = new CRT();
M11(0, s_8, 0, 0, true, s_1);
}
static ushort M11(ushort arg3, ushort[] arg0, ushort arg1, byte arg4, bool arg7, byte[] arg10)
{
arg1 += arg4;
arg1 += (byte)(arg7 ? 1 : 0);
arg1 += arg10[0];
arg4 += arg10[0];
if (arg7)
{
ulong var4 = (ulong)s_3;
arg3 = s_8[0];
ushort var5 = arg3;
s_rt.WriteLine(var4);
s_rt.WriteLine((int)var5); // potential garbage passed in upper half
}
if (!arg7)
{
var vr7 = arg0[0];
}
arg10[0] = arg4;
return arg1;
}
}Metadata
Metadata
Assignees
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIbug
Activity
mikedn commentedon Jul 11, 2018
It looks like this can also happen on x86:
generates
The difference is that on x86 it is very unlikely that the upper bytes are garbage. On x86
arg3is passed on the stack so its associated stack location is setup by the caller, usually with an integerpush. And the value pushed will normally be appropriately zero extended.However, on ARM32
arg3is passed in a register and its associated stack location is a normal local variable. That local variable is never written to beforearg3 = s_8[0]so it will contain stack garbage. And thenstrhstores only 2 bytes so the upper bytes remain garbage.I'm not sure what exactly is bugged. It could be that "normalize on store" is not set correctly on ARM32. Though the x86 behavior, while it happens to work, it's kind of weird as well:
jakobbotsch commentedon Jul 11, 2018
I haven't checked exactly the calling convention used by the JIT, but it looks like __fastcall. If so shouldn't we be able to see the same behavior if we pass a 16-bit value as one of the first two arguments and the JIT ends up spilling it to the stack?
mikedn commentedon Jul 11, 2018
Yep, that's something I've yet to check.
jakobbotsch commentedon Jul 11, 2018
Hmm nope. Here that happens:
But the JIT does a wide store to initialize the stack slot:
mikedn commentedon Jul 11, 2018
Yeah, for this to "work" on x86 we need to find a way to get it to store to a stack location without using
+=, as doing that introduces a load fromarg3. Trouble is, with the original code the variable gets enregistered on x86.Anyway, I noticed that the original code involves some copy propagation performed during morph. This results in
arg3being used to callWriteLineinstead ofvar5. This is another possible cause for this bug - these 2 variables have the same type but one is an argument and the other one is not. There are differences in the way method arguments are handled and it could be that copy propagation doesn't get things right.jakobbotsch commentedon Jul 11, 2018
Ah, that was the missing link. My previous example gives the bad code-gen on x86 when removing that.
RussKeldorph commentedon Jul 11, 2018
@dotnet/jit-contrib
[-]RyuJIT spills 16 bit value but reloads as 32 bits in ARM32 in release[/-][+]RyuJIT spills 16 bit value but reloads as 32 bits in ARM32/x86 in release[/+]mikedn commentedon Jul 12, 2018
This is a bug in copy propagation performed by local assertion propagation during morph. It does not account for variables having different "normalize on load" settings and it substitutes one "normal" lclvar (having "normalize on load" false) with an argument (having "normalize on load" true). Normalize on load requires a separate cast after the lclvar load and that cast is generated during global morph.
Local assertion propagation needs to either block copy propagation in such cases or perform a re-morph. The former is likely safer and it's also what VN based copy propagation does (it rejects lclvar that are parameters).
mikedn commentedon Jul 14, 2018
Or local assertion propagation shouldn't even generate an assertion in this case, it already does not generate an assertion if lclvar types do not match.
jakobbotsch commentedon Jul 16, 2018
Here is a repro for x64 Linux:
jakobbotsch commentedon Aug 7, 2018
Here is a case for x64 that looks like the same problem:
The 'magic division' for the modulo in the finally shows that RyuJIT incorrectly stores arg1 as 2 bytes, and then uses it as if it was sign-extended to 4 bytes:
Not entirely sure why the try-finally seems to be necessary -- an x64 example did not show up until I implemented them.
@mikedn does this look like the same problem?
mikedn commentedon Aug 15, 2018
It's similar but not quite identical. In the original case a copy assertion is generated incorrectly and the use of a "normalize on store" variable is substituted with a "normalize on load" variable. The later requires a cast to be present in the IR and substitution does not insert such a cast.
In the latest example a subrange assertion is generated due to the RHS of
arg1 = (short)vr19;beingshort. This makes morph think that the "normalize on load" cast is not needed so it does not insert it. From there things fail in various ways. But in many cases failures are not observable, at least because the result is assigned back to a short variable.That said, I'm not 100% sure what the bug is:
"Normalize on load" exists only to deal with those (fortunately rare) cases where the stores are performed outside the method - method arguments and local variables passed by reference to other methods. In those cases the subrange assertion can't obviously ever be generated and morph has to insert the cast.
So the only way that morph's use of the subrange assertion makes sense is if stores to "normalize on load" variables are actually 32 bit stores, so that the state of the upper 24/16 bits is known. Otherwise morph must always insert the cast.
jakobbotsch commentedon Aug 15, 2018
Thank you, that is very enlightening. I do have a much simpler example now:
I will open a new issue for it tomorrow some time.
mikedn commentedon Aug 18, 2018
Nice repro!
I've done a quick test - forcing morph to always insert a cast results in a small code size regression, around 40 bytes across all FX. It's reasonable and some of the regressions can be addressed by improvements in other parts of the JIT.