Skip to content

Commit

Permalink
renamed CriticalSection -> UniqueCriticalSection
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Oct 29, 2024
1 parent 06db740 commit 7e66294
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
27 changes: 24 additions & 3 deletions Modules/Common/Include/Handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T, auto Deleter>
using GenericHandle = std::unique_ptr<
Expand All @@ -26,8 +25,30 @@ using GenericHandle = std::unique_ptr<
#ifdef __linux__
using UniqueHandle = GenericHandle<FILE, ::fclose>;
#else
using UniqueHandle = GenericHandle<void, ::CloseHandle>;

///
/// @brief A unique (as-in `unique_ptr`) Windows handle. It will close itself on scope-exit.
///
using UniqueHandle = GenericHandle<void, ::CloseHandle>;

///
/// @brief A unique (as-in `unique_ptr`) Windows module handle.
///
using UniqueLibraryHandle = GenericHandle<HINSTANCE__, ::FreeLibrary>;

///
/// @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<UniqueHandle>;
19 changes: 6 additions & 13 deletions Modules/Process/Source/Win32/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ usize
GetPebLength();
EXTERN_C_END

using CriticalSection = GenericHandle<
RTL_CRITICAL_SECTION,
[](auto p)
{
::LeaveCriticalSection(p);
}>;


namespace pwn::Process
{
Expand Down Expand Up @@ -590,12 +583,12 @@ Process::EnumerateLocalModules()
{
std::vector<LDR_DATA_TABLE_ENTRY> 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);
Expand Down

0 comments on commit 7e66294

Please # to comment.