| 1 | // Copyright 2014 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <chrono> |
| 5 | |
| 6 | #include "Common/CommonTypes.h" |
| 7 | #include "Common/ScopeGuard.h" |
| 8 | #include "Core/Core.h" |
| 9 | #include "Core/MemTools.h" |
| 10 | #include "Core/PowerPC/JitInterface.h" |
| 11 | #include "Core/System.h" |
| 12 | |
| 13 | #include "StubJit.h" |
| 14 | |
| 15 | // include order is important |
| 16 | #include <gtest/gtest.h> // NOLINT |
| 17 | |
| 18 | enum |
| 19 | { |
| 20 | #ifdef _WIN32 |
| 21 | PAGE_GRAN = 0x10000 |
| 22 | #else |
| 23 | PAGE_GRAN = 0x1000 |
| 24 | #endif |
| 25 | }; |
| 26 | |
| 27 | class PageFaultFakeJit : public StubJit |
| 28 | { |
| 29 | public: |
| 30 | explicit PageFaultFakeJit(Core::System& system) : StubJit(system) {} |
| 31 | |
| 32 | bool HandleFault(uintptr_t access_address, SContext* ctx) override |
| 33 | { |
| 34 | m_pre_unprotect_time = std::chrono::high_resolution_clock::now(); |
| 35 | Common::UnWriteProtectMemory(m_data, PAGE_GRAN, /*allowExecute*/ false); |
| 36 | m_post_unprotect_time = std::chrono::high_resolution_clock::now(); |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | void* m_data = nullptr; |
| 41 | std::chrono::time_point<std::chrono::high_resolution_clock> m_pre_unprotect_time, |
| 42 | m_post_unprotect_time; |
| 43 | }; |
| 44 | |
| 45 | #ifdef _MSC_VER |
| 46 | #define ASAN_DISABLE __declspec(no_sanitize_address) |
| 47 | #else |
| 48 | #define ASAN_DISABLE |
| 49 | #endif |
| 50 | |
| 51 | static void ASAN_DISABLE perform_invalid_access(void* data) |
| 52 | { |
| 53 | *(volatile int*)data = 5; |
| 54 | } |
| 55 | |
| 56 | TEST(PageFault, PageFault) |
| 57 | { |
| 58 | if (!EMM::IsExceptionHandlerSupported()) |
| 59 | GTEST_SKIP() << "Skipping PageFault test because exception handler is unsupported."; |
| 60 | |
| 61 | EMM::InstallExceptionHandler(); |
| 62 | void* data = Common::AllocateMemoryPages(PAGE_GRAN); |
| 63 | EXPECT_NE(data, nullptr); |
| 64 | Common::WriteProtectMemory(data, PAGE_GRAN, false); |
| 65 | |
| 66 | Core::DeclareAsCPUThread(); |
| 67 | Common::ScopeGuard cpu_thread_guard([] { Core::UndeclareAsCPUThread(); }); |
| 68 | |
| 69 | auto& system = Core::System::GetInstance(); |
| 70 | auto unique_pfjit = std::make_unique<PageFaultFakeJit>(system); |
| 71 | auto& pfjit = *unique_pfjit; |
| 72 | system.GetJitInterface().SetJit(std::move(unique_pfjit)); |
| 73 | pfjit.m_data = data; |
| 74 | |
| 75 | auto start = std::chrono::high_resolution_clock::now(); |
| 76 | perform_invalid_access(data); |
| 77 | auto end = std::chrono::high_resolution_clock::now(); |
| 78 | |
| 79 | auto difference_in_nanoseconds = [](auto diff_start, auto diff_end) { |
| 80 | return std::chrono::duration_cast<std::chrono::nanoseconds>(diff_end - diff_start).count(); |
| 81 | }; |
| 82 | |
| 83 | EMM::UninstallExceptionHandler(); |
| 84 | |
| 85 | fmt::print("page fault timing:\n"); |
| 86 | fmt::print("start->HandleFault {} ns\n", |
| 87 | difference_in_nanoseconds(start, pfjit.m_pre_unprotect_time)); |
| 88 | fmt::print("UnWriteProtectMemory {} ns\n", |
| 89 | difference_in_nanoseconds(pfjit.m_pre_unprotect_time, pfjit.m_post_unprotect_time)); |
| 90 | fmt::print("HandleFault->end {} ns\n", |
| 91 | difference_in_nanoseconds(pfjit.m_post_unprotect_time, end)); |
| 92 | fmt::print("total {} ns\n", difference_in_nanoseconds(start, end)); |
| 93 | |
| 94 | system.GetJitInterface().SetJit(nullptr); |
| 95 | } |