From e4a97e84b9dfe75f9c1f91e7512fa93e949551dc Mon Sep 17 00:00:00 2001 From: momo5502 Date: Wed, 30 Oct 2024 15:24:29 +0100 Subject: [PATCH] Add exception tests --- src/test-sample/test.cpp | 47 +++++++++++++++++++++++ src/windows-emulator/windows_emulator.cpp | 10 +++-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/test-sample/test.cpp b/src/test-sample/test.cpp index 58aa7538..927a0fce 100644 --- a/src/test-sample/test.cpp +++ b/src/test-sample/test.cpp @@ -7,6 +7,10 @@ #include #include +// Externally visible and potentially modifiable state +// to trick compiler optimizations +__declspec(dllexport) bool do_the_task = true; + // getenv is broken right now :( std::string read_env(const char* env) { @@ -88,6 +92,47 @@ bool test_io() return text == buffer; } +void throw_exception() +{ + if (do_the_task) + { + throw std::runtime_error("OK"); + } +} + +bool test_exceptions() +{ + try + { + throw_exception(); + return false; + } + catch (const std::exception& e) + { + return e.what() == std::string("OK"); + } +} + +void throw_native_exception() +{ + if (do_the_task) + { + *reinterpret_cast(1) = 1; + } +} + +bool test_native_exceptions() +{ + __try + { + throw_native_exception(); + return false; + } + __except(EXCEPTION_EXECUTE_HANDLER) { + return true; + } +} + #define RUN_TEST(func, name) \ { \ printf("Running test '" name "': "); \ @@ -103,6 +148,8 @@ int main(int /*argc*/, const char* /*argv*/[]) RUN_TEST(test_io, "I/O") RUN_TEST(test_threads, "Threads") RUN_TEST(test_env, "Environment") + RUN_TEST(test_exceptions, "Exceptions") + RUN_TEST(test_native_exceptions, "Native Exceptions") return valid ? 0 : 1; } diff --git a/src/windows-emulator/windows_emulator.cpp b/src/windows-emulator/windows_emulator.cpp index 621ea073..393520bd 100644 --- a/src/windows-emulator/windows_emulator.cpp +++ b/src/windows-emulator/windows_emulator.cpp @@ -844,13 +844,15 @@ void windows_emulator::setup_hooks() if (type == memory_violation_type::protection) { - printf("Protection violation: 0x%llX (%zX) - %s at 0x%llX (%s)\n", address, size, permission.c_str(), ip, - name); + this->logger.print(color::gray, "Protection violation: 0x%llX (%zX) - %s at 0x%llX (%s)\n", address, size, + permission.c_str(), ip, + name); } else if (type == memory_violation_type::unmapped) { - printf("Mapping violation: 0x%llX (%zX) - %s at 0x%llX (%s)\n", address, size, permission.c_str(), ip, - name); + this->logger.print(color::gray, "Mapping violation: 0x%llX (%zX) - %s at 0x%llX (%s)\n", address, size, + permission.c_str(), ip, + name); } if (this->fuzzing)