From 14c8532fa0c407a096b27cc7037a5dd6d54f4e55 Mon Sep 17 00:00:00 2001 From: Igor Pissolati Date: Mon, 28 Apr 2025 18:25:28 -0300 Subject: [PATCH] Add `Trap Flag` test --- src/samples/test-sample/test.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/samples/test-sample/test.cpp b/src/samples/test-sample/test.cpp index 70f909b2..03b41b4f 100644 --- a/src/samples/test-sample/test.cpp +++ b/src/samples/test-sample/test.cpp @@ -11,6 +11,7 @@ #define NOMINMAX #define WIN32_LEAN_AND_MEAN +#include #include #include #include @@ -641,6 +642,36 @@ namespace return test_access_violation_exception() && test_illegal_instruction_exception(); } + bool trap_flag_cleared = false; + constexpr DWORD TRAP_FLAG_MASK = 0x100; + + LONG NTAPI single_step_handler(PEXCEPTION_POINTERS exception_info) + { + if (exception_info->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP) + { + PCONTEXT context = exception_info->ContextRecord; + trap_flag_cleared = (context->EFlags & TRAP_FLAG_MASK) == 0; + return EXCEPTION_CONTINUE_EXECUTION; + } + + return EXCEPTION_CONTINUE_SEARCH; + } + + bool test_interrupts() + { + PVOID veh_handle = AddVectoredExceptionHandler(1, single_step_handler); + if (!veh_handle) + return false; + + __writeeflags(__readeflags() | TRAP_FLAG_MASK); + + __nop(); + + RemoveVectoredExceptionHandler(veh_handle); + + return trap_flag_cleared; + } + void print_time() { const auto epoch_time = std::chrono::system_clock::now().time_since_epoch(); @@ -698,6 +729,7 @@ int main(const int argc, const char* argv[]) RUN_TEST(test_env, "Environment") RUN_TEST(test_exceptions, "Exceptions") RUN_TEST(test_native_exceptions, "Native Exceptions") + RUN_TEST(test_interrupts, "Interrupts") RUN_TEST(test_tls, "TLS") RUN_TEST(test_socket, "Socket") RUN_TEST(test_apc, "APC")