From d79cfb0614dfa256cfb43ba41ad1467d47f7b791 Mon Sep 17 00:00:00 2001 From: Exverge Date: Thu, 16 Jul 2026 16:06:27 -0400 Subject: [PATCH] [core] log error codes for failed VirtualAllocs --- src/common/host_memory.cpp | 20 +++++++++++++------- src/common/multi_level_page_table.inc | 6 +++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index 4179a95eb2..aac728f8ad 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -149,21 +149,21 @@ public: // Allocate backing file map backing_handle = pfn_CreateFileMapping2(INVALID_HANDLE_VALUE, nullptr, FILE_MAP_WRITE | FILE_MAP_READ, PAGE_READWRITE, SEC_COMMIT, backing_size, nullptr, nullptr, 0); if (!backing_handle) { - LOG_CRITICAL(HW_Memory, "Failed to allocate {} MiB of backing memory", backing_size >> 20); + LOG_CRITICAL(HW_Memory, "Failed to allocate {} MiB of backing memory, error {}", backing_size >> 20, GetLastError()); return false; } // Allocate a virtual memory for the backing file map as placeholder backing_base = static_cast(pfn_VirtualAlloc2(process, nullptr, backing_size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, nullptr, 0)); if (!backing_base) { Release(); - LOG_CRITICAL(HW_Memory, "Failed to reserve {} MiB of virtual memory", backing_size >> 20); + LOG_CRITICAL(HW_Memory, "Failed to reserve {} MiB of virtual memory, error {}", backing_size >> 20, GetLastError()); return false; } // Map backing placeholder void* const ret = pfn_MapViewOfFile3(backing_handle, process, backing_base, 0, backing_size, MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, nullptr, 0); if (ret != backing_base) { Release(); - LOG_CRITICAL(HW_Memory, "Failed to map {} MiB of virtual memory", backing_size >> 20); + LOG_CRITICAL(HW_Memory, "Failed to map {} MiB of virtual memory, error {}", backing_size >> 20, GetLastError()); return false; } @@ -176,7 +176,7 @@ public: auto res = pfn_VirtualQuery(reinterpret_cast(cursor), &info, sizeof(info)); if (res == 0) { - LOG_WARNING(HW_Memory, "Failed to check memory region: {}", GetLastError()); + LOG_WARNING(HW_Memory, "Failed to check memory region, error {}", GetLastError()); break; } @@ -190,12 +190,18 @@ public: if (virtual_base) { break; } else { - LOG_WARNING(HW_Memory, "Failed to allocate buffer at {:#x}, trying at at new address", start_aligned); + LOG_WARNING(HW_Memory, "Failed to allocate buffer at {:#x} with error {}, trying at at new address", start_aligned, GetLastError()); } } } - cursor = reinterpret_cast(info.BaseAddress) + info.RegionSize; + auto new_cursor = reinterpret_cast(info.BaseAddress) + info.RegionSize; + if (new_cursor <= cursor) { + // weird unknown error, let's just continue cursor so this isn't an infinite loop + cursor = cursor + HugePageSize; + continue; + } + cursor = new_cursor; } // Check if we failed to allocate for direct-mapping, otherwise map normally if (!virtual_base) { @@ -206,7 +212,7 @@ public: virtual_map_base = virtual_base; if (!virtual_base) { Release(); - LOG_CRITICAL(HW_Memory, "Failed to reserve {} GiB of virtual memory", virtual_size >> 30); + LOG_CRITICAL(HW_Memory, "Failed to reserve {} GiB of virtual memory, error {}", virtual_size >> 30, GetLastError()); return false; } return true; diff --git a/src/common/multi_level_page_table.inc b/src/common/multi_level_page_table.inc index 79125e8e0b..a2e61a161e 100644 --- a/src/common/multi_level_page_table.inc +++ b/src/common/multi_level_page_table.inc @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: 2026 Eden Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + // SPDX-FileCopyrightText: 2021 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later @@ -28,6 +31,7 @@ MultiLevelPageTable::MultiLevelPageTable(std::size_t address_space_bit first_level_map.resize(first_level_size, nullptr); #ifdef _WIN32 void* base{VirtualAlloc(nullptr, alloc_size, MEM_RESERVE, PAGE_READWRITE)}; + ASSERT_MSG(base, "Failed to allocate {} memory for page table, error {}", alloc_size, GetLastError()); #else void* base{mmap(nullptr, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)}; if (base == MAP_FAILED) @@ -63,7 +67,7 @@ void MultiLevelPageTable::AllocateLevel(u64 index) { void* ptr = reinterpret_cast(base_ptr) + index * first_level_chunk_size; #ifdef _WIN32 void* base = VirtualAlloc(ptr, first_level_chunk_size, MEM_COMMIT, PAGE_READWRITE); - ASSERT(base); + ASSERT_MSG(base, "Failed to allocate page level {} for page level, error {}", index, GetLastError()); #else void* base = ptr; #endif