-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
Description
I've noticed a memory corruption bug when using Vector3 from System.Numerics. It seems to be related to passing Vector3 to functions. It only happens in Release builds.
Reproduction Steps
The following code will trigger the memory corruption. It is not completely deterministic, but always happens after a second or so. Note that the bug only appears in Release build for me.
Sorry for the slightly lengthy code, I couldn't shorten it further.
using System.Numerics;
using System.Runtime.CompilerServices;
readonly struct Ray
{
public readonly Vector3 Origin;
public readonly Vector3 Direction;
public Ray(Vector3 origin, Vector3 direction)
{
Origin = origin;
Direction = direction;
}
}
class RayTracer
{
private static IEnumerable<object> hittables;
static RayTracer()
{
var list = new List<object>();
list.Add(new object());
hittables = list;
}
public static void Main(string[] args)
{
for (int x = 0; x < 100*100*100; x++)
{
Ray r = GetRay();
System.Console.WriteLine(r.Direction);
traceRay(r);
}
}
public static Ray GetRay()
{
return new Ray(Vector3.Zero, -Vector3.UnitY);
}
private static Vector3 traceRay(Ray r)
{
foreach (object h in hittables) {
TestHit(r);
return Vector3.One;
}
return Vector3.Zero;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void TestHit(Ray ray)
{
return;
}
}Expected behavior
No memory corruption
Actual behavior
Memory corruption, resulting in a System.AccessViolationException.
Regression?
No response
Known Workarounds
No response
Configuration
I'm running .NET 7 on macOS 10.15.7 (x64), code is being compiled in Release mode. I couldn't test it on other versions/platforms yet. Let me know if you need any further details of my setup.
% dotnet --version
7.0.100
% sw_vers
ProductName: Mac OS X
ProductVersion: 10.15.7
BuildVersion: 19H2026
Other information
There is a similar bug (#10972) that was fixed in .NET 3.