RyuJIT C#在使用/optimize时求和结果错误

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);
    }

如果我在Debug模式下构建我的应用程序,一切都很好,test index pre打印12和test index post打印14。如果我使用选中了Optimize code的Release模式进行测试,那么结果与前者相同。但是如果我勾选Optimize code并进行测试,test index post会打印18而不是14。
如果将index += ethType.Length;替换为index += 2;,则结果相同。似乎只有index++;index++;是有效的。
我尝试在一个空应用程序中运行此代码,总和是正确的。
应用程序是多线程的,但这里没有并发。
来自DLL的反编译代码看起来很好。
有任何想法为什么会出现这种情况吗?

编辑: 仅在以x64编译应用程序时才会发生。x86没问题。
编辑3:构建环境的一些信息:
Visual Studio 15.0.0-RTW+26228.4
Framework 4.7.02053
可以在Framework 4.6.2和4.7上触发此问题。未测试其他框架。
编辑5:新的、更小的示例项目。不需要依赖项。
编辑6:测试项目的反汇编在这里。(太长了,无法在此发布)


3
@StefanE 这里没有浮点数。 - rmbq
2
@rmbq 我认为你需要进一步缩小问题范围。你能提供一个 MCVE 吗? - vasek
3
如果您告诉它使用旧的 64 位 JIT 编译器,它是否能正常工作?(参考链接:https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/uselegacyjit-element) - James Thorpe
3
@JamesThorpe: useLegacyJit 似乎有帮助! - vasek
3
如果你觉得可能是一个 bug,并且已经成功将问题简化为尽可能小的项目,你可以考虑在这里搜索或提交 bug 报告 - James Thorpe
显示剩余11条评论
1个回答

0

这是RyuJIT中已经报告过的一个bug,更多细节在这里。很快会通过热修复进行修复。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接