-
Notifications
You must be signed in to change notification settings - Fork 112
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
Windows: Remove exception handler for pagemap on unloading #746
Conversation
…exception handler.
src/snmalloc/pal/pal_windows.h
Outdated
if (!g_Handler) | ||
{ | ||
g_Handler = AddVectoredExceptionHandler(1, HandleReadonlyLazyCommit); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about something like this:
if (!g_Handler) | |
{ | |
g_Handler = AddVectoredExceptionHandler(1, HandleReadonlyLazyCommit); | |
} | |
} | |
// Keep a handle for the exception handler, so we can remove it later | |
// when needed. | |
static PVOID g_Handler{}; | |
// Destructor for removing exception handler. | |
static OnDestruct tidy([](){ | |
if (g_Handler) | |
{ | |
RemoveVectoredExceptionHandler(g_Handler); | |
g_Handler = NULL; // Prevent dangling pointer | |
} | |
}); | |
// Add exception handler for lazy commit. | |
if (!g_Handler) | |
{ | |
g_Handler = AddVectoredExceptionHandler(1, HandleReadonlyLazyCommit); | |
} | |
} |
It wouldn't need the Singleton to change, and I believe should get called in the right place to remove the exception handler?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is using the
snmalloc/src/snmalloc/ds_core/helpers.h
Lines 71 to 86 in a3fe420
/** | |
* Helper class to execute a specified function on destruction. | |
*/ | |
template<typename F> | |
class OnDestruct | |
{ | |
F f; | |
public: | |
OnDestruct(F f) : f(f) {} | |
~OnDestruct() | |
{ | |
f(); | |
} | |
}; |
which is how I typically get things to get destructed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems like it was made exactly for this situation 👍
I'll just try it out today and update the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for addressing the feedback. This is not currently tested by CI. Can you confirm that it addresses your issue, then I can merge.
Everything looks good on my end. It is now able to remove the exception handler. Thanks. |
When using snmalloc in a DLL, the exception handler is not removed when the DLL is unloaded. This will cause a crash if the EXE later throws an exception.
Example:
To resolve this, I'm adding code to call RemoveVectoredExceptionHandler when the Singleton is destroyed. That way MSVC won't have a dangling function pointer after the DLL is unloaded.
Note: This adds an extra template param to Singleton class, which probably will affect other platforms, which I have not updated.