pardeike / Harmony

A library for patching, replacing and decorating .NET and Mono methods during runtime
https://www.patreon.com/pardeike
MIT License
5.17k stars 486 forks source link

Error when adding prefix to method that returns struct #274

Closed boformer closed 4 years ago

boformer commented 4 years ago

Describe the bug

Might be related to #272, but I'm just reporting it to be sure.

Cities: Skylines crashes to desktop when patching a method that returns a struct. I managed to slim down the struct to be completely empty and it still happens. Error disappears when changing the return type of the method to void

To Reproduce

Code that causes exception:

var harmony = new Harmony("boformer.Harmony2Example");
harmony.Patch(typeof(FakeWorkshop).GetMethod("UpdateItem"), new HarmonyMethod(typeof(WorkshopUpdateItemPatch).GetMethod("Prefix")));
public static class WorkshopUpdateItemPatch {
    public static void Prefix() {}
}

public class FakeWorkshop {
    public FakeUGCHandle UpdateItem() {
        return default;
    }
}

public struct FakeUGCHandle {}

Crash Log

0x00007FF9D24BA859 (KERNELBASE) RaiseException
0x00007FF99F251158 (mono) set_vprintf_func
0x00007FF99F25117C (mono) set_vprintf_func
0x00007FF99F271775 (mono) mono_class_get_full
0x00007FF99F3620D2 (mono) mono_set_break_policy
0x00007FF99F373F6C (mono) mono_set_defaults
0x00007FF99F375037 (mono) mono_set_defaults
0x00007FF99F375655 (mono) mono_set_defaults
0x00007FF99F3758CB (mono) mono_set_defaults
0x00007FF99F2C8499 (mono) mono_runtime_invoke
0x00007FF99F2CE9A7 (mono) mono_runtime_invoke_array
0x00007FF99F28CE3F (mono) mono_domain_finalize
0x000000000540C490 (Mono JIT Code) (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
0x000000000540C0DD (Mono JIT Code) System.Reflection.MonoMethod:Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo)
0x000000000540BF00 (Mono JIT Code) System.Reflection.MethodBase:Invoke (object,object[])
0x000000001B58F9F1 (Mono JIT Code) System.Reflection.Emit.DynamicMethod:Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo)
0x000000000540BF00 (Mono JIT Code) System.Reflection.MethodBase:Invoke (object,object[])
0x000000001B58F635 (Mono JIT Code) HarmonyLib.StructReturnBuffer:SizeOf (System.Type)
0x000000001B558272 (Mono JIT Code) HarmonyLib.StructReturnBuffer:NeedsFix (System.Reflection.MethodBase)
0x000000001B5574F6 (Mono JIT Code) HarmonyLib.MethodPatcher:.ctor (System.Reflection.MethodBase,System.Reflection.MethodBase,System.Collections.Generic.List`1<System.Reflection.MethodInfo>,System.Collections.Generic.List`1<System.Reflection.MethodInfo>,System.Collections.Generic.List`1<System.Reflection.MethodInfo>,System.Collections.Generic.List`1<System.Reflection.MethodInfo>,bool)
0x000000001B554143 (Mono JIT Code) HarmonyLib.PatchFunctions:UpdateWrapper (System.Reflection.MethodBase,HarmonyLib.PatchInfo)
0x000000001B5526AD (Mono JIT Code) HarmonyLib.PatchProcessor:Patch ()
0x000000001B5522CC (Mono JIT Code) HarmonyLib.Harmony:Patch (System.Reflection.MethodBase,HarmonyLib.HarmonyMethod,HarmonyLib.HarmonyMethod,HarmonyLib.HarmonyMethod,HarmonyLib.HarmonyMethod)
0x000000001B58F410 (Mono JIT Code) ExampleMod.Patcher:PatchAll ()
0x000000001B58F289 (Mono JIT Code) ExampleMod.Mod/<>c:<OnEnabled>b__4_0 ()
0x000000000540C752 (Mono JIT Code) CitiesHarmony.API.HarmonyHelper:DoOnHarmonyReady (System.Action)
0x000000000540C637 (Mono JIT Code) ExampleMod.Mod:OnEnabled ()
0x00000000053808CB (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
0x00007FF99F375CDF (mono) mono_set_defaults
0x00007FF99F2C8499 (mono) mono_runtime_invoke
0x00007FF99F2CE9A7 (mono) mono_runtime_invoke_array
0x00007FF99F28CE3F (mono) mono_domain_finalize
0x000000000540C490 (Mono JIT Code) (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
0x000000000540C0DD (Mono JIT Code) System.Reflection.MonoMethod:Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo)
0x000000000540BF00 (Mono JIT Code) System.Reflection.MethodBase:Invoke (object,object[])
0x000000000540A91C (Mono JIT Code) ColossalFramework.Plugins.PluginManager:AddPlugins (System.Collections.Generic.Dictionary`2<string, ColossalFramework.Plugins.PluginManager/PluginInfo>)
0x0000000005406103 (Mono JIT Code) ColossalFramework.Plugins.PluginManager:LoadPlugins ()
0x00000000053B0830 (Mono JIT Code) Starter:Awake ()
0x00000000053808CB (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
0x00007FF99F375CDF (mono) mono_set_defaults
0x00007FF99F2C8499 (mono) mono_runtime_invoke
  ERROR: SymGetSymFromAddr64, GetLastError: 'Es wurde versucht, auf eine unzulässige Adresse zuzugreifen.' (Address: 00007FF775D14954)
0x00007FF775D14954 (Cities) 
  ERROR: SymGetSymFromAddr64, GetLastError: 'Es wurde versucht, auf eine unzulässige Adresse zuzugreifen.' (Address: 00007FF775D0FC2A)
0x00007FF775D0FC2A (Cities) 

Runtime environment (please complete the following information):

pardeike commented 4 years ago

Nice work