-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
ExecutionEngineException when hooking/detouring GetSysColor() #78086
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
There are a lot of limitations on what such p/invoke can do. The hook is likely violating the contract. |
Well that solves the mystery at least, thanks. Is there anything I can do? Should I take this up with one of the hooking libraries' authors? |
You can bring it up with them, but I do not think they will be able to do anything about it. It is not technically possible to safely hook OS APIs that are called by the runtime when the GC mode transition is suppressed. |
Fair enough, thanks for the info. |
What was the reason for suppression? It feels like it regressed various customer scenarios. |
See #37284 In general, we make no guarantees that it is possible to detour any OS APIs back into managed code. Many OS APIs are called in cooperative GC mode by the runtime and detouring them back into managed code is going result into a crash. The exact set is implementation detail and changing every release. |
Closing this as "by-design" |
Description
I have an app that hooks/detours a handful of Windows functions as part of custom dark mode theming for WinForms. Those functions are:
The behavior:
The minimal repro uses MinHook.NET to demonstrate, but the same crash occurs in the same way with every hooking library I can find (Reloaded.Hooks, CoreHook, MinSharp). I even tried making a small C++ dll that uses MinHook (the original native version) and sets up the hooking entirely on the native side, with the .NET side simply calling a single "Start the hooking stuff" function and not actually p/invoking anything else through, but it still crashes in the same way.
Apologies if this should go in the WinForms repo; GetSysColor() might be considered WinForms-adjacent-ish, but it's possible that it's just a coincidence and some detours just crash and this is one of them, not sure.
Reproduction Steps
https://fenphoenix.com/getsyscolor/GetSysColorHookTest.zip
"Now inside the body of hooked function GetThemeColor_Hooked"
"Now inside the body of hooked function DrawThemeBackground_Hooked"
Expected behavior
The detoured GetSysColor() function runs and returns correctly with no crash.
Actual behavior
The detoured GetSysColor() function crashes with an ExecutionEngineException.
Clicking "Show call stack" brings up:
Clicking "Copy details" simply results in:
Clicking "View details" brings up a "Quick Watch" window with a large amount of information but I can't find a way to copy it out, or if it's even relevant.
Regression?
Works in .NET Framework 4.8.
In .NET 5, it works in x86 mode only.
Known Workarounds
No response
Configuration
Other information
No response
The text was updated successfully, but these errors were encountered: