Facepunch / sbox-issues

177 stars 12 forks source link

Game crash on hotload via Sandbox.ByteStream type constraint violation #4046

Closed trundlr closed 8 months ago

trundlr commented 1 year ago

Describe the bug

When hotloading a menu game, I am receiving relatively consistent crashes.

2023/09/21 21:28:57.8361    [ILHotload/GameMenu] GenericArguments[0], 'Sandbox.ByteStream', on 'Int32 SizeOf[T]()' violates the constraint of type 'T'. System.ArgumentException: GenericArguments[0], 'Sandbox.ByteStream', on 'Int32 SizeOf[T]()' violates the constraint of type 'T'.
 ---> System.Security.VerificationException: Method System.Runtime.CompilerServices.Unsafe.SizeOf: type argument 'Sandbox.ByteStream' violates the constraint of type parameter 'T'.
   at System.RuntimeMethodHandle.GetStubIfNeeded(RuntimeMethodHandleInternal method, RuntimeType declaringType, RuntimeType[] methodInstantiation)
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   --- End of inner exception stack trace ---
   at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   at MonoMod.Utils.Extensions.ComputeManagedSize(Type t)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at MonoMod.Utils.Extensions.GetManagedSize(Type t)
   at MonoMod.Core.Platforms.Systems.WindowsSystem.ClassifyX64(Type type, Boolean isReturn)
   at MonoMod.Core.Platforms.Abi.Classify(Type type, Boolean isReturn)
   at MonoMod.Core.Platforms.PlatformTriple.GetRealDetourTarget(MethodBase from, MethodBase to)
   at MonoMod.Core.Platforms.PlatformTripleDetourFactory.Detour..ctor(PlatformTriple triple, MethodBase src, MethodBase dst)
   at MonoMod.Core.Platforms.PlatformTripleDetourFactory.CreateDetour(CreateDetourRequest request)
   at MonoMod.Core.DetourFactory.CreateDetour(IDetourFactory factory, MethodBase source, MethodBase target, Boolean applyByDefault)
   at Sandbox.ILHotload.TryReplaceMethod(MethodBase source, MethodBase replace)
2023/09/21 21:28:57.8660    [ILHotload/GameMenu] Failed to replace method Hyperdrive.Player::GetDataStream  
        ILHotload.Replace - :0
        TypeLibrary.AddAssembly - :0
        TypeLibrary.AddAssembly - :0
        PackageLoader.LoadAssemblyFromPackage - :0
        PackageLoader.LoadPendingChanges - :0
        GameMenuDll.Tick - :0
        EngineLoop.FrameStart - :0
        EngineLoop.RunFrame - :0

2023/09/21 21:28:57.8812    [Generic] AppDomain unhandled exception System.NullReferenceException: Object reference not set to an instance of an object.
   at MonoMod.Core.Platforms.PlatformTripleDetourFactory.Detour.BeforeDispose()
   at MonoMod.Core.Platforms.PlatformTripleDetourFactory.DetourBase.Dispose(Boolean disposing)
   at MonoMod.Core.Platforms.PlatformTripleDetourFactory.DetourBase.Finalize()
2023/09/21 21:28:57.8966    [ILHotload/tools] GenericArguments[0], 'Sandbox.ByteStream', on 'Int32 SizeOf[T]()' violates the constraint of type 'T'.    System.ArgumentException: GenericArguments[0], 'Sandbox.ByteStream', on 'Int32 SizeOf[T]()' violates the constraint of type 'T'.
 ---> System.Security.VerificationException: Method System.Runtime.CompilerServices.Unsafe.SizeOf: type argument 'Sandbox.ByteStream' violates the constraint of type parameter 'T'.
   at System.RuntimeMethodHandle.GetStubIfNeeded(RuntimeMethodHandleInternal method, RuntimeType declaringType, RuntimeType[] methodInstantiation)
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   --- End of inner exception stack trace ---
   at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   at MonoMod.Utils.Extensions.ComputeManagedSize(Type t)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at MonoMod.Utils.Extensions.GetManagedSize(Type t)
   at MonoMod.Core.Platforms.Systems.WindowsSystem.ClassifyX64(Type type, Boolean isReturn)
   at MonoMod.Core.Platforms.Abi.Classify(Type type, Boolean isReturn)
   at MonoMod.Core.Platforms.PlatformTriple.GetRealDetourTarget(MethodBase from, MethodBase to)
   at MonoMod.Core.Platforms.PlatformTripleDetourFactory.Detour..ctor(PlatformTriple triple, MethodBase src, MethodBase dst)
   at MonoMod.Core.Platforms.PlatformTripleDetourFactory.CreateDetour(CreateDetourRequest request)
   at MonoMod.Core.DetourFactory.CreateDetour(IDetourFactory factory, MethodBase source, MethodBase target, Boolean applyByDefault)
   at Sandbox.ILHotload.TryReplaceMethod(MethodBase source, MethodBase replace)
2023/09/21 21:28:57.8966    [ILHotload/tools] Failed to replace method Hyperdrive.Player::GetDataStream 
        ILHotload.Replace - :0
        TypeLibrary.AddAssembly - :0
        ToolsDll.OnGameAssemblyLoaded - :0
        PackageLoader.LoadAssemblyFromPackage - :0
        PackageLoader.LoadPendingChanges - :0
        GameMenuDll.Tick - :0
        EngineLoop.FrameStart - :0
        EngineLoop.RunFrame - :0

To Reproduce

  1. Create a networked menu game
  2. Play a bit
  3. Hotload some code
  4. Observe crash

This occurs in our WIP game hyperdrive99. If you want access to the repo for easier reproduction, please reply, message us on Discord, or if you assign yourself to this issue I will simply add you to the repo.

Expected behavior

No crash 💯

Media/Files

No response

Additional context

No response

CarsonKompon commented 1 year ago

Can confirm this crash happened very frequently during the development of Pizza Clicker and Flappy Game so code may still be reproducable there as well: https://github.com/CarsonKompon/pizza_clicker https://github.com/CarsonKompon/flappy_game

trundlr commented 8 months ago

No longer valid