From 7e662946c90a0dc37edb51595707c4c74d12e249 Mon Sep 17 00:00:00 2001 From: hugsy Date: Tue, 29 Oct 2024 10:55:37 -0700 Subject: [PATCH] renamed `CriticalSection` -> `UniqueCriticalSection` --- Modules/Common/Include/Handle.hpp | 27 +++++++++++++++++++++--- Modules/Process/Source/Win32/Process.cpp | 19 ++++++----------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Modules/Common/Include/Handle.hpp b/Modules/Common/Include/Handle.hpp index e6eb9ae..629ec44 100644 --- a/Modules/Common/Include/Handle.hpp +++ b/Modules/Common/Include/Handle.hpp @@ -5,10 +5,9 @@ #include "Common.hpp" /// -/// Scope-managed handles +/// @brief Scope-managed handles /// /// @ref https://andreasfertig.blog/2022/08/cpp-insights-lambdas-in-unevaluated-contexts/ -/// @link https://developercommunity.visualstudio.com/t/c20-internal-compiler-error-for-lambda-in-decltype/1631476 /// template using GenericHandle = std::unique_ptr< @@ -26,8 +25,30 @@ using GenericHandle = std::unique_ptr< #ifdef __linux__ using UniqueHandle = GenericHandle; #else -using UniqueHandle = GenericHandle; + +/// +/// @brief A unique (as-in `unique_ptr`) Windows handle. It will close itself on scope-exit. +/// +using UniqueHandle = GenericHandle; + +/// +/// @brief A unique (as-in `unique_ptr`) Windows module handle. +/// using UniqueLibraryHandle = GenericHandle; + +/// +/// @brief A unique Windows Critical Section. +/// +using UniqueCriticalSection = GenericHandle< + RTL_CRITICAL_SECTION, + [](RTL_CRITICAL_SECTION* p) + { + if ( p ) + { + ::LeaveCriticalSection(p); + p = nullptr; + } + }>; #endif // __linux__ using SharedHandle = std::shared_ptr; diff --git a/Modules/Process/Source/Win32/Process.cpp b/Modules/Process/Source/Win32/Process.cpp index 17b6271..0aae86e 100644 --- a/Modules/Process/Source/Win32/Process.cpp +++ b/Modules/Process/Source/Win32/Process.cpp @@ -27,13 +27,6 @@ usize GetPebLength(); EXTERN_C_END -using CriticalSection = GenericHandle< - RTL_CRITICAL_SECTION, - [](auto p) - { - ::LeaveCriticalSection(p); - }>; - namespace pwn::Process { @@ -590,12 +583,12 @@ Process::EnumerateLocalModules() { std::vector res; auto peb = Peb(); - CriticalSection csLoaderLock {[&]() - { - auto lock = peb->LoaderLock; - ::EnterCriticalSection(lock); - return lock; - }()}; + UniqueCriticalSection csLoaderLock {[&]() + { + auto lock = peb->LoaderLock; + ::EnterCriticalSection(lock); + return lock; + }()}; if ( !peb->Ldr->Initialized ) return Ok(res);