Array initialization throws ArgumentOutOfRangeException if size is 0 in ILC mode
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
cc: @CIPop
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
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
This might be related to dotnet/corefx#21243
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
@hoyMS Can you please take a look and see if this is the same issue as dotnet/corefx#21243?
I'm taking a look. Which project file should I use to repro the failure?
@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.
@caesar1995 Thanks. Could you point me to the instructions on how to enable outerloop runs?
@caesar1995 Or can you provide the repro binaries and intermediates?
Could you point me to the instructions on how to enable outerloop runs?
msbuild /t:rebuildandtest /p:TargetGroup=uapaot /p:Outerloop=true
@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.
@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.
@caesar1995 I think test-run-uwp-ilc is OK.
@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!
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?
That will also cause an exception
No, explained offline.
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.
@hoyMS Thanks for your help Hongtao. But probably this is not the answer.
I have few doubts:
- I'm not able to find the implementation of UnsafeAddrOfPinnedArrayElement(), this is what get. Could you please share where can I find the implementation?
- Based on the above implementation. If this line of code causes touble,
_ptrSendPacketsDescriptor = Marshal.UnsafeAddrOfPinnedArrayElement(_sendPacketsDescriptor, 0);which means index is always == 0 forUnsafeAddrOfPinnedArrayElement(Array arr, int index) -
...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.
-
We should probably change the test source.
What test source, our product code, or test cases?
cc: @DavidGoll
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?