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

Crash after working some time. #34

Open
mrzaxaryan opened this issue Sep 23, 2023 · 13 comments
Open

Crash after working some time. #34

mrzaxaryan opened this issue Sep 23, 2023 · 13 comments

Comments

@mrzaxaryan
Copy link

mrzaxaryan commented Sep 23, 2023

Hi, thank you for your great job.
I compile MemoryModulePP to MemoryModulePP.dll
I have host.exe that is dynamically linked with MemoryModulePP.dll
Host.exe loads plugin.dll from byte[] using MemoryModulePP
App crash with Fail Fast exception after working for some time.

@mrzaxaryan mrzaxaryan changed the title Crash after working for while. Crash after working some time. Sep 23, 2023
@bb107
Copy link
Owner

bb107 commented Sep 23, 2023

Hi. Can you tell me the details about this exception? Thanks.

@mrzaxaryan
Copy link
Author

mrzaxaryan commented Sep 23, 2023

No exception is thrown app just crash.
From event viewer.
Faulting application name: Host.exe, version: 1.0.0.0, time stamp: 0x650ea3ea
Faulting module name: 3F929562B0A1EF11.DLL, version: 0.0.0.0, time stamp: 0x650e2dbc
Exception code: 0xc0000005
Fault offset: 0x000000000001b830
Faulting process id: 0x4094
Faulting application start time: 0x01d9edf9569ff980
Faulting application path: D:...\publish\Host.exe
Faulting module path: 6F80ED08F9C18F55\3F929562B0A1EF11.DLL
Report Id: 1e22cf37-50e8-44ed-9472-687aede1b8be
Faulting package full name:
Faulting package-relative application ID:

@mrzaxaryan
Copy link
Author

WinDbg
(c48.7adc): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
00000297b0fa82fa 48032cc8 add rbp,qword ptr [rax+rcx*8] ds:000002d7b25cb0d0=????????????????

@mrzaxaryan
Copy link
Author

mrzaxaryan commented Sep 23, 2023

I make a simple test plugin.dll that creates a new thread and prints "Hello World!" in console after printing app crash with Access violation - code c0000005
I think that it will make sense to write that plugin is written using C# net 8.0rc1 compiled using NativeAOT toolchain
repro

pfn = GetProcAddress(hModule, "thread");
if (pfn && pfn()) {
	printf("thread test failed.\n");
}

test.zip

@bb107
Copy link
Owner

bb107 commented Sep 24, 2023

Hi, I just created the MmpTlsFiber branch to solve this problem.

@mrzaxaryan
Copy link
Author

I can confirm that issue is fixed.
Can you add crypto wallet address into readme for donations?

@mrzaxaryan
Copy link
Author

Second round.
Steps.

  1. Load dll.
  2. Call dll method.
  3. App crashed.
    I think app is crashed on method return.

@mrzaxaryan mrzaxaryan closed this as not planned Won't fix, can't repro, duplicate, stale Sep 25, 2023
@mrzaxaryan mrzaxaryan reopened this Sep 25, 2023
@bb107
Copy link
Owner

bb107 commented Sep 25, 2023

Any exception details in debugger?

@mrzaxaryan
Copy link
Author

exception

Exception thrown at 0x0000022A85E7E140 in test.exe: 0xC0000005: 
Access violation executing location 0x0000022A85E7E140.

repro
test.zip

pfn = GetProcAddress(hModule, "thread");
if (pfn && pfn()) {
	printf("thread test failed.\n");
}

@bb107
Copy link
Owner

bb107 commented Sep 27, 2023

The reason for this problem is that calling the thread function creates a thread that runs continuously and does not stop when the function returns. If you call FreeLibraryMemory at this point to release the module, it will result in a memory access conflict for the aforementioned thread. The solution is not to release this module.
In addition, if the thread function is called, the module's load count will be set to -1, which means that FreeLibrary will not unload this module.
screenshot

@mrzaxaryan
Copy link
Author

App is crashed before FreeLibraryMemory is called.
After dll's method is called and on return application crashes.

@bb107
Copy link
Owner

bb107 commented Sep 27, 2023

Can you explain what happened in this method?

@bb107
Copy link
Owner

bb107 commented Sep 28, 2023

In addition, if the DLL is a .NET assembly, the DLL file buffer passed to the LoadLibraryMemory series of functions must be released after the DLL is unloaded. Because CLR.DLL will remap program files, but I'm not sure if it is still needed after Native AOT. If you can provide a sample, it can help me locate the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants