Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug IL2CPP bug with reflection (SSCCE inside)

Discussion in 'Editor & General Support' started by Ainulindale, Jun 1, 2020.

  1. Ainulindale

    Ainulindale

    Joined:
    Aug 7, 2017
    Posts:
    3
    I've noticed an issue when retrieving string constants via reflection. If you use certain characters the returned string becomes cut off. Here's a short, self-contained, correct example:

    Code (CSharp):
    1. public class BugExample : UnityEngine.MonoBehaviour {
    2.  
    3.     private const string example0 = "0123";
    4.     private const string example1 = "½123";
    5.     private const string example2 = "§123";
    6.     private const string example3 = "£123";
    7.     private const string example4 = "££123";
    8.  
    9.     void Start() {
    10.  
    11.         // IL2CPP bug example with reflection.
    12.         // Target Platform: Windows
    13.         // Architecture: x86_64
    14.         // Compression Method: Default
    15.         // No ticked checkboxes in the build settings (except to include my scene)
    16.         // My OS: Windows 10 (64-bit)
    17.         // Scripting Compatibility Level: .NET 4.x
    18.         // C++ Compiler Configuration: Release
    19.         // Use incremental GC: Yes
    20.         // Allow 'unsafe' Code: No
    21.         // Default, untouched, optimization (Managed Stripping Level is Low)
    22.         // Unity engine version: 2019.3.15f1
    23.  
    24.         UnityEngine.Debug.Log(example0);  //expected "0123", got it
    25.         UnityEngine.Debug.Log(example1);  //expected "½123", got it
    26.         UnityEngine.Debug.Log(example2);  //expected "§123", got it
    27.         UnityEngine.Debug.Log(example3);  //expected "£123", got it
    28.         UnityEngine.Debug.Log(example4);  //expected "££123", got it
    29.        
    30.         System.Reflection.FieldInfo[] constants = GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
    31.  
    32.         UnityEngine.Debug.Log((string)constants[0].GetValue(null));  //expected "0123", got it
    33.         UnityEngine.Debug.Log((string)constants[1].GetValue(null));  //expected "½123" but got "½12"
    34.         UnityEngine.Debug.Log((string)constants[2].GetValue(null));  //expected "§123" but got "§12"
    35.         UnityEngine.Debug.Log((string)constants[3].GetValue(null));  //expected "£123" but got "£12"
    36.         UnityEngine.Debug.Log((string)constants[4].GetValue(null));  //expected "££123" but got "££1"
    37.  
    38.         // --> The bug is that strings are cut short on more special characters (presumably "non-ASCII") when retrieved through reflection.
    39.         // Works fine when playing in the Unity Editor or when exporting with Mono as the script backend.
    40.  
    41.     }
    42.  
    43. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    Hmm I wonder if something somewhere is assuming 1 byte per character.