Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Issue using ReadOnlySpan construction
davetorutek
#1 Posted : Friday, December 13, 2024 1:22:33 AM(UTC)
Rank: Member

Groups: Registered
Joined: 5/31/2018(UTC)
Posts: 13
Location: New Zealand

Thanks: 4 times
Was thanked: 6 time(s) in 4 post(s)
Quote:
System.ArgumentException: Value does not fall within the expected range.
at System.Runtime.CompilerServices.RuntimeHelpers.GetSpanDataFrom(RuntimeFieldHandle fldHandle, RuntimeTypeHandle targetTypeHandle, Int32& count)
at System.Runtime.CompilerServices.RuntimeHelpers.CreateSpan[T](RuntimeFieldHandle fldHandle)
at TestProject2.UnitTest1.Test1() in C:\Users\DaveLeaver\Desktop\Code\TestProject2\TestProject2\UnitTest1.cs:line 11
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)


Using this very simple test (.net 8, xunit 2):

Quote:
[Fact]
public void Test1()
{
var numbers = new int[] { 1, 2 };
Assert.Equal([1, 2], numbers);
}



Minimal project:
https://github.com/danzel/NCrunchBug/

Built in VS test runner succeeds.

This thread came up in my searches, https://github.com/dotnet/runtime/issues/79477

Thanks!
Remco
#2 Posted : Friday, December 13, 2024 1:47:23 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,163

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
Thanks for sharing this issue and for the code sample. I've reproduced it as you've described. The problem is caused by an instrumentation clash between RDI and the inline span being created by the compiler. I've noted this down to see if I can find a way to properly resolve it. For the time being, the workaround is to disable RDI for the method involved.

Code:

//ncrunch: rdi off
[Fact]
public void Test1()
{
    var numbers = new int[] { 1, 2 };
    Assert.Equal([1, 2], numbers);
}
//ncrunch: rdi default
1 user thanked Remco for this useful post.
davetorutek on 12/13/2024(UTC)
Remco
#3 Posted : Saturday, December 14, 2024 2:01:20 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,163

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
I've conducted a more thorough investigation of this problem.

It seems to be coming from unstable behaviour in the .NET runtime. The error itself seems to be intermittent, depending upon the contents of memory at the time. You can run several copies of the same test doing the same thing and all will pass except one. Disabling RDI suppresses the problem because it changes the IL sequences and therefore the memory at the time is different.

Unfortunately, getting to the bottom of this problem will require me to pull the runtime apart and debug it (an extremely expensive thing to do in terms of time), and there is no guarantee that I will be able to find a more adequate workaround than the one above. Nor can I presently see how NCrunch is actually responsible for the problem.

Given that this seems to be a very niche issue, I feel the best plan right now is to just shut off RDI for any methods where you encounter this problem. If the problem becomes more widespread, I'll investigate further.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.029 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download