MemoryModulePP icon indicating copy to clipboard operation
MemoryModulePP copied to clipboard

Instablity Crash on Vulkan App using DLL with TLS and On Exit

Open CycloneRing opened this issue 1 year ago • 8 comments

Hi @bb107, After 2 years I'm back :D

I am working on a complicated project in Godot game engine. My project require loading some dll from memory which uses TLS (from Godot SDK) I tried many manual mappers and only one that works properly is yours which is really impressive.

However, After using MemoryModulePP (Static Link, DLL) It causes a instability in Godot when it tries to initialize Vulkan, D3D12, Sound Engine etc. and Leads to a _debugbreak And also It causes crash on Exit.

I did try many things and spent 3 days on debugging But I couldn't find the root, All I know is this is caused by loading MemoryModulePP and I guess it's caused by the hooks, Note that this only happens after loading the dll with TLS. Without TLS it looks fine.

I prepared a fully pre-setup project for looking into the issue which can be downloaded here.

This project contains Godot 4.3 engine, pre-built Godot SDK and GDExtension that uses MemoryModulePP. However in this test project I couldn't reproduce the startup crash on vulkan but exit crash is happening which I believe it's connected to the same startup bug.

This is a very critical project for me and I will really appreciate it if you can help me diagnose the issue and fix it.

One important thing is the startup initialization crash always happens after calling to RtlFreeHeap and raised by RtlReportCriticalFaliure and it happens same on Vulkan, D3D12, XInput etc.

Startup issue only happens randomly sometimes it doesn't happen sometimes it does.

image

Also sometimes break will be on MemoryModulePP itself at this line.

Do you have any idea or lead to fix the issue? Thanks

Update 2 :

I got some clue on what is happening, So I believe something going wrong with hooks and things done in MemoryModulePP initialization.

  • Linking statically against MemoryModulePP.lib = Startup Random Crash Happens
  • Linking dynamically against MemoryModulePP.dll = Startup Random Crash Happens
  • Linking dynamically against MemoryModulePP.dll As Delayed DLL = Stable Startup

So If I delay loading of MemoryModule.dll which happens when it is needed to be called from, Startup instability won't happen.

CycloneRing avatar Sep 30 '24 06:09 CycloneRing

Hi, thanks for your feedback, I will find time to test and fix this issue.

bb107 avatar Sep 30 '24 17:09 bb107

Hi, I just created a fix for this issue and it works fine on my computer. Please check out the MmpTlsFixup branch to test. For the crash on exit, just add the following code to DllMain to perform the cleanup:

if (fdwReason == DLL_PROCESS_DETACH) {
	MmCleanup();
}

bb107 avatar Oct 07 '24 18:10 bb107

In addition, you need to add the _HAS_AUTO_INITIALIZE macro definition to the client project that references MMPP if you need to call MmInitialize automatically.

bb107 avatar Oct 07 '24 18:10 bb107

@bb107 Thanks a lot, I will test and let you know.

CycloneRing avatar Oct 12 '24 09:10 CycloneRing

@bb107 Hey! I tested MmpTlsFixup in my project and now it seems working just fine. Thank you for your dedication, You truly made the most reliable PE loader for Windows.

Also it seems it fixed some parts of this issue https://github.com/bb107/MemoryModulePP/issues/53 However there is still a random crash on Mutex and it's very hard to reproduce. It doesn't happen if we comment out this line

You can close this issue and mark it as fixed. I appreciate if you can look or guess the root of the follow up issue.

CycloneRing avatar Oct 31 '24 07:10 CycloneRing

@bb107 New update, Now it crashes randomly in mid app image

CycloneRing avatar Oct 31 '24 23:10 CycloneRing

Hi, could you provide an example to reproduce these problems?

bb107 avatar Nov 07 '24 09:11 bb107

Hi, could you provide an example to reproduce these problems?

Hi @bb107, Unfortunately they happen rarely for instance, I am using my project for 3 days and it didn't happen again, My guess is it's all about threading and timing. I update the repo if I could find more consistent data to provide you.

CycloneRing avatar Nov 08 '24 21:11 CycloneRing