RyuJIT C# wrong sum result with /optimize
Asked Answered
B

1

9

I've this piece of code:

private void AnswerToCe(int currentBlock, int totalBlock = 0)
{
    byte[] bufferToSend;
    byte[] macDst = mac;
    byte[] macSrc = ConnectionManager.SInstance.GetMyMAC();
    byte[] ethType;
    byte[] header;

    if (Function == FlashFunction.UPLOAD_APPL || Function == FlashFunction.UPLOAD_BITSTREAM)
    {
        ethType = BitConverter.GetBytes((ushort)EthType.ETH_TYPE_UPLOAD);
        ethType = new byte[] { ethType[1], ethType[0] };
        header = Header.GetBytes((ushort)binaryBlocks.Count, (ushort)(currentBlock + 1), (ushort)binaryBlocks[currentBlock].Length);
        int index = 0;
        bufferToSend = new byte[macDst.Length + macSrc.Length + ethType.Length + header.Length + binaryBlocks[currentBlock].Length];
        Array.Copy(macDst, 0, bufferToSend, index, macDst.Length);
        index += macDst.Length;
        Array.Copy(macSrc, 0, bufferToSend, index, macSrc.Length);
        index += macSrc.Length;
        Logger.SInstance.Write(index.ToString(), "test index pre");
        Array.Copy(ethType, 0, bufferToSend, index, ethType.Length);
        index += ethType.Length;
        Logger.SInstance.Write(index.ToString(), "test index post");
        Array.Copy(header, 0, bufferToSend, index, header.Length);
        index += header.Length;
        Array.Copy(binaryBlocks[currentBlock], 0, bufferToSend, index, binaryBlocks[currentBlock].Length);
    }

If I build my application in Debug mode everything is ok, test index pre prints 12 and test index post prints 14. the same in Release mode with Optimize code unchecked. if i test with Optimize code checked test index post prints 18 instead of 14.
Same result if I replace index += ethType.Length; with index += 2;. seems only index++;index++; is working.
I tried this code in an empty application and sums are ok.
App is multithreading but there isn't no concurrency here.
Decompiled code from DLL seems ok.
Any ideas why this happen?

EDIT: Happens only when app is compiled for x64. x86 is ok.
EDIT 3: some info of the build env:
visual studio 15.0.0-RTW+26228.4
framework 4.7.02053
can trigger this issue on framework 4.6.2 and 4.7. other frameworks aren't tested.
EDIT 5: new, smaller example project. no dependencies needed.
EDIT 6: disassembly of the test project here. (too long to post it here)

Bowse answered 2/8, 2017 at 12:53 Comment(16)
"App is multithreading but there isn't no concurrency here." hard to say as this is only part of your code and it uses some variables from outside of this function.Wylma
msdn.microsoft.com/en-us/library/c151dt3s.aspxBarthelemy
@PawełŁukasik breakpoints are hit only once. index variable is not external.Bowse
@Barthelemy no floating point hereBowse
@Bowse I think you have to narrow the problem even more. Can you provide MCVE?Diminutive
@Diminutive sure, i'll try to make oneBowse
@Bowse os in release mode only the `"test index post" is wrong? pre is ok?Wylma
@PawełŁukasik yes, pre is ok.Bowse
"Happens only when app is compiled for x64" This wouldn't be the first bug within RyuJIT... first thing I'd check is that your .NET installation is fully up to date.Sholom
@JamesThorpe added info on build env. i think i'm quite up to dateBowse
Does it work correctly if you tell it to use the old 64 bit jit compiler?Sholom
@JamesThorpe useLegacyJit seems to help!Diminutive
Add System.Diagnostics.Debugger.Break() call then run in release mode outside visual studio. When the breakpoint is hit, attach from visual studio and show the disassembly of the actual machine code that the JIT generated.Factorage
If it's looking like it might be a bug, and you've managed to reduce your problem to as small a project as possible, you might want to consider searching for/raising a bug here.Sholom
it's already fixed here github.com/dotnet/coreclr/issues/11574 will be avaiable soon in a hotfixBowse
@Bowse Now you've confirmed that, you can self-answer your question with those detailsSholom
B
0

It was an already reported bug in RyuJIT, more details here. Will be fixed in an hotfix soon.

Bowse answered 4/8, 2017 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.