Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.AccessViolationException in Il2CppInterop.Runtime functions when memory scanning #178

Open
Chozothebrozo opened this issue Sep 27, 2024 · 6 comments
Labels
bug Something isn't working injection

Comments

@Chozothebrozo
Copy link

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Il2CppInterop.Runtime.MemoryUtils.FindSignatureInBlock(IntPtr, Int64, Char[], Char[], Int64)
   at Il2CppInterop.Runtime.MemoryUtils.FindSignatureInBlock(IntPtr, Int64, System.String, System.String, Int64)
   at Il2CppInterop.Runtime.MemoryUtils.FindSignatureInModule(System.Diagnostics.ProcessModule, SignatureDefinition)
   at Il2CppInterop.Runtime.Injection.InjectorHelpers+<>c.<FindClassInit>b__26_1(SignatureDefinition)
   at System.Linq.Enumerable+SelectArrayIterator`2[[Il2CppInterop.Runtime.MemoryUtils+SignatureDefinition, Il2CppInterop.Runtime, Version=1.4.6.0, Culture=neutral, PublicKeyToken=null],[System.IntPtr, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at System.Linq.Enumerable.TryGetFirst[[System.IntPtr, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.Generic.IEnumerable`1<IntPtr>, System.Func`2<IntPtr,Boolean>, Boolean ByRef)
   at System.Linq.Enumerable.FirstOrDefault[[System.IntPtr, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.Generic.IEnumerable`1<IntPtr>, System.Func`2<IntPtr,Boolean>)
   at Il2CppInterop.Runtime.Injection.InjectorHelpers.FindClassInit()
   at Il2CppInterop.Runtime.Injection.InjectorHelpers.Setup()
   at Il2CppInterop.Runtime.Injection.ClassInjector.RegisterTypeInIl2Cpp(System.Type, Il2CppInterop.Runtime.Injection.RegisterTypeOptions)
   at Il2CppInterop.Runtime.DelegateSupport.ConvertDelegate[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Delegate)
   at UnityEngine.Application+LogCallback.op_Implicit(System.Action`3<System.String,System.String,UnityEngine.LogType>)
   at BepInEx.Unity.IL2CPP.Logging.IL2CPPUnityLogSource..ctor()
   at BepInEx.Unity.IL2CPP.IL2CPPChainloader.OnInvokeMethod(IntPtr, IntPtr, IntPtr, IntPtr)

This error is also similarly mentioned in #123 and #138 with different methods of the same Runtime package

This seems to be consistent on multiple plugin loaders/injectors using this package, recreatable in 3 different frameworks for myself (Bepinex, MelonLoader, and UnityInjector), with the error being during memory scanning in general and no other plugins included

At a glance of the code in this case and following the stack upwards,, ex : https://github.com/BepInEx/Il2CppInterop/blob/master/Il2CppInterop.Runtime/MemoryUtils.cs

I am wondering if there is just mishandling of the read error and it could be caught/handled somewhere earlier in the package to skip the specific address that errored and continue on, rather than erroring all the way back up the stack as a fatal error?

Thanks for your time!

@ds5678 ds5678 added bug Something isn't working injection labels Sep 28, 2024
@Chozothebrozo
Copy link
Author

So I read up a lot on this (and a lot I didnt fully understand), but it seems like this kind of exception isnt able to be caught by the application as its expected to be a critical failure by the system. Reading on some articles there were methods to get around this in earlier versions of .net but it seems like that is deprecated and no longer supported

https://stackoverflow.com/questions/3469368/how-to-handle-accessviolationexception
https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-accessviolationexception
https://learn.microsoft.com/en-us/dotnet/api/system.runtime.exceptionservices.handleprocesscorruptedstateexceptionsattribute?view=net-8.0

I cloned the project and tried to randomly add some of the method attribute and json config properties mentioned but no luck. So either I'm doing something wrong or I don't understand dotnet enough to know where to properly apply the config.

Anyway if you have any thoughts on this or another workaround that would be amazing :)

@ds5678
Copy link
Collaborator

ds5678 commented Oct 2, 2024

Despite having the same exception type, #123, #138, and this issue are unique and likely unrelated.

@Chozothebrozo
Copy link
Author

Hey just bumping. Not sure if u had a chance to check out some of the research I did on the exception handling or if we think this is a non-fixable item at this point?

@ds5678
Copy link
Collaborator

ds5678 commented Oct 9, 2024

Except for the generator work that @js6pak and I have been doing lately, I don't think anyone has been working on the project.

this is a non-fixable item at this point?

There is no evidence to support the conclusion that your issue can't be fixed.

@Elyriand21
Copy link

I've been having this exact same issue and the solution that worked for me was to

  1. Bring up properties for the game
  2. Go to Launch Options at the bottom of the popup window
  3. add "--melonloader.disablestartscreen" to the launch options

The start screen was causing a massive GPU spike, failling my drivers and also causing a memory read/write error.

@Chozothebrozo
Copy link
Author

Thanks for the suggestion but unfortunately that would not apply here. There is no start screen at all in this process., and the larger audience is not using MelonLoader as the plugin injection framework at all.

Additionally game in question is kicked off via a standalone launcher which then launches the exe below that, so doubt you'd be able to specify any launch parameters when not using a shortcut

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working injection
Projects
None yet
Development

No branches or pull requests

3 participants