runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Array initialization throws ArgumentOutOfRangeException if size is 0 in ILC mode

Open caesar-chen opened this issue 9 years ago • 27 comments

System.Net.Sockets.SendPacketsAsync tests run fine in F5 ('uap') & netcore & netfx mode. But in ILC ('uap-aot') mode they are failing. It seems that the array initialization throws ArgumentOutOfRangeException if size is 0 (take one test for example):

        [Theory]
        [InlineData(SocketImplementationType.APM)]
        [InlineData(SocketImplementationType.Async)]
        public void EmptyBuffer_Ignored(SocketImplementationType type)
        {
            SendPackets(type, new SendPacketsElement(new byte[0]), 0);
        }

SendPackets() will eventually call into SetupPinHandlesSendPackets() in SocketAsyncEventArgs.Windows.cs

        private unsafe void SetupPinHandlesSendPackets()
        {
           ...
            _sendPacketsDescriptor =
                new Interop.Winsock.TransmitPacketsElement[_sendPacketsElementsFileCount + _sendPacketsElementsBufferCount];
            // throws ArgumentOutOfRangeException if size is 0, i.e _sendPacketsElementsFileCount + _sendPacketsElementsBufferCount == 0
           ...
        }

If _sendPacketsElementsFileCount + _sendPacketsElementsBufferCount == 0, we get failures like this:

System.ArgumentOutOfRangeException : Specified argument was out of the range of valid values.

Is this an expected problem from the UWP test runner in ILC mode? This is the cause for 90% of Sockets Functional tests failure in ILC mode.

cc: @davidsh

caesar-chen avatar Jun 17 '17 00:06 caesar-chen

cc: @CIPop

davidsh avatar Jun 20 '17 02:06 davidsh

Is this an expected problem from the UWP test runner in ILC mode?

@caesar1995 I think the general idea is that the code should work identically in both compiled and not compiled versions. Since we're using the not-compiled version for debugging, these kinds of differences will be extremely hard to understand and fix for app-developers.

@yizhang82 PTAL

CIPop avatar Jun 20 '17 17:06 CIPop

Should I change the label? All of the tests failed with the same reason (array initialized with size 0), they only failed in ILC run. I don't think it's Socket related problem

caesar-chen avatar Jun 20 '17 18:06 caesar-chen

This might be related to dotnet/corefx#21243

davidsh avatar Jun 20 '17 18:06 davidsh

Another note: if I directly do int[] a = new int[0]; for testing This will work in ILC mode and compile fine.

The situation is similar to the one I described here (From "But if I do"): dotnet/runtime#22297

caesar-chen avatar Jun 20 '17 18:06 caesar-chen

@hoyMS Can you please take a look and see if this is the same issue as dotnet/corefx#21243?

tijoytom-zz avatar Jun 21 '17 04:06 tijoytom-zz

I'm taking a look. Which project file should I use to repro the failure?

htyu avatar Jun 21 '17 16:06 htyu

@hoyMS System.Net.Sockets/tests/FunctionalTests/SendPacketsAsync.cs There are many failures in this test class. You may need to enable outerloop runs to see more failures.

caesar-chen avatar Jun 21 '17 16:06 caesar-chen

@caesar1995 Thanks. Could you point me to the instructions on how to enable outerloop runs?

htyu avatar Jun 21 '17 16:06 htyu

@caesar1995 Or can you provide the repro binaries and intermediates?

htyu avatar Jun 21 '17 17:06 htyu

Could you point me to the instructions on how to enable outerloop runs?

msbuild /t:rebuildandtest /p:TargetGroup=uapaot /p:Outerloop=true

davidsh avatar Jun 21 '17 17:06 davidsh

@hoyMS cd src\System.Net.Sockets\tests\FunctionalTests msbuild /t:rebuildandtest /p:TargetGroup=uapaot /p:Outerloop=true

You need to remove the [ActiveIssue(20135, TargetFrameworkMonikers.Uap)] label I put on, for example, the EmptyBuffer_Ignored() method. Please let me know if you are not able to repro by running those test.

caesar-chen avatar Jun 21 '17 17:06 caesar-chen

@hoyMS Which area label you think is appropriate for this issue?

The array initialization is not Sockets specific, and the test only fails in ILC run.

caesar-chen avatar Jun 21 '17 19:06 caesar-chen

@caesar1995 I think test-run-uwp-ilc is OK.

htyu avatar Jun 21 '17 20:06 htyu

@yizhang82 I've updated the area label to "Infrastructure". Not sure what label applies in general to .NET Native toolchain bugs. Please revise if necessary. Thanks!

davidsh avatar Jun 21 '17 20:06 davidsh

For the zero length array I'm seeing there are references to its element in the loop following the allocation. That will also cause an exception. Is it by design?

htyu avatar Jun 21 '17 21:06 htyu

That will also cause an exception

No, explained offline.

caesar-chen avatar Jun 21 '17 22:06 caesar-chen

I switched the no-opt test and verified that the failure is not from the loop but from the following routine with an zero length array and an zero index.

//==================================================================== // UnsafeAddrOfPinnedArrayElement() // // IMPORTANT NOTICE: This method does not do any verification on the // array. It must be used with EXTREME CAUTION since passing in // an array that is not pinned or in the fixed heap can cause // unexpected results ! //==================================================================== public static IntPtr UnsafeAddrOfPinnedArrayElement(Array arr, int index) { if (arr == null) throw new ArgumentNullException(nameof(arr));

        if (index < 0 || index >= arr.Length)
            **_throw new ArgumentOutOfRangeException(nameof(index)_**);
        Contract.EndContractBlock();

        int offset = checked(index * arr.GetElementSize());

        return arr.GetAddrOfPinnedArrayFromEETypeField() + offset;
    }

Call stack:

xunit.console.netcore.dll!$36_System::Runtime::InteropServices::Marshal.UnsafeAddrOfPinnedArrayElement(System::Array & arr, int index) Line 1443	Unknown
xunit.console.netcore.dll!$36_System::Runtime::InteropServices::Marshal.UnsafeAddrOfPinnedArrayElement<Interop+Winsock+TransmitPacketsElement>($28_Interop::Winsock::TransmitPacketsElement[] & arr, int index) Line 1453	Unknown

xunit.console.netcore.dll!$28_System::Net::Sockets::SocketAsyncEventArgs.SetupPinHandlesSendPackets() Line 1065 Unknown xunit.console.netcore.dll!$28_System::Net::Sockets::SocketAsyncEventArgs.CheckPinSendPackets() Line 847 Unknown xunit.console.netcore.dll!$28_System::Net::Sockets::SocketAsyncEventArgs.InnerStartOperationSendPackets() Line 661 Unknown xunit.console.netcore.dll!$28_System::Net::Sockets::SocketAsyncEventArgs.StartOperationSendPackets() Line 584 Unknown xunit.console.netcore.dll!$28_System::Net::Sockets::Socket.SendPacketsAsync($28_System::Net::Sockets::SocketAsyncEventArgs & e) Line 4313 Unknown xunit.console.netcore.dll!$29_System::Net::Sockets::Tests::SendPacketsAsync.SendPackets($29_System::Net::Sockets::Tests::SocketImplementationType type, $28_System::Net::Sockets::SendPacketsElement[] & elements, $24_System::Net::Sockets::SocketError expectedResut, int bytesExpected) Line 459 Unknown xunit.console.netcore.dll!$29_System::Net::Sockets::Tests::SendPacketsAsync.SendPackets($29_System::Net::Sockets::Tests::SocketImplementationType type, $28_System::Net::Sockets::SendPacketsElement & element, int bytesExpected) Line 433 Unknown xunit.console.netcore.dll!$29_System::Net::Sockets::Tests::SendPacketsAsync.BufferZeroCount_Ignored($29_System::Net::Sockets::Tests::SocketImplementationType type) Line 179 Unknown

At this point, I would say it’s not an optimizer issue. We should probably change the test source.

htyu avatar Jun 21 '17 23:06 htyu

@hoyMS Thanks for your help Hongtao. But probably this is not the answer.

I have few doubts:

  1. I'm not able to find the implementation of UnsafeAddrOfPinnedArrayElement(), this is what get. Could you please share where can I find the implementation?
  2. Based on the above implementation. If this line of code causes touble, _ptrSendPacketsDescriptor = Marshal.UnsafeAddrOfPinnedArrayElement(_sendPacketsDescriptor, 0); which means index is always == 0 for UnsafeAddrOfPinnedArrayElement(Array arr, int index)
  3. ...verified that the failure is not from the loop but from the following routine with an zero length array and an zero index

which means if (index < 0 || index >= arr.Length) will always be true (0 >= 0), therefore ArgumentOutOfRangeException will always been thrown. No matter it's netcore/uap/uapaot run. However, we are only observing the failure in ILC mode.

  1. We should probably change the test source.

What test source, our product code, or test cases?

cc: @DavidGoll

caesar-chen avatar Jun 22 '17 04:06 caesar-chen

The method UnsafeAddrOfPinnedArrayElement is not in CoreFX, instead it's in System.Private.Corelib:

CoreRT\src\System.Private.Interop\src\System\Runtime\InteropServices\Marshal.cs

@yizhang82 @tijoytom Looks like the method is .Net Native specific. Do we want to do something special in SetupPinHandlesSendPackets for .Net Native?

htyu avatar Jun 22 '17 16:06 htyu