From 9d431118295874cea317045da68fc3ca46ab9cd4 Mon Sep 17 00:00:00 2001 From: Peace-Maker Date: Wed, 2 May 2018 13:37:41 +0200 Subject: [PATCH] Don't call sourcepawn detour callbacks from threads other than the main thread The SourcePawn VM isn't threadsafe. --- dynhooks_sourcepawn.cpp | 4 ++++ extension.cpp | 3 +++ extension.h | 2 ++ 3 files changed, 9 insertions(+) diff --git a/dynhooks_sourcepawn.cpp b/dynhooks_sourcepawn.cpp index 03a885c..1d9158a 100644 --- a/dynhooks_sourcepawn.cpp +++ b/dynhooks_sourcepawn.cpp @@ -252,6 +252,10 @@ bool UpdateRegisterArgumentSizes(CHook* pDetour, HookSetup *setup) // Central handler for all detours. Heart of the detour support. ReturnAction_t HandleDetour(HookType_t hookType, CHook* pDetour) { + // Can't call into SourcePawn offthread. + if (g_MainThreadId != ke::GetCurrentThreadId()) + return ReturnAction_Ignored; + DetourMap *map; if (hookType == HOOKTYPE_PRE) map = &g_pPreDetours; diff --git a/extension.cpp b/extension.cpp index 0e905b3..c7e3105 100644 --- a/extension.cpp +++ b/extension.cpp @@ -15,6 +15,8 @@ HandleType_t g_HookSetupHandle = 0; HandleType_t g_HookParamsHandle = 0; HandleType_t g_HookReturnHandle = 0; +ke::ThreadId g_MainThreadId; + bool DHooks::SDK_OnLoad(char *error, size_t maxlength, bool late) { HandleError err; @@ -59,6 +61,7 @@ bool DHooks::SDK_OnLoad(char *error, size_t maxlength, bool late) g_pEntityListener = new DHooksEntityListener(); g_pSignatures = new SignatureGameConfig(); + g_MainThreadId = ke::GetCurrentThreadId(); return true; } diff --git a/extension.h b/extension.h index c6e7045..f66276a 100644 --- a/extension.h +++ b/extension.h @@ -43,6 +43,7 @@ #include #include #include "sdk-hacks.h" +#include /** * @brief Sample implementation of the SDK Extension. @@ -129,4 +130,5 @@ public: }; extern SourceHook::IHookManagerAutoGen *g_pHookManager; extern sp_nativeinfo_t g_Natives[]; +extern ke::ThreadId g_MainThreadId; #endif // _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_