From 4d99b6affdf11df7f352b54aec170384c7a7b344 Mon Sep 17 00:00:00 2001 From: Nemirtingas Date: Fri, 11 Oct 2019 00:51:03 +0200 Subject: [PATCH] Fix race condition Fix race condition when renderer was found and we were hooking a library func --- overlay_experimental/Renderer_Detector.cpp | 27 ++++++++++++------- overlay_experimental/Renderer_Detector.h | 2 ++ overlay_experimental/windows/Windows_Hook.cpp | 1 - 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/overlay_experimental/Renderer_Detector.cpp b/overlay_experimental/Renderer_Detector.cpp index 18b8446..72fcdec 100644 --- a/overlay_experimental/Renderer_Detector.cpp +++ b/overlay_experimental/Renderer_Detector.cpp @@ -522,18 +522,24 @@ void Renderer_Detector::find_renderer_proc(Renderer_Detector* _this) std::vector::const_iterator it = libraries.begin(); while (it != libraries.end()) { - it = std::find_if(it, libraries.end(), [](std::string const& name) { - auto x = GetModuleHandle(name.c_str()); - if (x != NULL) - return true; - return false; - }); + { + std::lock_guard lock(_this->_found_mutex); + if (_this->_renderer_found) + break; - if (it == libraries.end()) - break; + it = std::find_if(it, libraries.end(), [](std::string const& name) { + auto x = GetModuleHandle(name.c_str()); + if (x != NULL) + return true; + return false; + }); - _this->create_hook(it->c_str()); - ++it; + if (it == libraries.end()) + break; + + _this->create_hook(it->c_str()); + ++it; + } } std::this_thread::sleep_for(std::chrono::milliseconds(500)); @@ -824,6 +830,7 @@ extern "C" void* dlsym(void* handle, const char* name) void Renderer_Detector::renderer_found(Base_Hook* hook) { + std::lock_guard lock(_found_mutex); Hook_Manager& hm = Hook_Manager::Inst(); _renderer_found = true; diff --git a/overlay_experimental/Renderer_Detector.h b/overlay_experimental/Renderer_Detector.h index e0e5939..544e441 100644 --- a/overlay_experimental/Renderer_Detector.h +++ b/overlay_experimental/Renderer_Detector.h @@ -16,6 +16,7 @@ class Renderer_Detector private: // Variables std::thread* _hook_thread; + std::mutex _found_mutex; unsigned int _hook_retries; bool _renderer_found; // Is the renderer hooked ? bool _dx9_hooked; @@ -70,6 +71,7 @@ class Renderer_Detector { // Variables std::thread* _hook_thread; + std::mutex _found_mutex; unsigned int _hook_retries; bool _oglx_hooked; bool _renderer_found; // Is the renderer hooked ? diff --git a/overlay_experimental/windows/Windows_Hook.cpp b/overlay_experimental/windows/Windows_Hook.cpp index 40dea26..0adbedc 100644 --- a/overlay_experimental/windows/Windows_Hook.cpp +++ b/overlay_experimental/windows/Windows_Hook.cpp @@ -165,7 +165,6 @@ UINT WINAPI Windows_Hook::MyGetRawInputData(HRAWINPUT hRawInput, UINT uiCommand, RawMouseEvent(*reinterpret_cast(pData)); - //memset(pData, 0, *pcbSize); *pcbSize = 0; return 0;