From b9388935990c80c4ef35f33650ef236ffb39217c Mon Sep 17 00:00:00 2001 From: Zephyron Date: Sat, 18 Jan 2025 15:19:45 +1000 Subject: [PATCH] memory: Improve null pointer and unmapped memory handling - Update vcpkg baseline to a42af01b72c28a8e1d7b48107b33e4f286a55ef6 - Add SPIRV-Tools and SPIRV-Headers as submodules - Update Vulkan-related submodules to latest stable versions - Improve memory access error handling: - Add specific handling for null pointer accesses in ARM32 emulation - Return 0 for null pointer reads instead of undefined behavior - Silently ignore writes to null pointers - Add more detailed error messages distinguishing between null pointer access and other unmapped memory errors - Treat addresses below 0x1000 as potential null pointer accesses These changes should provide more graceful handling of null pointer accesses and improve stability when running games that attempt invalid memory operations. --- .gitmodules | 6 ++++++ externals/SPIRV-Headers | 1 + externals/SPIRV-Tools | 1 + externals/Vulkan-Headers | 2 +- externals/Vulkan-Utility-Libraries | 2 +- externals/VulkanMemoryAllocator | 2 +- externals/vcpkg | 2 +- src/core/arm/dynarmic/arm_dynarmic_32.cpp | 7 +++++++ src/core/memory.cpp | 16 ++++++++++++++++ vcpkg.json | 2 +- 10 files changed, 36 insertions(+), 5 deletions(-) create mode 160000 externals/SPIRV-Headers create mode 160000 externals/SPIRV-Tools diff --git a/.gitmodules b/.gitmodules index 0d8210906..8f1cc4342 100644 --- a/.gitmodules +++ b/.gitmodules @@ -67,3 +67,9 @@ [submodule "Vulkan-Utility-Libraries"] path = externals/Vulkan-Utility-Libraries url = https://github.com/KhronosGroup/Vulkan-Utility-Libraries.git +[submodule "externals/SPIRV-Tools"] + path = externals/SPIRV-Tools + url = https://github.com/KhronosGroup/SPIRV-Tools.git +[submodule "externals/SPIRV-Headers"] + path = externals/SPIRV-Headers + url = https://github.com/KhronosGroup/SPIRV-Headers.git diff --git a/externals/SPIRV-Headers b/externals/SPIRV-Headers new file mode 160000 index 000000000..2b2e05e08 --- /dev/null +++ b/externals/SPIRV-Headers @@ -0,0 +1 @@ +Subproject commit 2b2e05e088841c63c0b6fd4c9fb380d8688738d3 diff --git a/externals/SPIRV-Tools b/externals/SPIRV-Tools new file mode 160000 index 000000000..b9d5ced92 --- /dev/null +++ b/externals/SPIRV-Tools @@ -0,0 +1 @@ +Subproject commit b9d5ced92ac454caf526c3b80d5105a1f38878ce diff --git a/externals/Vulkan-Headers b/externals/Vulkan-Headers index d4a196d8c..a03d2f6d5 160000 --- a/externals/Vulkan-Headers +++ b/externals/Vulkan-Headers @@ -1 +1 @@ -Subproject commit d4a196d8c84e032d27f999adcea3075517c1c97f +Subproject commit a03d2f6d5753b365d704d58161825890baad0755 diff --git a/externals/Vulkan-Utility-Libraries b/externals/Vulkan-Utility-Libraries index 5a88b6042..7b23ba7a5 160000 --- a/externals/Vulkan-Utility-Libraries +++ b/externals/Vulkan-Utility-Libraries @@ -1 +1 @@ -Subproject commit 5a88b6042edb8f03eefc8de73bd73a899989373f +Subproject commit 7b23ba7a5f86936a8d783baf64a77c38977d6890 diff --git a/externals/VulkanMemoryAllocator b/externals/VulkanMemoryAllocator index f74c2d906..7ab8483d1 160000 --- a/externals/VulkanMemoryAllocator +++ b/externals/VulkanMemoryAllocator @@ -1 +1 @@ -Subproject commit f74c2d906f1537114fe0c0d855d5d27db91898cb +Subproject commit 7ab8483d10b665ba8d478e1502380c40e2374ac7 diff --git a/externals/vcpkg b/externals/vcpkg index d4f3e1220..cf035d991 160000 --- a/externals/vcpkg +++ b/externals/vcpkg @@ -1 +1 @@ -Subproject commit d4f3e122069912636c123b7ece673f6e01513cea +Subproject commit cf035d9916a0a23042b41fcae7ee0386d245af08 diff --git a/src/core/arm/dynarmic/arm_dynarmic_32.cpp b/src/core/arm/dynarmic/arm_dynarmic_32.cpp index 36478f722..28d062e7a 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_32.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic_32.cpp @@ -95,6 +95,13 @@ public: LOG_CRITICAL(Core_ARM, "Cannot execute instruction at unmapped address {:#08x}", pc); ReturnException(pc, PrefetchAbort); return; + case Dynarmic::A32::Exception::AccessViolation: + if (pc == 0 || pc < 0x1000) { + LOG_CRITICAL(Core_ARM, "Null pointer dereference at {:#08x}", pc); + ReturnException(pc, DataAbort); + return; + } + [[fallthrough]]; default: if (m_debugger_enabled) { ReturnException(pc, InstructionBreakpoint); diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 62437415b..4e9c58718 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -737,12 +737,21 @@ struct Memory::Impl { const u8* const ptr = GetPointerImpl( GetInteger(vaddr), [vaddr]() { + // Add special handling for null pointer reads + if (GetInteger(vaddr) == 0 || GetInteger(vaddr) < 0x1000) { + LOG_ERROR(HW_Memory, "Null pointer Read{} @ 0x{:016X}", sizeof(T) * 8, + GetInteger(vaddr)); + return; + } LOG_ERROR(HW_Memory, "Unmapped Read{} @ 0x{:016X}", sizeof(T) * 8, GetInteger(vaddr)); }, [&]() { HandleRasterizerDownload(GetInteger(vaddr), sizeof(T)); }); if (ptr) { std::memcpy(&result, ptr, sizeof(T)); + } else if (GetInteger(vaddr) == 0) { + // Return 0 for null pointer reads instead of random memory + result = 0; } return result; } @@ -761,6 +770,12 @@ struct Memory::Impl { u8* const ptr = GetPointerImpl( GetInteger(vaddr), [vaddr, data]() { + // Add special handling for null pointer writes + if (GetInteger(vaddr) == 0 || GetInteger(vaddr) < 0x1000) { + LOG_ERROR(HW_Memory, "Null pointer Write{} @ 0x{:016X} = 0x{:016X}", sizeof(T) * 8, + GetInteger(vaddr), static_cast(data)); + return; + } LOG_ERROR(HW_Memory, "Unmapped Write{} @ 0x{:016X} = 0x{:016X}", sizeof(T) * 8, GetInteger(vaddr), static_cast(data)); }, @@ -768,6 +783,7 @@ struct Memory::Impl { if (ptr) { std::memcpy(ptr, &data, sizeof(T)); } + // Silently ignore writes to null pointer } template diff --git a/vcpkg.json b/vcpkg.json index 180232afd..4c10341e9 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,7 +1,7 @@ { "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json", "name": "citron", - "builtin-baseline": "7adc2e4d49e8d0efc07a369079faa6bc3dbb90f3", + "builtin-baseline": "a42af01b72c28a8e1d7b48107b33e4f286a55ef6", "version": "1.0", "dependencies": [ "boost-algorithm",