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)

rmbq
  • 427
  • 4
  • 20
  • "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. – Paweł Łukasik Aug 02 '17 at 12:57
  • https://msdn.microsoft.com/en-us/library/c151dt3s.aspx – StefanE Aug 02 '17 at 12:57
  • @PawełŁukasik breakpoints are hit only once. index variable is not external. – rmbq Aug 02 '17 at 12:59
  • 3
    @StefanE no floating point here – rmbq Aug 02 '17 at 13:00
  • 2
    @rmbq I think you have to narrow the problem even more. Can you provide [MCVE](https://stackoverflow.com/help/mcve)? – vasek Aug 02 '17 at 13:02
  • @vasek sure, i'll try to make one – rmbq Aug 02 '17 at 13:07
  • @rmbq os in release mode only the `"test index post" is wrong? pre is ok? – Paweł Łukasik Aug 02 '17 at 13:11
  • @PawełŁukasik yes, pre is ok. – rmbq Aug 02 '17 at 13:12
  • _"Happens only when app is compiled for x64"_ This wouldn't be [the](https://blogs.msdn.microsoft.com/dotnet/2015/07/28/ryujit-bug-advisory-in-the-net-framework-4-6/) first [bug](https://stackoverflow.com/questions/31570521/ryujit-producing-incorrect-results) within RyuJIT... first thing I'd check is that your .NET installation is fully up to date. – James Thorpe Aug 02 '17 at 15:19
  • @JamesThorpe added info on build env. i think i'm quite up to date – rmbq Aug 02 '17 at 15:27
  • 3
    Does it work correctly if you tell it to [use the old 64 bit jit compiler](https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/uselegacyjit-element)? – James Thorpe Aug 02 '17 at 15:58
  • 3
    @JamesThorpe `useLegacyJit` seems to help! – vasek Aug 02 '17 at 17:20
  • 1
    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. – Ben Voigt Aug 02 '17 at 19:58
  • 3
    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](https://github.com/dotnet/coreclr/issues). – James Thorpe Aug 03 '17 at 08:40
  • it's already fixed here https://github.com/dotnet/coreclr/issues/11574 will be avaiable soon in a hotfix – rmbq Aug 04 '17 at 06:47
  • @rmbq Now you've confirmed that, you can [self-answer](https://stackoverflow.com/help/self-answer) your question with those details – James Thorpe Aug 04 '17 at 08:05

1 Answers1

0

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

rmbq
  • 427
  • 4
  • 20