Skip to content

Conversation

@bholmes
Copy link
Member

@bholmes bholmes commented Aug 16, 2021

Adding null check to avoid abort when invalid IL is encountered

When a callvirt instruction is encountered on a static method, there is
a hard abort/crash. This commit avoids the problem.

  • Should this pull request have release notes?
    • Yes
    • No
  • Do these changes need to be back ported?
    • Yes
    • No

Release notes

Fixed case 1353003 @bholmes:
Mono: Avoid runtime abort when JIT encounters a callvirt on a static method.

Backports

  • 2021.2

… 1353003]

When a callvirt instruction is encountered on a static method, there is
a hard abort/crash. This commit avoids the problem.
@bholmes
Copy link
Member Author

bholmes commented Aug 16, 2021

This also occurs for Mono in the dotnet runtime repo. Here is a PR to address the upstream code. dotnet/runtime#57522

@bholmes
Copy link
Member Author

bholmes commented Aug 16, 2021

Sharing the test program here as well.

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            MethodInfo testMethod = typeof(Program).GetMethod("TestMethod");

            DynamicMethod dynamicMethod = new DynamicMethod("TestMethod", typeof(int), new Type[0], restrictedSkipVisibility: true);
            ILGenerator iLGenerator = dynamicMethod.GetILGenerator();
            iLGenerator.Emit(OpCodes.Callvirt, testMethod);
            iLGenerator.Emit(OpCodes.Ret);
            var ttt = (Func<int>)dynamicMethod.CreateDelegate(typeof(Func<int>));

            Console.WriteLine($"{ttt()}");
        }

        static public int TestMethod ()
        {
            return 42;
        }
    }
}

Without this PR there is a runtime crash.

@bholmes bholmes requested a review from UnityAlex August 16, 2021 19:39
@bholmes bholmes merged commit 8be75a7 into unity-master Aug 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants