Search Unity

Bug IL2CPP Player crashes on Delegate.CreateDelegate() with specific parameter combinations Unity 2021.3

Discussion in 'Scripting' started by WongKit, Jul 2, 2022.

  1. WongKit

    WongKit

    Joined:
    Apr 27, 2017
    Posts:
    26
    Hi,

    I am experiencing reproducible crashes when using Delegate.CreateDelegate() in IL2CPP builds in Unity 2021.3f5 LTS. I already filed a bug report (IN-3846), but did not get any response within a month. This is why I try my luck at the forums. Maybe, there is someone knowing a fix or potential workaround.

    What happened
    The built project crashes during the execution of method "Delegate.CreateDelegate"

    How to reproduce

    Environment:
    • 2021.3.5f1 LTS, Windows
    • Empty Unity 3D project, no specific packages installed
    • Scripting Backend IL2CPP
    • Default Visual Studio 2019 installation from UnityHub
    Steps:
    • Add "Test.cs" to any GameObject in an empty scene
      Code (CSharp):
      1. using UnityEngine;
      2. using System.Linq;
      3. using System.Reflection;
      4. using System.Linq.Expressions;
      5. using System;
      6.  
      7. public class Test : MonoBehaviour {
      8.     void Start() {
      9.         MethodInfo[] methodInfos = typeof(Test).GetMethods(BindingFlags.Public | BindingFlags.Static);
      10.         foreach (MethodInfo methodInfo in methodInfos) {
      11.             if (methodInfo.Name.StartsWith("Test")) {
      12.                 Func<Type[], Type> getType = Expression.GetActionType;
      13.                 var types = methodInfo.GetParameters().Select(p => p.ParameterType);
      14.                 Delegate.CreateDelegate(getType(types.ToArray()), methodInfo);
      15.             }
      16.         }
      17.     }
      18.  
      19.     public static void Test1_Ok(string s1) { }
      20.     public static void Test2_Ok(bool b1) { }
      21.     public static void Test3_Ok(string s1, bool b1) { }
      22.     public static void Test4_Crash(bool b1, string s1) { }
      23.     public static void Test5_Ok(int i1, string s1) { }
      24.     public static void Test6_Crash(int i1, bool b1) { }
      25. }
    • Build the project and run
    The player crashes after showing the splash screen.

    Notes:
    • The issue does not appear on Mono runtime
    • The issue does not appear on IL2CPP Code Generation: Faster (smaller) Builds
    • The issue does not appear on Unity 2020.3.34f1 LTS or Unity 2022.1.2
    • The attached Test.cs contains different "Test*"-methods that have different parameters. Depending on the set of parameters, CreateDelegate works or crashes the player.
    • The Player.log can be found in the attachment
     

    Attached Files:

  2. WongKit

    WongKit

    Joined:
    Apr 27, 2017
    Posts:
    26