BepInEx / Il2CppInterop

A tool interoperate between CoreCLR and Il2Cpp at runtime
GNU Lesser General Public License v3.0
195 stars 61 forks source link

Don't unbox non-blittable generic valuetypes for parameter loading #69

Closed Kasuromi closed 1 year ago

Kasuromi commented 1 year ago

Fixes an issue where non-blittable generic value types get unboxed when they shouldn't be during parameter loading.

The most notable example is adding a boxed integer into a dictionary, the value field would have invalid data written to it.

private unsafe void Test() {
    long testLong = 2137;
    var testInt64 = new Il2CppSystem.Int64();
    *&testInt64.m_value = testLong;

    var dictionary = new Il2CppSystem.Collections.Generic.Dictionary<string, Il2CppSystem.Object>();
    dictionary.Add("test", testInt64.BoxIl2CppObject()); // PREV: Writes invalid data to value field

    foreach (var kv in dictionary)
    {
        Log.LogInfo(kv.Key);   // "test"
        Log.LogInfo(kv.Value); // 2137   (PREV: crash on access)
    }
}