dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.38k stars 4.75k forks source link

FatalExecutionEngineError in managed code #27779

Closed jptrxff closed 4 years ago

jptrxff commented 6 years ago

I have experienced Access Violation Exceptions and Fatal Execution Engine Error (it seems to be the same error) in managed code in C#. I have narrowed it down to the following snippet. Am I missing something? I thought it should not be possible to these kind of exceptions in managed code. I have seen it both in .net 4.7 and 4.5 and on multiple different computers? Is this a known issue in .Net?

It is primarily seen in x64 bit configurations, but also reproducible in Any CPU. For me it is reproducible both in Debug and Release configurations. Others have reported that it is only reproducible for them in Debug builds.

This snippet typically fails within the first 1-2 minutes.

using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;

public static class FatalExecutionEngineBugExposer
{

    public static void Main(string[] args)
    {

        for (int i = 0; i < 500000; i++)
        {
            try
            {
                var testProblem = new TestProblem();
                testProblem.AddTrianglesExperiment();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.GetBaseException());
            }
        }
    }

    public class TestProblem
    {
        public struct Point
        {
            public double X, Y, Z;
        }

        public void AddTrianglesExperiment()
        {
            var points = new Point[28800];

            Parallel.ForEach(Partitioner.Create(0, points.Length),
                range =>
                {
                    // Create cache
                    var cache = new CubeRefCache();
                    cache.Entries = new CubeRefCacheEntry[20000];

                    // Add triangles
                    for (int i = range.Item1; i < range.Item2; i++)
                    {
                        ProcessTriangle(ref points[i].X, ref points[i].Y, ref points[i].Z, cache);  
                    }
                });
        }

        void ProcessTriangle(ref double pt1, ref double pt2, ref double pt3, CubeRefCache cache)
        {
            cache.Add(0, 0);
        }

        public struct CubeRefCacheEntry
        {
            public int Index;
            public int Item;
        }

        class CubeRefCache
        {
            internal CubeRefCacheEntry[] Entries;

            internal void Add(int index, int item)
            {
                Entries[0].Index = index;
                Entries[0].Item = item;
            }
        }
    }
}

I was unsure where to post it, so I have posted it in various places here: https://stackoverflow.com/questions/53059965/fatal-execution-engine-error-in-managed-c-sharp-code

https://github.com/dotnet/coreclr/issues/20690#issuecomment-434529220

Multiple persons have been able to reproduce this issue on different pc's.

karelz commented 6 years ago

Duplicate of https://github.com/dotnet/coreclr/issues/20690