saucecontrol / PhotoSauce

MagicScaler high-performance, high-quality image processing pipeline for .NET
http://photosauce.net/
MIT License
589 stars 49 forks source link

SEHException raised on .NET 6 Preview. #72

Closed udaken closed 3 years ago

udaken commented 3 years ago

Hi,

An exception occurred when calling MagicImageProcessor.ProcessImage in .NET6.0 Preview environment. In .NET5.0, there was no problem.

Is this a bug in runtime? or MagicScaler?

> dotnet run -f net5.0
> dotnet run -f net6.0
Fatal error. System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
   at PhotoSauce.MagicScaler.ConverterFromLinear`2+Converter3X[[System.Single, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Byte, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].convertFloat(Byte*, Byte*, Byte*, IntPtr)
   at PhotoSauce.MagicScaler.ConverterFromLinear`2+Converter3X[[System.Single, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Byte, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].PhotoSauce.MagicScaler.IConversionProcessor.ConvertLine(Byte*, Byte*, IntPtr)
   at PhotoSauce.MagicScaler.Transforms.ConversionTransform.copyPixelsBuffered(PhotoSauce.MagicScaler.PixelArea ByRef, Int32, Int32, IntPtr)
   at PhotoSauce.MagicScaler.Transforms.ConversionTransform.CopyPixelsInternal(PhotoSauce.MagicScaler.PixelArea ByRef, Int32, Int32, IntPtr)
   at PhotoSauce.MagicScaler.PixelSource.CopyPixels(PhotoSauce.MagicScaler.PixelArea ByRef, Int32, Int32, IntPtr)
   at PhotoSauce.Interop.Wic.IWICBitmapSourceImpl.CopyPixels(PhotoSauce.Interop.Wic.IWICBitmapSourceImpl*, TerraFX.Interop.WICRect*, UInt32, UInt32, Byte*)
   at TerraFX.Interop.IWICBitmapFrameEncode.WriteSource(TerraFX.Interop.IWICBitmapSource*, TerraFX.Interop.WICRect*)
   at PhotoSauce.MagicScaler.WicImageEncoderFrame.WriteSource(PhotoSauce.MagicScaler.PipelineContext, PhotoSauce.MagicScaler.PixelArea)
   at PhotoSauce.MagicScaler.MagicImageProcessor.WriteOutput(PhotoSauce.MagicScaler.PipelineContext, System.IO.Stream)
   at PhotoSauce.MagicScaler.MagicImageProcessor.ProcessImage(System.String, System.IO.Stream, PhotoSauce.MagicScaler.ProcessImageSettings)
   at Net6Console.Program.Main(System.String[])

Code

using PhotoSauce.MagicScaler;
using System;
using System.IO;

namespace Net6Console
{
    class Program
    {
        static void Main(string[] args)
        {
            string inPath = @"./sample.png";
            var settings = new ProcessImageSettings {
                Height = 200,
                MatteColor = System.Drawing.Color.White,
                ResizeMode = CropScaleMode.Stretch,
                Width = 400,
            };

            using var outStream = new MemoryStream();
            MagicImageProcessor.ProcessImage(inPath, outStream, settings);
        }
    }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net5.0;net6.0</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="PhotoSauce.MagicScaler" Version="0.12.1" />
  </ItemGroup>
</Project>

Environment

> wmic os get name,version
Name                                                                                Version
Microsoft Windows 10 Enterprise Evaluation|C:\WINDOWS|\Device\Harddisk0\Partition2  10.0.19042

> wmic cpu get name
Name
AMD Ryzen 5 5600X 6-Core Processor
> dotnet --list-sdks
5.0.201 [C:\Program Files\dotnet\sdk]
6.0.100-preview.6.21355.2 [C:\Program Files\dotnet\sdk]
saucecontrol commented 3 years ago

Hey, thanks for bringing this to my attention! I did some testing on net6.0 preview 5 but hadn't tried preview 6 yet. I can repro the error on preview 6 and not on preview 5, so this would indeed appear to be a new runtime issue. I'll try with the nightlies and see if it's been resolved already and log an issue if not.

saucecontrol commented 3 years ago

Confirmed this is a JIT bug. It's emitting an illegal instruction encoding for AVX2 Gather instructions. I have filed https://github.com/dotnet/runtime/issues/56967 to get that fixed.

Thanks again for reporting and for the excellent detail!

saucecontrol commented 3 years ago

JIT fix has been merged and will be in .NET 6 RC1. I'll leave this open for visibility in the meantime.

udaken commented 3 years ago

Thanks for analyzing it and reporting it to the runtime team. I will wait for RC1 to be released.