From d64e73ad6e2d85dad14b729c5e72283bf40f2141 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 4 Jan 2025 11:06:10 +0100 Subject: [PATCH] Add test for illegal instructions --- src/test-sample/test.cpp | 44 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/test-sample/test.cpp b/src/test-sample/test.cpp index 4d146e56..6842432c 100644 --- a/src/test-sample/test.cpp +++ b/src/test-sample/test.cpp @@ -234,7 +234,7 @@ bool test_exceptions() } } -void throw_native_exception() +void throw_access_violation() { if (do_the_task) { @@ -242,19 +242,55 @@ void throw_native_exception() } } -bool test_native_exceptions() +bool test_access_violation_exception() { __try { - throw_native_exception(); + throw_access_violation(); return false; } __except (EXCEPTION_EXECUTE_HANDLER) { - return true; + return GetExceptionCode() == STATUS_ACCESS_VIOLATION; } } +bool test_ud2_exception(void* address) +{ + __try + { + static_cast(address)(); + return false; + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + return GetExceptionCode() == STATUS_ILLEGAL_INSTRUCTION; + } +} + +bool test_illegal_instruction_exception() +{ + const auto address = VirtualAlloc(nullptr, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE); + if (!address) + { + return false; + } + + memcpy(address, "\x0F\x0B", 2); // ud2 + + const auto res = test_ud2_exception(address); + + VirtualFree(address, 0x1000, MEM_RELEASE); + + return res; +} + +bool test_native_exceptions() +{ + return test_access_violation_exception() + && test_illegal_instruction_exception(); +} + void print_time() { const auto epoch_time = std::chrono::system_clock::now().time_since_epoch();