From 5d59f069d11a0bb0f058f89c1f31ee9a3fd7f02e Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Thu, 6 Aug 2026 10:04:23 +0300 Subject: [PATCH] test: scope page-state allocation limits --- test_secure_buffer.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/test_secure_buffer.cpp b/test_secure_buffer.cpp index 41f9404..9b5ea34 100644 --- a/test_secure_buffer.cpp +++ b/test_secure_buffer.cpp @@ -225,6 +225,24 @@ struct PageStateAllocationControl { int PageStateAllocationControl::successful_allocations_before_failure{-1}; +class ScopedPageStateAllocationLimit { +public: + ScopedPageStateAllocationLimit() noexcept + : saved_limit_(PageStateAllocationControl::successful_allocations_before_failure) {} + + ~ScopedPageStateAllocationLimit() { + PageStateAllocationControl::successful_allocations_before_failure = saved_limit_; + } + + void set(int successful_allocations_before_failure) noexcept { + PageStateAllocationControl::successful_allocations_before_failure = + successful_allocations_before_failure; + } + +private: + int saved_limit_{}; +}; + template struct ThrowingPageStateAllocator { using value_type = T; @@ -605,7 +623,8 @@ TEST(PageLockRegistryTest, PageStateInsertionFailureReturnsFalseAndClearsPrepare using Registry = hmac_cpp::detail::page_lock_registry; RegistryPageLocker::reset(); - PageStateAllocationControl::successful_allocations_before_failure = 1; + ScopedPageStateAllocationLimit allocation_limit; + allocation_limit.set(1); Registry registry(4096); void* const range = reinterpret_cast(static_cast(0x1000)); @@ -614,11 +633,11 @@ TEST(PageLockRegistryTest, PageStateInsertionFailureReturnsFalseAndClearsPrepare // If prepared entries remained, this range would reuse the first one and // reach the OS locker despite allocations being disabled. - PageStateAllocationControl::successful_allocations_before_failure = 0; + allocation_limit.set(0); EXPECT_FALSE(registry.lock(range, 8)); EXPECT_EQ(RegistryPageLocker::event_count, 0u); - PageStateAllocationControl::successful_allocations_before_failure = -1; + allocation_limit.set(-1); EXPECT_TRUE(registry.lock(range, 8)); EXPECT_TRUE(registry.unlock(range, 8)); EXPECT_EQ(RegistryPageLocker::event_count, 2u);