Currently AddVectoredExceptionHandlerFuncPtr and
RemoveVectoredExceptionHandlerFuncPtr are resolved by function named "ctor"
which is called only for GCC builds via __attribute__((constructor)).

Each function marked by this attribute is called from the DllMain()
function. DllMain() is called by DLL entry point function, and it is called
after executing all NT TLS callbacks.

Currenly the AddVectoredExceptionHandlerFuncPtr is used from the NT TLS
callback named __dyn_tls_pthread, hence it is always NULL and therefore
VectoredExceptionHandler is never used. This issue was introduced in the
commit cb7f42e05b94 ("winpthreads: Make winpthreads work on Win98").

Fix this problem and resolve AddVectoredExceptionHandler and
RemoveVectoredExceptionHandler pointers directly in the NT TLS callback
__dyn_tls_pthread, so they are available since beginning, and not just
after executing DllMain.
---
 mingw-w64-libraries/winpthreads/src/thread.c | 37 ++++++++++----------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/src/thread.c 
b/mingw-w64-libraries/winpthreads/src/thread.c
index 173e0666c15c..71dcae261820 100644
--- a/mingw-w64-libraries/winpthreads/src/thread.c
+++ b/mingw-w64-libraries/winpthreads/src/thread.c
@@ -95,19 +95,6 @@ SetThreadName_VEH (PEXCEPTION_POINTERS ExceptionInfo)
 
   return EXCEPTION_CONTINUE_SEARCH;
 }
-
-static PVOID (WINAPI *AddVectoredExceptionHandlerFuncPtr) (ULONG, 
PVECTORED_EXCEPTION_HANDLER);
-static ULONG (WINAPI *RemoveVectoredExceptionHandlerFuncPtr) (PVOID);
-
-static void __attribute__((constructor))
-ctor (void)
-{
-  HMODULE module = GetModuleHandleA("kernel32.dll");
-  if (module) {
-    AddVectoredExceptionHandlerFuncPtr = 
(__typeof__(AddVectoredExceptionHandlerFuncPtr)) GetProcAddress(module, 
"AddVectoredExceptionHandler");
-    RemoveVectoredExceptionHandlerFuncPtr = 
(__typeof__(RemoveVectoredExceptionHandlerFuncPtr)) GetProcAddress(module, 
"RemoveVectoredExceptionHandler");
-  }
-}
 #endif
 
 typedef struct _THREADNAME_INFO
@@ -448,16 +435,20 @@ replace_spin_keys (pthread_spinlock_t *old, 
pthread_spinlock_t new)
 static void WINAPI
 __dyn_tls_pthread (HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved)
 {
+#if defined(USE_VEH_FOR_MSC_SETTHREADNAME)
+  static PVOID (WINAPI *AddVectoredExceptionHandlerFuncPtr) (ULONG, PVOID) = 
NULL;
+  static ULONG (WINAPI *RemoveVectoredExceptionHandlerFuncPtr) (PVOID) = NULL;
+  static BOOL haveVectoredExceptionHandlerFuncs = FALSE;
+#endif
   _pthread_v *t = NULL;
   pthread_spinlock_t new_spin_keys = PTHREAD_SPINLOCK_INITIALIZER;
 
   if (dwReason == DLL_PROCESS_DETACH)
     {
 #if defined(USE_VEH_FOR_MSC_SETTHREADNAME)
-      if (lpreserved == NULL && SetThreadName_VEH_handle != NULL)
+      if (lpreserved == NULL && SetThreadName_VEH_handle != NULL && 
haveVectoredExceptionHandlerFuncs)
         {
-          if (RemoveVectoredExceptionHandlerFuncPtr != NULL)
-            RemoveVectoredExceptionHandlerFuncPtr (SetThreadName_VEH_handle);
+          RemoveVectoredExceptionHandlerFuncPtr (SetThreadName_VEH_handle);
           SetThreadName_VEH_handle = NULL;
         }
 #endif
@@ -466,10 +457,18 @@ __dyn_tls_pthread (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
   else if (dwReason == DLL_PROCESS_ATTACH)
     {
 #if defined(USE_VEH_FOR_MSC_SETTHREADNAME)
-      if (AddVectoredExceptionHandlerFuncPtr != NULL)
+      if (!haveVectoredExceptionHandlerFuncs)
+        {
+          HMODULE kernel32 = GetModuleHandleA("kernel32.dll");
+          if (kernel32)
+            {
+              AddVectoredExceptionHandlerFuncPtr = (PVOID (WINAPI *)(ULONG, 
PVOID))(void(*)(void)) GetProcAddress(kernel32, "AddVectoredExceptionHandler");
+              RemoveVectoredExceptionHandlerFuncPtr = (ULONG (WINAPI 
*)(PVOID))(void(*)(void)) GetProcAddress(kernel32, 
"RemoveVectoredExceptionHandler");
+              haveVectoredExceptionHandlerFuncs = 
AddVectoredExceptionHandlerFuncPtr != NULL && 
RemoveVectoredExceptionHandlerFuncPtr != NULL;
+            }
+        }
+      if (haveVectoredExceptionHandlerFuncs)
         SetThreadName_VEH_handle = AddVectoredExceptionHandlerFuncPtr (1, 
&SetThreadName_VEH);
-      else
-        SetThreadName_VEH_handle = NULL;
       /* Can't do anything on error anyway, check for NULL later */
 #endif
     }
-- 
2.20.1



_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to